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