📄 nwip.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.//------------------------------------------------------------------------// $Id: nwip.pc,v 1.95 2001/12/19 22:33:43 jmartin Exp $//// File: nwip.pc// By: Teresa Yan (tyan@cs.ucla.edu) // Jay Martin (jmartin@cs.ucla.edu) // Objectives: IP (Internet Protocol) // A very simple IP that do fragmentation, reassembly and// multiplexing and demultiplexing. // References: RFC 791// Date: 8/20/1999////------------------------------------------------------------------------#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 "nwcommon.h"#include "application.h"#include "transport.h"#include "nwlar1.h"#include "aodv.h"#include "dsr.h"#include "odmrp.h"#include "zrp.h"//#include "ospf.h"#include "java_gui.h"#include "fifoqueue.h"//----------------------------------------------------------------------------// IP PACKET HEADER INFORMATION MANIPULATION ROUTINES//// MACRO IpPacketIsFragmented()// PURPOSE Return whether the IP packet is fragmented.// PARAMETERS An IP Header.//#define IpPacketIsFragmented(IpHeader) \ (((IpHeader)->ip_more_fragments == 1) || \ (FragmentOffset(IpHeader) != 0))//// FUNCTION IpHeaderSourceRouteOptionField// PURPOSE Returns whether the IP header has a source route.// PARAMETERS An IP Header.//staticIpOptionsHeaderType* IpHeaderSourceRouteOptionField(IpHeaderType* ipHeader) { return (FindAnIpOptionField(ipHeader, IPOPT_SSRR));}#define IpHeaderHasSourceRoute(ipHeader) \ (IpHeaderSourceRouteOptionField(ipHeader) != NULL)//// FUNCTION IsMyPacket()// PURPOSE Determines whether the packet is for this node.// PARAMETERS msg - A Glomosim message that must be an IP packet.//staticBOOL IsMyPacket(GlomoNode *node, Message *msg){ IpHeaderType *ipHeader = (IpHeaderType *) msg->packet; if ((ipHeader->ip_dst == ANY_DEST) || (node->nodeAddr == ipHeader->ip_dst)) { return TRUE; } else { return FALSE; }}//// FUNCTION IsIpFragmentOf()// PURPOSE Determines whether two packet fragments are fragments of// the same packet.// PARAMETERS msg1 - A Glomosim message that must be an IP packet.// msg2 - A Glomosim message that must be an IP packet.//staticBOOL IsIpFragmentOf(const Message* msg1, const Message* msg2) { IpHeaderType* ipHeader1 = (IpHeaderType*)msg1->packet; IpHeaderType* ipHeader2 = (IpHeaderType*)msg2->packet; return ((ipHeader1->ip_id == ipHeader2->ip_id) && (ipHeader1->ip_src == ipHeader2->ip_src) && (ipHeader1->ip_p == ipHeader2->ip_p));}//// FUNCTION AddIpHeader() // PURPOSE Add an IP packet header to a Glomo message.// PARAMETERS // msg - A Glomosim message.// destinationAddress - Where this packet is going.// priority - Currently a MacQueuePriority (values are not standard for // "IP type of service field" but has correct function).// protocol - The IP protocol number to put in header.//void AddIpHeader( GlomoNode *node, Message *msg, NODE_ADDR destinationAddress, NetworkQueueingPriorityType priority, unsigned char protocol, unsigned int ttl){ GlomoNetworkIp* ipLayer = (GlomoNetworkIp *)node->networkData.networkVar; IpHeaderType* ipHeader; GLOMO_MsgAddHeader(node, msg, sizeof(IpHeaderType)); ipHeader = (IpHeaderType *) msg->packet; memset(ipHeader, 0, sizeof(IpHeaderType)); ipHeader->ip_id = ipLayer->packetIdCounter; ipLayer->packetIdCounter++; ipHeader->ip_src = node->nodeAddr; ipHeader->ip_dst = destinationAddress; if (ttl == 0) { ipHeader->ip_ttl = IPDEFTTL; } else { ipHeader->ip_ttl = ttl; } ipHeader->ip_tos = priority; ipHeader->ip_p = protocol; assert(msg->packetSize <= IP_MAXPACKET); ipHeader->ip_len = msg->packetSize; SetIpHeaderSize(ipHeader, sizeof(IpHeaderType));}//// FUNCTION ExpandOrShrinkIpHeader() // PURPOSE Increases the size of an Ip header so as to allow for// adding a new source route or other options.// PARAMETERS // msg - A Glomosim message.// NewHeaderSize - The new expanded IP header size.//staticvoid ExpandOrShrinkIpHeader(GlomoNode* node, Message* msg, int newHeaderSize) { IpHeaderType* ipHeader = (IpHeaderType*) msg->packet; assert(newHeaderSize <= IP_MAX_HEADER_SIZE); if (IpHeaderSize(ipHeader) != newHeaderSize) { IpHeaderType* origIpHeader = ipHeader; int oldIpHeaderSize = IpHeaderSize(ipHeader); GLOMO_MsgRemoveHeader(node, msg, oldIpHeaderSize); GLOMO_MsgAddHeader(node, msg, newHeaderSize); ipHeader = (IpHeaderType*) msg->packet; memmove((char*)ipHeader, (char*)origIpHeader, MIN(oldIpHeaderSize, newHeaderSize)); SetIpHeaderSize(ipHeader, newHeaderSize); }//if//}void AddIpOptionField( GlomoNode* node, Message* msg, int OptionCode, int OptionSize) { IpOptionsHeaderType* newIpOption = NULL; int oldHeaderSize = IpHeaderSize((IpHeaderType*)msg->packet); // Round up to nearest option size divisable by 4. int newIpOptionSize = 4 * ((((sizeof(IpOptionsHeaderType) + OptionSize) - 1)/4) + 1); int newHeaderSize = oldHeaderSize + newIpOptionSize; assert(FindAnIpOptionField((IpHeaderType*)msg->packet, OptionCode) == NULL); ExpandOrShrinkIpHeader(node, msg, newHeaderSize); newIpOption = (IpOptionsHeaderType*)((char*)msg->packet + oldHeaderSize); newIpOption->len = newIpOptionSize; newIpOption->code = OptionCode; newIpOption->ptr = IPOPT_MINOFF;}staticvoid ExpandOrShrinkIpOptionField( GlomoNode* node, Message* msg, const int optionCode, const int newIpOptionSize) { IpHeaderType* ipHeader = (IpHeaderType*)msg->packet; IpOptionsHeaderType* ipOption = FindAnIpOptionField(ipHeader, optionCode); int oldHeaderSize = IpHeaderSize(ipHeader); int oldIpOptionSize = ipOption->len; int deltaOptionSize = newIpOptionSize - ipOption->len; int newHeaderSize = oldHeaderSize + deltaOptionSize; int bytesAfterOption = (int)(((char*)ipHeader + oldHeaderSize) - ((char*)ipOption + oldIpOptionSize)); assert(newIpOptionSize % 4 == 0); ExpandOrShrinkIpHeader(node, msg, newHeaderSize); ipHeader = (IpHeaderType*)msg->packet; ipOption = FindAnIpOptionField(ipHeader, optionCode); ipOption->len = newIpOptionSize; // Move the header data after this option field to make room for // the new option data. memmove(((char*)ipOption + newIpOptionSize), ((char*)ipOption + oldIpOptionSize), bytesAfterOption);} //// FUNCTION NetworkIpRemoveIpHeader() // PURPOSE Removes the IP header while also returning the source // address and IP protocol code and other fields from the header.// // sourceAddress - Extracted source address.// destinationAddress - Extracted destination address.// priority - Currently a MacQueuePriority (values are not standard for // "IP type of service field" but has correct function).// protocol - Extracted IP protocol field. ////void NetworkIpRemoveIpHeader( GlomoNode *node, Message *msg, NODE_ADDR* sourceAddress, NODE_ADDR* destinationAddress, NetworkQueueingPriorityType* priority, unsigned char* protocol, unsigned int* ttl){ IpHeaderType *ipHeader = (IpHeaderType *)msg->packet; *priority = ipHeader->ip_tos; *ttl = ipHeader->ip_ttl; *protocol = ipHeader->ip_p; *sourceAddress = ipHeader->ip_src; *destinationAddress = ipHeader->ip_dst; GLOMO_MsgRemoveHeader(node, msg, IpHeaderSize(ipHeader));} //// FUNCTION ExtractIpSourceAndRecordedRoute() // PURPOSE Retrieves a copy of the source and recorded route from// the options field in the header.// // msg - The message with an IP header with a source/recorded route.// RouteAddress - An array for the returned addresses.// NumAddresses - The returned size of this array.// RouteAddressIndex - The index of the first address of the source// route, before this index is the recorded route.void ExtractIpSourceAndRecordedRoute( Message* msg, NODE_ADDR RouteAddresses[], int* NumAddresses, int* RouteAddressIndex){ IpHeaderType* ipHeader = (IpHeaderType*)msg->packet; IpOptionsHeaderType* ipOptions = IpHeaderSourceRouteOptionField(ipHeader); char* FirstAddress = ((char*)ipOptions + sizeof(IpOptionsHeaderType)); assert(IpHeaderHasSourceRoute(ipHeader)); *NumAddresses = (ipOptions->len - sizeof(IpOptionsHeaderType) - IP_SOURCE_ROUTE_OPTION_PADDING) / sizeof(NODE_ADDR); *RouteAddressIndex = (ipOptions->ptr - sizeof(IpOptionsHeaderType) - IP_SOURCE_ROUTE_OPTION_PADDING) / sizeof(NODE_ADDR); memmove(RouteAddresses, FirstAddress,((*NumAddresses) * sizeof(NODE_ADDR)));}//-----------------------------------------------------------------------------//-----------------------------------------------------------------------------// Network Queue Routines.typedef struct { NODE_ADDR nextHopAddress;} TackedOnInfoWhileInNetworkQueueType;BOOL NetworkIpOutputQueueIsEmpty(GlomoNode* node, InterfaceIdType interfaceId) { GlomoNetworkIp* ipLayer = (GlomoNetworkIp*)node->networkData.networkVar; IpOutputQueueType* queue; assert(interfaceId >= 0); assert(interfaceId < node->numberInterfaces); queue = ipLayer->interfaceQueues[interfaceId]; // Call the "isEmptyFunction" via a ugly function pointer. return ((*queue->isEmptyFunction)(queue->queueUnion));}int NetworkIpOutputQueueNumberInQueue( GlomoNode* node, InterfaceIdType interfaceId, NetworkQueueingPriorityType priority){ GlomoNetworkIp* ipLayer = (GlomoNetworkIp*)node->networkData.networkVar; IpOutputQueueType* queue; assert(interfaceId >= 0); assert(interfaceId < node->numberInterfaces); queue = ipLayer->interfaceQueues[interfaceId]; // Call the "numberInQueueFunction" via a ugly function pointer. return ((*queue->numberInQueueFunction)(queue->queueUnion, priority));}void NetworkIpOutputQueueDequeuePacket( GlomoNode* node, InterfaceIdType interfaceId, Message** msg, NODE_ADDR* nextHopAddress, NetworkQueueingPriorityType* priority){ GlomoNetworkIp* ipLayer = (GlomoNetworkIp*)node->networkData.networkVar; IpOutputQueueType* queue; NetworkQueueingPriorityType NotUsed = 0; assert(interfaceId >= 0); assert(interfaceId < node->numberInterfaces); queue = ipLayer->interfaceQueues[interfaceId]; (*queue->retrieveAndMaybeDequeueFunction)( queue->queueUnion, FALSE, NotUsed, 0, msg, priority, TRUE); assert(*msg != NULL); *nextHopAddress = ((TackedOnInfoWhileInNetworkQueueType*)((*msg)->info))->nextHopAddress;} void NetworkIpOutputQueueDequeuePacketForAPriority( GlomoNode* node, InterfaceIdType interfaceId, NetworkQueueingPriorityType priority, Message** msg, NODE_ADDR* nextHopAddress){ GlomoNetworkIp* ipLayer = (GlomoNetworkIp*)node->networkData.networkVar; IpOutputQueueType* queue; NetworkQueueingPriorityType NotUsed = 0; assert(interfaceId >= 0); assert(interfaceId < node->numberInterfaces); queue = ipLayer->interfaceQueues[interfaceId]; (*queue->retrieveAndMaybeDequeueFunction)( queue->queueUnion, TRUE, priority, 0, msg, &NotUsed, TRUE); *nextHopAddress = ((TackedOnInfoWhileInNetworkQueueType*)((*msg)->info))->nextHopAddress;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -