GNUnet 0.22.2
gnunet-nat.c File Reference
#include "platform.h"
#include "gnunet_util_lib.h"
#include "gnunet_nat_service.h"
Include dependency graph for gnunet-nat.c:

Go to the source code of this file.

Functions

static void test_finished ()
 Test if all activities have finished, and if so, terminate. More...
 
static void address_cb (void *cls, void **app_ctx, int add_remove, enum GNUNET_NAT_AddressClass ac, const struct sockaddr *addr, socklen_t addrlen)
 Signature of the callback passed to GNUNET_NAT_register() for a function to call whenever our set of 'valid' addresses changes. More...
 
static void reversal_cb (void *cls, const struct sockaddr *addr, socklen_t remote_addrlen)
 Signature of the callback passed to GNUNET_NAT_register(). More...
 
static void do_shutdown (void *cls)
 Task run on shutdown. More...
 
static void stun_read_task (void *cls)
 Task to receive incoming packets for STUN processing. More...
 
static void run (void *cls, char *const *args, const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *c)
 Main function that will be run. More...
 
int main (int argc, char *const argv[])
 Main function of gnunet-nat. More...
 

Variables

static int global_ret
 Value to return from main(). More...
 
static char * section_name
 Name of section in configuration file to use for additional options. More...
 
static int use_udp
 Flag set to 1 if we use IPPROTO_UDP. More...
 
static int listen_reversal
 Flag set to 1 if we are to listen for connection reversal requests. More...
 
static int use_tcp
 Flag set to 1 if we use IPPROTO_TCP. More...
 
static uint8_t proto
 Protocol to use. More...
 
static char * local_addr
 Local address to use for connection reversal request. More...
 
static char * remote_addr
 Remote address to use for connection reversal request. More...
 
static int do_stun
 Should we actually bind to #bind_addr and receive and process STUN requests? More...
 
static struct GNUNET_NAT_Handlenh
 Handle to NAT operation. More...
 
static struct GNUNET_NETWORK_Handlels
 Listen socket for STUN processing. More...
 
static struct GNUNET_SCHEDULER_Taskrtask
 Task for reading STUN packets. More...
 

Function Documentation

◆ test_finished()

static void test_finished ( void  )
static

Test if all activities have finished, and if so, terminate.

Definition at line 98 of file gnunet-nat.c.

99{
100 if (NULL != nh)
101 return;
102 if (NULL != rtask)
103 return;
105}
static struct GNUNET_NAT_Handle * nh
Handle to NAT operation.
Definition: gnunet-nat.c:80
static struct GNUNET_SCHEDULER_Task * rtask
Task for reading STUN packets.
Definition: gnunet-nat.c:90
void GNUNET_SCHEDULER_shutdown(void)
Request the shutdown of a scheduler.
Definition: scheduler.c:567

References GNUNET_SCHEDULER_shutdown(), nh, and rtask.

Referenced by run().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ address_cb()

static void address_cb ( void *  cls,
void **  app_ctx,
int  add_remove,
enum GNUNET_NAT_AddressClass  ac,
const struct sockaddr *  addr,
socklen_t  addrlen 
)
static

Signature of the callback passed to GNUNET_NAT_register() for a function to call whenever our set of 'valid' addresses changes.

Parameters
clsclosure, NULL
[in,out]app_ctxlocation where the app can store stuff on add and retrieve it on remove
add_removeGNUNET_YES to add a new public IP address, GNUNET_NO to remove a previous (now invalid) one
acaddress class the address belongs to
addreither the previous or the new public IP address
addrlenactual length of the addr

Definition at line 122 of file gnunet-nat.c.

128{
129 (void) cls;
130 (void) app_ctx;
131
132 fprintf (stdout,
133 "%s %s (%d)\n",
134 add_remove ? "+" : "-",
135 GNUNET_a2s (addr, addrlen),
136 (int) ac);
137}
const char * GNUNET_a2s(const struct sockaddr *addr, socklen_t addrlen)
Convert a "struct sockaddr*" (IPv4 or IPv6 address) to a string (for printing debug messages).

References GNUNET_a2s().

Referenced by run().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ reversal_cb()

static void reversal_cb ( void *  cls,
const struct sockaddr *  addr,
socklen_t  remote_addrlen 
)
static

Signature of the callback passed to GNUNET_NAT_register().

for a function to call whenever someone asks us to do connection reversal.

Parameters
clsclosure, NULL
remote_addrpublic IP address of the other peer
remote_addrlenactual length of the remote_addr

Definition at line 150 of file gnunet-nat.c.

