/*
- 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)
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);
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);
// 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!
{
#endif
{
CLOSESOCKET(sock);
+
return 0;
}