From 4fbe77baacfb2cb1736ab8db6743c772c291ff93 Mon Sep 17 00:00:00 2001 From: Chris Tallon Date: Wed, 9 Oct 2019 16:39:44 +0100 Subject: [PATCH] IPv6 support for main protocol --- tcp.cc | 46 ++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 42 insertions(+), 4 deletions(-) diff --git a/tcp.cc b/tcp.cc index 91e9049..43f8e13 100644 --- 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. @@ -19,13 +19,18 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -#include "tcp.h" +#include +#include +#include + #ifdef WIN32 #include #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; } -- 2.39.5