blob: ebb639232c0d6fa0c55b247892563019dac4afab [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"
akmhoque438b07e2012-08-21 10:13:57 -040023
akmhoquef71d9082012-08-22 12:51:53 -040024void
25make_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));
akmhoque438b07e2012-08-21 10:13:57 -040029
akmhoquef71d9082012-08-22 12:51:53 -040030 struct ccn_charbuf *c=ccn_charbuf_create();
31 ccn_charbuf_reset(c);
akmhoqued79438d2012-08-27 13:31:42 -050032 ccn_charbuf_putf(c, "%d", ls_type);
akmhoquef71d9082012-08-22 12:51:53 -040033 ccn_charbuf_append_string(key,ccn_charbuf_as_string(c));
34
35 ccn_charbuf_reset(c);
akmhoqued79438d2012-08-27 13:31:42 -050036 ccn_charbuf_putf(c, "%ld", nlsa_id);
akmhoquef71d9082012-08-22 12:51:53 -040037 ccn_charbuf_append_string(key,ccn_charbuf_as_string(c));
38
39 ccn_charbuf_reset(c);
akmhoqued79438d2012-08-27 13:31:42 -050040 ccn_charbuf_putf(c, "%ld", orig_time);
akmhoquef71d9082012-08-22 12:51:53 -040041 ccn_charbuf_append_string(key,ccn_charbuf_as_string(c));
akmhoqued79438d2012-08-27 13:31:42 -050042 //ccn_charbuf_append_string(key,orig_time);
akmhoquef71d9082012-08-22 12:51:53 -040043
44 ccn_charbuf_destroy(&c);
45
46 printf("Key: %s length: %d\n",ccn_charbuf_as_string(key),(int)key->length);
47
48}
49
50void
51install_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 = &ee;
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 *));
akmhoque8c1cdbf2012-08-26 21:09:11 -050072
akmhoquef71d9082012-08-22 12:51:53 -040073
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;
akmhoque8c1cdbf2012-08-26 21:09:11 -050077
78 name_lsa->header->orig_router=ccn_charbuf_create();
akmhoquef71d9082012-08-22 12:51:53 -040079 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();
akmhoque8c1cdbf2012-08-26 21:09:11 -050083 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);
akmhoquef71d9082012-08-22 12:51:53 -040085 }
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
akmhoque5d736332012-08-27 13:47:07 -050098void
99build_name_lsa(struct nlsa *name_lsa, struct ccn_charbuf *name_prefix)
akmhoquef71d9082012-08-22 12:51:53 -0400100{
101 printf("build_name_lsa called \n");
akmhoque5d736332012-08-27 13:47:07 -0500102 //struct nlsa *name_lsa=(struct nlsa*)malloc(sizeof(struct nlsa *));
akmhoquef71d9082012-08-22 12:51:53 -0400103
akmhoque5d736332012-08-27 13:47:07 -0500104 //name_lsa->header=(struct nlsa_header *)malloc(sizeof(struct nlsa_header *));
105 //name_lsa->header->orig_router=ccn_charbuf_create();
akmhoquef71d9082012-08-22 12:51:53 -0400106
107 name_lsa->header->ls_type=LS_TYPE_NAME;
akmhoqued79438d2012-08-27 13:31:42 -0500108 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);
akmhoquef71d9082012-08-22 12:51:53 -0400111 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
akmhoque5d736332012-08-27 13:47:07 -0500115 //name_lsa->name_prefix=ccn_charbuf_create();
akmhoquef71d9082012-08-22 12:51:53 -0400116 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
akmhoque5d736332012-08-27 13:47:07 -0500119 //return name_lsa;
akmhoquef71d9082012-08-22 12:51:53 -0400120}
121
122int
123initial_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 = &ee;
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
akmhoque5d736332012-08-27 13:47:07 -0500139 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
akmhoquef71d9082012-08-22 12:51:53 -0400144 struct ccn_charbuf *name;
145 name=ccn_charbuf_create();
146 ccn_charbuf_append_string(name,np->name);
akmhoque5d736332012-08-27 13:47:07 -0500147 build_name_lsa(name_lsa,name);
akmhoquef71d9082012-08-22 12:51:53 -0400148 install_name_lsa(name_lsa);
149
akmhoque8c1cdbf2012-08-26 21:09:11 -0500150 ccn_charbuf_destroy(&name_lsa->header->orig_router);
151 ccn_charbuf_destroy(&name_lsa->name_prefix);
152 free(name_lsa->header);
akmhoquef71d9082012-08-22 12:51:53 -0400153 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
166void
167print_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);
akmhoqued79438d2012-08-27 13:31:42 -0500176 printf(" Is Valid : %d\n",name_lsa->header->isValid);
akmhoquef71d9082012-08-22 12:51:53 -0400177
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
186void
187print_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 = &ee;
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}
akmhoqued79438d2012-08-27 13:31:42 -0500209
210
211int
212install_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}