NetSim Source Code Help v14.4
All 13 Components
 
Loading...
Searching...
No Matches
Arp.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 : 23-April-2012
14 ************************************************************************************/
15#define _CRT_SECURE_NO_DEPRECATE
16#include "main.h"
17#include "ARP.h"
18/**
19 This function is called by NetworkStack.dll, whenever the event gets triggered
20 inside the NetworkStack.dll for the ARP protocol
21*/
22_declspec (dllexport) int fn_NetSim_ARP_Run()
23{
24 SUB_EVENT nSub_Event_Type;
25 int nDestionation;
26 /* Get the Event and subevent Type from Event details */
27 nEventType=pstruEventDetails->nEventType;
28 nSub_Event_Type = pstruEventDetails->nSubEventType;
29
30 switch(nEventType) //Check event type
31 {
32 case NETWORK_OUT_EVENT:
33 switch(nSub_Event_Type) //Check the subevent type
34 {
35 case GENERATE_ARP_REQUEST: // Generate ARP Request
36 fn_NetSim_Generate_ARP_Request(pstruEventDetails,NETWORK);
37 break;
38 default: // READ_ARP_TABLE:
39 fn_NetSim_Read_ARP_Table(pstruEventDetails,NETWORK);
40 break;
41 }
42 break;
43
44 case TIMER_EVENT:
45 switch(nSub_Event_Type)
46 {
47 case ARP_REQUEST_TIMEOUT:// Request Time out event
48 fn_NetSim_ARP_Request_Timeout(pstruEventDetails,NETWORK);
49 break;
50 }
51 break;
52
53 case NETWORK_IN_EVENT:
54 switch(nSub_Event_Type)
55 {
56 case GENERATE_ARP_REPLY: // To generate ARP Reply
57
58 fn_NetSim_Generate_ARP_Reply(pstruEventDetails,NETWORK);
59 break;
60 case UPDATE_ARP_TABLE_FWD_PKT: // To Forward the buffered packet
61 fn_NetSim_Update_ARP_Table_ForwardPacket(pstruEventDetails,NETWORK);
62 break;
63 default:
64 switch(pstruEventDetails->pPacket->nControlDataType)//Check the Controlpacket type
65 {
66 case REQUEST_PACKET: // Add this subevent to generate reply packet
67 nDestionation = fn_Netsim_ARP_CheckDestinationDevice(pstruEventDetails,NETWORK);
68 if(nDestionation) // Destined device only generate reply
69 {
70 pstruEventDetails->nSubEventType = GENERATE_ARP_REPLY;
71 fnpAddEvent(pstruEventDetails);
72 }
73 pstruEventDetails->pPacket = NULL;
74 break;
75 case REPLY_PACKET: // Add this subevent to forward the buffered packet
76 pstruEventDetails->nSubEventType = UPDATE_ARP_TABLE_FWD_PKT;
77 fnpAddEvent(pstruEventDetails);
78 pstruEventDetails->pPacket = NULL;
79 break;
80 default: // For other than ARP control packet.
81 //Do nothing.
82 break;
83 }
84 break;
85 }
86 break;
87 default:
88 fnNetSimError("Unknown event type for ARP protocol");
89 break;
90 }
91
92 return 0;
93}
94/**
95 The Following functions are present in the Arp.lib.
96 so NetworkStack.dll can not call Arp.lib. It is calling Arp.dll.
97 From Arp.dll, Arp.lib functions are called.
98*/
99_declspec (dllexport) int fn_NetSim_ARP_Init(struct stru_NetSim_Network *NETWORK_Formal,\
100 NetSim_EVENTDETAILS *pstruEventDetails_Formal,char *pszAppPath_Formal,\
101 char *pszWritePath_Formal,int nVersion_Type,void **fnPointer)
102{
103 fn_NetSim_ARP_Init_F(NETWORK_Formal,pstruEventDetails_Formal,pszAppPath_Formal,\
104 pszWritePath_Formal,nVersion_Type,fnPointer);
105 return 0;
106}
107/**
108 This function is called by NetworkStack.dll, once simulation end to free the
109 allocated memory for the network.
110*/
111_declspec(dllexport) int fn_NetSim_ARP_Finish()
112{
113 fn_NetSim_ARP_Finish_F();
114 return 0;
115}
116/**
117 This function is called by NetworkStack.dll, while writing the evnt trace
118 to get the sub event as a string.
119*/
120_declspec (dllexport) char *fn_NetSim_ARP_Trace(int nSubEvent)
121{
122 return (fn_NetSim_ARP_Trace_F(nSubEvent));
123}
124/**
125 This function is called by NetworkStack.dll, while configuring the device
126 for ARP protocol.
127*/
128_declspec(dllexport) int fn_NetSim_ARP_Configure(void** var)
129{
130 return fn_NetSim_ARP_Configure_F(var);
131}
132/**
133 This function is called by NetworkStack.dll, to free the ARP protocol.
134*/
135_declspec(dllexport) int fn_NetSim_ARP_FreePacket(NetSim_PACKET* pstruPacket)
136{
137 return fn_NetSim_ARP_FreePacket_F(pstruPacket);
138}
139/**
140 This function is called by NetworkStack.dll, to copy the ARP protocol
141 details from source packet to destination.
142*/
143_declspec(dllexport) int fn_NetSim_ARP_CopyPacket(NetSim_PACKET* pstruDestPacket,NetSim_PACKET* pstruSrcPacket)
144{
145 return fn_NetSim_ARP_CopyPacket_F(pstruDestPacket,pstruSrcPacket);
146}
147/**
148This function write the Metrics
149*/
150_declspec(dllexport) int fn_NetSim_ARP_Metrics(PMETRICSWRITER writer)
151{
152 return 0;
153}
154/**
155This function will return the string to write packet trace heading.
156*/
157_declspec(dllexport) char* fn_NetSim_ARP_ConfigPacketTrace()
158{
159 return "";
160}
161/**
162 This function will return the string to write packet trace.
163*/
164_declspec(dllexport) char* fn_NetSim_ARP_WritePacketTrace(NetSim_PACKET* pstruPacket, char** ppszTrace)
165{
166 return "NULL";
167}
168
169
170
171/**
172 This function is called for NETWORK_IN_EVENT with packet type = REQUEST_PACKET.
173 Check the request packet received device is the destination. If so it add the
174 GENERATE ARP REPLY sub event, else update the table and drop the request packet.
175*/
176int fn_Netsim_ARP_CheckDestinationDevice(NetSim_EVENTDETAILS *pstruEventDetails, struct stru_NetSim_Network *NETWORK)
177{
178 NETSIM_ID nDeviceId;
179 NETSIM_ID nInterfaceId;
180 int nMerge_flag = 0;
181 int nType;
182 NETSIM_IPAddress szSrcIPadd;
183 NetSim_PACKET *pstruTemp_Data; // To store the packet from the event details
184 ARP_PACKET *pstruArpRequestPkt; // ARP PACKET to store ARP pcaket data
185 ARP_TABLE *pstruTableHead,*pstruCurrentTable; //Type casting Arp Table and variables
186 ARP_VARIABLES *pstruArpVariables; //Type casting Arp and variables
187
188 //Get the packet from the event
189 pstruTemp_Data =pstruEventDetails->pPacket;
190 // Get the device and interface Id from the event
191 nInterfaceId = pstruEventDetails->nInterfaceId;
192 nDeviceId = pstruEventDetails->nDeviceId;
193 // Get the Arp Packet from the NetworkData of temp packet
194 pstruArpRequestPkt = pstruTemp_Data->pstruNetworkData->Packet_NetworkProtocol;
195 // check for destination, if so Add the GENERATE ARP REPLY subevent, else drop the packet
196 if(IP_COMPARE(pstruArpRequestPkt->sz_ar$tpa,DEVICE_NWADDRESS(nDeviceId,nInterfaceId)) == 0)
197 {
198 return 1;
199 }
200 else // Update ARP table and drop the packet
201 {
202 // Get the Arp variables from device ipVar
203 pstruArpVariables = DEVICE_INTERFACE(nDeviceId,nInterfaceId)->ipVar;
204 pstruTableHead = pstruArpVariables->pstruArpTable;
205 pstruCurrentTable = pstruTableHead;
206 // Get the source IP address from the packet
207 szSrcIPadd = IP_COPY(pstruArpRequestPkt->sz_ar$spa);
208 // Check the Source entry in the ARP TABLE
209 while(pstruCurrentTable != NULL)
210 {
211 if(IP_COMPARE(pstruCurrentTable->szIPAddress,szSrcIPadd)== 0)
212 { // set the merge flag if entry exists
213 nMerge_flag = 1;
214 break;
215 }
216 else //
217 pstruCurrentTable = pstruCurrentTable->pstruNextEntry;
218 }
219 // Source entry not there in the ARP table, update the table.
220 if(nMerge_flag == 0)
221 {
222 nType = DYNAMIC;
223 fn_NetSim_Add_IP_MAC_AddressTo_ARP_Table(&pstruTableHead,pstruArpRequestPkt->sz_ar$spa,pstruArpRequestPkt->sz_ar$sha,nType);
224 pstruArpVariables->pstruArpTable = pstruTableHead;
225 nMerge_flag =1;
226 }
227 IP_FREE(szSrcIPadd);
228 //Drop the packet
229 fn_NetSim_Packet_FreePacket(pstruTemp_Data);
230 pstruTemp_Data = NULL;
231 pstruEventDetails->pPacket = NULL;
232 return 0;
233 }
234}
235
236
struct stru_ARP_Table * pstruNextEntry
Next entry pointer.
Definition ARP.h:127
NETSIM_IPAddress szIPAddress
IP address of the deivce.
Definition ARP.h:124
NETSIM_IPAddress sz_ar$spa
Protocol address of the sender.
Definition ARP.h:116
PNETSIM_MACADDRESS sz_ar$sha
Hardware address of the sender.
Definition ARP.h:115
NETSIM_IPAddress sz_ar$tpa
Protocol address of target.
Definition ARP.h:118