b6fab37bf27577c41bc1fc417e513658cae9c7d2
[oweals/gnunet.git] / src / vpn / vpn_api.c
1 /*
2      This file is part of GNUnet.
3      (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., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, 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    * Head of list of active redirection requests.
48    */
49   struct GNUNET_VPN_RedirectionRequest *rr_head;
50
51   /**
52    * Tail of list of active redirection requests.
53    */
54   struct GNUNET_VPN_RedirectionRequest *rr_tail;
55 };
56
57
58 /**
59  * Opaque redirection request handle.
60  */
61 struct GNUNET_VPN_RedirectionRequest
62 {
63   /**
64    * Element in DLL.
65    */
66   struct GNUNET_VPN_RedirectionRequest *next;
67
68   /**
69    * Element in DLL.
70    */
71   struct GNUNET_VPN_RedirectionRequest *prev;
72
73   /**
74    * Pointer to the VPN struct.
75    */
76   struct GNUNET_VPN_Handle *vh;
77
78   /**
79    * Target IP address for the redirection, or NULL for
80    * redirection to service.  Allocated after this struct.
81    */
82   const void *addr;
83
84   /**
85    * Function to call with the designated IP address.
86    */
87   GNUNET_VPN_AllocationCallback cb;
88   
89   /**
90    * Closure for 'cb'.
91    */
92   void *cb_cls;
93
94   /**
95    * For service redirection, identity of the peer offering the service.
96    */
97   struct GNUNET_PeerIdentity peer;
98
99   /**
100    * For service redirection, service descriptor.
101    */
102   GNUNET_HashCode serv;              
103
104   /**
105    * At what time should the created service mapping expire?
106    */
107   struct GNUNET_TIME_Absolute expiration_time;
108
109   /**
110    * Desired address family for the result.
111    */
112   int result_af;
113
114   /**
115    * Address family of 'addr'.  AF_INET or AF_INET6.
116    */
117   int addr_af;
118   
119   /**
120    * GNUNET_YES if we are to call the callback only after successful
121    * mesh tunnel creation.
122    */
123   int nac;
124   
125   /**
126    * For service redirection, IPPROT_UDP or IPPROTO_TCP.
127    */
128   uint8_t protocol;
129
130 };
131
132
133 /**
134  * Cancel redirection request with the service.
135  *
136  * @param rr request to cancel
137  */
138 void
139 GNUNET_VPN_cancel_request (struct GNUNET_VPN_RedirectionRequest *rr)
140 {
141   struct GNUNET_VPN_Handle *vh;
142
143   vh = rr->vh;
144   GNUNET_CONTAINER_DLL_remove (vh->rr_head,
145                                vh->rr_tail,
146                                rr);
147   GNUNET_free (rr);
148 }
149
150
151 /**
152  * Tell the VPN that a forwarding to a particular peer offering a
153  * particular service is requested.  The VPN is to reserve a
154  * particular IP for the redirection and return it.  The VPN will
155  * begin the redirection as soon as possible and maintain it as long
156  * as it is actively used and keeping it is feasible.  Given resource
157  * limitations, the longest inactive mappings will be destroyed.
158  *
159  * @param vh VPN handle
160  * @param af address family, AF_INET or AF_INET6
161  * @param protocol protocol, IPPROTO_UDP or IPPROTO_TCP
162  * @param peer target peer for the redirection
163  * @param serv service descriptor to give to the peer
164  * @param nac GNUNET_YES to notify via callback only after completion of
165  *            the MESH-level connection,
166  *            GNUNET_NO to notify as soon as the IP has been reserved
167  * @param expiration_time at what time should the redirection expire?
168  *        (this should not impact connections that are active at that time)
169  * @param cb function to call with the IP
170  * @param cb_cls closure for cb
171  * @return handle to cancel the request (means the callback won't be
172  *         invoked anymore; the mapping may or may not be established
173  *         anyway)
174  */
175 struct GNUNET_VPN_RedirectionRequest *
176 GNUNET_VPN_redirect_to_peer (struct GNUNET_VPN_Handle *rh,                 
177                              int af,
178                              uint8_t protocol,
179                              const struct GNUNET_PeerIdentity *peer,
180                              const GNUNET_HashCode *serv,
181                              int nac,
182                              struct GNUNET_TIME_Absolute expiration_time,
183                              GNUNET_VPN_AllocationCallback cb,
184                              void *cb_cls)
185 {
186   return NULL; // FIXME
187 }
188
189                 
190 /**
191  * Tell the VPN that forwarding to the Internet via some exit node is
192  * requested.  Note that both UDP and TCP traffic will be forwarded,
193  * but possibly to different exit nodes.  The VPN is to reserve a
194  * particular IP for the redirection and return it.  The VPN will
195  * begin the redirection as soon as possible and maintain it as long
196  * as it is actively used and keeping it is feasible.  Given resource
197  * limitations, the longest inactive mappings will be destroyed.
198  *
199  * @param vh VPN handle
200  * @param result_af desired address family for the returned allocation
201  * @param addr_af address family for 'addr', AF_INET or AF_INET6
202  * @param addr destination IP address on the Internet; destination
203  *             port is to be taken from the VPN packet itself
204  * @param nac GNUNET_YES to notify via callback only after completion of
205  *            the MESH-level connection,
206  *            GNUNET_NO to notify as soon as the IP has been reserved
207  * @param expiration_time at what time should the redirection expire?
208  *        (this should not impact connections that are active at that time)
209  * @param cb function to call with the IP
210  * @param cb_cls closure for cb
211  * @return handle to cancel the request (means the callback won't be
212  *         invoked anymore; the mapping may or may not be established
213  *         anyway)
214  */
215 struct GNUNET_VPN_RedirectionRequest *
216 GNUNET_VPN_redirect_to_ip (struct GNUNET_VPN_Handle *rh,                   
217                            int result_af,
218                            int addr_af,
219                            const void *addr,
220                            int nac,
221                            struct GNUNET_TIME_Absolute expiration_time,
222                            GNUNET_VPN_AllocationCallback cb,
223                            void *cb_cls)
224 {
225   return NULL; // FIXME
226 }
227
228
229 /**
230  * Connect to the VPN service
231  *
232  * @param cfg configuration to use
233  * @return VPN handle 
234  */
235 struct GNUNET_VPN_Handle *
236 GNUNET_VPN_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
237 {
238   struct GNUNET_VPN_Handle *vh;
239
240   vh = GNUNET_malloc (sizeof (struct GNUNET_VPN_Handle));
241   vh->cfg = cfg;
242   vh->client = GNUNET_CLIENT_connect ("vpn", cfg);
243   return vh;
244 }
245
246
247 /**
248  * Disconnect from the VPN service.
249  *
250  * @param vh VPN handle
251  */
252 void
253 GNUNET_VPN_disconnect (struct GNUNET_VPN_Handle *vh)
254 {
255   GNUNET_assert (NULL == vh->rr_head);
256   GNUNET_CLIENT_disconnect (vh->client, GNUNET_NO);
257   GNUNET_free (vh);
258 }
259
260 /* end of vpn_api.c */