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 | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 141 | char lst[2]; |
| 142 | memset(lst,0,2); |
| 143 | sprintf(lst,"%d",name_lsa->header->ls_type); |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 144 | |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 145 | char lsid[10]; |
| 146 | memset(lsid,0,10); |
| 147 | sprintf(lsid,"%ld",name_lsa->header->ls_id); |
| 148 | |
| 149 | |
| 150 | char *key=(char *)malloc(strlen(name_lsa->header->orig_router->name)+1+strlen(lst)+1+strlen(lsid)+1); |
| 151 | memset(key,0,strlen(name_lsa->header->orig_router->name)+1+strlen(lst)+1+strlen(lsid)+1); |
| 152 | |
| 153 | |
| 154 | make_name_lsa_key(key, name_lsa->header->orig_router->name,name_lsa->header->ls_type,name_lsa->header->ls_id); |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 155 | printf("Key:%s Length:%d\n",key,(int)strlen(key)); |
| 156 | |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 157 | struct nlsa *new_name_lsa=(struct nlsa*)malloc(sizeof(struct nlsa )); //free |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 158 | |
| 159 | struct hashtb_enumerator ee; |
| 160 | struct hashtb_enumerator *e = ⅇ |
| 161 | int res; |
| 162 | |
| 163 | hashtb_start(nlsr->lsdb->name_lsdb, e); |
| 164 | res = hashtb_seek(e, key, strlen(key), 0); |
| 165 | |
| 166 | if(res == HT_NEW_ENTRY ) |
| 167 | { |
| 168 | printf("New Name LSA... Adding to LSDB\n"); |
| 169 | new_name_lsa = e->data; |
| 170 | |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 171 | new_name_lsa->header=(struct nlsa_header *)malloc(sizeof(struct nlsa_header )); //free |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 172 | new_name_lsa->header->ls_type=name_lsa->header->ls_type; |
| 173 | |
| 174 | new_name_lsa->header->orig_time=(char *)malloc(strlen(name_lsa->header->orig_time)+1); |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 175 | memset(new_name_lsa->header->orig_time,0,strlen(name_lsa->header->orig_time)+1); |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 176 | memcpy(new_name_lsa->header->orig_time,name_lsa->header->orig_time,strlen(name_lsa->header->orig_time)+1); |
| 177 | |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 178 | new_name_lsa->header->ls_id=name_lsa->header->ls_id; |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 179 | new_name_lsa->header->orig_router=(struct name_prefix *)malloc(sizeof(struct name_prefix )); //free |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 180 | new_name_lsa->header->orig_router->name=(char *)malloc(name_lsa->header->orig_router->length); |
| 181 | memcpy(new_name_lsa->header->orig_router->name,name_lsa->header->orig_router->name,name_lsa->header->orig_router->length); |
| 182 | new_name_lsa->header->orig_router->length=name_lsa->header->orig_router->length; |
| 183 | new_name_lsa->header->isValid=name_lsa->header->isValid; |
| 184 | |
| 185 | |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 186 | new_name_lsa->name_prefix=(struct name_prefix *)malloc(sizeof(struct name_prefix )); //free |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 187 | new_name_lsa->name_prefix->name=(char *)malloc(name_lsa->name_prefix->length); |
| 188 | memcpy(new_name_lsa->name_prefix->name,name_lsa->name_prefix->name,name_lsa->name_prefix->length); |
| 189 | new_name_lsa->name_prefix->length=name_lsa->name_prefix->length; |
| 190 | |
| 191 | printf("New Name LSA Added....\n"); |
| 192 | |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 193 | printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version); |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 194 | set_new_lsdb_version(); |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 195 | printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version); |
| 196 | |
| 197 | int next_hop=get_next_hop(new_name_lsa->header->orig_router->name); |
| 198 | if ( next_hop == NO_NEXT_HOP ) |
| 199 | { |
| 200 | int check=add_npt_entry(new_name_lsa->header->orig_router->name,new_name_lsa->name_prefix->name,NO_FACE); |
| 201 | if ( check == HT_NEW_ENTRY ) |
| 202 | { |
| 203 | printf("Added in npt \n"); |
| 204 | } |
| 205 | } |
| 206 | else |
| 207 | { |
| 208 | int check=add_npt_entry(new_name_lsa->header->orig_router->name,new_name_lsa->name_prefix->name,next_hop); |
| 209 | if ( check == HT_NEW_ENTRY ) |
| 210 | { |
| 211 | printf("Added in npt \n"); |
| 212 | } |
| 213 | |
| 214 | } |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 215 | |
| 216 | } |
| 217 | else if(res == HT_OLD_ENTRY) |
| 218 | { |
| 219 | printf("Duplicate Name LSA. Discarded...\n"); |
| 220 | |
| 221 | } |
| 222 | |
| 223 | hashtb_end(e); |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 224 | |
| 225 | free(key); |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 229 | void |
| 230 | print_name_lsa(struct nlsa *name_lsa) |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 231 | { |
| 232 | |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 233 | printf("-----------Name LSA Content---------------\n"); |
| 234 | printf(" Origination Router : %s\n",name_lsa->header->orig_router->name); |
| 235 | printf(" Origination Router Length: %d\n",name_lsa->header->orig_router->length); |
| 236 | printf(" LS Type : %d\n",name_lsa->header->ls_type); |
| 237 | printf(" LS Id : %ld\n",name_lsa->header->ls_id); |
| 238 | printf(" Origination Time : %s\n",name_lsa->header->orig_time); |
| 239 | printf(" Is Valid : %d\n",name_lsa->header->isValid); |
| 240 | printf(" LSA Data \n"); |
| 241 | printf(" Name Prefix: : %s\n",name_lsa->name_prefix->name); |
| 242 | printf(" Name Prefix Length : %d\n",name_lsa->name_prefix->length); |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 243 | |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 244 | printf("\n"); |
akmhoque | f71d908 | 2012-08-22 12:51:53 -0400 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | void |
| 248 | print_name_lsdb(void) |
| 249 | { |
| 250 | printf("print_name_lsdb called \n"); |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 251 | int i, name_lsdb_element; |
akmhoque | f71d908 | 2012-08-22 12:51:53 -0400 | [diff] [blame] | 252 | struct nlsa *name_lsa; |
| 253 | |
| 254 | struct hashtb_enumerator ee; |
| 255 | struct hashtb_enumerator *e = ⅇ |
| 256 | |
| 257 | hashtb_start(nlsr->lsdb->name_lsdb, e); |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 258 | name_lsdb_element=hashtb_n(nlsr->lsdb->name_lsdb); |
akmhoque | f71d908 | 2012-08-22 12:51:53 -0400 | [diff] [blame] | 259 | |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 260 | for(i=0;i<name_lsdb_element;i++) |
akmhoque | f71d908 | 2012-08-22 12:51:53 -0400 | [diff] [blame] | 261 | { |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 262 | printf("-----------Name LSA (%d)---------------\n",i+1); |
akmhoque | f71d908 | 2012-08-22 12:51:53 -0400 | [diff] [blame] | 263 | name_lsa=e->data; |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 264 | print_name_lsa(name_lsa); |
akmhoque | f71d908 | 2012-08-22 12:51:53 -0400 | [diff] [blame] | 265 | hashtb_next(e); |
| 266 | } |
| 267 | |
| 268 | hashtb_end(e); |
| 269 | |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 270 | printf("\n"); |
| 271 | } |
| 272 | |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 273 | |
| 274 | void |
| 275 | 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] | 276 | { |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 277 | printf("build_and_install_others_name_lsa called \n"); |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 278 | |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 279 | struct nlsa *name_lsa=(struct nlsa *)malloc(sizeof( struct nlsa )); |
| 280 | build_others_name_lsa(name_lsa,orig_router,ls_type,ls_id,orig_time, isValid,np); |
| 281 | print_name_lsa(name_lsa); |
| 282 | install_name_lsa(name_lsa); |
| 283 | print_name_lsdb(); |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 284 | |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 285 | |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 286 | free(name_lsa->header->orig_router->name); |
| 287 | free(name_lsa->header->orig_router); |
| 288 | free(name_lsa->header); |
| 289 | free(name_lsa->name_prefix->name); |
| 290 | free(name_lsa->name_prefix); |
| 291 | free(name_lsa); |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 292 | |
| 293 | } |
| 294 | |
| 295 | void |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 296 | 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] | 297 | { |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 298 | printf("build_others_name_lsa called \n"); |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 299 | |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 300 | name_lsa->header=(struct nlsa_header *)malloc(sizeof(struct nlsa_header )); |
| 301 | name_lsa->header->ls_type=LS_TYPE_NAME; |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 302 | |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 303 | name_lsa->header->orig_time=(char *)malloc(strlen(orig_time)+1); |
| 304 | memset(name_lsa->header->orig_time,0,strlen(orig_time)+1); |
| 305 | memcpy(name_lsa->header->orig_time,orig_time,strlen(orig_time)+1); |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 306 | |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 307 | name_lsa->header->ls_id=ls_id; |
| 308 | name_lsa->header->orig_router=(struct name_prefix *)malloc(sizeof(struct name_prefix )); |
| 309 | name_lsa->header->orig_router->name=(char *)malloc(strlen(orig_router)+1); |
| 310 | memset(name_lsa->header->orig_router->name,0,strlen(orig_router)+1); |
| 311 | memcpy(name_lsa->header->orig_router->name,orig_router,strlen(orig_router)+1); |
| 312 | name_lsa->header->orig_router->length=strlen(orig_router)+1; |
| 313 | name_lsa->header->isValid=isValid; |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 314 | |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 315 | name_lsa->name_prefix=(struct name_prefix *)malloc(sizeof(struct name_prefix )); |
| 316 | name_lsa->name_prefix->name=(char *)malloc(strlen(np)+1); |
| 317 | memset(name_lsa->name_prefix->name,0,strlen(np)+1); |
| 318 | memcpy(name_lsa->name_prefix->name,np,strlen(np)+1); |
| 319 | name_lsa->name_prefix->length=strlen(np)+1; |
akmhoque | f71d908 | 2012-08-22 12:51:53 -0400 | [diff] [blame] | 320 | } |
akmhoque | d79438d | 2012-08-27 13:31:42 -0500 | [diff] [blame] | 321 | |
| 322 | |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 323 | void |
| 324 | make_adj_lsa_key(char *key,struct alsa *adj_lsa) |
akmhoque | d79438d | 2012-08-27 13:31:42 -0500 | [diff] [blame] | 325 | { |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 326 | memcpy(key+strlen(key),adj_lsa->header->orig_router->name,adj_lsa->header->orig_router->length); |
| 327 | memcpy(key+strlen(key),"/",1); |
| 328 | char ls_type[2]; |
| 329 | sprintf(ls_type,"%d",adj_lsa->header->ls_type); |
| 330 | memcpy(key+strlen(key),ls_type,strlen(ls_type)); |
| 331 | key[strlen(key)]='\0'; |
| 332 | } |
| 333 | |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 334 | int |
| 335 | build_and_install_adj_lsa(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags) |
| 336 | { |
| 337 | printf("build_and_install_adj_lsa called \n"); |
| 338 | |
| 339 | printf("adj_build_flag = %d \n",nlsr->adj_build_flag); |
| 340 | |
| 341 | if(nlsr->adj_build_flag > 0) |
| 342 | { |
| 343 | printf("is_adj_lsa_build = %d \n",is_adj_lsa_build()); |
| 344 | if ( is_adj_lsa_build()> 0) |
| 345 | { |
| 346 | struct alsa *adj_lsa=(struct alsa *)malloc(sizeof( struct alsa )); |
| 347 | build_adj_lsa(adj_lsa); |
| 348 | install_adj_lsa(adj_lsa); |
| 349 | |
| 350 | free(adj_lsa->header->orig_router->name); |
| 351 | free(adj_lsa->header->orig_router); |
| 352 | free(adj_lsa->header->orig_time); |
| 353 | free(adj_lsa->header); |
| 354 | free(adj_lsa->body); |
| 355 | free(adj_lsa); |
| 356 | nlsr->adj_build_flag=0; |
| 357 | print_adj_lsdb(); |
| 358 | } |
| 359 | else |
| 360 | { |
| 361 | printf("Can not build adj LSA now\n"); |
| 362 | } |
| 363 | } |
| 364 | nlsr->is_build_adj_lsa_sheduled=0; |
| 365 | return 0; |
| 366 | } |
| 367 | |
| 368 | |
| 369 | void |
| 370 | build_adj_lsa(struct alsa * adj_lsa) |
| 371 | { |
| 372 | printf("build_adj_lsa called \n"); |
| 373 | |
| 374 | int no_link=no_active_nbr(); |
| 375 | printf("Number of link in Adjacent LSA: %d\n",no_link); |
| 376 | |
| 377 | /*Filling Up Header Data */ |
| 378 | adj_lsa->header=(struct alsa_header *)malloc(sizeof(struct alsa_header )); |
| 379 | adj_lsa->header->orig_router=(struct name_prefix *)malloc(sizeof(struct name_prefix )); |
| 380 | adj_lsa->header->orig_router->name=(char *)malloc(strlen(nlsr->router_name)+1); |
| 381 | memset(adj_lsa->header->orig_router->name,0,strlen(nlsr->router_name)+1); |
| 382 | memcpy(adj_lsa->header->orig_router->name,nlsr->router_name,strlen(nlsr->router_name)+1); |
| 383 | adj_lsa->header->orig_router->length=strlen(nlsr->router_name)+1; |
| 384 | |
| 385 | adj_lsa->header->ls_type=(unsigned)LS_TYPE_ADJ; |
| 386 | |
| 387 | char *time_stamp=(char *)malloc(20); |
| 388 | memset(time_stamp,0,20); |
| 389 | get_current_timestamp_micro(time_stamp); |
| 390 | |
| 391 | adj_lsa->header->orig_time=(char *)malloc(strlen(time_stamp)+1); |
| 392 | memset(adj_lsa->header->orig_time,0,strlen(time_stamp)+1); |
| 393 | memcpy(adj_lsa->header->orig_time,time_stamp,strlen(time_stamp)+1); |
| 394 | free(time_stamp); |
| 395 | |
| 396 | |
| 397 | /* Filling Up Body Data */ |
| 398 | |
| 399 | adj_lsa->no_link=no_link; |
| 400 | /* |
| 401 | struct link *templ=(struct link *)malloc(2*sizeof(struct link)); |
| 402 | adj_lsa->links=templ; |
| 403 | |
| 404 | int i, adl_element; |
| 405 | struct ndn_neighbor *nbr; |
| 406 | struct hashtb_enumerator ee; |
| 407 | struct hashtb_enumerator *e = ⅇ |
| 408 | hashtb_start(nlsr->adl, e); |
| 409 | adl_element=hashtb_n(nlsr->adl); |
| 410 | |
| 411 | for(i=0;i<adl_element;i++) |
| 412 | { |
| 413 | nbr=e->data; |
| 414 | if( nbr->status == 1 ) |
| 415 | { |
| 416 | struct link *temp=(struct link *)malloc(sizeof(struct link)); |
| 417 | temp->nbr=(struct name_prefix *)malloc(sizeof(struct name_prefix)); |
| 418 | temp->nbr->name=(char *)malloc(nbr->neighbor->length); |
| 419 | memset(temp->nbr->name,0,nbr->neighbor->length); |
| 420 | memcpy(temp->nbr->name,nbr->neighbor->name,nbr->neighbor->length); |
| 421 | |
| 422 | temp->nbr->length=nbr->neighbor->length; |
| 423 | temp->face=nbr->face; |
| 424 | temp->metric=nbr->metric; |
| 425 | |
| 426 | templ=temp; |
| 427 | templ++; |
| 428 | } |
| 429 | hashtb_next(e); |
| 430 | } |
| 431 | |
| 432 | hashtb_end(e); |
| 433 | */ |
| 434 | |
| 435 | |
| 436 | struct ccn_charbuf *c=ccn_charbuf_create(); |
| 437 | get_active_nbr_adj_data(c); |
| 438 | char *data=ccn_charbuf_as_string(c); |
| 439 | |
| 440 | adj_lsa->body=(char *)malloc(strlen(data)+1); |
| 441 | memset(adj_lsa->body,0,strlen(data)+1); |
| 442 | memcpy(adj_lsa->body,(char *)data,strlen(data)+1); |
| 443 | ccn_charbuf_destroy(&c); |
| 444 | |
| 445 | |
| 446 | |
| 447 | if( !nlsr->is_send_lsdb_interest_scheduled ) |
| 448 | { |
| 449 | nlsr->event_send_lsdb_interest= ccn_schedule_event(nlsr->sched, 1000, &send_lsdb_interest, NULL, 0); |
| 450 | nlsr->is_send_lsdb_interest_scheduled=1; |
| 451 | } |
| 452 | |
| 453 | nlsr->adj_build_count++; |
| 454 | |
| 455 | |
| 456 | } |
| 457 | |
| 458 | |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 459 | void |
| 460 | install_adj_lsa(struct alsa * adj_lsa) |
| 461 | { |
akmhoque | d79438d | 2012-08-27 13:31:42 -0500 | [diff] [blame] | 462 | printf("install_adj_lsa called \n"); |
| 463 | |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 464 | char *key=(char *)malloc(adj_lsa->header->orig_router->length+2+2); |
| 465 | memset(key,0,adj_lsa->header->orig_router->length+2); |
| 466 | make_adj_lsa_key(key,adj_lsa); |
| 467 | printf("Adjacent LSA key: %s \n",key); |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 468 | |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 469 | struct alsa *new_adj_lsa=(struct alsa*)malloc(sizeof(struct alsa )); |
| 470 | |
| 471 | struct hashtb_enumerator ee; |
| 472 | struct hashtb_enumerator *e = ⅇ |
| 473 | int res; |
| 474 | |
| 475 | hashtb_start(nlsr->lsdb->adj_lsdb, e); |
| 476 | res = hashtb_seek(e, key, strlen(key), 0); |
| 477 | |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 478 | |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 479 | |
| 480 | if(res == HT_NEW_ENTRY ) |
| 481 | { |
| 482 | printf("New ADJ LSA... Adding to LSDB\n"); |
| 483 | new_adj_lsa = e->data; |
| 484 | |
| 485 | new_adj_lsa->header=(struct alsa_header *)malloc(sizeof(struct alsa_header )); |
| 486 | new_adj_lsa->header->ls_type=adj_lsa->header->ls_type; |
| 487 | new_adj_lsa->header->orig_time=(char *)malloc(strlen(adj_lsa->header->orig_time)+1); |
| 488 | memcpy(new_adj_lsa->header->orig_time,adj_lsa->header->orig_time,strlen(adj_lsa->header->orig_time)+1); |
| 489 | |
| 490 | new_adj_lsa->header->orig_router=(struct name_prefix *)malloc(sizeof(struct name_prefix )); |
| 491 | new_adj_lsa->header->orig_router->name=(char *)malloc(adj_lsa->header->orig_router->length); |
| 492 | memcpy(new_adj_lsa->header->orig_router->name,adj_lsa->header->orig_router->name,adj_lsa->header->orig_router->length); |
| 493 | new_adj_lsa->header->orig_router->length=adj_lsa->header->orig_router->length; |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 494 | |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 495 | new_adj_lsa->no_link=adj_lsa->no_link; |
| 496 | |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 497 | new_adj_lsa->body=(char *)malloc(strlen(adj_lsa->body)+1); |
| 498 | memset(new_adj_lsa->body,0,strlen(adj_lsa->body)+1); |
| 499 | memcpy(new_adj_lsa->body,adj_lsa->body,strlen(adj_lsa->body)+1); |
| 500 | |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 501 | printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version); |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 502 | set_new_lsdb_version(); |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 503 | printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version); |
| 504 | |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 505 | add_next_hop_router(new_adj_lsa->header->orig_router->name); |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 506 | |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 507 | add_next_hop_from_lsa_adj_body(new_adj_lsa->body,new_adj_lsa->no_link); |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 508 | } |
| 509 | else if(res == HT_OLD_ENTRY) |
| 510 | { |
| 511 | new_adj_lsa = e->data; |
| 512 | if(strcmp(adj_lsa->header->orig_time,new_adj_lsa->header->orig_time)<=0) |
| 513 | { |
| 514 | printf("Older/Duplicate Adj LSA. Discarded...\n"); |
| 515 | } |
| 516 | else |
| 517 | { |
| 518 | new_adj_lsa = e->data; |
| 519 | |
| 520 | free(new_adj_lsa->header->orig_time); |
| 521 | new_adj_lsa->header->orig_time=(char *)malloc(strlen(adj_lsa->header->orig_time)+1); |
| 522 | memcpy(new_adj_lsa->header->orig_time,adj_lsa->header->orig_time,strlen(adj_lsa->header->orig_time)+1); |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 523 | |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 524 | new_adj_lsa->no_link=adj_lsa->no_link; |
| 525 | |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 526 | new_adj_lsa->body=(char *)malloc(strlen(adj_lsa->body)+1); |
| 527 | memset(new_adj_lsa->body,0,strlen(adj_lsa->body)+1); |
| 528 | memcpy(new_adj_lsa->body,adj_lsa->body,strlen(adj_lsa->body)+1); |
| 529 | |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 530 | printf("Old Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version); |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 531 | set_new_lsdb_version(); |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 532 | printf("New Version Number of LSDB: %s \n",nlsr->lsdb->lsdb_version); |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 533 | |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 534 | add_next_hop_from_lsa_adj_body(new_adj_lsa->body,new_adj_lsa->no_link); |
| 535 | |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | } |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 539 | hashtb_end(e); |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 540 | |
akmhoque | 29c1db5 | 2012-09-07 14:47:43 -0500 | [diff] [blame] | 541 | if ( !nlsr->is_route_calculation_scheduled ) |
| 542 | { |
| 543 | nlsr->event_calculate_route = ccn_schedule_event(nlsr->sched, 1000000, &route_calculate, NULL, 0); |
| 544 | nlsr->is_route_calculation_scheduled=1; |
| 545 | } |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 546 | free(key); |
| 547 | } |
| 548 | |
| 549 | void |
| 550 | print_adj_lsa_body(const char *body, int no_link) |
| 551 | { |
| 552 | int i=0; |
| 553 | char *lsa_data=(char *)malloc(strlen(body)+1); |
| 554 | memset( lsa_data,0,strlen(body)+1); |
| 555 | memcpy(lsa_data,body,strlen(body)+1); |
| 556 | char *sep="|"; |
| 557 | char *rem; |
| 558 | char *rtr_id; |
| 559 | char *length; |
| 560 | char *face; |
| 561 | char *metric; |
| 562 | |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 563 | if(no_link >0 ) |
| 564 | { |
| 565 | rtr_id=strtok_r(lsa_data,sep,&rem); |
| 566 | length=strtok_r(NULL,sep,&rem); |
| 567 | face=strtok_r(NULL,sep,&rem); |
| 568 | metric=strtok_r(NULL,sep,&rem); |
| 569 | |
| 570 | printf(" Link %d \n",i+1); |
| 571 | printf(" Neighbor : %s \n",rtr_id); |
| 572 | printf(" Neighbor Length : %s \n",length); |
| 573 | printf(" Connecting Face : %s \n",face); |
| 574 | printf(" Metric : %s \n",metric); |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 575 | |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 576 | |
| 577 | for(i=1;i<no_link;i++) |
| 578 | { |
| 579 | rtr_id=strtok_r(NULL,sep,&rem); |
| 580 | length=strtok_r(NULL,sep,&rem); |
| 581 | face=strtok_r(NULL,sep,&rem); |
| 582 | metric=strtok_r(NULL,sep,&rem); |
| 583 | printf(" Link %d \n",i+1); |
| 584 | printf(" Neighbor : %s \n",rtr_id); |
| 585 | printf(" Neighbor Length : %s \n",length); |
| 586 | printf(" Connecting Face : %s \n",face); |
| 587 | printf(" Metric : %s \n",metric); |
| 588 | |
| 589 | } |
| 590 | } |
| 591 | |
| 592 | free(lsa_data); |
| 593 | } |
| 594 | |
| 595 | void |
| 596 | print_adj_lsa(struct alsa * adj_lsa) |
| 597 | { |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 598 | |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 599 | printf("print_adj_lsa called \n"); |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 600 | |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 601 | printf("-----------ADJ LSA Content---------------\n"); |
| 602 | printf(" Origination Router : %s\n",adj_lsa->header->orig_router->name); |
| 603 | printf(" Origination Router Length: %d\n",adj_lsa->header->orig_router->length); |
| 604 | printf(" LS Type : %d\n",adj_lsa->header->ls_type); |
| 605 | printf(" Origination Time : %s\n",adj_lsa->header->orig_time); |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 606 | printf(" Lsa Data:\n"); |
| 607 | printf(" No of Link : %d\n",adj_lsa->no_link); |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 608 | |
| 609 | print_adj_lsa_body(adj_lsa->body,adj_lsa->no_link); |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 610 | |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 611 | /* |
| 612 | struct link *templ=adj_lsa->links; |
| 613 | int i; |
| 614 | |
| 615 | for(i=0 ; i< adj_lsa->no_link ; i++) |
| 616 | { |
| 617 | printf(" Link %d \n",i+1); |
| 618 | printf(" Neighbor : %s \n",templ->nbr->name); |
| 619 | printf(" Neighbor Length : %d \n",templ->nbr->length); |
| 620 | printf(" Connecting Face : %d \n",templ->face); |
| 621 | printf(" Metric : %d \n",templ->metric); |
| 622 | |
| 623 | templ++; |
| 624 | } |
| 625 | */ |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 626 | printf("\n"); |
| 627 | |
| 628 | } |
| 629 | |
| 630 | void |
| 631 | print_adj_lsdb(void) |
| 632 | { |
| 633 | printf("print_name_lsdb called \n"); |
| 634 | int i, adj_lsdb_element; |
| 635 | struct alsa *adj_lsa; |
| 636 | |
| 637 | struct hashtb_enumerator ee; |
| 638 | struct hashtb_enumerator *e = ⅇ |
| 639 | |
| 640 | hashtb_start(nlsr->lsdb->adj_lsdb, e); |
| 641 | adj_lsdb_element=hashtb_n(nlsr->lsdb->adj_lsdb); |
| 642 | |
| 643 | for(i=0;i<adj_lsdb_element;i++) |
| 644 | { |
| 645 | printf("-----------Adj LSA (%d)---------------\n",i+1); |
| 646 | adj_lsa=e->data; |
| 647 | print_adj_lsa(adj_lsa); |
| 648 | hashtb_next(e); |
| 649 | } |
| 650 | |
| 651 | hashtb_end(e); |
| 652 | |
| 653 | printf("\n"); |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 654 | } |
| 655 | |
| 656 | void |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 657 | 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] | 658 | { |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 659 | printf("build_and_install_others_adj_lsa called \n"); |
| 660 | struct alsa *adj_lsa=(struct alsa *)malloc(sizeof( struct alsa )); |
| 661 | build_others_adj_lsa(adj_lsa,orig_router,ls_type,orig_time,no_link,data); |
| 662 | //print_adj_lsa(adj_lsa); |
| 663 | install_adj_lsa(adj_lsa); |
| 664 | |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 665 | |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 666 | free(adj_lsa->header->orig_router->name); |
| 667 | free(adj_lsa->header->orig_router); |
| 668 | free(adj_lsa->header->orig_time); |
| 669 | free(adj_lsa->header); |
| 670 | free(adj_lsa->body); |
| 671 | free(adj_lsa); |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 672 | |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 673 | print_adj_lsdb(); |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 674 | |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 675 | } |
| 676 | |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 677 | |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 678 | void |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 679 | 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] | 680 | { |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 681 | printf("build_others_adj_lsa called \n"); |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 682 | |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 683 | /*Filling Up Header Data */ |
| 684 | adj_lsa->header=(struct alsa_header *)malloc(sizeof(struct alsa_header )); |
| 685 | adj_lsa->header->orig_router=(struct name_prefix *)malloc(sizeof(struct name_prefix )); |
| 686 | adj_lsa->header->orig_router->name=(char *)malloc(strlen(orig_router)+1); |
| 687 | memset(adj_lsa->header->orig_router->name,0,strlen(orig_router)+1); |
| 688 | memcpy(adj_lsa->header->orig_router->name,orig_router,strlen(orig_router)+1); |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 689 | |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 690 | adj_lsa->header->orig_router->length=strlen(orig_router)+1; |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 691 | |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 692 | |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 693 | adj_lsa->header->ls_type=(unsigned)LS_TYPE_ADJ; |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 694 | |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 695 | adj_lsa->header->orig_time=(char *)malloc(strlen(orig_time)+1); |
| 696 | memset(adj_lsa->header->orig_time,0,strlen(orig_time)+1); |
| 697 | memcpy(adj_lsa->header->orig_time,orig_time,strlen(orig_time)+1); |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 698 | |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 699 | adj_lsa->no_link=no_link; |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 700 | |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 701 | adj_lsa->body=(char *)malloc(strlen(data)+1); |
| 702 | memset(adj_lsa->body,0,strlen(data)+1); |
| 703 | memcpy(adj_lsa->body,(char *)data,strlen(data)+1); |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 704 | |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 705 | } |
| 706 | |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 707 | |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 708 | long int |
| 709 | get_name_lsdb_num_element(void) |
| 710 | { |
| 711 | long int num_element; |
| 712 | |
| 713 | |
| 714 | struct hashtb_enumerator ee; |
| 715 | struct hashtb_enumerator *e = ⅇ |
| 716 | |
| 717 | hashtb_start(nlsr->lsdb->name_lsdb, e); |
| 718 | num_element=hashtb_n(nlsr->lsdb->name_lsdb); |
| 719 | hashtb_end(e); |
| 720 | |
| 721 | return num_element; |
| 722 | } |
| 723 | |
| 724 | long int |
| 725 | get_adj_lsdb_num_element(void) |
| 726 | { |
| 727 | long int num_element; |
| 728 | |
| 729 | |
| 730 | struct hashtb_enumerator ee; |
| 731 | struct hashtb_enumerator *e = ⅇ |
| 732 | |
| 733 | hashtb_start(nlsr->lsdb->adj_lsdb, e); |
| 734 | num_element=hashtb_n(nlsr->lsdb->adj_lsdb); |
| 735 | hashtb_end(e); |
| 736 | |
| 737 | return num_element; |
akmhoque | d79438d | 2012-08-27 13:31:42 -0500 | [diff] [blame] | 738 | } |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 739 | |
| 740 | void |
| 741 | get_name_lsdb_summary(struct ccn_charbuf *name_lsdb_data) |
| 742 | { |
| 743 | printf("get_name_lsdb_summary called \n"); |
| 744 | int i, name_lsdb_element; |
| 745 | |
| 746 | struct nlsa *name_lsa; |
| 747 | struct hashtb_enumerator ee; |
| 748 | struct hashtb_enumerator *e = ⅇ |
| 749 | |
| 750 | hashtb_start(nlsr->lsdb->name_lsdb, e); |
| 751 | name_lsdb_element=hashtb_n(nlsr->lsdb->name_lsdb); |
| 752 | |
| 753 | for(i=0;i<name_lsdb_element;i++) |
| 754 | { |
| 755 | name_lsa=e->data; |
| 756 | |
| 757 | ccn_charbuf_append_string(name_lsdb_data,name_lsa->header->orig_router->name); |
| 758 | ccn_charbuf_append_string(name_lsdb_data,"|"); |
| 759 | |
| 760 | char *lst=(char *)malloc(20); |
| 761 | memset(lst,0,20); |
| 762 | sprintf(lst,"%d",name_lsa->header->ls_type); |
| 763 | ccn_charbuf_append_string(name_lsdb_data,lst); |
| 764 | free(lst); |
| 765 | ccn_charbuf_append_string(name_lsdb_data,"|"); |
| 766 | |
| 767 | char *lsid=(char *)malloc(20); |
| 768 | memset(lsid,0,20); |
| 769 | sprintf(lsid,"%ld",name_lsa->header->ls_id); |
| 770 | ccn_charbuf_append_string(name_lsdb_data,lsid); |
| 771 | free(lsid); |
| 772 | ccn_charbuf_append_string(name_lsdb_data,"|"); |
| 773 | |
| 774 | ccn_charbuf_append_string(name_lsdb_data,name_lsa->header->orig_time); |
| 775 | ccn_charbuf_append_string(name_lsdb_data,"|"); |
| 776 | |
| 777 | hashtb_next(e); |
| 778 | } |
| 779 | |
| 780 | hashtb_end(e); |
| 781 | |
| 782 | } |
| 783 | |
| 784 | |
| 785 | void |
| 786 | get_adj_lsdb_summary(struct ccn_charbuf *adj_lsdb_data) |
| 787 | { |
| 788 | printf("get_adj_lsdb_summary called \n"); |
| 789 | int i, adj_lsdb_element; |
| 790 | struct alsa *adj_lsa; |
| 791 | |
| 792 | struct hashtb_enumerator ee; |
| 793 | struct hashtb_enumerator *e = ⅇ |
| 794 | |
| 795 | hashtb_start(nlsr->lsdb->adj_lsdb, e); |
| 796 | adj_lsdb_element=hashtb_n(nlsr->lsdb->adj_lsdb); |
| 797 | |
| 798 | for(i=0;i<adj_lsdb_element;i++) |
| 799 | { |
| 800 | adj_lsa=e->data; |
| 801 | |
| 802 | ccn_charbuf_append_string(adj_lsdb_data,adj_lsa->header->orig_router->name); |
| 803 | ccn_charbuf_append_string(adj_lsdb_data,"|"); |
| 804 | |
| 805 | char *lst=(char *)malloc(20); |
| 806 | memset(lst,0,20); |
| 807 | sprintf(lst,"%d",adj_lsa->header->ls_type); |
| 808 | ccn_charbuf_append_string(adj_lsdb_data,lst); |
| 809 | free(lst); |
| 810 | ccn_charbuf_append_string(adj_lsdb_data,"|"); |
| 811 | |
| 812 | ccn_charbuf_append_string(adj_lsdb_data,adj_lsa->header->orig_time); |
| 813 | ccn_charbuf_append_string(adj_lsdb_data,"|"); |
| 814 | |
| 815 | hashtb_next(e); |
| 816 | } |
| 817 | |
| 818 | hashtb_end(e); |
| 819 | } |
| 820 | |
| 821 | |
| 822 | void |
| 823 | get_lsdb_summary(struct ccn_charbuf *lsdb_data) |
| 824 | { |
| 825 | struct ccn_charbuf *name_lsdb_data=ccn_charbuf_create(); |
| 826 | struct ccn_charbuf *adj_lsdb_data=ccn_charbuf_create(); |
| 827 | |
| 828 | get_name_lsdb_summary(name_lsdb_data); |
| 829 | get_adj_lsdb_summary(adj_lsdb_data); |
| 830 | |
| 831 | long int num_lsa=get_name_lsdb_num_element() + get_adj_lsdb_num_element(); |
| 832 | char *num_element=(char *)malloc(15); |
| 833 | memset(num_element,0,15); |
| 834 | sprintf(num_element,"%ld",num_lsa); |
| 835 | |
| 836 | if( num_lsa > 0) |
| 837 | { |
| 838 | ccn_charbuf_append_string(lsdb_data,num_element); |
| 839 | ccn_charbuf_append_string(lsdb_data,"|"); |
| 840 | } |
| 841 | if(name_lsdb_data->length>0) |
| 842 | { |
| 843 | char *data1=ccn_charbuf_as_string(name_lsdb_data); |
| 844 | ccn_charbuf_append_string(lsdb_data,(char *)data1); |
| 845 | } |
| 846 | if(adj_lsdb_data->length>0) |
| 847 | { |
| 848 | char *data2=ccn_charbuf_as_string(adj_lsdb_data); |
| 849 | ccn_charbuf_append_string(lsdb_data,(char *)data2); |
| 850 | } |
| 851 | ccn_charbuf_destroy(&name_lsdb_data); |
| 852 | ccn_charbuf_destroy(&adj_lsdb_data); |
| 853 | free(num_element); |
| 854 | |
| 855 | } |
| 856 | |
| 857 | int |
| 858 | check_is_new_name_lsa(char *orig_router,char *lst,char *lsid,char *orig_time) |
| 859 | { |
| 860 | int ret=0; |
| 861 | struct ccn_charbuf *key=ccn_charbuf_create(); |
| 862 | ccn_charbuf_append_string(key,orig_router); |
| 863 | ccn_charbuf_append_string(key,"/"); |
| 864 | ccn_charbuf_append_string(key,lst); |
| 865 | ccn_charbuf_append_string(key,"/"); |
| 866 | ccn_charbuf_append_string(key,lsid); |
| 867 | |
| 868 | int res; |
| 869 | struct nlsa *name_lsa; |
| 870 | |
| 871 | struct hashtb_enumerator ee; |
| 872 | struct hashtb_enumerator *e = ⅇ |
| 873 | |
| 874 | hashtb_start(nlsr->lsdb->name_lsdb, e); |
| 875 | res = hashtb_seek(e, ccn_charbuf_as_string(key), key->length, 0); |
| 876 | |
| 877 | if( res == HT_NEW_ENTRY ) |
| 878 | { |
| 879 | hashtb_delete(e); |
| 880 | ret=1; |
| 881 | |
| 882 | } |
| 883 | else if(res == HT_OLD_ENTRY) |
| 884 | { |
| 885 | name_lsa=e->data; |
| 886 | if( strcmp ( orig_time , name_lsa->header->orig_time ) > 0 ) |
| 887 | { |
| 888 | ret=1; |
| 889 | } |
| 890 | } |
| 891 | |
| 892 | hashtb_end(e); |
| 893 | |
| 894 | ccn_charbuf_destroy(&key); |
| 895 | |
| 896 | return ret; |
| 897 | } |
| 898 | |
| 899 | int |
| 900 | check_is_new_adj_lsa(char *orig_router,char *lst,char *orig_time) |
| 901 | { |
| 902 | int ret=0; |
| 903 | struct ccn_charbuf *key=ccn_charbuf_create(); |
| 904 | ccn_charbuf_append_string(key,orig_router); |
| 905 | ccn_charbuf_append_string(key,"/"); |
| 906 | ccn_charbuf_append_string(key,lst); |
| 907 | |
| 908 | int res; |
| 909 | struct alsa *adj_lsa; |
| 910 | |
| 911 | struct hashtb_enumerator ee; |
| 912 | struct hashtb_enumerator *e = ⅇ |
| 913 | |
| 914 | hashtb_start(nlsr->lsdb->adj_lsdb, e); |
| 915 | res = hashtb_seek(e, ccn_charbuf_as_string(key), key->length, 0); |
| 916 | |
| 917 | if( res == HT_NEW_ENTRY ) |
| 918 | { |
| 919 | hashtb_delete(e); |
| 920 | ret=1; |
| 921 | |
| 922 | } |
| 923 | else if(res == HT_OLD_ENTRY) |
| 924 | { |
| 925 | adj_lsa=e->data; |
| 926 | if( strcmp ( orig_time , adj_lsa->header->orig_time ) > 0 ) |
| 927 | { |
| 928 | ret=1; |
| 929 | } |
| 930 | } |
| 931 | |
| 932 | hashtb_end(e); |
| 933 | |
| 934 | ccn_charbuf_destroy(&key); |
| 935 | |
| 936 | return ret; |
| 937 | } |
| 938 | |
| 939 | void |
| 940 | get_name_lsa_data(struct ccn_charbuf *lsa_data, struct name_prefix *lsaId) |
| 941 | { |
| 942 | printf("get_name_lsa_data called \n"); |
| 943 | |
| 944 | struct nlsa *name_lsa=(struct nlsa*)malloc(sizeof(struct nlsa )); |
| 945 | |
| 946 | struct hashtb_enumerator ee; |
| 947 | struct hashtb_enumerator *e = ⅇ |
| 948 | int res; |
| 949 | |
| 950 | hashtb_start(nlsr->lsdb->name_lsdb, e); |
| 951 | res = hashtb_seek(e, lsaId->name, lsaId->length-1, 0); |
| 952 | |
| 953 | if( res == HT_OLD_ENTRY ) |
| 954 | { |
| 955 | name_lsa=e->data; |
| 956 | printf("NAME LSA found\n"); |
| 957 | |
| 958 | ccn_charbuf_append_string(lsa_data,name_lsa->header->orig_router->name); |
| 959 | ccn_charbuf_append_string(lsa_data,"|"); |
| 960 | |
| 961 | char *temp_length=(char *)malloc(20); |
| 962 | memset(temp_length,0,20); |
| 963 | sprintf(temp_length,"%d",name_lsa->header->orig_router->length); |
| 964 | ccn_charbuf_append_string(lsa_data,temp_length); |
| 965 | free(temp_length); |
| 966 | ccn_charbuf_append_string(lsa_data,"|"); |
| 967 | |
| 968 | char *temp_ltype=(char *)malloc(20); |
| 969 | memset(temp_ltype,0,20); |
| 970 | sprintf(temp_ltype,"%d",name_lsa->header->ls_type); |
| 971 | ccn_charbuf_append_string(lsa_data,temp_ltype); |
| 972 | free(temp_ltype); |
| 973 | ccn_charbuf_append_string(lsa_data,"|"); |
| 974 | |
| 975 | char *temp_lsid=(char *)malloc(20); |
| 976 | memset(temp_lsid,0,20); |
| 977 | sprintf(temp_lsid,"%ld",name_lsa->header->ls_id); |
| 978 | ccn_charbuf_append_string(lsa_data,temp_lsid); |
| 979 | free(temp_lsid); |
| 980 | ccn_charbuf_append_string(lsa_data,"|"); |
| 981 | |
| 982 | ccn_charbuf_append_string(lsa_data,name_lsa->header->orig_time); |
| 983 | ccn_charbuf_append_string(lsa_data,"|"); |
| 984 | |
| 985 | char *temp_valid=(char *)malloc(20); |
| 986 | memset(temp_valid,0,20); |
| 987 | sprintf(temp_valid,"%d",name_lsa->header->isValid); |
| 988 | ccn_charbuf_append_string(lsa_data,temp_valid); |
| 989 | free(temp_valid); |
| 990 | ccn_charbuf_append_string(lsa_data,"|"); |
| 991 | |
| 992 | ccn_charbuf_append_string(lsa_data,name_lsa->name_prefix->name); |
| 993 | ccn_charbuf_append_string(lsa_data,"|"); |
| 994 | |
| 995 | char *temp_npl=(char *)malloc(20); |
| 996 | memset(temp_npl,0,20); |
| 997 | sprintf(temp_npl,"%d",name_lsa->name_prefix->length); |
| 998 | ccn_charbuf_append_string(lsa_data,temp_npl); |
| 999 | free(temp_npl); |
| 1000 | ccn_charbuf_append_string(lsa_data,"|"); |
| 1001 | |
| 1002 | } |
| 1003 | else if(res == HT_NEW_ENTRY) |
| 1004 | { |
| 1005 | hashtb_delete(e); |
| 1006 | } |
| 1007 | |
| 1008 | hashtb_end(e); |
| 1009 | } |
| 1010 | |
| 1011 | void |
| 1012 | get_adj_lsa_data(struct ccn_charbuf *lsa_data,struct name_prefix *lsaId) |
| 1013 | { |
| 1014 | printf("get_adj_lsa_data called \n"); |
| 1015 | |
| 1016 | struct alsa *adj_lsa=(struct alsa*)malloc(sizeof(struct alsa )); |
| 1017 | |
| 1018 | struct hashtb_enumerator ee; |
| 1019 | struct hashtb_enumerator *e = ⅇ |
| 1020 | int res; |
| 1021 | |
| 1022 | hashtb_start(nlsr->lsdb->adj_lsdb, e); |
| 1023 | res = hashtb_seek(e, lsaId->name, lsaId->length-1, 0); |
| 1024 | |
| 1025 | if( res == HT_OLD_ENTRY ) |
| 1026 | { |
| 1027 | adj_lsa=e->data; |
| 1028 | printf("NAME LSA found\n"); |
| 1029 | |
| 1030 | ccn_charbuf_append_string(lsa_data,adj_lsa->header->orig_router->name); |
| 1031 | ccn_charbuf_append_string(lsa_data,"|"); |
| 1032 | |
| 1033 | char *temp_length=(char *)malloc(20); |
| 1034 | memset(temp_length,0,20); |
| 1035 | sprintf(temp_length,"%d",adj_lsa->header->orig_router->length); |
| 1036 | ccn_charbuf_append_string(lsa_data,temp_length); |
| 1037 | free(temp_length); |
| 1038 | ccn_charbuf_append_string(lsa_data,"|"); |
| 1039 | |
| 1040 | char *temp_ltype=(char *)malloc(20); |
| 1041 | memset(temp_ltype,0,20); |
| 1042 | sprintf(temp_ltype,"%d",adj_lsa->header->ls_type); |
| 1043 | ccn_charbuf_append_string(lsa_data,temp_ltype); |
| 1044 | free(temp_ltype); |
| 1045 | ccn_charbuf_append_string(lsa_data,"|"); |
| 1046 | |
| 1047 | ccn_charbuf_append_string(lsa_data,adj_lsa->header->orig_time); |
| 1048 | ccn_charbuf_append_string(lsa_data,"|"); |
| 1049 | |
| 1050 | char *temp_nl=(char *)malloc(20); |
| 1051 | memset(temp_nl,0,20); |
| 1052 | sprintf(temp_nl,"%d",adj_lsa->no_link); |
| 1053 | ccn_charbuf_append_string(lsa_data,temp_nl); |
| 1054 | free(temp_nl); |
| 1055 | ccn_charbuf_append_string(lsa_data,"|"); |
| 1056 | |
| 1057 | ccn_charbuf_append_string(lsa_data,adj_lsa->body); |
| 1058 | |
| 1059 | |
| 1060 | } |
| 1061 | else if(res == HT_NEW_ENTRY) |
| 1062 | { |
| 1063 | hashtb_delete(e); |
| 1064 | } |
| 1065 | |
| 1066 | hashtb_end(e); |
| 1067 | } |