NetSim Source Code Help
Loading...
Searching...
No Matches
PIM_Hello.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
15#include "main.h"
16#include "List.h"
17#include "IP.h"
18#include "PIM_SM.h"
19#include "PIM_Msg.h"
20
22 UINT16 type,
23 UINT16 len,
24 void* opt)
25{
26 ptrPIM_HELLO hello = get_PIM_MSG(packet);
27 if (hello->optCount)
28 {
29 hello->option = realloc(hello->option, (hello->optCount + 1) * sizeof* hello->option);
30 }
31 else
32 {
33 hello->option = calloc(1, sizeof* hello->option);
34 }
35 hello->option[hello->optCount] = calloc(1, sizeof* hello->option[hello->optCount]);
36 hello->option[hello->optCount]->optionValue = opt;
37 hello->option[hello->optCount]->optionLength = len;
38 hello->option[hello->optCount]->optionType = type;
39 hello->optCount++;
40
43}
44
45static void* get_option_from_hello(ptrPIM_HELLO hello, UINT16 type)
46{
47 UINT i;
48 for (i = 0; i < hello->optCount; i++)
49 {
50 if (hello->option[i]->optionType == type)
51 return hello->option[i]->optionValue;
52 }
53 return NULL;
54}
55
57{
58 ptrOPTION_DRPRIORITY pri = calloc(1, sizeof* pri);
59 pri->DRPriority = GET_PIM_VAR(d)->DRPriority;
63 pri);
64 print_pim_sm_log("Adding DR Priority option. DR Priority = %d", pri->DRPriority);
65}
66
68{
69 ptrOPTION_GENERATIONID gen = calloc(1, sizeof* gen);
70 gen->GenerationId = GET_PIM_VAR(d)->genId;
74 gen);
75 print_pim_sm_log("Adding Generation Id option. Generation Id = %d", gen->GenerationId);
76}
77
79{
80 ptrOPTION_LANPRUNEDELAY lpd = calloc(1, sizeof* lpd);
81 lpd->overrideInterval = GET_PIM_VAR(d)->overrideInterval;
82 lpd->propagationDelay = GET_PIM_VAR(d)->propagationDelay;
83 lpd->T = false;
87 lpd);
88 print_pim_sm_log("Adding Lan prune delay option.");
89}
90
92{
93 ptrOPTION_ADDRLIST addrList = calloc(1, sizeof* addrList);
94 UINT16 len = 0;
95 NETSIM_ID c = DEVICE(d)->nNumOfInterface;
96 NETSIM_ID i;
97 if (c <= 1)
98 return; //No secondary interface
99 addrList->SeconadayAddr = calloc(c, sizeof* addrList->SeconadayAddr);
100 for (i = 0; i < c; i++)
101 {
102 addrList->SeconadayAddr[i] = encode_unicast_addr(DEVICE_NWADDRESS(d, i + 1));
104 }
105
106 len -= ENCODED_UNICAST_ADDR_LEN; //Remove source addr
107 addrList->c = c - 1;
108
109
110 set_option_in_hello(packet,
112 len,
113 addrList);
114 print_pim_sm_log("Adding secondary address list option.");
115}
116
118{
119 ptrPIM_HELLO hello = (ptrPIM_HELLO)calloc(1, sizeof* hello);
120 set_pim_hdr(&hello->hdr, PIMMSG_Hello);
121
122 return hello;
123}
124
126 double time)
127{
129 NETSIM_ID d = 0;
131 hello,
132 time,
133 dev,
134 DEVICE_NWADDRESS(dev, 1),
135 1,
136 &d,
138 1);
139 return packet;
140}
141
142void send_hello_msg(NETSIM_ID d, double time)
143{
144 ptrPIM_VAR var = GET_PIM_VAR(d);
145 NetSim_PACKET* packet = create_pim_hello(d, time);
146
147 add_dr_priority_option(d, packet);
148
149 add_genid_option(d, packet);
150
151 add_lanprunedelay_option(d, packet);
152
153 add_addresslist_option(d, packet);
154
155 send_pim_msg(d, time, packet);
156 print_pim_sm_log("Adding timer event %s at %0.3lf\n",
157 "PIM_SEND_HELLO",
158 (time + var->helloPeriod) / 1000);
160}
161
163{
164 bool isNeighbor = false;
168 ptrPIM_HELLO hello = get_PIM_MSG(packet);
169
171 ptrPIM_NEIGHBOR neigh = find_neighbor(d, src);
172 isNeighbor = neigh ? true : false;
173
174 if (!isNeighbor)
175 {
176 print_pim_sm_log("New neighbor %s is found on interface %d",
177 src->str_ip, ifid);
178 neigh = create_and_add_neighbor(d, ifid, src);
179 }
180
182 if (gen)
183 neigh->gen_id = gen->GenerationId;
184
186 if (pri)
187 {
188 neigh->dr_priority = pri->DRPriority;
189 neigh->dr_priority_present = true;
190 }
191
192 double t;
194 if (hold)
195 t = hold->holdTime*MILLISECOND;
196 else
197 t = GET_PIM_VAR(d)->helloPeriod*3.5;
198 if (!neigh->isTimeoutAdded)
199 {
200 print_pim_sm_log("Adding neighbor timeout event at %0.3lf",
201 (pstruEventDetails->dEventTime + t) / 1000);
205 NULL);
206 }
208 neigh->isTimeoutAdded = true;
209 print_pim_sm_log("Neighbor details updated as DR_priority=%d, GenId=%d, Timeout=%0.3lf",
210 neigh->dr_priority,
211 neigh->gen_id,
212 neigh->timeout / 1000);
213
214 elect_DR(d, ifid);
215
217 if (lpd)
218 {
219 neigh->lan_prune_delay_present = true;
222 neigh->tracking_support = lpd->T;
223 }
224 else
225 {
226 neigh->lan_prune_delay_present = false;
227 }
228
230 if (addrlist)
231 {
232 neigh->secondary_address_count = addrlist->c;
233 assert(addrlist->c);
234 UINT i;
235 UINT k;
237 neigh->secondary_address_list = calloc(addrlist->c, sizeof* neigh->secondary_address_list);
238 for (i = 0, k = 0; k < addrlist->c; i++,k++)
239 {
240 if (!IP_COMPARE(addrlist->SeconadayAddr[i]->unicastAddr, src))
241 {
242 k--;
243 continue;
244 }
245 neigh->secondary_address_list[k] = addrlist->SeconadayAddr[i]->unicastAddr;
246 }
247 }
248 else
249 {
250 neigh->secondary_address_count = 0;
252 neigh->secondary_address_list = NULL;
253 }
254 return true;
255}
unsigned int NETSIM_ID
Definition: Animation.h:45
@ EVENT_PIM_NEIGHBOR_TIMEOUT
Definition: IP.h:107
@ EVENT_PIM_SEND_HELLO
Definition: IP.h:106
#define IP_COMPARE(ip1, ip2)
Definition: IP_Addressing.h:67
#define c
#define UINT
Definition: Linux.h:38
#define UINT16
Definition: Linux.h:33
#define realloc(p, s)
Definition: Memory.h:32
#define free(p)
Definition: Memory.h:31
#define calloc(c, s)
Definition: Memory.h:29
static void add_genid_option(NETSIM_ID d, NetSim_PACKET *packet)
Definition: PIM_Hello.c:67
static void add_dr_priority_option(NETSIM_ID d, NetSim_PACKET *packet)
Definition: PIM_Hello.c:56
void send_hello_msg(NETSIM_ID d, double time)
Definition: PIM_Hello.c:142
static void add_lanprunedelay_option(NETSIM_ID d, NetSim_PACKET *packet)
Definition: PIM_Hello.c:78
static void set_option_in_hello(NetSim_PACKET *packet, UINT16 type, UINT16 len, void *opt)
Definition: PIM_Hello.c:21
static void * get_option_from_hello(ptrPIM_HELLO hello, UINT16 type)
Definition: PIM_Hello.c:45
bool process_pim_hello_packet()
Definition: PIM_Hello.c:162
static NetSim_PACKET * create_pim_hello(NETSIM_ID dev, double time)
Definition: PIM_Hello.c:125
static void add_addresslist_option(NETSIM_ID d, NetSim_PACKET *packet)
Definition: PIM_Hello.c:91
static ptrPIM_HELLO alloc_pim_hello()
Definition: PIM_Hello.c:117
void set_pim_hdr(ptrPIM_HDR hdr, PIMMSG type)
Definition: PIM_Msg.c:145
NetSim_PACKET * create_pim_packet(PIMMSG type, void *opt, double time, NETSIM_ID source, NETSIM_IPAddress sourceAddrss, UINT destCount, NETSIM_ID *destList, NETSIM_IPAddress group, UINT ttl)
Definition: PIM_Msg.c:91
void * get_PIM_MSG(NetSim_PACKET *packet)
Definition: PIM_Msg.c:21
void send_pim_msg(NETSIM_ID d, double time, NetSim_PACKET *packet)
Definition: PIM_Msg.c:125
ptrENCODED_UNICAST_ADDR encode_unicast_addr(NETSIM_IPAddress ip)
Definition: PIM_Msg.c:151
#define PIM_OPTION_DRPRIORITY_LEN
Definition: PIM_Msg.h:202
struct stru_pim_Hello * ptrPIM_HELLO
#define PIM_OPTION_HOLDTIME_TYPE
Definition: PIM_Msg.h:165
#define ENCODED_UNICAST_ADDR_LEN
Definition: PIM_Msg.h:88
#define PIM_OPTION_LANPRUNEDELAY_LEN
Definition: PIM_Msg.h:184
@ PIMMSG_Hello
Definition: PIM_Msg.h:41
#define PIM_OPTION_GENERATIONID_LEN
Definition: PIM_Msg.h:220
#define PIM_OPTION_LANPRUNEDELAY_TYPE
Definition: PIM_Msg.h:185
#define PIM_OPTION_ADDRLIST_TYPE
Definition: PIM_Msg.h:244
#define PIM_HELLO_OPTION_LEN
Definition: PIM_Msg.h:147
#define PIM_OPTION_GENERATIONID_TYPE
Definition: PIM_Msg.h:221
#define PIM_OPTION_DRPRIORITY_TYPE
Definition: PIM_Msg.h:203
void elect_DR(NETSIM_ID d, NETSIM_ID ifid)
Definition: PIM_Neighbor.c:95
ptrPIM_NEIGHBOR find_neighbor(NETSIM_ID d, NETSIM_IPAddress ip)
Definition: PIM_Neighbor.c:20
ptrPIM_NEIGHBOR create_and_add_neighbor(NETSIM_ID d, NETSIM_ID ifId, NETSIM_IPAddress address)
Definition: PIM_Neighbor.c:46
void pim_add_timeout_event(NETSIM_ID d, double time, IP_SUBEVENT eve, NETSIM_IPAddress group)
Definition: PIM_SM.c:50
void print_pim_sm_log(char *format,...)
Definition: PIM_SM.c:38
#define GET_PIM_VAR(d)
Definition: PIM_SM.h:226
NETSIM_IPAddress ALL_PIM_ROUTERS_ADDRESS
Definition: PIM_SM.h:48
#define DEVICE(DeviceId)
Definition: Stack.h:769
#define DEVICE_NWADDRESS(DeviceId, InterfaceId)
Definition: Stack.h:805
#define MILLISECOND
Definition: Stack.h:59
EXPORTED struct stru_NetSim_EventDetails * pstruEventDetails
Definition: Stack.h:837
struct stru_NetSim_Packet * pPacket
Definition: Stack.h:754
NETSIM_ID nDeviceId
Definition: Stack.h:750
NETSIM_ID nInterfaceId
Definition: Stack.h:751
NETSIM_IPAddress szSourceIP
Definition: Packet.h:198
struct stru_NetSim_Packet_NetworkLayer * pstruNetworkData
Definition: Packet.h:275
char str_ip[_NETSIM_IP_LEN]
Definition: IP_Addressing.h:54
ptrENCODED_UNICAST_ADDR * SeconadayAddr
Definition: PIM_Msg.h:241
ptrPIM_HELLO_OPTION * option
Definition: PIM_Msg.h:276
PIM_HDR hdr
Definition: PIM_Msg.h:274
UINT optCount
Definition: PIM_Msg.h:275
NETSIM_IPAddress unicastAddr
Definition: PIM_Msg.h:86
UINT16 propagation_delay
Definition: PIM_SM.h:170
bool tracking_support
Definition: PIM_SM.h:169
double timeout
Definition: PIM_SM.h:166
UINT16 override_interval
Definition: PIM_SM.h:171
NETSIM_IPAddress * secondary_address_list
Definition: PIM_SM.h:174
bool dr_priority_present
Definition: PIM_SM.h:164
bool lan_prune_delay_present
Definition: PIM_SM.h:168
bool isTimeoutAdded
Definition: PIM_SM.h:165
UINT16 dr_priority
Definition: PIM_SM.h:163
UINT secondary_address_count
Definition: PIM_SM.h:173
double helloPeriod
Definition: PIM_SM.h:219