blob: d9d66c2e6b0345a56aa478ed5be707b7d36469e7 [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
98struct nlsa *
99build_name_lsa(struct ccn_charbuf *name_prefix)
100{
101 printf("build_name_lsa called \n");
102 struct nlsa *name_lsa=(struct nlsa*)malloc(sizeof(struct nlsa *));
103
104 name_lsa->header=(struct nlsa_header *)malloc(sizeof(struct nlsa_header *));
105 name_lsa->header->orig_router=ccn_charbuf_create();
106
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
115 name_lsa->name_prefix=ccn_charbuf_create();
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
119 return name_lsa;
120}
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
139 struct nlsa *name_lsa;
140 struct ccn_charbuf *name;
141 name=ccn_charbuf_create();
142 ccn_charbuf_append_string(name,np->name);
143 name_lsa=build_name_lsa(name);
144 install_name_lsa(name_lsa);
145
akmhoque8c1cdbf2012-08-26 21:09:11 -0500146 ccn_charbuf_destroy(&name_lsa->header->orig_router);
147 ccn_charbuf_destroy(&name_lsa->name_prefix);
148 free(name_lsa->header);
akmhoquef71d9082012-08-22 12:51:53 -0400149 free(name_lsa);
150 ccn_charbuf_destroy(&name);
151
152 hashtb_next(e);
153 }
154
155 hashtb_end(e);
156
157 print_name_lsdb();
158
159 return 0;
160}
161
162void
163print_name_lsa(struct nlsa *name_lsa)
164{
165 printf("print_name_lsa called \n");
166 printf("-----------Name LSA---------------\n");
167 printf(" Origination Router : %s\n",ccn_charbuf_as_string(name_lsa->header->orig_router));
168 printf(" Origination Router Length: %d\n",(int)name_lsa->header->orig_router->length);
169 printf(" LS Type : %d\n",name_lsa->header->ls_type);
170 printf(" LS Id : %ld\n",name_lsa->header->ls_id);
171 printf(" Origination Time : %ld\n",name_lsa->header->orig_time);
akmhoqued79438d2012-08-27 13:31:42 -0500172 printf(" Is Valid : %d\n",name_lsa->header->isValid);
akmhoquef71d9082012-08-22 12:51:53 -0400173
174 printf(" LSA Data \n");
175 printf(" Name Prefix: : %s\n",ccn_charbuf_as_string(name_lsa->name_prefix));
176 printf(" Name Prefix Length : %d\n",(int)name_lsa->name_prefix->length);
177
178 printf("\n");
179
180}
181
182void
183print_name_lsdb(void)
184{
185 printf("print_name_lsdb called \n");
186 int i, lsdb_element;
187 struct nlsa *name_lsa;
188
189 struct hashtb_enumerator ee;
190 struct hashtb_enumerator *e = &ee;
191
192 hashtb_start(nlsr->lsdb->name_lsdb, e);
193 lsdb_element=hashtb_n(nlsr->lsdb->name_lsdb);
194
195 for(i=0;i<lsdb_element;i++)
196 {
197 name_lsa=e->data;
198 print_name_lsa(name_lsa);
199 hashtb_next(e);
200 }
201
202 hashtb_end(e);
203
204}
akmhoqued79438d2012-08-27 13:31:42 -0500205
206
207int
208install_adj_lsa(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags)
209{
210 printf("install_adj_lsa called \n");
211
212 ev = ccn_schedule_event(nlsr->sched, 1000000, &install_adj_lsa, NULL, 0);
213
214 return 0;
215
216}