📄 nwip.pc
字号:
//// FUNCTION NetworkIpSendNewPacketWithDelay()//// PURPOSE Used by routing protocols in the network layer// to send a new routing protocol packet to another node.// If the routing protocol has a routing function, the// it will quickly see it again to be routed.//// PARAMETERS destinationAddress - Where the packet is going.// payload, payloadSize - The payload to send.// priority - priority of packet.// protocol - IP protocol number.// delay - The delay until the packet is delivered to this// layer.//void NetworkIpSendNewPacketWithDelay( GlomoNode* node, NODE_ADDR destinationAddress, NetworkQueueingPriorityType priority, unsigned char protocol, unsigned int ttl, char* payload, int payloadSize, clocktype delay){ Message* newMessage = GLOMO_MsgAlloc(node, 0, 0, 0); GLOMO_MsgPacketAlloc(node, newMessage, payloadSize); memcpy(newMessage->packet, payload, payloadSize); NetworkIpSendRawGlomoMessageWithDelay( node, newMessage, destinationAddress, priority, protocol, ttl, delay);}//// FUNCTION NetworkIpReceivePacketFromTransportLayer()//// PURPOSE Interface for packets from above. // Simply calls an another function with the exact// same parameters..//// PARAMETERS destinationAddress - Where the packet is going.// priority - priority of the packet.// protocol - The protocol code (like UDP).// delay - The amount of layer simulation delay for the packet.//void NetworkIpReceivePacketFromTransportLayer( GlomoNode* node, Message* rawMessage, NODE_ADDR destinationAddress, NetworkQueueingPriorityType priority, unsigned char protocol, clocktype delay){ NetworkIpSendRawGlomoMessageWithDelay( node, rawMessage, destinationAddress, priority, protocol, 0, delay);}//---------------------------------------------------------------------------// EVENT HANDLING ROUTINES//// FUNCTION ProcessPacketForMeFromMac()//// PURPOSE Direct the packet either to the transport layer or// to a routing protocol.//staticvoid ProcessPacketForMeFromMac(GlomoNode *node, Message *msg){ GlomoNetworkIp* ipLayer = (GlomoNetworkIp *)node->networkData.networkVar; NODE_ADDR sourceAddress; NODE_ADDR destinationAddress; unsigned char IpProtocol; unsigned int ttl; NetworkQueueingPriorityType priority; if (ipLayer->routerFunction != NULL) { IpHeaderType *ipHeader = (IpHeaderType *) msg->packet; if (ipHeader->ip_dst != ANY_DEST) { // // Let the routing protocol get a look at the original packet // before it is given to the transport layer or a // routing protocol. // BOOL PacketWasRouted = FALSE; (ipLayer->routerFunction)(node, msg, ipHeader->ip_dst, &PacketWasRouted); assert(!PacketWasRouted); }//if// }//if// NetworkIpRemoveIpHeader(node, msg, &sourceAddress, &destinationAddress, &priority, &IpProtocol, &ttl); switch(IpProtocol) { case IPPROTO_UDP: { ipLayer->stats.numPacketsDeliveredToThisNode++; ipLayer->stats.deliveredPacketTtlTotal += ttl; SendToUdp(node, msg, priority, sourceAddress, destinationAddress); break; } case IPPROTO_TCP: { ipLayer->stats.numPacketsDeliveredToThisNode++; ipLayer->stats.deliveredPacketTtlTotal += ttl; SendToTcp(node, msg, priority, sourceAddress, destinationAddress); break; } case IPPROTO_ODMRP: { RoutingOdmrpHandleProtocolPacket(node, msg, sourceAddress, destinationAddress); break; } case IPPROTO_AODV: { RoutingAodvHandleProtocolPacket(node, msg, sourceAddress, destinationAddress, ttl); break; } case IPPROTO_DSR: { RoutingDsrHandleProtocolPacket(node, msg, sourceAddress, destinationAddress, ttl); break; } case IPPROTO_ZRP: { RoutingZrpHandleProtocolPacket(node, msg, sourceAddress, destinationAddress); break; } case IPPROTO_LAR1: { NetworkLar1HandleProtocolPacket(node, msg); break; } //case IPPROTO_OSPF: { // OspfHandleRoutingProtocolPacket(node, msg, sourceAddress); // break; //} default: NetworkIpUserHandleProtocolPacket(node, msg, IpProtocol, sourceAddress, destinationAddress, ttl); break; }//switch// }//ProcessPacketForMeFromMac////// FUNCTION ProcessPacketForAnotherFromMac()//// PURPOSE Figure out what to do with a packet for another node// from the MAC Layer. It needs to be routed or dropped.// PARAMETERS msg - GloMoSim Message to handle from the MAC layer.//staticvoid ProcessPacketForAnotherFromMac(GlomoNode *node, Message *msg) { GlomoNetworkIp* ipLayer = (GlomoNetworkIp *)node->networkData.networkVar; IpHeaderType *ipHeader = (IpHeaderType *) msg->packet; ipLayer->stats.numPacketsRoutedForOtherNodes++; if (ipHeader->ip_ttl == 0) { // Has transversed max. number of hops so drop it. ipLayer->stats.numTtlExpirationDrops++; GLOMO_MsgFree(node, msg); } else { RoutePacketAndSendToMac(node, msg); }//if//}//ProcessPacketForAnotherFromLowerLayer// //// FUNCTION GuiCrap()// PURPOSE //staticvoid GuiCrap(GlomoNode *node, Message* msg, NODE_ADDR lastHopAddress) { IpHeaderType* ipHeader = (IpHeaderType *) msg->packet; if ((node->networkData.guiOption == TRUE) && (ipHeader->ip_dst != ANY_DEST) && (ipHeader->ip_p != IPPROTO_ZRP)) { // // receive a packet from MAC, using VT to display */ // char simTime[50]; ctoa(simclock(), simTime); /* * JGUI_DrawThickLine(info->sourceAddr, node->nodeAddr, * 5, simTime, JGUI_RED); */ JGUI_DrawLine(lastHopAddress, node->nodeAddr, simTime, JGUI_RED); }}//// FUNCTION ProcessPacketFromMac()//// PURPOSE Figure out what to do with a packet from the MAC Layer.// PARAMETERS msg - GloMoSim Message to handle from the MAC layer.//staticvoid ProcessPacketFromMac(GlomoNode *node, Message* msg) { GlomoNetworkIp* ipLayer = (GlomoNetworkIp *)node->networkData.networkVar; IpHeaderType *ipHeader = (IpHeaderType *) msg->packet; ipHeader->ip_ttl = ipHeader->ip_ttl - 1; if (IsMyPacket(node, msg)) { BOOL packetIsComplete = TRUE; Message* completePacket = NULL; IpHeaderType *ipHeader = (IpHeaderType *) msg->packet; if (IpPacketIsFragmented(ipHeader)) { ProcessFragmentAndMaybeBuildCompletePacket( node, msg, &packetIsComplete, &completePacket); GLOMO_MsgFree(node, msg); } else { completePacket = msg; }//if// if (packetIsComplete) { ProcessPacketForMeFromMac(node, completePacket); }//if// } else { ProcessPacketForAnotherFromMac(node, msg); }//if//}void NetworkIpReceivePacketFromMacLayer( GlomoNode* node, Message* msg, NODE_ADDR lastHopAddress){ GuiCrap(node, msg, lastHopAddress); ProcessPacketFromMac(node, msg);}void NetworkIpNotifyOfPacketDrop( GlomoNode* node, Message* msg, NODE_ADDR triedNextHop){ GlomoNetworkIp* ipLayer = (GlomoNetworkIp *)node->networkData.networkVar; if (ipLayer->packetDropHandlerFunction != NULL) { ipLayer->packetDropHandlerFunction(node, msg, triedNextHop); }//if// GLOMO_MsgFree(node, msg); }//// FUNCTION NetworkIpLayer// PURPOSE To handle Network IP layer events, incoming messages and// messages sent to itself (timers,etc).// PARAMETERS // message - Abstract GloMoSim Message (Packet) to handle.//void NetworkIpLayer(GlomoNode *node, Message *msg) { switch (msg->protocolType) { case NETWORK_PROTOCOL_IP: { switch(msg->eventType) { case MSG_NETWORK_FromTransportOrRoutingProtocol: { RoutePacketAndSendToMac(node, msg); break; } case MSG_NETWORK_DelayedSendToMac: { ProcessDelayedSendToMac(node, msg); break; } default: assert(FALSE); abort(); }//switch// break; } case ROUTING_PROTOCOL_ODMRP: { RoutingOdmrpHandleProtocolEvent(node, msg); break; } case ROUTING_PROTOCOL_AODV: { RoutingAodvHandleProtocolEvent(node, msg); break; } case ROUTING_PROTOCOL_DSR: { RoutingDsrHandleProtocolEvent(node, msg); break; } case ROUTING_PROTOCOL_ZRP: { RoutingZrpHandleProtocolEvent(node, msg); break; } case ROUTING_PROTOCOL_LAR1: { NetworkLar1HandleCheckTimeoutAlarm(node, msg); break; } //case ROUTING_PROTOCOL_OSPF: { // OspfHandleRoutingProtocolEvent(node, msg); // break; //} //case ROUTING_PROTOCOL_ALL: { // break; //} default: NetworkIpUserHandleProtocolEvent(node, msg); break; }//switch//}//// FUNCTION NetworkIpInitStats// PURPOSE Initialize Statistic variables.//staticvoid NetworkIpInitStats(NetworkIpStatsType* stats){ stats->numPacketsSentToMac = 0; stats->numPacketsRoutedForOtherNodes = 0; stats->numPacketsDeliveredToThisNode = 0; stats->deliveredPacketTtlTotal = 0; stats->numNetworkUnreachableDrops = 0; stats->numTtlExpirationDrops = 0;} void NetworkIpAddNewInterfaceWithOutputQueue( GlomoNode* node, InterfaceIdType interfaceId, const GlomoNodeInput* nodeInput){ char queueTypeName[GLOMO_MAX_STRING_LENGTH]; BOOL wasFound; GlomoNetworkIp* ipLayer = (GlomoNetworkIp *)node->networkData.networkVar; assert((interfaceId >= 0) && (interfaceId < MAX_NUM_INTERFACES)); if (ipLayer->interfaceQueues[interfaceId] != NULL) { printf("GLOMO Error: Node %d : Conflict with interface %d .\n", node->id, interfaceId); abort(); }//if// ipLayer->interfaceQueues[interfaceId] = (IpOutputQueueType*)checked_pc_malloc(sizeof(IpOutputQueueType)); ipLayer->interfaceQueues[interfaceId]->packetsLostToOverflow = 0; GLOMO_ReadStringInstance( node->nodeAddr, nodeInput, "NETWORK-QUEUE-TYPE", interfaceId, TRUE, &wasFound, queueTypeName); if ((!wasFound) || (strcmp(queueTypeName, "FIFO") == 0)) { MultiFifoPacketQueue_Initialize( ipLayer->interfaceQueues[interfaceId], node, interfaceId, nodeInput); } else { printf("GLOMO Error: Unknown Network layer queuing " "type (%s).\n", queueTypeName); assert(FALSE); abort(); }//if//}//// FUNCTION NetworkIpPreInit// PURPOSE Just create and initialize variables and // datastructures to 0 or NULL.// init datastructures. This routine is// not dependent on any input or state and// should be called before initializing lower// layers.//void NetworkIpPreInit(GlomoNode* node) { GlomoNetworkIp* ipLayer = (GlomoNetworkIp *)checked_pc_malloc(sizeof(GlomoNetworkIp)); int I; node->networkData.networkVar = (void *)ipLayer; ipLayer->routingProtocolChoice = ROUTING_PROTOCOL_NONE; ipLayer->routingProtocol = NULL; ipLayer->routerFunction = NULL; ipLayer->packetDropHandlerFunction = NULL; ipLayer->promiscuousMessagePeekFunction = NULL; ipLayer->packetIdCounter = 0; NetworkInitForwardingTable(node); IpReassemblyBufferList_Init(&(ipLayer->reassemblyBufferList)); ipLayer->maxPacketLength = NET
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -