NetSim Source Code Help v14.4
All 13 Components
 
Loading...
Searching...
No Matches
ReadArpTable.c
1/************************************************************************************
2* Copyright (C) 2023 *
3* TETCOS, Bangalore. India *
4* *
5* Tetcos owns the intellectual property rights in the Product and its content. *
6* The copying, redistribution, reselling or publication of any or all of the *
7* Product or its content without express prior written consent of Tetcos is *
8* prohibited. Ownership and / or any other right relating to the software and all *
9* intellectual property rights therein shall remain at all times with Tetcos. *
10* *
11* This source code is licensed per the NetSim license agreement. *
12* *
13* No portion of this source code may be used as the basis for a derivative work, *
14* or used, for any purpose other than its intended use per the NetSim license *
15* agreement. *
16* *
17* This source code and the algorithms contained within it are confidential trade *
18* secrets of TETCOS and may not be used as the basis for any other software, *
19* hardware, product or service. *
20* *
21* Author: Shashi Kant Suman *
22* *
23* ----------------------------------------------------------------------------------*/
24
25#define _CRT_SECURE_NO_DEPRECATE
26#include "main.h"
27#include "ARP.h"
28/**
29~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
30Function: Read ARP Table
31When packet want to transmit the MAC OUT EVENT, the job of Address Resolution Protocol
32is to update the destination MAC address in the MAC data of the packet. To do that it
33follows the below steps.
341. Check the destination is in the same LAN. If yes, set the nDestinationId as well as
35 DestinationIP from the packet you received. if not,then set the nDestinationId ZERO
36 for the DefaultGateway and the DestinationIP as the GatewayIP.
372. Then check the DestinationIP entry present in the ARP table. If present,
38 set the nDestinationPresentFlag.
393 If nDestinationPresentFlag is set (ONE)then update the DestMAc of MACdata of packet
40 and forward to MAC layer by keeping the packet in the AccessBuffer of AccessInterface.
414. If nDestinationPresentFlag is not set and pnArpRequestFlag[nDestinationId](request flag
42 for the destination) is also not set, then buffer the packet and add event by adding
43 subevent type as GENERATE_ARP_REQUEST.
445. If nDestinationPresentFlag is not set pnArpRequestFlag[nDestinationId] is set, then just
45 buffer the packet.
46~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
47*/
48
49static PNETSIM_MACADDRESS read_table(ARP_VARIABLES* arp,NETSIM_IPAddress ip)
50{
51 ARP_TABLE* table = arp->pstruArpTable;
52 while (table)
53 {
54 if (!IP_COMPARE(table->szIPAddress, ip))
55 return table->szMACAddress;
56 table = table->pstruNextEntry;
57 }
58 return NULL;
59}
60
61int fn_NetSim_Read_ARP_Table()
62{
63 NETSIM_ID nDeviceId;
64 NETSIM_ID nInterfaceId, nDestinationId=0;
65 NETSIM_IPAddress szDestIPadd;
66 PNETSIM_MACADDRESS szDestMAC=NULL;
67 int nType;
68
69 NetSim_PACKET *pstruTemp_Data;// NetSim Temp packet to store the packet from the event details
70 ARP_TABLE *pstruTableHead, *pstruCurrentTable; //Type casting ARP table
71 ARP_VARIABLES *pstruArpVariables;//Type casting ARP variables
72
73 // Get packet from the event
74 pstruTemp_Data =pstruEventDetails->pPacket;
75
76 // Get the interface and device id from the event
77 nInterfaceId = pstruEventDetails->nInterfaceId;
78 nDeviceId = pstruEventDetails->nDeviceId;
79 szDestIPadd = pstruTemp_Data->pstruNetworkData->szNextHopIp;
80
81 if (isBroadcastIP(szDestIPadd))
82 {
83 szDestMAC = BROADCAST_MAC;
84 }
85 else if (isMulticastIP(szDestIPadd))
86 {
87 szDestMAC = multicastIP_to_Mac(szDestIPadd);
88 }
89 else
90 {
91 pstruArpVariables = DEVICE_INTERFACE(nDeviceId, nInterfaceId)->ipVar;
92 pstruTableHead = pstruArpVariables->pstruArpTable;
93
94 szDestMAC = read_table(pstruArpVariables, szDestIPadd);
95 }
96
97 // nDestinationId = pstruTemp_Data->nReceiverId;
98 // Corrected to resolve static ARP bug ID 3498 involving multiple applications
99 nDestinationId = get_first_dest_from_packet(pstruTemp_Data);
100
101 if(szDestMAC) //Destination MAC present,forward the packet
102 {
103 //Update the destination and Source MAC address in the MacData of the packet
104 pstruTemp_Data->pstruMacData->szDestMac = szDestMAC;
105 pstruTemp_Data->pstruMacData->szSourceMac = DEVICE_INTERFACE(nDeviceId,nInterfaceId)->pstruMACLayer->szMacAddress;
106
107 if(!fn_NetSim_GetBufferStatus(DEVICE_INTERFACE(nDeviceId,nInterfaceId)->pstruAccessInterface->pstruAccessBuffer))
108 {
109 pstruEventDetails->nProtocolId = fn_NetSim_Stack_GetMacProtocol(nDeviceId,nInterfaceId);
110 pstruEventDetails->dEventTime = pstruEventDetails->dEventTime;
111 pstruEventDetails->nSubEventType = 0;
112 pstruEventDetails->dPacketSize = pstruTemp_Data->pstruNetworkData->dPacketSize;
113 pstruEventDetails->nEventType=MAC_OUT_EVENT;
114 pstruEventDetails->pPacket=NULL;
115 fnpAddEvent( pstruEventDetails);
116 }
117 fn_NetSim_Packet_AddPacketToList(DEVICE_INTERFACE(nDeviceId,nInterfaceId)->pstruAccessInterface->pstruAccessBuffer,pstruTemp_Data,0);
118 }
119 /*check the request flag, ZERO for first time, then generate the request,and set the flag.
120 When the flag is ONE, the packets are buffered */
121 else if(pstruArpVariables->pnArpRequestFlag[nDestinationId]== 0)
122 {
123 /* Buffer the Packet*/
124 fn_NetSim_Add_PacketTo_Buffer(nDeviceId,pstruTemp_Data,szDestIPadd,nInterfaceId);
125 //Increment the buffered pkt count
126 pstruArpVariables->pstruArpMetrics->nPacketsInBuffer += 1;
127 /*Generate ARP Request */
128 pstruEventDetails->nSubEventType = GENERATE_ARP_REQUEST;
129 fnpAddEvent(pstruEventDetails);
130 pstruArpVariables->pnArpRequestFlag[nDestinationId]= 1;
131 }
132 else
133 { /* Buffer the Packet*/
134 fn_NetSim_Add_PacketTo_Buffer(nDeviceId,pstruTemp_Data,szDestIPadd,nInterfaceId);
135 //Increment the buffered pkt count
136 pstruArpVariables->pstruArpMetrics->nPacketsInBuffer += 1;
137 }
138
139 return 0;
140}
141
int * pnArpRequestFlag
Set when generate Request.
Definition ARP.h:153
ARP_METRICS * pstruArpMetrics
NetSim specific ARP metrics structure.
Definition ARP.h:157
int nPacketsInBuffer
Number of packets in the buffer.
Definition ARP.h:143
PNETSIM_MACADDRESS szMACAddress
MAC address or Hardware address of the device.
Definition ARP.h:125
struct stru_ARP_Table * pstruNextEntry
Next entry pointer.
Definition ARP.h:127
NETSIM_IPAddress szIPAddress
IP address of the deivce.
Definition ARP.h:124