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" |
| 21 | #include "nlsr_lsdb.h" |
akmhoque | f71d908 | 2012-08-22 12:51:53 -0400 | [diff] [blame] | 22 | #include "utility.h" |
akmhoque | 438b07e | 2012-08-21 10:13:57 -0400 | [diff] [blame] | 23 | |
akmhoque | f71d908 | 2012-08-22 12:51:53 -0400 | [diff] [blame] | 24 | void |
| 25 | make_name_lsa_key(struct ccn_charbuf *key, struct ccn_charbuf *orig_router, unsigned int ls_type, long int nlsa_id, long int orig_time) |
| 26 | { |
| 27 | printf("make_name_lsa_key called \n"); |
| 28 | ccn_charbuf_append_string(key,ccn_charbuf_as_string(orig_router)); |
akmhoque | 438b07e | 2012-08-21 10:13:57 -0400 | [diff] [blame] | 29 | |
akmhoque | f71d908 | 2012-08-22 12:51:53 -0400 | [diff] [blame] | 30 | struct ccn_charbuf *c=ccn_charbuf_create(); |
| 31 | ccn_charbuf_reset(c); |
| 32 | ccn_charbuf_putf(c, "%d", (unsigned)ls_type); |
| 33 | ccn_charbuf_append_string(key,ccn_charbuf_as_string(c)); |
| 34 | |
| 35 | ccn_charbuf_reset(c); |
| 36 | ccn_charbuf_putf(c, "%d", (unsigned)nlsa_id); |
| 37 | ccn_charbuf_append_string(key,ccn_charbuf_as_string(c)); |
| 38 | |
| 39 | ccn_charbuf_reset(c); |
| 40 | ccn_charbuf_putf(c, "%ld", (unsigned)orig_time); |
| 41 | ccn_charbuf_append_string(key,ccn_charbuf_as_string(c)); |
| 42 | |
| 43 | ccn_charbuf_destroy(&c); |
| 44 | |
| 45 | printf("Key: %s length: %d\n",ccn_charbuf_as_string(key),(int)key->length); |
| 46 | |
| 47 | } |
| 48 | |
| 49 | void |
| 50 | install_name_lsa(struct nlsa *new_name_lsa) |
| 51 | { |
| 52 | printf("install_name_lsa called \n"); |
| 53 | struct ccn_charbuf *key=ccn_charbuf_create(); |
| 54 | make_name_lsa_key(key,new_name_lsa->header->orig_router,new_name_lsa->header->ls_type,new_name_lsa->header->ls_id,new_name_lsa->header->orig_time); |
| 55 | |
| 56 | struct nlsa *name_lsa=(struct nlsa*)malloc(sizeof(struct nlsa *)); |
| 57 | |
| 58 | struct hashtb_enumerator ee; |
| 59 | struct hashtb_enumerator *e = ⅇ |
| 60 | int res; |
| 61 | |
| 62 | hashtb_start(nlsr->lsdb->name_lsdb, e); |
| 63 | res = hashtb_seek(e, key->buf , key->length, 0); |
| 64 | |
| 65 | if(res == HT_NEW_ENTRY ) |
| 66 | { |
| 67 | printf("New Name LSA. Added...\n"); |
| 68 | name_lsa = e->data; |
| 69 | |
| 70 | name_lsa->header=(struct nlsa_header *)malloc(sizeof(struct nlsa_header *)); |
akmhoque | 8c1cdbf | 2012-08-26 21:09:11 -0500 | [diff] [blame^] | 71 | |
akmhoque | f71d908 | 2012-08-22 12:51:53 -0400 | [diff] [blame] | 72 | |
| 73 | name_lsa->header->ls_type=new_name_lsa->header->ls_type; |
| 74 | name_lsa->header->orig_time=new_name_lsa->header->orig_time; |
| 75 | name_lsa->header->ls_id=new_name_lsa->header->ls_id; |
akmhoque | 8c1cdbf | 2012-08-26 21:09:11 -0500 | [diff] [blame^] | 76 | |
| 77 | name_lsa->header->orig_router=ccn_charbuf_create(); |
akmhoque | f71d908 | 2012-08-22 12:51:53 -0400 | [diff] [blame] | 78 | ccn_charbuf_append_string(name_lsa->header->orig_router,ccn_charbuf_as_string(new_name_lsa->header->orig_router)); |
| 79 | name_lsa->header->isValid=new_name_lsa->header->isValid; |
| 80 | |
| 81 | name_lsa->name_prefix=ccn_charbuf_create(); |
akmhoque | 8c1cdbf | 2012-08-26 21:09:11 -0500 | [diff] [blame^] | 82 | ccn_charbuf_append_string(name_lsa->name_prefix,ccn_charbuf_as_string(new_name_lsa->name_prefix)); |
| 83 | //ccn_charbuf_append_charbuf(name_lsa->name_prefix,new_name_lsa->name_prefix); |
akmhoque | f71d908 | 2012-08-22 12:51:53 -0400 | [diff] [blame] | 84 | } |
| 85 | else if(res == HT_OLD_ENTRY) |
| 86 | { |
| 87 | printf("Duplicate Name LSA. Discarded...\n"); |
| 88 | |
| 89 | } |
| 90 | |
| 91 | hashtb_end(e); |
| 92 | |
| 93 | ccn_charbuf_destroy(&key); |
| 94 | |
| 95 | } |
| 96 | |
| 97 | struct nlsa * |
| 98 | build_name_lsa(struct ccn_charbuf *name_prefix) |
| 99 | { |
| 100 | printf("build_name_lsa called \n"); |
| 101 | struct nlsa *name_lsa=(struct nlsa*)malloc(sizeof(struct nlsa *)); |
| 102 | |
| 103 | name_lsa->header=(struct nlsa_header *)malloc(sizeof(struct nlsa_header *)); |
| 104 | name_lsa->header->orig_router=ccn_charbuf_create(); |
| 105 | |
| 106 | name_lsa->header->ls_type=LS_TYPE_NAME; |
| 107 | name_lsa->header->orig_time=get_current_time_sec(); |
| 108 | name_lsa->header->ls_id=++nlsr->nlsa_id; |
| 109 | ccn_charbuf_append_string(name_lsa->header->orig_router,nlsr->router_name); |
| 110 | name_lsa->header->isValid=1; |
| 111 | |
| 112 | name_lsa->name_prefix=ccn_charbuf_create(); |
| 113 | ccn_charbuf_append_string(name_lsa->name_prefix,ccn_charbuf_as_string(name_prefix)); |
| 114 | //ccn_charbuf_append_charbuf(name_lsa->name_prefix,name_prefix); |
| 115 | |
| 116 | return name_lsa; |
| 117 | } |
| 118 | |
| 119 | int |
| 120 | initial_build_name_lsa(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags) |
| 121 | { |
| 122 | printf("initial_build_name_lsa called \n"); |
| 123 | int i, npl_element; |
| 124 | struct name_prefix *np; |
| 125 | |
| 126 | struct hashtb_enumerator ee; |
| 127 | struct hashtb_enumerator *e = ⅇ |
| 128 | |
| 129 | hashtb_start(nlsr->npl, e); |
| 130 | npl_element=hashtb_n(nlsr->npl); |
| 131 | |
| 132 | for(i=0;i<npl_element;i++) |
| 133 | { |
| 134 | np=e->data; |
| 135 | |
| 136 | struct nlsa *name_lsa; |
| 137 | struct ccn_charbuf *name; |
| 138 | name=ccn_charbuf_create(); |
| 139 | ccn_charbuf_append_string(name,np->name); |
| 140 | name_lsa=build_name_lsa(name); |
| 141 | install_name_lsa(name_lsa); |
| 142 | |
akmhoque | 8c1cdbf | 2012-08-26 21:09:11 -0500 | [diff] [blame^] | 143 | ccn_charbuf_destroy(&name_lsa->header->orig_router); |
| 144 | ccn_charbuf_destroy(&name_lsa->name_prefix); |
| 145 | free(name_lsa->header); |
akmhoque | f71d908 | 2012-08-22 12:51:53 -0400 | [diff] [blame] | 146 | free(name_lsa); |
| 147 | ccn_charbuf_destroy(&name); |
| 148 | |
| 149 | hashtb_next(e); |
| 150 | } |
| 151 | |
| 152 | hashtb_end(e); |
| 153 | |
| 154 | print_name_lsdb(); |
| 155 | |
| 156 | return 0; |
| 157 | } |
| 158 | |
| 159 | void |
| 160 | print_name_lsa(struct nlsa *name_lsa) |
| 161 | { |
| 162 | printf("print_name_lsa called \n"); |
| 163 | printf("-----------Name LSA---------------\n"); |
| 164 | printf(" Origination Router : %s\n",ccn_charbuf_as_string(name_lsa->header->orig_router)); |
| 165 | printf(" Origination Router Length: %d\n",(int)name_lsa->header->orig_router->length); |
| 166 | printf(" LS Type : %d\n",name_lsa->header->ls_type); |
| 167 | printf(" LS Id : %ld\n",name_lsa->header->ls_id); |
| 168 | printf(" Origination Time : %ld\n",name_lsa->header->orig_time); |
akmhoque | 8c1cdbf | 2012-08-26 21:09:11 -0500 | [diff] [blame^] | 169 | printf(" Is Valid : %u\n",name_lsa->header->isValid); |
akmhoque | f71d908 | 2012-08-22 12:51:53 -0400 | [diff] [blame] | 170 | |
| 171 | printf(" LSA Data \n"); |
| 172 | printf(" Name Prefix: : %s\n",ccn_charbuf_as_string(name_lsa->name_prefix)); |
| 173 | printf(" Name Prefix Length : %d\n",(int)name_lsa->name_prefix->length); |
| 174 | |
| 175 | printf("\n"); |
| 176 | |
| 177 | } |
| 178 | |
| 179 | void |
| 180 | print_name_lsdb(void) |
| 181 | { |
| 182 | printf("print_name_lsdb called \n"); |
| 183 | int i, lsdb_element; |
| 184 | struct nlsa *name_lsa; |
| 185 | |
| 186 | struct hashtb_enumerator ee; |
| 187 | struct hashtb_enumerator *e = ⅇ |
| 188 | |
| 189 | hashtb_start(nlsr->lsdb->name_lsdb, e); |
| 190 | lsdb_element=hashtb_n(nlsr->lsdb->name_lsdb); |
| 191 | |
| 192 | for(i=0;i<lsdb_element;i++) |
| 193 | { |
| 194 | name_lsa=e->data; |
| 195 | print_name_lsa(name_lsa); |
| 196 | hashtb_next(e); |
| 197 | } |
| 198 | |
| 199 | hashtb_end(e); |
| 200 | |
| 201 | } |