NetSim Source Code Help v14.4
All 13 Components
 
Loading...
Searching...
No Matches
UpdateArpTable.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/*********************************************************************************
16Function: Update Arp Table and Farward Packet
17This function is called whenever the NETWORK_IN_EVENT triggred with the control
18packet as REPLY_PACKET. That is ARP reply is received. Then call this function to
19update the MAC address and forward the buffred packet. Do the following,
201.Update the ARP table by updating the destination IP and MAC address
212.Take the buffered packet, update the destination MAC address and add the 20 byte IP header.
223.Add the packet to access buffer to forward the packet to next layer.
23*********************************************************************************/
24#define _CRT_SECURE_NO_DEPRECATE
25#include "main.h"
26#include "ARP.h"
27/**
28~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
29Function: Update Arp Table and Farward Packet
30This function is called whenever the NETWORK_IN_EVENT triggred with the control
31packet as REPLY_PACKET. That is ARP reply is received. Then call this function to
32update the MAC address and forward the buffred packet. Do the following,
331.Update the ARP table by updating the destination IP and MAC address
342.Take the buffered packet, update the destination MAC address and add the 20 byte IP header.
353.Add the packet to access buffer to forward the packet to next layer.
36~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
37*/
38int fn_NetSim_Update_ARP_Table_ForwardPacket(NetSim_EVENTDETAILS *pstruEventDetails, struct stru_NetSim_Network *NETWORK)
39{
40 NETSIM_ID nDeviceId;
41 NETSIM_ID nInterfaceId;
42 NETSIM_ID nDestinationId;
43 int nType;
44 PNETSIM_MACADDRESS szTempMAC;
45 NETSIM_IPAddress szSrcIPadd;
46 NETSIM_IPAddress szDestIPadd;
47
48 NetSim_PACKET *pstruTemp_Data, *pstruTempBufferData;
49 ARP_PACKET *pstruPacketArp;
50 ARP_TABLE *pstruTableHead;
51 ARP_VARIABLES *pstruArpVariables;
52 ARP_BUFFER *pstruPacketBuffer, *pstruPrevBuffer,*tempBuffer=NULL;
53
54 // Get the device and interface Id from the event
55 nInterfaceId = pstruEventDetails->nInterfaceId;
56 nDeviceId = pstruEventDetails->nDeviceId;
57 //Update the ARP table with destination MAC and IP address
58 pstruArpVariables = DEVICE_INTERFACE(nDeviceId,nInterfaceId)->ipVar;
59 pstruTableHead = pstruArpVariables->pstruArpTable;
60 //Get the packet from the event
61 pstruTemp_Data =pstruEventDetails->pPacket;
62 // Get the Arp Packet from the NetworkData of temp packet
63 pstruPacketArp = pstruTemp_Data->pstruNetworkData->Packet_NetworkProtocol;
64 szSrcIPadd = IP_COPY(pstruPacketArp->sz_ar$spa);
65 szTempMAC = pstruPacketArp->sz_ar$sha;
66 nType = DYNAMIC;
67 // call the function to update the table entry by adding the destination MAC
68 fn_NetSim_Add_IP_MAC_AddressTo_ARP_Table(&pstruTableHead,szSrcIPadd,szTempMAC,nType);
69
70 // Write to Log file
71 szDestIPadd = pstruPacketArp->sz_ar$tpa;
72 printf("ARP---ARP_ReplyGot\tEventTime:%0.3lf\tSrcIP:%s\tDestIP:%s\tMAC_Add:%s\n",pstruEventDetails->dEventTime,
73 szDestIPadd->str_ip,
74 szSrcIPadd->str_ip,
75 szTempMAC->szmacaddress);
76
77 // Get the device id from the event
78 nDeviceId = pstruEventDetails->nDeviceId;
79 pstruPacketBuffer = pstruArpVariables->pstruPacketBuffer;
80 pstruPrevBuffer = pstruArpVariables->pstruPacketBuffer;
81 while(pstruPacketBuffer)
82 {
83 if((IP_COMPARE(pstruPacketBuffer->szDestAdd,szSrcIPadd) == 0))
84 {
85 pstruTempBufferData = pstruPacketBuffer->pstruPacket;
86 pstruArpVariables->pstruArpMetrics->nPacketsInBuffer -= 1;
87 //Update the destination and Source MAC adress in the MacData of the packet
88 pstruTempBufferData->pstruMacData->szDestMac = szTempMAC;
89 nDeviceId = pstruEventDetails->nDeviceId;
90 pstruTempBufferData->pstruMacData->szSourceMac = DEVICE_INTERFACE(nDeviceId,nInterfaceId)->pstruMACLayer->szMacAddress;
91
92 if(!fn_NetSim_GetBufferStatus(DEVICE_INTERFACE(nDeviceId,nInterfaceId)->pstruAccessInterface->pstruAccessBuffer))
93 {
94 pstruEventDetails->nSubEventType = 0;
95 pstruEventDetails->dPacketSize = pstruTempBufferData->pstruNetworkData->dPacketSize;
96 if(pstruTempBufferData->pstruAppData)
97 {
98 pstruEventDetails->nApplicationId = pstruTempBufferData->pstruAppData->nApplicationId;
99 pstruEventDetails->nSegmentId = pstruTempBufferData->pstruAppData->nSegmentId;
100 }
101 else
102 {
103 pstruEventDetails->nApplicationId = 0;
104 pstruEventDetails->nSegmentId = 0;
105 }
106 pstruEventDetails->nInterfaceId = nInterfaceId; //InterfaceId from the buffer
107 pstruEventDetails->nProtocolId = fn_NetSim_Stack_GetMacProtocol(nDeviceId,nInterfaceId);
108 pstruEventDetails->nPacketId = pstruTempBufferData->nPacketId;
109 pstruEventDetails->nEventType=MAC_OUT_EVENT;
110 pstruEventDetails->pPacket = NULL;
111 fnpAddEvent( pstruEventDetails);
112 }
113 fn_NetSim_Packet_AddPacketToList(DEVICE_INTERFACE(nDeviceId,nInterfaceId)->pstruAccessInterface->pstruAccessBuffer,pstruTempBufferData,0);//0);
114
115 if(pstruArpVariables->pstruPacketBuffer == pstruPacketBuffer)
116 pstruArpVariables->pstruPacketBuffer = pstruPacketBuffer->pstruNextBuffer;
117 pstruPacketBuffer = pstruPacketBuffer->pstruNextBuffer;
118 IP_FREE(pstruPrevBuffer->szDestAdd);
119 fnpFreeMemory(pstruPrevBuffer);
120 if(tempBuffer)
121 tempBuffer->pstruNextBuffer = pstruPacketBuffer;
122 pstruPrevBuffer = pstruPacketBuffer;
123 continue;
124
125 }
126 tempBuffer = pstruPrevBuffer;
127 pstruPacketBuffer=pstruPacketBuffer->pstruNextBuffer;
128 pstruPrevBuffer = pstruPacketBuffer;
129 }
130
131 nDestinationId = pstruTemp_Data->nSourceId;
132
133 pstruArpVariables->pnArpReplyFlag[nDestinationId] = 1; // Set Reply flag
134 pstruArpVariables->pnArpRequestFlag[nDestinationId] = 0; //Reset the Request flag
135 // For Metrics
136 //Increament the Reply Received count
137 pstruArpVariables->pstruArpMetrics->nArpReplyReceivedCount+= 1;
138 IP_FREE(szSrcIPadd);
139 // Free the reply packet
140 fn_NetSim_Packet_FreePacket(pstruTemp_Data);
141 pstruTemp_Data = NULL;
142 pstruEventDetails->pPacket = NULL;
143
144 return 0;
145}
146
int * pnArpRequestFlag
Set when generate Request.
Definition ARP.h:153
ARP_METRICS * pstruArpMetrics
NetSim specific ARP metrics structure.
Definition ARP.h:157
int * pnArpReplyFlag
Set when receive the Reply.
Definition ARP.h:154
int nPacketsInBuffer
Number of packets in the buffer.
Definition ARP.h:143
int nArpReplyReceivedCount
Number of replies received from destination.
Definition ARP.h:141
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
NETSIM_IPAddress szDestAdd
Store the destination IP address.
Definition ARP.h:132
NetSim_PACKET * pstruPacket
Store the packet.
Definition ARP.h:134