-improve indentation, reduce duplication of PIDs in core's neighbour map
[oweals/gnunet.git] / src / vpn / vpn_api.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2012 Christian Grothoff
4
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.
9
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.
14
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.
19 */
20
21 /**
22  * @file vpn/vpn_api.c
23  * @brief library to access the VPN service and tell it how to redirect traffic
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
27 #include "gnunet_vpn_service.h"
28 #include "vpn.h"
29
30
31 /**
32  * Opaque VPN handle
33  */
34 struct GNUNET_VPN_Handle
35 {
36   /**
37    * Configuration we use.
38    */
39   const struct GNUNET_CONFIGURATION_Handle *cfg;
40
41   /**
42    * Connection to VPN service.
43    */
44   struct GNUNET_CLIENT_Connection *client;
45
46   /**
47    * Active transmission request.
48    */
49   struct GNUNET_CLIENT_TransmitHandle *th;
50
51   /**
52    * Head of list of active redirection requests.
53    */
54   struct GNUNET_VPN_RedirectionRequest *rr_head;
55
56   /**
57    * Tail of list of active redirection requests.
58    */
59   struct GNUNET_VPN_RedirectionRequest *rr_tail;
60
61   /**
62    * Identifier of a reconnect task.
63    */
64   struct GNUNET_SCHEDULER_Task * rt;
65
66   /**
67    * How long do we wait until we try to reconnect?
68    */
69   struct GNUNET_TIME_Relative backoff;
70
71   /**
72    * ID of the last request that was submitted to the service.
73    */
74   uint64_t request_id_gen;
75
76 };
77
78
79 /**
80  * Opaque redirection request handle.
81  */
82 struct GNUNET_VPN_RedirectionRequest
83 {
84   /**
85    * Element in DLL.
86    */
87   struct GNUNET_VPN_RedirectionRequest *next;
88
89   /**
90    * Element in DLL.
91    */
92   struct GNUNET_VPN_RedirectionRequest *prev;
93
94   /**
95    * Pointer to the VPN struct.
96    */
97   struct GNUNET_VPN_Handle *vh;
98
99   /**
100    * Target IP address for the redirection, or NULL for
101    * redirection to service.  Allocated after this struct.
102    */
103   const void *addr;
104
105   /**
106    * Function to call with the designated IP address.
107    */
108   GNUNET_VPN_AllocationCallback cb;
109
110   /**
111    * Closure for 'cb'.
112    */
113   void *cb_cls;
114
115   /**
116    * For service redirection, identity of the peer offering the service.
117    */
118   struct GNUNET_PeerIdentity peer;
119
120   /**
121    * For service redirection, service descriptor.
122    */
123   struct GNUNET_HashCode serv;
124
125   /**
126    * At what time should the created service mapping expire?
127    */
128   struct GNUNET_TIME_Absolute expiration_time;
129
130   /**
131    * non-zero if this request has been sent to the service.
132    */
133   uint64_t request_id;
134
135   /**
136    * Desired address family for the result.
137    */
138   int result_af;
139
140   /**
141    * Address family of 'addr'.  AF_INET or AF_INET6.
142    */
143   int addr_af;
144
145   /**
146    * For service redirection, IPPROT_UDP or IPPROTO_TCP.
147    */
148   uint8_t protocol;
149
150 };
151
152
153 /**
154  * Disconnect from the service (communication error) and reconnect later.
155  *
156  * @param vh handle to reconnect.
157  */
158 static void
159 reconnect (struct GNUNET_VPN_Handle *vh);
160
161
162 /**
163  * Function called when we receive a message from the VPN service.
164  *
165  * @param cls the `struct GNUNET_VPN_Handle`
166  * @param msg message received, NULL on timeout or fatal error
167  */
168 static void
169 receive_response (void *cls,
170                   const struct GNUNET_MessageHeader* msg)
171 {
172   struct GNUNET_VPN_Handle *vh = cls;
173   const struct RedirectToIpResponseMessage *rm;
174   struct GNUNET_VPN_RedirectionRequest *rr;
175   size_t msize;
176   size_t alen;
177   int af;
178
179   if (NULL == msg)
180   {
181     reconnect (vh);
182     return;
183   }
184   if ( (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_VPN_CLIENT_USE_IP) ||
185        (sizeof (struct RedirectToIpResponseMessage) > (msize = ntohs (msg->size))) )
186   {
187     GNUNET_break (0);
188     reconnect (vh);
189     return;
190   }
191   rm = (const struct RedirectToIpResponseMessage *) msg;
192   af = (int) ntohl (rm->result_af);
193   switch (af)
194   {
195   case AF_UNSPEC:
196     alen = 0;
197     break;
198   case AF_INET:
199     alen = sizeof (struct in_addr);
200     break;
201   case AF_INET6:
202     alen = sizeof (struct in6_addr);
203     break;
204   default:
205     GNUNET_break (0);
206     reconnect (vh);
207     return;
208   }
209   if ( (msize != alen + sizeof (struct RedirectToIpResponseMessage)) ||
210        (0 == rm->request_id) )
211   {
212     GNUNET_break (0);
213     reconnect (vh);
214     return;
215   }
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)
220   {
221     if (rr->request_id == rm->request_id)
222     {
223       GNUNET_CONTAINER_DLL_remove (vh->rr_head,
224                                    vh->rr_tail,
225                                    rr);
226       rr->cb (rr->cb_cls,
227               af,
228               (af == AF_UNSPEC) ? NULL : &rm[1]);
229       GNUNET_free (rr);
230       break;
231     }
232   }
233 }
234
235
236 /**
237  * We're ready to transmit a request to the VPN service. Do it.
238  *
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'
243  */
244 static size_t
245 transmit_request (void *cls,
246                   size_t size,
247                   void *buf)
248 {
249   struct GNUNET_VPN_Handle *vh = cls;
250   struct GNUNET_VPN_RedirectionRequest *rr;
251   struct RedirectToIpRequestMessage rip;
252   struct RedirectToServiceRequestMessage rs;
253   char *cbuf;
254   size_t alen;
255   size_t ret;
256
257   vh->th = NULL;
258   /* find a pending request */
259   rr = vh->rr_head;
260   while ( (NULL != rr) &&
261           (0 != rr->request_id) )
262     rr = rr->next;
263   if (NULL == rr)
264     return 0;
265   if (0 == size)
266   {
267     reconnect (vh);
268     return 0;
269   }
270
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)
277   {
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));
290   }
291   else
292   {
293     switch (rr->addr_af)
294     {
295     case AF_INET:
296       alen = sizeof (struct in_addr);
297       break;
298     case AF_INET6:
299       alen = sizeof (struct in6_addr);
300       break;
301     default:
302       GNUNET_assert (0);
303       return 0;
304     }
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;
314     cbuf = buf;
315     memcpy (cbuf, &rip, sizeof (struct RedirectToIpRequestMessage));
316     memcpy (&cbuf[sizeof (struct RedirectToIpRequestMessage)], rr->addr, alen);
317   }
318   /* test if there are more pending requests */
319   while ( (NULL != rr) &&
320           (0 != rr->request_id) )
321     rr = rr->next;
322   if (NULL != rr)
323     vh->th = GNUNET_CLIENT_notify_transmit_ready (vh->client,
324                                                   sizeof (struct RedirectToServiceRequestMessage),
325                                                   GNUNET_TIME_UNIT_FOREVER_REL,
326                                                   GNUNET_NO,
327                                                   &transmit_request,
328                                                   vh);
329   return ret;
330 }
331
332
333 /**
334  * Add a request to our request queue and transmit it.
335  *
336  * @param rr request to queue and transmit.
337  */
338 static void
339 queue_request (struct GNUNET_VPN_RedirectionRequest *rr)
340 {
341   struct GNUNET_VPN_Handle *vh;
342
343   vh = rr->vh;
344   GNUNET_CONTAINER_DLL_insert_tail (vh->rr_head,
345                                     vh->rr_tail,
346                                     rr);
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,
352                                                   GNUNET_NO,
353                                                   &transmit_request,
354                                                   vh);
355 }
356
357
358 /**
359  * Connect to the VPN service and start again to transmit our requests.
360  *
361  * @param cls the 'struct GNUNET_VPN_Handle *'
362  * @param tc scheduler context
363  */
364 static void
365 connect_task (void *cls,
366               const struct GNUNET_SCHEDULER_TaskContext *tc)
367 {
368   struct GNUNET_VPN_Handle *vh = cls;
369
370   vh->rt = NULL;
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,
378                                                   GNUNET_NO,
379                                                   &transmit_request,
380                                                   vh);
381 }
382
383
384 /**
385  * Disconnect from the service (communication error) and reconnect later.
386  *
387  * @param vh handle to reconnect.
388  */
389 static void
390 reconnect (struct GNUNET_VPN_Handle *vh)
391 {
392   struct GNUNET_VPN_RedirectionRequest *rr;
393
394   if (NULL != vh->th)
395   {
396     GNUNET_CLIENT_notify_transmit_ready_cancel (vh->th);
397     vh->th = NULL;
398   }
399   GNUNET_CLIENT_disconnect (vh->client);
400   vh->client = NULL;
401   vh->request_id_gen = 0;
402   for (rr = vh->rr_head; NULL != rr; rr = rr->next)
403     rr->request_id = 0;
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,
408                                          &connect_task,
409                                          vh);
410 }
411
412
413 /**
414  * Cancel redirection request with the service.
415  *
416  * @param rr request to cancel
417  */
418 void
419 GNUNET_VPN_cancel_request (struct GNUNET_VPN_RedirectionRequest *rr)
420 {
421   struct GNUNET_VPN_Handle *vh;
422
423   vh = rr->vh;
424   GNUNET_CONTAINER_DLL_remove (vh->rr_head,
425                                vh->rr_tail,
426                                rr);
427   GNUNET_free (rr);
428 }
429
430
431 /**
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.
438  *
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
451  *         anyway)
452  */
453 struct GNUNET_VPN_RedirectionRequest *
454 GNUNET_VPN_redirect_to_peer (struct GNUNET_VPN_Handle *vh,
455                              int result_af,
456                              uint8_t protocol,
457                              const struct GNUNET_PeerIdentity *peer,
458                              const struct GNUNET_HashCode *serv,
459                              struct GNUNET_TIME_Absolute expiration_time,
460                              GNUNET_VPN_AllocationCallback cb,
461                              void *cb_cls)
462 {
463   struct GNUNET_VPN_RedirectionRequest *rr;
464
465   rr = GNUNET_new (struct GNUNET_VPN_RedirectionRequest);
466   rr->vh = vh;
467   rr->cb = cb;
468   rr->cb_cls = cb_cls;
469   rr->peer = *peer;
470   rr->serv = *serv;
471   rr->expiration_time = expiration_time;
472   rr->result_af = result_af;
473   rr->protocol = protocol;
474   queue_request (rr);
475   return rr;
476 }
477
478
479 /**
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.
487  *
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
499  *         anyway)
500  */
501 struct GNUNET_VPN_RedirectionRequest *
502 GNUNET_VPN_redirect_to_ip (struct GNUNET_VPN_Handle *vh,
503                            int result_af,
504                            int addr_af,
505                            const void *addr,
506                            struct GNUNET_TIME_Absolute expiration_time,
507                            GNUNET_VPN_AllocationCallback cb,
508                            void *cb_cls)
509 {
510   struct GNUNET_VPN_RedirectionRequest *rr;
511   size_t alen;
512
513   switch (addr_af)
514   {
515   case AF_INET:
516     alen = sizeof (struct in_addr);
517     break;
518   case AF_INET6:
519     alen = sizeof (struct in6_addr);
520     break;
521   default:
522     GNUNET_break (0);
523     return NULL;
524   }
525   rr = GNUNET_malloc (sizeof (struct GNUNET_VPN_RedirectionRequest) + alen);
526   rr->vh = vh;
527   rr->addr = &rr[1];
528   rr->cb = cb;
529   rr->cb_cls = cb_cls;
530   rr->expiration_time = expiration_time;
531   rr->result_af = result_af;
532   rr->addr_af = addr_af;
533   memcpy (&rr[1], addr, alen);
534   queue_request (rr);
535   return rr;
536 }
537
538
539 /**
540  * Connect to the VPN service
541  *
542  * @param cfg configuration to use
543  * @return VPN handle
544  */
545 struct GNUNET_VPN_Handle *
546 GNUNET_VPN_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
547 {
548   struct GNUNET_VPN_Handle *vh;
549
550   vh = GNUNET_new (struct GNUNET_VPN_Handle);
551   vh->cfg = cfg;
552   vh->client = GNUNET_CLIENT_connect ("vpn", cfg);
553   if (NULL == vh->client)
554   {
555     GNUNET_free (vh);
556     return NULL;
557   }
558   return vh;
559 }
560
561
562 /**
563  * Disconnect from the VPN service.
564  *
565  * @param vh VPN handle
566  */
567 void
568 GNUNET_VPN_disconnect (struct GNUNET_VPN_Handle *vh)
569 {
570   GNUNET_assert (NULL == vh->rr_head);
571   if (NULL != vh->th)
572   {
573     GNUNET_CLIENT_notify_transmit_ready_cancel (vh->th);
574     vh->th = NULL;
575   }
576   if (NULL != vh->client)
577   {
578     GNUNET_CLIENT_disconnect (vh->client);
579     vh->client = NULL;
580   }
581   if (NULL != vh->rt)
582   {
583     GNUNET_SCHEDULER_cancel (vh->rt);
584     vh->rt = NULL;
585   }
586   GNUNET_free (vh);
587 }
588
589 /* end of vpn_api.c */