From: Mike Brady <4265913+mikebrady@users.noreply.github.com> Date: Sun, 2 Apr 2023 14:28:31 +0000 (+0100) Subject: Change open_sockets_at_port to be satisfied with just one port on IPv4 or IPv6. Fix... X-Git-Tag: 1.2~1^2~8 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f325a3c9ac38b4f0bc659ec464cf5f129b562f50;p=thirdparty%2Fnqptp.git Change open_sockets_at_port to be satisfied with just one port on IPv4 or IPv6. Fix misleading message. --- diff --git a/nqptp-utilities.c b/nqptp-utilities.c index 8940868..9d6a95d 100644 --- a/nqptp-utilities.c +++ b/nqptp-utilities.c @@ -43,12 +43,17 @@ #include "debug.h" -void open_sockets_at_port(const char *node, uint16_t port, sockets_open_bundle *sockets_open_stuff) { +void open_sockets_at_port(const char *node, uint16_t port, + sockets_open_bundle *sockets_open_stuff) { // open up sockets for UDP ports 319 and 320 + // will try IPv6 and IPv4 if IPV6_V6ONLY is not defined + struct addrinfo hints, *info, *p; int ret; + int sockets_opened = 0; + memset(&hints, 0, sizeof(hints)); hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_DGRAM; @@ -85,26 +90,31 @@ void open_sockets_at_port(const char *node, uint16_t port, sockets_open_bundle * fcntl(fd, F_SETFL, flags | O_NONBLOCK); // one of the address families will fail on some systems that - // report its availability. do not complain. - - if (ret) { - die("unable to listen on %s port %d. The error is: \"%s\". Daemon must run as root. Or is " - "a " - "separate PTP daemon running?", - p->ai_family == AF_INET6 ? "IPv6" : "IPv4", port, strerror(errno)); - } else { + // report its availability. Do not complain. + if (ret == 0) { // debug(1, "socket %d is listening on %s port %d.", fd, // p->ai_family == AF_INET6 ? "IPv6" : "IPv4", port); sockets_open_stuff->sockets[sockets_open_stuff->sockets_open].number = fd; sockets_open_stuff->sockets[sockets_open_stuff->sockets_open].port = port; sockets_open_stuff->sockets[sockets_open_stuff->sockets_open].family = p->ai_family; sockets_open_stuff->sockets_open++; + sockets_opened++; } } } - freeaddrinfo(info); + if (sockets_opened == 0) { + if (port < 1024) + die("unable to listen on port %d. The error is: \"%s\". NQPTP must run as root to access " + "this port. Or is another PTP daemon -- possibly another instance on NQPTP -- running " + "already?", + port, strerror(errno)); + else + die("unable to listen on port %d. The error is: \"%s\". " + "Is another instance on NQPTP running already?", + port, strerror(errno)); + } } void debug_print_buffer(int level, char *buf, size_t buf_len) {