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

library to access the VPN service and tell it how to redirect traffic More...

#include "platform.h"
#include "gnunet_vpn_service.h"
#include "vpn.h"
Include dependency graph for vpn_api.c:

Go to the source code of this file.

Data Structures

struct  GNUNET_VPN_Handle
 Opaque VPN handle. More...
 
struct  GNUNET_VPN_RedirectionRequest
 Opaque redirection request handle. More...
 

Functions

static void reconnect (struct GNUNET_VPN_Handle *vh)
 Disconnect from the service (communication error) and reconnect later. More...
 
static int check_use_ip (void *cls, const struct RedirectToIpResponseMessage *rm)
 Check a GNUNET_MESSAGE_TYPE_VPN_CLIENT_USE_IP message from the VPN service. More...
 
static void handle_use_ip (void *cls, const struct RedirectToIpResponseMessage *rm)
 Handle a GNUNET_MESSAGE_TYPE_VPN_CLIENT_USE_IP message from the VPN service. More...
 
static void send_request (struct GNUNET_VPN_RedirectionRequest *rr)
 Add a request to our request queue and transmit it. 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 void connect_task (void *cls)
 Connect to the VPN service and start again to transmit our requests. More...
 
void GNUNET_VPN_cancel_request (struct GNUNET_VPN_RedirectionRequest *rr)
 Cancel redirection request with the service. More...
 
struct GNUNET_VPN_RedirectionRequestGNUNET_VPN_redirect_to_peer (struct GNUNET_VPN_Handle *vh, int result_af, uint8_t protocol, const struct GNUNET_PeerIdentity *peer, const struct GNUNET_HashCode *serv, struct GNUNET_TIME_Absolute expiration_time, GNUNET_VPN_AllocationCallback cb, void *cb_cls)
 Tell the VPN that a forwarding to a particular peer offering a particular service is requested. More...
 
struct GNUNET_VPN_RedirectionRequestGNUNET_VPN_redirect_to_ip (struct GNUNET_VPN_Handle *vh, int result_af, int addr_af, const void *addr, struct GNUNET_TIME_Absolute expiration_time, GNUNET_VPN_AllocationCallback cb, void *cb_cls)
 Tell the VPN that forwarding to the Internet via some exit node is requested. More...
 
struct GNUNET_VPN_HandleGNUNET_VPN_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
 Connect to the VPN service. More...
 
void GNUNET_VPN_disconnect (struct GNUNET_VPN_Handle *vh)
 Disconnect from the VPN service. More...
 

Detailed Description

library to access the VPN service and tell it how to redirect traffic

Author
Christian Grothoff

Definition in file vpn_api.c.

Function Documentation

◆ reconnect()

static void reconnect ( struct GNUNET_VPN_Handle vh)
static

Disconnect from the service (communication error) and reconnect later.

Parameters
vhhandle to reconnect.

Definition at line 346 of file vpn_api.c.

References GNUNET_VPN_Handle::backoff, connect_task(), GNUNET_MQ_destroy(), GNUNET_SCHEDULER_add_delayed(), GNUNET_TIME_relative_max(), GNUNET_TIME_relative_min(), GNUNET_TIME_relative_multiply(), GNUNET_TIME_relative_saturating_multiply(), GNUNET_TIME_UNIT_MILLISECONDS, GNUNET_TIME_UNIT_SECONDS, GNUNET_VPN_Handle::mq, GNUNET_VPN_RedirectionRequest::next, GNUNET_VPN_RedirectionRequest::request_id, GNUNET_VPN_Handle::request_id_gen, GNUNET_VPN_Handle::rr_head, and GNUNET_VPN_Handle::rt.

Referenced by mq_error_handler().

