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 *)); |
| 71 | name_lsa->header->orig_router=ccn_charbuf_create(); |
| 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; |
| 76 | ccn_charbuf_append_string(name_lsa->header->orig_router,ccn_charbuf_as_string(new_name_lsa->header->orig_router)); |
| 77 | name_lsa->header->isValid=new_name_lsa->header->isValid; |
| 78 | |
| 79 | name_lsa->name_prefix=ccn_charbuf_create(); |
| 80 | //ccn_charbuf_append_string(name_lsa->name_prefix,ccn_charbuf_as_string(new_name_lsa->name_prefix)); |
| 81 | ccn_charbuf_append_charbuf(name_lsa->name_prefix,new_name_lsa->name_prefix); |
| 82 | } |
| 83 | else if(res == HT_OLD_ENTRY) |
| 84 | { |
| 85 | printf("Duplicate Name LSA. Discarded...\n"); |
| 86 | |
| 87 | } |
| 88 | |
| 89 | hashtb_end(e); |
| 90 | |
| 91 | ccn_charbuf_destroy(&key); |
| 92 | |
| 93 | } |
| 94 | |
| 95 | struct nlsa * |
| 96 | build_name_lsa(struct ccn_charbuf *name_prefix) |
| 97 | { |
| 98 | printf("build_name_lsa called \n"); |
| 99 | struct nlsa *name_lsa=(struct nlsa*)malloc(sizeof(struct nlsa *)); |
| 100 | |
| 101 | name_lsa->header=(struct nlsa_header *)malloc(sizeof(struct nlsa_header *)); |
| 102 | name_lsa->header->orig_router=ccn_charbuf_create(); |
| 103 | |
| 104 | name_lsa->header->ls_type=LS_TYPE_NAME; |
| 105 | name_lsa->header->orig_time=get_current_time_sec(); |
| 106 | name_lsa->header->ls_id=++nlsr->nlsa_id; |
| 107 | ccn_charbuf_append_string(name_lsa->header->orig_router,nlsr->router_name); |
| 108 | name_lsa->header->isValid=1; |
| 109 | |
| 110 | name_lsa->name_prefix=ccn_charbuf_create(); |
| 111 | ccn_charbuf_append_string(name_lsa->name_prefix,ccn_charbuf_as_string(name_prefix)); |
| 112 | //ccn_charbuf_append_charbuf(name_lsa->name_prefix,name_prefix); |
| 113 | |
| 114 | return name_lsa; |
| 115 | } |
| 116 | |
| 117 | int |
| 118 | initial_build_name_lsa(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags) |
| 119 | { |
| 120 | printf("initial_build_name_lsa called \n"); |
| 121 | int i, npl_element; |
| 122 | struct name_prefix *np; |
| 123 | |
| 124 | struct hashtb_enumerator ee; |
| 125 | struct hashtb_enumerator *e = ⅇ |
| 126 | |
| 127 | hashtb_start(nlsr->npl, e); |
| 128 | npl_element=hashtb_n(nlsr->npl); |
| 129 | |
| 130 | for(i=0;i<npl_element;i++) |
| 131 | { |
| 132 | np=e->data; |
| 133 | |
| 134 | struct nlsa *name_lsa; |
| 135 | struct ccn_charbuf *name; |
| 136 | name=ccn_charbuf_create(); |
| 137 | ccn_charbuf_append_string(name,np->name); |
| 138 | name_lsa=build_name_lsa(name); |
| 139 | install_name_lsa(name_lsa); |
| 140 | |
| 141 | free(name_lsa); |
| 142 | ccn_charbuf_destroy(&name); |
| 143 | |
| 144 | hashtb_next(e); |
| 145 | } |
| 146 | |
| 147 | hashtb_end(e); |
| 148 | |
| 149 | print_name_lsdb(); |
| 150 | |
| 151 | return 0; |
| 152 | } |
| 153 | |
| 154 | void |
| 155 | print_name_lsa(struct nlsa *name_lsa) |
| 156 | { |
| 157 | printf("print_name_lsa called \n"); |
| 158 | printf("-----------Name LSA---------------\n"); |
| 159 | printf(" Origination Router : %s\n",ccn_charbuf_as_string(name_lsa->header->orig_router)); |
| 160 | printf(" Origination Router Length: %d\n",(int)name_lsa->header->orig_router->length); |
| 161 | printf(" LS Type : %d\n",name_lsa->header->ls_type); |
| 162 | printf(" LS Id : %ld\n",name_lsa->header->ls_id); |
| 163 | printf(" Origination Time : %ld\n",name_lsa->header->orig_time); |
| 164 | printf(" Is Valid : %d\n",name_lsa->header->isValid); |
| 165 | |
| 166 | printf(" LSA Data \n"); |
| 167 | printf(" Name Prefix: : %s\n",ccn_charbuf_as_string(name_lsa->name_prefix)); |
| 168 | printf(" Name Prefix Length : %d\n",(int)name_lsa->name_prefix->length); |
| 169 | |
| 170 | printf("\n"); |
| 171 | |
| 172 | } |
| 173 | |
| 174 | void |
| 175 | print_name_lsdb(void) |
| 176 | { |
| 177 | printf("print_name_lsdb called \n"); |
| 178 | int i, lsdb_element; |
| 179 | struct nlsa *name_lsa; |
| 180 | |
| 181 | struct hashtb_enumerator ee; |
| 182 | struct hashtb_enumerator *e = ⅇ |
| 183 | |
| 184 | hashtb_start(nlsr->lsdb->name_lsdb, e); |
| 185 | lsdb_element=hashtb_n(nlsr->lsdb->name_lsdb); |
| 186 | |
| 187 | for(i=0;i<lsdb_element;i++) |
| 188 | { |
| 189 | name_lsa=e->data; |
| 190 | print_name_lsa(name_lsa); |
| 191 | hashtb_next(e); |
| 192 | } |
| 193 | |
| 194 | hashtb_end(e); |
| 195 | |
| 196 | } |