blob: db9c469834a480198ca7c3f43ed6ea832d706ca1 [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/**
akmhoque7ab49a32013-02-20 11:27:51 -0600266* Processes incoming interest for "info" interest. Send back reply content back,
267* if interest comes from a neighbor with status down, NLSR will send "info"
268* ineterst to that neighbor
akmhoque3d319d42013-02-20 11:08:32 -0600269*/
270
akmhoque53f64222012-09-05 13:57:51 -0500271void
272process_incoming_interest_info(struct ccn_closure *selfp, struct ccn_upcall_info *info)
273{
akmhoque7b791452012-10-30 11:24:56 -0500274 if ( nlsr->debugging )
275 {
276 printf("process_incoming_interest_info called \n");
277 printf("Sending Info Content back.....\n");
278 }
279 if ( nlsr->detailed_logging )
280 {
akmhoque3d319d42013-02-20 11:08:32 -0600281 writeLogg(__FILE__,__FUNCTION__,__LINE__,"process_incoming_interest_info"
282 " called \n");
akmhoque7b791452012-10-30 11:24:56 -0500283 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Sending Info Content back.....\n");
284 }
285
akmhoque53f64222012-09-05 13:57:51 -0500286
akmhoque53f64222012-09-05 13:57:51 -0500287 int res;
288 struct ccn_charbuf *data=ccn_charbuf_create();
289 struct ccn_charbuf *name=ccn_charbuf_create();
akmhoquedd7a7a72013-02-20 08:25:41 -0600290
akmhoque03004e62012-09-06 01:12:28 -0500291
akmhoque3d319d42013-02-20 11:08:32 -0600292 res=ccn_charbuf_append(name, info->interest_ccnb + info->pi->offset[CCN_PI_B_Name],
293 info->pi->offset[CCN_PI_E_Name] - info->pi->offset[CCN_PI_B_Name]);
akmhoque53f64222012-09-05 13:57:51 -0500294 if (res >= 0)
295 {
akmhoquedd7a7a72013-02-20 08:25:41 -0600296
akmhoquec06dcf12013-02-20 08:13:37 -0600297
298 struct ccn_charbuf *pubid = ccn_charbuf_create();
299 struct ccn_charbuf *pubkey = ccn_charbuf_create();
300
akmhoque7ca519b2013-02-20 09:49:43 -0600301 //pubid is the digest_result pubkey is result
302 ccn_get_public_key(nlsr->ccn, NULL, pubid, pubkey);
303
304
akmhoquedd7a7a72013-02-20 08:25:41 -0600305
306 struct ccn_signing_params sp=CCN_SIGNING_PARAMS_INIT;
307 sp.template_ccnb=ccn_charbuf_create();
akmhoque3d319d42013-02-20 11:08:32 -0600308
akmhoquec06dcf12013-02-20 08:13:37 -0600309 ccn_charbuf_append_tt(sp.template_ccnb, CCN_DTAG_SignedInfo, CCN_DTAG);
310 ccn_charbuf_append_tt(sp.template_ccnb, CCN_DTAG_KeyLocator, CCN_DTAG);
311 ccn_charbuf_append_tt(sp.template_ccnb, CCN_DTAG_KeyName, CCN_DTAG);
akmhoquedd7a7a72013-02-20 08:25:41 -0600312 ccn_charbuf_append_charbuf(sp.template_ccnb, name);
akmhoque53f64222012-09-05 13:57:51 -0500313 ccn_charbuf_append_closer(sp.template_ccnb);
akmhoquec06dcf12013-02-20 08:13:37 -0600314 ccn_charbuf_append_closer(sp.template_ccnb);
akmhoquec06dcf12013-02-20 08:13:37 -0600315 ccn_charbuf_append_closer(sp.template_ccnb);
akmhoquedd7a7a72013-02-20 08:25:41 -0600316
akmhoquec06dcf12013-02-20 08:13:37 -0600317 sp.sp_flags |= CCN_SP_TEMPL_KEY_LOCATOR;
318 sp.sp_flags |= CCN_SP_FINAL_BLOCK;
319 sp.type = CCN_CONTENT_KEY;
akmhoque63571b02013-02-20 08:18:49 -0600320 sp.freshness = 10;
akmhoquec06dcf12013-02-20 08:13:37 -0600321
akmhoque3d319d42013-02-20 11:08:32 -0600322
323 /*ccn_charbuf_append_tt(sp.template_ccnb,CCN_DTAG_SignedInfo, CCN_DTAG);
324 ccnb_tagged_putf(sp.template_ccnb, CCN_DTAG_FreshnessSeconds, "%ld", 10);
325 sp.sp_flags |= CCN_SP_TEMPL_FRESHNESS;
326 ccn_charbuf_append_closer(sp.template_ccnb);
327 */
akmhoque53f64222012-09-05 13:57:51 -0500328
akmhoqueb77b95f2013-02-08 12:28:47 -0600329 char *raw_data=(char *)malloc(20);
330 memset(raw_data,0,20);
331 sprintf(raw_data,"%s", nlsr->lsdb->lsdb_version);
akmhoque53f64222012-09-05 13:57:51 -0500332
akmhoquec06dcf12013-02-20 08:13:37 -0600333 res= ccn_sign_content(nlsr->ccn, data, name, &sp, pubkey->buf,pubkey->length);
akmhoque53f64222012-09-05 13:57:51 -0500334 if(res >= 0)
akmhoque7b791452012-10-30 11:24:56 -0500335 {
336 if ( nlsr->debugging )
337 printf("Signing info Content is successful \n");
338 if ( nlsr->detailed_logging )
akmhoque3d319d42013-02-20 11:08:32 -0600339 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Signing info Content"
340 " is successful \n");
akmhoque53f64222012-09-05 13:57:51 -0500341
akmhoque7b791452012-10-30 11:24:56 -0500342 }
akmhoque53f64222012-09-05 13:57:51 -0500343 res=ccn_put(nlsr->ccn,data->buf,data->length);
344 if(res >= 0)
akmhoque7b791452012-10-30 11:24:56 -0500345 {
346 if ( nlsr->debugging )
347 printf("Sending Info Content is successful \n");
348 if ( nlsr->detailed_logging )
akmhoque3d319d42013-02-20 11:08:32 -0600349 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Sending info Content"
350 " is successful \n");
akmhoque7b791452012-10-30 11:24:56 -0500351 }
352
akmhoque53f64222012-09-05 13:57:51 -0500353
354
akmhoquee1dd7772013-02-24 14:21:49 -0600355 struct name_prefix *nbr=(struct name_prefix * )malloc(sizeof(struct name_prefix));
akmhoque03004e62012-09-06 01:12:28 -0500356 get_lsa_identifier(nbr,selfp,info,-1);
akmhoque7b791452012-10-30 11:24:56 -0500357
358 if ( nlsr->debugging )
akmhoque3d319d42013-02-20 11:08:32 -0600359 printf("Neighbor : %s Length : %d Status : %d\n",nbr->name,nbr->length,
360 get_adjacent_status(nbr));
akmhoque7b791452012-10-30 11:24:56 -0500361 if ( nlsr->detailed_logging )
akmhoque3d319d42013-02-20 11:08:32 -0600362 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Neighbor : %s Length : %d"
363 " Status : %d\n",nbr->name,nbr->length,get_adjacent_status(nbr));
akmhoque03004e62012-09-06 01:12:28 -0500364
akmhoque53f64222012-09-05 13:57:51 -0500365
akmhoque3d319d42013-02-20 11:08:32 -0600366 if( get_adjacent_status(nbr) == 0 && get_timed_out_number(nbr) >=
367 nlsr->interest_retry )
akmhoque53f64222012-09-05 13:57:51 -0500368 {
akmhoqueb8195202012-09-25 11:53:23 -0500369 update_adjacent_timed_out_zero_to_adl(nbr);
akmhoque53f64222012-09-05 13:57:51 -0500370 send_info_interest_to_neighbor(nbr);
371 }
372
373 free(nbr);
374 free(raw_data);
375 ccn_charbuf_destroy(&sp.template_ccnb);
akmhoquec06dcf12013-02-20 08:13:37 -0600376 ccn_charbuf_destroy(&pubid);
377 ccn_charbuf_destroy(&pubkey);
akmhoque53f64222012-09-05 13:57:51 -0500378 }
akmhoque03004e62012-09-06 01:12:28 -0500379
akmhoque53f64222012-09-05 13:57:51 -0500380 ccn_charbuf_destroy(&data);
akmhoque03004e62012-09-06 01:12:28 -0500381 ccn_charbuf_destroy(&name);
akmhoque53f64222012-09-05 13:57:51 -0500382
akmhoquebf1aa832012-08-13 13:26:59 -0500383}
384
385
akmhoque7ab49a32013-02-20 11:27:51 -0600386/**
387* Call back function registered in ccnd to get all content coming to NLSR
388* application
389*/
akmhoque03004e62012-09-06 01:12:28 -0500390
akmhoque53f64222012-09-05 13:57:51 -0500391enum ccn_upcall_res incoming_content(struct ccn_closure* selfp,
392 enum ccn_upcall_kind kind, struct ccn_upcall_info* info)
akmhoqued79438d2012-08-27 13:31:42 -0500393{
394
akmhoqueffacaa82012-09-13 17:48:30 -0500395 nlsr_lock();
akmhoque03004e62012-09-06 01:12:28 -0500396
akmhoque53f64222012-09-05 13:57:51 -0500397 switch(kind) {
398 case CCN_UPCALL_FINAL:
399 break;
400 case CCN_UPCALL_CONTENT:
akmhoque7b791452012-10-30 11:24:56 -0500401 if ( nlsr->debugging )
402 printf("Content Received for Name: ");
403 if ( nlsr->detailed_logging )
404 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Content Received for Name: ");
405
akmhoque03004e62012-09-06 01:12:28 -0500406 struct ccn_charbuf*c;
407 c=ccn_charbuf_create();
408 ccn_uri_append(c,info->interest_ccnb,info->pi->offset[CCN_PI_E],0);
akmhoque7b791452012-10-30 11:24:56 -0500409 if ( nlsr->debugging )
410 printf("%s\n",ccn_charbuf_as_string(c));
411 if ( nlsr->detailed_logging )
412 writeLogg(__FILE__,__FUNCTION__,__LINE__,"%s\n",ccn_charbuf_as_string(c));
413
akmhoque03004e62012-09-06 01:12:28 -0500414 ccn_charbuf_destroy(&c);
415
416 process_incoming_content(selfp,info);
akmhoqued79438d2012-08-27 13:31:42 -0500417
akmhoque53f64222012-09-05 13:57:51 -0500418 break;
419 case CCN_UPCALL_INTEREST_TIMED_OUT:
akmhoque7b791452012-10-30 11:24:56 -0500420 //printf("Interest Timed Out Received for Name: ");
421 if ( nlsr->debugging )
422 printf("Interest Timed Out Received for Name: ");
423 if ( nlsr->detailed_logging )
akmhoque3d319d42013-02-20 11:08:32 -0600424 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Interest Timed Out Receiv"
425 "ed for Name: ");
akmhoque03004e62012-09-06 01:12:28 -0500426
427 struct ccn_charbuf*ito;
428 ito=ccn_charbuf_create();
429 ccn_uri_append(ito,info->interest_ccnb,info->pi->offset[CCN_PI_E],0);
akmhoque7b791452012-10-30 11:24:56 -0500430 if ( nlsr->debugging )
431 printf("%s\n",ccn_charbuf_as_string(ito));
432 if ( nlsr->detailed_logging )
433 writeLogg(__FILE__,__FUNCTION__,__LINE__,"%s\n",ccn_charbuf_as_string(ito));
akmhoque03004e62012-09-06 01:12:28 -0500434 ccn_charbuf_destroy(&ito);
435
akmhoquec06dcf12013-02-20 08:13:37 -0600436
437
akmhoque53f64222012-09-05 13:57:51 -0500438 process_incoming_timed_out_interest(selfp,info);
akmhoqued79438d2012-08-27 13:31:42 -0500439
akmhoque53f64222012-09-05 13:57:51 -0500440 break;
441 default:
442 fprintf(stderr, "Unexpected response of kind %d\n", kind);
akmhoque7b791452012-10-30 11:24:56 -0500443 if ( nlsr->debugging )
akmhoque3d319d42013-02-20 11:08:32 -0600444 printf("Unexpected response of kind %d\n", kind);
akmhoque7b791452012-10-30 11:24:56 -0500445 if ( nlsr->detailed_logging )
akmhoque3d319d42013-02-20 11:08:32 -0600446 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Unexpected response of "
447 "kind %d\n", kind);
akmhoqueffacaa82012-09-13 17:48:30 -0500448 break;
akmhoque53f64222012-09-05 13:57:51 -0500449 }
akmhoqueffacaa82012-09-13 17:48:30 -0500450
451 nlsr_unlock();
akmhoqued79438d2012-08-27 13:31:42 -0500452
akmhoque53f64222012-09-05 13:57:51 -0500453 return CCN_UPCALL_RESULT_OK;
akmhoqued79438d2012-08-27 13:31:42 -0500454}
455
akmhoque7ab49a32013-02-20 11:27:51 -0600456/**
457* process any incoming content to NLSR from ccnd
458*/
akmhoque03004e62012-09-06 01:12:28 -0500459
akmhoqued79438d2012-08-27 13:31:42 -0500460void
akmhoque53f64222012-09-05 13:57:51 -0500461process_incoming_content(struct ccn_closure *selfp, struct ccn_upcall_info* info)
akmhoqued79438d2012-08-27 13:31:42 -0500462{
akmhoque7b791452012-10-30 11:24:56 -0500463 if ( nlsr->debugging )
464 printf("process_incoming_content called \n");
465 if ( nlsr->detailed_logging )
466 writeLogg(__FILE__,__FUNCTION__,__LINE__,"process_incoming_content called \n");
akmhoque53f64222012-09-05 13:57:51 -0500467
468 const unsigned char *comp_ptr1;
469 size_t comp_size;
470 int res,i;
471 int nlsr_position=0;
472 int name_comps=(int)info->interest_comps->n;
473
474 for(i=0;i<name_comps;i++)
475 {
476 res=ccn_name_comp_strcmp(info->interest_ccnb,info->interest_comps,i,"nlsr");
477 if( res == 0)
478 {
479 nlsr_position=i;
480 break;
481 }
482 }
483
akmhoque3d319d42013-02-20 11:08:32 -0600484 res=ccn_name_comp_get(info->interest_ccnb, info->interest_comps,
485 nlsr_position+1,&comp_ptr1, &comp_size);
akmhoque53f64222012-09-05 13:57:51 -0500486
akmhoque53f64222012-09-05 13:57:51 -0500487
488 if(!strcmp((char *)comp_ptr1,"info"))
489 {
490 process_incoming_content_info(selfp,info);
491 }
akmhoque03004e62012-09-06 01:12:28 -0500492
akmhoque53f64222012-09-05 13:57:51 -0500493}
494
akmhoque7ab49a32013-02-20 11:27:51 -0600495/**
496* process any incoming "info" content to NLSR from ccnd
497*/
akmhoque03004e62012-09-06 01:12:28 -0500498
akmhoque53f64222012-09-05 13:57:51 -0500499void
akmhoque3d319d42013-02-20 11:08:32 -0600500process_incoming_content_info(struct ccn_closure *selfp,
501 struct ccn_upcall_info* info)
akmhoque53f64222012-09-05 13:57:51 -0500502{
akmhoque7b791452012-10-30 11:24:56 -0500503 if ( nlsr->debugging )
504 printf("process_incoming_content_info called \n");
505 if ( nlsr->detailed_logging )
akmhoque3d319d42013-02-20 11:08:32 -0600506 writeLogg(__FILE__,__FUNCTION__,__LINE__,"process_incoming_content_info"
507 " called \n");
akmhoque03004e62012-09-06 01:12:28 -0500508
akmhoque53f64222012-09-05 13:57:51 -0500509 struct name_prefix *nbr=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
510 get_nbr(nbr,selfp,info);
akmhoque03004e62012-09-06 01:12:28 -0500511
akmhoque7b791452012-10-30 11:24:56 -0500512 if ( nlsr->debugging )
akmhoque3d319d42013-02-20 11:08:32 -0600513 printf("Info Content Received For Neighbor: %s Length:%d\n",nbr->name,
514 nbr->length);
akmhoque7b791452012-10-30 11:24:56 -0500515 if ( nlsr->detailed_logging )
akmhoque3d319d42013-02-20 11:08:32 -0600516 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Info Content Received For Nei"
517 "ghbor: %s Length:%d\n",nbr->name,nbr->length);
akmhoque03004e62012-09-06 01:12:28 -0500518
akmhoque53f64222012-09-05 13:57:51 -0500519
akmhoquec06dcf12013-02-20 08:13:37 -0600520 if ( contain_key_name(info->content_ccnb, info->pco) == 1)
521 {
522 struct ccn_charbuf *key_name=get_key_name(info->content_ccnb, info->pco);
akmhoque7ca519b2013-02-20 09:49:43 -0600523 struct ccn_charbuf *key_uri = ccn_charbuf_create();
524 ccn_uri_append(key_uri, key_name->buf, key_name->length, 1);
525
akmhoquec06dcf12013-02-20 08:13:37 -0600526 if(nlsr->debugging)
akmhoque7ca519b2013-02-20 09:49:43 -0600527 printf("Key Name: %s\n",ccn_charbuf_as_string(key_uri));
528
529 ccn_charbuf_destroy(&key_uri);
530 ccn_charbuf_destroy(&key_name);
531
532
akmhoquec06dcf12013-02-20 08:13:37 -0600533 }
akmhoque03004e62012-09-06 01:12:28 -0500534
akmhoque53f64222012-09-05 13:57:51 -0500535 update_adjacent_timed_out_zero_to_adl(nbr);
536 update_adjacent_status_to_adl(nbr,NBR_ACTIVE);
akmhoque53f64222012-09-05 13:57:51 -0500537 print_adjacent_from_adl();
538
akmhoque03004e62012-09-06 01:12:28 -0500539
540
akmhoque53f64222012-09-05 13:57:51 -0500541 if(!nlsr->is_build_adj_lsa_sheduled)
542 {
akmhoque7b791452012-10-30 11:24:56 -0500543 if ( nlsr->debugging )
544 printf("Scheduling Build and Install Adj LSA...\n");
545 if ( nlsr->detailed_logging )
akmhoque3d319d42013-02-20 11:08:32 -0600546 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Scheduling Build and Inst"
547 "all Adj LSA...\n");
548 nlsr->event_build_adj_lsa = ccn_schedule_event(nlsr->sched, 100000,
549 &build_and_install_adj_lsa, NULL, 0);
akmhoque53f64222012-09-05 13:57:51 -0500550 nlsr->is_build_adj_lsa_sheduled=1;
551 }
552 else
553 {
akmhoque7b791452012-10-30 11:24:56 -0500554 if ( nlsr->debugging )
555 printf("Build and Install Adj LSA already scheduled\n");
556 if ( nlsr->detailed_logging )
akmhoque3d319d42013-02-20 11:08:32 -0600557 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Build and Install Adj LSA"
558 " already scheduled\n");
akmhoque53f64222012-09-05 13:57:51 -0500559 }
560
akmhoque03004e62012-09-06 01:12:28 -0500561
akmhoque53f64222012-09-05 13:57:51 -0500562 free(nbr);
akmhoque03004e62012-09-06 01:12:28 -0500563
akmhoque53f64222012-09-05 13:57:51 -0500564
565}
566
akmhoque7ab49a32013-02-20 11:27:51 -0600567/**
568* process any incoming interest timed out content to NLSR from ccnd
569*/
akmhoque53f64222012-09-05 13:57:51 -0500570
akmhoque03004e62012-09-06 01:12:28 -0500571
akmhoque53f64222012-09-05 13:57:51 -0500572void
akmhoque3d319d42013-02-20 11:08:32 -0600573process_incoming_timed_out_interest(struct ccn_closure* selfp,
574 struct ccn_upcall_info* info)
akmhoque53f64222012-09-05 13:57:51 -0500575{
akmhoque3171d652012-11-13 11:44:33 -0600576
577
578 if ( nlsr->debugging )
579 printf("process_incoming_timed_out_interest called \n");
580 if ( nlsr->detailed_logging )
akmhoque3d319d42013-02-20 11:08:32 -0600581 writeLogg(__FILE__,__FUNCTION__,__LINE__,"process_incoming_timed_out_int"
582 "erest called \n");
akmhoque3171d652012-11-13 11:44:33 -0600583
akmhoque53f64222012-09-05 13:57:51 -0500584 int res,i;
585 int nlsr_position=0;
586 int name_comps=(int)info->interest_comps->n;
587
588 for(i=0;i<name_comps;i++)
589 {
590 res=ccn_name_comp_strcmp(info->interest_ccnb,info->interest_comps,i,"nlsr");
591 if( res == 0)
592 {
593 nlsr_position=i;
594 break;
595 }
596 }
597
akmhoque3d319d42013-02-20 11:08:32 -0600598 if(ccn_name_comp_strcmp(info->interest_ccnb,info->interest_comps,
599 nlsr_position+1,"info") == 0)
akmhoque53f64222012-09-05 13:57:51 -0500600 {
601 process_incoming_timed_out_interest_info(selfp,info);
602 }
603}
604
akmhoque7ab49a32013-02-20 11:27:51 -0600605/**
606* process any incoming "info" interest timed out content to NLSR from ccnd
607*/
608
akmhoque53f64222012-09-05 13:57:51 -0500609void
akmhoque3d319d42013-02-20 11:08:32 -0600610process_incoming_timed_out_interest_info(struct ccn_closure* selfp, struct
611 ccn_upcall_info* info)
akmhoque53f64222012-09-05 13:57:51 -0500612{
akmhoque3171d652012-11-13 11:44:33 -0600613
614 if ( nlsr->debugging )
615 printf("process_incoming_timed_out_interest_info called \n");
616 if ( nlsr->detailed_logging )
akmhoque3d319d42013-02-20 11:08:32 -0600617 writeLogg(__FILE__,__FUNCTION__,__LINE__,"process_incoming_timed_out_int"
618 "erest_info called \n");
akmhoque53f64222012-09-05 13:57:51 -0500619
620 struct name_prefix *nbr=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
621 get_nbr(nbr,selfp,info);
622
akmhoque3171d652012-11-13 11:44:33 -0600623 if ( nlsr->debugging )
akmhoque3d319d42013-02-20 11:08:32 -0600624 printf("Info Interest Timed Out for for Neighbor: %s Length:%d\n",
625 nbr->name,nbr->length);
akmhoque3171d652012-11-13 11:44:33 -0600626 if ( nlsr->detailed_logging )
akmhoque3d319d42013-02-20 11:08:32 -0600627 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Info Interest Timed Out for"
628 " Neighbor: %s Length:%d\n",nbr->name,nbr->length);
akmhoque3171d652012-11-13 11:44:33 -0600629
akmhoque03004e62012-09-06 01:12:28 -0500630
631
akmhoque53f64222012-09-05 13:57:51 -0500632 update_adjacent_timed_out_to_adl(nbr,1);
633 print_adjacent_from_adl();
634 int timed_out=get_timed_out_number(nbr);
akmhoque03004e62012-09-06 01:12:28 -0500635
akmhoque3171d652012-11-13 11:44:33 -0600636 if ( nlsr->debugging )
akmhoque3d319d42013-02-20 11:08:32 -0600637 printf("Neighbor: %s Info Interest Timed Out: %d times\n",nbr->name,
638 timed_out);
akmhoque3171d652012-11-13 11:44:33 -0600639 if ( nlsr->detailed_logging )
akmhoque3d319d42013-02-20 11:08:32 -0600640 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Neighbor: %s Info Interest "
641 "Timed Out: %d times\n",nbr->name,timed_out);
akmhoque3171d652012-11-13 11:44:33 -0600642
akmhoque53f64222012-09-05 13:57:51 -0500643
644 if(timed_out<nlsr->interest_retry && timed_out>0) // use configured variables
645 {
akmhoque53f64222012-09-05 13:57:51 -0500646 send_info_interest_to_neighbor(nbr);
647 }
648 else
akmhoque3171d652012-11-13 11:44:33 -0600649 {
akmhoque53f64222012-09-05 13:57:51 -0500650 update_adjacent_status_to_adl(nbr,NBR_DOWN);
651 if(!nlsr->is_build_adj_lsa_sheduled)
652 {
akmhoque3d319d42013-02-20 11:08:32 -0600653 nlsr->event_build_adj_lsa = ccn_schedule_event(nlsr->sched, 1000,
654 &build_and_install_adj_lsa, NULL, 0);
akmhoque53f64222012-09-05 13:57:51 -0500655 nlsr->is_build_adj_lsa_sheduled=1;
656 }
657 }
658
659 free(nbr);
akmhoque03004e62012-09-06 01:12:28 -0500660
akmhoque53f64222012-09-05 13:57:51 -0500661
662}
663
akmhoque7ab49a32013-02-20 11:27:51 -0600664/**
665* send "info" interest to each and every neighbor in ADL and also schedule for
666* itself for periodical sending of "info" interest
667*/
akmhoque29c1db52012-09-07 14:47:43 -0500668
akmhoque03004e62012-09-06 01:12:28 -0500669int
akmhoque3d319d42013-02-20 11:08:32 -0600670send_info_interest(struct ccn_schedule *sched, void *clienth,
671 struct ccn_scheduled_event *ev, int flags)
akmhoque53f64222012-09-05 13:57:51 -0500672{
akmhoqueffacaa82012-09-13 17:48:30 -0500673 if(flags == CCN_SCHEDULE_CANCEL)
674 {
675 return -1;
676 }
677
678 nlsr_lock();
679
akmhoque3171d652012-11-13 11:44:33 -0600680 if ( nlsr->debugging )
681 printf("send_info_interest called \n");
682 if ( nlsr->detailed_logging )
683 writeLogg(__FILE__,__FUNCTION__,__LINE__,"send_info_interest called \n");
684
685 if ( nlsr->debugging )
686 printf("\n");
687 if ( nlsr->detailed_logging )
688 writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n");
akmhoqued79438d2012-08-27 13:31:42 -0500689
akmhoque53f64222012-09-05 13:57:51 -0500690 int adl_element,i;
akmhoque53f64222012-09-05 13:57:51 -0500691 struct ndn_neighbor *nbr;
692
693 struct hashtb_enumerator ee;
694 struct hashtb_enumerator *e = &ee;
695
696 hashtb_start(nlsr->adl, e);
697 adl_element=hashtb_n(nlsr->adl);
698
699 for(i=0;i<adl_element;i++)
700 {
701 nbr=e->data;
702 send_info_interest_to_neighbor(nbr->neighbor);
703 hashtb_next(e);
704 }
akmhoque53f64222012-09-05 13:57:51 -0500705 hashtb_end(e);
706
akmhoqueffacaa82012-09-13 17:48:30 -0500707 nlsr_unlock();
708
akmhoque3d319d42013-02-20 11:08:32 -0600709 nlsr->event = ccn_schedule_event(nlsr->sched, 60000000, &send_info_interest,
710 NULL, 0);
akmhoque9fa58a82012-10-05 07:56:02 -0500711
akmhoque53f64222012-09-05 13:57:51 -0500712 return 0;
713}
714
akmhoque7ab49a32013-02-20 11:27:51 -0600715
716/**
717* send "info" interest neighbor nbr
718*
719*/
720
akmhoque53f64222012-09-05 13:57:51 -0500721void
722send_info_interest_to_neighbor(struct name_prefix *nbr)
723{
akmhoque3171d652012-11-13 11:44:33 -0600724
725 if ( nlsr->debugging )
726 printf("send_info_interest_to_neighbor called \n");
727 if ( nlsr->detailed_logging )
akmhoque3d319d42013-02-20 11:08:32 -0600728 writeLogg(__FILE__,__FUNCTION__,__LINE__,"send_info_interest_to_neighbor"
729 " called \n");
akmhoque3171d652012-11-13 11:44:33 -0600730
akmhoqued79438d2012-08-27 13:31:42 -0500731
732 int res;
akmhoque53f64222012-09-05 13:57:51 -0500733 char info_str[5];
734 char nlsr_str[5];
akmhoque53f64222012-09-05 13:57:51 -0500735
akmhoqued79438d2012-08-27 13:31:42 -0500736 memset(&nlsr_str,0,5);
737 sprintf(nlsr_str,"nlsr");
738 memset(&info_str,0,5);
739 sprintf(info_str,"info");
740
akmhoque53f64222012-09-05 13:57:51 -0500741
742 struct ccn_charbuf *name;
akmhoqued79438d2012-08-27 13:31:42 -0500743 name=ccn_charbuf_create();
akmhoqued79438d2012-08-27 13:31:42 -0500744
akmhoque3d319d42013-02-20 11:08:32 -0600745 char *int_name=(char *)malloc(strlen(nbr->name)+1+strlen(nlsr_str)+1+
746 strlen(info_str)+strlen(nlsr->router_name)+1);
747 memset(int_name,0,strlen(nbr->name)+1+strlen(nlsr_str)+1+strlen(info_str)+
748 strlen(nlsr->router_name)+1);
akmhoque53f64222012-09-05 13:57:51 -0500749 memcpy(int_name+strlen(int_name),nbr->name,strlen(nbr->name));
750 memcpy(int_name+strlen(int_name),"/",1);
751 memcpy(int_name+strlen(int_name),nlsr_str,strlen(nlsr_str));
752 memcpy(int_name+strlen(int_name),"/",1);
753 memcpy(int_name+strlen(int_name),info_str,strlen(info_str));
akmhoque03004e62012-09-06 01:12:28 -0500754 memcpy(int_name+strlen(int_name),nlsr->router_name,strlen(nlsr->router_name));
akmhoque53f64222012-09-05 13:57:51 -0500755
756
757 res=ccn_name_from_uri(name,int_name);
758 if ( res >=0 )
759 {
akmhoque53f64222012-09-05 13:57:51 -0500760 /* adding InterestLifeTime and InterestScope filter */
761
762 struct ccn_charbuf *templ;
763 templ = ccn_charbuf_create();
764
765 ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG);
766 ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG);
767 ccn_charbuf_append_closer(templ); /* </Name> */
akmhoque03004e62012-09-06 01:12:28 -0500768 ccn_charbuf_append_tt(templ, CCN_DTAG_Scope, CCN_DTAG);
769 ccn_charbuf_append_tt(templ, 1, CCN_UDATA);
akmhoqueb77b95f2013-02-08 12:28:47 -0600770 /* Adding InterestLifeTime and InterestScope filter done */
akmhoque3d319d42013-02-20 11:08:32 -0600771 ccn_charbuf_append(templ, "2", 1); //scope of interest: 2
772 //(not further than next host)
akmhoque03004e62012-09-06 01:12:28 -0500773 ccn_charbuf_append_closer(templ); /* </Scope> */
akmhoque53f64222012-09-05 13:57:51 -0500774
775 appendLifetime(templ,nlsr->interest_resend_time);
akmhoqueb77b95f2013-02-08 12:28:47 -0600776 unsigned int face_id=get_next_hop_face_from_adl(nbr->name);
777 ccnb_tagged_putf(templ, CCN_DTAG_FaceID, "%u", face_id);
akmhoque53f64222012-09-05 13:57:51 -0500778 ccn_charbuf_append_closer(templ); /* </Interest> */
akmhoqueb77b95f2013-02-08 12:28:47 -0600779
akmhoque53f64222012-09-05 13:57:51 -0500780
akmhoque3171d652012-11-13 11:44:33 -0600781 if ( nlsr->debugging )
akmhoque3d319d42013-02-20 11:08:32 -0600782 printf("Sending info interest on name prefix : %s through Face:%u\n"
783 ,int_name,face_id);
akmhoque3171d652012-11-13 11:44:33 -0600784 if ( nlsr->detailed_logging )
akmhoque3d319d42013-02-20 11:08:32 -0600785 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Sending info interest on"
786 "name prefix : %s through Face:%u\n",int_name,face_id);
akmhoque53f64222012-09-05 13:57:51 -0500787
788 res=ccn_express_interest(nlsr->ccn,name,&(nlsr->in_content),templ);
789
790 if ( res >= 0 )
akmhoque3171d652012-11-13 11:44:33 -0600791 {
792 if ( nlsr->debugging )
793 printf("Info interest sending Successfull .... \n");
794 if ( nlsr->detailed_logging )
akmhoque3d319d42013-02-20 11:08:32 -0600795 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Info"
796 "interest sending Successfull .... \n");
akmhoque3171d652012-11-13 11:44:33 -0600797 }
akmhoque53f64222012-09-05 13:57:51 -0500798 ccn_charbuf_destroy(&templ);
799 }
800 ccn_charbuf_destroy(&name);
801 free(int_name);
akmhoquec06dcf12013-02-20 08:13:37 -0600802}
akmhoque53f64222012-09-05 13:57:51 -0500803
akmhoque7ab49a32013-02-20 11:27:51 -0600804/**
805* Check whether content name contains a key name. Return 1 for containing
806*/
807
akmhoquec06dcf12013-02-20 08:13:37 -0600808int
809contain_key_name(const unsigned char *ccnb, struct ccn_parsed_ContentObject *pco)
810{
811 if (pco->offset[CCN_PCO_B_KeyLocator] == pco->offset[CCN_PCO_E_KeyLocator])
812 return -1;
813
814 struct ccn_buf_decoder decoder;
815 struct ccn_buf_decoder *d;
akmhoque3d319d42013-02-20 11:08:32 -0600816 d = ccn_buf_decoder_start(&decoder, ccnb +
817 pco->offset[CCN_PCO_B_Key_Certificate_KeyName],
818 pco->offset[CCN_PCO_E_Key_Certificate_KeyName] -
819 pco->offset[CCN_PCO_B_Key_Certificate_KeyName]);
akmhoquec06dcf12013-02-20 08:13:37 -0600820 if (ccn_buf_match_dtag(d, CCN_DTAG_KeyName))
821 return 1;
822
823 return -1;
824}
825
akmhoque7ab49a32013-02-20 11:27:51 -0600826/**
827* Extract Key Name from Content Name and return the Key Name
828*
829*/
830
akmhoquec06dcf12013-02-20 08:13:37 -0600831struct ccn_charbuf *
832get_key_name(const unsigned char *ccnb, struct ccn_parsed_ContentObject *pco)
833{
834 struct ccn_charbuf *key_name = ccn_charbuf_create();
akmhoque3d319d42013-02-20 11:08:32 -0600835 ccn_charbuf_append(key_name, ccnb + pco->offset[CCN_PCO_B_KeyName_Name],
836 pco->offset[CCN_PCO_E_KeyName_Name] - pco->offset[CCN_PCO_B_KeyName_Name]);
akmhoquec06dcf12013-02-20 08:13:37 -0600837
838 return key_name;
akmhoque53f64222012-09-05 13:57:51 -0500839}