aboutsummaryrefslogtreecommitdiff
path: root/src/dns
diff options
context:
space:
mode:
authorng0 <ng0@n0.is>2019-09-08 12:33:09 +0000
committerng0 <ng0@n0.is>2019-09-08 12:33:09 +0000
commitd41ed82a4ea0cc8e1674b6d5d2c49fd6462610bb (patch)
tree9efd18ea7d425652085ed0bd5e8e45604bc5f6b9 /src/dns
parenta0fce305c565c0937d917a92712f15e9c5736260 (diff)
downloadgnunet-d41ed82a4ea0cc8e1674b6d5d2c49fd6462610bb.tar.gz
gnunet-d41ed82a4ea0cc8e1674b6d5d2c49fd6462610bb.zip
uncrustify as demanded.
Diffstat (limited to 'src/dns')
-rw-r--r--src/dns/dns.h17
-rw-r--r--src/dns/dns_api.c225
-rw-r--r--src/dns/gnunet-dns-monitor.c380
-rw-r--r--src/dns/gnunet-dns-redirector.c234
-rw-r--r--src/dns/gnunet-helper-dns.c1336
-rw-r--r--src/dns/gnunet-service-dns.c1251
-rw-r--r--src/dns/gnunet-zonewalk.c593
-rw-r--r--src/dns/plugin_block_dns.c191
8 files changed, 2131 insertions, 2096 deletions
diff --git a/src/dns/dns.h b/src/dns/dns.h
index af86bbaed..12d8f6025 100644
--- a/src/dns/dns.h
+++ b/src/dns/dns.h
@@ -11,7 +11,7 @@
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
@@ -32,10 +32,9 @@ GNUNET_NETWORK_STRUCT_BEGIN
32/** 32/**
33 * Message from client to DNS service to register itself. 33 * Message from client to DNS service to register itself.
34 */ 34 */
35struct GNUNET_DNS_Register 35struct GNUNET_DNS_Register {
36{
37 /** 36 /**
38 * Header of type #GNUNET_MESSAGE_TYPE_DNS_CLIENT_INIT 37 * Header of type #GNUNET_MESSAGE_TYPE_DNS_CLIENT_INIT
39 */ 38 */
40 struct GNUNET_MessageHeader header; 39 struct GNUNET_MessageHeader header;
41 40
@@ -49,10 +48,9 @@ struct GNUNET_DNS_Register
49/** 48/**
50 * Message from DNS service to client: please handle a request. 49 * Message from DNS service to client: please handle a request.
51 */ 50 */
52struct GNUNET_DNS_Request 51struct GNUNET_DNS_Request {
53{
54 /** 52 /**
55 * Header of type #GNUNET_MESSAGE_TYPE_DNS_CLIENT_REQUEST 53 * Header of type #GNUNET_MESSAGE_TYPE_DNS_CLIENT_REQUEST
56 */ 54 */
57 struct GNUNET_MessageHeader header; 55 struct GNUNET_MessageHeader header;
58 56
@@ -67,15 +65,13 @@ struct GNUNET_DNS_Request
67 uint64_t request_id GNUNET_PACKED; 65 uint64_t request_id GNUNET_PACKED;
68 66
69 /* followed by original DNS request (without UDP header) */ 67 /* followed by original DNS request (without UDP header) */
70
71}; 68};
72 69
73 70
74/** 71/**
75 * Message from client to DNS service: here is my reply. 72 * Message from client to DNS service: here is my reply.
76 */ 73 */
77struct GNUNET_DNS_Response 74struct GNUNET_DNS_Response {
78{
79 /** 75 /**
80 * Header of type #GNUNET_MESSAGE_TYPE_DNS_CLIENT_RESPONSE 76 * Header of type #GNUNET_MESSAGE_TYPE_DNS_CLIENT_RESPONSE
81 */ 77 */
@@ -92,7 +88,6 @@ struct GNUNET_DNS_Response
92 uint64_t request_id GNUNET_PACKED; 88 uint64_t request_id GNUNET_PACKED;
93 89
94 /* followed by original DNS request (without UDP header) */ 90 /* followed by original DNS request (without UDP header) */
95
96}; 91};
97 92
98 93
diff --git a/src/dns/dns_api.c b/src/dns/dns_api.c
index e34f02ef0..4dc8e00dd 100644
--- a/src/dns/dns_api.c
+++ b/src/dns/dns_api.c
@@ -11,7 +11,7 @@
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
@@ -31,9 +31,7 @@
31/** 31/**
32 * Handle to identify an individual DNS request. 32 * Handle to identify an individual DNS request.
33 */ 33 */
34struct GNUNET_DNS_RequestHandle 34struct GNUNET_DNS_RequestHandle {
35{
36
37 /** 35 /**
38 * Handle to DNS API. 36 * Handle to DNS API.
39 */ 37 */
@@ -48,16 +46,13 @@ struct GNUNET_DNS_RequestHandle
48 * Re-connect counter, to make sure we did not reconnect in the meantime. 46 * Re-connect counter, to make sure we did not reconnect in the meantime.
49 */ 47 */
50 uint32_t generation; 48 uint32_t generation;
51
52}; 49};
53 50
54 51
55/** 52/**
56 * DNS handle 53 * DNS handle
57 */ 54 */
58struct GNUNET_DNS_Handle 55struct GNUNET_DNS_Handle {
59{
60
61 /** 56 /**
62 * Connection to DNS service, or NULL. 57 * Connection to DNS service, or NULL.
63 */ 58 */
@@ -108,7 +103,7 @@ struct GNUNET_DNS_Handle
108 * @param tc scheduler context (unused) 103 * @param tc scheduler context (unused)
109 */ 104 */
110static void 105static void
111reconnect (void *cls); 106reconnect(void *cls);
112 107
113 108
114/** 109/**
@@ -117,17 +112,17 @@ reconnect (void *cls);
117 * @param dh handle with the connection 112 * @param dh handle with the connection
118 */ 113 */
119static void 114static void
120force_reconnect (struct GNUNET_DNS_Handle *dh) 115force_reconnect(struct GNUNET_DNS_Handle *dh)
121{ 116{
122 if (NULL != dh->mq) 117 if (NULL != dh->mq)
123 { 118 {
124 GNUNET_MQ_destroy (dh->mq); 119 GNUNET_MQ_destroy(dh->mq);
125 dh->mq = NULL; 120 dh->mq = NULL;
126 } 121 }
127 dh->reconnect_task = 122 dh->reconnect_task =
128 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, 123 GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_UNIT_SECONDS,
129 &reconnect, 124 &reconnect,
130 dh); 125 dh);
131} 126}
132 127
133 128
@@ -140,12 +135,12 @@ force_reconnect (struct GNUNET_DNS_Handle *dh)
140 * @param error error code 135 * @param error error code
141 */ 136 */
142static void 137static void
143mq_error_handler (void *cls, 138mq_error_handler(void *cls,
144 enum GNUNET_MQ_Error error) 139 enum GNUNET_MQ_Error error)
145{ 140{
146 struct GNUNET_DNS_Handle *dh = cls; 141 struct GNUNET_DNS_Handle *dh = cls;
147 142
148 force_reconnect (dh); 143 force_reconnect(dh);
149} 144}
150 145
151 146
@@ -158,14 +153,14 @@ mq_error_handler (void *cls,
158 * @param req message from the service (request) 153 * @param req message from the service (request)
159 */ 154 */
160static int 155static int
161check_request (void *cls, 156check_request(void *cls,
162 const struct GNUNET_DNS_Request *req) 157 const struct GNUNET_DNS_Request *req)
163{ 158{
164 if (0 != ntohl (req->reserved)) 159 if (0 != ntohl(req->reserved))
165 { 160 {
166 GNUNET_break (0); 161 GNUNET_break(0);
167 return GNUNET_SYSERR; 162 return GNUNET_SYSERR;
168 } 163 }
169 return GNUNET_OK; 164 return GNUNET_OK;
170} 165}
171 166
@@ -178,22 +173,22 @@ check_request (void *cls,
178 * @param msg message from the service (request) 173 * @param msg message from the service (request)
179 */ 174 */
180static void 175static void
181handle_request (void *cls, 176handle_request(void *cls,
182 const struct GNUNET_DNS_Request *req) 177 const struct GNUNET_DNS_Request *req)
183{ 178{
184 struct GNUNET_DNS_Handle *dh = cls; 179 struct GNUNET_DNS_Handle *dh = cls;
185 size_t payload_length = ntohs (req->header.size) - sizeof (*req); 180 size_t payload_length = ntohs(req->header.size) - sizeof(*req);
186 struct GNUNET_DNS_RequestHandle *rh; 181 struct GNUNET_DNS_RequestHandle *rh;
187 182
188 rh = GNUNET_new (struct GNUNET_DNS_RequestHandle); 183 rh = GNUNET_new(struct GNUNET_DNS_RequestHandle);
189 rh->dh =dh; 184 rh->dh = dh;
190 rh->request_id = req->request_id; 185 rh->request_id = req->request_id;
191 rh->generation = dh->generation; 186 rh->generation = dh->generation;
192 dh->pending_requests++; 187 dh->pending_requests++;
193 dh->rh (dh->rh_cls, 188 dh->rh(dh->rh_cls,
194 rh, 189 rh,
195 payload_length, 190 payload_length,
196 (const char*) &req[1]); 191 (const char*)&req[1]);
197} 192}
198 193
199 194
@@ -203,33 +198,33 @@ handle_request (void *cls,
203 * @param cls handle with the connection to connect 198 * @param cls handle with the connection to connect
204 */ 199 */
205static void 200static void
206reconnect (void *cls) 201reconnect(void *cls)
207{ 202{
208 struct GNUNET_DNS_Handle *dh = cls; 203 struct GNUNET_DNS_Handle *dh = cls;
209 struct GNUNET_MQ_MessageHandler handlers[] = { 204 struct GNUNET_MQ_MessageHandler handlers[] = {
210 GNUNET_MQ_hd_var_size (request, 205 GNUNET_MQ_hd_var_size(request,
211 GNUNET_MESSAGE_TYPE_DNS_CLIENT_REQUEST, 206 GNUNET_MESSAGE_TYPE_DNS_CLIENT_REQUEST,
212 struct GNUNET_DNS_Request, 207 struct GNUNET_DNS_Request,
213 dh), 208 dh),
214 GNUNET_MQ_handler_end () 209 GNUNET_MQ_handler_end()
215 }; 210 };
216 struct GNUNET_MQ_Envelope *env; 211 struct GNUNET_MQ_Envelope *env;
217 struct GNUNET_DNS_Register *msg; 212 struct GNUNET_DNS_Register *msg;
218 213
219 dh->reconnect_task = NULL; 214 dh->reconnect_task = NULL;
220 dh->mq = GNUNET_CLIENT_connect (dh->cfg, 215 dh->mq = GNUNET_CLIENT_connect(dh->cfg,
221 "dns", 216 "dns",
222 handlers, 217 handlers,
223 &mq_error_handler, 218 &mq_error_handler,
224 dh); 219 dh);
225 if (NULL == dh->mq) 220 if (NULL == dh->mq)
226 return; 221 return;
227 dh->generation++; 222 dh->generation++;
228 env = GNUNET_MQ_msg (msg, 223 env = GNUNET_MQ_msg(msg,
229 GNUNET_MESSAGE_TYPE_DNS_CLIENT_INIT); 224 GNUNET_MESSAGE_TYPE_DNS_CLIENT_INIT);
230 msg->flags = htonl (dh->flags); 225 msg->flags = htonl(dh->flags);
231 GNUNET_MQ_send (dh->mq, 226 GNUNET_MQ_send(dh->mq,
232 env); 227 env);
233} 228}
234 229
235 230
@@ -244,24 +239,24 @@ reconnect (void *cls)
244 * @param rh request that should now be forwarded 239 * @param rh request that should now be forwarded
245 */ 240 */
246void 241void
247GNUNET_DNS_request_forward (struct GNUNET_DNS_RequestHandle *rh) 242GNUNET_DNS_request_forward(struct GNUNET_DNS_RequestHandle *rh)
248{ 243{
249 struct GNUNET_MQ_Envelope *env; 244 struct GNUNET_MQ_Envelope *env;
250 struct GNUNET_DNS_Response *resp; 245 struct GNUNET_DNS_Response *resp;
251 246
252 GNUNET_assert (0 < rh->dh->pending_requests--); 247 GNUNET_assert(0 < rh->dh->pending_requests--);
253 if (rh->generation != rh->dh->generation) 248 if (rh->generation != rh->dh->generation)
254 { 249 {
255 GNUNET_free (rh); 250 GNUNET_free(rh);
256 return; 251 return;
257 } 252 }
258 env = GNUNET_MQ_msg (resp, 253 env = GNUNET_MQ_msg(resp,
259 GNUNET_MESSAGE_TYPE_DNS_CLIENT_RESPONSE); 254 GNUNET_MESSAGE_TYPE_DNS_CLIENT_RESPONSE);
260 resp->drop_flag = htonl (1); 255 resp->drop_flag = htonl(1);
261 resp->request_id = rh->request_id; 256 resp->request_id = rh->request_id;
262 GNUNET_MQ_send (rh->dh->mq, 257 GNUNET_MQ_send(rh->dh->mq,
263 env); 258 env);
264 GNUNET_free (rh); 259 GNUNET_free(rh);
265} 260}
266 261
267 262
@@ -272,24 +267,24 @@ GNUNET_DNS_request_forward (struct GNUNET_DNS_RequestHandle *rh)
272 * @param rh request that should now be dropped 267 * @param rh request that should now be dropped
273 */ 268 */
274void 269void
275GNUNET_DNS_request_drop (struct GNUNET_DNS_RequestHandle *rh) 270GNUNET_DNS_request_drop(struct GNUNET_DNS_RequestHandle *rh)
276{ 271{
277 struct GNUNET_MQ_Envelope *env; 272 struct GNUNET_MQ_Envelope *env;
278 struct GNUNET_DNS_Response *resp; 273 struct GNUNET_DNS_Response *resp;
279 274
280 GNUNET_assert (0 < rh->dh->pending_requests--); 275 GNUNET_assert(0 < rh->dh->pending_requests--);
281 if (rh->generation != rh->dh->generation) 276 if (rh->generation != rh->dh->generation)
282 { 277 {
283 GNUNET_free (rh); 278 GNUNET_free(rh);
284 return; 279 return;
285 } 280 }
286 env = GNUNET_MQ_msg (resp, 281 env = GNUNET_MQ_msg(resp,
287 GNUNET_MESSAGE_TYPE_DNS_CLIENT_RESPONSE); 282 GNUNET_MESSAGE_TYPE_DNS_CLIENT_RESPONSE);
288 resp->request_id = rh->request_id; 283 resp->request_id = rh->request_id;
289 resp->drop_flag = htonl (0); 284 resp->drop_flag = htonl(0);
290 GNUNET_MQ_send (rh->dh->mq, 285 GNUNET_MQ_send(rh->dh->mq,
291 env); 286 env);
292 GNUNET_free (rh); 287 GNUNET_free(rh);
293} 288}
294 289
295 290
@@ -303,37 +298,37 @@ GNUNET_DNS_request_drop (struct GNUNET_DNS_RequestHandle *rh)
303 * @param reply reply data 298 * @param reply reply data
304 */ 299 */
305void 300void
306GNUNET_DNS_request_answer (struct GNUNET_DNS_RequestHandle *rh, 301GNUNET_DNS_request_answer(struct GNUNET_DNS_RequestHandle *rh,
307 uint16_t reply_length, 302 uint16_t reply_length,
308 const char *reply) 303 const char *reply)
309{ 304{
310 struct GNUNET_MQ_Envelope *env; 305 struct GNUNET_MQ_Envelope *env;
311 struct GNUNET_DNS_Response *resp; 306 struct GNUNET_DNS_Response *resp;
312 307
313 GNUNET_assert (0 < rh->dh->pending_requests--); 308 GNUNET_assert(0 < rh->dh->pending_requests--);
314 if (rh->generation != rh->dh->generation) 309 if (rh->generation != rh->dh->generation)
315 { 310 {
316 GNUNET_free (rh); 311 GNUNET_free(rh);
317 return; 312 return;
318 } 313 }
319 if (reply_length + sizeof (struct GNUNET_DNS_Response) 314 if (reply_length + sizeof(struct GNUNET_DNS_Response)
320 >= GNUNET_MAX_MESSAGE_SIZE) 315 >= GNUNET_MAX_MESSAGE_SIZE)
321 { 316 {
322 GNUNET_break (0); 317 GNUNET_break(0);
323 GNUNET_free (rh); 318 GNUNET_free(rh);
324 return; 319 return;
325 } 320 }
326 env = GNUNET_MQ_msg_extra (resp, 321 env = GNUNET_MQ_msg_extra(resp,
327 reply_length, 322 reply_length,
328 GNUNET_MESSAGE_TYPE_DNS_CLIENT_RESPONSE); 323 GNUNET_MESSAGE_TYPE_DNS_CLIENT_RESPONSE);
329 resp->drop_flag = htonl (2); 324 resp->drop_flag = htonl(2);
330 resp->request_id = rh->request_id; 325 resp->request_id = rh->request_id;
331 GNUNET_memcpy (&resp[1], 326 GNUNET_memcpy(&resp[1],
332 reply, 327 reply,
333 reply_length); 328 reply_length);
334 GNUNET_MQ_send (rh->dh->mq, 329 GNUNET_MQ_send(rh->dh->mq,
335 env); 330 env);
336 GNUNET_free (rh); 331 GNUNET_free(rh);
337} 332}
338 333
339 334
@@ -347,19 +342,19 @@ GNUNET_DNS_request_answer (struct GNUNET_DNS_RequestHandle *rh,
347 * @return DNS handle 342 * @return DNS handle
348 */ 343 */
349struct GNUNET_DNS_Handle * 344struct GNUNET_DNS_Handle *
350GNUNET_DNS_connect (const struct GNUNET_CONFIGURATION_Handle *cfg, 345GNUNET_DNS_connect(const struct GNUNET_CONFIGURATION_Handle *cfg,
351 enum GNUNET_DNS_Flags flags, 346 enum GNUNET_DNS_Flags flags,
352 GNUNET_DNS_RequestHandler rh, 347 GNUNET_DNS_RequestHandler rh,
353 void *rh_cls) 348 void *rh_cls)
354{ 349{
355 struct GNUNET_DNS_Handle *dh; 350 struct GNUNET_DNS_Handle *dh;
356 351
357 dh = GNUNET_new (struct GNUNET_DNS_Handle); 352 dh = GNUNET_new(struct GNUNET_DNS_Handle);
358 dh->cfg = cfg; 353 dh->cfg = cfg;
359 dh->flags = flags; 354 dh->flags = flags;
360 dh->rh = rh; 355 dh->rh = rh;
361 dh->rh_cls = rh_cls; 356 dh->rh_cls = rh_cls;
362 dh->reconnect_task = GNUNET_SCHEDULER_add_now (&reconnect, dh); 357 dh->reconnect_task = GNUNET_SCHEDULER_add_now(&reconnect, dh);
363 return dh; 358 return dh;
364} 359}
365 360
@@ -370,21 +365,21 @@ GNUNET_DNS_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
370 * @param dh DNS handle 365 * @param dh DNS handle
371 */ 366 */
372void 367void
373GNUNET_DNS_disconnect (struct GNUNET_DNS_Handle *dh) 368GNUNET_DNS_disconnect(struct GNUNET_DNS_Handle *dh)
374{ 369{
375 if (NULL != dh->mq) 370 if (NULL != dh->mq)
376 { 371 {
377 GNUNET_MQ_destroy (dh->mq); 372 GNUNET_MQ_destroy(dh->mq);
378 dh->mq = NULL; 373 dh->mq = NULL;
379 } 374 }
380 if (NULL != dh->reconnect_task) 375 if (NULL != dh->reconnect_task)
381 { 376 {
382 GNUNET_SCHEDULER_cancel (dh->reconnect_task); 377 GNUNET_SCHEDULER_cancel(dh->reconnect_task);
383 dh->reconnect_task = NULL; 378 dh->reconnect_task = NULL;
384 } 379 }
385 /* make sure client has no pending requests left over! */ 380 /* make sure client has no pending requests left over! */
386 GNUNET_break (0 == dh->pending_requests); 381 GNUNET_break(0 == dh->pending_requests);
387 GNUNET_free (dh); 382 GNUNET_free(dh);
388} 383}
389 384
390/* end of dns_api.c */ 385/* end of dns_api.c */
diff --git a/src/dns/gnunet-dns-monitor.c b/src/dns/gnunet-dns-monitor.c
index e822b3211..819cb025d 100644
--- a/src/dns/gnunet-dns-monitor.c
+++ b/src/dns/gnunet-dns-monitor.c
@@ -11,12 +11,12 @@
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20 20
21/** 21/**
22 * @file src/dns/gnunet-dns-monitor.c 22 * @file src/dns/gnunet-dns-monitor.c
@@ -62,22 +62,31 @@ static unsigned int verbosity;
62 * @return type as string, only valid until the next call to this function 62 * @return type as string, only valid until the next call to this function
63 */ 63 */
64static const char * 64static const char *
65get_type (uint16_t type) 65get_type(uint16_t type)
66{ 66{
67 static char buf[6]; 67 static char buf[6];
68
68 switch (type) 69 switch (type)
69 { 70 {
70 case GNUNET_DNSPARSER_TYPE_A: return "A"; 71 case GNUNET_DNSPARSER_TYPE_A: return "A";
71 case GNUNET_DNSPARSER_TYPE_NS: return "NS"; 72
72 case GNUNET_DNSPARSER_TYPE_CNAME: return "CNAME"; 73 case GNUNET_DNSPARSER_TYPE_NS: return "NS";
73 case GNUNET_DNSPARSER_TYPE_SOA: return "SOA"; 74
74 case GNUNET_DNSPARSER_TYPE_PTR: return "PTR"; 75 case GNUNET_DNSPARSER_TYPE_CNAME: return "CNAME";
75 case GNUNET_DNSPARSER_TYPE_MX: return "MX"; 76
76 case GNUNET_DNSPARSER_TYPE_TXT: return "TXT"; 77 case GNUNET_DNSPARSER_TYPE_SOA: return "SOA";
77 case GNUNET_DNSPARSER_TYPE_AAAA: return "AAAA"; 78
78 case GNUNET_DNSPARSER_TYPE_SRV: return "SRV"; 79 case GNUNET_DNSPARSER_TYPE_PTR: return "PTR";
79 } 80
80 GNUNET_snprintf (buf, sizeof (buf), "%u", (unsigned int) type); 81 case GNUNET_DNSPARSER_TYPE_MX: return "MX";
82
83 case GNUNET_DNSPARSER_TYPE_TXT: return "TXT";
84
85 case GNUNET_DNSPARSER_TYPE_AAAA: return "AAAA";
86
87 case GNUNET_DNSPARSER_TYPE_SRV: return "SRV";
88 }
89 GNUNET_snprintf(buf, sizeof(buf), "%u", (unsigned int)type);
81 return buf; 90 return buf;
82} 91}
83 92
@@ -89,16 +98,19 @@ get_type (uint16_t type)
89 * @return class as string, only valid until the next call to this function 98 * @return class as string, only valid until the next call to this function
90 */ 99 */
91static const char * 100static const char *
92get_class (uint16_t class) 101get_class(uint16_t class)
93{ 102{
94 static char buf[6]; 103 static char buf[6];
104
95 switch (class) 105 switch (class)
96 { 106 {
97 case GNUNET_TUN_DNS_CLASS_INTERNET: return "IN"; 107 case GNUNET_TUN_DNS_CLASS_INTERNET: return "IN";
98 case GNUNET_TUN_DNS_CLASS_CHAOS: return "CHAOS"; 108
99 case GNUNET_TUN_DNS_CLASS_HESIOD: return "HESIOD"; 109 case GNUNET_TUN_DNS_CLASS_CHAOS: return "CHAOS";
100 } 110
101 GNUNET_snprintf (buf, sizeof (buf), "%u", (unsigned int) class); 111 case GNUNET_TUN_DNS_CLASS_HESIOD: return "HESIOD";
112 }
113 GNUNET_snprintf(buf, sizeof(buf), "%u", (unsigned int)class);
102 return buf; 114 return buf;
103} 115}
104 116
@@ -109,13 +121,13 @@ get_class (uint16_t class)
109 * @param query query to display. 121 * @param query query to display.
110 */ 122 */
111static void 123static void
112display_query (const struct GNUNET_DNSPARSER_Query *query) 124display_query(const struct GNUNET_DNSPARSER_Query *query)
113{ 125{
114 fprintf (stdout, 126 fprintf(stdout,
115 "\t\t%s %s: %s\n", 127 "\t\t%s %s: %s\n",
116 get_class (query->dns_traffic_class), 128 get_class(query->dns_traffic_class),
117 get_type (query->type), 129 get_type(query->type),
118 query->name); 130 query->name);
119} 131}
120 132
121 133
@@ -125,7 +137,7 @@ display_query (const struct GNUNET_DNSPARSER_Query *query)
125 * @param record record to display. 137 * @param record record to display.
126 */ 138 */
127static void 139static void
128display_record (const struct GNUNET_DNSPARSER_Record *record) 140display_record(const struct GNUNET_DNSPARSER_Record *record)
129{ 141{
130 const char *format; 142 const char *format;
131 char buf[INET6_ADDRSTRLEN]; 143 char buf[INET6_ADDRSTRLEN];
@@ -133,86 +145,93 @@ display_record (const struct GNUNET_DNSPARSER_Record *record)
133 145
134 tmp = NULL; 146 tmp = NULL;
135 switch (record->type) 147 switch (record->type)
136 {
137 case GNUNET_DNSPARSER_TYPE_A:
138 if (record->data.raw.data_len != sizeof (struct in_addr))
139 format = "<invalid>";
140 else
141 format = inet_ntop (AF_INET, record->data.raw.data, buf, sizeof (buf));
142 break;
143 case GNUNET_DNSPARSER_TYPE_AAAA:
144 if (record->data.raw.data_len != sizeof (struct in6_addr))
145 format = "<invalid>";
146 else
147 format = inet_ntop (AF_INET6, record->data.raw.data, buf, sizeof (buf));
148 break;
149 case GNUNET_DNSPARSER_TYPE_NS:
150 case GNUNET_DNSPARSER_TYPE_CNAME:
151 case GNUNET_DNSPARSER_TYPE_PTR:
152 format = record->data.hostname;
153 break;
154 case GNUNET_DNSPARSER_TYPE_SOA:
155 if (NULL == record->data.soa)
156 format = "<invalid>";
157 else
158 { 148 {
159 GNUNET_asprintf (&tmp, 149 case GNUNET_DNSPARSER_TYPE_A:
160 "origin: %s, mail: %s, serial = %u, refresh = %u s, retry = %u s, expire = %u s, minimum = %u s", 150 if (record->data.raw.data_len != sizeof(struct in_addr))
161 record->data.soa->mname, 151 format = "<invalid>";
162 record->data.soa->rname, 152 else
163 (unsigned int) record->data.soa->serial, 153 format = inet_ntop(AF_INET, record->data.raw.data, buf, sizeof(buf));
164 (unsigned int) record->data.soa->refresh, 154 break;
165 (unsigned int) record->data.soa->retry, 155
166 (unsigned int) record->data.soa->expire, 156 case GNUNET_DNSPARSER_TYPE_AAAA:
167 (unsigned int) record->data.soa->minimum_ttl); 157 if (record->data.raw.data_len != sizeof(struct in6_addr))
168 format = tmp; 158 format = "<invalid>";
169 } 159 else
170 break; 160 format = inet_ntop(AF_INET6, record->data.raw.data, buf, sizeof(buf));
171 case GNUNET_DNSPARSER_TYPE_MX: 161 break;
172 if (record->data.mx == NULL) 162
173 format = "<invalid>"; 163 case GNUNET_DNSPARSER_TYPE_NS:
174 else 164 case GNUNET_DNSPARSER_TYPE_CNAME:
175 { 165 case GNUNET_DNSPARSER_TYPE_PTR:
176 GNUNET_asprintf (&tmp, 166 format = record->data.hostname;
177 "%u: %s", 167 break;
178 record->data.mx->preference, 168
179 record->data.mx->mxhost); 169 case GNUNET_DNSPARSER_TYPE_SOA:
180 format = tmp; 170 if (NULL == record->data.soa)
181 } 171 format = "<invalid>";
182 break; 172 else
183 case GNUNET_DNSPARSER_TYPE_SRV: 173 {
184 if (NULL == record->data.srv) 174 GNUNET_asprintf(&tmp,
185 format = "<invalid>"; 175 "origin: %s, mail: %s, serial = %u, refresh = %u s, retry = %u s, expire = %u s, minimum = %u s",
186 else 176 record->data.soa->mname,
187 { 177 record->data.soa->rname,
188 GNUNET_asprintf (&tmp, 178 (unsigned int)record->data.soa->serial,
189 "priority %u, weight = %s, port = %u, target = %s", 179 (unsigned int)record->data.soa->refresh,
190 (unsigned int) record->data.srv->priority, 180 (unsigned int)record->data.soa->retry,
191 (unsigned int) record->data.srv->weight, 181 (unsigned int)record->data.soa->expire,
192 (unsigned int) record->data.srv->port, 182 (unsigned int)record->data.soa->minimum_ttl);
193 record->data.srv->target); 183 format = tmp;
184 }
185 break;
186
187 case GNUNET_DNSPARSER_TYPE_MX:
188 if (record->data.mx == NULL)
189 format = "<invalid>";
190 else
191 {
192 GNUNET_asprintf(&tmp,
193 "%u: %s",
194 record->data.mx->preference,
195 record->data.mx->mxhost);
196 format = tmp;
197 }
198 break;
199
200 case GNUNET_DNSPARSER_TYPE_SRV:
201 if (NULL == record->data.srv)
202 format = "<invalid>";
203 else
204 {
205 GNUNET_asprintf(&tmp,
206 "priority %u, weight = %s, port = %u, target = %s",
207 (unsigned int)record->data.srv->priority,
208 (unsigned int)record->data.srv->weight,
209 (unsigned int)record->data.srv->port,
210 record->data.srv->target);
211 format = tmp;
212 }
213 break;
214
215 case GNUNET_DNSPARSER_TYPE_TXT:
216 GNUNET_asprintf(&tmp,
217 "%.*s",
218 (unsigned int)record->data.raw.data_len,
219 record->data.raw.data);
194 format = tmp; 220 format = tmp;
221 break;
222
223 default:
224 format = "<payload>";
225 break;
195 } 226 }
196 break; 227 fprintf(stdout,
197 case GNUNET_DNSPARSER_TYPE_TXT: 228 "\t\t%s %s: %s = %s (%u s)\n",
198 GNUNET_asprintf (&tmp, 229 get_class(record->dns_traffic_class),
199 "%.*s", 230 get_type(record->type),
200 (unsigned int) record->data.raw.data_len, 231 record->name,
201 record->data.raw.data); 232 format,
202 format = tmp; 233 (unsigned int)(GNUNET_TIME_absolute_get_remaining(record->expiration_time).rel_value_us / 1000LL / 1000LL));
203 break; 234 GNUNET_free_non_null(tmp);
204 default:
205 format = "<payload>";
206 break;
207 }
208 fprintf (stdout,
209 "\t\t%s %s: %s = %s (%u s)\n",
210 get_class (record->dns_traffic_class),
211 get_type (record->type),
212 record->name,
213 format,
214 (unsigned int) (GNUNET_TIME_absolute_get_remaining (record->expiration_time).rel_value_us / 1000LL / 1000LL));
215 GNUNET_free_non_null (tmp);
216} 235}
217 236
218 237
@@ -240,62 +259,62 @@ display_record (const struct GNUNET_DNSPARSER_Record *record)
240 * @param request udp payload of the DNS request 259 * @param request udp payload of the DNS request
241 */ 260 */
242static void 261static void
243display_request (void *cls, 262display_request(void *cls,
244 struct GNUNET_DNS_RequestHandle *rh, 263 struct GNUNET_DNS_RequestHandle *rh,
245 size_t request_length, 264 size_t request_length,
246 const char *request) 265 const char *request)
247{ 266{
248 static const char *return_codes[] = 267 static const char *return_codes[] =
249 { 268 {
250 "No error", "Format error", "Server failure", "Name error", 269 "No error", "Format error", "Server failure", "Name error",
251 "Not implemented", "Refused", "YXDomain", "YXRRset", 270 "Not implemented", "Refused", "YXDomain", "YXRRset",
252 "NXRRset", "NOT AUTH", "NOT ZONE", "<invalid>", 271 "NXRRset", "NOT AUTH", "NOT ZONE", "<invalid>",
253 "<invalid>", "<invalid>", "<invalid>", "<invalid>" 272 "<invalid>", "<invalid>", "<invalid>", "<invalid>"
254 }; 273 };
255 static const char *op_codes[] = 274 static const char *op_codes[] =
256 { 275 {
257 "Query", "Inverse query", "Status", "<invalid>", 276 "Query", "Inverse query", "Status", "<invalid>",
258 "<invalid>", "<invalid>", "<invalid>", "<invalid>", 277 "<invalid>", "<invalid>", "<invalid>", "<invalid>",
259 "<invalid>", "<invalid>", "<invalid>", "<invalid>", 278 "<invalid>", "<invalid>", "<invalid>", "<invalid>",
260 "<invalid>", "<invalid>", "<invalid>", "<invalid>" 279 "<invalid>", "<invalid>", "<invalid>", "<invalid>"
261 }; 280 };
262 struct GNUNET_DNSPARSER_Packet *p; 281 struct GNUNET_DNSPARSER_Packet *p;
263 unsigned int i; 282 unsigned int i;
264 283
265 p = GNUNET_DNSPARSER_parse (request, request_length); 284 p = GNUNET_DNSPARSER_parse(request, request_length);
266 if (NULL == p) 285 if (NULL == p)
267 { 286 {
268 fprintf (stderr, "Received malformed DNS packet!\n"); 287 fprintf(stderr, "Received malformed DNS packet!\n");
269 // FIXME: drop instead? 288 // FIXME: drop instead?
270 GNUNET_DNS_request_forward (rh); 289 GNUNET_DNS_request_forward(rh);
271 return; 290 return;
272 } 291 }
273 fprintf (stdout, 292 fprintf(stdout,
274 "%s with ID: %5u Flags: %s%s%s%s%s%s, Return Code: %s, Opcode: %s\n", 293 "%s with ID: %5u Flags: %s%s%s%s%s%s, Return Code: %s, Opcode: %s\n",
275 p->flags.query_or_response ? "Response" : "Query", 294 p->flags.query_or_response ? "Response" : "Query",
276 p->id, 295 p->id,
277 p->flags.recursion_desired ? "RD " : "", 296 p->flags.recursion_desired ? "RD " : "",
278 p->flags.message_truncated ? "MT " : "", 297 p->flags.message_truncated ? "MT " : "",
279 p->flags.authoritative_answer ? "AA " : "", 298 p->flags.authoritative_answer ? "AA " : "",
280 p->flags.checking_disabled ? "CD " : "", 299 p->flags.checking_disabled ? "CD " : "",
281 p->flags.authenticated_data ? "AD " : "", 300 p->flags.authenticated_data ? "AD " : "",
282 p->flags.recursion_available ? "RA " : "", 301 p->flags.recursion_available ? "RA " : "",
283 return_codes[p->flags.return_code & 15], 302 return_codes[p->flags.return_code & 15],
284 op_codes[p->flags.opcode & 15]); 303 op_codes[p->flags.opcode & 15]);
285 if (p->num_queries > 0) 304 if (p->num_queries > 0)
286 fprintf (stdout, 305 fprintf(stdout,
287 "\tQueries:\n"); 306 "\tQueries:\n");
288 for (i=0;i<p->num_queries;i++) 307 for (i = 0; i < p->num_queries; i++)
289 display_query (&p->queries[i]); 308 display_query(&p->queries[i]);
290 309
291 if (p->num_answers > 0) 310 if (p->num_answers > 0)
292 fprintf (stdout, 311 fprintf(stdout,
293 "\tAnswers:\n"); 312 "\tAnswers:\n");
294 for (i=0;i<p->num_answers;i++) 313 for (i = 0; i < p->num_answers; i++)
295 display_record (&p->answers[i]); 314 display_record(&p->answers[i]);
296 fprintf (stdout, "\n"); 315 fprintf(stdout, "\n");
297 GNUNET_DNSPARSER_free_packet (p); 316 GNUNET_DNSPARSER_free_packet(p);
298 GNUNET_DNS_request_forward (rh); 317 GNUNET_DNS_request_forward(rh);
299} 318}
300 319
301 320
@@ -303,13 +322,13 @@ display_request (void *cls,
303 * Shutdown. 322 * Shutdown.
304 */ 323 */
305static void 324static void
306do_disconnect (void *cls) 325do_disconnect(void *cls)
307{ 326{
308 if (NULL != handle) 327 if (NULL != handle)
309 { 328 {
310 GNUNET_DNS_disconnect (handle); 329 GNUNET_DNS_disconnect(handle);
311 handle = NULL; 330 handle = NULL;
312 } 331 }
313} 332}
314 333
315 334
@@ -322,8 +341,8 @@ do_disconnect (void *cls)
322 * @param cfg configuration 341 * @param cfg configuration
323 */ 342 */
324static void 343static void
325run (void *cls, char *const *args, const char *cfgfile, 344run(void *cls, char *const *args, const char *cfgfile,
326 const struct GNUNET_CONFIGURATION_Handle *cfg) 345 const struct GNUNET_CONFIGURATION_Handle *cfg)
327{ 346{
328 enum GNUNET_DNS_Flags flags; 347 enum GNUNET_DNS_Flags flags;
329 348
@@ -335,41 +354,40 @@ run (void *cls, char *const *args, const char *cfgfile,
335 if (outbound_only) 354 if (outbound_only)
336 flags |= GNUNET_DNS_FLAG_RESPONSE_MONITOR; 355 flags |= GNUNET_DNS_FLAG_RESPONSE_MONITOR;
337 handle = 356 handle =
338 GNUNET_DNS_connect (cfg, 357 GNUNET_DNS_connect(cfg,
339 flags, 358 flags,
340 &display_request, 359 &display_request,
341 NULL); 360 NULL);
342 GNUNET_SCHEDULER_add_shutdown (&do_disconnect, NULL); 361 GNUNET_SCHEDULER_add_shutdown(&do_disconnect, NULL);
343} 362}
344 363
345 364
346int 365int
347main (int argc, char *const *argv) 366main(int argc, char *const *argv)
348{ 367{
349 struct GNUNET_GETOPT_CommandLineOption options[] = { 368 struct GNUNET_GETOPT_CommandLineOption options[] = {
369 GNUNET_GETOPT_option_flag('i',
370 "inbound-only",
371 gettext_noop("only monitor DNS queries"),
372 &inbound_only),
350 373
351 GNUNET_GETOPT_option_flag ('i', 374 GNUNET_GETOPT_option_flag('o',
352 "inbound-only", 375 "outbound-only",
353 gettext_noop ("only monitor DNS queries"), 376 gettext_noop("only monitor DNS queries"),
354 &inbound_only), 377 &outbound_only),
355
356 GNUNET_GETOPT_option_flag ('o',
357 "outbound-only",
358 gettext_noop ("only monitor DNS queries"),
359 &outbound_only),
360 378
361 GNUNET_GETOPT_option_verbose (&verbosity), 379 GNUNET_GETOPT_option_verbose(&verbosity),
362 GNUNET_GETOPT_OPTION_END 380 GNUNET_GETOPT_OPTION_END
363 }; 381 };
364 382
365 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv)) 383 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args(argc, argv, &argc, &argv))
366 return 2; 384 return 2;
367 ret = (GNUNET_OK == 385 ret = (GNUNET_OK ==
368 GNUNET_PROGRAM_run (argc, argv, "gnunet-dns-monitor", 386 GNUNET_PROGRAM_run(argc, argv, "gnunet-dns-monitor",
369 gettext_noop 387 gettext_noop
370 ("Monitor DNS queries."), options, 388 ("Monitor DNS queries."), options,
371 &run, NULL)) ? ret : 1; 389 &run, NULL)) ? ret : 1;
372 GNUNET_free ((void*) argv); 390 GNUNET_free((void*)argv);
373 return ret; 391 return ret;
374} 392}
375 393
diff --git a/src/dns/gnunet-dns-redirector.c b/src/dns/gnunet-dns-redirector.c
index 608540e12..f1978ce92 100644
--- a/src/dns/gnunet-dns-redirector.c
+++ b/src/dns/gnunet-dns-redirector.c
@@ -11,12 +11,12 @@
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20 20
21/** 21/**
22 * @file src/dns/gnunet-dns-redirector.c 22 * @file src/dns/gnunet-dns-redirector.c
@@ -61,48 +61,51 @@ static unsigned int verbosity;
61 * @param record record to modify 61 * @param record record to modify
62 */ 62 */
63static void 63static void
64modify_record (const struct GNUNET_DNSPARSER_Record *record) 64modify_record(const struct GNUNET_DNSPARSER_Record *record)
65{ 65{
66 char buf[INET6_ADDRSTRLEN]; 66 char buf[INET6_ADDRSTRLEN];
67 67
68 switch (record->type) 68 switch (record->type)
69 {
70 case GNUNET_DNSPARSER_TYPE_A:
71 if (record->data.raw.data_len != sizeof (struct in_addr))
72 return;
73 if (NULL != n4)
74 { 69 {
75 if (verbosity > 1) 70 case GNUNET_DNSPARSER_TYPE_A:
76 fprintf (stderr, 71 if (record->data.raw.data_len != sizeof(struct in_addr))
77 "Changing A record from `%s' to `%s'\n", 72 return;
78 inet_ntop (AF_INET, record->data.raw.data, buf, sizeof (buf)), 73 if (NULL != n4)
79 n4); 74 {
80 GNUNET_assert (1 == inet_pton (AF_INET, n4, record->data.raw.data)); 75 if (verbosity > 1)
81 } 76 fprintf(stderr,
82 break; 77 "Changing A record from `%s' to `%s'\n",
83 case GNUNET_DNSPARSER_TYPE_AAAA: 78 inet_ntop(AF_INET, record->data.raw.data, buf, sizeof(buf)),
84 if (record->data.raw.data_len != sizeof (struct in6_addr)) 79 n4);
85 return; 80 GNUNET_assert(1 == inet_pton(AF_INET, n4, record->data.raw.data));
86 if (NULL != n6) 81 }
87 { 82 break;
88 if (verbosity > 1) 83
89 fprintf (stderr, 84 case GNUNET_DNSPARSER_TYPE_AAAA:
90 "Changing AAAA record from `%s' to `%s'\n", 85 if (record->data.raw.data_len != sizeof(struct in6_addr))
91 inet_ntop (AF_INET6, record->data.raw.data, buf, sizeof (buf)), 86 return;
92 n6); 87 if (NULL != n6)
93 GNUNET_assert (1 == inet_pton (AF_INET6, n6, record->data.raw.data)); 88 {
89 if (verbosity > 1)
90 fprintf(stderr,
91 "Changing AAAA record from `%s' to `%s'\n",
92 inet_ntop(AF_INET6, record->data.raw.data, buf, sizeof(buf)),
93 n6);
94 GNUNET_assert(1 == inet_pton(AF_INET6, n6, record->data.raw.data));
95 }
96 break;
97
98 case GNUNET_DNSPARSER_TYPE_NS:
99 case GNUNET_DNSPARSER_TYPE_CNAME:
100 case GNUNET_DNSPARSER_TYPE_PTR:
101 case GNUNET_DNSPARSER_TYPE_SOA:
102 case GNUNET_DNSPARSER_TYPE_MX:
103 case GNUNET_DNSPARSER_TYPE_TXT:
104 break;
105
106 default:
107 break;
94 } 108 }
95 break;
96 case GNUNET_DNSPARSER_TYPE_NS:
97 case GNUNET_DNSPARSER_TYPE_CNAME:
98 case GNUNET_DNSPARSER_TYPE_PTR:
99 case GNUNET_DNSPARSER_TYPE_SOA:
100 case GNUNET_DNSPARSER_TYPE_MX:
101 case GNUNET_DNSPARSER_TYPE_TXT:
102 break;
103 default:
104 break;
105 }
106} 109}
107 110
108 111
@@ -130,10 +133,10 @@ modify_record (const struct GNUNET_DNSPARSER_Record *record)
130 * @param request udp payload of the DNS request 133 * @param request udp payload of the DNS request
131 */ 134 */
132static void 135static void
133modify_request (void *cls, 136modify_request(void *cls,
134 struct GNUNET_DNS_RequestHandle *rh, 137 struct GNUNET_DNS_RequestHandle *rh,
135 size_t request_length, 138 size_t request_length,
136 const char *request) 139 const char *request)
137{ 140{
138 struct GNUNET_DNSPARSER_Packet *p; 141 struct GNUNET_DNSPARSER_Packet *p;
139 unsigned int i; 142 unsigned int i;
@@ -141,35 +144,35 @@ modify_request (void *cls,
141 size_t len; 144 size_t len;
142 int ret; 145 int ret;
143 146
144 p = GNUNET_DNSPARSER_parse (request, request_length); 147 p = GNUNET_DNSPARSER_parse(request, request_length);
145 if (NULL == p) 148 if (NULL == p)
146 { 149 {
147 fprintf (stderr, "Received malformed DNS packet, leaving it untouched\n"); 150 fprintf(stderr, "Received malformed DNS packet, leaving it untouched\n");
148 GNUNET_DNS_request_forward (rh); 151 GNUNET_DNS_request_forward(rh);
149 return; 152 return;
150 } 153 }
151 for (i=0;i<p->num_answers;i++) 154 for (i = 0; i < p->num_answers; i++)
152 modify_record (&p->answers[i]); 155 modify_record(&p->answers[i]);
153 buf = NULL; 156 buf = NULL;
154 ret = GNUNET_DNSPARSER_pack (p, 1024, &buf, &len); 157 ret = GNUNET_DNSPARSER_pack(p, 1024, &buf, &len);
155 GNUNET_DNSPARSER_free_packet (p); 158 GNUNET_DNSPARSER_free_packet(p);
156 if (GNUNET_OK != ret) 159 if (GNUNET_OK != ret)
157 { 160 {
158 if (GNUNET_NO == ret) 161 if (GNUNET_NO == ret)
159 fprintf (stderr, 162 fprintf(stderr,
160 "Modified DNS response did not fit, keeping old response\n"); 163 "Modified DNS response did not fit, keeping old response\n");
161 else 164 else
162 GNUNET_break (0); /* our modifications should have been sane! */ 165 GNUNET_break(0); /* our modifications should have been sane! */
163 GNUNET_DNS_request_forward (rh); 166 GNUNET_DNS_request_forward(rh);
164 } 167 }
165 else 168 else
166 { 169 {
167 if (verbosity > 0) 170 if (verbosity > 0)
168 fprintf (stdout, 171 fprintf(stdout,
169 "Injecting modified DNS response\n"); 172 "Injecting modified DNS response\n");
170 GNUNET_DNS_request_answer (rh, len, buf); 173 GNUNET_DNS_request_answer(rh, len, buf);
171 } 174 }
172 GNUNET_free_non_null (buf); 175 GNUNET_free_non_null(buf);
173} 176}
174 177
175 178
@@ -177,13 +180,13 @@ modify_request (void *cls,
177 * Shutdown. 180 * Shutdown.
178 */ 181 */
179static void 182static void
180do_disconnect (void *cls) 183do_disconnect(void *cls)
181{ 184{
182 if (NULL != handle) 185 if (NULL != handle)
183 { 186 {
184 GNUNET_DNS_disconnect (handle); 187 GNUNET_DNS_disconnect(handle);
185 handle = NULL; 188 handle = NULL;
186 } 189 }
187} 190}
188 191
189 192
@@ -196,66 +199,67 @@ do_disconnect (void *cls)
196 * @param cfg configuration 199 * @param cfg configuration
197 */ 200 */
198static void 201static void
199run (void *cls, char *const *args, const char *cfgfile, 202run(void *cls, char *const *args, const char *cfgfile,
200 const struct GNUNET_CONFIGURATION_Handle *cfg) 203 const struct GNUNET_CONFIGURATION_Handle *cfg)
201{ 204{
202 struct in_addr i4; 205 struct in_addr i4;
203 struct in6_addr i6; 206 struct in6_addr i6;
204 if ( (n4 != NULL) && 207
205 (1 != inet_pton (AF_INET, n4, &i4)) ) 208 if ((n4 != NULL) &&
206 { 209 (1 != inet_pton(AF_INET, n4, &i4)))
207 fprintf (stderr, 210 {
208 "`%s' is nto a valid IPv4 address!\n", 211 fprintf(stderr,
209 n4); 212 "`%s' is nto a valid IPv4 address!\n",
210 return; 213 n4);
211 } 214 return;
212 if ( (n6 != NULL) && 215 }
213 (1 != inet_pton (AF_INET6, n6, &i6)) ) 216 if ((n6 != NULL) &&
214 { 217 (1 != inet_pton(AF_INET6, n6, &i6)))
215 fprintf (stderr, 218 {
216 "`%s' is nto a valid IPv6 address!\n", 219 fprintf(stderr,
217 n6); 220 "`%s' is nto a valid IPv6 address!\n",
218 return; 221 n6);
219 } 222 return;
223 }
220 224
221 handle = 225 handle =
222 GNUNET_DNS_connect (cfg, 226 GNUNET_DNS_connect(cfg,
223 GNUNET_DNS_FLAG_POST_RESOLUTION, 227 GNUNET_DNS_FLAG_POST_RESOLUTION,
224 &modify_request, 228 &modify_request,
225 NULL); 229 NULL);
226 GNUNET_SCHEDULER_add_shutdown (&do_disconnect, NULL); 230 GNUNET_SCHEDULER_add_shutdown(&do_disconnect, NULL);
227} 231}
228 232
229 233
230int 234int
231main (int argc, char *const *argv) 235main(int argc, char *const *argv)
232{ 236{
233 struct GNUNET_GETOPT_CommandLineOption options[] = { 237 struct GNUNET_GETOPT_CommandLineOption options[] = {
234 GNUNET_GETOPT_option_string ('4', 238 GNUNET_GETOPT_option_string('4',
235 "ipv4", 239 "ipv4",
236 "IPV4", 240 "IPV4",
237 gettext_noop ("set A records"), 241 gettext_noop("set A records"),
238 &n4), 242 &n4),
239 243
240 GNUNET_GETOPT_option_string ('6', 244 GNUNET_GETOPT_option_string('6',
241 "ipv4", 245 "ipv4",
242 "IPV6", 246 "IPV6",
243 gettext_noop ("set AAAA records"), 247 gettext_noop("set AAAA records"),
244 &n6), 248 &n6),
245 249
246 GNUNET_GETOPT_option_verbose (&verbosity), 250 GNUNET_GETOPT_option_verbose(&verbosity),
247 GNUNET_GETOPT_OPTION_END 251 GNUNET_GETOPT_OPTION_END
248 }; 252 };
249 253
250 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv)) 254 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args(argc, argv, &argc, &argv))
251 return 2; 255 return 2;
252 256
253 ret = (GNUNET_OK == 257 ret = (GNUNET_OK ==
254 GNUNET_PROGRAM_run (argc, argv, "gnunet-dns-redirector", 258 GNUNET_PROGRAM_run(argc, argv, "gnunet-dns-redirector",
255 gettext_noop 259 gettext_noop
256 ("Change DNS replies to point elsewhere."), options, 260 ("Change DNS replies to point elsewhere."), options,
257 &run, NULL)) ? ret : 1; 261 &run, NULL)) ? ret : 1;
258 GNUNET_free ((void*) argv); 262 GNUNET_free((void*)argv);
259 return ret; 263 return ret;
260} 264}
261 265
diff --git a/src/dns/gnunet-helper-dns.c b/src/dns/gnunet-helper-dns.c
index 554727094..85c72579d 100644
--- a/src/dns/gnunet-helper-dns.c
+++ b/src/dns/gnunet-helper-dns.c
@@ -16,7 +16,7 @@
16 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20 20
21/** 21/**
22 * @file dns/gnunet-helper-dns.c 22 * @file dns/gnunet-helper-dns.c
@@ -87,8 +87,7 @@
87/** 87/**
88 * This is in linux/include/net/ipv6.h, but not always exported... 88 * This is in linux/include/net/ipv6.h, but not always exported...
89 */ 89 */
90struct in6_ifreq 90struct in6_ifreq {
91{
92 struct in6_addr ifr6_addr; 91 struct in6_addr ifr6_addr;
93 uint32_t ifr6_prefixlen; 92 uint32_t ifr6_prefixlen;
94 unsigned int ifr6_ifindex; 93 unsigned int ifr6_ifindex;
@@ -152,11 +151,11 @@ static int cpipe[2];
152 * @param signal signal number of the signal (not used) 151 * @param signal signal number of the signal (not used)
153 */ 152 */
154static void 153static void
155signal_handler (int signal) 154signal_handler(int signal)
156{ 155{
157 /* ignore return value, as the signal handler could theoretically 156 /* ignore return value, as the signal handler could theoretically
158 be called many times before the shutdown can actually happen */ 157 be called many times before the shutdown can actually happen */
159 (void) write (cpipe[1], "K", 1); 158 (void)write(cpipe[1], "K", 1);
160} 159}
161 160
162 161
@@ -168,22 +167,22 @@ signal_handler (int signal)
168 * @param flags open flags (O_RDONLY, O_WRONLY) 167 * @param flags open flags (O_RDONLY, O_WRONLY)
169 */ 168 */
170static void 169static void
171open_dev_null (int target_fd, 170open_dev_null(int target_fd,
172 int flags) 171 int flags)
173{ 172{
174 int fd; 173 int fd;
175 174
176 fd = open ("/dev/null", flags); 175 fd = open("/dev/null", flags);
177 if (-1 == fd) 176 if (-1 == fd)
178 abort (); 177 abort();
179 if (fd == target_fd) 178 if (fd == target_fd)
180 return; 179 return;
181 if (-1 == dup2 (fd, target_fd)) 180 if (-1 == dup2(fd, target_fd))
182 { 181 {
183 (void) close (fd); 182 (void)close(fd);
184 abort (); 183 abort();
185 } 184 }
186 (void) close (fd); 185 (void)close(fd);
187} 186}
188 187
189 188
@@ -195,49 +194,50 @@ open_dev_null (int target_fd,
195 * @return 0 on success, 1 on any error 194 * @return 0 on success, 1 on any error
196 */ 195 */
197static int 196static int
198fork_and_exec (const char *file, 197fork_and_exec(const char *file,
199 char *const cmd[]) 198 char *const cmd[])
200{ 199{
201 int status; 200 int status;
202 pid_t pid; 201 pid_t pid;
203 pid_t ret; 202 pid_t ret;
204 203
205 pid = fork (); 204 pid = fork();
206 if (-1 == pid) 205 if (-1 == pid)
207 { 206 {
208 fprintf (stderr, 207 fprintf(stderr,
209 "fork failed: %s\n", 208 "fork failed: %s\n",
210 strerror (errno)); 209 strerror(errno));
211 return 1; 210 return 1;
212 } 211 }
213 if (0 == pid) 212 if (0 == pid)
214 { 213 {
215 /* we are the child process */ 214 /* we are the child process */
216 /* close stdin/stdout to not cause interference 215 /* close stdin/stdout to not cause interference
217 with the helper's main protocol! */ 216 with the helper's main protocol! */
218 (void) close (0); 217 (void)close(0);
219 open_dev_null (0, O_RDONLY); 218 open_dev_null(0, O_RDONLY);
220 (void) close (1); 219 (void)close(1);
221 open_dev_null (1, O_WRONLY); 220 open_dev_null(1, O_WRONLY);
222 (void) execv (file, cmd); 221 (void)execv(file, cmd);
223 /* can only get here on error */ 222 /* can only get here on error */
224 fprintf (stderr, 223 fprintf(stderr,
225 "exec `%s' failed: %s\n", 224 "exec `%s' failed: %s\n",
226 file, 225 file,
227 strerror (errno)); 226 strerror(errno));
228 _exit (1); 227 _exit(1);
229 } 228 }
230 /* keep running waitpid as long as the only error we get is 'EINTR' */ 229 /* keep running waitpid as long as the only error we get is 'EINTR' */
231 while ( (-1 == (ret = waitpid (pid, &status, 0))) && 230 while ((-1 == (ret = waitpid(pid, &status, 0))) &&
232 (errno == EINTR) ); 231 (errno == EINTR))
232 ;
233 if (-1 == ret) 233 if (-1 == ret)
234 { 234 {
235 fprintf (stderr, 235 fprintf(stderr,
236 "waitpid failed: %s\n", 236 "waitpid failed: %s\n",
237 strerror (errno)); 237 strerror(errno));
238 return 1; 238 return 1;
239 } 239 }
240 if (! (WIFEXITED (status) && (0 == WEXITSTATUS (status)))) 240 if (!(WIFEXITED(status) && (0 == WEXITSTATUS(status))))
241 return 1; 241 return 1;
242 /* child process completed and returned success, we're happy */ 242 /* child process completed and returned success, we're happy */
243 return 0; 243 return 0;
@@ -252,45 +252,45 @@ fork_and_exec (const char *file,
252 * @return the fd to the tun or -1 on error 252 * @return the fd to the tun or -1 on error
253 */ 253 */
254static int 254static int
255init_tun (char *dev) 255init_tun(char *dev)
256{ 256{
257 struct ifreq ifr; 257 struct ifreq ifr;
258 int fd; 258 int fd;
259 259
260 if (NULL == dev) 260 if (NULL == dev)
261 { 261 {
262 errno = EINVAL; 262 errno = EINVAL;
263 return -1; 263 return -1;
264 } 264 }
265 265
266 if (-1 == (fd = open ("/dev/net/tun", O_RDWR))) 266 if (-1 == (fd = open("/dev/net/tun", O_RDWR)))
267 { 267 {
268 fprintf (stderr, "Error opening `%s': %s\n", "/dev/net/tun", 268 fprintf(stderr, "Error opening `%s': %s\n", "/dev/net/tun",
269 strerror (errno)); 269 strerror(errno));
270 return -1; 270 return -1;
271 } 271 }
272 272
273 if (fd >= FD_SETSIZE) 273 if (fd >= FD_SETSIZE)
274 { 274 {
275 fprintf (stderr, "File descriptor to large: %d", fd); 275 fprintf(stderr, "File descriptor to large: %d", fd);
276 (void) close (fd); 276 (void)close(fd);
277 return -1; 277 return -1;
278 } 278 }
279 279
280 memset (&ifr, 0, sizeof (ifr)); 280 memset(&ifr, 0, sizeof(ifr));
281 ifr.ifr_flags = IFF_TUN; 281 ifr.ifr_flags = IFF_TUN;
282 282
283 if ('\0' != *dev) 283 if ('\0' != *dev)
284 strncpy (ifr.ifr_name, dev, IFNAMSIZ); 284 strncpy(ifr.ifr_name, dev, IFNAMSIZ);
285 285
286 if (-1 == ioctl (fd, TUNSETIFF, (void *) &ifr)) 286 if (-1 == ioctl(fd, TUNSETIFF, (void *)&ifr))
287 { 287 {
288 fprintf (stderr, "Error with ioctl on `%s': %s\n", "/dev/net/tun", 288 fprintf(stderr, "Error with ioctl on `%s': %s\n", "/dev/net/tun",
289 strerror (errno)); 289 strerror(errno));
290 (void) close (fd); 290 (void)close(fd);
291 return -1; 291 return -1;
292 } 292 }
293 strcpy (dev, ifr.ifr_name); 293 strcpy(dev, ifr.ifr_name);
294 return fd; 294 return fd;
295} 295}
296 296
@@ -303,7 +303,7 @@ init_tun (char *dev)
303 * @param prefix_len the length of the network-prefix 303 * @param prefix_len the length of the network-prefix
304 */ 304 */
305static void 305static void
306set_address6 (const char *dev, const char *address, unsigned long prefix_len) 306set_address6(const char *dev, const char *address, unsigned long prefix_len)
307{ 307{
308 struct ifreq ifr; 308 struct ifreq ifr;
309 struct in6_ifreq ifr6; 309 struct in6_ifreq ifr6;
@@ -313,39 +313,39 @@ set_address6 (const char *dev, const char *address, unsigned long prefix_len)
313 /* 313 /*
314 * parse the new address 314 * parse the new address
315 */ 315 */
316 memset (&sa6, 0, sizeof (struct sockaddr_in6)); 316 memset(&sa6, 0, sizeof(struct sockaddr_in6));
317 sa6.sin6_family = AF_INET6; 317 sa6.sin6_family = AF_INET6;
318 if (1 != inet_pton (AF_INET6, address, sa6.sin6_addr.s6_addr)) 318 if (1 != inet_pton(AF_INET6, address, sa6.sin6_addr.s6_addr))
319 { 319 {
320 fprintf (stderr, 320 fprintf(stderr,
321 "Failed to parse IPv6 address `%s': %s\n", 321 "Failed to parse IPv6 address `%s': %s\n",
322 address, 322 address,
323 strerror (errno)); 323 strerror(errno));
324 exit (1); 324 exit(1);
325 } 325 }
326 326
327 if (-1 == (fd = socket (PF_INET6, SOCK_DGRAM, 0))) 327 if (-1 == (fd = socket(PF_INET6, SOCK_DGRAM, 0)))
328 { 328 {
329 fprintf (stderr, 329 fprintf(stderr,
330 "Error creating IPv6 socket: %s (ignored)\n", 330 "Error creating IPv6 socket: %s (ignored)\n",
331 strerror (errno)); 331 strerror(errno));
332 /* ignore error, maybe only IPv4 works on this system! */ 332 /* ignore error, maybe only IPv4 works on this system! */
333 return; 333 return;
334 } 334 }
335 335
336 memset (&ifr, 0, sizeof (struct ifreq)); 336 memset(&ifr, 0, sizeof(struct ifreq));
337 /* 337 /*
338 * Get the index of the if 338 * Get the index of the if
339 */ 339 */
340 strncpy (ifr.ifr_name, dev, IFNAMSIZ); 340 strncpy(ifr.ifr_name, dev, IFNAMSIZ);
341 if (-1 == ioctl (fd, SIOGIFINDEX, &ifr)) 341 if (-1 == ioctl(fd, SIOGIFINDEX, &ifr))
342 { 342 {
343 fprintf (stderr, "ioctl failed at %d: %s\n", __LINE__, strerror (errno)); 343 fprintf(stderr, "ioctl failed at %d: %s\n", __LINE__, strerror(errno));
344 (void) close (fd); 344 (void)close(fd);
345 exit (1); 345 exit(1);
346 } 346 }
347 347
348 memset (&ifr6, 0, sizeof (struct in6_ifreq)); 348 memset(&ifr6, 0, sizeof(struct in6_ifreq));
349 ifr6.ifr6_addr = sa6.sin6_addr; 349 ifr6.ifr6_addr = sa6.sin6_addr;
350 ifr6.ifr6_ifindex = ifr.ifr_ifindex; 350 ifr6.ifr6_ifindex = ifr.ifr_ifindex;
351 ifr6.ifr6_prefixlen = prefix_len; 351 ifr6.ifr6_prefixlen = prefix_len;
@@ -353,42 +353,42 @@ set_address6 (const char *dev, const char *address, unsigned long prefix_len)
353 /* 353 /*
354 * Set the address 354 * Set the address
355 */ 355 */
356 if (-1 == ioctl (fd, SIOCSIFADDR, &ifr6)) 356 if (-1 == ioctl(fd, SIOCSIFADDR, &ifr6))
357 { 357 {
358 fprintf (stderr, "ioctl failed at line %d: %s\n", __LINE__, 358 fprintf(stderr, "ioctl failed at line %d: %s\n", __LINE__,
359 strerror (errno)); 359 strerror(errno));
360 (void) close (fd); 360 (void)close(fd);
361 exit (1); 361 exit(1);
362 } 362 }
363 363
364 /* 364 /*
365 * Get the flags 365 * Get the flags
366 */ 366 */
367 if (-1 == ioctl (fd, SIOCGIFFLAGS, &ifr)) 367 if (-1 == ioctl(fd, SIOCGIFFLAGS, &ifr))
368 { 368 {
369 fprintf (stderr, "ioctl failed at line %d: %s\n", __LINE__, 369 fprintf(stderr, "ioctl failed at line %d: %s\n", __LINE__,
370 strerror (errno)); 370 strerror(errno));
371 (void) close (fd); 371 (void)close(fd);
372 exit (1); 372 exit(1);
373 } 373 }
374 374
375 /* 375 /*
376 * Add the UP and RUNNING flags 376 * Add the UP and RUNNING flags
377 */ 377 */
378 ifr.ifr_flags |= IFF_UP | IFF_RUNNING; 378 ifr.ifr_flags |= IFF_UP | IFF_RUNNING;
379 if (-1 == ioctl (fd, SIOCSIFFLAGS, &ifr)) 379 if (-1 == ioctl(fd, SIOCSIFFLAGS, &ifr))
380 { 380 {
381 fprintf (stderr, "ioctl failed at line %d: %s\n", __LINE__, 381 fprintf(stderr, "ioctl failed at line %d: %s\n", __LINE__,
382 strerror (errno)); 382 strerror(errno));
383 (void) close (fd); 383 (void)close(fd);
384 exit (1); 384 exit(1);
385 } 385 }
386 386
387 if (0 != close (fd)) 387 if (0 != close(fd))
388 { 388 {
389 fprintf (stderr, "close failed: %s\n", strerror (errno)); 389 fprintf(stderr, "close failed: %s\n", strerror(errno));
390 exit (1); 390 exit(1);
391 } 391 }
392} 392}
393 393
394 394
@@ -400,100 +400,100 @@ set_address6 (const char *dev, const char *address, unsigned long prefix_len)
400 * @param mask the netmask 400 * @param mask the netmask
401 */ 401 */
402static void 402static void
403set_address4 (const char *dev, const char *address, const char *mask) 403set_address4(const char *dev, const char *address, const char *mask)
404{ 404{
405 int fd; 405 int fd;
406 struct sockaddr_in *addr; 406 struct sockaddr_in *addr;
407 struct ifreq ifr; 407 struct ifreq ifr;
408 408
409 memset (&ifr, 0, sizeof (struct ifreq)); 409 memset(&ifr, 0, sizeof(struct ifreq));
410 addr = (struct sockaddr_in *) &(ifr.ifr_addr); 410 addr = (struct sockaddr_in *)&(ifr.ifr_addr);
411 addr->sin_family = AF_INET; 411 addr->sin_family = AF_INET;
412 412
413 /* 413 /*
414 * Parse the address 414 * Parse the address
415 */ 415 */
416 if (1 != inet_pton (AF_INET, address, &addr->sin_addr.s_addr)) 416 if (1 != inet_pton(AF_INET, address, &addr->sin_addr.s_addr))
417 { 417 {
418 fprintf (stderr, 418 fprintf(stderr,
419 "Failed to parse IPv4 address `%s': %s\n", 419 "Failed to parse IPv4 address `%s': %s\n",
420 address, 420 address,
421 strerror (errno)); 421 strerror(errno));
422 exit (1); 422 exit(1);
423 } 423 }
424 424
425 if (-1 == (fd = socket (PF_INET, SOCK_DGRAM, 0))) 425 if (-1 == (fd = socket(PF_INET, SOCK_DGRAM, 0)))
426 { 426 {
427 fprintf (stderr, 427 fprintf(stderr,
428 "Error creating IPv4 socket: %s\n", 428 "Error creating IPv4 socket: %s\n",
429 strerror (errno)); 429 strerror(errno));
430 exit (1); 430 exit(1);
431 } 431 }
432 432
433 strncpy (ifr.ifr_name, dev, IFNAMSIZ); 433 strncpy(ifr.ifr_name, dev, IFNAMSIZ);
434 434
435 /* 435 /*
436 * Set the address 436 * Set the address
437 */ 437 */
438 if (-1 == ioctl (fd, SIOCSIFADDR, &ifr)) 438 if (-1 == ioctl(fd, SIOCSIFADDR, &ifr))
439 { 439 {
440 fprintf (stderr, "ioctl failed at %d: %s\n", __LINE__, strerror (errno)); 440 fprintf(stderr, "ioctl failed at %d: %s\n", __LINE__, strerror(errno));
441 (void) close (fd); 441 (void)close(fd);
442 exit (1); 442 exit(1);
443 } 443 }
444 444
445 /* 445 /*
446 * Parse the netmask 446 * Parse the netmask
447 */ 447 */
448 addr = (struct sockaddr_in *) &(ifr.ifr_netmask); 448 addr = (struct sockaddr_in *)&(ifr.ifr_netmask);
449 if (1 != inet_pton (AF_INET, mask, &addr->sin_addr.s_addr)) 449 if (1 != inet_pton(AF_INET, mask, &addr->sin_addr.s_addr))
450 { 450 {
451 fprintf (stderr, "Failed to parse address `%s': %s\n", mask, 451 fprintf(stderr, "Failed to parse address `%s': %s\n", mask,
452 strerror (errno)); 452 strerror(errno));
453 (void) close (fd); 453 (void)close(fd);
454 exit (1); 454 exit(1);
455 } 455 }
456 456
457 /* 457 /*
458 * Set the netmask 458 * Set the netmask
459 */ 459 */
460 if (-1 == ioctl (fd, SIOCSIFNETMASK, &ifr)) 460 if (-1 == ioctl(fd, SIOCSIFNETMASK, &ifr))
461 { 461 {
462 fprintf (stderr, "ioctl failed at line %d: %s\n", __LINE__, 462 fprintf(stderr, "ioctl failed at line %d: %s\n", __LINE__,
463 strerror (errno)); 463 strerror(errno));
464 (void) close (fd); 464 (void)close(fd);
465 exit (1); 465 exit(1);
466 } 466 }
467 467
468 /* 468 /*
469 * Get the flags 469 * Get the flags
470 */ 470 */
471 if (-1 == ioctl (fd, SIOCGIFFLAGS, &ifr)) 471 if (-1 == ioctl(fd, SIOCGIFFLAGS, &ifr))
472 { 472 {
473 fprintf (stderr, "ioctl failed at line %d: %s\n", __LINE__, 473 fprintf(stderr, "ioctl failed at line %d: %s\n", __LINE__,
474 strerror (errno)); 474 strerror(errno));
475 (void) close (fd); 475 (void)close(fd);
476 exit (1); 476 exit(1);
477 } 477 }
478 478
479 /* 479 /*
480 * Add the UP and RUNNING flags 480 * Add the UP and RUNNING flags
481 */ 481 */
482 ifr.ifr_flags |= IFF_UP | IFF_RUNNING; 482 ifr.ifr_flags |= IFF_UP | IFF_RUNNING;
483 if (-1 == ioctl (fd, SIOCSIFFLAGS, &ifr)) 483 if (-1 == ioctl(fd, SIOCSIFFLAGS, &ifr))
484 { 484 {
485 fprintf (stderr, "ioctl failed at line %d: %s\n", __LINE__, 485 fprintf(stderr, "ioctl failed at line %d: %s\n", __LINE__,
486 strerror (errno)); 486 strerror(errno));
487 (void) close (fd); 487 (void)close(fd);
488 exit (1); 488 exit(1);
489 } 489 }
490 490
491 if (0 != close (fd)) 491 if (0 != close(fd))
492 { 492 {
493 fprintf (stderr, "close failed: %s\n", strerror (errno)); 493 fprintf(stderr, "close failed: %s\n", strerror(errno));
494 (void) close (fd); 494 (void)close(fd);
495 exit (1); 495 exit(1);
496 } 496 }
497} 497}
498 498
499 499
@@ -505,7 +505,7 @@ set_address4 (const char *dev, const char *address, const char *mask)
505 * @param fd_tun tunnel FD 505 * @param fd_tun tunnel FD
506 */ 506 */
507static void 507static void
508run (int fd_tun) 508run(int fd_tun)
509{ 509{
510 /* 510 /*
511 * The buffer filled by reading from fd_tun 511 * The buffer filled by reading from fd_tun
@@ -526,176 +526,176 @@ run (int fd_tun)
526 int max; 526 int max;
527 527
528 while (1) 528 while (1)
529 {
530 FD_ZERO (&fds_w);
531 FD_ZERO (&fds_r);
532
533 /*
534 * We are supposed to read and the buffer is empty
535 * -> select on read from tun
536 */
537 if (0 == buftun_size)
538 FD_SET (fd_tun, &fds_r);
539
540 /*
541 * We are supposed to read and the buffer is not empty
542 * -> select on write to stdout
543 */
544 if (0 < buftun_size)
545 FD_SET (1, &fds_w);
546
547 /*
548 * We are supposed to write and the buffer is empty
549 * -> select on read from stdin
550 */
551 if (NULL == bufin_read)
552 FD_SET (0, &fds_r);
553
554 /*
555 * We are supposed to write and the buffer is not empty
556 * -> select on write to tun
557 */
558 if (NULL != bufin_read)
559 FD_SET (fd_tun, &fds_w);
560
561 FD_SET (cpipe[0], &fds_r);
562 max = (fd_tun > cpipe[0]) ? fd_tun : cpipe[0];
563
564 int r = select (max + 1, &fds_r, &fds_w, NULL, NULL);
565
566 if (-1 == r)
567 {
568 if (EINTR == errno)
569 continue;
570 fprintf (stderr, "select failed: %s\n", strerror (errno));
571 return;
572 }
573
574 if (r > 0)
575 { 529 {
576 if (FD_ISSET (cpipe[0], &fds_r)) 530 FD_ZERO(&fds_w);
577 return; /* aborted by signal */ 531 FD_ZERO(&fds_r);
578 532
579 if (FD_ISSET (fd_tun, &fds_r)) 533 /*
580 { 534 * We are supposed to read and the buffer is empty
581 buftun_size = 535 * -> select on read from tun
582 read (fd_tun, buftun + sizeof (struct GNUNET_MessageHeader), 536 */
583 MAX_SIZE - sizeof (struct GNUNET_MessageHeader)); 537 if (0 == buftun_size)
584 if (-1 == buftun_size) 538 FD_SET(fd_tun, &fds_r);
539
540 /*
541 * We are supposed to read and the buffer is not empty
542 * -> select on write to stdout
543 */
544 if (0 < buftun_size)
545 FD_SET(1, &fds_w);
546
547 /*
548 * We are supposed to write and the buffer is empty
549 * -> select on read from stdin
550 */
551 if (NULL == bufin_read)
552 FD_SET(0, &fds_r);
553
554 /*
555 * We are supposed to write and the buffer is not empty
556 * -> select on write to tun
557 */
558 if (NULL != bufin_read)
559 FD_SET(fd_tun, &fds_w);
560
561 FD_SET(cpipe[0], &fds_r);
562 max = (fd_tun > cpipe[0]) ? fd_tun : cpipe[0];
563
564 int r = select(max + 1, &fds_r, &fds_w, NULL, NULL);
565
566 if (-1 == r)
585 { 567 {
586 if ( (errno == EINTR) || 568 if (EINTR == errno)
587 (errno == EAGAIN) ) 569 continue;
588 { 570 fprintf(stderr, "select failed: %s\n", strerror(errno));
589 buftun_size = 0;
590 continue;
591 }
592 fprintf (stderr, "read-error: %s\n", strerror (errno));
593 return;
594 }
595 if (0 == buftun_size)
596 {
597 fprintf (stderr, "EOF on tun\n");
598 return;
599 }
600 buftun_read = buftun;
601 {
602 struct GNUNET_MessageHeader *hdr =
603 (struct GNUNET_MessageHeader *) buftun;
604 buftun_size += sizeof (struct GNUNET_MessageHeader);
605 hdr->type = htons (GNUNET_MESSAGE_TYPE_DNS_HELPER);
606 hdr->size = htons (buftun_size);
607 }
608 }
609 else if (FD_ISSET (1, &fds_w))
610 {
611 ssize_t written = write (1, buftun_read, buftun_size);
612
613 if (-1 == written)
614 {
615 if ( (errno == EINTR) ||
616 (errno == EAGAIN) )
617 continue;
618 fprintf (stderr, "write-error to stdout: %s\n", strerror (errno));
619 return;
620 }
621 if (0 == written)
622 {
623 fprintf (stderr, "write returned 0\n");
624 return; 571 return;
625 } 572 }
626 buftun_size -= written;
627 buftun_read += written;
628 }
629 573
630 if (FD_ISSET (0, &fds_r)) 574 if (r > 0)
631 {
632 bufin_size = read (0, bufin + bufin_rpos, MAX_SIZE - bufin_rpos);
633 if (-1 == bufin_size)
634 { 575 {
635 bufin_read = NULL; 576 if (FD_ISSET(cpipe[0], &fds_r))
636 if ( (errno == EINTR) || 577 return; /* aborted by signal */
637 (errno == EAGAIN) ) 578
638 continue; 579 if (FD_ISSET(fd_tun, &fds_r))
639 fprintf (stderr, "read-error: %s\n", strerror (errno)); 580 {
640 return; 581 buftun_size =
641 } 582 read(fd_tun, buftun + sizeof(struct GNUNET_MessageHeader),
642 if (0 == bufin_size) 583 MAX_SIZE - sizeof(struct GNUNET_MessageHeader));
643 { 584 if (-1 == buftun_size)
644 bufin_read = NULL; 585 {
645 fprintf (stderr, "EOF on stdin\n"); 586 if ((errno == EINTR) ||
646 return; 587 (errno == EAGAIN))
647 } 588 {
648 { 589 buftun_size = 0;
649 struct GNUNET_MessageHeader *hdr; 590 continue;
591 }
592 fprintf(stderr, "read-error: %s\n", strerror(errno));
593 return;
594 }
595 if (0 == buftun_size)
596 {
597 fprintf(stderr, "EOF on tun\n");
598 return;
599 }
600 buftun_read = buftun;
601 {
602 struct GNUNET_MessageHeader *hdr =
603 (struct GNUNET_MessageHeader *)buftun;
604 buftun_size += sizeof(struct GNUNET_MessageHeader);
605 hdr->type = htons(GNUNET_MESSAGE_TYPE_DNS_HELPER);
606 hdr->size = htons(buftun_size);
607 }
608 }
609 else if (FD_ISSET(1, &fds_w))
610 {
611 ssize_t written = write(1, buftun_read, buftun_size);
612
613 if (-1 == written)
614 {
615 if ((errno == EINTR) ||
616 (errno == EAGAIN))
617 continue;
618 fprintf(stderr, "write-error to stdout: %s\n", strerror(errno));
619 return;
620 }
621 if (0 == written)
622 {
623 fprintf(stderr, "write returned 0\n");
624 return;
625 }
626 buftun_size -= written;
627 buftun_read += written;
628 }
629
630 if (FD_ISSET(0, &fds_r))
631 {
632 bufin_size = read(0, bufin + bufin_rpos, MAX_SIZE - bufin_rpos);
633 if (-1 == bufin_size)
634 {
635 bufin_read = NULL;
636 if ((errno == EINTR) ||
637 (errno == EAGAIN))
638 continue;
639 fprintf(stderr, "read-error: %s\n", strerror(errno));
640 return;
641 }
642 if (0 == bufin_size)
643 {
644 bufin_read = NULL;
645 fprintf(stderr, "EOF on stdin\n");
646 return;
647 }
648 {
649 struct GNUNET_MessageHeader *hdr;
650 650
651PROCESS_BUFFER: 651PROCESS_BUFFER:
652 bufin_rpos += bufin_size; 652 bufin_rpos += bufin_size;
653 if (bufin_rpos < sizeof (struct GNUNET_MessageHeader)) 653 if (bufin_rpos < sizeof(struct GNUNET_MessageHeader))
654 continue; 654 continue;
655 hdr = (struct GNUNET_MessageHeader *) bufin; 655 hdr = (struct GNUNET_MessageHeader *)bufin;
656 if (ntohs (hdr->type) != GNUNET_MESSAGE_TYPE_DNS_HELPER) 656 if (ntohs(hdr->type) != GNUNET_MESSAGE_TYPE_DNS_HELPER)
657 { 657 {
658 fprintf (stderr, "protocol violation!\n"); 658 fprintf(stderr, "protocol violation!\n");
659 return; 659 return;
660 } 660 }
661 if (ntohs (hdr->size) > bufin_rpos) 661 if (ntohs(hdr->size) > bufin_rpos)
662 continue; 662 continue;
663 bufin_read = bufin + sizeof (struct GNUNET_MessageHeader); 663 bufin_read = bufin + sizeof(struct GNUNET_MessageHeader);
664 bufin_size = ntohs (hdr->size) - sizeof (struct GNUNET_MessageHeader); 664 bufin_size = ntohs(hdr->size) - sizeof(struct GNUNET_MessageHeader);
665 bufin_rpos -= bufin_size + sizeof (struct GNUNET_MessageHeader); 665 bufin_rpos -= bufin_size + sizeof(struct GNUNET_MessageHeader);
666 } 666 }
667 } 667 }
668 else if (FD_ISSET (fd_tun, &fds_w)) 668 else if (FD_ISSET(fd_tun, &fds_w))
669 { 669 {
670 ssize_t written = write (fd_tun, bufin_read, bufin_size); 670 ssize_t written = write(fd_tun, bufin_read, bufin_size);
671 671
672 if (-1 == written) 672 if (-1 == written)
673 { 673 {
674 if ( (errno == EINTR) || 674 if ((errno == EINTR) ||
675 (errno == EAGAIN) ) 675 (errno == EAGAIN))
676 continue; 676 continue;
677 fprintf (stderr, "write-error to tun: %s\n", strerror (errno)); 677 fprintf(stderr, "write-error to tun: %s\n", strerror(errno));
678 return; 678 return;
679 } 679 }
680 if (0 == written) 680 if (0 == written)
681 { 681 {
682 fprintf (stderr, "write returned 0\n"); 682 fprintf(stderr, "write returned 0\n");
683 return; 683 return;
684 }
685 {
686 bufin_size -= written;
687 bufin_read += written;
688 if (0 == bufin_size)
689 {
690 memmove(bufin, bufin_read, bufin_rpos);
691 bufin_read = NULL; /* start reading again */
692 bufin_size = 0;
693 goto PROCESS_BUFFER;
694 }
695 }
696 }
684 } 697 }
685 {
686 bufin_size -= written;
687 bufin_read += written;
688 if (0 == bufin_size)
689 {
690 memmove (bufin, bufin_read, bufin_rpos);
691 bufin_read = NULL; /* start reading again */
692 bufin_size = 0;
693 goto PROCESS_BUFFER;
694 }
695 }
696 }
697 } 698 }
698 }
699} 699}
700 700
701 701
@@ -732,7 +732,7 @@ PROCESS_BUFFER:
732 * 255 failed to handle kill signal properly 732 * 255 failed to handle kill signal properly
733 */ 733 */
734int 734int
735main (int argc, char *const*argv) 735main(int argc, char *const*argv)
736{ 736{
737 int r; 737 int r;
738 char dev[IFNAMSIZ]; 738 char dev[IFNAMSIZ];
@@ -742,224 +742,224 @@ main (int argc, char *const*argv)
742 int nortsetup = 0; 742 int nortsetup = 0;
743 743
744 if (7 != argc) 744 if (7 != argc)
745 { 745 {
746 fprintf (stderr, "Fatal: must supply 6 arguments!\n"); 746 fprintf(stderr, "Fatal: must supply 6 arguments!\n");
747 return 1; 747 return 1;
748 } 748 }
749 749
750 /* assert privs so we can modify the firewall rules! */ 750 /* assert privs so we can modify the firewall rules! */
751 uid = getuid (); 751 uid = getuid();
752#ifdef HAVE_SETRESUID 752#ifdef HAVE_SETRESUID
753 if (0 != setresuid (uid, 0, 0)) 753 if (0 != setresuid(uid, 0, 0))
754 { 754 {
755 fprintf (stderr, "Failed to setresuid to root: %s\n", strerror (errno)); 755 fprintf(stderr, "Failed to setresuid to root: %s\n", strerror(errno));
756 return 254; 756 return 254;
757 } 757 }
758#else 758#else
759 if (0 != seteuid (0)) 759 if (0 != seteuid(0))
760 { 760 {
761 fprintf (stderr, "Failed to seteuid back to root: %s\n", strerror (errno)); 761 fprintf(stderr, "Failed to seteuid back to root: %s\n", strerror(errno));
762 return 254; 762 return 254;
763 } 763 }
764#endif 764#endif
765 if (0 == strncmp (argv[6], "1", 2)) 765 if (0 == strncmp(argv[6], "1", 2))
766 nortsetup = 1; 766 nortsetup = 1;
767 767
768 if (0 == nortsetup) 768 if (0 == nortsetup)
769 { 769 {
770 /* verify that the binaries we care about are executable */ 770 /* verify that the binaries we care about are executable */
771#ifdef IPTABLES 771#ifdef IPTABLES
772 if (0 == access (IPTABLES, X_OK)) 772 if (0 == access(IPTABLES, X_OK))
773 sbin_iptables = IPTABLES; 773 sbin_iptables = IPTABLES;
774 else 774 else
775#endif 775#endif
776 if (0 == access ("/sbin/iptables", X_OK)) 776 if (0 == access("/sbin/iptables", X_OK))
777 sbin_iptables = "/sbin/iptables"; 777 sbin_iptables = "/sbin/iptables";
778 else if (0 == access ("/usr/sbin/iptables", X_OK)) 778 else if (0 == access("/usr/sbin/iptables", X_OK))
779 sbin_iptables = "/usr/sbin/iptables"; 779 sbin_iptables = "/usr/sbin/iptables";
780 else 780 else
781 { 781 {
782 fprintf (stderr, 782 fprintf(stderr,
783 "Fatal: executable iptables not found in approved directories: %s\n", 783 "Fatal: executable iptables not found in approved directories: %s\n",
784 strerror (errno)); 784 strerror(errno));
785 return 3; 785 return 3;
786 } 786 }
787#ifdef IP6TABLES 787#ifdef IP6TABLES
788 if (0 == access (IP6TABLES, X_OK)) 788 if (0 == access(IP6TABLES, X_OK))
789 sbin_ip6tables = IP6TABLES; 789 sbin_ip6tables = IP6TABLES;
790 else 790 else
791#endif 791#endif
792 if (0 == access ("/sbin/ip6tables", X_OK)) 792 if (0 == access("/sbin/ip6tables", X_OK))
793 sbin_ip6tables = "/sbin/ip6tables"; 793 sbin_ip6tables = "/sbin/ip6tables";
794 else if (0 == access ("/usr/sbin/ip6tables", X_OK)) 794 else if (0 == access("/usr/sbin/ip6tables", X_OK))
795 sbin_ip6tables = "/usr/sbin/ip6tables"; 795 sbin_ip6tables = "/usr/sbin/ip6tables";
796 else 796 else
797 { 797 {
798 fprintf (stderr, 798 fprintf(stderr,
799 "Fatal: executable ip6tables not found in approved directories: %s\n", 799 "Fatal: executable ip6tables not found in approved directories: %s\n",
800 strerror (errno)); 800 strerror(errno));
801 return 3; 801 return 3;
802 } 802 }
803#ifdef PATH_TO_IP 803#ifdef PATH_TO_IP
804 if (0 == access (PATH_TO_IP, X_OK)) 804 if (0 == access(PATH_TO_IP, X_OK))
805 sbin_ip = PATH_TO_IP; 805 sbin_ip = PATH_TO_IP;
806 else 806 else
807#endif 807#endif
808 if (0 == access ("/sbin/ip", X_OK)) 808 if (0 == access("/sbin/ip", X_OK))
809 sbin_ip = "/sbin/ip"; 809 sbin_ip = "/sbin/ip";
810 else if (0 == access ("/usr/sbin/ip", X_OK)) 810 else if (0 == access("/usr/sbin/ip", X_OK))
811 sbin_ip = "/usr/sbin/ip"; 811 sbin_ip = "/usr/sbin/ip";
812 else if (0 == access ("/bin/ip", X_OK)) /* gentoo has it there */ 812 else if (0 == access("/bin/ip", X_OK)) /* gentoo has it there */
813 sbin_ip = "/bin/ip"; 813 sbin_ip = "/bin/ip";
814 else 814 else
815 { 815 {
816 fprintf (stderr, 816 fprintf(stderr,
817 "Fatal: executable ip not found in approved directories: %s\n", 817 "Fatal: executable ip not found in approved directories: %s\n",
818 strerror (errno)); 818 strerror(errno));
819 return 4; 819 return 4;
820 } 820 }
821#ifdef SYSCTL 821#ifdef SYSCTL
822 if (0 == access (SYSCTL, X_OK)) 822 if (0 == access(SYSCTL, X_OK))
823 sbin_sysctl = SYSCTL; 823 sbin_sysctl = SYSCTL;
824 else 824 else
825#endif 825#endif
826 if (0 == access ("/sbin/sysctl", X_OK)) 826 if (0 == access("/sbin/sysctl", X_OK))
827 sbin_sysctl = "/sbin/sysctl"; 827 sbin_sysctl = "/sbin/sysctl";
828 else if (0 == access ("/usr/sbin/sysctl", X_OK)) 828 else if (0 == access("/usr/sbin/sysctl", X_OK))
829 sbin_sysctl = "/usr/sbin/sysctl"; 829 sbin_sysctl = "/usr/sbin/sysctl";
830 else 830 else
831 { 831 {
832 fprintf (stderr, 832 fprintf(stderr,
833 "Fatal: executable sysctl not found in approved directories: %s\n", 833 "Fatal: executable sysctl not found in approved directories: %s\n",
834 strerror (errno)); 834 strerror(errno));
835 return 5; 835 return 5;
836 }
836 } 837 }
837 }
838 838
839 /* setup 'mygid' string */ 839 /* setup 'mygid' string */
840 snprintf (mygid, sizeof (mygid), "%d", (int) getegid()); 840 snprintf(mygid, sizeof(mygid), "%d", (int)getegid());
841 841
842 /* do not die on SIGPIPE */ 842 /* do not die on SIGPIPE */
843 if (SIG_ERR == signal (SIGPIPE, SIG_IGN)) 843 if (SIG_ERR == signal(SIGPIPE, SIG_IGN))
844 { 844 {
845 fprintf (stderr, "Failed to protect against SIGPIPE: %s\n", 845 fprintf(stderr, "Failed to protect against SIGPIPE: %s\n",
846 strerror (errno)); 846 strerror(errno));
847 return 7; 847 return 7;
848 } 848 }
849 849
850 /* setup pipe to shutdown nicely on SIGINT */ 850 /* setup pipe to shutdown nicely on SIGINT */
851 if (0 != pipe (cpipe)) 851 if (0 != pipe(cpipe))
852 {
853 fprintf (stderr,
854 "Fatal: could not setup control pipe: %s\n",
855 strerror (errno));
856 return 6;
857 }
858 if (cpipe[0] >= FD_SETSIZE)
859 {
860 fprintf (stderr, "Pipe file descriptor to large: %d", cpipe[0]);
861 (void) close (cpipe[0]);
862 (void) close (cpipe[1]);
863 return 6;
864 }
865 {
866 /* make pipe non-blocking, as we theoretically could otherwise block
867 in the signal handler */
868 int flags = fcntl (cpipe[1], F_GETFL);
869 if (-1 == flags)
870 { 852 {
871 fprintf (stderr, "Failed to read flags for pipe: %s", strerror (errno)); 853 fprintf(stderr,
872 (void) close (cpipe[0]); 854 "Fatal: could not setup control pipe: %s\n",
873 (void) close (cpipe[1]); 855 strerror(errno));
874 return 6; 856 return 6;
875 } 857 }
876 flags |= O_NONBLOCK; 858 if (cpipe[0] >= FD_SETSIZE)
877 if (0 != fcntl (cpipe[1], F_SETFL, flags))
878 { 859 {
879 fprintf (stderr, "Failed to make pipe non-blocking: %s", strerror (errno)); 860 fprintf(stderr, "Pipe file descriptor to large: %d", cpipe[0]);
880 (void) close (cpipe[0]); 861 (void)close(cpipe[0]);
881 (void) close (cpipe[1]); 862 (void)close(cpipe[1]);
882 return 6; 863 return 6;
883 } 864 }
865 {
866 /* make pipe non-blocking, as we theoretically could otherwise block
867 in the signal handler */
868 int flags = fcntl(cpipe[1], F_GETFL);
869 if (-1 == flags)
870 {
871 fprintf(stderr, "Failed to read flags for pipe: %s", strerror(errno));
872 (void)close(cpipe[0]);
873 (void)close(cpipe[1]);
874 return 6;
875 }
876 flags |= O_NONBLOCK;
877 if (0 != fcntl(cpipe[1], F_SETFL, flags))
878 {
879 fprintf(stderr, "Failed to make pipe non-blocking: %s", strerror(errno));
880 (void)close(cpipe[0]);
881 (void)close(cpipe[1]);
882 return 6;
883 }
884 } 884 }
885 if ( (SIG_ERR == signal (SIGTERM, &signal_handler)) || 885 if ((SIG_ERR == signal(SIGTERM, &signal_handler)) ||
886#if (SIGTERM != GNUNET_TERM_SIG) 886#if (SIGTERM != GNUNET_TERM_SIG)
887 (SIG_ERR == signal (GNUNET_TERM_SIG, &signal_handler)) || 887 (SIG_ERR == signal(GNUNET_TERM_SIG, &signal_handler)) ||
888#endif 888#endif
889 (SIG_ERR == signal (SIGINT, &signal_handler)) || 889 (SIG_ERR == signal(SIGINT, &signal_handler)) ||
890 (SIG_ERR == signal (SIGHUP, &signal_handler)) ) 890 (SIG_ERR == signal(SIGHUP, &signal_handler)))
891 { 891 {
892 fprintf (stderr, 892 fprintf(stderr,
893 "Fatal: could not initialize signal handler: %s\n", 893 "Fatal: could not initialize signal handler: %s\n",
894 strerror (errno)); 894 strerror(errno));
895 (void) close (cpipe[0]); 895 (void)close(cpipe[0]);
896 (void) close (cpipe[1]); 896 (void)close(cpipe[1]);
897 return 7; 897 return 7;
898 } 898 }
899 899
900 900
901 /* get interface name */ 901 /* get interface name */
902 strncpy (dev, argv[1], IFNAMSIZ); 902 strncpy(dev, argv[1], IFNAMSIZ);
903 dev[IFNAMSIZ - 1] = '\0'; 903 dev[IFNAMSIZ - 1] = '\0';
904 904
905 /* Disable rp filtering */ 905 /* Disable rp filtering */
906 if (0 == nortsetup) 906 if (0 == nortsetup)
907 {
908 char *const sysctl_args[] = {"sysctl", "-w",
909 "net.ipv4.conf.all.rp_filter=0", NULL};
910 char *const sysctl_args2[] = {"sysctl", "-w",
911 "net.ipv4.conf.default.rp_filter=0", NULL};
912 if ((0 != fork_and_exec (sbin_sysctl, sysctl_args)) ||
913 (0 != fork_and_exec (sbin_sysctl, sysctl_args2)))
914 { 907 {
915 fprintf (stderr, 908 char *const sysctl_args[] = { "sysctl", "-w",
916 "Failed to disable rp filtering.\n"); 909 "net.ipv4.conf.all.rp_filter=0", NULL };
917 return 5; 910 char *const sysctl_args2[] = { "sysctl", "-w",
911 "net.ipv4.conf.default.rp_filter=0", NULL };
912 if ((0 != fork_and_exec(sbin_sysctl, sysctl_args)) ||
913 (0 != fork_and_exec(sbin_sysctl, sysctl_args2)))
914 {
915 fprintf(stderr,
916 "Failed to disable rp filtering.\n");
917 return 5;
918 }
918 } 919 }
919 }
920 920
921 921
922 /* now open virtual interface (first part that requires root) */ 922 /* now open virtual interface (first part that requires root) */
923 if (-1 == (fd_tun = init_tun (dev))) 923 if (-1 == (fd_tun = init_tun(dev)))
924 { 924 {
925 fprintf (stderr, "Fatal: could not initialize tun-interface\n"); 925 fprintf(stderr, "Fatal: could not initialize tun-interface\n");
926 (void) signal (SIGTERM, SIG_IGN); 926 (void)signal(SIGTERM, SIG_IGN);
927#if (SIGTERM != GNUNET_TERM_SIG) 927#if (SIGTERM != GNUNET_TERM_SIG)
928 (void) signal (GNUNET_TERM_SIG, SIG_IGN); 928 (void)signal(GNUNET_TERM_SIG, SIG_IGN);
929#endif 929#endif
930 (void) signal (SIGINT, SIG_IGN); 930 (void)signal(SIGINT, SIG_IGN);
931 (void) signal (SIGHUP, SIG_IGN); 931 (void)signal(SIGHUP, SIG_IGN);
932 (void) close (cpipe[0]); 932 (void)close(cpipe[0]);
933 (void) close (cpipe[1]); 933 (void)close(cpipe[1]);
934 return 5; 934 return 5;
935 } 935 }
936 936
937 /* now set interface addresses */ 937 /* now set interface addresses */
938 { 938 {
939 const char *address = argv[2]; 939 const char *address = argv[2];
940 long prefix_len = atol (argv[3]); 940 long prefix_len = atol(argv[3]);
941 941
942 if ((prefix_len < 1) || (prefix_len > 127)) 942 if ((prefix_len < 1) || (prefix_len > 127))
943 { 943 {
944 fprintf (stderr, "Fatal: prefix_len out of range\n"); 944 fprintf(stderr, "Fatal: prefix_len out of range\n");
945 (void) signal (SIGTERM, SIG_IGN); 945 (void)signal(SIGTERM, SIG_IGN);
946#if (SIGTERM != GNUNET_TERM_SIG) 946#if (SIGTERM != GNUNET_TERM_SIG)
947 (void) signal (GNUNET_TERM_SIG, SIG_IGN); 947 (void)signal(GNUNET_TERM_SIG, SIG_IGN);
948#endif 948#endif
949 (void) signal (SIGINT, SIG_IGN); 949 (void)signal(SIGINT, SIG_IGN);
950 (void) signal (SIGHUP, SIG_IGN); 950 (void)signal(SIGHUP, SIG_IGN);
951 (void) close (cpipe[0]); 951 (void)close(cpipe[0]);
952 (void) close (cpipe[1]); 952 (void)close(cpipe[1]);
953 return 2; 953 return 2;
954 } 954 }
955 set_address6 (dev, address, prefix_len); 955 set_address6(dev, address, prefix_len);
956 } 956 }
957 957
958 { 958 {
959 const char *address = argv[4]; 959 const char *address = argv[4];
960 const char *mask = argv[5]; 960 const char *mask = argv[5];
961 961
962 set_address4 (dev, address, mask); 962 set_address4(dev, address, mask);
963 } 963 }
964 964
965 965
@@ -968,237 +968,237 @@ main (int argc, char *const*argv)
968 by the 'gnunet-service-dns') and with destination 968 by the 'gnunet-service-dns') and with destination
969 to port 53 on UDP, without hijacking */ 969 to port 53 on UDP, without hijacking */
970 if (0 == nortsetup) 970 if (0 == nortsetup)
971 {
972 r = 8; /* failed to fully setup routing table */
973 { 971 {
974 char *const mangle_args[] = 972 r = 8; /* failed to fully setup routing table */
973 {
974 char *const mangle_args[] =
975 { 975 {
976 "iptables", "-m", "owner", "-t", "mangle", "-I", "OUTPUT", "1", "-p", 976 "iptables", "-m", "owner", "-t", "mangle", "-I", "OUTPUT", "1", "-p",
977 "udp", "--gid-owner", mygid, "--dport", DNS_PORT, "-j", 977 "udp", "--gid-owner", mygid, "--dport", DNS_PORT, "-j",
978 "ACCEPT", NULL 978 "ACCEPT", NULL
979 }; 979 };
980 if (0 != fork_and_exec (sbin_iptables, mangle_args)) 980 if (0 != fork_and_exec(sbin_iptables, mangle_args))
981 goto cleanup_rest; 981 goto cleanup_rest;
982 } 982 }
983 { 983 {
984 char *const mangle_args[] = 984 char *const mangle_args[] =
985 { 985 {
986 "ip6tables", "-m", "owner", "-t", "mangle", "-I", "OUTPUT", "1", "-p", 986 "ip6tables", "-m", "owner", "-t", "mangle", "-I", "OUTPUT", "1", "-p",
987 "udp", "--gid-owner", mygid, "--dport", DNS_PORT, "-j", 987 "udp", "--gid-owner", mygid, "--dport", DNS_PORT, "-j",
988 "ACCEPT", NULL 988 "ACCEPT", NULL
989 }; 989 };
990 if (0 != fork_and_exec (sbin_ip6tables, mangle_args)) 990 if (0 != fork_and_exec(sbin_ip6tables, mangle_args))
991 goto cleanup_mangle_1b; 991 goto cleanup_mangle_1b;
992 } 992 }
993 /* Mark all of the other DNS traffic using our mark DNS_MARK, 993 /* Mark all of the other DNS traffic using our mark DNS_MARK,
994 unless it is on a link-local IPv6 address, which we cannot support. */ 994 unless it is on a link-local IPv6 address, which we cannot support. */
995 { 995 {
996 char *const mark_args[] = 996 char *const mark_args[] =
997 { 997 {
998 "iptables", "-t", "mangle", "-I", "OUTPUT", "2", "-p", 998 "iptables", "-t", "mangle", "-I", "OUTPUT", "2", "-p",
999 "udp", "--dport", DNS_PORT, 999 "udp", "--dport", DNS_PORT,
1000 "-j", "MARK", "--set-mark", DNS_MARK, 1000 "-j", "MARK", "--set-mark", DNS_MARK,
1001 NULL 1001 NULL
1002 }; 1002 };
1003 if (0 != fork_and_exec (sbin_iptables, mark_args)) 1003 if (0 != fork_and_exec(sbin_iptables, mark_args))
1004 goto cleanup_mangle_1; 1004 goto cleanup_mangle_1;
1005 } 1005 }
1006 { 1006 {
1007 char *const mark_args[] = 1007 char *const mark_args[] =
1008 { 1008 {
1009 "ip6tables", "-t", "mangle", "-I", "OUTPUT", "2", "-p", 1009 "ip6tables", "-t", "mangle", "-I", "OUTPUT", "2", "-p",
1010 "udp", "--dport", DNS_PORT, 1010 "udp", "--dport", DNS_PORT,
1011 "!", "-s", "fe80::/10", /* this line excludes link-local traffic */ 1011 "!", "-s", "fe80::/10", /* this line excludes link-local traffic */
1012 "-j", "MARK", "--set-mark", DNS_MARK, 1012 "-j", "MARK", "--set-mark", DNS_MARK,
1013 NULL 1013 NULL
1014 }; 1014 };
1015 if (0 != fork_and_exec (sbin_ip6tables, mark_args)) 1015 if (0 != fork_and_exec(sbin_ip6tables, mark_args))
1016 goto cleanup_mark_2b; 1016 goto cleanup_mark_2b;
1017 } 1017 }
1018 /* Forward all marked DNS traffic to our DNS_TABLE */ 1018 /* Forward all marked DNS traffic to our DNS_TABLE */
1019 { 1019 {
1020 char *const forward_args[] = 1020 char *const forward_args[] =
1021 { 1021 {
1022 "ip", "rule", "add", "fwmark", DNS_MARK, "table", DNS_TABLE, NULL 1022 "ip", "rule", "add", "fwmark", DNS_MARK, "table", DNS_TABLE, NULL
1023 }; 1023 };
1024 if (0 != fork_and_exec (sbin_ip, forward_args)) 1024 if (0 != fork_and_exec(sbin_ip, forward_args))
1025 goto cleanup_mark_2; 1025 goto cleanup_mark_2;
1026 } 1026 }
1027 { 1027 {
1028 char *const forward_args[] = 1028 char *const forward_args[] =
1029 { 1029 {
1030 "ip", "-6", "rule", "add", "fwmark", DNS_MARK, "table", DNS_TABLE, NULL 1030 "ip", "-6", "rule", "add", "fwmark", DNS_MARK, "table", DNS_TABLE, NULL
1031 }; 1031 };
1032 if (0 != fork_and_exec (sbin_ip, forward_args)) 1032 if (0 != fork_and_exec(sbin_ip, forward_args))
1033 goto cleanup_forward_3b; 1033 goto cleanup_forward_3b;
1034 } 1034 }
1035 /* Finally, add rule in our forwarding table to pass to our virtual interface */ 1035 /* Finally, add rule in our forwarding table to pass to our virtual interface */
1036 { 1036 {
1037 char *const route_args[] = 1037 char *const route_args[] =
1038 { 1038 {
1039 "ip", "route", "add", "default", "dev", dev, 1039 "ip", "route", "add", "default", "dev", dev,
1040 "table", DNS_TABLE, NULL 1040 "table", DNS_TABLE, NULL
1041 }; 1041 };
1042 if (0 != fork_and_exec (sbin_ip, route_args)) 1042 if (0 != fork_and_exec(sbin_ip, route_args))
1043 goto cleanup_forward_3; 1043 goto cleanup_forward_3;
1044 } 1044 }
1045 { 1045 {
1046 char *const route_args[] = 1046 char *const route_args[] =
1047 { 1047 {
1048 "ip", "-6", "route", "add", "default", "dev", dev, 1048 "ip", "-6", "route", "add", "default", "dev", dev,
1049 "table", DNS_TABLE, NULL 1049 "table", DNS_TABLE, NULL
1050 }; 1050 };
1051 if (0 != fork_and_exec (sbin_ip, route_args)) 1051 if (0 != fork_and_exec(sbin_ip, route_args))
1052 goto cleanup_route_4b; 1052 goto cleanup_route_4b;
1053 }
1053 } 1054 }
1054 }
1055 1055
1056 /* drop privs *except* for the saved UID; this is not perfect, but better 1056 /* drop privs *except* for the saved UID; this is not perfect, but better
1057 than doing nothing */ 1057 than doing nothing */
1058#ifdef HAVE_SETRESUID 1058#ifdef HAVE_SETRESUID
1059 if (0 != setresuid (uid, uid, 0)) 1059 if (0 != setresuid(uid, uid, 0))
1060 { 1060 {
1061 fprintf (stderr, "Failed to setresuid: %s\n", strerror (errno)); 1061 fprintf(stderr, "Failed to setresuid: %s\n", strerror(errno));
1062 r = 24; 1062 r = 24;
1063 goto cleanup_route_4; 1063 goto cleanup_route_4;
1064 } 1064 }
1065#else 1065#else
1066 /* Note: no 'setuid' here as we must keep our saved UID as root */ 1066 /* Note: no 'setuid' here as we must keep our saved UID as root */
1067 if (0 != seteuid (uid)) 1067 if (0 != seteuid(uid))
1068 { 1068 {
1069 fprintf (stderr, "Failed to seteuid: %s\n", strerror (errno)); 1069 fprintf(stderr, "Failed to seteuid: %s\n", strerror(errno));
1070 r = 24; 1070 r = 24;
1071 goto cleanup_route_4; 1071 goto cleanup_route_4;
1072 } 1072 }
1073#endif 1073#endif
1074 1074
1075 r = 0; /* did fully setup routing table (if nothing else happens, we were successful!) */ 1075 r = 0; /* did fully setup routing table (if nothing else happens, we were successful!) */
1076 1076
1077 /* now forward until we hit a problem */ 1077 /* now forward until we hit a problem */
1078 run (fd_tun); 1078 run(fd_tun);
1079 1079
1080 /* now need to regain privs so we can remove the firewall rules we added! */ 1080 /* now need to regain privs so we can remove the firewall rules we added! */
1081#ifdef HAVE_SETRESUID 1081#ifdef HAVE_SETRESUID
1082 if (0 != setresuid (uid, 0, 0)) 1082 if (0 != setresuid(uid, 0, 0))
1083 { 1083 {
1084 fprintf (stderr, "Failed to setresuid back to root: %s\n", strerror (errno)); 1084 fprintf(stderr, "Failed to setresuid back to root: %s\n", strerror(errno));
1085 r = 40; 1085 r = 40;
1086 goto cleanup_route_4; 1086 goto cleanup_route_4;
1087 } 1087 }
1088#else 1088#else
1089 if (0 != seteuid (0)) 1089 if (0 != seteuid(0))
1090 { 1090 {
1091 fprintf (stderr, "Failed to seteuid back to root: %s\n", strerror (errno)); 1091 fprintf(stderr, "Failed to seteuid back to root: %s\n", strerror(errno));
1092 r = 40; 1092 r = 40;
1093 goto cleanup_route_4; 1093 goto cleanup_route_4;
1094 } 1094 }
1095#endif 1095#endif
1096 1096
1097 /* update routing tables again -- this is why we could not fully drop privs */ 1097 /* update routing tables again -- this is why we could not fully drop privs */
1098 /* now undo updating of routing tables; normal exit or clean-up-on-error case */ 1098 /* now undo updating of routing tables; normal exit or clean-up-on-error case */
1099 cleanup_route_4: 1099cleanup_route_4:
1100 if (0 == nortsetup) 1100 if (0 == nortsetup)
1101 { 1101 {
1102 char *const route_clean_args[] = 1102 char *const route_clean_args[] =
1103 { 1103 {
1104 "ip", "-6", "route", "del", "default", "dev", dev, 1104 "ip", "-6", "route", "del", "default", "dev", dev,
1105 "table", DNS_TABLE, NULL 1105 "table", DNS_TABLE, NULL
1106 }; 1106 };
1107 if (0 != fork_and_exec (sbin_ip, route_clean_args)) 1107 if (0 != fork_and_exec(sbin_ip, route_clean_args))
1108 r += 1; 1108 r += 1;
1109 } 1109 }
1110 cleanup_route_4b: 1110cleanup_route_4b:
1111 if (0 == nortsetup) 1111 if (0 == nortsetup)
1112 { 1112 {
1113 char *const route_clean_args[] = 1113 char *const route_clean_args[] =
1114 { 1114 {
1115 "ip", "route", "del", "default", "dev", dev, 1115 "ip", "route", "del", "default", "dev", dev,
1116 "table", DNS_TABLE, NULL 1116 "table", DNS_TABLE, NULL
1117 }; 1117 };
1118 if (0 != fork_and_exec (sbin_ip, route_clean_args)) 1118 if (0 != fork_and_exec(sbin_ip, route_clean_args))
1119 r += 1; 1119 r += 1;
1120 } 1120 }
1121 cleanup_forward_3: 1121cleanup_forward_3:
1122 if (0 == nortsetup) 1122 if (0 == nortsetup)
1123 { 1123 {
1124 char *const forward_clean_args[] = 1124 char *const forward_clean_args[] =
1125 { 1125 {
1126 "ip", "-6", "rule", "del", "fwmark", DNS_MARK, "table", DNS_TABLE, NULL 1126 "ip", "-6", "rule", "del", "fwmark", DNS_MARK, "table", DNS_TABLE, NULL
1127 }; 1127 };
1128 if (0 != fork_and_exec (sbin_ip, forward_clean_args)) 1128 if (0 != fork_and_exec(sbin_ip, forward_clean_args))
1129 r += 2; 1129 r += 2;
1130 } 1130 }
1131 cleanup_forward_3b: 1131cleanup_forward_3b:
1132 if (0 == nortsetup) 1132 if (0 == nortsetup)
1133 { 1133 {
1134 char *const forward_clean_args[] = 1134 char *const forward_clean_args[] =
1135 { 1135 {
1136 "ip", "rule", "del", "fwmark", DNS_MARK, "table", DNS_TABLE, NULL 1136 "ip", "rule", "del", "fwmark", DNS_MARK, "table", DNS_TABLE, NULL
1137 }; 1137 };
1138 if (0 != fork_and_exec (sbin_ip, forward_clean_args)) 1138 if (0 != fork_and_exec(sbin_ip, forward_clean_args))
1139 r += 2; 1139 r += 2;
1140 } 1140 }
1141 cleanup_mark_2: 1141cleanup_mark_2:
1142 if (0 == nortsetup) 1142 if (0 == nortsetup)
1143 { 1143 {
1144 char *const mark_clean_args[] = 1144 char *const mark_clean_args[] =
1145 { 1145 {
1146 "ip6tables", "-t", "mangle", "-D", "OUTPUT", "-p", "udp", 1146 "ip6tables", "-t", "mangle", "-D", "OUTPUT", "-p", "udp",
1147 "--dport", DNS_PORT, 1147 "--dport", DNS_PORT,
1148 "!", "-s", "fe80::/10", /* this line excludes link-local traffic */ 1148 "!", "-s", "fe80::/10", /* this line excludes link-local traffic */
1149 "-j", "MARK", "--set-mark", DNS_MARK, NULL 1149 "-j", "MARK", "--set-mark", DNS_MARK, NULL
1150 }; 1150 };
1151 if (0 != fork_and_exec (sbin_ip6tables, mark_clean_args)) 1151 if (0 != fork_and_exec(sbin_ip6tables, mark_clean_args))
1152 r += 4; 1152 r += 4;
1153 } 1153 }
1154 cleanup_mark_2b: 1154cleanup_mark_2b:
1155 if (0 == nortsetup) 1155 if (0 == nortsetup)
1156 { 1156 {
1157 char *const mark_clean_args[] = 1157 char *const mark_clean_args[] =
1158 { 1158 {
1159 "iptables", "-t", "mangle", "-D", "OUTPUT", "-p", "udp", 1159 "iptables", "-t", "mangle", "-D", "OUTPUT", "-p", "udp",
1160 "--dport", DNS_PORT, "-j", "MARK", "--set-mark", DNS_MARK, NULL 1160 "--dport", DNS_PORT, "-j", "MARK", "--set-mark", DNS_MARK, NULL
1161 }; 1161 };
1162 if (0 != fork_and_exec (sbin_iptables, mark_clean_args)) 1162 if (0 != fork_and_exec(sbin_iptables, mark_clean_args))
1163 r += 4; 1163 r += 4;
1164 } 1164 }
1165 cleanup_mangle_1: 1165cleanup_mangle_1:
1166 if (0 == nortsetup) 1166 if (0 == nortsetup)
1167 { 1167 {
1168 char *const mangle_clean_args[] = 1168 char *const mangle_clean_args[] =
1169 { 1169 {
1170 "ip6tables", "-m", "owner", "-t", "mangle", "-D", "OUTPUT", "-p", "udp", 1170 "ip6tables", "-m", "owner", "-t", "mangle", "-D", "OUTPUT", "-p", "udp",
1171 "--gid-owner", mygid, "--dport", DNS_PORT, "-j", "ACCEPT", 1171 "--gid-owner", mygid, "--dport", DNS_PORT, "-j", "ACCEPT",
1172 NULL 1172 NULL
1173 }; 1173 };
1174 if (0 != fork_and_exec (sbin_ip6tables, mangle_clean_args)) 1174 if (0 != fork_and_exec(sbin_ip6tables, mangle_clean_args))
1175 r += 8; 1175 r += 8;
1176 } 1176 }
1177 cleanup_mangle_1b: 1177cleanup_mangle_1b:
1178 if (0 == nortsetup) 1178 if (0 == nortsetup)
1179 { 1179 {
1180 char *const mangle_clean_args[] = 1180 char *const mangle_clean_args[] =
1181 { 1181 {
1182 "iptables", "-m", "owner", "-t", "mangle", "-D", "OUTPUT", "-p", "udp", 1182 "iptables", "-m", "owner", "-t", "mangle", "-D", "OUTPUT", "-p", "udp",
1183 "--gid-owner", mygid, "--dport", DNS_PORT, "-j", "ACCEPT", 1183 "--gid-owner", mygid, "--dport", DNS_PORT, "-j", "ACCEPT",
1184 NULL 1184 NULL
1185 }; 1185 };
1186 if (0 != fork_and_exec (sbin_iptables, mangle_clean_args)) 1186 if (0 != fork_and_exec(sbin_iptables, mangle_clean_args))
1187 r += 8; 1187 r += 8;
1188 } 1188 }
1189 1189
1190 cleanup_rest: 1190cleanup_rest:
1191 /* close virtual interface */ 1191 /* close virtual interface */
1192 (void) close (fd_tun); 1192 (void)close(fd_tun);
1193 /* remove signal handler so we can close the pipes */ 1193 /* remove signal handler so we can close the pipes */
1194 (void) signal (SIGTERM, SIG_IGN); 1194 (void)signal(SIGTERM, SIG_IGN);
1195#if (SIGTERM != GNUNET_TERM_SIG) 1195#if (SIGTERM != GNUNET_TERM_SIG)
1196 (void) signal (GNUNET_TERM_SIG, SIG_IGN); 1196 (void)signal(GNUNET_TERM_SIG, SIG_IGN);
1197#endif 1197#endif
1198 (void) signal (SIGINT, SIG_IGN); 1198 (void)signal(SIGINT, SIG_IGN);
1199 (void) signal (SIGHUP, SIG_IGN); 1199 (void)signal(SIGHUP, SIG_IGN);
1200 (void) close (cpipe[0]); 1200 (void)close(cpipe[0]);
1201 (void) close (cpipe[1]); 1201 (void)close(cpipe[1]);
1202 return r; 1202 return r;
1203} 1203}
1204 1204
diff --git a/src/dns/gnunet-service-dns.c b/src/dns/gnunet-service-dns.c
index 9c4f1f490..9a2293b2d 100644
--- a/src/dns/gnunet-service-dns.c
+++ b/src/dns/gnunet-service-dns.c
@@ -11,12 +11,12 @@
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20 20
21/** 21/**
22 * @file dns/gnunet-service-dns.c 22 * @file dns/gnunet-service-dns.c
@@ -59,14 +59,13 @@
59 * Generic logging shorthand 59 * Generic logging shorthand
60 */ 60 */
61#define LOG(kind, ...) \ 61#define LOG(kind, ...) \
62 GNUNET_log_from (kind, "dns", __VA_ARGS__); 62 GNUNET_log_from(kind, "dns", __VA_ARGS__);
63 63
64 64
65/** 65/**
66 * Phases each request goes through. 66 * Phases each request goes through.
67 */ 67 */
68enum RequestPhase 68enum RequestPhase {
69{
70 /** 69 /**
71 * Request has just been received. 70 * Request has just been received.
72 */ 71 */
@@ -112,16 +111,15 @@ enum RequestPhase
112/** 111/**
113 * Entry we keep for each client. 112 * Entry we keep for each client.
114 */ 113 */
115struct ClientRecord 114struct ClientRecord {
116{
117 /** 115 /**
118 * Kept in doubly-linked list. 116 * Kept in doubly-linked list.
119 */ 117 */
120 struct ClientRecord *next; 118 struct ClientRecord *next;
121 119
122 /** 120 /**
123 * Kept in doubly-linked list. 121 * Kept in doubly-linked list.
124 */ 122 */
125 struct ClientRecord *prev; 123 struct ClientRecord *prev;
126 124
127 /** 125 /**
@@ -138,16 +136,13 @@ struct ClientRecord
138 * Flags for the client. 136 * Flags for the client.
139 */ 137 */
140 enum GNUNET_DNS_Flags flags; 138 enum GNUNET_DNS_Flags flags;
141
142}; 139};
143 140
144 141
145/** 142/**
146 * Entry we keep for each active request. 143 * Entry we keep for each active request.
147 */ 144 */
148struct RequestRecord 145struct RequestRecord {
149{
150
151 /** 146 /**
152 * List of clients that still need to see this request (each entry 147 * List of clients that still need to see this request (each entry
153 * is set to NULL when the client is done). 148 * is set to NULL when the client is done).
@@ -197,7 +192,6 @@ struct RequestRecord
197 * In which phase this this request? 192 * In which phase this this request?
198 */ 193 */
199 enum RequestPhase phase; 194 enum RequestPhase phase;
200
201}; 195};
202 196
203 197
@@ -258,14 +252,14 @@ static struct GNUNET_DNSSTUB_Context *dnsstub;
258 * @param rr request to clean up 252 * @param rr request to clean up
259 */ 253 */
260static void 254static void
261cleanup_rr (struct RequestRecord *rr) 255cleanup_rr(struct RequestRecord *rr)
262{ 256{
263 GNUNET_free_non_null (rr->payload); 257 GNUNET_free_non_null(rr->payload);
264 rr->payload = NULL; 258 rr->payload = NULL;
265 rr->payload_length = 0; 259 rr->payload_length = 0;
266 GNUNET_array_grow (rr->client_wait_list, 260 GNUNET_array_grow(rr->client_wait_list,
267 rr->client_wait_list_length, 261 rr->client_wait_list_length,
268 0); 262 0);
269} 263}
270 264
271 265
@@ -275,28 +269,28 @@ cleanup_rr (struct RequestRecord *rr)
275 * @param cls unused 269 * @param cls unused
276 */ 270 */
277static void 271static void
278cleanup_task (void *cls GNUNET_UNUSED) 272cleanup_task(void *cls GNUNET_UNUSED)
279{ 273{
280 if (NULL != hijacker) 274 if (NULL != hijacker)
281 { 275 {
282 GNUNET_HELPER_stop (hijacker, GNUNET_NO); 276 GNUNET_HELPER_stop(hijacker, GNUNET_NO);
283 hijacker = NULL; 277 hijacker = NULL;
284 } 278 }
285 for (unsigned int i=0;i<8;i++) 279 for (unsigned int i = 0; i < 8; i++)
286 GNUNET_free_non_null (helper_argv[i]); 280 GNUNET_free_non_null(helper_argv[i]);
287 for (unsigned int i=0;i<=UINT16_MAX;i++) 281 for (unsigned int i = 0; i <= UINT16_MAX; i++)
288 cleanup_rr (&requests[i]); 282 cleanup_rr(&requests[i]);
289 if (NULL != stats) 283 if (NULL != stats)
290 { 284 {
291 GNUNET_STATISTICS_destroy (stats, 285 GNUNET_STATISTICS_destroy(stats,
292 GNUNET_NO); 286 GNUNET_NO);
293 stats = NULL; 287 stats = NULL;
294 } 288 }
295 if (NULL != dnsstub) 289 if (NULL != dnsstub)
296 { 290 {
297 GNUNET_DNSSTUB_stop (dnsstub); 291 GNUNET_DNSSTUB_stop(dnsstub);
298 dnsstub = NULL; 292 dnsstub = NULL;
299 } 293 }
300} 294}
301 295
302 296
@@ -306,54 +300,56 @@ cleanup_task (void *cls GNUNET_UNUSED)
306 * @param rr request send to the network or just clean up. 300 * @param rr request send to the network or just clean up.
307 */ 301 */
308static void 302static void
309request_done (struct RequestRecord *rr) 303request_done(struct RequestRecord *rr)
310{ 304{
311 struct GNUNET_MessageHeader *hdr; 305 struct GNUNET_MessageHeader *hdr;
312 size_t reply_len; 306 size_t reply_len;
313 uint16_t source_port; 307 uint16_t source_port;
314 uint16_t destination_port; 308 uint16_t destination_port;
315 309
316 GNUNET_array_grow (rr->client_wait_list, 310 GNUNET_array_grow(rr->client_wait_list,
317 rr->client_wait_list_length, 311 rr->client_wait_list_length,
318 0); 312 0);
319 if (RP_RESPONSE_MONITOR != rr->phase) 313 if (RP_RESPONSE_MONITOR != rr->phase)
320 { 314 {
321 /* no response, drop */ 315 /* no response, drop */
322 LOG (GNUNET_ERROR_TYPE_DEBUG, 316 LOG(GNUNET_ERROR_TYPE_DEBUG,
323 "Got no response for request %llu, dropping\n", 317 "Got no response for request %llu, dropping\n",
324 (unsigned long long) rr->request_id); 318 (unsigned long long)rr->request_id);
325 cleanup_rr (rr); 319 cleanup_rr(rr);
326 return; 320 return;
327 } 321 }
328 322
329 LOG (GNUNET_ERROR_TYPE_DEBUG, 323 LOG(GNUNET_ERROR_TYPE_DEBUG,
330 "Transmitting response for request %llu\n", 324 "Transmitting response for request %llu\n",
331 (unsigned long long) rr->request_id); 325 (unsigned long long)rr->request_id);
332 /* send response via hijacker */ 326 /* send response via hijacker */
333 reply_len = sizeof (struct GNUNET_MessageHeader); 327 reply_len = sizeof(struct GNUNET_MessageHeader);
334 reply_len += sizeof (struct GNUNET_TUN_Layer2PacketHeader); 328 reply_len += sizeof(struct GNUNET_TUN_Layer2PacketHeader);
335 switch (rr->src_addr.ss_family) 329 switch (rr->src_addr.ss_family)
336 { 330 {
337 case AF_INET: 331 case AF_INET:
338 reply_len += sizeof (struct GNUNET_TUN_IPv4Header); 332 reply_len += sizeof(struct GNUNET_TUN_IPv4Header);
339 break; 333 break;
340 case AF_INET6: 334
341 reply_len += sizeof (struct GNUNET_TUN_IPv6Header); 335 case AF_INET6:
342 break; 336 reply_len += sizeof(struct GNUNET_TUN_IPv6Header);
343 default: 337 break;
344 GNUNET_break (0); 338
345 cleanup_rr (rr); 339 default:
346 return; 340 GNUNET_break(0);
347 } 341 cleanup_rr(rr);
348 reply_len += sizeof (struct GNUNET_TUN_UdpHeader); 342 return;
343 }
344 reply_len += sizeof(struct GNUNET_TUN_UdpHeader);
349 reply_len += rr->payload_length; 345 reply_len += rr->payload_length;
350 if (reply_len >= GNUNET_MAX_MESSAGE_SIZE) 346 if (reply_len >= GNUNET_MAX_MESSAGE_SIZE)
351 { 347 {
352 /* response too big, drop */ 348 /* response too big, drop */
353 GNUNET_break (0); /* how can this be? */ 349 GNUNET_break(0); /* how can this be? */
354 cleanup_rr(rr); 350 cleanup_rr(rr);
355 return; 351 return;
356 } 352 }
357 { 353 {
358 char buf[reply_len] GNUNET_ALIGN; 354 char buf[reply_len] GNUNET_ALIGN;
359 size_t off; 355 size_t off;
@@ -361,68 +357,70 @@ request_done (struct RequestRecord *rr)
361 struct GNUNET_TUN_IPv6Header ip6; 357 struct GNUNET_TUN_IPv6Header ip6;
362 358
363 /* first, GNUnet message header */ 359 /* first, GNUnet message header */
364 hdr = (struct GNUNET_MessageHeader*) buf; 360 hdr = (struct GNUNET_MessageHeader*)buf;
365 hdr->type = htons (GNUNET_MESSAGE_TYPE_DNS_HELPER); 361 hdr->type = htons(GNUNET_MESSAGE_TYPE_DNS_HELPER);
366 hdr->size = htons ((uint16_t) reply_len); 362 hdr->size = htons((uint16_t)reply_len);
367 off = sizeof (struct GNUNET_MessageHeader); 363 off = sizeof(struct GNUNET_MessageHeader);
368 364
369 /* first, TUN header */ 365 /* first, TUN header */
370 { 366 {
371 struct GNUNET_TUN_Layer2PacketHeader tun; 367 struct GNUNET_TUN_Layer2PacketHeader tun;
372 368
373 tun.flags = htons (0); 369 tun.flags = htons(0);
374 if (rr->src_addr.ss_family == AF_INET) 370 if (rr->src_addr.ss_family == AF_INET)
375 tun.proto = htons (ETH_P_IPV4); 371 tun.proto = htons(ETH_P_IPV4);
376 else 372 else
377 tun.proto = htons (ETH_P_IPV6); 373 tun.proto = htons(ETH_P_IPV6);
378 GNUNET_memcpy (&buf[off], 374 GNUNET_memcpy(&buf[off],
379 &tun, 375 &tun,
380 sizeof (struct GNUNET_TUN_Layer2PacketHeader)); 376 sizeof(struct GNUNET_TUN_Layer2PacketHeader));
381 off += sizeof (struct GNUNET_TUN_Layer2PacketHeader); 377 off += sizeof(struct GNUNET_TUN_Layer2PacketHeader);
382 } 378 }
383 379
384 /* now IP header */ 380 /* now IP header */
385 switch (rr->src_addr.ss_family) 381 switch (rr->src_addr.ss_family)
386 {
387 case AF_INET:
388 { 382 {
389 struct sockaddr_in *src = (struct sockaddr_in *) &rr->src_addr; 383 case AF_INET:
390 struct sockaddr_in *dst = (struct sockaddr_in *) &rr->dst_addr; 384 {
391 385 struct sockaddr_in *src = (struct sockaddr_in *)&rr->src_addr;
392 source_port = dst->sin_port; 386 struct sockaddr_in *dst = (struct sockaddr_in *)&rr->dst_addr;
393 destination_port = src->sin_port; 387
394 GNUNET_TUN_initialize_ipv4_header (&ip4, 388 source_port = dst->sin_port;
395 IPPROTO_UDP, 389 destination_port = src->sin_port;
396 reply_len - off - sizeof (struct GNUNET_TUN_IPv4Header), 390 GNUNET_TUN_initialize_ipv4_header(&ip4,
397 &dst->sin_addr, 391 IPPROTO_UDP,
398 &src->sin_addr); 392 reply_len - off - sizeof(struct GNUNET_TUN_IPv4Header),
399 GNUNET_memcpy (&buf[off], 393 &dst->sin_addr,
400 &ip4, 394 &src->sin_addr);
401 sizeof (ip4)); 395 GNUNET_memcpy(&buf[off],
402 off += sizeof (ip4); 396 &ip4,
397 sizeof(ip4));
398 off += sizeof(ip4);
403 } 399 }
404 break; 400 break;
405 case AF_INET6: 401
402 case AF_INET6:
406 { 403 {
407 struct sockaddr_in6 *src = (struct sockaddr_in6 *) &rr->src_addr; 404 struct sockaddr_in6 *src = (struct sockaddr_in6 *)&rr->src_addr;
408 struct sockaddr_in6 *dst = (struct sockaddr_in6 *) &rr->dst_addr; 405 struct sockaddr_in6 *dst = (struct sockaddr_in6 *)&rr->dst_addr;
409 406
410 source_port = dst->sin6_port; 407 source_port = dst->sin6_port;
411 destination_port = src->sin6_port; 408 destination_port = src->sin6_port;
412 GNUNET_TUN_initialize_ipv6_header (&ip6, 409 GNUNET_TUN_initialize_ipv6_header(&ip6,
413 IPPROTO_UDP, 410 IPPROTO_UDP,
414 reply_len - off - sizeof (struct GNUNET_TUN_IPv6Header), 411 reply_len - off - sizeof(struct GNUNET_TUN_IPv6Header),
415 &dst->sin6_addr, 412 &dst->sin6_addr,
416 &src->sin6_addr); 413 &src->sin6_addr);
417 GNUNET_memcpy (&buf[off], 414 GNUNET_memcpy(&buf[off],
418 &ip6, 415 &ip6,
419 sizeof (ip6)); 416 sizeof(ip6));
420 off += sizeof (ip6); 417 off += sizeof(ip6);
421 } 418 }
422 break; 419 break;
423 default: 420
424 GNUNET_assert (0); 421 default:
425 } 422 GNUNET_assert(0);
423 }
426 424
427 /* now UDP header */ 425 /* now UDP header */
428 { 426 {
@@ -430,40 +428,40 @@ request_done (struct RequestRecord *rr)
430 428
431 udp.source_port = source_port; 429 udp.source_port = source_port;
432 udp.destination_port = destination_port; 430 udp.destination_port = destination_port;
433 udp.len = htons (reply_len - off); 431 udp.len = htons(reply_len - off);
434 if (AF_INET == rr->src_addr.ss_family) 432 if (AF_INET == rr->src_addr.ss_family)
435 GNUNET_TUN_calculate_udp4_checksum (&ip4, 433 GNUNET_TUN_calculate_udp4_checksum(&ip4,
436 &udp, 434 &udp,
437 rr->payload, 435 rr->payload,
438 rr->payload_length); 436 rr->payload_length);
439 else 437 else
440 GNUNET_TUN_calculate_udp6_checksum (&ip6, 438 GNUNET_TUN_calculate_udp6_checksum(&ip6,
441 &udp, 439 &udp,
442 rr->payload, 440 rr->payload,
443 rr->payload_length); 441 rr->payload_length);
444 GNUNET_memcpy (&buf[off], 442 GNUNET_memcpy(&buf[off],
445 &udp, 443 &udp,
446 sizeof (udp)); 444 sizeof(udp));
447 off += sizeof (udp); 445 off += sizeof(udp);
448 } 446 }
449 447
450 /* now DNS payload */ 448 /* now DNS payload */
451 { 449 {
452 GNUNET_memcpy (&buf[off], rr->payload, rr->payload_length); 450 GNUNET_memcpy(&buf[off], rr->payload, rr->payload_length);
453 off += rr->payload_length; 451 off += rr->payload_length;
454 } 452 }
455 /* final checks & sending */ 453 /* final checks & sending */
456 GNUNET_assert (off == reply_len); 454 GNUNET_assert(off == reply_len);
457 (void) GNUNET_HELPER_send (hijacker, 455 (void)GNUNET_HELPER_send(hijacker,
458 hdr, 456 hdr,
459 GNUNET_YES, 457 GNUNET_YES,
460 NULL, NULL); 458 NULL, NULL);
461 GNUNET_STATISTICS_update (stats, 459 GNUNET_STATISTICS_update(stats,
462 gettext_noop ("# DNS requests answered via TUN interface"), 460 gettext_noop("# DNS requests answered via TUN interface"),
463 1, GNUNET_NO); 461 1, GNUNET_NO);
464 } 462 }
465 /* clean up, we're done */ 463 /* clean up, we're done */
466 cleanup_rr (rr); 464 cleanup_rr(rr);
467} 465}
468 466
469 467
@@ -475,31 +473,31 @@ request_done (struct RequestRecord *rr)
475 * @param cr client to send the response to 473 * @param cr client to send the response to
476 */ 474 */
477static void 475static void
478send_request_to_client (struct RequestRecord *rr, 476send_request_to_client(struct RequestRecord *rr,
479 struct ClientRecord *cr) 477 struct ClientRecord *cr)
480{ 478{
481 struct GNUNET_MQ_Envelope *env; 479 struct GNUNET_MQ_Envelope *env;
482 struct GNUNET_DNS_Request *req; 480 struct GNUNET_DNS_Request *req;
483 481
484 if (sizeof (struct GNUNET_DNS_Request) + rr->payload_length >= GNUNET_MAX_MESSAGE_SIZE) 482 if (sizeof(struct GNUNET_DNS_Request) + rr->payload_length >= GNUNET_MAX_MESSAGE_SIZE)
485 { 483 {
486 GNUNET_break (0); 484 GNUNET_break(0);
487 cleanup_rr (rr); 485 cleanup_rr(rr);
488 return; 486 return;
489 } 487 }
490 LOG (GNUNET_ERROR_TYPE_DEBUG, 488 LOG(GNUNET_ERROR_TYPE_DEBUG,
491 "Sending information about request %llu to local client\n", 489 "Sending information about request %llu to local client\n",
492 (unsigned long long) rr->request_id); 490 (unsigned long long)rr->request_id);
493 env = GNUNET_MQ_msg_extra (req, 491 env = GNUNET_MQ_msg_extra(req,
494 rr->payload_length, 492 rr->payload_length,
495 GNUNET_MESSAGE_TYPE_DNS_CLIENT_REQUEST); 493 GNUNET_MESSAGE_TYPE_DNS_CLIENT_REQUEST);
496 req->reserved = htonl (0); 494 req->reserved = htonl(0);
497 req->request_id = rr->request_id; 495 req->request_id = rr->request_id;
498 GNUNET_memcpy (&req[1], 496 GNUNET_memcpy(&req[1],
499 rr->payload, 497 rr->payload,
500 rr->payload_length); 498 rr->payload_length);
501 GNUNET_MQ_send (cr->mq, 499 GNUNET_MQ_send(cr->mq,
502 env); 500 env);
503} 501}
504 502
505 503
@@ -512,9 +510,9 @@ send_request_to_client (struct RequestRecord *rr,
512 * @param r number of bytes in dns 510 * @param r number of bytes in dns
513 */ 511 */
514static void 512static void
515process_dns_result (void *cls, 513process_dns_result(void *cls,
516 const struct GNUNET_TUN_DnsHeader *dns, 514 const struct GNUNET_TUN_DnsHeader *dns,
517 size_t r); 515 size_t r);
518 516
519 517
520/** 518/**
@@ -524,129 +522,138 @@ process_dns_result (void *cls,
524 * @param rr request to process further 522 * @param rr request to process further
525 */ 523 */
526static void 524static void
527next_phase (struct RequestRecord *rr) 525next_phase(struct RequestRecord *rr)
528{ 526{
529 struct ClientRecord *cr; 527 struct ClientRecord *cr;
530 int nz; 528 int nz;
531 529
532 if (rr->phase == RP_DROP) 530 if (rr->phase == RP_DROP)
533 { 531 {
534 cleanup_rr (rr); 532 cleanup_rr(rr);
535 return; 533 return;
536 } 534 }
537 nz = -1; 535 nz = -1;
538 for (unsigned int j=0;j<rr->client_wait_list_length;j++) 536 for (unsigned int j = 0; j < rr->client_wait_list_length; j++)
539 {
540 if (NULL != rr->client_wait_list[j])
541 { 537 {
542 nz = (int) j; 538 if (NULL != rr->client_wait_list[j])
543 break; 539 {
540 nz = (int)j;
541 break;
542 }
544 } 543 }
545 }
546 if (-1 != nz) 544 if (-1 != nz)
547 {
548 send_request_to_client (rr,
549 rr->client_wait_list[nz]);
550 return;
551 }
552 /* done with current phase, advance! */
553 LOG (GNUNET_ERROR_TYPE_DEBUG,
554 "Request %llu now in phase %d\n",
555 rr->request_id,
556 rr->phase);
557 switch (rr->phase)
558 {
559 case RP_INIT:
560 rr->phase = RP_REQUEST_MONITOR;
561 for (cr = clients_head; NULL != cr; cr = cr->next)
562 { 545 {
563 if (0 != (cr->flags & GNUNET_DNS_FLAG_REQUEST_MONITOR)) 546 send_request_to_client(rr,
564 GNUNET_array_append (rr->client_wait_list, 547 rr->client_wait_list[nz]);
565 rr->client_wait_list_length, 548 return;
566 cr);
567 } 549 }
568 next_phase (rr); 550 /* done with current phase, advance! */
569 return; 551 LOG(GNUNET_ERROR_TYPE_DEBUG,
570 case RP_REQUEST_MONITOR: 552 "Request %llu now in phase %d\n",
571 rr->phase = RP_QUERY; 553 rr->request_id,
572 for (cr = clients_head; NULL != cr; cr = cr->next) 554 rr->phase);
555 switch (rr->phase)
573 { 556 {
574 if (0 != (cr->flags & GNUNET_DNS_FLAG_PRE_RESOLUTION)) 557 case RP_INIT:
575 GNUNET_array_append (rr->client_wait_list, 558 rr->phase = RP_REQUEST_MONITOR;
576 rr->client_wait_list_length, 559 for (cr = clients_head; NULL != cr; cr = cr->next)
577 cr); 560 {
578 } 561 if (0 != (cr->flags & GNUNET_DNS_FLAG_REQUEST_MONITOR))
579 next_phase (rr); 562 GNUNET_array_append(rr->client_wait_list,
580 return; 563 rr->client_wait_list_length,
581 case RP_QUERY: 564 cr);
565 }
566 next_phase(rr);
567 return;
568
569 case RP_REQUEST_MONITOR:
570 rr->phase = RP_QUERY;
571 for (cr = clients_head; NULL != cr; cr = cr->next)
572 {
573 if (0 != (cr->flags & GNUNET_DNS_FLAG_PRE_RESOLUTION))
574 GNUNET_array_append(rr->client_wait_list,
575 rr->client_wait_list_length,
576 cr);
577 }
578 next_phase(rr);
579 return;
580
581 case RP_QUERY:
582#if 0 582#if 0
583 /* TODO: optionally, use this to forward DNS requests to the 583 /* TODO: optionally, use this to forward DNS requests to the
584 *original* DNS server instead of the one we have configured... 584 * original* DNS server instead of the one we have configured...
585 (but then we need to create a fresh dnsstub for each request 585 (but then we need to create a fresh dnsstub for each request
586 *and* manage the timeout) */ 586 * and* manage the timeout) */
587 switch (rr->dst_addr.ss_family) 587 switch (rr->dst_addr.ss_family)
588 { 588 {
589 case AF_INET: 589 case AF_INET:
590 salen = sizeof (struct sockaddr_in); 590 salen = sizeof(struct sockaddr_in);
591 sa = (const struct sockaddr *) &rr->dst_addr; 591 sa = (const struct sockaddr *)&rr->dst_addr;
592 break;
593
594 case AF_INET6:
595 salen = sizeof(struct sockaddr_in6);
596 sa = (const struct sockaddr *)&rr->dst_addr;
597 break;
598
599 default:
600 GNUNET_assert(0);
601 }
602#endif
603 rr->phase = RP_INTERNET_DNS;
604 rr->rs = GNUNET_DNSSTUB_resolve(dnsstub,
605 rr->payload,
606 rr->payload_length,
607 &process_dns_result,
608 NULL);
609 if (NULL == rr->rs)
610 {
611 GNUNET_STATISTICS_update(stats,
612 gettext_noop("# DNS exit failed (failed to open socket)"),
613 1,
614 GNUNET_NO);
615 cleanup_rr(rr);
616 return;
617 }
618 return;
619
620 case RP_INTERNET_DNS:
621 rr->phase = RP_MODIFY;
622 for (cr = clients_head; NULL != cr; cr = cr->next)
623 {
624 if (0 != (cr->flags & GNUNET_DNS_FLAG_POST_RESOLUTION))
625 GNUNET_array_append(rr->client_wait_list,
626 rr->client_wait_list_length,
627 cr);
628 }
629 next_phase(rr);
630 return;
631
632 case RP_MODIFY:
633 rr->phase = RP_RESPONSE_MONITOR;
634 for (cr = clients_head; NULL != cr; cr = cr->next)
635 {
636 if (0 != (cr->flags & GNUNET_DNS_FLAG_RESPONSE_MONITOR))
637 GNUNET_array_append(rr->client_wait_list,
638 rr->client_wait_list_length,
639 cr);
640 }
641 next_phase(rr);
642 return;
643
644 case RP_RESPONSE_MONITOR:
645 request_done(rr);
592 break; 646 break;
593 case AF_INET6: 647
594 salen = sizeof (struct sockaddr_in6); 648 case RP_DROP:
595 sa = (const struct sockaddr *) &rr->dst_addr; 649 cleanup_rr(rr);
596 break; 650 break;
651
597 default: 652 default:
598 GNUNET_assert (0); 653 GNUNET_break(0);
599 } 654 cleanup_rr(rr);
600#endif 655 break;
601 rr->phase = RP_INTERNET_DNS;
602 rr->rs = GNUNET_DNSSTUB_resolve (dnsstub,
603 rr->payload,
604 rr->payload_length,
605 &process_dns_result,
606 NULL);
607 if (NULL == rr->rs)
608 {
609 GNUNET_STATISTICS_update (stats,
610 gettext_noop ("# DNS exit failed (failed to open socket)"),
611 1,
612 GNUNET_NO);
613 cleanup_rr (rr);
614 return;
615 }
616 return;
617 case RP_INTERNET_DNS:
618 rr->phase = RP_MODIFY;
619 for (cr = clients_head; NULL != cr; cr = cr->next)
620 {
621 if (0 != (cr->flags & GNUNET_DNS_FLAG_POST_RESOLUTION))
622 GNUNET_array_append (rr->client_wait_list,
623 rr->client_wait_list_length,
624 cr);
625 }
626 next_phase (rr);
627 return;
628 case RP_MODIFY:
629 rr->phase = RP_RESPONSE_MONITOR;
630 for (cr = clients_head; NULL != cr; cr = cr->next)
631 {
632 if (0 != (cr->flags & GNUNET_DNS_FLAG_RESPONSE_MONITOR))
633 GNUNET_array_append (rr->client_wait_list,
634 rr->client_wait_list_length,
635 cr);
636 } 656 }
637 next_phase (rr);
638 return;
639 case RP_RESPONSE_MONITOR:
640 request_done (rr);
641 break;
642 case RP_DROP:
643 cleanup_rr (rr);
644 break;
645 default:
646 GNUNET_break (0);
647 cleanup_rr (rr);
648 break;
649 }
650} 657}
651 658
652 659
@@ -659,18 +666,18 @@ next_phase (struct RequestRecord *rr)
659 * @return our `struct ClientRecord` 666 * @return our `struct ClientRecord`
660 */ 667 */
661static void * 668static void *
662client_connect_cb (void *cls, 669client_connect_cb(void *cls,
663 struct GNUNET_SERVICE_Client *client, 670 struct GNUNET_SERVICE_Client *client,
664 struct GNUNET_MQ_Handle *mq) 671 struct GNUNET_MQ_Handle *mq)
665{ 672{
666 struct ClientRecord *cr = cls; 673 struct ClientRecord *cr = cls;
667 674
668 cr = GNUNET_new (struct ClientRecord); 675 cr = GNUNET_new(struct ClientRecord);
669 cr->client = client; 676 cr->client = client;
670 cr->mq = mq; 677 cr->mq = mq;
671 GNUNET_CONTAINER_DLL_insert (clients_head, 678 GNUNET_CONTAINER_DLL_insert(clients_head,
672 clients_tail, 679 clients_tail,
673 cr); 680 cr);
674 return cr; 681 return cr;
675} 682}
676 683
@@ -683,31 +690,31 @@ client_connect_cb (void *cls,
683 * @param app_ctx our `struct ClientRecord` 690 * @param app_ctx our `struct ClientRecord`
684 */ 691 */
685static void 692static void
686client_disconnect_cb (void *cls, 693client_disconnect_cb(void *cls,
687 struct GNUNET_SERVICE_Client *client, 694 struct GNUNET_SERVICE_Client *client,
688 void *app_ctx) 695 void *app_ctx)
689{ 696{
690 struct ClientRecord *cr = app_ctx; 697 struct ClientRecord *cr = app_ctx;
691 struct RequestRecord *rr; 698 struct RequestRecord *rr;
692 699
693 GNUNET_CONTAINER_DLL_remove (clients_head, 700 GNUNET_CONTAINER_DLL_remove(clients_head,
694 clients_tail, 701 clients_tail,
695 cr); 702 cr);
696 for (unsigned int i=0;i<UINT16_MAX;i++) 703 for (unsigned int i = 0; i < UINT16_MAX; i++)
697 {
698 rr = &requests[i];
699 if (0 == rr->client_wait_list_length)
700 continue; /* not in use */
701 for (unsigned int j=0;j<rr->client_wait_list_length;j++)
702 { 704 {
703 if (rr->client_wait_list[j] == cr) 705 rr = &requests[i];
704 { 706 if (0 == rr->client_wait_list_length)
705 rr->client_wait_list[j] = NULL; 707 continue; /* not in use */
706 next_phase (rr); 708 for (unsigned int j = 0; j < rr->client_wait_list_length; j++)
707 } 709 {
710 if (rr->client_wait_list[j] == cr)
711 {
712 rr->client_wait_list[j] = NULL;
713 next_phase(rr);
714 }
715 }
708 } 716 }
709 } 717 GNUNET_free(cr);
710 GNUNET_free (cr);
711} 718}
712 719
713 720
@@ -720,39 +727,39 @@ client_disconnect_cb (void *cls,
720 * @param r number of bytes in dns 727 * @param r number of bytes in dns
721 */ 728 */
722static void 729static void
723process_dns_result (void *cls, 730process_dns_result(void *cls,
724 const struct GNUNET_TUN_DnsHeader *dns, 731 const struct GNUNET_TUN_DnsHeader *dns,
725 size_t r) 732 size_t r)
726{ 733{
727 struct RequestRecord *rr; 734 struct RequestRecord *rr;
728 735
729 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 736 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
730 "Processing DNS result from stub resolver\n"); 737 "Processing DNS result from stub resolver\n");
731 GNUNET_assert (NULL == cls); 738 GNUNET_assert(NULL == cls);
732 if (NULL == dns) 739 if (NULL == dns)
733 return; /* ignore */ 740 return; /* ignore */
734 741
735 rr = &requests[dns->id]; 742 rr = &requests[dns->id];
736 if (rr->phase != RP_INTERNET_DNS) 743 if (rr->phase != RP_INTERNET_DNS)
737 { 744 {
738 /* unexpected / bogus reply */ 745 /* unexpected / bogus reply */
739 GNUNET_STATISTICS_update (stats, 746 GNUNET_STATISTICS_update(stats,
740 gettext_noop ("# External DNS response discarded (no matching request)"), 747 gettext_noop("# External DNS response discarded (no matching request)"),
741 1, GNUNET_NO); 748 1, GNUNET_NO);
742 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 749 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
743 "Received DNS reply that does not match any pending request. Dropping.\n"); 750 "Received DNS reply that does not match any pending request. Dropping.\n");
744 return; 751 return;
745 } 752 }
746 LOG (GNUNET_ERROR_TYPE_DEBUG, 753 LOG(GNUNET_ERROR_TYPE_DEBUG,
747 "Got a response from the stub resolver for DNS request %llu intercepted locally!\n", 754 "Got a response from the stub resolver for DNS request %llu intercepted locally!\n",
748 (unsigned long long) rr->request_id); 755 (unsigned long long)rr->request_id);
749 GNUNET_free_non_null (rr->payload); 756 GNUNET_free_non_null(rr->payload);
750 rr->payload = GNUNET_malloc (r); 757 rr->payload = GNUNET_malloc(r);
751 GNUNET_memcpy (rr->payload, 758 GNUNET_memcpy(rr->payload,
752 dns, 759 dns,
753 r); 760 r);
754 rr->payload_length = r; 761 rr->payload_length = r;
755 next_phase (rr); 762 next_phase(rr);
756} 763}
757 764
758 765
@@ -763,13 +770,13 @@ process_dns_result (void *cls,
763 * @param reg the init message 770 * @param reg the init message
764 */ 771 */
765static void 772static void
766handle_client_init (void *cls, 773handle_client_init(void *cls,
767 const struct GNUNET_DNS_Register *reg) 774 const struct GNUNET_DNS_Register *reg)
768{ 775{
769 struct ClientRecord *cr = cls; 776 struct ClientRecord *cr = cls;
770 777
771 cr->flags = (enum GNUNET_DNS_Flags) ntohl (reg->flags); 778 cr->flags = (enum GNUNET_DNS_Flags)ntohl(reg->flags);
772 GNUNET_SERVICE_client_continue (cr->client); 779 GNUNET_SERVICE_client_continue(cr->client);
773} 780}
774 781
775 782
@@ -781,8 +788,8 @@ handle_client_init (void *cls,
781 * @return #GNUNET_OK (always fine) 788 * @return #GNUNET_OK (always fine)
782 */ 789 */
783static int 790static int
784check_client_response (void *cls, 791check_client_response(void *cls,
785 const struct GNUNET_DNS_Response *resp) 792 const struct GNUNET_DNS_Response *resp)
786{ 793{
787 return GNUNET_OK; /* any payload is acceptable */ 794 return GNUNET_OK; /* any payload is acceptable */
788} 795}
@@ -795,87 +802,89 @@ check_client_response (void *cls,
795 * @param resp the response 802 * @param resp the response
796 */ 803 */
797static void 804static void
798handle_client_response (void *cls, 805handle_client_response(void *cls,
799 const struct GNUNET_DNS_Response *resp) 806 const struct GNUNET_DNS_Response *resp)
800{ 807{
801 struct ClientRecord *cr = cls; 808 struct ClientRecord *cr = cls;
802 struct RequestRecord *rr; 809 struct RequestRecord *rr;
803 uint16_t msize; 810 uint16_t msize;
804 uint16_t off; 811 uint16_t off;
805 812
806 msize = ntohs (resp->header.size); 813 msize = ntohs(resp->header.size);
807 off = (uint16_t) resp->request_id; 814 off = (uint16_t)resp->request_id;
808 rr = &requests[off]; 815 rr = &requests[off];
809 LOG (GNUNET_ERROR_TYPE_DEBUG, 816 LOG(GNUNET_ERROR_TYPE_DEBUG,
810 "Received DNS response with ID %llu from local client!\n", 817 "Received DNS response with ID %llu from local client!\n",
811 (unsigned long long) resp->request_id); 818 (unsigned long long)resp->request_id);
812 if (rr->request_id != resp->request_id) 819 if (rr->request_id != resp->request_id)
813 {
814 GNUNET_STATISTICS_update (stats,
815 gettext_noop ("# Client response discarded (no matching request)"),
816 1,
817 GNUNET_NO);
818 GNUNET_SERVICE_client_continue (cr->client);
819 return;
820 }
821 for (unsigned int i=0;i<rr->client_wait_list_length;i++)
822 {
823 if (NULL == rr->client_wait_list[i])
824 continue;
825 if (rr->client_wait_list[i] != cr)
826 continue;
827 rr->client_wait_list[i] = NULL;
828 switch (ntohl (resp->drop_flag))
829 { 820 {
830 case 0: /* drop */ 821 GNUNET_STATISTICS_update(stats,
831 rr->phase = RP_DROP; 822 gettext_noop("# Client response discarded (no matching request)"),
832 break; 823 1,
833 case 1: /* no change */ 824 GNUNET_NO);
834 break; 825 GNUNET_SERVICE_client_continue(cr->client);
835 case 2: /* update */ 826 return;
836 msize -= sizeof (struct GNUNET_DNS_Response); 827 }
837 if ( (sizeof (struct GNUNET_TUN_DnsHeader) > msize) || 828 for (unsigned int i = 0; i < rr->client_wait_list_length; i++)
838 (RP_REQUEST_MONITOR == rr->phase) || 829 {
839 (RP_RESPONSE_MONITOR == rr->phase) ) 830 if (NULL == rr->client_wait_list[i])
840 { 831 continue;
841 GNUNET_break (0); 832 if (rr->client_wait_list[i] != cr)
842 GNUNET_SERVICE_client_drop (cr->client); 833 continue;
843 next_phase (rr); 834 rr->client_wait_list[i] = NULL;
844 return; 835 switch (ntohl(resp->drop_flag))
845 } 836 {
846 GNUNET_free_non_null (rr->payload); 837 case 0: /* drop */
847 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 838 rr->phase = RP_DROP;
848 "Changing DNS reply according to client specifications\n"); 839 break;
849 rr->payload = GNUNET_malloc (msize); 840
850 rr->payload_length = msize; 841 case 1: /* no change */
851 GNUNET_memcpy (rr->payload, &resp[1], msize); 842 break;
852 if (rr->phase == RP_QUERY) 843
853 { 844 case 2: /* update */
854 /* clear wait list, we're moving to MODIFY phase next */ 845 msize -= sizeof(struct GNUNET_DNS_Response);
855 GNUNET_array_grow (rr->client_wait_list, 846 if ((sizeof(struct GNUNET_TUN_DnsHeader) > msize) ||
856 rr->client_wait_list_length, 847 (RP_REQUEST_MONITOR == rr->phase) ||
857 0); 848 (RP_RESPONSE_MONITOR == rr->phase))
858 } 849 {
859 /* if query changed to answer, move past DNS resolution phase... */ 850 GNUNET_break(0);
860 if ( (RP_QUERY == rr->phase) && 851 GNUNET_SERVICE_client_drop(cr->client);
861 (rr->payload_length > sizeof (struct GNUNET_TUN_DnsHeader)) && 852 next_phase(rr);
862 ((struct GNUNET_TUN_DnsFlags*)&(((struct GNUNET_TUN_DnsHeader*) rr->payload)->flags))->query_or_response == 1) 853 return;
863 { 854 }
864 rr->phase = RP_INTERNET_DNS; 855 GNUNET_free_non_null(rr->payload);
865 GNUNET_array_grow (rr->client_wait_list, 856 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
866 rr->client_wait_list_length, 857 "Changing DNS reply according to client specifications\n");
867 0); 858 rr->payload = GNUNET_malloc(msize);
868 } 859 rr->payload_length = msize;
869 break; 860 GNUNET_memcpy(rr->payload, &resp[1], msize);
861 if (rr->phase == RP_QUERY)
862 {
863 /* clear wait list, we're moving to MODIFY phase next */
864 GNUNET_array_grow(rr->client_wait_list,
865 rr->client_wait_list_length,
866 0);
867 }
868 /* if query changed to answer, move past DNS resolution phase... */
869 if ((RP_QUERY == rr->phase) &&
870 (rr->payload_length > sizeof(struct GNUNET_TUN_DnsHeader)) &&
871 ((struct GNUNET_TUN_DnsFlags*)&(((struct GNUNET_TUN_DnsHeader*)rr->payload)->flags))->query_or_response == 1)
872 {
873 rr->phase = RP_INTERNET_DNS;
874 GNUNET_array_grow(rr->client_wait_list,
875 rr->client_wait_list_length,
876 0);
877 }
878 break;
879 }
880 next_phase(rr);
881 GNUNET_SERVICE_client_continue(cr->client);
882 return;
870 } 883 }
871 next_phase (rr);
872 GNUNET_SERVICE_client_continue (cr->client);
873 return;
874 }
875 /* odd, client was not on our list for the request, that ought 884 /* odd, client was not on our list for the request, that ought
876 to be an error */ 885 to be an error */
877 GNUNET_break (0); 886 GNUNET_break(0);
878 GNUNET_SERVICE_client_drop (cr->client); 887 GNUNET_SERVICE_client_drop(cr->client);
879} 888}
880 889
881 890
@@ -887,8 +896,8 @@ handle_client_response (void *cls,
887 * @param message the actual message, a DNS request we should handle 896 * @param message the actual message, a DNS request we should handle
888 */ 897 */
889static int 898static int
890process_helper_messages (void *cls, 899process_helper_messages(void *cls,
891 const struct GNUNET_MessageHeader *message) 900 const struct GNUNET_MessageHeader *message)
892{ 901{
893 uint16_t msize; 902 uint16_t msize;
894 const struct GNUNET_TUN_Layer2PacketHeader *tun; 903 const struct GNUNET_TUN_Layer2PacketHeader *tun;
@@ -902,93 +911,95 @@ process_helper_messages (void *cls,
902 struct sockaddr_in *dsta4; 911 struct sockaddr_in *dsta4;
903 struct sockaddr_in6 *dsta6; 912 struct sockaddr_in6 *dsta6;
904 913
905 LOG (GNUNET_ERROR_TYPE_DEBUG, 914 LOG(GNUNET_ERROR_TYPE_DEBUG,
906 "Intercepted message via DNS hijacker\n"); 915 "Intercepted message via DNS hijacker\n");
907 msize = ntohs (message->size); 916 msize = ntohs(message->size);
908 if (msize < sizeof (struct GNUNET_MessageHeader) + sizeof (struct GNUNET_TUN_Layer2PacketHeader) + sizeof (struct GNUNET_TUN_IPv4Header)) 917 if (msize < sizeof(struct GNUNET_MessageHeader) + sizeof(struct GNUNET_TUN_Layer2PacketHeader) + sizeof(struct GNUNET_TUN_IPv4Header))
909 {
910 /* non-IP packet received on TUN!? */
911 GNUNET_break (0);
912 return GNUNET_OK;
913 }
914 msize -= sizeof (struct GNUNET_MessageHeader);
915 tun = (const struct GNUNET_TUN_Layer2PacketHeader *) &message[1];
916 msize -= sizeof (struct GNUNET_TUN_Layer2PacketHeader);
917 switch (ntohs (tun->proto))
918 {
919 case ETH_P_IPV4:
920 ip4 = (const struct GNUNET_TUN_IPv4Header *) &tun[1];
921 ip6 = NULL; /* make compiler happy */
922 if ( (msize < sizeof (struct GNUNET_TUN_IPv4Header)) ||
923 (ip4->version != 4) ||
924 (ip4->header_length != sizeof (struct GNUNET_TUN_IPv4Header) / 4) ||
925 (ntohs(ip4->total_length) != msize) ||
926 (ip4->protocol != IPPROTO_UDP) )
927 { 918 {
928 /* non-IP/UDP packet received on TUN (or with options) */ 919 /* non-IP packet received on TUN!? */
929 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 920 GNUNET_break(0);
930 _("Received malformed IPv4-UDP packet on TUN interface.\n"));
931 return GNUNET_OK; 921 return GNUNET_OK;
932 } 922 }
933 udp = (const struct GNUNET_TUN_UdpHeader*) &ip4[1]; 923 msize -= sizeof(struct GNUNET_MessageHeader);
934 msize -= sizeof (struct GNUNET_TUN_IPv4Header); 924 tun = (const struct GNUNET_TUN_Layer2PacketHeader *)&message[1];
935 break; 925 msize -= sizeof(struct GNUNET_TUN_Layer2PacketHeader);
936 case ETH_P_IPV6: 926 switch (ntohs(tun->proto))
937 ip4 = NULL; /* make compiler happy */
938 ip6 = (const struct GNUNET_TUN_IPv6Header *) &tun[1];
939 if ( (msize < sizeof (struct GNUNET_TUN_IPv6Header)) ||
940 (ip6->version != 6) ||
941 (ntohs (ip6->payload_length) != msize - sizeof (struct GNUNET_TUN_IPv6Header)) ||
942 (ip6->next_header != IPPROTO_UDP) )
943 { 927 {
944 /* non-IP/UDP packet received on TUN (or with extensions) */ 928 case ETH_P_IPV4:
945 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 929 ip4 = (const struct GNUNET_TUN_IPv4Header *)&tun[1];
946 _("Received malformed IPv6-UDP packet on TUN interface.\n")); 930 ip6 = NULL; /* make compiler happy */
931 if ((msize < sizeof(struct GNUNET_TUN_IPv4Header)) ||
932 (ip4->version != 4) ||
933 (ip4->header_length != sizeof(struct GNUNET_TUN_IPv4Header) / 4) ||
934 (ntohs(ip4->total_length) != msize) ||
935 (ip4->protocol != IPPROTO_UDP))
936 {
937 /* non-IP/UDP packet received on TUN (or with options) */
938 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
939 _("Received malformed IPv4-UDP packet on TUN interface.\n"));
940 return GNUNET_OK;
941 }
942 udp = (const struct GNUNET_TUN_UdpHeader*)&ip4[1];
943 msize -= sizeof(struct GNUNET_TUN_IPv4Header);
944 break;
945
946 case ETH_P_IPV6:
947 ip4 = NULL; /* make compiler happy */
948 ip6 = (const struct GNUNET_TUN_IPv6Header *)&tun[1];
949 if ((msize < sizeof(struct GNUNET_TUN_IPv6Header)) ||
950 (ip6->version != 6) ||
951 (ntohs(ip6->payload_length) != msize - sizeof(struct GNUNET_TUN_IPv6Header)) ||
952 (ip6->next_header != IPPROTO_UDP))
953 {
954 /* non-IP/UDP packet received on TUN (or with extensions) */
955 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
956 _("Received malformed IPv6-UDP packet on TUN interface.\n"));
957 return GNUNET_OK;
958 }
959 udp = (const struct GNUNET_TUN_UdpHeader *)&ip6[1];
960 msize -= sizeof(struct GNUNET_TUN_IPv6Header);
961 break;
962
963 default:
964 /* non-IP packet received on TUN!? */
965 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
966 _("Got non-IP packet with %u bytes and protocol %u from TUN\n"),
967 (unsigned int)msize,
968 ntohs(tun->proto));
947 return GNUNET_OK; 969 return GNUNET_OK;
948 } 970 }
949 udp = (const struct GNUNET_TUN_UdpHeader *) &ip6[1]; 971 if ((msize <= sizeof(struct GNUNET_TUN_UdpHeader) + sizeof(struct GNUNET_TUN_DnsHeader)) ||
950 msize -= sizeof (struct GNUNET_TUN_IPv6Header); 972 (DNS_PORT != ntohs(udp->destination_port)))
951 break; 973 {
952 default: 974 /* non-DNS packet received on TUN, ignore */
953 /* non-IP packet received on TUN!? */ 975 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
954 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 976 _("DNS interceptor got non-DNS packet (dropped)\n"));
955 _("Got non-IP packet with %u bytes and protocol %u from TUN\n"), 977 GNUNET_STATISTICS_update(stats,
956 (unsigned int) msize, 978 gettext_noop("# Non-DNS UDP packet received via TUN interface"),
957 ntohs (tun->proto)); 979 1, GNUNET_NO);
958 return GNUNET_OK; 980 return GNUNET_OK;
959 } 981 }
960 if ( (msize <= sizeof (struct GNUNET_TUN_UdpHeader) + sizeof (struct GNUNET_TUN_DnsHeader)) || 982 msize -= sizeof(struct GNUNET_TUN_UdpHeader);
961 (DNS_PORT != ntohs (udp->destination_port)) ) 983 dns = (const struct GNUNET_TUN_DnsHeader*)&udp[1];
962 {
963 /* non-DNS packet received on TUN, ignore */
964 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
965 _("DNS interceptor got non-DNS packet (dropped)\n"));
966 GNUNET_STATISTICS_update (stats,
967 gettext_noop ("# Non-DNS UDP packet received via TUN interface"),
968 1, GNUNET_NO);
969 return GNUNET_OK;
970 }
971 msize -= sizeof (struct GNUNET_TUN_UdpHeader);
972 dns = (const struct GNUNET_TUN_DnsHeader*) &udp[1];
973 rr = &requests[dns->id]; 984 rr = &requests[dns->id];
974 985
975 /* clean up from previous request */ 986 /* clean up from previous request */
976 GNUNET_free_non_null (rr->payload); 987 GNUNET_free_non_null(rr->payload);
977 rr->payload = NULL; 988 rr->payload = NULL;
978 GNUNET_array_grow (rr->client_wait_list, 989 GNUNET_array_grow(rr->client_wait_list,
979 rr->client_wait_list_length, 990 rr->client_wait_list_length,
980 0); 991 0);
981 992
982 /* setup new request */ 993 /* setup new request */
983 rr->phase = RP_INIT; 994 rr->phase = RP_INIT;
984 switch (ntohs (tun->proto)) 995 switch (ntohs(tun->proto))
985 {
986 case ETH_P_IPV4:
987 { 996 {
988 srca4 = (struct sockaddr_in*) &rr->src_addr; 997 case ETH_P_IPV4:
989 dsta4 = (struct sockaddr_in*) &rr->dst_addr; 998 {
990 memset (srca4, 0, sizeof (struct sockaddr_in)); 999 srca4 = (struct sockaddr_in*)&rr->src_addr;
991 memset (dsta4, 0, sizeof (struct sockaddr_in)); 1000 dsta4 = (struct sockaddr_in*)&rr->dst_addr;
1001 memset(srca4, 0, sizeof(struct sockaddr_in));
1002 memset(dsta4, 0, sizeof(struct sockaddr_in));
992 srca4->sin_family = AF_INET; 1003 srca4->sin_family = AF_INET;
993 dsta4->sin_family = AF_INET; 1004 dsta4->sin_family = AF_INET;
994 srca4->sin_addr = ip4->source_address; 1005 srca4->sin_addr = ip4->source_address;
@@ -996,17 +1007,18 @@ process_helper_messages (void *cls,
996 srca4->sin_port = udp->source_port; 1007 srca4->sin_port = udp->source_port;
997 dsta4->sin_port = udp->destination_port; 1008 dsta4->sin_port = udp->destination_port;
998#if HAVE_SOCKADDR_IN_SIN_LEN 1009#if HAVE_SOCKADDR_IN_SIN_LEN
999 srca4->sin_len = sizeof (struct sockaddr_in); 1010 srca4->sin_len = sizeof(struct sockaddr_in);
1000 dsta4->sin_len = sizeof (struct sockaddr_in); 1011 dsta4->sin_len = sizeof(struct sockaddr_in);
1001#endif 1012#endif
1002 } 1013 }
1003 break; 1014 break;
1004 case ETH_P_IPV6: 1015
1016 case ETH_P_IPV6:
1005 { 1017 {
1006 srca6 = (struct sockaddr_in6*) &rr->src_addr; 1018 srca6 = (struct sockaddr_in6*)&rr->src_addr;
1007 dsta6 = (struct sockaddr_in6*) &rr->dst_addr; 1019 dsta6 = (struct sockaddr_in6*)&rr->dst_addr;
1008 memset (srca6, 0, sizeof (struct sockaddr_in6)); 1020 memset(srca6, 0, sizeof(struct sockaddr_in6));
1009 memset (dsta6, 0, sizeof (struct sockaddr_in6)); 1021 memset(dsta6, 0, sizeof(struct sockaddr_in6));
1010 srca6->sin6_family = AF_INET6; 1022 srca6->sin6_family = AF_INET6;
1011 dsta6->sin6_family = AF_INET6; 1023 dsta6->sin6_family = AF_INET6;
1012 srca6->sin6_addr = ip6->source_address; 1024 srca6->sin6_addr = ip6->source_address;
@@ -1014,27 +1026,28 @@ process_helper_messages (void *cls,
1014 srca6->sin6_port = udp->source_port; 1026 srca6->sin6_port = udp->source_port;
1015 dsta6->sin6_port = udp->destination_port; 1027 dsta6->sin6_port = udp->destination_port;
1016#if HAVE_SOCKADDR_IN_SIN_LEN 1028#if HAVE_SOCKADDR_IN_SIN_LEN
1017 srca6->sin6_len = sizeof (struct sockaddr_in6); 1029 srca6->sin6_len = sizeof(struct sockaddr_in6);
1018 dsta6->sin6_len = sizeof (struct sockaddr_in6); 1030 dsta6->sin6_len = sizeof(struct sockaddr_in6);
1019#endif 1031#endif
1020 } 1032 }
1021 break; 1033 break;
1022 default: 1034
1023 GNUNET_assert (0); 1035 default:
1024 } 1036 GNUNET_assert(0);
1025 rr->payload = GNUNET_malloc (msize); 1037 }
1038 rr->payload = GNUNET_malloc(msize);
1026 rr->payload_length = msize; 1039 rr->payload_length = msize;
1027 GNUNET_memcpy (rr->payload, dns, msize); 1040 GNUNET_memcpy(rr->payload, dns, msize);
1028 rr->request_id = dns->id | (request_id_gen << 16); 1041 rr->request_id = dns->id | (request_id_gen << 16);
1029 request_id_gen++; 1042 request_id_gen++;
1030 LOG (GNUNET_ERROR_TYPE_DEBUG, 1043 LOG(GNUNET_ERROR_TYPE_DEBUG,
1031 "Creating new DNS request %llu\n", 1044 "Creating new DNS request %llu\n",
1032 (unsigned long long) rr->request_id); 1045 (unsigned long long)rr->request_id);
1033 GNUNET_STATISTICS_update (stats, 1046 GNUNET_STATISTICS_update(stats,
1034 gettext_noop ("# DNS requests received via TUN interface"), 1047 gettext_noop("# DNS requests received via TUN interface"),
1035 1, GNUNET_NO); 1048 1, GNUNET_NO);
1036 /* start request processing state machine */ 1049 /* start request processing state machine */
1037 next_phase (rr); 1050 next_phase(rr);
1038 return GNUNET_OK; 1051 return GNUNET_OK;
1039} 1052}
1040 1053
@@ -1045,9 +1058,9 @@ process_helper_messages (void *cls,
1045 * @param service the initialized service 1058 * @param service the initialized service
1046 */ 1059 */
1047static void 1060static void
1048run (void *cls, 1061run(void *cls,
1049 const struct GNUNET_CONFIGURATION_Handle *cfg_, 1062 const struct GNUNET_CONFIGURATION_Handle *cfg_,
1050 struct GNUNET_SERVICE_Handle *service) 1063 struct GNUNET_SERVICE_Handle *service)
1051{ 1064{
1052 char *ifc_name; 1065 char *ifc_name;
1053 char *ipv4addr; 1066 char *ipv4addr;
@@ -1059,126 +1072,126 @@ run (void *cls,
1059 int nortsetup; 1072 int nortsetup;
1060 1073
1061 cfg = cfg_; 1074 cfg = cfg_;
1062 stats = GNUNET_STATISTICS_create ("dns", cfg); 1075 stats = GNUNET_STATISTICS_create("dns", cfg);
1063 GNUNET_SCHEDULER_add_shutdown (&cleanup_task, 1076 GNUNET_SCHEDULER_add_shutdown(&cleanup_task,
1064 cls); 1077 cls);
1065 dnsstub = GNUNET_DNSSTUB_start (128); 1078 dnsstub = GNUNET_DNSSTUB_start(128);
1066 /* TODO: support multiple DNS_EXIT servers being configured */ 1079 /* TODO: support multiple DNS_EXIT servers being configured */
1067 /* TODO: see above TODO on using DNS server from original packet. 1080 /* TODO: see above TODO on using DNS server from original packet.
1068 Not sure which is best... */ 1081 Not sure which is best... */
1069 dns_exit = NULL; 1082 dns_exit = NULL;
1070 if ( (GNUNET_OK != 1083 if ((GNUNET_OK !=
1071 GNUNET_CONFIGURATION_get_value_string (cfg, 1084 GNUNET_CONFIGURATION_get_value_string(cfg,
1072 "dns", 1085 "dns",
1073 "DNS_EXIT", 1086 "DNS_EXIT",
1074 &dns_exit)) || 1087 &dns_exit)) ||
1075 (GNUNET_OK != 1088 (GNUNET_OK !=
1076 GNUNET_DNSSTUB_add_dns_ip (dnsstub, 1089 GNUNET_DNSSTUB_add_dns_ip(dnsstub,
1077 dns_exit)) ) 1090 dns_exit)))
1078 { 1091 {
1079 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR, 1092 GNUNET_log_config_invalid(GNUNET_ERROR_TYPE_ERROR,
1080 "dns", 1093 "dns",
1081 "DNS_EXIT", 1094 "DNS_EXIT",
1082 _("need a valid IPv4 or IPv6 address\n")); 1095 _("need a valid IPv4 or IPv6 address\n"));
1083 GNUNET_free_non_null (dns_exit); 1096 GNUNET_free_non_null(dns_exit);
1084 } 1097 }
1085 binary = GNUNET_OS_get_suid_binary_path (cfg, "gnunet-helper-dns"); 1098 binary = GNUNET_OS_get_suid_binary_path(cfg, "gnunet-helper-dns");
1086 1099
1087 if (GNUNET_YES != 1100 if (GNUNET_YES !=
1088 GNUNET_OS_check_helper_binary (binary, 1101 GNUNET_OS_check_helper_binary(binary,
1089 GNUNET_YES, 1102 GNUNET_YES,
1090 NULL)) // TODO: once we have a windows-testcase, add test parameters here 1103 NULL)) // TODO: once we have a windows-testcase, add test parameters here
1091 { 1104 {
1092 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 1105 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
1093 _("`%s' is not SUID or the path is invalid, " 1106 _("`%s' is not SUID or the path is invalid, "
1094 "will not run DNS interceptor\n"), 1107 "will not run DNS interceptor\n"),
1095 binary); 1108 binary);
1096 global_ret = 1; 1109 global_ret = 1;
1097 GNUNET_free (binary); 1110 GNUNET_free(binary);
1098 return; 1111 return;
1099 } 1112 }
1100 GNUNET_free (binary); 1113 GNUNET_free(binary);
1101 1114
1102 helper_argv[0] = GNUNET_strdup ("gnunet-dns"); 1115 helper_argv[0] = GNUNET_strdup("gnunet-dns");
1103 if (GNUNET_SYSERR == 1116 if (GNUNET_SYSERR ==
1104 GNUNET_CONFIGURATION_get_value_string (cfg, 1117 GNUNET_CONFIGURATION_get_value_string(cfg,
1105 "dns", 1118 "dns",
1106 "IFNAME", 1119 "IFNAME",
1107 &ifc_name)) 1120 &ifc_name))
1108 { 1121 {
1109 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 1122 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1110 "No entry 'IFNAME' in configuration!\n"); 1123 "No entry 'IFNAME' in configuration!\n");
1111 GNUNET_free (binary); 1124 GNUNET_free(binary);
1112 GNUNET_SCHEDULER_shutdown (); 1125 GNUNET_SCHEDULER_shutdown();
1113 return; 1126 return;
1114 } 1127 }
1115 helper_argv[1] = ifc_name; 1128 helper_argv[1] = ifc_name;
1116 if ( (GNUNET_SYSERR == 1129 if ((GNUNET_SYSERR ==
1117 GNUNET_CONFIGURATION_get_value_string (cfg, 1130 GNUNET_CONFIGURATION_get_value_string(cfg,
1118 "dns", 1131 "dns",
1119 "IPV6ADDR", 1132 "IPV6ADDR",
1120 &ipv6addr)) ) 1133 &ipv6addr)))
1121 { 1134 {
1122 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 1135 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1123 "No entry 'IPV6ADDR' in configuration!\n"); 1136 "No entry 'IPV6ADDR' in configuration!\n");
1124 GNUNET_free (binary); 1137 GNUNET_free(binary);
1125 GNUNET_SCHEDULER_shutdown (); 1138 GNUNET_SCHEDULER_shutdown();
1126 return; 1139 return;
1127 } 1140 }
1128 helper_argv[2] = ipv6addr; 1141 helper_argv[2] = ipv6addr;
1129 if (GNUNET_SYSERR == 1142 if (GNUNET_SYSERR ==
1130 GNUNET_CONFIGURATION_get_value_string (cfg, 1143 GNUNET_CONFIGURATION_get_value_string(cfg,
1131 "dns", 1144 "dns",
1132 "IPV6PREFIX", 1145 "IPV6PREFIX",
1133 &ipv6prefix)) 1146 &ipv6prefix))
1134 { 1147 {
1135 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 1148 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1136 "No entry 'IPV6PREFIX' in configuration!\n"); 1149 "No entry 'IPV6PREFIX' in configuration!\n");
1137 GNUNET_free (binary); 1150 GNUNET_free(binary);
1138 GNUNET_SCHEDULER_shutdown (); 1151 GNUNET_SCHEDULER_shutdown();
1139 return; 1152 return;
1140 } 1153 }
1141 helper_argv[3] = ipv6prefix; 1154 helper_argv[3] = ipv6prefix;
1142 1155
1143 if (GNUNET_SYSERR == 1156 if (GNUNET_SYSERR ==
1144 GNUNET_CONFIGURATION_get_value_string (cfg, 1157 GNUNET_CONFIGURATION_get_value_string(cfg,
1145 "dns", 1158 "dns",
1146 "IPV4ADDR", 1159 "IPV4ADDR",
1147 &ipv4addr)) 1160 &ipv4addr))
1148 { 1161 {
1149 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 1162 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1150 "No entry 'IPV4ADDR' in configuration!\n"); 1163 "No entry 'IPV4ADDR' in configuration!\n");
1151 GNUNET_free (binary); 1164 GNUNET_free(binary);
1152 GNUNET_SCHEDULER_shutdown (); 1165 GNUNET_SCHEDULER_shutdown();
1153 return; 1166 return;
1154 } 1167 }
1155 helper_argv[4] = ipv4addr; 1168 helper_argv[4] = ipv4addr;
1156 if (GNUNET_SYSERR == 1169 if (GNUNET_SYSERR ==
1157 GNUNET_CONFIGURATION_get_value_string (cfg, "dns", "IPV4MASK", 1170 GNUNET_CONFIGURATION_get_value_string(cfg, "dns", "IPV4MASK",
1158 &ipv4mask)) 1171 &ipv4mask))
1159 { 1172 {
1160 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 1173 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1161 "No entry 'IPV4MASK' in configuration!\n"); 1174 "No entry 'IPV4MASK' in configuration!\n");
1162 GNUNET_free (binary); 1175 GNUNET_free(binary);
1163 GNUNET_SCHEDULER_shutdown (); 1176 GNUNET_SCHEDULER_shutdown();
1164 return; 1177 return;
1165 } 1178 }
1166 helper_argv[5] = ipv4mask; 1179 helper_argv[5] = ipv4mask;
1167 1180
1168 nortsetup = GNUNET_CONFIGURATION_get_value_yesno (cfg, "dns", 1181 nortsetup = GNUNET_CONFIGURATION_get_value_yesno(cfg, "dns",
1169 "SKIP_ROUTING_SETUP"); 1182 "SKIP_ROUTING_SETUP");
1170 if (GNUNET_YES == nortsetup) 1183 if (GNUNET_YES == nortsetup)
1171 helper_argv[6] = GNUNET_strdup("1"); 1184 helper_argv[6] = GNUNET_strdup("1");
1172 else 1185 else
1173 helper_argv[6] = GNUNET_strdup("0"); 1186 helper_argv[6] = GNUNET_strdup("0");
1174 1187
1175 helper_argv[7] = NULL; 1188 helper_argv[7] = NULL;
1176 hijacker = GNUNET_HELPER_start (GNUNET_NO, 1189 hijacker = GNUNET_HELPER_start(GNUNET_NO,
1177 binary, 1190 binary,
1178 helper_argv, 1191 helper_argv,
1179 &process_helper_messages, 1192 &process_helper_messages,
1180 NULL, NULL); 1193 NULL, NULL);
1181 GNUNET_free (binary); 1194 GNUNET_free(binary);
1182} 1195}
1183 1196
1184 1197
@@ -1186,21 +1199,21 @@ run (void *cls,
1186 * Define "main" method using service macro. 1199 * Define "main" method using service macro.
1187 */ 1200 */
1188GNUNET_SERVICE_MAIN 1201GNUNET_SERVICE_MAIN
1189("dns", 1202 ("dns",
1190 GNUNET_SERVICE_OPTION_NONE, 1203 GNUNET_SERVICE_OPTION_NONE,
1191 &run, 1204 &run,
1192 &client_connect_cb, 1205 &client_connect_cb,
1193 &client_disconnect_cb, 1206 &client_disconnect_cb,
1194 NULL, 1207 NULL,
1195 GNUNET_MQ_hd_fixed_size (client_init, 1208 GNUNET_MQ_hd_fixed_size(client_init,
1196 GNUNET_MESSAGE_TYPE_DNS_CLIENT_INIT, 1209 GNUNET_MESSAGE_TYPE_DNS_CLIENT_INIT,
1197 struct GNUNET_DNS_Register, 1210 struct GNUNET_DNS_Register,
1198 NULL), 1211 NULL),
1199 GNUNET_MQ_hd_var_size (client_response, 1212 GNUNET_MQ_hd_var_size(client_response,
1200 GNUNET_MESSAGE_TYPE_DNS_CLIENT_RESPONSE, 1213 GNUNET_MESSAGE_TYPE_DNS_CLIENT_RESPONSE,
1201 struct GNUNET_DNS_Response, 1214 struct GNUNET_DNS_Response,
1202 NULL), 1215 NULL),
1203 GNUNET_MQ_handler_end ()); 1216 GNUNET_MQ_handler_end());
1204 1217
1205 1218
1206/* FIXME: this might need a port on systems without 'getresgid' */ 1219/* FIXME: this might need a port on systems without 'getresgid' */
@@ -1209,28 +1222,28 @@ GNUNET_SERVICE_MAIN
1209 * Enable use of SGID capabilities on POSIX 1222 * Enable use of SGID capabilities on POSIX
1210 */ 1223 */
1211void __attribute__ ((constructor)) 1224void __attribute__ ((constructor))
1212GNUNET_DNS_init () 1225GNUNET_DNS_init()
1213{ 1226{
1214 gid_t rgid; 1227 gid_t rgid;
1215 gid_t egid; 1228 gid_t egid;
1216 gid_t sgid; 1229 gid_t sgid;
1217 1230
1218 if (-1 == getresgid (&rgid, 1231 if (-1 == getresgid(&rgid,
1219 &egid, 1232 &egid,
1220 &sgid)) 1233 &sgid))
1221 { 1234 {
1222 fprintf (stderr, 1235 fprintf(stderr,
1223 "getresgid failed: %s\n", 1236 "getresgid failed: %s\n",
1224 strerror (errno)); 1237 strerror(errno));
1225 } 1238 }
1226 else if (sgid != rgid) 1239 else if (sgid != rgid)
1227 { 1240 {
1228 if (-1 == setregid (sgid, 1241 if (-1 == setregid(sgid,
1229 sgid)) 1242 sgid))
1230 fprintf (stderr, 1243 fprintf(stderr,
1231 "setregid failed: %s\n", 1244 "setregid failed: %s\n",
1232 strerror (errno)); 1245 strerror(errno));
1233 } 1246 }
1234} 1247}
1235#endif 1248#endif
1236 1249
diff --git a/src/dns/gnunet-zonewalk.c b/src/dns/gnunet-zonewalk.c
index 19fc5b78d..2968d0209 100644
--- a/src/dns/gnunet-zonewalk.c
+++ b/src/dns/gnunet-zonewalk.c
@@ -11,12 +11,12 @@
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20 20
21/** 21/**
22 * @file src/dns/gnunet-zoneimport.c 22 * @file src/dns/gnunet-zoneimport.c
@@ -31,8 +31,7 @@
31/** 31/**
32 * Request we should make. 32 * Request we should make.
33 */ 33 */
34struct Request 34struct Request {
35{
36 /** 35 /**
37 * Requests are kept in a DLL. 36 * Requests are kept in a DLL.
38 */ 37 */
@@ -144,145 +143,155 @@ static struct GNUNET_SCHEDULER_Task *t;
144 * @param rec response 143 * @param rec response
145 */ 144 */
146static void 145static void
147process_record (struct Request *req, 146process_record(struct Request *req,
148 struct GNUNET_DNSPARSER_Record *rec) 147 struct GNUNET_DNSPARSER_Record *rec)
149{ 148{
150 char buf[INET6_ADDRSTRLEN]; 149 char buf[INET6_ADDRSTRLEN];
151 150
152 records++; 151 records++;
153 switch (rec->type) 152 switch (rec->type)
154 { 153 {
155 case GNUNET_DNSPARSER_TYPE_A: 154 case GNUNET_DNSPARSER_TYPE_A:
156 fprintf (stdout, 155 fprintf(stdout,
157 "%s A %s\n", 156 "%s A %s\n",
158 req->hostname, 157 req->hostname,
159 inet_ntop (AF_INET, 158 inet_ntop(AF_INET,
160 rec->data.raw.data, 159 rec->data.raw.data,
161 buf, 160 buf,
162 sizeof (buf))); 161 sizeof(buf)));
163 break; 162 break;
164 case GNUNET_DNSPARSER_TYPE_AAAA: 163
165 fprintf (stdout, 164 case GNUNET_DNSPARSER_TYPE_AAAA:
166 "%s AAAA %s\n", 165 fprintf(stdout,
167 req->hostname, 166 "%s AAAA %s\n",
168 inet_ntop (AF_INET6, 167 req->hostname,
168 inet_ntop(AF_INET6,
169 rec->data.raw.data, 169 rec->data.raw.data,
170 buf, 170 buf,
171 sizeof (buf))); 171 sizeof(buf)));
172 break; 172 break;
173 case GNUNET_DNSPARSER_TYPE_NS: 173
174 fprintf (stdout, 174 case GNUNET_DNSPARSER_TYPE_NS:
175 "%s NS %s\n", 175 fprintf(stdout,
176 req->hostname, 176 "%s NS %s\n",
177 rec->data.hostname); 177 req->hostname,
178 break; 178 rec->data.hostname);
179 case GNUNET_DNSPARSER_TYPE_CNAME: 179 break;
180 fprintf (stdout, 180
181 "%s CNAME %s\n", 181 case GNUNET_DNSPARSER_TYPE_CNAME:
182 req->hostname, 182 fprintf(stdout,
183 rec->data.hostname); 183 "%s CNAME %s\n",
184 break; 184 req->hostname,
185 case GNUNET_DNSPARSER_TYPE_MX: 185 rec->data.hostname);
186 fprintf (stdout, 186 break;
187 "%s MX %u %s\n", 187
188 req->hostname, 188 case GNUNET_DNSPARSER_TYPE_MX:
189 (unsigned int) rec->data.mx->preference, 189 fprintf(stdout,
190 rec->data.mx->mxhost); 190 "%s MX %u %s\n",
191 break; 191 req->hostname,
192 case GNUNET_DNSPARSER_TYPE_SOA: 192 (unsigned int)rec->data.mx->preference,
193 fprintf (stdout, 193 rec->data.mx->mxhost);
194 "%s SOA %s %s %u %u %u %u %u\n", 194 break;
195 req->hostname, 195
196 rec->data.soa->mname, 196 case GNUNET_DNSPARSER_TYPE_SOA:
197 rec->data.soa->rname, 197 fprintf(stdout,
198 (unsigned int) rec->data.soa->serial, 198 "%s SOA %s %s %u %u %u %u %u\n",
199 (unsigned int) rec->data.soa->refresh, 199 req->hostname,
200 (unsigned int) rec->data.soa->retry, 200 rec->data.soa->mname,
201 (unsigned int) rec->data.soa->expire, 201 rec->data.soa->rname,
202 (unsigned int) rec->data.soa->minimum_ttl); 202 (unsigned int)rec->data.soa->serial,
203 break; 203 (unsigned int)rec->data.soa->refresh,
204 case GNUNET_DNSPARSER_TYPE_SRV: 204 (unsigned int)rec->data.soa->retry,
205 fprintf (stdout, 205 (unsigned int)rec->data.soa->expire,
206 "%s SRV %s %u %u %u\n", 206 (unsigned int)rec->data.soa->minimum_ttl);
207 req->hostname, 207 break;
208 rec->data.srv->target, 208
209 rec->data.srv->priority, 209 case GNUNET_DNSPARSER_TYPE_SRV:
210 rec->data.srv->weight, 210 fprintf(stdout,
211 rec->data.srv->port); 211 "%s SRV %s %u %u %u\n",
212 break; 212 req->hostname,
213 case GNUNET_DNSPARSER_TYPE_PTR: 213 rec->data.srv->target,
214 fprintf (stdout, 214 rec->data.srv->priority,
215 "%s PTR %s\n", 215 rec->data.srv->weight,
216 req->hostname, 216 rec->data.srv->port);
217 rec->data.hostname); 217 break;
218 break; 218
219 case GNUNET_DNSPARSER_TYPE_TXT: 219 case GNUNET_DNSPARSER_TYPE_PTR:
220 fprintf (stdout, 220 fprintf(stdout,
221 "%s TXT %.*s\n", 221 "%s PTR %s\n",
222 req->hostname, 222 req->hostname,
223 (int) rec->data.raw.data_len, 223 rec->data.hostname);
224 (char *) rec->data.raw.data); 224 break;
225 break; 225
226 case GNUNET_DNSPARSER_TYPE_DNAME: 226 case GNUNET_DNSPARSER_TYPE_TXT:
227 fprintf (stdout, 227 fprintf(stdout,
228 "%s DNAME %s\n", 228 "%s TXT %.*s\n",
229 req->hostname, 229 req->hostname,
230 rec->data.hostname); 230 (int)rec->data.raw.data_len,
231 break; 231 (char *)rec->data.raw.data);
232 break;
233
234 case GNUNET_DNSPARSER_TYPE_DNAME:
235 fprintf(stdout,
236 "%s DNAME %s\n",
237 req->hostname,
238 rec->data.hostname);
239 break;
232 240
233 /* obscure records */ 241 /* obscure records */
234 case GNUNET_DNSPARSER_TYPE_AFSDB: 242 case GNUNET_DNSPARSER_TYPE_AFSDB:
235 case GNUNET_DNSPARSER_TYPE_NAPTR: 243 case GNUNET_DNSPARSER_TYPE_NAPTR:
236 case GNUNET_DNSPARSER_TYPE_APL: 244 case GNUNET_DNSPARSER_TYPE_APL:
237 case GNUNET_DNSPARSER_TYPE_DHCID: 245 case GNUNET_DNSPARSER_TYPE_DHCID:
238 case GNUNET_DNSPARSER_TYPE_HIP: 246 case GNUNET_DNSPARSER_TYPE_HIP:
239 case GNUNET_DNSPARSER_TYPE_LOC: 247 case GNUNET_DNSPARSER_TYPE_LOC:
240 case GNUNET_DNSPARSER_TYPE_RP: 248 case GNUNET_DNSPARSER_TYPE_RP:
241 case GNUNET_DNSPARSER_TYPE_TKEY: 249 case GNUNET_DNSPARSER_TYPE_TKEY:
242 case GNUNET_DNSPARSER_TYPE_TSIG: 250 case GNUNET_DNSPARSER_TYPE_TSIG:
243 case GNUNET_DNSPARSER_TYPE_URI: 251 case GNUNET_DNSPARSER_TYPE_URI:
244 case GNUNET_DNSPARSER_TYPE_TA: 252 case GNUNET_DNSPARSER_TYPE_TA:
245 253
246 /* DNSSEC */ 254 /* DNSSEC */
247 case GNUNET_DNSPARSER_TYPE_DS: 255 case GNUNET_DNSPARSER_TYPE_DS:
248 case GNUNET_DNSPARSER_TYPE_RRSIG: 256 case GNUNET_DNSPARSER_TYPE_RRSIG:
249 case GNUNET_DNSPARSER_TYPE_NSEC: 257 case GNUNET_DNSPARSER_TYPE_NSEC:
250 case GNUNET_DNSPARSER_TYPE_DNSKEY: 258 case GNUNET_DNSPARSER_TYPE_DNSKEY:
251 case GNUNET_DNSPARSER_TYPE_NSEC3: 259 case GNUNET_DNSPARSER_TYPE_NSEC3:
252 case GNUNET_DNSPARSER_TYPE_NSEC3PARAM: 260 case GNUNET_DNSPARSER_TYPE_NSEC3PARAM:
253 case GNUNET_DNSPARSER_TYPE_CDS: 261 case GNUNET_DNSPARSER_TYPE_CDS:
254 case GNUNET_DNSPARSER_TYPE_CDNSKEY: 262 case GNUNET_DNSPARSER_TYPE_CDNSKEY:
255 263
256 /* DNSSEC payload */ 264 /* DNSSEC payload */
257 case GNUNET_DNSPARSER_TYPE_CERT: 265 case GNUNET_DNSPARSER_TYPE_CERT:
258 case GNUNET_DNSPARSER_TYPE_SSHFP: 266 case GNUNET_DNSPARSER_TYPE_SSHFP:
259 case GNUNET_DNSPARSER_TYPE_IPSECKEY: 267 case GNUNET_DNSPARSER_TYPE_IPSECKEY:
260 case GNUNET_DNSPARSER_TYPE_TLSA: 268 case GNUNET_DNSPARSER_TYPE_TLSA:
261 case GNUNET_DNSPARSER_TYPE_OPENPGPKEY: 269 case GNUNET_DNSPARSER_TYPE_OPENPGPKEY:
262 270
263 /* obsolete records */ 271 /* obsolete records */
264 case GNUNET_DNSPARSER_TYPE_SIG: 272 case GNUNET_DNSPARSER_TYPE_SIG:
265 case GNUNET_DNSPARSER_TYPE_KEY: 273 case GNUNET_DNSPARSER_TYPE_KEY:
266 case GNUNET_DNSPARSER_TYPE_KX: 274 case GNUNET_DNSPARSER_TYPE_KX:
267 { 275 {
268 char *base32; 276 char *base32;
269 277
270 base32 = GNUNET_STRINGS_data_to_string_alloc (rec->data.raw.data, 278 base32 = GNUNET_STRINGS_data_to_string_alloc(rec->data.raw.data,
271 rec->data.raw.data_len); 279 rec->data.raw.data_len);
272 fprintf (stdout, 280 fprintf(stdout,
273 "%s (%u) %s\n", 281 "%s (%u) %s\n",
274 req->hostname, 282 req->hostname,
275 rec->type, 283 rec->type,
276 base32); 284 base32);
277 GNUNET_free (base32); 285 GNUNET_free(base32);
278 } 286 }
279 break; 287 break;
280 default: 288
281 fprintf (stderr, 289 default:
282 "Unsupported type %u\n", 290 fprintf(stderr,
283 (unsigned int) rec->type); 291 "Unsupported type %u\n",
284 break; 292 (unsigned int)rec->type);
285 } 293 break;
294 }
286} 295}
287 296
288 297
@@ -294,90 +303,90 @@ process_record (struct Request *req,
294 * @param dns_len number of bytes in @a dns 303 * @param dns_len number of bytes in @a dns
295 */ 304 */
296static void 305static void
297process_result (void *cls, 306process_result(void *cls,
298 const struct GNUNET_TUN_DnsHeader *dns, 307 const struct GNUNET_TUN_DnsHeader *dns,
299 size_t dns_len) 308 size_t dns_len)
300{ 309{
301 struct Request *req = cls; 310 struct Request *req = cls;
302 struct GNUNET_DNSPARSER_Packet *p; 311 struct GNUNET_DNSPARSER_Packet *p;
303 312
304 if (NULL == dns) 313 if (NULL == dns)
305 {
306 /* stub gave up */
307 pending--;
308 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
309 "Stub gave up on DNS reply for `%s'\n",
310 req->hostname);
311 GNUNET_CONTAINER_DLL_remove (req_head,
312 req_tail,
313 req);
314 if (req->issue_num > MAX_RETRIES)
315 { 314 {
316 failures++; 315 /* stub gave up */
317 GNUNET_free (req->hostname); 316 pending--;
318 GNUNET_free (req->raw); 317 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
319 GNUNET_free (req); 318 "Stub gave up on DNS reply for `%s'\n",
319 req->hostname);
320 GNUNET_CONTAINER_DLL_remove(req_head,
321 req_tail,
322 req);
323 if (req->issue_num > MAX_RETRIES)
324 {
325 failures++;
326 GNUNET_free(req->hostname);
327 GNUNET_free(req->raw);
328 GNUNET_free(req);
329 return;
330 }
331 GNUNET_CONTAINER_DLL_insert_tail(req_head,
332 req_tail,
333 req);
334 req->rs = NULL;
320 return; 335 return;
321 } 336 }
322 GNUNET_CONTAINER_DLL_insert_tail (req_head,
323 req_tail,
324 req);
325 req->rs = NULL;
326 return;
327 }
328 if (req->id != dns->id) 337 if (req->id != dns->id)
329 return; 338 return;
330 pending--; 339 pending--;
331 GNUNET_DNSSTUB_resolve_cancel (req->rs); 340 GNUNET_DNSSTUB_resolve_cancel(req->rs);
332 req->rs = NULL; 341 req->rs = NULL;
333 GNUNET_CONTAINER_DLL_remove (req_head, 342 GNUNET_CONTAINER_DLL_remove(req_head,
334 req_tail, 343 req_tail,
335 req); 344 req);
336 p = GNUNET_DNSPARSER_parse ((const char *) dns, 345 p = GNUNET_DNSPARSER_parse((const char *)dns,
337 dns_len); 346 dns_len);
338 if (NULL == p) 347 if (NULL == p)
339 {
340 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
341 "Failed to parse DNS reply for `%s'\n",
342 req->hostname);
343 if (req->issue_num > MAX_RETRIES)
344 { 348 {
345 failures++; 349 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
346 GNUNET_free (req->hostname); 350 "Failed to parse DNS reply for `%s'\n",
347 GNUNET_free (req->raw); 351 req->hostname);
348 GNUNET_free (req); 352 if (req->issue_num > MAX_RETRIES)
353 {
354 failures++;
355 GNUNET_free(req->hostname);
356 GNUNET_free(req->raw);
357 GNUNET_free(req);
358 return;
359 }
360 GNUNET_CONTAINER_DLL_insert_tail(req_head,
361 req_tail,
362 req);
349 return; 363 return;
350 } 364 }
351 GNUNET_CONTAINER_DLL_insert_tail (req_head, 365 for (unsigned int i = 0; i < p->num_answers; i++)
352 req_tail, 366 {
353 req); 367 struct GNUNET_DNSPARSER_Record *rs = &p->answers[i];
354 return; 368
355 } 369 process_record(req,
356 for (unsigned int i=0;i<p->num_answers;i++) 370 rs);
357 { 371 }
358 struct GNUNET_DNSPARSER_Record *rs = &p->answers[i]; 372 for (unsigned int i = 0; i < p->num_authority_records; i++)
359 373 {
360 process_record (req, 374 struct GNUNET_DNSPARSER_Record *rs = &p->authority_records[i];
361 rs); 375
362 } 376 process_record(req,
363 for (unsigned int i=0;i<p->num_authority_records;i++) 377 rs);
364 { 378 }
365 struct GNUNET_DNSPARSER_Record *rs = &p->authority_records[i]; 379 for (unsigned int i = 0; i < p->num_additional_records; i++)
366 380 {
367 process_record (req, 381 struct GNUNET_DNSPARSER_Record *rs = &p->additional_records[i];
368 rs); 382
369 } 383 process_record(req,
370 for (unsigned int i=0;i<p->num_additional_records;i++) 384 rs);
371 { 385 }
372 struct GNUNET_DNSPARSER_Record *rs = &p->additional_records[i]; 386 GNUNET_DNSPARSER_free_packet(p);
373 387 GNUNET_free(req->hostname);
374 process_record (req, 388 GNUNET_free(req->raw);
375 rs); 389 GNUNET_free(req);
376 }
377 GNUNET_DNSPARSER_free_packet (p);
378 GNUNET_free (req->hostname);
379 GNUNET_free (req->raw);
380 GNUNET_free (req);
381} 390}
382 391
383 392
@@ -391,31 +400,31 @@ process_result (void *cls,
391 * #GNUNET_SYSERR if we are at the rate limit 400 * #GNUNET_SYSERR if we are at the rate limit
392 */ 401 */
393static int 402static int
394submit_req (struct Request *req) 403submit_req(struct Request *req)
395{ 404{
396 static struct timeval last_request; 405 static struct timeval last_request;
397 struct timeval now; 406 struct timeval now;
398 407
399 if (NULL != req->rs) 408 if (NULL != req->rs)
400 return GNUNET_NO; /* already submitted */ 409 return GNUNET_NO; /* already submitted */
401 gettimeofday (&now, 410 gettimeofday(&now,
402 NULL); 411 NULL);
403 if ( ( ( (now.tv_sec - last_request.tv_sec) == 0) && 412 if ((((now.tv_sec - last_request.tv_sec) == 0) &&
404 ( (now.tv_usec - last_request.tv_usec) < TIME_THRESH) ) || 413 ((now.tv_usec - last_request.tv_usec) < TIME_THRESH)) ||
405 (pending >= THRESH) ) 414 (pending >= THRESH))
406 return GNUNET_SYSERR; 415 return GNUNET_SYSERR;
407 GNUNET_assert (NULL == req->rs); 416 GNUNET_assert(NULL == req->rs);
408 req->rs = GNUNET_DNSSTUB_resolve (ctx, 417 req->rs = GNUNET_DNSSTUB_resolve(ctx,
409 req->raw, 418 req->raw,
410 req->raw_len, 419 req->raw_len,
411 &process_result, 420 &process_result,
412 req); 421 req);
413 GNUNET_assert (NULL != req->rs); 422 GNUNET_assert(NULL != req->rs);
414 req->issue_num++; 423 req->issue_num++;
415 last_request = now; 424 last_request = now;
416 lookups++; 425 lookups++;
417 pending++; 426 pending++;
418 req->time = time (NULL); 427 req->time = time(NULL);
419 return GNUNET_OK; 428 return GNUNET_OK;
420} 429}
421 430
@@ -428,21 +437,21 @@ submit_req (struct Request *req)
428static void 437static void
429process_queue(void *cls) 438process_queue(void *cls)
430{ 439{
431 (void) cls; 440 (void)cls;
432 t = NULL; 441 t = NULL;
433 for (struct Request *req = req_head; 442 for (struct Request *req = req_head;
434 NULL != req; 443 NULL != req;
435 req = req->next) 444 req = req->next)
436 { 445 {
437 if (GNUNET_SYSERR == submit_req (req)) 446 if (GNUNET_SYSERR == submit_req(req))
438 break; 447 break;
439 } 448 }
440 if (NULL != req_head) 449 if (NULL != req_head)
441 t = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MILLISECONDS, 450 t = GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_UNIT_MILLISECONDS,
442 &process_queue, 451 &process_queue,
443 NULL); 452 NULL);
444 else 453 else
445 GNUNET_SCHEDULER_shutdown (); 454 GNUNET_SCHEDULER_shutdown();
446} 455}
447 456
448 457
@@ -452,15 +461,15 @@ process_queue(void *cls)
452 * @param cls NULL 461 * @param cls NULL
453 */ 462 */
454static void 463static void
455do_shutdown (void *cls) 464do_shutdown(void *cls)
456{ 465{
457 (void) cls; 466 (void)cls;
458 if (NULL != t) 467 if (NULL != t)
459 { 468 {
460 GNUNET_SCHEDULER_cancel (t); 469 GNUNET_SCHEDULER_cancel(t);
461 t = NULL; 470 t = NULL;
462 } 471 }
463 GNUNET_DNSSTUB_stop (ctx); 472 GNUNET_DNSSTUB_stop(ctx);
464 ctx = NULL; 473 ctx = NULL;
465} 474}
466 475
@@ -472,14 +481,14 @@ do_shutdown (void *cls)
472 * @param cls NULL 481 * @param cls NULL
473 */ 482 */
474static void 483static void
475run (void *cls) 484run(void *cls)
476{ 485{
477 (void) cls; 486 (void)cls;
478 487
479 GNUNET_SCHEDULER_add_shutdown (&do_shutdown, 488 GNUNET_SCHEDULER_add_shutdown(&do_shutdown,
480 NULL);
481 t = GNUNET_SCHEDULER_add_now (&process_queue,
482 NULL); 489 NULL);
490 t = GNUNET_SCHEDULER_add_now(&process_queue,
491 NULL);
483} 492}
484 493
485 494
@@ -489,7 +498,7 @@ run (void *cls)
489 * @param hostname name to resolve 498 * @param hostname name to resolve
490 */ 499 */
491static void 500static void
492queue (const char *hostname) 501queue(const char *hostname)
493{ 502{
494 struct GNUNET_DNSPARSER_Packet p; 503 struct GNUNET_DNSPARSER_Packet p;
495 struct GNUNET_DNSPARSER_Query q; 504 struct GNUNET_DNSPARSER_Query q;
@@ -499,46 +508,46 @@ queue (const char *hostname)
499 int ret; 508 int ret;
500 509
501 if (GNUNET_OK != 510 if (GNUNET_OK !=
502 GNUNET_DNSPARSER_check_name (hostname)) 511 GNUNET_DNSPARSER_check_name(hostname))
503 { 512 {
504 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 513 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
505 "Refusing invalid hostname `%s'\n", 514 "Refusing invalid hostname `%s'\n",
506 hostname); 515 hostname);
507 return; 516 return;
508 } 517 }
509 q.name = (char *) hostname; 518 q.name = (char *)hostname;
510 q.type = GNUNET_DNSPARSER_TYPE_NS; 519 q.type = GNUNET_DNSPARSER_TYPE_NS;
511 q.dns_traffic_class = GNUNET_TUN_DNS_CLASS_INTERNET; 520 q.dns_traffic_class = GNUNET_TUN_DNS_CLASS_INTERNET;
512 521
513 memset (&p, 522 memset(&p,
514 0, 523 0,
515 sizeof (p)); 524 sizeof(p));
516 p.num_queries = 1; 525 p.num_queries = 1;
517 p.queries = &q; 526 p.queries = &q;
518 p.id = (uint16_t) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, 527 p.id = (uint16_t)GNUNET_CRYPTO_random_u32(GNUNET_CRYPTO_QUALITY_NONCE,
519 UINT16_MAX); 528 UINT16_MAX);
520 ret = GNUNET_DNSPARSER_pack (&p, 529 ret = GNUNET_DNSPARSER_pack(&p,
521 UINT16_MAX, 530 UINT16_MAX,
522 &raw, 531 &raw,
523 &raw_size); 532 &raw_size);
524 if (GNUNET_OK != ret) 533 if (GNUNET_OK != ret)
525 { 534 {
526 if (GNUNET_NO == ret) 535 if (GNUNET_NO == ret)
527 GNUNET_free (raw); 536 GNUNET_free(raw);
528 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 537 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
529 "Failed to pack query for hostname `%s'\n", 538 "Failed to pack query for hostname `%s'\n",
530 hostname); 539 hostname);
531 return; 540 return;
532 } 541 }
533 542
534 req = GNUNET_new (struct Request); 543 req = GNUNET_new(struct Request);
535 req->hostname = strdup (hostname); 544 req->hostname = strdup(hostname);
536 req->raw = raw; 545 req->raw = raw;
537 req->raw_len = raw_size; 546 req->raw_len = raw_size;
538 req->id = p.id; 547 req->id = p.id;
539 GNUNET_CONTAINER_DLL_insert_tail (req_head, 548 GNUNET_CONTAINER_DLL_insert_tail(req_head,
540 req_tail, 549 req_tail,
541 req); 550 req);
542} 551}
543 552
544 553
@@ -550,50 +559,50 @@ queue (const char *hostname)
550 * @return 0 on success 559 * @return 0 on success
551 */ 560 */
552int 561int
553main (int argc, 562main(int argc,
554 char **argv) 563 char **argv)
555{ 564{
556 char hn[256]; 565 char hn[256];
557 566
558 if (2 != argc) 567 if (2 != argc)
559 { 568 {
560 fprintf (stderr, 569 fprintf(stderr,
561 "Missing required configuration argument\n"); 570 "Missing required configuration argument\n");
562 return -1; 571 return -1;
563 } 572 }
564 ctx = GNUNET_DNSSTUB_start (256); 573 ctx = GNUNET_DNSSTUB_start(256);
565 if (NULL == ctx) 574 if (NULL == ctx)
566 { 575 {
567 fprintf (stderr, 576 fprintf(stderr,
568 "Failed to initialize GNUnet DNS STUB\n"); 577 "Failed to initialize GNUnet DNS STUB\n");
569 return 1; 578 return 1;
570 } 579 }
571 if (GNUNET_OK != 580 if (GNUNET_OK !=
572 GNUNET_DNSSTUB_add_dns_ip (ctx, 581 GNUNET_DNSSTUB_add_dns_ip(ctx,
573 argv[1])) 582 argv[1]))
574 { 583 {
575 fprintf (stderr, 584 fprintf(stderr,
576 "Failed to use `%s' for DNS resolver\n", 585 "Failed to use `%s' for DNS resolver\n",
577 argv[1]); 586 argv[1]);
578 return 1; 587 return 1;
579 } 588 }
580 589
581 while (NULL != 590 while (NULL !=
582 fgets (hn, 591 fgets(hn,
583 sizeof (hn), 592 sizeof(hn),
584 stdin)) 593 stdin))
585 { 594 {
586 if (strlen(hn) > 0) 595 if (strlen(hn) > 0)
587 hn[strlen(hn)-1] = '\0'; /* eat newline */ 596 hn[strlen(hn) - 1] = '\0'; /* eat newline */
588 queue (hn); 597 queue(hn);
589 } 598 }
590 GNUNET_SCHEDULER_run (&run, 599 GNUNET_SCHEDULER_run(&run,
591 NULL); 600 NULL);
592 fprintf (stderr, 601 fprintf(stderr,
593 "Did %u lookups, found %u records, %u lookups failed, %u pending on shutdown\n", 602 "Did %u lookups, found %u records, %u lookups failed, %u pending on shutdown\n",
594 lookups, 603 lookups,
595 records, 604 records,
596 failures, 605 failures,
597 pending); 606 pending);
598 return 0; 607 return 0;
599} 608}
diff --git a/src/dns/plugin_block_dns.c b/src/dns/plugin_block_dns.c
index ed75ce2f3..fd4d844c5 100644
--- a/src/dns/plugin_block_dns.c
+++ b/src/dns/plugin_block_dns.c
@@ -11,12 +11,12 @@
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU Affero General Public License 15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20 20
21/** 21/**
22 * @file dns/plugin_block_dns.c 22 * @file dns/plugin_block_dns.c
@@ -54,37 +54,37 @@
54 * by this @a type of block (this is not an error) 54 * by this @a type of block (this is not an error)
55 */ 55 */
56static struct GNUNET_BLOCK_Group * 56static struct GNUNET_BLOCK_Group *
57block_plugin_dns_create_group (void *cls, 57block_plugin_dns_create_group(void *cls,
58 enum GNUNET_BLOCK_Type type, 58 enum GNUNET_BLOCK_Type type,
59 uint32_t nonce, 59 uint32_t nonce,
60 const void *raw_data, 60 const void *raw_data,
61 size_t raw_data_size, 61 size_t raw_data_size,
62 va_list va) 62 va_list va)
63{ 63{
64 unsigned int bf_size; 64 unsigned int bf_size;
65 const char *guard; 65 const char *guard;
66 66
67 guard = va_arg (va, const char *); 67 guard = va_arg(va, const char *);
68 if (0 == strcmp (guard, 68 if (0 == strcmp(guard,
69 "seen-set-size")) 69 "seen-set-size"))
70 bf_size = GNUNET_BLOCK_GROUP_compute_bloomfilter_size (va_arg (va, unsigned int), 70 bf_size = GNUNET_BLOCK_GROUP_compute_bloomfilter_size(va_arg(va, unsigned int),
71 BLOOMFILTER_K); 71 BLOOMFILTER_K);
72 else if (0 == strcmp (guard, 72 else if (0 == strcmp(guard,
73 "filter-size")) 73 "filter-size"))
74 bf_size = va_arg (va, unsigned int); 74 bf_size = va_arg(va, unsigned int);
75 else 75 else
76 { 76 {
77 GNUNET_break (0); 77 GNUNET_break(0);
78 bf_size = 8; 78 bf_size = 8;
79 } 79 }
80 GNUNET_break (NULL == va_arg (va, const char *)); 80 GNUNET_break(NULL == va_arg(va, const char *));
81 return GNUNET_BLOCK_GROUP_bf_create (cls, 81 return GNUNET_BLOCK_GROUP_bf_create(cls,
82 bf_size, 82 bf_size,
83 BLOOMFILTER_K, 83 BLOOMFILTER_K,
84 type, 84 type,
85 nonce, 85 nonce,
86 raw_data, 86 raw_data,
87 raw_data_size); 87 raw_data_size);
88} 88}
89 89
90 90
@@ -105,71 +105,72 @@ block_plugin_dns_create_group (void *cls,
105 * @return characterization of result 105 * @return characterization of result
106 */ 106 */
107static enum GNUNET_BLOCK_EvaluationResult 107static enum GNUNET_BLOCK_EvaluationResult
108block_plugin_dns_evaluate (void *cls, 108block_plugin_dns_evaluate(void *cls,
109 struct GNUNET_BLOCK_Context *ctx, 109 struct GNUNET_BLOCK_Context *ctx,
110 enum GNUNET_BLOCK_Type type, 110 enum GNUNET_BLOCK_Type type,
111 struct GNUNET_BLOCK_Group *bg, 111 struct GNUNET_BLOCK_Group *bg,
112 enum GNUNET_BLOCK_EvaluationOptions eo, 112 enum GNUNET_BLOCK_EvaluationOptions eo,
113 const struct GNUNET_HashCode * query, 113 const struct GNUNET_HashCode * query,
114 const void *xquery, 114 const void *xquery,
115 size_t xquery_size, 115 size_t xquery_size,
116 const void *reply_block, 116 const void *reply_block,
117 size_t reply_block_size) 117 size_t reply_block_size)
118{ 118{
119 const struct GNUNET_DNS_Advertisement *ad; 119 const struct GNUNET_DNS_Advertisement *ad;
120 struct GNUNET_HashCode phash; 120 struct GNUNET_HashCode phash;
121 121
122 switch (type) 122 switch (type)
123 {
124 case GNUNET_BLOCK_TYPE_DNS:
125 if (0 != xquery_size)
126 return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
127
128 if (NULL == reply_block)
129 return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
130
131 if (sizeof (struct GNUNET_DNS_Advertisement) != reply_block_size)
132 {
133 GNUNET_break_op (0);
134 return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
135 }
136 ad = reply_block;
137
138 if (ntohl (ad->purpose.size) !=
139 sizeof (struct GNUNET_DNS_Advertisement) -
140 sizeof (struct GNUNET_CRYPTO_EddsaSignature))
141 {
142 GNUNET_break_op (0);
143 return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
144 }
145 if (0 ==
146 GNUNET_TIME_absolute_get_remaining (GNUNET_TIME_absolute_ntoh
147 (ad->expiration_time)).rel_value_us)
148 {
149 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
150 "DNS advertisement has expired\n");
151 return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
152 }
153 if (GNUNET_OK !=
154 GNUNET_CRYPTO_eddsa_verify (GNUNET_SIGNATURE_PURPOSE_DNS_RECORD,
155 &ad->purpose,
156 &ad->signature,
157 &ad->peer.public_key))
158 { 123 {
159 GNUNET_break_op (0); 124 case GNUNET_BLOCK_TYPE_DNS:
160 return GNUNET_BLOCK_EVALUATION_RESULT_INVALID; 125 if (0 != xquery_size)
126 return GNUNET_BLOCK_EVALUATION_REQUEST_INVALID;
127
128 if (NULL == reply_block)
129 return GNUNET_BLOCK_EVALUATION_REQUEST_VALID;
130
131 if (sizeof(struct GNUNET_DNS_Advertisement) != reply_block_size)
132 {
133 GNUNET_break_op(0);
134 return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
135 }
136 ad = reply_block;
137
138 if (ntohl(ad->purpose.size) !=
139 sizeof(struct GNUNET_DNS_Advertisement) -
140 sizeof(struct GNUNET_CRYPTO_EddsaSignature))
141 {
142 GNUNET_break_op(0);
143 return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
144 }
145 if (0 ==
146 GNUNET_TIME_absolute_get_remaining(GNUNET_TIME_absolute_ntoh
147 (ad->expiration_time)).rel_value_us)
148 {
149 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
150 "DNS advertisement has expired\n");
151 return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
152 }
153 if (GNUNET_OK !=
154 GNUNET_CRYPTO_eddsa_verify(GNUNET_SIGNATURE_PURPOSE_DNS_RECORD,
155 &ad->purpose,
156 &ad->signature,
157 &ad->peer.public_key))
158 {
159 GNUNET_break_op(0);
160 return GNUNET_BLOCK_EVALUATION_RESULT_INVALID;
161 }
162 GNUNET_CRYPTO_hash(reply_block,
163 reply_block_size,
164 &phash);
165 if (GNUNET_YES ==
166 GNUNET_BLOCK_GROUP_bf_test_and_set(bg,
167 &phash))
168 return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE;
169 return GNUNET_BLOCK_EVALUATION_OK_MORE;
170
171 default:
172 return GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED;
161 } 173 }
162 GNUNET_CRYPTO_hash (reply_block,
163 reply_block_size,
164 &phash);
165 if (GNUNET_YES ==
166 GNUNET_BLOCK_GROUP_bf_test_and_set (bg,
167 &phash))
168 return GNUNET_BLOCK_EVALUATION_OK_DUPLICATE;
169 return GNUNET_BLOCK_EVALUATION_OK_MORE;
170 default:
171 return GNUNET_BLOCK_EVALUATION_TYPE_NOT_SUPPORTED;
172 }
173} 174}
174 175
175 176
@@ -185,11 +186,11 @@ block_plugin_dns_evaluate (void *cls,
185 * (or if extracting a key from a block of this type does not work) 186 * (or if extracting a key from a block of this type does not work)
186 */ 187 */
187static int 188static int
188block_plugin_dns_get_key (void *cls, 189block_plugin_dns_get_key(void *cls,
189 enum GNUNET_BLOCK_Type type, 190 enum GNUNET_BLOCK_Type type,
190 const void *block, 191 const void *block,
191 size_t block_size, 192 size_t block_size,
192 struct GNUNET_HashCode *key) 193 struct GNUNET_HashCode *key)
193{ 194{
194 /* we cannot extract a key from a block of this type */ 195 /* we cannot extract a key from a block of this type */
195 return GNUNET_SYSERR; 196 return GNUNET_SYSERR;
@@ -200,7 +201,7 @@ block_plugin_dns_get_key (void *cls,
200 * Entry point for the plugin. 201 * Entry point for the plugin.
201 */ 202 */
202void * 203void *
203libgnunet_plugin_block_dns_init (void *cls) 204libgnunet_plugin_block_dns_init(void *cls)
204{ 205{
205 static enum GNUNET_BLOCK_Type types[] = 206 static enum GNUNET_BLOCK_Type types[] =
206 { 207 {
@@ -209,7 +210,7 @@ libgnunet_plugin_block_dns_init (void *cls)
209 }; 210 };
210 struct GNUNET_BLOCK_PluginFunctions *api; 211 struct GNUNET_BLOCK_PluginFunctions *api;
211 212
212 api = GNUNET_new (struct GNUNET_BLOCK_PluginFunctions); 213 api = GNUNET_new(struct GNUNET_BLOCK_PluginFunctions);
213 api->evaluate = &block_plugin_dns_evaluate; 214 api->evaluate = &block_plugin_dns_evaluate;
214 api->get_key = &block_plugin_dns_get_key; 215 api->get_key = &block_plugin_dns_get_key;
215 api->create_group = &block_plugin_dns_create_group; 216 api->create_group = &block_plugin_dns_create_group;
@@ -222,11 +223,11 @@ libgnunet_plugin_block_dns_init (void *cls)
222 * Exit point from the plugin. 223 * Exit point from the plugin.
223 */ 224 */
224void * 225void *
225libgnunet_plugin_block_dns_done (void *cls) 226libgnunet_plugin_block_dns_done(void *cls)
226{ 227{
227 struct GNUNET_BLOCK_PluginFunctions *api = cls; 228 struct GNUNET_BLOCK_PluginFunctions *api = cls;
228 229
229 GNUNET_free (api); 230 GNUNET_free(api);
230 return NULL; 231 return NULL;
231} 232}
232 233