blob: 3839f671e0f44cce8a112d774db06fbe79f43ee0 [file] [log] [blame]
akmhoque8fdd6412012-12-04 15:05:33 -06001#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 <sys/stat.h>
8#include <assert.h>
9#include <sys/types.h>
10#include <signal.h>
11#include <sys/socket.h>
12#include <sys/un.h>
13#include <fcntl.h>
14#include <sys/ioctl.h>
15#include <netinet/in.h>
16#include <netdb.h>
17#include <arpa/inet.h>
18
19#include <ccn/ccn.h>
20#include <ccn/uri.h>
21#include <ccn/keystore.h>
22#include <ccn/signing.h>
23#include <ccn/schedule.h>
24#include <ccn/hashtb.h>
25#include <ccn/sync.h>
26#include <ccn/seqwriter.h>
27
28#include "nlsr.h"
29#include "nlsr_sync.h"
30#include "nlsr_lsdb.h"
31
32int
33get_lsa_position(struct ccn_charbuf * ccnb, struct ccn_indexbuf *comps)
34{
35
36
37
38 int res,i;
39 int lsa_position=0;
40 int name_comps=(int)comps->n;
41
42 for(i=0;i<name_comps;i++)
43 {
44 res=ccn_name_comp_strcmp(ccnb->buf,comps,i,"LSA");
45 if( res == 0)
46 {
47 lsa_position=i;
48 break;
49 }
50 }
51
52 return lsa_position;
53
54}
55
56void
57get_name_part(struct name_prefix *name_part,struct ccn_charbuf * interest_ccnb, struct ccn_indexbuf *interest_comps, int offset)
58{
59
60
61
62 int res,i;
63 int lsa_position=0;
64 int len=0;
65
66 lsa_position=get_lsa_position(interest_ccnb,interest_comps);
67
68 const unsigned char *comp_ptr1;
69 size_t comp_size;
70 for(i=lsa_position+1+offset;i<interest_comps->n-1;i++)
71 {
72 res=ccn_name_comp_get(interest_ccnb->buf, interest_comps,i,&comp_ptr1, &comp_size);
73 len+=1;
74 len+=(int)comp_size;
75 }
76 len++;
77
78 char *neighbor=(char *)malloc(len);
79 memset(neighbor,0,len);
80
81 for(i=lsa_position+1+offset; i<interest_comps->n-1;i++)
82 {
83 res=ccn_name_comp_get(interest_ccnb->buf, interest_comps,i,&comp_ptr1, &comp_size);
84 memcpy(neighbor+strlen(neighbor),"/",1);
85 memcpy(neighbor+strlen(neighbor),(char *)comp_ptr1,strlen((char *)comp_ptr1));
86
87 }
88
89 name_part->name=(char *)malloc(strlen(neighbor)+1);
90 memset(name_part->name,0,strlen(neighbor)+1);
91 memcpy(name_part->name,neighbor,strlen(neighbor)+1);
92 name_part->length=strlen(neighbor)+1;
93
94
95}
96
97
98char *
99get_content_by_content_name(char *content_name)
100{
101
102 struct ccn_charbuf *name = NULL;
103 struct ccn_charbuf *templ = NULL;
104 struct ccn_charbuf *resultbuf = NULL;
105 struct ccn_parsed_ContentObject pcobuf = { 0 };
106 int res;
107 int allow_stale = 0;
108 int content_only = 1;
109 int scope = -1;
110 const unsigned char *ptr;
111 size_t length;
112 int resolve_version = CCN_V_HIGHEST;
113 int timeout_ms = 3000;
114 const unsigned lifetime_default = CCN_INTEREST_LIFETIME_SEC << 12;
115 unsigned lifetime_l12 = lifetime_default;
116 int get_flags = 0;
117
118
119 name = ccn_charbuf_create();
120 res = ccn_name_from_uri(name,content_name);
121 if (res < 0) {
122 fprintf(stderr, "Bad ccn URI: %s\n", content_name);
123 exit(1);
124 }
125
126
127 if (allow_stale || lifetime_l12 != lifetime_default || scope != -1) {
128 templ = ccn_charbuf_create();
129 ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG);
130 ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG);
131 ccn_charbuf_append_closer(templ); /* </Name> */
132 if (allow_stale) {
133 ccn_charbuf_append_tt(templ, CCN_DTAG_AnswerOriginKind, CCN_DTAG);
134 ccnb_append_number(templ,
135 CCN_AOK_DEFAULT | CCN_AOK_STALE);
136 ccn_charbuf_append_closer(templ); /* </AnswerOriginKind> */
137 }
138 if (scope != -1) {
139 ccnb_tagged_putf(templ, CCN_DTAG_Scope, "%d", scope);
140 }
141 if (lifetime_l12 != lifetime_default) {
142 /*
143 * Choose the interest lifetime so there are at least 3
144 * expressions (in the unsatisfied case).
145 */
146 unsigned char buf[3] = { 0 };
147 int i;
148 for (i = sizeof(buf) - 1; i >= 0; i--, lifetime_l12 >>= 8)
149 buf[i] = lifetime_l12 & 0xff;
150 ccnb_append_tagged_blob(templ, CCN_DTAG_InterestLifetime, buf, sizeof(buf));
151 }
152 ccn_charbuf_append_closer(templ); /* </Interest> */
153 }
154 resultbuf = ccn_charbuf_create();
155 if (resolve_version != 0) {
156 res = ccn_resolve_version(nlsr->ccn, name, resolve_version, 500);
157 if (res >= 0) {
158 ccn_uri_append(resultbuf, name->buf, name->length, 1);
159 //fprintf(stderr, "== %s\n",ccn_charbuf_as_string(resultbuf));
160 resultbuf->length = 0;
161 }
162 }
163 res = ccn_get(nlsr->ccn, name, templ, timeout_ms, resultbuf, &pcobuf, NULL, get_flags);
164 if (res >= 0) {
165 ptr = resultbuf->buf;
166 length = resultbuf->length;
167 if (content_only)
168 ccn_content_get_value(ptr, length, &pcobuf, &ptr, &length);
169 }
170 ccn_charbuf_destroy(&resultbuf);
171 ccn_charbuf_destroy(&templ);
172 ccn_charbuf_destroy(&name);
173 return (char *)ptr;
174}
175
176void
177process_content_from_sync(struct ccn_charbuf *content_name, struct ccn_indexbuf *components)
178{
179 int lsa_position;
180 int res;
181 size_t comp_size;
182 const unsigned char *lst;
183 const unsigned char *lsid;
184 const unsigned char *origtime;
185
186 int ls_type;
187 long int ls_id=0;
188
189 struct ccn_charbuf *uri = ccn_charbuf_create();
190 ccn_uri_append(uri, content_name->buf, content_name->length, 0);
191
192 struct name_prefix *orig_router=(struct name_prefix *)malloc(sizeof(struct name_prefix));
193
194 lsa_position=get_lsa_position(content_name, components);
195
196 res=ccn_name_comp_get(content_name->buf, components,lsa_position+1,&lst, &comp_size);
197
198 printf("Ls Type: %s\n",lst);
199 ls_type=atoi((char *)lst);
200 if(ls_type == LS_TYPE_NAME)
201 {
202 res=ccn_name_comp_get(content_name->buf, components,lsa_position+2,&lsid, &comp_size);
203 ls_id=atoi((char *)lsid);
204 res=ccn_name_comp_get(content_name->buf, components,lsa_position+3,&origtime, &comp_size);
205 get_name_part(orig_router,content_name,components,3);
206 printf("Ls ID: %s\nOrig Time: %s\nOrig Router: %s\n",lsid,origtime,orig_router->name);
207
208 int is_new_name_lsa=check_is_new_name_lsa(orig_router->name,(char *)lst,(char *)lsid,(char *)origtime);
209 if ( is_new_name_lsa == 1 )
210 {
211 printf("New NAME LSA.....\n");
212 char *content_data=get_content_by_content_name(ccn_charbuf_as_string(uri));
213 printf("Content Data: %s \n",content_data);
214 }
215 else
216 {
217 printf("Name LSA / Newer Name LSA already xists in LSDB\n");
218 char *content_data=get_content_by_content_name(ccn_charbuf_as_string(uri));
219 printf("Content Data: %s \n",content_data);
220 }
221 }
222 else if(ls_type == LS_TYPE_ADJ)
223 {
224 res=ccn_name_comp_get(content_name->buf, components,lsa_position+2,&origtime, &comp_size);
225 get_name_part(orig_router,content_name,components,2);
226 printf("Orig Time: %s\nOrig Router: %s\n",origtime,orig_router->name);
227
228 int is_new_adj_lsa=check_is_new_adj_lsa(orig_router->name,(char *)lst,(char *)origtime);
229 if ( is_new_adj_lsa == 1 )
230 {
231 printf("New Adj LSA.....\n");
232 char *content_data=get_content_by_content_name(ccn_charbuf_as_string(uri));
233 printf("Content Data: %s \n",content_data);
234 }
235 else
236 {
237
238 printf("Adj LSA / Newer Adj LSA already exists in LSDB\n");
239 char *content_data=get_content_by_content_name(ccn_charbuf_as_string(uri));
240 printf("Content Data: %s \n",content_data);
241 }
242
243 }
244
245 ccn_charbuf_destroy(&uri);
246
247}
248
249int
250sync_callback(struct ccns_name_closure *nc,
251 struct ccn_charbuf *lhash,
252 struct ccn_charbuf *rhash,
253 struct ccn_charbuf *name)
254{
255
256
257 struct ccn_indexbuf cid={0};
258
259 struct ccn_indexbuf *components=&cid;
260 ccn_name_split (name, components);
261 ccn_name_chop(name,components,-3);
262
263 process_content_from_sync(name,components);
264
265 return(0);
266}
267
268
269void
270sync_monitor(char *topo_prefix, char *slice_prefix)
271{
272
273 static struct ccns_name_closure nc={0};
274
275 nlsr->closure = &nc;
276 struct ccn_charbuf *prefix = ccn_charbuf_create();
277 struct ccn_charbuf *roothash = NULL;
278 struct ccn_charbuf *topo = ccn_charbuf_create();
279 nlsr->slice = ccns_slice_create();
280 ccn_charbuf_reset(prefix);
281 ccn_charbuf_reset(topo);
282
283 ccn_charbuf_reset(prefix);
284 ccn_name_from_uri(prefix, slice_prefix);
285
286 ccn_charbuf_reset(topo);
287 ccn_name_from_uri(topo, topo_prefix);
288
289
290 ccns_slice_set_topo_prefix(nlsr->slice, topo, prefix);
291 nlsr->closure->callback = &sync_callback;
292 nlsr->ccns = ccns_open(nlsr->ccn, nlsr->slice, nlsr->closure, roothash, NULL);
293
294}
295
296struct ccn_charbuf *
297make_template(int scope)
298{
299 struct ccn_charbuf *templ = NULL;
300 templ = ccn_charbuf_create();
301 ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG);
302 ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG);
303 ccn_charbuf_append_closer(templ); /* </Name> */
304 if (0 <= scope && scope <= 2)
305 ccnb_tagged_putf(templ, CCN_DTAG_Scope, "%d", scope);
306 ccn_charbuf_append_closer(templ); /* </Interest> */
307 return(templ);
308}
309
310void
311write_data_to_repo(char *data, char *name_prefix)
312{
313
314 struct ccn_charbuf *name = NULL;
315 struct ccn_seqwriter *w = NULL;
316 int blocksize = 1024;
317 int freshness = -1;
318 int torepo = 1;
319 int scope = 1;
320 int res;
321 size_t blockread;
322 struct ccn_charbuf *templ;
323
324 name = ccn_charbuf_create();
325 res = ccn_name_from_uri(name, name_prefix);
326 if (res < 0) {
327 fprintf(stderr, "bad CCN URI: %s\n",name_prefix);
328 exit(1);
329 }
330
331
332 w = ccn_seqw_create(nlsr->ccn, name);
333 if (w == NULL) {
334 fprintf(stderr, "ccn_seqw_create failed\n");
335 exit(1);
336 }
337 ccn_seqw_set_block_limits(w, blocksize, blocksize);
338 if (freshness > -1)
339 ccn_seqw_set_freshness(w, freshness);
340 if (torepo) {
341 struct ccn_charbuf *name_v = ccn_charbuf_create();
342 ccn_seqw_get_name(w, name_v);
343 ccn_name_from_uri(name_v, "%C1.R.sw");
344 ccn_name_append_nonce(name_v);
345 templ = make_template(scope);
346 res = ccn_get(nlsr->ccn, name_v, templ, 60000, NULL, NULL, NULL, 0);
347 ccn_charbuf_destroy(&templ);
348 ccn_charbuf_destroy(&name_v);
349 if (res < 0) {
350 fprintf(stderr, "No response from repository\n");
351 exit(1);
352 }
353 }
354
355
356
357
358 blockread = 0;
359 //struct ccn_charbuf *buf=ccn_charbuf_create();
360 //get_name_lsdb_summary(buf);
361 //blockread=buf->length;
362
363 blockread=strlen(data);
364
365 if (blockread > 0) {
366 //res = ccn_seqw_write(w, ccn_charbuf_as_string(buf), blockread);
367 res = ccn_seqw_write(w, data, blockread);
368 while (res == -1) {
369 ccn_run(nlsr->ccn, 100);
370 //res = ccn_seqw_write(w, ccn_charbuf_as_string(buf), blockread);
371 res = ccn_seqw_write(w, data, blockread);
372 }
373 }
374
375
376 ccn_run(nlsr->ccn, 1);
377
378 ccn_seqw_close(w);
379 //ccn_charbuf_destroy(&buf);
380 ccn_charbuf_destroy(&name);
381}
382
383
384int
385create_sync_slice(char *topo_prefix, char *slice_prefix)
386{
387 int res;
388 struct ccns_slice *slice;
389 struct ccn_charbuf *prefix = ccn_charbuf_create();
390 struct ccn_charbuf *topo = ccn_charbuf_create();
391 struct ccn_charbuf *clause = ccn_charbuf_create();
392 struct ccn_charbuf *slice_name = ccn_charbuf_create();
393 struct ccn_charbuf *slice_uri = ccn_charbuf_create();
394
395 if (prefix == NULL || topo == NULL || clause == NULL ||
396 slice_name == NULL || slice_uri == NULL) {
397 fprintf(stderr, "Unable to allocate required memory.\n");
398 exit(1);
399 }
400
401
402 slice = ccns_slice_create();
403
404 ccn_charbuf_reset(topo);
405 ccn_name_from_uri(topo, topo_prefix);
406 ccn_charbuf_reset(prefix);
407 ccn_name_from_uri(prefix,slice_prefix );
408 ccns_slice_set_topo_prefix(slice, topo, prefix);
409
410
411 res = ccns_write_slice(nlsr->ccn, slice, slice_name);
412
413 ccns_slice_destroy(&slice);
414 ccn_charbuf_destroy(&prefix);
415 ccn_charbuf_destroy(&topo);
416 ccn_charbuf_destroy(&clause);
417 ccn_charbuf_destroy(&slice_name);
418 ccn_charbuf_destroy(&slice_uri);
419
420 return 0;
421}
422