NetSim Source Code Help v14.4
All 13 Components
 
Loading...
Searching...
No Matches
RouteError.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#include "IP_Addressing.h"
18
19int fn_NetSim_DSR_ForwardRERR(NetSim_EVENTDETAILS* pstruEventDetails);
20/**
21This function gets the previous hop to send the route error to.
22*/
23NETSIM_IPAddress fnGetPrevHop(NetSim_PACKET* packet)
24{
25 DSR_OPTION_HEADER* option = (DSR_OPTION_HEADER*)packet->pstruNetworkData->Packet_RoutingProtocol;
26 DSR_SOURCE_ROUTE_OPTION* srcRouteOption = (DSR_SOURCE_ROUTE_OPTION*)option->options;
27
28 unsigned int nlength = (srcRouteOption->nOptDataLen >= 2) ? (srcRouteOption->nOptDataLen - 2) / 4 : 0;
29
30 if (!srcRouteOption->Address || nlength == 0)
31 return packet->pstruNetworkData->szSourceIP;
32
33 NETSIM_IPAddress currIP = dsr_get_curr_ip();
34
35 for (unsigned int i = 0; i < nlength; ++i)
36 {
37 if (!IP_COMPARE(srcRouteOption->Address[i], currIP))
38 if (i != 0)
39 return srcRouteOption->Address[i - 1];
40 }
41
42 return packet->pstruNetworkData->szSourceIP;
43}
44/**
45This function generates a route error packet.
46*/
47int fn_NetSim_DSR_GenerateRERR(DSR_MAINT_BUFFER* maintBuffer,NetSim_EVENTDETAILS* pstruEventDetails)
48{
49 unsigned int length;
50 unsigned int loop;
51 NETSIM_ID recv;
52 NetSim_EVENTDETAILS pevent;
53 NetSim_PACKET* packet = maintBuffer->packetList;
54 DSR_OPTION_HEADER* option = (DSR_OPTION_HEADER*)packet->pstruNetworkData->Packet_RoutingProtocol;
55 DSR_SOURCE_ROUTE_OPTION* srcRouteOption = (DSR_SOURCE_ROUTE_OPTION*)option->options;
56 NETSIM_IPAddress nexthop = fnGetPrevHop(packet);
57 NetSim_PACKET* rerrPacket = fn_NetSim_DSR_GenerateCtrlPacket(pstruEventDetails->nDeviceId,
58 maintBuffer->source,
59 maintBuffer->source,
60 pstruEventDetails->dEventTime,
61 ctrlPacket_ROUTE_ERROR);
62 DSR_RERR_OPTION* rerrOption=calloc(1,sizeof* rerrOption);
63 option = calloc(1,sizeof* option);
64 rerrPacket->pstruNetworkData->Packet_RoutingProtocol = option;
65 option->nNextHeader = NO_NEXT_HEADER;
66 option->optType = optType_RouteError;
67 option->options = rerrOption;
68 rerrOption->nErrorType = NODE_UNREACHABLE;
69 rerrOption->errorSourceAddress = IP_COPY(rerrPacket->pstruNetworkData->szSourceIP);
70 rerrOption->errorDestinationAddress = IP_COPY(rerrPacket->pstruNetworkData->szDestIP);
71 rerrOption->TypeSpecificInformation = IP_COPY(fn_NetSim_Stack_GetIPAddressAsId(maintBuffer->nextHop,1));
72 rerrOption->nOptDataLen = DSR_RERR_SIZE_FIXED;
73 rerrOption->nOptionType = optType_RouteError;
74 length = /*(srcRouteOption->nOptDataLen-2)/4-*/srcRouteOption->nSegsLeft;
75 rerrOption->length = length;
76 rerrOption->nSegsLeft = length;
77 if (length)
78 {
79 rerrOption->Address = calloc(length, sizeof* rerrOption->Address);
80 for (loop = 0; loop < length; loop++)
81 rerrOption->Address[loop] = IP_COPY(srcRouteOption->Address[length - loop - 1]);
82 }
83
84 option->nPayloadLength = DSR_RERR_SIZE_FIXED;
85 rerrPacket->pstruNetworkData->nTTL= length +1;
86 rerrPacket->pstruNetworkData->dOverhead = DSR_RERR_SIZE_FIXED+DSR_OPTION_HEADER_SIZE;
87 rerrPacket->pstruNetworkData->dPacketSize = rerrPacket->pstruNetworkData->dOverhead;
88 rerrPacket->pstruNetworkData->szNextHopIp = IP_COPY(nexthop);
89 rerrPacket->nReceiverId = fn_NetSim_Stack_GetDeviceId_asIP(nexthop,&recv);
90
91 //Add network out event to transmit packet
92 memcpy(&pevent,pstruEventDetails,sizeof* pstruEventDetails);
93 pevent.nApplicationId = 0;
94 pevent.nEventType = NETWORK_OUT_EVENT;
95 pevent.nPacketId = 0;
96 pevent.nProtocolId = NW_PROTOCOL_DSR;
97 pevent.nSegmentId = 0;
98 pevent.nSubEventType = subevent_PROCESS_RERR;
99 pevent.pPacket = rerrPacket;
100 pevent.dPacketSize = rerrPacket->pstruNetworkData->dPacketSize;
101 pevent.szOtherDetails = NULL;
102 fnpAddEvent(&pevent);
103 //Update the metrics
104 DSR_DEV_VAR(pstruEventDetails->nDeviceId)->dsrMetrics.rerrSent++;
105 return 1;
106}
107/**
108This function process the route error that is received. It deletes the entry of the route to
109the target from the route cache and then forwards the Route error to the previous HOP.
110*/
111int fn_NetSim_DSR_ProcessRerr(NetSim_EVENTDETAILS* pstruEventDetails)
112{
113 char ip[_NETSIM_IP_LEN];
114 NetSim_PACKET* packet = pstruEventDetails->pPacket;
115 DSR_OPTION_HEADER* option = (DSR_OPTION_HEADER*)packet->pstruNetworkData->Packet_RoutingProtocol;
116 DSR_RERR_OPTION* rerr = (DSR_RERR_OPTION*)option->options;
117
118 //Delete entry from route cache
119 DSR_DELETE_ENTRY_CACHE(&(DSR_DEV_VAR(pstruEventDetails->nDeviceId)->pstruRouteCache),
120 rerr->errorSourceAddress,
121 (NETSIM_IPAddress)rerr->TypeSpecificInformation);
122
123 if(rerr->errorDestinationAddress!= fn_NetSim_Stack_GetIPAddressAsId(pstruEventDetails->pPacket->nReceiverId,1))
124 fn_NetSim_DSR_ForwardRERR(pstruEventDetails);
125 return 1;
126}
127/**
128This function frees the route error packet if RERR packet reaches the final destination.
129Otherwise, it forwards the REER packet to the previous HOP.
130*/
131int fn_NetSim_DSR_ForwardRERR(NetSim_EVENTDETAILS* pstruEventDetails)
132{
133 NETSIM_ID interfaceId;
134 NetSim_PACKET* packet = pstruEventDetails->pPacket;
135 DSR_OPTION_HEADER* option = packet->pstruNetworkData->Packet_RoutingProtocol;
136 DSR_RERR_OPTION* rerr = option->options;
137 packet->pstruNetworkData->szSourceIP = dsr_get_curr_ip();
138 if(rerr->nSegsLeft == 0)
139 {
140 fn_NetSim_Packet_FreePacket(pstruEventDetails->pPacket);
141 pstruEventDetails->pPacket = NULL;
142 return 0;
143 }
144 rerr->nSegsLeft--;
145
146 if(rerr->nSegsLeft!=2)
147 if(fn_NetSim_Stack_GetDeviceId_asIP(rerr->Address[rerr->nSegsLeft], &interfaceId)!=pstruEventDetails->nDeviceId)
148 packet->pstruNetworkData->szNextHopIp = IP_COPY(rerr->Address[rerr->nSegsLeft]);
149 else
150 packet->pstruNetworkData->szNextHopIp = IP_COPY(packet->pstruNetworkData->szDestIP);
151 else
152 packet->pstruNetworkData->szNextHopIp = IP_COPY(packet->pstruNetworkData->szDestIP);
153
154 packet->nTransmitterId = pstruEventDetails->nDeviceId;
155 packet->nReceiverId = fn_NetSim_Stack_GetDeviceId_asIP(packet->pstruNetworkData->szNextHopIp,&interfaceId);
156 //Add the network out event
157 pstruEventDetails->nEventType = NETWORK_OUT_EVENT;
158 pstruEventDetails->nSubEventType = 0;
159 fnpAddEvent(pstruEventDetails);
160 //update the metrics
161 DSR_DEV_VAR(pstruEventDetails->nDeviceId)->dsrMetrics.rerrForwarded++;
162 return 1;
163}
NetSim_PACKET * packetList
packetList - list of packets sent to the destination
Definition DSR.h:630
DSR_OPTION_TYPE nOptionType
Definition DSR.h:352
void * TypeSpecificInformation
Definition DSR.h:424
unsigned int nOptDataLen
Definition DSR.h:357
enum stru_NetSim_DSR_RERR_Option::@234164063043207073373340307253340021156076321263 nErrorType
NETSIM_IPAddress errorSourceAddress
Definition DSR.h:409
NETSIM_IPAddress errorDestinationAddress
Definition DSR.h:415
NETSIM_IPAddress * Address
Definition DSR.h:182