📄 nwlar1.pc
字号:
/* Transmit the data packet */ NetworkLar1TransmitData(node, bufMsg); bufMsg = NetworkLar1RetrieveSendBuf(lar1, pkt->sourceAddr); } GLOMO_MsgFree(node, msg); } else if (path[left] == node->nodeAddr) { /* This node is an intermediate node for the route reply packet */ NODE_ADDR nextHop = path[pkt->segmentLeft-1]; #ifdef DEBUG printf(" I'm the intended intermediate node.\n"); printf(" Propagate Route Reply for %d to %d\n", pkt->destAddr, nextHop);#endif lar1->RouteRepliesRelayed++; /* Propagate this control message towards its destination */ NetworkIpSendRawGlomoMessageToMacLayer( node, msg, nextHop, CONTROL, IPPROTO_LAR1, LAR1_MAX_ROUTE_LENGTH, DEFAULT_INTERFACE, nextHop); } else { /* This packet should not have reached this node, because it should have been unicasted to the next node in the path towards the data source. */ GLOMO_MsgFree(node, msg); assert(FALSE); }}//// FUNCTION NetworkLar1TransmitData()// PURPOSE Retrieve route from route cache, transmit data packet// PARAMETERS outMsg - the packet to be sent//void NetworkLar1TransmitData(GlomoNode *node, Message *outMsg){ int numRoutes; NODE_ADDR *routes; struct ip *ipHdr = (struct ip *)GLOMO_MsgReturnPacket(outMsg); NODE_ADDR destAddr = ipHdr->ip_dst; LAR1_RouteCacheEntry *cache_entry; GlomoNetworkLar1 *lar1; GlomoNetworkIp* ipLayer; ipLayer = (GlomoNetworkIp *) node->networkData.networkVar; lar1 = (GlomoNetworkLar1 *) ipLayer->routingProtocol; /* Retrieve the cache entry for the desired destination */ cache_entry = NetworkLar1RetrieveCacheEntry(lar1->routeCacheHead, destAddr); assert(cache_entry); /* Extract route information from the cache entry */ routes = cache_entry->path; numRoutes = cache_entry->pathLength; /* Use route information to send source routed IP datagram */ NetworkIpSendPacketToMacLayerWithNewStrictSourceRoute( node, outMsg, routes, numRoutes, FALSE); lar1->DataPacketsSentAsSource++;}//// FUNCTION NetworkLar1RetrieveSendBuf()// PURPOSE Retrieve next data packet for transmission for specified// destination// PARAMETERS destAddr - destination node//Message *NetworkLar1RetrieveSendBuf(GlomoNetworkLar1 *lar1, NODE_ADDR destAddr){ LAR1_SendBufferEntry *entry; int bufsize; int index; int i; bufsize = (lar1->sendBufTail + LAR1_SEND_BUFFER_SIZE) - lar1->sendBufHead; bufsize = bufsize % LAR1_SEND_BUFFER_SIZE; if (bufsize == 0) return NULL; index = lar1->sendBufHead; /* Update Head Pointer */ entry = lar1->sendBuf[index]; while ((entry == NULL) && (lar1->sendBufHead != lar1->sendBufTail) && (bufsize > 0)) {#ifdef DEBUG printf(" Advance Head Pointer over useless entry.\n");#endif lar1->sendBufHead = (lar1->sendBufHead + 1) % LAR1_SEND_BUFFER_SIZE; bufsize -= 1; index = lar1->sendBufHead; entry = lar1->sendBuf[index]; } /* Return and remove first useful entry, if any */ for (i = 0; i < bufsize; i++) { index = (lar1->sendBufHead + i) % LAR1_SEND_BUFFER_SIZE; entry = lar1->sendBuf[index]; if (entry != NULL) { if (entry->destAddr == destAddr) { Message *outMsg = entry->msg; assert(outMsg); pc_free(entry); lar1->sendBuf[index] = NULL; return outMsg; } } } return NULL;}//// FUNCTION NetworkLar1HandleRouteRequest()// PURPOSE Determine course of action for LAR RREQ packet// PARAMETERS msg - the packet//void NetworkLar1HandleRouteRequest(GlomoNode *node, Message *msg){ LAR1_RouteRequest *pkt = (LAR1_RouteRequest *) GLOMO_MsgReturnPacket(msg); char *path; GlomoNetworkLar1 *lar1; GlomoNetworkIp* ipLayer; ipLayer = (GlomoNetworkIp *) node->networkData.networkVar; lar1 = (GlomoNetworkLar1 *) ipLayer->routingProtocol; /* Increase the hop count */ pkt->currentHop = pkt->currentHop + 1; /* Process packet only if the node is within the request zone */ /* or the packet is purely flooded */ if (!(pkt->flooding) && !(NetworkLar1NodeInZone(node, &pkt->requestZone))) {#ifdef DEBUG printf(" Not in Zone, Not Flooding Pkt. Discard.\n");#endif return; } if (pkt->currentHop <= LAR1_MAX_ROUTE_LENGTH) { /* if not seen before */ NetworkLar1FlushRequestSeenCache(lar1); if (!NetworkLar1LookupRequestSeen(lar1->reqSeenHead, pkt->sourceAddr, pkt->seqNum)) {#ifdef DEBUG printf(" First Time Seeing This Request.\n");#endif NetworkLar1InsertRequestSeen(lar1, pkt->sourceAddr, pkt->seqNum); path = GLOMO_MsgReturnPacket(msg) + sizeof(LAR1_RouteRequest); if (!NetworkLar1NodeInReqPath(node, (NODE_ADDR *) path, pkt->currentHop)) { if (pkt->destAddr == node->nodeAddr) {#ifdef DEBUG printf(" I am the destination node.\n");#endif NetworkLar1InitiateRouteReply(node, msg); } else { /* relay the packet */#ifdef DEBUG printf(" Relay the packet.\n");#endif NetworkLar1PropagateRouteRequest(node, msg); } } else {#ifdef DEBUG printf(" This node already in traversed path.\n");#endif return; } } else {#ifdef DEBUG printf(" Request already seen. Discard.\n");#endif return; } } else {#ifdef DEBUG printf(" Over Hop Limit. Discard.\n");#endif return; }}//// FUNCTION NetworkLar1InitiateRouteReply()// PURPOSE Create and transmit LAR Route Reply packet// PARAMETERS oldMsg - the original LAR Route Request packet//void NetworkLar1InitiateRouteReply(GlomoNode *node, Message *oldMsg){ Message *newMsg; LAR1_RouteRequest *pkt; LAR1_RouteReply *reply; NODE_ADDR *n_addr, *old_n_addr; NODE_ADDR nextHop; char *pktptr; int i; int pktSize = sizeof(LAR1_RouteReply); GlomoNetworkLar1 *lar1; GlomoNetworkIp* ipLayer; ipLayer = (GlomoNetworkIp *) node->networkData.networkVar; lar1 = (GlomoNetworkLar1 *) ipLayer->routingProtocol; pkt = (LAR1_RouteRequest *) GLOMO_MsgReturnPacket(oldMsg); pktSize += (sizeof(NODE_ADDR) * (pkt->currentHop + 1)); newMsg = GLOMO_MsgAlloc(node, GLOMO_MAC_LAYER, 0, MSG_MAC_FromNetwork); GLOMO_MsgPacketAlloc(node, newMsg, pktSize); pktptr = (char *) GLOMO_MsgReturnPacket(newMsg); reply = (LAR1_RouteReply *) pktptr; /* Position the n_addr pointer onto the array of node addresses */ pktptr += sizeof(LAR1_RouteReply); n_addr = (NODE_ADDR *) pktptr; /* Position the old_n_addr pointer onto the array of node addresses */ pktptr = (char *) GLOMO_MsgReturnPacket(oldMsg) + sizeof(LAR1_RouteRequest); old_n_addr = (NODE_ADDR *) pktptr; reply->packetType = LAR1_ROUTE_REPLY; reply->sourceAddr = pkt->destAddr; reply->destAddr = pkt->sourceAddr; reply->segmentLeft = pkt->currentHop; reply->destLocation.x = GLOMO_MobilityReturnPositionX(node); reply->destLocation.y = GLOMO_MobilityReturnPositionY(node); reply->locationTimestamp = simclock(); reply->destVelocity = GLOMO_MobilityReturnAvgSpd(node); memcpy(n_addr, old_n_addr, (sizeof(NODE_ADDR) * pkt->currentHop)); n_addr[pkt->currentHop] = node->nodeAddr;#ifdef DEBUG printf(" Transmit Route Reply for %d to %d, i = %d\n", reply->destAddr, n_addr[pkt->currentHop-1], pkt->currentHop);#endif nextHop = n_addr[pkt->currentHop-1]; lar1->RouteRepliesSentAsRecvr++; NetworkIpSendRawGlomoMessageToMacLayer( node, newMsg, nextHop, CONTROL, IPPROTO_LAR1, LAR1_MAX_ROUTE_LENGTH, DEFAULT_INTERFACE, nextHop);}//// FUNCTION NetworkLar1NodeInReqPath()// PURPOSE Return TRUE if node address appears in path array// PARAMETERS path - array of NODE_ADDR// hopcount- number of entries in path array//BOOL NetworkLar1NodeInReqPath(GlomoNode *node, NODE_ADDR *path, int hopcount){ int i; for (i = 0; i < hopcount; i++) {#ifdef DEBUG printf(" path[%d] = %d\n", i, path[i]);#endif if (path[i] == node->nodeAddr) return TRUE; } return FALSE;}//// FUNCTION NetworkLar1FlushRequestSeenCache()// PURPOSE Remove Request Seen Cache entries older than// LAR1_REQUEST_SEEN_LIFETIME// PARAMETERS lar1 - LAR1 variable space//void NetworkLar1FlushRequestSeenCache(GlomoNetworkLar1 *lar1){ LAR1_RequestSeenEntry *head = lar1->reqSeenHead; LAR1_RequestSeenEntry *parent; BOOL firstEntry = TRUE; while ((head != NULL) && firstEntry) { if (head->lifetime < simclock()) { lar1->reqSeenHead = head->next; pc_free(head); head = lar1->reqSeenHead; } else { parent = head; head = head->next; firstEntry = FALSE; } } while (head != NULL) { if (head->lifetime < simclock()) { parent->next = head->next; pc_free(head); head = parent->next; } else { parent = head; head = head->next; } }}//// FUNCTION NetworkLar1InsertRequestSeen()// PURPOSE Insert Request source address and sequence num into cache// PARAMETERS sourceAddr - source of LAR Request Packet// seqNum - sequence number assigned by source//void NetworkLar1InsertRequestSeen(GlomoNetworkLar1 *lar1, NODE_ADDR sourceAddr, int seqNum){ LAR1_RequestSeenEntry *entry = pc_malloc(sizeof(LAR1_RequestSeenEntry)); assert(entry); entry->sourceAddr = sourceAddr; entry->seqNum = seqNum; entry->lifetime = simclock() + LAR1_REQUEST_SEEN_LIFETIME; entry->next = lar1->reqSeenHead; lar1->reqSeenHead = entry;}//// FUNCTION NetworkLar1InsertRequestSent()// PURPOSE Insert destination address for locally generated // LAR Request Packet into cache// PARAMETERS destAddr - destination address//void NetworkLar1InsertRequestSent(GlomoNetworkLar1 *lar1, NODE_ADDR destAddr){ LAR1_RequestSentEntry *entry = checked_pc_malloc(sizeof(LAR1_RequestSentEntry)); entry->destAddr = destAddr; entry->next = lar1->reqSentHead; lar1->reqSentHead = entry;}//// FUNCTION NetworkLar1RemoveRequestSent()// PURPOSE Remove destination address for locally generated// LAR Request Packet from cache (Reply received)// PARAMETERS destAddr - destination address//void NetworkLar1RemoveRequestSent(GlomoNetworkLar1 *lar1, NODE_ADDR destAddr){ LAR1_RequestSentEntry *entry = lar1->reqSentHead; LAR1_RequestSentEntry *parent; BOOL firstEntry = TRUE; while ((entry != NULL) && firstEntry) { if (entry->destAddr == destAddr) { lar1->reqSentHead = entry->next; pc_free(entry); entry = lar1->reqSentHead; } else { firstEntry = FALSE; parent = entry; entry = entry->next; } } while (entry != NULL) { if (entry->destAddr == destAddr) { parent->next = entry->next; pc_free(entry); entry = parent->next; } else { parent = entry; entry = entry->next; } }}//// FUNCTION NetworkLar1LookupRequestSeen()// PURPOSE Return TRUE if the (source addr, seq num) appears in cache// PARAMETERS sourceAddr - source of LAR Request Packet// seqNum - sequence number of LAR Request Packet//BOOL NetworkLar1LookupRequestSeen(LAR1_RequestSeenEntry *reqSeen, NODE_ADDR sourceAddr, int seqNum){ while (reqSeen != NULL) { if ((reqSeen->sourceAddr == sourceAddr) && (reqSeen->seqNum == seqNum)) return TRUE; reqSeen = reqSeen->next; } return FALSE;}//// FUNCTION NetworkLar1PropagateRouteRequest()// PURPOSE Propagate a received LAR Route Request Packet// PARAMETERS oldMsg - the received LAR Route Request Packet//void NetworkLar1PropagateRouteRequest(GlomoNode *node, Message *oldMsg){
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -