From d05d8bbddc505a0117010dd05da4d9b89c0c728f Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Wed, 6 May 2009 07:42:59 +0000 Subject: [PATCH] 2009-05-06 Tatsuhiro Tsujikawa Added static member _protocolFamily to SocketCore. By default, SocketCore uses AF_UNSPEC for getaddrinfo hints to resolve address. Sometime SocketCore::bind() and SocketCore::establishConnection() use difference protocl family and latter cannot connect to former. To avoid this situation, we limit protocol family to AF_INET for unit tests. * src/SocketCore.cc * src/SocketCore.h * test/AllTest.cc --- ChangeLog | 12 ++++++++++++ src/SocketCore.cc | 9 +++++---- src/SocketCore.h | 7 +++++++ test/AllTest.cc | 14 +++++++++++++- 4 files changed, 37 insertions(+), 5 deletions(-) diff --git a/ChangeLog b/ChangeLog index 61e43528..f92c2900 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,15 @@ +2009-05-06 Tatsuhiro Tsujikawa + + Added static member _protocolFamily to SocketCore. By default, + SocketCore uses AF_UNSPEC for getaddrinfo hints to resolve + address. Sometime SocketCore::bind() and + SocketCore::establishConnection() use difference protocl family + and latter cannot connect to former. To avoid this situation, we + limit protocol family to AF_INET for unit tests. + * src/SocketCore.cc + * src/SocketCore.h + * test/AllTest.cc + 2009-05-06 Tatsuhiro Tsujikawa Avoid std::bad_alloc for negative bencode string length. diff --git a/src/SocketCore.cc b/src/SocketCore.cc index e01f5af6..5b45e3ec 100644 --- a/src/SocketCore.cc +++ b/src/SocketCore.cc @@ -49,7 +49,6 @@ #include "DlAbortEx.h" #include "StringFormat.h" #include "Util.h" -#include "LogFactory.h" #include "TimeA2.h" #include "a2functional.h" #ifdef ENABLE_SSL @@ -82,6 +81,8 @@ SocketCore::PollMethod SocketCore::_pollMethod = SocketCore::POLL_METHOD_EPOLL; SocketCore::PollMethod SocketCore::_pollMethod = SocketCore::POLL_METHOD_SELECT; #endif // !HAVE_EPOLL +int SocketCore::_protocolFamily = AF_UNSPEC; + #ifdef ENABLE_SSL SharedHandle SocketCore::_tlsContext; @@ -165,7 +166,7 @@ void SocketCore::bind(uint16_t port) struct addrinfo hints; struct addrinfo* res; memset(&hints, 0, sizeof(hints)); - hints.ai_family = AF_UNSPEC; + hints.ai_family = _protocolFamily; hints.ai_socktype = _sockType; hints.ai_flags = AI_PASSIVE; hints.ai_protocol = 0; @@ -246,7 +247,7 @@ void SocketCore::establishConnection(const std::string& host, uint16_t port) struct addrinfo hints; struct addrinfo* res; memset(&hints, 0, sizeof(hints)); - hints.ai_family = AF_UNSPEC; + hints.ai_family = _protocolFamily; hints.ai_socktype = _sockType; hints.ai_flags = 0; hints.ai_protocol = 0; @@ -976,7 +977,7 @@ ssize_t SocketCore::writeData(const char* data, size_t len, struct addrinfo hints; struct addrinfo* res; memset(&hints, 0, sizeof(hints)); - hints.ai_family = AF_UNSPEC; + hints.ai_family = _protocolFamily; hints.ai_socktype = _sockType; hints.ai_flags = 0; hints.ai_protocol = 0; diff --git a/src/SocketCore.h b/src/SocketCore.h index 1c0c553e..aa46dc71 100644 --- a/src/SocketCore.h +++ b/src/SocketCore.h @@ -90,6 +90,8 @@ private: static PollMethod _pollMethod; + static int _protocolFamily; + bool blocking; int secure; @@ -341,6 +343,11 @@ public: #ifdef ENABLE_SSL static void setTLSContext(const SharedHandle& tlsContext); #endif // ENABLE_SSL + + static void setProtocolFamily(int protocolFamily) + { + _protocolFamily = protocolFamily; + } }; } // namespace aria2 diff --git a/test/AllTest.cc b/test/AllTest.cc index 9fb27e0d..02b1f074 100644 --- a/test/AllTest.cc +++ b/test/AllTest.cc @@ -1,9 +1,14 @@ -#include "Platform.h" +#include "common.h" + #include + #include #include #include +#include "Platform.h" +#include "SocketCore.h" + int main(int argc, char* argv[]) { aria2::Platform platform; @@ -13,6 +18,13 @@ int main(int argc, char* argv[]) { setlocale (LC_MESSAGES, "C"); #endif // ENABLE_NLS + // By default, SocketCore uses AF_UNSPEC for getaddrinfo hints to + // resolve address. Sometime SocketCore::bind() and + // SocketCore::establishConnection() use difference protocl family + // and latter cannot connect to former. To avoid this situation, we + // limit protocol family to AF_INET for unit tests. + aria2::SocketCore::setProtocolFamily(AF_INET); + CppUnit::Test* suite = CppUnit::TestFactoryRegistry::getRegistry().makeTest(); CppUnit::TextUi::TestRunner runner; runner.addTest(suite);