NetSim Source Code Help
Loading...
Searching...
No Matches
ReadArpTable.c
Go to the documentation of this file.
1/************************************************************************************
2* Copyright (C) 2021 *
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
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
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 {
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 */
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 fn_NetSim_Add_PacketTo_Buffer(NETSIM_ID nDeviceId, NetSim_PACKET *pstruNewPacket, NETSIM_IPAddress szDestIPadd, NETSIM_ID nInterfaceId)
@ GENERATE_ARP_REQUEST
Definition: ARP.h:58
unsigned int NETSIM_ID
Definition: Animation.h:45
bool isBroadcastIP(NETSIM_IPAddress ip)
#define IP_COMPARE(ip1, ip2)
Definition: IP_Addressing.h:67
bool isMulticastIP(NETSIM_IPAddress ip)
bool fn_NetSim_GetBufferStatus(NetSim_BUFFER *pstruBuffer)
Definition: Scheduling.c:41
NETSIM_ID get_first_dest_from_packet(NetSim_PACKET *packet)
int fn_NetSim_Read_ARP_Table()
Definition: ReadArpTable.c:61
static PNETSIM_MACADDRESS read_table(ARP_VARIABLES *arp, NETSIM_IPAddress ip)
Definition: ReadArpTable.c:49
PNETSIM_MACADDRESS multicastIP_to_Mac(NETSIM_IPAddress multicastIP)
EXPORTED PNETSIM_MACADDRESS BROADCAST_MAC
Definition: Stack.h:393
MAC_LAYER_PROTOCOL fn_NetSim_Stack_GetMacProtocol(NETSIM_ID nDeviceId, NETSIM_ID nInterfaceId)
@ 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
#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_METRICS * pstruArpMetrics
NetSim specific ARP metrics structure.
Definition: ARP.h:157
ARP_TABLE * pstruArpTable
Definition: ARP.h:150
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
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
NETSIM_ID nInterfaceId
Definition: Stack.h:751
PNETSIM_MACADDRESS szDestMac
Definition: Packet.h:221
PNETSIM_MACADDRESS szSourceMac
Definition: Packet.h:220
NETSIM_IPAddress szNextHopIp
Definition: Packet.h:201
struct stru_NetSim_Packet_NetworkLayer * pstruNetworkData
Definition: Packet.h:275
struct stru_NetSim_Packet_MACLayer * pstruMacData
Definition: Packet.h:276