ftbfs
[oweals/gnunet.git] / src / nat-auto / gnunet-service-nat-auto_legacy.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2015 GNUnet e.V.
4
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.
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      Affero General Public License for more details.
14     
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/>.
17
18      SPDX-License-Identifier: AGPL3.0-or-later
19 */
20
21 /**
22  * @file nat/nat_auto.c
23  * @brief functions for auto-configuration of the network
24  * @author Christian Grothoff
25  * @author Bruno Cabral
26  */
27 #include "platform.h"
28 #include "gnunet_util_lib.h"
29 #include "gnunet_resolver_service.h"
30 #include "gnunet_nat_lib.h"
31 #include "nat.h"
32
33 #define LOG(kind,...) GNUNET_log_from (kind, "nat", __VA_ARGS__)
34
35
36 /**
37  * How long do we wait for the NAT test to report success?
38  */
39 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15)
40
41 #define NAT_SERVER_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
42
43 /**
44  * Phases of the auto configuration.
45  */
46 enum AutoPhase
47 {
48   /**
49    * Initial start value.
50    */
51   AUTO_INIT = 0,
52
53   /**
54    * Test our external IP.
55    */
56   AUTO_EXTERNAL_IP,
57
58   /**
59    * Test our external IP.
60    */
61    AUTO_STUN,
62
63   /**
64    * Test our internal IP.
65    */
66   AUTO_LOCAL_IP,
67
68   /**
69    * Test if NAT was punched.
70    */
71   AUTO_NAT_PUNCHED,
72
73   /**
74    * Test if UPnP is working.
75    */
76   AUTO_UPNPC,
77
78   /**
79    * Test if ICMP server works.
80    */
81   AUTO_ICMP_SERVER,
82
83   /**
84    * Test if ICMP client works.
85    */
86   AUTO_ICMP_CLIENT,
87
88   /**
89    * Last phase, we're done.
90    */
91   AUTO_DONE
92
93 };
94
95
96 /**
97  * Handle to auto-configuration in progress.
98  */
99 struct GNUNET_NAT_AutoHandle
100 {
101
102   /**
103    * Handle to the active NAT test.
104    */
105   struct GNUNET_NAT_Test *tst;
106
107   /**
108    * Function to call when done.
109    */
110   GNUNET_NAT_AutoResultCallback fin_cb;
111
112   /**
113    * Closure for @e fin_cb.
114    */
115   void *fin_cb_cls;
116
117   /**
118    * Handle for active 'GNUNET_NAT_mini_get_external_ipv4'-operation.
119    */
120   struct GNUNET_NAT_ExternalHandle *eh;
121
122   /**
123    * Current configuration (with updates from previous phases)
124    */
125   struct GNUNET_CONFIGURATION_Handle *cfg;
126
127   /**
128    * Original configuration (used to calculate differences)
129    */
130   struct GNUNET_CONFIGURATION_Handle *initial_cfg;
131
132   /**
133    * Task identifier for the timeout.
134    */
135   struct GNUNET_SCHEDULER_Task *task;
136
137   /**
138    * Message queue to the gnunet-nat-server.
139    */
140   struct GNUNET_MQ_Handle *mq;
141
142   /**
143    * Where are we in the test?
144    */
145   enum AutoPhase phase;
146
147   /**
148    * Situation of the NAT
149    */
150   enum GNUNET_NAT_Type type;
151
152   /**
153    * Do we have IPv6?
154    */
155   int have_v6;
156
157   /**
158    * UPnP already set the external ip address ?
159    */
160   int upnp_set_external_address;
161
162   /**
163    * Did the external server connected back ?
164    */
165   int connected_back;
166
167   /**
168    * Address detected by STUN
169    */
170   char *stun_ip;
171
172   unsigned int stun_port;
173
174   /**
175    * Internal IP is the same as the public one ?
176    */
177   int internal_ip_is_public;
178
179   /**
180    * Error code for better debugging and user feedback
181    */
182   enum GNUNET_NAT_StatusCode ret;
183 };
184
185
186 /**
187  * The listen socket of the service for IPv4
188  */
189 static struct GNUNET_NETWORK_Handle *lsock4;
190
191 /**
192  * The listen task ID for IPv4
193  */
194 static struct GNUNET_SCHEDULER_Task *ltask4;
195
196 /**
197  * The port the test service is running on (default 7895)
198  */
199 static unsigned long long port = 7895;
200
201 static char *stun_server = "stun.ekiga.net";
202
203 static unsigned int stun_port = 3478;
204
205
206 /**
207  * Run the next phase of the auto test.
208  *
209  * @param ah auto test handle
210  */
211 static void
212 next_phase (struct GNUNET_NAT_AutoHandle *ah);
213
214
215 static void
216 process_stun_reply(struct sockaddr_in *answer,
217                    struct GNUNET_NAT_AutoHandle *ah)
218 {
219   ah->stun_ip = inet_ntoa(answer->sin_addr);
220   ah->stun_port = ntohs (answer->sin_port);
221   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
222               "External IP is: %s , with port %u\n",
223               ah->stun_ip,
224               ah->stun_port);
225   next_phase (ah);
226 }
227
228
229 /**
230  * Function that terminates the test.
231  */
232 static void
233 stop_stun ()
234 {
235   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
236               "Stopping STUN and quitting...\n");
237   /* Clean task */
238   if (NULL != ltask4)
239   {
240     GNUNET_SCHEDULER_cancel (ltask4);
241     ltask4 = NULL;
242   }
243   /* Clean socket */
244   if (NULL != lsock4)
245   {
246     GNUNET_NETWORK_socket_close (lsock4);
247     lsock4 = NULL;
248   }
249 }
250
251
252 /**
253  * Activity on our incoming socket.  Read data from the
254  * incoming connection.
255  *
256  * @param cls
257  */
258 static void
259 do_udp_read (void *cls)
260 {
261   struct GNUNET_NAT_AutoHandle *ah = cls;
262   unsigned char reply_buf[1024];
263   ssize_t rlen;
264   struct sockaddr_in answer;
265   const struct GNUNET_SCHEDULER_TaskContext *tc;
266
267   tc = GNUNET_SCHEDULER_get_task_context ();
268   if ((0 != (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY)) &&
269       (GNUNET_NETWORK_fdset_isset (tc->read_ready,
270                                    lsock4)))
271   {
272     rlen = GNUNET_NETWORK_socket_recv (lsock4,
273                                        reply_buf,
274                                        sizeof (reply_buf));
275
276     //Lets handle the packet
277     memset (&answer, 0, sizeof(struct sockaddr_in));
278     if (ah->phase == AUTO_NAT_PUNCHED)
279     {
280       //Destroy the connection
281       GNUNET_NETWORK_socket_close (lsock4);
282       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
283                   "The external server was able to connect back");
284       ah->connected_back = GNUNET_YES;
285       next_phase (ah);
286     }
287     else
288     {
289       if (GNUNET_OK ==
290           GNUNET_NAT_stun_handle_packet (reply_buf, rlen, &answer))
291       {
292         //Process the answer
293         process_stun_reply (&answer, ah);
294       }
295       else
296       {
297         next_phase (ah);
298       }
299     }
300   }
301   else
302   {
303     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
304                 "TIMEOUT while waiting for an answer\n");
305     if (ah->phase == AUTO_NAT_PUNCHED)
306     {
307       stop_stun();
308     }
309
310     next_phase (ah);
311   }
312 }
313
314
315 /**
316  * Create an IPv4 listen socket bound to our port.
317  *
318  * @return NULL on error
319  */
320 static struct GNUNET_NETWORK_Handle *
321 bind_v4 ()
322 {
323   struct GNUNET_NETWORK_Handle *ls;
324   struct sockaddr_in sa4;
325   int eno;
326
327   memset (&sa4, 0, sizeof (sa4));
328   sa4.sin_family = AF_INET;
329   sa4.sin_port = htons (port);
330 #if HAVE_SOCKADDR_IN_SIN_LEN
331     sa4.sin_len = sizeof (sa4);
332 #endif
333   ls = GNUNET_NETWORK_socket_create (AF_INET,
334                                      SOCK_DGRAM,
335                                      0);
336   if (NULL == ls)
337     return NULL;
338   if (GNUNET_OK !=
339       GNUNET_NETWORK_socket_bind (ls, (const struct sockaddr *) &sa4,
340                                   sizeof (sa4)))
341   {
342     eno = errno;
343     GNUNET_NETWORK_socket_close (ls);
344     errno = eno;
345     return NULL;
346   }
347   return ls;
348 }
349
350
351 static void
352 request_callback (void *cls,
353                   enum GNUNET_NAT_StatusCode result)
354 {
355   // struct GNUNET_NAT_AutoHandle *ah = cls;
356
357   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
358               "Request callback: stop and quit\n");
359   stop_stun ();
360
361   // next_phase (ah); FIXME this always will be NULL, as called in test_stun()
362 }
363
364
365 /**
366  * Function called by NAT to report the outcome of the nat-test.
367  * Clean up and update GUI.
368  *
369  * @param cls the auto handle
370  * @param success currently always #GNUNET_OK
371  * @param emsg NULL on success, otherwise an error message
372  */
373 static void
374 result_callback (void *cls,
375                  enum GNUNET_NAT_StatusCode ret)
376 {
377   struct GNUNET_NAT_AutoHandle *ah = cls;
378
379   if (GNUNET_NAT_ERROR_SUCCESS == ret)
380     GNUNET_NAT_test_stop (ah->tst);
381   ah->tst = NULL;
382   ah->ret = ret;
383   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
384               GNUNET_NAT_ERROR_SUCCESS == ret
385               ? _("NAT traversal with ICMP Server succeeded.\n")
386               : _("NAT traversal with ICMP Server failed.\n"));
387   GNUNET_CONFIGURATION_set_value_string (ah->cfg, "nat", "ENABLE_ICMP_SERVER",
388                                          GNUNET_NAT_ERROR_SUCCESS == ret ? "NO" : "YES");
389   next_phase (ah);
390 }
391
392
393 /**
394  * Main function for the connection reversal test.
395  *
396  * @param cls the `struct GNUNET_NAT_AutoHandle`
397  */
398 static void
399 reversal_test (void *cls)
400 {
401   struct GNUNET_NAT_AutoHandle *ah = cls;
402
403   ah->task = NULL;
404   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
405               _("Testing connection reversal with ICMP server.\n"));
406   GNUNET_RESOLVER_connect (ah->cfg);
407   ah->tst = GNUNET_NAT_test_start (ah->cfg, GNUNET_YES, 0, 0, TIMEOUT,
408                                    &result_callback, ah);
409 }
410
411
412 /**
413  * Set our external IPv4 address based on the UPnP.
414  *
415  *
416  * @param cls closure with our setup context
417  * @param addr the address, NULL on errors
418  * @param emsg NULL on success, otherwise an error message
419  */
420 static void
421 set_external_ipv4 (void *cls,
422                    const struct in_addr *addr,
423                    enum GNUNET_NAT_StatusCode ret)
424 {
425   struct GNUNET_NAT_AutoHandle *ah = cls;
426   char buf[INET_ADDRSTRLEN];
427
428   ah->eh = NULL;
429   ah->ret = ret;
430   if (GNUNET_NAT_ERROR_SUCCESS != ret)
431   {
432     next_phase (ah);
433     return;
434   }
435   /* enable 'behind nat' */
436   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
437               _("Detected external IP `%s'\n"),
438               inet_ntop (AF_INET,
439                          addr,
440                          buf,
441                          sizeof (buf)));
442   GNUNET_CONFIGURATION_set_value_string (ah->cfg, "nat", "BEHIND_NAT", "YES");
443
444   /* set external IP address */
445   if (NULL == inet_ntop (AF_INET, addr, buf, sizeof (buf)))
446   {
447     GNUNET_break (0);
448     /* actually, this should never happen, as the caller already executed just
449      * this check, but for consistency (eg: future changes in the caller)
450      * we still need to report this error...
451      */
452     ah->ret = GNUNET_NAT_ERROR_EXTERNAL_IP_ADDRESS_INVALID;
453     next_phase (ah);
454     return;
455   }
456   GNUNET_CONFIGURATION_set_value_string (ah->cfg, "nat", "EXTERNAL_ADDRESS",
457                                          buf);
458   ah->upnp_set_external_address = GNUNET_YES;
459   next_phase (ah);
460 }
461
462
463 /**
464  * Determine our external IPv4 address.
465  *
466  * @param ah auto setup context
467  */
468 static void
469 test_external_ip (struct GNUNET_NAT_AutoHandle *ah)
470 {
471   if (GNUNET_NAT_ERROR_SUCCESS != ah->ret)
472     next_phase (ah);
473
474   // FIXME: CPS?
475   /* try to detect external IP */
476   ah->eh = GNUNET_NAT_mini_get_external_ipv4 (TIMEOUT,
477                                               &set_external_ipv4, ah);
478 }
479
480
481 /**
482  * Determine our external IPv4 address and port using an external STUN server
483  *
484  * @param ah auto setup context
485  */
486 static void
487 test_stun (struct GNUNET_NAT_AutoHandle *ah)
488 {
489
490   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Running STUN test\n");
491
492   /* Get port from the configuration */
493   if (GNUNET_OK !=
494       GNUNET_CONFIGURATION_get_value_number (ah->cfg,
495                                              "transport-udp",
496                                              "PORT",
497                                              &port))
498   {
499     port = 2086;
500   }
501
502   //Lets create the socket
503   lsock4 = bind_v4 ();
504   if (NULL == lsock4)
505   {
506     GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind");
507     next_phase(ah);
508     return;
509   }
510   else
511   {
512     //Lets call our function now when it accepts
513     ltask4 = GNUNET_SCHEDULER_add_read_net (NAT_SERVER_TIMEOUT,
514                                             lsock4,
515                                             &do_udp_read,
516                                             ah);
517   }
518
519
520   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
521               "STUN service listens on port %u\n",
522               (unsigned int) port);
523   if (GNUNET_NO ==
524       GNUNET_NAT_stun_make_request (stun_server,
525                                     stun_port,
526                                     lsock4,
527                                     &request_callback,
528                                     NULL))
529   {
530     /*An error happened*/
531     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "STUN error, stopping\n");
532     stop_stun ();
533     next_phase (ah);
534   }
535 }
536
537
538 /**
539  * Process list of local IP addresses.  Find and set the
540  * one of the default interface.
541  *
542  * @param cls our `struct GNUNET_NAT_AutoHandle`
543  * @param name name of the interface (can be NULL for unknown)
544  * @param isDefault is this presumably the default interface
545  * @param addr address of this interface (can be NULL for unknown or unassigned)
546  * @param broadcast_addr the broadcast address (can be NULL for unknown or unassigned)
547  * @param netmask the network mask (can be NULL for unknown or unassigned))
548  * @param addrlen length of the @a addr and @a broadcast_addr
549  * @return #GNUNET_OK to continue iteration, #GNUNET_SYSERR to abort
550  */
551 static int
552 process_if (void *cls,
553             const char *name,
554             int isDefault,
555             const struct sockaddr *addr,
556             const struct sockaddr *broadcast_addr,
557             const struct sockaddr *netmask,
558             socklen_t addrlen)
559 {
560   struct GNUNET_NAT_AutoHandle *ah = cls;
561   const struct sockaddr_in *in;
562   char buf[INET_ADDRSTRLEN];
563
564
565   if ( (sizeof (struct sockaddr_in6) == addrlen) &&
566        (0 != GNUNET_memcmp (&in6addr_loopback, &((const struct sockaddr_in6 *) addr)->sin6_addr)) &&
567        (! IN6_IS_ADDR_LINKLOCAL(&((const struct sockaddr_in6 *) addr)->sin6_addr)) )
568   {
569     ah->have_v6 = GNUNET_YES;
570     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
571                 _("This system has a global IPv6 address, setting IPv6 to supported.\n"));
572
573     return GNUNET_OK;
574   }
575   if (addrlen != sizeof (struct sockaddr_in))
576     return GNUNET_OK;
577   in = (const struct sockaddr_in *) addr;
578
579
580   /* set internal IP address */
581   if (NULL == inet_ntop (AF_INET, &in->sin_addr, buf, sizeof (buf)))
582   {
583     GNUNET_break (0);
584     return GNUNET_OK;
585   }
586   GNUNET_CONFIGURATION_set_value_string (ah->cfg, "nat", "INTERNAL_ADDRESS",
587                                          buf);
588   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
589               _("Detected internal network address `%s'.\n"),
590               buf);
591
592
593   ah->ret = GNUNET_NAT_ERROR_SUCCESS;
594
595   /* Check if our internal IP is the same as the External detect by STUN*/
596   if(ah->stun_ip && (strcmp(buf, ah->stun_ip) == 0) )
597   {
598     ah->internal_ip_is_public = GNUNET_YES;
599     GNUNET_log (GNUNET_ERROR_TYPE_INFO,"A internal IP is the sameas the external");
600     /* No need to continue*/
601     return GNUNET_SYSERR;
602   }
603
604   /* no need to continue iteration if we found the default */
605   if (!isDefault)
606     return GNUNET_OK;
607   else
608     return GNUNET_SYSERR;
609 }
610
611
612 /**
613  * Determine our local IP addresses; detect internal IP & IPv6-support
614  *
615  * @param ah auto setup context
616  */
617 static void
618 test_local_ip (struct GNUNET_NAT_AutoHandle *ah)
619 {
620   ah->have_v6 = GNUNET_NO;
621   ah->ret = GNUNET_NAT_ERROR_NO_VALID_IF_IP_COMBO; // reset to success if any of the IFs in below iterator has a valid IP
622   GNUNET_OS_network_interfaces_list (&process_if, ah);
623
624   GNUNET_CONFIGURATION_set_value_string (ah->cfg, "nat", "DISABLEV6",
625                                          (GNUNET_YES == ah->have_v6) ? "NO" : "YES");
626   next_phase (ah);
627 }
628
629
630 /**
631  * We got disconnected from the NAT server.  Stop
632  * waiting for a reply.
633  *
634  * @param cls the `struct GNUNET_NAT_AutoHandle`
635  * @param error error code
636  */
637 static void
638 mq_error_handler (void *cls,
639                   enum GNUNET_MQ_Error error)
640 {
641   struct GNUNET_NAT_AutoHandle *ah = cls;
642
643   GNUNET_MQ_destroy (ah->mq);
644   ah->mq = NULL;
645   /* wait a bit first? */
646   next_phase (ah);
647 }
648
649
650 /**
651  * Test if NAT has been punched
652  *
653  * @param ah auto setup context
654  */
655 static void
656 test_nat_punched (struct GNUNET_NAT_AutoHandle *ah)
657 {
658   struct GNUNET_NAT_TestMessage *msg;
659   struct GNUNET_MQ_Envelope *env;
660
661   if (! ah->stun_ip)
662   {
663     LOG (GNUNET_ERROR_TYPE_INFO,
664          "We don't have a STUN IP");
665     next_phase (ah);
666     return;
667   }
668
669   LOG (GNUNET_ERROR_TYPE_INFO,
670        "Asking gnunet-nat-server to connect to `%s'\n",
671        ah->stun_ip);
672   ah->mq = GNUNET_CLIENT_connect (ah->cfg,
673                                   "gnunet-nat-server",
674                                   NULL,
675                                   &mq_error_handler,
676                                   ah);
677   if (NULL == ah->mq)
678   {
679     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
680                 _("Failed to connect to `gnunet-nat-server'\n"));
681     next_phase (ah);
682     return;
683   }
684   env = GNUNET_MQ_msg (msg,
685                        GNUNET_MESSAGE_TYPE_NAT_TEST);
686   msg->dst_ipv4 = inet_addr (ah->stun_ip);
687   msg->dport = htons (ah->stun_port);
688   msg->data = port;
689   msg->is_tcp = htonl ((uint32_t) GNUNET_NO);
690   GNUNET_MQ_send (ah->mq,
691                   env);
692   if (NULL != ltask4)
693   {
694     GNUNET_SCHEDULER_cancel (ltask4);
695     ltask4 = GNUNET_SCHEDULER_add_read_net (NAT_SERVER_TIMEOUT,
696                                             lsock4,
697                                             &do_udp_read,
698                                             ah);
699   }
700 }
701
702
703 /**
704  * Test if UPnPC works.
705  *
706  * @param ah auto setup context
707  */
708 static void
709 test_upnpc (struct GNUNET_NAT_AutoHandle *ah)
710 {
711
712   int have_upnpc;
713
714   if (GNUNET_NAT_ERROR_SUCCESS != ah->ret)
715     next_phase (ah);
716
717   // test if upnpc is available
718   have_upnpc = (GNUNET_SYSERR !=
719                 GNUNET_OS_check_helper_binary ("upnpc", GNUNET_NO, NULL));
720   //FIXME: test if upnpc is actually working, that is, if transports start to work once we use UPnP
721   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
722               (have_upnpc)
723               ? _("upnpc found, enabling its use\n")
724               : _("upnpc not found\n"));
725   GNUNET_CONFIGURATION_set_value_string (ah->cfg, "nat", "ENABLE_UPNP",
726                                          (GNUNET_YES == have_upnpc) ? "YES" : "NO");
727   next_phase (ah);
728
729 }
730
731
732 /**
733  * Test if ICMP server is working
734  *
735  * @param ah auto setup context
736  */
737 static void
738 test_icmp_server (struct GNUNET_NAT_AutoHandle *ah)
739 {
740
741   int ext_ip;
742   int nated;
743   int binary;
744   char *tmp;
745   char *helper;
746   ext_ip = GNUNET_NO;
747   nated = GNUNET_NO;
748   binary = GNUNET_NO;
749
750   tmp = NULL;
751   helper = GNUNET_OS_get_libexec_binary_path ("gnunet-helper-nat-server");
752   if ( (GNUNET_OK ==
753         GNUNET_CONFIGURATION_get_value_string (ah->cfg,
754                                                "nat",
755                                                "EXTERNAL_ADDRESS",
756                                                &tmp)) &&
757        (0 < strlen (tmp)) )
758   {
759     ext_ip = GNUNET_OK;
760     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
761                 _("test_icmp_server not possible, as we have no public IPv4 address\n"));
762   }
763   else
764     goto err;
765
766   if (GNUNET_YES ==
767       GNUNET_CONFIGURATION_get_value_yesno (ah->cfg,
768                                             "nat",
769                                             "BEHIND_NAT"))
770   {
771     nated = GNUNET_YES;
772     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
773                 _("test_icmp_server not possible, as we are not behind NAT\n"));
774   }
775   else
776     goto err;
777
778   if (GNUNET_YES ==
779       GNUNET_OS_check_helper_binary (helper,
780                                      GNUNET_YES,
781                                      "-d 127.0.0.1" ))
782   {
783     binary = GNUNET_OK; // use localhost as source for that one udp-port, ok for testing
784     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
785                 _("No working gnunet-helper-nat-server found\n"));
786   }
787 err:
788   GNUNET_free_non_null (tmp);
789   GNUNET_free (helper);
790
791   if ( (GNUNET_OK == ext_ip) &&
792        (GNUNET_YES == nated) &&
793        (GNUNET_OK == binary) )
794     ah->task = GNUNET_SCHEDULER_add_now (&reversal_test,
795                                          ah);
796   else
797     next_phase (ah);
798 }
799
800
801 /**
802  * Test if ICMP client is working
803  *
804  * @param ah auto setup context
805  */
806 static void
807 test_icmp_client (struct GNUNET_NAT_AutoHandle *ah)
808 {
809   char *tmp;
810   char *helper;
811
812   tmp = NULL;
813   helper = GNUNET_OS_get_libexec_binary_path ("gnunet-helper-nat-client");
814   if ( (GNUNET_OK ==
815         GNUNET_CONFIGURATION_get_value_string (ah->cfg,
816                                                "nat",
817                                                "INTERNAL_ADDRESS",
818                                                &tmp)) &&
819        (0 < strlen (tmp)) )
820   {
821     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
822                 _("test_icmp_client not possible, as we have no internal IPv4 address\n"));
823   }
824   else
825     goto err;
826
827   if (GNUNET_YES !=
828       GNUNET_CONFIGURATION_get_value_yesno (ah->cfg,
829                                             "nat",
830                                             "BEHIND_NAT"))
831   {
832     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
833                 _("test_icmp_server not possible, as we are not behind NAT\n"));
834   }
835   else
836     goto err;
837
838   if (GNUNET_YES ==
839       GNUNET_OS_check_helper_binary (helper,
840                                      GNUNET_YES,
841                                      "-d 127.0.0.1 127.0.0.2 42"))
842   {
843     // none of these parameters are actually used in privilege testing mode
844     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
845                 _("No working gnunet-helper-nat-server found\n"));
846   }
847 err:
848   GNUNET_free_non_null (tmp);
849   GNUNET_free (helper);
850
851   next_phase (ah);
852 }
853
854
855 /**
856  * Run the next phase of the auto test.
857  */
858 static void
859 next_phase (struct GNUNET_NAT_AutoHandle *ah)
860 {
861   struct GNUNET_CONFIGURATION_Handle *diff;
862
863   ah->phase++;
864   switch (ah->phase)
865   {
866   case AUTO_INIT:
867     GNUNET_assert (0);
868     break;
869   case AUTO_EXTERNAL_IP:
870     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
871                 "Will run AUTO_EXTERNAL_IP\n");
872     test_external_ip (ah);
873     break;
874   case AUTO_STUN:
875     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
876                 "Will run AUTO_STUN\n");
877     test_stun (ah);
878     break;
879   case AUTO_LOCAL_IP:
880     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
881                 "Will run AUTO_LOCAL_IP\n");
882     test_local_ip (ah);
883     break;
884   case AUTO_NAT_PUNCHED:
885     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
886                 "Will run AUTO_NAT_PUNCHED\n");
887     test_nat_punched (ah);
888     break;
889   case AUTO_UPNPC:
890     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
891                 "Will run AUTO_UPNPC\n");
892     test_upnpc (ah);
893     break;
894   case AUTO_ICMP_SERVER:
895     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
896                 "Will run AUTO_ICMP_SERVER\n");
897     test_icmp_server (ah);
898     break;
899   case AUTO_ICMP_CLIENT:
900     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
901                 "Will run AUTO_ICMP_CLIENT\n");
902     test_icmp_client (ah);
903     break;
904   case AUTO_DONE:
905     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
906                 "Done with tests\n");
907     if (!ah->internal_ip_is_public)
908     {
909       GNUNET_CONFIGURATION_set_value_string (ah->cfg,
910                                              "nat",
911                                              "BEHIND_NAT",
912                                              "YES");
913
914       if (ah->connected_back)
915       {
916         GNUNET_CONFIGURATION_set_value_string (ah->cfg,
917                                                "nat",
918                                                "PUNCHED_NAT",
919                                                "YES");
920       }
921       else
922       {
923         GNUNET_CONFIGURATION_set_value_string (ah->cfg,
924                                                "nat",
925                                                "PUNCHED_NAT",
926                                                "NO");
927       }
928
929       if (ah->stun_ip)
930       {
931         GNUNET_CONFIGURATION_set_value_string (ah->cfg,
932                                                "nat",
933                                                "EXTERNAL_ADDRESS",
934                                                ah->stun_ip);
935         if (ah->connected_back)
936         {
937           ah->type = GNUNET_NAT_TYPE_STUN_PUNCHED_NAT;
938           GNUNET_CONFIGURATION_set_value_string (ah->cfg,
939                                                  "nat",
940                                                  "USE_STUN",
941                                                  "YES");
942         }
943         else
944         {
945           ah->type = GNUNET_NAT_TYPE_UNREACHABLE_NAT;
946           GNUNET_CONFIGURATION_set_value_string (ah->cfg,
947                                                  "nat",
948                                                  "USE_STUN",
949                                                  "NO");
950         }
951
952       }
953       if (0 != ah->stun_port)
954       {
955         GNUNET_CONFIGURATION_set_value_number (ah->cfg,
956                                                "transport-udp",
957                                                "ADVERTISED_PORT",
958                                                ah->stun_port);
959       }
960
961     }
962     else
963     {
964       //The internal IP is the same as public, but we didn't got a incoming connection
965       if (ah->connected_back)
966       {
967         ah->type = GNUNET_NAT_TYPE_NO_NAT;
968         GNUNET_CONFIGURATION_set_value_string (ah->cfg,
969                                                "nat",
970                                                "BEHIND_NAT",
971                                                "NO");
972       }
973       else
974       {
975         GNUNET_CONFIGURATION_set_value_string (ah->cfg,
976                                                "nat",
977                                                "BEHIND_NAT",
978                                                "YES");
979         ah->type = GNUNET_NAT_TYPE_UNREACHABLE_NAT;
980         if (ah->stun_ip)
981         {
982           GNUNET_CONFIGURATION_set_value_string (ah->cfg,
983                                                  "nat",
984                                                  "EXTERNAL_ADDRESS",
985                                                  ah->stun_ip);
986         }
987         if (0 != ah->stun_port)
988         {
989           GNUNET_CONFIGURATION_set_value_number (ah->cfg,
990                                                  "transport-udp",
991                                                  "ADVERTISED_PORT",
992                                                  ah->stun_port);
993
994         }
995       }
996     }
997
998     diff = GNUNET_CONFIGURATION_get_diff (ah->initial_cfg,
999                                           ah->cfg);
1000
1001
1002     ah->fin_cb (ah->fin_cb_cls,
1003                 diff,
1004                 ah->ret,
1005                 ah->type);
1006     GNUNET_CONFIGURATION_destroy (diff);
1007     GNUNET_NAT_autoconfig_cancel (ah);
1008   }
1009 }
1010
1011
1012 /**
1013  * Start auto-configuration routine.  The resolver service should
1014  * be available when this function is called.
1015  *
1016  * @param cfg initial configuration
1017  * @param cb function to call with autoconfiguration result
1018  * @param cb_cls closure for @a cb
1019  * @return handle to cancel operation
1020  */
1021 struct GNUNET_NAT_AutoHandle *
1022 GNUNET_NAT_autoconfig_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
1023                              GNUNET_NAT_AutoResultCallback cb,
1024                              void *cb_cls)
1025 {
1026   struct GNUNET_NAT_AutoHandle *ah;
1027
1028   ah = GNUNET_new (struct GNUNET_NAT_AutoHandle);
1029   ah->fin_cb = cb;
1030   ah->fin_cb_cls = cb_cls;
1031   ah->ret = GNUNET_NAT_ERROR_SUCCESS;
1032   ah->cfg = GNUNET_CONFIGURATION_dup (cfg);
1033   ah->initial_cfg = GNUNET_CONFIGURATION_dup (cfg);
1034
1035   /* never use loopback addresses if user wanted autoconfiguration */
1036   GNUNET_CONFIGURATION_set_value_string (ah->cfg,
1037                                          "nat",
1038                                          "USE_LOCALADDR",
1039                                          "NO");
1040
1041   next_phase (ah);
1042   return ah;
1043 }
1044
1045
1046 /**
1047  * Abort autoconfiguration.
1048  *
1049  * @param ah handle for operation to abort
1050  */
1051 void
1052 GNUNET_NAT_autoconfig_cancel (struct GNUNET_NAT_AutoHandle *ah)
1053 {
1054   if (NULL != ah->tst)
1055   {
1056     GNUNET_NAT_test_stop (ah->tst);
1057     ah->tst = NULL;
1058   }
1059   if (NULL != ah->eh)
1060   {
1061     GNUNET_NAT_mini_get_external_ipv4_cancel (ah->eh);
1062     ah->eh = NULL;
1063   }
1064   if (NULL != ah->mq)
1065   {
1066     GNUNET_MQ_destroy (ah->mq);
1067     ah->mq = NULL;
1068   }
1069   if (NULL != ah->task)
1070   {
1071     GNUNET_SCHEDULER_cancel (ah->task);
1072     ah->task = NULL;
1073   }
1074   GNUNET_CONFIGURATION_destroy (ah->cfg);
1075   GNUNET_CONFIGURATION_destroy (ah->initial_cfg);
1076   GNUNET_free (ah);
1077 }
1078
1079
1080 /* end of nat_auto.c */