GNUnet  0.11.x
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 
91  enum GNUNET_DNS_Flags flags;
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 
201 static void
202 reconnect (void *cls)
203 {
204  struct GNUNET_DNS_Handle *dh = cls;
205  struct GNUNET_MQ_MessageHandler handlers[] = {
208  struct GNUNET_DNS_Request,
209  dh),
211  };
212  struct GNUNET_MQ_Envelope *env;
213  struct GNUNET_DNS_Register *msg;
214 
215  dh->reconnect_task = NULL;
216  dh->mq = GNUNET_CLIENT_connect (dh->cfg,
217  "dns",
218  handlers,
220  dh);
221  if (NULL == dh->mq)
222  return;
223  dh->generation++;
224  env = GNUNET_MQ_msg (msg,
226  msg->flags = htonl (dh->flags);
227  GNUNET_MQ_send (dh->mq,
228  env);
229 }
230 
231 
242 void
244 {
245  struct GNUNET_MQ_Envelope *env;
246  struct GNUNET_DNS_Response *resp;
247 
248  GNUNET_assert (0 < rh->dh->pending_requests--);
249  if (rh->generation != rh->dh->generation)
250  {
251  GNUNET_free (rh);
252  return;
253  }
254  env = GNUNET_MQ_msg (resp,
256  resp->drop_flag = htonl (1);
257  resp->request_id = rh->request_id;
258  GNUNET_MQ_send (rh->dh->mq,
259  env);
260  GNUNET_free (rh);
261 }
262 
263 
270 void
272 {
273  struct GNUNET_MQ_Envelope *env;
274  struct GNUNET_DNS_Response *resp;
275 
276  GNUNET_assert (0 < rh->dh->pending_requests--);
277  if (rh->generation != rh->dh->generation)
278  {
279  GNUNET_free (rh);
280  return;
281  }
282  env = GNUNET_MQ_msg (resp,
284  resp->request_id = rh->request_id;
285  resp->drop_flag = htonl (0);
286  GNUNET_MQ_send (rh->dh->mq,
287  env);
288  GNUNET_free (rh);
289 }
290 
291 
301 void
303  uint16_t reply_length,
304  const char *reply)
305 {
306  struct GNUNET_MQ_Envelope *env;
307  struct GNUNET_DNS_Response *resp;
308 
309  GNUNET_assert (0 < rh->dh->pending_requests--);
310  if (rh->generation != rh->dh->generation)
311  {
312  GNUNET_free (rh);
313  return;
314  }
315  if (reply_length + sizeof(struct GNUNET_DNS_Response)
317  {
318  GNUNET_break (0);
319  GNUNET_free (rh);
320  return;
321  }
322  env = GNUNET_MQ_msg_extra (resp,
323  reply_length,
325  resp->drop_flag = htonl (2);
326  resp->request_id = rh->request_id;
327  GNUNET_memcpy (&resp[1],
328  reply,
329  reply_length);
330  GNUNET_MQ_send (rh->dh->mq,
331  env);
332  GNUNET_free (rh);
333 }
334 
335 
345 struct GNUNET_DNS_Handle *
347  enum GNUNET_DNS_Flags flags,
349  void *rh_cls)
350 {
351  struct GNUNET_DNS_Handle *dh;
352 
353  dh = GNUNET_new (struct GNUNET_DNS_Handle);
354  dh->cfg = cfg;
355  dh->flags = flags;
356  dh->rh = rh;
357  dh->rh_cls = rh_cls;
359  return dh;
360 }
361 
362 
368 void
370 {
371  if (NULL != dh->mq)
372  {
373  GNUNET_MQ_destroy (dh->mq);
374  dh->mq = NULL;
375  }
376  if (NULL != dh->reconnect_task)
377  {
379  dh->reconnect_task = NULL;
380  }
381  /* make sure client has no pending requests left over! */
382  GNUNET_break (0 == dh->pending_requests);
383  GNUNET_free (dh);
384 }
385 
386 
387 /* end of dns_api.c */
uint32_t flags
NBO encoding of enum GNUNET_DNS_Flags for the client.
Definition: dns.h:45
static void reconnect(void *cls)
Reconnect to the DNS service.
Definition: dns_api.c:202
struct GNUNET_MessageHeader * msg
Definition: 005.c:2
static struct GNUNET_VPN_RedirectionRequest * request
Opaque redirection request handle.
Definition: gnunet-vpn.c:41
void * rh_cls
Closure for rh.
Definition: dns_api.c:76
static const struct GNUNET_CONFIGURATION_Handle * cfg
Configuration we are using.
Definition: gnunet-abd.c:36
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:1057
enum GNUNET_DNS_Flags flags
Flags for events we care about.
Definition: dns_api.c:91
GNUNET_MQ_Error
Error codes for the queue.
uint32_t generation
Re-connect counter, to make sure we did not reconnect in the meantime.
Definition: dns_api.c:49
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
uint32_t generation
Re-connect counter, to make sure we did not reconnect in the meantime.
Definition: dns_api.c:86
struct GNUNET_MQ_Handle * mq
Connection to DNS service, or NULL.
Definition: dns_api.c:61
#define GNUNET_TIME_UNIT_SECONDS
One second.
struct GNUNET_DNS_Handle * dh
Handle to DNS API.
Definition: dns_api.c:39
static void force_reconnect(struct GNUNET_DNS_Handle *dh)
Drop the existing connection and reconnect to the DNS service.
Definition: dns_api.c:117
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
#define GNUNET_MESSAGE_TYPE_DNS_CLIENT_REQUEST
Type of messages between the gnunet-helper-dns and the service.
#define GNUNET_MQ_msg(mvar, type)
Allocate a GNUNET_MQ_Envelope.
Definition: gnunet_mq_lib.h:67
Message from client to DNS service: here is my reply.
Definition: dns.h:76
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
#define GNUNET_new(type)
Allocate a struct or union of the given type.
uint16_t size
The length of the struct (in bytes, including the length field itself), in big-endian format...
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
struct GNUNET_SCHEDULER_Task * reconnect_task
Task to reconnect to the service.
Definition: dns_api.c:81
struct GNUNET_MessageHeader header
Header of type GNUNET_MESSAGE_TYPE_DNS_CLIENT_REQUEST.
Definition: dns.h:57
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 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
#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:52
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:1253
void GNUNET_DNS_disconnect(struct GNUNET_DNS_Handle *dh)
Disconnect from the DNS service.
Definition: dns_api.c:369
#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.
#define GNUNET_MQ_hd_var_size(name, code, str, ctx)
Message from client to DNS service to register itself.
Definition: dns.h:35
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:1280
unsigned int pending_requests
Number of GNUNET_DNS_RequestHandles we have outstanding.
Definition: dns_api.c:97
uint64_t request_id
Unique request ID.
Definition: dns.h:67
Message handler for a specific message type.
DNS handle.
Definition: dns_api.c:56
#define GNUNET_SYSERR
Definition: gnunet_common.h:76
uint64_t request_id
Stored in network byte order (as for us, it is just a random number).
Definition: dns_api.c:44
uint64_t request_id
Unique request ID.
Definition: dns.h:91
uint32_t reserved
Always zero.
Definition: dns.h:62
struct GNUNET_MQ_Envelope * env
Definition: 005.c:1
#define GNUNET_MAX_MESSAGE_SIZE
Largest supported message (to be precise, one byte more than the largest possible message...
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:346
Handle to a message queue.
Definition: mq.c:85
GNUNET_DNS_RequestHandler rh
Function to call to get replies.
Definition: dns_api.c:71
const struct GNUNET_CONFIGURATION_Handle * cfg
Configuration to use.
Definition: dns_api.c:66
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:302
Handle to identify an individual DNS request.
Definition: dns_api.c:34
configuration data
Definition: configuration.c:85
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:243
Message from DNS service to client: please handle a request.
Definition: dns.h:52
Entry in list of pending tasks.
Definition: scheduler.c:134
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:271
void GNUNET_MQ_destroy(struct GNUNET_MQ_Handle *mq)
Destroy the message queue.
Definition: mq.c:824
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:353
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
GNUNET_DNS_Flags
Flags that specify when to call the client&#39;s handler.
uint32_t drop_flag
Zero to drop, 1 for no change (no payload), 2 for update (message has payload).
Definition: dns.h:86
#define GNUNET_MQ_handler_end()
End-marker for the handlers array.
IPC messages between DNS API and DNS service.
#define GNUNET_free(ptr)
Wrapper around free.
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition: scheduler.c:966