static void flood_test(char *packet, size_t length, int choose_link, int avoid_link) { NL_PACKET *p = (NL_PACKET *) packet; /* REQUIRED LINK IS PROVIDED - USE IT */ if (choose_link != 0) { if (linkinfo[choose_link].mtu < p->min_mtu) p->min_mtu = linkinfo[choose_link].mtu; CHECK(down_to_datalink(choose_link, packet, length)); } /* OTHERWISE, CHOOSE THE BEST KNOWN LINKS, AVOIDING ANY SPECIFIED ONE */ else { int links_wanted = NL_linksofminhops(p->dest); int link; for (link = 1; link <= nodeinfo.nlinks; ++link) { if (link == avoid_link) /* possibly avoid this one */{ continue; } if (links_wanted & (1 << link)) /* use this link if wanted */ { if (linkinfo[link].mtu < p->min_mtu) p->min_mtu = linkinfo[link].mtu; CHECK(down_to_datalink(link, packet, length)); } } } }
// here the second parameter is the length of msg of packet! void sendPacketPiecesToDatalink(char *packet, size_t length, int choose_link) { //printf("send packet pieces to data link at %s\n", nodeinfo.nodename); size_t maxPacketLength = linkinfo[choose_link].mtu - PACKET_HEADER_SIZE; NL_PACKET *tempPacket = (NL_PACKET *) packet; size_t tempLength = length; char *str = tempPacket->msg; //printf("src = %d, des = %d, linkinfo[choose_link].mtu = %d, PACKET_HEADER_SIZE = %d\n", tempPacket->src, tempPacket->dest, linkinfo[choose_link].mtu, PACKET_HEADER_SIZE); NL_PACKET piecePacket; piecePacket.src = tempPacket->src; piecePacket.dest = tempPacket->dest; piecePacket.kind = tempPacket->kind; piecePacket.seqno = tempPacket->seqno; piecePacket.hopcount = tempPacket->hopcount; piecePacket.pieceNumber = tempPacket->pieceNumber; piecePacket.pieceEnd = tempPacket->pieceEnd; piecePacket.src_packet_length = tempPacket->src_packet_length; piecePacket.checksum = tempPacket->checksum; piecePacket.trans_time = tempPacket->trans_time; piecePacket.is_resent = tempPacket->is_resent; while (tempLength > maxPacketLength) { //printf("packet remains %d bytes\n", tempLength); piecePacket.length = maxPacketLength; memcpy(piecePacket.msg, str, maxPacketLength); CHECK(down_to_datalink(choose_link, (char *) &piecePacket, PACKET_SIZE(piecePacket))); //("piece %d down_to_datalink\n", piecePacket.pieceNumber); str = str + maxPacketLength; piecePacket.pieceNumber = piecePacket.pieceNumber + 1; tempLength = tempLength - maxPacketLength; } piecePacket.pieceEnd = 1; piecePacket.length = tempLength; //printf("last piece contains %d bytes, pieceNumber = %d\n\n", tempLength ,piecePacket.pieceNumber); memcpy(piecePacket.msg, str, tempLength); ////("Required link is provided link = %d\n", choose_link); CHECK(down_to_datalink(choose_link, (char *) &piecePacket, PACKET_SIZE(piecePacket))); //("last piece %d down_to_datalink\n", piecePacket.pieceNumber); ////("Provided link = %d sent! \n", choose_link); }
static void selective_flood(char *packet, int length, int links_wanted) { int link; for(link=1 ; link<=nodeinfo.nlinks ; ++link) if( links_wanted & (1<<link) ) CHECK(down_to_datalink(link, packet, length)); }
// here the second parameter is the length of msg of packet! void sendPacketPiecesToDatalink(char *packet, size_t length, int choose_link) { size_t maxPacketLength = linkinfo[choose_link].mtu - PACKET_HEADER_SIZE; NL_PACKET *tempPacket = (NL_PACKET *) packet; size_t tempLength = length; char *str = tempPacket->msg; NL_PACKET piecePacket; piecePacket.src = tempPacket->src; piecePacket.dest = tempPacket->dest; piecePacket.kind = tempPacket->kind; piecePacket.seqno = tempPacket->seqno; //piecePacket.hopcount = tempPacket->hopcount; piecePacket.trans_time = tempPacket->trans_time; piecePacket.traveled_hops_count = tempPacket->traveled_hops_count; memcpy(piecePacket.traveled_hops,tempPacket->traveled_hops, MAXHOPS); piecePacket.pieceEnd = tempPacket->pieceEnd; piecePacket.pieceNumber = tempPacket->pieceNumber; piecePacket.checksum = tempPacket->checksum; while (tempLength > maxPacketLength) { piecePacket.length = maxPacketLength; memcpy(piecePacket.msg, str, maxPacketLength); CHECK(down_to_datalink(choose_link, (char *) &piecePacket, PACKET_SIZE(piecePacket))); str = str + maxPacketLength; piecePacket.pieceNumber = piecePacket.pieceNumber + 1; tempLength = tempLength - maxPacketLength; } piecePacket.pieceEnd = 1; piecePacket.length = tempLength; memcpy(piecePacket.msg, str, tempLength); CHECK(down_to_datalink(choose_link, (char *) &piecePacket, PACKET_SIZE(piecePacket))); }