NetSim Source Code Help
Loading...
Searching...
No Matches
UpdateArpTable.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/*********************************************************************************
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*/
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 {
96 if(pstruTempBufferData->pstruAppData)
97 {
99 pstruEventDetails->nSegmentId = pstruTempBufferData->pstruAppData->nSegmentId;
100 }
101 else
102 {
105 }
106 pstruEventDetails->nInterfaceId = nInterfaceId; //InterfaceId from the buffer
108 pstruEventDetails->nPacketId = pstruTempBufferData->nPacketId;
110 pstruEventDetails->pPacket = NULL;
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;
143
144 return 0;
145}
146
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)
@ DYNAMIC
Definition: ARP.h:95
unsigned int NETSIM_ID
Definition: Animation.h:45
NETSIM_IPAddress IP_COPY(NETSIM_IPAddress ip)
void IP_FREE(NETSIM_IPAddress ip)
#define IP_COMPARE(ip1, ip2)
Definition: IP_Addressing.h:67
#define fnpFreeMemory(p)
Definition: Memory.h:36
bool fn_NetSim_GetBufferStatus(NetSim_BUFFER *pstruBuffer)
Definition: Scheduling.c:41
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
EXPORTED struct stru_NetSim_EventDetails * pstruEventDetails
Definition: Stack.h:837
#define DEVICE_INTERFACE(DeviceId, InterfaceId)
Definition: Stack.h:780
int fn_NetSim_Update_ARP_Table_ForwardPacket(NetSim_EVENTDETAILS *pstruEventDetails, struct stru_NetSim_Network *NETWORK)
#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
int * pnArpRequestFlag
Set when generate Request.
Definition: ARP.h:153
ARP_BUFFER * pstruPacketBuffer
Definition: ARP.h:156
ARP_METRICS * pstruArpMetrics
NetSim specific ARP metrics structure.
Definition: ARP.h:157
ARP_TABLE * pstruArpTable
Definition: ARP.h:150
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
This Arp packet structure is according to RFC 826.
Definition: ARP.h:105
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
struct stru_ArpDataPacket_Buffer * pstruNextBuffer
Definition: ARP.h:135
NETSIM_IPAddress szDestAdd
Store the destination IP address.
Definition: ARP.h:132
NetSim_PACKET * pstruPacket
Store the packet
Definition: ARP.h:134
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
long long int nPacketId
Definition: Packet.h:256
struct stru_NetSim_Packet_AppLayer * pstruAppData
Definition: Packet.h:273
struct stru_NetSim_Packet_NetworkLayer * pstruNetworkData
Definition: Packet.h:275
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