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 | { |
akmhoque | 42098b1 | 2012-08-27 22:54:23 -0500 | [diff] [blame^] | 126 | my_lock(); |
akmhoque | f71d908 | 2012-08-22 12:51:53 -0400 | [diff] [blame] | 127 | printf("initial_build_name_lsa called \n"); |
| 128 | int i, npl_element; |
| 129 | struct name_prefix *np; |
| 130 | |
| 131 | struct hashtb_enumerator ee; |
| 132 | struct hashtb_enumerator *e = ⅇ |
| 133 | |
| 134 | hashtb_start(nlsr->npl, e); |
| 135 | npl_element=hashtb_n(nlsr->npl); |
| 136 | |
| 137 | for(i=0;i<npl_element;i++) |
| 138 | { |
| 139 | np=e->data; |
| 140 | |
akmhoque | 5d73633 | 2012-08-27 13:47:07 -0500 | [diff] [blame] | 141 | struct nlsa *name_lsa=(struct nlsa*)malloc(sizeof(struct nlsa *)); |
| 142 | name_lsa->header=(struct nlsa_header *)malloc(sizeof(struct nlsa_header *)); |
| 143 | name_lsa->header->orig_router=ccn_charbuf_create(); |
| 144 | name_lsa->name_prefix=ccn_charbuf_create(); |
| 145 | |
akmhoque | f71d908 | 2012-08-22 12:51:53 -0400 | [diff] [blame] | 146 | struct ccn_charbuf *name; |
| 147 | name=ccn_charbuf_create(); |
akmhoque | 6428c92 | 2012-08-27 14:26:13 -0500 | [diff] [blame] | 148 | |
akmhoque | f71d908 | 2012-08-22 12:51:53 -0400 | [diff] [blame] | 149 | ccn_charbuf_append_string(name,np->name); |
akmhoque | 5d73633 | 2012-08-27 13:47:07 -0500 | [diff] [blame] | 150 | build_name_lsa(name_lsa,name); |
akmhoque | f71d908 | 2012-08-22 12:51:53 -0400 | [diff] [blame] | 151 | install_name_lsa(name_lsa); |
| 152 | |
akmhoque | b7b4639 | 2012-08-27 14:27:51 -0500 | [diff] [blame] | 153 | //ccn_charbuf_destroy(&name_lsa->header->orig_router); |
| 154 | //free(name_lsa->header); |
| 155 | //ccn_charbuf_destroy(&name_lsa->name_prefix); |
akmhoque | f71d908 | 2012-08-22 12:51:53 -0400 | [diff] [blame] | 156 | free(name_lsa); |
| 157 | ccn_charbuf_destroy(&name); |
| 158 | |
| 159 | hashtb_next(e); |
| 160 | } |
| 161 | |
| 162 | hashtb_end(e); |
| 163 | |
| 164 | print_name_lsdb(); |
akmhoque | 42098b1 | 2012-08-27 22:54:23 -0500 | [diff] [blame^] | 165 | |
| 166 | my_unlock(); |
akmhoque | 887fc0c | 2012-08-27 15:06:06 -0500 | [diff] [blame] | 167 | //nlsr->event_send_info_interest = ccn_schedule_event(nlsr->sched, 1, &send_info_interest, NULL, 0); |
akmhoque | f71d908 | 2012-08-22 12:51:53 -0400 | [diff] [blame] | 168 | return 0; |
| 169 | } |
| 170 | |
| 171 | void |
| 172 | print_name_lsa(struct nlsa *name_lsa) |
| 173 | { |
| 174 | printf("print_name_lsa called \n"); |
| 175 | printf("-----------Name LSA---------------\n"); |
| 176 | printf(" Origination Router : %s\n",ccn_charbuf_as_string(name_lsa->header->orig_router)); |
| 177 | printf(" Origination Router Length: %d\n",(int)name_lsa->header->orig_router->length); |
| 178 | printf(" LS Type : %d\n",name_lsa->header->ls_type); |
| 179 | printf(" LS Id : %ld\n",name_lsa->header->ls_id); |
| 180 | printf(" Origination Time : %ld\n",name_lsa->header->orig_time); |
akmhoque | d79438d | 2012-08-27 13:31:42 -0500 | [diff] [blame] | 181 | printf(" Is Valid : %d\n",name_lsa->header->isValid); |
akmhoque | f71d908 | 2012-08-22 12:51:53 -0400 | [diff] [blame] | 182 | |
| 183 | printf(" LSA Data \n"); |
| 184 | printf(" Name Prefix: : %s\n",ccn_charbuf_as_string(name_lsa->name_prefix)); |
| 185 | printf(" Name Prefix Length : %d\n",(int)name_lsa->name_prefix->length); |
| 186 | |
| 187 | printf("\n"); |
| 188 | |
| 189 | } |
| 190 | |
| 191 | void |
| 192 | print_name_lsdb(void) |
| 193 | { |
| 194 | printf("print_name_lsdb called \n"); |
| 195 | int i, lsdb_element; |
| 196 | struct nlsa *name_lsa; |
| 197 | |
| 198 | struct hashtb_enumerator ee; |
| 199 | struct hashtb_enumerator *e = ⅇ |
| 200 | |
| 201 | hashtb_start(nlsr->lsdb->name_lsdb, e); |
| 202 | lsdb_element=hashtb_n(nlsr->lsdb->name_lsdb); |
| 203 | |
| 204 | for(i=0;i<lsdb_element;i++) |
| 205 | { |
| 206 | name_lsa=e->data; |
| 207 | print_name_lsa(name_lsa); |
| 208 | hashtb_next(e); |
| 209 | } |
| 210 | |
| 211 | hashtb_end(e); |
| 212 | |
| 213 | } |
akmhoque | d79438d | 2012-08-27 13:31:42 -0500 | [diff] [blame] | 214 | |
| 215 | |
| 216 | int |
| 217 | install_adj_lsa(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags) |
| 218 | { |
akmhoque | 42098b1 | 2012-08-27 22:54:23 -0500 | [diff] [blame^] | 219 | my_lock(); |
| 220 | |
akmhoque | d79438d | 2012-08-27 13:31:42 -0500 | [diff] [blame] | 221 | printf("install_adj_lsa called \n"); |
| 222 | |
| 223 | ev = ccn_schedule_event(nlsr->sched, 1000000, &install_adj_lsa, NULL, 0); |
| 224 | |
akmhoque | 42098b1 | 2012-08-27 22:54:23 -0500 | [diff] [blame^] | 225 | my_unlock(); |
akmhoque | d79438d | 2012-08-27 13:31:42 -0500 | [diff] [blame] | 226 | return 0; |
| 227 | |
| 228 | } |