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 it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 * @brief library to access the VPN service and tell it how to redirect traffic
22 * @author Christian Grothoff
25 #include "gnunet_vpn_service.h"
32 struct GNUNET_VPN_Handle
35 * Configuration we use.
37 const struct GNUNET_CONFIGURATION_Handle *cfg;
40 * Connection to VPN service.
42 struct GNUNET_MQ_Handle *mq;
45 * Head of list of active redirection requests.
47 struct GNUNET_VPN_RedirectionRequest *rr_head;
50 * Tail of list of active redirection requests.
52 struct GNUNET_VPN_RedirectionRequest *rr_tail;
55 * Identifier of a reconnect task.
57 struct GNUNET_SCHEDULER_Task *rt;
60 * How long do we wait until we try to reconnect?
62 struct GNUNET_TIME_Relative backoff;
65 * ID of the last request that was submitted to the service.
67 uint64_t request_id_gen;
73 * Opaque redirection request handle.
75 struct GNUNET_VPN_RedirectionRequest
80 struct GNUNET_VPN_RedirectionRequest *next;
85 struct GNUNET_VPN_RedirectionRequest *prev;
88 * Pointer to the VPN struct.
90 struct GNUNET_VPN_Handle *vh;
93 * Target IP address for the redirection, or NULL for
94 * redirection to service. Allocated after this struct.
99 * Function to call with the designated IP address.
101 GNUNET_VPN_AllocationCallback cb;
109 * For service redirection, identity of the peer offering the service.
111 struct GNUNET_PeerIdentity peer;
114 * For service redirection, service descriptor.
116 struct GNUNET_HashCode serv;
119 * At what time should the created service mapping expire?
121 struct GNUNET_TIME_Absolute expiration_time;
124 * non-zero if this request has been sent to the service.
129 * Desired address family for the result.
134 * Address family of @e addr. AF_INET or AF_INET6.
139 * For service redirection, IPPROT_UDP or IPPROTO_TCP.
147 * Disconnect from the service (communication error) and reconnect later.
149 * @param vh handle to reconnect.
152 reconnect (struct GNUNET_VPN_Handle *vh);
156 * Check a #GNUNET_MESSAGE_TYPE_VPN_CLIENT_USE_IP message from the
159 * @param cls the `struct GNUNET_VPN_Handle`
160 * @param rm message received
161 * @return #GNUNET_OK if @a rm is well-formed
164 check_use_ip (void *cls,
165 const struct RedirectToIpResponseMessage *rm)
170 af = (int) ntohl (rm->result_af);
177 alen = sizeof (struct in_addr);
180 alen = sizeof (struct in6_addr);
184 return GNUNET_SYSERR;
186 if ( (ntohs (rm->header.size) != alen + sizeof (*rm)) ||
187 (0 == rm->request_id) )
190 return GNUNET_SYSERR;
197 * Handle a #GNUNET_MESSAGE_TYPE_VPN_CLIENT_USE_IP message from the
200 * @param cls the `struct GNUNET_VPN_Handle`
201 * @param rm message received
204 handle_use_ip (void *cls,
205 const struct RedirectToIpResponseMessage *rm)
207 struct GNUNET_VPN_Handle *vh = cls;
208 struct GNUNET_VPN_RedirectionRequest *rr;
211 af = (int) ntohl (rm->result_af);
212 for (rr = vh->rr_head; NULL != rr; rr = rr->next)
214 if (rr->request_id == rm->request_id)
216 GNUNET_CONTAINER_DLL_remove (vh->rr_head,
221 (af == AF_UNSPEC) ? NULL : &rm[1]);
230 * Add a request to our request queue and transmit it.
232 * @param rr request to queue and transmit.
235 send_request (struct GNUNET_VPN_RedirectionRequest *rr)
237 struct GNUNET_VPN_Handle *vh = rr->vh;
238 struct RedirectToIpRequestMessage *rip;
239 struct RedirectToServiceRequestMessage *rs;
240 struct GNUNET_MQ_Envelope *env;
245 if (NULL == rr->addr)
247 env = GNUNET_MQ_msg (rs,
248 GNUNET_MESSAGE_TYPE_VPN_CLIENT_REDIRECT_TO_SERVICE);
249 rs->reserved = htonl (0);
250 rs->expiration_time = GNUNET_TIME_absolute_hton (rr->expiration_time);
251 rs->protocol = htonl (rr->protocol);
252 rs->result_af = htonl (rr->result_af);
253 rs->target = rr->peer;
254 rs->service_descriptor = rr->serv;
255 rs->request_id = rr->request_id = ++vh->request_id_gen;
262 alen = sizeof (struct in_addr);
265 alen = sizeof (struct in6_addr);
271 env = GNUNET_MQ_msg_extra (rip,
273 GNUNET_MESSAGE_TYPE_VPN_CLIENT_REDIRECT_TO_IP);
274 rip->reserved = htonl (0);
275 rip->expiration_time = GNUNET_TIME_absolute_hton (rr->expiration_time);
276 rip->result_af = htonl (rr->result_af);
277 rip->addr_af = htonl (rr->addr_af);
278 rip->request_id = rr->request_id = ++vh->request_id_gen;
279 GNUNET_memcpy (&rip[1],
283 GNUNET_MQ_send (vh->mq,
289 * Generic error handler, called with the appropriate error code and
290 * the same closure specified at the creation of the message queue.
291 * Not every message queue implementation supports an error handler.
293 * @param cls closure with the `struct GNUNET_VPN_Handle *`
294 * @param error error code
297 mq_error_handler (void *cls,
298 enum GNUNET_MQ_Error error)
300 struct GNUNET_VPN_Handle *vh = cls;
307 * Connect to the VPN service and start again to transmit our requests.
309 * @param cls the `struct GNUNET_VPN_Handle *`
312 connect_task (void *cls)
314 struct GNUNET_VPN_Handle *vh = cls;
315 struct GNUNET_MQ_MessageHandler handlers[] = {
316 GNUNET_MQ_hd_var_size (use_ip,
317 GNUNET_MESSAGE_TYPE_VPN_CLIENT_USE_IP,
318 struct RedirectToIpResponseMessage,
320 GNUNET_MQ_handler_end ()
322 struct GNUNET_VPN_RedirectionRequest *rr;
325 vh->mq = GNUNET_CLIENT_connect (vh->cfg,
332 for (rr = vh->rr_head; NULL != rr; rr = rr->next)
338 * Disconnect from the service (communication error) and reconnect later.
340 * @param vh handle to reconnect.
343 reconnect (struct GNUNET_VPN_Handle *vh)
345 struct GNUNET_VPN_RedirectionRequest *rr;
347 GNUNET_MQ_destroy (vh->mq);
349 vh->request_id_gen = 0;
350 for (rr = vh->rr_head; NULL != rr; rr = rr->next)
352 vh->backoff = GNUNET_TIME_relative_max (GNUNET_TIME_UNIT_MILLISECONDS,
353 GNUNET_TIME_relative_min (GNUNET_TIME_relative_saturating_multiply (vh->backoff, 2),
354 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)));
355 vh->rt = GNUNET_SCHEDULER_add_delayed (vh->backoff,
362 * Cancel redirection request with the service.
364 * @param rr request to cancel
367 GNUNET_VPN_cancel_request (struct GNUNET_VPN_RedirectionRequest *rr)
369 struct GNUNET_VPN_Handle *vh;
372 GNUNET_CONTAINER_DLL_remove (vh->rr_head,
380 * Tell the VPN that a forwarding to a particular peer offering a
381 * particular service is requested. The VPN is to reserve a
382 * particular IP for the redirection and return it. The VPN will
383 * begin the redirection as soon as possible and maintain it as long
384 * as it is actively used and keeping it is feasible. Given resource
385 * limitations, the longest inactive mappings will be destroyed.
387 * @param vh VPN handle
388 * @param result_af desired address family for the returned allocation
389 * can also be AF_UNSPEC
390 * @param protocol protocol, IPPROTO_UDP or IPPROTO_TCP
391 * @param peer target peer for the redirection
392 * @param serv service descriptor to give to the peer
393 * @param expiration_time at what time should the redirection expire?
394 * (this should not impact connections that are active at that time)
395 * @param cb function to call with the IP
396 * @param cb_cls closure for @a cb
397 * @return handle to cancel the request (means the callback won't be
398 * invoked anymore; the mapping may or may not be established
401 struct GNUNET_VPN_RedirectionRequest *
402 GNUNET_VPN_redirect_to_peer (struct GNUNET_VPN_Handle *vh,
405 const struct GNUNET_PeerIdentity *peer,
406 const struct GNUNET_HashCode *serv,
407 struct GNUNET_TIME_Absolute expiration_time,
408 GNUNET_VPN_AllocationCallback cb,
411 struct GNUNET_VPN_RedirectionRequest *rr;
413 rr = GNUNET_new (struct GNUNET_VPN_RedirectionRequest);
419 rr->expiration_time = expiration_time;
420 rr->result_af = result_af;
421 rr->protocol = protocol;
422 GNUNET_CONTAINER_DLL_insert_tail (vh->rr_head,
431 * Tell the VPN that forwarding to the Internet via some exit node is
432 * requested. Note that both UDP and TCP traffic will be forwarded,
433 * but possibly to different exit nodes. 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 * @param addr_af address family for @a addr, AF_INET or AF_INET6
442 * @param addr destination IP address on the Internet; destination
443 * port is to be taken from the VPN packet itself
444 * @param expiration_time at what time should the redirection expire?
445 * (this should not impact connections that are active at that time)
446 * @param cb function to call with the IP
447 * @param cb_cls closure for @a cb
448 * @return handle to cancel the request (means the callback won't be
449 * invoked anymore; the mapping may or may not be established
452 struct GNUNET_VPN_RedirectionRequest *
453 GNUNET_VPN_redirect_to_ip (struct GNUNET_VPN_Handle *vh,
457 struct GNUNET_TIME_Absolute expiration_time,
458 GNUNET_VPN_AllocationCallback cb,
461 struct GNUNET_VPN_RedirectionRequest *rr;
467 alen = sizeof (struct in_addr);
470 alen = sizeof (struct in6_addr);
476 rr = GNUNET_malloc (sizeof (struct GNUNET_VPN_RedirectionRequest) + alen);
481 rr->expiration_time = expiration_time;
482 rr->result_af = result_af;
483 rr->addr_af = addr_af;
484 GNUNET_memcpy (&rr[1],
487 GNUNET_CONTAINER_DLL_insert_tail (vh->rr_head,
496 * Connect to the VPN service
498 * @param cfg configuration to use
501 struct GNUNET_VPN_Handle *
502 GNUNET_VPN_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
504 struct GNUNET_VPN_Handle *vh
505 = GNUNET_new (struct GNUNET_VPN_Handle);
519 * Disconnect from the VPN service.
521 * @param vh VPN handle
524 GNUNET_VPN_disconnect (struct GNUNET_VPN_Handle *vh)
526 GNUNET_assert (NULL == vh->rr_head);
529 GNUNET_MQ_destroy (vh->mq);
534 GNUNET_SCHEDULER_cancel (vh->rt);
540 /* end of vpn_api.c */