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