NetSim Source Code Help
Loading...
Searching...
No Matches
RIP.c
Go to the documentation of this file.
1/************************************************************************************
2* Copyright (C) 2020 *
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,\
31{
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*/
41{
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*/
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*/
79{
80 return "";
81}
82/**
83 This function will return the string to write packet trace.
84*/
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 */
96{
98 return 0; //Not routing packet
100}
101/** This function is used to send the RIP updates whenever routing table is updated*/
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 &&
122 {
123 nLink_Id = fn_NetSim_Stack_GetConnectedDevice(nDeviceid,nLoop+1,&nConnectedDevId,&nConnectedInterfaceId);
124 if(!nLink_Id)
125 continue;
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
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
172 pstruControlPacket->pstruAppData->dOverhead=0;
173 pstruControlPacket->pstruAppData->dPacketSize=pstruControlPacket->pstruAppData->dPayload+pstruControlPacket->pstruAppData->dOverhead;
175 pstruControlPacket->pstruAppData->Packet_AppProtocol=pstruRIPPacket;
176 //Assign the Network layer details of the packet
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;
192 else if(NETWORK->ppstruDeviceList[nDeviceid-1]->pstruTransportLayer->isTCP)
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);
204 pstruEventDetails->nDeviceId=nDeviceid;
209 pstruEventDetails->szOtherDetails = pstruRouter->sock;
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.*/
224{
225 SUB_EVENT nSub_Event_Type;
226
228
229 nSub_Event_Type = pstruEventDetails->nSubEventType;
230 //
231 //Check the event type
232 switch(nEventType)
233 {
235 switch(nSub_Event_Type)
236 {
239#ifdef _RIP_Trace
241#endif
242 break;
243 default:
245 break;
246 }
247 break;
248 case TIMER_EVENT:
249 switch(nSub_Event_Type)
250 {
253 break;
254 case RIP_TIMEOUT:
256 break;
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
enum enum_ARP_Subevent_Type SUB_EVENT
Definition: ARP.h:44
unsigned int NETSIM_ID
Definition: Animation.h:45
int fn_NetSim_RIP_DistanceVectorAlgorithm(struct stru_NetSim_Network *NETWORK, NetSim_EVENTDETAILS *pstruEventDetails)
int fn_NetSim_RIP_Garbage_Collection_Timer(struct stru_NetSim_Network *pstruNETWORK, NetSim_EVENTDETAILS *pstruEventDetails)
NETSIM_IPAddress IP_COPY(NETSIM_IPAddress ip)
#define _declspec(dllexport)
This function is used to trigger the update.
Definition: Linux.h:41
#define fnNetSimError(x,...)
Definition: Linux.h:56
#define calloc(c, s)
Definition: Memory.h:29
void * PMETRICSWRITER
Definition: MetricsWriter.h:27
@ PacketType_Control
Definition: Packet.h:66
void add_dest_to_packet(NetSim_PACKET *packet, NETSIM_ID dest)
int fn_NetSim_RIP_CopyPacket(NetSim_PACKET *pstruDestPacket, NetSim_PACKET *pstruSrcPacket)
Definition: RIP.c:64
int fn_NetSim_RIP_Init(struct stru_NetSim_Network *NETWORK_Formal, NetSim_EVENTDETAILS *pstruEventDetails_Formal, char *pszAppPath_Formal, char *pszWritePath_Formal, int nVersion_Type, void **fnPointer)
Definition: RIP.c:28
char * fn_NetSim_RIP_WritePacketTrace()
Definition: RIP.c:85
char * fn_NetSim_RIP_ConfigPacketTrace()
Definition: RIP.c:78
int fn_NetSim_RIP_Run()
Definition: RIP.c:95
int fn_NetSim_RIP_Finish()
Definition: RIP.c:40
char * fn_NetSim_RIP_Trace(int nSubEvent)
Definition: RIP.c:49
int fn_NetSim_RIP_TriggeredUpdate(struct stru_NetSim_Network *pstruNETWORK, NetSim_EVENTDETAILS *pstruEventDetails)
Definition: RIP.c:102
int fn_NetSim_RIP_Configure(void **var)
Definition: RIP.c:21
int fn_NetSim_RIP_Run_F()
Definition: RIP.c:223
int fn_NetSim_RIP_Metrics(PMETRICSWRITER metricsWriter)
Definition: RIP.c:71
int fn_NetSim_RIP_FreePacket(NetSim_PACKET *pstruPacket)
Definition: RIP.c:56
@ RIP_TIMEOUT
Definition: RIP.h:51
@ SEND_RIP_UPDATE_PKT
Definition: RIP.h:50
@ RIP_GARBAGE_COLLECTION
Definition: RIP.h:52
@ ROUTING_TABLE_UPDATION
Definition: RIP.h:53
NetSim_PACKET *int fn_NetSim_RIP_CopyPacket_F(NetSim_PACKET *, NetSim_PACKET *)
int fn_NetSim_RIP_Finish_F()
#define RIP_PACKET_SIZE_WITH_HEADER
Definition: RIP.h:28
#define RIP_PORT
Definition: RIP.h:29
int fn_NetSim_RIP_Timeout_Timer(struct stru_NetSim_Network *pstruNETWORK, NetSim_EVENTDETAILS *pstruEventDetails)
Definition: TimeoutTimer.c:25
NetSim_EVENTDETAILS * pstruEventDetails_Formal
Definition: RIP.h:178
@ RIP_Packet
Definition: RIP.h:68
int fn_NetSim_RIP_Update_Timer(struct stru_NetSim_Network *pstruNETWORK, NetSim_EVENTDETAILS *pstruEventDetails)
Definition: UpdateTimer.c:26
int fn_NetSim_RIP_Init_F(struct stru_NetSim_Network *, NetSim_EVENTDETAILS *, char *, char *, int, void **fnPointer)
NetSim_EVENTDETAILS char * pszAppPath_Formal
Definition: RIP.h:178
int fn_NetSim_RIP_Metrics_F(PMETRICSWRITER metricsWriter)
NetSim_EVENTDETAILS char char int nVersion_Type
Definition: RIP.h:178
char * fn_NetSim_RIP_Trace_F(int nSubEvent)
int fn_NetSim_RIPTrace(struct stru_NetSim_Network *, NETSIM_ID, int)
NetSim_EVENTDETAILS char char * pszWritePath_Formal
Definition: RIP.h:178
DEVICE_ROUTER * get_RIP_var(NETSIM_ID d)
int fn_NetSim_RIP_FreePacket_F(NetSim_PACKET *)
NetSim_EVENTDETAILS char char int void ** fnPointer
Definition: RIP.h:178
int fn_NetSim_RIP_ReceivingOf_RIP_Message(struct stru_NetSim_Network *pstruNETWORK, NetSim_EVENTDETAILS *pstruEventDetails)
Definition: ReceivingRIP.c:23
int fnCheckRoutingPacket()
Definition: Routing.c:77
int(* fnRouter[10])()
Definition: Routing.h:21
NETSIM_ID fn_NetSim_Stack_GetConnectedDevice(NETSIM_ID nDeviceId, NETSIM_ID nInterfaceId, NETSIM_ID *nConnectedDevId, NETSIM_ID *nConnectedInterfaceId)
void fn_NetSim_Socket_PassPacketToInterface(NETSIM_ID deviceId, NetSim_PACKET *packet, ptrSOCKETINTERFACE socketInterface)
EXPORTED EVENT_TYPE nEventType
Definition: Stack.h:839
@ NW_PROTOCOL_IPV4
Definition: Stack.h:189
@ APP_PROTOCOL_RIP
Definition: Stack.h:154
@ TX_PROTOCOL_UDP
Definition: Stack.h:181
@ TX_PROTOCOL_TCP
Definition: Stack.h:180
EXPORTED struct stru_NetSim_Network * NETWORK
Definition: Stack.h:742
@ TIMER_EVENT
Definition: Stack.h:114
@ APPLICATION_IN_EVENT
Definition: Stack.h:113
@ TRANSPORT_OUT_EVENT
Definition: Stack.h:110
NETSIM_IPAddress fn_NetSim_Stack_GetIPAddressAsId(NETSIM_ID nDeviceId, NETSIM_ID nInterfaceId)
@ APPLICATION_LAYER
Definition: Stack.h:98
bool fn_NetSim_Socket_GetBufferStatus(ptrSOCKETINTERFACE s)
EXPORTED struct stru_NetSim_EventDetails * pstruEventDetails
Definition: Stack.h:837
@ INTERFACE_WAN_ROUTER
Definition: Stack.h:254
#define fn_NetSim_Packet_CreatePacket(layer)
Definition: main.h:186
#define fnpAddEvent(pstruEvent)
Definition: main.h:191
struct stru_NetSim_TransportLayer * pstruTransportLayer
Definition: Stack.h:720
struct stru_NetSim_Interface ** ppstruInterfaceList
Definition: Stack.h:717
NETSIM_ID nNumOfInterface
Definition: Stack.h:714
struct stru_NetSim_ApplicationLayer * pstruApplicationLayer
Definition: Stack.h:718
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
NETSIM_ID nInterfaceId
Definition: Stack.h:751
INTERFACE_TYPE nInterfaceType
Definition: Stack.h:677
NETSIM_IPAddress szAddress
Definition: Stack.h:688
struct stru_NetSim_Device ** ppstruDeviceList
Definition: Stack.h:737
APPLICATION_LAYER_PROTOCOL nApplicationProtocol
Definition: Packet.h:172
NETWORK_LAYER_PROTOCOL nNetworkProtocol
Definition: Packet.h:204
NETSIM_IPAddress szDestIP
Definition: Packet.h:199
NETSIM_IPAddress szSourceIP
Definition: Packet.h:198
TRANSPORT_LAYER_PROTOCOL nTransportProtocol
Definition: Packet.h:186
unsigned short int nSourcePort
Definition: Packet.h:184
unsigned short int nDestinationPort
Definition: Packet.h:185
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 nReceiverId
Definition: Packet.h:266
unsigned int nControlDataType
Definition: Packet.h:258
NETSIM_ID nTransmitterId
Definition: Packet.h:265
PACKET_TYPE nPacketType
Definition: Packet.h:257
struct stru_NetSim_Packet_TransportLayer * pstruTransportData
Definition: Packet.h:274
NETSIM_ID nSourceId
Definition: Packet.h:263
unsigned int nVersion
The version field is used to specify the RIP version (version 1 or 2)
Definition: RIP.h:104
struct stru_RIPEntry * pstruRIPEntry
Definition: RIP.h:105
ROUTING_TABLES * pstruRoutingTables
Definition: Routing.h:29
APPLICATION_LAYER_PROTOCOL * RoutingProtocol
Definition: Routing.h:32
ptrSOCKETINTERFACE sock
Definition: Routing.h:33
union stru_NetSim_Router::uni_Interior_Routing uniInteriorRouting
unsigned int isUDP
Definition: Stack.h:503
unsigned int isTCP
Definition: Stack.h:502
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
struct stru_RIPEntry * pstru_RIP_NextEntry
Definition: RIP.h:137
unsigned int n_RIP_Version
Definition: RIP.h:145
RIP_ROUTING_DATABASE * pstruRIP_RoutingTable
Definition: Routing.h:43
Routing database structure Reference : RFC 2453, November 1998, Page 8.
Definition: RIP.h:73
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
struct stru_Router_RIP_Routing_database * pstru_Router_NextEntry
Definition: RIP.h:80
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