NetSim Source Code Help v14.4
All 13 Components
 
Loading...
Searching...
No Matches
RouteReply.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#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*/
20int fn_NetSim_DSR_GenerateRREP(NetSim_PACKET* rreqPacket,NetSim_EVENTDETAILS* pstruEventDetails)
21{
22 NETSIM_ID nLoop;
23 unsigned int addressCount;
24 DSR_RREQ_OPTION* rreq = ((DSR_OPTION_HEADER*)rreqPacket->pstruNetworkData->Packet_RoutingProtocol)->options;
25 NetSim_PACKET* packet = fn_NetSim_DSR_GenerateCtrlPacket(
26 pstruEventDetails->nDeviceId,
27 rreqPacket->nSourceId,
28 rreqPacket->nSourceId,
29 pstruEventDetails->dEventTime,
30 ctrlPacket_ROUTE_REPLY);
31 DSR_OPTION_HEADER* option = calloc(1,sizeof* option);
32 DSR_RREP_OPTION* rrep = calloc(1,sizeof* rrep);
33
34 option->nFlowState = 0;
35 option->nNextHeader = NO_NEXT_HEADER;
36 option->nPayloadLength = DSR_RREP_SIZE_FIXED;
37 option->nReserved = 0;
38 option->optType = optType_RouteReply;
39 packet->pstruNetworkData->Packet_RoutingProtocol = option;
40
41 option->options = rrep;
42 rrep->nOptionType = optType_RouteReply;
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));
56 IP_FREE(packet->pstruNetworkData->szNextHopIp);
57 packet->pstruNetworkData->szNextHopIp = IP_COPY(rrep->Address[addressCount-2]);
58 packet->pstruNetworkData->dPacketSize = rrep->nOptDataLen+DSR_OPTION_HEADER_SIZE;
59 packet->pstruNetworkData->dOverhead = rrep->nOptDataLen+DSR_OPTION_HEADER_SIZE;
60 packet->pstruNetworkData->nTTL = addressCount+2;
61
62 //Generate Network out event
63 pstruEventDetails->dEventTime+=fn_NetSim_DSR_GetBroadCastJitter();
64 pstruEventDetails->dPacketSize = rrep->nOptDataLen+DSR_OPTION_HEADER_SIZE;
65 pstruEventDetails->nEventType = NETWORK_OUT_EVENT;
66 pstruEventDetails->nProtocolId = NW_PROTOCOL_DSR;
67 pstruEventDetails->nSubEventType = 0;
68 pstruEventDetails->pPacket = packet;
69 fnpAddEvent(pstruEventDetails);
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*/
81int fn_NetSim_DSR_ProcessRREP(NetSim_EVENTDETAILS* pstruEventDetails)
82{
83 DSR_OPTION_HEADER* option = (DSR_OPTION_HEADER*)pstruEventDetails->pPacket->pstruNetworkData->Packet_RoutingProtocol;
84 DSR_RREP_OPTION* rrep = (DSR_RREP_OPTION*)option->options;
85 DSR_UPDATE_ROUTE_CACHE(DSR_RREP_LEN(rrep),
86 rrep->Address,
87 pstruEventDetails->dEventTime);
88 DSR_CHECK_SEND_BUFFER(pstruEventDetails->nDeviceId,
89 pstruEventDetails->dEventTime);
90 fn_NetSim_DSR_ForwardRREP();
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*/
113bool fn_NetSim_DSR_GenerateRREPUsingRouteCache(DSR_DEVICE_VAR* devVar,
114 NetSim_PACKET* rreqPacket,
115 double dTime,
116 NetSim_EVENTDETAILS* pstruEventDetails)
117{
118 DSR_RREQ_OPTION* rreq = ((DSR_OPTION_HEADER*)(rreqPacket->pstruNetworkData->Packet_RoutingProtocol))->options;
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;
130 NetSim_PACKET* packet = fn_NetSim_DSR_GenerateCtrlPacket(
131 pstruEventDetails->nDeviceId,
132 rreqPacket->nSourceId,
133 rreqPacket->nSourceId,
134 pstruEventDetails->dEventTime,
135 ctrlPacket_ROUTE_REPLY);
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;
147 option->nPayloadLength = DSR_RREP_SIZE_FIXED;
148 option->nReserved = 0;
149 option->optType = optType_RouteReply;
150 packet->pstruNetworkData->Packet_RoutingProtocol = option;
151
152 option->options = rrep;
153 rrep->nOptionType = optType_RouteReply;
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 packet->pstruNetworkData->nTTL = addressCount + 2;
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
176 pstruEventDetails->dEventTime+=fn_NetSim_DSR_GetBroadCastJitter();
177 pstruEventDetails->dPacketSize = rrep->nOptDataLen+DSR_OPTION_HEADER_SIZE;
178 pstruEventDetails->nEventType = NETWORK_OUT_EVENT;
179 pstruEventDetails->nProtocolId = NW_PROTOCOL_DSR;
180 pstruEventDetails->nSubEventType = 0;
181 pstruEventDetails->pPacket = packet;
182 fnpAddEvent(pstruEventDetails);
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*/
194int fn_NetSim_DSR_ForwardRREP()
195{
196 NetSim_PACKET* packet = pstruEventDetails->pPacket;
197 NETSIM_ID nDeviceId = pstruEventDetails->nDeviceId;
198
199 DSR_OPTION_HEADER* option = (DSR_OPTION_HEADER*)packet->pstruNetworkData->Packet_RoutingProtocol;
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 }
214 fn_NetSim_Packet_FreePacket(packet);
215 pstruEventDetails->pPacket=NULL;
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 {
230 IP_FREE(packet->pstruNetworkData->szNextHopIp);
231 packet->pstruNetworkData->szNextHopIp = IP_COPY(nextHop);
232 packet->pstruNetworkData->szGatewayIP = dsr_get_curr_ip();
233 packet->nTransmitterId = nDeviceId;
234 packet->nReceiverId = fn_NetSim_Stack_GetDeviceId_asIP(nextHop,&n);
235 //Add Network out event
236 pstruEventDetails->nEventType = NETWORK_OUT_EVENT;
237 fnpAddEvent(pstruEventDetails);
238 //Update the metrics
239 DSR_DEV_VAR(nDeviceId)->dsrMetrics.rrepForwarded++;
240 }
241 return 0;
242}
243
unsigned int nReserved
Definition DSR.h:548
unsigned int nNextHeader
Definition DSR.h:536
unsigned int nFlowState
Definition DSR.h:543
DSR_OPTION_TYPE optType
Definition DSR.h:558
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