NetSim Source Code Help
Loading...
Searching...
No Matches
GenerateArpRequest.c
Go to the documentation of this file.
1/************************************************************************************
2 * Copyright (C) 2021 *
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*/
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
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
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;
134 pstruControlPacket->pstruNetworkData->dOverhead = IPV4_NETWORK_OVERHEADS;
135 }
136 else
137 {
138 pstruArpRequestPkt->usn_ar$pln = IPV6_PROTOCOL_ADDREES_LENGTH;
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 {
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
176 pstruEventDetails->nEventType = TIMER_EVENT; //NETWORK_OUT_EVENT;
178 pstruEventDetails->pPacket = pstruControlPacket;
181 pstruArpVariables->pnArpRetryCount[nDestinationId]+= 1; //Increment Retry count
182 pstruEventDetails->pPacket = NULL;
183 return 0;
184}
185
186
NETSIM_IPAddress szBroadcastIPaddress
Definition: ARP.h:168
#define IPV4_PROTOCOL_ADDREES_LENGTH
Length of 32 bit IP address in bytes.
Definition: ARP.h:26
#define IPV4_ARP_PACKET_SIZE_WITH_ETH_HEADER
ARP frame size in Bytes: 28 + 14.
Definition: ARP.h:28
@ ADDRESS_RESOLUTION
Definition: ARP.h:72
@ IEEE802
Definition: ARP.h:78
#define IPV6_NETWORK_OVERHEADS
In bytes for IPV6 (320 bits).
Definition: ARP.h:33
#define IPV6_PROTOCOL_ADDREES_LENGTH
Length of 128 bit IP address in bytes.
Definition: ARP.h:30
@ REQUEST_PACKET
Definition: ARP.h:88
#define IPV4_NETWORK_OVERHEADS
In bytes for IPV4.
Definition: ARP.h:29
#define HARDWARE_ADDRESS_LENGTH
Lenghth of 48 bit MAC address in bytes.
Definition: ARP.h:24
@ ARP_REQUEST_TIMEOUT
Definition: ARP.h:61
@ ares_opSREQUEST
Definition: ARP.h:66
@ ARP_TO_RESOLVE_IP
Definition: ARP.h:83
#define IPV6_ARP_PACKET_SIZE_WITH_ETH_HEADER
ARP frame size in Bytes: 52 + 14.
Definition: ARP.h:32
unsigned int NETSIM_ID
Definition: Animation.h:45
int fn_NetSim_Generate_ARP_Request(NetSim_EVENTDETAILS *pstruEventDetails, struct stru_NetSim_Network *NETWORK)
NETSIM_IPAddress IP_COPY(NETSIM_IPAddress ip)
#define _NETSIM_IP_LEN
Definition: IP_Addressing.h:29
#define IP_TO_STR(ip, ipstr)
Definition: IP_Addressing.h:71
#define IP_IS_IN_SAME_NETWORK_IPV4(ip1, ip2, subnet)
Definition: IP_Addressing.h:97
#define IP_COMPARE(ip1, ip2)
Definition: IP_Addressing.h:67
#define fnNetSimError(x,...)
Definition: Linux.h:56
#define fnpAllocateMemory(count, size)
Definition: Memory.h:34
bool fn_NetSim_GetBufferStatus(NetSim_BUFFER *pstruBuffer)
Definition: Scheduling.c:41
@ PacketType_Control
Definition: Packet.h:66
NETSIM_ID get_first_dest_from_packet(NetSim_PACKET *packet)
void remove_dest_from_packet(NetSim_PACKET *packet, NETSIM_ID dest)
void add_dest_to_packet(NetSim_PACKET *packet, NETSIM_ID dest)
@ Priority_High
Definition: Packet.h:82
NETWORK_LAYER_PROTOCOL fn_NetSim_Stack_GetNWProtocol(NETSIM_ID nDeviceId)
EXPORTED PNETSIM_MACADDRESS BROADCAST_MAC
Definition: Stack.h:393
#define DEVICE_NWADDRESS(DeviceId, InterfaceId)
Definition: Stack.h:805
@ NW_PROTOCOL_ARP
Definition: Stack.h:191
@ NW_PROTOCOL_IPV4
Definition: Stack.h:189
MAC_LAYER_PROTOCOL fn_NetSim_Stack_GetMacProtocol(NETSIM_ID nDeviceId, NETSIM_ID nInterfaceId)
EXPORTED struct stru_NetSim_Network * NETWORK
Definition: Stack.h:742
@ TIMER_EVENT
Definition: Stack.h:114
@ MAC_OUT_EVENT
Definition: Stack.h:106
@ NETWORK_LAYER
Definition: Stack.h:96
EXPORTED struct stru_NetSim_EventDetails * pstruEventDetails
Definition: Stack.h:837
#define DEVICE_INTERFACE(DeviceId, InterfaceId)
Definition: Stack.h:780
#define fn_NetSim_Packet_CopyPacket(pstruPacket)
Definition: main.h:182
#define fn_NetSim_Packet_CreatePacket(layer)
Definition: main.h:186
#define fnpAddEvent(pstruEvent)
Definition: main.h:191
#define fn_NetSim_Packet_AddPacketToList(pstruBuffer, pstruPacket, nInsertionType)
Definition: main.h:179
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
This Arp packet structure is according to RFC 826.
Definition: ARP.h:105
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
NETSIM_ID nApplicationId
Definition: Stack.h:752
EVENT_TYPE nEventType
Definition: Stack.h:747
NETSIM_ID nProtocolId
Definition: Stack.h:748
struct stru_NetSim_Packet * pPacket
Definition: Stack.h:754
NETSIM_ID nSubEventType
Definition: Stack.h:757
NETSIM_ID nDeviceId
Definition: Stack.h:750
long long int nPacketId
Definition: Stack.h:755
NETSIM_ID nInterfaceId
Definition: Stack.h:751
PNETSIM_MACADDRESS szDestMac
Definition: Packet.h:221
PNETSIM_MACADDRESS szSourceMac
Definition: Packet.h:220
NETWORK_LAYER_PROTOCOL nNetworkProtocol
Definition: Packet.h:204
NETSIM_IPAddress szDestIP
Definition: Packet.h:199
NETSIM_IPAddress szNextHopIp
Definition: Packet.h:201
NETSIM_IPAddress szSourceIP
Definition: Packet.h:198
struct stru_NetSim_Packet_NetworkLayer * pstruNetworkData
Definition: Packet.h:275
double dEventTime
Definition: Packet.h:262
NETSIM_ID nReceiverId
Definition: Packet.h:266
unsigned int nControlDataType
Definition: Packet.h:258
struct stru_NetSim_Packet * pstruNextPacket
Definition: Packet.h:278
NETSIM_ID nTransmitterId
Definition: Packet.h:265
PACKET_TYPE nPacketType
Definition: Packet.h:257
PACKET_PRIORITY nPacketPriority
Definition: Packet.h:260
NETSIM_ID nSourceId
Definition: Packet.h:263
struct stru_NetSim_Packet_MACLayer * pstruMacData
Definition: Packet.h:276
char str_ip[_NETSIM_IP_LEN]
Definition: IP_Addressing.h:54