NetSim Source Code Help v14.4
All 13 Components
 
Loading...
Searching...
No Matches
GenerateArpReply.c
1/************************************************************************************
2 * Copyright (C) 2023
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*/
31int fn_NetSim_Generate_ARP_Reply(NetSim_EVENTDETAILS *pstruEventDetails, struct stru_NetSim_Network *NETWORK)
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;
125 add_dest_to_packet(pstruControlPacket, pstruEventDetails->pPacket->nSourceId);
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
138 pstruControlPacket->pstruNetworkData->dArrivalTime = pstruEventDetails->dEventTime;
139 pstruControlPacket->pstruNetworkData->dEndTime = pstruEventDetails->dEventTime;
140 pstruControlPacket->pstruNetworkData->dStartTime = pstruEventDetails->dEventTime;
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
147 pstruControlPacket->pstruNetworkData->dPayload = IPV4_ARP_PACKET_SIZE_WITH_ETH_HEADER;
148 pstruControlPacket->pstruNetworkData->dOverhead =IPV4_NETWORK_OVERHEADS;
149 }
150 else
151 {
152 pstruArpReplyPkt->usn_ar$pln = IPV6_PROTOCOL_ADDREES_LENGTH; // IP add length in bytes
153 pstruControlPacket->pstruNetworkData->dPayload = IPV6_ARP_PACKET_SIZE_WITH_ETH_HEADER;
154 pstruControlPacket->pstruNetworkData->dOverhead = IPV6_NETWORK_OVERHEADS;
155 }
156 pstruControlPacket->pstruNetworkData->dPacketSize=pstruControlPacket->pstruNetworkData->dPayload + pstruControlPacket->pstruNetworkData->dOverhead;
157 // Assign Protocol Id
158 pstruControlPacket->pstruNetworkData->nNetworkProtocol= NW_PROTOCOL_ARP;
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 {
166 pstruEventDetails->nSubEventType = 0;
167 pstruEventDetails->dPacketSize = pstruControlPacket->pstruNetworkData->dPacketSize;
168 pstruEventDetails->nProtocolId = fn_NetSim_Stack_GetMacProtocol(nDeviceId,nInterfaceId);
169 pstruEventDetails->nEventType=MAC_OUT_EVENT;
170 pstruEventDetails->pPacket = NULL;
171 fnpAddEvent( pstruEventDetails);
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",
183 pstruEventDetails->dEventTime,
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
ARP_METRICS * pstruArpMetrics
NetSim specific ARP metrics structure.
Definition ARP.h:157
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
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