NetSim Source Code Help v14.4
All 13 Components
 
Loading...
Searching...
No Matches
RREP.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: Shashi Kant Suman *
12* *
13* ---------------------------------------------------------------------------------*/
14
15#include "main.h"
16#include "AODV.h"
17#include "List.h"
18
19/**
20This function generates a route reply
21*/
22int fn_NetSim_AODV_GenerateRREP(NetSim_EVENTDETAILS* pstruEventDetails)
23{
24 NETSIM_ID id;
25 NetSim_EVENTDETAILS pevent;
26 AODV_RREQ* rreq = (AODV_RREQ*)pstruEventDetails->pPacket->pstruNetworkData->Packet_RoutingProtocol;
27 NetSim_PACKET* packet = fn_NetSim_AODV_GenerateCtrlPacket(pstruEventDetails->nDeviceId,
28 fn_NetSim_Stack_GetDeviceId_asIP(rreq->OriginatorIPAddress,&id),
29 fn_NetSim_Stack_GetDeviceId_asIP(rreq->LastAddress,&id),
30 pstruEventDetails->dEventTime,
31 AODVctrlPacket_RREP);
32 AODV_RREP* rrep = (AODV_RREP*)calloc(1,sizeof* rrep);
33 packet->pstruNetworkData->Packet_RoutingProtocol = rrep;
34 packet->pstruNetworkData->dPacketSize = AODV_RREP_SIZE(pstruEventDetails->nDeviceId);
35 packet->pstruNetworkData->dOverhead = AODV_RREP_SIZE(pstruEventDetails->nDeviceId);
36 packet->pstruNetworkData->nTTL = rreq->HopCount+2;
37 rrep->DestinationIPaddress = IP_COPY(rreq->DestinationIPAddress);
38 rrep->DestinationSequenceNumber = ++AODV_DEV_VAR(pstruEventDetails->nDeviceId)->nSequenceNumber;
39 rrep->HopCount = 1;
40 rrep->Lifetime = (UINT)AODV_MY_ROUTE_TIMEOUT;
41 rrep->OriginatorIPaddress = IP_COPY(rreq->OriginatorIPAddress);
42 rrep->PrefixSz = 0;
43 rrep->Type = 2;
44 rrep->LastAddress = aodv_get_curr_ip();
45 //Add network out event
46 memcpy(&pevent,pstruEventDetails,sizeof pevent);
47 pevent.dPacketSize = AODV_RREP_SIZE(pstruEventDetails->nDeviceId);
48 pevent.nEventType = NETWORK_OUT_EVENT;
49 pevent.nSubEventType = 0;
50 pevent.pPacket = packet;
51 pstruEventDetails->pPacket->pstruNetworkData->szNextHopIp=NULL;
52 fnpAddEvent(&pevent);
53 //Update the metrics
54 AODV_METRICS_VAR(pstruEventDetails->nDeviceId).rrepSent++;
55 return 1;
56}
57/**
58This function processes a route reply.
59It adds the entry of the next hop in Route Table, transmits the packets in FIFO Buffer and
60inserts precursor.
61Deletes the RREQ entry from sent table and forwards the rrep if the device is not
62the source node.
63*/
64int fn_NetSim_AODV_ProcessRREP(NetSim_EVENTDETAILS* pstruEventDetails)
65{
66 AODV_RREP* rrep = (AODV_RREP*)pstruEventDetails->pPacket->pstruNetworkData->Packet_RoutingProtocol;
67 //Update the routing table
68 if(rrep->DestinationIPaddress == aodv_get_curr_ip())
69 return 0;
70
71 AODV_INSERT_ROUTE_TABLE(rrep->DestinationIPaddress,
73 rrep->HopCount,
74 rrep->LastAddress,
75 pstruEventDetails->dEventTime+AODV_ACTIVE_ROUTE_TIMEOUT);
76 //Transmit the buffer
77 AODV_TRANSMIT_FIFO(AODV_DEV_VAR(pstruEventDetails->nDeviceId));
78 //Update the precursor list
79 AODV_INSERT_PRECURSOR(rrep->LastAddress);
80 sprintf(comment, "Received RREP as Intermediate");
81 AODV_UPDATE_ROUTE_TABLE(rrep->LastAddress,rrep->Lifetime);
82 if(!IP_COMPARE(aodv_get_curr_ip(),rrep->OriginatorIPaddress))
83 {
84 //Delete entry from sent table
85 AODV_RREQ_SENT_TABLE* table = AODV_DEV_VAR(pstruEventDetails->nDeviceId)->rreqSentTable;
86 while(table)
87 {
88 if(!IP_COMPARE(table->DestAddress,rrep->DestinationIPaddress))
89 {
90 IP_FREE(table->DestAddress);
91 LIST_FREE((void**)&AODV_DEV_VAR(pstruEventDetails->nDeviceId)->rreqSentTable,table);
92 break;
93 }
94 table = (AODV_RREQ_SENT_TABLE*)LIST_NEXT(table);
95 }
96 sprintf(comment, "Received RREP as Source");
97 //Drop the packet
98 fn_NetSim_Packet_FreePacket(pstruEventDetails->pPacket);
99 }
100 else
101 {
102 if (pstruEventDetails->pPacket->szPacketType &&
103 _stricmp(pstruEventDetails->pPacket->szPacketType, "AODV_RREP(HELLO)") == 0)
104 sprintf(comment, "Received Hello");
105 else
106 sprintf(comment, "Received RREP as Intermediate");
107 //Forward the route reply
108 AODV_FORWARD_RREP();
109 }
110 AODV_UPDATE_ROUTE_TABLE(rrep->LastAddress, rrep->Lifetime);
111 return 1;
112}
113/**
114This functiopn is used to forward the RREP packet to the previous hop, else the
115packet is freed.
116*/
117int fn_NetSim_AODV_ForwardRREP(NetSim_EVENTDETAILS* pstruEventDetails)
118{
119 AODV_RREP* rrep = (AODV_RREP*)pstruEventDetails->pPacket->pstruNetworkData->Packet_RoutingProtocol;
120 if(AODV_CHECK_ROUTE_FOUND(rrep->OriginatorIPaddress) &&
121 !isBroadcastPacket(pstruEventDetails->pPacket)/*For hello packet*/)
122 {
123 NetSim_EVENTDETAILS pevent;
124 NETSIM_ID port;
125 NETSIM_IPAddress nextHop = IP_COPY(AODV_FIND_NEXT_HOP(AODV_DEV_VAR(pstruEventDetails->nDeviceId),rrep->OriginatorIPaddress));
126 rrep->HopCount+=1;
127 rrep->LastAddress = aodv_get_curr_ip();
128 pstruEventDetails->pPacket->nReceiverId = fn_NetSim_Stack_GetDeviceId_asIP(nextHop,&port);
129 pstruEventDetails->pPacket->nTransmitterId = pstruEventDetails->nDeviceId;
130 pstruEventDetails->pPacket->pstruNetworkData->szNextHopIp = (nextHop);
131 //Add network out event to transmit RREP
132 memcpy(&pevent,pstruEventDetails,sizeof pevent);
133 pevent.nEventType = NETWORK_OUT_EVENT;
134 pstruEventDetails->pPacket->pstruNetworkData->szNextHopIp=NULL;
135 fnpAddEvent(&pevent);
136 //Update the metrics
137 if(pevent.pPacket->pstruNetworkData->nTTL>1)
138 AODV_METRICS_VAR(pevent.nDeviceId).rrepForwarded++;
139 }
140 else
141 fn_NetSim_Packet_FreePacket(pstruEventDetails->pPacket);
142 return 1;
143}
144/**
145This function generates a RREP by an intermediate node if the intermediate node has
146a route to the target entered in the route table.
147*/
148int fn_NetSim_AODV_GenerateRREPByIntermediate(NetSim_EVENTDETAILS* pstruEventDetails)
149{
150 NETSIM_ID id;
151 NetSim_EVENTDETAILS pevent;
152 AODV_RREQ* rreq = (AODV_RREQ*)pstruEventDetails->pPacket->pstruNetworkData->Packet_RoutingProtocol;
153 AODV_ROUTETABLE* table = fnFindRouteTable(AODV_DEV_VAR(pstruEventDetails->nDeviceId)->routeTable,rreq->DestinationIPAddress);
154 NetSim_PACKET* packet = fn_NetSim_AODV_GenerateCtrlPacket(pstruEventDetails->nDeviceId,
155 fn_NetSim_Stack_GetDeviceId_asIP(rreq->OriginatorIPAddress,&id),
156 fn_NetSim_Stack_GetDeviceId_asIP(rreq->LastAddress,&id),
157 pstruEventDetails->dEventTime,
158 AODVctrlPacket_RREP);
159 AODV_RREP* rrep = (AODV_RREP*)calloc(1,sizeof* rrep);
160
161 if(!IP_COMPARE(table->NextHop,rreq->LastAddress))
162 {
163 AODV_FORWARD_RREQ();
164 return 0;
165 }
166
167 packet->pstruNetworkData->Packet_RoutingProtocol = rrep;
168 packet->pstruNetworkData->dPacketSize = AODV_RREP_SIZE(pstruEventDetails->nDeviceId);
169 packet->pstruNetworkData->dOverhead = AODV_RREP_SIZE(pstruEventDetails->nDeviceId);
170 packet->pstruNetworkData->nTTL = AODV_NET_DIAMETER;
171 rrep->DestinationIPaddress = IP_COPY(rreq->DestinationIPAddress);
172 rrep->DestinationSequenceNumber = fnFindSequenceNumber(AODV_DEV_VAR(pstruEventDetails->nDeviceId),
173 rreq->DestinationIPAddress);
174 rrep->HopCount = table->HopCount;
175 rrep->Lifetime = (unsigned int)(table->Lifetime-pstruEventDetails->dEventTime);
176 rrep->OriginatorIPaddress = IP_COPY(rreq->OriginatorIPAddress);
177 rrep->PrefixSz = 0;
178 rrep->Type = 2;
179 rrep->LastAddress = aodv_get_curr_ip();
180 //Add network out event
181 memcpy(&pevent,pstruEventDetails,sizeof pevent);
182 pevent.dPacketSize = AODV_RREP_SIZE(pstruEventDetails->nDeviceId);
183 pevent.nEventType = NETWORK_OUT_EVENT;
184 pevent.nSubEventType = 0;
185 pevent.pPacket = packet;
186 pstruEventDetails->pPacket->pstruNetworkData->szNextHopIp=NULL;
187 fnpAddEvent(&pevent);
188 //Update the metrics
189 AODV_METRICS_VAR(pevent.nDeviceId).rrepSent++;
190 return 1;
191}
192
NETSIM_IPAddress DestAddress
Destination IP Address.
Definition AODV.h:351
unsigned int HopCount
Definition AODV.h:196
NETSIM_IPAddress OriginatorIPaddress
The IP address of the node which originated the RREQ for which the route is supplied.
Definition AODV.h:204
unsigned int PrefixSz
Definition AODV.h:190
unsigned int DestinationSequenceNumber
The destination sequence number associated to the route.
Definition AODV.h:203
unsigned int Lifetime
The time in milliseconds for which nodes receiving the RREP consider the route to be valid.
Definition AODV.h:205