From e5564514f643fecce0d7ba6b4204d1556fe0f115 Mon Sep 17 00:00:00 2001
From: Chris Tallon <chris@vomp.tv>
Date: Fri, 17 Sep 2021 15:16:57 +0100
Subject: [PATCH] Support IPv6 in InputUDP

---
 inputudp.cc | 14 +++++++-------
 inputudp.h  |  4 ++--
 udp6.cc     | 19 +++++++------------
 udp6.h      |  2 +-
 4 files changed, 17 insertions(+), 22 deletions(-)

diff --git a/inputudp.cc b/inputudp.cc
index b876512..b8750dc 100644
--- a/inputudp.cc
+++ b/inputudp.cc
@@ -41,9 +41,9 @@ bool InputUDP::init()
 
   log->debug(TAG, "Starting InputUDP command server on port {}", port);
 
-  if (!udp4.init(static_cast<USHORT>(port)))
+  if (!udp6.init(static_cast<USHORT>(port)))
   {
-    log->debug(TAG, "UDP4 init error");
+    log->debug(TAG, "UDP6 init error");
     initted = false;
     return false;
   }
@@ -58,7 +58,7 @@ bool InputUDP::init()
   {
     log->error(TAG, "pipe2() fail");
 #endif
-    udp4.shutdown();
+    udp6.shutdown();
     initted = false;
     return false;
   }
@@ -72,7 +72,7 @@ void InputUDP::shutdown()
   CLOSESOCKET(quitPipe);
 #endif
 
-  udp4.shutdown();
+  udp6.shutdown();
 
 #ifndef WIN32
   CLOSESOCKET(pfds[1]);
@@ -122,15 +122,15 @@ void InputUDP::listenLoop()
   while(1)
   {
 #ifdef WIN32
-    retval = udp4.waitforMessage(3, quitPipe);
+    retval = udp6.waitforMessage(3, quitPipe);
 #else
-    retval = udp4.waitforMessage(3, pfds[0]);
+    retval = udp6.waitforMessage(3, pfds[0]);
 #endif
     log->debug(TAG, "Back from waitForMessage");
 
     if (retval == 2)
     {
-      processRequest(udp4.getData(), udp4.getDataLength());
+      processRequest(udp6.getData(), udp6.getDataLength());
     }
     else if (retval == 3) // quit
     {
diff --git a/inputudp.h b/inputudp.h
index f875290..3d866b3 100644
--- a/inputudp.h
+++ b/inputudp.h
@@ -29,7 +29,7 @@
 #endif
 
 #include "defines.h"
-#include "udp4.h"
+#include "udp6.h"
 #include "input.h"
 
 class LogNT;
@@ -54,7 +54,7 @@ class InputUDP : public Input
     const char* modName() { return myModName; }
 
     bool initted{};
-    UDP4 udp4; // FIXME UDP6 ?
+    UDP6 udp6;
     LogNT* log{};
 
     std::thread listenThread;
diff --git a/udp6.cc b/udp6.cc
index 1edc895..4d665fc 100644
--- a/udp6.cc
+++ b/udp6.cc
@@ -58,24 +58,19 @@ int UDP6::init(USHORT tPort)
   if ((socketnum = socket(AF_INET6, SOCK_DGRAM, 0)) == -1)
   { perror("socket"); return 0; }
 
-  // FIXME - implement server side
-
-  /*
-
   memset(&myAddr, 0, sizeof(myAddr));
   myAddr.sin6_family = AF_INET6;        // host byte order
   myAddr.sin6_port = htons(myPort);     // short, network byte order
+  myAddr.sin6_addr = in6addr_any;
 
-//  myAddr.sin_addr.s_addr = getIPNumber(iterate_ip++); // auto-fill with my IP
-
-  inet_pton(AF_INET6, "", &myAddr.sin6_addr);
+  /*
+   * FIXME This _might_ need porting to Windows
+    myAddr.sin_addr.s_addr = getIPNumber(iterate_ip++); // auto-fill with my IP
+    inet_pton(AF_INET6, "", &myAddr.sin6_addr);
+  */
 
-/ *
   if (bind(socketnum, reinterpret_cast<struct sockaddr *>(&myAddr), addrlen) == -1)
-  { perror("bind"); return 0; }
-* /
-
-*/
+  { perror("bind6"); return 0; }
 
   FD_ZERO(&readfds);
   FD_SET(socketnum, &readfds);
diff --git a/udp6.h b/udp6.h
index 9caad45..413a2a2 100644
--- a/udp6.h
+++ b/udp6.h
@@ -53,7 +53,7 @@ class UDP6
     ULONG iterate_ip{};
     int socketnum;                  // Socket descriptor
     USHORT myPort{};                // My port number
-//    struct sockaddr_in6 myAddr;     // My address
+    struct sockaddr_in6 myAddr;     // My address
     struct sockaddr_in6 theirAddr;  // User address
     socklen_t addrlen;              // length of sockaddr struct
     char buf[MAXBUFLEN];            // main data buffer
-- 
2.39.5