blob: f3b5fc8d945174044b4d6f4b5617ac81623b37fb [file] [log] [blame]
akmhoque3560cb62012-09-09 10:52:30 -05001#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#include <sys/types.h>
12#include <signal.h>
13
14
15
16#include <ccn/ccn.h>
17#include <ccn/uri.h>
18#include <ccn/keystore.h>
19#include <ccn/signing.h>
20#include <ccn/schedule.h>
21#include <ccn/hashtb.h>
22
23#include "nlsr.h"
24#include "nlsr_npt.h"
25#include "nlsr_fib.h"
26
27void
28make_npt_key(char *key, char *orig_router, char *name_prefix)
29{
30 memcpy(key+strlen(key),orig_router,strlen(orig_router));
31 memcpy(key+strlen(key),name_prefix,strlen(name_prefix));
32}
33
34int
35add_npt_entry(char *orig_router, char *name_prefix, int face)
36{
37 if ( strcmp(orig_router,nlsr->router_name)== 0)
38 {
39 return -1;
40 }
41
42 struct npt_entry *ne=(struct npt_entry*)malloc(sizeof(struct npt_entry ));
43
44 int res,res_nht;
45 struct hashtb_enumerator ee;
46 struct hashtb_enumerator *e = &ee;
47
48 char *key=(char *)malloc(strlen(orig_router)+strlen(name_prefix)+2);
49 memset(key,0,strlen(orig_router)+strlen(name_prefix)+2);
50
51 make_npt_key(key,orig_router,name_prefix);
52
53 hashtb_start(nlsr->npt, e);
54 res = hashtb_seek(e, key, strlen(key), 0);
55
56 if(res == HT_NEW_ENTRY)
57 {
58 ne=e->data;
59
60 ne->name_prefix=(char *)malloc(strlen(name_prefix)+1);
61 memset(ne->name_prefix,0,strlen(name_prefix)+1);
62 memcpy(ne->name_prefix,name_prefix,strlen(name_prefix));
63
64 /* Adding Orig Router in Orig Router List */
65
66 struct next_hop_entry *nhe=(struct next_hop_entry *)malloc(sizeof(struct next_hop_entry));
67
68 struct hashtb_param param_nht = {0};
69 ne->next_hop_table= hashtb_create(sizeof(struct next_hop_entry ), &param_nht);
70
71 struct hashtb_enumerator eenht;
72 struct hashtb_enumerator *enht = &eenht;
73
74 hashtb_start(ne->next_hop_table, enht);
75 res_nht = hashtb_seek(enht, orig_router, strlen(orig_router), 0);
76
77 if(res_nht == HT_NEW_ENTRY )
78 {
79 nhe=enht->data;
80 nhe->orig_router=(char *)malloc(strlen(orig_router)+1);
81 memset(nhe->orig_router,0,strlen(orig_router)+1);
82 memcpy(nhe->orig_router,orig_router,strlen(orig_router));
83
84 nhe->next_hop_face=face;
85
86 }
87 hashtb_end(enht);
88
89 if ( face != NO_FACE )
90 {
91 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)name_prefix, OP_REG, face);
92 }
93
94 }
95 else if (res == HT_OLD_ENTRY)
96 {
97 free(ne);
98 struct npt_entry *one;
99
100 one=e->data;
101
102 struct next_hop_entry *nhe=(struct next_hop_entry *)malloc(sizeof(struct next_hop_entry));
103 struct hashtb_param param_nht = {0};
104 ne->next_hop_table= hashtb_create(sizeof(struct next_hop_entry ), &param_nht);
105
106 struct hashtb_enumerator eenht;
107 struct hashtb_enumerator *enht = &eenht;
108
109 hashtb_start(one->next_hop_table, enht);
110 res_nht = hashtb_seek(enht, orig_router, strlen(orig_router), 0);
111
112 if(res_nht == HT_NEW_ENTRY )
113 {
114 nhe=enht->data;
115 nhe->orig_router=(char *)malloc(strlen(orig_router)+1);
116 memset(nhe->orig_router,0,strlen(orig_router)+1);
117 memcpy(nhe->orig_router,orig_router,strlen(orig_router));
118
119 nhe->next_hop_face=face;
120
121 if ( face != NO_FACE )
122 {
123 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)name_prefix, OP_REG, face);
124 }
125
126 }
127 else if ( res_nht == HT_OLD_ENTRY)
128 {
129 free(nhe);
130 struct next_hop_entry *onhe;
131 onhe=enht->data;
132
133 if(onhe->next_hop_face != face )
134 {
135 if ( onhe->next_hop_face != NO_FACE )
136 {
137 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)name_prefix, OP_UNREG, onhe->next_hop_face);
138 }
139
140 if ( face != NO_FACE )
141 {
142 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)name_prefix, OP_REG, onhe->next_hop_face);
143 }
144
145 onhe->next_hop_face=face;
146
147 }
148
149 }
150 hashtb_end(enht);
151
152
153
154 }
155 hashtb_end(e);
156
157 free(key);
158 return res;
159}
160
161
162void
163print_npt(void)
164{
165 printf("\n");
166 printf("print_npt called\n\n");
167 int i, npt_element;
168
169 struct npt_entry *ne;
170
171 struct hashtb_enumerator ee;
172 struct hashtb_enumerator *e = &ee;
173
174 hashtb_start(nlsr->npt, e);
175 npt_element=hashtb_n(nlsr->npt);
176
177 for(i=0;i<npt_element;i++)
178 {
179 printf("\n");
180 printf("----------NPT ENTRY %d------------------\n",i+1);
181 ne=e->data;
182 printf(" Name Prefix: %s \n",ne->name_prefix);
183
184 struct next_hop_entry *nhe;
185 struct hashtb_enumerator eenht;
186 struct hashtb_enumerator *enht = &eenht;
187
188 int j, nht_element;
189 hashtb_start(ne->next_hop_table, enht);
190 nht_element=hashtb_n(ne->next_hop_table);
191
192 for (j=0;j<nht_element;j++)
193 {
194 nhe=enht->data;
195 printf(" Origination Router: %s \n",nhe->orig_router);
196 nhe->next_hop_face == NO_FACE ? printf(" Next Hop Face: NO_NEXT_HOP \n") : printf(" Next Hop Face: %d \n", nhe->next_hop_face);
197
198 hashtb_next(enht);
199 }
200 hashtb_end(enht);
201
202 hashtb_next(e);
203 }
204
205 hashtb_end(e);
206
207 printf("\n");
208}