diff -aur nzbget-0.2.3/Connection.cpp nzbget-0.2.3.ipv6/Connection.cpp --- nzbget-0.2.3/Connection.cpp 2005-09-02 09:00:41.000000000 +0200 +++ nzbget-0.2.3.ipv6/Connection.cpp 2008-02-01 15:16:52.000000000 +0100 @@ -255,37 +255,42 @@ m_iBufferLength = 0; m_iBufferPos = 0; - struct hostent *hinfo; - - hinfo = ::gethostbyname( m_pNetAddress->GetHost() ); - - if( !hinfo ) + int res; + + struct addrinfo addr_hints, *addr_list, *addr; + char iPortStr[sizeof(int) * 4 + 1]; //is enough to hold any converted int + + memset(&addr_hints, 0, sizeof(addr_hints)); + addr_hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */ + addr_hints.ai_socktype = SOCK_STREAM, + + sprintf(iPortStr, "%d", m_pNetAddress->GetPort()); + + res = ::getaddrinfo( m_pNetAddress->GetHost(), iPortStr, &addr_hints, &addr_list ); + + if( res != 0 ) { error( "Could not resolve hostname %s", m_pNetAddress->GetHost() ); return -1; } - memset(&m_SocketAddress, '\0', sizeof(m_SocketAddress)); - m_SocketAddress.sin_family = AF_INET; - memcpy(&m_SocketAddress.sin_addr.s_addr, hinfo->h_addr_list[0], sizeof(m_SocketAddress.sin_addr.s_addr)); - m_SocketAddress.sin_port = htons( m_pNetAddress->GetPort() ); - - m_iSocket = ::socket(PF_INET,SOCK_STREAM,0); - - if( m_iSocket < 0 ) - { - error("Socket creation failed for %s!", m_pNetAddress->GetHost() ); - return -1; - } - - int res = ::connect( m_iSocket ,(struct sockaddr *) &m_SocketAddress, sizeof(m_SocketAddress) ); - - if (res < 0) + for (addr = addr_list; addr != NULL; addr = addr->ai_next) + { + m_iSocket = ::socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol); + if (m_iSocket == -1) continue; + + res = ::connect( m_iSocket , addr->ai_addr, addr->ai_addrlen ); + if (res != -1) break; /* Connection established */ + } + + if( addr == NULL ) { - error("Connection to %s failed!", m_pNetAddress->GetHost() ); + error("Socket creation or connection failed for %s!", m_pNetAddress->GetHost() ); return -1; } + freeaddrinfo( addr_list ); /* No longer needed */ + m_pFileBuffer = fdopen( m_iSocket, "r+" ); if( !m_pFileBuffer ) @@ -339,34 +344,42 @@ { debug("Connection::DoBind()"); - struct hostent *hinfo; - - hinfo = ::gethostbyname( m_pNetAddress->GetHost() ); - - if( !hinfo ) + int res; + + struct addrinfo addr_hints, *addr_list, *addr; + char iPortStr[sizeof(int) * 4 + 1]; //is enough to hold any converted int + + memset(&addr_hints, 0, sizeof(addr_hints)); + addr_hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */ + addr_hints.ai_socktype = SOCK_STREAM, + addr_hints.ai_flags = AI_PASSIVE; /* For wildcard IP address */ + + sprintf(iPortStr, "%d", m_pNetAddress->GetPort()); + + res = ::getaddrinfo( m_pNetAddress->GetHost(), iPortStr, &addr_hints, &addr_list ); + + if( res != 0 ) { error( "Could not resolve hostname %s", m_pNetAddress->GetHost() ); return -1; } - m_iSocket = ::socket( PF_INET, SOCK_STREAM, 0 ); - - if( m_iSocket < 0 ) + for (addr = addr_list; addr != NULL; addr = addr->ai_next) + { + m_iSocket = ::socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol); + if (m_iSocket == -1) continue; + + res = ::bind( m_iSocket , addr->ai_addr, addr->ai_addrlen ); + if (res != -1) break; /* Connection established */ + } + + if( addr == NULL ) { - error("Socket creation failed for %s!", m_pNetAddress->GetHost() ); + error("Socket creation or binding failed for %s!", m_pNetAddress->GetHost() ); return -1; } - memset(&m_SocketAddress, '\0', sizeof(m_SocketAddress)); - m_SocketAddress.sin_family = AF_INET; - m_SocketAddress.sin_addr.s_addr = htonl(INADDR_ANY); - m_SocketAddress.sin_port = htons( m_pNetAddress->GetPort() ); - - if( bind( m_iSocket, (struct sockaddr *) &m_SocketAddress, sizeof(m_SocketAddress)) < 0) - { - error( "binding socket failed for %s!", m_pNetAddress->GetHost() ); - return -1; - } + freeaddrinfo( addr_list ); /* No longer needed */ if( listen( m_iSocket, 10 ) < 0 ) { diff -aur nzbget-0.2.3/Connection.h nzbget-0.2.3.ipv6/Connection.h --- nzbget-0.2.3/Connection.h 2005-09-02 09:00:41.000000000 +0200 +++ nzbget-0.2.3.ipv6/Connection.h 2008-02-01 15:18:09.000000000 +0100 @@ -42,7 +42,6 @@ class Connection { protected: - struct sockaddr_in m_SocketAddress; NetAddress* m_pNetAddress; int m_iSocket; int m_iStatus; @@ -67,7 +66,6 @@ int Recv( int iSocket, char* pBuffer, int iSize ); NetAddress* GetServer() { return m_pNetAddress; } int GetSocket() { return m_iSocket; } - struct sockaddr_in* GetSocketAddressStruct() { return &m_SocketAddress; } const char* GetReadBuffer() const { return m_pReadBuffer; } int SendBuffer( void* pData, int iLength ); int SendBuffer( int iSocket, void* pData, int iLength );