NetSim Source Code Help v14.4
All 13 Components
 
Loading...
Searching...
No Matches
BRP.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
18//#define DEBUG_BRP_MSG
19#ifdef DEBUG_BRP_MSG
20static FILE* fpDebugBRP;
21#endif
22
23int fn_NetSim_BRP_Init()
24{
25 NETSIM_ID i;
26
27#ifdef DEBUG_BRP_MSG
28 char s[BUFSIZ];
29 sprintf(s, "%s/%s",
30 pszIOPath,
31 "DebugBRP.csv");
32 fpDebugBRP = fopen(s, "w");
33 if (!fpDebugBRP)
34 {
35 fnSystemError("Unable to open DebugBRP.csv file\n");
36 perror(s);
37 }
38 else
39 {
40 fprintf(fpDebugBRP, "%s,%s,%s,%s,\n",
41 "Type",
42 "EventId",
43 "BRPHeader",
44 "EncapPacket");
45 }
46#endif
47
48 for(i=0;i<NETWORK->nDeviceCount;i++)
49 {
50 if(DEVICE_NWLAYER(i+1)->nRoutingProtocolId==NW_PROTOCOL_BRP)
51 {
52 NODE_ZRP* zrp=(NODE_ZRP*)DEVICE_NWROUTINGVAR(i+1);
53 zrp->brp=calloc(1,sizeof(NODE_BRP));
54 }
55 }
56 return 0;
57}
58
59int fn_NetSim_BRP_Update(ptrIP_ROUTINGTABLE iarpTable)
60{
61 NODE_ZRP* zrp=(NODE_ZRP*)DEVICE_NWROUTINGVAR(pstruEventDetails->nDeviceId);
62 NODE_BRP* brp=(NODE_BRP*)zrp->brp;
63 flushBodercastTree(brp);
64 flushZone(zrp);
65 flushIerpTable((NODE_IERP*)zrp->ierp);
66 addToIERPTableFromIARP((NODE_IERP*)zrp->ierp,iarpTable);
67 flushIERPTableFromIARP((NODE_IERP*)zrp->ierp,iarpTable);
68 while(iarpTable)
69 {
70 if(iarpTable->Metric == zrp->nZoneRadius)
71 addToBodercastTree(brp,iarpTable->networkDestination,iarpTable->gateway);
72 if(iarpTable->Metric <= zrp->nZoneRadius)
73 {
74 addToZoneList(zrp,iarpTable->networkDestination);
75 }
76 iarpTable=(ptrIP_ROUTINGTABLE)LIST_NEXT(iarpTable);
77 }
78 return 0;
79}
80
81int addToBodercastTree(NODE_BRP* brp,
82 NETSIM_IPAddress boderIP,
83 NETSIM_IPAddress nextHop)
84{
85 BRP_BODERCAST_TREE* bodercastTree=BRP_BODERCAST_TREE_ALLOC();
86 bodercastTree->boderId = fn_NetSim_Stack_GetDeviceId_asIP(boderIP,NULL);
87 bodercastTree->borderIP=IP_COPY(boderIP);
88 bodercastTree->nexthop=IP_COPY(nextHop);
89 LIST_ADD_LAST((void**)&brp->bodercastTree,bodercastTree);
90 return 0;
91}
92
93int fn_NetSim_BRP_BodercastPacket(NetSim_PACKET* dataPacket)
94{
95 NODE_ZRP* zrp=(NODE_ZRP*)DEVICE_NWROUTINGVAR(pstruEventDetails->nDeviceId);
96 NODE_BRP* brp=(NODE_BRP*)zrp->brp;
97 BRP_BODERCAST_TREE* tree=brp->bodercastTree;
98 while(tree)
99 {
100 NetSim_EVENTDETAILS pevent;
101 NetSim_PACKET* packet = fn_NetSim_ZRP_GeneratePacket(pstruEventDetails->dEventTime,
102 dataPacket->nControlDataType+BRP_DIFF,
103 NW_PROTOCOL_BRP,
104 pstruEventDetails->nDeviceId,
105 tree->boderId,
106 fnGetPacketSize(dataPacket)+BRP_PACKET_SIZE);
107 BRP_HEADER* header = (BRP_HEADER*)calloc(1,sizeof* header);
108 packet->pstruNetworkData->Packet_RoutingProtocol=header;
109 packet->pstruNetworkData->nTTL = dataPacket->pstruNetworkData->nTTL;
110 packet->pstruNetworkData->szNextHopIp = NULL;
111 header->EncapsulatedPacket = fn_NetSim_Packet_CopyPacket(dataPacket);
112
113#ifdef DEBUG_BRP_MSG
114 if (fpDebugBRP)
115 {
116 fprintf(fpDebugBRP, "%s,%lld,0x%p,0x%p,\n",
117 "Create",
118 pstruEventDetails->nEventId,
119 (void*)header,
120 (void*)header->EncapsulatedPacket);
121 fflush(fpDebugBRP);
122 }
123#endif
124 header->QuerySourceAddress = IP_COPY(DEVICE_NWADDRESS(pstruEventDetails->nDeviceId,1));
125 header->QueryDestinationAddress = IP_COPY(tree->borderIP);
126 header->QueryID = ++brp->nQueryId;
127 header->QueryExtension = 1;
128 header->PrevBordercastAddress = IP_COPY(DEVICE_NWADDRESS(pstruEventDetails->nDeviceId,1));
129
130 //write network out for transmit packet
131 pevent.dEventTime=pstruEventDetails->dEventTime;
132 pevent.dPacketSize=fnGetPacketSize(packet);
133 pevent.nApplicationId=0;
134 pevent.nDeviceId=pstruEventDetails->nDeviceId;
135 pevent.nDeviceType=pstruEventDetails->nDeviceType;
136 pevent.nEventType=NETWORK_OUT_EVENT;
137 pevent.nInterfaceId=0;
138 pevent.nPacketId=0;
139 pevent.nProtocolId=NW_PROTOCOL_IPV4;
140 pevent.nSegmentId=0;
141 pevent.nSubEventType=0;
142 pevent.pPacket=packet;
143 pevent.szOtherDetails=NULL;
144 fnpAddEvent(&pevent);
145
146 tree=(BRP_BODERCAST_TREE*)LIST_NEXT(tree);
147 }
148 return 0;
149}
150int fn_NetSim_BRP_ProcessPacket()
151{
152 bool bodercastFlag=false;
153 BRP_HEADER* header = (BRP_HEADER*)pstruEventDetails->pPacket->pstruNetworkData->Packet_RoutingProtocol;
154 bodercastFlag = fn_NetSim_IERP_ProcessPacket(header->EncapsulatedPacket);
155 if(bodercastFlag && !IP_COMPARE(pstruEventDetails->pPacket->pstruNetworkData->szDestIP,
156 DEVICE_NWADDRESS(pstruEventDetails->nDeviceId,1)))
157 {
158 NODE_ZRP* zrp=(NODE_ZRP*)DEVICE_NWROUTINGVAR(pstruEventDetails->nDeviceId);
159 NODE_BRP* brp=(NODE_BRP*)zrp->brp;
160 BRP_BODERCAST_TREE* tree=brp->bodercastTree;
161 header->QueryID = ++brp->nQueryId;
162 while(tree)
163 {
164 if(IP_COMPARE(tree->borderIP,header->PrevBordercastAddress))
165 {
166 NetSim_EVENTDETAILS pevent;
167 NetSim_PACKET* packet = fn_NetSim_Packet_CopyPacket(pstruEventDetails->pPacket);
168 BRP_HEADER* temp = (BRP_HEADER*)packet->pstruNetworkData->Packet_RoutingProtocol;
169
170 temp->PrevBordercastAddress = IP_COPY(DEVICE_NWADDRESS(pstruEventDetails->nDeviceId,1));
171
172 packet->pstruNetworkData->szNextHopIp = NULL;
173 packet->pstruNetworkData->szDestIP = IP_COPY(tree->borderIP);
174 packet->pstruNetworkData->szSourceIP = IP_COPY(DEVICE_NWADDRESS(pstruEventDetails->nDeviceId,1));
175 packet->pstruNetworkData->szGatewayIP = IP_COPY(DEVICE_NWADDRESS(pstruEventDetails->nDeviceId,1));
176 packet->pstruNetworkData->Packet_RoutingProtocol = temp;
177 packet->nSourceId = pstruEventDetails->nDeviceId;
178 remove_dest_from_packet(packet, get_first_dest_from_packet(packet));
179 add_dest_to_packet(packet, tree->boderId);
180 packet->nTransmitterId = pstruEventDetails->nDeviceId;
181 packet->nReceiverId = tree->boderId;
182 packet->pstruNetworkData->dOverhead = fnGetPacketSize(temp->EncapsulatedPacket)+BRP_PACKET_SIZE;
183 packet->pstruNetworkData->dPacketSize = fnGetPacketSize(temp->EncapsulatedPacket)+BRP_PACKET_SIZE;
184
185 //write network out for transmit packet
186 pevent.dEventTime=pstruEventDetails->dEventTime;
187 pevent.dPacketSize=fnGetPacketSize(packet);
188 pevent.nApplicationId=0;
189 pevent.nDeviceId=pstruEventDetails->nDeviceId;
190 pevent.nDeviceType=pstruEventDetails->nDeviceType;
191 pevent.nEventType=NETWORK_OUT_EVENT;
192 pevent.nInterfaceId=0;
193 pevent.nPacketId=0;
194 pevent.nProtocolId=NW_PROTOCOL_IPV4;
195 pevent.nSegmentId=0;
196 pevent.nSubEventType=0;
197 pevent.pPacket=packet;
198 pevent.szOtherDetails=NULL;
199 fnpAddEvent(&pevent);
200 }
201 tree=(BRP_BODERCAST_TREE*)LIST_NEXT(tree);
202 }
203 pstruEventDetails->pPacket=NULL;
204 }
205 else if(!bodercastFlag)
206 pstruEventDetails->pPacket = NULL;
207 return 0;
208}
209
210
211int fn_NetSim_BRP_FreeBRPHeader(NetSim_PACKET* packet)
212{
213 BRP_HEADER* header = (BRP_HEADER*)packet->pstruNetworkData->Packet_RoutingProtocol;
214#ifdef DEBUG_BRP_MSG
215 if (fpDebugBRP)
216 {
217 fprintf(fpDebugBRP, "%s,%lld,0x%p,0x%p,\n",
218 "Free",
219 pstruEventDetails->nEventId,
220 (void*)header,
221 (void*)header->EncapsulatedPacket);
222 fflush(fpDebugBRP);
223 }
224#endif
225 fn_NetSim_Packet_FreePacket(header->EncapsulatedPacket);
226 IP_FREE(header->PrevBordercastAddress);
227 IP_FREE(header->QueryDestinationAddress);
228 IP_FREE(header->QuerySourceAddress);
229 free(header);
230 return 0;
231}
232
233int fn_NetSim_BRP_CopyBRPHeader(const NetSim_PACKET* destPacket,const NetSim_PACKET* srcPacket)
234{
235 BRP_HEADER* dest= (BRP_HEADER*)calloc(1,sizeof* dest);
236 BRP_HEADER* src= (BRP_HEADER*)srcPacket->pstruNetworkData->Packet_RoutingProtocol;
237 destPacket->pstruNetworkData->Packet_RoutingProtocol = dest;
238 memcpy(dest,src,sizeof* dest);
239#ifdef DEBUG_BRP_MSG
240 if (fpDebugBRP)
241 {
242 fprintf(fpDebugBRP, "%s,%lld,0x%p,0x%p,0x%p,\n",
243 "After Copy",
244 pstruEventDetails->nEventId,
245 (void*)src,
246 (void*)dest->EncapsulatedPacket,
247 (void*)dest);
248 fflush(fpDebugBRP);
249 }
250#endif
251 dest->EncapsulatedPacket = fn_NetSim_Packet_CopyPacket(src->EncapsulatedPacket);
252#ifdef DEBUG_BRP_MSG
253 if (fpDebugBRP)
254 {
255 fprintf(fpDebugBRP, "%s,%lld,0x%p,0x%p,0x%p,\n",
256 "After Copy",
257 pstruEventDetails->nEventId,
258 (void*)src,
259 (void*)dest->EncapsulatedPacket,
260 (void*)dest);
261 fflush(fpDebugBRP);
262 }
263#endif
264 dest->PrevBordercastAddress = IP_COPY(src->PrevBordercastAddress);
265 dest->QueryDestinationAddress = IP_COPY(src->QueryDestinationAddress);
266 dest->QuerySourceAddress = IP_COPY(src->QuerySourceAddress);
267 return 0;
268}
269
270