akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [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 <assert.h> |
| 8 | #ifdef HAVE_CONFIG_H |
| 9 | #include <config.h> |
| 10 | #endif |
| 11 | #include <sys/types.h> |
| 12 | #include <signal.h> |
| 13 | |
| 14 | |
| 15 | |
| 16 | #include <ccn/ccn.h> |
| 17 | #include <ccn/uri.h> |
| 18 | #include <ccn/keystore.h> |
| 19 | #include <ccn/signing.h> |
| 20 | #include <ccn/schedule.h> |
| 21 | #include <ccn/hashtb.h> |
| 22 | |
| 23 | #include "nlsr.h" |
| 24 | #include "nlsr_npt.h" |
| 25 | #include "nlsr_fib.h" |
akmhoque | 810a5b5 | 2012-09-09 16:53:14 -0500 | [diff] [blame] | 26 | #include "nlsr_route.h" |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 27 | |
| 28 | int |
| 29 | add_npt_entry(char *orig_router, char *name_prefix, int face) |
| 30 | { |
| 31 | if ( strcmp(orig_router,nlsr->router_name)== 0) |
| 32 | { |
| 33 | return -1; |
| 34 | } |
| 35 | |
| 36 | struct npt_entry *ne=(struct npt_entry*)malloc(sizeof(struct npt_entry )); |
| 37 | |
akmhoque | 810a5b5 | 2012-09-09 16:53:14 -0500 | [diff] [blame] | 38 | int res,res_nle; |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 39 | struct hashtb_enumerator ee; |
| 40 | struct hashtb_enumerator *e = ⅇ |
akmhoque | 810a5b5 | 2012-09-09 16:53:14 -0500 | [diff] [blame] | 41 | |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 42 | |
| 43 | hashtb_start(nlsr->npt, e); |
akmhoque | 810a5b5 | 2012-09-09 16:53:14 -0500 | [diff] [blame] | 44 | res = hashtb_seek(e, orig_router, strlen(orig_router), 0); |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 45 | |
| 46 | if(res == HT_NEW_ENTRY) |
| 47 | { |
| 48 | ne=e->data; |
| 49 | |
akmhoque | 810a5b5 | 2012-09-09 16:53:14 -0500 | [diff] [blame] | 50 | ne->orig_router=(char *)malloc(strlen(orig_router)+1); |
| 51 | memset(ne->orig_router,0,strlen(orig_router)+1); |
| 52 | memcpy(ne->orig_router,orig_router,strlen(orig_router)); |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 53 | |
akmhoque | 810a5b5 | 2012-09-09 16:53:14 -0500 | [diff] [blame] | 54 | |
| 55 | struct name_list_entry *nle=(struct name_list_entry *)malloc(sizeof(struct name_list_entry)); |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 56 | |
akmhoque | 810a5b5 | 2012-09-09 16:53:14 -0500 | [diff] [blame] | 57 | struct hashtb_param param_nle = {0}; |
| 58 | ne->name_list= hashtb_create(sizeof(struct name_list_entry ), ¶m_nle); |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 59 | |
akmhoque | 810a5b5 | 2012-09-09 16:53:14 -0500 | [diff] [blame] | 60 | struct hashtb_enumerator eenle; |
| 61 | struct hashtb_enumerator *enle = &eenle; |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 62 | |
akmhoque | 810a5b5 | 2012-09-09 16:53:14 -0500 | [diff] [blame] | 63 | hashtb_start(ne->name_list, enle); |
| 64 | res_nle = hashtb_seek(enle, name_prefix, strlen(name_prefix), 0); |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 65 | |
akmhoque | 810a5b5 | 2012-09-09 16:53:14 -0500 | [diff] [blame] | 66 | if(res_nle == HT_NEW_ENTRY ) |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 67 | { |
akmhoque | 810a5b5 | 2012-09-09 16:53:14 -0500 | [diff] [blame] | 68 | nle=enle->data; |
| 69 | nle->name=(char *)malloc(strlen(name_prefix)+1); |
| 70 | memset(nle->name,0,strlen(name_prefix)+1); |
| 71 | memcpy(nle->name,name_prefix,strlen(name_prefix)); |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 72 | |
akmhoque | 810a5b5 | 2012-09-09 16:53:14 -0500 | [diff] [blame] | 73 | |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 74 | |
| 75 | } |
akmhoque | 810a5b5 | 2012-09-09 16:53:14 -0500 | [diff] [blame] | 76 | hashtb_end(enle); |
| 77 | |
| 78 | ne->next_hop_face=face; |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 79 | |
| 80 | if ( face != NO_FACE ) |
| 81 | { |
| 82 | add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)name_prefix, OP_REG, face); |
| 83 | } |
| 84 | |
| 85 | } |
| 86 | else if (res == HT_OLD_ENTRY) |
| 87 | { |
| 88 | free(ne); |
| 89 | struct npt_entry *one; |
| 90 | |
| 91 | one=e->data; |
| 92 | |
akmhoque | 810a5b5 | 2012-09-09 16:53:14 -0500 | [diff] [blame] | 93 | struct name_list_entry *nle=(struct name_list_entry *)malloc(sizeof(struct name_list_entry)); |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 94 | |
akmhoque | 810a5b5 | 2012-09-09 16:53:14 -0500 | [diff] [blame] | 95 | struct hashtb_param param_nle = {0}; |
| 96 | ne->name_list= hashtb_create(sizeof(struct name_list_entry ), ¶m_nle); |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 97 | |
akmhoque | 810a5b5 | 2012-09-09 16:53:14 -0500 | [diff] [blame] | 98 | struct hashtb_enumerator eenle; |
| 99 | struct hashtb_enumerator *enle = &eenle; |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 100 | |
akmhoque | 810a5b5 | 2012-09-09 16:53:14 -0500 | [diff] [blame] | 101 | hashtb_start(one->name_list, enle); |
| 102 | res_nle = hashtb_seek(enle, name_prefix, strlen(name_prefix), 0); |
| 103 | |
| 104 | if(res_nle == HT_NEW_ENTRY ) |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 105 | { |
akmhoque | 810a5b5 | 2012-09-09 16:53:14 -0500 | [diff] [blame] | 106 | nle=enle->data; |
| 107 | nle->name=(char *)malloc(strlen(name_prefix)+1); |
| 108 | memset(nle->name,0,strlen(name_prefix)+1); |
| 109 | memcpy(nle->name,name_prefix,strlen(name_prefix)); |
| 110 | |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 111 | if ( face != NO_FACE ) |
| 112 | { |
| 113 | add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)name_prefix, OP_REG, face); |
| 114 | } |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 115 | } |
akmhoque | 810a5b5 | 2012-09-09 16:53:14 -0500 | [diff] [blame] | 116 | else if(res_nle == HT_OLD_ENTRY ) |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 117 | { |
akmhoque | 810a5b5 | 2012-09-09 16:53:14 -0500 | [diff] [blame] | 118 | free(nle); |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 119 | } |
akmhoque | 810a5b5 | 2012-09-09 16:53:14 -0500 | [diff] [blame] | 120 | hashtb_end(enle); |
| 121 | |
| 122 | |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 123 | |
| 124 | } |
| 125 | hashtb_end(e); |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 126 | return res; |
| 127 | } |
| 128 | |
akmhoque | ffacaa8 | 2012-09-13 17:48:30 -0500 | [diff] [blame] | 129 | int |
| 130 | delete_npt_entry(char *orig_router, char *name_prefix) |
| 131 | { |
| 132 | if ( strcmp(orig_router,nlsr->router_name)== 0) |
| 133 | { |
| 134 | return -1; |
| 135 | } |
| 136 | |
| 137 | struct npt_entry *ne; |
| 138 | |
| 139 | int res,res_nle; |
| 140 | struct hashtb_enumerator ee; |
| 141 | struct hashtb_enumerator *e = ⅇ |
| 142 | |
| 143 | |
| 144 | hashtb_start(nlsr->npt, e); |
| 145 | res = hashtb_seek(e, orig_router, strlen(orig_router), 0); |
| 146 | |
| 147 | if(res == HT_NEW_ENTRY) |
| 148 | { |
| 149 | hashtb_delete(e); |
| 150 | return -1; |
| 151 | } |
| 152 | else if (res == HT_OLD_ENTRY) |
| 153 | { |
| 154 | ne=e->data; |
| 155 | |
| 156 | struct hashtb_enumerator eenle; |
| 157 | struct hashtb_enumerator *enle = &eenle; |
| 158 | |
| 159 | hashtb_start(ne->name_list, enle); |
| 160 | res_nle = hashtb_seek(enle, name_prefix, strlen(name_prefix), 0); |
| 161 | |
| 162 | if(res_nle == HT_NEW_ENTRY ) |
| 163 | { |
| 164 | hashtb_delete(enle); |
| 165 | } |
| 166 | else if(res_nle == HT_OLD_ENTRY ) |
| 167 | { |
| 168 | if (ne->next_hop_face != NO_FACE ) |
| 169 | { |
| 170 | add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)name_prefix, OP_UNREG, ne->next_hop_face); |
| 171 | } |
| 172 | hashtb_delete(enle); |
| 173 | } |
| 174 | |
| 175 | hashtb_end(enle); |
akmhoque | da5b683 | 2012-09-13 22:33:55 -0500 | [diff] [blame] | 176 | |
| 177 | if ( hashtb_n(ne->name_list) == 0 ) |
| 178 | { |
| 179 | hashtb_delete(e); |
| 180 | } |
akmhoque | ffacaa8 | 2012-09-13 17:48:30 -0500 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | hashtb_end(e); |
| 184 | |
| 185 | return 0; |
| 186 | } |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 187 | |
| 188 | void |
| 189 | print_npt(void) |
| 190 | { |
| 191 | printf("\n"); |
| 192 | printf("print_npt called\n\n"); |
| 193 | int i, npt_element; |
| 194 | |
| 195 | struct npt_entry *ne; |
| 196 | |
| 197 | struct hashtb_enumerator ee; |
| 198 | struct hashtb_enumerator *e = ⅇ |
| 199 | |
| 200 | hashtb_start(nlsr->npt, e); |
| 201 | npt_element=hashtb_n(nlsr->npt); |
| 202 | |
| 203 | for(i=0;i<npt_element;i++) |
| 204 | { |
| 205 | printf("\n"); |
| 206 | printf("----------NPT ENTRY %d------------------\n",i+1); |
| 207 | ne=e->data; |
akmhoque | 810a5b5 | 2012-09-09 16:53:14 -0500 | [diff] [blame] | 208 | printf(" Origination Router: %s \n",ne->orig_router); |
| 209 | ne->next_hop_face == NO_FACE ? printf(" Next Hop Face: NO_NEXT_HOP \n") : printf(" Next Hop Face: %d \n", ne->next_hop_face); |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 210 | |
akmhoque | 810a5b5 | 2012-09-09 16:53:14 -0500 | [diff] [blame] | 211 | int j, nl_element; |
| 212 | struct name_list_entry *nle; |
| 213 | struct hashtb_enumerator eenle; |
| 214 | struct hashtb_enumerator *enle = &eenle; |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 215 | |
akmhoque | 810a5b5 | 2012-09-09 16:53:14 -0500 | [diff] [blame] | 216 | hashtb_start(ne->name_list, enle); |
| 217 | nl_element=hashtb_n(ne->name_list); |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 218 | |
akmhoque | 810a5b5 | 2012-09-09 16:53:14 -0500 | [diff] [blame] | 219 | for (j=0;j<nl_element;j++) |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 220 | { |
akmhoque | 810a5b5 | 2012-09-09 16:53:14 -0500 | [diff] [blame] | 221 | nle=enle->data; |
| 222 | printf(" Name Prefix: %s \n",nle->name); |
| 223 | hashtb_next(enle); |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 224 | } |
akmhoque | 810a5b5 | 2012-09-09 16:53:14 -0500 | [diff] [blame] | 225 | hashtb_end(enle); |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 226 | |
| 227 | hashtb_next(e); |
| 228 | } |
| 229 | |
| 230 | hashtb_end(e); |
| 231 | |
| 232 | printf("\n"); |
| 233 | } |
akmhoque | 810a5b5 | 2012-09-09 16:53:14 -0500 | [diff] [blame] | 234 | |
| 235 | void |
| 236 | delete_orig_router_from_npt(char *orig_router,int next_hop_face) |
| 237 | { |
| 238 | int res; |
| 239 | struct npt_entry *ne; |
| 240 | |
| 241 | struct hashtb_enumerator ee; |
| 242 | struct hashtb_enumerator *e = ⅇ |
| 243 | |
| 244 | hashtb_start(nlsr->npt, e); |
| 245 | res = hashtb_seek(e, orig_router, strlen(orig_router), 0); |
| 246 | |
| 247 | if ( res == HT_OLD_ENTRY ) |
| 248 | { |
| 249 | ne=e->data; |
| 250 | if ( next_hop_face == ne->next_hop_face ) |
| 251 | { |
| 252 | if ( next_hop_face != NO_NEXT_HOP ) |
| 253 | { |
| 254 | int j, nl_element; |
| 255 | struct name_list_entry *nle; |
| 256 | struct hashtb_enumerator eenle; |
| 257 | struct hashtb_enumerator *enle = &eenle; |
| 258 | |
| 259 | hashtb_start(ne->name_list, enle); |
| 260 | nl_element=hashtb_n(ne->name_list); |
| 261 | |
| 262 | for (j=0;j<nl_element;j++) |
| 263 | { |
| 264 | nle=enle->data; |
| 265 | add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_UNREG , next_hop_face); |
| 266 | hashtb_next(enle); |
| 267 | } |
| 268 | hashtb_end(enle); |
| 269 | |
| 270 | } |
| 271 | |
| 272 | } |
| 273 | hashtb_destroy(&ne->name_list); |
| 274 | hashtb_delete(e); |
| 275 | } |
| 276 | else if ( res == HT_NEW_ENTRY ) |
| 277 | { |
| 278 | hashtb_delete(e); |
| 279 | } |
| 280 | hashtb_end(e); |
| 281 | } |
akmhoque | fbfd098 | 2012-09-09 20:59:03 -0500 | [diff] [blame] | 282 | |
| 283 | |
| 284 | void |
| 285 | update_npt_with_new_route(char * orig_router,int next_hop_face) |
| 286 | { |
| 287 | int res; |
| 288 | struct npt_entry *ne; |
| 289 | |
| 290 | struct hashtb_enumerator ee; |
| 291 | struct hashtb_enumerator *e = ⅇ |
| 292 | |
| 293 | hashtb_start(nlsr->npt, e); |
| 294 | res = hashtb_seek(e, orig_router, strlen(orig_router), 0); |
| 295 | |
| 296 | if ( res == HT_OLD_ENTRY ) |
| 297 | { |
| 298 | ne=e->data; |
| 299 | |
| 300 | if ( next_hop_face != ne->next_hop_face ) |
| 301 | { |
| 302 | int j, nl_element; |
| 303 | struct name_list_entry *nle; |
| 304 | struct hashtb_enumerator eenle; |
| 305 | struct hashtb_enumerator *enle = &eenle; |
| 306 | |
| 307 | hashtb_start(ne->name_list, enle); |
| 308 | nl_element=hashtb_n(ne->name_list); |
| 309 | |
| 310 | for (j=0;j<nl_element;j++) |
| 311 | { |
| 312 | nle=enle->data; |
| 313 | if (ne->next_hop_face != NO_FACE ) |
| 314 | { |
| 315 | add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_UNREG , ne->next_hop_face); |
| 316 | } |
| 317 | if (next_hop_face != NO_FACE ) |
| 318 | { |
| 319 | add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_REG , next_hop_face); |
| 320 | } |
| 321 | hashtb_next(enle); |
| 322 | } |
| 323 | hashtb_end(enle); |
| 324 | ne->next_hop_face=next_hop_face; |
| 325 | } |
| 326 | |
| 327 | } |
| 328 | else if ( res == HT_NEW_ENTRY ) |
| 329 | { |
| 330 | hashtb_delete(e); |
| 331 | } |
| 332 | hashtb_end(e); |
| 333 | } |
| 334 | |
| 335 | void |
| 336 | destroy_all_face_by_nlsr(void) |
| 337 | { |
| 338 | int i, npt_element; |
| 339 | |
| 340 | struct npt_entry *ne; |
| 341 | |
| 342 | struct hashtb_enumerator ee; |
| 343 | struct hashtb_enumerator *e = ⅇ |
| 344 | |
| 345 | hashtb_start(nlsr->npt, e); |
| 346 | npt_element=hashtb_n(nlsr->npt); |
| 347 | |
| 348 | for(i=0;i<npt_element;i++) |
| 349 | { |
akmhoque | fbfd098 | 2012-09-09 20:59:03 -0500 | [diff] [blame] | 350 | ne=e->data; |
akmhoque | fbfd098 | 2012-09-09 20:59:03 -0500 | [diff] [blame] | 351 | |
| 352 | int j, nl_element; |
| 353 | struct name_list_entry *nle; |
| 354 | struct hashtb_enumerator eenle; |
| 355 | struct hashtb_enumerator *enle = &eenle; |
| 356 | |
| 357 | hashtb_start(ne->name_list, enle); |
| 358 | nl_element=hashtb_n(ne->name_list); |
| 359 | |
| 360 | for (j=0;j<nl_element;j++) |
| 361 | { |
| 362 | nle=enle->data; |
| 363 | if ( ne->next_hop_face != NO_FACE) |
| 364 | { |
| 365 | add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_UNREG , ne->next_hop_face); |
| 366 | } |
akmhoque | ffacaa8 | 2012-09-13 17:48:30 -0500 | [diff] [blame] | 367 | //printf(" Name Prefix: %s Face: %d \n",nle->name,ne->next_hop_face); |
akmhoque | fbfd098 | 2012-09-09 20:59:03 -0500 | [diff] [blame] | 368 | hashtb_next(enle); |
| 369 | } |
| 370 | hashtb_end(enle); |
| 371 | |
| 372 | hashtb_next(e); |
| 373 | } |
| 374 | |
| 375 | hashtb_end(e); |
| 376 | |
| 377 | printf("\n"); |
| 378 | } |