GNUnet  0.10.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 {
36 
41 
45  uint64_t request_id;
46 
50  uint32_t generation;
51 
52 };
53 
54 
59 {
60 
65 
70 
75 
79  void *rh_cls;
80 
85 
89  uint32_t generation;
90 
94  enum GNUNET_DNS_Flags flags;
95 
100  unsigned int pending_requests;
101 };
102 
103 
110 static void
111 reconnect (void *cls);
112 
113 
119 static void
121 {
122  if (NULL != dh->mq)
123  {
124  GNUNET_MQ_destroy (dh->mq);
125  dh->mq = NULL;
126  }
127  dh->reconnect_task =
129  &reconnect,
130  dh);
131 }
132 
133 
142 static void
143 mq_error_handler (void *cls,
144  enum GNUNET_MQ_Error error)
145 {
146  struct GNUNET_DNS_Handle *dh = cls;
147 
148  force_reconnect (dh);
149 }
150 
151 
152 
160 static int
161 check_request (void *cls,
162  const struct GNUNET_DNS_Request *req)
163 {
164  if (0 != ntohl (req->reserved))
165  {
166  GNUNET_break (0);
167  return GNUNET_SYSERR;
168  }
169  return GNUNET_OK;
170 }
171 
172 
180 static void
181 handle_request (void *cls,
182  const struct GNUNET_DNS_Request *req)
183 {
184  struct GNUNET_DNS_Handle *dh = cls;
185  size_t payload_length = ntohs (req->header.size) - sizeof (*req);
186  struct GNUNET_DNS_RequestHandle *rh;
187 
188  rh = GNUNET_new (struct GNUNET_DNS_RequestHandle);
189  rh->dh =dh;
190  rh->request_id = req->request_id;
191  rh->generation = dh->generation;
192  dh->pending_requests++;
193  dh->rh (dh->rh_cls,
194  rh,
195  payload_length,
196  (const char*) &req[1]);
197 }
198 
199 
205 static void
206 reconnect (void *cls)
207 {
208  struct GNUNET_DNS_Handle *dh = cls;
209  struct GNUNET_MQ_MessageHandler handlers[] = {
212  struct GNUNET_DNS_Request,
213  dh),
215  };
216  struct GNUNET_MQ_Envelope *env;
217  struct GNUNET_DNS_Register *msg;
218 
219  dh->reconnect_task = NULL;
220  dh->mq = GNUNET_CLIENT_connect (dh->cfg,
221  "dns",
222  handlers,
224  dh);
225  if (NULL == dh->mq)
226  return;
227  dh->generation++;
228  env = GNUNET_MQ_msg (msg,
230  msg->flags = htonl (dh->flags);
231  GNUNET_MQ_send (dh->mq,
232  env);
233 }
234 
235 
246 void
248 {
249  struct GNUNET_MQ_Envelope *env;
250  struct GNUNET_DNS_Response *resp;
251 
252  GNUNET_assert (0 < rh->dh->pending_requests--);
253  if (rh->generation != rh->dh->generation)
254  {
255  GNUNET_free (rh);
256  return;
257  }
258  env = GNUNET_MQ_msg (resp,
260  resp->drop_flag = htonl (1);
261  resp->request_id = rh->request_id;
262  GNUNET_MQ_send (rh->dh->mq,
263  env);
264  GNUNET_free (rh);
265 }
266 
267 
274 void
276 {
277  struct GNUNET_MQ_Envelope *env;
278  struct GNUNET_DNS_Response *resp;
279 
280  GNUNET_assert (0 < rh->dh->pending_requests--);
281  if (rh->generation != rh->dh->generation)
282  {
283  GNUNET_free (rh);
284  return;
285  }
286  env = GNUNET_MQ_msg (resp,
288  resp->request_id = rh->request_id;
289  resp->drop_flag = htonl (0);
290  GNUNET_MQ_send (rh->dh->mq,
291  env);
292  GNUNET_free (rh);
293 }
294 
295 
305 void
307  uint16_t reply_length,
308  const char *reply)
309 {
310  struct GNUNET_MQ_Envelope *env;
311  struct GNUNET_DNS_Response *resp;
312 
313  GNUNET_assert (0 < rh->dh->pending_requests--);
314  if (rh->generation != rh->dh->generation)
315  {
316  GNUNET_free (rh);
317  return;
318  }
319  if (reply_length + sizeof (struct GNUNET_DNS_Response)
321  {
322  GNUNET_break (0);
323  GNUNET_free (rh);
324  return;
325  }
326  env = GNUNET_MQ_msg_extra (resp,
327  reply_length,
329  resp->drop_flag = htonl (2);
330  resp->request_id = rh->request_id;
331  GNUNET_memcpy (&resp[1],
332  reply,
333  reply_length);
334  GNUNET_MQ_send (rh->dh->mq,
335  env);
336  GNUNET_free (rh);
337 }
338 
339 
349 struct GNUNET_DNS_Handle *
351  enum GNUNET_DNS_Flags flags,
353  void *rh_cls)
354 {
355  struct GNUNET_DNS_Handle *dh;
356 
357  dh = GNUNET_new (struct GNUNET_DNS_Handle);
358  dh->cfg = cfg;
359  dh->flags = flags;
360  dh->rh = rh;
361  dh->rh_cls = rh_cls;
363  return dh;
364 }
365 
366 
372 void
374 {
375  if (NULL != dh->mq)
376  {
377  GNUNET_MQ_destroy (dh->mq);
378  dh->mq = NULL;
379  }
380  if (NULL != dh->reconnect_task)
381  {
383  dh->reconnect_task = NULL;
384  }
385  /* make sure client has no pending requests left over! */
386  GNUNET_break (0 == dh->pending_requests);
387  GNUNET_free (dh);
388 }
389 
390 /* 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:206
struct GNUNET_MessageHeader * msg
Definition: 005.c:2
void * rh_cls
Closure for rh.
Definition: dns_api.c:79
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:901
enum GNUNET_DNS_Flags flags
Flags for events we care about.
Definition: dns_api.c:94
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:50
#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:89
struct GNUNET_MQ_Handle * mq
Connection to DNS service, or NULL.
Definition: dns_api.c:64
#define GNUNET_TIME_UNIT_SECONDS
One second.
struct GNUNET_DNS_Handle * dh
Handle to DNS API.
Definition: dns_api.c:40
static void force_reconnect(struct GNUNET_DNS_Handle *dh)
Drop the existing connection and reconnect to the DNS service.
Definition: dns_api.c:120
#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:77
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:78
#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:84
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:181
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:161
#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:1246
void GNUNET_DNS_disconnect(struct GNUNET_DNS_Handle *dh)
Disconnect from the DNS service.
Definition: dns_api.c:373
#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_memcpy(dst, src, n)
#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:1273
unsigned int pending_requests
Number of GNUNET_DNS_RequestHandles we have outstanding.
Definition: dns_api.c:100
uint64_t request_id
Unique request ID.
Definition: dns.h:67
Message handler for a specific message type.
static struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
Definition: gnunet-arm.c:104
DNS handle.
Definition: dns_api.c:58
#define GNUNET_SYSERR
Definition: gnunet_common.h:79
uint64_t request_id
Stored in network byte order (as for us, it is just a random number).
Definition: dns_api.c:45
uint64_t request_id
Unique request ID.
Definition: dns.h:92
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:350
Handle to a message queue.
Definition: mq.c:85
GNUNET_DNS_RequestHandler rh
Function to call to get replies.
Definition: dns_api.c:74
const struct GNUNET_CONFIGURATION_Handle * cfg
Configuration to use.
Definition: dns_api.c:69
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:306
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:247
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:275
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:143
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:87
#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:965