2 This file is part of GNUnet.
3 Copyright (C) 2012 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_CLIENT_Connection *client;
47 * Active transmission request.
49 struct GNUNET_CLIENT_TransmitHandle *th;
52 * Head of list of active redirection requests.
54 struct GNUNET_VPN_RedirectionRequest *rr_head;
57 * Tail of list of active redirection requests.
59 struct GNUNET_VPN_RedirectionRequest *rr_tail;
62 * Identifier of a reconnect task.
64 struct GNUNET_SCHEDULER_Task * rt;
67 * How long do we wait until we try to reconnect?
69 struct GNUNET_TIME_Relative backoff;
72 * ID of the last request that was submitted to the service.
74 uint64_t request_id_gen;
80 * Opaque redirection request handle.
82 struct GNUNET_VPN_RedirectionRequest
87 struct GNUNET_VPN_RedirectionRequest *next;
92 struct GNUNET_VPN_RedirectionRequest *prev;
95 * Pointer to the VPN struct.
97 struct GNUNET_VPN_Handle *vh;
100 * Target IP address for the redirection, or NULL for
101 * redirection to service. Allocated after this struct.
106 * Function to call with the designated IP address.
108 GNUNET_VPN_AllocationCallback cb;
116 * For service redirection, identity of the peer offering the service.
118 struct GNUNET_PeerIdentity peer;
121 * For service redirection, service descriptor.
123 struct GNUNET_HashCode serv;
126 * At what time should the created service mapping expire?
128 struct GNUNET_TIME_Absolute expiration_time;
131 * non-zero if this request has been sent to the service.
136 * Desired address family for the result.
141 * Address family of 'addr'. AF_INET or AF_INET6.
146 * For service redirection, IPPROT_UDP or IPPROTO_TCP.
154 * Disconnect from the service (communication error) and reconnect later.
156 * @param vh handle to reconnect.
159 reconnect (struct GNUNET_VPN_Handle *vh);
163 * Function called when we receive a message from the VPN service.
165 * @param cls the `struct GNUNET_VPN_Handle`
166 * @param msg message received, NULL on timeout or fatal error
169 receive_response (void *cls,
170 const struct GNUNET_MessageHeader* msg)
172 struct GNUNET_VPN_Handle *vh = cls;
173 const struct RedirectToIpResponseMessage *rm;
174 struct GNUNET_VPN_RedirectionRequest *rr;
184 if ( (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_VPN_CLIENT_USE_IP) ||
185 (sizeof (struct RedirectToIpResponseMessage) > (msize = ntohs (msg->size))) )
191 rm = (const struct RedirectToIpResponseMessage *) msg;
192 af = (int) ntohl (rm->result_af);
199 alen = sizeof (struct in_addr);
202 alen = sizeof (struct in6_addr);
209 if ( (msize != alen + sizeof (struct RedirectToIpResponseMessage)) ||
210 (0 == rm->request_id) )
216 GNUNET_CLIENT_receive (vh->client,
217 &receive_response, vh,
218 GNUNET_TIME_UNIT_FOREVER_REL);
219 for (rr = vh->rr_head; NULL != rr; rr = rr->next)
221 if (rr->request_id == rm->request_id)
223 GNUNET_CONTAINER_DLL_remove (vh->rr_head,
228 (af == AF_UNSPEC) ? NULL : &rm[1]);
237 * We're ready to transmit a request to the VPN service. Do it.
239 * @param cls the 'struct GNUNET_VPN_Handle*'
240 * @param size number of bytes available in buf
241 * @param buf where to copy the request
242 * @return number of bytes copied to 'buf'
245 transmit_request (void *cls,
249 struct GNUNET_VPN_Handle *vh = cls;
250 struct GNUNET_VPN_RedirectionRequest *rr;
251 struct RedirectToIpRequestMessage rip;
252 struct RedirectToServiceRequestMessage rs;
258 /* find a pending request */
260 while ( (NULL != rr) &&
261 (0 != rr->request_id) )
271 /* if first request, start receive loop */
272 if (0 == vh->request_id_gen)
273 GNUNET_CLIENT_receive (vh->client,
274 &receive_response, vh,
275 GNUNET_TIME_UNIT_FOREVER_REL);
276 if (NULL == rr->addr)
278 ret = sizeof (struct RedirectToServiceRequestMessage);
279 GNUNET_assert (ret <= size);
280 rs.header.size = htons ((uint16_t) ret);
281 rs.header.type = htons (GNUNET_MESSAGE_TYPE_VPN_CLIENT_REDIRECT_TO_SERVICE);
282 rs.reserved = htonl (0);
283 rs.expiration_time = GNUNET_TIME_absolute_hton (rr->expiration_time);
284 rs.protocol = htonl (rr->protocol);
285 rs.result_af = htonl (rr->result_af);
286 rs.target = rr->peer;
287 rs.service_descriptor = rr->serv;
288 rs.request_id = rr->request_id = ++vh->request_id_gen;
289 memcpy (buf, &rs, sizeof (struct RedirectToServiceRequestMessage));
296 alen = sizeof (struct in_addr);
299 alen = sizeof (struct in6_addr);
305 ret = alen + sizeof (struct RedirectToIpRequestMessage);
306 GNUNET_assert (ret <= size);
307 rip.header.size = htons ((uint16_t) ret);
308 rip.header.type = htons (GNUNET_MESSAGE_TYPE_VPN_CLIENT_REDIRECT_TO_IP);
309 rip.reserved = htonl (0);
310 rip.expiration_time = GNUNET_TIME_absolute_hton (rr->expiration_time);
311 rip.result_af = htonl (rr->result_af);
312 rip.addr_af = htonl (rr->addr_af);
313 rip.request_id = rr->request_id = ++vh->request_id_gen;
315 memcpy (cbuf, &rip, sizeof (struct RedirectToIpRequestMessage));
316 memcpy (&cbuf[sizeof (struct RedirectToIpRequestMessage)], rr->addr, alen);
318 /* test if there are more pending requests */
319 while ( (NULL != rr) &&
320 (0 != rr->request_id) )
323 vh->th = GNUNET_CLIENT_notify_transmit_ready (vh->client,
324 sizeof (struct RedirectToServiceRequestMessage),
325 GNUNET_TIME_UNIT_FOREVER_REL,
334 * Add a request to our request queue and transmit it.
336 * @param rr request to queue and transmit.
339 queue_request (struct GNUNET_VPN_RedirectionRequest *rr)
341 struct GNUNET_VPN_Handle *vh;
344 GNUNET_CONTAINER_DLL_insert_tail (vh->rr_head,
347 if ( (NULL == vh->th) &&
348 (NULL != vh->client) )
349 vh->th = GNUNET_CLIENT_notify_transmit_ready (vh->client,
350 sizeof (struct RedirectToServiceRequestMessage),
351 GNUNET_TIME_UNIT_FOREVER_REL,
359 * Connect to the VPN service and start again to transmit our requests.
361 * @param cls the 'struct GNUNET_VPN_Handle *'
362 * @param tc scheduler context
365 connect_task (void *cls,
366 const struct GNUNET_SCHEDULER_TaskContext *tc)
368 struct GNUNET_VPN_Handle *vh = cls;
371 vh->client = GNUNET_CLIENT_connect ("vpn", vh->cfg);
372 GNUNET_assert (NULL != vh->client);
373 GNUNET_assert (NULL == vh->th);
374 if (NULL != vh->rr_head)
375 vh->th = GNUNET_CLIENT_notify_transmit_ready (vh->client,
376 sizeof (struct RedirectToServiceRequestMessage),
377 GNUNET_TIME_UNIT_FOREVER_REL,
385 * Disconnect from the service (communication error) and reconnect later.
387 * @param vh handle to reconnect.
390 reconnect (struct GNUNET_VPN_Handle *vh)
392 struct GNUNET_VPN_RedirectionRequest *rr;
396 GNUNET_CLIENT_notify_transmit_ready_cancel (vh->th);
399 GNUNET_CLIENT_disconnect (vh->client);
401 vh->request_id_gen = 0;
402 for (rr = vh->rr_head; NULL != rr; rr = rr->next)
404 vh->backoff = GNUNET_TIME_relative_max (GNUNET_TIME_UNIT_MILLISECONDS,
405 GNUNET_TIME_relative_min (GNUNET_TIME_relative_multiply (vh->backoff, 2),
406 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)));
407 vh->rt = GNUNET_SCHEDULER_add_delayed (vh->backoff,
414 * Cancel redirection request with the service.
416 * @param rr request to cancel
419 GNUNET_VPN_cancel_request (struct GNUNET_VPN_RedirectionRequest *rr)
421 struct GNUNET_VPN_Handle *vh;
424 GNUNET_CONTAINER_DLL_remove (vh->rr_head,
432 * Tell the VPN that a forwarding to a particular peer offering a
433 * particular service is requested. The VPN is to reserve a
434 * particular IP for the redirection and return it. The VPN will
435 * begin the redirection as soon as possible and maintain it as long
436 * as it is actively used and keeping it is feasible. Given resource
437 * limitations, the longest inactive mappings will be destroyed.
439 * @param vh VPN handle
440 * @param result_af desired address family for the returned allocation
441 * can also be AF_UNSPEC
442 * @param protocol protocol, IPPROTO_UDP or IPPROTO_TCP
443 * @param peer target peer for the redirection
444 * @param serv service descriptor to give to the peer
445 * @param expiration_time at what time should the redirection expire?
446 * (this should not impact connections that are active at that time)
447 * @param cb function to call with the IP
448 * @param cb_cls closure for cb
449 * @return handle to cancel the request (means the callback won't be
450 * invoked anymore; the mapping may or may not be established
453 struct GNUNET_VPN_RedirectionRequest *
454 GNUNET_VPN_redirect_to_peer (struct GNUNET_VPN_Handle *vh,
457 const struct GNUNET_PeerIdentity *peer,
458 const struct GNUNET_HashCode *serv,
459 struct GNUNET_TIME_Absolute expiration_time,
460 GNUNET_VPN_AllocationCallback cb,
463 struct GNUNET_VPN_RedirectionRequest *rr;
465 rr = GNUNET_new (struct GNUNET_VPN_RedirectionRequest);
471 rr->expiration_time = expiration_time;
472 rr->result_af = result_af;
473 rr->protocol = protocol;
480 * Tell the VPN that forwarding to the Internet via some exit node is
481 * requested. Note that both UDP and TCP traffic will be forwarded,
482 * but possibly to different exit nodes. The VPN is to reserve a
483 * particular IP for the redirection and return it. The VPN will
484 * begin the redirection as soon as possible and maintain it as long
485 * as it is actively used and keeping it is feasible. Given resource
486 * limitations, the longest inactive mappings will be destroyed.
488 * @param vh VPN handle
489 * @param result_af desired address family for the returned allocation
490 * @param addr_af address family for 'addr', AF_INET or AF_INET6
491 * @param addr destination IP address on the Internet; destination
492 * port is to be taken from the VPN packet itself
493 * @param expiration_time at what time should the redirection expire?
494 * (this should not impact connections that are active at that time)
495 * @param cb function to call with the IP
496 * @param cb_cls closure for cb
497 * @return handle to cancel the request (means the callback won't be
498 * invoked anymore; the mapping may or may not be established
501 struct GNUNET_VPN_RedirectionRequest *
502 GNUNET_VPN_redirect_to_ip (struct GNUNET_VPN_Handle *vh,
506 struct GNUNET_TIME_Absolute expiration_time,
507 GNUNET_VPN_AllocationCallback cb,
510 struct GNUNET_VPN_RedirectionRequest *rr;
516 alen = sizeof (struct in_addr);
519 alen = sizeof (struct in6_addr);
525 rr = GNUNET_malloc (sizeof (struct GNUNET_VPN_RedirectionRequest) + alen);
530 rr->expiration_time = expiration_time;
531 rr->result_af = result_af;
532 rr->addr_af = addr_af;
533 memcpy (&rr[1], addr, alen);
540 * Connect to the VPN service
542 * @param cfg configuration to use
545 struct GNUNET_VPN_Handle *
546 GNUNET_VPN_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
548 struct GNUNET_VPN_Handle *vh;
550 vh = GNUNET_new (struct GNUNET_VPN_Handle);
552 vh->client = GNUNET_CLIENT_connect ("vpn", cfg);
553 if (NULL == vh->client)
563 * Disconnect from the VPN service.
565 * @param vh VPN handle
568 GNUNET_VPN_disconnect (struct GNUNET_VPN_Handle *vh)
570 GNUNET_assert (NULL == vh->rr_head);
573 GNUNET_CLIENT_notify_transmit_ready_cancel (vh->th);
576 if (NULL != vh->client)
578 GNUNET_CLIENT_disconnect (vh->client);
583 GNUNET_SCHEDULER_cancel (vh->rt);
589 /* end of vpn_api.c */