2 This file is part of GNUnet
3 Copyright (C) 2012, 2016 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
23 * @brief API to access the DNS service.
24 * @author Christian Grothoff
27 #include "gnunet_dns_service.h"
32 * Handle to identify an individual DNS request.
34 struct GNUNET_DNS_RequestHandle
39 struct GNUNET_DNS_Handle *dh;
42 * Stored in network byte order (as for us, it is just a random number).
47 * Re-connect counter, to make sure we did not reconnect in the meantime.
56 struct GNUNET_DNS_Handle
59 * Connection to DNS service, or NULL.
61 struct GNUNET_MQ_Handle *mq;
64 * Configuration to use.
66 const struct GNUNET_CONFIGURATION_Handle *cfg;
69 * Function to call to get replies.
71 GNUNET_DNS_RequestHandler rh;
79 * Task to reconnect to the service.
81 struct GNUNET_SCHEDULER_Task *reconnect_task;
84 * Re-connect counter, to make sure we did not reconnect in the meantime.
89 * Flags for events we care about.
91 enum GNUNET_DNS_Flags flags;
94 * Number of GNUNET_DNS_RequestHandles we have outstanding. Must be 0 before
95 * we can be disconnected.
97 unsigned int pending_requests;
102 * Reconnect to the DNS service.
104 * @param cls handle with the connection to connect
105 * @param tc scheduler context (unused)
108 reconnect (void *cls);
112 * Drop the existing connection and reconnect to the DNS service.
114 * @param dh handle with the connection
117 force_reconnect (struct GNUNET_DNS_Handle *dh)
121 GNUNET_MQ_destroy (dh->mq);
125 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
132 * Generic error handler, called with the appropriate error code and
133 * the same closure specified at the creation of the message queue.
134 * Not every message queue implementation supports an error handler.
136 * @param cls closure with the `struct GNUNET_DNS_Handle *`
137 * @param error error code
140 mq_error_handler (void *cls,
141 enum GNUNET_MQ_Error error)
143 struct GNUNET_DNS_Handle *dh = cls;
145 force_reconnect (dh);
150 * This receives packets from the DNS service and calls the application to
151 * check that the request is well-formed
153 * @param cls the struct GNUNET_DNS_Handle
154 * @param req message from the service (request)
157 check_request (void *cls,
158 const struct GNUNET_DNS_Request *req)
160 if (0 != ntohl (req->reserved))
163 return GNUNET_SYSERR;
170 * This receives packets from the DNS service and calls the application to
173 * @param cls the `struct GNUNET_DNS_Handle *`
174 * @param msg message from the service (request)
177 handle_request (void *cls,
178 const struct GNUNET_DNS_Request *req)
180 struct GNUNET_DNS_Handle *dh = cls;
181 size_t payload_length = ntohs (req->header.size) - sizeof(*req);
182 struct GNUNET_DNS_RequestHandle *rh;
184 rh = GNUNET_new (struct GNUNET_DNS_RequestHandle);
186 rh->request_id = req->request_id;
187 rh->generation = dh->generation;
188 dh->pending_requests++;
192 (const char *) &req[1]);
197 * Reconnect to the DNS service.
199 * @param cls handle with the connection to connect
202 reconnect (void *cls)
204 struct GNUNET_DNS_Handle *dh = cls;
205 struct GNUNET_MQ_MessageHandler handlers[] = {
206 GNUNET_MQ_hd_var_size (request,
207 GNUNET_MESSAGE_TYPE_DNS_CLIENT_REQUEST,
208 struct GNUNET_DNS_Request,
210 GNUNET_MQ_handler_end ()
212 struct GNUNET_MQ_Envelope *env;
213 struct GNUNET_DNS_Register *msg;
215 dh->reconnect_task = NULL;
216 dh->mq = GNUNET_CLIENT_connect (dh->cfg,
224 env = GNUNET_MQ_msg (msg,
225 GNUNET_MESSAGE_TYPE_DNS_CLIENT_INIT);
226 msg->flags = htonl (dh->flags);
227 GNUNET_MQ_send (dh->mq,
233 * If a GNUNET_DNS_RequestHandler calls this function, the request is
234 * given to other clients or the global DNS for resolution. Once a
235 * global response has been obtained, the request handler is AGAIN
236 * called to give it a chance to observe and modify the response after
237 * the "normal" resolution. It is not legal for the request handler
238 * to call this function if a response is already present.
240 * @param rh request that should now be forwarded
243 GNUNET_DNS_request_forward (struct GNUNET_DNS_RequestHandle *rh)
245 struct GNUNET_MQ_Envelope *env;
246 struct GNUNET_DNS_Response *resp;
248 GNUNET_assert (0 < rh->dh->pending_requests--);
249 if (rh->generation != rh->dh->generation)
254 env = GNUNET_MQ_msg (resp,
255 GNUNET_MESSAGE_TYPE_DNS_CLIENT_RESPONSE);
256 resp->drop_flag = htonl (1);
257 resp->request_id = rh->request_id;
258 GNUNET_MQ_send (rh->dh->mq,
265 * If a GNUNET_DNS_RequestHandler calls this function, the request is
266 * to be dropped and no response should be generated.
268 * @param rh request that should now be dropped
271 GNUNET_DNS_request_drop (struct GNUNET_DNS_RequestHandle *rh)
273 struct GNUNET_MQ_Envelope *env;
274 struct GNUNET_DNS_Response *resp;
276 GNUNET_assert (0 < rh->dh->pending_requests--);
277 if (rh->generation != rh->dh->generation)
282 env = GNUNET_MQ_msg (resp,
283 GNUNET_MESSAGE_TYPE_DNS_CLIENT_RESPONSE);
284 resp->request_id = rh->request_id;
285 resp->drop_flag = htonl (0);
286 GNUNET_MQ_send (rh->dh->mq,
293 * If a GNUNET_DNS_RequestHandler calls this function, the request is
294 * supposed to be answered with the data provided to this call (with
295 * the modifications the function might have made).
297 * @param rh request that should now be answered
298 * @param reply_length size of @a reply (uint16_t to force sane size)
299 * @param reply reply data
302 GNUNET_DNS_request_answer (struct GNUNET_DNS_RequestHandle *rh,
303 uint16_t reply_length,
306 struct GNUNET_MQ_Envelope *env;
307 struct GNUNET_DNS_Response *resp;
309 GNUNET_assert (0 < rh->dh->pending_requests--);
310 if (rh->generation != rh->dh->generation)
315 if (reply_length + sizeof(struct GNUNET_DNS_Response)
316 >= GNUNET_MAX_MESSAGE_SIZE)
322 env = GNUNET_MQ_msg_extra (resp,
324 GNUNET_MESSAGE_TYPE_DNS_CLIENT_RESPONSE);
325 resp->drop_flag = htonl (2);
326 resp->request_id = rh->request_id;
327 GNUNET_memcpy (&resp[1],
330 GNUNET_MQ_send (rh->dh->mq,
337 * Connect to the service-dns
339 * @param cfg configuration to use
340 * @param flags when to call @a rh
341 * @param rh function to call with DNS requests
342 * @param rh_cls closure to pass to @a rh
345 struct GNUNET_DNS_Handle *
346 GNUNET_DNS_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
347 enum GNUNET_DNS_Flags flags,
348 GNUNET_DNS_RequestHandler rh,
351 struct GNUNET_DNS_Handle *dh;
353 dh = GNUNET_new (struct GNUNET_DNS_Handle);
358 dh->reconnect_task = GNUNET_SCHEDULER_add_now (&reconnect, dh);
364 * Disconnect from the DNS service.
366 * @param dh DNS handle
369 GNUNET_DNS_disconnect (struct GNUNET_DNS_Handle *dh)
373 GNUNET_MQ_destroy (dh->mq);
376 if (NULL != dh->reconnect_task)
378 GNUNET_SCHEDULER_cancel (dh->reconnect_task);
379 dh->reconnect_task = NULL;
381 /* make sure client has no pending requests left over! */
382 GNUNET_break (0 == dh->pending_requests);
387 /* end of dns_api.c */