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