GNUnet  0.10.x
Data Structures | Functions
gnunet-service-nat_helper.c File Reference

runs the gnunet-helper-nat-server More...

#include "platform.h"
#include "gnunet_util_lib.h"
#include "gnunet-service-nat_helper.h"
Include dependency graph for gnunet-service-nat_helper.c:

Go to the source code of this file.

Data Structures

struct  HelperContext
 Information we keep per NAT helper process. More...
 

Functions

static void restart_nat_server (void *cls)
 Task that restarts the gnunet-helper-nat-server process after a crash after a certain delay. More...
 
static void try_again (struct HelperContext *h)
 Try again starting the helper later. More...
 
static void nat_server_read (void *cls)
 We have been notified that gnunet-helper-nat-server has written something to stdout. More...
 
struct HelperContextGN_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. More...
 
void GN_stop_gnunet_nat_server_ (struct HelperContext *h)
 Start the gnunet-helper-nat-server and process incoming requests. More...
 
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. More...
 

Detailed Description

runs the gnunet-helper-nat-server

Author
Milan Bouchet-Valat
Christian Grothoff

Definition in file gnunet-service-nat_helper.c.

Function Documentation

◆ restart_nat_server()

static void restart_nat_server ( void *  cls)
static

Task that restarts the gnunet-helper-nat-server process after a crash after a certain delay.

Parameters
clsa struct HelperContext

Definition at line 204 of file gnunet-service-nat_helper.c.

References _, HelperContext::cfg, GNUNET_assert, GNUNET_DISK_pipe(), GNUNET_DISK_pipe_close(), GNUNET_DISK_pipe_close_end(), GNUNET_DISK_PIPE_END_READ, GNUNET_DISK_PIPE_END_WRITE, GNUNET_DISK_pipe_handle(), GNUNET_ERROR_TYPE_DEBUG, GNUNET_ERROR_TYPE_ERROR, GNUNET_ERROR_TYPE_WARNING, GNUNET_free, GNUNET_log, GNUNET_log_strerror, GNUNET_NO, GNUNET_OS_check_helper_binary(), GNUNET_OS_get_suid_binary_path(), GNUNET_OS_start_process(), GNUNET_SCHEDULER_add_read_file(), GNUNET_TIME_STD_EXPONENTIAL_BACKOFF_THRESHOLD, GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_YES, h, HelperContext::internal_address, nat_server_read(), HelperContext::server_proc, HelperContext::server_read_task, HelperContext::server_retry_delay, HelperContext::server_stdout, HelperContext::server_stdout_handle, and try_again().

Referenced by GN_start_gnunet_nat_server_(), and try_again().

