aboutsummaryrefslogtreecommitdiff
path: root/src/dns
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2012-01-02 19:24:13 +0000
committerChristian Grothoff <christian@grothoff.org>2012-01-02 19:24:13 +0000
commit4c82e28a40c038caef74a1a08ab0df3257615c4b (patch)
tree1fc3aec695663cee40eedf2ae4501f1cfc08e9c8 /src/dns
parent19a49ce74a0b987b865e2ef15561e5fa71405029 (diff)
downloadgnunet-4c82e28a40c038caef74a1a08ab0df3257615c4b.tar.gz
gnunet-4c82e28a40c038caef74a1a08ab0df3257615c4b.zip
-moving DNS API into the new direction outlined in my last comment
Diffstat (limited to 'src/dns')
-rw-r--r--src/dns/dns_api_new.c60
-rw-r--r--src/dns/dns_new.h49
2 files changed, 31 insertions, 78 deletions
diff --git a/src/dns/dns_api_new.c b/src/dns/dns_api_new.c
index 0b654e139..a61676a47 100644
--- a/src/dns/dns_api_new.c
+++ b/src/dns/dns_api_new.c
@@ -224,8 +224,7 @@ request_handler (void *cls,
224 struct GNUNET_DNS_Handle *dh = cls; 224 struct GNUNET_DNS_Handle *dh = cls;
225 const struct GNUNET_DNS_Request *req; 225 const struct GNUNET_DNS_Request *req;
226 struct GNUNET_DNS_RequestHandle *rh; 226 struct GNUNET_DNS_RequestHandle *rh;
227 const char *name; 227 size_t payload_length;
228 uint16_t name_len;
229 228
230 /* the service disconnected, reconnect after short wait */ 229 /* the service disconnected, reconnect after short wait */
231 if (msg == NULL) 230 if (msg == NULL)
@@ -236,20 +235,18 @@ request_handler (void *cls,
236 &reconnect, dh); 235 &reconnect, dh);
237 return; 236 return;
238 } 237 }
239 /* the service did something strange, reconnect immediately */
240 req = (const struct GNUNET_DNS_Request *) msg;
241 name = (const char*) &req[1];
242
243 if ( (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_DNS_CLIENT_REQUEST) || 238 if ( (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_DNS_CLIENT_REQUEST) ||
244 (ntohs (msg->size) < sizeof (struct GNUNET_DNS_Request)) || 239 (ntohs (msg->size) < sizeof (struct GNUNET_DNS_Request)) )
245 (ntohs (msg->size) != sizeof (struct GNUNET_DNS_Request) + ntohs (req->rdata_length) + (name_len = ntohs (req->name_length))) ||
246 (name[name_len-1] != '\0') )
247 { 240 {
241 /* the service did something strange, reconnect immediately */
248 GNUNET_break (0); 242 GNUNET_break (0);
249 disconnect (dh); 243 disconnect (dh);
250 dh->reconnect_task = GNUNET_SCHEDULER_add_now (&reconnect, dh); 244 dh->reconnect_task = GNUNET_SCHEDULER_add_now (&reconnect, dh);
251 return; 245 return;
252 } 246 }
247 req = (const struct GNUNET_DNS_Request *) msg;
248 GNUNET_break (ntohl (req->reserved) == 0);
249 payload_length = ntohs (req->header.size) - sizeof (struct GNUNET_DNS_Request);
253 GNUNET_CLIENT_receive (dh->dns_connection, 250 GNUNET_CLIENT_receive (dh->dns_connection,
254 &request_handler, dh, 251 &request_handler, dh,
255 GNUNET_TIME_UNIT_FOREVER_REL); 252 GNUNET_TIME_UNIT_FOREVER_REL);
@@ -262,12 +259,8 @@ request_handler (void *cls,
262 dh->pending_requests++; 259 dh->pending_requests++;
263 dh->rh (dh->rh_cls, 260 dh->rh (dh->rh_cls,
264 rh, 261 rh,
265 name, 262 payload_length,
266 ntohs (req->dns_type), 263 (const char*) &req[1]);
267 ntohs (req->dns_class),
268 ntohl (req->dns_ttl),
269 ntohs (req->rdata_length),
270 &name[name_len]);
271} 264}
272 265
273 266
@@ -393,10 +386,8 @@ GNUNET_DNS_request_forward (struct GNUNET_DNS_RequestHandle *rh)
393 qe->msg = &resp->header; 386 qe->msg = &resp->header;
394 resp->header.size = htons (sizeof (struct GNUNET_DNS_Response)); 387 resp->header.size = htons (sizeof (struct GNUNET_DNS_Response));
395 resp->header.type = htons (GNUNET_MESSAGE_TYPE_DNS_CLIENT_RESPONSE); 388 resp->header.type = htons (GNUNET_MESSAGE_TYPE_DNS_CLIENT_RESPONSE);
396 resp->dns_ttl = htonl (0); 389 resp->drop_flag = htonl (1);
397 resp->request_id = rh->request_id; 390 resp->request_id = rh->request_id;
398 resp->drop_flag = htons (0);
399 resp->rdata_length = htons (0);
400 queue_reply (rh->dh, qe); 391 queue_reply (rh->dh, qe);
401 GNUNET_free (rh); 392 GNUNET_free (rh);
402} 393}
@@ -426,29 +417,26 @@ GNUNET_DNS_request_drop (struct GNUNET_DNS_RequestHandle *rh)
426 qe->msg = &resp->header; 417 qe->msg = &resp->header;
427 resp->header.size = htons (sizeof (struct GNUNET_DNS_Response)); 418 resp->header.size = htons (sizeof (struct GNUNET_DNS_Response));
428 resp->header.type = htons (GNUNET_MESSAGE_TYPE_DNS_CLIENT_RESPONSE); 419 resp->header.type = htons (GNUNET_MESSAGE_TYPE_DNS_CLIENT_RESPONSE);
429 resp->dns_ttl = htonl (0);
430 resp->request_id = rh->request_id; 420 resp->request_id = rh->request_id;
431 resp->drop_flag = htons (1); 421 resp->drop_flag = htonl (0);
432 resp->rdata_length = htons (0);
433 queue_reply (rh->dh, qe); 422 queue_reply (rh->dh, qe);
434 GNUNET_free (rh); 423 GNUNET_free (rh);
435} 424}
436 425
437 426
438/** 427/**
439 * If a GNUNET_DNS_RequestHandler calls this function, the request is supposed to 428 * If a GNUNET_DNS_RequestHandler calls this function, the request is
440 * be answered with the data provided to this call (with the modifications the function might have made). 429 * supposed to be answered with the data provided to this call (with
430 * the modifications the function might have made).
441 * 431 *
442 * @param rh request that should now be answered 432 * @param rh request that should now be answered
443 * @param dns_ttl seconds that the RR stays valid, can be updated 433 * @param reply_length size of reply (uint16_t to force sane size)
444 * @param rdata_length size of rdata, can be updated 434 * @param reply reply data
445 * @param rdata record data, can be updated
446 */ 435 */
447void 436void
448GNUNET_DNS_request_answer (struct GNUNET_DNS_RequestHandle *rh, 437GNUNET_DNS_request_answer (struct GNUNET_DNS_RequestHandle *rh,
449 uint32_t dns_ttl, 438 uint16_t reply_length,
450 uint16_t rdata_length, 439 const char *reply)
451 char *rdata)
452{ 440{
453 struct ReplyQueueEntry *qe; 441 struct ReplyQueueEntry *qe;
454 struct GNUNET_DNS_Response *resp; 442 struct GNUNET_DNS_Response *resp;
@@ -459,23 +447,21 @@ GNUNET_DNS_request_answer (struct GNUNET_DNS_RequestHandle *rh,
459 GNUNET_free (rh); 447 GNUNET_free (rh);
460 return; 448 return;
461 } 449 }
462 if (rdata_length + sizeof (struct GNUNET_DNS_Response) >= GNUNET_SERVER_MAX_MESSAGE_SIZE) 450 if (reply_length + sizeof (struct GNUNET_DNS_Response) >= GNUNET_SERVER_MAX_MESSAGE_SIZE)
463 { 451 {
464 GNUNET_break (0); 452 GNUNET_break (0);
465 GNUNET_free (rh); 453 GNUNET_free (rh);
466 return; 454 return;
467 } 455 }
468 qe = GNUNET_malloc (sizeof (struct ReplyQueueEntry) + 456 qe = GNUNET_malloc (sizeof (struct ReplyQueueEntry) +
469 sizeof (struct GNUNET_DNS_Response) + rdata_length); 457 sizeof (struct GNUNET_DNS_Response) + reply_length);
470 resp = (struct GNUNET_DNS_Response*) &qe[1]; 458 resp = (struct GNUNET_DNS_Response*) &qe[1];
471 qe->msg = &resp->header; 459 qe->msg = &resp->header;
472 resp->header.size = htons (sizeof (struct GNUNET_DNS_Response) + rdata_length); 460 resp->header.size = htons (sizeof (struct GNUNET_DNS_Response) + reply_length);
473 resp->header.type = htons (GNUNET_MESSAGE_TYPE_DNS_CLIENT_RESPONSE); 461 resp->header.type = htons (GNUNET_MESSAGE_TYPE_DNS_CLIENT_RESPONSE);
474 resp->dns_ttl = htonl (dns_ttl); 462 resp->drop_flag = htons (2);
475 resp->request_id = rh->request_id; 463 resp->request_id = rh->request_id;
476 resp->drop_flag = htons (0); 464 memcpy (&resp[1], reply, reply_length);
477 resp->rdata_length = htons (rdata_length);
478 memcpy (&resp[1], rdata, rdata_length);
479 queue_reply (rh->dh, qe); 465 queue_reply (rh->dh, qe);
480 GNUNET_free (rh); 466 GNUNET_free (rh);
481} 467}
diff --git a/src/dns/dns_new.h b/src/dns/dns_new.h
index 4f3493db2..7cf3a316a 100644
--- a/src/dns/dns_new.h
+++ b/src/dns/dns_new.h
@@ -39,38 +39,16 @@ struct GNUNET_DNS_Request
39 struct GNUNET_MessageHeader header; 39 struct GNUNET_MessageHeader header;
40 40
41 /** 41 /**
42 * A DNS type (GNUNET_DNS_TYPE_*) 42 * Always zero.
43 */ 43 */
44 uint16_t dns_type GNUNET_PACKED; 44 uint32_t reserved GNUNET_PACKED;
45 45
46 /** 46 /**
47 * A DNS class (usually 1).
48 */
49 uint16_t dns_class GNUNET_PACKED;
50
51 /**
52 * Unique request ID. 47 * Unique request ID.
53 */ 48 */
54 uint64_t request_id GNUNET_PACKED; 49 uint64_t request_id GNUNET_PACKED;
55 50
56 /** 51 /* followed by original DNS request (without UDP header) */
57 * TTL if rdata is present, otherwise 0.
58 */
59 uint32_t dns_ttl GNUNET_PACKED;
60
61 /**
62 * Number of bytes of rdata that follow at the end.
63 */
64 uint16_t rdata_length GNUNET_PACKED;
65
66 /**
67 * Number of bytes of the name that follow right now (including 0-termination).
68 */
69 uint16_t name_length GNUNET_PACKED;
70
71 /* followed by char name[name_length] */
72
73 /* followed by char rdata[rdata_length] */
74 52
75}; 53};
76 54
@@ -86,27 +64,16 @@ struct GNUNET_DNS_Response
86 struct GNUNET_MessageHeader header; 64 struct GNUNET_MessageHeader header;
87 65
88 /** 66 /**
89 * TTL if rdata is present, otherwise 0. 67 * Zero to drop, 1 for no change (no payload), 2 for update (message has payload).
90 */ 68 */
91 uint32_t dns_ttl GNUNET_PACKED; 69 uint32_t drop_flag GNUNET_PACKED;
92 70
93 /** 71 /**
94 * Unique request ID, matches the original request. 72 * Unique request ID.
95 */ 73 */
96 uint64_t request_id GNUNET_PACKED; 74 uint64_t request_id GNUNET_PACKED;
97 75
98 /** 76 /* followed by original DNS request (without UDP header) */
99 * 1 to drop request, 0 to forward if there is no response
100 * or to answer if there is a response.
101 */
102 uint16_t drop_flag GNUNET_PACKED;
103
104 /**
105 * Number of bytes of rdata that follow at the end.
106 */
107 uint16_t rdata_length GNUNET_PACKED;
108
109 /* followed by char rdata[rdata_length] */
110 77
111}; 78};
112 79