blob: c605ad0c560e09637823c2ba18d6741b8f69d948 [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);
akmhoquececba942013-02-25 17:33:34 -0600109 free(neighbor);
akmhoque53f64222012-09-05 13:57:51 -0500110}
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
akmhoquececba942013-02-25 17:33:34 -0600153 char *neighbor=(char *)calloc(len,sizeof(char));
154 //memset(neighbor,0,len);
akmhoque53f64222012-09-05 13:57:51 -0500155
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
akmhoquececba942013-02-25 17:33:34 -0600176 free(neighbor);
akmhoque03004e62012-09-06 01:12:28 -0500177}
178
179
akmhoque3d319d42013-02-20 11:08:32 -0600180
181/**
182* Call back function registered in ccnd to get all interest coming to NLSR
183* application
184*/
akmhoque03004e62012-09-06 01:12:28 -0500185
akmhoque59980a52012-08-09 12:36:09 -0500186enum ccn_upcall_res
187incoming_interest(struct ccn_closure *selfp,
188 enum ccn_upcall_kind kind, struct ccn_upcall_info *info)
189{
akmhoqueffacaa82012-09-13 17:48:30 -0500190
191 nlsr_lock();
192
akmhoque59980a52012-08-09 12:36:09 -0500193 switch (kind) {
194 case CCN_UPCALL_FINAL:
195 break;
196 case CCN_UPCALL_INTEREST:
akmhoque03004e62012-09-06 01:12:28 -0500197 // printing the name prefix for which it received interest
akmhoque7b791452012-10-30 11:24:56 -0500198 if ( nlsr->debugging )
199 printf("Interest Received for name: ");
200 if ( nlsr->detailed_logging )
201 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Interest Received for name: ");
202
akmhoque03004e62012-09-06 01:12:28 -0500203 struct ccn_charbuf*c;
204 c=ccn_charbuf_create();
205 ccn_uri_append(c,info->interest_ccnb,info->pi->offset[CCN_PI_E_Name],0);
akmhoque7b791452012-10-30 11:24:56 -0500206
207 if ( nlsr->debugging )
208 printf("%s\n",ccn_charbuf_as_string(c));
209 if ( nlsr->detailed_logging )
210 writeLogg(__FILE__,__FUNCTION__,__LINE__,"%s\n",ccn_charbuf_as_string(c));
211
akmhoque03004e62012-09-06 01:12:28 -0500212 ccn_charbuf_destroy(&c);
213
akmhoque1c9b92f2012-08-13 10:57:50 -0500214 process_incoming_interest(selfp, info);
akmhoque03004e62012-09-06 01:12:28 -0500215
akmhoque59980a52012-08-09 12:36:09 -0500216 break;
akmhoque03004e62012-09-06 01:12:28 -0500217
akmhoque59980a52012-08-09 12:36:09 -0500218 default:
219 break;
220 }
akmhoque03004e62012-09-06 01:12:28 -0500221
akmhoqueffacaa82012-09-13 17:48:30 -0500222 nlsr_unlock();
223
akmhoque59980a52012-08-09 12:36:09 -0500224 return CCN_UPCALL_RESULT_OK;
225}
226
akmhoque3d319d42013-02-20 11:08:32 -0600227/**
228* Function for processing incoming interest and reply with content/NACK content
229*/
akmhoque59980a52012-08-09 12:36:09 -0500230
akmhoqued79438d2012-08-27 13:31:42 -0500231void
akmhoque1c9b92f2012-08-13 10:57:50 -0500232process_incoming_interest(struct ccn_closure *selfp, struct ccn_upcall_info *info)
233{
akmhoque7b791452012-10-30 11:24:56 -0500234 if ( nlsr->debugging )
235 printf("process_incoming_interest called \n");
236 if ( nlsr->detailed_logging )
237 writeLogg(__FILE__,__FUNCTION__,__LINE__,"process_incoming_interest called \n");
238
akmhoque1c9b92f2012-08-13 10:57:50 -0500239 const unsigned char *comp_ptr1;
240 size_t comp_size;
241 int res,i;
242 int nlsr_position=0;
243 int name_comps=(int)info->interest_comps->n;
akmhoque53f64222012-09-05 13:57:51 -0500244
akmhoque1c9b92f2012-08-13 10:57:50 -0500245 for(i=0;i<name_comps;i++)
246 {
247 res=ccn_name_comp_strcmp(info->interest_ccnb,info->interest_comps,i,"nlsr");
248 if( res == 0)
249 {
akmhoqueea3603e2012-08-13 11:24:09 -0500250 nlsr_position=i;
akmhoque1c9b92f2012-08-13 10:57:50 -0500251 break;
252 }
253 }
254
akmhoque3d319d42013-02-20 11:08:32 -0600255 res=ccn_name_comp_get(info->interest_ccnb, info->interest_comps,nlsr_position+1,
256 &comp_ptr1, &comp_size);
akmhoque53f64222012-09-05 13:57:51 -0500257
akmhoque1c9b92f2012-08-13 10:57:50 -0500258
akmhoqued79438d2012-08-27 13:31:42 -0500259 if(!strcmp((char *)comp_ptr1,"info"))
260 {
261 process_incoming_interest_info(selfp,info);
262 }
akmhoqueb77b95f2013-02-08 12:28:47 -0600263
akmhoque53f64222012-09-05 13:57:51 -0500264}
265
akmhoque3d319d42013-02-20 11:08:32 -0600266/**
akmhoque7ab49a32013-02-20 11:27:51 -0600267* Processes incoming interest for "info" interest. Send back reply content back,
268* if interest comes from a neighbor with status down, NLSR will send "info"
269* ineterst to that neighbor
akmhoque3d319d42013-02-20 11:08:32 -0600270*/
271
akmhoque53f64222012-09-05 13:57:51 -0500272void
273process_incoming_interest_info(struct ccn_closure *selfp, struct ccn_upcall_info *info)
274{
akmhoque7b791452012-10-30 11:24:56 -0500275 if ( nlsr->debugging )
276 {
277 printf("process_incoming_interest_info called \n");
278 printf("Sending Info Content back.....\n");
279 }
280 if ( nlsr->detailed_logging )
281 {
akmhoque3d319d42013-02-20 11:08:32 -0600282 writeLogg(__FILE__,__FUNCTION__,__LINE__,"process_incoming_interest_info"
283 " called \n");
akmhoque7b791452012-10-30 11:24:56 -0500284 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Sending Info Content back.....\n");
285 }
286
akmhoque53f64222012-09-05 13:57:51 -0500287
akmhoque53f64222012-09-05 13:57:51 -0500288 int res;
289 struct ccn_charbuf *data=ccn_charbuf_create();
290 struct ccn_charbuf *name=ccn_charbuf_create();
akmhoquedd7a7a72013-02-20 08:25:41 -0600291
akmhoque03004e62012-09-06 01:12:28 -0500292
akmhoque3d319d42013-02-20 11:08:32 -0600293 res=ccn_charbuf_append(name, info->interest_ccnb + info->pi->offset[CCN_PI_B_Name],
294 info->pi->offset[CCN_PI_E_Name] - info->pi->offset[CCN_PI_B_Name]);
akmhoque53f64222012-09-05 13:57:51 -0500295 if (res >= 0)
296 {
akmhoquedd7a7a72013-02-20 08:25:41 -0600297
akmhoquec06dcf12013-02-20 08:13:37 -0600298
299 struct ccn_charbuf *pubid = ccn_charbuf_create();
300 struct ccn_charbuf *pubkey = ccn_charbuf_create();
301
akmhoque7ca519b2013-02-20 09:49:43 -0600302 //pubid is the digest_result pubkey is result
303 ccn_get_public_key(nlsr->ccn, NULL, pubid, pubkey);
304
305
akmhoquedd7a7a72013-02-20 08:25:41 -0600306
307 struct ccn_signing_params sp=CCN_SIGNING_PARAMS_INIT;
308 sp.template_ccnb=ccn_charbuf_create();
akmhoque3d319d42013-02-20 11:08:32 -0600309
akmhoquec06dcf12013-02-20 08:13:37 -0600310 ccn_charbuf_append_tt(sp.template_ccnb, CCN_DTAG_SignedInfo, CCN_DTAG);
311 ccn_charbuf_append_tt(sp.template_ccnb, CCN_DTAG_KeyLocator, CCN_DTAG);
312 ccn_charbuf_append_tt(sp.template_ccnb, CCN_DTAG_KeyName, CCN_DTAG);
akmhoquedd7a7a72013-02-20 08:25:41 -0600313 ccn_charbuf_append_charbuf(sp.template_ccnb, name);
akmhoque53f64222012-09-05 13:57:51 -0500314 ccn_charbuf_append_closer(sp.template_ccnb);
akmhoquec06dcf12013-02-20 08:13:37 -0600315 ccn_charbuf_append_closer(sp.template_ccnb);
akmhoquec06dcf12013-02-20 08:13:37 -0600316 ccn_charbuf_append_closer(sp.template_ccnb);
akmhoquedd7a7a72013-02-20 08:25:41 -0600317
akmhoquec06dcf12013-02-20 08:13:37 -0600318 sp.sp_flags |= CCN_SP_TEMPL_KEY_LOCATOR;
319 sp.sp_flags |= CCN_SP_FINAL_BLOCK;
320 sp.type = CCN_CONTENT_KEY;
akmhoque63571b02013-02-20 08:18:49 -0600321 sp.freshness = 10;
akmhoquec06dcf12013-02-20 08:13:37 -0600322
akmhoque3d319d42013-02-20 11:08:32 -0600323
324 /*ccn_charbuf_append_tt(sp.template_ccnb,CCN_DTAG_SignedInfo, CCN_DTAG);
325 ccnb_tagged_putf(sp.template_ccnb, CCN_DTAG_FreshnessSeconds, "%ld", 10);
326 sp.sp_flags |= CCN_SP_TEMPL_FRESHNESS;
327 ccn_charbuf_append_closer(sp.template_ccnb);
328 */
akmhoque53f64222012-09-05 13:57:51 -0500329
akmhoqueb77b95f2013-02-08 12:28:47 -0600330 char *raw_data=(char *)malloc(20);
331 memset(raw_data,0,20);
332 sprintf(raw_data,"%s", nlsr->lsdb->lsdb_version);
akmhoque53f64222012-09-05 13:57:51 -0500333
akmhoquec06dcf12013-02-20 08:13:37 -0600334 res= ccn_sign_content(nlsr->ccn, data, name, &sp, pubkey->buf,pubkey->length);
akmhoque53f64222012-09-05 13:57:51 -0500335 if(res >= 0)
akmhoque7b791452012-10-30 11:24:56 -0500336 {
337 if ( nlsr->debugging )
338 printf("Signing info Content is successful \n");
339 if ( nlsr->detailed_logging )
akmhoque3d319d42013-02-20 11:08:32 -0600340 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Signing info Content"
341 " is successful \n");
akmhoque53f64222012-09-05 13:57:51 -0500342
akmhoque7b791452012-10-30 11:24:56 -0500343 }
akmhoque53f64222012-09-05 13:57:51 -0500344 res=ccn_put(nlsr->ccn,data->buf,data->length);
345 if(res >= 0)
akmhoque7b791452012-10-30 11:24:56 -0500346 {
347 if ( nlsr->debugging )
348 printf("Sending Info Content is successful \n");
349 if ( nlsr->detailed_logging )
akmhoque3d319d42013-02-20 11:08:32 -0600350 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Sending info Content"
351 " is successful \n");
akmhoque7b791452012-10-30 11:24:56 -0500352 }
353
akmhoque53f64222012-09-05 13:57:51 -0500354
355
akmhoquee1dd7772013-02-24 14:21:49 -0600356 struct name_prefix *nbr=(struct name_prefix * )malloc(sizeof(struct name_prefix));
akmhoque03004e62012-09-06 01:12:28 -0500357 get_lsa_identifier(nbr,selfp,info,-1);
akmhoque7b791452012-10-30 11:24:56 -0500358
359 if ( nlsr->debugging )
akmhoque3d319d42013-02-20 11:08:32 -0600360 printf("Neighbor : %s Length : %d Status : %d\n",nbr->name,nbr->length,
361 get_adjacent_status(nbr));
akmhoque7b791452012-10-30 11:24:56 -0500362 if ( nlsr->detailed_logging )
akmhoque3d319d42013-02-20 11:08:32 -0600363 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Neighbor : %s Length : %d"
364 " Status : %d\n",nbr->name,nbr->length,get_adjacent_status(nbr));
akmhoque03004e62012-09-06 01:12:28 -0500365
akmhoque53f64222012-09-05 13:57:51 -0500366
akmhoque3d319d42013-02-20 11:08:32 -0600367 if( get_adjacent_status(nbr) == 0 && get_timed_out_number(nbr) >=
368 nlsr->interest_retry )
akmhoque53f64222012-09-05 13:57:51 -0500369 {
akmhoqueb8195202012-09-25 11:53:23 -0500370 update_adjacent_timed_out_zero_to_adl(nbr);
akmhoque53f64222012-09-05 13:57:51 -0500371 send_info_interest_to_neighbor(nbr);
372 }
373
374 free(nbr);
375 free(raw_data);
376 ccn_charbuf_destroy(&sp.template_ccnb);
akmhoquec06dcf12013-02-20 08:13:37 -0600377 ccn_charbuf_destroy(&pubid);
378 ccn_charbuf_destroy(&pubkey);
akmhoque53f64222012-09-05 13:57:51 -0500379 }
akmhoque03004e62012-09-06 01:12:28 -0500380
akmhoque53f64222012-09-05 13:57:51 -0500381 ccn_charbuf_destroy(&data);
akmhoque03004e62012-09-06 01:12:28 -0500382 ccn_charbuf_destroy(&name);
akmhoque53f64222012-09-05 13:57:51 -0500383
akmhoquebf1aa832012-08-13 13:26:59 -0500384}
385
386
akmhoque7ab49a32013-02-20 11:27:51 -0600387/**
388* Call back function registered in ccnd to get all content coming to NLSR
389* application
390*/
akmhoque03004e62012-09-06 01:12:28 -0500391
akmhoque53f64222012-09-05 13:57:51 -0500392enum ccn_upcall_res incoming_content(struct ccn_closure* selfp,
393 enum ccn_upcall_kind kind, struct ccn_upcall_info* info)
akmhoqued79438d2012-08-27 13:31:42 -0500394{
395
akmhoqueffacaa82012-09-13 17:48:30 -0500396 nlsr_lock();
akmhoque03004e62012-09-06 01:12:28 -0500397
akmhoque53f64222012-09-05 13:57:51 -0500398 switch(kind) {
399 case CCN_UPCALL_FINAL:
400 break;
401 case CCN_UPCALL_CONTENT:
akmhoque7b791452012-10-30 11:24:56 -0500402 if ( nlsr->debugging )
403 printf("Content Received for Name: ");
404 if ( nlsr->detailed_logging )
405 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Content Received for Name: ");
406
akmhoque03004e62012-09-06 01:12:28 -0500407 struct ccn_charbuf*c;
408 c=ccn_charbuf_create();
409 ccn_uri_append(c,info->interest_ccnb,info->pi->offset[CCN_PI_E],0);
akmhoque7b791452012-10-30 11:24:56 -0500410 if ( nlsr->debugging )
411 printf("%s\n",ccn_charbuf_as_string(c));
412 if ( nlsr->detailed_logging )
413 writeLogg(__FILE__,__FUNCTION__,__LINE__,"%s\n",ccn_charbuf_as_string(c));
414
akmhoque03004e62012-09-06 01:12:28 -0500415 ccn_charbuf_destroy(&c);
416
417 process_incoming_content(selfp,info);
akmhoqued79438d2012-08-27 13:31:42 -0500418
akmhoque53f64222012-09-05 13:57:51 -0500419 break;
420 case CCN_UPCALL_INTEREST_TIMED_OUT:
akmhoque7b791452012-10-30 11:24:56 -0500421 //printf("Interest Timed Out Received for Name: ");
422 if ( nlsr->debugging )
423 printf("Interest Timed Out Received for Name: ");
424 if ( nlsr->detailed_logging )
akmhoque3d319d42013-02-20 11:08:32 -0600425 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Interest Timed Out Receiv"
426 "ed for Name: ");
akmhoque03004e62012-09-06 01:12:28 -0500427
428 struct ccn_charbuf*ito;
429 ito=ccn_charbuf_create();
430 ccn_uri_append(ito,info->interest_ccnb,info->pi->offset[CCN_PI_E],0);
akmhoque7b791452012-10-30 11:24:56 -0500431 if ( nlsr->debugging )
432 printf("%s\n",ccn_charbuf_as_string(ito));
433 if ( nlsr->detailed_logging )
434 writeLogg(__FILE__,__FUNCTION__,__LINE__,"%s\n",ccn_charbuf_as_string(ito));
akmhoque03004e62012-09-06 01:12:28 -0500435 ccn_charbuf_destroy(&ito);
436
akmhoquec06dcf12013-02-20 08:13:37 -0600437
438
akmhoque53f64222012-09-05 13:57:51 -0500439 process_incoming_timed_out_interest(selfp,info);
akmhoqued79438d2012-08-27 13:31:42 -0500440
akmhoque53f64222012-09-05 13:57:51 -0500441 break;
442 default:
443 fprintf(stderr, "Unexpected response of kind %d\n", kind);
akmhoque7b791452012-10-30 11:24:56 -0500444 if ( nlsr->debugging )
akmhoque3d319d42013-02-20 11:08:32 -0600445 printf("Unexpected response of kind %d\n", kind);
akmhoque7b791452012-10-30 11:24:56 -0500446 if ( nlsr->detailed_logging )
akmhoque3d319d42013-02-20 11:08:32 -0600447 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Unexpected response of "
448 "kind %d\n", kind);
akmhoqueffacaa82012-09-13 17:48:30 -0500449 break;
akmhoque53f64222012-09-05 13:57:51 -0500450 }
akmhoqueffacaa82012-09-13 17:48:30 -0500451
452 nlsr_unlock();
akmhoqued79438d2012-08-27 13:31:42 -0500453
akmhoque53f64222012-09-05 13:57:51 -0500454 return CCN_UPCALL_RESULT_OK;
akmhoqued79438d2012-08-27 13:31:42 -0500455}
456
akmhoque7ab49a32013-02-20 11:27:51 -0600457/**
458* process any incoming content to NLSR from ccnd
459*/
akmhoque03004e62012-09-06 01:12:28 -0500460
akmhoqued79438d2012-08-27 13:31:42 -0500461void
akmhoque53f64222012-09-05 13:57:51 -0500462process_incoming_content(struct ccn_closure *selfp, struct ccn_upcall_info* info)
akmhoqued79438d2012-08-27 13:31:42 -0500463{
akmhoque7b791452012-10-30 11:24:56 -0500464 if ( nlsr->debugging )
465 printf("process_incoming_content called \n");
466 if ( nlsr->detailed_logging )
467 writeLogg(__FILE__,__FUNCTION__,__LINE__,"process_incoming_content called \n");
akmhoque53f64222012-09-05 13:57:51 -0500468
469 const unsigned char *comp_ptr1;
470 size_t comp_size;
471 int res,i;
472 int nlsr_position=0;
473 int name_comps=(int)info->interest_comps->n;
474
475 for(i=0;i<name_comps;i++)
476 {
477 res=ccn_name_comp_strcmp(info->interest_ccnb,info->interest_comps,i,"nlsr");
478 if( res == 0)
479 {
480 nlsr_position=i;
481 break;
482 }
483 }
484
akmhoque3d319d42013-02-20 11:08:32 -0600485 res=ccn_name_comp_get(info->interest_ccnb, info->interest_comps,
486 nlsr_position+1,&comp_ptr1, &comp_size);
akmhoque53f64222012-09-05 13:57:51 -0500487
akmhoque53f64222012-09-05 13:57:51 -0500488
489 if(!strcmp((char *)comp_ptr1,"info"))
490 {
491 process_incoming_content_info(selfp,info);
492 }
akmhoque03004e62012-09-06 01:12:28 -0500493
akmhoque53f64222012-09-05 13:57:51 -0500494}
495
akmhoque7ab49a32013-02-20 11:27:51 -0600496/**
497* process any incoming "info" content to NLSR from ccnd
498*/
akmhoque03004e62012-09-06 01:12:28 -0500499
akmhoque53f64222012-09-05 13:57:51 -0500500void
akmhoque3d319d42013-02-20 11:08:32 -0600501process_incoming_content_info(struct ccn_closure *selfp,
502 struct ccn_upcall_info* info)
akmhoque53f64222012-09-05 13:57:51 -0500503{
akmhoque7b791452012-10-30 11:24:56 -0500504 if ( nlsr->debugging )
505 printf("process_incoming_content_info called \n");
506 if ( nlsr->detailed_logging )
akmhoque3d319d42013-02-20 11:08:32 -0600507 writeLogg(__FILE__,__FUNCTION__,__LINE__,"process_incoming_content_info"
508 " called \n");
akmhoque03004e62012-09-06 01:12:28 -0500509
akmhoque53f64222012-09-05 13:57:51 -0500510 struct name_prefix *nbr=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
511 get_nbr(nbr,selfp,info);
akmhoque03004e62012-09-06 01:12:28 -0500512
akmhoque7b791452012-10-30 11:24:56 -0500513 if ( nlsr->debugging )
akmhoque3d319d42013-02-20 11:08:32 -0600514 printf("Info Content Received For Neighbor: %s Length:%d\n",nbr->name,
515 nbr->length);
akmhoque7b791452012-10-30 11:24:56 -0500516 if ( nlsr->detailed_logging )
akmhoque3d319d42013-02-20 11:08:32 -0600517 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Info Content Received For Nei"
518 "ghbor: %s Length:%d\n",nbr->name,nbr->length);
akmhoque03004e62012-09-06 01:12:28 -0500519
akmhoque53f64222012-09-05 13:57:51 -0500520
akmhoquec06dcf12013-02-20 08:13:37 -0600521 if ( contain_key_name(info->content_ccnb, info->pco) == 1)
522 {
523 struct ccn_charbuf *key_name=get_key_name(info->content_ccnb, info->pco);
akmhoque7ca519b2013-02-20 09:49:43 -0600524 struct ccn_charbuf *key_uri = ccn_charbuf_create();
525 ccn_uri_append(key_uri, key_name->buf, key_name->length, 1);
526
akmhoquec06dcf12013-02-20 08:13:37 -0600527 if(nlsr->debugging)
akmhoque7ca519b2013-02-20 09:49:43 -0600528 printf("Key Name: %s\n",ccn_charbuf_as_string(key_uri));
529
530 ccn_charbuf_destroy(&key_uri);
531 ccn_charbuf_destroy(&key_name);
532
533
akmhoquec06dcf12013-02-20 08:13:37 -0600534 }
akmhoque03004e62012-09-06 01:12:28 -0500535
akmhoque53f64222012-09-05 13:57:51 -0500536 update_adjacent_timed_out_zero_to_adl(nbr);
537 update_adjacent_status_to_adl(nbr,NBR_ACTIVE);
akmhoque53f64222012-09-05 13:57:51 -0500538 print_adjacent_from_adl();
539
akmhoque03004e62012-09-06 01:12:28 -0500540
541
akmhoque53f64222012-09-05 13:57:51 -0500542 if(!nlsr->is_build_adj_lsa_sheduled)
543 {
akmhoque7b791452012-10-30 11:24:56 -0500544 if ( nlsr->debugging )
545 printf("Scheduling Build and Install Adj LSA...\n");
546 if ( nlsr->detailed_logging )
akmhoque3d319d42013-02-20 11:08:32 -0600547 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Scheduling Build and Inst"
548 "all Adj LSA...\n");
549 nlsr->event_build_adj_lsa = ccn_schedule_event(nlsr->sched, 100000,
550 &build_and_install_adj_lsa, NULL, 0);
akmhoque53f64222012-09-05 13:57:51 -0500551 nlsr->is_build_adj_lsa_sheduled=1;
552 }
553 else
554 {
akmhoque7b791452012-10-30 11:24:56 -0500555 if ( nlsr->debugging )
556 printf("Build and Install Adj LSA already scheduled\n");
557 if ( nlsr->detailed_logging )
akmhoque3d319d42013-02-20 11:08:32 -0600558 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Build and Install Adj LSA"
559 " already scheduled\n");
akmhoque53f64222012-09-05 13:57:51 -0500560 }
561
akmhoque03004e62012-09-06 01:12:28 -0500562
akmhoque53f64222012-09-05 13:57:51 -0500563 free(nbr);
akmhoque03004e62012-09-06 01:12:28 -0500564
akmhoque53f64222012-09-05 13:57:51 -0500565
566}
567
akmhoque7ab49a32013-02-20 11:27:51 -0600568/**
569* process any incoming interest timed out content to NLSR from ccnd
570*/
akmhoque53f64222012-09-05 13:57:51 -0500571
akmhoque03004e62012-09-06 01:12:28 -0500572
akmhoque53f64222012-09-05 13:57:51 -0500573void
akmhoque3d319d42013-02-20 11:08:32 -0600574process_incoming_timed_out_interest(struct ccn_closure* selfp,
575 struct ccn_upcall_info* info)
akmhoque53f64222012-09-05 13:57:51 -0500576{
akmhoque3171d652012-11-13 11:44:33 -0600577
578
579 if ( nlsr->debugging )
580 printf("process_incoming_timed_out_interest called \n");
581 if ( nlsr->detailed_logging )
akmhoque3d319d42013-02-20 11:08:32 -0600582 writeLogg(__FILE__,__FUNCTION__,__LINE__,"process_incoming_timed_out_int"
583 "erest called \n");
akmhoque3171d652012-11-13 11:44:33 -0600584
akmhoque53f64222012-09-05 13:57:51 -0500585 int res,i;
586 int nlsr_position=0;
587 int name_comps=(int)info->interest_comps->n;
588
589 for(i=0;i<name_comps;i++)
590 {
591 res=ccn_name_comp_strcmp(info->interest_ccnb,info->interest_comps,i,"nlsr");
592 if( res == 0)
593 {
594 nlsr_position=i;
595 break;
596 }
597 }
598
akmhoque3d319d42013-02-20 11:08:32 -0600599 if(ccn_name_comp_strcmp(info->interest_ccnb,info->interest_comps,
600 nlsr_position+1,"info") == 0)
akmhoque53f64222012-09-05 13:57:51 -0500601 {
602 process_incoming_timed_out_interest_info(selfp,info);
603 }
604}
605
akmhoque7ab49a32013-02-20 11:27:51 -0600606/**
607* process any incoming "info" interest timed out content to NLSR from ccnd
608*/
609
akmhoque53f64222012-09-05 13:57:51 -0500610void
akmhoque3d319d42013-02-20 11:08:32 -0600611process_incoming_timed_out_interest_info(struct ccn_closure* selfp, struct
612 ccn_upcall_info* info)
akmhoque53f64222012-09-05 13:57:51 -0500613{
akmhoque3171d652012-11-13 11:44:33 -0600614
615 if ( nlsr->debugging )
616 printf("process_incoming_timed_out_interest_info called \n");
617 if ( nlsr->detailed_logging )
akmhoque3d319d42013-02-20 11:08:32 -0600618 writeLogg(__FILE__,__FUNCTION__,__LINE__,"process_incoming_timed_out_int"
619 "erest_info called \n");
akmhoque53f64222012-09-05 13:57:51 -0500620
621 struct name_prefix *nbr=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
622 get_nbr(nbr,selfp,info);
623
akmhoque3171d652012-11-13 11:44:33 -0600624 if ( nlsr->debugging )
akmhoque3d319d42013-02-20 11:08:32 -0600625 printf("Info Interest Timed Out for for Neighbor: %s Length:%d\n",
626 nbr->name,nbr->length);
akmhoque3171d652012-11-13 11:44:33 -0600627 if ( nlsr->detailed_logging )
akmhoque3d319d42013-02-20 11:08:32 -0600628 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Info Interest Timed Out for"
629 " Neighbor: %s Length:%d\n",nbr->name,nbr->length);
akmhoque3171d652012-11-13 11:44:33 -0600630
akmhoque03004e62012-09-06 01:12:28 -0500631
632
akmhoque53f64222012-09-05 13:57:51 -0500633 update_adjacent_timed_out_to_adl(nbr,1);
634 print_adjacent_from_adl();
635 int timed_out=get_timed_out_number(nbr);
akmhoque03004e62012-09-06 01:12:28 -0500636
akmhoque3171d652012-11-13 11:44:33 -0600637 if ( nlsr->debugging )
akmhoque3d319d42013-02-20 11:08:32 -0600638 printf("Neighbor: %s Info Interest Timed Out: %d times\n",nbr->name,
639 timed_out);
akmhoque3171d652012-11-13 11:44:33 -0600640 if ( nlsr->detailed_logging )
akmhoque3d319d42013-02-20 11:08:32 -0600641 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Neighbor: %s Info Interest "
642 "Timed Out: %d times\n",nbr->name,timed_out);
akmhoque3171d652012-11-13 11:44:33 -0600643
akmhoque53f64222012-09-05 13:57:51 -0500644
645 if(timed_out<nlsr->interest_retry && timed_out>0) // use configured variables
646 {
akmhoque53f64222012-09-05 13:57:51 -0500647 send_info_interest_to_neighbor(nbr);
648 }
649 else
akmhoque3171d652012-11-13 11:44:33 -0600650 {
akmhoque53f64222012-09-05 13:57:51 -0500651 update_adjacent_status_to_adl(nbr,NBR_DOWN);
652 if(!nlsr->is_build_adj_lsa_sheduled)
653 {
akmhoque3d319d42013-02-20 11:08:32 -0600654 nlsr->event_build_adj_lsa = ccn_schedule_event(nlsr->sched, 1000,
655 &build_and_install_adj_lsa, NULL, 0);
akmhoque53f64222012-09-05 13:57:51 -0500656 nlsr->is_build_adj_lsa_sheduled=1;
657 }
658 }
659
660 free(nbr);
akmhoque03004e62012-09-06 01:12:28 -0500661
akmhoque53f64222012-09-05 13:57:51 -0500662
663}
664
akmhoque7ab49a32013-02-20 11:27:51 -0600665/**
666* send "info" interest to each and every neighbor in ADL and also schedule for
667* itself for periodical sending of "info" interest
668*/
akmhoque29c1db52012-09-07 14:47:43 -0500669
akmhoque03004e62012-09-06 01:12:28 -0500670int
akmhoque3d319d42013-02-20 11:08:32 -0600671send_info_interest(struct ccn_schedule *sched, void *clienth,
672 struct ccn_scheduled_event *ev, int flags)
akmhoque53f64222012-09-05 13:57:51 -0500673{
akmhoqueffacaa82012-09-13 17:48:30 -0500674 if(flags == CCN_SCHEDULE_CANCEL)
675 {
676 return -1;
677 }
678
679 nlsr_lock();
680
akmhoque3171d652012-11-13 11:44:33 -0600681 if ( nlsr->debugging )
682 printf("send_info_interest called \n");
683 if ( nlsr->detailed_logging )
684 writeLogg(__FILE__,__FUNCTION__,__LINE__,"send_info_interest called \n");
685
686 if ( nlsr->debugging )
687 printf("\n");
688 if ( nlsr->detailed_logging )
689 writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n");
akmhoqued79438d2012-08-27 13:31:42 -0500690
akmhoque53f64222012-09-05 13:57:51 -0500691 int adl_element,i;
akmhoque53f64222012-09-05 13:57:51 -0500692 struct ndn_neighbor *nbr;
693
694 struct hashtb_enumerator ee;
695 struct hashtb_enumerator *e = &ee;
696
697 hashtb_start(nlsr->adl, e);
698 adl_element=hashtb_n(nlsr->adl);
699
700 for(i=0;i<adl_element;i++)
701 {
702 nbr=e->data;
703 send_info_interest_to_neighbor(nbr->neighbor);
704 hashtb_next(e);
705 }
akmhoque53f64222012-09-05 13:57:51 -0500706 hashtb_end(e);
707
akmhoqueffacaa82012-09-13 17:48:30 -0500708 nlsr_unlock();
709
akmhoque3d319d42013-02-20 11:08:32 -0600710 nlsr->event = ccn_schedule_event(nlsr->sched, 60000000, &send_info_interest,
711 NULL, 0);
akmhoque9fa58a82012-10-05 07:56:02 -0500712
akmhoque53f64222012-09-05 13:57:51 -0500713 return 0;
714}
715
akmhoque7ab49a32013-02-20 11:27:51 -0600716
717/**
718* send "info" interest neighbor nbr
719*
720*/
721
akmhoque53f64222012-09-05 13:57:51 -0500722void
723send_info_interest_to_neighbor(struct name_prefix *nbr)
724{
akmhoque3171d652012-11-13 11:44:33 -0600725
726 if ( nlsr->debugging )
727 printf("send_info_interest_to_neighbor called \n");
728 if ( nlsr->detailed_logging )
akmhoque3d319d42013-02-20 11:08:32 -0600729 writeLogg(__FILE__,__FUNCTION__,__LINE__,"send_info_interest_to_neighbor"
730 " called \n");
akmhoque3171d652012-11-13 11:44:33 -0600731
akmhoqued79438d2012-08-27 13:31:42 -0500732
733 int res;
akmhoque53f64222012-09-05 13:57:51 -0500734 char info_str[5];
735 char nlsr_str[5];
akmhoque53f64222012-09-05 13:57:51 -0500736
akmhoqued79438d2012-08-27 13:31:42 -0500737 memset(&nlsr_str,0,5);
738 sprintf(nlsr_str,"nlsr");
739 memset(&info_str,0,5);
740 sprintf(info_str,"info");
741
akmhoque53f64222012-09-05 13:57:51 -0500742
743 struct ccn_charbuf *name;
akmhoqued79438d2012-08-27 13:31:42 -0500744 name=ccn_charbuf_create();
akmhoqued79438d2012-08-27 13:31:42 -0500745
akmhoque3d319d42013-02-20 11:08:32 -0600746 char *int_name=(char *)malloc(strlen(nbr->name)+1+strlen(nlsr_str)+1+
747 strlen(info_str)+strlen(nlsr->router_name)+1);
748 memset(int_name,0,strlen(nbr->name)+1+strlen(nlsr_str)+1+strlen(info_str)+
749 strlen(nlsr->router_name)+1);
akmhoque53f64222012-09-05 13:57:51 -0500750 memcpy(int_name+strlen(int_name),nbr->name,strlen(nbr->name));
751 memcpy(int_name+strlen(int_name),"/",1);
752 memcpy(int_name+strlen(int_name),nlsr_str,strlen(nlsr_str));
753 memcpy(int_name+strlen(int_name),"/",1);
754 memcpy(int_name+strlen(int_name),info_str,strlen(info_str));
akmhoque03004e62012-09-06 01:12:28 -0500755 memcpy(int_name+strlen(int_name),nlsr->router_name,strlen(nlsr->router_name));
akmhoque53f64222012-09-05 13:57:51 -0500756
757
758 res=ccn_name_from_uri(name,int_name);
759 if ( res >=0 )
760 {
akmhoque53f64222012-09-05 13:57:51 -0500761 /* adding InterestLifeTime and InterestScope filter */
762
763 struct ccn_charbuf *templ;
764 templ = ccn_charbuf_create();
765
766 ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG);
767 ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG);
768 ccn_charbuf_append_closer(templ); /* </Name> */
akmhoque03004e62012-09-06 01:12:28 -0500769 ccn_charbuf_append_tt(templ, CCN_DTAG_Scope, CCN_DTAG);
770 ccn_charbuf_append_tt(templ, 1, CCN_UDATA);
akmhoqueb77b95f2013-02-08 12:28:47 -0600771 /* Adding InterestLifeTime and InterestScope filter done */
akmhoque3d319d42013-02-20 11:08:32 -0600772 ccn_charbuf_append(templ, "2", 1); //scope of interest: 2
773 //(not further than next host)
akmhoque03004e62012-09-06 01:12:28 -0500774 ccn_charbuf_append_closer(templ); /* </Scope> */
akmhoque53f64222012-09-05 13:57:51 -0500775
776 appendLifetime(templ,nlsr->interest_resend_time);
akmhoqueb77b95f2013-02-08 12:28:47 -0600777 unsigned int face_id=get_next_hop_face_from_adl(nbr->name);
778 ccnb_tagged_putf(templ, CCN_DTAG_FaceID, "%u", face_id);
akmhoque53f64222012-09-05 13:57:51 -0500779 ccn_charbuf_append_closer(templ); /* </Interest> */
akmhoqueb77b95f2013-02-08 12:28:47 -0600780
akmhoque53f64222012-09-05 13:57:51 -0500781
akmhoque3171d652012-11-13 11:44:33 -0600782 if ( nlsr->debugging )
akmhoque3d319d42013-02-20 11:08:32 -0600783 printf("Sending info interest on name prefix : %s through Face:%u\n"
784 ,int_name,face_id);
akmhoque3171d652012-11-13 11:44:33 -0600785 if ( nlsr->detailed_logging )
akmhoque3d319d42013-02-20 11:08:32 -0600786 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Sending info interest on"
787 "name prefix : %s through Face:%u\n",int_name,face_id);
akmhoque53f64222012-09-05 13:57:51 -0500788
789 res=ccn_express_interest(nlsr->ccn,name,&(nlsr->in_content),templ);
790
791 if ( res >= 0 )
akmhoque3171d652012-11-13 11:44:33 -0600792 {
793 if ( nlsr->debugging )
794 printf("Info interest sending Successfull .... \n");
795 if ( nlsr->detailed_logging )
akmhoque3d319d42013-02-20 11:08:32 -0600796 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Info"
797 "interest sending Successfull .... \n");
akmhoque3171d652012-11-13 11:44:33 -0600798 }
akmhoque53f64222012-09-05 13:57:51 -0500799 ccn_charbuf_destroy(&templ);
800 }
801 ccn_charbuf_destroy(&name);
802 free(int_name);
akmhoquec06dcf12013-02-20 08:13:37 -0600803}
akmhoque53f64222012-09-05 13:57:51 -0500804
akmhoque7ab49a32013-02-20 11:27:51 -0600805/**
806* Check whether content name contains a key name. Return 1 for containing
807*/
808
akmhoquec06dcf12013-02-20 08:13:37 -0600809int
810contain_key_name(const unsigned char *ccnb, struct ccn_parsed_ContentObject *pco)
811{
812 if (pco->offset[CCN_PCO_B_KeyLocator] == pco->offset[CCN_PCO_E_KeyLocator])
813 return -1;
814
815 struct ccn_buf_decoder decoder;
816 struct ccn_buf_decoder *d;
akmhoque3d319d42013-02-20 11:08:32 -0600817 d = ccn_buf_decoder_start(&decoder, ccnb +
818 pco->offset[CCN_PCO_B_Key_Certificate_KeyName],
819 pco->offset[CCN_PCO_E_Key_Certificate_KeyName] -
820 pco->offset[CCN_PCO_B_Key_Certificate_KeyName]);
akmhoquec06dcf12013-02-20 08:13:37 -0600821 if (ccn_buf_match_dtag(d, CCN_DTAG_KeyName))
822 return 1;
823
824 return -1;
825}
826
akmhoque7ab49a32013-02-20 11:27:51 -0600827/**
828* Extract Key Name from Content Name and return the Key Name
829*
830*/
831
akmhoquec06dcf12013-02-20 08:13:37 -0600832struct ccn_charbuf *
833get_key_name(const unsigned char *ccnb, struct ccn_parsed_ContentObject *pco)
834{
835 struct ccn_charbuf *key_name = ccn_charbuf_create();
akmhoque3d319d42013-02-20 11:08:32 -0600836 ccn_charbuf_append(key_name, ccnb + pco->offset[CCN_PCO_B_KeyName_Name],
837 pco->offset[CCN_PCO_E_KeyName_Name] - pco->offset[CCN_PCO_B_KeyName_Name]);
akmhoquec06dcf12013-02-20 08:13:37 -0600838
839 return key_name;
akmhoque53f64222012-09-05 13:57:51 -0500840}