205 {
206  struct HelperContext *h = cls;
207  char *binary;
208  char ia[INET_ADDRSTRLEN];
209 
210  h->server_read_task = NULL;
211  GNUNET_assert(NULL !=
212  inet_ntop(AF_INET, &h->internal_address, ia, sizeof(ia)));
213  /* Start the server process */
214  binary = GNUNET_OS_get_suid_binary_path(h->cfg, "gnunet-helper-nat-server");
216  {
217  /* move instantly to max delay, as this is unlikely to be fixed */
219  GNUNET_free(binary);
220  try_again(h);
221  return;
222  }
223  h->server_stdout =
225  if (NULL == h->server_stdout)
226  {
228  GNUNET_free(binary);
229  try_again(h);
230  return;
231  }
233  "Starting `%s' at `%s'\n",
234  "gnunet-helper-nat-server",
235  ia);
237  0,
238  NULL,
239  h->server_stdout,
240  NULL,
241  binary,
242  "gnunet-helper-nat-server",
243  ia,
244  NULL);
245  GNUNET_free(binary);
246  if (NULL == h->server_proc)
247  {
249  _("Failed to start %s\n"),
250  "gnunet-helper-nat-server");
252  h->server_stdout = NULL;
253  try_again(h);
254  return;
255  }
256  /* Close the write end of the read pipe */
260  h->server_read_task =
264  h);
265 }
struct GNUNET_OS_Process * server_proc
The process id of the server process (if behind NAT)
static void try_again(struct HelperContext *h)
Try again starting the helper later.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
struct GNUNET_OS_Process * GNUNET_OS_start_process(int pipe_control, 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:687
#define GNUNET_NO
Definition: gnunet_common.h:78
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:1615
int GNUNET_OS_check_helper_binary(const char *binary, int check_suid, const char *params)
Check whether an executable exists and possibly if the suid bit is set on the file.
struct GNUNET_TIME_Relative server_retry_delay
How long do we wait for restarting a crashed gnunet-helper-nat-server?
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:1744
static struct GNUNET_ARM_Handle * h
Connection with ARM.
Definition: gnunet-arm.c:94
struct GNUNET_SCHEDULER_Task * server_read_task
ID of select gnunet-helper-nat-server stdout read task.
#define _(String)
GNU gettext support macro.
Definition: platform.h:181
#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...
#define GNUNET_TIME_STD_EXPONENTIAL_BACKOFF_THRESHOLD
Threshold after which exponential backoff should not increase (15 m).
char * GNUNET_OS_get_suid_binary_path(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 ...
#define GNUNET_TIME_UNIT_FOREVER_REL
Constant used to specify "forever".
The writing-end of a pipe.
The reading-end of a pipe.
struct GNUNET_DISK_PipeHandle * GNUNET_DISK_pipe(int blocking_read, int blocking_write, int inherit_read, int inherit_write)
Creates an interprocess channel.
Definition: disk.c:1518
struct GNUNET_DISK_PipeHandle * server_stdout
stdout pipe handle for the gnunet-helper-nat-server process
int GNUNET_DISK_pipe_close(struct GNUNET_DISK_PipeHandle *p)
Closes an interprocess channel.
Definition: disk.c:1706
struct in_addr internal_address
IP address we pass to the NAT helper.
int 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:1635
static void nat_server_read(void *cls)
We have been notified that gnunet-helper-nat-server has written something to stdout.
#define GNUNET_log(kind,...)
#define GNUNET_YES
Definition: gnunet_common.h:77
const struct GNUNET_DISK_FileHandle * server_stdout_handle
stdout file handle (for reading) for the gnunet-helper-nat-server process
Information we keep per NAT helper process.
const struct GNUNET_CONFIGURATION_Handle * cfg
Handle to the GNUnet configuration.
#define GNUNET_free(ptr)
Wrapper around free.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ try_again()

static void try_again ( struct HelperContext h)
static

Try again starting the helper later.

Parameters
hcontext of the helper

Definition at line 99 of file gnunet-service-nat_helper.c.

References GNUNET_assert, GNUNET_SCHEDULER_add_delayed(), GNUNET_TIME_STD_BACKOFF, restart_nat_server(), HelperContext::server_read_task, and HelperContext::server_retry_delay.

Referenced by nat_server_read(), and restart_nat_server().

100 {
101  GNUNET_assert(NULL == h->server_read_task);
105  h);
106 }
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
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 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:1237
static void restart_nat_server(void *cls)
Task that restarts the gnunet-helper-nat-server process after a crash after a certain delay...
#define GNUNET_TIME_STD_BACKOFF(r)
Perform our standard exponential back-off calculation, starting at 1 ms and then going by a factor of...
Here is the call graph for this function:
Here is the caller graph for this function:

◆ nat_server_read()

static void nat_server_read ( void *  cls)
static

We have been notified that gnunet-helper-nat-server has written something to stdout.

Handle the output, then reschedule this function to be called again once more is available.

Parameters
clsthe struct HelperContext

Definition at line 117 of file gnunet-service-nat_helper.c.

References _, HelperContext::cb, HelperContext::cb_cls, GNUNET_DISK_file_read(), GNUNET_DISK_pipe_close(), GNUNET_ERROR_TYPE_DEBUG, GNUNET_ERROR_TYPE_WARNING, GNUNET_log, GNUNET_log_from_strerror, GNUNET_OS_process_destroy(), GNUNET_OS_process_kill(), GNUNET_OS_process_wait(), GNUNET_SCHEDULER_add_read_file(), GNUNET_TERM_SIG, GNUNET_TIME_UNIT_FOREVER_REL, h, port, HelperContext::server_proc, HelperContext::server_read_task, HelperContext::server_stdout, HelperContext::server_stdout_handle, and try_again().

Referenced by restart_nat_server().

118 {
119  struct HelperContext *h = cls;
120  char mybuf[40];
121  ssize_t bytes;
122  int port;
123  const char *port_start;
124  struct sockaddr_in sin_addr;
125 
126  h->server_read_task = NULL;
127  memset(mybuf, 0, sizeof(mybuf));
128  bytes =
129  GNUNET_DISK_file_read(h->server_stdout_handle, mybuf, sizeof(mybuf));
130  if (bytes < 1)
131  {
133  "Finished reading from server stdout with code: %d\n",
134  (int)bytes);
139  h->server_proc = NULL;
141  h->server_stdout = NULL;
142  h->server_stdout_handle = NULL;
143  try_again(h);
144  return;
145  }
146 
147  port_start = NULL;
148  for (size_t i = 0; i < sizeof(mybuf); i++)
149  {
150  if (mybuf[i] == '\n')
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  }
160  }
161 
162  /* construct socket address of sender */
163  memset(&sin_addr, 0, sizeof(sin_addr));
164  sin_addr.sin_family = AF_INET;
165 #if HAVE_SOCKADDR_IN_SIN_LEN
166  sin_addr.sin_len = sizeof(sin_addr);
167 #endif
168  if ((NULL == port_start) || (1 != sscanf(port_start, "%d", &port)) ||
169  (-1 == inet_pton(AF_INET, mybuf, &sin_addr.sin_addr)))
170  {
171  /* should we restart gnunet-helper-nat-server? */
173  _(
174  "gnunet-helper-nat-server generated malformed address `%s'\n"),
175  mybuf);
176  h->server_read_task =
180  h);
181  return;
182  }
183  sin_addr.sin_port = htons((uint16_t)port);
185  "gnunet-helper-nat-server read: %s:%d\n",
186  mybuf,
187  port);
188  h->cb(h->cb_cls, &sin_addr);
189  h->server_read_task =
193  h);
194 }
struct GNUNET_OS_Process * server_proc
The process id of the server process (if behind NAT)
GN_ReversalCallback cb
Function to call if we receive a reversal request.
static void try_again(struct HelperContext *h)
Try again starting the helper later.
void GNUNET_OS_process_destroy(struct GNUNET_OS_Process *proc)
Cleans up process structure contents (OS-dependent) and deallocates it.
Definition: os_priority.c:286
#define GNUNET_log_from_strerror(level, component, cmd)
Log an error message at log-level &#39;level&#39; that indicates a failure of the command &#39;cmd&#39; with the mess...
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:729
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:1615
static struct GNUNET_ARM_Handle * h
Connection with ARM.
Definition: gnunet-arm.c:94
struct GNUNET_SCHEDULER_Task * server_read_task
ID of select gnunet-helper-nat-server stdout read task.
#define _(String)
GNU gettext support macro.
Definition: platform.h:181
#define GNUNET_TIME_UNIT_FOREVER_REL
Constant used to specify "forever".
struct GNUNET_DISK_PipeHandle * server_stdout
stdout pipe handle for the gnunet-helper-nat-server process
int GNUNET_DISK_pipe_close(struct GNUNET_DISK_PipeHandle *p)
Closes an interprocess channel.
Definition: disk.c:1706
static void nat_server_read(void *cls)
We have been notified that gnunet-helper-nat-server has written something to stdout.
int GNUNET_OS_process_wait(struct GNUNET_OS_Process *proc)
Wait for a process to terminate.
Definition: os_priority.c:1000
static uint16_t port
Port number.
Definition: gnunet-bcd.c:81
void * cb_cls
Closure for cb.
#define GNUNET_log(kind,...)
const struct GNUNET_DISK_FileHandle * server_stdout_handle
stdout file handle (for reading) for the gnunet-helper-nat-server process
#define GNUNET_TERM_SIG
The termination signal.
Definition: platform.h:242
Information we keep per NAT helper process.
int GNUNET_OS_process_kill(struct GNUNET_OS_Process *proc, int sig)
Sends a signal to the process.
Definition: os_priority.c:224
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GN_start_gnunet_nat_server_()

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.

