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 206 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, and GNUNET_DNS_Handle::reconnect_task.

Referenced by force_reconnect(), and GNUNET_DNS_connect().

207 {
208  struct GNUNET_DNS_Handle *dh = cls;
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 }
uint32_t flags
NBO encoding of enum GNUNET_DNS_Flags for the client.
Definition: dns.h:45
struct GNUNET_MessageHeader * msg
Definition: 005.c:2
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
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_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:84
#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:58
struct GNUNET_MQ_Envelope * env
Definition: 005.c:1
const struct GNUNET_CONFIGURATION_Handle * cfg
Configuration to use.
Definition: dns_api.c:69
Message from DNS service to client: please handle a request.
Definition: dns.h:52
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
#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 120 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().

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 }
static void reconnect(void *cls)
Reconnect to the DNS service.
Definition: dns_api.c:206
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_SCHEDULER_Task * reconnect_task
Task to reconnect to the service.
Definition: dns_api.c:84
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_MQ_destroy(struct GNUNET_MQ_Handle *mq)
Destroy the message queue.
Definition: mq.c:824
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 143 of file dns_api.c.

References GNUNET_DNS_RequestHandle::dh, and force_reconnect().

Referenced by reconnect().

145 {
146  struct GNUNET_DNS_Handle *dh = cls;
147 
148  force_reconnect (dh);
149 }
static void force_reconnect(struct GNUNET_DNS_Handle *dh)
Drop the existing connection and reconnect to the DNS service.
Definition: dns_api.c:120
DNS handle.
Definition: dns_api.c:58
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 161 of file dns_api.c.

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

163 {
164  if (0 != ntohl (req->reserved))
165  {
166  GNUNET_break (0);
167  return GNUNET_SYSERR;
168  }
169  return GNUNET_OK;
170 }
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:78
#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:79
uint32_t reserved
Always zero.
Definition: dns.h:62

◆ 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 181 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.

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 }
void * rh_cls
Closure for rh.
Definition: dns_api.c:79
uint32_t generation
Re-connect counter, to make sure we did not reconnect in the meantime.
Definition: dns_api.c:50
uint32_t generation
Re-connect counter, to make sure we did not reconnect in the meantime.
Definition: dns_api.c:89
struct GNUNET_DNS_Handle * dh
Handle to DNS API.
Definition: dns_api.c:40
#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:57
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
DNS handle.
Definition: dns_api.c:58
uint64_t request_id
Stored in network byte order (as for us, it is just a random number).
Definition: dns_api.c:45
GNUNET_DNS_RequestHandler rh
Function to call to get replies.
Definition: dns_api.c:74
Handle to identify an individual DNS request.
Definition: dns_api.c:34