]> git.ipfire.org Git - thirdparty/nqptp.git/commitdiff
Change open_sockets_at_port to be satisfied with just one port on IPv4 or IPv6. Fix...
authorMike Brady <4265913+mikebrady@users.noreply.github.com>
Sun, 2 Apr 2023 14:28:31 +0000 (15:28 +0100)
committerMike Brady <4265913+mikebrady@users.noreply.github.com>
Sun, 2 Apr 2023 14:28:31 +0000 (15:28 +0100)
nqptp-utilities.c

index 89408685e6ab3b37e2870906879383f890bc0d70..9d6a95dfc2a8d3a40897fe549faf8d35d7347bb2 100644 (file)
 
 #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) {