Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only use IPv6 in the webserver when available #1725

Merged
merged 1 commit into from
Nov 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 24 additions & 11 deletions src/config/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -1364,17 +1364,30 @@ void readFTLconf(struct config *conf, const bool rewrite)
const in_port_t https_port = port_in_use(443) ? 8443 : 443;

// Create a string with the default ports
if(http_port == 80 && https_port == 443)
conf->webserver.port.v.s = (char*)"80,[::]:80,443s,[::]:443s";
else if(http_port == 8080 && https_port == 443)
conf->webserver.port.v.s = (char*)"8080,[::]:8080,443s,[::]:443s";
else if(http_port == 80 && https_port == 8443)
conf->webserver.port.v.s = (char*)"80,[::]:80,8443s,[::]:8443s";
else
conf->webserver.port.v.s = (char*)"8080,[::]:8080,8443s,[::]:8443s";

log_info("Initialised webserver ports at %d (HTTP) and %d (HTTPS)",
http_port, https_port);
// Allocate memory for the string
char *ports = calloc(32, sizeof(char));
if(ports == NULL)
{
log_err("Unable to allocate memory for default ports string");
return;
}
// Create the string
snprintf(ports, 32, "%d,%ds", http_port, https_port);

// Append IPv6 ports if IPv6 is enabled
const bool have_ipv6 = ipv6_enabled();
if(have_ipv6)
snprintf(ports + strlen(ports), 32 - strlen(ports),
",[::]:%d,[::]:%ds", http_port, https_port);

// Set default values for webserver ports
if(conf->webserver.port.t == CONF_STRING_ALLOCATED)
free(conf->webserver.port.v.s);
conf->webserver.port.v.s = ports;
conf->webserver.port.t = CONF_STRING_ALLOCATED;

log_info("Initialised webserver ports at %d (HTTP) and %d (HTTPS), IPv6 support is %s",
http_port, https_port, have_ipv6 ? "enabled" : "disabled");

// Initialize the TOML config file
writeFTLtoml(true);
Expand Down
48 changes: 48 additions & 0 deletions src/daemon.c
Original file line number Diff line number Diff line change
Expand Up @@ -396,3 +396,51 @@ ssize_t getrandom_fallback(void *buf, size_t buflen, unsigned int flags)

return buflen;
}

bool ipv6_enabled(void)
{
// First we check a few virtual system files to see if IPv6 is disabled
const char *files[] = {
"/sys/module/ipv6/parameters/disable", // GRUB - ipv6.disable=1
"/proc/sys/net/ipv6/conf/all/disable_ipv6", // sysctl.conf - net.ipv6.conf.all.disable_ipv6=1
"/proc/sys/net/ipv6/conf/default/disable_ipv6", // sysctl.conf - net.ipv6.conf.all.disable_ipv6=1
NULL
};

// Loop over the files
for(int i = 0; files[i] != NULL; i++)
{
// Open file for reading
FILE *f = fopen(files[i], "r");
if(f == NULL)
continue;

// Read first character
const int c = fgetc(f);
fclose(f);
// If the first character is a 1, then IPv6 is disabled
if(c == '1')
return false;
}

// If the file does not exist or if it does not contain a 1, then we check
// if /proc/net/if_inet6 has any IPv6-capable interfaces
// Since Linux 2.6.25 (April 2008), files in /proc/net are a symlink to
// /proc/self/net and provide information about the network devices and
// interfaces for the network namespace of which the process is a member
FILE *f = fopen("/proc/net/if_inet6", "r");

if(f != NULL)
{
// If the file exists, we check if it is empty
const int c = fgetc(f);
fclose(f);
// If the file is empty, then there are no IPv6-capable interfaces
if(c == EOF)
return false;
}

// else: IPv6 is not obviously disabled and there is at least one
// IPv6-capable interface
return true;
}
1 change: 1 addition & 0 deletions src/daemon.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ void cleanup(const int ret);
void set_nice(void);
void calc_cpu_usage(void);
float get_cpu_percentage(void) __attribute__((pure));
bool ipv6_enabled(void);

#include <sys/syscall.h>
#include <unistd.h>
Expand Down