From 6463e8edc8e24bc10beeffa6ad6aea6d03c1efa1 Mon Sep 17 00:00:00 2001 From: Steffen Pohle Date: Sat, 24 Oct 2026 23:34:06 +0200 Subject: [PATCH] fixing issues on linux system with no IPV6 support --- Changelog | 3 +++ tcp.cc | 36 ++++++++++++++++++++++++++++++++---- 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/Changelog b/Changelog index 47e4b7b..e4eed11 100644 --- a/Changelog +++ b/Changelog @@ -1,3 +1,6 @@ +2026-10-24: +- tcp.cc: on hosts with disabled ipv6, we could not listen on IPV4 only + 2026-08-04: - JSON handling with arrays had serval issues. added test-json file to test also nested arrays. diff --git a/tcp.cc b/tcp.cc index 2e19513..d7b8719 100644 --- a/tcp.cc +++ b/tcp.cc @@ -84,27 +84,55 @@ int TCP::Listen(int port) { #endif hints.ai_socktype = SOCK_STREAM; if ((err = getaddrinfo (NULL, itoa(buffer, port, 32), &hints, &res)) != 0) { - return 0; - } + fprintf (stderr, "getaddrinfo: %s\n", gai_strerror(err)); + return 0; + } // // walk through all results until we could connect // for (rp = res; rp != NULL; rp = rp->ai_next) { sock = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol); - if (sock == -1) continue; - + if (sock == -1) { + printf ("%s:%d socket error: %s\n", __FILE__, __LINE__, strerror(errno)); + // + // in case of an unsupported address family, we will check only for IPV4 + // + if (errno == EAFNOSUPPORT) { + printf ("%s:%d try with IPV4 only\n", __FILE__, __LINE__); + hints.ai_family = AF_INET; + hints.ai_socktype = SOCK_STREAM; + hints.ai_flags = AI_PASSIVE; + freeaddrinfo (res); + if ((err = getaddrinfo (NULL, itoa(buffer, port, 32), &hints, &res)) != 0) { + fprintf (stderr, "getaddrinfo: %s\n", gai_strerror(err)); + return 0; + } + rp = res; + + if (res != NULL) { + sock = socket (AF_INET, SOCK_STREAM, 0); + if (sock == -1) { + printf ("%s:%d socket error: %s\n", __FILE__, __LINE__, strerror(errno)); + continue; + } + } + } + else continue; + } i = 1; if ((err = setsockopt (sock, SOL_SOCKET, SO_REUSEADDR, (char*)&i, sizeof (i))) != 0) { printf ("%s:%d setsockopt error\n", __FILE__, __LINE__); } if ((err = bind (sock, rp->ai_addr, rp->ai_addrlen)) < 0) { + printf ("%s:%d bind error: %s\n", __FILE__, __LINE__, strerror(errno)); close (sock); sock = -1; continue; } if (listen (sock, 8) < 0) { // maximum of 8 connections at the time + printf ("%s:%d listen error: %s\n", __FILE__, __LINE__, strerror(errno)); close (sock); sock = -1; continue;