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