📄 nwlar1.pc
字号:
/* * GloMoSim is COPYRIGHTED software. Release 2.02 of GloMoSim is available * at no cost to educational users only. * * Commercial use of this software requires a separate license. No cost, * evaluation licenses are available for such purposes; please contact * info@scalable-networks.com * * By obtaining copies of this and any other files that comprise GloMoSim2.02, * you, the Licensee, agree to abide by the following conditions and * understandings with respect to the copyrighted software: * * 1.Permission to use, copy, and modify this software and its documentation * for education and non-commercial research purposes only is hereby granted * to Licensee, provided that the copyright notice, the original author's * names and unit identification, and this permission notice appear on all * such copies, and that no charge be made for such copies. Any entity * desiring permission to use this software for any commercial or * non-educational research purposes should contact: * * Professor Rajive Bagrodia * University of California, Los Angeles * Department of Computer Science * Box 951596 * 3532 Boelter Hall * Los Angeles, CA 90095-1596 * rajive@cs.ucla.edu * * 2.NO REPRESENTATIONS ARE MADE ABOUT THE SUITABILITY OF THE SOFTWARE FOR ANY * PURPOSE. IT IS PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY. * * 3.Neither the software developers, the Parallel Computing Lab, UCLA, or any * affiliate of the UC system shall be liable for any damages suffered by * Licensee from the use of this software. */// Use the latest version of Parsec if this line causes a compiler error.#include <stdlib.h>#include <stdio.h>#include <string.h>#include <assert.h>#include <math.h>#include "api.h"#include "structmsg.h"#include "fileio.h"#include "message.h"#include "network.h"#include "ip.h"#include "nwip.h"#include "nwlar1.h"#include "nwcommon.h"#include "application.h"#include "transport.h"#include "java_gui.h"/* * Notes: * Implementation followed the specification of Mobicom 98 paper by * Ko and Vaidya. Other details followed based on discussions with * Mr. Youngbae Ko of Texas A & M. * * We assume that underlying MAC protocol sends a signal when the packet * cannot be reached to the next hop (after retransmissions). MAC protocols * such as IEEE 802.11 and MACAW have these functionality. * Nodes detect link breaks by receiving a signal from the IEEE 802.11 MAC * Protocol. If other MAC protocol is used, users need to modify the LAR * code so that it can detect link breaks (for instance, using passive acks( * * Send questions to Julian Hsu (gandy@cs.ucla.edu) and * S.J. Lee (sjlee@cs.ucla.edu) *///// FUNCTION NetworkLar1Init()// PURPOSE Initialize LAR1 Routing Protocol Dataspace// PARAMETERS lar1 - pointer for dataspace// nodeInput//void NetworkLar1Init( GlomoNode *node, GlomoNetworkLar1** lar1, const GlomoNodeInput *nodeInput){ int i; char buf[80]; *lar1 = checked_pc_malloc(sizeof(GlomoNetworkLar1)); (*lar1)->routeCacheHead = NULL; (*lar1)->reqSeenHead = NULL; (*lar1)->reqSentHead = NULL; (*lar1)->sendBufHead = (*lar1)->sendBufTail = 0; for (i=0; i<LAR1_SEND_BUFFER_SIZE; i++) { (*lar1)->sendBuf[i] = NULL; } (*lar1)->seqNum = 0; (*lar1)->DataPacketsSentAsSource = 0; (*lar1)->DataPacketsRelayed = 0; (*lar1)->RouteRequestsSentAsSource = 0; (*lar1)->RouteRepliesSentAsRecvr = 0; (*lar1)->RouteErrorsSentAsErrorSource = 0; (*lar1)->RouteRequestsRelayed = 0; (*lar1)->RouteRepliesRelayed = 0; (*lar1)->RouteErrorsRelayed = 0; i = GLOMO_ReadString(node->nodeAddr, nodeInput, "APPLICATION-STATISTICS", buf); if ((i == FALSE) || (strcmp(buf, "NO") == 0)) (*lar1)->statsCollected = FALSE; else if (strcmp(buf, "YES") == 0) (*lar1)->statsCollected = TRUE; NetworkIpSetRouterFunction(node, &NetworkLar1RouterFunction); NetworkIpSetPacketDropNotificationFunction(node, &NetworkLar1PacketDropNotificationHandler); #ifdef DEBUG printf("#%d: NetworkLar1Init()\n", node->nodeAddr);#endif}//// FUNCTION NetworkLar1Finalize()// PURPOSE Finalize statistics Collection//void NetworkLar1Finalize(GlomoNode *node){#ifdef DEBUG printf("#%d: NetworkLar1Finalize()\n", node->nodeAddr);#endif GlomoNetworkIp* ipLayer = (GlomoNetworkIp *) node->networkData.networkVar; GlomoNetworkLar1* lar1 = (GlomoNetworkLar1 *) ipLayer->routingProtocol; char buf[200]; if (lar1->statsCollected) { sprintf(buf, "Data Packets Sent As Data Source: %d", lar1->DataPacketsSentAsSource); GLOMO_PrintStat(node, "RoutingLar1", buf); sprintf(buf, "Data Packets Relayed: %d", lar1->DataPacketsRelayed); GLOMO_PrintStat(node, "RoutingLar1", buf); sprintf(buf, "Route Requests Sent As Data Source: %d", lar1->RouteRequestsSentAsSource); GLOMO_PrintStat(node, "RoutingLar1", buf); sprintf(buf, "Route Replies Sent as Data Receiver: %d", lar1->RouteRepliesSentAsRecvr); GLOMO_PrintStat(node, "RoutingLar1", buf); sprintf(buf, "Route Error Packets Sent As Source of Error: %d", lar1->RouteErrorsSentAsErrorSource); GLOMO_PrintStat(node, "RoutingLar1", buf); sprintf(buf, "Route Requests Relayed as Intermediate Node: %d", lar1->RouteRequestsRelayed); GLOMO_PrintStat(node, "RoutingLar1", buf); sprintf(buf, "Route Replies Relayed as Intermediate Node: %d", lar1->RouteRepliesRelayed); GLOMO_PrintStat(node, "RoutingLar1", buf); sprintf(buf, "Route Error Packets Relayed as Intermediate Node: %d", lar1->RouteErrorsRelayed); GLOMO_PrintStat(node, "RoutingLar1", buf); }}//// FUNCTION NetworkLar1HandleProtocolPacket()// PURPOSE Process a LAR1 generated control packet// PARAMETERS msg - The packet//void NetworkLar1HandleProtocolPacket(GlomoNode* node, Message* msg) { LAR1_PacketType* larHeader = (LAR1_PacketType*)GLOMO_MsgReturnPacket(msg);#ifdef DEBUG printf("#%d: LAR packet.\n",node->nodeAddr);#endif switch (*larHeader) { case LAR1_ROUTE_REQUEST: {#ifdef DEBUG int numEntries; /* Calculate the number of node addresses that are included in this packet by taking the full size of the packet, and subtracting the size of the LAR1 Route Reply Header, and then dividing by the size of a node address */ numEntries = GLOMO_MsgReturnPacketSize(msg); numEntries -= sizeof(LAR1_RouteRequest); numEntries = numEntries / sizeof(NODE_ADDR); printf(" Route Request Packet %d entries.\n", numEntries);#endif NetworkLar1HandleRouteRequest(node, msg); GLOMO_MsgFree(node, msg); break; } case LAR1_ROUTE_REPLY: {#ifdef DEBUG int numEntries; /* Calculate the number of node addresses that are included in this packet by taking the full size of the packet, and subtracting the size of the LAR1 Route Reply Header, and then dividing by the size of a node address */ numEntries = GLOMO_MsgReturnPacketSize(msg); numEntries -= sizeof(LAR1_RouteReply); numEntries = numEntries / sizeof(NODE_ADDR); printf(" Route Reply Packet %d entries.\n", numEntries);#endif NetworkLar1HandleRouteReply(node, msg); break; } case LAR1_ROUTE_ERROR: {#ifdef DEBUG int numEntries; /* Calculate the number of node addresses that are included in this packet by taking the full size of the packet, and subtracting the size of the LAR1 Route Reply Header, and then dividing by the size of a node address */ numEntries = GLOMO_MsgReturnPacketSize(msg); numEntries -= sizeof(LAR1_RouteError); numEntries = numEntries / sizeof(NODE_ADDR); printf(" Route Error Packet %d entries.\n", numEntries);#endif NetworkLar1HandleRouteErrorPacket(node, msg); break; } default: assert(FALSE); }//switch//}//// FUNCTION NetworkLar1HandleCheckTimeoutAlarm()// PURPOSE Process timeouts sent by LAR1 to itself// PARAMETERS msg - the timer//void NetworkLar1HandleCheckTimeoutAlarm(GlomoNode* node, Message* msg) { NODE_ADDR *info = (NODE_ADDR *)GLOMO_MsgReturnInfo(msg); GlomoNetworkLar1 *lar1; GlomoNetworkIp* ipLayer; ipLayer = (GlomoNetworkIp *) node->networkData.networkVar; lar1 = (GlomoNetworkLar1 *) ipLayer->routingProtocol;#ifdef DEBUG printf("#%d: Check for route reply for dest %d\n", node->nodeAddr, *info);#endif /* Check if Route In Cache */ if (NetworkLar1RouteExists(lar1->routeCacheHead, *info)) { /* Already received the anticipated Route Reply packet */ GLOMO_MsgFree(node, msg); } else { /* Remove route and related information from route cache */ NetworkLar1DeleteRoute(node, *info); /* Need to retransmit a Route Request packet */ NetworkLar1InitiateRouteRequest(node, *info); GLOMO_MsgFree(node, msg); }}//// FUNCTION NetworkLar1NodeInZone()// PURPOSE Returns TRUE if node is within the zone coordinates// PARAMETERS zone - structure containing corner coordinates//BOOL NetworkLar1NodeInZone(GlomoNode *node, LAR1_Zone *zone){#ifdef DEBUG printf(" GPS = (%f, %f)\n", GLOMO_MobilityReturnPositionX(node), GLOMO_MobilityReturnPositionY(node));#endif if ((GLOMO_MobilityReturnPositionX(node) >= zone->bottomLeft.x) && (GLOMO_MobilityReturnPositionX(node) <= zone->topRight.x) && (GLOMO_MobilityReturnPositionY(node) >= zone->bottomLeft.y) && (GLOMO_MobilityReturnPositionY(node) <= zone->topRight.y)) { return TRUE; } else return FALSE;}//// FUNCTION NetworkLar1HandleRouteErrorPacket()// PURPOSE Handle received LAR1 Route Error control packets// PARAMETERS msg - the control packet//void NetworkLar1HandleRouteErrorPacket(GlomoNode *node, Message *msg){ LAR1_RouteError *pkt = (LAR1_RouteError *) GLOMO_MsgReturnPacket(msg); char *pktptr; NODE_ADDR *path; NODE_ADDR fromHop, nextHop; int left, i, numEntries; GlomoNetworkLar1 *lar1; GlomoNetworkIp* ipLayer; ipLayer = (GlomoNetworkIp *) node->networkData.networkVar; lar1 = (GlomoNetworkLar1 *) ipLayer->routingProtocol;#ifdef DEBUG printf(" HandleRouteErrorPacket()\n");#endif /* Calculate the number of node addresses that are included in this packet by taking the full size of the packet, and subtracting the size of the LAR1 Route Reply Header, and then dividing by the size of a node address */ numEntries = GLOMO_MsgReturnPacketSize(msg); numEntries -= sizeof(LAR1_RouteError); numEntries = numEntries / sizeof(NODE_ADDR); /* Record the segmentLeft */ left = pkt->segmentLeft; /* Record the node pair involved in the link break */ fromHop = pkt->fromHop; nextHop = pkt->nextHop; /* Delete routes in cache that use the broken link */ NetworkLar1InvalidateRoutesThroughBrokenLink(node, fromHop, nextHop); /* Position the path pointer onto the array of node addresses */ pktptr = (char *) GLOMO_MsgReturnPacket(msg) + sizeof(LAR1_RouteError); path = (NODE_ADDR *) pktptr;#ifdef DEBUG printf(" destAddr = %d, path[left] = %d\n", pkt->destAddr, path[left]);#endif /* Decrease the segment left */ pkt->segmentLeft = pkt->segmentLeft - 1; if ((pkt->destAddr == node->nodeAddr) && (path[left] == node->nodeAddr)) { /* This error notification has reached its destination (i.e., the source of the broken route */#ifdef DEBUG printf(" I am destination for this route error packet.\n");#endif GLOMO_MsgFree(node, msg); } else if (path[left] == node->nodeAddr) { /* This error notification has reached an intermediate node of the broken route */ NODE_ADDR nextHop = path[pkt->segmentLeft];#ifdef DEBUG printf(" I am relay for this route error packet.\n"); printf(" Send it on to %d\n", nextHop);#endif lar1->RouteErrorsRelayed++; /* 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 NetworkLar1HandleRouteReply()// PURPOSE Handle received LAR1 Route Reply control packets// PARAMETERS msg - the control packet//void NetworkLar1HandleRouteReply(GlomoNode *node, Message *msg){ LAR1_RouteReply *pkt = (LAR1_RouteReply *) GLOMO_MsgReturnPacket(msg); char *pktptr; NODE_ADDR *path; int left; int i; int numEntries; GlomoNetworkLar1 *lar1; GlomoNetworkIp* ipLayer; ipLayer = (GlomoNetworkIp *) node->networkData.networkVar; lar1 = (GlomoNetworkLar1 *) ipLayer->routingProtocol; /* Calculate the number of node addresses that are included in this packet by taking the full size of the packet, and subtracting the size of the LAR1 Route Reply Header, and then dividing by the size of a node address */ numEntries = GLOMO_MsgReturnPacketSize(msg); numEntries -= sizeof(LAR1_RouteReply); numEntries = numEntries / sizeof(NODE_ADDR); /* Decrease the segment left */ pkt->segmentLeft = pkt->segmentLeft - 1; left = pkt->segmentLeft; /* Position the path pointer onto the array of node addresses */ pktptr = (char *) GLOMO_MsgReturnPacket(msg) + sizeof(LAR1_RouteReply); path = (NODE_ADDR *) pktptr;#ifdef DEBUG printf(" destAddr = %d, path[left] = %d\n", pkt->destAddr, path[left]);#endif if ((pkt->destAddr == node->nodeAddr) && (path[left] == node->nodeAddr) && (!NetworkLar1RouteExists(lar1->routeCacheHead, pkt->destAddr))) { /* The node originated the route request received the first rotue reply */ Message *bufMsg; /* Remove this request from the list of outstanding route requests */ NetworkLar1RemoveRequestSent(lar1, pkt->sourceAddr);#ifdef DEBUG printf(" I have received valid and useful path to %d\n", pkt->sourceAddr); for (i = 0; i < numEntries; i++) { printf(" path step #%d = %d\n", i, path[i]); }#endif /* Insert the new route into the route cache */ NetworkLar1InsertRoute(lar1, pkt, path, numEntries); /* Retrieve and send all packets in the buffer for this destination */ bufMsg = NetworkLar1RetrieveSendBuf(lar1, pkt->sourceAddr); while (bufMsg != NULL) {#ifdef DEBUG printf(" Send Message to %d size %d\n", pkt->sourceAddr, GLOMO_MsgReturnPacketSize(bufMsg));#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -