NetSim Source Code Help
Loading...
Searching...
No Matches
GenerateArpReply.c
Go to the documentation of this file.
1/************************************************************************************
2 * Copyright (C) 2020
3 *
4 * TETCOS, Bangalore. India *
5
6 * Tetcos owns the intellectual property rights in the Product and its content. *
7 * The copying, redistribution, reselling or publication of any or all of the *
8 * Product or its content without express prior written consent of Tetcos is *
9 * prohibited. Ownership and / or any other right relating to the software and all *
10 * intellectual property rights therein shall remain at all times with Tetcos. *
11
12 * Author: Basamma YB
13 * Date :
14 ************************************************************************************/
15#define _CRT_SECURE_NO_DEPRECATE
16#include "main.h"
17#include "ARP.h"
18/**
19~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
20This function is called whenever the NETWORK_IN_EVENT triggred with the control
21packet as REQUEST_PACKET. That is ARP request is received. Then call this
22function to generate REPLY_PACKET. Do the following to generate Reply
231.Check for the hardware type, protocol type,MAC address lenth, IP adress length
24 are correct matching.
252. Check the Source IP and MAC entry present in the table, if not update the table.
263. Check the packet received device is the destination, if not, dicard the packet,
27 else Generate the REPLY_PACKET by updating its MAC address.
284.Add the control packet to access buffer to farward the packet to next layer.
29~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
30*/
32//_declspec (dllexport) int fn_NetSim_Generate_ARP_Reply()
33{
34 NETSIM_ID nDeviceId;
35 NETSIM_ID nInterfaceId;
36 int nMerge_flag = 0;
37 int nType;
38 unsigned int unProtocolId;
39 NETSIM_IPAddress szSrcIPadd;
40 NETSIM_IPAddress szDestIPadd;
41 PNETSIM_MACADDRESS szTargetMAC;
42
43 NetSim_PACKET *pstruTemp_Data; // NetSim Temp packet to store the packet from the event details
44 NetSim_PACKET *pstruControlPacket; // NetSim packet To store the ARP packet
45 ARP_PACKET *pstruArpRequestPkt; // ARP PACKET to store ARP Request pcaket
46 ARP_PACKET *pstruArpReplyPkt; // ARP PACKET to store ARP Reply pcaket
47 ARP_TABLE *pstruTableHead,*pstruCurrentTable; //Type casting Arp Table
48 ARP_VARIABLES *pstruArpVariables; //type casting Arp variables
49
50 //Get the packet from the event
51 pstruTemp_Data =pstruEventDetails->pPacket;
52 pstruControlPacket = pstruEventDetails->pPacket;
53 // Get the Arp Packet from the NetworkData of temp packet
54 pstruArpRequestPkt = pstruTemp_Data->pstruNetworkData->Packet_NetworkProtocol;
55 // Get the device and interface Id from the event
56 nInterfaceId = pstruEventDetails->nInterfaceId;
57 nDeviceId = pstruEventDetails->nDeviceId;
58
59 if(pstruArpRequestPkt->n_ar$hrd != IEEE802)
60 fnNetSimError("Mismatch in ARP Hardware Type");
61 if(pstruArpRequestPkt->usn_ar$hln != HARDWARE_ADDRESS_LENGTH)
62 fnNetSimError("Mismatch in ARP Hardware/MAC Adress Length ");
63 if(pstruArpRequestPkt->n_ar$pro != ARP_TO_RESOLVE_IP)
64 fnNetSimError("Mismatch in ARP Protocol Type");
65 unProtocolId = fn_NetSim_Stack_GetNWProtocol(nDeviceId);
66 if(unProtocolId == NW_PROTOCOL_IPV4)
67 {
68 if(pstruArpRequestPkt->usn_ar$pln != IPV4_PROTOCOL_ADDREES_LENGTH)
69 fnNetSimError("Mismatch in ARP Protocol IPV4 Adress Length ");
70 }
71 else
72 {
73 if(pstruArpRequestPkt->usn_ar$pln != IPV6_PROTOCOL_ADDREES_LENGTH)
74 fnNetSimError("Mismatch in ARP Protocol IPV6 Adress Length ");
75 }
76 // Get the Arp variables from device ipVar
77 pstruArpVariables = DEVICE_INTERFACE(nDeviceId,nInterfaceId)->ipVar;
78 pstruTableHead = pstruArpVariables->pstruArpTable;
79 pstruCurrentTable = pstruTableHead;
80 // Get the source IP address from the request packet
81 szSrcIPadd = pstruArpRequestPkt->sz_ar$spa;//_strdup(pstruArpRequestPkt->sz_ar$spa);
82 // Check the Source entry in the ARP TABLE
83 while(pstruCurrentTable != NULL)
84 {
85 if(IP_COMPARE(pstruCurrentTable->szIPAddress,szSrcIPadd)== 0)
86 { // set the merge flag if entry exists
87 nMerge_flag = 1;
88 break;
89 }
90 else
91 pstruCurrentTable = pstruCurrentTable->pstruNextEntry;
92 }
93 // Source entry not there in the ARP table, update the table.
94 if(nMerge_flag == 0)
95 {
96 nType = DYNAMIC;
97 fn_NetSim_Add_IP_MAC_AddressTo_ARP_Table(&pstruTableHead,pstruArpRequestPkt->sz_ar$spa,pstruArpRequestPkt->sz_ar$sha,nType);
98 pstruArpVariables->pstruArpTable = pstruTableHead;
99 nMerge_flag =1;
100 }
101 /* Generate ARP Reply */
102 // Check for REQUEST, and send the REPLY PACKET
103 if(pstruArpRequestPkt->n_ar$op == ares_opSREQUEST)
104 {
105 //Allocate memory for the ARP Reply packet
106 pstruArpReplyPkt = fnpAllocateMemory(1,sizeof(ARP_PACKET));
107 //Swap IP and MAC of source and destinations from the Request packet
108 pstruArpReplyPkt->sz_ar$spa = IP_COPY(pstruArpRequestPkt->sz_ar$tpa);
109 pstruArpReplyPkt->sz_ar$sha = DEVICE_INTERFACE(nDeviceId,nInterfaceId)->pstruMACLayer->szMacAddress;
110 pstruArpReplyPkt->sz_ar$tpa = IP_COPY(pstruArpRequestPkt->sz_ar$spa);
111 pstruArpReplyPkt->sz_ar$tha = pstruArpRequestPkt->sz_ar$sha;
112 pstruArpReplyPkt->n_ar$op = ares_opSREPLY; // Set Opcode as REPLY
113 //Set the Ethernet header details
114 pstruArpReplyPkt->szDestMac = pstruArpRequestPkt->sz_ar$sha;
115 pstruArpReplyPkt->szSrcMac = (DEVICE_INTERFACE(nDeviceId,nInterfaceId)->pstruMACLayer->szMacAddress);
116 pstruArpReplyPkt->nEther_type = ADDRESS_RESOLUTION;
117 //Add other relevent fields
118 pstruArpReplyPkt->n_ar$hrd = IEEE802; // H/W type
119 pstruArpReplyPkt->n_ar$pro = ARP_TO_RESOLVE_IP; // Protocol type
120 pstruArpReplyPkt->usn_ar$hln = HARDWARE_ADDRESS_LENGTH; // MAC add length in bytes
121 // Create netsim packet for NETWORK_LAYER
122 pstruControlPacket = fn_NetSim_Packet_CreatePacket(NETWORK_LAYER);
123 //Generate the control packet
124 pstruControlPacket->dEventTime = pstruEventDetails->dEventTime;
126 pstruControlPacket->nPacketType = PacketType_Control;
127 pstruControlPacket->nControlDataType = REPLY_PACKET;
128 pstruControlPacket->nTransmitterId=pstruEventDetails->nDeviceId;
129 pstruControlPacket->nReceiverId = pstruEventDetails->pPacket->nTransmitterId;//0;
130 pstruControlPacket->nSourceId = pstruEventDetails->nDeviceId;
131 //Update IP address in the packet
132 pstruControlPacket->pstruNetworkData->szSourceIP = IP_COPY(DEVICE_NWADDRESS(nDeviceId,nInterfaceId));
133 pstruControlPacket->pstruNetworkData->szDestIP = IP_COPY(pstruArpRequestPkt->sz_ar$spa);
134 //Update MAC address in the packet
135 pstruControlPacket->pstruMacData->szSourceMac = DEVICE_INTERFACE(nDeviceId,nInterfaceId)->pstruMACLayer->szMacAddress;
136 pstruControlPacket->pstruMacData->szDestMac = pstruArpRequestPkt->sz_ar$sha;
137 //Update NetworkData packet timings
141 pstruControlPacket->nPacketPriority = Priority_High;
142 //Add overheads and size to NetworkData of the packet
143 unProtocolId = fn_NetSim_Stack_GetNWProtocol(nDeviceId);
144 if(unProtocolId == NW_PROTOCOL_IPV4)
145 {
146 pstruArpReplyPkt->usn_ar$pln = IPV4_PROTOCOL_ADDREES_LENGTH; // IP add length in bytes
149 }
150 else
151 {
152 pstruArpReplyPkt->usn_ar$pln = IPV6_PROTOCOL_ADDREES_LENGTH; // IP add length in bytes
154 pstruControlPacket->pstruNetworkData->dOverhead = IPV6_NETWORK_OVERHEADS;
155 }
156 pstruControlPacket->pstruNetworkData->dPacketSize=pstruControlPacket->pstruNetworkData->dPayload + pstruControlPacket->pstruNetworkData->dOverhead;
157 // Assign Protocol Id
159 // Assign ARP Reply packet to NetworkProtocolPacket
160 pstruControlPacket->pstruNetworkData->Packet_NetworkProtocol = pstruArpReplyPkt;
161 pstruControlPacket->pstruNextPacket=NULL;
162
163 // Get buffer status
164 if(!fn_NetSim_GetBufferStatus(DEVICE_INTERFACE(nDeviceId,nInterfaceId)->pstruAccessInterface->pstruAccessBuffer))
165 {
170 pstruEventDetails->pPacket = NULL;
172 }
173 //Add packet to mac buffer
174 fn_NetSim_Packet_AddPacketToList(DEVICE_INTERFACE(nDeviceId,nInterfaceId)->pstruAccessInterface->pstruAccessBuffer,pstruControlPacket,0);
175
176 // Increment the reply sent count
177 pstruArpVariables->pstruArpMetrics->nArpReplySentCount += 1;
178
179 // Write to LogFile
180 szDestIPadd = DEVICE_NWADDRESS(nDeviceId,nInterfaceId);
181 szTargetMAC = pstruArpReplyPkt->sz_ar$sha;
182 printf("ARP---ARP_ReplySent\tEventTime:%0.3lf\tSrcIP:%s\tMAC_Add:%s\tDestIP:%s \n",
184 szDestIPadd->str_ip,
185 szTargetMAC->szmacaddress,
186 szSrcIPadd->str_ip);
187
188 }
189 else
190 {
191 fnNetSimError("Error in ARP REQEUST. This function should never be called");
192 }
193 // After sending the reply drop the request packet
194 fn_NetSim_Packet_FreePacket(pstruTemp_Data);
195 pstruTemp_Data = NULL;
196 pstruEventDetails->pPacket = NULL;
197 return 0;
198}
199
200
201
#define IPV4_PROTOCOL_ADDREES_LENGTH
Length of 32 bit IP address in bytes.
Definition: ARP.h:26
int fn_NetSim_Add_IP_MAC_AddressTo_ARP_Table(ARP_TABLE **, NETSIM_IPAddress, PNETSIM_MACADDRESS, int)
Function to add the new entry to the ARP_TABLE(IP add, MAC add and Type)
#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
@ REPLY_PACKET
Definition: ARP.h:89
#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
@ DYNAMIC
Definition: ARP.h:95
@ ares_opSREQUEST
Definition: ARP.h:66
@ ares_opSREPLY
Definition: ARP.h:67
@ 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_Reply(NetSim_EVENTDETAILS *pstruEventDetails, struct stru_NetSim_Network *NETWORK)
NETSIM_IPAddress IP_COPY(NETSIM_IPAddress ip)
#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
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)
#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
@ 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_CreatePacket(layer)
Definition: main.h:186
#define fn_NetSim_Packet_FreePacket(pstruPacket)
Definition: main.h:177
#define fnpAddEvent(pstruEvent)
Definition: main.h:191
#define fn_NetSim_Packet_AddPacketToList(pstruBuffer, pstruPacket, nInsertionType)
Definition: main.h:179
ARP_METRICS * pstruArpMetrics
NetSim specific ARP metrics structure.
Definition: ARP.h:157
ARP_TABLE * pstruArpTable
Definition: ARP.h:150
int nArpReplySentCount
Number of replies sent from the destination.
Definition: ARP.h:142
struct stru_ARP_Table * pstruNextEntry
Next entry pointer.
Definition: ARP.h:127
NETSIM_IPAddress szIPAddress
IP address of the deivce
Definition: ARP.h:124
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
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
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 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
char szmacaddress[13]
Definition: Stack.h:389