347 {
349 
350  GNUNET_MQ_destroy(vh->mq);
351  vh->mq = NULL;
352  vh->request_id_gen = 0;
353  for (rr = vh->rr_head; NULL != rr; rr = rr->next)
354  rr->request_id = 0;
359  &connect_task,
360  vh);
361 }
struct GNUNET_SCHEDULER_Task * rt
Identifier of a reconnect task.
Definition: vpn_api.c:58
uint64_t request_id_gen
ID of the last request that was submitted to the service.
Definition: vpn_api.c:68
struct GNUNET_VPN_RedirectionRequest * next
Element in DLL.
Definition: vpn_api.c:79
struct GNUNET_TIME_Relative GNUNET_TIME_relative_max(struct GNUNET_TIME_Relative t1, struct GNUNET_TIME_Relative t2)
Return the maximum of two relative time values.
Definition: time.c:287
#define GNUNET_TIME_UNIT_SECONDS
One second.
static void connect_task(void *cls)
Connect to the VPN service and start again to transmit our requests.
Definition: vpn_api.c:315
uint64_t request_id
non-zero if this request has been sent to the service.
Definition: vpn_api.c:125
struct GNUNET_MQ_Handle * mq
Connection to VPN service.
Definition: vpn_api.c:43
struct GNUNET_TIME_Relative backoff
How long do we wait until we try to reconnect?
Definition: vpn_api.c:63
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
struct GNUNET_TIME_Relative GNUNET_TIME_relative_multiply(struct GNUNET_TIME_Relative rel, unsigned long long factor)
Multiply relative time by a given factor.
Definition: time.c:440
#define GNUNET_TIME_UNIT_MILLISECONDS
One millisecond.
struct GNUNET_TIME_Relative GNUNET_TIME_relative_min(struct GNUNET_TIME_Relative t1, struct GNUNET_TIME_Relative t2)
Return the minimum of two relative time values.
Definition: time.c:272
Opaque redirection request handle.
Definition: vpn_api.c:75
struct GNUNET_VPN_RedirectionRequest * rr_head
Head of list of active redirection requests.
Definition: vpn_api.c:48
void GNUNET_MQ_destroy(struct GNUNET_MQ_Handle *mq)
Destroy the message queue.
Definition: mq.c:821
struct GNUNET_TIME_Relative GNUNET_TIME_relative_saturating_multiply(struct GNUNET_TIME_Relative rel, unsigned long long factor)
Saturating multiply relative time by a given factor.
Definition: time.c:499
Here is the call graph for this function:
Here is the caller graph for this function:

◆ check_use_ip()

static int check_use_ip ( void *  cls,
const struct RedirectToIpResponseMessage rm 
)
static

Check a GNUNET_MESSAGE_TYPE_VPN_CLIENT_USE_IP message from the VPN service.

Parameters
clsthe struct GNUNET_VPN_Handle
rmmessage received
Returns
GNUNET_OK if rm is well-formed

Definition at line 162 of file vpn_api.c.

References GNUNET_break, GNUNET_OK, GNUNET_SYSERR, RedirectToIpResponseMessage::header, RedirectToIpResponseMessage::request_id, RedirectToIpResponseMessage::result_af, and GNUNET_MessageHeader::size.

164 {
165  size_t alen;
166  int af;
167 
168  af = (int)ntohl(rm->result_af);
169  switch (af)
170  {
171  case AF_UNSPEC:
172  alen = 0;
173  break;
174 
175  case AF_INET:
176  alen = sizeof(struct in_addr);
177  break;
178 
179  case AF_INET6:
180  alen = sizeof(struct in6_addr);
181  break;
182 
183  default:
184  GNUNET_break(0);
185  return GNUNET_SYSERR;
186  }
187  if ((ntohs(rm->header.size) != alen + sizeof(*rm)) ||
188  (0 == rm->request_id))
189  {
190  GNUNET_break(0);
191  return GNUNET_SYSERR;
192  }
193  return GNUNET_OK;
194 }
uint64_t request_id
Unique ID to match the response to a request.
Definition: vpn.h:142
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
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_MessageHeader header
Type is GNUNET_MESSAGE_TYPE_VPN_CLIENT_USE_IP.
Definition: vpn.h:131
#define GNUNET_SYSERR
Definition: gnunet_common.h:76
int32_t result_af
Address family of the allocated address that follows; will match "result_af" from the request...
Definition: vpn.h:137

◆ handle_use_ip()

static void handle_use_ip ( void *  cls,
const struct RedirectToIpResponseMessage rm 
)
static

Handle a GNUNET_MESSAGE_TYPE_VPN_CLIENT_USE_IP message from the VPN service.

Parameters
clsthe struct GNUNET_VPN_Handle
rmmessage received

Definition at line 205 of file vpn_api.c.

