blob: 9841da62358c11bd35bcb566abab7d276e06f329 [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
129
130void
131print_npt(void)
132{
133 printf("\n");
134 printf("print_npt called\n\n");
135 int i, npt_element;
136
137 struct npt_entry *ne;
138
139 struct hashtb_enumerator ee;
140 struct hashtb_enumerator *e = &ee;
141
142 hashtb_start(nlsr->npt, e);
143 npt_element=hashtb_n(nlsr->npt);
144
145 for(i=0;i<npt_element;i++)
146 {
147 printf("\n");
148 printf("----------NPT ENTRY %d------------------\n",i+1);
149 ne=e->data;
akmhoque810a5b52012-09-09 16:53:14 -0500150 printf(" Origination Router: %s \n",ne->orig_router);
151 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 -0500152
akmhoque810a5b52012-09-09 16:53:14 -0500153 int j, nl_element;
154 struct name_list_entry *nle;
155 struct hashtb_enumerator eenle;
156 struct hashtb_enumerator *enle = &eenle;
akmhoque3560cb62012-09-09 10:52:30 -0500157
akmhoque810a5b52012-09-09 16:53:14 -0500158 hashtb_start(ne->name_list, enle);
159 nl_element=hashtb_n(ne->name_list);
akmhoque3560cb62012-09-09 10:52:30 -0500160
akmhoque810a5b52012-09-09 16:53:14 -0500161 for (j=0;j<nl_element;j++)
akmhoque3560cb62012-09-09 10:52:30 -0500162 {
akmhoque810a5b52012-09-09 16:53:14 -0500163 nle=enle->data;
164 printf(" Name Prefix: %s \n",nle->name);
165 hashtb_next(enle);
akmhoque3560cb62012-09-09 10:52:30 -0500166 }
akmhoque810a5b52012-09-09 16:53:14 -0500167 hashtb_end(enle);
akmhoque3560cb62012-09-09 10:52:30 -0500168
169 hashtb_next(e);
170 }
171
172 hashtb_end(e);
173
174 printf("\n");
175}
akmhoque810a5b52012-09-09 16:53:14 -0500176
177void
178delete_orig_router_from_npt(char *orig_router,int next_hop_face)
179{
180 int res;
181 struct npt_entry *ne;
182
183 struct hashtb_enumerator ee;
184 struct hashtb_enumerator *e = &ee;
185
186 hashtb_start(nlsr->npt, e);
187 res = hashtb_seek(e, orig_router, strlen(orig_router), 0);
188
189 if ( res == HT_OLD_ENTRY )
190 {
191 ne=e->data;
192 if ( next_hop_face == ne->next_hop_face )
193 {
194 if ( next_hop_face != NO_NEXT_HOP )
195 {
196 int j, nl_element;
197 struct name_list_entry *nle;
198 struct hashtb_enumerator eenle;
199 struct hashtb_enumerator *enle = &eenle;
200
201 hashtb_start(ne->name_list, enle);
202 nl_element=hashtb_n(ne->name_list);
203
204 for (j=0;j<nl_element;j++)
205 {
206 nle=enle->data;
207 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_UNREG , next_hop_face);
208 hashtb_next(enle);
209 }
210 hashtb_end(enle);
211
212 }
213
214 }
215 hashtb_destroy(&ne->name_list);
216 hashtb_delete(e);
217 }
218 else if ( res == HT_NEW_ENTRY )
219 {
220 hashtb_delete(e);
221 }
222 hashtb_end(e);
223}
akmhoquefbfd0982012-09-09 20:59:03 -0500224
225
226void
227update_npt_with_new_route(char * orig_router,int next_hop_face)
228{
229 int res;
230 struct npt_entry *ne;
231
232 struct hashtb_enumerator ee;
233 struct hashtb_enumerator *e = &ee;
234
235 hashtb_start(nlsr->npt, e);
236 res = hashtb_seek(e, orig_router, strlen(orig_router), 0);
237
238 if ( res == HT_OLD_ENTRY )
239 {
240 ne=e->data;
241
242 if ( next_hop_face != ne->next_hop_face )
243 {
244 int j, nl_element;
245 struct name_list_entry *nle;
246 struct hashtb_enumerator eenle;
247 struct hashtb_enumerator *enle = &eenle;
248
249 hashtb_start(ne->name_list, enle);
250 nl_element=hashtb_n(ne->name_list);
251
252 for (j=0;j<nl_element;j++)
253 {
254 nle=enle->data;
255 if (ne->next_hop_face != NO_FACE )
256 {
257 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_UNREG , ne->next_hop_face);
258 }
259 if (next_hop_face != NO_FACE )
260 {
261 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_REG , next_hop_face);
262 }
263 hashtb_next(enle);
264 }
265 hashtb_end(enle);
266 ne->next_hop_face=next_hop_face;
267 }
268
269 }
270 else if ( res == HT_NEW_ENTRY )
271 {
272 hashtb_delete(e);
273 }
274 hashtb_end(e);
275}
276
277void
278destroy_all_face_by_nlsr(void)
279{
280 int i, npt_element;
281
282 struct npt_entry *ne;
283
284 struct hashtb_enumerator ee;
285 struct hashtb_enumerator *e = &ee;
286
287 hashtb_start(nlsr->npt, e);
288 npt_element=hashtb_n(nlsr->npt);
289
290 for(i=0;i<npt_element;i++)
291 {
292 //printf("\n");
293 //printf("----------NPT ENTRY %d------------------\n",i+1);
294 ne=e->data;
295 //printf(" Origination Router: %s \n",ne->orig_router);
296 //ne->next_hop_face == NO_FACE ? printf(" Next Hop Face: NO_NEXT_HOP \n") : printf(" Next Hop Face: %d \n", ne->next_hop_face);
297
298 int j, nl_element;
299 struct name_list_entry *nle;
300 struct hashtb_enumerator eenle;
301 struct hashtb_enumerator *enle = &eenle;
302
303 hashtb_start(ne->name_list, enle);
304 nl_element=hashtb_n(ne->name_list);
305
306 for (j=0;j<nl_element;j++)
307 {
308 nle=enle->data;
309 if ( ne->next_hop_face != NO_FACE)
310 {
311 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_UNREG , ne->next_hop_face);
312 }
313 //printf(" Name Prefix: %s \n",nle->name);
314 hashtb_next(enle);
315 }
316 hashtb_end(enle);
317
318 hashtb_next(e);
319 }
320
321 hashtb_end(e);
322
323 printf("\n");
324}