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 * 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 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 'addr'. AF_INET or AF_INET6.
141 * GNUNET_YES if we are to call the callback only after successful
142 * mesh tunnel creation.
147 * For service redirection, IPPROT_UDP or IPPROTO_TCP.
155 * Disconnect from the service (communication error) and reconnect later.
157 * @param vh handle to reconnect.
160 reconnect (struct GNUNET_VPN_Handle *vh);
164 * Function called when we receive a message from the VPN service.
166 * @param cls the 'struct GNUNET_VPN_Handle'
167 * @param msg message received, NULL on timeout or fatal error
170 receive_response (void *cls,
171 const struct GNUNET_MessageHeader* msg)
173 struct GNUNET_VPN_Handle *vh = cls;
174 const struct RedirectToIpResponseMessage *rm;
175 struct GNUNET_VPN_RedirectionRequest *rr;
185 if ( (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_VPN_CLIENT_USE_IP) ||
186 (sizeof (struct RedirectToIpResponseMessage) > (msize = ntohs (msg->size))) )
192 rm = (const struct RedirectToIpResponseMessage *) msg;
193 af = (int) ntohl (rm->result_af);
200 alen = sizeof (struct in_addr);
203 alen = sizeof (struct in6_addr);
210 if ( (msize != alen + sizeof (struct RedirectToIpResponseMessage)) ||
211 (0 == rm->request_id) )
217 for (rr = vh->rr_head; NULL != rr; rr = rr->next)
219 if (rr->request_id == rm->request_id)
221 GNUNET_CONTAINER_DLL_remove (vh->rr_head,
226 (af == AF_UNSPEC) ? NULL : &rm[1]);
231 GNUNET_CLIENT_receive (vh->client,
232 &receive_response, vh,
233 GNUNET_TIME_UNIT_FOREVER_REL);
238 * We're ready to transmit a request to the VPN service. Do it.
240 * @param cls the 'struct GNUNET_VPN_Handle*'
241 * @param size number of bytes available in buf
242 * @param buf where to copy the request
243 * @return number of bytes copied to 'buf'
246 transmit_request (void *cls,
250 struct GNUNET_VPN_Handle *vh = cls;
251 struct GNUNET_VPN_RedirectionRequest *rr;
252 struct RedirectToIpRequestMessage rip;
253 struct RedirectToServiceRequestMessage rs;
259 /* find a pending request */
261 while ( (NULL != rr) &&
262 (0 != rr->request_id) )
268 /* if first request, start receive loop */
269 if (0 == vh->request_id_gen)
270 GNUNET_CLIENT_receive (vh->client,
271 &receive_response, vh,
272 GNUNET_TIME_UNIT_FOREVER_REL);
273 if (NULL == rr->addr)
275 ret = sizeof (struct RedirectToServiceRequestMessage);
276 GNUNET_assert (ret <= size);
277 rs.header.size = htons ((uint16_t) ret);
278 rs.header.type = htons (GNUNET_MESSAGE_TYPE_VPN_CLIENT_REDIRECT_TO_SERVICE);
279 rs.nac = htonl (rr->nac);
280 rs.expiration_time = GNUNET_TIME_absolute_hton (rr->expiration_time);
281 rs.protocol = htonl (rr->protocol);
282 rs.result_af = htonl (rr->result_af);
283 rs.target = rr->peer;
284 rs.service_descriptor = rr->serv;
285 rs.request_id = rr->request_id = ++vh->request_id_gen;
286 memcpy (buf, &rs, sizeof (struct RedirectToServiceRequestMessage));
293 alen = sizeof (struct in_addr);
296 alen = sizeof (struct in6_addr);
302 ret = alen + sizeof (struct RedirectToIpRequestMessage);
303 GNUNET_assert (ret <= size);
304 rip.header.size = htons ((uint16_t) ret);
305 rip.header.type = htons (GNUNET_MESSAGE_TYPE_VPN_CLIENT_REDIRECT_TO_IP);
306 rip.nac = htonl (rr->nac);
307 rip.expiration_time = GNUNET_TIME_absolute_hton (rr->expiration_time);
308 rip.result_af = htonl (rr->result_af);
309 rip.addr_af = htonl (rr->addr_af);
310 rip.request_id = rr->request_id = ++vh->request_id_gen;
312 memcpy (cbuf, &rip, sizeof (struct RedirectToIpRequestMessage));
313 memcpy (&cbuf[sizeof (struct RedirectToIpRequestMessage)], rr->addr, alen);
315 /* test if there are more pending requests */
316 while ( (NULL != rr) &&
317 (0 != rr->request_id) )
320 vh->th = GNUNET_CLIENT_notify_transmit_ready (vh->client,
321 sizeof (struct RedirectToServiceRequestMessage),
322 GNUNET_TIME_UNIT_FOREVER_REL,
331 * Add a request to our request queue and transmit it.
333 * @param rr request to queue and transmit.
336 queue_request (struct GNUNET_VPN_RedirectionRequest *rr)
338 struct GNUNET_VPN_Handle *vh;
341 GNUNET_CONTAINER_DLL_insert_tail (vh->rr_head,
344 if ( (NULL == vh->th) &&
345 (NULL != vh->client) )
346 vh->th = GNUNET_CLIENT_notify_transmit_ready (vh->client,
347 sizeof (struct RedirectToServiceRequestMessage),
348 GNUNET_TIME_UNIT_FOREVER_REL,
356 * Connect to the VPN service and start again to transmit our requests.
358 * @param cls the 'struct GNUNET_VPN_Handle *'
359 * @param tc scheduler context
362 connect_task (void *cls,
363 const struct GNUNET_SCHEDULER_TaskContext *tc)
365 struct GNUNET_VPN_Handle *vh = cls;
367 vh->client = GNUNET_CLIENT_connect ("vpn", vh->cfg);
368 GNUNET_assert (NULL != vh->client);
369 GNUNET_assert (NULL != vh->th);
370 if (NULL != vh->rr_head)
371 vh->th = GNUNET_CLIENT_notify_transmit_ready (vh->client,
372 sizeof (struct RedirectToServiceRequestMessage),
373 GNUNET_TIME_UNIT_FOREVER_REL,
381 * Disconnect from the service (communication error) and reconnect later.
383 * @param vh handle to reconnect.
386 reconnect (struct GNUNET_VPN_Handle *vh)
388 struct GNUNET_VPN_RedirectionRequest *rr;
392 GNUNET_CLIENT_notify_transmit_ready_cancel (vh->th);
395 GNUNET_CLIENT_disconnect (vh->client, GNUNET_NO);
397 vh->request_id_gen = 0;
398 for (rr = vh->rr_head; NULL != rr; rr = rr->next)
400 vh->rt = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
407 * Cancel redirection request with the service.
409 * @param rr request to cancel
412 GNUNET_VPN_cancel_request (struct GNUNET_VPN_RedirectionRequest *rr)
414 struct GNUNET_VPN_Handle *vh;
417 GNUNET_CONTAINER_DLL_remove (vh->rr_head,
425 * Tell the VPN that a forwarding to a particular peer offering a
426 * particular service is requested. The VPN is to reserve a
427 * particular IP for the redirection and return it. The VPN will
428 * begin the redirection as soon as possible and maintain it as long
429 * as it is actively used and keeping it is feasible. Given resource
430 * limitations, the longest inactive mappings will be destroyed.
432 * @param vh VPN handle
433 * @param result_af desired address family for the returned allocation
434 * can also be AF_UNSPEC
435 * @param protocol protocol, IPPROTO_UDP or IPPROTO_TCP
436 * @param peer target peer for the redirection
437 * @param serv service descriptor to give to the peer
438 * @param nac GNUNET_YES to notify via callback only after completion of
439 * the MESH-level connection,
440 * GNUNET_NO to notify as soon as the IP has been reserved
441 * @param expiration_time at what time should the redirection expire?
442 * (this should not impact connections that are active at that time)
443 * @param cb function to call with the IP
444 * @param cb_cls closure for cb
445 * @return handle to cancel the request (means the callback won't be
446 * invoked anymore; the mapping may or may not be established
449 struct GNUNET_VPN_RedirectionRequest *
450 GNUNET_VPN_redirect_to_peer (struct GNUNET_VPN_Handle *vh,
453 const struct GNUNET_PeerIdentity *peer,
454 const GNUNET_HashCode *serv,
456 struct GNUNET_TIME_Absolute expiration_time,
457 GNUNET_VPN_AllocationCallback cb,
460 struct GNUNET_VPN_RedirectionRequest *rr;
462 rr = GNUNET_malloc (sizeof (struct GNUNET_VPN_RedirectionRequest));
468 rr->expiration_time = expiration_time;
469 rr->result_af = result_af;
471 rr->protocol = protocol;
478 * Tell the VPN that forwarding to the Internet via some exit node is
479 * requested. Note that both UDP and TCP traffic will be forwarded,
480 * but possibly to different exit nodes. The VPN is to reserve a
481 * particular IP for the redirection and return it. The VPN will
482 * begin the redirection as soon as possible and maintain it as long
483 * as it is actively used and keeping it is feasible. Given resource
484 * limitations, the longest inactive mappings will be destroyed.
486 * @param vh VPN handle
487 * @param result_af desired address family for the returned allocation
488 * @param addr_af address family for 'addr', AF_INET or AF_INET6
489 * @param addr destination IP address on the Internet; destination
490 * port is to be taken from the VPN packet itself
491 * @param nac GNUNET_YES to notify via callback only after completion of
492 * the MESH-level connection,
493 * GNUNET_NO to notify as soon as the IP has been reserved
494 * @param expiration_time at what time should the redirection expire?
495 * (this should not impact connections that are active at that time)
496 * @param cb function to call with the IP
497 * @param cb_cls closure for cb
498 * @return handle to cancel the request (means the callback won't be
499 * invoked anymore; the mapping may or may not be established
502 struct GNUNET_VPN_RedirectionRequest *
503 GNUNET_VPN_redirect_to_ip (struct GNUNET_VPN_Handle *vh,
508 struct GNUNET_TIME_Absolute expiration_time,
509 GNUNET_VPN_AllocationCallback cb,
512 struct GNUNET_VPN_RedirectionRequest *rr;
518 alen = sizeof (struct in_addr);
521 alen = sizeof (struct in6_addr);
527 rr = GNUNET_malloc (sizeof (struct GNUNET_VPN_RedirectionRequest) + alen);
532 rr->expiration_time = expiration_time;
533 rr->result_af = result_af;
534 rr->addr_af = addr_af;
536 memcpy (&rr[1], addr, alen);
543 * Connect to the VPN service
545 * @param cfg configuration to use
548 struct GNUNET_VPN_Handle *
549 GNUNET_VPN_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
551 struct GNUNET_VPN_Handle *vh;
553 vh = GNUNET_malloc (sizeof (struct GNUNET_VPN_Handle));
555 vh->client = GNUNET_CLIENT_connect ("vpn", cfg);
556 if (NULL == vh->client)
566 * Disconnect from the VPN service.
568 * @param vh VPN handle
571 GNUNET_VPN_disconnect (struct GNUNET_VPN_Handle *vh)
573 GNUNET_assert (NULL == vh->rr_head);
576 GNUNET_CLIENT_notify_transmit_ready_cancel (vh->th);
579 if (NULL != vh->client)
581 GNUNET_CLIENT_disconnect (vh->client, GNUNET_NO);
584 if (GNUNET_SCHEDULER_NO_TASK != vh->rt)
586 GNUNET_SCHEDULER_cancel (vh->rt);
587 vh->rt = GNUNET_SCHEDULER_NO_TASK;
592 /* end of vpn_api.c */