NetSim Source Code Help v14.4
All 13 Components
 
Loading...
Searching...
No Matches
RIP.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* Author: Thangarasu *
12* *
13* ---------------------------------------------------------------------------------*/
14#define _CRT_SECURE_NO_DEPRECATE
15#include "main.h"
16#include "Routing.h"
17/**
18 This function is called by NetworkStack.dll, while configuring the device
19 Application layer for RIP protocol.
20*/
21_declspec(dllexport)int fn_NetSim_RIP_Configure(void** var)
22{
23 return fn_NetSim_RIP_Configure_F(var);
24}
25/**
26 This functon initializes the RIP parameters.
27*/
28_declspec (dllexport) int fn_NetSim_RIP_Init(struct stru_NetSim_Network *NETWORK_Formal,\
29 NetSim_EVENTDETAILS *pstruEventDetails_Formal,char *pszAppPath_Formal,\
30 char *pszWritePath_Formal,int nVersion_Type,void **fnPointer)
31{
32 fn_NetSim_RIP_Init_F(NETWORK_Formal,pstruEventDetails_Formal,pszAppPath_Formal,\
33 pszWritePath_Formal,nVersion_Type,fnPointer);
34 return 0;
35}
36/**
37 This function is called by NetworkStack.dll, once simulation end to free the
38 allocated memory for the network.
39*/
40_declspec(dllexport) int fn_NetSim_RIP_Finish()
41{
42 fn_NetSim_RIP_Finish_F();
43 return 0;
44}
45/**
46 This function is called by NetworkStack.dll, while writing the evnt trace
47 to get the sub event as a string.
48*/
49_declspec (dllexport) char *fn_NetSim_RIP_Trace(int nSubEvent)
50{
51 return (fn_NetSim_RIP_Trace_F(nSubEvent));
52}
53/**
54 This function is called by NetworkStack.dll, to free the packet of RIP protocol
55*/
56_declspec(dllexport) int fn_NetSim_RIP_FreePacket(NetSim_PACKET* pstruPacket)
57{
58 return fn_NetSim_RIP_FreePacket_F(pstruPacket);
59}
60/**
61 This function is called by NetworkStack.dll, to copy the packet of RIP protocol
62 from source to destination.
63*/
64_declspec(dllexport) int fn_NetSim_RIP_CopyPacket(NetSim_PACKET* pstruDestPacket,NetSim_PACKET* pstruSrcPacket)
65{
66 return fn_NetSim_RIP_CopyPacket_F(pstruDestPacket,pstruSrcPacket);
67}
68/**
69 This function write the Metrics in Metrics.txt
70*/
71_declspec(dllexport) int fn_NetSim_RIP_Metrics(PMETRICSWRITER metricsWriter)
72{
73 return fn_NetSim_RIP_Metrics_F(metricsWriter);
74}
75/**
76 This function will return the string to write packet trace heading.
77*/
78_declspec(dllexport)char* fn_NetSim_RIP_ConfigPacketTrace()
79{
80 return "";
81}
82/**
83 This function will return the string to write packet trace.
84*/
85_declspec(dllexport)char* fn_NetSim_RIP_WritePacketTrace()
86{
87 return "";
88}
89/**
90 This function is called by NetworkStack.dll, whenever the event gets triggered <br/>
91 inside the NetworkStack.dll for the Application layer RIP protocol<br/>
92 It includes APPLICATION_IN,NETWORK_OUT,NETWORK_IN and TIMER_EVENT.<br/>
93
94 */
95_declspec(dllexport)int fn_NetSim_RIP_Run()
96{
97 if(pstruEventDetails->pPacket && !fnCheckRoutingPacket())
98 return 0; //Not routing packet
99 return fnRouter[pstruEventDetails->nProtocolId?pstruEventDetails->nProtocolId%100:pstruEventDetails->pPacket->nControlDataType/100%100]();
100}
101/** This function is used to send the RIP updates whenever routing table is updated*/
102_declspec(dllexport)int fn_NetSim_RIP_TriggeredUpdate(struct stru_NetSim_Network *pstruNETWORK,NetSim_EVENTDETAILS *pstruEventDetails)
103{
104 int nLoop1=0,nLink_Id=0;
105 NETSIM_ID nConnectedDevId=0,nDeviceid;
106 NETSIM_ID nConnectedInterfaceId=0;
107 NETSIM_ID nInterfaceId,nLoop=0;
108 NETSIM_ID nInterfaceCount;
109 DEVICE_ROUTER *pstruRouter;
110 RIP_PACKET *pstruRIPPacket = NULL;
111 NetSim_PACKET* pstruControlPacket=NULL;
112 RIP_ROUTING_DATABASE *pstrudatabase;
113 RIP_ENTRY *pstruRIPNewEntry,*pstruRIPCurrentEntry;
114 NETWORK=pstruNETWORK;
115 nDeviceid=pstruEventDetails->nDeviceId;
116 nInterfaceCount=NETWORK->ppstruDeviceList[nDeviceid-1]->nNumOfInterface;
117
118 for(nLoop=0;nLoop<nInterfaceCount;nLoop++)
119 {
120 if(NETWORK->ppstruDeviceList[nDeviceid-1]->ppstruInterfaceList[nLoop]->szAddress &&
121 NETWORK->ppstruDeviceList[nDeviceid-1]->ppstruInterfaceList[nLoop]->nInterfaceType==INTERFACE_WAN_ROUTER)
122 {
123 nLink_Id = fn_NetSim_Stack_GetConnectedDevice(nDeviceid,nLoop+1,&nConnectedDevId,&nConnectedInterfaceId);
124 if(!nLink_Id)
125 continue;
126 if(NETWORK->ppstruDeviceList[nDeviceid-1]->pstruApplicationLayer)
127 {
128 pstruRouter = get_RIP_var(nDeviceid);
129 if(pstruRouter && pstruRouter->RoutingProtocol[nLoop] == APP_PROTOCOL_RIP)
130 {
131 //Creating the RIP packet
132 pstruRIPPacket=calloc(1,sizeof(RIP_PACKET));
133 //Assign the Version type
134 pstruRIPPacket->nVersion=pstruRouter->uniInteriorRouting.struRIP.n_RIP_Version;
135 pstrudatabase=pstruRouter->pstruRoutingTables->pstruRIP_RoutingTable;
136 //Add all the entries in routing database to RIP packet
137 while(pstrudatabase!=NULL)
138 {
139 pstruRIPNewEntry=calloc(1,sizeof(struct stru_RIPEntry));
140 pstruRIPNewEntry->szIPv4_address=IP_COPY(pstrudatabase->szAddress);
141 pstruRIPNewEntry->szNext_Hop=IP_COPY(pstrudatabase->szRouter);
142 pstruRIPNewEntry->nAddress_family_identifier=2; // The Address family identifier value is 2 for IP
143 pstruRIPNewEntry->szSubnet_Mask=IP_COPY(pstrudatabase->szSubnetmask);
144 pstruRIPNewEntry->nMetric=pstrudatabase->nMetric;
145 if(pstruRIPPacket->pstruRIPEntry)
146 {
147 pstruRIPCurrentEntry = pstruRIPPacket->pstruRIPEntry;
148 while(pstruRIPCurrentEntry->pstru_RIP_NextEntry != NULL)
149 pstruRIPCurrentEntry = pstruRIPCurrentEntry->pstru_RIP_NextEntry;
150 pstruRIPCurrentEntry->pstru_RIP_NextEntry = pstruRIPNewEntry;
151 }
152 else
153 pstruRIPPacket->pstruRIPEntry = pstruRIPNewEntry;
154
155 pstrudatabase=pstrudatabase->pstru_Router_NextEntry;
156 }
157 nInterfaceId=nLoop+1;
158 //Create the Application layer packet to carry the RIP packet
159 pstruControlPacket=fn_NetSim_Packet_CreatePacket(APPLICATION_LAYER);
160 pstruControlPacket->nControlDataType=RIP_Packet;
161 pstruControlPacket->nPacketId=0;
162 pstruControlPacket->nPacketType=PacketType_Control;
163 pstruControlPacket->nSourceId=nDeviceid;
164 add_dest_to_packet(pstruControlPacket, nConnectedDevId);
165 pstruControlPacket->nTransmitterId=nDeviceid;
166 pstruControlPacket->nReceiverId=nConnectedDevId;
167 //Assign the Application layer details of the packet
168 pstruControlPacket->pstruAppData->dArrivalTime=pstruEventDetails->dEventTime;
169 pstruControlPacket->pstruAppData->dEndTime=pstruEventDetails->dEventTime;
170 pstruControlPacket->pstruAppData->dStartTime=pstruEventDetails->dEventTime;
171 pstruControlPacket->pstruAppData->dPayload=RIP_PACKET_SIZE_WITH_HEADER;
172 pstruControlPacket->pstruAppData->dOverhead=0;
173 pstruControlPacket->pstruAppData->dPacketSize=pstruControlPacket->pstruAppData->dPayload+pstruControlPacket->pstruAppData->dOverhead;
174 pstruControlPacket->pstruAppData->nApplicationProtocol=APP_PROTOCOL_RIP;
175 pstruControlPacket->pstruAppData->Packet_AppProtocol=pstruRIPPacket;
176 //Assign the Network layer details of the packet
177 pstruControlPacket->pstruNetworkData->nNetworkProtocol=NW_PROTOCOL_IPV4;
178 pstruControlPacket->pstruNetworkData->nTTL = 1;
179 pstruControlPacket->pstruNetworkData->szSourceIP=IP_COPY(fn_NetSim_Stack_GetIPAddressAsId(nDeviceid,nInterfaceId));
180 pstruControlPacket->pstruNetworkData->szDestIP=IP_COPY(fn_NetSim_Stack_GetIPAddressAsId(nConnectedDevId,nConnectedInterfaceId));
181 if(!pstruControlPacket->pstruNetworkData->szDestIP)
182 {
183 char str[5000];
184 sprintf(str,"Unable to get the connected device IP address for Router %d Interface %d",nDeviceid,nInterfaceId);
185 fnNetSimError(str);
186 }
187 //Assign the Transport layer details of the packet
188 pstruControlPacket->pstruTransportData->nSourcePort=RIP_PORT+1;
189 pstruControlPacket->pstruTransportData->nDestinationPort=RIP_PORT;
190 if(NETWORK->ppstruDeviceList[nDeviceid-1]->pstruTransportLayer->isUDP)
191 pstruControlPacket->pstruTransportData->nTransportProtocol=TX_PROTOCOL_UDP;
192 else if(NETWORK->ppstruDeviceList[nDeviceid-1]->pstruTransportLayer->isTCP)
193 pstruControlPacket->pstruTransportData->nTransportProtocol=TX_PROTOCOL_TCP;
194 else
195 pstruControlPacket->pstruTransportData->nTransportProtocol=0;
196 //Add the packets to the socket buffer
197 if(!fn_NetSim_Socket_GetBufferStatus(pstruRouter->sock))
198 {
199 fn_NetSim_Socket_PassPacketToInterface(nDeviceid, pstruControlPacket, pstruRouter->sock);
200 pstruEventDetails->dEventTime=pstruEventDetails->dEventTime;
201 pstruEventDetails->dPacketSize=pstruControlPacket->pstruAppData->dPacketSize;
202 pstruEventDetails->nApplicationId=0;
203 pstruEventDetails->nProtocolId=pstruControlPacket->pstruTransportData->nTransportProtocol;
204 pstruEventDetails->nDeviceId=nDeviceid;
205 pstruEventDetails->nInterfaceId=0;
206 pstruEventDetails->nEventType=TRANSPORT_OUT_EVENT;
207 pstruEventDetails->nSubEventType=0;
208 pstruEventDetails->pPacket=NULL;
209 pstruEventDetails->szOtherDetails = pstruRouter->sock;
210 fnpAddEvent(pstruEventDetails);
211 }
212 else
213 {
214 fn_NetSim_Socket_PassPacketToInterface(nDeviceid, pstruControlPacket, pstruRouter->sock);
215 }
216 }
217 }
218 }
219 }
220 return 0;
221}
222/** This function is used to execute the RIP functionalities.*/
223int fn_NetSim_RIP_Run_F()
224{
225 SUB_EVENT nSub_Event_Type;
226
227 nEventType=pstruEventDetails->nEventType;
228
229 nSub_Event_Type = pstruEventDetails->nSubEventType;
230 //
231 //Check the event type
232 switch(nEventType)
233 {
234 case APPLICATION_IN_EVENT:
235 switch(nSub_Event_Type)
236 {
237 case ROUTING_TABLE_UPDATION:
238 fn_NetSim_RIP_DistanceVectorAlgorithm(NETWORK,pstruEventDetails);
239#ifdef _RIP_Trace
240 fn_NetSim_RIPTrace(NETWORK,pstruEventDetails->nDeviceId,1);
241#endif
242 break;
243 default:
244 fn_NetSim_RIP_ReceivingOf_RIP_Message(NETWORK,pstruEventDetails);
245 break;
246 }
247 break;
248 case TIMER_EVENT:
249 switch(nSub_Event_Type)
250 {
251 case SEND_RIP_UPDATE_PKT:
252 fn_NetSim_RIP_Update_Timer(NETWORK,pstruEventDetails);
253 break;
254 case RIP_TIMEOUT:
255 fn_NetSim_RIP_Timeout_Timer(NETWORK,pstruEventDetails);
256 break;
257 case RIP_GARBAGE_COLLECTION:
258 fn_NetSim_RIP_Garbage_Collection_Timer(NETWORK,pstruEventDetails);
259 break;
260 default:
261 printf("\nInvalid sub event");
262 break;
263 }
264 break;
265 default:
266 printf("\nInvalid event");
267 break;
268 }
269
270 return 0;
271}
272
273
unsigned int nVersion
The version field is used to specify the RIP version (version 1 or 2)
Definition RIP.h:104
Definition RIP.h:129
unsigned int nMetric
Cost to reach the destination.
Definition RIP.h:136
unsigned int nAddress_family_identifier
This is used to identify the Address family of the IP address.
Definition RIP.h:130
NETSIM_IPAddress szSubnet_Mask
Destination Subnet Mask.
Definition RIP.h:133
NETSIM_IPAddress szIPv4_address
Destination IPv4 address.
Definition RIP.h:132
NETSIM_IPAddress szNext_Hop
Definition RIP.h:134
unsigned int nMetric
Distance to the destination.
Definition RIP.h:78
NETSIM_IPAddress szRouter
The first router along the route to the destination.
Definition RIP.h:76
NETSIM_IPAddress szAddress
IP address of the destination host or destination network.
Definition RIP.h:74
NETSIM_IPAddress szSubnetmask
Subnet mask for the destination network.
Definition RIP.h:75