Parameters
internal_address
cbfunction to call if we receive a request
cb_clsclosure for cb
cfgHandle to the GNUnet configuration
Returns
NULL on error

Definition at line 279 of file gnunet-service-nat_helper.c.

References HelperContext::cb, HelperContext::cb_cls, HelperContext::cfg, GN_stop_gnunet_nat_server_(), GNUNET_new, h, HelperContext::internal_address, restart_nat_server(), and HelperContext::server_stdout.

Referenced by run_scan().

283 {
284  struct HelperContext *h;
285 
286  h = GNUNET_new(struct HelperContext);
287  h->cb = cb;
288  h->cb_cls = cb_cls;
290  h->cfg = cfg;
292  if (NULL == h->server_stdout)
293  {
295  return NULL;
296  }
297  return h;
298 }
GN_ReversalCallback cb
Function to call if we receive a reversal request.
void GN_stop_gnunet_nat_server_(struct HelperContext *h)
Start the gnunet-helper-nat-server and process incoming requests.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
static struct GNUNET_ARM_Handle * h
Connection with ARM.
Definition: gnunet-arm.c:94
static struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
Definition: gnunet-arm.c:104
struct GNUNET_DISK_PipeHandle * server_stdout
stdout pipe handle for the gnunet-helper-nat-server process
static void restart_nat_server(void *cls)
Task that restarts the gnunet-helper-nat-server process after a crash after a certain delay...
struct in_addr internal_address
IP address we pass to the NAT helper.
void * cb_cls
Closure for cb.
Information we keep per NAT helper process.
const struct GNUNET_CONFIGURATION_Handle * cfg
Handle to the GNUnet configuration.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GN_stop_gnunet_nat_server_()

