GNUnet 0.22.2
gnunet-service-nat_helper.c
Go to the documentation of this file.
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2009, 2010, 2011, 2016 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
27#include "platform.h"
28#include "gnunet_util_lib.h"
30
31
36{
40 struct in_addr internal_address;
41
46
50 void *cb_cls;
51
56
61
66
71
76
81};
82
83
90static void
91restart_nat_server (void *cls);
92
93
99static void
101{
102 GNUNET_assert (NULL == h->server_read_task);
103 h->server_retry_delay = GNUNET_TIME_STD_BACKOFF (h->server_retry_delay);
104 h->server_read_task = GNUNET_SCHEDULER_add_delayed (h->server_retry_delay,
106 h);
107}
108
109
117static void
119{
120 struct HelperContext *h = cls;
121 char mybuf[40];
122 ssize_t bytes;
123 int port;
124 const char *port_start;
125 struct sockaddr_in sin_addr;
126
127 h->server_read_task = NULL;
128 memset (mybuf, 0, sizeof(mybuf));
129 bytes =
130 GNUNET_DISK_file_read (h->server_stdout_handle, mybuf, sizeof(mybuf));
131 if (bytes < 1)
132 {
134 "Finished reading from server stdout with code: %d\n",
135 (int) bytes);
136 if (0 != GNUNET_OS_process_kill (h->server_proc, GNUNET_TERM_SIG))
138 GNUNET_OS_process_wait (h->server_proc);
139 GNUNET_OS_process_destroy (h->server_proc);
140 h->server_proc = NULL;
141 GNUNET_DISK_pipe_close (h->server_stdout);
142 h->server_stdout = NULL;
143 h->server_stdout_handle = NULL;
144 try_again (h);
145 return;
146 }
147
148 port_start = NULL;
149 for (size_t i = 0; i < sizeof(mybuf); i++)
150 {
151 if (mybuf[i] == '\n')
152 {
153 mybuf[i] = '\0';
154 break;
155 }
156 if ((mybuf[i] == ':') && (i + 1 < sizeof(mybuf)))
157 {
158 mybuf[i] = '\0';
159 port_start = &mybuf[i + 1];
160 }
161 }
162
163 /* construct socket address of sender */
164 memset (&sin_addr, 0, sizeof(sin_addr));
165 sin_addr.sin_family = AF_INET;
166#if HAVE_SOCKADDR_IN_SIN_LEN
167 sin_addr.sin_len = sizeof(sin_addr);
168#endif
169 if ((NULL == port_start) || (1 != sscanf (port_start, "%d", &port)) ||
170 (-1 == inet_pton (AF_INET, mybuf, &sin_addr.sin_addr)))
171 {
172 /* should we restart gnunet-helper-nat-server? */
174 _ (
175 "gnunet-helper-nat-server generated malformed address `%s'\n")
176 ,
177 mybuf);
178 h->server_read_task =
180 h->server_stdout_handle,
182 h);
183 return;
184 }
185 sin_addr.sin_port = htons ((uint16_t) port);
187 "gnunet-helper-nat-server read: %s:%d\n",
188 mybuf,
189 port);
190 h->cb (h->cb_cls, &sin_addr);
191 h->server_read_task =
193 h->server_stdout_handle,
195 h);
196}
197
198
205static void
207{
208 struct HelperContext *h = cls;
209 char *binary;
210 char ia[INET_ADDRSTRLEN];
211
212 h->server_read_task = NULL;
213 GNUNET_assert (NULL !=
214 inet_ntop (AF_INET, &h->internal_address, ia, sizeof(ia)));
215 /* Start the server process */
217 h->cfg,
218 "gnunet-helper-nat-server");
219 if (GNUNET_YES !=
222 ia))
223 {
224 /* move instantly to max delay, as this is unlikely to be fixed */
226 GNUNET_free (binary);
227 try_again (h);
228 return;
229 }
230 h->server_stdout =
232 if (NULL == h->server_stdout)
233 {
235 GNUNET_free (binary);
236 try_again (h);
237 return;
238 }
240 "Starting `%s' at `%s'\n",
241 "gnunet-helper-nat-server",
242 ia);
244 NULL,
245 h->server_stdout,
246 NULL,
247 binary,
248 "gnunet-helper-nat-server",
249 ia,
250 NULL);
251 GNUNET_free (binary);
252 if (NULL == h->server_proc)
253 {
255 _ ("Failed to start %s\n"),
256 "gnunet-helper-nat-server");
257 GNUNET_DISK_pipe_close (h->server_stdout);
258 h->server_stdout = NULL;
259 try_again (h);
260 return;
261 }
262 /* Close the write end of the read pipe */
264 h->server_stdout_handle =
266 h->server_read_task =
268 h->server_stdout_handle,
270 h);
271}
272
273
274struct HelperContext *
277 void *cb_cls,
278 const struct GNUNET_CONFIGURATION_Handle *cfg)
279{
280 struct HelperContext *h;
281
282 h = GNUNET_new (struct HelperContext);
283 h->cb = cb;
284 h->cb_cls = cb_cls;
285 h->internal_address = *internal_address;
286 h->cfg = cfg;
288 if (NULL == h->server_stdout)
289 {
291 return NULL;
292 }
293 return h;
294}
295
296
303void
305{
306 if (NULL != h->server_read_task)
307 {
308 GNUNET_SCHEDULER_cancel (h->server_read_task);
309 h->server_read_task = NULL;
310 }
311 if (NULL != h->server_proc)
312 {
313 if (0 != GNUNET_OS_process_kill (h->server_proc, GNUNET_TERM_SIG))
315 GNUNET_OS_process_wait (h->server_proc);
316 GNUNET_OS_process_destroy (h->server_proc);
317 h->server_proc = NULL;
318 GNUNET_DISK_pipe_close (h->server_stdout);
319 h->server_stdout = NULL;
320 h->server_stdout_handle = NULL;
321 }
322 if (NULL != h->server_stdout)
323 {
324 GNUNET_DISK_pipe_close (h->server_stdout);
325 h->server_stdout = NULL;
326 h->server_stdout_handle = NULL;
327 }
328 GNUNET_free (h);
329}
330
331
344int
346 uint16_t internal_port,
347 const struct in_addr *remote_v4,
348 const struct GNUNET_CONFIGURATION_Handle *cfg)
349{
350 char intv4[INET_ADDRSTRLEN];
351 char remv4[INET_ADDRSTRLEN];
352 char port_as_string[6];
353 struct GNUNET_OS_Process *proc;
354 char *binary;
355
356 if (NULL == inet_ntop (AF_INET, internal_address, intv4, INET_ADDRSTRLEN))
357 {
359 return GNUNET_SYSERR;
360 }
361 if (NULL == inet_ntop (AF_INET, remote_v4, remv4, INET_ADDRSTRLEN))
362 {
364 return GNUNET_SYSERR;
365 }
366 GNUNET_snprintf (port_as_string,
367 sizeof(port_as_string),
368 "%d",
369 internal_port);
371 "Running gnunet-helper-nat-client %s %s %u\n",
372 intv4,
373 remv4,
374 internal_port);
376 cfg,
377 "gnunet-helper-nat-client");
379 NULL,
380 NULL,
381 NULL,
382 binary,
383 "gnunet-helper-nat-client",
384 intv4,
385 remv4,
386 port_as_string,
387 NULL);
388 GNUNET_free (binary);
389 if (NULL == proc)
390 return GNUNET_SYSERR;
391 /* we know that the gnunet-helper-nat-client will terminate virtually
392 * instantly */
395 return GNUNET_OK;
396}
397
398
399/* end of gnunet-service-nat_helper.c */
static struct GNUNET_ARM_Handle * h
Connection with ARM.
Definition: gnunet-arm.c:98
static struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
Definition: gnunet-arm.c:108
static uint16_t port
Port number.
Definition: gnunet-bcd.c:146
int GN_request_connection_reversal(const struct in_addr *internal_address, uint16_t internal_port, const struct in_addr *remote_v4, const struct GNUNET_CONFIGURATION_Handle *cfg)
We want to connect to a peer that is behind NAT.
struct HelperContext * GN_start_gnunet_nat_server_(const struct in_addr *internal_address, GN_ReversalCallback cb, void *cb_cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
Start the gnunet-helper-nat-server and process incoming requests.
static void nat_server_read(void *cls)
We have been notified that gnunet-helper-nat-server has written something to stdout.
void GN_stop_gnunet_nat_server_(struct HelperContext *h)
Start the gnunet-helper-nat-server and process incoming requests.
static void try_again(struct HelperContext *h)
Try again starting the helper later.
static void restart_nat_server(void *cls)
Task that restarts the gnunet-helper-nat-server process after a crash after a certain delay.
runs the gnunet-helper-nat-server
void(* GN_ReversalCallback)(void *cls, const struct sockaddr_in *ra)
Function called whenever we get a connection reversal request from another peer.
const struct GNUNET_DISK_FileHandle * GNUNET_DISK_pipe_handle(const struct GNUNET_DISK_PipeHandle *p, enum GNUNET_DISK_PipeEnd n)
Get the handle to a particular pipe end.
Definition: disk.c:1621
struct GNUNET_DISK_PipeHandle * GNUNET_DISK_pipe(enum GNUNET_DISK_PipeFlags pf)
Creates an interprocess channel.
Definition: disk.c:1444
enum GNUNET_GenericReturnValue GNUNET_DISK_pipe_close(struct GNUNET_DISK_PipeHandle *p)
Closes an interprocess channel.
Definition: disk.c:1591
ssize_t GNUNET_DISK_file_read(const struct GNUNET_DISK_FileHandle *h, void *result, size_t len)
Read the contents of a binary file into a buffer.
Definition: disk.c:660
enum GNUNET_GenericReturnValue GNUNET_DISK_pipe_close_end(struct GNUNET_DISK_PipeHandle *p, enum GNUNET_DISK_PipeEnd end)
Closes one half of an interprocess channel.
Definition: disk.c:1538
@ GNUNET_DISK_PF_BLOCKING_RW
Configure both pipe ends for blocking operations if set.
@ GNUNET_DISK_PIPE_END_WRITE
The writing-end of a pipe.
@ GNUNET_DISK_PIPE_END_READ
The reading-end of a pipe.
#define GNUNET_log(kind,...)
@ GNUNET_OK
@ GNUNET_YES
@ GNUNET_SYSERR
#define GNUNET_log_from_strerror(level, component, cmd)
Log an error message at log-level 'level' that indicates a failure of the command 'cmd' with the mess...
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_log_strerror(level, cmd)
Log an error message at log-level 'level' that indicates a failure of the command 'cmd' with the mess...
@ GNUNET_ERROR_TYPE_WARNING
@ GNUNET_ERROR_TYPE_ERROR
@ GNUNET_ERROR_TYPE_DEBUG
int GNUNET_snprintf(char *buf, size_t size, const char *format,...) __attribute__((format(printf
Like snprintf, just aborts if the buffer is of insufficient size.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define GNUNET_free(ptr)
Wrapper around free.
struct GNUNET_OS_Process * GNUNET_OS_start_process(enum GNUNET_OS_InheritStdioFlags std_inheritance, struct GNUNET_DISK_PipeHandle *pipe_stdin, struct GNUNET_DISK_PipeHandle *pipe_stdout, struct GNUNET_DISK_PipeHandle *pipe_stderr, const char *filename,...)
Start a process.
Definition: os_priority.c:620
const struct GNUNET_OS_ProjectData * GNUNET_OS_project_data_gnunet(void)
Return default project data used by 'libgnunetutil' for GNUnet.
void GNUNET_OS_process_destroy(struct GNUNET_OS_Process *proc)
Cleans up process structure contents (OS-dependent) and deallocates it.
Definition: os_priority.c:260
enum GNUNET_GenericReturnValue GNUNET_OS_check_helper_binary(const char *binary, bool check_suid, const char *params)
Check whether an executable exists and possibly if the suid bit is set on the file.
int GNUNET_OS_process_kill(struct GNUNET_OS_Process *proc, int sig)
Sends a signal to the process.
Definition: os_priority.c:210
char * GNUNET_OS_get_suid_binary_path(const struct GNUNET_OS_ProjectData *pd, const struct GNUNET_CONFIGURATION_Handle *cfg, const char *progname)
Given the name of a helper, service or daemon binary construct the full path to the binary using the ...
enum GNUNET_GenericReturnValue GNUNET_OS_process_wait(struct GNUNET_OS_Process *proc)
Wait for a process to terminate.
Definition: os_priority.c:871
@ GNUNET_OS_INHERIT_STD_NONE
No standard streams should be inherited.
Definition: gnunet_os_lib.h:77
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_read_file(struct GNUNET_TIME_Relative delay, const struct GNUNET_DISK_FileHandle *rfd, GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run with a specified delay or when the specified file descriptor is ready f...
Definition: scheduler.c:1661
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition: scheduler.c:980
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_delayed(struct GNUNET_TIME_Relative delay, GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run with a specified delay.
Definition: scheduler.c:1277
#define GNUNET_TIME_UNIT_FOREVER_REL
Constant used to specify "forever".
#define GNUNET_TIME_STD_EXPONENTIAL_BACKOFF_THRESHOLD
Threshold after which exponential backoff should not increase (15 m).
#define GNUNET_TIME_STD_BACKOFF(r)
Perform our standard exponential back-off calculation, starting at 1 ms and then going by a factor of...
#define _(String)
GNU gettext support macro.
Definition: platform.h:179
#define GNUNET_TERM_SIG
The termination signal.
Definition: platform.h:235
const struct GNUNET_CONFIGURATION_Handle * cfg
The configuration that we are using.
Definition: arm_api.c:112
Handle used to access files (and pipes).
Handle used to manage a pipe.
Definition: disk.c:69
Entry in list of pending tasks.
Definition: scheduler.c:136
Time for relative time used by GNUnet, in microseconds.
Information we keep per NAT helper process.
struct GNUNET_DISK_PipeHandle * server_stdout
stdout pipe handle for the gnunet-helper-nat-server process
const struct GNUNET_CONFIGURATION_Handle * cfg
Handle to the GNUnet configuration.
struct GNUNET_OS_Process * server_proc
The process id of the server process (if behind NAT)
struct GNUNET_TIME_Relative server_retry_delay
How long do we wait for restarting a crashed gnunet-helper-nat-server?
struct GNUNET_SCHEDULER_Task * server_read_task
ID of select gnunet-helper-nat-server stdout read task.
struct in_addr internal_address
IP address we pass to the NAT helper.
const struct GNUNET_DISK_FileHandle * server_stdout_handle
stdout file handle (for reading) for the gnunet-helper-nat-server process
GN_ReversalCallback cb
Function to call if we receive a reversal request.
void * cb_cls
Closure for cb.