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