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