blob: 6ef81cbd42092bce650336cafb2ac242055356c3 [file] [log] [blame]
akmhoque29c1db52012-09-07 14:47:43 -05001#include <stdio.h>
2#include <stdlib.h>
3#include <netinet/in.h>
4#include <netdb.h>
5#include <sys/time.h>
6#include <sys/types.h>
7#include <unistd.h>
8#include <stdarg.h>
9#include <string.h>
10
11#include <ccn/ccn.h>
12#include <ccn/uri.h>
13#include <ccn/face_mgmt.h>
14#include <ccn/reg_mgmt.h>
15#include <ccn/charbuf.h>
16
17#include "nlsr_fib.h"
akmhoqueb77b95f2013-02-08 12:28:47 -060018#include "nlsr.h"
akmhoque29c1db52012-09-07 14:47:43 -050019
20
21static void
22ccn_fib_warn(int lineno, const char *format, ...)
23{
24 struct timeval t;
25 va_list ap;
26 va_start(ap, format);
27 gettimeofday(&t, NULL);
28 fprintf(stderr, "%d.%06d ccn_fib[%d]:%d: ", (int)t.tv_sec, (unsigned)t.tv_usec, (int)getpid(), lineno);
29 vfprintf(stderr, format, ap);
30 va_end(ap);
31}
32
33#define ON_ERROR_CLEANUP(resval) \
34{ \
35 if ((resval) < 0) { \
36 ccn_fib_warn (__LINE__, "OnError cleanup\n"); \
37 goto cleanup; \
38 } \
39}
40
41#define ON_NULL_CLEANUP(resval) \
42{ \
43 if ((resval) == NULL) { \
44 ccn_fib_warn(__LINE__, "OnNull cleanup\n"); \
45 goto cleanup; \
46 } \
47}
48
49
50
51static int
52register_unregister_prefix(struct ccn *h, struct ccn_charbuf *local_scope_template,
53 struct ccn_charbuf *no_name, struct ccn_charbuf *name_prefix,const unsigned char *ccndid, size_t ccnd_id_size, int faceid, int operation)
54{
55 struct ccn_charbuf *temp = NULL;
56 struct ccn_charbuf *resultbuf = NULL;
57 struct ccn_charbuf *signed_info = NULL;
58 struct ccn_charbuf *name = NULL;
59 struct ccn_charbuf *prefixreg = NULL;
60 struct ccn_parsed_ContentObject pcobuf = {0};
61 struct ccn_forwarding_entry forwarding_entry_storage = {0};
62 struct ccn_forwarding_entry *forwarding_entry = &forwarding_entry_storage;
63 struct ccn_forwarding_entry *new_forwarding_entry;
64 const unsigned char *ptr = NULL;
65 size_t length = 0;
66 int res;
67
68 /* Register or unregister the prefix */
69 forwarding_entry->action = (operation == OP_REG) ? "prefixreg" : "unreg";
70 forwarding_entry->name_prefix = name_prefix;
71 forwarding_entry->ccnd_id = ccndid;
72 forwarding_entry->ccnd_id_size =ccnd_id_size;
73 forwarding_entry->faceid = faceid;
74 forwarding_entry->flags = -1;
akmhoqueb77b95f2013-02-08 12:28:47 -060075 forwarding_entry->lifetime = 2100;
akmhoque29c1db52012-09-07 14:47:43 -050076
77 prefixreg = ccn_charbuf_create();
78 ccnb_append_forwarding_entry(prefixreg, forwarding_entry);
79 temp = ccn_charbuf_create();
80 res = ccn_sign_content(h, temp, no_name, NULL, prefixreg->buf, prefixreg->length);
81 resultbuf = ccn_charbuf_create();
82
83 /* construct Interest containing prefixreg request */
84 name = ccn_charbuf_create();
85 ccn_name_init(name);
86 ccn_name_append_str(name, "ccnx");
87 ccn_name_append(name, ccndid, ccnd_id_size);
88 ccn_name_append_str(name, (operation == OP_REG) ? "prefixreg" : "unreg");
89 ccn_name_append(name, temp->buf, temp->length);
90
91 /* send Interest, get Data */
92 res = ccn_get(h, name, local_scope_template, 1000, resultbuf, &pcobuf, NULL, 0);
93 ON_ERROR_CLEANUP(res);
94
95 res = ccn_content_get_value(resultbuf->buf, resultbuf->length, &pcobuf, &ptr, &length);
96 ON_ERROR_CLEANUP(res);
97
98 /* extract new forwarding entry from Data */
99 new_forwarding_entry = ccn_forwarding_entry_parse(ptr, length);
100 ON_NULL_CLEANUP(new_forwarding_entry);
101
102 res = new_forwarding_entry->faceid;
103
104 ccn_forwarding_entry_destroy(&new_forwarding_entry);
105 ccn_charbuf_destroy(&signed_info);
106 ccn_charbuf_destroy(&temp);
107 ccn_charbuf_destroy(&resultbuf);
108 ccn_charbuf_destroy(&name);
109 ccn_charbuf_destroy(&prefixreg);
110
111 return res;
112
113 cleanup:
114 ccn_forwarding_entry_destroy(&new_forwarding_entry);
115 ccn_charbuf_destroy(&signed_info);
116 ccn_charbuf_destroy(&temp);
117 ccn_charbuf_destroy(&resultbuf);
118 ccn_charbuf_destroy(&name);
119 ccn_charbuf_destroy(&prefixreg);
120
121 return -1;
122}
123
124
125static int
126get_ccndid(struct ccn *h, struct ccn_charbuf *local_scope_template,
127 unsigned char *ccndid)
128{
129 struct ccn_charbuf *name = NULL;
130 struct ccn_charbuf *resultbuf = NULL;
131 struct ccn_parsed_ContentObject pcobuf = {0};
132 char ccndid_uri[] = "ccnx:/%C1.M.S.localhost/%C1.M.SRV/ccnd/KEY";
133 const unsigned char *ccndid_result;
134 static size_t ccndid_result_size;
135 int res;
136
137 name = ccn_charbuf_create();
138 resultbuf = ccn_charbuf_create();
139
140 res = ccn_name_from_uri(name, ccndid_uri);
141 ON_ERROR_CLEANUP(res);
142
143 /* get Data */
144 res = ccn_get(h, name, local_scope_template, 4500, resultbuf, &pcobuf, NULL, 0);
145 ON_ERROR_CLEANUP(res);
146
147 /* extract from Data */
148 res = ccn_ref_tagged_BLOB(CCN_DTAG_PublisherPublicKeyDigest,
149 resultbuf->buf,
150 pcobuf.offset[CCN_PCO_B_PublisherPublicKeyDigest],
151 pcobuf.offset[CCN_PCO_E_PublisherPublicKeyDigest],
152 &ccndid_result, &ccndid_result_size);
153 ON_ERROR_CLEANUP(res);
154
155 memcpy((void *)ccndid, ccndid_result, ccndid_result_size);
156
157 ccn_charbuf_destroy(&name);
158 ccn_charbuf_destroy(&resultbuf);
159
160 return (ccndid_result_size);
161
162 cleanup:
163 ccn_charbuf_destroy(&name);
164 ccn_charbuf_destroy(&resultbuf);
165
166 return -1;
167}
168
169static void
170init_data(struct ccn_charbuf *local_scope_template,
171 struct ccn_charbuf *no_name)
172{
173 ccn_charbuf_append_tt(local_scope_template, CCN_DTAG_Interest, CCN_DTAG);
174 ccn_charbuf_append_tt(local_scope_template, CCN_DTAG_Name, CCN_DTAG);
175 ccn_charbuf_append_closer(local_scope_template); /* </Name> */
176 ccnb_tagged_putf(local_scope_template, CCN_DTAG_Scope, "1");
177 ccn_charbuf_append_closer(local_scope_template); /* </Interest> */
178
179 ccn_name_init(no_name);
180}
181
182int
183add_delete_ccn_face_by_face_id(struct ccn *h, const char *uri, int operation, int faceid)
184{
akmhoqueb77b95f2013-02-08 12:28:47 -0600185 if ( nlsr->debugging )
186 {
187 printf("add_delete_ccn_face_by_face_id called\n");
188 printf("Uri: %s Face: %d Operation: %s \n",(char *)uri , faceid, operation == OP_REG ? "Registration" : "Unregistration");
189 }
190
akmhoque29c1db52012-09-07 14:47:43 -0500191 struct ccn_charbuf *prefix;
192 struct ccn_charbuf *local_scope_template = ccn_charbuf_create();
193 struct ccn_charbuf *no_name = ccn_charbuf_create();
194 unsigned char ccndid_storage[32] = {0};
195 unsigned char *ccndid = ccndid_storage;
196 size_t ccndid_size = 0;
197 int res;
198
199 prefix = ccn_charbuf_create();
200 res = ccn_name_from_uri(prefix, uri);
201 ON_ERROR_CLEANUP(res);
202
203
204 init_data(local_scope_template, no_name);
205
206 ccndid_size = get_ccndid(h, local_scope_template, ccndid);
207 if (ccndid_size != sizeof(ccndid_storage))
208 {
209 fprintf(stderr, "Incorrect size for ccnd id in response\n");
210 ON_ERROR_CLEANUP(-1);
211 }
212
213 res = register_unregister_prefix(h, local_scope_template, no_name, prefix,ccndid, ccndid_size,faceid, operation);
214
215 ON_ERROR_CLEANUP(res);
216
217 ccn_charbuf_destroy(&local_scope_template);
218 ccn_charbuf_destroy(&no_name);
219 ccn_charbuf_destroy(&prefix);
220
221 return 0;
222
223 cleanup:
224 ccn_charbuf_destroy(&prefix);
225 ccn_charbuf_destroy(&local_scope_template);
226 ccn_charbuf_destroy(&no_name);
227
228 return -1;
229}
230
231