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