NetSim Source Code Help v14.4
All 13 Components
 
Loading...
Searching...
No Matches
GenerateArpRequest.c
1/************************************************************************************
2 * Copyright (C) 2023 *
3 * TETCOS, Bangalore. India *
4 * Tetcos owns the intellectual property rights in the Product and its content. *
5 * The copying, redistribution, reselling or publication of any or all of the *
6 * Product or its content without express prior written consent of Tetcos is *
7 * prohibited. Ownership and / or any other right relating to the software and all *
8 * intellectual property rights therein shall remain at all times with Tetcos. *
9 * Author: Basamma YB
10 * Date :
11 ************************************************************************************/
12
13#define _CRT_SECURE_NO_DEPRECATE
14#include "main.h"
15#include "ARP.h"
16/**
17~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
18Function: Generate ARP Request
19Whenever the ARP Table do not have the destination device MAC address, this function
20is called to generate BROARDCAST ARP request. Once the Request packet generated, if
21reply won't get, before generating the next request, it has to wait till the
22ARP_RETRY_INTERVAL (default 10secs) The number of retries is equal to ARP_RETRY_LIMIT
23(default 3). So ARP Request packet generated, buffer the packet till you get reply or
24RequestSentCount reaches ARP_RETRY_LIMIT. If you got the reply, farward the buffered
25packet, else drop the buffered packets.
26Do the following to generate Request
271. Check the RequestFlag. First time it is 0, buffer the packet, Generate the request
28 and set the flag to 1.
292.Add the relent filelds hardware type, protocol type,MAC address lenth, IP adress length
30 SrcAddIP, DestAddIP,SrcMacAdd etc relevent fields for ARP request
313.Add the request packet to access buffer to farward the packet to next layer.
324. Generate the Request TimeOut event by adding the ARP_RETRY_INTERVAL to the current event time.
335.When the RequestFlag is 1, buffer the packets
34~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
35*/
36int fn_NetSim_Generate_ARP_Request(NetSim_EVENTDETAILS *pstruEventDetails, struct stru_NetSim_Network *NETWORK)
37{
38 NETSIM_ID nDeviceId;
39 NETSIM_ID nInterfaceId;
40 NETSIM_ID nDestinationId = 0;
41 int nCheckSubnet;
42 NETSIM_IPAddress szSrcIPadd,szNextHopIp;
43 NETSIM_IPAddress szSubnetMaskAdd;
44 NETSIM_IPAddress szDestIPadd;
45 unsigned int unProtocolId;
46
47 NetSim_PACKET *pstruTemp_Data; // NetSim Temp packet to store the packet from the event details
48 NetSim_PACKET *pstruControlPacket; // NetSim packet To store the ARP packet
49 ARP_PACKET *pstruArpRequestPkt; // ARP PACKET to store ARP packet data
50 ARP_VARIABLES *pstruArpVariables; // type casting Arp variables
51
52 //Get the packet from the event
53 pstruTemp_Data =pstruEventDetails->pPacket;
54 // Get the device and interface Id from the event
55 nInterfaceId = pstruEventDetails->nInterfaceId;
56 nDeviceId = pstruEventDetails->nDeviceId;
57 // Get the source and destination IP address from the packet
58 szSrcIPadd = pstruTemp_Data->pstruNetworkData->szSourceIP;
59 szDestIPadd = pstruTemp_Data->pstruNetworkData->szDestIP;
60 szNextHopIp = pstruTemp_Data->pstruNetworkData->szNextHopIp;
61 if(IP_COMPARE(szSrcIPadd,DEVICE_INTERFACE(nDeviceId,nInterfaceId)->szAddress) != 0)
62 {
63 szSrcIPadd = DEVICE_INTERFACE(nDeviceId,nInterfaceId)->szAddress;
64 }
65 if(szNextHopIp)
66 szDestIPadd = szNextHopIp;
67
68 // Get the SubnetMaskAdd from the device
69 szSubnetMaskAdd = DEVICE_INTERFACE(nDeviceId,nInterfaceId)->szSubnetMask;
70 // Call the IPV4 function to check Destination MAC in the same subnet/LAN
71 nCheckSubnet = IP_IS_IN_SAME_NETWORK_IPV4(szSrcIPadd,szDestIPadd,szSubnetMaskAdd);
72 // Get the Arp variables from device ipVar
73 pstruArpVariables = DEVICE_INTERFACE(nDeviceId,nInterfaceId)->ipVar;
74 /* Generate ARP Request */
75 //Allocate memory for the ARP packet
76 pstruArpRequestPkt = fnpAllocateMemory(1,sizeof(ARP_PACKET));
77 switch(nCheckSubnet)
78 {
79 case 1: // Destination is in the same LAN
80 nDestinationId = get_first_dest_from_packet(pstruEventDetails->pPacket);
81 pstruArpRequestPkt->sz_ar$tpa = IP_COPY(szDestIPadd);
82 break;
83 default:
84 {
85 char sip[_NETSIM_IP_LEN],dip[_NETSIM_IP_LEN];
86 IP_TO_STR(szSrcIPadd,sip);
87 IP_TO_STR(szDestIPadd,dip);
88 fnNetSimError("ARP--- Packet nexthop and gateway in different LAN\nSrc IP = %s\tDest IP =%s\n",sip,dip);
89 }
90 break;
91 }
92 //Add the relevant fields for ARP REQUEST
93 pstruArpRequestPkt->n_ar$hrd = IEEE802; // H/W type
94 pstruArpRequestPkt->n_ar$pro = ARP_TO_RESOLVE_IP; // Protocol type
95 pstruArpRequestPkt->usn_ar$hln = HARDWARE_ADDRESS_LENGTH; // MAC add length in bytes
96 pstruArpRequestPkt->n_ar$op = ares_opSREQUEST; // 1-REQUEST, 2-REPLY
97 //Get the source MAC address from the device MAC layer details
98 pstruArpRequestPkt->sz_ar$sha= DEVICE_INTERFACE(nDeviceId,nInterfaceId)->pstruMACLayer->szMacAddress;
99 pstruArpRequestPkt->sz_ar$spa = IP_COPY(szSrcIPadd); // Source IP Add
100 pstruArpRequestPkt->sz_ar$tha = NULL;
101 //Add the Ethernet header details
102 pstruArpRequestPkt->szDestMac = BROADCAST_MAC;
103 pstruArpRequestPkt->szSrcMac = DEVICE_INTERFACE(nDeviceId,nInterfaceId)->pstruMACLayer->szMacAddress;
104 pstruArpRequestPkt->nEther_type = ADDRESS_RESOLUTION;
105 // Create netsim packet for NETWORK_LAYER
106 pstruControlPacket = fn_NetSim_Packet_CreatePacket(NETWORK_LAYER);
107 //Generate the control packet
108 pstruControlPacket->dEventTime = pstruEventDetails->dEventTime;
109 add_dest_to_packet(pstruControlPacket, 0);
110 pstruControlPacket->nPacketType = PacketType_Control;
111 pstruControlPacket->nControlDataType = REQUEST_PACKET;
112 pstruControlPacket->nTransmitterId = pstruEventDetails->nDeviceId;
113 pstruControlPacket->nReceiverId = 0;
114 pstruControlPacket->nSourceId = pstruEventDetails->nDeviceId;
115 //Update IP address in the packet.
116 pstruControlPacket->pstruNetworkData->szSourceIP = IP_COPY(DEVICE_NWADDRESS(nDeviceId,nInterfaceId));
117 // For the BROADCAST control packet, DestIP should be broadcast IP
118 pstruControlPacket->pstruNetworkData->szDestIP = szBroadcastIPaddress;
119 //Update MAC address in the packet
120 pstruControlPacket->pstruMacData->szSourceMac = (DEVICE_INTERFACE(nDeviceId,nInterfaceId)->pstruMACLayer->szMacAddress);
121 // for the BROADCAST control packet, DestMac should be broadcast MAC
122 pstruControlPacket->pstruMacData->szDestMac = BROADCAST_MAC;
123 //Update NetworkData packet timings
124 pstruControlPacket->pstruNetworkData->dArrivalTime = pstruEventDetails->dEventTime;
125 pstruControlPacket->pstruNetworkData->dEndTime = pstruEventDetails->dEventTime;
126 pstruControlPacket->pstruNetworkData->dStartTime = pstruEventDetails->dEventTime;
127 pstruControlPacket->nPacketPriority = Priority_High;
128 //Add overheads and size to NetworkData of the packet
129 unProtocolId = fn_NetSim_Stack_GetNWProtocol(nDeviceId);
130 if(unProtocolId == NW_PROTOCOL_IPV4)
131 {
132 pstruArpRequestPkt->usn_ar$pln = IPV4_PROTOCOL_ADDREES_LENGTH;
133 pstruControlPacket->pstruNetworkData->dPayload = IPV4_ARP_PACKET_SIZE_WITH_ETH_HEADER;
134 pstruControlPacket->pstruNetworkData->dOverhead = IPV4_NETWORK_OVERHEADS;
135 }
136 else
137 {
138 pstruArpRequestPkt->usn_ar$pln = IPV6_PROTOCOL_ADDREES_LENGTH;
139 pstruControlPacket->pstruNetworkData->dPayload = IPV6_ARP_PACKET_SIZE_WITH_ETH_HEADER;
140 pstruControlPacket->pstruNetworkData->dOverhead = IPV6_NETWORK_OVERHEADS;
141 }
142 pstruControlPacket->pstruNetworkData->dPacketSize = pstruControlPacket->pstruNetworkData->dPayload + pstruControlPacket->pstruNetworkData->dOverhead;
143 // Assign Protocol Id
144 pstruControlPacket->pstruNetworkData->nNetworkProtocol = NW_PROTOCOL_ARP;
145 // Assign ARP Request packet to NetworkProtocolPacket
146 pstruControlPacket->pstruNetworkData->Packet_NetworkProtocol = pstruArpRequestPkt;
147 pstruControlPacket->pstruNextPacket=NULL;
148
149 // Get buffer status
150 if(!fn_NetSim_GetBufferStatus(DEVICE_INTERFACE(nDeviceId,nInterfaceId)->pstruAccessInterface->pstruAccessBuffer))
151 {
152 pstruEventDetails->nSubEventType = 0;
153 pstruEventDetails->dPacketSize = pstruControlPacket->pstruNetworkData->dPacketSize;
154 pstruEventDetails->nProtocolId = fn_NetSim_Stack_GetMacProtocol(nDeviceId,nInterfaceId);
155 pstruEventDetails->nApplicationId = 0;
156 pstruEventDetails->nPacketId = 0;
157 pstruEventDetails->nSegmentId = 0;
158 pstruEventDetails->nEventType =MAC_OUT_EVENT;
159 pstruEventDetails->pPacket = NULL;
160 fnpAddEvent( pstruEventDetails);
161 }
162 fn_NetSim_Packet_AddPacketToList(DEVICE_INTERFACE(nDeviceId,nInterfaceId)->pstruAccessInterface->pstruAccessBuffer,pstruControlPacket,0);
163
164 pstruArpVariables->pnArpRequestFlag[nDestinationId] = 1; //Set the request Flag
165 pstruArpVariables->pstruArpMetrics->nArpRequestSentCount+= 1;
166
167 // Write to Log file
168 printf("ARP---ARP_RequestSent\tEventTime:%0.3lf\tSrcIP:%s\tDestIP:%s \n",pstruEventDetails->dEventTime,szSrcIPadd->str_ip,pstruArpRequestPkt->sz_ar$tpa->str_ip);
169
170 // copy the control packet for generating time out event
171 pstruControlPacket = fn_NetSim_Packet_CopyPacket(pstruTemp_Data);
172 remove_dest_from_packet(pstruControlPacket, 0);
173 add_dest_to_packet(pstruControlPacket, nDestinationId);
174 // Generate Timeout Event by adding ARP_RETRY_INTERVAL to EventTime
175 pstruEventDetails->dEventTime = pstruEventDetails->dEventTime + (pstruArpVariables->nArpRetryInterval*1000000);
176 pstruEventDetails->nEventType = TIMER_EVENT; //NETWORK_OUT_EVENT;
177 pstruEventDetails->nSubEventType = ARP_REQUEST_TIMEOUT;
178 pstruEventDetails->pPacket = pstruControlPacket;
179 pstruEventDetails->nProtocolId = NW_PROTOCOL_ARP;
180 fnpAddEvent(pstruEventDetails);
181 pstruArpVariables->pnArpRetryCount[nDestinationId]+= 1; //Increment Retry count
182 pstruEventDetails->pPacket = NULL;
183 return 0;
184}
185
186
int * pnArpRequestFlag
Set when generate Request.
Definition ARP.h:153
int * pnArpRetryCount
To keep track of number of retries.
Definition ARP.h:155
ARP_METRICS * pstruArpMetrics
NetSim specific ARP metrics structure.
Definition ARP.h:157
int nArpRetryInterval
Store the ARP_RETRY_INTERVAL from the config file.
Definition ARP.h:152
int nArpRequestSentCount
Number of requests sent from the source.
Definition ARP.h:140
PNETSIM_MACADDRESS szDestMac
Destination MAC address.
Definition ARP.h:106
unsigned short int usn_ar$pln
Protocol address length 1 byte,specifies the sizes of the protocol address in bytes.
Definition ARP.h:113
ETHERNET_TYPE nEther_type
Ethernet Type.
Definition ARP.h:108
NETSIM_IPAddress sz_ar$spa
Protocol address of the sender.
Definition ARP.h:116
PNETSIM_MACADDRESS sz_ar$tha
Hardware address of target (if know) otherwise empty 6 bytes.
Definition ARP.h:117
OPCODE n_ar$op
Operation REQUEST/REPLY 2 bytes.
Definition ARP.h:114
unsigned short int usn_ar$hln
H/W address length 1 byte ,specifies the sizes of the H/W address in bytes.
Definition ARP.h:112
HARDWARETYPE n_ar$hrd
Hardware Type 2 bytes.
Definition ARP.h:110
PNETSIM_MACADDRESS sz_ar$sha
Hardware address of the sender.
Definition ARP.h:115
PNETSIM_MACADDRESS szSrcMac
Source MAC address.
Definition ARP.h:107
PROTOCOLTYPE n_ar$pro
Protocol Type 2 bytes.
Definition ARP.h:111
NETSIM_IPAddress sz_ar$tpa
Protocol address of target.
Definition ARP.h:118