void GN_stop_gnunet_nat_server_ ( struct HelperContext h)

Start the gnunet-helper-nat-server and process incoming requests.

Parameters
hhelper context to stop

Definition at line 308 of file gnunet-service-nat_helper.c.

References GNUNET_DISK_pipe_close(), GNUNET_ERROR_TYPE_WARNING, GNUNET_free, GNUNET_log_strerror, GNUNET_OS_process_destroy(), GNUNET_OS_process_kill(), GNUNET_OS_process_wait(), GNUNET_SCHEDULER_cancel(), GNUNET_TERM_SIG, HelperContext::server_proc, HelperContext::server_read_task, HelperContext::server_stdout, and HelperContext::server_stdout_handle.

Referenced by free_lal(), and GN_start_gnunet_nat_server_().

309 {
310  if (NULL != h->server_read_task)
311  {
313  h->server_read_task = NULL;
314  }
315  if (NULL != h->server_proc)
316  {
321  h->server_proc = NULL;
323  h->server_stdout = NULL;
324  h->server_stdout_handle = NULL;
325  }
326  if (NULL != h->server_stdout)
327  {
329  h->server_stdout = NULL;
330  h->server_stdout_handle = NULL;
331  }
332  GNUNET_free(h);
333 }
struct GNUNET_OS_Process * server_proc
The process id of the server process (if behind NAT)
void GNUNET_OS_process_destroy(struct GNUNET_OS_Process *proc)
Cleans up process structure contents (OS-dependent) and deallocates it.
Definition: os_priority.c:286
struct GNUNET_SCHEDULER_Task * server_read_task
ID of select gnunet-helper-nat-server stdout read task.
#define GNUNET_log_strerror(level, cmd)
Log an error message at log-level &#39;level&#39; that indicates a failure of the command &#39;cmd&#39; with the mess...
struct GNUNET_DISK_PipeHandle * server_stdout
stdout pipe handle for the gnunet-helper-nat-server process
int GNUNET_DISK_pipe_close(struct GNUNET_DISK_PipeHandle *p)
Closes an interprocess channel.
Definition: disk.c:1706
int GNUNET_OS_process_wait(struct GNUNET_OS_Process *proc)
Wait for a process to terminate.
Definition: os_priority.c:1000
const struct GNUNET_DISK_FileHandle * server_stdout_handle
stdout file handle (for reading) for the gnunet-helper-nat-server process
#define GNUNET_TERM_SIG
The termination signal.
Definition: platform.h:242
#define GNUNET_free(ptr)
Wrapper around free.
int GNUNET_OS_process_kill(struct GNUNET_OS_Process *proc, int sig)
Sends a signal to the process.
Definition: os_priority.c:224
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition: scheduler.c:956
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GN_request_connection_reversal()

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.

