akmhoque | 8fdd641 | 2012-12-04 15:05:33 -0600 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <string.h> |
| 3 | #include <stdlib.h> |
| 4 | #include <unistd.h> |
| 5 | #include <getopt.h> |
| 6 | #include <sys/time.h> |
| 7 | #include <sys/stat.h> |
| 8 | #include <assert.h> |
| 9 | #include <sys/types.h> |
| 10 | #include <signal.h> |
| 11 | #include <sys/socket.h> |
| 12 | #include <sys/un.h> |
| 13 | #include <fcntl.h> |
| 14 | #include <sys/ioctl.h> |
| 15 | #include <netinet/in.h> |
| 16 | #include <netdb.h> |
| 17 | #include <arpa/inet.h> |
| 18 | |
| 19 | #include <ccn/ccn.h> |
| 20 | #include <ccn/uri.h> |
| 21 | #include <ccn/keystore.h> |
| 22 | #include <ccn/signing.h> |
| 23 | #include <ccn/schedule.h> |
| 24 | #include <ccn/hashtb.h> |
| 25 | #include <ccn/sync.h> |
| 26 | #include <ccn/seqwriter.h> |
| 27 | |
| 28 | #include "nlsr.h" |
| 29 | #include "nlsr_sync.h" |
| 30 | #include "nlsr_lsdb.h" |
akmhoque | 4ae1694 | 2012-12-10 11:50:43 -0600 | [diff] [blame] | 31 | #include "utility.h" |
Syed Obaid Amin | 3a3864b | 2013-01-09 03:05:50 -0600 | [diff] [blame] | 32 | //test method |
| 33 | char * |
| 34 | hex_string(unsigned char *s, size_t l) |
| 35 | { |
| 36 | const char *hex_digits = "0123456789abcdef"; |
| 37 | char *r; |
| 38 | int i; |
| 39 | r = calloc(1, 1 + 2 * l); |
| 40 | for (i = 0; i < l; i++) { |
| 41 | r[2*i] = hex_digits[(s[i]>>4) & 0xf]; |
| 42 | r[1+2*i] = hex_digits[s[i] & 0xf]; |
| 43 | } |
| 44 | return(r); |
| 45 | } |
| 46 | |
| 47 | int |
| 48 | sync_cb(struct ccns_name_closure *nc, |
| 49 | struct ccn_charbuf *lhash, |
| 50 | struct ccn_charbuf *rhash, |
| 51 | struct ccn_charbuf *name) |
| 52 | { |
| 53 | char *hexL; |
| 54 | char *hexR; |
| 55 | struct ccn_charbuf *uri = ccn_charbuf_create(); |
| 56 | if (lhash == NULL || lhash->length == 0) { |
| 57 | hexL = strdup("none"); |
| 58 | } else |
| 59 | hexL = hex_string(lhash->buf, lhash->length); |
| 60 | if (rhash == NULL || rhash->length == 0) { |
| 61 | hexR = strdup("none"); |
| 62 | } else |
| 63 | hexR = hex_string(rhash->buf, rhash->length); |
| 64 | if (name != NULL) |
| 65 | ccn_uri_append(uri, name->buf, name->length, 1); |
| 66 | else |
| 67 | ccn_charbuf_append_string(uri, "(null)"); |
a64adam | 04be648 | 2013-01-17 11:29:32 -0600 | [diff] [blame] | 68 | |
| 69 | if ( nlsr->debugging ) |
| 70 | printf("%s %s %s\n", ccn_charbuf_as_string(uri), hexL, hexR); |
Syed Obaid Amin | 3a3864b | 2013-01-09 03:05:50 -0600 | [diff] [blame] | 71 | fflush(stdout); |
| 72 | free(hexL); |
| 73 | free(hexR); |
| 74 | ccn_charbuf_destroy(&uri); |
akmhoque | d6cd61d | 2013-01-17 10:31:15 -0600 | [diff] [blame] | 75 | |
| 76 | |
| 77 | //--Doing ourthing from here |
akmhoque | b29edd8 | 2013-01-14 20:54:11 -0600 | [diff] [blame] | 78 | struct ccn_indexbuf cid={0}; |
Syed Obaid Amin | 3a3864b | 2013-01-09 03:05:50 -0600 | [diff] [blame] | 79 | |
| 80 | struct ccn_indexbuf *components=&cid; |
| 81 | ccn_name_split (name, components); |
| 82 | ccn_name_chop(name,components,-3); |
| 83 | |
| 84 | process_content_from_sync(name,components); |
| 85 | |
| 86 | |
| 87 | |
| 88 | return(0); |
| 89 | } |
| 90 | //test method |
akmhoque | 8fdd641 | 2012-12-04 15:05:33 -0600 | [diff] [blame] | 91 | |
| 92 | int |
| 93 | get_lsa_position(struct ccn_charbuf * ccnb, struct ccn_indexbuf *comps) |
| 94 | { |
| 95 | |
| 96 | |
| 97 | |
| 98 | int res,i; |
| 99 | int lsa_position=0; |
| 100 | int name_comps=(int)comps->n; |
| 101 | |
| 102 | for(i=0;i<name_comps;i++) |
| 103 | { |
| 104 | res=ccn_name_comp_strcmp(ccnb->buf,comps,i,"LSA"); |
| 105 | if( res == 0) |
| 106 | { |
| 107 | lsa_position=i; |
| 108 | break; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | return lsa_position; |
| 113 | |
| 114 | } |
| 115 | |
| 116 | void |
| 117 | get_name_part(struct name_prefix *name_part,struct ccn_charbuf * interest_ccnb, struct ccn_indexbuf *interest_comps, int offset) |
| 118 | { |
| 119 | |
| 120 | |
| 121 | |
| 122 | int res,i; |
| 123 | int lsa_position=0; |
| 124 | int len=0; |
| 125 | |
| 126 | lsa_position=get_lsa_position(interest_ccnb,interest_comps); |
| 127 | |
| 128 | const unsigned char *comp_ptr1; |
| 129 | size_t comp_size; |
| 130 | for(i=lsa_position+1+offset;i<interest_comps->n-1;i++) |
| 131 | { |
| 132 | res=ccn_name_comp_get(interest_ccnb->buf, interest_comps,i,&comp_ptr1, &comp_size); |
| 133 | len+=1; |
| 134 | len+=(int)comp_size; |
| 135 | } |
| 136 | len++; |
| 137 | |
| 138 | char *neighbor=(char *)malloc(len); |
| 139 | memset(neighbor,0,len); |
| 140 | |
| 141 | for(i=lsa_position+1+offset; i<interest_comps->n-1;i++) |
| 142 | { |
| 143 | res=ccn_name_comp_get(interest_ccnb->buf, interest_comps,i,&comp_ptr1, &comp_size); |
| 144 | memcpy(neighbor+strlen(neighbor),"/",1); |
| 145 | memcpy(neighbor+strlen(neighbor),(char *)comp_ptr1,strlen((char *)comp_ptr1)); |
| 146 | |
| 147 | } |
| 148 | |
| 149 | name_part->name=(char *)malloc(strlen(neighbor)+1); |
| 150 | memset(name_part->name,0,strlen(neighbor)+1); |
| 151 | memcpy(name_part->name,neighbor,strlen(neighbor)+1); |
| 152 | name_part->length=strlen(neighbor)+1; |
| 153 | |
| 154 | |
| 155 | } |
| 156 | |
akmhoque | 09c0afa | 2012-12-14 09:27:00 -0600 | [diff] [blame] | 157 | void |
| 158 | get_host_name_from_command_string(struct name_prefix *name_part,char *nbr_name_uri, int offset) |
| 159 | { |
| 160 | |
| 161 | |
| 162 | |
| 163 | int res,i; |
| 164 | int len=0; |
| 165 | const unsigned char *comp_ptr1; |
| 166 | size_t comp_size; |
| 167 | |
| 168 | struct ccn_charbuf *name=ccn_charbuf_create(); |
| 169 | name = ccn_charbuf_create(); |
| 170 | res = ccn_name_from_uri(name,nbr_name_uri); |
| 171 | if (res < 0) { |
| 172 | fprintf(stderr, "Bad ccn URI: %s\n", nbr_name_uri); |
| 173 | exit(1); |
| 174 | } |
| 175 | |
| 176 | struct ccn_indexbuf cid={0}; |
| 177 | |
| 178 | struct ccn_indexbuf *components=&cid; |
| 179 | ccn_name_split (name, components); |
| 180 | |
| 181 | for(i=components->n-2;i> (0+offset);i--) |
| 182 | { |
| 183 | res=ccn_name_comp_get(name->buf, components,i,&comp_ptr1, &comp_size); |
| 184 | len+=1; |
| 185 | len+=(int)comp_size; |
| 186 | } |
| 187 | len++; |
| 188 | |
| 189 | char *neighbor=(char *)malloc(len); |
| 190 | memset(neighbor,0,len); |
| 191 | |
| 192 | for(i=components->n-2;i> (0+offset);i--) |
| 193 | { |
| 194 | res=ccn_name_comp_get(name->buf, components,i,&comp_ptr1, &comp_size); |
| 195 | if ( i != components->n-2) |
| 196 | memcpy(neighbor+strlen(neighbor),".",1); |
| 197 | memcpy(neighbor+strlen(neighbor),(char *)comp_ptr1,strlen((char *)comp_ptr1)); |
| 198 | |
| 199 | } |
| 200 | |
| 201 | name_part->name=(char *)malloc(strlen(neighbor)+1); |
| 202 | memset(name_part->name,0,strlen(neighbor)+1); |
| 203 | memcpy(name_part->name,neighbor,strlen(neighbor)+1); |
| 204 | name_part->length=strlen(neighbor)+1; |
| 205 | |
| 206 | |
| 207 | } |
| 208 | |
akmhoque | 8fdd641 | 2012-12-04 15:05:33 -0600 | [diff] [blame] | 209 | |
soamin | 1504362 | 2013-01-15 23:10:26 -0600 | [diff] [blame] | 210 | //char * |
| 211 | void |
Obaid Amin | e63022a | 2013-01-15 23:36:40 -0600 | [diff] [blame] | 212 | get_content_by_content_name(char *content_name, unsigned char **content_data) |
akmhoque | 8fdd641 | 2012-12-04 15:05:33 -0600 | [diff] [blame] | 213 | { |
| 214 | |
soamin | 1504362 | 2013-01-15 23:10:26 -0600 | [diff] [blame] | 215 | struct ccn_charbuf *name = NULL; |
| 216 | struct ccn_charbuf *templ = NULL; |
| 217 | struct ccn_charbuf *resultbuf = NULL; |
| 218 | struct ccn_parsed_ContentObject pcobuf = { 0 }; |
| 219 | int res; |
| 220 | int allow_stale = 0; |
| 221 | int content_only = 1; |
| 222 | int scope = -1; |
| 223 | const unsigned char *ptr; |
| 224 | size_t length; |
| 225 | int resolve_version = CCN_V_HIGHEST; |
| 226 | int timeout_ms = 3000; |
| 227 | const unsigned lifetime_default = CCN_INTEREST_LIFETIME_SEC << 12; |
| 228 | unsigned lifetime_l12 = lifetime_default; |
| 229 | int get_flags = 0; |
akmhoque | 8fdd641 | 2012-12-04 15:05:33 -0600 | [diff] [blame] | 230 | |
soamin | 1504362 | 2013-01-15 23:10:26 -0600 | [diff] [blame] | 231 | name = ccn_charbuf_create(); |
| 232 | res = ccn_name_from_uri(name,content_name); |
| 233 | if (res < 0) { |
| 234 | fprintf(stderr, "Bad ccn URI: %s\n", content_name); |
| 235 | exit(1); |
| 236 | } |
| 237 | |
akmhoque | 8fdd641 | 2012-12-04 15:05:33 -0600 | [diff] [blame] | 238 | if (allow_stale || lifetime_l12 != lifetime_default || scope != -1) { |
soamin | 1504362 | 2013-01-15 23:10:26 -0600 | [diff] [blame] | 239 | templ = ccn_charbuf_create(); |
| 240 | ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG); |
| 241 | ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG); |
| 242 | ccn_charbuf_append_closer(templ); /* </Name> */ |
akmhoque | 8fdd641 | 2012-12-04 15:05:33 -0600 | [diff] [blame] | 243 | if (allow_stale) { |
| 244 | ccn_charbuf_append_tt(templ, CCN_DTAG_AnswerOriginKind, CCN_DTAG); |
| 245 | ccnb_append_number(templ, |
soamin | 1504362 | 2013-01-15 23:10:26 -0600 | [diff] [blame] | 246 | CCN_AOK_DEFAULT | CCN_AOK_STALE); |
akmhoque | 8fdd641 | 2012-12-04 15:05:33 -0600 | [diff] [blame] | 247 | ccn_charbuf_append_closer(templ); /* </AnswerOriginKind> */ |
| 248 | } |
soamin | 1504362 | 2013-01-15 23:10:26 -0600 | [diff] [blame] | 249 | if (scope != -1) { |
| 250 | ccnb_tagged_putf(templ, CCN_DTAG_Scope, "%d", scope); |
| 251 | } |
akmhoque | 8fdd641 | 2012-12-04 15:05:33 -0600 | [diff] [blame] | 252 | if (lifetime_l12 != lifetime_default) { |
| 253 | /* |
| 254 | * Choose the interest lifetime so there are at least 3 |
| 255 | * expressions (in the unsatisfied case). |
| 256 | */ |
| 257 | unsigned char buf[3] = { 0 }; |
| 258 | int i; |
| 259 | for (i = sizeof(buf) - 1; i >= 0; i--, lifetime_l12 >>= 8) |
| 260 | buf[i] = lifetime_l12 & 0xff; |
| 261 | ccnb_append_tagged_blob(templ, CCN_DTAG_InterestLifetime, buf, sizeof(buf)); |
| 262 | } |
soamin | 1504362 | 2013-01-15 23:10:26 -0600 | [diff] [blame] | 263 | ccn_charbuf_append_closer(templ); /* </Interest> */ |
| 264 | } |
| 265 | resultbuf = ccn_charbuf_create(); |
| 266 | if (resolve_version != 0) { |
| 267 | res = ccn_resolve_version(nlsr->ccn, name, resolve_version, 500); |
| 268 | if (res >= 0) { |
| 269 | ccn_uri_append(resultbuf, name->buf, name->length, 1); |
| 270 | //fprintf(stderr, "== %s\n",ccn_charbuf_as_string(resultbuf)); |
| 271 | resultbuf->length = 0; |
| 272 | } |
| 273 | } |
| 274 | res = ccn_get(nlsr->ccn, name, templ, timeout_ms, resultbuf, &pcobuf, NULL, get_flags); |
| 275 | if (res >= 0) { |
| 276 | ptr = resultbuf->buf; |
| 277 | length = resultbuf->length; |
| 278 | if (content_only){ |
| 279 | ccn_content_get_value(ptr, length, &pcobuf, &ptr, &length); |
Obaid Amin | e63022a | 2013-01-15 23:36:40 -0600 | [diff] [blame] | 280 | *content_data = (unsigned char *) calloc(length, sizeof(char *)); |
| 281 | memcpy (*content_data, ptr, length); |
soamin | 1504362 | 2013-01-15 23:10:26 -0600 | [diff] [blame] | 282 | } |
| 283 | } |
| 284 | ccn_charbuf_destroy(&resultbuf); |
| 285 | ccn_charbuf_destroy(&templ); |
| 286 | ccn_charbuf_destroy(&name); |
| 287 | //return (unsigned char *)ptr; |
akmhoque | 8fdd641 | 2012-12-04 15:05:33 -0600 | [diff] [blame] | 288 | } |
| 289 | |
akmhoque | 4ae1694 | 2012-12-10 11:50:43 -0600 | [diff] [blame] | 290 | void |
soamin | 1504362 | 2013-01-15 23:10:26 -0600 | [diff] [blame] | 291 | process_incoming_sync_content_lsa( unsigned char *content_data) |
akmhoque | 4ae1694 | 2012-12-10 11:50:43 -0600 | [diff] [blame] | 292 | { |
| 293 | |
| 294 | |
| 295 | if ( nlsr->debugging ) |
| 296 | printf("process_incoming_sync_content_lsa called \n"); |
| 297 | //if ( nlsr->detailed_logging ) |
| 298 | //writeLogg(__FILE__,__FUNCTION__,__LINE__,"process_incoming_content_lsa called \n"); |
| 299 | |
| 300 | char *sep="|"; |
| 301 | char *rem; |
| 302 | char *orig_router; |
| 303 | char *orl; |
| 304 | int orig_router_length; |
| 305 | char *lst; |
| 306 | int ls_type; |
| 307 | char *lsid; |
| 308 | long int ls_id; |
| 309 | char *isvld; |
| 310 | int isValid; |
| 311 | char *num_link; |
| 312 | int no_link; |
| 313 | char *np; |
| 314 | char *np_length; |
| 315 | int name_length; |
| 316 | char *data; |
| 317 | char *orig_time; |
| 318 | |
| 319 | |
| 320 | if ( nlsr->debugging ) |
| 321 | printf("LSA Data \n"); |
| 322 | //if ( nlsr->detailed_logging ) |
| 323 | // writeLogg(__FILE__,__FUNCTION__,__LINE__,"LSA Data\n"); |
| 324 | |
soamin | 1504362 | 2013-01-15 23:10:26 -0600 | [diff] [blame] | 325 | if( strlen((char *)content_data ) > 0 ) |
akmhoque | 4ae1694 | 2012-12-10 11:50:43 -0600 | [diff] [blame] | 326 | { |
| 327 | |
| 328 | orig_router=strtok_r((char *)content_data,sep,&rem); |
| 329 | orl=strtok_r(NULL,sep,&rem); |
| 330 | orig_router_length=atoi(orl); |
| 331 | |
| 332 | if ( nlsr->debugging ) |
| 333 | { |
| 334 | printf(" Orig Router Name : %s\n",orig_router); |
| 335 | printf(" Orig Router Length: %d\n",orig_router_length); |
| 336 | } |
| 337 | |
| 338 | lst=strtok_r(NULL,sep,&rem); |
| 339 | ls_type=atoi(lst); |
| 340 | |
| 341 | if ( nlsr->debugging ) |
| 342 | printf(" LS Type : %d\n",ls_type); |
| 343 | |
| 344 | if ( ls_type == LS_TYPE_NAME ) |
| 345 | { |
| 346 | lsid=strtok_r(NULL,sep,&rem); |
| 347 | ls_id=atoi(lsid); |
| 348 | orig_time=strtok_r(NULL,sep,&rem); |
| 349 | isvld=strtok_r(NULL,sep,&rem); |
| 350 | isValid=atoi(isvld); |
| 351 | np=strtok_r(NULL,sep,&rem); |
| 352 | np_length=strtok_r(NULL,sep,&rem); |
| 353 | name_length=atoi(np_length); |
| 354 | if ( nlsr->debugging ) |
| 355 | { |
| 356 | printf(" LS ID : %ld\n",ls_id); |
| 357 | printf(" isValid : %d\n",isValid); |
| 358 | printf(" Name Prefix : %s\n",np); |
| 359 | printf(" Orig Time : %s\n",orig_time); |
| 360 | printf(" Name Prefix length: %d\n",name_length); |
| 361 | } |
| 362 | |
| 363 | build_and_install_others_name_lsa(orig_router,ls_type,ls_id,orig_time,isValid,np); |
| 364 | |
| 365 | print_name_lsdb(); |
| 366 | |
| 367 | } |
| 368 | else if ( ls_type == LS_TYPE_ADJ ) |
| 369 | { |
| 370 | orig_time=strtok_r(NULL,sep,&rem); |
| 371 | num_link=strtok_r(NULL,sep,&rem); |
| 372 | no_link=atoi(num_link); |
| 373 | data=rem; |
| 374 | |
| 375 | if ( nlsr->debugging ) |
| 376 | { |
| 377 | printf(" No Link : %d\n",no_link); |
| 378 | printf(" Data : %s\n",data); |
| 379 | } |
| 380 | build_and_install_others_adj_lsa(orig_router,ls_type,orig_time,no_link,data); |
| 381 | } |
| 382 | } |
| 383 | } |
| 384 | |
akmhoque | 8fdd641 | 2012-12-04 15:05:33 -0600 | [diff] [blame] | 385 | void |
| 386 | process_content_from_sync(struct ccn_charbuf *content_name, struct ccn_indexbuf *components) |
| 387 | { |
| 388 | int lsa_position; |
| 389 | int res; |
| 390 | size_t comp_size; |
| 391 | const unsigned char *lst; |
| 392 | const unsigned char *lsid; |
| 393 | const unsigned char *origtime; |
| 394 | |
| 395 | int ls_type; |
| 396 | long int ls_id=0; |
| 397 | |
soamin | 1504362 | 2013-01-15 23:10:26 -0600 | [diff] [blame] | 398 | unsigned char *content_data = NULL; |
| 399 | |
akmhoque | 4ae1694 | 2012-12-10 11:50:43 -0600 | [diff] [blame] | 400 | char *time_stamp=(char *)malloc(20); |
| 401 | memset(time_stamp,0,20); |
| 402 | get_current_timestamp_micro(time_stamp); |
| 403 | |
akmhoque | 8fdd641 | 2012-12-04 15:05:33 -0600 | [diff] [blame] | 404 | struct ccn_charbuf *uri = ccn_charbuf_create(); |
| 405 | ccn_uri_append(uri, content_name->buf, content_name->length, 0); |
| 406 | |
| 407 | struct name_prefix *orig_router=(struct name_prefix *)malloc(sizeof(struct name_prefix)); |
| 408 | |
| 409 | lsa_position=get_lsa_position(content_name, components); |
| 410 | |
| 411 | res=ccn_name_comp_get(content_name->buf, components,lsa_position+1,&lst, &comp_size); |
| 412 | |
akmhoque | 4ae1694 | 2012-12-10 11:50:43 -0600 | [diff] [blame] | 413 | //printf("Ls Type: %s\n",lst); |
akmhoque | 8fdd641 | 2012-12-04 15:05:33 -0600 | [diff] [blame] | 414 | ls_type=atoi((char *)lst); |
| 415 | if(ls_type == LS_TYPE_NAME) |
| 416 | { |
akmhoque | 4ae1694 | 2012-12-10 11:50:43 -0600 | [diff] [blame] | 417 | |
akmhoque | 8fdd641 | 2012-12-04 15:05:33 -0600 | [diff] [blame] | 418 | res=ccn_name_comp_get(content_name->buf, components,lsa_position+2,&lsid, &comp_size); |
| 419 | ls_id=atoi((char *)lsid); |
| 420 | res=ccn_name_comp_get(content_name->buf, components,lsa_position+3,&origtime, &comp_size); |
| 421 | get_name_part(orig_router,content_name,components,3); |
akmhoque | 8fdd641 | 2012-12-04 15:05:33 -0600 | [diff] [blame] | 422 | |
akmhoque | 4ae1694 | 2012-12-10 11:50:43 -0600 | [diff] [blame] | 423 | int lsa_life_time=get_time_diff(time_stamp,(char *)origtime); |
| 424 | |
| 425 | //printf("Ls ID: %s\nOrig Time: %s\nOrig Router: %s\n",lsid,origtime,orig_router->name); |
| 426 | |
| 427 | if ( (strcmp((char *)orig_router,nlsr->router_name) == 0 && lsa_life_time < nlsr->lsa_refresh_time) || (strcmp((char *)orig_router,nlsr->router_name) != 0 && lsa_life_time < nlsr->router_dead_interval) ) |
akmhoque | 8fdd641 | 2012-12-04 15:05:33 -0600 | [diff] [blame] | 428 | { |
akmhoque | 4ae1694 | 2012-12-10 11:50:43 -0600 | [diff] [blame] | 429 | int is_new_name_lsa=check_is_new_name_lsa(orig_router->name,(char *)lst,(char *)lsid,(char *)origtime); |
| 430 | if ( is_new_name_lsa == 1 ) |
| 431 | { |
a64adam | 04be648 | 2013-01-17 11:29:32 -0600 | [diff] [blame] | 432 | if ( nlsr->debugging ) |
| 433 | printf("New NAME LSA.....\n"); |
soamin | 1504362 | 2013-01-15 23:10:26 -0600 | [diff] [blame] | 434 | //content_data=get_content_by_content_name(ccn_charbuf_as_string(uri)); |
Obaid Amin | e63022a | 2013-01-15 23:36:40 -0600 | [diff] [blame] | 435 | get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data); |
a64adam | f7b051d | 2013-01-17 11:39:58 -0600 | [diff] [blame^] | 436 | if ( nlsr->debugging ) |
| 437 | printf("Content Data: %s \n",content_data); |
akmhoque | 4ae1694 | 2012-12-10 11:50:43 -0600 | [diff] [blame] | 438 | process_incoming_sync_content_lsa(content_data); |
| 439 | } |
| 440 | else |
| 441 | { |
a64adam | 04be648 | 2013-01-17 11:29:32 -0600 | [diff] [blame] | 442 | if ( nlsr->debugging ) |
| 443 | printf("Name LSA / Newer Name LSA already xists in LSDB\n"); |
soamin | 1504362 | 2013-01-15 23:10:26 -0600 | [diff] [blame] | 444 | //content_data=get_content_by_content_name(ccn_charbuf_as_string(uri)); |
Obaid Amin | e63022a | 2013-01-15 23:36:40 -0600 | [diff] [blame] | 445 | get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data); |
a64adam | 04be648 | 2013-01-17 11:29:32 -0600 | [diff] [blame] | 446 | |
| 447 | if ( nlsr->debugging ) |
| 448 | printf("Content Data: %s \n",content_data); |
akmhoque | 4ae1694 | 2012-12-10 11:50:43 -0600 | [diff] [blame] | 449 | } |
akmhoque | 8fdd641 | 2012-12-04 15:05:33 -0600 | [diff] [blame] | 450 | } |
| 451 | else |
| 452 | { |
a64adam | 04be648 | 2013-01-17 11:29:32 -0600 | [diff] [blame] | 453 | if ( nlsr->debugging ) |
| 454 | printf("Lsa is older than Router LSA refresh time/ Dead Interval\n"); |
akmhoque | 8fdd641 | 2012-12-04 15:05:33 -0600 | [diff] [blame] | 455 | } |
| 456 | } |
| 457 | else if(ls_type == LS_TYPE_ADJ) |
| 458 | { |
| 459 | res=ccn_name_comp_get(content_name->buf, components,lsa_position+2,&origtime, &comp_size); |
| 460 | get_name_part(orig_router,content_name,components,2); |
a64adam | 04be648 | 2013-01-17 11:29:32 -0600 | [diff] [blame] | 461 | |
| 462 | if ( nlsr->debugging ) |
| 463 | printf("Orig Time: %s\nOrig Router: %s\n",origtime,orig_router->name); |
akmhoque | 8fdd641 | 2012-12-04 15:05:33 -0600 | [diff] [blame] | 464 | |
akmhoque | 4ae1694 | 2012-12-10 11:50:43 -0600 | [diff] [blame] | 465 | int lsa_life_time=get_time_diff(time_stamp,(char *)origtime); |
akmhoque | 8fdd641 | 2012-12-04 15:05:33 -0600 | [diff] [blame] | 466 | |
akmhoque | 4ae1694 | 2012-12-10 11:50:43 -0600 | [diff] [blame] | 467 | //printf("Ls ID: %s\nOrig Time: %s\nOrig Router: %s\n",lsid,origtime,orig_router->name); |
| 468 | |
| 469 | if ( (strcmp((char *)orig_router,nlsr->router_name) == 0 && lsa_life_time < nlsr->lsa_refresh_time) || (strcmp((char *)orig_router,nlsr->router_name) != 0 && lsa_life_time < nlsr->router_dead_interval) ) |
| 470 | { |
| 471 | int is_new_adj_lsa=check_is_new_adj_lsa(orig_router->name,(char *)lst,(char *)origtime); |
| 472 | if ( is_new_adj_lsa == 1 ) |
| 473 | { |
a64adam | 04be648 | 2013-01-17 11:29:32 -0600 | [diff] [blame] | 474 | if ( nlsr->debugging ) |
| 475 | printf("New Adj LSA.....\n"); |
soamin | 1504362 | 2013-01-15 23:10:26 -0600 | [diff] [blame] | 476 | //content_data=get_content_by_content_name(ccn_charbuf_as_string(uri)); |
Obaid Amin | e63022a | 2013-01-15 23:36:40 -0600 | [diff] [blame] | 477 | get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data); |
a64adam | 04be648 | 2013-01-17 11:29:32 -0600 | [diff] [blame] | 478 | |
| 479 | if ( nlsr->debugging ) |
| 480 | printf("Content Data: %s \n",content_data); |
akmhoque | 4ae1694 | 2012-12-10 11:50:43 -0600 | [diff] [blame] | 481 | process_incoming_sync_content_lsa(content_data); |
| 482 | } |
| 483 | else |
| 484 | { |
a64adam | 04be648 | 2013-01-17 11:29:32 -0600 | [diff] [blame] | 485 | if ( nlsr->debugging ) |
| 486 | printf("Adj LSA / Newer Adj LSA already exists in LSDB\n"); |
Obaid Amin | e63022a | 2013-01-15 23:36:40 -0600 | [diff] [blame] | 487 | get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data); |
a64adam | 04be648 | 2013-01-17 11:29:32 -0600 | [diff] [blame] | 488 | if ( nlsr->debugging ) |
| 489 | printf("Content Data: %s \n",content_data); |
akmhoque | 4ae1694 | 2012-12-10 11:50:43 -0600 | [diff] [blame] | 490 | } |
| 491 | } |
| 492 | else |
| 493 | { |
a64adam | 04be648 | 2013-01-17 11:29:32 -0600 | [diff] [blame] | 494 | if ( nlsr->debugging ) |
| 495 | printf("Lsa is older than Router LSA refresh time/ Dead Interval\n"); |
akmhoque | 4ae1694 | 2012-12-10 11:50:43 -0600 | [diff] [blame] | 496 | } |
akmhoque | 8fdd641 | 2012-12-04 15:05:33 -0600 | [diff] [blame] | 497 | } |
| 498 | |
Obaid Amin | e63022a | 2013-01-15 23:36:40 -0600 | [diff] [blame] | 499 | if (content_data != NULL) |
| 500 | free(content_data); |
akmhoque | 8fdd641 | 2012-12-04 15:05:33 -0600 | [diff] [blame] | 501 | ccn_charbuf_destroy(&uri); |
akmhoque | 8fdd641 | 2012-12-04 15:05:33 -0600 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | int |
| 505 | sync_callback(struct ccns_name_closure *nc, |
| 506 | struct ccn_charbuf *lhash, |
| 507 | struct ccn_charbuf *rhash, |
| 508 | struct ccn_charbuf *name) |
| 509 | { |
| 510 | |
| 511 | |
| 512 | struct ccn_indexbuf cid={0}; |
| 513 | |
| 514 | struct ccn_indexbuf *components=&cid; |
| 515 | ccn_name_split (name, components); |
| 516 | ccn_name_chop(name,components,-3); |
| 517 | |
| 518 | process_content_from_sync(name,components); |
| 519 | |
| 520 | return(0); |
| 521 | } |
| 522 | |
| 523 | |
| 524 | void |
| 525 | sync_monitor(char *topo_prefix, char *slice_prefix) |
| 526 | { |
| 527 | |
| 528 | static struct ccns_name_closure nc={0}; |
| 529 | |
| 530 | nlsr->closure = &nc; |
| 531 | struct ccn_charbuf *prefix = ccn_charbuf_create(); |
| 532 | struct ccn_charbuf *roothash = NULL; |
| 533 | struct ccn_charbuf *topo = ccn_charbuf_create(); |
| 534 | nlsr->slice = ccns_slice_create(); |
| 535 | ccn_charbuf_reset(prefix); |
| 536 | ccn_charbuf_reset(topo); |
| 537 | |
| 538 | ccn_charbuf_reset(prefix); |
| 539 | ccn_name_from_uri(prefix, slice_prefix); |
| 540 | |
| 541 | ccn_charbuf_reset(topo); |
| 542 | ccn_name_from_uri(topo, topo_prefix); |
| 543 | |
| 544 | |
| 545 | ccns_slice_set_topo_prefix(nlsr->slice, topo, prefix); |
Syed Obaid Amin | 3a3864b | 2013-01-09 03:05:50 -0600 | [diff] [blame] | 546 | nlsr->closure->callback = &sync_cb; |
| 547 | //nlsr->closure->callback = &sync_callback; |
akmhoque | 8fdd641 | 2012-12-04 15:05:33 -0600 | [diff] [blame] | 548 | nlsr->ccns = ccns_open(nlsr->ccn, nlsr->slice, nlsr->closure, roothash, NULL); |
akmhoque | 8fdd641 | 2012-12-04 15:05:33 -0600 | [diff] [blame] | 549 | } |
| 550 | |
| 551 | struct ccn_charbuf * |
| 552 | make_template(int scope) |
| 553 | { |
| 554 | struct ccn_charbuf *templ = NULL; |
| 555 | templ = ccn_charbuf_create(); |
| 556 | ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG); |
| 557 | ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG); |
| 558 | ccn_charbuf_append_closer(templ); /* </Name> */ |
| 559 | if (0 <= scope && scope <= 2) |
| 560 | ccnb_tagged_putf(templ, CCN_DTAG_Scope, "%d", scope); |
| 561 | ccn_charbuf_append_closer(templ); /* </Interest> */ |
| 562 | return(templ); |
| 563 | } |
| 564 | |
| 565 | void |
| 566 | write_data_to_repo(char *data, char *name_prefix) |
| 567 | { |
akmhoque | 260698b | 2013-01-17 03:54:09 -0600 | [diff] [blame] | 568 | if ( nlsr->debugging ) |
| 569 | { |
| 570 | printf("write_data_to_repo called\n"); |
akmhoque | 47d19d1 | 2013-01-17 04:00:32 -0600 | [diff] [blame] | 571 | printf("Content Name: %s \n",name_prefix); |
akmhoque | 260698b | 2013-01-17 03:54:09 -0600 | [diff] [blame] | 572 | printf("Content Data: %s \n",data); |
| 573 | } |
akmhoque | 8fdd641 | 2012-12-04 15:05:33 -0600 | [diff] [blame] | 574 | |
| 575 | struct ccn_charbuf *name = NULL; |
| 576 | struct ccn_seqwriter *w = NULL; |
| 577 | int blocksize = 1024; |
| 578 | int freshness = -1; |
| 579 | int torepo = 1; |
| 580 | int scope = 1; |
| 581 | int res; |
| 582 | size_t blockread; |
| 583 | struct ccn_charbuf *templ; |
| 584 | |
| 585 | name = ccn_charbuf_create(); |
| 586 | res = ccn_name_from_uri(name, name_prefix); |
| 587 | if (res < 0) { |
| 588 | fprintf(stderr, "bad CCN URI: %s\n",name_prefix); |
| 589 | exit(1); |
| 590 | } |
| 591 | |
| 592 | |
| 593 | w = ccn_seqw_create(nlsr->ccn, name); |
| 594 | if (w == NULL) { |
| 595 | fprintf(stderr, "ccn_seqw_create failed\n"); |
| 596 | exit(1); |
| 597 | } |
| 598 | ccn_seqw_set_block_limits(w, blocksize, blocksize); |
| 599 | if (freshness > -1) |
| 600 | ccn_seqw_set_freshness(w, freshness); |
| 601 | if (torepo) { |
| 602 | struct ccn_charbuf *name_v = ccn_charbuf_create(); |
| 603 | ccn_seqw_get_name(w, name_v); |
| 604 | ccn_name_from_uri(name_v, "%C1.R.sw"); |
| 605 | ccn_name_append_nonce(name_v); |
| 606 | templ = make_template(scope); |
| 607 | res = ccn_get(nlsr->ccn, name_v, templ, 60000, NULL, NULL, NULL, 0); |
| 608 | ccn_charbuf_destroy(&templ); |
| 609 | ccn_charbuf_destroy(&name_v); |
| 610 | if (res < 0) { |
| 611 | fprintf(stderr, "No response from repository\n"); |
| 612 | exit(1); |
| 613 | } |
| 614 | } |
| 615 | |
| 616 | |
| 617 | |
| 618 | |
| 619 | blockread = 0; |
akmhoque | 260698b | 2013-01-17 03:54:09 -0600 | [diff] [blame] | 620 | |
akmhoque | 8fdd641 | 2012-12-04 15:05:33 -0600 | [diff] [blame] | 621 | |
| 622 | blockread=strlen(data); |
| 623 | |
| 624 | if (blockread > 0) { |
akmhoque | d6cd61d | 2013-01-17 10:31:15 -0600 | [diff] [blame] | 625 | //ccn_run(nlsr->ccn, 100); |
akmhoque | 8fdd641 | 2012-12-04 15:05:33 -0600 | [diff] [blame] | 626 | res = ccn_seqw_write(w, data, blockread); |
akmhoque | 260698b | 2013-01-17 03:54:09 -0600 | [diff] [blame] | 627 | while (res == -1) { |
akmhoque | d6cd61d | 2013-01-17 10:31:15 -0600 | [diff] [blame] | 628 | //ccn_run(nlsr->ccn, 100); |
akmhoque | 260698b | 2013-01-17 03:54:09 -0600 | [diff] [blame] | 629 | res = ccn_seqw_write(w, data, blockread); |
| 630 | } |
akmhoque | 8fdd641 | 2012-12-04 15:05:33 -0600 | [diff] [blame] | 631 | } |
| 632 | |
akmhoque | 8fdd641 | 2012-12-04 15:05:33 -0600 | [diff] [blame] | 633 | ccn_seqw_close(w); |
akmhoque | 260698b | 2013-01-17 03:54:09 -0600 | [diff] [blame] | 634 | //ccn_run(nlsr->ccn, 1); |
akmhoque | 8fdd641 | 2012-12-04 15:05:33 -0600 | [diff] [blame] | 635 | ccn_charbuf_destroy(&name); |
| 636 | } |
| 637 | |
| 638 | |
| 639 | int |
| 640 | create_sync_slice(char *topo_prefix, char *slice_prefix) |
| 641 | { |
| 642 | int res; |
| 643 | struct ccns_slice *slice; |
| 644 | struct ccn_charbuf *prefix = ccn_charbuf_create(); |
| 645 | struct ccn_charbuf *topo = ccn_charbuf_create(); |
| 646 | struct ccn_charbuf *clause = ccn_charbuf_create(); |
| 647 | struct ccn_charbuf *slice_name = ccn_charbuf_create(); |
| 648 | struct ccn_charbuf *slice_uri = ccn_charbuf_create(); |
| 649 | |
| 650 | if (prefix == NULL || topo == NULL || clause == NULL || |
| 651 | slice_name == NULL || slice_uri == NULL) { |
| 652 | fprintf(stderr, "Unable to allocate required memory.\n"); |
| 653 | exit(1); |
| 654 | } |
| 655 | |
| 656 | |
| 657 | slice = ccns_slice_create(); |
| 658 | |
| 659 | ccn_charbuf_reset(topo); |
| 660 | ccn_name_from_uri(topo, topo_prefix); |
| 661 | ccn_charbuf_reset(prefix); |
| 662 | ccn_name_from_uri(prefix,slice_prefix ); |
| 663 | ccns_slice_set_topo_prefix(slice, topo, prefix); |
| 664 | |
| 665 | |
| 666 | res = ccns_write_slice(nlsr->ccn, slice, slice_name); |
Syed Obaid Amin | 83eea12 | 2013-01-08 12:21:37 -0600 | [diff] [blame] | 667 | /* |
| 668 | // Obaid: commenting out the following lines to resolve a bug. |
| 669 | // If commenting them can resolve the issue, then we |
| 670 | // need to call them before terminating the program. |
akmhoque | 8fdd641 | 2012-12-04 15:05:33 -0600 | [diff] [blame] | 671 | ccns_slice_destroy(&slice); |
| 672 | ccn_charbuf_destroy(&prefix); |
| 673 | ccn_charbuf_destroy(&topo); |
| 674 | ccn_charbuf_destroy(&clause); |
| 675 | ccn_charbuf_destroy(&slice_name); |
| 676 | ccn_charbuf_destroy(&slice_uri); |
Syed Obaid Amin | 83eea12 | 2013-01-08 12:21:37 -0600 | [diff] [blame] | 677 | */ |
akmhoque | 8fdd641 | 2012-12-04 15:05:33 -0600 | [diff] [blame] | 678 | return 0; |
| 679 | } |
| 680 | |