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