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