summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuke Bratch <luke@bratch.co.uk>2024-07-17 21:32:17 +0100
committerLuke Bratch <luke@bratch.co.uk>2024-07-17 21:32:17 +0100
commit12bba1d2e5c1c3729ad01c316bd730077ff9737c (patch)
treeb077dd2d49eef4663ce8bea7a54c5a56396f71cc
parent49196c8c62274826ddc5e5c3f84e2a457a7ab41a (diff)
Ensure file and socket descriptors are valid in config.c and sockets.c.
-rw-r--r--config.c2
-rw-r--r--sockets.c9
2 files changed, 9 insertions, 2 deletions
diff --git a/config.c b/config.c
index 2ee47f5..d7ad856 100644
--- a/config.c
+++ b/config.c
@@ -288,7 +288,7 @@ int createconfigfile(char *filename) {
// Make sure the parent directory exists
struct stat st = {0};
- if (stat(dir, &st) == -1) {
+ if (dir != NULL && stat(dir, &st) == -1) {
if (mkdir(dir, 0700)) {
printf("Error creating config directory '%s'.\n", dir);
debugprint(DEBUG_CRIT, "Error creating config directory '%s'.\n", dir);
diff --git a/sockets.c b/sockets.c
index 0921e55..0f0eefd 100644
--- a/sockets.c
+++ b/sockets.c
@@ -76,7 +76,7 @@ int createserversocket(char *host, char *port) {
// Create listening socket to listen for bouncer client connections
int createclientsocket(char *listenport) {
- int listener; // listening socket descriptor
+ int listener = -1; // listening socket descriptor
int rv; // return value for getaddrinfo (for error message)
struct addrinfo hints, *ai, *p;
int yes = 1; // for enabling socket options with setsockopt
@@ -119,6 +119,13 @@ int createclientsocket(char *listenport) {
}
}
+ // If both IPv4 and IPv6 failed then give up
+ if (listener < 0) {
+ printf("bouncer-client: failed to create socket, exiting...\n");
+ debugprint(DEBUG_CRIT, "bouncer-client: failed to create socket, exiting...\n");
+ exit(1);
+ }
+
// allow address re-use
setsockopt(listener, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)); // 1 as in non-zero as in enable