References GNUNET_VPN_RedirectionRequest::cb, GNUNET_VPN_RedirectionRequest::cb_cls, GNUNET_CONTAINER_DLL_remove, GNUNET_free, GNUNET_VPN_RedirectionRequest::next, GNUNET_VPN_RedirectionRequest::request_id, RedirectToIpResponseMessage::request_id, RedirectToIpResponseMessage::result_af, GNUNET_VPN_Handle::rr_head, and GNUNET_VPN_Handle::rr_tail.

207 {
208  struct GNUNET_VPN_Handle *vh = cls;
210  int af;
211 
212  af = (int)ntohl(rm->result_af);
213  for (rr = vh->rr_head; NULL != rr; rr = rr->next)
214  {
215  if (rr->request_id == rm->request_id)
216  {
218  vh->rr_tail,
219  rr);
220  rr->cb(rr->cb_cls,
221  af,
222  (af == AF_UNSPEC) ? NULL : &rm[1]);
223  GNUNET_free(rr);
224  break;
225  }
226  }
227 }
#define GNUNET_CONTAINER_DLL_remove(head, tail, element)
Remove an element from a DLL.
struct GNUNET_VPN_RedirectionRequest * next
Element in DLL.
Definition: vpn_api.c:79
uint64_t request_id
Unique ID to match the response to a request.
Definition: vpn.h:142
uint64_t request_id
non-zero if this request has been sent to the service.
Definition: vpn_api.c:125
Opaque VPN handle.
Definition: vpn_api.c:34
GNUNET_VPN_AllocationCallback cb
Function to call with the designated IP address.
Definition: vpn_api.c:100
struct GNUNET_VPN_RedirectionRequest * rr_tail
Tail of list of active redirection requests.
Definition: vpn_api.c:53
int32_t result_af
Address family of the allocated address that follows; will match "result_af" from the request...
Definition: vpn.h:137
Opaque redirection request handle.
Definition: vpn_api.c:75
void * cb_cls
Closure for cb.
Definition: vpn_api.c:105
struct GNUNET_VPN_RedirectionRequest * rr_head
Head of list of active redirection requests.
Definition: vpn_api.c:48
#define GNUNET_free(ptr)
Wrapper around free.

◆ send_request()

static void send_request ( struct GNUNET_VPN_RedirectionRequest rr)
static

Add a request to our request queue and transmit it.

Parameters
rrrequest to queue and transmit.

Definition at line 236 of file vpn_api.c.

References GNUNET_VPN_RedirectionRequest::addr, RedirectToIpRequestMessage::addr_af, GNUNET_VPN_RedirectionRequest::addr_af, env, RedirectToIpRequestMessage::expiration_time, RedirectToServiceRequestMessage::expiration_time, GNUNET_VPN_RedirectionRequest::expiration_time, GNUNET_assert, GNUNET_memcpy, GNUNET_MESSAGE_TYPE_VPN_CLIENT_REDIRECT_TO_IP, GNUNET_MESSAGE_TYPE_VPN_CLIENT_REDIRECT_TO_SERVICE, GNUNET_MQ_msg, GNUNET_MQ_msg_extra, GNUNET_MQ_send(), GNUNET_TIME_absolute_hton(), GNUNET_VPN_Handle::mq, GNUNET_VPN_RedirectionRequest::peer, RedirectToServiceRequestMessage::protocol, GNUNET_VPN_RedirectionRequest::protocol, RedirectToIpRequestMessage::request_id, RedirectToServiceRequestMessage::request_id, GNUNET_VPN_RedirectionRequest::request_id, GNUNET_VPN_Handle::request_id_gen, RedirectToIpRequestMessage::reserved, RedirectToServiceRequestMessage::reserved, RedirectToIpRequestMessage::result_af, RedirectToServiceRequestMessage::result_af, GNUNET_VPN_RedirectionRequest::result_af, GNUNET_VPN_RedirectionRequest::serv, RedirectToServiceRequestMessage::service_descriptor, RedirectToServiceRequestMessage::target, and GNUNET_VPN_RedirectionRequest::vh.

Referenced by connect_task(), GNUNET_VPN_redirect_to_ip(), and GNUNET_VPN_redirect_to_peer().

