blob: c8c416075ae2c24464bb63b48c666efd95c5f3a2 [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{
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 = &ee;
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
akmhoque5d736332012-08-27 13:47:07 -0500140 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
akmhoquef71d9082012-08-22 12:51:53 -0400145 struct ccn_charbuf *name;
146 name=ccn_charbuf_create();
akmhoque6428c922012-08-27 14:26:13 -0500147
akmhoquef71d9082012-08-22 12:51:53 -0400148 ccn_charbuf_append_string(name,np->name);
akmhoque5d736332012-08-27 13:47:07 -0500149 build_name_lsa(name_lsa,name);
akmhoquef71d9082012-08-22 12:51:53 -0400150 install_name_lsa(name_lsa);
151
akmhoqueb7b46392012-08-27 14:27:51 -0500152 //ccn_charbuf_destroy(&name_lsa->header->orig_router);
153 //free(name_lsa->header);
154 //ccn_charbuf_destroy(&name_lsa->name_prefix);
akmhoquef71d9082012-08-22 12:51:53 -0400155 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
akmhoque887fc0c2012-08-27 15:06:06 -0500165 //nlsr->event_send_info_interest = ccn_schedule_event(nlsr->sched, 1, &send_info_interest, NULL, 0);
166
akmhoquef71d9082012-08-22 12:51:53 -0400167 return 0;
168}
169
170void
171print_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);
akmhoqued79438d2012-08-27 13:31:42 -0500180 printf(" Is Valid : %d\n",name_lsa->header->isValid);
akmhoquef71d9082012-08-22 12:51:53 -0400181
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
190void
191print_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 = &ee;
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}
akmhoqued79438d2012-08-27 13:31:42 -0500213
214
215int
216install_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}