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