/**
* A packet from a neighbour has been detected.
*/
void
WATCHDOG::neighboursPacketDetected(int32_t source_ip, int32_t destination_ip, int source_mac, int destination_mac, int sourcePort, int destinationPort, char * tmp_data, packet_t packet_type, double pktime) {
if (debug > 2) printf("NeighbourPacket\n");
neighbour *neigh_src, *neigh_dst;
int data_size;
data_size = strlen(tmp_data);
protocol = packet_type;
neigh_src = neigs->ExistNeighbourMac(source_mac);
neigh_dst = neigs->ExistNeighbourMac(destination_mac);
if (isInterestingPacket(source_ip, source_mac, destination_ip, destination_mac)) {
/* If the destination IP is different of the destination neighbour, means that must be forwarded */
if (neigh_dst != NULL && (neigh_dst->ip != 666) && (destination_ip != neigh_dst->ip)) {
neigs->ShowAllNeighbours();
waitingForwardPacket(source_ip, destination_ip, sourcePort, destinationPort, tmp_data, data_size, neigh_dst, protocol, pktime);
}else{
if(destination_mac!=ownMAC){
neigs->ShowAllNeighbours();
}
}
/* If the IP source is different of the sender, means that is a forwarded packet */
if ((neigh_src != NULL) && (neigh_src->ip != 666)){
if (debug > 2) printf("Sender: %d Hop: %d Destination: %d\n", source_mac, neigh_src->mac, destination_ip);
if (source_ip != neigh_src->ip) {
packetForwarded(destination_ip, source_ip, tmp_data, data_size, neigh_src, protocol, pktime);
}}
}
}
/**
* A packet has been forwarded, delete it from the waiting forward list.
*/
void
WATCHDOG::packetForwarded(int32_t destination_ip, int32_t source_ip, char *body, int data_size, neighbour *neigh, int protocol, double pktime) {
if (debug > 2) printf("packetForwarded\n");
if (neigh != NULL) {
neigs->AddNeighbourPacketForwarded(neigh, body, destination_ip, source_ip, data_size, storing_packet_timeout, protocol, pktime);
}
}
* Add a packet to a neighbour not forwarded yet.
*/
void
NEIGHBOURS::AddNeighbourPacketForwarded(neighbour *neig, char *body, int32_t ip_dst, int32_t ip_src, int data_size, int storing_time, int protocol, double pktime) {
neig->time = pktime;
neig->pckts->AddPacketForwarded(body, ip_dst, ip_src, data_size, storing_time, protocol, pktime);
}
/**
* Indicates that a new packet forwarded has been listened.
*/
void
PACKETS::AddPacketForwarded( char *body, int32_t ip_dst, int32_t ip_src, int data_size, int storing_time, int protocol, double pktime) {
packet * exists;
exists = ExistsPacket(body, ip_dst, ip_src, data_size, storing_time, protocol, pktime);
if (exists != NULL) {
pckts->forwarded_packets[protocol]++;
RemovePacket(exists, protocol);
}
}
/**
* Delete a stored packet.
*/
void
PACKETS::RemovePacket( packet *pk, int protocol) {
packet *next, *prev;
if (pk != NULL) {
next = pk->next;
prev = pk->previous;
/* is the first packet */
if (pk == pckts->packets[protocol]) {
pckts->packets[protocol] = pk->next;
}
/* is the last packet */
if (pk == pckts->last_packet[protocol]) {
pckts->last_packet[protocol] = pk->previous;
}
if (next != NULL) next->previous = prev;
if (prev != NULL) prev->next = next;
pckts->stored_packets--;
pckts->stored_packets_protocol[protocol] = pckts->stored_packets_protocol[protocol] - (1 / pk->devalue);
DeletePacket(pk);
pk = NULL;
} else {
printf("This packet does not exists!\n");
}
}