NetSim Source Code Help v14.4
All 13 Components
 
Loading...
Searching...
No Matches
OLSR_TC.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 "ZRP.h"
16#include "ZRP_Enum.h"
17
18int fn_NetSim_OLSR_ScheduleTCTransmission(NETSIM_ID nNodeId,double dTime)
19{
20 double jitter;
21 NODE_OLSR* olsr=GetOLSRData(nNodeId);
22 jitter = (int)((fn_NetSim_Utilities_GenerateRandomNo(&NETWORK->ppstruDeviceList[nNodeId-1]->ulSeed[0],
23 &NETWORK->ppstruDeviceList[nNodeId-1]->ulSeed[1])/NETSIM_RAND_MAX)*MAXJITTER);
24 pstruEventDetails->dEventTime = dTime+olsr->dTCInterval-jitter;
25 pstruEventDetails->dPacketSize=0;
26 pstruEventDetails->nApplicationId=0;
27 pstruEventDetails->nDeviceId=nNodeId;
28 pstruEventDetails->nDeviceType=DEVICE_TYPE(nNodeId);
29 pstruEventDetails->nEventType=TIMER_EVENT;
30 pstruEventDetails->nInterfaceId=1;
31 pstruEventDetails->nPacketId=0;
32 pstruEventDetails->nProtocolId=NW_PROTOCOL_OLSR;
33 pstruEventDetails->nSegmentId=0;
34 pstruEventDetails->nSubEventType=OLSR_ScheduleTCTransmission;
35 pstruEventDetails->pPacket=NULL;
36 pstruEventDetails->szOtherDetails=NULL;
37 fnpAddEvent(pstruEventDetails);
38 return 0;
39}
40int fn_NetSim_OLSR_TransmitTCMessage()
41{
42 NetSim_EVENTDETAILS pevent;
43 NODE_OLSR* olsr=GetOLSRData(pstruEventDetails->nDeviceId);
44 NetSim_PACKET* packet;
45 OLSR_HEADER* header=(OLSR_HEADER*)calloc(1,sizeof* header);
46 OLSR_TC_MESSAGE* tc=(OLSR_TC_MESSAGE*)calloc(1,sizeof* tc);
47 OLSR_MPR_SELECTION_SET* set=olsr->mprSelectionSet;
48 olsr->bTCUpdateFlag=false;
49 //Fill TC information
50 tc->ANSN = ++olsr->ANSN;
51 while(set)
52 {
53 tc->AdvertisedNeighborCount++;
54 tc->AdvertisedNeighborMainAddress=(NETSIM_IPAddress*)realloc(tc->AdvertisedNeighborMainAddress,
55 tc->AdvertisedNeighborCount*sizeof* tc->AdvertisedNeighborMainAddress);
56 tc->AdvertisedNeighborMainAddress[tc->AdvertisedNeighborCount-1]=IP_COPY(set->MS_main_addr);
57 set=(OLSR_MPR_SELECTION_SET*)LIST_NEXT(set);
58 }
59
60 //Fill header information
61 header->message = (OLSR_HEADER_MESSAGE*)calloc(1,sizeof* header->message);
62 header->message->HopCount=min(olsr->nZoneRadius,TC_MESSAGE_TTL)+1;
63 header->message->MESSAGE=tc;
64 header->message->MessageSequenceNumber=++olsr->nMessageSequenceNumber;
65 header->message->MessageType = TC_MESSAGE;
66 header->message->OriginatorAddress = IP_COPY(olsr->mainAddress);
67 header->message->TimeToLive = min(olsr->nZoneRadius,TC_MESSAGE_TTL);
68 header->message->Vtime = olsrConvertDoubleToME(TOP_HOLD_TIME);
69 header->message->MessageSize = TC_MESSAGE_SIZE_FIXED+tc->AdvertisedNeighborCount*4+MESSAGE_HEADER_SIZE;
70
71 header->PacketLength = header->message->MessageSize+OLSR_HEADER_SIZE;
72 header->PacketSequenceNumber = ++olsr->nPacketSequenceNumber;
73
74 //Create Packet
75 packet=fn_NetSim_ZRP_GeneratePacket(pstruEventDetails->dEventTime,
76 OLSR_CONTROL_PACKET(TC_MESSAGE),
77 NW_PROTOCOL_OLSR,
78 pstruEventDetails->nDeviceId,
79 0,
80 header->PacketLength);
81
82 packet->pstruNetworkData->Packet_RoutingProtocol=header;
83
84 packet->pstruNetworkData->szNextHopIp = NULL;
85 packet->pstruNetworkData->nTTL = min(olsr->nZoneRadius,TC_MESSAGE_TTL);
86 //Add network out event
87 pevent.dEventTime=pstruEventDetails->dEventTime;
88 pevent.dPacketSize = header->PacketLength;
89 pevent.nDeviceId=pstruEventDetails->nDeviceId;
90 pevent.nDeviceType=pstruEventDetails->nDeviceType;
91 pevent.nApplicationId = 0;
92 pevent.nEventType = NETWORK_OUT_EVENT;
93 pevent.nInterfaceId = 0;
94 pevent.nPacketId = 0;
95 pevent.nProtocolId = NW_PROTOCOL_IPV4;
96 pevent.nSegmentId = 0;
97 pevent.nSubEventType = 0;
98 pevent.pPacket = packet;
99 fnpAddEvent(&pevent);
100 return 0;
101}
102int fn_NetSim_OLSR_CopyTCMessage(OLSR_HEADER_MESSAGE* dest,OLSR_HEADER_MESSAGE* src)
103{
104 unsigned int i;
105 OLSR_TC_MESSAGE* srcTC=(OLSR_TC_MESSAGE*)src->MESSAGE;
106 OLSR_TC_MESSAGE* destTC = (OLSR_TC_MESSAGE*)calloc(1,sizeof* destTC);
107 destTC->AdvertisedNeighborCount=srcTC->AdvertisedNeighborCount;
108 destTC->ANSN=srcTC->ANSN;
109 destTC->Reserved=srcTC->Reserved;
110 if(destTC->AdvertisedNeighborCount)
111 destTC->AdvertisedNeighborMainAddress=(NETSIM_IPAddress*)calloc(destTC->AdvertisedNeighborCount,sizeof* destTC->AdvertisedNeighborMainAddress);
112 for(i=0;i<srcTC->AdvertisedNeighborCount;i++)
113 destTC->AdvertisedNeighborMainAddress[i]=IP_COPY(srcTC->AdvertisedNeighborMainAddress[i]);
114 dest->MESSAGE=destTC;
115 return 0;
116}
117int fn_NetSim_OLSR_FreeTCMessage(OLSR_HEADER_MESSAGE* message)
118{
119 unsigned int i;
120 OLSR_TC_MESSAGE* tc = (OLSR_TC_MESSAGE*)message->MESSAGE;
121 for(i=0;i<tc->AdvertisedNeighborCount;i++)
122 IP_FREE(tc->AdvertisedNeighborMainAddress[i]);
123 free(tc->AdvertisedNeighborMainAddress);
124 free(tc);
125 message->MESSAGE=NULL;
126 return 0;
127}
128
129int fn_NetSim_OLSR_ReceiveTC()
130{
131 //Section 9.5
132 NetSim_PACKET* packet=pstruEventDetails->pPacket;
133 NODE_OLSR* olsr=GetOLSRData(pstruEventDetails->nDeviceId);
134 OLSR_HEADER* header=(OLSR_HEADER*)pstruEventDetails->pPacket->pstruNetworkData->Packet_RoutingProtocol;
135 OLSR_TC_MESSAGE* tc = (OLSR_TC_MESSAGE*)header->message->MESSAGE;
136 //Condition 1
137 OLSR_NEIGHBOR_SET* neighbor=olsrFindNeighborSet(olsr->neighborSet,pstruEventDetails->pPacket->pstruNetworkData->szGatewayIP);
138 if(IP_COMPARE(header->message->OriginatorAddress,pstruEventDetails->pPacket->pstruNetworkData->szGatewayIP) &&
139 neighbor && neighbor->N_status==SYM_NEIGH)
140 {
141 fn_NetSim_Packet_FreePacket(pstruEventDetails->pPacket);
142 pstruEventDetails->pPacket=NULL;
143 return -1;
144 }
145 //Condition 2
146 if(olsrValidateTopologyInfoOnCondition2(olsr->topologyInfoBase,header->message->OriginatorAddress,tc->ANSN))
147 {
148 fn_NetSim_Packet_FreePacket(pstruEventDetails->pPacket);
149 pstruEventDetails->pPacket=NULL;
150 return -2;
151 }
152
153 //Condition 4
154 olsrUpdateTopologySet(olsr,&olsr->topologyInfoBase,header->message);
155
156
157 fn_NetSim_OLSR_PacketForwarding();
158 fn_NetSim_Packet_FreePacket(packet);
159 pstruEventDetails->pPacket=NULL;
160 return 0;
161}
162
163OLSR_TOPOLOGY_INFORMATION_BASE* olsrFindTopologyInfo(OLSR_TOPOLOGY_INFORMATION_BASE* topology,NETSIM_IPAddress originator,NETSIM_IPAddress neighbor)
164{
165 while(topology)
166 {
167 if(!IP_COMPARE(topology->T_last_addr,originator) &&
168 !IP_COMPARE(topology->T_dest_addr,neighbor))
169 return topology;
170 topology=(OLSR_TOPOLOGY_INFORMATION_BASE*)LIST_NEXT(topology);
171 }
172 return NULL;
173}
174
175bool olsrValidateTopologyInfoOnCondition2(OLSR_TOPOLOGY_INFORMATION_BASE* topology,NETSIM_IPAddress originator,unsigned short int ANSN)
176{
177 while(topology)
178 {
179 if(!IP_COMPARE(topology->T_last_addr,originator) &&
180 topology->T_seq > ANSN)
181 return true;
182 topology = (OLSR_TOPOLOGY_INFORMATION_BASE*)LIST_NEXT(topology);
183 }
184 return false;
185}
186int olsrRemoveFromTopologySet(OLSR_TOPOLOGY_INFORMATION_BASE** topology,NETSIM_IPAddress originator,unsigned short int ANSN)
187{
188 OLSR_TOPOLOGY_INFORMATION_BASE* info=*topology;
189 while(info)
190 {
191 if(!IP_COMPARE(info->T_last_addr,originator) &&
192 info->T_seq < ANSN)
193 {
194 LIST_FREE((void**)topology,info);
195 info=*topology;
196 continue;
197 }
198 info=(OLSR_TOPOLOGY_INFORMATION_BASE*)LIST_NEXT(info);
199 }
200 return 0;
201}
202int olsrUpdateTopologySet(NODE_OLSR* olsr,OLSR_TOPOLOGY_INFORMATION_BASE** topology,OLSR_HEADER_MESSAGE* message)
203{
204 bool flag = false;
205 unsigned int i;
206 OLSR_TC_MESSAGE* tc = (OLSR_TC_MESSAGE*)message->MESSAGE;
207 OLSR_TOPOLOGY_INFORMATION_BASE* info;
208 double validityTime=olsrConvertMEToDouble(message->Vtime);
209 for(i=0;i<tc->AdvertisedNeighborCount;i++)
210 {
211 info=olsrFindTopologyInfo(*topology,message->OriginatorAddress,tc->AdvertisedNeighborMainAddress[i]);
212 if(info)
213 {
214 //condition 4.1
215 info->T_time=validityTime*SECOND+pstruEventDetails->dEventTime;
216 }
217 else
218 {
219 //Condition 4.2
220 info=TOPOLOGY_INFO_BASE_ALLOC();
221 info->T_dest_addr=IP_COPY(tc->AdvertisedNeighborMainAddress[i]);
222 info->T_last_addr=IP_COPY(message->OriginatorAddress);
223 info->T_seq=tc->ANSN;
224 info->T_time=validityTime*SECOND+pstruEventDetails->dEventTime;
225 LIST_ADD_LAST((void**)topology,info);
226 olsr->bRoutingTableUpdate=true;
227 flag=true;
228 }
229 }
230 if(flag && !olsr->bTCUpdateFlag)
231 {
232 NetSim_EVENTDETAILS pevent;
233 pevent.nSubEventType = OLSR_ScheduleTCTransmission;
234 pevent.dEventTime =pstruEventDetails->dEventTime + (fn_NetSim_Utilities_GenerateRandomNo(
235 &NETWORK->ppstruDeviceList[pstruEventDetails->nDeviceId-1]->ulSeed[0],
236 &NETWORK->ppstruDeviceList[pstruEventDetails->nDeviceId-1]->ulSeed[1])/NETSIM_RAND_MAX)*SECOND;
237 pevent.dPacketSize=0;
238 pevent.nApplicationId=0;
239 pevent.nDeviceId=pstruEventDetails->nDeviceId;
240 pevent.nDeviceType=pstruEventDetails->nDeviceType;
241 pevent.nEventType=TIMER_EVENT;
242 pevent.nInterfaceId=1;
243 pevent.nPacketId=0;
244 pevent.nProtocolId=NW_PROTOCOL_OLSR;
245 pevent.pPacket=NULL;
246 pevent.nSegmentId=0;
247 pevent.szOtherDetails=NULL;
248 fnpAddEvent(&pevent);
249 olsr->bTCUpdateFlag=true;
250 }
251 return 0;
252}
253