GNUnet  0.10.x
Data Structures | Functions
dns_api.c File Reference

API to access the DNS service. More...

#include "platform.h"
#include "gnunet_dns_service.h"
#include "dns.h"
Include dependency graph for dns_api.c:

Go to the source code of this file.

Data Structures

struct  GNUNET_DNS_RequestHandle
 Handle to identify an individual DNS request. More...
 
struct  GNUNET_DNS_Handle
 DNS handle. More...
 

Functions

static void reconnect (void *cls)
 Reconnect to the DNS service. More...
 
static void force_reconnect (struct GNUNET_DNS_Handle *dh)
 Drop the existing connection and reconnect to the DNS service. More...
 
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 creation of the message queue. More...
 
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 well-formed. More...
 
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. More...
 
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 global DNS for resolution. More...
 
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 should be generated. More...
 
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 data provided to this call (with the modifications the function might have made). More...
 
struct GNUNET_DNS_HandleGNUNET_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. More...
 
void GNUNET_DNS_disconnect (struct GNUNET_DNS_Handle *dh)
 Disconnect from the DNS service. More...
 

Detailed Description

API to access the DNS service.

Author
Christian Grothoff

Definition in file dns_api.c.

Function Documentation

◆ reconnect()

static void reconnect ( void *  cls)
static

Reconnect to the DNS service.

Parameters
clshandle with the connection to connect
tcscheduler context (unused)
clshandle with the connection to connect

Definition at line 201 of file dns_api.c.

References GNUNET_DNS_Handle::cfg, GNUNET_DNS_RequestHandle::dh, env, GNUNET_DNS_Register::flags, GNUNET_DNS_Handle::flags, GNUNET_DNS_Handle::generation, GNUNET_CLIENT_connect(), GNUNET_MESSAGE_TYPE_DNS_CLIENT_INIT, GNUNET_MESSAGE_TYPE_DNS_CLIENT_REQUEST, GNUNET_MQ_handler_end, GNUNET_MQ_hd_var_size, GNUNET_MQ_msg, GNUNET_MQ_send(), GNUNET_DNS_Handle::mq, mq_error_handler(), msg, GNUNET_DNS_Handle::reconnect_task, and request.

Referenced by force_reconnect(), and GNUNET_DNS_connect().

202 {
203  struct GNUNET_DNS_Handle *dh = cls;
207  struct GNUNET_DNS_Request,
208  dh),
210  };
211  struct GNUNET_MQ_Envelope *env;
212  struct GNUNET_DNS_Register *msg;
213 
214  dh->reconnect_task = NULL;
215  dh->mq = GNUNET_CLIENT_connect(dh->cfg,
216  "dns",
217  handlers,
219  dh);
220  if (NULL == dh->mq)
221  return;
222  dh->generation++;
223  env = GNUNET_MQ_msg(msg,
225  msg->flags = htonl(dh->flags);
226  GNUNET_MQ_send(dh->mq,
227  env);
228 }
uint32_t flags
NBO encoding of enum GNUNET_DNS_Flags for the client.
Definition: dns.h:44
struct GNUNET_MessageHeader * msg
Definition: 005.c:2
static struct GNUNET_VPN_RedirectionRequest * request
Opaque redirection request handle.
Definition: gnunet-vpn.c:41
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:900
enum GNUNET_DNS_Flags flags
Flags for events we care about.
Definition: dns_api.c:89
uint32_t generation
Re-connect counter, to make sure we did not reconnect in the meantime.
Definition: dns_api.c:84
struct GNUNET_MQ_Handle * mq
Connection to DNS service, or NULL.
Definition: dns_api.c:59
#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
struct GNUNET_SCHEDULER_Task * reconnect_task
Task to reconnect to the service.
Definition: dns_api.c:79
#define GNUNET_MESSAGE_TYPE_DNS_CLIENT_INIT
Initial message from client to DNS service for registration.
static struct GNUNET_CADET_MessageHandler handlers[]
Handlers, for diverse services.
#define GNUNET_MQ_hd_var_size(name, code, str, ctx)
Message from client to DNS service to register itself.
Definition: dns.h:35
Message handler for a specific message type.
DNS handle.
Definition: dns_api.c:55
struct GNUNET_MQ_Envelope * env
Definition: 005.c:1
const struct GNUNET_CONFIGURATION_Handle * cfg
Configuration to use.
Definition: dns_api.c:64
Message from DNS service to client: please handle a request.
Definition: dns.h:51
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:351
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:138
#define GNUNET_MQ_handler_end()
End-marker for the handlers array.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ force_reconnect()

static void force_reconnect ( struct GNUNET_DNS_Handle dh)
static

Drop the existing connection and reconnect to the DNS service.

Parameters
dhhandle with the connection

Definition at line 115 of file dns_api.c.

References GNUNET_MQ_destroy(), GNUNET_SCHEDULER_add_delayed(), GNUNET_TIME_UNIT_SECONDS, GNUNET_DNS_Handle::mq, reconnect(), and GNUNET_DNS_Handle::reconnect_task.

