]> git.vomp.tv Git - vompclient.git/commitdiff
IPv6 support for main protocol
authorChris Tallon <chris@vomp.tv>
Wed, 9 Oct 2019 15:39:44 +0000 (16:39 +0100)
committerChris Tallon <chris@vomp.tv>
Wed, 9 Oct 2019 15:39:44 +0000 (16:39 +0100)
tcp.cc

diff --git a/tcp.cc b/tcp.cc
index 91e9049d00b530f2ee05e089323b7a1bc9944dbd..43f8e131c9e29e9b213c7e026ce3c05010260f62 100644 (file)
--- a/tcp.cc
+++ b/tcp.cc
@@ -1,5 +1,5 @@
 /*
-    Copyright 2004-2005 Chris Tallon
+    Copyright 2004-2019 Chris Tallon
     Copyright 2003-2004 University Of Bradford
 
     This file is part of VOMP.
     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
 
-#include "tcp.h"
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netdb.h>
+
 #ifdef WIN32
 #include <Iphlpapi.h>
 #endif
 
 #include "log.h"
 
+#include "tcp.h"
+
 #ifndef WIN32
 #define MUTEX_LOCK(mutex) pthread_mutex_lock(mutex)
 #define MUTEX_UNLOCK(mutex) pthread_mutex_unlock(mutex)
@@ -109,9 +114,13 @@ void TCP::getMAC(char* dest)
 
 int TCP::connectTo(char* host, unsigned short port)
 {
+#define IPV 6
+
+
+#if IPV == 4
+
   sock = socket(PF_INET, SOCK_STREAM, 0);
   if (sock == -1) return 0;
-
   struct sockaddr_in dest_addr;
   dest_addr.sin_family = AF_INET;
   dest_addr.sin_port = htons(port);
@@ -129,6 +138,28 @@ int TCP::connectTo(char* host, unsigned short port)
 
   memset(&(dest_addr.sin_zero), '\0', 8);
 
+#elif IPV == 6
+
+  char portstring[10];
+  snprintf(portstring, 10, "%u", port);
+
+  struct addrinfo hints;
+  struct addrinfo* res;
+  memset(&hints, 0, sizeof(hints));
+  hints.ai_family = AF_UNSPEC;
+  hints.ai_socktype = SOCK_STREAM;
+  if (getaddrinfo(host, portstring, &hints, &res))
+  {
+    return 0;
+  }
+
+  sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
+  if (sock == -1) return 0;
+
+#endif
+
+
+
   // set non blocking
 #ifndef WIN32
   int oldflags = fcntl (sock, F_GETFL, 0);
@@ -144,7 +175,13 @@ int TCP::connectTo(char* host, unsigned short port)
 
   // ok, how to open a connection in non blocking mode (and therefore have a self set timeout!!)
 
-  int success = connect(sock, (struct sockaddr *)&dest_addr, sizeof(struct sockaddr));
+#if IPV == 4
+  int success = connect(sock, (struct sockaddr *)&dest_addr, sizeof(dest_addr));
+#elif IPV == 6
+  int success = connect(sock, res->ai_addr, res->ai_addrlen);
+  freeaddrinfo(res);
+#endif
+
 
   if (success == 0)  // if by some miracle the connection succeeded in no time flat, just return!
   {
@@ -162,6 +199,7 @@ int TCP::connectTo(char* host, unsigned short port)
 #endif
   {
     CLOSESOCKET(sock);
+
     return 0;
   }