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