NetSim Source Code Help
Loading...
Searching...
No Matches
RouteReply.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: Shashi Kant Suman *
12* *
13* ---------------------------------------------------------------------------------*/
14#include "main.h"
15#include "DSR.h"
16#include "List.h"
17/**
18This function Generates a route reply to be sent to the source.
19*/
21{
22 NETSIM_ID nLoop;
23 unsigned int addressCount;
27 rreqPacket->nSourceId,
28 rreqPacket->nSourceId,
31 DSR_OPTION_HEADER* option = calloc(1,sizeof* option);
32 DSR_RREP_OPTION* rrep = calloc(1,sizeof* rrep);
33
34 option->nFlowState = 0;
37 option->nReserved = 0;
40
41 option->options = rrep;
43 rrep->nLastHopExternal = 0;
44 rrep->nReserved = 0;
45 addressCount = DSR_RREQ_LEN(rreq);
46 addressCount+=2; //Add for initiator and target
47 rrep->nOptDataLen = (addressCount-1)*4+3;
48 rrep->Address = calloc(addressCount,sizeof* rrep->Address);
49 rrep->Address[0]= dsr_get_dev_ip(rreqPacket->nSourceId);
50 rrep->Address[addressCount - 1] = dsr_get_curr_ip();
51
52
53 for(nLoop=1;nLoop<addressCount-1;nLoop++)
54 rrep->Address[nLoop] = IP_COPY(rreq->address[nLoop-1]);
55 packet->nReceiverId = fn_NetSim_Stack_GetDeviceId_asIP(rrep->Address[addressCount-2],&(nLoop));
57 packet->pstruNetworkData->szNextHopIp = IP_COPY(rrep->Address[addressCount-2]);
60 packet->pstruNetworkData->nTTL = addressCount+2;
61
62 //Generate Network out event
68 pstruEventDetails->pPacket = packet;
70 //Update the metrics
71 DSR_DEV_VAR(pstruEventDetails->nDeviceId)->dsrMetrics.rrepSent++;
72 return 0;
73}
74/**
75This function processes the route reply.
76It Updates the route cache of the device and adds the new route to the destination in the
77route cache.
78It checks the Send Buffer if any packets are in the list to be sent.
79Then it forwards the RREP packet to the prev HOP.
80*/
82{
84 DSR_RREP_OPTION* rrep = (DSR_RREP_OPTION*)option->options;
86 rrep->Address,
91 return 0;
92}
93
94static void determine_position(DSR_ROUTE_CACHE* cache, int* my, int* de, NETSIM_IPAddress myIP, NETSIM_IPAddress dest)
95{
96 UINT i;
97 for (i = 0; i < cache->nLength; i++)
98 {
99 if (!IP_COMPARE(cache->address[i], myIP))
100 *my = i + 1;
101 if (!IP_COMPARE(cache->address[i], dest))
102 {
103 *de = i + 1;
104 return;
105 }
106 }
107}
108
109/**
110This functions generates a route reply if the route to destination is there in the
111route cache.
112*/
114 NetSim_PACKET* rreqPacket,
115 double dTime,
117{
119 DSR_ROUTE_CACHE* cache = DSR_FIND_CACHE(devVar,rreq->targetAddress,dTime);
120 if(!DSR_VALIDATE_CACHE(cache,rreq->address,DSR_RREQ_LEN(rreq)))
121 {
122 return false;
123 }
124 else
125 {
126 int nLoop;
127 unsigned int addressCount;
128 int my=-1, de=-1;
129 NETSIM_ID n=0;
132 rreqPacket->nSourceId,
133 rreqPacket->nSourceId,
136 DSR_OPTION_HEADER* option = calloc(1,sizeof* option);
137 DSR_RREP_OPTION* rrep = calloc(1,sizeof* rrep);
138
139 determine_position(cache, &my, &de,
140 dsr_get_dev_ip(rreqPacket->nSourceId),
141 rreq->targetAddress);
142
143 UINT len = my == -1 ? de : de - my;
144
145 option->nFlowState = 0;
146 option->nNextHeader = NO_NEXT_HEADER;
148 option->nReserved = 0;
149 option->optType = optType_RouteReply;
151
152 option->options = rrep;
154 rrep->nLastHopExternal = 0;
155 rrep->nReserved = 0;
156 addressCount = DSR_RREQ_LEN(rreq) + len;//cache->nLength;
157 addressCount+=1; //Add for initiator
158
159 rrep->nOptDataLen = (addressCount-1)*4+3;
160 rrep->Address = calloc(addressCount,sizeof* rrep->Address);
161
162 rrep->Address[0]= dsr_get_dev_ip(rreqPacket->nSourceId);
163
164 for(nLoop=1;nLoop<=DSR_RREQ_LEN(rreq);nLoop++)
165 rrep->Address[nLoop] = rreq->address[nLoop-1];
166
167 packet->nReceiverId = fn_NetSim_Stack_GetDeviceId_asIP(rrep->Address[nLoop-1],&n);
168 packet->pstruNetworkData->szNextHopIp = IP_COPY(rrep->Address[nLoop-1]);
169
170 n = my == -1 ? 0 : my;
171 for(;(unsigned int)nLoop<addressCount;nLoop++)
172 rrep->Address[nLoop] = IP_COPY(cache->address[n++]);
173
174
175 //Generate Network out event
181 pstruEventDetails->pPacket = packet;
183 //Update the metrics
184 devVar->dsrMetrics.rrepSent++;
185 return true;
186 }
187}
188/**
189This function checks if the current device is the last HOP. If it is, it drops the packet and
190no further forwarding is done.
191
192Else, the RREP is forwarded to the Previous HOP
193*/
195{
198
200 DSR_RREP_OPTION* rrep = (DSR_RREP_OPTION*)option->options;
201 int length = DSR_RREP_LEN(rrep);
202 NETSIM_ID n=0;
203 NETSIM_IPAddress nextHop=NULL;
204
205 if(!IP_COMPARE(rrep->Address[0],dsr_get_curr_ip()))
206 {
207 DSR_RREQ_TABLE* table = getRREQTable(rrep->Address[DSR_RREP_LEN(rrep)-1],
208 DSR_DEV_VAR(pstruEventDetails->nDeviceId)->pstruRREQTable);
209 if(table)
210 {
211 table->flag = true;//RREP received
212 fnDeleteEvent(table->nEventId);
213 }
216 return 1; //Last hop no need to forward
217 }
218 while(n<(NETSIM_ID)length)
219 {
220
221 if(!IP_COMPARE(rrep->Address[n],dsr_get_curr_ip()))
222 {
223 nextHop = rrep->Address[n-1];
224 break;
225 }
226 n++;
227 }
228 if(nextHop)
229 {
231 packet->pstruNetworkData->szNextHopIp = IP_COPY(nextHop);
233 packet->nTransmitterId = nDeviceId;
234 packet->nReceiverId = fn_NetSim_Stack_GetDeviceId_asIP(nextHop,&n);
235 //Add Network out event
238 //Update the metrics
239 DSR_DEV_VAR(nDeviceId)->dsrMetrics.rrepForwarded++;
240 }
241 return 0;
242}
243
unsigned int NETSIM_ID
Definition: Animation.h:45
NETSIM_IPAddress dsr_get_dev_ip(NETSIM_ID d)
DSR_RREQ_TABLE * getRREQTable(NETSIM_IPAddress target, DSR_RREQ_TABLE *table)
Definition: RouteRequest.c:184
#define DSR_FIND_CACHE(devVar, address, dTime)
Definition: DSR.h:726
#define DSR_CHECK_SEND_BUFFER(nDeviceId, dTime)
Definition: DSR.h:744
#define DSR_UPDATE_ROUTE_CACHE(length, address, dTime)
Definition: DSR.h:725
#define DSR_DEV_VAR(dev)
Definition: DSR.h:772
#define DSR_RREP_SIZE_FIXED
Definition: DSR.h:33
#define DSR_VALIDATE_CACHE(cache, addList, count)
Definition: DSR.h:727
@ ctrlPacket_ROUTE_REPLY
Definition: DSR.h:88
#define DSR_OPTION_HEADER_SIZE
Definition: DSR.h:30
NetSim_PACKET * fn_NetSim_DSR_GenerateCtrlPacket(NETSIM_ID src, NETSIM_ID dest, NETSIM_ID recv, double dTime, DSR_CONTROL_PACKET type)
#define DSR_RREP_LEN(rrep)
Definition: DSR.h:774
#define NO_NEXT_HEADER
Definition: DSR.h:45
#define DSR_RREQ_LEN(rreq)
Definition: DSR.h:773
double fn_NetSim_DSR_GetBroadCastJitter()
NETSIM_IPAddress dsr_get_curr_ip()
@ optType_RouteReply
Definition: DSR.h:97
NETSIM_IPAddress IP_COPY(NETSIM_IPAddress ip)
void IP_FREE(NETSIM_IPAddress ip)
#define IP_COMPARE(ip1, ip2)
Definition: IP_Addressing.h:67
#define UINT
Definition: Linux.h:38
#define calloc(c, s)
Definition: Memory.h:29
bool fn_NetSim_DSR_GenerateRREPUsingRouteCache(DSR_DEVICE_VAR *devVar, NetSim_PACKET *rreqPacket, double dTime, NetSim_EVENTDETAILS *pstruEventDetails)
Definition: RouteReply.c:113
int fn_NetSim_DSR_ProcessRREP(NetSim_EVENTDETAILS *pstruEventDetails)
Definition: RouteReply.c:81
static void determine_position(DSR_ROUTE_CACHE *cache, int *my, int *de, NETSIM_IPAddress myIP, NETSIM_IPAddress dest)
Definition: RouteReply.c:94
int fn_NetSim_DSR_ForwardRREP()
Definition: RouteReply.c:194
int fn_NetSim_DSR_GenerateRREP(NetSim_PACKET *rreqPacket, NetSim_EVENTDETAILS *pstruEventDetails)
Definition: RouteReply.c:20
@ NW_PROTOCOL_DSR
Definition: Stack.h:196
@ NETWORK_OUT_EVENT
Definition: Stack.h:108
int fnDeleteEvent(unsigned long long int nEventId)
EXPORTED struct stru_NetSim_EventDetails * pstruEventDetails
Definition: Stack.h:837
NETSIM_ID fn_NetSim_Stack_GetDeviceId_asIP(NETSIM_IPAddress ip, NETSIM_ID *nInterfaceId)
#define fn_NetSim_Packet_FreePacket(pstruPacket)
Definition: main.h:177
#define fnpAddEvent(pstruEvent)
Definition: main.h:191
struct stru_DSR_Metrics dsrMetrics
Definition: DSR.h:666
unsigned int rrepSent
Definition: DSR.h:640
unsigned int nPayloadLength
Definition: DSR.h:557
unsigned int nReserved
Definition: DSR.h:548
void * options
Definition: DSR.h:565
unsigned int nNextHeader
Definition: DSR.h:536
unsigned int nFlowState
Definition: DSR.h:543
DSR_OPTION_TYPE optType
Definition: DSR.h:558
unsigned int nLength
Definition: DSR.h:582
NETSIM_IPAddress * address
Definition: DSR.h:583
unsigned long long int nEventId
Definition: DSR.h:615
unsigned int nReserved
Definition: DSR.h:313
NETSIM_IPAddress * Address
Definition: DSR.h:317
unsigned int nOptDataLen
Definition: DSR.h:293
DSR_OPTION_TYPE nOptionType
Definition: DSR.h:287
unsigned int nLastHopExternal
Definition: DSR.h:300
NETSIM_IPAddress * address
Definition: DSR.h:251
NETSIM_IPAddress targetAddress
Definition: DSR.h:249
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_IPAddress szGatewayIP
Definition: Packet.h:200
NETSIM_IPAddress szNextHopIp
Definition: Packet.h:201
struct stru_NetSim_Packet_NetworkLayer * pstruNetworkData
Definition: Packet.h:275
NETSIM_ID nReceiverId
Definition: Packet.h:266
NETSIM_ID nTransmitterId
Definition: Packet.h:265
NETSIM_ID nSourceId
Definition: Packet.h:263