153{
155 "Connection reversal requested by %s\n",
156 GNUNET_a2s (addr, remote_addrlen));
157}
#define GNUNET_log(kind,...)
@ GNUNET_ERROR_TYPE_MESSAGE

References GNUNET_a2s(), GNUNET_ERROR_TYPE_MESSAGE, and GNUNET_log.

Referenced by run().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ do_shutdown()

static void do_shutdown ( void *  cls)
static

Task run on shutdown.

Parameters
clsNULL

Definition at line 166 of file gnunet-nat.c.

167{
168 if (NULL != nh)
169 {
171 nh = NULL;
172 }
173 if (NULL != ls)
174 {
176 ls = NULL;
177 }
178 if (NULL != rtask)
179 {
181 rtask = NULL;
182 }
183}
static struct GNUNET_NETWORK_Handle * ls
Listen socket for STUN processing.
Definition: gnunet-nat.c:85
void GNUNET_NAT_unregister(struct GNUNET_NAT_Handle *nh)
Stop port redirection and public IP address detection for the given handle.
Definition: nat_api.c:703
enum GNUNET_GenericReturnValue GNUNET_NETWORK_socket_close(struct GNUNET_NETWORK_Handle *desc)
Close a socket.
Definition: network.c:508
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition: scheduler.c:980

References GNUNET_NAT_unregister(), GNUNET_NETWORK_socket_close(), GNUNET_SCHEDULER_cancel(), ls, nh, and rtask.

Referenced by run().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ stun_read_task()

static void stun_read_task ( void *  cls)
static

Task to receive incoming packets for STUN processing.

Definition at line 190 of file gnunet-nat.c.

