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