节点发送数据包,则将数据包缓存。

/**
* I read my own packet.
*/
void
WATCHDOG::ownPacketDetected(int32_t source_ip, int32_t destination_ip, int sourcePort, int destinationPort, int mac_dst, char * tmp_data, packet_t packet_type, double pktime) {
if (debug > 2) printf("Ownpacket\n");
int data_size;
neighbour *neigh_dst;

data_size = strlen(tmp_data);
protocol = packet_type;

neigh_dst = neigs->ExistNeighbourMac(mac_dst);

if (mac_dst != 666) {
/* If the destination IP is different of the destination neighbour, means that must be forwarded */
//if (neigh_dst != NULL){
if (neigh_dst != NULL && (neigh_dst->ip != 666) && (destination_ip != neigh_dst->ip)) {
waitingForwardPacket(source_ip, destination_ip, sourcePort, destinationPort, tmp_data, data_size, neigh_dst, protocol, pktime);
neigs->ShowAllNeighbours();
}else{
neigs->ShowAllNeighbours();
}
}

}

自己发送的数据包,则调用waitingForwardPacket

/**
* A new packet has been sended, i must store it until confirmation of forward.
*/
void
WATCHDOG::waitingForwardPacket(int32_t source_ip, int32_t destination_ip, int sourcePort, int destinationPort, char *body, int data_size, neighbour *neigh, int protocol, double pktime) {
if (debug > 2) printf("waitingForwardPacket\n");
if (neigh != NULL) {
neigs->AddNeighbourPacketToForward(neigh, body, destination_ip, source_ip, data_size, storing_packet_timeout, protocol, sourcePort, destinationPort, pktime);
neigs->AddFlow(source_ip, destination_ip, neigh, pktime);
}
}
/**
* Add a packet to a neighbour not forwarded yet.0,918367347
*/
void
NEIGHBOURS::AddNeighbourPacketToForward(neighbour *neig, char *body, int32_t ip_dst, int32_t ip_src, int data_size, int storing_time, int protocol, int originPort, int destinationPort, double pktime) {
neig->time = pktime;
neig->pckts->AddPacketToForward(body, ip_dst, ip_src, data_size, storing_time, protocol, originPort, destinationPort, pktime);
}
/**
* Indicates that a new packet that must to be forwarded has been listened.
*/
void
PACKETS::AddPacketToForward(char *body, int32_t ip_dst, int32_t ip_src, int data_size, int storing_time, int protocol, int originPort, int destinationPort, double pktime) {
packet *pk;
packet *exists;

/* Sometimes can listen a packet two or more times, we must avoid insert the same packet two times */
exists = ShortExistsPacket(body, ip_dst, ip_src, data_size, storing_time, protocol, pktime);
if (exists == NULL) {
if ((pk = (packet *) (malloc(sizeof (packet)))) < 0)
error("Memory assignment of packet!\n");
pk->time = pktime;
pk->data = (char *)malloc(sizeof (char) * data_size);
//memcpy(pk->data, body, data_size);
strcpy(pk->data, body);
pk->ip_src = ip_src;
pk->ip_dst = ip_dst;
pk->data_size = data_size;
pk->time = pktime;
pk->originPort = originPort;
pk->destinationPort = destinationPort;
pk->protocol = protocol;
pk->previous = NULL;
pk->next = NULL;
pk->devalue = 1;

pckts->received_packets[protocol]++;
pckts->stored_packets++;
pckts->stored_packets_protocol[protocol]++;

if (pckts->stored_packets > max_packets_lost) max_packets_lost = pckts->stored_packets;

if (pckts->last_packet[protocol] == NULL) {
pckts->packets[protocol] = pk;
pckts->last_packet[protocol] = pk;
pk->previous = NULL;
} else {
pckts->last_packet[protocol]->next = pk;
pk->previous = pckts->last_packet[protocol];
pckts->last_packet[protocol] = pk;
}

} else {
}
}

 

/**
* Add a new flow definded by a IP to a neighbour.
*/
void
NEIGHBOURS::AddFlow(int32_t source, int32_t destination, neighbour *nb, double pktime) {
flow *f, *ant, *n;
f = nb->flows;

ant = NULL;
if (!ExistFlow(source, destination, nb, pktime)) {
while (f != NULL) {
ant = f;
f = f->next;
}
n = NewFlow(source, destination, pktime);
n->previous = ant;
if (ant != NULL) {
ant->next = n;
} else {
nb->flows = n;
}
nb->number_flows++;
}
}






 posted on 2012-03-08 14:11  Fuzzy Joke  阅读(309)  评论(0编辑  收藏  举报