diff options
Diffstat (limited to 'src/vpn/gnunet-vpn.c')
-rw-r--r-- | src/vpn/gnunet-vpn.c | 375 |
1 files changed, 189 insertions, 186 deletions
diff --git a/src/vpn/gnunet-vpn.c b/src/vpn/gnunet-vpn.c index 1651767f4..e5b79be6e 100644 --- a/src/vpn/gnunet-vpn.c +++ b/src/vpn/gnunet-vpn.c | |||
@@ -11,12 +11,12 @@ | |||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | 11 | WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | Affero General Public License for more details. | 13 | Affero General Public License for more details. |
14 | 14 | ||
15 | You should have received a copy of the GNU Affero General Public License | 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/>. | 16 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
17 | 17 | ||
18 | SPDX-License-Identifier: AGPL3.0-or-later | 18 | SPDX-License-Identifier: AGPL3.0-or-later |
19 | */ | 19 | */ |
20 | 20 | ||
21 | /** | 21 | /** |
22 | * @file src/vpn/gnunet-vpn.c | 22 | * @file src/vpn/gnunet-vpn.c |
@@ -88,28 +88,28 @@ static int ret; | |||
88 | /** | 88 | /** |
89 | * Option '-d': duration of the mapping | 89 | * Option '-d': duration of the mapping |
90 | */ | 90 | */ |
91 | static struct GNUNET_TIME_Relative duration = {5 * 60 * 1000}; | 91 | static struct GNUNET_TIME_Relative duration = { 5 * 60 * 1000 }; |
92 | 92 | ||
93 | 93 | ||
94 | /** | 94 | /** |
95 | * Shutdown. | 95 | * Shutdown. |
96 | */ | 96 | */ |
97 | static void | 97 | static void |
98 | do_disconnect (void *cls) | 98 | do_disconnect(void *cls) |
99 | { | 99 | { |
100 | if (NULL != request) | 100 | if (NULL != request) |
101 | { | 101 | { |
102 | GNUNET_VPN_cancel_request (request); | 102 | GNUNET_VPN_cancel_request(request); |
103 | request = NULL; | 103 | request = NULL; |
104 | } | 104 | } |
105 | if (NULL != handle) | 105 | if (NULL != handle) |
106 | { | 106 | { |
107 | GNUNET_VPN_disconnect (handle); | 107 | GNUNET_VPN_disconnect(handle); |
108 | handle = NULL; | 108 | handle = NULL; |
109 | } | 109 | } |
110 | GNUNET_free_non_null (peer_id); | 110 | GNUNET_free_non_null(peer_id); |
111 | GNUNET_free_non_null (service_name); | 111 | GNUNET_free_non_null(service_name); |
112 | GNUNET_free_non_null (target_ip); | 112 | GNUNET_free_non_null(target_ip); |
113 | } | 113 | } |
114 | 114 | ||
115 | 115 | ||
@@ -127,25 +127,27 @@ do_disconnect (void *cls) | |||
127 | * specified target peer; NULL on error | 127 | * specified target peer; NULL on error |
128 | */ | 128 | */ |
129 | static void | 129 | static void |
130 | allocation_cb (void *cls, int af, const void *address) | 130 | allocation_cb(void *cls, int af, const void *address) |
131 | { | 131 | { |
132 | char buf[INET6_ADDRSTRLEN]; | 132 | char buf[INET6_ADDRSTRLEN]; |
133 | 133 | ||
134 | request = NULL; | 134 | request = NULL; |
135 | switch (af) | 135 | switch (af) |
136 | { | 136 | { |
137 | case AF_INET6: | 137 | case AF_INET6: |
138 | case AF_INET: | 138 | case AF_INET: |
139 | fprintf (stdout, "%s\n", inet_ntop (af, address, buf, sizeof (buf))); | 139 | fprintf(stdout, "%s\n", inet_ntop(af, address, buf, sizeof(buf))); |
140 | break; | 140 | break; |
141 | case AF_UNSPEC: | 141 | |
142 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _ ("Error creating tunnel\n")); | 142 | case AF_UNSPEC: |
143 | ret = 1; | 143 | GNUNET_log(GNUNET_ERROR_TYPE_ERROR, _("Error creating tunnel\n")); |
144 | break; | 144 | ret = 1; |
145 | default: | 145 | break; |
146 | break; | 146 | |
147 | } | 147 | default: |
148 | GNUNET_SCHEDULER_shutdown (); | 148 | break; |
149 | } | ||
150 | GNUNET_SCHEDULER_shutdown(); | ||
149 | } | 151 | } |
150 | 152 | ||
151 | 153 | ||
@@ -158,10 +160,10 @@ allocation_cb (void *cls, int af, const void *address) | |||
158 | * @param cfg configuration | 160 | * @param cfg configuration |
159 | */ | 161 | */ |
160 | static void | 162 | static void |
161 | run (void *cls, | 163 | run(void *cls, |
162 | char *const *args, | 164 | char *const *args, |
163 | const char *cfgfile, | 165 | const char *cfgfile, |
164 | const struct GNUNET_CONFIGURATION_Handle *cfg) | 166 | const struct GNUNET_CONFIGURATION_Handle *cfg) |
165 | { | 167 | { |
166 | int dst_af; | 168 | int dst_af; |
167 | int req_af; | 169 | int req_af; |
@@ -173,188 +175,189 @@ run (void *cls, | |||
173 | uint8_t protocol; | 175 | uint8_t protocol; |
174 | struct GNUNET_TIME_Absolute etime; | 176 | struct GNUNET_TIME_Absolute etime; |
175 | 177 | ||
176 | etime = GNUNET_TIME_relative_to_absolute (duration); | 178 | etime = GNUNET_TIME_relative_to_absolute(duration); |
177 | GNUNET_SCHEDULER_add_shutdown (&do_disconnect, NULL); | 179 | GNUNET_SCHEDULER_add_shutdown(&do_disconnect, NULL); |
178 | handle = GNUNET_VPN_connect (cfg); | 180 | handle = GNUNET_VPN_connect(cfg); |
179 | if (NULL == handle) | 181 | if (NULL == handle) |
180 | goto error; | 182 | goto error; |
181 | req_af = AF_UNSPEC; | 183 | req_af = AF_UNSPEC; |
182 | if (ipv4) | 184 | if (ipv4) |
183 | { | ||
184 | if (ipv6) | ||
185 | { | 185 | { |
186 | fprintf (stderr, | 186 | if (ipv6) |
187 | _ ("Option `%s' makes no sense with option `%s'.\n"), | 187 | { |
188 | "-4", | 188 | fprintf(stderr, |
189 | "-6"); | 189 | _("Option `%s' makes no sense with option `%s'.\n"), |
190 | goto error; | 190 | "-4", |
191 | "-6"); | ||
192 | goto error; | ||
193 | } | ||
194 | req_af = AF_INET; | ||
191 | } | 195 | } |
192 | req_af = AF_INET; | ||
193 | } | ||
194 | if (ipv6) | 196 | if (ipv6) |
195 | req_af = AF_INET6; | 197 | req_af = AF_INET6; |
196 | 198 | ||
197 | if (NULL == target_ip) | 199 | if (NULL == target_ip) |
198 | { | ||
199 | if (NULL == service_name) | ||
200 | { | ||
201 | fprintf (stderr, _ ("Option `%s' or `%s' is required.\n"), "-i", "-s"); | ||
202 | goto error; | ||
203 | } | ||
204 | if (NULL == peer_id) | ||
205 | { | ||
206 | fprintf (stderr, | ||
207 | _ ("Option `%s' is required when using option `%s'.\n"), | ||
208 | "-p", | ||
209 | "-s"); | ||
210 | goto error; | ||
211 | } | ||
212 | if (! (tcp | udp)) | ||
213 | { | ||
214 | fprintf (stderr, | ||
215 | _ ("Option `%s' or `%s' is required when using option `%s'.\n"), | ||
216 | "-t", | ||
217 | "-u", | ||
218 | "-s"); | ||
219 | goto error; | ||
220 | } | ||
221 | if (tcp & udp) | ||
222 | { | 200 | { |
223 | fprintf (stderr, | 201 | if (NULL == service_name) |
224 | _ ("Option `%s' makes no sense with option `%s'.\n"), | 202 | { |
225 | "-t", | 203 | fprintf(stderr, _("Option `%s' or `%s' is required.\n"), "-i", "-s"); |
226 | "-u"); | 204 | goto error; |
227 | goto error; | 205 | } |
206 | if (NULL == peer_id) | ||
207 | { | ||
208 | fprintf(stderr, | ||
209 | _("Option `%s' is required when using option `%s'.\n"), | ||
210 | "-p", | ||
211 | "-s"); | ||
212 | goto error; | ||
213 | } | ||
214 | if (!(tcp | udp)) | ||
215 | { | ||
216 | fprintf(stderr, | ||
217 | _("Option `%s' or `%s' is required when using option `%s'.\n"), | ||
218 | "-t", | ||
219 | "-u", | ||
220 | "-s"); | ||
221 | goto error; | ||
222 | } | ||
223 | if (tcp & udp) | ||
224 | { | ||
225 | fprintf(stderr, | ||
226 | _("Option `%s' makes no sense with option `%s'.\n"), | ||
227 | "-t", | ||
228 | "-u"); | ||
229 | goto error; | ||
230 | } | ||
231 | if (tcp) | ||
232 | protocol = IPPROTO_TCP; | ||
233 | if (udp) | ||
234 | protocol = IPPROTO_UDP; | ||
235 | if (GNUNET_OK != | ||
236 | GNUNET_CRYPTO_eddsa_public_key_from_string(peer_id, | ||
237 | strlen(peer_id), | ||
238 | &peer.public_key)) | ||
239 | { | ||
240 | fprintf(stderr, _("`%s' is not a valid peer identifier.\n"), peer_id); | ||
241 | goto error; | ||
242 | } | ||
243 | GNUNET_TUN_service_name_to_hash(service_name, &sd); | ||
244 | request = GNUNET_VPN_redirect_to_peer(handle, | ||
245 | req_af, | ||
246 | protocol, | ||
247 | &peer, | ||
248 | &sd, | ||
249 | etime, | ||
250 | &allocation_cb, | ||
251 | NULL); | ||
228 | } | 252 | } |
229 | if (tcp) | ||
230 | protocol = IPPROTO_TCP; | ||
231 | if (udp) | ||
232 | protocol = IPPROTO_UDP; | ||
233 | if (GNUNET_OK != | ||
234 | GNUNET_CRYPTO_eddsa_public_key_from_string (peer_id, | ||
235 | strlen (peer_id), | ||
236 | &peer.public_key)) | ||
237 | { | ||
238 | fprintf (stderr, _ ("`%s' is not a valid peer identifier.\n"), peer_id); | ||
239 | goto error; | ||
240 | } | ||
241 | GNUNET_TUN_service_name_to_hash (service_name, &sd); | ||
242 | request = GNUNET_VPN_redirect_to_peer (handle, | ||
243 | req_af, | ||
244 | protocol, | ||
245 | &peer, | ||
246 | &sd, | ||
247 | etime, | ||
248 | &allocation_cb, | ||
249 | NULL); | ||
250 | } | ||
251 | else | 253 | else |
252 | { | ||
253 | if (1 != inet_pton (AF_INET6, target_ip, &v6)) | ||
254 | { | 254 | { |
255 | if (1 != inet_pton (AF_INET, target_ip, &v4)) | 255 | if (1 != inet_pton(AF_INET6, target_ip, &v6)) |
256 | { | 256 | { |
257 | fprintf (stderr, _ ("`%s' is not a valid IP address.\n"), target_ip); | 257 | if (1 != inet_pton(AF_INET, target_ip, &v4)) |
258 | goto error; | 258 | { |
259 | } | 259 | fprintf(stderr, _("`%s' is not a valid IP address.\n"), target_ip); |
260 | goto error; | ||
261 | } | ||
262 | else | ||
263 | { | ||
264 | dst_af = AF_INET; | ||
265 | addr = &v4; | ||
266 | } | ||
267 | } | ||
260 | else | 268 | else |
261 | { | 269 | { |
262 | dst_af = AF_INET; | 270 | dst_af = AF_INET6; |
263 | addr = &v4; | 271 | addr = &v6; |
264 | } | 272 | } |
265 | } | 273 | request = GNUNET_VPN_redirect_to_ip(handle, |
266 | else | 274 | req_af, |
267 | { | 275 | dst_af, |
268 | dst_af = AF_INET6; | 276 | addr, |
269 | addr = &v6; | 277 | etime, |
278 | &allocation_cb, | ||
279 | NULL); | ||
270 | } | 280 | } |
271 | request = GNUNET_VPN_redirect_to_ip (handle, | ||
272 | req_af, | ||
273 | dst_af, | ||
274 | addr, | ||
275 | etime, | ||
276 | &allocation_cb, | ||
277 | NULL); | ||
278 | } | ||
279 | return; | 281 | return; |
280 | 282 | ||
281 | error: | 283 | error: |
282 | GNUNET_SCHEDULER_shutdown (); | 284 | GNUNET_SCHEDULER_shutdown(); |
283 | ret = 1; | 285 | ret = 1; |
284 | } | 286 | } |
285 | 287 | ||
286 | 288 | ||
287 | int | 289 | int |
288 | main (int argc, char *const *argv) | 290 | main(int argc, char *const *argv) |
289 | { | 291 | { |
290 | struct GNUNET_GETOPT_CommandLineOption options[] = | 292 | struct GNUNET_GETOPT_CommandLineOption options[] = |
291 | {GNUNET_GETOPT_option_flag ('4', | 293 | { GNUNET_GETOPT_option_flag('4', |
292 | "ipv4", | 294 | "ipv4", |
293 | gettext_noop ( | 295 | gettext_noop( |
294 | "request that result should be an IPv4 address"), | 296 | "request that result should be an IPv4 address"), |
295 | &ipv4), | 297 | &ipv4), |
296 | 298 | ||
297 | GNUNET_GETOPT_option_flag ('6', | 299 | GNUNET_GETOPT_option_flag('6', |
298 | "ipv6", | 300 | "ipv6", |
299 | gettext_noop ( | 301 | gettext_noop( |
300 | "request that result should be an IPv6 address"), | 302 | "request that result should be an IPv6 address"), |
301 | &ipv6), | 303 | &ipv6), |
302 | 304 | ||
303 | GNUNET_GETOPT_option_relative_time ( | 305 | GNUNET_GETOPT_option_relative_time( |
304 | 'd', | 306 | 'd', |
305 | "duration", | 307 | "duration", |
306 | "TIME", | 308 | "TIME", |
307 | gettext_noop ("how long should the mapping be valid for new tunnels?"), | 309 | gettext_noop("how long should the mapping be valid for new tunnels?"), |
308 | &duration), | 310 | &duration), |
309 | 311 | ||
310 | GNUNET_GETOPT_option_string ('i', | 312 | GNUNET_GETOPT_option_string('i', |
311 | "ip", | 313 | "ip", |
312 | "IP", | 314 | "IP", |
313 | gettext_noop ( | 315 | gettext_noop( |
314 | "destination IP for the tunnel"), | 316 | "destination IP for the tunnel"), |
315 | &target_ip), | 317 | &target_ip), |
316 | 318 | ||
317 | GNUNET_GETOPT_option_string ( | 319 | GNUNET_GETOPT_option_string( |
318 | 'p', | 320 | 'p', |
319 | "peer", | 321 | "peer", |
320 | "PEERID", | 322 | "PEERID", |
321 | gettext_noop ("peer offering the service we would like to access"), | 323 | gettext_noop("peer offering the service we would like to access"), |
322 | &peer_id), | 324 | &peer_id), |
323 | 325 | ||
324 | GNUNET_GETOPT_option_string ('s', | 326 | GNUNET_GETOPT_option_string('s', |
325 | "service", | 327 | "service", |
326 | "NAME", | 328 | "NAME", |
327 | gettext_noop ( | 329 | gettext_noop( |
328 | "name of the service we would like to access"), | 330 | "name of the service we would like to access"), |
329 | &service_name), | 331 | &service_name), |
330 | 332 | ||
331 | GNUNET_GETOPT_option_flag ('t', | 333 | GNUNET_GETOPT_option_flag('t', |
332 | "tcp", | 334 | "tcp", |
333 | gettext_noop ("service is offered via TCP"), | 335 | gettext_noop("service is offered via TCP"), |
334 | &tcp), | 336 | &tcp), |
335 | 337 | ||
336 | GNUNET_GETOPT_option_flag ('u', | 338 | GNUNET_GETOPT_option_flag('u', |
337 | "udp", | 339 | "udp", |
338 | gettext_noop ("service is offered via UDP"), | 340 | gettext_noop("service is offered via UDP"), |
339 | &udp), | 341 | &udp), |
340 | 342 | ||
341 | GNUNET_GETOPT_option_verbose (&verbosity), | 343 | GNUNET_GETOPT_option_verbose(&verbosity), |
342 | 344 | ||
343 | GNUNET_GETOPT_OPTION_END}; | 345 | GNUNET_GETOPT_OPTION_END }; |
344 | if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv)) | 346 | |
347 | if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args(argc, argv, &argc, &argv)) | ||
345 | return 2; | 348 | return 2; |
346 | 349 | ||
347 | ret = | 350 | ret = |
348 | (GNUNET_OK == GNUNET_PROGRAM_run (argc, | 351 | (GNUNET_OK == GNUNET_PROGRAM_run(argc, |
349 | argv, | 352 | argv, |
350 | "gnunet-vpn", | 353 | "gnunet-vpn", |
351 | gettext_noop ("Setup tunnels via VPN."), | 354 | gettext_noop("Setup tunnels via VPN."), |
352 | options, | 355 | options, |
353 | &run, | 356 | &run, |
354 | NULL)) | 357 | NULL)) |
355 | ? ret | 358 | ? ret |
356 | : 1; | 359 | : 1; |
357 | GNUNET_free ((void *) argv); | 360 | GNUNET_free((void *)argv); |
358 | return ret; | 361 | return ret; |
359 | } | 362 | } |
360 | 363 | ||