aboutsummaryrefslogtreecommitdiff
path: root/src/nat/gnunet-service-nat_helper.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/nat/gnunet-service-nat_helper.c')
-rw-r--r--src/nat/gnunet-service-nat_helper.c368
1 files changed, 183 insertions, 185 deletions
diff --git a/src/nat/gnunet-service-nat_helper.c b/src/nat/gnunet-service-nat_helper.c
index cf5038dd7..42612751d 100644
--- a/src/nat/gnunet-service-nat_helper.c
+++ b/src/nat/gnunet-service-nat_helper.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 nat/gnunet-service-nat_helper.c 22 * @file nat/gnunet-service-nat_helper.c
@@ -32,9 +32,7 @@
32/** 32/**
33 * Information we keep per NAT helper process. 33 * Information we keep per NAT helper process.
34 */ 34 */
35struct HelperContext 35struct HelperContext {
36{
37
38 /** 36 /**
39 * IP address we pass to the NAT helper. 37 * IP address we pass to the NAT helper.
40 */ 38 */
@@ -89,7 +87,7 @@ struct HelperContext
89 * @param cls a `struct HelperContext` 87 * @param cls a `struct HelperContext`
90 */ 88 */
91static void 89static void
92restart_nat_server (void *cls); 90restart_nat_server(void *cls);
93 91
94 92
95/** 93/**
@@ -98,13 +96,13 @@ restart_nat_server (void *cls);
98 * @param h context of the helper 96 * @param h context of the helper
99 */ 97 */
100static void 98static void
101try_again (struct HelperContext *h) 99try_again(struct HelperContext *h)
102{ 100{
103 GNUNET_assert (NULL == h->server_read_task); 101 GNUNET_assert(NULL == h->server_read_task);
104 h->server_retry_delay = GNUNET_TIME_STD_BACKOFF (h->server_retry_delay); 102 h->server_retry_delay = GNUNET_TIME_STD_BACKOFF(h->server_retry_delay);
105 h->server_read_task = GNUNET_SCHEDULER_add_delayed (h->server_retry_delay, 103 h->server_read_task = GNUNET_SCHEDULER_add_delayed(h->server_retry_delay,
106 &restart_nat_server, 104 &restart_nat_server,
107 h); 105 h);
108} 106}
109 107
110 108
@@ -116,7 +114,7 @@ try_again (struct HelperContext *h)
116 * @param cls the `struct HelperContext` 114 * @param cls the `struct HelperContext`
117 */ 115 */
118static void 116static void
119nat_server_read (void *cls) 117nat_server_read(void *cls)
120{ 118{
121 struct HelperContext *h = cls; 119 struct HelperContext *h = cls;
122 char mybuf[40]; 120 char mybuf[40];
@@ -126,73 +124,73 @@ nat_server_read (void *cls)
126 struct sockaddr_in sin_addr; 124 struct sockaddr_in sin_addr;
127 125
128 h->server_read_task = NULL; 126 h->server_read_task = NULL;
129 memset (mybuf, 0, sizeof (mybuf)); 127 memset(mybuf, 0, sizeof(mybuf));
130 bytes = 128 bytes =
131 GNUNET_DISK_file_read (h->server_stdout_handle, mybuf, sizeof (mybuf)); 129 GNUNET_DISK_file_read(h->server_stdout_handle, mybuf, sizeof(mybuf));
132 if (bytes < 1) 130 if (bytes < 1)
133 {
134 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
135 "Finished reading from server stdout with code: %d\n",
136 (int) bytes);
137 if (0 != GNUNET_OS_process_kill (h->server_proc, GNUNET_TERM_SIG))
138 GNUNET_log_from_strerror (GNUNET_ERROR_TYPE_WARNING, "nat", "kill");
139 GNUNET_OS_process_wait (h->server_proc);
140 GNUNET_OS_process_destroy (h->server_proc);
141 h->server_proc = NULL;
142 GNUNET_DISK_pipe_close (h->server_stdout);
143 h->server_stdout = NULL;
144 h->server_stdout_handle = NULL;
145 try_again (h);
146 return;
147 }
148
149 port_start = NULL;
150 for (size_t i = 0; i < sizeof (mybuf); i++)
151 {
152 if (mybuf[i] == '\n')
153 { 131 {
154 mybuf[i] = '\0'; 132 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
155 break; 133 "Finished reading from server stdout with code: %d\n",
134 (int)bytes);
135 if (0 != GNUNET_OS_process_kill(h->server_proc, GNUNET_TERM_SIG))
136 GNUNET_log_from_strerror(GNUNET_ERROR_TYPE_WARNING, "nat", "kill");
137 GNUNET_OS_process_wait(h->server_proc);
138 GNUNET_OS_process_destroy(h->server_proc);
139 h->server_proc = NULL;
140 GNUNET_DISK_pipe_close(h->server_stdout);
141 h->server_stdout = NULL;
142 h->server_stdout_handle = NULL;
143 try_again(h);
144 return;
156 } 145 }
157 if ((mybuf[i] == ':') && (i + 1 < sizeof (mybuf))) 146
147 port_start = NULL;
148 for (size_t i = 0; i < sizeof(mybuf); i++)
158 { 149 {
159 mybuf[i] = '\0'; 150 if (mybuf[i] == '\n')
160 port_start = &mybuf[i + 1]; 151 {
152 mybuf[i] = '\0';
153 break;
154 }
155 if ((mybuf[i] == ':') && (i + 1 < sizeof(mybuf)))
156 {
157 mybuf[i] = '\0';
158 port_start = &mybuf[i + 1];
159 }
161 } 160 }
162 }
163 161
164 /* construct socket address of sender */ 162 /* construct socket address of sender */
165 memset (&sin_addr, 0, sizeof (sin_addr)); 163 memset(&sin_addr, 0, sizeof(sin_addr));
166 sin_addr.sin_family = AF_INET; 164 sin_addr.sin_family = AF_INET;
167#if HAVE_SOCKADDR_IN_SIN_LEN 165#if HAVE_SOCKADDR_IN_SIN_LEN
168 sin_addr.sin_len = sizeof (sin_addr); 166 sin_addr.sin_len = sizeof(sin_addr);
169#endif 167#endif
170 if ((NULL == port_start) || (1 != sscanf (port_start, "%d", &port)) || 168 if ((NULL == port_start) || (1 != sscanf(port_start, "%d", &port)) ||
171 (-1 == inet_pton (AF_INET, mybuf, &sin_addr.sin_addr))) 169 (-1 == inet_pton(AF_INET, mybuf, &sin_addr.sin_addr)))
172 { 170 {
173 /* should we restart gnunet-helper-nat-server? */ 171 /* should we restart gnunet-helper-nat-server? */
174 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 172 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
175 _ ( 173 _(
176 "gnunet-helper-nat-server generated malformed address `%s'\n"), 174 "gnunet-helper-nat-server generated malformed address `%s'\n"),
177 mybuf); 175 mybuf);
178 h->server_read_task = 176 h->server_read_task =
179 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, 177 GNUNET_SCHEDULER_add_read_file(GNUNET_TIME_UNIT_FOREVER_REL,
180 h->server_stdout_handle, 178 h->server_stdout_handle,
181 &nat_server_read, 179 &nat_server_read,
182 h); 180 h);
183 return; 181 return;
184 } 182 }
185 sin_addr.sin_port = htons ((uint16_t) port); 183 sin_addr.sin_port = htons((uint16_t)port);
186 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 184 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
187 "gnunet-helper-nat-server read: %s:%d\n", 185 "gnunet-helper-nat-server read: %s:%d\n",
188 mybuf, 186 mybuf,
189 port); 187 port);
190 h->cb (h->cb_cls, &sin_addr); 188 h->cb(h->cb_cls, &sin_addr);
191 h->server_read_task = 189 h->server_read_task =
192 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, 190 GNUNET_SCHEDULER_add_read_file(GNUNET_TIME_UNIT_FOREVER_REL,
193 h->server_stdout_handle, 191 h->server_stdout_handle,
194 &nat_server_read, 192 &nat_server_read,
195 h); 193 h);
196} 194}
197 195
198 196
@@ -203,67 +201,67 @@ nat_server_read (void *cls)
203 * @param cls a `struct HelperContext` 201 * @param cls a `struct HelperContext`
204 */ 202 */
205static void 203static void
206restart_nat_server (void *cls) 204restart_nat_server(void *cls)
207{ 205{
208 struct HelperContext *h = cls; 206 struct HelperContext *h = cls;
209 char *binary; 207 char *binary;
210 char ia[INET_ADDRSTRLEN]; 208 char ia[INET_ADDRSTRLEN];
211 209
212 h->server_read_task = NULL; 210 h->server_read_task = NULL;
213 GNUNET_assert (NULL != 211 GNUNET_assert(NULL !=
214 inet_ntop (AF_INET, &h->internal_address, ia, sizeof (ia))); 212 inet_ntop(AF_INET, &h->internal_address, ia, sizeof(ia)));
215 /* Start the server process */ 213 /* Start the server process */
216 binary = GNUNET_OS_get_suid_binary_path (h->cfg, "gnunet-helper-nat-server"); 214 binary = GNUNET_OS_get_suid_binary_path(h->cfg, "gnunet-helper-nat-server");
217 if (GNUNET_YES != GNUNET_OS_check_helper_binary (binary, GNUNET_YES, ia)) 215 if (GNUNET_YES != GNUNET_OS_check_helper_binary(binary, GNUNET_YES, ia))
218 { 216 {
219 /* move instantly to max delay, as this is unlikely to be fixed */ 217 /* move instantly to max delay, as this is unlikely to be fixed */
220 h->server_retry_delay = GNUNET_TIME_STD_EXPONENTIAL_BACKOFF_THRESHOLD; 218 h->server_retry_delay = GNUNET_TIME_STD_EXPONENTIAL_BACKOFF_THRESHOLD;
221 GNUNET_free (binary); 219 GNUNET_free(binary);
222 try_again (h); 220 try_again(h);
223 return; 221 return;
224 } 222 }
225 h->server_stdout = 223 h->server_stdout =
226 GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO, GNUNET_YES); 224 GNUNET_DISK_pipe(GNUNET_YES, GNUNET_YES, GNUNET_NO, GNUNET_YES);
227 if (NULL == h->server_stdout) 225 if (NULL == h->server_stdout)
228 { 226 {
229 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "pipe"); 227 GNUNET_log_strerror(GNUNET_ERROR_TYPE_ERROR, "pipe");
230 GNUNET_free (binary); 228 GNUNET_free(binary);
231 try_again (h); 229 try_again(h);
232 return; 230 return;
233 } 231 }
234 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 232 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
235 "Starting `%s' at `%s'\n", 233 "Starting `%s' at `%s'\n",
236 "gnunet-helper-nat-server", 234 "gnunet-helper-nat-server",
237 ia); 235 ia);
238 h->server_proc = GNUNET_OS_start_process (GNUNET_NO, 236 h->server_proc = GNUNET_OS_start_process(GNUNET_NO,
239 0, 237 0,
240 NULL, 238 NULL,
241 h->server_stdout, 239 h->server_stdout,
242 NULL, 240 NULL,
243 binary, 241 binary,
244 "gnunet-helper-nat-server", 242 "gnunet-helper-nat-server",
245 ia, 243 ia,
246 NULL); 244 NULL);
247 GNUNET_free (binary); 245 GNUNET_free(binary);
248 if (NULL == h->server_proc) 246 if (NULL == h->server_proc)
249 { 247 {
250 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 248 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
251 _ ("Failed to start %s\n"), 249 _("Failed to start %s\n"),
252 "gnunet-helper-nat-server"); 250 "gnunet-helper-nat-server");
253 GNUNET_DISK_pipe_close (h->server_stdout); 251 GNUNET_DISK_pipe_close(h->server_stdout);
254 h->server_stdout = NULL; 252 h->server_stdout = NULL;
255 try_again (h); 253 try_again(h);
256 return; 254 return;
257 } 255 }
258 /* Close the write end of the read pipe */ 256 /* Close the write end of the read pipe */
259 GNUNET_DISK_pipe_close_end (h->server_stdout, GNUNET_DISK_PIPE_END_WRITE); 257 GNUNET_DISK_pipe_close_end(h->server_stdout, GNUNET_DISK_PIPE_END_WRITE);
260 h->server_stdout_handle = 258 h->server_stdout_handle =
261 GNUNET_DISK_pipe_handle (h->server_stdout, GNUNET_DISK_PIPE_END_READ); 259 GNUNET_DISK_pipe_handle(h->server_stdout, GNUNET_DISK_PIPE_END_READ);
262 h->server_read_task = 260 h->server_read_task =
263 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, 261 GNUNET_SCHEDULER_add_read_file(GNUNET_TIME_UNIT_FOREVER_REL,
264 h->server_stdout_handle, 262 h->server_stdout_handle,
265 &nat_server_read, 263 &nat_server_read,
266 h); 264 h);
267} 265}
268 266
269 267
@@ -278,24 +276,24 @@ restart_nat_server (void *cls)
278 * @return NULL on error 276 * @return NULL on error
279 */ 277 */
280struct HelperContext * 278struct HelperContext *
281GN_start_gnunet_nat_server_ (const struct in_addr *internal_address, 279GN_start_gnunet_nat_server_(const struct in_addr *internal_address,
282 GN_ReversalCallback cb, 280 GN_ReversalCallback cb,
283 void *cb_cls, 281 void *cb_cls,
284 const struct GNUNET_CONFIGURATION_Handle *cfg) 282 const struct GNUNET_CONFIGURATION_Handle *cfg)
285{ 283{
286 struct HelperContext *h; 284 struct HelperContext *h;
287 285
288 h = GNUNET_new (struct HelperContext); 286 h = GNUNET_new(struct HelperContext);
289 h->cb = cb; 287 h->cb = cb;
290 h->cb_cls = cb_cls; 288 h->cb_cls = cb_cls;
291 h->internal_address = *internal_address; 289 h->internal_address = *internal_address;
292 h->cfg = cfg; 290 h->cfg = cfg;
293 restart_nat_server (h); 291 restart_nat_server(h);
294 if (NULL == h->server_stdout) 292 if (NULL == h->server_stdout)
295 { 293 {
296 GN_stop_gnunet_nat_server_ (h); 294 GN_stop_gnunet_nat_server_(h);
297 return NULL; 295 return NULL;
298 } 296 }
299 return h; 297 return h;
300} 298}
301 299
@@ -307,31 +305,31 @@ GN_start_gnunet_nat_server_ (const struct in_addr *internal_address,
307 * @param h helper context to stop 305 * @param h helper context to stop
308 */ 306 */
309void 307void
310GN_stop_gnunet_nat_server_ (struct HelperContext *h) 308GN_stop_gnunet_nat_server_(struct HelperContext *h)
311{ 309{
312 if (NULL != h->server_read_task) 310 if (NULL != h->server_read_task)
313 { 311 {
314 GNUNET_SCHEDULER_cancel (h->server_read_task); 312 GNUNET_SCHEDULER_cancel(h->server_read_task);
315 h->server_read_task = NULL; 313 h->server_read_task = NULL;
316 } 314 }
317 if (NULL != h->server_proc) 315 if (NULL != h->server_proc)
318 { 316 {
319 if (0 != GNUNET_OS_process_kill (h->server_proc, GNUNET_TERM_SIG)) 317 if (0 != GNUNET_OS_process_kill(h->server_proc, GNUNET_TERM_SIG))
320 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill"); 318 GNUNET_log_strerror(GNUNET_ERROR_TYPE_WARNING, "kill");
321 GNUNET_OS_process_wait (h->server_proc); 319 GNUNET_OS_process_wait(h->server_proc);
322 GNUNET_OS_process_destroy (h->server_proc); 320 GNUNET_OS_process_destroy(h->server_proc);
323 h->server_proc = NULL; 321 h->server_proc = NULL;
324 GNUNET_DISK_pipe_close (h->server_stdout); 322 GNUNET_DISK_pipe_close(h->server_stdout);
325 h->server_stdout = NULL; 323 h->server_stdout = NULL;
326 h->server_stdout_handle = NULL; 324 h->server_stdout_handle = NULL;
327 } 325 }
328 if (NULL != h->server_stdout) 326 if (NULL != h->server_stdout)
329 { 327 {
330 GNUNET_DISK_pipe_close (h->server_stdout); 328 GNUNET_DISK_pipe_close(h->server_stdout);
331 h->server_stdout = NULL; 329 h->server_stdout = NULL;
332 h->server_stdout_handle = NULL; 330 h->server_stdout_handle = NULL;
333 } 331 }
334 GNUNET_free (h); 332 GNUNET_free(h);
335} 333}
336 334
337 335
@@ -348,10 +346,10 @@ GN_stop_gnunet_nat_server_ (struct HelperContext *h)
348 * #GNUNET_OK otherwise 346 * #GNUNET_OK otherwise
349 */ 347 */
350int 348int
351GN_request_connection_reversal (const struct in_addr *internal_address, 349GN_request_connection_reversal(const struct in_addr *internal_address,
352 uint16_t internal_port, 350 uint16_t internal_port,
353 const struct in_addr *remote_v4, 351 const struct in_addr *remote_v4,
354 const struct GNUNET_CONFIGURATION_Handle *cfg) 352 const struct GNUNET_CONFIGURATION_Handle *cfg)
355{ 353{
356 char intv4[INET_ADDRSTRLEN]; 354 char intv4[INET_ADDRSTRLEN];
357 char remv4[INET_ADDRSTRLEN]; 355 char remv4[INET_ADDRSTRLEN];
@@ -359,44 +357,44 @@ GN_request_connection_reversal (const struct in_addr *internal_address,
359 struct GNUNET_OS_Process *proc; 357 struct GNUNET_OS_Process *proc;
360 char *binary; 358 char *binary;
361 359
362 if (NULL == inet_ntop (AF_INET, internal_address, intv4, INET_ADDRSTRLEN)) 360 if (NULL == inet_ntop(AF_INET, internal_address, intv4, INET_ADDRSTRLEN))
363 { 361 {
364 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "inet_ntop"); 362 GNUNET_log_strerror(GNUNET_ERROR_TYPE_WARNING, "inet_ntop");
365 return GNUNET_SYSERR; 363 return GNUNET_SYSERR;
366 } 364 }
367 if (NULL == inet_ntop (AF_INET, remote_v4, remv4, INET_ADDRSTRLEN)) 365 if (NULL == inet_ntop(AF_INET, remote_v4, remv4, INET_ADDRSTRLEN))
368 { 366 {
369 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "inet_ntop"); 367 GNUNET_log_strerror(GNUNET_ERROR_TYPE_WARNING, "inet_ntop");
370 return GNUNET_SYSERR; 368 return GNUNET_SYSERR;
371 } 369 }
372 GNUNET_snprintf (port_as_string, 370 GNUNET_snprintf(port_as_string,
373 sizeof (port_as_string), 371 sizeof(port_as_string),
374 "%d", 372 "%d",
375 internal_port); 373 internal_port);
376 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 374 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
377 "Running gnunet-helper-nat-client %s %s %u\n", 375 "Running gnunet-helper-nat-client %s %s %u\n",
378 intv4, 376 intv4,
379 remv4, 377 remv4,
380 internal_port); 378 internal_port);
381 binary = GNUNET_OS_get_suid_binary_path (cfg, "gnunet-helper-nat-client"); 379 binary = GNUNET_OS_get_suid_binary_path(cfg, "gnunet-helper-nat-client");
382 proc = GNUNET_OS_start_process (GNUNET_NO, 380 proc = GNUNET_OS_start_process(GNUNET_NO,
383 0, 381 0,
384 NULL, 382 NULL,
385 NULL, 383 NULL,
386 NULL, 384 NULL,
387 binary, 385 binary,
388 "gnunet-helper-nat-client", 386 "gnunet-helper-nat-client",
389 intv4, 387 intv4,
390 remv4, 388 remv4,
391 port_as_string, 389 port_as_string,
392 NULL); 390 NULL);
393 GNUNET_free (binary); 391 GNUNET_free(binary);
394 if (NULL == proc) 392 if (NULL == proc)
395 return GNUNET_SYSERR; 393 return GNUNET_SYSERR;
396 /* we know that the gnunet-helper-nat-client will terminate virtually 394 /* we know that the gnunet-helper-nat-client will terminate virtually
397 * instantly */ 395 * instantly */
398 GNUNET_OS_process_wait (proc); 396 GNUNET_OS_process_wait(proc);
399 GNUNET_OS_process_destroy (proc); 397 GNUNET_OS_process_destroy(proc);
400 return GNUNET_OK; 398 return GNUNET_OK;
401} 399}
402 400