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