NetSim Source Code Help
Loading...
Searching...
No Matches
DAO.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 "RPL.h"
16#include "RPL_Message.h"
17#include "../IP/IP.h"
18#include "RPL_enum.h"
19
21{
22 PRPL_NODE rpl = GET_RPL_NODE(d);
23
24 if (rpl_node_is_joined(rpl))
25 {
26 double min_time = rpl->DAODelayTime / 100;
27 double max_time = rpl->DAODelayTime;
28
29 UINT16 min_rank = RPL_RANK_ROOT;
30 UINT16 max_rank = INFINITE_RANK;
31
32 UINT16 rank = rpl->joined_dodag->rank;
33 double delay = max_time + min_time - ((rank - min_rank) * (max_time - min_time) / (max_rank - min_rank) + min_time);
34
35 return delay;
36 }
37 else
38 {
39 return rpl->DAODelayTime;
40 }
41}
42
44{
46 PRPL_NODE rpl = GET_RPL_NODE(d);
47
49 {
50 print_rpl_log("node '%d': canceled DAO mechanism", d);
51 return;
52 }
56
57 /* for every DAO unicast route send a DAO message to the preferred parent */
58 UINT i, route_count;
59 ptrIP_ROUTINGTABLE* route_list = iptable_get_table_by_type(IP_TABLE_GET(d), "RPL_DAO", &route_count);
60
61 for (i = 0; i < route_count; i++)
62 {
63 ptrIP_ROUTINGTABLE route = route_list[i];
65 route->prefix_len,
66 route->networkDestination);
67 }
68
69 if (route_list != NULL)
70 free(route_list);
71
72 /* send a DAO message for ourselves */
74 128,
75 DEVICE_NWADDRESS(d,1));
76 rpl_node_send_msg(d, dao_pdu);
77
78 /* reschedule the "Delay DAO timer" */
80 memset(&pevent, 0, sizeof pevent);
82 pevent.nDeviceId = d;
83 pevent.nDeviceType = DEVICE_TYPE(d);
84 pevent.nEventType = TIMER_EVENT;
86 pevent.nSubEventType = RPL_SEND_DAO;
87 fnpAddEvent(&pevent);
88}
89
91 ptrIP_ROUTINGTABLE* route_list,
92 UINT route_count)
93{
94 UINT i;
95 for (i = 0; i < route_count; i++)
96 {
97 if (!IP_COMPARE(target->Traget_Prefix, route_list[i]->networkDestination)
98 && target->Prefix_Length == route_list[i]->prefix_len)
99 return route_list[i];
100 }
101 return NULL;
102}
103
105{
108 PRPL_NODE drpl = GET_RPL_NODE(d);
109 PRPL_DIO_BASE daobase = daoPdu->Base;
111 UINT target_count;
113
114 bool* ispresent;
115 ispresent = (bool*)calloc(target_count, sizeof* ispresent);
116
117 /* check if the route already exists */
118 UINT i, route_count;
119 ptrIP_ROUTINGTABLE* route_list = iptable_get_table_by_type(IP_TABLE_GET(d), "RPL_DAO", &route_count);
120
121 for (i = 0; i < target_count; i++)
122 {
123 /* mark routes as updated, if they exist */
124 ptrIP_ROUTINGTABLE route;
125 route = check_is_route_is_present(target[i], route_list, route_count);
126 if (route)
127 {
128 ispresent[i] = true;
130#ifdef DEBUG_RPL_PRINT_DAO_ROUTE_INFOMATION
131 print_rpl_log("Node '%d',%0.3lfms: received dao msg with old route information. dest = %s, gateway=%s.",
132 d,
134 RPL_IP_TO_STR(target[i]->Traget_Prefix),
135 RPL_IP_TO_STR(route->gateway));
136#endif
137 }
138 }
139
140 if (route_list != NULL)
141 free(route_list);
142
143 for (i = 0; i < target_count; i++)
144 {
145 if (!ispresent[i])
146 { /* the route doesn't exist */
147#ifdef DEBUG_RPL_PRINT_DAO_ROUTE_INFOMATION
148 print_rpl_log("Node '%d',%0.3lfms: received dao msg with new route information. dest = %s, gateway= %s.",
149 d,
151 RPL_IP_TO_STR(target[i]->Traget_Prefix),
152 RPL_IP_TO_STR(gateway));
153#endif
154 NETSIM_ID inetrfaceId = 1;
156 target[i]->Traget_Prefix,
157 NULL,
158 target[i]->Prefix_Length,
159 gateway,
160 1,
161 &DEVICE_NWADDRESS(d, 1),
162 &inetrfaceId,
163 1,
164 "RPL_DAO");
166
167 /* schedule a timeout to remove this route */
168 NetSim_EVENTDETAILS pevent;
169 memset(&pevent, 0, sizeof pevent);
171 pevent.nDeviceId = d;
172 pevent.nDeviceType = DEVICE_TYPE(d);
173 pevent.nEventType = TIMER_EVENT;
175 pevent.nSubEventType = RPL_DAO_ROUTE_TIMEOUT;
176 fnpAddEvent(&pevent);
177 }
178 }
179 free(target);
180 free(ispresent);
181}
182
184{
185 double time = pstruEventDetails->dEventTime;
187 UINT i, route_count;
188 double minupdatetime = 0xFFFFFFFFFFFF;
189 ptrIP_ROUTINGTABLE* route_list = iptable_get_table_by_type(IP_TABLE_GET(d), "RPL_DAO", &route_count);
190 for (i = 0; i < route_count; i++)
191 {
192 ptrIP_ROUTINGTABLE route = route_list[i];
193 if (route->update_time + RPL_DEFAULT_DAO_REMOVE_TIMEOUT <= time)
194 {
195#ifdef DEBUG_RPL_PRINT_DAO_ROUTE_INFOMATION
196 print_rpl_log("Node '%d',%0.3lfms: Route %s, prefix %d is expired.",
197 d,
198 time/1000,
200 route->prefix_len);
201#endif
202 //Delete the old route
204 }
205 else
206 {
207 minupdatetime = min(minupdatetime, route->update_time);
208 }
209 }
210 //Add the route time out event
213
214 if (route_list)
215 free(route_list);
216}
217
219{
221 PRPL_DAO_BASE b = rpl->Base;
222
223 free(b);
224
225 UINT i;
226 for (i = 0; i < rpl->option_count; i++)
228
229 free(rpl);
230}
231
232void rpl_dao_msg_copy(const NetSim_PACKET* destPacket, const NetSim_PACKET* srcPacket)
233{
235 PRPL_CTRL_MSG drpl = (PRPL_CTRL_MSG)calloc(1, sizeof* drpl);
236 memcpy(drpl, srpl, sizeof* drpl);
237 destPacket->pstruNetworkData->Packet_RoutingProtocol = drpl;
238
239 PRPL_DAO_BASE b = srpl->Base;
240 PRPL_DAO_BASE db = (PRPL_DAO_BASE)calloc(1, sizeof* db);
241 memcpy(db, b, sizeof* db);
242 drpl->Base = db;
243
244 UINT i;
245 drpl->options = (PRPL_OPTION*)calloc(drpl->option_count, sizeof* drpl->options);
246 for (i = 0; i < srpl->option_count; i++)
247 drpl->options[i] = rpl_option_copy(srpl->options[i]);
248}
unsigned int NETSIM_ID
Definition: Animation.h:45
void rpl_dao_msg_copy(const NetSim_PACKET *destPacket, const NetSim_PACKET *srcPacket)
Definition: DAO.c:232
static double compute_dao_delay(NETSIM_ID d)
Definition: DAO.c:20
void rpl_process_dao_msg()
Definition: DAO.c:104
void rpl_dao_msg_destroy(NetSim_PACKET *packet)
Definition: DAO.c:218
void rpl_send_dao()
Definition: DAO.c:43
static ptrIP_ROUTINGTABLE check_is_route_is_present(PRPL_TARGET_OPTION target, ptrIP_ROUTINGTABLE *route_list, UINT route_count)
Definition: DAO.c:90
void rpl_dao_route_timeout()
Definition: DAO.c:183
int iptable_delete_by_route(ptrIP_WRAPPER wrapper, ptrIP_ROUTINGTABLE route)
ptrIP_ROUTINGTABLE iptable_add(ptrIP_WRAPPER wrapper, NETSIM_IPAddress dest, NETSIM_IPAddress subnet, unsigned int prefix_len, NETSIM_IPAddress gateway, UINT interfaceCount, NETSIM_IPAddress *interfaceIp, NETSIM_ID *interfaceId, unsigned int metric, char *type)
ptrIP_ROUTINGTABLE * iptable_get_table_by_type(ptrIP_ROUTINGTABLE table, char *type, UINT *count)
NETSIM_IPAddress IP_COPY(NETSIM_IPAddress ip)
#define IP_COMPARE(ip1, ip2)
Definition: IP_Addressing.h:67
#define UINT
Definition: Linux.h:38
#define UINT16
Definition: Linux.h:33
#define min(a, b)
Definition: Linux.h:106
#define free(p)
Definition: Memory.h:31
#define calloc(c, s)
Definition: Memory.h:29
#define GET_RPL_NODE(d)
Definition: RPL.h:212
bool rpl_node_is_joined(PRPL_NODE r)
#define RPL_RANK_ROOT
Definition: RPL.h:82
void print_rpl_log(char *format,...)
#define INFINITE_RANK
Definition: RPL.h:81
#define RPL_DEFAULT_DAO_REMOVE_TIMEOUT
Definition: RPL.h:88
#define RPL_IP_TO_STR(ip)
Definition: RPL.h:316
void create_and_add_rpl_target_option(NetSim_PACKET *dao_pdu, UINT8 prefix_len, NETSIM_IPAddress dest)
Definition: RPL_Message.c:379
PRPL_OPTION rpl_option_copy(PRPL_OPTION option)
Definition: RPL_Message.c:114
void rpl_node_send_msg(NETSIM_ID ndevid, NetSim_PACKET *packet)
Definition: RPL_Message.c:418
void ** get_all_option_from_msg(PRPL_CTRL_MSG msg, RPL_OPTION_TYPE type, UINT *count)
Definition: RPL_Message.c:73
NetSim_PACKET * create_dao_message(NETSIM_ID ndevid, double time, NETSIM_ID parent)
Definition: RPL_Message.c:370
void rpl_option_destroy(PRPL_OPTION option)
Definition: RPL_Message.c:96
@ RPLOPTION_RPLTARGET
Definition: RPL_Message.h:46
#define GET_PRPL_CTRL_MSG(packet)
Definition: RPL_Message.h:83
struct stru_rpl_ctrl_message * PRPL_CTRL_MSG
struct stru_rpl_dao_base * PRPL_DAO_BASE
#define DEVICE_TYPE(DeviceId)
Definition: Stack.h:773
#define DEVICE_NWADDRESS(DeviceId, InterfaceId)
Definition: Stack.h:805
@ NW_PROTOCOL_RPL
Definition: Stack.h:200
@ TIMER_EVENT
Definition: Stack.h:114
EXPORTED struct stru_NetSim_EventDetails * pstruEventDetails
Definition: Stack.h:837
#define IP_WRAPPER_GET(DeviceId)
Definition: Stack.h:801
#define IP_TABLE_GET(DeviceId)
Definition: Stack.h:802
#define fnpAddEvent(pstruEvent)
Definition: main.h:191
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
netsimDEVICE_TYPE nDeviceType
Definition: Stack.h:749
Structure to store ip routing table.
Definition: IP.h:157
unsigned int prefix_len
Definition: IP.h:163
NETSIM_IPAddress networkDestination
Definition: IP.h:158
NETSIM_IPAddress gateway
Definition: IP.h:160
NETSIM_IPAddress szSourceIP
Definition: Packet.h:198
struct stru_NetSim_Packet_NetworkLayer * pstruNetworkData
Definition: Packet.h:275
PRPL_OPTION * options
Definition: RPL_Message.h:80
UINT16 rank
Definition: RPL.h:158
PRPL_NEIGHBOR pref_parent
Definition: RPL.h:164
bool dao_supported
Definition: RPL.h:146
NETSIM_ID nodeId
Definition: RPL.h:116
PRPL_DODAG joined_dodag
Definition: RPL.h:178
double DAODelayTime
Definition: RPL.h:172
NETSIM_IPAddress Traget_Prefix
Definition: RPL_Message.h:306