2 This file is part of GNUnet.
3 Copyright (C) 2012, 2016 Christian Grothoff
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
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 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
23 * @brief library to access the VPN service and tell it how to redirect traffic
24 * @author Christian Grothoff
27 #include "gnunet_vpn_service.h"
34 struct GNUNET_VPN_Handle
37 * Configuration we use.
39 const struct GNUNET_CONFIGURATION_Handle *cfg;
42 * Connection to VPN service.
44 struct GNUNET_MQ_Handle *mq;
47 * Head of list of active redirection requests.
49 struct GNUNET_VPN_RedirectionRequest *rr_head;
52 * Tail of list of active redirection requests.
54 struct GNUNET_VPN_RedirectionRequest *rr_tail;
57 * Identifier of a reconnect task.
59 struct GNUNET_SCHEDULER_Task *rt;
62 * How long do we wait until we try to reconnect?
64 struct GNUNET_TIME_Relative backoff;
67 * ID of the last request that was submitted to the service.
69 uint64_t request_id_gen;
75 * Opaque redirection request handle.
77 struct GNUNET_VPN_RedirectionRequest
82 struct GNUNET_VPN_RedirectionRequest *next;
87 struct GNUNET_VPN_RedirectionRequest *prev;
90 * Pointer to the VPN struct.
92 struct GNUNET_VPN_Handle *vh;
95 * Target IP address for the redirection, or NULL for
96 * redirection to service. Allocated after this struct.
101 * Function to call with the designated IP address.
103 GNUNET_VPN_AllocationCallback cb;
111 * For service redirection, identity of the peer offering the service.
113 struct GNUNET_PeerIdentity peer;
116 * For service redirection, service descriptor.
118 struct GNUNET_HashCode serv;
121 * At what time should the created service mapping expire?
123 struct GNUNET_TIME_Absolute expiration_time;
126 * non-zero if this request has been sent to the service.
131 * Desired address family for the result.
136 * Address family of @e addr. AF_INET or AF_INET6.
141 * For service redirection, IPPROT_UDP or IPPROTO_TCP.
149 * Disconnect from the service (communication error) and reconnect later.
151 * @param vh handle to reconnect.
154 reconnect (struct GNUNET_VPN_Handle *vh);
158 * Check a #GNUNET_MESSAGE_TYPE_VPN_CLIENT_USE_IP message from the
161 * @param cls the `struct GNUNET_VPN_Handle`
162 * @param rm message received
163 * @return #GNUNET_OK if @a rm is well-formed
166 check_use_ip (void *cls,
167 const struct RedirectToIpResponseMessage *rm)
172 af = (int) ntohl (rm->result_af);
179 alen = sizeof (struct in_addr);
182 alen = sizeof (struct in6_addr);
186 return GNUNET_SYSERR;
188 if ( (ntohs (rm->header.size) != alen + sizeof (*rm)) ||
189 (0 == rm->request_id) )
192 return GNUNET_SYSERR;
199 * Handle a #GNUNET_MESSAGE_TYPE_VPN_CLIENT_USE_IP message from the
202 * @param cls the `struct GNUNET_VPN_Handle`
203 * @param rm message received
206 handle_use_ip (void *cls,
207 const struct RedirectToIpResponseMessage *rm)
209 struct GNUNET_VPN_Handle *vh = cls;
210 struct GNUNET_VPN_RedirectionRequest *rr;
213 af = (int) ntohl (rm->result_af);
214 for (rr = vh->rr_head; NULL != rr; rr = rr->next)
216 if (rr->request_id == rm->request_id)
218 GNUNET_CONTAINER_DLL_remove (vh->rr_head,
223 (af == AF_UNSPEC) ? NULL : &rm[1]);
232 * Add a request to our request queue and transmit it.
234 * @param rr request to queue and transmit.
237 send_request (struct GNUNET_VPN_RedirectionRequest *rr)
239 struct GNUNET_VPN_Handle *vh = rr->vh;
240 struct RedirectToIpRequestMessage *rip;
241 struct RedirectToServiceRequestMessage *rs;
242 struct GNUNET_MQ_Envelope *env;
247 if (NULL == rr->addr)
249 env = GNUNET_MQ_msg (rs,
250 GNUNET_MESSAGE_TYPE_VPN_CLIENT_REDIRECT_TO_SERVICE);
251 rs->reserved = htonl (0);
252 rs->expiration_time = GNUNET_TIME_absolute_hton (rr->expiration_time);
253 rs->protocol = htonl (rr->protocol);
254 rs->result_af = htonl (rr->result_af);
255 rs->target = rr->peer;
256 rs->service_descriptor = rr->serv;
257 rs->request_id = rr->request_id = ++vh->request_id_gen;
264 alen = sizeof (struct in_addr);
267 alen = sizeof (struct in6_addr);
273 env = GNUNET_MQ_msg_extra (rip,
275 GNUNET_MESSAGE_TYPE_VPN_CLIENT_REDIRECT_TO_IP);
276 rip->reserved = htonl (0);
277 rip->expiration_time = GNUNET_TIME_absolute_hton (rr->expiration_time);
278 rip->result_af = htonl (rr->result_af);
279 rip->addr_af = htonl (rr->addr_af);
280 rip->request_id = rr->request_id = ++vh->request_id_gen;
281 GNUNET_memcpy (&rip[1],
285 GNUNET_MQ_send (vh->mq,
291 * Generic error handler, called with the appropriate error code and
292 * the same closure specified at the creation of the message queue.
293 * Not every message queue implementation supports an error handler.
295 * @param cls closure with the `struct GNUNET_VPN_Handle *`
296 * @param error error code
299 mq_error_handler (void *cls,
300 enum GNUNET_MQ_Error error)
302 struct GNUNET_VPN_Handle *vh = cls;
309 * Connect to the VPN service and start again to transmit our requests.
311 * @param cls the `struct GNUNET_VPN_Handle *`
314 connect_task (void *cls)
316 struct GNUNET_VPN_Handle *vh = cls;
317 struct GNUNET_MQ_MessageHandler handlers[] = {
318 GNUNET_MQ_hd_var_size (use_ip,
319 GNUNET_MESSAGE_TYPE_VPN_CLIENT_USE_IP,
320 struct RedirectToIpResponseMessage,
322 GNUNET_MQ_handler_end ()
324 struct GNUNET_VPN_RedirectionRequest *rr;
327 vh->mq = GNUNET_CLIENT_connect (vh->cfg,
334 for (rr = vh->rr_head; NULL != rr; rr = rr->next)
340 * Disconnect from the service (communication error) and reconnect later.
342 * @param vh handle to reconnect.
345 reconnect (struct GNUNET_VPN_Handle *vh)
347 struct GNUNET_VPN_RedirectionRequest *rr;
349 GNUNET_MQ_destroy (vh->mq);
351 vh->request_id_gen = 0;
352 for (rr = vh->rr_head; NULL != rr; rr = rr->next)
354 vh->backoff = GNUNET_TIME_relative_max (GNUNET_TIME_UNIT_MILLISECONDS,
355 GNUNET_TIME_relative_min (GNUNET_TIME_relative_saturating_multiply (vh->backoff, 2),
356 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)));
357 vh->rt = GNUNET_SCHEDULER_add_delayed (vh->backoff,
364 * Cancel redirection request with the service.
366 * @param rr request to cancel
369 GNUNET_VPN_cancel_request (struct GNUNET_VPN_RedirectionRequest *rr)
371 struct GNUNET_VPN_Handle *vh;
374 GNUNET_CONTAINER_DLL_remove (vh->rr_head,
382 * Tell the VPN that a forwarding to a particular peer offering a
383 * particular service is requested. The VPN is to reserve a
384 * particular IP for the redirection and return it. The VPN will
385 * begin the redirection as soon as possible and maintain it as long
386 * as it is actively used and keeping it is feasible. Given resource
387 * limitations, the longest inactive mappings will be destroyed.
389 * @param vh VPN handle
390 * @param result_af desired address family for the returned allocation
391 * can also be AF_UNSPEC
392 * @param protocol protocol, IPPROTO_UDP or IPPROTO_TCP
393 * @param peer target peer for the redirection
394 * @param serv service descriptor to give to the peer
395 * @param expiration_time at what time should the redirection expire?
396 * (this should not impact connections that are active at that time)
397 * @param cb function to call with the IP
398 * @param cb_cls closure for @a cb
399 * @return handle to cancel the request (means the callback won't be
400 * invoked anymore; the mapping may or may not be established
403 struct GNUNET_VPN_RedirectionRequest *
404 GNUNET_VPN_redirect_to_peer (struct GNUNET_VPN_Handle *vh,
407 const struct GNUNET_PeerIdentity *peer,
408 const struct GNUNET_HashCode *serv,
409 struct GNUNET_TIME_Absolute expiration_time,
410 GNUNET_VPN_AllocationCallback cb,
413 struct GNUNET_VPN_RedirectionRequest *rr;
415 rr = GNUNET_new (struct GNUNET_VPN_RedirectionRequest);
421 rr->expiration_time = expiration_time;
422 rr->result_af = result_af;
423 rr->protocol = protocol;
424 GNUNET_CONTAINER_DLL_insert_tail (vh->rr_head,
433 * Tell the VPN that forwarding to the Internet via some exit node is
434 * requested. Note that both UDP and TCP traffic will be forwarded,
435 * but possibly to different exit nodes. The VPN is to reserve a
436 * particular IP for the redirection and return it. The VPN will
437 * begin the redirection as soon as possible and maintain it as long
438 * as it is actively used and keeping it is feasible. Given resource
439 * limitations, the longest inactive mappings will be destroyed.
441 * @param vh VPN handle
442 * @param result_af desired address family for the returned allocation
443 * @param addr_af address family for @a addr, AF_INET or AF_INET6
444 * @param addr destination IP address on the Internet; destination
445 * port is to be taken from the VPN packet itself
446 * @param expiration_time at what time should the redirection expire?
447 * (this should not impact connections that are active at that time)
448 * @param cb function to call with the IP
449 * @param cb_cls closure for @a cb
450 * @return handle to cancel the request (means the callback won't be
451 * invoked anymore; the mapping may or may not be established
454 struct GNUNET_VPN_RedirectionRequest *
455 GNUNET_VPN_redirect_to_ip (struct GNUNET_VPN_Handle *vh,
459 struct GNUNET_TIME_Absolute expiration_time,
460 GNUNET_VPN_AllocationCallback cb,
463 struct GNUNET_VPN_RedirectionRequest *rr;
469 alen = sizeof (struct in_addr);
472 alen = sizeof (struct in6_addr);
478 rr = GNUNET_malloc (sizeof (struct GNUNET_VPN_RedirectionRequest) + alen);
483 rr->expiration_time = expiration_time;
484 rr->result_af = result_af;
485 rr->addr_af = addr_af;
486 GNUNET_memcpy (&rr[1],
489 GNUNET_CONTAINER_DLL_insert_tail (vh->rr_head,
498 * Connect to the VPN service
500 * @param cfg configuration to use
503 struct GNUNET_VPN_Handle *
504 GNUNET_VPN_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
506 struct GNUNET_VPN_Handle *vh
507 = GNUNET_new (struct GNUNET_VPN_Handle);
521 * Disconnect from the VPN service.
523 * @param vh VPN handle
526 GNUNET_VPN_disconnect (struct GNUNET_VPN_Handle *vh)
528 GNUNET_assert (NULL == vh->rr_head);
531 GNUNET_MQ_destroy (vh->mq);
536 GNUNET_SCHEDULER_cancel (vh->rt);
542 /* end of vpn_api.c */