Referenced by mq_error_handler().

116 {
117  if (NULL != dh->mq)
118  {
119  GNUNET_MQ_destroy(dh->mq);
120  dh->mq = NULL;
121  }
122  dh->reconnect_task =
124  &reconnect,
125  dh);
126 }
static void reconnect(void *cls)
Reconnect to the DNS service.
Definition: dns_api.c:201
struct GNUNET_MQ_Handle * mq
Connection to DNS service, or NULL.
Definition: dns_api.c:59
#define GNUNET_TIME_UNIT_SECONDS
One second.
struct GNUNET_SCHEDULER_Task * reconnect_task
Task to reconnect to the service.
Definition: dns_api.c:79
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:1237
void GNUNET_MQ_destroy(struct GNUNET_MQ_Handle *mq)
Destroy the message queue.
Definition: mq.c:821
Here is the call graph for this function:
Here is the caller graph for this function:

◆ mq_error_handler()

static void mq_error_handler ( void *  cls,
enum GNUNET_MQ_Error  error 
)
static

Generic error handler, called with the appropriate error code and the same closure specified at the creation of the message queue.

Not every message queue implementation supports an error handler.

Parameters
clsclosure with the struct GNUNET_DNS_Handle *
errorerror code

Definition at line 138 of file dns_api.c.

References GNUNET_DNS_RequestHandle::dh, and force_reconnect().

Referenced by reconnect().

140 {
141  struct GNUNET_DNS_Handle *dh = cls;
142 
143  force_reconnect(dh);
144 }
static void force_reconnect(struct GNUNET_DNS_Handle *dh)
Drop the existing connection and reconnect to the DNS service.
Definition: dns_api.c:115
DNS handle.
Definition: dns_api.c:55
Here is the call graph for this function:
Here is the caller graph for this function:

◆ check_request()

static int check_request ( void *  cls,
const struct GNUNET_DNS_Request req 
)
static

This receives packets from the DNS service and calls the application to check that the request is well-formed.

Parameters
clsthe struct GNUNET_DNS_Handle
reqmessage from the service (request)

Definition at line 156 of file dns_api.c.

References GNUNET_break, GNUNET_OK, GNUNET_SYSERR, and GNUNET_DNS_Request::reserved.

158 {
159  if (0 != ntohl(req->reserved))
160  {
161  GNUNET_break(0);
162  return GNUNET_SYSERR;
163  }
164  return GNUNET_OK;
165 }
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
#define GNUNET_SYSERR
Definition: gnunet_common.h:76
uint32_t reserved
Always zero.
Definition: dns.h:60

◆ handle_request()

static void handle_request ( void *  cls,
const struct GNUNET_DNS_Request req 
)
static

This receives packets from the DNS service and calls the application to handle it.

Parameters
clsthe struct GNUNET_DNS_Handle *
msgmessage from the service (request)

Definition at line 176 of file dns_api.c.

References GNUNET_DNS_RequestHandle::dh, GNUNET_DNS_RequestHandle::generation, GNUNET_DNS_Handle::generation, GNUNET_new, GNUNET_DNS_Request::header, GNUNET_DNS_Handle::pending_requests, GNUNET_DNS_RequestHandle::request_id, GNUNET_DNS_Request::request_id, GNUNET_DNS_Handle::rh, GNUNET_DNS_Handle::rh_cls, and GNUNET_MessageHeader::size.

178 {
179  struct GNUNET_DNS_Handle *dh = cls;
180  size_t payload_length = ntohs(req->header.size) - sizeof(*req);
181  struct GNUNET_DNS_RequestHandle *rh;
182 
184  rh->dh = dh;
185  rh->request_id = req->request_id;
186  rh->generation = dh->generation;
187  dh->pending_requests++;
188  dh->rh(dh->rh_cls,
189  rh,
190  payload_length,
191  (const char*)&req[1]);
192 }
void * rh_cls
Closure for rh.
Definition: dns_api.c:74
uint32_t generation
Re-connect counter, to make sure we did not reconnect in the meantime.
Definition: dns_api.c:48
uint32_t generation
Re-connect counter, to make sure we did not reconnect in the meantime.
Definition: dns_api.c:84
struct GNUNET_DNS_Handle * dh
Handle to DNS API.
Definition: dns_api.c:38
#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...
struct GNUNET_MessageHeader header
Header of type GNUNET_MESSAGE_TYPE_DNS_CLIENT_REQUEST.
Definition: dns.h:55
unsigned int pending_requests
Number of GNUNET_DNS_RequestHandles we have outstanding.
Definition: dns_api.c:95
uint64_t request_id
Unique request ID.
Definition: dns.h:65
DNS handle.
Definition: dns_api.c:55
uint64_t request_id
Stored in network byte order (as for us, it is just a random number).
Definition: dns_api.c:43
GNUNET_DNS_RequestHandler rh
Function to call to get replies.
Definition: dns_api.c:69
Handle to identify an individual DNS request.
Definition: dns_api.c:34