From 701eaef980de4f7dbb5c31c4fee9b7e1e266d7a1 Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Sat, 1 Dec 2018 11:36:03 -0500 Subject: [PATCH] Move net.inet.ip.random_id code to lib/net/ --- src/app/config/config.c | 21 +-------------------- src/lib/net/socket.c | 29 +++++++++++++++++++++++++++++ src/lib/net/socket.h | 1 + 3 files changed, 31 insertions(+), 20 deletions(-) diff --git a/src/app/config/config.c b/src/app/config/config.c index d907b07136..50f3793d6c 100644 --- a/src/app/config/config.c +++ b/src/app/config/config.c @@ -157,10 +157,6 @@ #include "core/or/connection_st.h" #include "core/or/port_cfg_st.h" -#ifdef __FreeBSD__ -#include -#endif - #ifdef HAVE_SYSTEMD # if defined(__COVERITY__) && !defined(__INCLUDE_LEVEL__) /* Systemd's use of gcc's __INCLUDE_LEVEL__ extension macro appears to confuse @@ -3386,22 +3382,7 @@ options_validate(or_options_t *old_options, or_options_t *options, if (ContactInfo && !string_is_utf8(ContactInfo, strlen(ContactInfo))) REJECT("ContactInfo config option must be UTF-8."); -#ifdef __FreeBSD__ - if (server_mode(options)) { - int random_id_state; - size_t state_size = sizeof(random_id_state); - - if (sysctlbyname("net.inet.ip.random_id", &random_id_state, - &state_size, NULL, 0)) { - log_warn(LD_CONFIG, - "Failed to figure out if IP ids are randomized."); - } else if (random_id_state == 0) { - log_warn(LD_CONFIG, "Looks like IP ids are not randomized. " - "Please consider setting the net.inet.ip.random_id sysctl, " - "so your relay makes it harder to figure out how busy it is."); - } - } -#endif + check_network_configuration(server_mode(options)); /* Special case on first boot if no Log options are given. */ if (!options->Logs && !options->RunAsDaemon && !from_setconf) { diff --git a/src/lib/net/socket.c b/src/lib/net/socket.c index cd7c9685cd..8940e00591 100644 --- a/src/lib/net/socket.c +++ b/src/lib/net/socket.c @@ -31,6 +31,9 @@ #endif #include #include +#ifdef __FreeBSD__ +#include +#endif /** Called before we make any calls to network-related functions. * (Some operating systems require their network libraries to be @@ -60,6 +63,32 @@ network_init(void) return 0; } +/** + * Warn the user if any system network parameters should be changed. + */ +void +check_network_configuration(bool server_mode) +{ +#ifdef __FreeBSD__ + if (server_mode) { + int random_id_state; + size_t state_size = sizeof(random_id_state); + + if (sysctlbyname("net.inet.ip.random_id", &random_id_state, + &state_size, NULL, 0)) { + log_warn(LD_CONFIG, + "Failed to figure out if IP ids are randomized."); + } else if (random_id_state == 0) { + log_warn(LD_CONFIG, "Looks like IP ids are not randomized. " + "Please consider setting the net.inet.ip.random_id sysctl, " + "so your relay makes it harder to figure out how busy it is."); + } + } +#else + (void) server_mode; +#endif +} + /* When set_max_file_sockets() is called, update this with the max file * descriptor value so we can use it to check the limit when opening a new * socket. Default value is what Debian sets as the default hard limit. */ diff --git a/src/lib/net/socket.h b/src/lib/net/socket.h index 2b87441fc6..822b9975e6 100644 --- a/src/lib/net/socket.h +++ b/src/lib/net/socket.h @@ -54,6 +54,7 @@ int tor_addr_from_getsockname(struct tor_addr_t *addr_out, tor_socket_t sock); int set_socket_nonblocking(tor_socket_t socket); int tor_socketpair(int family, int type, int protocol, tor_socket_t fd[2]); int network_init(void); +void check_network_configuration(bool server_mode); int get_max_sockets(void); void set_max_sockets(int);