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