237 {
238  struct GNUNET_VPN_Handle *vh = rr->vh;
239  struct RedirectToIpRequestMessage *rip;
241  struct GNUNET_MQ_Envelope *env;
242  size_t alen;
243 
244  if (NULL == vh->mq)
245  return;
246  if (NULL == rr->addr)
247  {
248  env = GNUNET_MQ_msg(rs,
250  rs->reserved = htonl(0);
252  rs->protocol = htonl(rr->protocol);
253  rs->result_af = htonl(rr->result_af);
254  rs->target = rr->peer;
255  rs->service_descriptor = rr->serv;
256  rs->request_id = rr->request_id = ++vh->request_id_gen;
257  }
258  else
259  {
260  switch (rr->addr_af)
261  {
262  case AF_INET:
263  alen = sizeof(struct in_addr);
264  break;
265 
266  case AF_INET6:
267  alen = sizeof(struct in6_addr);
268  break;
269 
270  default:
271  GNUNET_assert(0);
272  return;
273  }
274  env = GNUNET_MQ_msg_extra(rip,
275  alen,
277  rip->reserved = htonl(0);
279  rip->result_af = htonl(rr->result_af);
280  rip->addr_af = htonl(rr->addr_af);
281  rip->request_id = rr->request_id = ++vh->request_id_gen;
282  GNUNET_memcpy(&rip[1],
283  rr->addr,
284  alen);
285  }
286  GNUNET_MQ_send(vh->mq,
287  env);
288 }
struct GNUNET_HashCode service_descriptor
Service descriptor identifying the service.
Definition: vpn.h:113
int result_af
Desired address family for the result.
Definition: vpn_api.c:130
uint64_t request_id_gen
ID of the last request that was submitted to the service.
Definition: vpn_api.c:68
struct GNUNET_PeerIdentity target
Target peer offering the service.
Definition: vpn.h:108
#define GNUNET_MESSAGE_TYPE_VPN_CLIENT_REDIRECT_TO_SERVICE
Client asks VPN service to setup an IP to redirect traffic to some peer offering a service...
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
struct GNUNET_PeerIdentity peer
For service redirection, identity of the peer offering the service.
Definition: vpn_api.c:110
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
#define GNUNET_MQ_msg(mvar, type)
Allocate a GNUNET_MQ_Envelope.
Definition: gnunet_mq_lib.h:67
int32_t result_af
Address family desired for the result (AF_INET or AF_INET6 or AF_UNSPEC, in nbo)
Definition: vpn.h:103
int32_t protocol
Desired protocol (IPPROTO_UDP or IPPROTO_TCP)
Definition: vpn.h:98
uint64_t request_id
non-zero if this request has been sent to the service.
Definition: vpn_api.c:125
struct GNUNET_MQ_Handle * mq
Connection to VPN service.
Definition: vpn_api.c:43
Message send by the VPN client to the VPN service requesting the setup of a redirection from some IP ...
Definition: vpn.h:38
struct GNUNET_TIME_Absolute expiration_time
At what time should the created service mapping expire?
Definition: vpn_api.c:120
Opaque VPN handle.
Definition: vpn_api.c:34
uint32_t reserved
Always zero.
Definition: vpn.h:47
#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
#define GNUNET_MESSAGE_TYPE_VPN_CLIENT_REDIRECT_TO_IP
Client asks VPN service to setup an IP to redirect traffic via an exit node to some global IP address...
int32_t result_af
Address family desired for the result (AF_INET or AF_INET6 or AF_UNSPEC, in nbo)
Definition: vpn.h:57
struct GNUNET_MQ_Envelope * env
Definition: 005.c:1
uint32_t reserved
Always zero.
Definition: vpn.h:88
struct GNUNET_TIME_AbsoluteNBO expiration_time
How long should the redirection be maintained at most?
Definition: vpn.h:93
uint64_t request_id
Unique ID to match a future response to this request.
Definition: vpn.h:119
int32_t addr_af
Address family used for the destination address (AF_INET or AF_INET6, in nbo)
Definition: vpn.h:62
struct GNUNET_TIME_AbsoluteNBO expiration_time
How long should the redirection be maintained at most?
Definition: vpn.h:52
uint8_t protocol
For service redirection, IPPROT_UDP or IPPROTO_TCP.
Definition: vpn_api.c:140
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
int addr_af
Address family of addr.
Definition: vpn_api.c:135
uint64_t request_id
Unique ID to match a future response to this request.
Definition: vpn.h:68
struct GNUNET_TIME_AbsoluteNBO GNUNET_TIME_absolute_hton(struct GNUNET_TIME_Absolute a)
Convert absolute time to network byte order.
Definition: time.c:655
struct GNUNET_HashCode serv
For service redirection, service descriptor.
Definition: vpn_api.c:115
Message send by the VPN client to the VPN service requesting the setup of a redirection from some IP ...
Definition: vpn.h:79
const void * addr
Target IP address for the redirection, or NULL for redirection to service.
Definition: vpn_api.c:95
struct GNUNET_VPN_Handle * vh
Pointer to the VPN struct.
Definition: vpn_api.c:89
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_VPN_Handle *
errorerror code

Definition at line 300 of file vpn_api.c.

References reconnect().

Referenced by connect_task().

302 {
303  struct GNUNET_VPN_Handle *vh = cls;
304 
305  reconnect(vh);
306 }
Opaque VPN handle.
Definition: vpn_api.c:34
static void reconnect(struct GNUNET_VPN_Handle *vh)
Disconnect from the service (communication error) and reconnect later.
Definition: vpn_api.c:346
Here is the call graph for this function:
Here is the caller graph for this function:

◆ connect_task()

static void connect_task ( void *  cls)
static

Connect to the VPN service and start again to transmit our requests.

Parameters
clsthe struct GNUNET_VPN_Handle *

Definition at line 315 of file vpn_api.c.

References GNUNET_VPN_Handle::cfg, GNUNET_CLIENT_connect(), GNUNET_MESSAGE_TYPE_VPN_CLIENT_USE_IP, GNUNET_MQ_handler_end, GNUNET_MQ_hd_var_size, GNUNET_VPN_Handle::mq, mq_error_handler(), GNUNET_VPN_RedirectionRequest::next, GNUNET_VPN_Handle::rr_head, GNUNET_VPN_Handle::rt, and send_request().

Referenced by GNUNET_VPN_connect(), and reconnect().

316 {
317  struct GNUNET_VPN_Handle *vh = cls;
319  GNUNET_MQ_hd_var_size(use_ip,
322  cls),
324  };
326 
327  vh->rt = NULL;
328  vh->mq = GNUNET_CLIENT_connect(vh->cfg,
329  "vpn",
330  handlers,
332  vh);
333  if (NULL == vh->mq)
334  return;
335  for (rr = vh->rr_head; NULL != rr; rr = rr->next)
336  send_request(rr);
337 }
Response from the VPN service to a VPN client informing about the IP that was assigned for the reques...
Definition: vpn.h:127
struct GNUNET_SCHEDULER_Task * rt
Identifier of a reconnect task.
Definition: vpn_api.c:58
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
struct GNUNET_VPN_RedirectionRequest * next
Element in DLL.
Definition: vpn_api.c:79
struct GNUNET_MQ_Handle * mq
Connection to VPN service.
Definition: vpn_api.c:43
Opaque VPN handle.
Definition: vpn_api.c:34
#define GNUNET_MESSAGE_TYPE_VPN_CLIENT_USE_IP
VPN service responds to client with an IP to use for the requested redirection.
void * cls
Closure for mv and cb.
static struct GNUNET_CADET_MessageHandler handlers[]
Handlers, for diverse services.
#define GNUNET_MQ_hd_var_size(name, code, str, ctx)
Message handler for a specific message type.
Opaque redirection request handle.
Definition: vpn_api.c:75
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: vpn_api.c:300
struct GNUNET_VPN_RedirectionRequest * rr_head
Head of list of active redirection requests.
Definition: vpn_api.c:48
const struct GNUNET_CONFIGURATION_Handle * cfg
Configuration we use.
Definition: vpn_api.c:38
#define GNUNET_MQ_handler_end()
End-marker for the handlers array.
static void send_request(struct GNUNET_VPN_RedirectionRequest *rr)
Add a request to our request queue and transmit it.
Definition: vpn_api.c:236
Here is the call graph for this function:
Here is the caller graph for this function: