blob: a83899d8b9aff9d8aba63842affb4d7480c3c7af [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,
akmhoque483c1eb2013-03-08 00:51:09 -060053 struct ccn_charbuf *no_name, struct ccn_charbuf *name_prefix,
54 const unsigned char *ccndid, size_t ccnd_id_size, int faceid,
55 int operation, long int lifetime)
akmhoque29c1db52012-09-07 14:47:43 -050056{
57 struct ccn_charbuf *temp = NULL;
58 struct ccn_charbuf *resultbuf = NULL;
59 struct ccn_charbuf *signed_info = NULL;
60 struct ccn_charbuf *name = NULL;
61 struct ccn_charbuf *prefixreg = NULL;
62 struct ccn_parsed_ContentObject pcobuf = {0};
63 struct ccn_forwarding_entry forwarding_entry_storage = {0};
64 struct ccn_forwarding_entry *forwarding_entry = &forwarding_entry_storage;
65 struct ccn_forwarding_entry *new_forwarding_entry;
66 const unsigned char *ptr = NULL;
67 size_t length = 0;
68 int res;
69
70 /* Register or unregister the prefix */
71 forwarding_entry->action = (operation == OP_REG) ? "prefixreg" : "unreg";
72 forwarding_entry->name_prefix = name_prefix;
73 forwarding_entry->ccnd_id = ccndid;
74 forwarding_entry->ccnd_id_size =ccnd_id_size;
75 forwarding_entry->faceid = faceid;
76 forwarding_entry->flags = -1;
akmhoque483c1eb2013-03-08 00:51:09 -060077 forwarding_entry->lifetime = lifetime;
akmhoque29c1db52012-09-07 14:47:43 -050078
79 prefixreg = ccn_charbuf_create();
80 ccnb_append_forwarding_entry(prefixreg, forwarding_entry);
81 temp = ccn_charbuf_create();
82 res = ccn_sign_content(h, temp, no_name, NULL, prefixreg->buf, prefixreg->length);
83 resultbuf = ccn_charbuf_create();
84
85 /* construct Interest containing prefixreg request */
86 name = ccn_charbuf_create();
87 ccn_name_init(name);
88 ccn_name_append_str(name, "ccnx");
89 ccn_name_append(name, ccndid, ccnd_id_size);
90 ccn_name_append_str(name, (operation == OP_REG) ? "prefixreg" : "unreg");
91 ccn_name_append(name, temp->buf, temp->length);
92
93 /* send Interest, get Data */
94 res = ccn_get(h, name, local_scope_template, 1000, resultbuf, &pcobuf, NULL, 0);
95 ON_ERROR_CLEANUP(res);
96
97 res = ccn_content_get_value(resultbuf->buf, resultbuf->length, &pcobuf, &ptr, &length);
98 ON_ERROR_CLEANUP(res);
99
100 /* extract new forwarding entry from Data */
101 new_forwarding_entry = ccn_forwarding_entry_parse(ptr, length);
102 ON_NULL_CLEANUP(new_forwarding_entry);
103
104 res = new_forwarding_entry->faceid;
105
106 ccn_forwarding_entry_destroy(&new_forwarding_entry);
107 ccn_charbuf_destroy(&signed_info);
108 ccn_charbuf_destroy(&temp);
109 ccn_charbuf_destroy(&resultbuf);
110 ccn_charbuf_destroy(&name);
111 ccn_charbuf_destroy(&prefixreg);
112
113 return res;
114
115 cleanup:
116 ccn_forwarding_entry_destroy(&new_forwarding_entry);
117 ccn_charbuf_destroy(&signed_info);
118 ccn_charbuf_destroy(&temp);
119 ccn_charbuf_destroy(&resultbuf);
120 ccn_charbuf_destroy(&name);
121 ccn_charbuf_destroy(&prefixreg);
122
123 return -1;
124}
125
126
127static int
128get_ccndid(struct ccn *h, struct ccn_charbuf *local_scope_template,
129 unsigned char *ccndid)
130{
131 struct ccn_charbuf *name = NULL;
132 struct ccn_charbuf *resultbuf = NULL;
133 struct ccn_parsed_ContentObject pcobuf = {0};
134 char ccndid_uri[] = "ccnx:/%C1.M.S.localhost/%C1.M.SRV/ccnd/KEY";
135 const unsigned char *ccndid_result;
136 static size_t ccndid_result_size;
137 int res;
138
139 name = ccn_charbuf_create();
140 resultbuf = ccn_charbuf_create();
141
142 res = ccn_name_from_uri(name, ccndid_uri);
143 ON_ERROR_CLEANUP(res);
144
145 /* get Data */
146 res = ccn_get(h, name, local_scope_template, 4500, resultbuf, &pcobuf, NULL, 0);
147 ON_ERROR_CLEANUP(res);
148
149 /* extract from Data */
150 res = ccn_ref_tagged_BLOB(CCN_DTAG_PublisherPublicKeyDigest,
151 resultbuf->buf,
152 pcobuf.offset[CCN_PCO_B_PublisherPublicKeyDigest],
153 pcobuf.offset[CCN_PCO_E_PublisherPublicKeyDigest],
154 &ccndid_result, &ccndid_result_size);
155 ON_ERROR_CLEANUP(res);
156
157 memcpy((void *)ccndid, ccndid_result, ccndid_result_size);
158
159 ccn_charbuf_destroy(&name);
160 ccn_charbuf_destroy(&resultbuf);
161
162 return (ccndid_result_size);
163
164 cleanup:
165 ccn_charbuf_destroy(&name);
166 ccn_charbuf_destroy(&resultbuf);
167
168 return -1;
169}
170
171static void
172init_data(struct ccn_charbuf *local_scope_template,
173 struct ccn_charbuf *no_name)
174{
175 ccn_charbuf_append_tt(local_scope_template, CCN_DTAG_Interest, CCN_DTAG);
176 ccn_charbuf_append_tt(local_scope_template, CCN_DTAG_Name, CCN_DTAG);
177 ccn_charbuf_append_closer(local_scope_template); /* </Name> */
178 ccnb_tagged_putf(local_scope_template, CCN_DTAG_Scope, "1");
179 ccn_charbuf_append_closer(local_scope_template); /* </Interest> */
180
181 ccn_name_init(no_name);
182}
183
184int
akmhoque483c1eb2013-03-08 00:51:09 -0600185add_delete_ccn_face_by_face_id(struct ccn *h, const char *uri, int operation, int faceid, long int lifetime)
akmhoque29c1db52012-09-07 14:47:43 -0500186{
akmhoqueb77b95f2013-02-08 12:28:47 -0600187 if ( nlsr->debugging )
188 {
189 printf("add_delete_ccn_face_by_face_id called\n");
190 printf("Uri: %s Face: %d Operation: %s \n",(char *)uri , faceid, operation == OP_REG ? "Registration" : "Unregistration");
191 }
192
akmhoque29c1db52012-09-07 14:47:43 -0500193 struct ccn_charbuf *prefix;
194 struct ccn_charbuf *local_scope_template = ccn_charbuf_create();
195 struct ccn_charbuf *no_name = ccn_charbuf_create();
196 unsigned char ccndid_storage[32] = {0};
197 unsigned char *ccndid = ccndid_storage;
198 size_t ccndid_size = 0;
199 int res;
200
201 prefix = ccn_charbuf_create();
202 res = ccn_name_from_uri(prefix, uri);
203 ON_ERROR_CLEANUP(res);
204
205
206 init_data(local_scope_template, no_name);
207
208 ccndid_size = get_ccndid(h, local_scope_template, ccndid);
209 if (ccndid_size != sizeof(ccndid_storage))
210 {
211 fprintf(stderr, "Incorrect size for ccnd id in response\n");
212 ON_ERROR_CLEANUP(-1);
213 }
214
akmhoque483c1eb2013-03-08 00:51:09 -0600215 res = register_unregister_prefix(h, local_scope_template, no_name, prefix,ccndid, ccndid_size,faceid, operation,lifetime);
akmhoque29c1db52012-09-07 14:47:43 -0500216
217 ON_ERROR_CLEANUP(res);
218
219 ccn_charbuf_destroy(&local_scope_template);
220 ccn_charbuf_destroy(&no_name);
221 ccn_charbuf_destroy(&prefix);
222
223 return 0;
224
225 cleanup:
226 ccn_charbuf_destroy(&prefix);
227 ccn_charbuf_destroy(&local_scope_template);
228 ccn_charbuf_destroy(&no_name);
229
230 return -1;
231}
232
233