blob: 9fe456593dcf5c433153df03cda73f1c7d65f11b [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);
176 }
177
178 hashtb_end(e);
179
180 return 0;
181}
akmhoque3560cb62012-09-09 10:52:30 -0500182
183void
184print_npt(void)
185{
186 printf("\n");
187 printf("print_npt called\n\n");
188 int i, npt_element;
189
190 struct npt_entry *ne;
191
192 struct hashtb_enumerator ee;
193 struct hashtb_enumerator *e = &ee;
194
195 hashtb_start(nlsr->npt, e);
196 npt_element=hashtb_n(nlsr->npt);
197
198 for(i=0;i<npt_element;i++)
199 {
200 printf("\n");
201 printf("----------NPT ENTRY %d------------------\n",i+1);
202 ne=e->data;
akmhoque810a5b52012-09-09 16:53:14 -0500203 printf(" Origination Router: %s \n",ne->orig_router);
204 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 -0500205
akmhoque810a5b52012-09-09 16:53:14 -0500206 int j, nl_element;
207 struct name_list_entry *nle;
208 struct hashtb_enumerator eenle;
209 struct hashtb_enumerator *enle = &eenle;
akmhoque3560cb62012-09-09 10:52:30 -0500210
akmhoque810a5b52012-09-09 16:53:14 -0500211 hashtb_start(ne->name_list, enle);
212 nl_element=hashtb_n(ne->name_list);
akmhoque3560cb62012-09-09 10:52:30 -0500213
akmhoque810a5b52012-09-09 16:53:14 -0500214 for (j=0;j<nl_element;j++)
akmhoque3560cb62012-09-09 10:52:30 -0500215 {
akmhoque810a5b52012-09-09 16:53:14 -0500216 nle=enle->data;
217 printf(" Name Prefix: %s \n",nle->name);
218 hashtb_next(enle);
akmhoque3560cb62012-09-09 10:52:30 -0500219 }
akmhoque810a5b52012-09-09 16:53:14 -0500220 hashtb_end(enle);
akmhoque3560cb62012-09-09 10:52:30 -0500221
222 hashtb_next(e);
223 }
224
225 hashtb_end(e);
226
227 printf("\n");
228}
akmhoque810a5b52012-09-09 16:53:14 -0500229
230void
231delete_orig_router_from_npt(char *orig_router,int next_hop_face)
232{
233 int res;
234 struct npt_entry *ne;
235
236 struct hashtb_enumerator ee;
237 struct hashtb_enumerator *e = &ee;
238
239 hashtb_start(nlsr->npt, e);
240 res = hashtb_seek(e, orig_router, strlen(orig_router), 0);
241
242 if ( res == HT_OLD_ENTRY )
243 {
244 ne=e->data;
245 if ( next_hop_face == ne->next_hop_face )
246 {
247 if ( next_hop_face != NO_NEXT_HOP )
248 {
249 int j, nl_element;
250 struct name_list_entry *nle;
251 struct hashtb_enumerator eenle;
252 struct hashtb_enumerator *enle = &eenle;
253
254 hashtb_start(ne->name_list, enle);
255 nl_element=hashtb_n(ne->name_list);
256
257 for (j=0;j<nl_element;j++)
258 {
259 nle=enle->data;
260 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_UNREG , next_hop_face);
261 hashtb_next(enle);
262 }
263 hashtb_end(enle);
264
265 }
266
267 }
268 hashtb_destroy(&ne->name_list);
269 hashtb_delete(e);
270 }
271 else if ( res == HT_NEW_ENTRY )
272 {
273 hashtb_delete(e);
274 }
275 hashtb_end(e);
276}
akmhoquefbfd0982012-09-09 20:59:03 -0500277
278
279void
280update_npt_with_new_route(char * orig_router,int next_hop_face)
281{
282 int res;
283 struct npt_entry *ne;
284
285 struct hashtb_enumerator ee;
286 struct hashtb_enumerator *e = &ee;
287
288 hashtb_start(nlsr->npt, e);
289 res = hashtb_seek(e, orig_router, strlen(orig_router), 0);
290
291 if ( res == HT_OLD_ENTRY )
292 {
293 ne=e->data;
294
295 if ( next_hop_face != ne->next_hop_face )
296 {
297 int j, nl_element;
298 struct name_list_entry *nle;
299 struct hashtb_enumerator eenle;
300 struct hashtb_enumerator *enle = &eenle;
301
302 hashtb_start(ne->name_list, enle);
303 nl_element=hashtb_n(ne->name_list);
304
305 for (j=0;j<nl_element;j++)
306 {
307 nle=enle->data;
308 if (ne->next_hop_face != NO_FACE )
309 {
310 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_UNREG , ne->next_hop_face);
311 }
312 if (next_hop_face != NO_FACE )
313 {
314 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_REG , next_hop_face);
315 }
316 hashtb_next(enle);
317 }
318 hashtb_end(enle);
319 ne->next_hop_face=next_hop_face;
320 }
321
322 }
323 else if ( res == HT_NEW_ENTRY )
324 {
325 hashtb_delete(e);
326 }
327 hashtb_end(e);
328}
329
330void
331destroy_all_face_by_nlsr(void)
332{
333 int i, npt_element;
334
335 struct npt_entry *ne;
336
337 struct hashtb_enumerator ee;
338 struct hashtb_enumerator *e = &ee;
339
340 hashtb_start(nlsr->npt, e);
341 npt_element=hashtb_n(nlsr->npt);
342
343 for(i=0;i<npt_element;i++)
344 {
akmhoquefbfd0982012-09-09 20:59:03 -0500345 ne=e->data;
akmhoquefbfd0982012-09-09 20:59:03 -0500346
347 int j, nl_element;
348 struct name_list_entry *nle;
349 struct hashtb_enumerator eenle;
350 struct hashtb_enumerator *enle = &eenle;
351
352 hashtb_start(ne->name_list, enle);
353 nl_element=hashtb_n(ne->name_list);
354
355 for (j=0;j<nl_element;j++)
356 {
357 nle=enle->data;
358 if ( ne->next_hop_face != NO_FACE)
359 {
360 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_UNREG , ne->next_hop_face);
361 }
akmhoqueffacaa82012-09-13 17:48:30 -0500362 //printf(" Name Prefix: %s Face: %d \n",nle->name,ne->next_hop_face);
akmhoquefbfd0982012-09-09 20:59:03 -0500363 hashtb_next(enle);
364 }
365 hashtb_end(enle);
366
367 hashtb_next(e);
368 }
369
370 hashtb_end(e);
371
372 printf("\n");
373}