Run the gnunet-helper-nat-client to send dummy ICMP responses to cause that peer to connect to us (connection reversal).

Parameters
internal_addressout internal address to use
internal_portport to use
remote_v4the address of the peer (IPv4-only)
cfghandle to the GNUnet configuration
Returns
GNUNET_SYSERR on error, GNUNET_OK otherwise

Definition at line 349 of file gnunet-service-nat_helper.c.

References GNUNET_ERROR_TYPE_DEBUG, GNUNET_ERROR_TYPE_WARNING, GNUNET_free, GNUNET_log, GNUNET_log_strerror, GNUNET_NO, GNUNET_OK, GNUNET_OS_get_suid_binary_path(), GNUNET_OS_process_destroy(), GNUNET_OS_process_wait(), GNUNET_OS_start_process(), GNUNET_snprintf(), and GNUNET_SYSERR.

Referenced by handle_request_connection_reversal().

353 {
354  char intv4[INET_ADDRSTRLEN];
355  char remv4[INET_ADDRSTRLEN];
356  char port_as_string[6];
357  struct GNUNET_OS_Process *proc;
358  char *binary;
359 
360  if (NULL == inet_ntop(AF_INET, internal_address, intv4, INET_ADDRSTRLEN))
361  {
363  return GNUNET_SYSERR;
364  }
365  if (NULL == inet_ntop(AF_INET, remote_v4, remv4, INET_ADDRSTRLEN))
366  {
368  return GNUNET_SYSERR;
369  }
370  GNUNET_snprintf(port_as_string,
371  sizeof(port_as_string),
372  "%d",
373  internal_port);
375  "Running gnunet-helper-nat-client %s %s %u\n",
376  intv4,
377  remv4,
378  internal_port);
379  binary = GNUNET_OS_get_suid_binary_path(cfg, "gnunet-helper-nat-client");
381  0,
382  NULL,
383  NULL,
384  NULL,
385  binary,
386  "gnunet-helper-nat-client",
387  intv4,
388  remv4,
389  port_as_string,
390  NULL);
391  GNUNET_free(binary);
392  if (NULL == proc)
393  return GNUNET_SYSERR;
394  /* we know that the gnunet-helper-nat-client will terminate virtually
395  * instantly */
398  return GNUNET_OK;
399 }
void GNUNET_OS_process_destroy(struct GNUNET_OS_Process *proc)
Cleans up process structure contents (OS-dependent) and deallocates it.
Definition: os_priority.c:286
int GNUNET_snprintf(char *buf, size_t size, const char *format,...)
Like snprintf, just aborts if the buffer is of insufficient size.
struct GNUNET_OS_Process * GNUNET_OS_start_process(int pipe_control, 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:687
#define GNUNET_NO
Definition: gnunet_common.h:78
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
#define GNUNET_log_strerror(level, cmd)
Log an error message at log-level &#39;level&#39; that indicates a failure of the command &#39;cmd&#39; with the mess...
char * GNUNET_OS_get_suid_binary_path(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 ...
#define GNUNET_SYSERR
Definition: gnunet_common.h:76
int GNUNET_OS_process_wait(struct GNUNET_OS_Process *proc)
Wait for a process to terminate.
Definition: os_priority.c:1000
#define GNUNET_log(kind,...)
#define GNUNET_free(ptr)
Wrapper around free.
Here is the call graph for this function:
Here is the caller graph for this function: