blob: 22d3f1c2284bec1b7382ef53f2c515e017057b33 [file] [log] [blame]
akmhoque59980a52012-08-09 12:36:09 -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
12
13#include <ccn/ccn.h>
14#include <ccn/uri.h>
15#include <ccn/keystore.h>
16#include <ccn/signing.h>
17#include <ccn/schedule.h>
18#include <ccn/hashtb.h>
akmhoque7f337272012-08-14 15:16:30 -050019#include <ccn/bloom.h>
akmhoque59980a52012-08-09 12:36:09 -050020
21#include "nlsr.h"
22#include "nlsr_ndn.h"
23#include "utility.h"
24
25enum ccn_upcall_res
26incoming_interest(struct ccn_closure *selfp,
27 enum ccn_upcall_kind kind, struct ccn_upcall_info *info)
28{
29
30 switch (kind) {
31 case CCN_UPCALL_FINAL:
32 break;
33 case CCN_UPCALL_INTEREST:
34 // printing the name prefix for which it received interest
35 printf("Interest Received for name: ");
36 struct ccn_charbuf*c;
37 c=ccn_charbuf_create();
38 ccn_uri_append(c,info->interest_ccnb,info->pi->offset[CCN_PI_E_Name]-info->pi->offset[CCN_PI_B_Name],0);
39 //ccn_name_chop(c,NULL,-1);
40 printf("%s\n",ccn_charbuf_as_string(c));
41 ccn_charbuf_destroy(&c);
42
akmhoque1c9b92f2012-08-13 10:57:50 -050043 process_incoming_interest(selfp, info);
akmhoque59980a52012-08-09 12:36:09 -050044
45 /*
46 struct ccn_charbuf *data=ccn_charbuf_create();
47 struct ccn_charbuf *name=ccn_charbuf_create();
48 struct ccn_signing_params sp=CCN_SIGNING_PARAMS_INIT;
49
50 ccn_charbuf_append(name, info->interest_ccnb + info->pi->offset[CCN_PI_B_Name],
51 info->pi->offset[CCN_PI_E_Name] - info->pi->offset[CCN_PI_B_Name]);
52
53 sp.template_ccnb=ccn_charbuf_create();
54 ccn_charbuf_append_tt(sp.template_ccnb,CCN_DTAG_SignedInfo, CCN_DTAG);
55 ccnb_tagged_putf(sp.template_ccnb, CCN_DTAG_FreshnessSeconds, "%ld", 1010);
56 sp.sp_flags |= CCN_SP_TEMPL_FRESHNESS;
57 ccn_charbuf_append_closer(sp.template_ccnb);
58
59 res= ccn_sign_content(ospfndn->ccn, data, name, &sp, "hello", strlen("hello"));
60 res=ccn_put(ospfndn->ccn,data->buf,data->length);
61 ccn_charbuf_destroy(&data);
62
63 */
64 break;
65
66 default:
67 break;
68 }
69
70 return CCN_UPCALL_RESULT_OK;
71}
72
73
74enum ccn_upcall_res incoming_content(struct ccn_closure* selfp,
75 enum ccn_upcall_kind kind, struct ccn_upcall_info* info)
76{
77
78
79 switch(kind) {
80 case CCN_UPCALL_FINAL:
81 break;
82 case CCN_UPCALL_CONTENT:
83 printf("Content Received for name: ");
84 struct ccn_charbuf*c;
85 c=ccn_charbuf_create();
86 ccn_uri_append(c,info->interest_ccnb,info->pi->offset[CCN_PI_E],0);
87 printf("%s\n",ccn_charbuf_as_string(c));
88 ccn_charbuf_destroy(&c);
89
akmhoquecb017752012-08-16 11:03:45 -050090 printf("Content Received for name: ");
91 struct ccn_charbuf*cc;
92 cc=ccn_charbuf_create();
93 ccn_uri_append(cc,info->content_ccnb,info->pco->offset[CCN_PCO_E],0);
94 printf("%s\n",ccn_charbuf_as_string(cc));
95 ccn_charbuf_destroy(&cc);
96
97 process_incoming_content(selfp, info);
98
akmhoque59980a52012-08-09 12:36:09 -050099 break;
100 case CCN_UPCALL_INTEREST_TIMED_OUT:
akmhoquecb017752012-08-16 11:03:45 -0500101 printf("Interest timed out \n");
102 struct ccn_charbuf*ic;
103 ic=ccn_charbuf_create();
104 ccn_uri_append(ic,info->interest_ccnb,info->pi->offset[CCN_PI_E_Name],0);
105 printf("%s\n",ccn_charbuf_as_string(ic));
106 ccn_charbuf_destroy(&ic);
107 process_incoming_timed_out_interest(selfp,info);
akmhoque59980a52012-08-09 12:36:09 -0500108
akmhoque59980a52012-08-09 12:36:09 -0500109 break;
110 default:
111 fprintf(stderr, "Unexpected response of kind %d\n", kind);
112 return CCN_UPCALL_RESULT_ERR;
113 }
114
115 return CCN_UPCALL_RESULT_OK;
116}
akmhoque61fe4472012-08-10 10:13:34 -0500117
akmhoquecb017752012-08-16 11:03:45 -0500118
119void
120process_incoming_content(struct ccn_closure* selfp, struct ccn_upcall_info* info)
121{
122 printf("process_incoming_content called \n");
123
akmhoque638c20a2012-08-16 11:57:48 -0500124 struct ccn_charbuf*c;
125 c=ccn_charbuf_create();
126 ccn_uri_append(c,info->interest_ccnb,info->pi->offset[CCN_PI_E_Name]-info->pi->offset[CCN_PI_B_Name],0);
127 printf("%s\n",ccn_charbuf_as_string(c));
128 ccn_charbuf_destroy(&c);
129
130 const unsigned char *comp_ptr1;
131 size_t comp_size;
132 int res,i;
133 int nlsr_position=0;
134 int name_comps=(int)info->interest_comps->n;
135
136 for(i=0;i<name_comps;i++)
137 {
138 res=ccn_name_comp_strcmp(info->interest_ccnb,info->interest_comps,i,"nlsr");
139 if( res == 0)
140 {
141 nlsr_position=i;
142 break;
143 }
144 }
145
146 res=ccn_name_comp_get(info->interest_ccnb, info->interest_comps,nlsr_position+1,&comp_ptr1, &comp_size);
147
148
149 printf("Det= %s \n",comp_ptr1);
150
151 if(!strcmp((char *)comp_ptr1,"lsdb"))
152 {
153 process_incoming_content_lsdb(selfp,info);
154 }
155
156}
157
158
159void
160process_incoming_content_lsdb(struct ccn_closure* selfp, struct ccn_upcall_info* info)
161{
162 printf("process_incoming_content_lsdb called \n");
163
164 const unsigned char *content_data;
165 size_t length;
166 ccn_content_get_value(info->content_ccnb, info->pco->offset[CCN_PCO_E_Content]-info->pco->offset[CCN_PCO_B_Content], info->pco, &content_data, &length);
167
168 printf(" Content Data: %s \n",content_data);
169
170
akmhoquecb017752012-08-16 11:03:45 -0500171}
172
173
174void
175process_incoming_timed_out_interest(struct ccn_closure* selfp, struct ccn_upcall_info* info)
176{
177 printf("process_incoming_timed_out_interest called \n");
178
179}
180
181
akmhoque1c9b92f2012-08-13 10:57:50 -0500182void
183process_incoming_interest(struct ccn_closure *selfp, struct ccn_upcall_info *info)
184{
185 printf("process_incoming_interest called \n");
186
187
188 struct ccn_charbuf*c;
189 c=ccn_charbuf_create();
190 ccn_uri_append(c,info->interest_ccnb,info->pi->offset[CCN_PI_E_Name]-info->pi->offset[CCN_PI_B_Name],0);
191 printf("%s\n",ccn_charbuf_as_string(c));
192 ccn_charbuf_destroy(&c);
193
194 const unsigned char *comp_ptr1;
195 size_t comp_size;
196 int res,i;
197 int nlsr_position=0;
198 int name_comps=(int)info->interest_comps->n;
199
200 for(i=0;i<name_comps;i++)
201 {
202 res=ccn_name_comp_strcmp(info->interest_ccnb,info->interest_comps,i,"nlsr");
203 if( res == 0)
204 {
akmhoqueea3603e2012-08-13 11:24:09 -0500205 nlsr_position=i;
akmhoque1c9b92f2012-08-13 10:57:50 -0500206 break;
207 }
208 }
209
210 res=ccn_name_comp_get(info->interest_ccnb, info->interest_comps,nlsr_position+1,&comp_ptr1, &comp_size);
211
212
213 printf("Det= %s \n",comp_ptr1);
214
215 if(!strcmp((char *)comp_ptr1,"lsdb"))
216 {
akmhoquebf1aa832012-08-13 13:26:59 -0500217 process_incoming_interest_lsdb(selfp,info);
akmhoque1c9b92f2012-08-13 10:57:50 -0500218 }
akmhoquebf1aa832012-08-13 13:26:59 -0500219
220
221}
222
223
224void
225process_incoming_interest_lsdb(struct ccn_closure *selfp, struct ccn_upcall_info *info)
226{
227 printf("process_incoming_interest_lsdb called \n");
228
akmhoquecb017752012-08-16 11:03:45 -0500229 int l, res;
akmhoquefce8cfc2012-08-14 14:00:33 -0500230 const unsigned char *exclbase;
akmhoque6d49e4d2012-08-14 13:49:30 -0500231 size_t size;
akmhoquefce8cfc2012-08-14 14:00:33 -0500232 struct ccn_buf_decoder decoder;
233 struct ccn_buf_decoder *d;
234 const unsigned char *comp;
235
akmhoque6d49e4d2012-08-14 13:49:30 -0500236
237 l = info->pi->offset[CCN_PI_E_Exclude] - info->pi->offset[CCN_PI_B_Exclude];
238 if (l > 0)
239 {
240 comp = NULL;
241 size = 0;
242 exclbase = info->interest_ccnb + info->pi->offset[CCN_PI_B_Exclude];
243 d = ccn_buf_decoder_start(&decoder, exclbase, l);
244 if (ccn_buf_match_dtag(d, CCN_DTAG_Exclude))
245 {
246 ccn_buf_advance(d);
247 if (ccn_buf_match_dtag(d, CCN_DTAG_Any))
248 ccn_buf_advance_past_element(d);
249 if (ccn_buf_match_dtag(d, CCN_DTAG_Component))
250 {
251 ccn_buf_advance(d);
252 ccn_buf_match_blob(d, &comp, &size);
akmhoquec9286692012-08-16 09:57:58 -0500253 ccn_buf_check_close(d);
254
255
akmhoque6d49e4d2012-08-14 13:49:30 -0500256 }
257 ccn_buf_check_close(d);
258 }
akmhoque07dd8cc2012-08-16 10:23:01 -0500259 //if (d->decoder.state < 0)
260 //printf("Parse Failed\n");
akmhoque6d49e4d2012-08-14 13:49:30 -0500261 if (comp != NULL)
akmhoque37e3adf2012-08-14 15:52:50 -0500262 printf("Number in Exclusion Filter is %s\n",comp);
akmhoque6d49e4d2012-08-14 13:49:30 -0500263
264 /* Now comp points to the start of your potential number, and size is its length */
265 }
266
akmhoque07dd8cc2012-08-16 10:23:01 -0500267 int dbcmp=strncmp(nlsr->lsdb->version,(char *)comp,16);
akmhoque898d4aa2012-08-16 10:26:15 -0500268
269 printf (" dbcmp = %d \n",dbcmp);
270
akmhoque07dd8cc2012-08-16 10:23:01 -0500271 if(dbcmp > 0)
akmhoquecb017752012-08-16 11:03:45 -0500272 {
akmhoque898d4aa2012-08-16 10:26:15 -0500273 printf("Has Updated database (Older: %s New: %s)\n",comp,nlsr->lsdb->version);
akmhoquecb017752012-08-16 11:03:45 -0500274 }
akmhoque07dd8cc2012-08-16 10:23:01 -0500275 else
akmhoquecb017752012-08-16 11:03:45 -0500276 {
akmhoque898d4aa2012-08-16 10:26:15 -0500277 printf("Data base is not updated than the older one (Older: %s New: %s)\n",comp,nlsr->lsdb->version);
akmhoquecb017752012-08-16 11:03:45 -0500278 printf("Sending NACK Content back.....\n");
akmhoque07dd8cc2012-08-16 10:23:01 -0500279
akmhoquecb017752012-08-16 11:03:45 -0500280 struct ccn_charbuf *data=ccn_charbuf_create();
281 struct ccn_charbuf *name=ccn_charbuf_create();
282 struct ccn_signing_params sp=CCN_SIGNING_PARAMS_INIT;
283
akmhoque638c20a2012-08-16 11:57:48 -0500284 ccn_charbuf_append(name, info->interest_ccnb + info->pi->offset[CCN_PI_B_Name],info->pi->offset[CCN_PI_E_Name] - info->pi->offset[CCN_PI_B_Name]);
akmhoque683f5082012-08-16 11:15:13 -0500285 //ccn_name_append_str(name,nlsr->lsdb->version);
akmhoque3b219462012-08-16 11:40:36 -0500286
akmhoquecb017752012-08-16 11:03:45 -0500287
288 sp.template_ccnb=ccn_charbuf_create();
289 ccn_charbuf_append_tt(sp.template_ccnb,CCN_DTAG_SignedInfo, CCN_DTAG);
290 ccnb_tagged_putf(sp.template_ccnb, CCN_DTAG_FreshnessSeconds, "%ld", 10);
291 sp.sp_flags |= CCN_SP_TEMPL_FRESHNESS;
292 ccn_charbuf_append_closer(sp.template_ccnb);
293
294 res= ccn_sign_content(nlsr->ccn, data, name, &sp, "NACK", strlen("NACK"));
295 res=ccn_put(nlsr->ccn,data->buf,data->length);
akmhoquebf11b1d2012-08-16 11:11:17 -0500296
297 if(res >= 0)
298 printf("Sending NACK Content is successful \n");
299
akmhoquecb017752012-08-16 11:03:45 -0500300 ccn_charbuf_destroy(&data);
301 ccn_charbuf_destroy(&sp.template_ccnb);
302
303 }
304
akmhoque1c9b92f2012-08-13 10:57:50 -0500305}
306
307int
308send_lsdb_interest(struct ccn_schedule *sched, void *clienth,
309 struct ccn_scheduled_event *ev, int flags)
310{
311
312 struct ccn_charbuf *name;
313 long int rnum;
314 char rnumstr[20];
akmhoqueea3603e2012-08-13 11:24:09 -0500315 char lsdb_str[5];
316 char nlsr_str[5];
akmhoque1c9b92f2012-08-13 10:57:50 -0500317
318 int res,i;
319 int adl_element;
320
321 rnum=random();
322 memset(&rnumstr,0,20);
323 sprintf(rnumstr,"%ld",rnum);
akmhoqueea3603e2012-08-13 11:24:09 -0500324 memset(&nlsr_str,0,5);
325 sprintf(nlsr_str,"nlsr");
326 memset(&lsdb_str,0,5);
327 sprintf(lsdb_str,"lsdb");
328
akmhoque1c9b92f2012-08-13 10:57:50 -0500329
330 struct ndn_neighbor *nbr;
331
332 struct hashtb_enumerator ee;
333 struct hashtb_enumerator *e = &ee;
334
335 hashtb_start(nlsr->adl, e);
336 adl_element=hashtb_n(nlsr->adl);
akmhoque07dd8cc2012-08-16 10:23:01 -0500337 //int mynumber=15;
akmhoque1c9b92f2012-08-13 10:57:50 -0500338
339 for(i=0;i<adl_element;i++)
340 {
341 nbr=e->data;
akmhoqueea3603e2012-08-13 11:24:09 -0500342 printf("Sending interest for name prefix:%s/%s/%s/%s\n",nbr->neighbor->name,nlsr_str,lsdb_str,rnumstr);
akmhoque1c9b92f2012-08-13 10:57:50 -0500343 name=ccn_charbuf_create();
344 res=ccn_name_from_uri(name,nbr->neighbor->name);
akmhoqueea3603e2012-08-13 11:24:09 -0500345 ccn_name_append_str(name,nlsr_str);
346 ccn_name_append_str(name,lsdb_str);
akmhoque1c9b92f2012-08-13 10:57:50 -0500347 ccn_name_append_str(name,rnumstr);
348
akmhoque6d49e4d2012-08-14 13:49:30 -0500349 /* adding Exclusion filter */
akmhoquebf1aa832012-08-13 13:26:59 -0500350
akmhoque6d49e4d2012-08-14 13:49:30 -0500351 struct ccn_charbuf *templ;
352 templ = ccn_charbuf_create();
akmhoque7f337272012-08-14 15:16:30 -0500353
akmhoque6d49e4d2012-08-14 13:49:30 -0500354 struct ccn_charbuf *c;
355 c = ccn_charbuf_create();
akmhoque7f337272012-08-14 15:16:30 -0500356
357
358 ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG);
359 ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG);
360 ccn_charbuf_append_closer(templ); /* </Name> */
akmhoque6d49e4d2012-08-14 13:49:30 -0500361 ccn_charbuf_append_tt(templ, CCN_DTAG_Exclude, CCN_DTAG);
362 ccnb_tagged_putf(templ, CCN_DTAG_Any, "");
363 ccn_charbuf_reset(c);
akmhoque07dd8cc2012-08-16 10:23:01 -0500364 //ccn_charbuf_putf(c, "%u", (unsigned)mynumber);
365 ccn_charbuf_putf(c, "%s", nbr->last_lsdb_version);
akmhoque6d49e4d2012-08-14 13:49:30 -0500366 ccnb_append_tagged_blob(templ, CCN_DTAG_Component, c->buf, c->length);
367 ccn_charbuf_append_closer(templ); /* </Exclude> */
akmhoque7f337272012-08-14 15:16:30 -0500368 ccn_charbuf_append_closer(templ); /* </Interest> */
akmhoque37e3adf2012-08-14 15:52:50 -0500369
akmhoque6d49e4d2012-08-14 13:49:30 -0500370
371 /* Adding Exclusion filter done */
372
akmhoque7f337272012-08-14 15:16:30 -0500373 res=ccn_express_interest(nlsr->ccn,name,&(nlsr->in_content),templ);
374
akmhoque1c9b92f2012-08-13 10:57:50 -0500375 if ( res >= 0 )
376 printf("Interest sending Successfull .... \n");
akmhoque37e3adf2012-08-14 15:52:50 -0500377 ccn_charbuf_destroy(&c);
akmhoque7f337272012-08-14 15:16:30 -0500378 ccn_charbuf_destroy(&templ);
akmhoque1c9b92f2012-08-13 10:57:50 -0500379 ccn_charbuf_destroy(&name);
380
381 hashtb_next(e);
382 }
383
384 hashtb_end(e);
385
akmhoquebf11b1d2012-08-16 11:11:17 -0500386 nlsr->event_send_lsdb_interest = ccn_schedule_event(nlsr->sched, 60000000, &send_lsdb_interest, NULL, 0);
akmhoque1c9b92f2012-08-13 10:57:50 -0500387
388 return 0;
389
390}
391