GNUnet  0.17.5
dns_api.c
Go to the documentation of this file.
1 /*
2  This file is part of GNUnet
3  Copyright (C) 2012, 2016 GNUnet e.V.
4 
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.
9 
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.
14 
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/>.
17 
18  SPDX-License-Identifier: AGPL3.0-or-later
19  */
20 
26 #include "platform.h"
27 #include "gnunet_dns_service.h"
28 #include "dns.h"
29 
30 
35 {
40 
44  uint64_t request_id;
45 
49  uint32_t generation;
50 };
51 
52 
57 {
62 
67 
72 
76  void *rh_cls;
77 
82 
86  uint32_t generation;
87 
92 
97  unsigned int pending_requests;
98 };
99 
100 
107 static void
108 reconnect (void *cls);
109 
110 
116 static void
118 {
119  if (NULL != dh->mq)
120  {
121  GNUNET_MQ_destroy (dh->mq);
122  dh->mq = NULL;
123  }
124  dh->reconnect_task =
126  &reconnect,
127  dh);
128 }
129 
130 
139 static void
140 mq_error_handler (void *cls,
141  enum GNUNET_MQ_Error error)
142 {
143  struct GNUNET_DNS_Handle *dh = cls;
144 
145  force_reconnect (dh);
146 }
147 
148 
156 static int
157 check_request (void *cls,
158  const struct GNUNET_DNS_Request *req)
159 {
160  if (0 != ntohl (req->reserved))
161  {
162  GNUNET_break (0);
163  return GNUNET_SYSERR;
164  }
165  return GNUNET_OK;
166 }
167 
168 
176 static void
177 handle_request (void *cls,
178  const struct GNUNET_DNS_Request *req)
179 {
180  struct GNUNET_DNS_Handle *dh = cls;
181  size_t payload_length = ntohs (req->header.size) - sizeof(*req);
182  struct GNUNET_DNS_RequestHandle *rh;
183 
184  rh = GNUNET_new (struct GNUNET_DNS_RequestHandle);
185  rh->dh = dh;
186  rh->request_id = req->request_id;
187  rh->generation = dh->generation;
188  dh->pending_requests++;
189  dh->rh (dh->rh_cls,
190  rh,
191  payload_length,
192  (const char *) &req[1]);
193 }
194 
195 
196 static void
197 reconnect (void *cls)
198 {
199  struct GNUNET_DNS_Handle *dh = cls;
203  struct GNUNET_DNS_Request,
204  dh),
206  };
207  struct GNUNET_MQ_Envelope *env;
208  struct GNUNET_DNS_Register *msg;
209 
210  dh->reconnect_task = NULL;
211  dh->mq = GNUNET_CLIENT_connect (dh->cfg,
212  "dns",
213  handlers,
215  dh);
216  if (NULL == dh->mq)
217  return;
218  dh->generation++;
219  env = GNUNET_MQ_msg (msg,
221  msg->flags = htonl (dh->flags);
222  GNUNET_MQ_send (dh->mq,
223  env);
224 }
225 
226 
237 void
239 {
240  struct GNUNET_MQ_Envelope *env;
241  struct GNUNET_DNS_Response *resp;
242 
243  GNUNET_assert (0 < rh->dh->pending_requests--);
244  if (rh->generation != rh->dh->generation)
245  {
246  GNUNET_free (rh);
247  return;
248  }
249  env = GNUNET_MQ_msg (resp,
251  resp->drop_flag = htonl (1);
252  resp->request_id = rh->request_id;
253  GNUNET_MQ_send (rh->dh->mq,
254  env);
255  GNUNET_free (rh);
256 }
257 
258 
265 void
267 {
268  struct GNUNET_MQ_Envelope *env;
269  struct GNUNET_DNS_Response *resp;
270 
271  GNUNET_assert (0 < rh->dh->pending_requests--);
272  if (rh->generation != rh->dh->generation)
273  {
274  GNUNET_free (rh);
275  return;
276  }
277  env = GNUNET_MQ_msg (resp,
279  resp->request_id = rh->request_id;
280  resp->drop_flag = htonl (0);
281  GNUNET_MQ_send (rh->dh->mq,
282  env);
283  GNUNET_free (rh);
284 }
285 
286 
296 void
298  uint16_t reply_length,
299  const char *reply)
300 {
301  struct GNUNET_MQ_Envelope *env;
302  struct GNUNET_DNS_Response *resp;
303 
304  GNUNET_assert (0 < rh->dh->pending_requests--);
305  if (rh->generation != rh->dh->generation)
306  {
307  GNUNET_free (rh);
308  return;
309  }
310  if (reply_length + sizeof(struct GNUNET_DNS_Response)
312  {
313  GNUNET_break (0);
314  GNUNET_free (rh);
315  return;
316  }
317  env = GNUNET_MQ_msg_extra (resp,
318  reply_length,
320  resp->drop_flag = htonl (2);
321  resp->request_id = rh->request_id;
322  GNUNET_memcpy (&resp[1],
323  reply,
324  reply_length);
325  GNUNET_MQ_send (rh->dh->mq,
326  env);
327  GNUNET_free (rh);
328 }
329 
330 
340 struct GNUNET_DNS_Handle *
342  enum GNUNET_DNS_Flags flags,
344  void *rh_cls)
345 {
346  struct GNUNET_DNS_Handle *dh;
347 
348  dh = GNUNET_new (struct GNUNET_DNS_Handle);
349  dh->cfg = cfg;
350  dh->flags = flags;
351  dh->rh = rh;
352  dh->rh_cls = rh_cls;
354  return dh;
355 }
356 
357 
363 void
365 {
366  if (NULL != dh->mq)
367  {
368  GNUNET_MQ_destroy (dh->mq);
369  dh->mq = NULL;
370  }
371  if (NULL != dh->reconnect_task)
372  {
374  dh->reconnect_task = NULL;
375  }
376  /* make sure client has no pending requests left over! */
377  GNUNET_break (0 == dh->pending_requests);
378  GNUNET_free (dh);
379 }
380 
381 
382 /* end of dns_api.c */
struct GNUNET_MessageHeader * msg
Definition: 005.c:2
struct GNUNET_MQ_Envelope * env
Definition: 005.c:1
IPC messages between DNS API and DNS service.
static void force_reconnect(struct GNUNET_DNS_Handle *dh)
Drop the existing connection and reconnect to the DNS service.
Definition: dns_api.c:117
static void mq_error_handler(void *cls, enum GNUNET_MQ_Error error)
Generic error handler, called with the appropriate error code and the same closure specified at the c...
Definition: dns_api.c:140
static void reconnect(void *cls)
Reconnect to the DNS service.
Definition: dns_api.c:197
static int check_request(void *cls, const struct GNUNET_DNS_Request *req)
This receives packets from the DNS service and calls the application to check that the request is wel...
Definition: dns_api.c:157
static void handle_request(void *cls, const struct GNUNET_DNS_Request *req)
This receives packets from the DNS service and calls the application to handle it.
Definition: dns_api.c:177
static const struct GNUNET_CONFIGURATION_Handle * cfg
Configuration we are using.
Definition: gnunet-abd.c:36
static struct GNUNET_CADET_MessageHandler handlers[]
Handlers, for diverse services.
static struct GNUNET_VPN_RedirectionRequest * request
Opaque redirection request handle.
Definition: gnunet-vpn.c:41
API to access the DNS service.
#define GNUNET_MAX_MESSAGE_SIZE
Largest supported message (to be precise, one byte more than the largest possible message,...
struct GNUNET_MQ_Handle * GNUNET_CLIENT_connect(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *service_name, const struct GNUNET_MQ_MessageHandler *handlers, GNUNET_MQ_ErrorHandler error_handler, void *error_handler_cls)
Create a message queue to connect to a GNUnet service.
Definition: client.c:1055
void(* GNUNET_DNS_RequestHandler)(void *cls, struct GNUNET_DNS_RequestHandle *rh, size_t request_length, const char *request)
Signature of a function that is called whenever the DNS service encounters a DNS request and needs to...
struct GNUNET_DNS_Handle * GNUNET_DNS_connect(const struct GNUNET_CONFIGURATION_Handle *cfg, enum GNUNET_DNS_Flags flags, GNUNET_DNS_RequestHandler rh, void *rh_cls)
Connect to the service-dns.
Definition: dns_api.c:341
void GNUNET_DNS_disconnect(struct GNUNET_DNS_Handle *dh)
Disconnect from the DNS service.
Definition: dns_api.c:364
void GNUNET_DNS_request_forward(struct GNUNET_DNS_RequestHandle *rh)
If a GNUNET_DNS_RequestHandler calls this function, the request is given to other clients or the glob...
Definition: dns_api.c:238
void GNUNET_DNS_request_answer(struct GNUNET_DNS_RequestHandle *rh, uint16_t reply_length, const char *reply)
If a GNUNET_DNS_RequestHandler calls this function, the request is supposed to be answered with the d...
Definition: dns_api.c:297
void GNUNET_DNS_request_drop(struct GNUNET_DNS_RequestHandle *rh)
If a GNUNET_DNS_RequestHandler calls this function, the request is to be dropped and no response shou...
Definition: dns_api.c:266
GNUNET_DNS_Flags
Flags that specify when to call the client's handler.
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
@ GNUNET_OK
Definition: gnunet_common.h:99
@ GNUNET_SYSERR
Definition: gnunet_common.h:97
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define GNUNET_free(ptr)
Wrapper around free.
GNUNET_MQ_Error
Error codes for the queue.
void GNUNET_MQ_send(struct GNUNET_MQ_Handle *mq, struct GNUNET_MQ_Envelope *ev)
Send a message with the given message queue.
Definition: mq.c:302
#define GNUNET_MQ_handler_end()
End-marker for the handlers array.
#define GNUNET_MQ_msg_extra(mvar, esize, type)
Allocate an envelope, with extra space allocated after the space needed by the message struct.
Definition: gnunet_mq_lib.h:56
#define GNUNET_MQ_msg(mvar, type)
Allocate a GNUNET_MQ_Envelope.
Definition: gnunet_mq_lib.h:71
#define GNUNET_MQ_hd_var_size(name, code, str, ctx)
void GNUNET_MQ_destroy(struct GNUNET_MQ_Handle *mq)
Destroy the message queue.
Definition: mq.c:685
#define GNUNET_MESSAGE_TYPE_DNS_CLIENT_REQUEST
Type of messages between the gnunet-helper-dns and the service.
#define GNUNET_MESSAGE_TYPE_DNS_CLIENT_RESPONSE
Type of messages between the gnunet-helper-dns and the service.
#define GNUNET_MESSAGE_TYPE_DNS_CLIENT_INIT
Initial message from client to DNS service for registration.
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_now(GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run as soon as possible.
Definition: scheduler.c:1281
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition: scheduler.c:957
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_delayed(struct GNUNET_TIME_Relative delay, GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run with a specified delay.
Definition: scheduler.c:1254
#define GNUNET_TIME_UNIT_SECONDS
One second.
DNS handle.
Definition: dns_api.c:57
struct GNUNET_SCHEDULER_Task * reconnect_task
Task to reconnect to the service.
Definition: dns_api.c:81
GNUNET_DNS_RequestHandler rh
Function to call to get replies.
Definition: dns_api.c:71
enum GNUNET_DNS_Flags flags
Flags for events we care about.
Definition: dns_api.c:91
const struct GNUNET_CONFIGURATION_Handle * cfg
Configuration to use.
Definition: dns_api.c:66
struct GNUNET_MQ_Handle * mq
Connection to DNS service, or NULL.
Definition: dns_api.c:61
uint32_t generation
Re-connect counter, to make sure we did not reconnect in the meantime.
Definition: dns_api.c:86
unsigned int pending_requests
Number of GNUNET_DNS_RequestHandles we have outstanding.
Definition: dns_api.c:97
void * rh_cls
Closure for rh.
Definition: dns_api.c:76
Message from client to DNS service to register itself.
Definition: dns.h:36
Handle to identify an individual DNS request.
Definition: dns_api.c:35
struct GNUNET_DNS_Handle * dh
Handle to DNS API.
Definition: dns_api.c:39
uint32_t generation
Re-connect counter, to make sure we did not reconnect in the meantime.
Definition: dns_api.c:49
uint64_t request_id
Stored in network byte order (as for us, it is just a random number).
Definition: dns_api.c:44
Message from DNS service to client: please handle a request.
Definition: dns.h:53
uint32_t reserved
Always zero.
Definition: dns.h:62
uint64_t request_id
Unique request ID.
Definition: dns.h:67
struct GNUNET_MessageHeader header
Header of type GNUNET_MESSAGE_TYPE_DNS_CLIENT_REQUEST.
Definition: dns.h:57
Message from client to DNS service: here is my reply.
Definition: dns.h:77
uint32_t drop_flag
Zero to drop, 1 for no change (no payload), 2 for update (message has payload).
Definition: dns.h:86
uint64_t request_id
Unique request ID.
Definition: dns.h:91
Handle to a message queue.
Definition: mq.c:86
Message handler for a specific message type.
uint16_t size
The length of the struct (in bytes, including the length field itself), in big-endian format.
Entry in list of pending tasks.
Definition: scheduler.c:135