akmhoque | f1161eb | 2012-08-21 09:37:21 -0400 | [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 | |
| 12 | |
| 13 | #include <ccn/ccn.h> |
| 14 | #include <ccn/uri.h> |
| 15 | #include <ccn/keystore.h> |
| 16 | #include <ccn/signing.h> |
| 17 | #include <ccn/schedule.h> |
| 18 | #include <ccn/hashtb.h> |
| 19 | |
| 20 | #include "nlsr.h" |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 21 | #include "nlsr_ndn.h" |
akmhoque | f1161eb | 2012-08-21 09:37:21 -0400 | [diff] [blame] | 22 | #include "nlsr_lsdb.h" |
akmhoque | f71d908 | 2012-08-22 12:51:53 -0400 | [diff] [blame] | 23 | #include "utility.h" |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 24 | #include "nlsr_npl.h" |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 25 | #include "nlsr_adl.h" |
akmhoque | 29c1db5 | 2012-09-07 14:47:43 -0500 | [diff] [blame] | 26 | #include "nlsr_route.h" |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 27 | #include "nlsr_npt.h" |
akmhoque | 438b07e | 2012-08-21 10:13:57 -0400 | [diff] [blame] | 28 | |
akmhoque | f71d908 | 2012-08-22 12:51:53 -0400 | [diff] [blame] | 29 | void |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 30 | set_new_lsdb_version(void) |
akmhoque | f71d908 | 2012-08-22 12:51:53 -0400 | [diff] [blame] | 31 | { |
akmhoque | f71d908 | 2012-08-22 12:51:53 -0400 | [diff] [blame] | 32 | |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 33 | char *time_stamp=(char *)malloc(20); |
| 34 | memset(time_stamp,0,20); |
| 35 | get_current_timestamp_micro(time_stamp); |
akmhoque | f71d908 | 2012-08-22 12:51:53 -0400 | [diff] [blame] | 36 | |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 37 | free(nlsr->lsdb->lsdb_version); |
| 38 | nlsr->lsdb->lsdb_version=(char *)malloc(strlen(time_stamp)+1); |
| 39 | memset(nlsr->lsdb->lsdb_version,0,strlen(time_stamp)+1); |
| 40 | memcpy(nlsr->lsdb->lsdb_version,time_stamp,strlen(time_stamp)+1); |
| 41 | |
| 42 | free(time_stamp); |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 43 | |
akmhoque | f71d908 | 2012-08-22 12:51:53 -0400 | [diff] [blame] | 44 | } |
| 45 | |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 46 | void |
| 47 | make_name_lsa_key(char *key, char *orig_router, int ls_type, long int ls_id) |
| 48 | { |
| 49 | |
| 50 | |
| 51 | printf("Orig Router: %s LS Type: %d LS Id: %ld\n",orig_router,ls_type,ls_id); |
| 52 | |
| 53 | char lst[2]; |
| 54 | memset(lst,0,2); |
| 55 | sprintf(lst,"%d",ls_type); |
| 56 | |
| 57 | char lsid[10]; |
| 58 | memset(lsid,0,10); |
| 59 | sprintf(lsid,"%ld",ls_id); |
| 60 | |
| 61 | memcpy(key+strlen(key),orig_router,strlen(orig_router)); |
| 62 | memcpy(key+strlen(key),"/",1); |
| 63 | memcpy(key+strlen(key),lst,strlen(lst)); |
| 64 | memcpy(key+strlen(key),"/",1); |
| 65 | memcpy(key+strlen(key),lsid,strlen(lsid)); |
| 66 | |
| 67 | printf("Key: %s\n",key); |
| 68 | |
| 69 | } |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 70 | |
akmhoque | f71d908 | 2012-08-22 12:51:53 -0400 | [diff] [blame] | 71 | void |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 72 | build_and_install_name_lsas(void) |
akmhoque | f71d908 | 2012-08-22 12:51:53 -0400 | [diff] [blame] | 73 | { |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 74 | printf("build_and_install_name_lsas called \n"); |
akmhoque | f71d908 | 2012-08-22 12:51:53 -0400 | [diff] [blame] | 75 | |
akmhoque | f71d908 | 2012-08-22 12:51:53 -0400 | [diff] [blame] | 76 | int i, npl_element; |
| 77 | struct name_prefix *np; |
| 78 | |
| 79 | struct hashtb_enumerator ee; |
| 80 | struct hashtb_enumerator *e = ⅇ |
| 81 | |
| 82 | hashtb_start(nlsr->npl, e); |
| 83 | npl_element=hashtb_n(nlsr->npl); |
| 84 | |
| 85 | for(i=0;i<npl_element;i++) |
| 86 | { |
| 87 | np=e->data; |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 88 | struct nlsa *name_lsa=(struct nlsa *)malloc(sizeof( struct nlsa )); |
| 89 | build_name_lsa(name_lsa,np); |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 90 | |
akmhoque | f71d908 | 2012-08-22 12:51:53 -0400 | [diff] [blame] | 91 | install_name_lsa(name_lsa); |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 92 | free(name_lsa->header->orig_router->name); |
| 93 | free(name_lsa->header->orig_router); |
| 94 | free(name_lsa->header); |
| 95 | free(name_lsa->name_prefix->name); |
| 96 | free(name_lsa->name_prefix); |
akmhoque | f71d908 | 2012-08-22 12:51:53 -0400 | [diff] [blame] | 97 | free(name_lsa); |
akmhoque | f71d908 | 2012-08-22 12:51:53 -0400 | [diff] [blame] | 98 | hashtb_next(e); |
| 99 | } |
| 100 | |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 101 | hashtb_end(e); |
akmhoque | f71d908 | 2012-08-22 12:51:53 -0400 | [diff] [blame] | 102 | |
akmhoque | f71d908 | 2012-08-22 12:51:53 -0400 | [diff] [blame] | 103 | } |
| 104 | |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 105 | void |
| 106 | build_name_lsa(struct nlsa *name_lsa, struct name_prefix *np) |
| 107 | { |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 108 | name_lsa->header=(struct nlsa_header *)malloc(sizeof(struct nlsa_header )); |
| 109 | name_lsa->header->ls_type=LS_TYPE_NAME; |
| 110 | |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 111 | char *time_stamp=(char *)malloc(20); |
| 112 | memset(time_stamp,0,20); |
| 113 | get_current_timestamp_micro(time_stamp); |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 114 | |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 115 | name_lsa->header->orig_time=(char *)malloc(strlen(time_stamp)+1); //free |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 116 | memset(name_lsa->header->orig_time,0,strlen(time_stamp)+1); |
| 117 | memcpy(name_lsa->header->orig_time,time_stamp,strlen(time_stamp)+1); |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 118 | |
| 119 | free(time_stamp); |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 120 | |
| 121 | name_lsa->header->ls_id=++nlsr->nlsa_id; |
| 122 | name_lsa->header->orig_router=(struct name_prefix *)malloc(sizeof(struct name_prefix )); |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 123 | name_lsa->header->orig_router->name=(char *)malloc(strlen(nlsr->router_name)+1); |
| 124 | memset(name_lsa->header->orig_router->name,0,strlen(nlsr->router_name)+1); |
| 125 | memcpy(name_lsa->header->orig_router->name,nlsr->router_name,strlen(nlsr->router_name)+1); |
| 126 | name_lsa->header->orig_router->length=strlen(nlsr->router_name)+1; |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 127 | name_lsa->header->isValid=1; |
| 128 | |
| 129 | |
| 130 | name_lsa->name_prefix=(struct name_prefix *)malloc(sizeof(struct name_prefix )); |
| 131 | name_lsa->name_prefix->name=(char *)malloc(np->length); |
| 132 | memcpy(name_lsa->name_prefix->name,np->name,np->length); |
| 133 | name_lsa->name_prefix->length=np->length; |
| 134 | |
| 135 | } |
| 136 | |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 137 | void |
| 138 | install_name_lsa(struct nlsa *name_lsa) |
| 139 | { |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 140 | |
akmhoque | ffacaa8 | 2012-09-13 17:48:30 -0500 | [diff] [blame] | 141 | char *time_stamp=(char *)malloc(20); |
| 142 | memset(time_stamp,0,20); |
| 143 | get_current_timestamp_micro(time_stamp); |
| 144 | long int lsa_life_time=get_time_diff(time_stamp,name_lsa->header->orig_time); |
| 145 | |
| 146 | printf("time difference: %ld \n",lsa_life_time); |
| 147 | |
| 148 | |
| 149 | char lst[2]; |
| 150 | memset(lst,0,2); |
| 151 | sprintf(lst,"%d",name_lsa->header->ls_type); |
| 152 | |
| 153 | char lsid[10]; |
| 154 | memset(lsid,0,10); |
| 155 | sprintf(lsid,"%ld",name_lsa->header->ls_id); |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 156 | |
| 157 | |
akmhoque | ffacaa8 | 2012-09-13 17:48:30 -0500 | [diff] [blame] | 158 | char *key=(char *)malloc(strlen(name_lsa->header->orig_router->name)+1+strlen(lst)+1+strlen(lsid)+1); |
| 159 | memset(key,0,strlen(name_lsa->header->orig_router->name)+1+strlen(lst)+1+strlen(lsid)+1); |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 160 | |
| 161 | |
akmhoque | ffacaa8 | 2012-09-13 17:48:30 -0500 | [diff] [blame] | 162 | make_name_lsa_key(key, name_lsa->header->orig_router->name,name_lsa->header->ls_type,name_lsa->header->ls_id); |
| 163 | printf("Key:%s Length:%d\n",key,(int)strlen(key)); |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 164 | |
akmhoque | ffacaa8 | 2012-09-13 17:48:30 -0500 | [diff] [blame] | 165 | struct nlsa *new_name_lsa=(struct nlsa*)malloc(sizeof(struct nlsa )); //free |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 166 | |
akmhoque | ffacaa8 | 2012-09-13 17:48:30 -0500 | [diff] [blame] | 167 | struct hashtb_enumerator ee; |
| 168 | struct hashtb_enumerator *e = ⅇ |
| 169 | int res; |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 170 | |
akmhoque | ffacaa8 | 2012-09-13 17:48:30 -0500 | [diff] [blame] | 171 | hashtb_start(nlsr->lsdb->name_lsdb, e); |
| 172 | res = hashtb_seek(e, key, strlen(key), 0); |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 173 | |
akmhoque | ffacaa8 | 2012-09-13 17:48:30 -0500 | [diff] [blame] | 174 | if(res == HT_NEW_ENTRY ) |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 175 | { |
akmhoque | ffacaa8 | 2012-09-13 17:48:30 -0500 | [diff] [blame] | 176 | |
| 177 | |
| 178 | |
| 179 | |
| 180 | printf("New Name LSA... Adding to LSDB\n"); |
| 181 | new_name_lsa = e->data; |
| 182 | |
| 183 | new_name_lsa->header=(struct nlsa_header *)malloc(sizeof(struct nlsa_header )); //free |
| 184 | new_name_lsa->header->ls_type=name_lsa->header->ls_type; |
| 185 | |
| 186 | new_name_lsa->header->orig_time=(char *)malloc(strlen(name_lsa->header->orig_time)+1); |
| 187 | memset(new_name_lsa->header->orig_time,0,strlen(name_lsa->header->orig_time)+1); |
| 188 | memcpy(new_name_lsa->header->orig_time,name_lsa->header->orig_time,strlen(name_lsa->header->orig_time)+1); |
| 189 | |
| 190 | new_name_lsa->header->ls_id=name_lsa->header->ls_id; |
| 191 | new_name_lsa->header->orig_router=(struct name_prefix *)malloc(sizeof(struct name_prefix )); //free |
| 192 | new_name_lsa->header->orig_router->name=(char *)malloc(name_lsa->header->orig_router->length); |
| 193 | memcpy(new_name_lsa->header->orig_router->name,name_lsa->header->orig_router->name,name_lsa->header->orig_router->length); |
| 194 | new_name_lsa->header->orig_router->length=name_lsa->header->orig_router->length; |
| 195 | new_name_lsa->header->isValid=name_lsa->header->isValid; |
| 196 | |
| 197 | |
| 198 | new_name_lsa->name_prefix=(struct name_prefix *)malloc(sizeof(struct name_prefix )); //free |
| 199 | new_name_lsa->name_prefix->name=(char *)malloc(name_lsa->name_prefix->length); |
| 200 | memcpy(new_name_lsa->name_prefix->name,name_lsa->name_prefix->name,name_lsa->name_prefix->length); |
| 201 | new_name_lsa->name_prefix->length=name_lsa->name_prefix->length; |
| 202 | |
| 203 | printf("New Name LSA Added....\n"); |
| 204 | |
| 205 | printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version); |
| 206 | set_new_lsdb_version(); |
| 207 | printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version); |
| 208 | |
| 209 | int next_hop=get_next_hop(new_name_lsa->header->orig_router->name); |
| 210 | if ( next_hop == NO_NEXT_HOP ) |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 211 | { |
akmhoque | ffacaa8 | 2012-09-13 17:48:30 -0500 | [diff] [blame] | 212 | int check=add_npt_entry(new_name_lsa->header->orig_router->name,new_name_lsa->name_prefix->name,NO_FACE); |
| 213 | if ( check == HT_NEW_ENTRY ) |
| 214 | { |
| 215 | printf("Added in npt \n"); |
| 216 | } |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 217 | } |
akmhoque | ffacaa8 | 2012-09-13 17:48:30 -0500 | [diff] [blame] | 218 | else |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 219 | { |
akmhoque | ffacaa8 | 2012-09-13 17:48:30 -0500 | [diff] [blame] | 220 | int check=add_npt_entry(new_name_lsa->header->orig_router->name,new_name_lsa->name_prefix->name,next_hop); |
| 221 | if ( check == HT_NEW_ENTRY ) |
| 222 | { |
| 223 | printf("Added in npt \n"); |
| 224 | } |
| 225 | |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 226 | } |
| 227 | |
akmhoque | ffacaa8 | 2012-09-13 17:48:30 -0500 | [diff] [blame] | 228 | |
| 229 | free(time_stamp); |
| 230 | |
| 231 | } |
| 232 | else if(res == HT_OLD_ENTRY) |
| 233 | { |
| 234 | new_name_lsa=e->data; |
| 235 | if(strcmp(name_lsa->header->orig_time,new_name_lsa->header->orig_time)<=0) |
| 236 | { |
| 237 | printf("Older/Duplicate Adj LSA. Discarded...\n"); |
| 238 | } |
| 239 | else |
| 240 | { |
| 241 | if ( name_lsa->header->isValid == 0 ) |
| 242 | { |
| 243 | // have to call to delete npt table entry |
| 244 | delete_npt_entry(new_name_lsa->header->orig_router->name,new_name_lsa->name_prefix->name); |
| 245 | |
| 246 | if ( strcmp(name_lsa->header->orig_router->name,nlsr->router_name)!= 0) |
| 247 | { |
| 248 | hashtb_delete(e); |
| 249 | printf("isValid bit not set for Router %s so LSA Deleted from LSDB\n",name_lsa->header->orig_router->name); |
| 250 | } |
| 251 | else |
| 252 | { |
| 253 | new_name_lsa->header->isValid=name_lsa->header->isValid; |
| 254 | free(new_name_lsa->header->orig_time); |
| 255 | new_name_lsa->header->orig_time=(char *)malloc(strlen(name_lsa->header->orig_time)+1); |
| 256 | memset(new_name_lsa->header->orig_time,0,strlen(name_lsa->header->orig_time)+1); |
| 257 | memcpy(new_name_lsa->header->orig_time,name_lsa->header->orig_time,strlen(name_lsa->header->orig_time)+1); |
| 258 | } |
| 259 | printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version); |
| 260 | set_new_lsdb_version(); |
| 261 | printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version); |
| 262 | } |
| 263 | else |
| 264 | { |
| 265 | int is_npt_update=0; |
| 266 | if ( strcmp(new_name_lsa->name_prefix->name,name_lsa->name_prefix->name) != 0 ) |
| 267 | { |
| 268 | is_npt_update=1; |
| 269 | delete_npt_entry(new_name_lsa->header->orig_router->name,new_name_lsa->name_prefix->name); |
| 270 | } |
| 271 | |
| 272 | // copying LSA content with header |
| 273 | |
| 274 | free(new_name_lsa->header->orig_time); |
| 275 | new_name_lsa->header->orig_time=(char *)malloc(strlen(name_lsa->header->orig_time)+1); |
| 276 | memset(new_name_lsa->header->orig_time,0,strlen(name_lsa->header->orig_time)+1); |
| 277 | memcpy(new_name_lsa->header->orig_time,name_lsa->header->orig_time,strlen(name_lsa->header->orig_time)+1); |
| 278 | |
| 279 | new_name_lsa->header->isValid=name_lsa->header->isValid; |
| 280 | |
| 281 | free(new_name_lsa->name_prefix->name); |
| 282 | free(new_name_lsa->name_prefix); |
| 283 | new_name_lsa->name_prefix=(struct name_prefix *)malloc(sizeof(struct name_prefix )); |
| 284 | new_name_lsa->name_prefix->name=(char *)malloc(name_lsa->name_prefix->length); |
| 285 | memcpy(new_name_lsa->name_prefix->name,name_lsa->name_prefix->name,name_lsa->name_prefix->length); |
| 286 | new_name_lsa->name_prefix->length=name_lsa->name_prefix->length; |
| 287 | |
| 288 | printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version); |
| 289 | set_new_lsdb_version(); |
| 290 | printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version); |
| 291 | |
| 292 | if( is_npt_update == 1 ) |
| 293 | { |
| 294 | int next_hop=get_next_hop(new_name_lsa->header->orig_router->name); |
| 295 | if ( next_hop == NO_NEXT_HOP ) |
| 296 | { |
| 297 | int check=add_npt_entry(new_name_lsa->header->orig_router->name,new_name_lsa->name_prefix->name,NO_FACE); |
| 298 | if ( check == HT_NEW_ENTRY ) |
| 299 | { |
| 300 | printf("Added in npt \n"); |
| 301 | } |
| 302 | } |
| 303 | else |
| 304 | { |
| 305 | int check=add_npt_entry(new_name_lsa->header->orig_router->name,new_name_lsa->name_prefix->name,next_hop); |
| 306 | if ( check == HT_NEW_ENTRY ) |
| 307 | { |
| 308 | printf("Added in npt \n"); |
| 309 | } |
| 310 | } |
| 311 | } |
| 312 | } |
| 313 | } |
| 314 | |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 315 | } |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 316 | |
akmhoque | ffacaa8 | 2012-09-13 17:48:30 -0500 | [diff] [blame] | 317 | hashtb_end(e); |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 318 | |
akmhoque | ffacaa8 | 2012-09-13 17:48:30 -0500 | [diff] [blame] | 319 | free(key); |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 323 | void |
| 324 | print_name_lsa(struct nlsa *name_lsa) |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 325 | { |
| 326 | |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 327 | printf("-----------Name LSA Content---------------\n"); |
| 328 | printf(" Origination Router : %s\n",name_lsa->header->orig_router->name); |
| 329 | printf(" Origination Router Length: %d\n",name_lsa->header->orig_router->length); |
| 330 | printf(" LS Type : %d\n",name_lsa->header->ls_type); |
| 331 | printf(" LS Id : %ld\n",name_lsa->header->ls_id); |
| 332 | printf(" Origination Time : %s\n",name_lsa->header->orig_time); |
| 333 | printf(" Is Valid : %d\n",name_lsa->header->isValid); |
| 334 | printf(" LSA Data \n"); |
| 335 | printf(" Name Prefix: : %s\n",name_lsa->name_prefix->name); |
| 336 | printf(" Name Prefix Length : %d\n",name_lsa->name_prefix->length); |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 337 | |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 338 | printf("\n"); |
akmhoque | f71d908 | 2012-08-22 12:51:53 -0400 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | void |
| 342 | print_name_lsdb(void) |
| 343 | { |
| 344 | printf("print_name_lsdb called \n"); |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 345 | int i, name_lsdb_element; |
akmhoque | f71d908 | 2012-08-22 12:51:53 -0400 | [diff] [blame] | 346 | struct nlsa *name_lsa; |
| 347 | |
| 348 | struct hashtb_enumerator ee; |
| 349 | struct hashtb_enumerator *e = ⅇ |
| 350 | |
| 351 | hashtb_start(nlsr->lsdb->name_lsdb, e); |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 352 | name_lsdb_element=hashtb_n(nlsr->lsdb->name_lsdb); |
akmhoque | f71d908 | 2012-08-22 12:51:53 -0400 | [diff] [blame] | 353 | |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 354 | for(i=0;i<name_lsdb_element;i++) |
akmhoque | f71d908 | 2012-08-22 12:51:53 -0400 | [diff] [blame] | 355 | { |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 356 | printf("-----------Name LSA (%d)---------------\n",i+1); |
akmhoque | f71d908 | 2012-08-22 12:51:53 -0400 | [diff] [blame] | 357 | name_lsa=e->data; |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 358 | print_name_lsa(name_lsa); |
akmhoque | f71d908 | 2012-08-22 12:51:53 -0400 | [diff] [blame] | 359 | hashtb_next(e); |
| 360 | } |
| 361 | |
| 362 | hashtb_end(e); |
| 363 | |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 364 | printf("\n"); |
| 365 | } |
| 366 | |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 367 | |
| 368 | void |
| 369 | build_and_install_others_name_lsa(char *orig_router,int ls_type,long int ls_id,char *orig_time, int isValid,char *np) |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 370 | { |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 371 | printf("build_and_install_others_name_lsa called \n"); |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 372 | |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 373 | struct nlsa *name_lsa=(struct nlsa *)malloc(sizeof( struct nlsa )); |
| 374 | build_others_name_lsa(name_lsa,orig_router,ls_type,ls_id,orig_time, isValid,np); |
| 375 | print_name_lsa(name_lsa); |
| 376 | install_name_lsa(name_lsa); |
| 377 | print_name_lsdb(); |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 378 | |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 379 | |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 380 | free(name_lsa->header->orig_router->name); |
| 381 | free(name_lsa->header->orig_router); |
| 382 | free(name_lsa->header); |
| 383 | free(name_lsa->name_prefix->name); |
| 384 | free(name_lsa->name_prefix); |
| 385 | free(name_lsa); |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 386 | |
| 387 | } |
| 388 | |
| 389 | void |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 390 | build_others_name_lsa(struct nlsa *name_lsa, char *orig_router,int ls_type,long int ls_id,char *orig_time, int isValid,char *np) |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 391 | { |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 392 | printf("build_others_name_lsa called \n"); |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 393 | |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 394 | name_lsa->header=(struct nlsa_header *)malloc(sizeof(struct nlsa_header )); |
| 395 | name_lsa->header->ls_type=LS_TYPE_NAME; |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 396 | |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 397 | name_lsa->header->orig_time=(char *)malloc(strlen(orig_time)+1); |
| 398 | memset(name_lsa->header->orig_time,0,strlen(orig_time)+1); |
| 399 | memcpy(name_lsa->header->orig_time,orig_time,strlen(orig_time)+1); |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 400 | |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 401 | name_lsa->header->ls_id=ls_id; |
| 402 | name_lsa->header->orig_router=(struct name_prefix *)malloc(sizeof(struct name_prefix )); |
| 403 | name_lsa->header->orig_router->name=(char *)malloc(strlen(orig_router)+1); |
| 404 | memset(name_lsa->header->orig_router->name,0,strlen(orig_router)+1); |
| 405 | memcpy(name_lsa->header->orig_router->name,orig_router,strlen(orig_router)+1); |
| 406 | name_lsa->header->orig_router->length=strlen(orig_router)+1; |
| 407 | name_lsa->header->isValid=isValid; |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 408 | |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 409 | name_lsa->name_prefix=(struct name_prefix *)malloc(sizeof(struct name_prefix )); |
| 410 | name_lsa->name_prefix->name=(char *)malloc(strlen(np)+1); |
| 411 | memset(name_lsa->name_prefix->name,0,strlen(np)+1); |
| 412 | memcpy(name_lsa->name_prefix->name,np,strlen(np)+1); |
| 413 | name_lsa->name_prefix->length=strlen(np)+1; |
akmhoque | f71d908 | 2012-08-22 12:51:53 -0400 | [diff] [blame] | 414 | } |
akmhoque | d79438d | 2012-08-27 13:31:42 -0500 | [diff] [blame] | 415 | |
| 416 | |
akmhoque | ffacaa8 | 2012-09-13 17:48:30 -0500 | [diff] [blame] | 417 | |
| 418 | |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 419 | void |
| 420 | make_adj_lsa_key(char *key,struct alsa *adj_lsa) |
akmhoque | d79438d | 2012-08-27 13:31:42 -0500 | [diff] [blame] | 421 | { |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 422 | memcpy(key+strlen(key),adj_lsa->header->orig_router->name,adj_lsa->header->orig_router->length); |
| 423 | memcpy(key+strlen(key),"/",1); |
| 424 | char ls_type[2]; |
| 425 | sprintf(ls_type,"%d",adj_lsa->header->ls_type); |
| 426 | memcpy(key+strlen(key),ls_type,strlen(ls_type)); |
| 427 | key[strlen(key)]='\0'; |
| 428 | } |
| 429 | |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 430 | int |
| 431 | build_and_install_adj_lsa(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags) |
| 432 | { |
akmhoque | ffacaa8 | 2012-09-13 17:48:30 -0500 | [diff] [blame] | 433 | if(flags == CCN_SCHEDULE_CANCEL) |
| 434 | { |
| 435 | return -1; |
| 436 | } |
| 437 | |
| 438 | nlsr_lock(); |
| 439 | |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 440 | printf("build_and_install_adj_lsa called \n"); |
| 441 | |
| 442 | printf("adj_build_flag = %d \n",nlsr->adj_build_flag); |
| 443 | |
| 444 | if(nlsr->adj_build_flag > 0) |
| 445 | { |
| 446 | printf("is_adj_lsa_build = %d \n",is_adj_lsa_build()); |
| 447 | if ( is_adj_lsa_build()> 0) |
| 448 | { |
| 449 | struct alsa *adj_lsa=(struct alsa *)malloc(sizeof( struct alsa )); |
| 450 | build_adj_lsa(adj_lsa); |
| 451 | install_adj_lsa(adj_lsa); |
| 452 | |
| 453 | free(adj_lsa->header->orig_router->name); |
| 454 | free(adj_lsa->header->orig_router); |
| 455 | free(adj_lsa->header->orig_time); |
| 456 | free(adj_lsa->header); |
| 457 | free(adj_lsa->body); |
| 458 | free(adj_lsa); |
| 459 | nlsr->adj_build_flag=0; |
| 460 | print_adj_lsdb(); |
| 461 | } |
| 462 | else |
| 463 | { |
| 464 | printf("Can not build adj LSA now\n"); |
| 465 | } |
| 466 | } |
| 467 | nlsr->is_build_adj_lsa_sheduled=0; |
akmhoque | ffacaa8 | 2012-09-13 17:48:30 -0500 | [diff] [blame] | 468 | |
| 469 | nlsr_unlock(); |
| 470 | |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 471 | return 0; |
| 472 | } |
| 473 | |
| 474 | |
| 475 | void |
| 476 | build_adj_lsa(struct alsa * adj_lsa) |
| 477 | { |
| 478 | printf("build_adj_lsa called \n"); |
| 479 | |
| 480 | int no_link=no_active_nbr(); |
| 481 | printf("Number of link in Adjacent LSA: %d\n",no_link); |
| 482 | |
| 483 | /*Filling Up Header Data */ |
| 484 | adj_lsa->header=(struct alsa_header *)malloc(sizeof(struct alsa_header )); |
| 485 | adj_lsa->header->orig_router=(struct name_prefix *)malloc(sizeof(struct name_prefix )); |
| 486 | adj_lsa->header->orig_router->name=(char *)malloc(strlen(nlsr->router_name)+1); |
| 487 | memset(adj_lsa->header->orig_router->name,0,strlen(nlsr->router_name)+1); |
| 488 | memcpy(adj_lsa->header->orig_router->name,nlsr->router_name,strlen(nlsr->router_name)+1); |
| 489 | adj_lsa->header->orig_router->length=strlen(nlsr->router_name)+1; |
| 490 | |
| 491 | adj_lsa->header->ls_type=(unsigned)LS_TYPE_ADJ; |
| 492 | |
| 493 | char *time_stamp=(char *)malloc(20); |
| 494 | memset(time_stamp,0,20); |
| 495 | get_current_timestamp_micro(time_stamp); |
| 496 | |
| 497 | adj_lsa->header->orig_time=(char *)malloc(strlen(time_stamp)+1); |
| 498 | memset(adj_lsa->header->orig_time,0,strlen(time_stamp)+1); |
| 499 | memcpy(adj_lsa->header->orig_time,time_stamp,strlen(time_stamp)+1); |
| 500 | free(time_stamp); |
| 501 | |
| 502 | |
| 503 | /* Filling Up Body Data */ |
| 504 | |
| 505 | adj_lsa->no_link=no_link; |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 506 | |
| 507 | |
| 508 | struct ccn_charbuf *c=ccn_charbuf_create(); |
| 509 | get_active_nbr_adj_data(c); |
| 510 | char *data=ccn_charbuf_as_string(c); |
| 511 | |
| 512 | adj_lsa->body=(char *)malloc(strlen(data)+1); |
| 513 | memset(adj_lsa->body,0,strlen(data)+1); |
| 514 | memcpy(adj_lsa->body,(char *)data,strlen(data)+1); |
| 515 | ccn_charbuf_destroy(&c); |
| 516 | |
| 517 | |
| 518 | |
| 519 | if( !nlsr->is_send_lsdb_interest_scheduled ) |
| 520 | { |
| 521 | nlsr->event_send_lsdb_interest= ccn_schedule_event(nlsr->sched, 1000, &send_lsdb_interest, NULL, 0); |
| 522 | nlsr->is_send_lsdb_interest_scheduled=1; |
| 523 | } |
| 524 | |
| 525 | nlsr->adj_build_count++; |
| 526 | |
| 527 | |
| 528 | } |
| 529 | |
| 530 | |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 531 | void |
| 532 | install_adj_lsa(struct alsa * adj_lsa) |
| 533 | { |
akmhoque | d79438d | 2012-08-27 13:31:42 -0500 | [diff] [blame] | 534 | printf("install_adj_lsa called \n"); |
| 535 | |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 536 | |
akmhoque | ffacaa8 | 2012-09-13 17:48:30 -0500 | [diff] [blame] | 537 | char *time_stamp=(char *)malloc(20); |
| 538 | memset(time_stamp,0,20); |
| 539 | get_current_timestamp_micro(time_stamp); |
| 540 | long int lsa_life_time=get_time_diff(time_stamp,adj_lsa->header->orig_time); |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 541 | |
akmhoque | ffacaa8 | 2012-09-13 17:48:30 -0500 | [diff] [blame] | 542 | printf("time difference: %ld \n",lsa_life_time); |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 543 | |
akmhoque | ffacaa8 | 2012-09-13 17:48:30 -0500 | [diff] [blame] | 544 | |
| 545 | char *key=(char *)malloc(adj_lsa->header->orig_router->length+2+2); |
| 546 | memset(key,0,adj_lsa->header->orig_router->length+2); |
| 547 | make_adj_lsa_key(key,adj_lsa); |
| 548 | printf("Adjacent LSA key: %s \n",key); |
| 549 | |
| 550 | struct alsa *new_adj_lsa=(struct alsa*)malloc(sizeof(struct alsa )); |
| 551 | |
| 552 | struct hashtb_enumerator ee; |
| 553 | struct hashtb_enumerator *e = ⅇ |
| 554 | int res; |
| 555 | |
| 556 | hashtb_start(nlsr->lsdb->adj_lsdb, e); |
| 557 | res = hashtb_seek(e, key, strlen(key), 0); |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 558 | |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 559 | |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 560 | |
akmhoque | ffacaa8 | 2012-09-13 17:48:30 -0500 | [diff] [blame] | 561 | if(res == HT_NEW_ENTRY ) |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 562 | { |
akmhoque | ffacaa8 | 2012-09-13 17:48:30 -0500 | [diff] [blame] | 563 | printf("New ADJ LSA... Adding to LSDB\n"); |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 564 | new_adj_lsa = e->data; |
| 565 | |
akmhoque | ffacaa8 | 2012-09-13 17:48:30 -0500 | [diff] [blame] | 566 | new_adj_lsa->header=(struct alsa_header *)malloc(sizeof(struct alsa_header )); |
| 567 | new_adj_lsa->header->ls_type=adj_lsa->header->ls_type; |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 568 | new_adj_lsa->header->orig_time=(char *)malloc(strlen(adj_lsa->header->orig_time)+1); |
akmhoque | ffacaa8 | 2012-09-13 17:48:30 -0500 | [diff] [blame] | 569 | memcpy(new_adj_lsa->header->orig_time,adj_lsa->header->orig_time,strlen(adj_lsa->header->orig_time)+1); |
| 570 | |
| 571 | new_adj_lsa->header->orig_router=(struct name_prefix *)malloc(sizeof(struct name_prefix )); |
| 572 | new_adj_lsa->header->orig_router->name=(char *)malloc(adj_lsa->header->orig_router->length); |
| 573 | memcpy(new_adj_lsa->header->orig_router->name,adj_lsa->header->orig_router->name,adj_lsa->header->orig_router->length); |
| 574 | new_adj_lsa->header->orig_router->length=adj_lsa->header->orig_router->length; |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 575 | |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 576 | new_adj_lsa->no_link=adj_lsa->no_link; |
akmhoque | ffacaa8 | 2012-09-13 17:48:30 -0500 | [diff] [blame] | 577 | |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 578 | new_adj_lsa->body=(char *)malloc(strlen(adj_lsa->body)+1); |
| 579 | memset(new_adj_lsa->body,0,strlen(adj_lsa->body)+1); |
| 580 | memcpy(new_adj_lsa->body,adj_lsa->body,strlen(adj_lsa->body)+1); |
| 581 | |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 582 | printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version); |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 583 | set_new_lsdb_version(); |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 584 | printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version); |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 585 | |
akmhoque | ffacaa8 | 2012-09-13 17:48:30 -0500 | [diff] [blame] | 586 | add_next_hop_router(new_adj_lsa->header->orig_router->name); |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 587 | |
akmhoque | ffacaa8 | 2012-09-13 17:48:30 -0500 | [diff] [blame] | 588 | add_next_hop_from_lsa_adj_body(new_adj_lsa->body,new_adj_lsa->no_link); |
| 589 | } |
| 590 | else if(res == HT_OLD_ENTRY) |
| 591 | { |
| 592 | new_adj_lsa = e->data; |
| 593 | if(strcmp(adj_lsa->header->orig_time,new_adj_lsa->header->orig_time)<=0) |
| 594 | { |
| 595 | printf("Older/Duplicate Adj LSA. Discarded...\n"); |
| 596 | } |
| 597 | else |
| 598 | { |
| 599 | new_adj_lsa = e->data; |
| 600 | |
| 601 | free(new_adj_lsa->header->orig_time); |
| 602 | new_adj_lsa->header->orig_time=(char *)malloc(strlen(adj_lsa->header->orig_time)+1); |
| 603 | memcpy(new_adj_lsa->header->orig_time,adj_lsa->header->orig_time,strlen(adj_lsa->header->orig_time)+1); |
| 604 | |
| 605 | new_adj_lsa->no_link=adj_lsa->no_link; |
| 606 | |
| 607 | new_adj_lsa->body=(char *)malloc(strlen(adj_lsa->body)+1); |
| 608 | memset(new_adj_lsa->body,0,strlen(adj_lsa->body)+1); |
| 609 | memcpy(new_adj_lsa->body,adj_lsa->body,strlen(adj_lsa->body)+1); |
| 610 | |
| 611 | printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version); |
| 612 | set_new_lsdb_version(); |
| 613 | printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version); |
| 614 | |
| 615 | add_next_hop_from_lsa_adj_body(new_adj_lsa->body,new_adj_lsa->no_link); |
| 616 | |
| 617 | } |
| 618 | |
| 619 | } |
| 620 | hashtb_end(e); |
| 621 | |
| 622 | if ( !nlsr->is_route_calculation_scheduled ) |
| 623 | { |
| 624 | nlsr->event_calculate_route = ccn_schedule_event(nlsr->sched, 1000000, &route_calculate, NULL, 0); |
| 625 | nlsr->is_route_calculation_scheduled=1; |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 626 | } |
| 627 | |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 628 | |
akmhoque | ffacaa8 | 2012-09-13 17:48:30 -0500 | [diff] [blame] | 629 | free(key); |
| 630 | |
| 631 | free(time_stamp); |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 632 | } |
| 633 | |
| 634 | void |
| 635 | print_adj_lsa_body(const char *body, int no_link) |
| 636 | { |
| 637 | int i=0; |
| 638 | char *lsa_data=(char *)malloc(strlen(body)+1); |
| 639 | memset( lsa_data,0,strlen(body)+1); |
| 640 | memcpy(lsa_data,body,strlen(body)+1); |
| 641 | char *sep="|"; |
| 642 | char *rem; |
| 643 | char *rtr_id; |
| 644 | char *length; |
| 645 | char *face; |
| 646 | char *metric; |
| 647 | |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 648 | if(no_link >0 ) |
| 649 | { |
| 650 | rtr_id=strtok_r(lsa_data,sep,&rem); |
| 651 | length=strtok_r(NULL,sep,&rem); |
| 652 | face=strtok_r(NULL,sep,&rem); |
| 653 | metric=strtok_r(NULL,sep,&rem); |
| 654 | |
| 655 | printf(" Link %d \n",i+1); |
| 656 | printf(" Neighbor : %s \n",rtr_id); |
| 657 | printf(" Neighbor Length : %s \n",length); |
| 658 | printf(" Connecting Face : %s \n",face); |
| 659 | printf(" Metric : %s \n",metric); |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 660 | |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 661 | |
| 662 | for(i=1;i<no_link;i++) |
| 663 | { |
| 664 | rtr_id=strtok_r(NULL,sep,&rem); |
| 665 | length=strtok_r(NULL,sep,&rem); |
| 666 | face=strtok_r(NULL,sep,&rem); |
| 667 | metric=strtok_r(NULL,sep,&rem); |
| 668 | printf(" Link %d \n",i+1); |
| 669 | printf(" Neighbor : %s \n",rtr_id); |
| 670 | printf(" Neighbor Length : %s \n",length); |
| 671 | printf(" Connecting Face : %s \n",face); |
| 672 | printf(" Metric : %s \n",metric); |
| 673 | |
| 674 | } |
| 675 | } |
| 676 | |
| 677 | free(lsa_data); |
| 678 | } |
| 679 | |
| 680 | void |
| 681 | print_adj_lsa(struct alsa * adj_lsa) |
| 682 | { |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 683 | |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 684 | printf("print_adj_lsa called \n"); |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 685 | |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 686 | printf("-----------ADJ LSA Content---------------\n"); |
| 687 | printf(" Origination Router : %s\n",adj_lsa->header->orig_router->name); |
| 688 | printf(" Origination Router Length: %d\n",adj_lsa->header->orig_router->length); |
| 689 | printf(" LS Type : %d\n",adj_lsa->header->ls_type); |
| 690 | printf(" Origination Time : %s\n",adj_lsa->header->orig_time); |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 691 | printf(" Lsa Data:\n"); |
| 692 | printf(" No of Link : %d\n",adj_lsa->no_link); |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 693 | |
| 694 | print_adj_lsa_body(adj_lsa->body,adj_lsa->no_link); |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 695 | |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 696 | /* |
| 697 | struct link *templ=adj_lsa->links; |
| 698 | int i; |
| 699 | |
| 700 | for(i=0 ; i< adj_lsa->no_link ; i++) |
| 701 | { |
| 702 | printf(" Link %d \n",i+1); |
| 703 | printf(" Neighbor : %s \n",templ->nbr->name); |
| 704 | printf(" Neighbor Length : %d \n",templ->nbr->length); |
| 705 | printf(" Connecting Face : %d \n",templ->face); |
| 706 | printf(" Metric : %d \n",templ->metric); |
| 707 | |
| 708 | templ++; |
| 709 | } |
| 710 | */ |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 711 | printf("\n"); |
| 712 | |
| 713 | } |
| 714 | |
| 715 | void |
| 716 | print_adj_lsdb(void) |
| 717 | { |
| 718 | printf("print_name_lsdb called \n"); |
| 719 | int i, adj_lsdb_element; |
| 720 | struct alsa *adj_lsa; |
| 721 | |
| 722 | struct hashtb_enumerator ee; |
| 723 | struct hashtb_enumerator *e = ⅇ |
| 724 | |
| 725 | hashtb_start(nlsr->lsdb->adj_lsdb, e); |
| 726 | adj_lsdb_element=hashtb_n(nlsr->lsdb->adj_lsdb); |
| 727 | |
| 728 | for(i=0;i<adj_lsdb_element;i++) |
| 729 | { |
| 730 | printf("-----------Adj LSA (%d)---------------\n",i+1); |
| 731 | adj_lsa=e->data; |
| 732 | print_adj_lsa(adj_lsa); |
| 733 | hashtb_next(e); |
| 734 | } |
| 735 | |
| 736 | hashtb_end(e); |
| 737 | |
| 738 | printf("\n"); |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 739 | } |
| 740 | |
| 741 | void |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 742 | build_and_install_others_adj_lsa(char *orig_router,int ls_type,char *orig_time, int no_link,char *data) |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 743 | { |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 744 | printf("build_and_install_others_adj_lsa called \n"); |
| 745 | struct alsa *adj_lsa=(struct alsa *)malloc(sizeof( struct alsa )); |
| 746 | build_others_adj_lsa(adj_lsa,orig_router,ls_type,orig_time,no_link,data); |
| 747 | //print_adj_lsa(adj_lsa); |
| 748 | install_adj_lsa(adj_lsa); |
| 749 | |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 750 | |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 751 | free(adj_lsa->header->orig_router->name); |
| 752 | free(adj_lsa->header->orig_router); |
| 753 | free(adj_lsa->header->orig_time); |
| 754 | free(adj_lsa->header); |
| 755 | free(adj_lsa->body); |
| 756 | free(adj_lsa); |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 757 | |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 758 | print_adj_lsdb(); |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 759 | |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 760 | } |
| 761 | |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 762 | |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 763 | void |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 764 | build_others_adj_lsa(struct alsa *adj_lsa,char *orig_router,int ls_type,char *orig_time,int no_link,char *data) |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 765 | { |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 766 | printf("build_others_adj_lsa called \n"); |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 767 | |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 768 | /*Filling Up Header Data */ |
| 769 | adj_lsa->header=(struct alsa_header *)malloc(sizeof(struct alsa_header )); |
| 770 | adj_lsa->header->orig_router=(struct name_prefix *)malloc(sizeof(struct name_prefix )); |
| 771 | adj_lsa->header->orig_router->name=(char *)malloc(strlen(orig_router)+1); |
| 772 | memset(adj_lsa->header->orig_router->name,0,strlen(orig_router)+1); |
| 773 | memcpy(adj_lsa->header->orig_router->name,orig_router,strlen(orig_router)+1); |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 774 | |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 775 | adj_lsa->header->orig_router->length=strlen(orig_router)+1; |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 776 | |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 777 | |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 778 | adj_lsa->header->ls_type=(unsigned)LS_TYPE_ADJ; |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 779 | |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 780 | adj_lsa->header->orig_time=(char *)malloc(strlen(orig_time)+1); |
| 781 | memset(adj_lsa->header->orig_time,0,strlen(orig_time)+1); |
| 782 | memcpy(adj_lsa->header->orig_time,orig_time,strlen(orig_time)+1); |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 783 | |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 784 | adj_lsa->no_link=no_link; |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 785 | |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 786 | adj_lsa->body=(char *)malloc(strlen(data)+1); |
| 787 | memset(adj_lsa->body,0,strlen(data)+1); |
| 788 | memcpy(adj_lsa->body,(char *)data,strlen(data)+1); |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 789 | |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 790 | } |
| 791 | |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 792 | |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 793 | long int |
| 794 | get_name_lsdb_num_element(void) |
| 795 | { |
| 796 | long int num_element; |
| 797 | |
| 798 | |
| 799 | struct hashtb_enumerator ee; |
| 800 | struct hashtb_enumerator *e = ⅇ |
| 801 | |
| 802 | hashtb_start(nlsr->lsdb->name_lsdb, e); |
| 803 | num_element=hashtb_n(nlsr->lsdb->name_lsdb); |
| 804 | hashtb_end(e); |
| 805 | |
| 806 | return num_element; |
| 807 | } |
| 808 | |
| 809 | long int |
| 810 | get_adj_lsdb_num_element(void) |
| 811 | { |
| 812 | long int num_element; |
| 813 | |
| 814 | |
| 815 | struct hashtb_enumerator ee; |
| 816 | struct hashtb_enumerator *e = ⅇ |
| 817 | |
| 818 | hashtb_start(nlsr->lsdb->adj_lsdb, e); |
| 819 | num_element=hashtb_n(nlsr->lsdb->adj_lsdb); |
| 820 | hashtb_end(e); |
| 821 | |
| 822 | return num_element; |
akmhoque | d79438d | 2012-08-27 13:31:42 -0500 | [diff] [blame] | 823 | } |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 824 | |
| 825 | void |
| 826 | get_name_lsdb_summary(struct ccn_charbuf *name_lsdb_data) |
| 827 | { |
| 828 | printf("get_name_lsdb_summary called \n"); |
| 829 | int i, name_lsdb_element; |
| 830 | |
| 831 | struct nlsa *name_lsa; |
| 832 | struct hashtb_enumerator ee; |
| 833 | struct hashtb_enumerator *e = ⅇ |
| 834 | |
| 835 | hashtb_start(nlsr->lsdb->name_lsdb, e); |
| 836 | name_lsdb_element=hashtb_n(nlsr->lsdb->name_lsdb); |
| 837 | |
| 838 | for(i=0;i<name_lsdb_element;i++) |
| 839 | { |
| 840 | name_lsa=e->data; |
| 841 | |
| 842 | ccn_charbuf_append_string(name_lsdb_data,name_lsa->header->orig_router->name); |
| 843 | ccn_charbuf_append_string(name_lsdb_data,"|"); |
| 844 | |
| 845 | char *lst=(char *)malloc(20); |
| 846 | memset(lst,0,20); |
| 847 | sprintf(lst,"%d",name_lsa->header->ls_type); |
| 848 | ccn_charbuf_append_string(name_lsdb_data,lst); |
| 849 | free(lst); |
| 850 | ccn_charbuf_append_string(name_lsdb_data,"|"); |
| 851 | |
| 852 | char *lsid=(char *)malloc(20); |
| 853 | memset(lsid,0,20); |
| 854 | sprintf(lsid,"%ld",name_lsa->header->ls_id); |
| 855 | ccn_charbuf_append_string(name_lsdb_data,lsid); |
| 856 | free(lsid); |
| 857 | ccn_charbuf_append_string(name_lsdb_data,"|"); |
| 858 | |
| 859 | ccn_charbuf_append_string(name_lsdb_data,name_lsa->header->orig_time); |
| 860 | ccn_charbuf_append_string(name_lsdb_data,"|"); |
| 861 | |
| 862 | hashtb_next(e); |
| 863 | } |
| 864 | |
| 865 | hashtb_end(e); |
| 866 | |
| 867 | } |
| 868 | |
| 869 | |
| 870 | void |
| 871 | get_adj_lsdb_summary(struct ccn_charbuf *adj_lsdb_data) |
| 872 | { |
| 873 | printf("get_adj_lsdb_summary called \n"); |
| 874 | int i, adj_lsdb_element; |
| 875 | struct alsa *adj_lsa; |
| 876 | |
| 877 | struct hashtb_enumerator ee; |
| 878 | struct hashtb_enumerator *e = ⅇ |
| 879 | |
| 880 | hashtb_start(nlsr->lsdb->adj_lsdb, e); |
| 881 | adj_lsdb_element=hashtb_n(nlsr->lsdb->adj_lsdb); |
| 882 | |
| 883 | for(i=0;i<adj_lsdb_element;i++) |
| 884 | { |
| 885 | adj_lsa=e->data; |
| 886 | |
| 887 | ccn_charbuf_append_string(adj_lsdb_data,adj_lsa->header->orig_router->name); |
| 888 | ccn_charbuf_append_string(adj_lsdb_data,"|"); |
| 889 | |
| 890 | char *lst=(char *)malloc(20); |
| 891 | memset(lst,0,20); |
| 892 | sprintf(lst,"%d",adj_lsa->header->ls_type); |
| 893 | ccn_charbuf_append_string(adj_lsdb_data,lst); |
| 894 | free(lst); |
| 895 | ccn_charbuf_append_string(adj_lsdb_data,"|"); |
| 896 | |
| 897 | ccn_charbuf_append_string(adj_lsdb_data,adj_lsa->header->orig_time); |
| 898 | ccn_charbuf_append_string(adj_lsdb_data,"|"); |
| 899 | |
| 900 | hashtb_next(e); |
| 901 | } |
| 902 | |
| 903 | hashtb_end(e); |
| 904 | } |
| 905 | |
| 906 | |
| 907 | void |
| 908 | get_lsdb_summary(struct ccn_charbuf *lsdb_data) |
| 909 | { |
| 910 | struct ccn_charbuf *name_lsdb_data=ccn_charbuf_create(); |
| 911 | struct ccn_charbuf *adj_lsdb_data=ccn_charbuf_create(); |
| 912 | |
| 913 | get_name_lsdb_summary(name_lsdb_data); |
| 914 | get_adj_lsdb_summary(adj_lsdb_data); |
| 915 | |
| 916 | long int num_lsa=get_name_lsdb_num_element() + get_adj_lsdb_num_element(); |
| 917 | char *num_element=(char *)malloc(15); |
| 918 | memset(num_element,0,15); |
| 919 | sprintf(num_element,"%ld",num_lsa); |
| 920 | |
| 921 | if( num_lsa > 0) |
| 922 | { |
| 923 | ccn_charbuf_append_string(lsdb_data,num_element); |
| 924 | ccn_charbuf_append_string(lsdb_data,"|"); |
| 925 | } |
| 926 | if(name_lsdb_data->length>0) |
| 927 | { |
| 928 | char *data1=ccn_charbuf_as_string(name_lsdb_data); |
| 929 | ccn_charbuf_append_string(lsdb_data,(char *)data1); |
| 930 | } |
| 931 | if(adj_lsdb_data->length>0) |
| 932 | { |
| 933 | char *data2=ccn_charbuf_as_string(adj_lsdb_data); |
| 934 | ccn_charbuf_append_string(lsdb_data,(char *)data2); |
| 935 | } |
| 936 | ccn_charbuf_destroy(&name_lsdb_data); |
| 937 | ccn_charbuf_destroy(&adj_lsdb_data); |
| 938 | free(num_element); |
| 939 | |
| 940 | } |
| 941 | |
| 942 | int |
| 943 | check_is_new_name_lsa(char *orig_router,char *lst,char *lsid,char *orig_time) |
| 944 | { |
| 945 | int ret=0; |
| 946 | struct ccn_charbuf *key=ccn_charbuf_create(); |
| 947 | ccn_charbuf_append_string(key,orig_router); |
| 948 | ccn_charbuf_append_string(key,"/"); |
| 949 | ccn_charbuf_append_string(key,lst); |
| 950 | ccn_charbuf_append_string(key,"/"); |
| 951 | ccn_charbuf_append_string(key,lsid); |
| 952 | |
| 953 | int res; |
| 954 | struct nlsa *name_lsa; |
| 955 | |
| 956 | struct hashtb_enumerator ee; |
| 957 | struct hashtb_enumerator *e = ⅇ |
| 958 | |
| 959 | hashtb_start(nlsr->lsdb->name_lsdb, e); |
| 960 | res = hashtb_seek(e, ccn_charbuf_as_string(key), key->length, 0); |
| 961 | |
| 962 | if( res == HT_NEW_ENTRY ) |
| 963 | { |
| 964 | hashtb_delete(e); |
| 965 | ret=1; |
| 966 | |
| 967 | } |
| 968 | else if(res == HT_OLD_ENTRY) |
| 969 | { |
| 970 | name_lsa=e->data; |
| 971 | if( strcmp ( orig_time , name_lsa->header->orig_time ) > 0 ) |
| 972 | { |
| 973 | ret=1; |
| 974 | } |
| 975 | } |
| 976 | |
| 977 | hashtb_end(e); |
| 978 | |
| 979 | ccn_charbuf_destroy(&key); |
| 980 | |
| 981 | return ret; |
| 982 | } |
| 983 | |
| 984 | int |
| 985 | check_is_new_adj_lsa(char *orig_router,char *lst,char *orig_time) |
| 986 | { |
| 987 | int ret=0; |
| 988 | struct ccn_charbuf *key=ccn_charbuf_create(); |
| 989 | ccn_charbuf_append_string(key,orig_router); |
| 990 | ccn_charbuf_append_string(key,"/"); |
| 991 | ccn_charbuf_append_string(key,lst); |
| 992 | |
| 993 | int res; |
| 994 | struct alsa *adj_lsa; |
| 995 | |
| 996 | struct hashtb_enumerator ee; |
| 997 | struct hashtb_enumerator *e = ⅇ |
| 998 | |
| 999 | hashtb_start(nlsr->lsdb->adj_lsdb, e); |
| 1000 | res = hashtb_seek(e, ccn_charbuf_as_string(key), key->length, 0); |
| 1001 | |
| 1002 | if( res == HT_NEW_ENTRY ) |
| 1003 | { |
| 1004 | hashtb_delete(e); |
| 1005 | ret=1; |
| 1006 | |
| 1007 | } |
| 1008 | else if(res == HT_OLD_ENTRY) |
| 1009 | { |
| 1010 | adj_lsa=e->data; |
| 1011 | if( strcmp ( orig_time , adj_lsa->header->orig_time ) > 0 ) |
| 1012 | { |
| 1013 | ret=1; |
| 1014 | } |
| 1015 | } |
| 1016 | |
| 1017 | hashtb_end(e); |
| 1018 | |
| 1019 | ccn_charbuf_destroy(&key); |
| 1020 | |
| 1021 | return ret; |
| 1022 | } |
| 1023 | |
| 1024 | void |
| 1025 | get_name_lsa_data(struct ccn_charbuf *lsa_data, struct name_prefix *lsaId) |
| 1026 | { |
| 1027 | printf("get_name_lsa_data called \n"); |
| 1028 | |
| 1029 | struct nlsa *name_lsa=(struct nlsa*)malloc(sizeof(struct nlsa )); |
| 1030 | |
| 1031 | struct hashtb_enumerator ee; |
| 1032 | struct hashtb_enumerator *e = ⅇ |
| 1033 | int res; |
| 1034 | |
| 1035 | hashtb_start(nlsr->lsdb->name_lsdb, e); |
| 1036 | res = hashtb_seek(e, lsaId->name, lsaId->length-1, 0); |
| 1037 | |
| 1038 | if( res == HT_OLD_ENTRY ) |
| 1039 | { |
| 1040 | name_lsa=e->data; |
| 1041 | printf("NAME LSA found\n"); |
| 1042 | |
| 1043 | ccn_charbuf_append_string(lsa_data,name_lsa->header->orig_router->name); |
| 1044 | ccn_charbuf_append_string(lsa_data,"|"); |
| 1045 | |
| 1046 | char *temp_length=(char *)malloc(20); |
| 1047 | memset(temp_length,0,20); |
| 1048 | sprintf(temp_length,"%d",name_lsa->header->orig_router->length); |
| 1049 | ccn_charbuf_append_string(lsa_data,temp_length); |
| 1050 | free(temp_length); |
| 1051 | ccn_charbuf_append_string(lsa_data,"|"); |
| 1052 | |
| 1053 | char *temp_ltype=(char *)malloc(20); |
| 1054 | memset(temp_ltype,0,20); |
| 1055 | sprintf(temp_ltype,"%d",name_lsa->header->ls_type); |
| 1056 | ccn_charbuf_append_string(lsa_data,temp_ltype); |
| 1057 | free(temp_ltype); |
| 1058 | ccn_charbuf_append_string(lsa_data,"|"); |
| 1059 | |
| 1060 | char *temp_lsid=(char *)malloc(20); |
| 1061 | memset(temp_lsid,0,20); |
| 1062 | sprintf(temp_lsid,"%ld",name_lsa->header->ls_id); |
| 1063 | ccn_charbuf_append_string(lsa_data,temp_lsid); |
| 1064 | free(temp_lsid); |
| 1065 | ccn_charbuf_append_string(lsa_data,"|"); |
| 1066 | |
| 1067 | ccn_charbuf_append_string(lsa_data,name_lsa->header->orig_time); |
| 1068 | ccn_charbuf_append_string(lsa_data,"|"); |
| 1069 | |
| 1070 | char *temp_valid=(char *)malloc(20); |
| 1071 | memset(temp_valid,0,20); |
| 1072 | sprintf(temp_valid,"%d",name_lsa->header->isValid); |
| 1073 | ccn_charbuf_append_string(lsa_data,temp_valid); |
| 1074 | free(temp_valid); |
| 1075 | ccn_charbuf_append_string(lsa_data,"|"); |
| 1076 | |
| 1077 | ccn_charbuf_append_string(lsa_data,name_lsa->name_prefix->name); |
| 1078 | ccn_charbuf_append_string(lsa_data,"|"); |
| 1079 | |
| 1080 | char *temp_npl=(char *)malloc(20); |
| 1081 | memset(temp_npl,0,20); |
| 1082 | sprintf(temp_npl,"%d",name_lsa->name_prefix->length); |
| 1083 | ccn_charbuf_append_string(lsa_data,temp_npl); |
| 1084 | free(temp_npl); |
| 1085 | ccn_charbuf_append_string(lsa_data,"|"); |
| 1086 | |
| 1087 | } |
| 1088 | else if(res == HT_NEW_ENTRY) |
| 1089 | { |
| 1090 | hashtb_delete(e); |
| 1091 | } |
| 1092 | |
| 1093 | hashtb_end(e); |
| 1094 | } |
| 1095 | |
| 1096 | void |
| 1097 | get_adj_lsa_data(struct ccn_charbuf *lsa_data,struct name_prefix *lsaId) |
| 1098 | { |
| 1099 | printf("get_adj_lsa_data called \n"); |
| 1100 | |
| 1101 | struct alsa *adj_lsa=(struct alsa*)malloc(sizeof(struct alsa )); |
| 1102 | |
| 1103 | struct hashtb_enumerator ee; |
| 1104 | struct hashtb_enumerator *e = ⅇ |
| 1105 | int res; |
| 1106 | |
| 1107 | hashtb_start(nlsr->lsdb->adj_lsdb, e); |
| 1108 | res = hashtb_seek(e, lsaId->name, lsaId->length-1, 0); |
| 1109 | |
| 1110 | if( res == HT_OLD_ENTRY ) |
| 1111 | { |
| 1112 | adj_lsa=e->data; |
| 1113 | printf("NAME LSA found\n"); |
| 1114 | |
| 1115 | ccn_charbuf_append_string(lsa_data,adj_lsa->header->orig_router->name); |
| 1116 | ccn_charbuf_append_string(lsa_data,"|"); |
| 1117 | |
| 1118 | char *temp_length=(char *)malloc(20); |
| 1119 | memset(temp_length,0,20); |
| 1120 | sprintf(temp_length,"%d",adj_lsa->header->orig_router->length); |
| 1121 | ccn_charbuf_append_string(lsa_data,temp_length); |
| 1122 | free(temp_length); |
| 1123 | ccn_charbuf_append_string(lsa_data,"|"); |
| 1124 | |
| 1125 | char *temp_ltype=(char *)malloc(20); |
| 1126 | memset(temp_ltype,0,20); |
| 1127 | sprintf(temp_ltype,"%d",adj_lsa->header->ls_type); |
| 1128 | ccn_charbuf_append_string(lsa_data,temp_ltype); |
| 1129 | free(temp_ltype); |
| 1130 | ccn_charbuf_append_string(lsa_data,"|"); |
| 1131 | |
| 1132 | ccn_charbuf_append_string(lsa_data,adj_lsa->header->orig_time); |
| 1133 | ccn_charbuf_append_string(lsa_data,"|"); |
| 1134 | |
| 1135 | char *temp_nl=(char *)malloc(20); |
| 1136 | memset(temp_nl,0,20); |
| 1137 | sprintf(temp_nl,"%d",adj_lsa->no_link); |
| 1138 | ccn_charbuf_append_string(lsa_data,temp_nl); |
| 1139 | free(temp_nl); |
| 1140 | ccn_charbuf_append_string(lsa_data,"|"); |
| 1141 | |
| 1142 | ccn_charbuf_append_string(lsa_data,adj_lsa->body); |
| 1143 | |
| 1144 | |
| 1145 | } |
| 1146 | else if(res == HT_NEW_ENTRY) |
| 1147 | { |
| 1148 | hashtb_delete(e); |
| 1149 | } |
| 1150 | |
| 1151 | hashtb_end(e); |
| 1152 | } |
akmhoque | ffacaa8 | 2012-09-13 17:48:30 -0500 | [diff] [blame] | 1153 | |
akmhoque | da5b683 | 2012-09-13 22:33:55 -0500 | [diff] [blame] | 1154 | int |
| 1155 | delete_name_lsa(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags) |
| 1156 | { |
| 1157 | printf("delete_name_lsa called \n"); |
| 1158 | |
| 1159 | if(flags == CCN_SCHEDULE_CANCEL) |
| 1160 | { |
| 1161 | return -1; |
| 1162 | } |
| 1163 | |
| 1164 | |
| 1165 | |
| 1166 | nlsr_lock(); |
| 1167 | |
| 1168 | printf("LSA Key: %s \n",(char *)ev->evdata); |
| 1169 | |
| 1170 | struct nlsa *nlsa; |
| 1171 | |
| 1172 | struct hashtb_enumerator ee; |
| 1173 | struct hashtb_enumerator *e = ⅇ |
| 1174 | |
| 1175 | int res; |
| 1176 | |
| 1177 | hashtb_start(nlsr->lsdb->name_lsdb, e); |
| 1178 | res = hashtb_seek(e, (char *)ev->evdata, strlen((char *)ev->evdata), 0); |
| 1179 | |
| 1180 | if( res == HT_OLD_ENTRY ) |
| 1181 | { |
| 1182 | nlsa=e->data; |
| 1183 | delete_npt_entry(nlsa->header->orig_router->name, nlsa->name_prefix->name); |
| 1184 | hashtb_delete(e); |
| 1185 | } |
| 1186 | else if( res == HT_OLD_ENTRY ) |
| 1187 | { |
| 1188 | nlsa=e->data; |
| 1189 | delete_npt_entry(nlsa->header->orig_router->name, nlsa->name_prefix->name); |
| 1190 | hashtb_delete(e); |
| 1191 | } |
| 1192 | hashtb_end(e); |
| 1193 | |
| 1194 | |
| 1195 | printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version); |
| 1196 | set_new_lsdb_version(); |
| 1197 | printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version); |
| 1198 | |
| 1199 | //print_name_lsdb(); |
| 1200 | |
| 1201 | nlsr_unlock(); |
| 1202 | |
| 1203 | return 0; |
| 1204 | } |
| 1205 | |
| 1206 | int |
| 1207 | delete_adj_lsa(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags) |
| 1208 | { |
| 1209 | printf("delete_adj_lsa called \n"); |
| 1210 | |
| 1211 | if(flags == CCN_SCHEDULE_CANCEL) |
| 1212 | { |
| 1213 | return -1; |
| 1214 | } |
| 1215 | nlsr_lock(); |
| 1216 | |
| 1217 | printf("LSA Key: %s \n",(char *)ev->evdata); |
| 1218 | |
| 1219 | struct hashtb_enumerator ee; |
| 1220 | struct hashtb_enumerator *e = ⅇ |
| 1221 | int res; |
| 1222 | |
| 1223 | hashtb_start(nlsr->lsdb->adj_lsdb, e); |
| 1224 | res = hashtb_seek(e, (char *)ev->evdata, strlen((char *)ev->evdata), 0); |
| 1225 | |
| 1226 | if( res == HT_OLD_ENTRY ) |
| 1227 | { |
| 1228 | hashtb_delete(e); |
| 1229 | } |
| 1230 | else if( res == HT_OLD_ENTRY ) |
| 1231 | { |
| 1232 | hashtb_delete(e); |
| 1233 | } |
| 1234 | hashtb_end(e); |
| 1235 | |
| 1236 | printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version); |
| 1237 | set_new_lsdb_version(); |
| 1238 | printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version); |
| 1239 | |
| 1240 | if ( !nlsr->is_route_calculation_scheduled) |
| 1241 | { |
| 1242 | nlsr->event_calculate_route = ccn_schedule_event(nlsr->sched, 1000000, &route_calculate, NULL, 0); |
| 1243 | nlsr->is_route_calculation_scheduled=1; |
| 1244 | } |
| 1245 | |
| 1246 | //print_adj_lsdb(); |
| 1247 | |
| 1248 | nlsr_unlock(); |
| 1249 | |
| 1250 | return 0; |
| 1251 | } |
| 1252 | |
akmhoque | ffacaa8 | 2012-09-13 17:48:30 -0500 | [diff] [blame] | 1253 | void |
| 1254 | refresh_name_lsdb(void) |
| 1255 | { |
| 1256 | printf("refresh_name_lsdb called \n"); |
| 1257 | |
akmhoque | da5b683 | 2012-09-13 22:33:55 -0500 | [diff] [blame] | 1258 | //int lsa_change_count=0; |
| 1259 | |
akmhoque | ffacaa8 | 2012-09-13 17:48:30 -0500 | [diff] [blame] | 1260 | char *time_stamp=(char *)malloc(20); |
| 1261 | memset(time_stamp,0,20); |
| 1262 | get_current_timestamp_micro(time_stamp); |
| 1263 | |
| 1264 | long int lsa_life_time; |
| 1265 | |
| 1266 | int i, name_lsdb_element; |
| 1267 | struct nlsa *name_lsa; |
| 1268 | |
| 1269 | struct hashtb_enumerator ee; |
| 1270 | struct hashtb_enumerator *e = ⅇ |
| 1271 | |
| 1272 | hashtb_start(nlsr->lsdb->name_lsdb, e); |
| 1273 | name_lsdb_element=hashtb_n(nlsr->lsdb->name_lsdb); |
| 1274 | |
| 1275 | for(i=0;i<name_lsdb_element;i++) |
| 1276 | { |
| 1277 | name_lsa=e->data; |
| 1278 | |
| 1279 | lsa_life_time=get_time_diff(time_stamp,name_lsa->header->orig_time); |
akmhoque | da5b683 | 2012-09-13 22:33:55 -0500 | [diff] [blame] | 1280 | printf("LSA Life Time: %ld \n",lsa_life_time); |
| 1281 | |
| 1282 | if ( strcmp(name_lsa->header->orig_router->name,nlsr->router_name) == 0) |
| 1283 | { |
| 1284 | if ( lsa_life_time > nlsr->lsa_refresh_time ) |
| 1285 | { |
| 1286 | printf("Own Name LSA need to be refrshed\n"); |
| 1287 | char *current_time_stamp=(char *)malloc(20); |
| 1288 | memset(current_time_stamp,0,20); |
| 1289 | get_current_timestamp_micro(current_time_stamp); |
| 1290 | |
| 1291 | free(name_lsa->header->orig_time); |
| 1292 | name_lsa->header->orig_time=(char *)malloc(strlen(current_time_stamp)+1); //free |
| 1293 | memset(name_lsa->header->orig_time,0,strlen(current_time_stamp)+1); |
| 1294 | memcpy(name_lsa->header->orig_time,current_time_stamp,strlen(current_time_stamp)+1); |
| 1295 | |
| 1296 | free(current_time_stamp); |
| 1297 | |
| 1298 | printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version); |
| 1299 | set_new_lsdb_version(); |
| 1300 | printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version); |
| 1301 | |
| 1302 | print_name_lsdb(); |
| 1303 | //lsa_change_count++; |
| 1304 | } |
| 1305 | } |
| 1306 | else |
| 1307 | { |
| 1308 | if ( lsa_life_time > nlsr->router_dead_interval ) |
| 1309 | { |
| 1310 | printf("Others Name LSA need to be deleted\n"); |
| 1311 | |
| 1312 | char lst[2]; |
| 1313 | memset(lst,0,2); |
| 1314 | sprintf(lst,"%d",name_lsa->header->ls_type); |
| 1315 | |
| 1316 | char lsid[10]; |
| 1317 | memset(lsid,0,10); |
| 1318 | sprintf(lsid,"%ld",name_lsa->header->ls_id); |
| 1319 | |
| 1320 | |
| 1321 | char *key=(char *)malloc(strlen(name_lsa->header->orig_router->name)+1+strlen(lst)+1+strlen(lsid)+1); |
| 1322 | memset(key,0,strlen(name_lsa->header->orig_router->name)+1+strlen(lst)+1+strlen(lsid)+1); |
| 1323 | |
| 1324 | |
| 1325 | make_name_lsa_key(key, name_lsa->header->orig_router->name,name_lsa->header->ls_type,name_lsa->header->ls_id); |
| 1326 | printf("Key:%s Length:%d\n",key,(int)strlen(key)); |
| 1327 | |
| 1328 | nlsr->event = ccn_schedule_event(nlsr->sched, 10, &delete_name_lsa, (void *)key, 0); |
| 1329 | //lsa_change_count++; |
| 1330 | } |
| 1331 | } |
akmhoque | ffacaa8 | 2012-09-13 17:48:30 -0500 | [diff] [blame] | 1332 | |
| 1333 | hashtb_next(e); |
| 1334 | } |
| 1335 | |
| 1336 | hashtb_end(e); |
| 1337 | |
| 1338 | free(time_stamp); |
akmhoque | da5b683 | 2012-09-13 22:33:55 -0500 | [diff] [blame] | 1339 | |
akmhoque | ffacaa8 | 2012-09-13 17:48:30 -0500 | [diff] [blame] | 1340 | |
| 1341 | } |
| 1342 | |
akmhoque | da5b683 | 2012-09-13 22:33:55 -0500 | [diff] [blame] | 1343 | void |
akmhoque | ffacaa8 | 2012-09-13 17:48:30 -0500 | [diff] [blame] | 1344 | refresh_adj_lsdb(void) |
| 1345 | { |
| 1346 | printf("refresh_adj_lsdb called \n"); |
| 1347 | |
akmhoque | da5b683 | 2012-09-13 22:33:55 -0500 | [diff] [blame] | 1348 | |
akmhoque | ffacaa8 | 2012-09-13 17:48:30 -0500 | [diff] [blame] | 1349 | char *time_stamp=(char *)malloc(20); |
| 1350 | memset(time_stamp,0,20); |
| 1351 | get_current_timestamp_micro(time_stamp); |
| 1352 | |
| 1353 | long int lsa_life_time; |
| 1354 | |
| 1355 | int i, adj_lsdb_element; |
| 1356 | struct alsa *adj_lsa; |
| 1357 | |
| 1358 | struct hashtb_enumerator ee; |
| 1359 | struct hashtb_enumerator *e = ⅇ |
| 1360 | |
| 1361 | hashtb_start(nlsr->lsdb->adj_lsdb, e); |
| 1362 | adj_lsdb_element=hashtb_n(nlsr->lsdb->adj_lsdb); |
| 1363 | |
| 1364 | for(i=0;i<adj_lsdb_element;i++) |
| 1365 | { |
| 1366 | adj_lsa=e->data; |
| 1367 | |
| 1368 | lsa_life_time=get_time_diff(time_stamp,adj_lsa->header->orig_time); |
| 1369 | printf("LSA Life Time: %ld \n",lsa_life_time); |
| 1370 | |
akmhoque | da5b683 | 2012-09-13 22:33:55 -0500 | [diff] [blame] | 1371 | if ( strcmp(adj_lsa->header->orig_router->name,nlsr->router_name) == 0) |
| 1372 | { |
| 1373 | if ( lsa_life_time > nlsr->lsa_refresh_time ) |
| 1374 | { |
| 1375 | printf("Own Adj LSA need to be refrshed\n"); |
| 1376 | |
| 1377 | char *current_time_stamp=(char *)malloc(20); |
| 1378 | memset(current_time_stamp,0,20); |
| 1379 | get_current_timestamp_micro(current_time_stamp); |
| 1380 | |
| 1381 | free(adj_lsa->header->orig_time); |
| 1382 | adj_lsa->header->orig_time=(char *)malloc(strlen(current_time_stamp)+1); //free |
| 1383 | memset(adj_lsa->header->orig_time,0,strlen(current_time_stamp)+1); |
| 1384 | memcpy(adj_lsa->header->orig_time,current_time_stamp,strlen(current_time_stamp)+1); |
| 1385 | |
| 1386 | free(current_time_stamp); |
| 1387 | |
| 1388 | |
| 1389 | printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version); |
| 1390 | set_new_lsdb_version(); |
| 1391 | printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version); |
| 1392 | |
| 1393 | print_adj_lsdb(); |
| 1394 | } |
| 1395 | } |
| 1396 | else |
| 1397 | { |
| 1398 | if ( lsa_life_time > nlsr->router_dead_interval ) |
| 1399 | { |
| 1400 | printf("Others Adj LSA need to be deleted\n"); |
| 1401 | |
| 1402 | char *key=(char *)malloc(adj_lsa->header->orig_router->length+2+2); |
| 1403 | memset(key,0,adj_lsa->header->orig_router->length+2); |
| 1404 | make_adj_lsa_key(key,adj_lsa); |
| 1405 | printf("Adjacent LSA key: %s \n",key); |
| 1406 | |
| 1407 | nlsr->event = ccn_schedule_event(nlsr->sched, 10, &delete_adj_lsa, (void *)key, 0); |
| 1408 | } |
| 1409 | } |
| 1410 | |
| 1411 | |
| 1412 | |
akmhoque | ffacaa8 | 2012-09-13 17:48:30 -0500 | [diff] [blame] | 1413 | hashtb_next(e); |
| 1414 | } |
| 1415 | |
| 1416 | hashtb_end(e); |
| 1417 | |
| 1418 | free(time_stamp); |
akmhoque | ffacaa8 | 2012-09-13 17:48:30 -0500 | [diff] [blame] | 1419 | } |
| 1420 | |
| 1421 | int |
| 1422 | refresh_lsdb(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags) |
| 1423 | { |
| 1424 | if(flags == CCN_SCHEDULE_CANCEL) |
| 1425 | { |
| 1426 | return -1; |
| 1427 | } |
| 1428 | |
| 1429 | nlsr_lock(); |
akmhoque | da5b683 | 2012-09-13 22:33:55 -0500 | [diff] [blame] | 1430 | |
akmhoque | ffacaa8 | 2012-09-13 17:48:30 -0500 | [diff] [blame] | 1431 | printf("refresh_lsdb called \n"); |
| 1432 | |
| 1433 | refresh_name_lsdb(); |
| 1434 | refresh_adj_lsdb(); |
| 1435 | |
akmhoque | ffacaa8 | 2012-09-13 17:48:30 -0500 | [diff] [blame] | 1436 | nlsr->event = ccn_schedule_event(nlsr->sched, 60000000, &refresh_lsdb, NULL, 0); |
akmhoque | da5b683 | 2012-09-13 22:33:55 -0500 | [diff] [blame] | 1437 | |
akmhoque | ffacaa8 | 2012-09-13 17:48:30 -0500 | [diff] [blame] | 1438 | nlsr_unlock(); |
| 1439 | return 0; |
| 1440 | } |