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 | |
| 129 | |
| 130 | void |
| 131 | print_npt(void) |
| 132 | { |
| 133 | printf("\n"); |
| 134 | printf("print_npt called\n\n"); |
| 135 | int i, npt_element; |
| 136 | |
| 137 | struct npt_entry *ne; |
| 138 | |
| 139 | struct hashtb_enumerator ee; |
| 140 | struct hashtb_enumerator *e = ⅇ |
| 141 | |
| 142 | hashtb_start(nlsr->npt, e); |
| 143 | npt_element=hashtb_n(nlsr->npt); |
| 144 | |
| 145 | for(i=0;i<npt_element;i++) |
| 146 | { |
| 147 | printf("\n"); |
| 148 | printf("----------NPT ENTRY %d------------------\n",i+1); |
| 149 | ne=e->data; |
akmhoque | 810a5b5 | 2012-09-09 16:53:14 -0500 | [diff] [blame] | 150 | printf(" Origination Router: %s \n",ne->orig_router); |
| 151 | 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] | 152 | |
akmhoque | 810a5b5 | 2012-09-09 16:53:14 -0500 | [diff] [blame] | 153 | int j, nl_element; |
| 154 | struct name_list_entry *nle; |
| 155 | struct hashtb_enumerator eenle; |
| 156 | struct hashtb_enumerator *enle = &eenle; |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 157 | |
akmhoque | 810a5b5 | 2012-09-09 16:53:14 -0500 | [diff] [blame] | 158 | hashtb_start(ne->name_list, enle); |
| 159 | nl_element=hashtb_n(ne->name_list); |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 160 | |
akmhoque | 810a5b5 | 2012-09-09 16:53:14 -0500 | [diff] [blame] | 161 | for (j=0;j<nl_element;j++) |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 162 | { |
akmhoque | 810a5b5 | 2012-09-09 16:53:14 -0500 | [diff] [blame] | 163 | nle=enle->data; |
| 164 | printf(" Name Prefix: %s \n",nle->name); |
| 165 | hashtb_next(enle); |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 166 | } |
akmhoque | 810a5b5 | 2012-09-09 16:53:14 -0500 | [diff] [blame] | 167 | hashtb_end(enle); |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 168 | |
| 169 | hashtb_next(e); |
| 170 | } |
| 171 | |
| 172 | hashtb_end(e); |
| 173 | |
| 174 | printf("\n"); |
| 175 | } |
akmhoque | 810a5b5 | 2012-09-09 16:53:14 -0500 | [diff] [blame] | 176 | |
| 177 | void |
| 178 | delete_orig_router_from_npt(char *orig_router,int next_hop_face) |
| 179 | { |
| 180 | int res; |
| 181 | struct npt_entry *ne; |
| 182 | |
| 183 | struct hashtb_enumerator ee; |
| 184 | struct hashtb_enumerator *e = ⅇ |
| 185 | |
| 186 | hashtb_start(nlsr->npt, e); |
| 187 | res = hashtb_seek(e, orig_router, strlen(orig_router), 0); |
| 188 | |
| 189 | if ( res == HT_OLD_ENTRY ) |
| 190 | { |
| 191 | ne=e->data; |
| 192 | if ( next_hop_face == ne->next_hop_face ) |
| 193 | { |
| 194 | if ( next_hop_face != NO_NEXT_HOP ) |
| 195 | { |
| 196 | int j, nl_element; |
| 197 | struct name_list_entry *nle; |
| 198 | struct hashtb_enumerator eenle; |
| 199 | struct hashtb_enumerator *enle = &eenle; |
| 200 | |
| 201 | hashtb_start(ne->name_list, enle); |
| 202 | nl_element=hashtb_n(ne->name_list); |
| 203 | |
| 204 | for (j=0;j<nl_element;j++) |
| 205 | { |
| 206 | nle=enle->data; |
| 207 | add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_UNREG , next_hop_face); |
| 208 | hashtb_next(enle); |
| 209 | } |
| 210 | hashtb_end(enle); |
| 211 | |
| 212 | } |
| 213 | |
| 214 | } |
| 215 | hashtb_destroy(&ne->name_list); |
| 216 | hashtb_delete(e); |
| 217 | } |
| 218 | else if ( res == HT_NEW_ENTRY ) |
| 219 | { |
| 220 | hashtb_delete(e); |
| 221 | } |
| 222 | hashtb_end(e); |
| 223 | } |
akmhoque | fbfd098 | 2012-09-09 20:59:03 -0500 | [diff] [blame] | 224 | |
| 225 | |
| 226 | void |
| 227 | update_npt_with_new_route(char * orig_router,int next_hop_face) |
| 228 | { |
| 229 | int res; |
| 230 | struct npt_entry *ne; |
| 231 | |
| 232 | struct hashtb_enumerator ee; |
| 233 | struct hashtb_enumerator *e = ⅇ |
| 234 | |
| 235 | hashtb_start(nlsr->npt, e); |
| 236 | res = hashtb_seek(e, orig_router, strlen(orig_router), 0); |
| 237 | |
| 238 | if ( res == HT_OLD_ENTRY ) |
| 239 | { |
| 240 | ne=e->data; |
| 241 | |
| 242 | if ( next_hop_face != ne->next_hop_face ) |
| 243 | { |
| 244 | int j, nl_element; |
| 245 | struct name_list_entry *nle; |
| 246 | struct hashtb_enumerator eenle; |
| 247 | struct hashtb_enumerator *enle = &eenle; |
| 248 | |
| 249 | hashtb_start(ne->name_list, enle); |
| 250 | nl_element=hashtb_n(ne->name_list); |
| 251 | |
| 252 | for (j=0;j<nl_element;j++) |
| 253 | { |
| 254 | nle=enle->data; |
| 255 | if (ne->next_hop_face != NO_FACE ) |
| 256 | { |
| 257 | add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_UNREG , ne->next_hop_face); |
| 258 | } |
| 259 | if (next_hop_face != NO_FACE ) |
| 260 | { |
| 261 | add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_REG , next_hop_face); |
| 262 | } |
| 263 | hashtb_next(enle); |
| 264 | } |
| 265 | hashtb_end(enle); |
| 266 | ne->next_hop_face=next_hop_face; |
| 267 | } |
| 268 | |
| 269 | } |
| 270 | else if ( res == HT_NEW_ENTRY ) |
| 271 | { |
| 272 | hashtb_delete(e); |
| 273 | } |
| 274 | hashtb_end(e); |
| 275 | } |
| 276 | |
| 277 | void |
| 278 | destroy_all_face_by_nlsr(void) |
| 279 | { |
| 280 | int i, npt_element; |
| 281 | |
| 282 | struct npt_entry *ne; |
| 283 | |
| 284 | struct hashtb_enumerator ee; |
| 285 | struct hashtb_enumerator *e = ⅇ |
| 286 | |
| 287 | hashtb_start(nlsr->npt, e); |
| 288 | npt_element=hashtb_n(nlsr->npt); |
| 289 | |
| 290 | for(i=0;i<npt_element;i++) |
| 291 | { |
| 292 | //printf("\n"); |
| 293 | //printf("----------NPT ENTRY %d------------------\n",i+1); |
| 294 | ne=e->data; |
| 295 | //printf(" Origination Router: %s \n",ne->orig_router); |
| 296 | //ne->next_hop_face == NO_FACE ? printf(" Next Hop Face: NO_NEXT_HOP \n") : printf(" Next Hop Face: %d \n", ne->next_hop_face); |
| 297 | |
| 298 | int j, nl_element; |
| 299 | struct name_list_entry *nle; |
| 300 | struct hashtb_enumerator eenle; |
| 301 | struct hashtb_enumerator *enle = &eenle; |
| 302 | |
| 303 | hashtb_start(ne->name_list, enle); |
| 304 | nl_element=hashtb_n(ne->name_list); |
| 305 | |
| 306 | for (j=0;j<nl_element;j++) |
| 307 | { |
| 308 | nle=enle->data; |
| 309 | if ( ne->next_hop_face != NO_FACE) |
| 310 | { |
| 311 | add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nle->name, OP_UNREG , ne->next_hop_face); |
| 312 | } |
| 313 | //printf(" Name Prefix: %s \n",nle->name); |
| 314 | hashtb_next(enle); |
| 315 | } |
| 316 | hashtb_end(enle); |
| 317 | |
| 318 | hashtb_next(e); |
| 319 | } |
| 320 | |
| 321 | hashtb_end(e); |
| 322 | |
| 323 | printf("\n"); |
| 324 | } |