📄 dsr.pc
字号:
buffer->size = buffer->size - 1; } else { last = current; current = current->next; }//if// }//while//}/* * RoutingDsrLookupBuffer * * Check if any packet to the destAddr is buffered * return TRUE if found; FALSE otherwise */BOOL RoutingDsrLookupBuffer(NODE_ADDR destAddr, DSR_BUFFER *buffer){ DSR_BUFFER_Node *current; if (buffer->size == 0) { return (FALSE); } for (current = buffer->head; current != NULL && current->destAddr <= destAddr; current = current->next) { if (current->destAddr == destAddr) { return(TRUE); } } return (FALSE);}/* * RoutingDsrUpdateRequestTable * * Update the backoff interval and last request field of an entry */void RoutingDsrUpdateRequestTable(NODE_ADDR destAddr, DSR_RequestTable *requestTable){ DSR_RequestTableEntry *current; for (current = requestTable->head; current != NULL && current->destAddr <= destAddr; current = current->next) { if (current->destAddr == destAddr) { current->lastRequest = simclock(); if (current->backoffInterval < DSR_MAX_REQUEST_PERIOD) { current->backoffInterval *= 2; } current->ttl = DSR_MAX_TTL; } }} /* * RoutingDsrUpdateTtl * * Update the ttl value of an entry */void RoutingDsrUpdateTtl(NODE_ADDR destAddr, DSR_RequestTable *requestTable){ DSR_RequestTableEntry *current; for (current = requestTable->head; current != NULL && current->destAddr <= destAddr; current = current->next) { if (current->destAddr == destAddr) { current->ttl = 1; } }}/* * RoutingDsrCheckRequestTable * * Check if the destAddr entry is recorded in the request table */BOOL RoutingDsrCheckRequestTable(NODE_ADDR destAddr, DSR_RequestTable *requestTable){ DSR_RequestTableEntry *current; for (current = requestTable->head; current != NULL && current->destAddr <= destAddr; current = current->next) { if (current->destAddr == destAddr) { return (TRUE); } } return (FALSE);}/* * RoutingDsrGetBackoff * * Get the backoff interval for destAddr */clocktype RoutingDsrGetBackoff( NODE_ADDR destAddr, DSR_RequestTable *requestTable){ DSR_RequestTableEntry *current; for (current = requestTable->head; current != NULL && current->destAddr <= destAddr; current = current->next) { if (current->destAddr == destAddr) { return (current->backoffInterval); } } return (DSR_RING0_REQUEST_TO);}/* * RoutingDsrHandleProtocolPacket * * Called when packet is received from MAC */void RoutingDsrHandleProtocolPacket( GlomoNode *node, Message *msg, NODE_ADDR srcAddr, NODE_ADDR destAddr, int ttl){ DSR_PacketType *dsrHeader = (DSR_PacketType*)GLOMO_MsgReturnPacket(msg); switch (*dsrHeader) { case DSR_ROUTE_REQUEST: { RoutingDsrHandleRequest(node, msg, ttl); break; } /* RREQ */ case DSR_ROUTE_REPLY: { RoutingDsrHandleReply(node, msg, destAddr); break; } /* RREP */ case DSR_ROUTE_ERROR: { RoutingDsrHandleError(node, msg, srcAddr, destAddr); break; } /* RERR */ } /* switch */} /* RoutingDsrHandleProtocolPacket *//* * RoutingDsrHandleProtocolEvent * * Handles all the protocol events */void RoutingDsrHandleProtocolEvent(GlomoNode *node, Message *msg){ GlomoNetworkIp* ipLayer = (GlomoNetworkIp *) node->networkData.networkVar; GlomoRoutingDsr* dsr = (GlomoRoutingDsr *) ipLayer->routingProtocol; switch (msg->eventType) { /* Remove an entry from the request seen table */ case MSG_NETWORK_FlushTables: { RoutingDsrDeleteSeenTable(&dsr->requestSeenTable); GLOMO_MsgFree(node, msg); break; } /* check if a route is obtained after sending a Route Request */ case MSG_NETWORK_CheckReplied: { DSR_CR *cr = (DSR_CR *)GLOMO_MsgReturnInfo(msg); int ttl; if (!RoutingDsrCheckRouteExist( cr->destAddr, &dsr->routeCacheTable)) { if (cr->ttl == 1) { ttl = DSR_MAX_TTL; } else { ttl = 1; } RoutingDsrRetryRREQ(node, cr->destAddr, ttl); } /* if no route */ GLOMO_MsgFree(node, msg); break; } default: fprintf(stderr, "RoutingDsr: Unknown MSG type %d!\n", msg->eventType); assert(FALSE); } /* switch */} /* RoutingDsrHandleProtocolEvent *//* * RoutingDsrRouterFunction * * Determine the routing action to take for a the given data packet * set the PacketWasRouted variable to TRUE if no further handling of * this packet by IP is necessary */void RoutingDsrRouterFunction( GlomoNode *node, Message *msg, NODE_ADDR destAddr, BOOL *packetWasRouted){ GlomoNetworkIp* ipLayer = (GlomoNetworkIp *) node->networkData.networkVar; GlomoRoutingDsr* dsr = (GlomoRoutingDsr *) ipLayer->routingProtocol; IpHeaderType *ipHeader = (IpHeaderType *) msg->packet; DsrIpOptionType* option; NODE_ADDR path[DSR_MAX_SR_LEN + 1]; int length; int current; if (ipHeader->ip_p == IPPROTO_DSR) { return; } if (ipHeader->ip_src == node->nodeAddr) { *packetWasRouted = TRUE; } else { ExtractIpSourceAndRecordedRoute(msg, path, &length, ¤t); assert(length <= (DSR_MAX_SR_LEN + 1)); option = GetPtrToDsrIpOptionField(msg); option->segmentLeft = option->segmentLeft - 1; /* Check if received the packet before */ if (!RoutingDsrCheckDataSeen(node, path, current)) { *packetWasRouted = FALSE; } else { *packetWasRouted = TRUE; } } if (ipHeader->ip_src != node->nodeAddr) { /* check if i'm the dest */ if (destAddr == node->nodeAddr && path[current - 1] == node->nodeAddr) { dsr->stats.numDataReceived++; } /* if dest */ /* I'm the intended intermediate node */ else if (path[current - 1] == node->nodeAddr) { dsr->stats.numDataTxed++; } /* else if i'm the intended receiver */ } /* Source of the route and route to the destination is known */ else if (RoutingDsrCheckRouteExist(destAddr, &dsr->routeCacheTable)) { RoutingDsrTransmitData(node, msg, destAddr); } /* No route to the dest is known and no Route Request has been sent */ else if (!RoutingDsrLookupBuffer(destAddr, &dsr->buffer)) { RoutingDsrInsertBuffer(msg, destAddr, &dsr->buffer); if (RoutingDsrLookupRequestTable(destAddr, &dsr->requestTable)) { RoutingDsrInitiateRREQ(node, destAddr); } } /* Already sent an Route Request; just buffer the packet */ else { RoutingDsrInsertBuffer(msg, destAddr, &dsr->buffer); }} /* RoutingDsrRouterFunction *//* * RoutingDsrPeekFunction * * Handles packets overheard promiscuously */void RoutingDsrPeekFunction(GlomoNode *node, const Message *msg){ GlomoNetworkIp* ipLayer = (GlomoNetworkIp *) node->networkData.networkVar; GlomoRoutingDsr* dsr = (GlomoRoutingDsr *) ipLayer->routingProtocol; IpHeaderType* ipHeader = (IpHeaderType *) GLOMO_MsgReturnPacket(msg); Message *newMsg = GLOMO_MsgCopy(node, msg); DsrIpOptionType *option; /* Control packets */ if (ipHeader->ip_p == IPPROTO_DSR) { DSR_PacketType *dsrHeader; NODE_ADDR sourceAddress; NODE_ADDR destinationAddress; unsigned char IpProtocol; unsigned int ttl; NetworkQueueingPriorityType priority; NetworkIpRemoveIpHeader(node, newMsg, &sourceAddress, &destinationAddress, &priority, &IpProtocol, &ttl); dsrHeader = (DSR_PacketType *) GLOMO_MsgReturnPacket(newMsg); switch (*dsrHeader) { case DSR_ROUTE_REQUEST: { break; } /* RREQ */ case DSR_ROUTE_REPLY: { DSR_RouteReply *rrep; NODE_ADDR newPath[DSR_MAX_SR_LEN]; int i, j; int segLeft; rrep = (DSR_RouteReply *) GLOMO_MsgReturnPacket(newMsg); segLeft = rrep->segLeft - 1; /* If not already processed this packet before */ if (!RoutingDsrCheckUnprocessedPath(node, segLeft - 1, rrep->hopCount, rrep->path)) { /* Insert routes into the cache */ for (i = segLeft; i < rrep->hopCount + segLeft; i++) { for (j = 0; j <= i - segLeft; j++) { newPath[j] = rrep->path[j + segLeft]; } for (j = i + 1 - segLeft; j < DSR_MAX_SR_LEN; j++) { newPath[j] = ANY_DEST; } if (!RoutingDsrCompareRoute( rrep->path[i], i + 1 - segLeft, newPath, &dsr->routeCacheTable) && !RoutingDsrCheckDataSeen( node, newPath, i + 1 - segLeft)) { RoutingDsrInsertRouteCache( rrep->path[i], i + 1 - segLeft, newPath, &dsr->routeCacheTable); } /* if compare route */ } /* for */ } break; } /* RREP */ case DSR_ROUTE_ERROR: { DSR_RouteError *rerr = (DSR_RouteError *) GLOMO_MsgReturnPacket(newMsg); /* Delete routes in cache that use the broken link */ RoutingDsrDeleteRouteCache(node, rerr->srcAddr, rerr->unreachableAddr, &dsr->routeCacheTable); break; } default: fprintf(stderr, "RoutingDsr: Unknown MSG type %d!\n", newMsg->eventType); assert(FALSE); } /* switch */ } /* data packets */ else { NODE_ADDR path[DSR_MAX_SR_LEN + 1]; int length; int current; ExtractIpSourceAndRecordedRoute(newMsg, path, &length, ¤t); assert(length <= (DSR_MAX_SR_LEN + 1)); option = GetPtrToDsrIpOptionField(newMsg); option->segmentLeft = option->segmentLeft - 1; /* check to see if gratuitous route reply can be sent */ if (RoutingDsrCheckUnprocessedPath(node, current - 1, option->segmentLeft, path) && option->salvagedBit == FALSE) { RoutingDsrGratuitousRREP( node, ipHeader->ip_src, ipHeader->ip_dst, path, current - 1, length); } } /* else - data */ GLOMO_MsgFree(node, newMsg);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -