|
From: <gb...@us...> - 2008-07-14 02:00:40
|
Revision: 290
http://gearbox.svn.sourceforge.net/gearbox/?rev=290&view=rev
Author: gbiggs
Date: 2008-07-13 19:00:48 -0700 (Sun, 13 Jul 2008)
Log Message:
-----------
Replaced deprecated gethostbyname function with getaddrinfo, defined HOST_NAME_MAX on systems that lack it.
Modified Paths:
--------------
gearbox/trunk/src/flexiport/CMakeLists.txt
gearbox/trunk/src/flexiport/flexiport_config.h.in
gearbox/trunk/src/flexiport/tcpport.cpp
Modified: gearbox/trunk/src/flexiport/CMakeLists.txt
===================================================================
--- gearbox/trunk/src/flexiport/CMakeLists.txt 2008-07-12 07:25:25 UTC (rev 289)
+++ gearbox/trunk/src/flexiport/CMakeLists.txt 2008-07-14 02:00:48 UTC (rev 290)
@@ -12,6 +12,12 @@
OPTION (FLEXIPORT_INCLUDE_LOGGING "Include the log reader/writer ports in FlexiPort" ON)
MARK_AS_ADVANCED (FLEXIPORT_INCLUDE_SERIAL FLEXIPORT_INCLUDE_TCP)
+ IF (GBX_OS_QNX)
+ SET (CMAKE_REQUIRED_LIBRARIES socket)
+ ENDIF (GBX_OS_QNX)
+ CHECK_FUNCTION_EXISTS (getaddrinfo FLEXIPORT_HAVE_GETADDRINFO)
+ SET (CMAKE_REQUIRED_LIBRARIES)
+
SET (flexiport_config_h_in ${CMAKE_CURRENT_SOURCE_DIR}/flexiport_config.h.in)
SET (flexiport_config_h ${CMAKE_CURRENT_BINARY_DIR}/flexiport_config.h)
CONFIGURE_FILE (${flexiport_config_h_in} ${flexiport_config_h})
Modified: gearbox/trunk/src/flexiport/flexiport_config.h.in
===================================================================
--- gearbox/trunk/src/flexiport/flexiport_config.h.in 2008-07-12 07:25:25 UTC (rev 289)
+++ gearbox/trunk/src/flexiport/flexiport_config.h.in 2008-07-14 02:00:48 UTC (rev 290)
@@ -2,4 +2,5 @@
#cmakedefine FLEXIPORT_INCLUDE_SERIAL 1
#cmakedefine FLEXIPORT_INCLUDE_TCP 1
-#cmakedefine FLEXIPORT_INCLUDE_LOGGING 1
\ No newline at end of file
+#cmakedefine FLEXIPORT_INCLUDE_LOGGING 1
+#cmakedefine FLEXIPORT_HAVE_GETADDRINFO 1
\ No newline at end of file
Modified: gearbox/trunk/src/flexiport/tcpport.cpp
===================================================================
--- gearbox/trunk/src/flexiport/tcpport.cpp 2008-07-12 07:25:25 UTC (rev 289)
+++ gearbox/trunk/src/flexiport/tcpport.cpp 2008-07-14 02:00:48 UTC (rev 290)
@@ -27,7 +27,12 @@
#include "flexiport.h"
#include "tcpport.h"
+#include "flexiport_config.h"
+#if defined (FLEXIPORT_HAVE_GETADDRINFO)
+ #include <sys/socket.h>
+ #include <netdb.h>
+#endif
#include <sys/types.h>
#include <fcntl.h>
#include <string.h>
@@ -39,7 +44,6 @@
#include <winsock2.h>
#include <ws2tcpip.h>
#define __func__ __FUNCTION__
- #define HOST_NAME_MAX 64
#else
#include <unistd.h>
#include <errno.h>
@@ -48,6 +52,10 @@
#include <netdb.h>
#endif
+#if !defined (HOST_NAME_MAX)
+ #define HOST_NAME_MAX 256
+#endif
+
namespace flexiport
{
@@ -546,8 +554,6 @@
// Connect to a remote server: used when not in listen mode
void TCPPort::Connect (void)
{
- struct hostent *hp = NULL;
-
Close (); // To make sure
_sock = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
@@ -565,10 +571,38 @@
sockaddr_in sockAddr;
memset (&sockAddr, 0, sizeof (sockAddr));
+#if defined (FLEXIPORT_HAVE_GETADDRINFO)
+ struct addrinfo *res = NULL, hints;
+ memset (&hints, 0, sizeof (hints));
+ hints.ai_family = AF_INET;
+ hints.ai_socktype = SOCK_STREAM;
+ hints.ai_protocol = IPPROTO_TCP;
+ int errorCode;
+ if ((errorCode = getaddrinfo (_ip.c_str (), NULL, &hints, &res)) != 0)
+ {
+ Close ();
+ stringstream ss;
+#if defined (WIN32)
+ ss << "TCPPort::" << __func__ << "() getaddrinfo() error: (" << ErrNo () << ") " <<
+ StrError (ErrNo ());
+#else
+ ss << "TCPPort::" << __func__ << "() getaddrinfo() error: (" << errorCode << ") " <<
+ gai_strerror (errorCode);
+#endif
+ throw PortException (ss.str ());
+ }
+ memcpy (&sockAddr, res[0].ai_addr, res[0].ai_addrlen);
+ freeaddrinfo (res);
+#else // defined (FLEXIPORT_HAVE_GETADDRINFO)
+ struct hostent *hp = NULL;
if ((hp = gethostbyname (_ip.c_str ())) == NULL)
+ {
+ Close ();
throw PortException (string ("TCPPort::") + __func__ + string (" gethostbyname() error."));
+ }
memcpy (&sockAddr.sin_addr, hp->h_addr, hp->h_length);
sockAddr.sin_family = hp->h_addrtype;
+#endif // defined (FLEXIPORT_HAVE_GETADDRINFO)
sockAddr.sin_port = htons (_port);
if (_debug >= 1)
@@ -628,12 +662,36 @@
else
{
// Listen on the specified interface only
+#if defined (FLEXIPORT_HAVE_GETADDRINFO)
+ struct addrinfo *res = NULL, hints;
+ memset (&hints, 0, sizeof (hints));
+ hints.ai_family = AF_INET;
+ hints.ai_socktype = SOCK_STREAM;
+ hints.ai_protocol = IPPROTO_TCP;
+ int errorCode;
+ if ((errorCode = getaddrinfo (_ip.c_str (), NULL, &hints, &res)) != 0)
+ {
+ Close ();
+ stringstream ss;
+#if defined (WIN32)
+ ss << "TCPPort::" << __func__ << "() getaddrinfo() error: (" << ErrNo () << ") " <<
+ StrError (ErrNo ());
+#else
+ ss << "TCPPort::" << __func__ << "() getaddrinfo() error: (" << errorCode << ") " <<
+ gai_strerror (errorCode);
+#endif
+ throw PortException (ss.str ());
+ }
+ memcpy (&sockAddr, res[0].ai_addr, res[0].ai_addrlen);
+ freeaddrinfo (res);
+#else // defined (FLEXIPORT_HAVE_GETADDRINFO)
if ((hp = gethostbyname (_ip.c_str ())) == NULL)
{
throw PortException (string ("TCPPort::") + __func__ +
string (" gethostbyname() error."));
}
sockAddr.sin_family = hp->h_addrtype;
+#endif // defined (FLEXIPORT_HAVE_GETADDRINFO)
sockAddr.sin_port = htons (_port);
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|