watchdog读取数据包,维护以下四个包计数器。

    float readed_packets;
float ip_packets;
float arp_packets;
float interesting_readed_packets;

如果是ARP,AODV数据包,则维护邻居。

如果是IP报文,则interesting_readed_packets++。若接收到的是来源于自身的报文,则调用ownPacketDetected,否则维护邻居表,并针对非广播报文调用neighboursPacketDetected

/**
* Read one packet of the network device.
*/
void
WATCHDOG::readPacket(int32_t source_ip, int32_t destination_ip, int source_mac, int destination_mac, int sourcePort, int destinationPort, packet_t packet_type, char * tmp_data, double pktime) {
if (debug > 2) printf("Reading packet\n");
readed_packets++;

if (packet_type == PT_ARP) { /* Is a ARP packet*/
arp_packets++;
if (source_ip == ownIP) { /* is my own ARP packet*/
//ownMAC = source_mac;
} else { /* Is a neighbour ARP packet */
neigs->AssignIpToNeighbour(source_mac, source_ip, isInterestingPacket(source_ip, source_mac, destination_ip, destination_mac), pktime, ownMAC);
/* Show debug data */
if ((debug > 1) && isInterestingPacket(source_ip, source_mac, destination_ip, destination_mac)) {
//PrintArpPacketInformation(n);
//ShowAllNeighbours(&neigs);
}
}
} else if (packet_type == PT_AODV){ /* Is a AODV packet */
if(destination_ip ==-1 && destination_mac == -1 && sourcePort == 255 && destinationPort == 255 && packet_type == 38){ /* Broadcast AODV packets, are sended by the own node.*/
neigs->AssignIpToNeighbour(source_mac, source_ip, 1, pktime, ownMAC);
}
} else { /* Is a IP packet */
ip_packets++;
/* is my packet */
if (source_mac == ownMAC) {
ownPacketDetected(source_ip, destination_ip, sourcePort, destinationPort, destination_mac, tmp_data, packet_type, pktime);
interesting_readed_packets++;
} else {
if (isInterestingPacket(source_ip, source_mac, destination_ip, destination_mac)) {
interesting_readed_packets++;
neigs->AssignIpToNeighbour(source_mac, source_mac, isInterestingPacket(source_ip, source_mac, destination_ip, destination_mac), pktime, ownMAC); //This is because ns2 does not work well and the watchdog may be can not listen the AODV or ARP packets to obtain the IP of a node. But, we it is equal to the MAC!!!!
neighboursPacketDetected(source_ip, destination_ip, source_mac, destination_mac, sourcePort, destinationPort, tmp_data, packet_type, pktime);
} else if(source_ip!=-1 && source_mac!=-1 && (destination_mac==-1)) {
/* Broadcast message*/
neigs->AssignIpToNeighbour(source_mac, source_ip, 1, pktime, ownMAC);
}
}
}
}
/**
* Determines if a packet is interesting for a net.
*/
int
WATCHDOG::isInterestingPacket(int32_t source_ip, int source_mac, int32_t destination_ip, int destination_mac) {
/* No broadcast */
if(destination_ip!=-1 && source_mac!=-1 && destination_mac!=-1){
return 1;
}

return 0;
}




 posted on 2012-03-08 13:51  Fuzzy Joke  阅读(379)  评论(0编辑  收藏  举报