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