NetSim Source Code Help
Loading...
Searching...
No Matches
UpdateTimer.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: Thangarasu.K *
13* ---------------------------------------------------------------------------------*/
14#include "main.h"
15#include "Routing.h"
16/**
17~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
18 Every 30 seconds, the RIP process is awakened to send an unsolicited response message
19 containing the complete routing table to every neighboring router.
20
21 1. Create the RIP packet
22 2. Add the RIP packets in the WAN ports of the current router
23 3. Add the packet in the socket buffer for transmitting the RIP packet
24~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
25*/
27{
28 int nLoop1=0,nLink_Id=0;
29 NETSIM_ID nConnectedDevId=0,nDeviceid;
30 NETSIM_ID nConnectedInterfaceId=0;
31 NETSIM_ID nInterfaceId,nLoop=0;
32 NETSIM_ID nInterfaceCount;
33 DEVICE_ROUTER *pstruRouter;
34 bool bWanPort=false;
35 RIP_PACKET *pstruRIPPacket = NULL;
36 NetSim_PACKET* pstruControlPacket=NULL;
37 RIP_ROUTING_DATABASE *pstrudatabase;
38 RIP_ENTRY *pstruRIPNewEntry,*pstruRIPCurrentEntry;
39 NETWORK=pstruNETWORK;
41 nInterfaceCount=NETWORK->ppstruDeviceList[nDeviceid-1]->nNumOfInterface;
42 for(nLoop=0;nLoop<nInterfaceCount;nLoop++)
43 {
44 if(NETWORK->ppstruDeviceList[nDeviceid-1]->ppstruInterfaceList[nLoop]->szAddress &&
46 {
47 nLink_Id = fn_NetSim_Stack_GetConnectedDevice(nDeviceid,nLoop+1,&nConnectedDevId,&nConnectedInterfaceId);
48 if(!nLink_Id)
49 continue;
50 pstruRouter = get_RIP_var(nDeviceid);
51 if(pstruRouter && pstruRouter->RoutingProtocol[nLoop] == APP_PROTOCOL_RIP)
52 {
53 //Get the Interface type
54 bWanPort=true;
55 //Creating the RIP packet
56 pstruRIPPacket=calloc(1,sizeof(RIP_PACKET));
57 //Assign the Version type
58 pstruRIPPacket->nVersion=pstruRouter->uniInteriorRouting.struRIP.n_RIP_Version;
59 pstrudatabase=pstruRouter->pstruRoutingTables->pstruRIP_RoutingTable;
60 //Add all the entries in routing database to RIP packet
61 while(pstrudatabase!=NULL)
62 {
63 pstruRIPNewEntry=calloc(1,sizeof(struct stru_RIPEntry));
64 pstruRIPNewEntry->szIPv4_address=IP_COPY(pstrudatabase->szAddress);
65 pstruRIPNewEntry->szNext_Hop=IP_COPY(pstrudatabase->szRouter);
66 pstruRIPNewEntry->nAddress_family_identifier=2; // The Address family identifier value is 2 for IP
67 pstruRIPNewEntry->szSubnet_Mask=IP_COPY(pstrudatabase->szSubnetmask);
68 pstruRIPNewEntry->nMetric=pstrudatabase->nMetric;
69 if(pstruRIPPacket->pstruRIPEntry)
70 {
71 pstruRIPCurrentEntry = pstruRIPPacket->pstruRIPEntry;
72 while(pstruRIPCurrentEntry->pstru_RIP_NextEntry != NULL)
73 pstruRIPCurrentEntry = pstruRIPCurrentEntry->pstru_RIP_NextEntry;
74 pstruRIPCurrentEntry->pstru_RIP_NextEntry = pstruRIPNewEntry;
75 }
76 else
77 pstruRIPPacket->pstruRIPEntry = pstruRIPNewEntry;
78 pstrudatabase=pstrudatabase->pstru_Router_NextEntry;
79 }
80 nInterfaceId=nLoop+1;
81 //Create the Application layer packet to carry the RIP packet
83 pstruControlPacket->nControlDataType=RIP_Packet;
84 pstruControlPacket->nPacketId=0;
85 pstruControlPacket->nPacketType=PacketType_Control;
86 pstruControlPacket->nSourceId=nDeviceid;
87 add_dest_to_packet(pstruControlPacket, nConnectedDevId);
88 pstruControlPacket->nTransmitterId=nDeviceid;
89 pstruControlPacket->nReceiverId=nConnectedDevId;
90 //Assign the Application layer details of the packet
95 pstruControlPacket->pstruAppData->dOverhead=0;
96 pstruControlPacket->pstruAppData->dPacketSize=pstruControlPacket->pstruAppData->dPayload+pstruControlPacket->pstruAppData->dOverhead;
98 pstruControlPacket->pstruAppData->Packet_AppProtocol=pstruRIPPacket;
99 //Assign the Network layer details of the packet
101 pstruControlPacket->pstruNetworkData->nTTL = 1;
102 pstruControlPacket->pstruNetworkData->szSourceIP=IP_COPY(fn_NetSim_Stack_GetIPAddressAsId(nDeviceid,nInterfaceId));
103 pstruControlPacket->pstruNetworkData->szDestIP=IP_COPY(fn_NetSim_Stack_GetIPAddressAsId(nConnectedDevId,nConnectedInterfaceId));
104 if(!pstruControlPacket->pstruNetworkData->szDestIP)
105 {
106 char str[5000];
107 sprintf(str,"Unable to get the connected device IP address for Router %d Interface %d",nDeviceid,nInterfaceId);
108 fnNetSimError(str);
109 }
110 //Assign the Transport layer details of the packet
111 pstruControlPacket->pstruTransportData->nSourcePort=RIP_PORT+1;
112 pstruControlPacket->pstruTransportData->nDestinationPort=RIP_PORT;
115 else if(NETWORK->ppstruDeviceList[nDeviceid-1]->pstruTransportLayer->isTCP)
117 else
118 pstruControlPacket->pstruTransportData->nTransportProtocol=0;
119 //Add the packets to the socket buffer
120 if(!fn_NetSim_Socket_GetBufferStatus(pstruRouter->sock))
121 {
122 fn_NetSim_Socket_PassPacketToInterface(nDeviceid, pstruControlPacket, pstruRouter->sock);
127 pstruEventDetails->nDeviceId=nDeviceid;
132 pstruEventDetails->szOtherDetails = pstruRouter->sock;
134 }
135 else
136 {
137 fn_NetSim_Socket_PassPacketToInterface(nDeviceid, pstruControlPacket, pstruRouter->sock);
138 }
139 }
140 }
141 }
142 //To add the next update event
143 if(bWanPort)
144 {
153 }
154 return 0;
155}
156
157
158
159
unsigned int NETSIM_ID
Definition: Animation.h:45
NETSIM_IPAddress IP_COPY(NETSIM_IPAddress ip)
#define fnNetSimError(x,...)
Definition: Linux.h:56
#define calloc(c, s)
Definition: Memory.h:29
@ PacketType_Control
Definition: Packet.h:66
void add_dest_to_packet(NetSim_PACKET *packet, NETSIM_ID dest)
@ SEND_RIP_UPDATE_PKT
Definition: RIP.h:50
#define RIP_PACKET_SIZE_WITH_HEADER
Definition: RIP.h:28
#define RIP_PORT
Definition: RIP.h:29
@ RIP_Packet
Definition: RIP.h:68
DEVICE_ROUTER * get_RIP_var(NETSIM_ID d)
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)
@ 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
@ 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
int fn_NetSim_RIP_Update_Timer(struct stru_NetSim_Network *pstruNETWORK, NetSim_EVENTDETAILS *pstruEventDetails)
Definition: UpdateTimer.c:26
#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
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
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
int n_Update_timer
These variable values are get from configuration.xml,in RFC the update timer is mentioned as 30-secon...
Definition: RIP.h:142
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