191{
192 ssize_t size;
193
195 ls,
197 NULL);
199 if (size < 0)
200 {
201 GNUNET_break (0);
203 global_ret = 1;
204 return;
205 }
206 {
207 char buf[size + 1];
208 struct sockaddr_storage sa;
209 socklen_t salen = sizeof(sa);
210 ssize_t ret;
211
213 buf,
214 size + 1,
215 (struct sockaddr *) &sa,
216 &salen);
217 if (ret < size)
218 {
219 GNUNET_break (0);
221 global_ret = 1;
222 return;
223 }
225 (const struct sockaddr *) &sa,
226 salen,
227 buf,
228 ret);
229 }
230}
static int ret
Final status code.
Definition: gnunet-arm.c:93
static int global_ret
Value to return from main().
Definition: gnunet-nat.c:34
static void stun_read_task(void *cls)
Task to receive incoming packets for STUN processing.
Definition: gnunet-nat.c:190
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur.
int GNUNET_NAT_stun_handle_packet(struct GNUNET_NAT_Handle *nh, const struct sockaddr *sender_addr, size_t sender_addr_len, const void *data, size_t data_size)
Handle an incoming STUN message.
Definition: nat_api.c:601
ssize_t GNUNET_NETWORK_socket_recvfrom(const struct GNUNET_NETWORK_Handle *desc, void *buffer, size_t length, struct sockaddr *src_addr, socklen_t *addrlen)
Read data from a socket (always non-blocking).
Definition: network.c:688
ssize_t GNUNET_NETWORK_socket_recvfrom_amount(const struct GNUNET_NETWORK_Handle *desc)
How much data is available to be read on this descriptor?
Definition: network.c:671
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_read_net(struct GNUNET_TIME_Relative delay, struct GNUNET_NETWORK_Handle *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:1511
#define GNUNET_TIME_UNIT_FOREVER_REL
Constant used to specify "forever".
static unsigned int size
Size of the "table".
Definition: peer.c:68

References global_ret, GNUNET_break, GNUNET_NAT_stun_handle_packet(), GNUNET_NETWORK_socket_recvfrom(), GNUNET_NETWORK_socket_recvfrom_amount(), GNUNET_SCHEDULER_add_read_net(), GNUNET_SCHEDULER_shutdown(), GNUNET_TIME_UNIT_FOREVER_REL, ls, nh, ret, rtask, size, and stun_read_task().

Referenced by run(), and stun_read_task().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ run()

static void run ( void *  cls,
char *const *  args,
const char *  cfgfile,
const struct GNUNET_CONFIGURATION_Handle c 
)
static

Main function that will be run.

Parameters
clsclosure
argsremaining command-line arguments
cfgfilename of the configuration file used (for saving, can be NULL!)
cconfiguration

Definition at line 242 of file gnunet-nat.c.

246{
247 uint8_t af;
248 struct sockaddr *local_sa;
249 struct sockaddr *remote_sa;
250 socklen_t local_len;
251 size_t remote_len;
252
253 if (use_tcp && use_udp)
254 {
255 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE, "Cannot use TCP and UDP\n");
256 global_ret = 1;
257 return;
258 }
259 proto = 0;
260 if (use_tcp)
261 proto = IPPROTO_TCP;
262 if (use_udp)
263 proto = IPPROTO_UDP;
264
266
267 if (0 == proto)
268 {
269 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE, "Must specify either TCP or UDP\n");
270 global_ret = 1;
271 return;
272 }
273 local_len = 0;
274 local_sa = NULL;
275 remote_len = 0;
276 remote_sa = NULL;
277 if (NULL != local_addr)
278 {
279 local_len =
280 (socklen_t) GNUNET_STRINGS_parse_socket_addr (local_addr, &af, &local_sa);
281 if (0 == local_len)
282 {
284 "Invalid socket address `%s'\n",
285 local_addr);
286 goto fail_and_shutdown;
287 }
288 }
289
290 if (NULL != remote_addr)
291 {
292 remote_len =
294 if (0 == remote_len)
295 {
297 "Invalid socket address `%s'\n",
299 goto fail_and_shutdown;
300 }
301 }
302
303 if (NULL != local_addr)
304 {
305 if (NULL == section_name)
306 section_name = GNUNET_strdup ("undefined");
309 proto,
310 1,
311 (const struct sockaddr **) &local_sa,
312 &local_len,
313 &address_cb,
314 (listen_reversal) ? &reversal_cb : NULL,
315 NULL);
316 }
317 else if (listen_reversal)
318 {
320 "Use of `-W` only effective in combination with `-i`\n");
321 goto fail_and_shutdown;
322 }
323
324 if (NULL != remote_addr)
325 {
326 int ret;
327
328 if ((NULL == nh) || (sizeof(struct sockaddr_in) != local_len))
329 {
331 "Require IPv4 local address to initiate connection reversal\n");
332 goto fail_and_shutdown;
333 }
334 if (sizeof(struct sockaddr_in) != remote_len)
335 {
337 "Require IPv4 reversal target address\n");
338 goto fail_and_shutdown;
339 }
340 GNUNET_assert (AF_INET == local_sa->sa_family);
341 GNUNET_assert (AF_INET == remote_sa->sa_family);
343 (const struct sockaddr_in *) local_sa,
344 (const struct sockaddr_in *) remote_sa);
345 switch (ret)
346 {
347 case GNUNET_SYSERR:
349 "Connection reversal internal error\n");
350 break;
351
352 case GNUNET_NO:
354 "Connection reversal unavailable\n");
355 break;
356
357 case GNUNET_OK:
358 /* operation in progress */
359 break;
360 }
361 }
362
363 if (do_stun)
364 {
365 if (NULL == local_addr)
366 {
368 "Require local address to support STUN requests\n");
369 goto fail_and_shutdown;
370 }
371 if (IPPROTO_UDP != proto)
372 {
373 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE, "STUN only supported over UDP\n");
374 goto fail_and_shutdown;
375 }
376 ls = GNUNET_NETWORK_socket_create (af, SOCK_DGRAM, IPPROTO_UDP);
377 if (NULL == ls)
378 {
379 GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE, "Failed to create socket\n");
380 goto fail_and_shutdown;
381 }
382 if (GNUNET_OK != GNUNET_NETWORK_socket_bind (ls, local_sa, local_len))
383 {
385 "Failed to bind to %s: %s\n",
386 GNUNET_a2s (local_sa, local_len),
387 strerror (errno));
388 goto fail_and_shutdown;
389 }
391 ls,
393 NULL);
394 }
395 GNUNET_free (remote_sa);
396 GNUNET_free (local_sa);
397 test_finished ();
398 return;
399fail_and_shutdown:
400 global_ret = 1;
402 GNUNET_free (remote_sa);
403 GNUNET_free (local_sa);
404}
static void reversal_cb(void *cls, const struct sockaddr *addr, socklen_t remote_addrlen)
Signature of the callback passed to GNUNET_NAT_register().
Definition: gnunet-nat.c:150
static char * section_name
Name of section in configuration file to use for additional options.
Definition: gnunet-nat.c:40
static int listen_reversal
Flag set to 1 if we are to listen for connection reversal requests.
Definition: gnunet-nat.c:50
static void do_shutdown(void *cls)
Task run on shutdown.
Definition: gnunet-nat.c:166
static int use_udp
Flag set to 1 if we use IPPROTO_UDP.
Definition: gnunet-nat.c:45
static char * remote_addr
Remote address to use for connection reversal request.
Definition: gnunet-nat.c:70
static char * local_addr
Local address to use for connection reversal request.
Definition: gnunet-nat.c:65
static int use_tcp
Flag set to 1 if we use IPPROTO_TCP.
Definition: gnunet-nat.c:55
static uint8_t proto
Protocol to use.
Definition: gnunet-nat.c:60
static void test_finished()
Test if all activities have finished, and if so, terminate.
Definition: gnunet-nat.c:98
static int do_stun
Should we actually bind to #bind_addr and receive and process STUN requests?
Definition: gnunet-nat.c:75
static void address_cb(void *cls, void **app_ctx, int add_remove, enum GNUNET_NAT_AddressClass ac, const struct sockaddr *addr, socklen_t addrlen)
Signature of the callback passed to GNUNET_NAT_register() for a function to call whenever our set of ...
Definition: gnunet-nat.c:122
@ GNUNET_OK
@ GNUNET_NO
@ GNUNET_SYSERR
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
@ GNUNET_ERROR_TYPE_ERROR
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
#define GNUNET_free(ptr)
Wrapper around free.
int GNUNET_NAT_request_reversal(struct GNUNET_NAT_Handle *nh, const struct sockaddr_in *local_sa, const struct sockaddr_in *remote_sa)
We learned about a peer (possibly behind NAT) so run the gnunet-nat-client to send dummy ICMP respons...
Definition: nat_api.c:675
struct GNUNET_NAT_Handle * GNUNET_NAT_register(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *config_section, uint8_t proto, unsigned int num_addrs, const struct sockaddr **addrs, const socklen_t *addrlens, GNUNET_NAT_AddressCallback address_callback, GNUNET_NAT_ReversalCallback reversal_callback, void *callback_cls)
Attempt to enable port redirection and detect public IP address contacting UPnP or NAT-PMP routers on...
Definition: nat_api.c:366
struct GNUNET_NETWORK_Handle * GNUNET_NETWORK_socket_create(int domain, int type, int protocol)
Create a new socket.
Definition: network.c:833
enum GNUNET_GenericReturnValue GNUNET_NETWORK_socket_bind(struct GNUNET_NETWORK_Handle *desc, const struct sockaddr *address, socklen_t address_len)
Bind a socket to a particular address.
Definition: network.c:439
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_shutdown(GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run on shutdown, that is when a CTRL-C signal is received,...
Definition: scheduler.c:1339
size_t GNUNET_STRINGS_parse_socket_addr(const char *addr, uint8_t *af, struct sockaddr **sa)
Parse an address given as a string into a struct sockaddr.
Definition: strings.c:1179

References address_cb(), do_shutdown(), do_stun, global_ret, GNUNET_a2s(), GNUNET_assert, GNUNET_ERROR_TYPE_ERROR, GNUNET_ERROR_TYPE_MESSAGE, GNUNET_free, GNUNET_log, GNUNET_NAT_register(), GNUNET_NAT_request_reversal(), GNUNET_NETWORK_socket_bind(), GNUNET_NETWORK_socket_create(), GNUNET_NO, GNUNET_OK, GNUNET_SCHEDULER_add_read_net(), GNUNET_SCHEDULER_add_shutdown(), GNUNET_SCHEDULER_shutdown(), GNUNET_strdup, GNUNET_STRINGS_parse_socket_addr(), GNUNET_SYSERR, GNUNET_TIME_UNIT_FOREVER_REL, listen_reversal, local_addr, ls, nh, proto, remote_addr, ret, reversal_cb(), rtask, section_name, stun_read_task(), test_finished(), use_tcp, and use_udp.

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ main()

int main ( int  argc,
char *const  argv[] 
)

Main function of gnunet-nat.

Parameters
argcnumber of command-line arguments
argvcommand line
Returns
0 on success, -1 on error

Definition at line 415 of file gnunet-nat.c.

416{
419 'i',
420 "in",
421 "ADDRESS",
422 gettext_noop ("which IP and port are we locally using to bind/listen to"),
423 &local_addr),
424
426 'r',
427 "remote",
428 "ADDRESS",
430 "which remote IP and port should be asked for connection reversal"),
431 &remote_addr),
432
434 'S',
435 "section",
436 NULL,
438 "name of configuration section to find additional options, such as manual host punching data"),
439 &section_name),
440
442 "stun",
443 gettext_noop ("enable STUN processing"),
444 &do_stun),
445
446 GNUNET_GETOPT_option_flag ('t', "tcp", gettext_noop ("use TCP"), &use_tcp),
447
448 GNUNET_GETOPT_option_flag ('u', "udp", gettext_noop ("use UDP"), &use_udp),
449
451 "watch",
453 "watch for connection reversal requests"),
456 };
457
458 if (GNUNET_OK !=
460 argc,
461 argv,
462 "gnunet-nat [options]",
463 _ ("GNUnet NAT traversal autoconfigure daemon"),
464 options,
465 &run,
466 NULL))
467 {
468 global_ret = 1;
469 }
470 return global_ret;
471}
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_OPTION_END
Definition: 002.c:13
struct GNUNET_GETOPT_CommandLineOption options[]
Definition: 002.c:5
#define gettext_noop(String)
Definition: gettext.h:74
static void run(void *cls, char *const *args, const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *c)
Main function that will be run.
Definition: gnunet-nat.c:242
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_flag(char shortName, const char *name, const char *description, int *val)
Allow user to specify a flag (which internally means setting an integer to 1/GNUNET_YES/GNUNET_OK.
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_string(char shortName, const char *name, const char *argumentHelp, const char *description, char **str)
Allow user to specify a string.
const struct GNUNET_OS_ProjectData * GNUNET_OS_project_data_gnunet(void)
Return default project data used by 'libgnunetutil' for GNUnet.
enum GNUNET_GenericReturnValue GNUNET_PROGRAM_run(const struct GNUNET_OS_ProjectData *pd, int argc, char *const *argv, const char *binaryName, const char *binaryHelp, const struct GNUNET_GETOPT_CommandLineOption *options, GNUNET_PROGRAM_Main task, void *task_cls)
Run a standard GNUnet command startup sequence (initialize loggers and configuration,...
Definition: program.c:407
#define _(String)
GNU gettext support macro.
Definition: platform.h:178
Definition of a command line option.

References _, do_stun, gettext_noop, global_ret, GNUNET_GETOPT_OPTION_END, GNUNET_GETOPT_option_flag(), GNUNET_GETOPT_option_string(), GNUNET_OK, GNUNET_OS_project_data_gnunet(), GNUNET_PROGRAM_run(), listen_reversal, local_addr, options, remote_addr, run(), section_name, use_tcp, and use_udp.

Here is the call graph for this function:

Variable Documentation

◆ global_ret

int global_ret
static

Value to return from main().

Definition at line 34 of file gnunet-nat.c.

Referenced by main(), run(), and stun_read_task().

◆ section_name

char* section_name
static

Name of section in configuration file to use for additional options.

Definition at line 40 of file gnunet-nat.c.

Referenced by main(), and run().

◆ use_udp

int use_udp
static

Flag set to 1 if we use IPPROTO_UDP.

Definition at line 45 of file gnunet-nat.c.

Referenced by main(), and run().

◆ listen_reversal

int listen_reversal
static

Flag set to 1 if we are to listen for connection reversal requests.

Definition at line 50 of file gnunet-nat.c.

Referenced by main(), and run().

◆ use_tcp

int use_tcp
static

Flag set to 1 if we use IPPROTO_TCP.

Definition at line 55 of file gnunet-nat.c.

Referenced by main(), and run().

◆ proto

uint8_t proto
static

Protocol to use.

Definition at line 60 of file gnunet-nat.c.

Referenced by run().

◆ local_addr

char* local_addr
static

Local address to use for connection reversal request.

Definition at line 65 of file gnunet-nat.c.

Referenced by client_quic_init(), connection_feed_data(), connection_init(), connection_on_read(), create_conn(), main(), mq_init(), run(), server_read_pkt(), and sock_read().

◆ remote_addr

char* remote_addr
static

Remote address to use for connection reversal request.

Definition at line 70 of file gnunet-nat.c.

Referenced by client_quic_init(), connection_feed_data(), connection_init(), connection_on_read(), main(), mq_init(), run(), and server_read_pkt().

◆ do_stun

int do_stun
static

Should we actually bind to #bind_addr and receive and process STUN requests?

Definition at line 75 of file gnunet-nat.c.

Referenced by main(), and run().

◆ nh

struct GNUNET_NAT_Handle* nh
static

Handle to NAT operation.

Definition at line 80 of file gnunet-nat.c.

Referenced by do_shutdown(), run(), stun_read_task(), and test_finished().

◆ ls

struct GNUNET_NETWORK_Handle* ls
static

Listen socket for STUN processing.

Definition at line 85 of file gnunet-nat.c.

Referenced by bind_v4(), bind_v6(), do_shutdown(), new_service_channel(), run(), setup_service(), start_process(), and stun_read_task().

◆ rtask

struct GNUNET_SCHEDULER_Task* rtask
static

Task for reading STUN packets.

Definition at line 90 of file gnunet-nat.c.

Referenced by do_shutdown(), run(), stun_read_task(), and test_finished().