📄 nwlar1.pc
字号:
lar1->routeCacheHead = entry;}//// FUNCTION NetworkLar1SetTimer()// PURPOSE Set a timer to expire just in case a route reply is not // received in the allotted time.// PARAMETERS eventType - the event that is triggered by the timer// destAddr - the destination node that the timer is// interested in// delay - the delay between now and timer expiration//void NetworkLar1SetTimer(GlomoNode *node, long eventType, NODE_ADDR destAddr, clocktype delay){ Message *newMsg; NODE_ADDR *info; newMsg = GLOMO_MsgAlloc(node, GLOMO_NETWORK_LAYER, ROUTING_PROTOCOL_LAR1, eventType); GLOMO_MsgInfoAlloc(node, newMsg, sizeof(NODE_ADDR)); info = (NODE_ADDR *) GLOMO_MsgReturnInfo(newMsg); *info = destAddr; GLOMO_MsgSend(node, newMsg, delay);}//// FUNCTION NetworkLar1RouterFunction()// PURPOSE Determine the routing action to take for a the given data// packet, and set the PacketWasRouted variable to TRUE if no// further handling of this packet by IP is necessary.// PARAMETERS msg - the data packet// destAddr - the destination node of this data packet// PacketWasRouted - variable that indicates to IP that it// no longer needs to act on this data packet//void NetworkLar1RouterFunction( GlomoNode *node, Message *msg, NODE_ADDR destAddr, BOOL* PacketWasRouted){ GlomoNetworkLar1 *lar1; GlomoNetworkIp* ipLayer; IpHeaderType *ipHeader = (IpHeaderType *) GLOMO_MsgReturnPacket(msg); ipLayer = (GlomoNetworkIp *) node->networkData.networkVar; lar1 = (GlomoNetworkLar1 *) ipLayer->routingProtocol; if (destAddr == node->nodeAddr) { *PacketWasRouted = FALSE; return; }#ifdef DEBUG printf("#%d: NetworkLar1HandleDatapacket()\n",node->nodeAddr);#endif if (ipHeader->ip_src != node->nodeAddr) {#ifdef DEBUG printf(" Source Routing this packet.\n");#endif lar1->DataPacketsRelayed++; *PacketWasRouted = FALSE; return; } /* Check if Route In Cache */ if (NetworkLar1RouteExists(lar1->routeCacheHead, destAddr)) { *PacketWasRouted = TRUE;#ifdef DEBUG printf(" Route In Cache.\n");#endif NetworkLar1TransmitData(node, msg); } else /* No Route In Cache */ { *PacketWasRouted = TRUE; #ifdef DEBUG printf(" No Route in Cache to #%d.\n", destAddr);#endif /* Check if there is already a pending route request for this dest */ if (NetworkLar1PendingRouteReq(node, destAddr)) {#ifdef DEBUG printf(" Already pending request. Buffer pkt.\n");#endif if (NetworkLar1BufferPacket(node, msg, destAddr)) {#ifdef DEBUG printf(" Buffered size %d.\n", GLOMO_MsgReturnPacketSize(msg));#endif } else {#ifdef DEBUG printf(" Not enough space in buffer. ", "Dropped.\n");#endif GLOMO_MsgFree(node, msg); } } else {#ifdef DEBUG printf(" Initiate route request and buffer pkt.\n");#endif if (NetworkLar1BufferPacket(node, msg, destAddr)) {#ifdef DEBUG printf(" Buffered. Initiate Route Request.\n"); printf(" size %d\n", GLOMO_MsgReturnPacketSize(msg));#endif NetworkLar1InsertRequestSeen(lar1, node->nodeAddr, lar1->seqNum); NetworkLar1InitiateRouteRequest(node, destAddr); } else {#ifdef DEBUG printf(" Not enough space in buffer. ", "Dropped.\n");#endif GLOMO_MsgFree(node, msg); } } }}//// FUNCTION NetworkLar1FreeCacheEntry()// PURPOSE Free the memory used by a route rache entry// PARAMETERS cacheEntry - the entry to free//void NetworkLar1FreeCacheEntry(LAR1_RouteCacheEntry *cacheEntry){ pc_free(cacheEntry->path); pc_free(cacheEntry);}//// FUNCTION NetworkLar1InvalidateRoutesThroughBrokenLink()// PURPOSE Mark as unusable routes in the cache which contain the// given node pair// PARAMETERS fromHop - the first node in the node pair// toHop - the receiving node of the node pair//void NetworkLar1InvalidateRoutesThroughBrokenLink( GlomoNode *node, NODE_ADDR fromHop, NODE_ADDR toHop){ GlomoNetworkLar1 *lar1; GlomoNetworkIp* ipLayer; LAR1_RouteCacheEntry *cacheEntry; NODE_ADDR *path; int pathCount; ipLayer = (GlomoNetworkIp *) node->networkData.networkVar; lar1 = (GlomoNetworkLar1 *) ipLayer->routingProtocol; cacheEntry = lar1->routeCacheHead; while (cacheEntry != NULL) { path = cacheEntry->path; if ((node->nodeAddr == fromHop) && (path[0] == toHop)) cacheEntry->valid = FALSE; else { for (pathCount = 0; pathCount < cacheEntry->pathLength; pathCount++) { if (path[pathCount] == fromHop) { if (pathCount < (cacheEntry->pathLength - 1)) if (path[pathCount+1] == toHop) { cacheEntry->valid = FALSE; pathCount = cacheEntry->pathLength; } } } } cacheEntry = cacheEntry->next; }}//// FUNCTION NetworkLar1DeleteRoute()// PURPOSE Remove route to the given destination from the route cache// PARAMETERS destAddr - the given destination//void NetworkLar1DeleteRoute(GlomoNode *node, NODE_ADDR destAddr){ GlomoNetworkLar1 *lar1; GlomoNetworkIp* ipLayer; LAR1_RouteCacheEntry *cacheEntry, *tempCacheEntry; BOOL firstEntry = TRUE; ipLayer = (GlomoNetworkIp *) node->networkData.networkVar; lar1 = (GlomoNetworkLar1 *) ipLayer->routingProtocol; cacheEntry = lar1->routeCacheHead;#ifdef DEBUG printf(" NetworkLar1DeleteRoute(%d)\n", destAddr);#endif if (cacheEntry == NULL) return; while ((cacheEntry != NULL) && (firstEntry == TRUE)) { if (cacheEntry->destAddr == destAddr) { lar1->routeCacheHead = cacheEntry->next; NetworkLar1FreeCacheEntry(cacheEntry); cacheEntry = lar1->routeCacheHead; } else { firstEntry = FALSE; tempCacheEntry = cacheEntry; if (tempCacheEntry != NULL) cacheEntry = cacheEntry->next; else cacheEntry = NULL; } } while (cacheEntry != NULL) { if (cacheEntry->destAddr == destAddr) { tempCacheEntry->next = cacheEntry->next; NetworkLar1FreeCacheEntry(cacheEntry); cacheEntry = tempCacheEntry->next; } else { tempCacheEntry = cacheEntry; cacheEntry = cacheEntry->next; } }}//// FUNCTION NetworkLar1TransmitErrorPacket()// PURPOSE Given a packet which MAC 802.11 was unable to transmit to // the neighbor node listed in the source route, this function// will transmit to the data packet's source node, a LAR// Route Error indicating that the route is broken at this link.// PARAMETERS oldMsg - the data packet// nextHop - the nextHop to which the data packet should// have gone//void NetworkLar1TransmitErrorPacket(GlomoNode *node, const Message *oldMsg, NODE_ADDR nextHop){ Message *newMsg; LAR1_RouteError *error; NODE_ADDR *n_addr, *old_n_addr; NODE_ADDR prevHop; char *pktptr; int i; int numHops; int pktSize = sizeof(LAR1_RouteError); IpHeaderType *ipHeader = (IpHeaderType *)GLOMO_MsgReturnPacket(oldMsg); IpOptionsHeaderType* ipOptions = (IpOptionsHeaderType*) (GLOMO_MsgReturnPacket(oldMsg) + sizeof(IpHeaderType)); int ptr = 4; char* routeRawAddressBytes = (char*)ipOptions + ptr - 1; NODE_ADDR nHop = ANY_DEST; GlomoNetworkLar1 *lar1; GlomoNetworkIp* ipLayer; ipLayer = (GlomoNetworkIp *) node->networkData.networkVar; lar1 = (GlomoNetworkLar1 *) ipLayer->routingProtocol;/* Determine number of hops back to source node */ numHops = 0;#ifdef DEBUG printf(" Determine path from source to here\n");#endif while ((ptr < ipOptions->len) && (nHop != node->nodeAddr)) { memcpy(&nHop, routeRawAddressBytes, sizeof(NODE_ADDR)); ptr += 4; routeRawAddressBytes += 4; numHops++; } assert(nHop == node->nodeAddr); pktSize += (sizeof(NODE_ADDR) * (numHops+1)); newMsg = GLOMO_MsgAlloc(node, GLOMO_MAC_LAYER, 0, MSG_MAC_FromNetwork); GLOMO_MsgPacketAlloc(node, newMsg, pktSize); pktptr = (char *) GLOMO_MsgReturnPacket(newMsg); error = (LAR1_RouteError *) pktptr; /* Position the n_addr pointer onto the array of node addresses */ pktptr += sizeof(LAR1_RouteError); n_addr = (NODE_ADDR *) pktptr; error->packetType = LAR1_ROUTE_ERROR; error->sourceAddr = ipHeader->ip_dst; error->destAddr = ipHeader->ip_src; error->fromHop = node->nodeAddr; error->nextHop = nextHop; error->segmentLeft = numHops-1; routeRawAddressBytes = (char*)ipOptions + 4 - 1; memcpy(&(n_addr[1]), routeRawAddressBytes, (sizeof(NODE_ADDR) * (numHops-1))); nHop = ipHeader->ip_src; memcpy(&(n_addr[0]), &nHop, sizeof(NODE_ADDR)); #ifdef DEBUG for (i=0; i<numHops; i++) printf(" --- hop %d = %d\n", i+1, n_addr[i]);#endif prevHop = n_addr[error->segmentLeft];#ifdef DEBUG printf(" Send Route Error packet To Next Hop %d\n", prevHop);#endif lar1->RouteErrorsSentAsErrorSource++; NetworkIpSendRawGlomoMessageToMacLayer( node, newMsg, prevHop, CONTROL, IPPROTO_LAR1, LAR1_MAX_ROUTE_LENGTH, DEFAULT_INTERFACE, prevHop);}//// FUNCTION NetworkLar1HandleBrokenLink()// PURPOSE Handle message from MAC 802.11 regarding broken link// PARAMETERS msg - the packet returned by MAC 802.11//void NetworkLar1HandleBrokenLink(GlomoNode *node, const Message *msg){ IpHeaderType* ipHeader = (IpHeaderType *) GLOMO_MsgReturnPacket(msg); IpOptionsHeaderType* ipOptions = (IpOptionsHeaderType*) ((char *)GLOMO_MsgReturnPacket(msg) + sizeof(IpHeaderType)); NODE_ADDR nextHop, destAddr; GlomoNetworkLar1 *lar1; GlomoNetworkIp* ipLayer; ipLayer = (GlomoNetworkIp *) node->networkData.networkVar; lar1 = (GlomoNetworkLar1 *) ipLayer->routingProtocol; /* Return if the IP packet is not a data packet */ if (ipHeader->ip_p == IPPROTO_LAR1) return; if (ipOptions->ptr < (ipOptions->len+4) ) { // Extract Next Address // The definition of "ptr" seems to number 1 as the // first byte of the the options field. char* routeRawAddressBytes = (char*)ipOptions + ipOptions->ptr - 1; routeRawAddressBytes -= 4; memcpy(&nextHop, routeRawAddressBytes, sizeof(NODE_ADDR)); } else assert(FALSE); ipHeader = (IpHeaderType *) GLOMO_MsgReturnPacket(msg); destAddr = ipHeader->ip_dst;#ifdef DEBUG printf(" HandleBrokenLink (%d - %d)\n",node->nodeAddr, nextHop);#endif /* If Node is the source of broken route */ if (node->nodeAddr == ipHeader->ip_src) { Message *bufMsg = GLOMO_MsgCopy(node, msg);#ifdef DEBUG printf(" Node is source of dropped packet.\n"); printf(" Initiate route request and buffer pkt.\n");#endif if (NetworkLar1BufferPacket(node, bufMsg, destAddr)) { /* Check if there is already a pending route request for this dest */ if ((!NetworkLar1PendingRouteReq(node, destAddr)) || (NetworkLar1RouteExists(lar1->routeCacheHead, destAddr))) {#ifdef DEBUG printf(" Buffered. Initiate Route Request.\n");#endif NetworkLar1InsertRequestSeen(lar1, node->nodeAddr, lar1->seqNum); NetworkLar1InitiateRouteRequest(node, destAddr); }#ifdef DEBUG else { printf(" Buffered. Pending Route Request.\n"); }#endif } else {#ifdef DEBUG printf(" Not enough space in buffer. ", "Dropped.\n");#endif GLOMO_MsgFree(node, bufMsg); } /* Delete Routes that Use the Broken Link */ NetworkLar1InvalidateRoutesThroughBrokenLink(node, node->nodeAddr, nextHop); } else {#ifdef DEBUG printf(" Node is intermediate. Build error packet.\n");#endif /* Build Error Packet */ NetworkLar1TransmitErrorPacket(node, msg, nextHop); }}//// FUNCTION NetworkLar1PacketDropNotificationHandler()// PURPOSE Handle internal messages from MAC Layer// PARAMETERS msg - the packet returned by MAC//void NetworkLar1PacketDropNotificationHandler( GlomoNode *node, const Message* msg, const NODE_ADDR lastHopAddress){#ifdef DEBUG printf("#%d: NetworkLar1MacLayerStatusHandler()\n", node->nodeAddr);#endif#ifdef DEBUG printf(" PacketDropped.\n");#endif NetworkLar1HandleBrokenLink(node, msg);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -