akmhoque | a0e7115 | 2013-02-11 09:47:59 -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" |
| 31 | #include "utility.h" |
| 32 | |
| 33 | |
| 34 | char * |
| 35 | hex_string(unsigned char *s, size_t l) |
| 36 | { |
| 37 | const char *hex_digits = "0123456789abcdef"; |
| 38 | char *r; |
| 39 | int i; |
| 40 | r = calloc(1, 1 + 2 * l); |
| 41 | for (i = 0; i < l; i++) { |
| 42 | r[2*i] = hex_digits[(s[i]>>4) & 0xf]; |
| 43 | r[1+2*i] = hex_digits[s[i] & 0xf]; |
| 44 | } |
| 45 | return(r); |
| 46 | } |
| 47 | |
| 48 | int |
| 49 | sync_cb(struct ccns_name_closure *nc, |
| 50 | struct ccn_charbuf *lhash, |
| 51 | struct ccn_charbuf *rhash, |
| 52 | struct ccn_charbuf *name) |
| 53 | { |
| 54 | char *hexL; |
| 55 | char *hexR; |
| 56 | int res; |
| 57 | struct ccn_charbuf *uri = ccn_charbuf_create(); |
| 58 | if (lhash == NULL || lhash->length == 0) { |
| 59 | hexL = strdup("none"); |
| 60 | } else |
| 61 | hexL = hex_string(lhash->buf, lhash->length); |
| 62 | if (rhash == NULL || rhash->length == 0) { |
| 63 | hexR = strdup("none"); |
| 64 | } else |
| 65 | hexR = hex_string(rhash->buf, rhash->length); |
| 66 | if (name != NULL) |
| 67 | ccn_uri_append(uri, name->buf, name->length, 1); |
| 68 | else |
| 69 | ccn_charbuf_append_string(uri, "(null)"); |
| 70 | |
| 71 | |
| 72 | if ( nlsr->debugging ) |
| 73 | printf("Response from sync in the name: %s \n",ccn_charbuf_as_string(uri)); |
| 74 | |
| 75 | fflush(stdout); |
| 76 | free(hexL); |
| 77 | free(hexR); |
| 78 | ccn_charbuf_destroy(&uri); |
| 79 | |
| 80 | |
| 81 | //--Doing our thing from here |
| 82 | struct ccn_indexbuf cid={0}; |
| 83 | |
| 84 | struct ccn_indexbuf *components=&cid; |
akmhoque | 018692c | 2013-02-11 11:33:39 -0600 | [diff] [blame] | 85 | res=ccn_name_split (name, components); |
| 86 | if ( res < 0 ) |
| 87 | return 0; |
akmhoque | a0e7115 | 2013-02-11 09:47:59 -0600 | [diff] [blame] | 88 | //ccn_name_chop(name,components,-3); |
| 89 | //process_content_from_sync(name,components); |
| 90 | |
| 91 | struct ccn_charbuf *content_name = ccn_charbuf_create(); |
| 92 | ccn_name_init(content_name); |
akmhoque | 018692c | 2013-02-11 11:33:39 -0600 | [diff] [blame] | 93 | if (components->n < 2) |
| 94 | return 0; |
akmhoque | a0e7115 | 2013-02-11 09:47:59 -0600 | [diff] [blame] | 95 | res = ccn_name_append_components(content_name, name->buf, components->buf[0], components->buf[components->n - 1]); |
| 96 | |
akmhoque | 018692c | 2013-02-11 11:33:39 -0600 | [diff] [blame] | 97 | if ( res < 0) |
| 98 | return 0; |
| 99 | |
| 100 | |
akmhoque | a0e7115 | 2013-02-11 09:47:59 -0600 | [diff] [blame] | 101 | // debugging purpose |
| 102 | struct ccn_charbuf *temp=ccn_charbuf_create(); |
| 103 | ccn_uri_append(temp, content_name->buf, content_name->length, 0); |
| 104 | if ( nlsr->debugging ) |
| 105 | printf("Name before chopping: %s \n",ccn_charbuf_as_string(temp)); |
| 106 | ccn_charbuf_destroy(&temp); |
| 107 | |
| 108 | struct ccn_indexbuf cid1={0}; |
| 109 | struct ccn_indexbuf *components1=&cid1; |
| 110 | res=ccn_name_split (content_name, components1); |
akmhoque | 018692c | 2013-02-11 11:33:39 -0600 | [diff] [blame] | 111 | if ( res < 0) |
| 112 | return 0; |
| 113 | |
akmhoque | a0e7115 | 2013-02-11 09:47:59 -0600 | [diff] [blame] | 114 | if ( nlsr->debugging ) |
| 115 | { |
| 116 | printf("Number of components in name = %d \n",res); |
| 117 | printf("Number of components in name as indexbuf->n = %d \n",(int)components1->n); |
| 118 | } |
| 119 | ccn_name_chop(content_name,components1,-3); |
| 120 | if ( nlsr->debugging ) |
| 121 | printf("Number of components in name as indexbuf->n after chopping= %d \n",(int)components1->n); |
| 122 | |
| 123 | //debugging purpose |
| 124 | struct ccn_charbuf *temp1=ccn_charbuf_create(); |
| 125 | ccn_uri_append(temp1, content_name->buf, content_name->length, 0); |
| 126 | if ( nlsr->debugging ) |
| 127 | printf("Name after chopping: %s \n",ccn_charbuf_as_string(temp1)); |
| 128 | ccn_charbuf_destroy(&temp1); |
| 129 | |
| 130 | process_content_from_sync(content_name,components1); |
| 131 | ccn_charbuf_destroy(&content_name); |
| 132 | |
| 133 | return(0); |
| 134 | } |
| 135 | |
| 136 | |
| 137 | int |
| 138 | get_lsa_position(struct ccn_charbuf * ccnb, struct ccn_indexbuf *comps) |
| 139 | { |
| 140 | |
| 141 | |
| 142 | |
| 143 | int res,i; |
| 144 | int lsa_position=0; |
| 145 | int name_comps=(int)comps->n; |
| 146 | |
| 147 | for(i=0;i<name_comps;i++) |
| 148 | { |
| 149 | res=ccn_name_comp_strcmp(ccnb->buf,comps,i,"LSA"); |
| 150 | if( res == 0) |
| 151 | { |
| 152 | lsa_position=i; |
| 153 | break; |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | return lsa_position; |
| 158 | |
| 159 | } |
| 160 | |
| 161 | void |
| 162 | get_name_part(struct name_prefix *name_part,struct ccn_charbuf * interest_ccnb, struct ccn_indexbuf *interest_comps, int offset) |
| 163 | { |
| 164 | |
| 165 | |
| 166 | |
| 167 | int res,i; |
| 168 | int lsa_position=0; |
| 169 | int len=0; |
| 170 | |
| 171 | |
| 172 | struct ccn_indexbuf cid={0}; |
| 173 | struct ccn_indexbuf *components=&cid; |
| 174 | struct ccn_charbuf *name=ccn_charbuf_create(); |
| 175 | ccn_name_from_uri(name,nlsr->slice_prefix); |
| 176 | ccn_name_split (name, components); |
| 177 | lsa_position=components->n-2; |
| 178 | |
| 179 | ccn_charbuf_destroy(&name); |
| 180 | |
| 181 | |
| 182 | const unsigned char *comp_ptr1; |
| 183 | size_t comp_size; |
| 184 | for(i=lsa_position+1+offset;i<interest_comps->n-1;i++) |
| 185 | { |
| 186 | res=ccn_name_comp_get(interest_ccnb->buf, interest_comps,i,&comp_ptr1, &comp_size); |
| 187 | len+=1; |
| 188 | len+=(int)comp_size; |
| 189 | } |
| 190 | len++; |
| 191 | |
| 192 | char *neighbor=(char *)malloc(len); |
| 193 | memset(neighbor,0,len); |
| 194 | |
| 195 | for(i=lsa_position+1+offset; i<interest_comps->n-1;i++) |
| 196 | { |
| 197 | res=ccn_name_comp_get(interest_ccnb->buf, interest_comps,i,&comp_ptr1, &comp_size); |
| 198 | memcpy(neighbor+strlen(neighbor),"/",1); |
| 199 | memcpy(neighbor+strlen(neighbor),(char *)comp_ptr1,strlen((char *)comp_ptr1)); |
| 200 | |
| 201 | } |
| 202 | |
| 203 | name_part->name=(char *)malloc(strlen(neighbor)+1); |
| 204 | memset(name_part->name,0,strlen(neighbor)+1); |
| 205 | memcpy(name_part->name,neighbor,strlen(neighbor)+1); |
| 206 | name_part->length=strlen(neighbor)+1; |
| 207 | |
| 208 | // Add 01/31/2013 |
| 209 | free(neighbor); |
| 210 | } |
| 211 | |
| 212 | void |
| 213 | get_host_name_from_command_string(struct name_prefix *name_part,char *nbr_name_uri, int offset) |
| 214 | { |
| 215 | |
| 216 | |
| 217 | |
| 218 | int res,i; |
| 219 | int len=0; |
| 220 | const unsigned char *comp_ptr1; |
| 221 | size_t comp_size; |
| 222 | |
| 223 | struct ccn_charbuf *name=ccn_charbuf_create(); |
| 224 | name = ccn_charbuf_create(); |
| 225 | res = ccn_name_from_uri(name,nbr_name_uri); |
| 226 | if (res < 0) { |
| 227 | fprintf(stderr, "Bad ccn URI: %s\n", nbr_name_uri); |
| 228 | exit(1); |
| 229 | } |
| 230 | |
| 231 | struct ccn_indexbuf cid={0}; |
| 232 | |
| 233 | struct ccn_indexbuf *components=&cid; |
| 234 | ccn_name_split (name, components); |
| 235 | |
| 236 | for(i=components->n-2;i> (0+offset);i--) |
| 237 | { |
| 238 | res=ccn_name_comp_get(name->buf, components,i,&comp_ptr1, &comp_size); |
| 239 | len+=1; |
| 240 | len+=(int)comp_size; |
| 241 | } |
| 242 | len++; |
| 243 | |
| 244 | char *neighbor=(char *)malloc(len); |
| 245 | memset(neighbor,0,len); |
| 246 | |
| 247 | for(i=components->n-2;i> (0+offset);i--) |
| 248 | { |
| 249 | res=ccn_name_comp_get(name->buf, components,i,&comp_ptr1, &comp_size); |
| 250 | if ( i != components->n-2) |
| 251 | memcpy(neighbor+strlen(neighbor),".",1); |
| 252 | memcpy(neighbor+strlen(neighbor),(char *)comp_ptr1,strlen((char *)comp_ptr1)); |
| 253 | |
| 254 | } |
| 255 | |
| 256 | name_part->name=(char *)malloc(strlen(neighbor)+1); |
| 257 | memset(name_part->name,0,strlen(neighbor)+1); |
| 258 | memcpy(name_part->name,neighbor,strlen(neighbor)+1); |
| 259 | name_part->length=strlen(neighbor)+1; |
| 260 | |
| 261 | // 01/31/2013 |
| 262 | free(neighbor); |
| 263 | ccn_charbuf_destroy(&name); |
| 264 | } |
| 265 | |
| 266 | |
| 267 | |
| 268 | void |
| 269 | get_content_by_content_name(char *content_name, unsigned char **content_data) |
| 270 | { |
| 271 | |
| 272 | struct ccn_charbuf *name = NULL; |
| 273 | struct ccn_charbuf *templ = NULL; |
| 274 | struct ccn_charbuf *resultbuf = NULL; |
| 275 | struct ccn_parsed_ContentObject pcobuf = { 0 }; |
| 276 | int res; |
| 277 | int allow_stale = 0; |
| 278 | int content_only = 1; |
| 279 | int scope = -1; |
| 280 | const unsigned char *ptr; |
| 281 | size_t length; |
| 282 | int resolve_version = CCN_V_HIGHEST; |
| 283 | int timeout_ms = 3000; |
| 284 | const unsigned lifetime_default = CCN_INTEREST_LIFETIME_SEC << 12; |
| 285 | unsigned lifetime_l12 = lifetime_default; |
| 286 | int get_flags = 0; |
| 287 | |
| 288 | name = ccn_charbuf_create(); |
| 289 | res = ccn_name_from_uri(name,content_name); |
| 290 | if (res < 0) { |
| 291 | fprintf(stderr, "Bad ccn URI: %s\n", content_name); |
| 292 | exit(1); |
| 293 | } |
| 294 | |
| 295 | if (allow_stale || lifetime_l12 != lifetime_default || scope != -1) { |
| 296 | templ = ccn_charbuf_create(); |
| 297 | ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG); |
| 298 | ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG); |
| 299 | ccn_charbuf_append_closer(templ); /* </Name> */ |
| 300 | if (allow_stale) { |
| 301 | ccn_charbuf_append_tt(templ, CCN_DTAG_AnswerOriginKind, CCN_DTAG); |
| 302 | ccnb_append_number(templ, |
| 303 | CCN_AOK_DEFAULT | CCN_AOK_STALE); |
| 304 | ccn_charbuf_append_closer(templ); /* </AnswerOriginKind> */ |
| 305 | } |
| 306 | if (scope != -1) { |
| 307 | ccnb_tagged_putf(templ, CCN_DTAG_Scope, "%d", scope); |
| 308 | } |
| 309 | if (lifetime_l12 != lifetime_default) { |
| 310 | /* |
| 311 | * Choose the interest lifetime so there are at least 3 |
| 312 | * expressions (in the unsatisfied case). |
| 313 | */ |
| 314 | unsigned char buf[3] = { 0 }; |
| 315 | int i; |
| 316 | for (i = sizeof(buf) - 1; i >= 0; i--, lifetime_l12 >>= 8) |
| 317 | buf[i] = lifetime_l12 & 0xff; |
| 318 | ccnb_append_tagged_blob(templ, CCN_DTAG_InterestLifetime, buf, sizeof(buf)); |
| 319 | } |
| 320 | ccn_charbuf_append_closer(templ); /* </Interest> */ |
| 321 | } |
| 322 | resultbuf = ccn_charbuf_create(); |
| 323 | if (resolve_version != 0) { |
| 324 | res = ccn_resolve_version(nlsr->ccn, name, resolve_version, 500); |
| 325 | if (res >= 0) { |
| 326 | ccn_uri_append(resultbuf, name->buf, name->length, 1); |
| 327 | resultbuf->length = 0; |
| 328 | } |
| 329 | } |
| 330 | res = ccn_get(nlsr->ccn, name, templ, timeout_ms, resultbuf, &pcobuf, NULL, get_flags); |
| 331 | if (res >= 0) { |
| 332 | ptr = resultbuf->buf; |
| 333 | length = resultbuf->length; |
| 334 | if (content_only){ |
| 335 | ccn_content_get_value(ptr, length, &pcobuf, &ptr, &length); |
| 336 | *content_data = (unsigned char *) calloc(length, sizeof(char *)); |
| 337 | memcpy (*content_data, ptr, length); |
| 338 | } |
| 339 | } |
| 340 | ccn_charbuf_destroy(&resultbuf); |
| 341 | ccn_charbuf_destroy(&templ); |
| 342 | ccn_charbuf_destroy(&name); |
| 343 | } |
| 344 | |
| 345 | void |
| 346 | process_incoming_sync_content_lsa( unsigned char *content_data) |
| 347 | { |
| 348 | |
| 349 | |
| 350 | if ( nlsr->debugging ) |
| 351 | printf("process_incoming_sync_content_lsa called \n"); |
| 352 | |
| 353 | char *sep="|"; |
| 354 | char *rem; |
| 355 | char *orig_router; |
| 356 | char *orl; |
| 357 | int orig_router_length; |
| 358 | char *lst; |
| 359 | int ls_type; |
| 360 | char *lsid; |
| 361 | long int ls_id; |
| 362 | char *isvld; |
| 363 | int isValid; |
| 364 | char *num_link; |
| 365 | int no_link; |
| 366 | char *np; |
| 367 | char *np_length; |
| 368 | int name_length; |
| 369 | char *data; |
| 370 | char *orig_time; |
| 371 | |
| 372 | |
| 373 | if ( nlsr->debugging ) |
| 374 | printf("LSA Data \n"); |
| 375 | |
| 376 | if( strlen((char *)content_data ) > 0 ) |
| 377 | { |
| 378 | |
| 379 | orig_router=strtok_r((char *)content_data,sep,&rem); |
| 380 | orl=strtok_r(NULL,sep,&rem); |
| 381 | orig_router_length=atoi(orl); |
| 382 | |
| 383 | if ( nlsr->debugging ) |
| 384 | { |
| 385 | printf(" Orig Router Name : %s\n",orig_router); |
| 386 | printf(" Orig Router Length: %d\n",orig_router_length); |
| 387 | } |
| 388 | |
| 389 | lst=strtok_r(NULL,sep,&rem); |
| 390 | ls_type=atoi(lst); |
| 391 | |
| 392 | if ( nlsr->debugging ) |
| 393 | printf(" LS Type : %d\n",ls_type); |
| 394 | |
| 395 | if ( ls_type == LS_TYPE_NAME ) |
| 396 | { |
| 397 | lsid=strtok_r(NULL,sep,&rem); |
| 398 | ls_id=atoi(lsid); |
| 399 | orig_time=strtok_r(NULL,sep,&rem); |
| 400 | isvld=strtok_r(NULL,sep,&rem); |
| 401 | isValid=atoi(isvld); |
| 402 | np=strtok_r(NULL,sep,&rem); |
| 403 | np_length=strtok_r(NULL,sep,&rem); |
| 404 | name_length=atoi(np_length); |
| 405 | if ( nlsr->debugging ) |
| 406 | { |
| 407 | printf(" LS ID : %ld\n",ls_id); |
| 408 | printf(" isValid : %d\n",isValid); |
| 409 | printf(" Name Prefix : %s\n",np); |
| 410 | printf(" Orig Time : %s\n",orig_time); |
| 411 | printf(" Name Prefix length: %d\n",name_length); |
| 412 | } |
| 413 | |
| 414 | build_and_install_others_name_lsa(orig_router,ls_type,ls_id,orig_time,isValid,np); |
| 415 | |
| 416 | print_name_lsdb(); |
| 417 | |
| 418 | } |
| 419 | else if ( ls_type == LS_TYPE_ADJ ) |
| 420 | { |
| 421 | orig_time=strtok_r(NULL,sep,&rem); |
| 422 | num_link=strtok_r(NULL,sep,&rem); |
| 423 | no_link=atoi(num_link); |
| 424 | data=rem; |
| 425 | |
| 426 | if ( nlsr->debugging ) |
| 427 | { |
| 428 | printf(" Orig Time : %s\n",orig_time); |
| 429 | printf(" No Link : %d\n",no_link); |
| 430 | printf(" Data : %s\n",data); |
| 431 | } |
| 432 | build_and_install_others_adj_lsa(orig_router,ls_type,orig_time,no_link,data); |
| 433 | } |
| 434 | else if ( ls_type == LS_TYPE_COR ) |
| 435 | { |
| 436 | orig_time=strtok_r(NULL,sep,&rem); |
| 437 | char *cor_r=strtok_r(NULL,sep,&rem); |
| 438 | char *cor_theta=strtok_r(NULL,sep,&rem); |
| 439 | |
| 440 | double r, theta; |
| 441 | r=strtof(cor_r,NULL); |
| 442 | theta=strtof(cor_theta,NULL); |
| 443 | |
| 444 | if ( nlsr->debugging ) |
| 445 | { |
| 446 | printf(" Orig Time : %s\n",orig_time); |
| 447 | printf(" Cor R : %f\n",r); |
| 448 | printf(" Cor Theta : %f\n",theta); |
| 449 | } |
| 450 | build_and_install_others_cor_lsa(orig_router,ls_type,orig_time, (double)r, (double)theta); |
| 451 | } |
| 452 | |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | void |
| 457 | process_content_from_sync(struct ccn_charbuf *content_name, struct ccn_indexbuf *components) |
| 458 | { |
| 459 | //int lsa_position; |
| 460 | int res; |
| 461 | size_t comp_size; |
| 462 | char *lst; |
| 463 | char *lsid; |
| 464 | const unsigned char *second_last_comp; |
| 465 | const unsigned char *third_last_comp; |
| 466 | const unsigned char *origtime; |
| 467 | char *sep="."; |
| 468 | char *rem; |
| 469 | char *second_comp_type; |
| 470 | |
| 471 | int ls_type; |
| 472 | long int ls_id=0; |
| 473 | |
| 474 | unsigned char *content_data = NULL; |
| 475 | |
| 476 | char *time_stamp=(char *)malloc(20); |
| 477 | memset(time_stamp,0,20); |
| 478 | get_current_timestamp_micro(time_stamp); |
| 479 | |
| 480 | struct ccn_charbuf *uri = ccn_charbuf_create(); |
| 481 | ccn_uri_append(uri, content_name->buf, content_name->length, 0); |
| 482 | |
| 483 | struct name_prefix *orig_router=(struct name_prefix *)malloc(sizeof(struct name_prefix)); |
| 484 | |
| 485 | |
| 486 | struct ccn_indexbuf cid={0}; |
| 487 | struct ccn_indexbuf *temp_components=&cid; |
| 488 | struct ccn_charbuf *name=ccn_charbuf_create(); |
| 489 | ccn_name_from_uri(name,nlsr->slice_prefix); |
| 490 | ccn_name_split (name, temp_components); |
| 491 | //lsa_position=temp_components->n-2; |
| 492 | ccn_charbuf_destroy(&name); |
| 493 | |
| 494 | |
| 495 | //res=ccn_name_comp_get(content_name->buf, components,lsa_position+1,&lst, &comp_size); |
| 496 | res=ccn_name_comp_get(content_name->buf, components,components->n-2-1,&second_last_comp, &comp_size); |
| 497 | //ls_type=atoi((char *)lst); |
| 498 | |
| 499 | printf("2nd Last Component: %s \n",second_last_comp); |
| 500 | second_comp_type=strtok_r((char *)second_last_comp,sep,&rem); |
| 501 | if ( strcmp( second_comp_type, "lsId" ) == 0 ) |
| 502 | { |
| 503 | lsid=rem; |
| 504 | ls_id=atoi(rem); |
| 505 | res=ccn_name_comp_get(content_name->buf, components,components->n-2-2,&third_last_comp, &comp_size); |
| 506 | lst=strtok_r((char *)third_last_comp,sep,&rem); |
| 507 | lst=rem; |
| 508 | ls_type=atoi(lst); |
| 509 | res=ccn_name_comp_get(content_name->buf, components,components->n-2,&origtime, &comp_size); |
| 510 | ccn_name_chop(content_name,components,-3); |
| 511 | get_name_part(orig_router,content_name,components,0); |
| 512 | |
| 513 | if ( nlsr->debugging ) |
| 514 | printf("Orig Router: %s Ls Type: %d Ls id: %ld Orig Time: %s\n",orig_router->name,ls_type,ls_id,origtime); |
| 515 | |
| 516 | int lsa_life_time=get_time_diff(time_stamp,(char *)origtime); |
| 517 | |
| 518 | 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) ) |
| 519 | { |
| 520 | int is_new_name_lsa=check_is_new_name_lsa(orig_router->name,(char *)lst,(char *)lsid,(char *)origtime); |
| 521 | if ( is_new_name_lsa == 1 ) |
| 522 | { |
| 523 | if ( nlsr->debugging ) |
| 524 | printf("New NAME LSA.....\n"); |
| 525 | get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data); |
| 526 | if ( nlsr->debugging ) |
| 527 | printf("Content Data: %s \n",content_data); |
| 528 | process_incoming_sync_content_lsa(content_data); |
| 529 | } |
| 530 | else |
| 531 | { |
| 532 | if ( nlsr->debugging ) |
| 533 | printf("Name LSA / Newer Name LSA already xists in LSDB\n"); |
| 534 | get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data); |
| 535 | |
| 536 | if ( nlsr->debugging ) |
| 537 | printf("Content Data: %s \n",content_data); |
| 538 | } |
| 539 | } |
| 540 | else |
| 541 | { |
| 542 | if ( nlsr->debugging ) |
| 543 | printf("Lsa is older than Router LSA refresh time/ Dead Interval\n"); |
| 544 | } |
| 545 | } |
| 546 | else |
| 547 | { |
| 548 | ls_type=atoi(rem); |
| 549 | lst=rem; |
| 550 | if(ls_type == LS_TYPE_ADJ) |
| 551 | { |
| 552 | res=ccn_name_comp_get(content_name->buf, components,components->n-2,&origtime, &comp_size); |
| 553 | ccn_name_chop(content_name,components,-2); |
| 554 | get_name_part(orig_router,content_name,components,0); |
| 555 | |
| 556 | if ( nlsr->debugging ) |
| 557 | printf("Orig Router: %s Ls Type: %d Orig Time: %s\n",orig_router->name,ls_type,origtime); |
| 558 | |
| 559 | int lsa_life_time=get_time_diff(time_stamp,(char *)origtime); |
| 560 | 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) ) |
| 561 | { |
| 562 | int is_new_adj_lsa=check_is_new_adj_lsa(orig_router->name,(char *)lst,(char *)origtime); |
| 563 | if ( is_new_adj_lsa == 1 ) |
| 564 | { |
| 565 | if ( nlsr->debugging ) |
| 566 | printf("New Adj LSA.....\n"); |
| 567 | get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data); |
| 568 | |
| 569 | if ( nlsr->debugging ) |
| 570 | printf("Content Data: %s \n",content_data); |
| 571 | process_incoming_sync_content_lsa(content_data); |
| 572 | } |
| 573 | else |
| 574 | { |
| 575 | if ( nlsr->debugging ) |
| 576 | printf("Adj LSA / Newer Adj LSA already exists in LSDB\n"); |
| 577 | get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data); |
| 578 | if ( nlsr->debugging ) |
| 579 | printf("Content Data: %s \n",content_data); |
| 580 | } |
| 581 | } |
| 582 | else |
| 583 | { |
| 584 | if ( nlsr->debugging ) |
| 585 | printf("Lsa is older than Router LSA refresh time/ Dead Interval\n"); |
| 586 | } |
| 587 | } |
| 588 | else if(ls_type == LS_TYPE_COR) |
| 589 | { |
| 590 | res=ccn_name_comp_get(content_name->buf, components,components->n-2,&origtime, &comp_size); |
| 591 | ccn_name_chop(content_name,components,-2); |
| 592 | get_name_part(orig_router,content_name,components,0); |
| 593 | |
| 594 | if ( nlsr->debugging ) |
| 595 | printf("Orig Router: %s Ls Type: %d Orig Time: %s\n",orig_router->name,ls_type,origtime); |
| 596 | |
| 597 | int lsa_life_time=get_time_diff(time_stamp,(char *)origtime); |
| 598 | 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) ) |
| 599 | { |
| 600 | int is_new_cor_lsa=check_is_new_cor_lsa(orig_router->name,(char *)lst,(char *)origtime); |
| 601 | if ( is_new_cor_lsa == 1 ) |
| 602 | { |
| 603 | if ( nlsr->debugging ) |
| 604 | printf("New Cor LSA.....\n"); |
| 605 | get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data); |
| 606 | |
| 607 | if ( nlsr->debugging ) |
| 608 | printf("Content Data: %s \n",content_data); |
| 609 | process_incoming_sync_content_lsa(content_data); |
| 610 | } |
| 611 | else |
| 612 | { |
| 613 | if ( nlsr->debugging ) |
| 614 | printf("Cor LSA / Newer Cor LSA already exists in LSDB\n"); |
| 615 | get_content_by_content_name(ccn_charbuf_as_string(uri), &content_data); |
| 616 | if ( nlsr->debugging ) |
| 617 | printf("Content Data: %s \n",content_data); |
| 618 | } |
| 619 | } |
| 620 | else |
| 621 | { |
| 622 | if ( nlsr->debugging ) |
| 623 | printf("Lsa is older than Router LSA refresh time/ Dead Interval\n"); |
| 624 | } |
| 625 | |
| 626 | } |
| 627 | } |
| 628 | |
akmhoque | 866c222 | 2013-02-12 10:49:33 -0600 | [diff] [blame^] | 629 | //if (content_data != NULL) |
| 630 | //free(content_data); |
akmhoque | a0e7115 | 2013-02-11 09:47:59 -0600 | [diff] [blame] | 631 | ccn_charbuf_destroy(&uri); |
| 632 | //01/31/2013 |
| 633 | free(time_stamp); |
| 634 | } |
| 635 | |
akmhoque | 866c222 | 2013-02-12 10:49:33 -0600 | [diff] [blame^] | 636 | int |
akmhoque | a0e7115 | 2013-02-11 09:47:59 -0600 | [diff] [blame] | 637 | sync_monitor(char *topo_prefix, char *slice_prefix) |
| 638 | { |
| 639 | |
| 640 | static struct ccns_name_closure nc={0}; |
| 641 | |
| 642 | nlsr->closure = &nc; |
| 643 | struct ccn_charbuf *prefix = ccn_charbuf_create(); |
| 644 | struct ccn_charbuf *roothash = NULL; |
| 645 | struct ccn_charbuf *topo = ccn_charbuf_create(); |
| 646 | nlsr->slice = ccns_slice_create(); |
| 647 | ccn_charbuf_reset(prefix); |
| 648 | ccn_charbuf_reset(topo); |
| 649 | |
| 650 | ccn_charbuf_reset(prefix); |
| 651 | ccn_name_from_uri(prefix, slice_prefix); |
| 652 | |
| 653 | ccn_charbuf_reset(topo); |
| 654 | ccn_name_from_uri(topo, topo_prefix); |
| 655 | |
| 656 | |
| 657 | ccns_slice_set_topo_prefix(nlsr->slice, topo, prefix); |
| 658 | nlsr->closure->callback = &sync_cb; |
| 659 | nlsr->ccns = ccns_open(nlsr->ccn, nlsr->slice, nlsr->closure, roothash, NULL); |
| 660 | |
| 661 | //01/31/2013 |
| 662 | ccn_charbuf_destroy(&prefix); |
| 663 | ccn_charbuf_destroy(&topo); |
akmhoque | 866c222 | 2013-02-12 10:49:33 -0600 | [diff] [blame^] | 664 | ccn_charbuf_destroy(&roothash); |
| 665 | return 0; |
akmhoque | a0e7115 | 2013-02-11 09:47:59 -0600 | [diff] [blame] | 666 | } |
| 667 | |
| 668 | struct ccn_charbuf * |
| 669 | make_template(int scope) |
| 670 | { |
| 671 | struct ccn_charbuf *templ = NULL; |
| 672 | templ = ccn_charbuf_create(); |
| 673 | ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG); |
| 674 | ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG); |
| 675 | ccn_charbuf_append_closer(templ); /* </Name> */ |
| 676 | if (0 <= scope && scope <= 2) |
| 677 | ccnb_tagged_putf(templ, CCN_DTAG_Scope, "%d", scope); |
| 678 | ccn_charbuf_append_closer(templ); /* </Interest> */ |
| 679 | return(templ); |
| 680 | } |
| 681 | |
| 682 | int |
| 683 | write_data_to_repo(char *data, char *name_prefix) |
| 684 | { |
| 685 | if ( nlsr->debugging ) |
| 686 | { |
| 687 | printf("write_data_to_repo called\n"); |
| 688 | printf("Content Name: %s \n",name_prefix); |
| 689 | printf("Content Data: %s \n",data); |
| 690 | } |
| 691 | |
| 692 | struct ccn *temp_ccn; |
| 693 | temp_ccn=ccn_create(); |
| 694 | int ccn_fd=ccn_connect(temp_ccn, NULL); |
| 695 | if(ccn_fd == -1) |
| 696 | { |
| 697 | fprintf(stderr,"Could not connect to ccnd for Data Writing\n"); |
| 698 | writeLogg(__FILE__,__FUNCTION__,__LINE__,"Could not connect to ccnd for Data Writing\n"); |
| 699 | return -1; |
| 700 | } |
| 701 | struct ccn_charbuf *name = NULL; |
| 702 | struct ccn_seqwriter *w = NULL; |
| 703 | int blocksize = 4096; |
| 704 | int freshness = -1; |
| 705 | int torepo = 1; |
| 706 | int scope = 1; |
| 707 | int res; |
| 708 | size_t blockread; |
| 709 | struct ccn_charbuf *templ; |
| 710 | |
| 711 | name = ccn_charbuf_create(); |
| 712 | res = ccn_name_from_uri(name, name_prefix); |
| 713 | if (res < 0) { |
| 714 | fprintf(stderr, "bad CCN URI: %s\n",name_prefix); |
| 715 | return -1; |
| 716 | } |
| 717 | |
| 718 | |
| 719 | w = ccn_seqw_create(temp_ccn, name); |
| 720 | if (w == NULL) { |
| 721 | fprintf(stderr, "ccn_seqw_create failed\n"); |
| 722 | return -1; |
| 723 | } |
| 724 | ccn_seqw_set_block_limits(w, blocksize, blocksize); |
| 725 | if (freshness > -1) |
| 726 | ccn_seqw_set_freshness(w, freshness); |
| 727 | if (torepo) { |
| 728 | struct ccn_charbuf *name_v = ccn_charbuf_create(); |
| 729 | ccn_seqw_get_name(w, name_v); |
| 730 | ccn_name_from_uri(name_v, "%C1.R.sw"); |
| 731 | ccn_name_append_nonce(name_v); |
| 732 | templ = make_template(scope); |
| 733 | res = ccn_get(temp_ccn, name_v, templ, 60000, NULL, NULL, NULL, 0); |
| 734 | ccn_charbuf_destroy(&templ); |
| 735 | ccn_charbuf_destroy(&name_v); |
| 736 | if (res < 0) { |
| 737 | fprintf(stderr, "No response from repository\n"); |
| 738 | return -1; |
| 739 | } |
| 740 | } |
| 741 | |
| 742 | |
| 743 | |
| 744 | |
| 745 | blockread = 0; |
| 746 | |
| 747 | |
| 748 | blockread=strlen(data); |
| 749 | |
| 750 | if (blockread > 0) { |
| 751 | ccn_run(temp_ccn, 100); |
| 752 | res = ccn_seqw_write(w, data, blockread); |
| 753 | while (res == -1) { |
| 754 | ccn_run(temp_ccn, 100); |
| 755 | res = ccn_seqw_write(w, data, blockread); |
| 756 | } |
| 757 | } |
| 758 | |
| 759 | ccn_seqw_close(w); |
| 760 | ccn_run(temp_ccn, 100); |
| 761 | ccn_charbuf_destroy(&name); |
| 762 | ccn_destroy(&temp_ccn); |
| 763 | |
| 764 | return 0; |
| 765 | } |
| 766 | |
| 767 | |
| 768 | int |
| 769 | create_sync_slice(char *topo_prefix, char *slice_prefix) |
| 770 | { |
| 771 | int res; |
akmhoque | 866c222 | 2013-02-12 10:49:33 -0600 | [diff] [blame^] | 772 | struct ccn *handle; |
akmhoque | a0e7115 | 2013-02-11 09:47:59 -0600 | [diff] [blame] | 773 | struct ccns_slice *slice; |
| 774 | struct ccn_charbuf *prefix = ccn_charbuf_create(); |
| 775 | struct ccn_charbuf *topo = ccn_charbuf_create(); |
| 776 | struct ccn_charbuf *clause = ccn_charbuf_create(); |
| 777 | struct ccn_charbuf *slice_name = ccn_charbuf_create(); |
| 778 | struct ccn_charbuf *slice_uri = ccn_charbuf_create(); |
| 779 | |
| 780 | if (prefix == NULL || topo == NULL || clause == NULL || |
| 781 | slice_name == NULL || slice_uri == NULL) { |
| 782 | fprintf(stderr, "Unable to allocate required memory.\n"); |
| 783 | return -1; |
| 784 | } |
| 785 | |
akmhoque | 866c222 | 2013-02-12 10:49:33 -0600 | [diff] [blame^] | 786 | handle = ccn_create(); |
| 787 | res = ccn_connect(handle, NULL); |
| 788 | if (0 > res) { |
| 789 | fprintf(stderr, "Unable to connect to ccnd.\n"); |
| 790 | return -1; |
| 791 | } |
| 792 | |
akmhoque | a0e7115 | 2013-02-11 09:47:59 -0600 | [diff] [blame] | 793 | slice = ccns_slice_create(); |
| 794 | |
| 795 | ccn_charbuf_reset(topo); |
| 796 | ccn_name_from_uri(topo, topo_prefix); |
| 797 | ccn_charbuf_reset(prefix); |
| 798 | ccn_name_from_uri(prefix,slice_prefix ); |
| 799 | ccns_slice_set_topo_prefix(slice, topo, prefix); |
| 800 | |
| 801 | |
akmhoque | 866c222 | 2013-02-12 10:49:33 -0600 | [diff] [blame^] | 802 | res = ccns_write_slice(handle, slice, slice_name); |
akmhoque | a0e7115 | 2013-02-11 09:47:59 -0600 | [diff] [blame] | 803 | |
| 804 | //01/31/2013 |
| 805 | ccns_slice_destroy(&slice); |
akmhoque | 866c222 | 2013-02-12 10:49:33 -0600 | [diff] [blame^] | 806 | ccn_destroy(&handle); |
akmhoque | a0e7115 | 2013-02-11 09:47:59 -0600 | [diff] [blame] | 807 | ccn_charbuf_destroy(&prefix); |
| 808 | ccn_charbuf_destroy(&topo); |
| 809 | ccn_charbuf_destroy(&clause); |
| 810 | ccn_charbuf_destroy(&slice_name); |
| 811 | ccn_charbuf_destroy(&slice_uri); |
| 812 | |
| 813 | return 0; |
| 814 | } |
| 815 | |