porting xdht to new service API, major code de-duplication effort
[oweals/gnunet.git] / src / nat / gnunet-nat.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 src/nat/gnunet-nat.c
23  * @brief Daemon to auto configure nat
24  * @author Christian Grothoff
25  * @author Bruno Cabral
26  */
27 #include "platform.h"
28 #include "gnunet_util_lib.h"
29 #include "gnunet_nat_lib.h"
30 #include "gnunet_protocols.h"
31 #include "nat.h"
32
33
34 struct GNUNET_CONFIGURATION_Handle *cfg;
35
36
37
38 /**
39  * Function to iterate over sugested changes options
40  *
41  * @param cls closure
42  * @param section name of the section
43  * @param option name of the option
44  * @param value value of the option
45  */
46 static void
47 auto_conf_iter (void *cls,
48                 const char *section,
49                 const char *option,
50                 const char *value)
51 {
52
53   PRINTF ( "%s: %s \n", option, value);
54 }
55
56
57
58 /**
59  * Function called with the result from the autoconfiguration.
60  *
61  * @param cls closure
62  * @param diff minimal suggested changes to the original configuration
63  *             to make it work (as best as we can)
64  * @param result #GNUNET_NAT_ERROR_SUCCESS on success, otherwise the specific error code
65  * @param type what the situation of the NAT
66  */
67
68 void
69 auto_config_cb(void *cls,
70                const struct GNUNET_CONFIGURATION_Handle *diff,
71                enum GNUNET_NAT_StatusCode result, enum GNUNET_NAT_Type type)
72 {
73   char* nat_type;
74   char unknown_type[64];
75
76   switch (type)
77   {
78     case GNUNET_NAT_TYPE_NO_NAT:
79       nat_type = "NO NAT";
80       break;
81     case GNUNET_NAT_TYPE_UNREACHABLE_NAT:
82       nat_type = "NAT but we can traverse";
83       break;
84     case GNUNET_NAT_TYPE_STUN_PUNCHED_NAT:
85       nat_type = "NAT but STUN is able to identify the correct information";
86       break;
87     case GNUNET_NAT_TYPE_UPNP_NAT:
88       nat_type = "NAT but UPNP opened the ports";
89       break;
90     default:
91       SPRINTF (unknown_type, "NAT unknown, type %u", type);
92       nat_type = unknown_type;
93   }
94
95   PRINTF ("NAT status: %s \n", nat_type );
96   PRINTF ("SUGGESTED CHANGES: \n" );
97
98   GNUNET_CONFIGURATION_iterate_section_values (diff,
99                                                "nat",
100                                                &auto_conf_iter,
101                                                NULL);
102
103   //TODO: Save config
104
105 }
106
107
108
109
110
111 /**
112  * Main function that will be run.
113  *
114  * @param cls closure
115  * @param args remaining command-line arguments
116  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
117  * @param c configuration
118  */
119 static void
120 run (void *cls, char *const *args, const char *cfgfile,
121      const struct GNUNET_CONFIGURATION_Handle *c)
122 {
123   GNUNET_NAT_autoconfig_start (c, auto_config_cb, NULL);
124 }
125
126
127 /**
128  * Main function of gnunet-nat
129  *
130  * @param argc number of command-line arguments
131  * @param argv command line
132  * @return 0 on success, -1 on error
133  */
134 int
135 main (int argc, char *const argv[])
136 {
137   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
138     GNUNET_GETOPT_OPTION_END
139   };
140
141   int ret = 0;
142   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
143     return 2;
144
145   /* Lets start resolver */
146   char *fn;
147   struct GNUNET_OS_Process *proc;
148
149   fn = GNUNET_OS_get_libexec_binary_path ("gnunet-service-resolver");
150   proc = GNUNET_OS_start_process (GNUNET_YES,
151                                   GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
152                                   NULL, NULL, NULL,
153                                   fn,
154                                   "gnunet-service-resolver");
155   GNUNET_assert (NULL != proc);
156
157   if (GNUNET_OK !=
158       GNUNET_PROGRAM_run (argc, argv, "gnunet-nat [options]",
159                           _("GNUnet NAT traversal autoconfigure daemon"), options,
160                           &run, NULL))
161   {
162       ret = 1;
163   }
164
165   /* Now kill the resolver */
166   if (0 != GNUNET_OS_process_kill (proc, GNUNET_TERM_SIG))
167   {
168       GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
169   }
170   GNUNET_OS_process_wait (proc);
171   GNUNET_OS_process_destroy (proc);
172   proc = NULL;
173
174
175   GNUNET_free ((void*) argv);
176   return ret;
177 }
178
179
180 /* end of gnunet-nat-server.c */