diff options
author | Bruno Cabral <bcabral@uw.edu> | 2015-08-22 06:24:03 +0000 |
---|---|---|
committer | Bruno Cabral <bcabral@uw.edu> | 2015-08-22 06:24:03 +0000 |
commit | 5475027f60525bcf22d75fb7f7ee721703e01cfd (patch) | |
tree | 374daa52fd9da6a975b7ef37dbf276fa152c7103 /src/nat/gnunet-nat.c | |
parent | dd1927b960c7cea13733e061a11142274652ba27 (diff) | |
download | gnunet-5475027f60525bcf22d75fb7f7ee721703e01cfd.tar.gz gnunet-5475027f60525bcf22d75fb7f7ee721703e01cfd.zip |
gnunet-nat, tool to suggest changes to configure based on NAT configuration
Diffstat (limited to 'src/nat/gnunet-nat.c')
-rw-r--r-- | src/nat/gnunet-nat.c | 181 |
1 files changed, 181 insertions, 0 deletions
diff --git a/src/nat/gnunet-nat.c b/src/nat/gnunet-nat.c new file mode 100644 index 000000000..f9aae10cf --- /dev/null +++ b/src/nat/gnunet-nat.c | |||
@@ -0,0 +1,181 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | Copyright (C) 2015 Christian Grothoff (and other contributing authors) | ||
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 | |||
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 | |||
91 | } | ||
92 | |||
93 | printf("NAT status: %s \n", nat_type ); | ||
94 | printf("SUGGESTED CHANGES: \n" ); | ||
95 | |||
96 | GNUNET_CONFIGURATION_iterate_section_values (diff, | ||
97 | "nat", | ||
98 | &auto_conf_iter, | ||
99 | NULL); | ||
100 | |||
101 | //TODO: Save config | ||
102 | |||
103 | } | ||
104 | |||
105 | |||
106 | |||
107 | |||
108 | |||
109 | /** | ||
110 | * Main function that will be run. | ||
111 | * | ||
112 | * @param cls closure | ||
113 | * @param args remaining command-line arguments | ||
114 | * @param cfgfile name of the configuration file used (for saving, can be NULL!) | ||
115 | * @param c configuration | ||
116 | */ | ||
117 | static void | ||
118 | run (void *cls, char *const *args, const char *cfgfile, | ||
119 | const struct GNUNET_CONFIGURATION_Handle *c) | ||
120 | { | ||
121 | |||
122 | |||
123 | GNUNET_NAT_autoconfig_start(c,auto_config_cb, NULL); | ||
124 | |||
125 | } | ||
126 | |||
127 | |||
128 | /** | ||
129 | * Main function of gnunet-nat | ||
130 | * | ||
131 | * @param argc number of command-line arguments | ||
132 | * @param argv command line | ||
133 | * @return 0 on success, -1 on error | ||
134 | */ | ||
135 | int | ||
136 | main (int argc, char *const argv[]) | ||
137 | { | ||
138 | static const struct GNUNET_GETOPT_CommandLineOption options[] = { | ||
139 | GNUNET_GETOPT_OPTION_END | ||
140 | }; | ||
141 | |||
142 | int ret = 0; | ||
143 | if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv)) | ||
144 | return 2; | ||
145 | |||
146 | /* Lets start resolver */ | ||
147 | char *fn; | ||
148 | struct GNUNET_OS_Process *proc; | ||
149 | |||
150 | fn = GNUNET_OS_get_libexec_binary_path ("gnunet-service-resolver"); | ||
151 | proc = GNUNET_OS_start_process (GNUNET_YES, | ||
152 | GNUNET_OS_INHERIT_STD_OUT_AND_ERR, | ||
153 | NULL, NULL, NULL, | ||
154 | fn, | ||
155 | "gnunet-service-resolver"); | ||
156 | GNUNET_assert (NULL != proc); | ||
157 | |||
158 | if (GNUNET_OK != | ||
159 | GNUNET_PROGRAM_run (argc, argv, "gnunet-nat [options]", | ||
160 | _("GNUnet NAT traversal autoconfigure daemon"), options, | ||
161 | &run, NULL)) | ||
162 | { | ||
163 | ret = 1; | ||
164 | } | ||
165 | |||
166 | /* Now kill the resolver */ | ||
167 | if (0 != GNUNET_OS_process_kill (proc, GNUNET_TERM_SIG)) | ||
168 | { | ||
169 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill"); | ||
170 | } | ||
171 | GNUNET_OS_process_wait (proc); | ||
172 | GNUNET_OS_process_destroy (proc); | ||
173 | proc = NULL; | ||
174 | |||
175 | |||
176 | GNUNET_free ((void*) argv); | ||
177 | return ret; | ||
178 | } | ||
179 | |||
180 | |||
181 | /* end of gnunet-nat-server.c */ | ||