blob: b6b115e5e34bc83485ef07410e5a6595f78ee097 [file] [log] [blame]
akmhoquef1161eb2012-08-21 09:37:21 -04001#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"
akmhoquef71d9082012-08-22 12:51:53 -040022#include "utility.h"
akmhoque887fc0c2012-08-27 15:06:06 -050023#include "nlsr_ndn.h"
akmhoque438b07e2012-08-21 10:13:57 -040024
akmhoquef71d9082012-08-22 12:51:53 -040025void
26make_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));
akmhoque438b07e2012-08-21 10:13:57 -040030
akmhoquef71d9082012-08-22 12:51:53 -040031 struct ccn_charbuf *c=ccn_charbuf_create();
32 ccn_charbuf_reset(c);
akmhoqued79438d2012-08-27 13:31:42 -050033 ccn_charbuf_putf(c, "%d", ls_type);
akmhoquef71d9082012-08-22 12:51:53 -040034 ccn_charbuf_append_string(key,ccn_charbuf_as_string(c));
35
36 ccn_charbuf_reset(c);
akmhoqued79438d2012-08-27 13:31:42 -050037 ccn_charbuf_putf(c, "%ld", nlsa_id);
akmhoquef71d9082012-08-22 12:51:53 -040038 ccn_charbuf_append_string(key,ccn_charbuf_as_string(c));
39
40 ccn_charbuf_reset(c);
akmhoqued79438d2012-08-27 13:31:42 -050041 ccn_charbuf_putf(c, "%ld", orig_time);
akmhoquef71d9082012-08-22 12:51:53 -040042 ccn_charbuf_append_string(key,ccn_charbuf_as_string(c));
akmhoqued79438d2012-08-27 13:31:42 -050043 //ccn_charbuf_append_string(key,orig_time);
akmhoquef71d9082012-08-22 12:51:53 -040044
45 ccn_charbuf_destroy(&c);
46
47 printf("Key: %s length: %d\n",ccn_charbuf_as_string(key),(int)key->length);
48
49}
50
51void
52install_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 = &ee;
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 *));
akmhoque8c1cdbf2012-08-26 21:09:11 -050073
akmhoquef71d9082012-08-22 12:51:53 -040074
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;
akmhoque8c1cdbf2012-08-26 21:09:11 -050078
79 name_lsa->header->orig_router=ccn_charbuf_create();
akmhoquef71d9082012-08-22 12:51:53 -040080 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();
akmhoque8c1cdbf2012-08-26 21:09:11 -050084 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);
akmhoquef71d9082012-08-22 12:51:53 -040086 }
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
akmhoque5d736332012-08-27 13:47:07 -050099void
100build_name_lsa(struct nlsa *name_lsa, struct ccn_charbuf *name_prefix)
akmhoquef71d9082012-08-22 12:51:53 -0400101{
102 printf("build_name_lsa called \n");
akmhoque5d736332012-08-27 13:47:07 -0500103 //struct nlsa *name_lsa=(struct nlsa*)malloc(sizeof(struct nlsa *));
akmhoquef71d9082012-08-22 12:51:53 -0400104
akmhoque5d736332012-08-27 13:47:07 -0500105 //name_lsa->header=(struct nlsa_header *)malloc(sizeof(struct nlsa_header *));
106 //name_lsa->header->orig_router=ccn_charbuf_create();
akmhoquef71d9082012-08-22 12:51:53 -0400107
108 name_lsa->header->ls_type=LS_TYPE_NAME;
akmhoqued79438d2012-08-27 13:31:42 -0500109 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);
akmhoquef71d9082012-08-22 12:51:53 -0400112 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
akmhoque5d736332012-08-27 13:47:07 -0500116 //name_lsa->name_prefix=ccn_charbuf_create();
akmhoquef71d9082012-08-22 12:51:53 -0400117 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
akmhoque5d736332012-08-27 13:47:07 -0500120 //return name_lsa;
akmhoquef71d9082012-08-22 12:51:53 -0400121}
122
123int
124initial_build_name_lsa(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags)
125{
akmhoque42098b12012-08-27 22:54:23 -0500126 my_lock();
akmhoquef71d9082012-08-22 12:51:53 -0400127 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 = &ee;
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
akmhoque5d736332012-08-27 13:47:07 -0500141 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
akmhoquef71d9082012-08-22 12:51:53 -0400146 struct ccn_charbuf *name;
147 name=ccn_charbuf_create();
akmhoque6428c922012-08-27 14:26:13 -0500148
akmhoquef71d9082012-08-22 12:51:53 -0400149 ccn_charbuf_append_string(name,np->name);
akmhoque5d736332012-08-27 13:47:07 -0500150 build_name_lsa(name_lsa,name);
akmhoquef71d9082012-08-22 12:51:53 -0400151 install_name_lsa(name_lsa);
152
akmhoqueb7b46392012-08-27 14:27:51 -0500153 //ccn_charbuf_destroy(&name_lsa->header->orig_router);
154 //free(name_lsa->header);
155 //ccn_charbuf_destroy(&name_lsa->name_prefix);
akmhoquef71d9082012-08-22 12:51:53 -0400156 free(name_lsa);
157 ccn_charbuf_destroy(&name);
158
159 hashtb_next(e);
160 }
161
162 hashtb_end(e);
163
164 print_name_lsdb();
akmhoque42098b12012-08-27 22:54:23 -0500165
166 my_unlock();
akmhoque887fc0c2012-08-27 15:06:06 -0500167 //nlsr->event_send_info_interest = ccn_schedule_event(nlsr->sched, 1, &send_info_interest, NULL, 0);
akmhoquef71d9082012-08-22 12:51:53 -0400168 return 0;
169}
170
171void
172print_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);
akmhoqued79438d2012-08-27 13:31:42 -0500181 printf(" Is Valid : %d\n",name_lsa->header->isValid);
akmhoquef71d9082012-08-22 12:51:53 -0400182
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
191void
192print_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 = &ee;
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}
akmhoqued79438d2012-08-27 13:31:42 -0500214
215
216int
217install_adj_lsa(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags)
218{
akmhoque42098b12012-08-27 22:54:23 -0500219 my_lock();
220
akmhoqued79438d2012-08-27 13:31:42 -0500221 printf("install_adj_lsa called \n");
222
223 ev = ccn_schedule_event(nlsr->sched, 1000000, &install_adj_lsa, NULL, 0);
224
akmhoque42098b12012-08-27 22:54:23 -0500225 my_unlock();
akmhoqued79438d2012-08-27 13:31:42 -0500226 return 0;
227
228}