2 This file is part of GNUnet.
3 (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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, 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 GNUNET_SCHEDULER_TaskIdentifier 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 * GNUNET_YES if we are to call the callback only after successful
147 * mesh tunnel creation.
152 * For service redirection, IPPROT_UDP or IPPROTO_TCP.
160 * Disconnect from the service (communication error) and reconnect later.
162 * @param vh handle to reconnect.
165 reconnect (struct GNUNET_VPN_Handle *vh);
169 * Function called when we receive a message from the VPN service.
171 * @param cls the 'struct GNUNET_VPN_Handle'
172 * @param msg message received, NULL on timeout or fatal error
175 receive_response (void *cls,
176 const struct GNUNET_MessageHeader* msg)
178 struct GNUNET_VPN_Handle *vh = cls;
179 const struct RedirectToIpResponseMessage *rm;
180 struct GNUNET_VPN_RedirectionRequest *rr;
190 if ( (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_VPN_CLIENT_USE_IP) ||
191 (sizeof (struct RedirectToIpResponseMessage) > (msize = ntohs (msg->size))) )
197 rm = (const struct RedirectToIpResponseMessage *) msg;
198 af = (int) ntohl (rm->result_af);
205 alen = sizeof (struct in_addr);
208 alen = sizeof (struct in6_addr);
215 if ( (msize != alen + sizeof (struct RedirectToIpResponseMessage)) ||
216 (0 == rm->request_id) )
222 GNUNET_CLIENT_receive (vh->client,
223 &receive_response, vh,
224 GNUNET_TIME_UNIT_FOREVER_REL);
225 for (rr = vh->rr_head; NULL != rr; rr = rr->next)
227 if (rr->request_id == rm->request_id)
229 GNUNET_CONTAINER_DLL_remove (vh->rr_head,
234 (af == AF_UNSPEC) ? NULL : &rm[1]);
243 * We're ready to transmit a request to the VPN service. Do it.
245 * @param cls the 'struct GNUNET_VPN_Handle*'
246 * @param size number of bytes available in buf
247 * @param buf where to copy the request
248 * @return number of bytes copied to 'buf'
251 transmit_request (void *cls,
255 struct GNUNET_VPN_Handle *vh = cls;
256 struct GNUNET_VPN_RedirectionRequest *rr;
257 struct RedirectToIpRequestMessage rip;
258 struct RedirectToServiceRequestMessage rs;
264 /* find a pending request */
266 while ( (NULL != rr) &&
267 (0 != rr->request_id) )
277 /* if first request, start receive loop */
278 if (0 == vh->request_id_gen)
279 GNUNET_CLIENT_receive (vh->client,
280 &receive_response, vh,
281 GNUNET_TIME_UNIT_FOREVER_REL);
282 if (NULL == rr->addr)
284 ret = sizeof (struct RedirectToServiceRequestMessage);
285 GNUNET_assert (ret <= size);
286 rs.header.size = htons ((uint16_t) ret);
287 rs.header.type = htons (GNUNET_MESSAGE_TYPE_VPN_CLIENT_REDIRECT_TO_SERVICE);
288 rs.nac = htonl (rr->nac);
289 rs.expiration_time = GNUNET_TIME_absolute_hton (rr->expiration_time);
290 rs.protocol = htonl (rr->protocol);
291 rs.result_af = htonl (rr->result_af);
292 rs.target = rr->peer;
293 rs.service_descriptor = rr->serv;
294 rs.request_id = rr->request_id = ++vh->request_id_gen;
295 memcpy (buf, &rs, sizeof (struct RedirectToServiceRequestMessage));
302 alen = sizeof (struct in_addr);
305 alen = sizeof (struct in6_addr);
311 ret = alen + sizeof (struct RedirectToIpRequestMessage);
312 GNUNET_assert (ret <= size);
313 rip.header.size = htons ((uint16_t) ret);
314 rip.header.type = htons (GNUNET_MESSAGE_TYPE_VPN_CLIENT_REDIRECT_TO_IP);
315 rip.nac = htonl (rr->nac);
316 rip.expiration_time = GNUNET_TIME_absolute_hton (rr->expiration_time);
317 rip.result_af = htonl (rr->result_af);
318 rip.addr_af = htonl (rr->addr_af);
319 rip.request_id = rr->request_id = ++vh->request_id_gen;
321 memcpy (cbuf, &rip, sizeof (struct RedirectToIpRequestMessage));
322 memcpy (&cbuf[sizeof (struct RedirectToIpRequestMessage)], rr->addr, alen);
324 /* test if there are more pending requests */
325 while ( (NULL != rr) &&
326 (0 != rr->request_id) )
329 vh->th = GNUNET_CLIENT_notify_transmit_ready (vh->client,
330 sizeof (struct RedirectToServiceRequestMessage),
331 GNUNET_TIME_UNIT_FOREVER_REL,
340 * Add a request to our request queue and transmit it.
342 * @param rr request to queue and transmit.
345 queue_request (struct GNUNET_VPN_RedirectionRequest *rr)
347 struct GNUNET_VPN_Handle *vh;
350 GNUNET_CONTAINER_DLL_insert_tail (vh->rr_head,
353 if ( (NULL == vh->th) &&
354 (NULL != vh->client) )
355 vh->th = GNUNET_CLIENT_notify_transmit_ready (vh->client,
356 sizeof (struct RedirectToServiceRequestMessage),
357 GNUNET_TIME_UNIT_FOREVER_REL,
365 * Connect to the VPN service and start again to transmit our requests.
367 * @param cls the 'struct GNUNET_VPN_Handle *'
368 * @param tc scheduler context
371 connect_task (void *cls,
372 const struct GNUNET_SCHEDULER_TaskContext *tc)
374 struct GNUNET_VPN_Handle *vh = cls;
376 vh->rt = GNUNET_SCHEDULER_NO_TASK;
377 vh->client = GNUNET_CLIENT_connect ("vpn", vh->cfg);
378 GNUNET_assert (NULL != vh->client);
379 GNUNET_assert (NULL == vh->th);
380 if (NULL != vh->rr_head)
381 vh->th = GNUNET_CLIENT_notify_transmit_ready (vh->client,
382 sizeof (struct RedirectToServiceRequestMessage),
383 GNUNET_TIME_UNIT_FOREVER_REL,
391 * Disconnect from the service (communication error) and reconnect later.
393 * @param vh handle to reconnect.
396 reconnect (struct GNUNET_VPN_Handle *vh)
398 struct GNUNET_VPN_RedirectionRequest *rr;
402 GNUNET_CLIENT_notify_transmit_ready_cancel (vh->th);
405 GNUNET_CLIENT_disconnect (vh->client);
407 vh->request_id_gen = 0;
408 for (rr = vh->rr_head; NULL != rr; rr = rr->next)
410 vh->backoff = GNUNET_TIME_relative_max (GNUNET_TIME_UNIT_MILLISECONDS,
411 GNUNET_TIME_relative_min (GNUNET_TIME_relative_multiply (vh->backoff, 2),
412 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)));
413 vh->rt = GNUNET_SCHEDULER_add_delayed (vh->backoff,
420 * Cancel redirection request with the service.
422 * @param rr request to cancel
425 GNUNET_VPN_cancel_request (struct GNUNET_VPN_RedirectionRequest *rr)
427 struct GNUNET_VPN_Handle *vh;
430 GNUNET_CONTAINER_DLL_remove (vh->rr_head,
438 * Tell the VPN that a forwarding to a particular peer offering a
439 * particular service is requested. The VPN is to reserve a
440 * particular IP for the redirection and return it. The VPN will
441 * begin the redirection as soon as possible and maintain it as long
442 * as it is actively used and keeping it is feasible. Given resource
443 * limitations, the longest inactive mappings will be destroyed.
445 * @param vh VPN handle
446 * @param result_af desired address family for the returned allocation
447 * can also be AF_UNSPEC
448 * @param protocol protocol, IPPROTO_UDP or IPPROTO_TCP
449 * @param peer target peer for the redirection
450 * @param serv service descriptor to give to the peer
451 * @param nac GNUNET_YES to notify via callback only after completion of
452 * the MESH-level connection,
453 * GNUNET_NO to notify as soon as the IP has been reserved
454 * @param expiration_time at what time should the redirection expire?
455 * (this should not impact connections that are active at that time)
456 * @param cb function to call with the IP
457 * @param cb_cls closure for cb
458 * @return handle to cancel the request (means the callback won't be
459 * invoked anymore; the mapping may or may not be established
462 struct GNUNET_VPN_RedirectionRequest *
463 GNUNET_VPN_redirect_to_peer (struct GNUNET_VPN_Handle *vh,
466 const struct GNUNET_PeerIdentity *peer,
467 const struct GNUNET_HashCode *serv,
469 struct GNUNET_TIME_Absolute expiration_time,
470 GNUNET_VPN_AllocationCallback cb,
473 struct GNUNET_VPN_RedirectionRequest *rr;
475 rr = GNUNET_malloc (sizeof (struct GNUNET_VPN_RedirectionRequest));
481 rr->expiration_time = expiration_time;
482 rr->result_af = result_af;
484 rr->protocol = protocol;
491 * Tell the VPN that forwarding to the Internet via some exit node is
492 * requested. Note that both UDP and TCP traffic will be forwarded,
493 * but possibly to different exit nodes. The VPN is to reserve a
494 * particular IP for the redirection and return it. The VPN will
495 * begin the redirection as soon as possible and maintain it as long
496 * as it is actively used and keeping it is feasible. Given resource
497 * limitations, the longest inactive mappings will be destroyed.
499 * @param vh VPN handle
500 * @param result_af desired address family for the returned allocation
501 * @param addr_af address family for 'addr', AF_INET or AF_INET6
502 * @param addr destination IP address on the Internet; destination
503 * port is to be taken from the VPN packet itself
504 * @param nac GNUNET_YES to notify via callback only after completion of
505 * the MESH-level connection,
506 * GNUNET_NO to notify as soon as the IP has been reserved
507 * @param expiration_time at what time should the redirection expire?
508 * (this should not impact connections that are active at that time)
509 * @param cb function to call with the IP
510 * @param cb_cls closure for cb
511 * @return handle to cancel the request (means the callback won't be
512 * invoked anymore; the mapping may or may not be established
515 struct GNUNET_VPN_RedirectionRequest *
516 GNUNET_VPN_redirect_to_ip (struct GNUNET_VPN_Handle *vh,
521 struct GNUNET_TIME_Absolute expiration_time,
522 GNUNET_VPN_AllocationCallback cb,
525 struct GNUNET_VPN_RedirectionRequest *rr;
531 alen = sizeof (struct in_addr);
534 alen = sizeof (struct in6_addr);
540 rr = GNUNET_malloc (sizeof (struct GNUNET_VPN_RedirectionRequest) + alen);
545 rr->expiration_time = expiration_time;
546 rr->result_af = result_af;
547 rr->addr_af = addr_af;
549 memcpy (&rr[1], addr, alen);
556 * Connect to the VPN service
558 * @param cfg configuration to use
561 struct GNUNET_VPN_Handle *
562 GNUNET_VPN_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
564 struct GNUNET_VPN_Handle *vh;
566 vh = GNUNET_malloc (sizeof (struct GNUNET_VPN_Handle));
568 vh->client = GNUNET_CLIENT_connect ("vpn", cfg);
569 if (NULL == vh->client)
579 * Disconnect from the VPN service.
581 * @param vh VPN handle
584 GNUNET_VPN_disconnect (struct GNUNET_VPN_Handle *vh)
586 GNUNET_assert (NULL == vh->rr_head);
589 GNUNET_CLIENT_notify_transmit_ready_cancel (vh->th);
592 if (NULL != vh->client)
594 GNUNET_CLIENT_disconnect (vh->client);
597 if (GNUNET_SCHEDULER_NO_TASK != vh->rt)
599 GNUNET_SCHEDULER_cancel (vh->rt);
600 vh->rt = GNUNET_SCHEDULER_NO_TASK;
605 /* end of vpn_api.c */