blob: a4f3e3352f8f52c68a079328f0ba71d98b7c3b25 [file] [log] [blame]
akmhoque3560cb62012-09-09 10:52:30 -05001#include<stdio.h>
2#include<string.h>
3#include<stdlib.h>
4#include <unistd.h>
5#include <getopt.h>
6#include <sys/time.h>
7#include <assert.h>
8#ifdef HAVE_CONFIG_H
9#include <config.h>
10#endif
11#include <sys/types.h>
12#include <signal.h>
13
14
15
16#include <ccn/ccn.h>
17#include <ccn/uri.h>
18#include <ccn/keystore.h>
19#include <ccn/signing.h>
20#include <ccn/schedule.h>
21#include <ccn/hashtb.h>
22
23#include "nlsr.h"
24#include "nlsr_npt.h"
25#include "nlsr_fib.h"
akmhoque810a5b52012-09-09 16:53:14 -050026#include "nlsr_route.h"
akmhoque3560cb62012-09-09 10:52:30 -050027
28int
29add_npt_entry(char *orig_router, char *name_prefix, int face)
30{
31 if ( strcmp(orig_router,nlsr->router_name)== 0)
32 {
33 return -1;
34 }
35
36 struct npt_entry *ne=(struct npt_entry*)malloc(sizeof(struct npt_entry ));
37
akmhoque810a5b52012-09-09 16:53:14 -050038 int res,res_nle;
akmhoque3560cb62012-09-09 10:52:30 -050039 struct hashtb_enumerator ee;
40 struct hashtb_enumerator *e = &ee;
akmhoque810a5b52012-09-09 16:53:14 -050041
akmhoque3560cb62012-09-09 10:52:30 -050042
43 hashtb_start(nlsr->npt, e);
akmhoque810a5b52012-09-09 16:53:14 -050044 res = hashtb_seek(e, orig_router, strlen(orig_router), 0);
akmhoque3560cb62012-09-09 10:52:30 -050045
46 if(res == HT_NEW_ENTRY)
47 {
48 ne=e->data;
49
akmhoque810a5b52012-09-09 16:53:14 -050050 ne->orig_router=(char *)malloc(strlen(orig_router)+1);
51 memset(ne->orig_router,0,strlen(orig_router)+1);
52 memcpy(ne->orig_router,orig_router,strlen(orig_router));
akmhoque3560cb62012-09-09 10:52:30 -050053
akmhoque810a5b52012-09-09 16:53:14 -050054
55 struct name_list_entry *nle=(struct name_list_entry *)malloc(sizeof(struct name_list_entry));
akmhoque3560cb62012-09-09 10:52:30 -050056
akmhoque810a5b52012-09-09 16:53:14 -050057 struct hashtb_param param_nle = {0};
58 ne->name_list= hashtb_create(sizeof(struct name_list_entry ), &param_nle);
akmhoque3560cb62012-09-09 10:52:30 -050059
akmhoque810a5b52012-09-09 16:53:14 -050060 struct hashtb_enumerator eenle;
61 struct hashtb_enumerator *enle = &eenle;
akmhoque3560cb62012-09-09 10:52:30 -050062
akmhoque810a5b52012-09-09 16:53:14 -050063 hashtb_start(ne->name_list, enle);
64 res_nle = hashtb_seek(enle, name_prefix, strlen(name_prefix), 0);
akmhoque3560cb62012-09-09 10:52:30 -050065
akmhoque810a5b52012-09-09 16:53:14 -050066 if(res_nle == HT_NEW_ENTRY )
akmhoque3560cb62012-09-09 10:52:30 -050067 {
akmhoque810a5b52012-09-09 16:53:14 -050068 nle=enle->data;
69 nle->name=(char *)malloc(strlen(name_prefix)+1);
70 memset(nle->name,0,strlen(name_prefix)+1);
71 memcpy(nle->name,name_prefix,strlen(name_prefix));
akmhoque3560cb62012-09-09 10:52:30 -050072
akmhoque810a5b52012-09-09 16:53:14 -050073
akmhoque3560cb62012-09-09 10:52:30 -050074
75 }
akmhoque810a5b52012-09-09 16:53:14 -050076 hashtb_end(enle);
77
78 ne->next_hop_face=face;
akmhoque3560cb62012-09-09 10:52:30 -050079
80 if ( face != NO_FACE )
81 {
82 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)name_prefix, OP_REG, face);
83 }
84
85 }
86 else if (res == HT_OLD_ENTRY)
87 {
88 free(ne);
89 struct npt_entry *one;
90
91 one=e->data;
92
akmhoque810a5b52012-09-09 16:53:14 -050093 struct name_list_entry *nle=(struct name_list_entry *)malloc(sizeof(struct name_list_entry));
akmhoque3560cb62012-09-09 10:52:30 -050094
akmhoque810a5b52012-09-09 16:53:14 -050095 struct hashtb_param param_nle = {0};
96 ne->name_list= hashtb_create(sizeof(struct name_list_entry ), &param_nle);
akmhoque3560cb62012-09-09 10:52:30 -050097
akmhoque810a5b52012-09-09 16:53:14 -050098 struct hashtb_enumerator eenle;
99 struct hashtb_enumerator *enle = &eenle;
akmhoque3560cb62012-09-09 10:52:30 -0500100
akmhoque810a5b52012-09-09 16:53:14 -0500101 hashtb_start(one->name_list, enle);
102 res_nle = hashtb_seek(enle, name_prefix, strlen(name_prefix), 0);
103
104 if(res_nle == HT_NEW_ENTRY )
akmhoque3560cb62012-09-09 10:52:30 -0500105 {
akmhoque810a5b52012-09-09 16:53:14 -0500106 nle=enle->data;
107 nle->name=(char *)malloc(strlen(name_prefix)+1);
108 memset(nle->name,0,strlen(name_prefix)+1);
109 memcpy(nle->name,name_prefix,strlen(name_prefix));
110
akmhoque3560cb62012-09-09 10:52:30 -0500111 if ( face != NO_FACE )
112 {
113 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)name_prefix, OP_REG, face);
114 }
akmhoque3560cb62012-09-09 10:52:30 -0500115 }
akmhoque810a5b52012-09-09 16:53:14 -0500116 else if(res_nle == HT_OLD_ENTRY )
akmhoque3560cb62012-09-09 10:52:30 -0500117 {
akmhoque810a5b52012-09-09 16:53:14 -0500118 free(nle);
akmhoque3560cb62012-09-09 10:52:30 -0500119 }
akmhoque810a5b52012-09-09 16:53:14 -0500120 hashtb_end(enle);
121
122
akmhoque3560cb62012-09-09 10:52:30 -0500123
124 }
125 hashtb_end(e);
akmhoque3560cb62012-09-09 10:52:30 -0500126 return res;
127}
128
akmhoqueffacaa82012-09-13 17:48:30 -0500129int
130delete_npt_entry(char *orig_router, char *name_prefix)
131{
132 if ( strcmp(orig_router,nlsr->router_name)== 0)
133 {
134 return -1;
135 }
136
137 struct npt_entry *ne;
138
139 int res,res_nle;
140 struct hashtb_enumerator ee;
141 struct hashtb_enumerator *e = &ee;
142
143
144 hashtb_start(nlsr->npt, e);
145 res = hashtb_seek(e, orig_router, strlen(orig_router), 0);
146
147 if(res == HT_NEW_ENTRY)
148 {
149 hashtb_delete(e);
150 return -1;
151 }
152 else if (res == HT_OLD_ENTRY)
153 {
154 ne=e->data;
155
156 struct hashtb_enumerator eenle;
157 struct hashtb_enumerator *enle = &eenle;
158
159 hashtb_start(ne->name_list, enle);
160 res_nle = hashtb_seek(enle, name_prefix, strlen(name_prefix), 0);
161
162 if(res_nle == HT_NEW_ENTRY )
163 {
164 hashtb_delete(enle);
165 }
166 else if(res_nle == HT_OLD_ENTRY )
167 {
168 if (ne->next_hop_face != NO_FACE )
169 {
170 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)name_prefix, OP_UNREG, ne->next_hop_face);
171 }
172 hashtb_delete(enle);
173 }
174
175 hashtb_end(enle);
akmhoqueda5b6832012-09-13 22:33:55 -0500176
177 if ( hashtb_n(ne->name_list) == 0 )
178 {
179 hashtb_delete(e);
180 }
akmhoqueffacaa82012-09-13 17:48:30 -0500181 }
182
183 hashtb_end(e);
184
185 return 0;
186}
akmhoque3560cb62012-09-09 10:52:30 -0500187
188void
189print_npt(void)
190{
191 printf("\n");
192 printf("print_npt called\n\n");
193 int i, npt_element;
194
195 struct npt_entry *ne;
196
197 struct hashtb_enumerator ee;
198 struct hashtb_enumerator *e = &ee;
199
200 hashtb_start(nlsr->npt, e);
201 npt_element=hashtb_n(nlsr->npt);
202
203 for(i=0;i<npt_element;i++)
204 {
205 printf("\n");
206 printf("----------NPT ENTRY %d------------------\n",i+1);
207 ne=e->data;
akmhoque810a5b52012-09-09 16:53:14 -0500208 printf(" Origination Router: %s \n",ne->orig_router);
209 ne->next_hop_face == NO_FACE ? printf(" Next Hop Face: NO_NEXT_HOP \n") : printf(" Next Hop Face: %d \n", ne->next_hop_face);
akmhoque3560cb62012-09-09 10:52:30 -0500210
akmhoque810a5b52012-09-09 16:53:14 -0500211 int j, nl_element;
212 struct name_list_entry *nle;
213 struct hashtb_enumerator eenle;
214 struct hashtb_enumerator *enle = &eenle;
akmhoque3560cb62012-09-09 10:52:30 -0500215
akmhoque810a5b52012-09-09 16:53:14 -0500216 hashtb_start(ne->name_list, enle);
217 nl_element=hashtb_n(ne->name_list);
akmhoque3560cb62012-09-09 10:52:30 -0500218
akmhoque810a5b52012-09-09 16:53:14 -0500219 for (j=0;j<nl_element;j++)
akmhoque3560cb62012-09-09 10:52:30 -0500220 {
akmhoque810a5b52012-09-09 16:53:14 -0500221 nle=enle->data;
222 printf(" Name Prefix: %s \n",nle->name);
223 hashtb_next(enle);
akmhoque3560cb62012-09-09 10:52:30 -0500224 }
akmhoque810a5b52012-09-09 16:53:14 -0500225 hashtb_end(enle);
akmhoque3560cb62012-09-09 10:52:30 -0500226
227 hashtb_next(e);
228 }
229
230 hashtb_end(e);
231
232 printf("\n");
233}
akmhoque810a5b52012-09-09 16:53:14 -0500234
235void
236delete_orig_router_from_npt(char *orig_router,int next_hop_face)
237{
238 int res;
239 struct npt_entry *ne;
240
241 struct hashtb_enumerator ee;
242 struct hashtb_enumerator *e = &ee;
243
244 hashtb_start(nlsr->npt, e);
245 res = hashtb_seek(e, orig_router, strlen(orig_router), 0);
246
247 if ( res == HT_OLD_ENTRY )
248 {
249 ne=e->data;
250 if ( next_hop_face == ne->next_hop_face )
251 {
252 if ( next_hop_face != NO_NEXT_HOP )
253 {
254 int j, nl_element;
255 struct name_list_entry *nle;
256 struct hashtb_enumerator eenle;
257 struct hashtb_enumerator *enle = &eenle;
258
259 hashtb_start(ne->name_list, enle);
260 nl_element=hashtb_n(ne->name_list);
261
262 for (j=0;j<nl_element;j++)
263 {
264 nle=enle->data;
265 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_UNREG , next_hop_face);
266 hashtb_next(enle);
267 }
268 hashtb_end(enle);
269
270 }
271
272 }
273 hashtb_destroy(&ne->name_list);
274 hashtb_delete(e);
275 }
276 else if ( res == HT_NEW_ENTRY )
277 {
278 hashtb_delete(e);
279 }
280 hashtb_end(e);
281}
akmhoquefbfd0982012-09-09 20:59:03 -0500282
283
284void
285update_npt_with_new_route(char * orig_router,int next_hop_face)
286{
287 int res;
288 struct npt_entry *ne;
289
290 struct hashtb_enumerator ee;
291 struct hashtb_enumerator *e = &ee;
292
293 hashtb_start(nlsr->npt, e);
294 res = hashtb_seek(e, orig_router, strlen(orig_router), 0);
295
296 if ( res == HT_OLD_ENTRY )
297 {
298 ne=e->data;
299
300 if ( next_hop_face != ne->next_hop_face )
301 {
302 int j, nl_element;
303 struct name_list_entry *nle;
304 struct hashtb_enumerator eenle;
305 struct hashtb_enumerator *enle = &eenle;
306
307 hashtb_start(ne->name_list, enle);
308 nl_element=hashtb_n(ne->name_list);
309
310 for (j=0;j<nl_element;j++)
311 {
312 nle=enle->data;
313 if (ne->next_hop_face != NO_FACE )
314 {
315 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_UNREG , ne->next_hop_face);
316 }
317 if (next_hop_face != NO_FACE )
318 {
319 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_REG , next_hop_face);
320 }
321 hashtb_next(enle);
322 }
323 hashtb_end(enle);
324 ne->next_hop_face=next_hop_face;
325 }
326
327 }
328 else if ( res == HT_NEW_ENTRY )
329 {
330 hashtb_delete(e);
331 }
332 hashtb_end(e);
333}
334
335void
336destroy_all_face_by_nlsr(void)
337{
338 int i, npt_element;
339
340 struct npt_entry *ne;
341
342 struct hashtb_enumerator ee;
343 struct hashtb_enumerator *e = &ee;
344
345 hashtb_start(nlsr->npt, e);
346 npt_element=hashtb_n(nlsr->npt);
347
348 for(i=0;i<npt_element;i++)
349 {
akmhoquefbfd0982012-09-09 20:59:03 -0500350 ne=e->data;
akmhoquefbfd0982012-09-09 20:59:03 -0500351
352 int j, nl_element;
353 struct name_list_entry *nle;
354 struct hashtb_enumerator eenle;
355 struct hashtb_enumerator *enle = &eenle;
356
357 hashtb_start(ne->name_list, enle);
358 nl_element=hashtb_n(ne->name_list);
359
360 for (j=0;j<nl_element;j++)
361 {
362 nle=enle->data;
363 if ( ne->next_hop_face != NO_FACE)
364 {
365 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_UNREG , ne->next_hop_face);
366 }
akmhoqueffacaa82012-09-13 17:48:30 -0500367 //printf(" Name Prefix: %s Face: %d \n",nle->name,ne->next_hop_face);
akmhoquefbfd0982012-09-09 20:59:03 -0500368 hashtb_next(enle);
369 }
370 hashtb_end(enle);
371
372 hashtb_next(e);
373 }
374
375 hashtb_end(e);
376
377 printf("\n");
378}