blob: e79ca87327019e3eb22eb6629e527c4a48f54d95 [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>
19
20#include "nlsr.h"
21#include "nlsr_ndn.h"
akmhoque03004e62012-09-06 01:12:28 -050022#include "nlsr_npl.h"
akmhoque8a5babe2012-08-16 17:39:33 -050023#include "nlsr_adl.h"
akmhoqued79438d2012-08-27 13:31:42 -050024#include "nlsr_lsdb.h"
akmhoque53f64222012-09-05 13:57:51 -050025#include "utility.h"
26
akmhoque3d319d42013-02-20 11:08:32 -060027/**
28* add lifetime in second to a interest template
29*/
akmhoqued79438d2012-08-27 13:31:42 -050030int
31appendLifetime(struct ccn_charbuf *cb, int lifetime)
32{
33 unsigned char buf[sizeof(int32_t)];
34 int32_t dreck = lifetime << 12;
35 int pos = sizeof(int32_t);
36 int res = 0;
37 while (dreck > 0 && pos > 0)
38 {
39 pos--;
40 buf[pos] = dreck & 255;
41 dreck = dreck >> 8;
42 }
akmhoque3d319d42013-02-20 11:08:32 -060043 res |= ccnb_append_tagged_blob(cb, CCN_DTAG_InterestLifetime, buf+pos,
44 sizeof(buf)-pos);
akmhoqued79438d2012-08-27 13:31:42 -050045 return res;
46}
akmhoque59980a52012-08-09 12:36:09 -050047
akmhoque3d319d42013-02-20 11:08:32 -060048/**
49* get neighbor name prefix from interest/content name and put into nbr
50*/
akmhoque53f64222012-09-05 13:57:51 -050051
52void
akmhoque3d319d42013-02-20 11:08:32 -060053get_nbr(struct name_prefix *nbr,struct ccn_closure *selfp,
54 struct ccn_upcall_info *info)
akmhoque53f64222012-09-05 13:57:51 -050055{
akmhoque7b791452012-10-30 11:24:56 -050056 if ( nlsr->debugging )
57 printf("get_nbr called\n");
58 if ( nlsr->detailed_logging )
59 writeLogg(__FILE__,__FUNCTION__,__LINE__,"get_nbr called\n");
akmhoque53f64222012-09-05 13:57:51 -050060
akmhoque53f64222012-09-05 13:57:51 -050061 int res,i;
62 int nlsr_position=0;
63 int name_comps=(int)info->interest_comps->n;
64 int len=0;
65
66 for(i=0;i<name_comps;i++)
67 {
68 res=ccn_name_comp_strcmp(info->interest_ccnb,info->interest_comps,i,"nlsr");
69 if( res == 0)
70 {
71 nlsr_position=i;
72 break;
73 }
74 }
75
76
77 const unsigned char *comp_ptr1;
78 size_t comp_size;
79 for(i=0;i<nlsr_position;i++)
80 {
akmhoque3d319d42013-02-20 11:08:32 -060081 res=ccn_name_comp_get(info->interest_ccnb, info->interest_comps,i,&
82 comp_ptr1, &comp_size);
akmhoque53f64222012-09-05 13:57:51 -050083 len+=1;
84 len+=(int)comp_size;
85 }
86 len++;
87
88 char *neighbor=(char *)malloc(len);
89 memset(neighbor,0,len);
90
91 for(i=0; i<nlsr_position;i++)
92 {
akmhoque3d319d42013-02-20 11:08:32 -060093 res=ccn_name_comp_get(info->interest_ccnb, info->interest_comps,i,
94 &comp_ptr1, &comp_size);
akmhoque53f64222012-09-05 13:57:51 -050095 memcpy(neighbor+strlen(neighbor),"/",1);
96 memcpy(neighbor+strlen(neighbor),(char *)comp_ptr1,strlen((char *)comp_ptr1));
97
98 }
akmhoque53f64222012-09-05 13:57:51 -050099
100 nbr->name=(char *)malloc(strlen(neighbor)+1);
101 memcpy(nbr->name,neighbor,strlen(neighbor)+1);
102 nbr->length=strlen(neighbor)+1;
103
akmhoque7b791452012-10-30 11:24:56 -0500104 if ( nlsr->debugging )
105 printf("Neighbor: %s Length: %d\n",nbr->name,nbr->length);
106 if ( nlsr->detailed_logging )
akmhoque3d319d42013-02-20 11:08:32 -0600107 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Neighbor: %s Length: %d\n",
108 nbr->name,nbr->length);
akmhoque53f64222012-09-05 13:57:51 -0500109
110}
111
akmhoque3d319d42013-02-20 11:08:32 -0600112/**
113* Retrieve LSA identifier from content name
114*/
akmhoque53f64222012-09-05 13:57:51 -0500115
akmhoque3d319d42013-02-20 11:08:32 -0600116void
117get_lsa_identifier(struct name_prefix *lsaId,struct ccn_closure *selfp,
118 struct ccn_upcall_info *info, int offset)
119{
akmhoque7b791452012-10-30 11:24:56 -0500120
121 if ( nlsr->debugging )
122 printf("get_lsa_identifier called\n");
123 if ( nlsr->detailed_logging )
124 writeLogg(__FILE__,__FUNCTION__,__LINE__,"get_lsa_identifier called\n");
125
akmhoque53f64222012-09-05 13:57:51 -0500126 int res,i;
127 int nlsr_position=0;
128 int name_comps=(int)info->interest_comps->n;
129 int len=0;
130
131 for(i=0;i<name_comps;i++)
132 {
133 res=ccn_name_comp_strcmp(info->interest_ccnb,info->interest_comps,i,"nlsr");
134 if( res == 0)
135 {
136 nlsr_position=i;
137 break;
138 }
139 }
140
141
142 const unsigned char *comp_ptr1;
143 size_t comp_size;
akmhoque03004e62012-09-06 01:12:28 -0500144 for(i=nlsr_position+3+offset;i<info->interest_comps->n-1;i++)
akmhoque53f64222012-09-05 13:57:51 -0500145 {
akmhoque3d319d42013-02-20 11:08:32 -0600146 res=ccn_name_comp_get(info->interest_ccnb, info->interest_comps,i,
147 &comp_ptr1, &comp_size);
akmhoque53f64222012-09-05 13:57:51 -0500148 len+=1;
149 len+=(int)comp_size;
150 }
151 len++;
152
153 char *neighbor=(char *)malloc(len);
154 memset(neighbor,0,len);
155
akmhoque03004e62012-09-06 01:12:28 -0500156 for(i=nlsr_position+3+offset; i<info->interest_comps->n-1;i++)
akmhoque53f64222012-09-05 13:57:51 -0500157 {
akmhoque3d319d42013-02-20 11:08:32 -0600158 res=ccn_name_comp_get(info->interest_ccnb, info->interest_comps,i,
159 &comp_ptr1, &comp_size);
akmhoque53f64222012-09-05 13:57:51 -0500160 memcpy(neighbor+strlen(neighbor),"/",1);
161 memcpy(neighbor+strlen(neighbor),(char *)comp_ptr1,strlen((char *)comp_ptr1));
162
163 }
akmhoque53f64222012-09-05 13:57:51 -0500164
165 lsaId->name=(char *)malloc(strlen(neighbor)+1);
166 memset(lsaId->name,0,strlen(neighbor)+1);
167 memcpy(lsaId->name,neighbor,strlen(neighbor)+1);
168 lsaId->length=strlen(neighbor)+1;
169
akmhoque7b791452012-10-30 11:24:56 -0500170 if ( nlsr->debugging )
171 printf("LSA Identifier: %s Length: %d\n",lsaId->name,lsaId->length-1);
172 if ( nlsr->detailed_logging )
akmhoque3d319d42013-02-20 11:08:32 -0600173 writeLogg(__FILE__,__FUNCTION__,__LINE__,"LSA Identifier: %s Length: "
174 "%d\n",lsaId->name,lsaId->length-1);
akmhoque03004e62012-09-06 01:12:28 -0500175
176}
177
178
akmhoque3d319d42013-02-20 11:08:32 -0600179
180/**
181* Call back function registered in ccnd to get all interest coming to NLSR
182* application
183*/
akmhoque03004e62012-09-06 01:12:28 -0500184
akmhoque59980a52012-08-09 12:36:09 -0500185enum ccn_upcall_res
186incoming_interest(struct ccn_closure *selfp,
187 enum ccn_upcall_kind kind, struct ccn_upcall_info *info)
188{
akmhoqueffacaa82012-09-13 17:48:30 -0500189
190 nlsr_lock();
191
akmhoque59980a52012-08-09 12:36:09 -0500192 switch (kind) {
193 case CCN_UPCALL_FINAL:
194 break;
195 case CCN_UPCALL_INTEREST:
akmhoque03004e62012-09-06 01:12:28 -0500196 // printing the name prefix for which it received interest
akmhoque7b791452012-10-30 11:24:56 -0500197 if ( nlsr->debugging )
198 printf("Interest Received for name: ");
199 if ( nlsr->detailed_logging )
200 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Interest Received for name: ");
201
akmhoque03004e62012-09-06 01:12:28 -0500202 struct ccn_charbuf*c;
203 c=ccn_charbuf_create();
204 ccn_uri_append(c,info->interest_ccnb,info->pi->offset[CCN_PI_E_Name],0);
akmhoque7b791452012-10-30 11:24:56 -0500205
206 if ( nlsr->debugging )
207 printf("%s\n",ccn_charbuf_as_string(c));
208 if ( nlsr->detailed_logging )
209 writeLogg(__FILE__,__FUNCTION__,__LINE__,"%s\n",ccn_charbuf_as_string(c));
210
akmhoque03004e62012-09-06 01:12:28 -0500211 ccn_charbuf_destroy(&c);
212
akmhoque1c9b92f2012-08-13 10:57:50 -0500213 process_incoming_interest(selfp, info);
akmhoque03004e62012-09-06 01:12:28 -0500214
akmhoque59980a52012-08-09 12:36:09 -0500215 break;
akmhoque03004e62012-09-06 01:12:28 -0500216
akmhoque59980a52012-08-09 12:36:09 -0500217 default:
218 break;
219 }
akmhoque03004e62012-09-06 01:12:28 -0500220
akmhoqueffacaa82012-09-13 17:48:30 -0500221 nlsr_unlock();
222
akmhoque59980a52012-08-09 12:36:09 -0500223 return CCN_UPCALL_RESULT_OK;
224}
225
akmhoque3d319d42013-02-20 11:08:32 -0600226/**
227* Function for processing incoming interest and reply with content/NACK content
228*/
akmhoque59980a52012-08-09 12:36:09 -0500229
akmhoqued79438d2012-08-27 13:31:42 -0500230void
akmhoque1c9b92f2012-08-13 10:57:50 -0500231process_incoming_interest(struct ccn_closure *selfp, struct ccn_upcall_info *info)
232{
akmhoque7b791452012-10-30 11:24:56 -0500233 if ( nlsr->debugging )
234 printf("process_incoming_interest called \n");
235 if ( nlsr->detailed_logging )
236 writeLogg(__FILE__,__FUNCTION__,__LINE__,"process_incoming_interest called \n");
237
akmhoque1c9b92f2012-08-13 10:57:50 -0500238 const unsigned char *comp_ptr1;
239 size_t comp_size;
240 int res,i;
241 int nlsr_position=0;
242 int name_comps=(int)info->interest_comps->n;
akmhoque53f64222012-09-05 13:57:51 -0500243
akmhoque1c9b92f2012-08-13 10:57:50 -0500244 for(i=0;i<name_comps;i++)
245 {
246 res=ccn_name_comp_strcmp(info->interest_ccnb,info->interest_comps,i,"nlsr");
247 if( res == 0)
248 {
akmhoqueea3603e2012-08-13 11:24:09 -0500249 nlsr_position=i;
akmhoque1c9b92f2012-08-13 10:57:50 -0500250 break;
251 }
252 }
253
akmhoque3d319d42013-02-20 11:08:32 -0600254 res=ccn_name_comp_get(info->interest_ccnb, info->interest_comps,nlsr_position+1,
255 &comp_ptr1, &comp_size);
akmhoque53f64222012-09-05 13:57:51 -0500256
akmhoque1c9b92f2012-08-13 10:57:50 -0500257
akmhoqued79438d2012-08-27 13:31:42 -0500258 if(!strcmp((char *)comp_ptr1,"info"))
259 {
260 process_incoming_interest_info(selfp,info);
261 }
akmhoqueb77b95f2013-02-08 12:28:47 -0600262
akmhoque53f64222012-09-05 13:57:51 -0500263}
264
akmhoque3d319d42013-02-20 11:08:32 -0600265/**
266*
267*/
268
akmhoque53f64222012-09-05 13:57:51 -0500269void
270process_incoming_interest_info(struct ccn_closure *selfp, struct ccn_upcall_info *info)
271{
akmhoque7b791452012-10-30 11:24:56 -0500272 if ( nlsr->debugging )
273 {
274 printf("process_incoming_interest_info called \n");
275 printf("Sending Info Content back.....\n");
276 }
277 if ( nlsr->detailed_logging )
278 {
akmhoque3d319d42013-02-20 11:08:32 -0600279 writeLogg(__FILE__,__FUNCTION__,__LINE__,"process_incoming_interest_info"
280 " called \n");
akmhoque7b791452012-10-30 11:24:56 -0500281 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Sending Info Content back.....\n");
282 }
283
akmhoque53f64222012-09-05 13:57:51 -0500284
akmhoque53f64222012-09-05 13:57:51 -0500285 int res;
286 struct ccn_charbuf *data=ccn_charbuf_create();
287 struct ccn_charbuf *name=ccn_charbuf_create();
akmhoquedd7a7a72013-02-20 08:25:41 -0600288
akmhoque03004e62012-09-06 01:12:28 -0500289
akmhoque3d319d42013-02-20 11:08:32 -0600290 res=ccn_charbuf_append(name, info->interest_ccnb + info->pi->offset[CCN_PI_B_Name],
291 info->pi->offset[CCN_PI_E_Name] - info->pi->offset[CCN_PI_B_Name]);
akmhoque53f64222012-09-05 13:57:51 -0500292 if (res >= 0)
293 {
akmhoquedd7a7a72013-02-20 08:25:41 -0600294
akmhoquec06dcf12013-02-20 08:13:37 -0600295
296 struct ccn_charbuf *pubid = ccn_charbuf_create();
297 struct ccn_charbuf *pubkey = ccn_charbuf_create();
298
akmhoque7ca519b2013-02-20 09:49:43 -0600299 //pubid is the digest_result pubkey is result
300 ccn_get_public_key(nlsr->ccn, NULL, pubid, pubkey);
301
302
akmhoquedd7a7a72013-02-20 08:25:41 -0600303
304 struct ccn_signing_params sp=CCN_SIGNING_PARAMS_INIT;
305 sp.template_ccnb=ccn_charbuf_create();
akmhoque3d319d42013-02-20 11:08:32 -0600306
akmhoquec06dcf12013-02-20 08:13:37 -0600307 ccn_charbuf_append_tt(sp.template_ccnb, CCN_DTAG_SignedInfo, CCN_DTAG);
308 ccn_charbuf_append_tt(sp.template_ccnb, CCN_DTAG_KeyLocator, CCN_DTAG);
309 ccn_charbuf_append_tt(sp.template_ccnb, CCN_DTAG_KeyName, CCN_DTAG);
akmhoquedd7a7a72013-02-20 08:25:41 -0600310 ccn_charbuf_append_charbuf(sp.template_ccnb, name);
akmhoque53f64222012-09-05 13:57:51 -0500311 ccn_charbuf_append_closer(sp.template_ccnb);
akmhoquec06dcf12013-02-20 08:13:37 -0600312 ccn_charbuf_append_closer(sp.template_ccnb);
akmhoquec06dcf12013-02-20 08:13:37 -0600313 ccn_charbuf_append_closer(sp.template_ccnb);
akmhoquedd7a7a72013-02-20 08:25:41 -0600314
akmhoquec06dcf12013-02-20 08:13:37 -0600315 sp.sp_flags |= CCN_SP_TEMPL_KEY_LOCATOR;
316 sp.sp_flags |= CCN_SP_FINAL_BLOCK;
317 sp.type = CCN_CONTENT_KEY;
akmhoque63571b02013-02-20 08:18:49 -0600318 sp.freshness = 10;
akmhoquec06dcf12013-02-20 08:13:37 -0600319
akmhoque3d319d42013-02-20 11:08:32 -0600320
321 /*ccn_charbuf_append_tt(sp.template_ccnb,CCN_DTAG_SignedInfo, CCN_DTAG);
322 ccnb_tagged_putf(sp.template_ccnb, CCN_DTAG_FreshnessSeconds, "%ld", 10);
323 sp.sp_flags |= CCN_SP_TEMPL_FRESHNESS;
324 ccn_charbuf_append_closer(sp.template_ccnb);
325 */
akmhoque53f64222012-09-05 13:57:51 -0500326
akmhoqueb77b95f2013-02-08 12:28:47 -0600327 char *raw_data=(char *)malloc(20);
328 memset(raw_data,0,20);
329 sprintf(raw_data,"%s", nlsr->lsdb->lsdb_version);
akmhoque53f64222012-09-05 13:57:51 -0500330
akmhoquec06dcf12013-02-20 08:13:37 -0600331 res= ccn_sign_content(nlsr->ccn, data, name, &sp, pubkey->buf,pubkey->length);
akmhoque53f64222012-09-05 13:57:51 -0500332 if(res >= 0)
akmhoque7b791452012-10-30 11:24:56 -0500333 {
334 if ( nlsr->debugging )
335 printf("Signing info Content is successful \n");
336 if ( nlsr->detailed_logging )
akmhoque3d319d42013-02-20 11:08:32 -0600337 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Signing info Content"
338 " is successful \n");
akmhoque53f64222012-09-05 13:57:51 -0500339
akmhoque7b791452012-10-30 11:24:56 -0500340 }
akmhoque53f64222012-09-05 13:57:51 -0500341 res=ccn_put(nlsr->ccn,data->buf,data->length);
342 if(res >= 0)
akmhoque7b791452012-10-30 11:24:56 -0500343 {
344 if ( nlsr->debugging )
345 printf("Sending Info Content is successful \n");
346 if ( nlsr->detailed_logging )
akmhoque3d319d42013-02-20 11:08:32 -0600347 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Sending info Content"
348 " is successful \n");
akmhoque7b791452012-10-30 11:24:56 -0500349 }
350
akmhoque53f64222012-09-05 13:57:51 -0500351
352
353 struct name_prefix *nbr=(struct name_prefix * )malloc(sizeof(struct name_prefix *));
akmhoque03004e62012-09-06 01:12:28 -0500354 get_lsa_identifier(nbr,selfp,info,-1);
akmhoque7b791452012-10-30 11:24:56 -0500355
356 if ( nlsr->debugging )
akmhoque3d319d42013-02-20 11:08:32 -0600357 printf("Neighbor : %s Length : %d Status : %d\n",nbr->name,nbr->length,
358 get_adjacent_status(nbr));
akmhoque7b791452012-10-30 11:24:56 -0500359 if ( nlsr->detailed_logging )
akmhoque3d319d42013-02-20 11:08:32 -0600360 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Neighbor : %s Length : %d"
361 " Status : %d\n",nbr->name,nbr->length,get_adjacent_status(nbr));
akmhoque03004e62012-09-06 01:12:28 -0500362
akmhoque53f64222012-09-05 13:57:51 -0500363
akmhoque3d319d42013-02-20 11:08:32 -0600364 if( get_adjacent_status(nbr) == 0 && get_timed_out_number(nbr) >=
365 nlsr->interest_retry )
akmhoque53f64222012-09-05 13:57:51 -0500366 {
akmhoqueb8195202012-09-25 11:53:23 -0500367 update_adjacent_timed_out_zero_to_adl(nbr);
akmhoque53f64222012-09-05 13:57:51 -0500368 send_info_interest_to_neighbor(nbr);
369 }
370
371 free(nbr);
372 free(raw_data);
373 ccn_charbuf_destroy(&sp.template_ccnb);
akmhoquec06dcf12013-02-20 08:13:37 -0600374 ccn_charbuf_destroy(&pubid);
375 ccn_charbuf_destroy(&pubkey);
akmhoque53f64222012-09-05 13:57:51 -0500376 }
akmhoque03004e62012-09-06 01:12:28 -0500377
akmhoque53f64222012-09-05 13:57:51 -0500378 ccn_charbuf_destroy(&data);
akmhoque03004e62012-09-06 01:12:28 -0500379 ccn_charbuf_destroy(&name);
akmhoque53f64222012-09-05 13:57:51 -0500380
akmhoquebf1aa832012-08-13 13:26:59 -0500381}
382
383
akmhoque03004e62012-09-06 01:12:28 -0500384/* Call back function registered in ccnd to get all content coming to NLSR application */
385
akmhoque53f64222012-09-05 13:57:51 -0500386enum ccn_upcall_res incoming_content(struct ccn_closure* selfp,
387 enum ccn_upcall_kind kind, struct ccn_upcall_info* info)
akmhoqued79438d2012-08-27 13:31:42 -0500388{
389
akmhoqueffacaa82012-09-13 17:48:30 -0500390 nlsr_lock();
akmhoque03004e62012-09-06 01:12:28 -0500391
akmhoque53f64222012-09-05 13:57:51 -0500392 switch(kind) {
393 case CCN_UPCALL_FINAL:
394 break;
395 case CCN_UPCALL_CONTENT:
akmhoque7b791452012-10-30 11:24:56 -0500396 if ( nlsr->debugging )
397 printf("Content Received for Name: ");
398 if ( nlsr->detailed_logging )
399 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Content Received for Name: ");
400
akmhoque03004e62012-09-06 01:12:28 -0500401 struct ccn_charbuf*c;
402 c=ccn_charbuf_create();
403 ccn_uri_append(c,info->interest_ccnb,info->pi->offset[CCN_PI_E],0);
akmhoque7b791452012-10-30 11:24:56 -0500404 if ( nlsr->debugging )
405 printf("%s\n",ccn_charbuf_as_string(c));
406 if ( nlsr->detailed_logging )
407 writeLogg(__FILE__,__FUNCTION__,__LINE__,"%s\n",ccn_charbuf_as_string(c));
408
akmhoque03004e62012-09-06 01:12:28 -0500409 ccn_charbuf_destroy(&c);
410
411 process_incoming_content(selfp,info);
akmhoqued79438d2012-08-27 13:31:42 -0500412
akmhoque53f64222012-09-05 13:57:51 -0500413 break;
414 case CCN_UPCALL_INTEREST_TIMED_OUT:
akmhoque7b791452012-10-30 11:24:56 -0500415 //printf("Interest Timed Out Received for Name: ");
416 if ( nlsr->debugging )
417 printf("Interest Timed Out Received for Name: ");
418 if ( nlsr->detailed_logging )
akmhoque3d319d42013-02-20 11:08:32 -0600419 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Interest Timed Out Receiv"
420 "ed for Name: ");
akmhoque03004e62012-09-06 01:12:28 -0500421
422 struct ccn_charbuf*ito;
423 ito=ccn_charbuf_create();
424 ccn_uri_append(ito,info->interest_ccnb,info->pi->offset[CCN_PI_E],0);
akmhoque7b791452012-10-30 11:24:56 -0500425 if ( nlsr->debugging )
426 printf("%s\n",ccn_charbuf_as_string(ito));
427 if ( nlsr->detailed_logging )
428 writeLogg(__FILE__,__FUNCTION__,__LINE__,"%s\n",ccn_charbuf_as_string(ito));
akmhoque03004e62012-09-06 01:12:28 -0500429 ccn_charbuf_destroy(&ito);
430
akmhoquec06dcf12013-02-20 08:13:37 -0600431
432
akmhoque53f64222012-09-05 13:57:51 -0500433 process_incoming_timed_out_interest(selfp,info);
akmhoqued79438d2012-08-27 13:31:42 -0500434
akmhoque53f64222012-09-05 13:57:51 -0500435 break;
436 default:
437 fprintf(stderr, "Unexpected response of kind %d\n", kind);
akmhoque7b791452012-10-30 11:24:56 -0500438 if ( nlsr->debugging )
akmhoque3d319d42013-02-20 11:08:32 -0600439 printf("Unexpected response of kind %d\n", kind);
akmhoque7b791452012-10-30 11:24:56 -0500440 if ( nlsr->detailed_logging )
akmhoque3d319d42013-02-20 11:08:32 -0600441 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Unexpected response of "
442 "kind %d\n", kind);
akmhoqueffacaa82012-09-13 17:48:30 -0500443 break;
akmhoque53f64222012-09-05 13:57:51 -0500444 }
akmhoqueffacaa82012-09-13 17:48:30 -0500445
446 nlsr_unlock();
akmhoqued79438d2012-08-27 13:31:42 -0500447
akmhoque53f64222012-09-05 13:57:51 -0500448 return CCN_UPCALL_RESULT_OK;
akmhoqued79438d2012-08-27 13:31:42 -0500449}
450
akmhoque03004e62012-09-06 01:12:28 -0500451
akmhoqued79438d2012-08-27 13:31:42 -0500452void
akmhoque53f64222012-09-05 13:57:51 -0500453process_incoming_content(struct ccn_closure *selfp, struct ccn_upcall_info* info)
akmhoqued79438d2012-08-27 13:31:42 -0500454{
akmhoque7b791452012-10-30 11:24:56 -0500455 if ( nlsr->debugging )
456 printf("process_incoming_content called \n");
457 if ( nlsr->detailed_logging )
458 writeLogg(__FILE__,__FUNCTION__,__LINE__,"process_incoming_content called \n");
akmhoque53f64222012-09-05 13:57:51 -0500459
460 const unsigned char *comp_ptr1;
461 size_t comp_size;
462 int res,i;
463 int nlsr_position=0;
464 int name_comps=(int)info->interest_comps->n;
465
466 for(i=0;i<name_comps;i++)
467 {
468 res=ccn_name_comp_strcmp(info->interest_ccnb,info->interest_comps,i,"nlsr");
469 if( res == 0)
470 {
471 nlsr_position=i;
472 break;
473 }
474 }
475
akmhoque3d319d42013-02-20 11:08:32 -0600476 res=ccn_name_comp_get(info->interest_ccnb, info->interest_comps,
477 nlsr_position+1,&comp_ptr1, &comp_size);
akmhoque53f64222012-09-05 13:57:51 -0500478
akmhoque53f64222012-09-05 13:57:51 -0500479
480 if(!strcmp((char *)comp_ptr1,"info"))
481 {
482 process_incoming_content_info(selfp,info);
483 }
akmhoque03004e62012-09-06 01:12:28 -0500484
akmhoque53f64222012-09-05 13:57:51 -0500485}
486
akmhoque03004e62012-09-06 01:12:28 -0500487
akmhoque53f64222012-09-05 13:57:51 -0500488void
akmhoque3d319d42013-02-20 11:08:32 -0600489process_incoming_content_info(struct ccn_closure *selfp,
490 struct ccn_upcall_info* info)
akmhoque53f64222012-09-05 13:57:51 -0500491{
akmhoque7b791452012-10-30 11:24:56 -0500492 if ( nlsr->debugging )
493 printf("process_incoming_content_info called \n");
494 if ( nlsr->detailed_logging )
akmhoque3d319d42013-02-20 11:08:32 -0600495 writeLogg(__FILE__,__FUNCTION__,__LINE__,"process_incoming_content_info"
496 " called \n");
akmhoque03004e62012-09-06 01:12:28 -0500497
akmhoque53f64222012-09-05 13:57:51 -0500498 struct name_prefix *nbr=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
499 get_nbr(nbr,selfp,info);
akmhoque03004e62012-09-06 01:12:28 -0500500
akmhoque7b791452012-10-30 11:24:56 -0500501 if ( nlsr->debugging )
akmhoque3d319d42013-02-20 11:08:32 -0600502 printf("Info Content Received For Neighbor: %s Length:%d\n",nbr->name,
503 nbr->length);
akmhoque7b791452012-10-30 11:24:56 -0500504 if ( nlsr->detailed_logging )
akmhoque3d319d42013-02-20 11:08:32 -0600505 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Info Content Received For Nei"
506 "ghbor: %s Length:%d\n",nbr->name,nbr->length);
akmhoque03004e62012-09-06 01:12:28 -0500507
akmhoque53f64222012-09-05 13:57:51 -0500508
akmhoquec06dcf12013-02-20 08:13:37 -0600509 if ( contain_key_name(info->content_ccnb, info->pco) == 1)
510 {
511 struct ccn_charbuf *key_name=get_key_name(info->content_ccnb, info->pco);
akmhoque7ca519b2013-02-20 09:49:43 -0600512 struct ccn_charbuf *key_uri = ccn_charbuf_create();
513 ccn_uri_append(key_uri, key_name->buf, key_name->length, 1);
514
akmhoquec06dcf12013-02-20 08:13:37 -0600515 if(nlsr->debugging)
akmhoque7ca519b2013-02-20 09:49:43 -0600516 printf("Key Name: %s\n",ccn_charbuf_as_string(key_uri));
517
518 ccn_charbuf_destroy(&key_uri);
519 ccn_charbuf_destroy(&key_name);
520
521
akmhoquec06dcf12013-02-20 08:13:37 -0600522 }
akmhoque03004e62012-09-06 01:12:28 -0500523
akmhoque53f64222012-09-05 13:57:51 -0500524 update_adjacent_timed_out_zero_to_adl(nbr);
525 update_adjacent_status_to_adl(nbr,NBR_ACTIVE);
akmhoque53f64222012-09-05 13:57:51 -0500526 print_adjacent_from_adl();
527
akmhoque03004e62012-09-06 01:12:28 -0500528
529
akmhoque53f64222012-09-05 13:57:51 -0500530 if(!nlsr->is_build_adj_lsa_sheduled)
531 {
akmhoque7b791452012-10-30 11:24:56 -0500532 if ( nlsr->debugging )
533 printf("Scheduling Build and Install Adj LSA...\n");
534 if ( nlsr->detailed_logging )
akmhoque3d319d42013-02-20 11:08:32 -0600535 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Scheduling Build and Inst"
536 "all Adj LSA...\n");
537 nlsr->event_build_adj_lsa = ccn_schedule_event(nlsr->sched, 100000,
538 &build_and_install_adj_lsa, NULL, 0);
akmhoque53f64222012-09-05 13:57:51 -0500539 nlsr->is_build_adj_lsa_sheduled=1;
540 }
541 else
542 {
akmhoque7b791452012-10-30 11:24:56 -0500543 if ( nlsr->debugging )
544 printf("Build and Install Adj LSA already scheduled\n");
545 if ( nlsr->detailed_logging )
akmhoque3d319d42013-02-20 11:08:32 -0600546 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Build and Install Adj LSA"
547 " already scheduled\n");
akmhoque53f64222012-09-05 13:57:51 -0500548 }
549
akmhoque03004e62012-09-06 01:12:28 -0500550
akmhoque53f64222012-09-05 13:57:51 -0500551 free(nbr);
akmhoque03004e62012-09-06 01:12:28 -0500552
akmhoque53f64222012-09-05 13:57:51 -0500553
554}
555
akmhoque03004e62012-09-06 01:12:28 -0500556
akmhoque53f64222012-09-05 13:57:51 -0500557
akmhoque03004e62012-09-06 01:12:28 -0500558
akmhoque53f64222012-09-05 13:57:51 -0500559void
akmhoque3d319d42013-02-20 11:08:32 -0600560process_incoming_timed_out_interest(struct ccn_closure* selfp,
561 struct ccn_upcall_info* info)
akmhoque53f64222012-09-05 13:57:51 -0500562{
akmhoque3171d652012-11-13 11:44:33 -0600563
564
565 if ( nlsr->debugging )
566 printf("process_incoming_timed_out_interest called \n");
567 if ( nlsr->detailed_logging )
akmhoque3d319d42013-02-20 11:08:32 -0600568 writeLogg(__FILE__,__FUNCTION__,__LINE__,"process_incoming_timed_out_int"
569 "erest called \n");
akmhoque3171d652012-11-13 11:44:33 -0600570
akmhoque53f64222012-09-05 13:57:51 -0500571 int res,i;
572 int nlsr_position=0;
573 int name_comps=(int)info->interest_comps->n;
574
575 for(i=0;i<name_comps;i++)
576 {
577 res=ccn_name_comp_strcmp(info->interest_ccnb,info->interest_comps,i,"nlsr");
578 if( res == 0)
579 {
580 nlsr_position=i;
581 break;
582 }
583 }
584
akmhoque3d319d42013-02-20 11:08:32 -0600585 if(ccn_name_comp_strcmp(info->interest_ccnb,info->interest_comps,
586 nlsr_position+1,"info") == 0)
akmhoque53f64222012-09-05 13:57:51 -0500587 {
588 process_incoming_timed_out_interest_info(selfp,info);
589 }
590}
591
592void
akmhoque3d319d42013-02-20 11:08:32 -0600593process_incoming_timed_out_interest_info(struct ccn_closure* selfp, struct
594 ccn_upcall_info* info)
akmhoque53f64222012-09-05 13:57:51 -0500595{
akmhoque3171d652012-11-13 11:44:33 -0600596
597 if ( nlsr->debugging )
598 printf("process_incoming_timed_out_interest_info called \n");
599 if ( nlsr->detailed_logging )
akmhoque3d319d42013-02-20 11:08:32 -0600600 writeLogg(__FILE__,__FUNCTION__,__LINE__,"process_incoming_timed_out_int"
601 "erest_info called \n");
akmhoque53f64222012-09-05 13:57:51 -0500602
603 struct name_prefix *nbr=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
604 get_nbr(nbr,selfp,info);
605
akmhoque3171d652012-11-13 11:44:33 -0600606 if ( nlsr->debugging )
akmhoque3d319d42013-02-20 11:08:32 -0600607 printf("Info Interest Timed Out for for Neighbor: %s Length:%d\n",
608 nbr->name,nbr->length);
akmhoque3171d652012-11-13 11:44:33 -0600609 if ( nlsr->detailed_logging )
akmhoque3d319d42013-02-20 11:08:32 -0600610 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Info Interest Timed Out for"
611 " Neighbor: %s Length:%d\n",nbr->name,nbr->length);
akmhoque3171d652012-11-13 11:44:33 -0600612
akmhoque03004e62012-09-06 01:12:28 -0500613
614
akmhoque53f64222012-09-05 13:57:51 -0500615 update_adjacent_timed_out_to_adl(nbr,1);
616 print_adjacent_from_adl();
617 int timed_out=get_timed_out_number(nbr);
akmhoque03004e62012-09-06 01:12:28 -0500618
akmhoque3171d652012-11-13 11:44:33 -0600619 if ( nlsr->debugging )
akmhoque3d319d42013-02-20 11:08:32 -0600620 printf("Neighbor: %s Info Interest Timed Out: %d times\n",nbr->name,
621 timed_out);
akmhoque3171d652012-11-13 11:44:33 -0600622 if ( nlsr->detailed_logging )
akmhoque3d319d42013-02-20 11:08:32 -0600623 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Neighbor: %s Info Interest "
624 "Timed Out: %d times\n",nbr->name,timed_out);
akmhoque3171d652012-11-13 11:44:33 -0600625
akmhoque53f64222012-09-05 13:57:51 -0500626
627 if(timed_out<nlsr->interest_retry && timed_out>0) // use configured variables
628 {
akmhoque53f64222012-09-05 13:57:51 -0500629 send_info_interest_to_neighbor(nbr);
630 }
631 else
akmhoque3171d652012-11-13 11:44:33 -0600632 {
akmhoque53f64222012-09-05 13:57:51 -0500633 update_adjacent_status_to_adl(nbr,NBR_DOWN);
634 if(!nlsr->is_build_adj_lsa_sheduled)
635 {
akmhoque3d319d42013-02-20 11:08:32 -0600636 nlsr->event_build_adj_lsa = ccn_schedule_event(nlsr->sched, 1000,
637 &build_and_install_adj_lsa, NULL, 0);
akmhoque53f64222012-09-05 13:57:51 -0500638 nlsr->is_build_adj_lsa_sheduled=1;
639 }
640 }
641
642 free(nbr);
akmhoque03004e62012-09-06 01:12:28 -0500643
akmhoque53f64222012-09-05 13:57:51 -0500644
645}
646
akmhoque29c1db52012-09-07 14:47:43 -0500647
akmhoque03004e62012-09-06 01:12:28 -0500648int
akmhoque3d319d42013-02-20 11:08:32 -0600649send_info_interest(struct ccn_schedule *sched, void *clienth,
650 struct ccn_scheduled_event *ev, int flags)
akmhoque53f64222012-09-05 13:57:51 -0500651{
akmhoqueffacaa82012-09-13 17:48:30 -0500652 if(flags == CCN_SCHEDULE_CANCEL)
653 {
654 return -1;
655 }
656
657 nlsr_lock();
658
akmhoque3171d652012-11-13 11:44:33 -0600659 if ( nlsr->debugging )
660 printf("send_info_interest called \n");
661 if ( nlsr->detailed_logging )
662 writeLogg(__FILE__,__FUNCTION__,__LINE__,"send_info_interest called \n");
663
664 if ( nlsr->debugging )
665 printf("\n");
666 if ( nlsr->detailed_logging )
667 writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n");
akmhoqued79438d2012-08-27 13:31:42 -0500668
akmhoque53f64222012-09-05 13:57:51 -0500669 int adl_element,i;
akmhoque53f64222012-09-05 13:57:51 -0500670 struct ndn_neighbor *nbr;
671
672 struct hashtb_enumerator ee;
673 struct hashtb_enumerator *e = &ee;
674
675 hashtb_start(nlsr->adl, e);
676 adl_element=hashtb_n(nlsr->adl);
677
678 for(i=0;i<adl_element;i++)
679 {
680 nbr=e->data;
681 send_info_interest_to_neighbor(nbr->neighbor);
682 hashtb_next(e);
683 }
akmhoque53f64222012-09-05 13:57:51 -0500684 hashtb_end(e);
685
akmhoqueffacaa82012-09-13 17:48:30 -0500686 nlsr_unlock();
687
akmhoque3d319d42013-02-20 11:08:32 -0600688 nlsr->event = ccn_schedule_event(nlsr->sched, 60000000, &send_info_interest,
689 NULL, 0);
akmhoque9fa58a82012-10-05 07:56:02 -0500690
akmhoque53f64222012-09-05 13:57:51 -0500691 return 0;
692}
693
694void
695send_info_interest_to_neighbor(struct name_prefix *nbr)
696{
akmhoque3171d652012-11-13 11:44:33 -0600697
698 if ( nlsr->debugging )
699 printf("send_info_interest_to_neighbor called \n");
700 if ( nlsr->detailed_logging )
akmhoque3d319d42013-02-20 11:08:32 -0600701 writeLogg(__FILE__,__FUNCTION__,__LINE__,"send_info_interest_to_neighbor"
702 " called \n");
akmhoque3171d652012-11-13 11:44:33 -0600703
akmhoqued79438d2012-08-27 13:31:42 -0500704
705 int res;
akmhoque53f64222012-09-05 13:57:51 -0500706 char info_str[5];
707 char nlsr_str[5];
akmhoque53f64222012-09-05 13:57:51 -0500708
akmhoqued79438d2012-08-27 13:31:42 -0500709 memset(&nlsr_str,0,5);
710 sprintf(nlsr_str,"nlsr");
711 memset(&info_str,0,5);
712 sprintf(info_str,"info");
713
akmhoque53f64222012-09-05 13:57:51 -0500714
715 struct ccn_charbuf *name;
akmhoqued79438d2012-08-27 13:31:42 -0500716 name=ccn_charbuf_create();
akmhoqued79438d2012-08-27 13:31:42 -0500717
akmhoque3d319d42013-02-20 11:08:32 -0600718 char *int_name=(char *)malloc(strlen(nbr->name)+1+strlen(nlsr_str)+1+
719 strlen(info_str)+strlen(nlsr->router_name)+1);
720 memset(int_name,0,strlen(nbr->name)+1+strlen(nlsr_str)+1+strlen(info_str)+
721 strlen(nlsr->router_name)+1);
akmhoque53f64222012-09-05 13:57:51 -0500722 memcpy(int_name+strlen(int_name),nbr->name,strlen(nbr->name));
723 memcpy(int_name+strlen(int_name),"/",1);
724 memcpy(int_name+strlen(int_name),nlsr_str,strlen(nlsr_str));
725 memcpy(int_name+strlen(int_name),"/",1);
726 memcpy(int_name+strlen(int_name),info_str,strlen(info_str));
akmhoque03004e62012-09-06 01:12:28 -0500727 memcpy(int_name+strlen(int_name),nlsr->router_name,strlen(nlsr->router_name));
akmhoque53f64222012-09-05 13:57:51 -0500728
729
730 res=ccn_name_from_uri(name,int_name);
731 if ( res >=0 )
732 {
akmhoque53f64222012-09-05 13:57:51 -0500733 /* adding InterestLifeTime and InterestScope filter */
734
735 struct ccn_charbuf *templ;
736 templ = ccn_charbuf_create();
737
738 ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG);
739 ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG);
740 ccn_charbuf_append_closer(templ); /* </Name> */
akmhoque03004e62012-09-06 01:12:28 -0500741 ccn_charbuf_append_tt(templ, CCN_DTAG_Scope, CCN_DTAG);
742 ccn_charbuf_append_tt(templ, 1, CCN_UDATA);
akmhoqueb77b95f2013-02-08 12:28:47 -0600743 /* Adding InterestLifeTime and InterestScope filter done */
akmhoque3d319d42013-02-20 11:08:32 -0600744 ccn_charbuf_append(templ, "2", 1); //scope of interest: 2
745 //(not further than next host)
akmhoque03004e62012-09-06 01:12:28 -0500746 ccn_charbuf_append_closer(templ); /* </Scope> */
akmhoque53f64222012-09-05 13:57:51 -0500747
748 appendLifetime(templ,nlsr->interest_resend_time);
akmhoqueb77b95f2013-02-08 12:28:47 -0600749 unsigned int face_id=get_next_hop_face_from_adl(nbr->name);
750 ccnb_tagged_putf(templ, CCN_DTAG_FaceID, "%u", face_id);
akmhoque53f64222012-09-05 13:57:51 -0500751 ccn_charbuf_append_closer(templ); /* </Interest> */
akmhoqueb77b95f2013-02-08 12:28:47 -0600752
akmhoque53f64222012-09-05 13:57:51 -0500753
akmhoque3171d652012-11-13 11:44:33 -0600754 if ( nlsr->debugging )
akmhoque3d319d42013-02-20 11:08:32 -0600755 printf("Sending info interest on name prefix : %s through Face:%u\n"
756 ,int_name,face_id);
akmhoque3171d652012-11-13 11:44:33 -0600757 if ( nlsr->detailed_logging )
akmhoque3d319d42013-02-20 11:08:32 -0600758 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Sending info interest on"
759 "name prefix : %s through Face:%u\n",int_name,face_id);
akmhoque53f64222012-09-05 13:57:51 -0500760
761 res=ccn_express_interest(nlsr->ccn,name,&(nlsr->in_content),templ);
762
763 if ( res >= 0 )
akmhoque3171d652012-11-13 11:44:33 -0600764 {
765 if ( nlsr->debugging )
766 printf("Info interest sending Successfull .... \n");
767 if ( nlsr->detailed_logging )
akmhoque3d319d42013-02-20 11:08:32 -0600768 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Info"
769 "interest sending Successfull .... \n");
akmhoque3171d652012-11-13 11:44:33 -0600770 }
akmhoque53f64222012-09-05 13:57:51 -0500771 ccn_charbuf_destroy(&templ);
772 }
773 ccn_charbuf_destroy(&name);
774 free(int_name);
akmhoquec06dcf12013-02-20 08:13:37 -0600775}
akmhoque53f64222012-09-05 13:57:51 -0500776
akmhoquec06dcf12013-02-20 08:13:37 -0600777int
778contain_key_name(const unsigned char *ccnb, struct ccn_parsed_ContentObject *pco)
779{
780 if (pco->offset[CCN_PCO_B_KeyLocator] == pco->offset[CCN_PCO_E_KeyLocator])
781 return -1;
782
783 struct ccn_buf_decoder decoder;
784 struct ccn_buf_decoder *d;
akmhoque3d319d42013-02-20 11:08:32 -0600785 d = ccn_buf_decoder_start(&decoder, ccnb +
786 pco->offset[CCN_PCO_B_Key_Certificate_KeyName],
787 pco->offset[CCN_PCO_E_Key_Certificate_KeyName] -
788 pco->offset[CCN_PCO_B_Key_Certificate_KeyName]);
akmhoquec06dcf12013-02-20 08:13:37 -0600789 if (ccn_buf_match_dtag(d, CCN_DTAG_KeyName))
790 return 1;
791
792 return -1;
793}
794
795struct ccn_charbuf *
796get_key_name(const unsigned char *ccnb, struct ccn_parsed_ContentObject *pco)
797{
798 struct ccn_charbuf *key_name = ccn_charbuf_create();
akmhoque3d319d42013-02-20 11:08:32 -0600799 ccn_charbuf_append(key_name, ccnb + pco->offset[CCN_PCO_B_KeyName_Name],
800 pco->offset[CCN_PCO_E_KeyName_Name] - pco->offset[CCN_PCO_B_KeyName_Name]);
akmhoquec06dcf12013-02-20 08:13:37 -0600801
802 return key_name;
akmhoque53f64222012-09-05 13:57:51 -0500803}