mirror of https://github.com/aria2/aria2
2009-12-24 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
Use AI_ADDRCONFIG flag if it is available. Refactored so that getaddrinfo calls are not scattered around. Unset AI_ADDRCONFIG when conducting unit tests because they fail if networking interface is not configured with IPv4 address. * src/NameResolver.cc * src/SocketCore.cc * src/SocketCore.h * src/a2netcompat.h * test/AllTest.ccpull/1/head
parent
0190564ad3
commit
88bcc6e681
12
ChangeLog
12
ChangeLog
|
@ -1,3 +1,15 @@
|
||||||
|
2009-12-24 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
|
||||||
|
|
||||||
|
Use AI_ADDRCONFIG flag if it is available. Refactored so that
|
||||||
|
getaddrinfo calls are not scattered around. Unset AI_ADDRCONFIG
|
||||||
|
when conducting unit tests because they fail if networking
|
||||||
|
interface is not configured with IPv4 address.
|
||||||
|
* src/NameResolver.cc
|
||||||
|
* src/SocketCore.cc
|
||||||
|
* src/SocketCore.h
|
||||||
|
* src/a2netcompat.h
|
||||||
|
* test/AllTest.cc
|
||||||
|
|
||||||
2009-12-24 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
|
2009-12-24 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
|
||||||
|
|
||||||
Fixed unit test error
|
Fixed unit test error
|
||||||
|
|
|
@ -40,6 +40,7 @@
|
||||||
#include "message.h"
|
#include "message.h"
|
||||||
#include "StringFormat.h"
|
#include "StringFormat.h"
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
|
#include "SocketCore.h"
|
||||||
|
|
||||||
namespace aria2 {
|
namespace aria2 {
|
||||||
|
|
||||||
|
@ -48,26 +49,20 @@ NameResolver::NameResolver():_socktype(0), _family(AF_UNSPEC) {}
|
||||||
void NameResolver::resolve(std::deque<std::string>& resolvedAddresses,
|
void NameResolver::resolve(std::deque<std::string>& resolvedAddresses,
|
||||||
const std::string& hostname)
|
const std::string& hostname)
|
||||||
{
|
{
|
||||||
struct addrinfo hints;
|
|
||||||
struct addrinfo* res;
|
struct addrinfo* res;
|
||||||
memset(&hints, 0, sizeof(hints));
|
|
||||||
hints.ai_family = _family;
|
|
||||||
hints.ai_socktype = _socktype;
|
|
||||||
hints.ai_flags = 0;
|
|
||||||
hints.ai_protocol = 0;
|
|
||||||
int s;
|
int s;
|
||||||
s = getaddrinfo(hostname.c_str(), 0, &hints, &res);
|
s = callGetaddrinfo(&res, hostname.c_str(), 0, _family, _socktype, 0, 0);
|
||||||
if(s) {
|
if(s) {
|
||||||
throw DL_ABORT_EX(StringFormat(EX_RESOLVE_HOSTNAME,
|
throw DL_ABORT_EX(StringFormat(EX_RESOLVE_HOSTNAME,
|
||||||
hostname.c_str(), gai_strerror(s)).str());
|
hostname.c_str(), gai_strerror(s)).str());
|
||||||
}
|
}
|
||||||
|
auto_delete<struct addrinfo*> resDeleter(res, freeaddrinfo);
|
||||||
struct addrinfo* rp;
|
struct addrinfo* rp;
|
||||||
for(rp = res; rp; rp = rp->ai_next) {
|
for(rp = res; rp; rp = rp->ai_next) {
|
||||||
std::pair<std::string, uint16_t> addressPort
|
std::pair<std::string, uint16_t> addressPort
|
||||||
= util::getNumericNameInfo(rp->ai_addr, rp->ai_addrlen);
|
= util::getNumericNameInfo(rp->ai_addr, rp->ai_addrlen);
|
||||||
resolvedAddresses.push_back(addressPort.first);
|
resolvedAddresses.push_back(addressPort.first);
|
||||||
}
|
}
|
||||||
freeaddrinfo(res);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void NameResolver::setSocktype(int socktype)
|
void NameResolver::setSocktype(int socktype)
|
||||||
|
|
|
@ -191,14 +191,9 @@ static sock_t bindTo
|
||||||
(const char* host, uint16_t port, int family, int sockType,
|
(const char* host, uint16_t port, int family, int sockType,
|
||||||
int getaddrinfoFlags, std::string& error)
|
int getaddrinfoFlags, std::string& error)
|
||||||
{
|
{
|
||||||
struct addrinfo hints;
|
|
||||||
struct addrinfo* res;
|
struct addrinfo* res;
|
||||||
memset(&hints, 0, sizeof(hints));
|
int s = callGetaddrinfo(&res, host, uitos(port).c_str(), family, sockType,
|
||||||
hints.ai_family = family;
|
getaddrinfoFlags, 0);
|
||||||
hints.ai_socktype = sockType;
|
|
||||||
hints.ai_flags = getaddrinfoFlags;
|
|
||||||
hints.ai_protocol = 0;
|
|
||||||
int s = getaddrinfo(host, uitos(port).c_str(), &hints, &res);
|
|
||||||
if(s) {
|
if(s) {
|
||||||
error = gai_strerror(s);
|
error = gai_strerror(s);
|
||||||
return -1;
|
return -1;
|
||||||
|
@ -307,19 +302,15 @@ void SocketCore::establishConnection(const std::string& host, uint16_t port)
|
||||||
{
|
{
|
||||||
closeConnection();
|
closeConnection();
|
||||||
|
|
||||||
struct addrinfo hints;
|
|
||||||
struct addrinfo* res;
|
struct addrinfo* res;
|
||||||
memset(&hints, 0, sizeof(hints));
|
|
||||||
hints.ai_family = _protocolFamily;
|
|
||||||
hints.ai_socktype = _sockType;
|
|
||||||
hints.ai_flags = 0;
|
|
||||||
hints.ai_protocol = 0;
|
|
||||||
int s;
|
int s;
|
||||||
s = getaddrinfo(host.c_str(), uitos(port).c_str(), &hints, &res);
|
s = callGetaddrinfo(&res, host.c_str(), uitos(port).c_str(), _protocolFamily,
|
||||||
|
_sockType, 0, 0);
|
||||||
if(s) {
|
if(s) {
|
||||||
throw DL_ABORT_EX(StringFormat(EX_RESOLVE_HOSTNAME,
|
throw DL_ABORT_EX(StringFormat(EX_RESOLVE_HOSTNAME,
|
||||||
host.c_str(), gai_strerror(s)).str());
|
host.c_str(), gai_strerror(s)).str());
|
||||||
}
|
}
|
||||||
|
auto_delete<struct addrinfo*> resDeleter(res, freeaddrinfo);
|
||||||
struct addrinfo* rp;
|
struct addrinfo* rp;
|
||||||
for(rp = res; rp; rp = rp->ai_next) {
|
for(rp = res; rp; rp = rp->ai_next) {
|
||||||
sock_t fd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
|
sock_t fd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
|
||||||
|
@ -362,7 +353,6 @@ void SocketCore::establishConnection(const std::string& host, uint16_t port)
|
||||||
// later. In such case, next ai_addr should be tried.
|
// later. In such case, next ai_addr should be tried.
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
freeaddrinfo(res);
|
|
||||||
if(sockfd == (sock_t) -1) {
|
if(sockfd == (sock_t) -1) {
|
||||||
throw DL_ABORT_EX(StringFormat(EX_SOCKET_CONNECT, host.c_str(),
|
throw DL_ABORT_EX(StringFormat(EX_SOCKET_CONNECT, host.c_str(),
|
||||||
strerror(errno)).str());
|
strerror(errno)).str());
|
||||||
|
@ -1058,18 +1048,14 @@ ssize_t SocketCore::writeData(const char* data, size_t len,
|
||||||
_wantRead = false;
|
_wantRead = false;
|
||||||
_wantWrite = false;
|
_wantWrite = false;
|
||||||
|
|
||||||
struct addrinfo hints;
|
|
||||||
struct addrinfo* res;
|
struct addrinfo* res;
|
||||||
memset(&hints, 0, sizeof(hints));
|
|
||||||
hints.ai_family = _protocolFamily;
|
|
||||||
hints.ai_socktype = _sockType;
|
|
||||||
hints.ai_flags = 0;
|
|
||||||
hints.ai_protocol = 0;
|
|
||||||
int s;
|
int s;
|
||||||
s = getaddrinfo(host.c_str(), uitos(port).c_str(), &hints, &res);
|
s = callGetaddrinfo(&res, host.c_str(), uitos(port).c_str(), _protocolFamily,
|
||||||
|
_sockType, 0, 0);
|
||||||
if(s) {
|
if(s) {
|
||||||
throw DL_ABORT_EX(StringFormat(EX_SOCKET_SEND, gai_strerror(s)).str());
|
throw DL_ABORT_EX(StringFormat(EX_SOCKET_SEND, gai_strerror(s)).str());
|
||||||
}
|
}
|
||||||
|
auto_delete<struct addrinfo*> resDeleter(res, freeaddrinfo);
|
||||||
struct addrinfo* rp;
|
struct addrinfo* rp;
|
||||||
ssize_t r = -1;
|
ssize_t r = -1;
|
||||||
for(rp = res; rp; rp = rp->ai_next) {
|
for(rp = res; rp; rp = rp->ai_next) {
|
||||||
|
@ -1083,7 +1069,6 @@ ssize_t SocketCore::writeData(const char* data, size_t len,
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
freeaddrinfo(res);
|
|
||||||
if(r == -1) {
|
if(r == -1) {
|
||||||
throw DL_ABORT_EX(StringFormat(EX_SOCKET_SEND, errorMsg()).str());
|
throw DL_ABORT_EX(StringFormat(EX_SOCKET_SEND, errorMsg()).str());
|
||||||
}
|
}
|
||||||
|
@ -1200,15 +1185,10 @@ void SocketCore::bindAddress(const std::string& interface)
|
||||||
}
|
}
|
||||||
#endif // HAVE_GETIFADDRS
|
#endif // HAVE_GETIFADDRS
|
||||||
if(bindAddrs.empty()) {
|
if(bindAddrs.empty()) {
|
||||||
struct addrinfo hints;
|
struct addrinfo* res;
|
||||||
struct addrinfo* res = 0;
|
|
||||||
memset(&hints, 0, sizeof(hints));
|
|
||||||
hints.ai_family = _protocolFamily;
|
|
||||||
hints.ai_socktype = SOCK_STREAM;
|
|
||||||
hints.ai_flags = 0;
|
|
||||||
hints.ai_protocol = 0;
|
|
||||||
int s;
|
int s;
|
||||||
s = getaddrinfo(interface.c_str(), 0, &hints, &res);
|
s = callGetaddrinfo(&res, interface.c_str(), 0, _protocolFamily,
|
||||||
|
SOCK_STREAM, 0, 0);
|
||||||
if(s) {
|
if(s) {
|
||||||
throw DL_ABORT_EX
|
throw DL_ABORT_EX
|
||||||
(StringFormat(MSG_INTERFACE_NOT_FOUND,
|
(StringFormat(MSG_INTERFACE_NOT_FOUND,
|
||||||
|
@ -1255,4 +1235,34 @@ void SocketCore::bindAddress(const std::string& interface)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
int defaultAIFlags = DEFAULT_AI_FLAGS;
|
||||||
|
|
||||||
|
int getDefaultAIFlags()
|
||||||
|
{
|
||||||
|
return defaultAIFlags;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void setDefaultAIFlags(int flags)
|
||||||
|
{
|
||||||
|
defaultAIFlags = flags;
|
||||||
|
}
|
||||||
|
|
||||||
|
int callGetaddrinfo
|
||||||
|
(struct addrinfo** resPtr, const char* host, const char* service, int family,
|
||||||
|
int sockType, int flags, int protocol)
|
||||||
|
{
|
||||||
|
struct addrinfo hints;
|
||||||
|
memset(&hints, 0, sizeof(hints));
|
||||||
|
hints.ai_family = family;
|
||||||
|
hints.ai_socktype = sockType;
|
||||||
|
hints.ai_flags = getDefaultAIFlags();
|
||||||
|
hints.ai_flags |= flags;
|
||||||
|
hints.ai_protocol = protocol;
|
||||||
|
return getaddrinfo(host, service, &hints, resPtr);
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace aria2
|
} // namespace aria2
|
||||||
|
|
|
@ -363,6 +363,18 @@ public:
|
||||||
static void bindAddress(const std::string& interface);
|
static void bindAddress(const std::string& interface);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Set default ai_flags. hints.ai_flags is initialized with this
|
||||||
|
// value.
|
||||||
|
void setDefaultAIFlags(int flags);
|
||||||
|
|
||||||
|
// Wrapper function for getaddrinfo(). The value
|
||||||
|
// flags|DEFAULT_AI_FLAGS is used as ai_flags. You can override
|
||||||
|
// DEFAULT_AI_FLAGS value by calling setDefaultAIFlags() with new
|
||||||
|
// flags.
|
||||||
|
int callGetaddrinfo
|
||||||
|
(struct addrinfo** resPtr, const char* host, const char* service, int family,
|
||||||
|
int sockType, int flags, int protocol);
|
||||||
|
|
||||||
} // namespace aria2
|
} // namespace aria2
|
||||||
|
|
||||||
#endif // _D_SOCKET_CORE_H_
|
#endif // _D_SOCKET_CORE_H_
|
||||||
|
|
|
@ -97,4 +97,10 @@
|
||||||
# define sock_t int
|
# define sock_t int
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifdef AI_ADDRCONFIG
|
||||||
|
# define DEFAULT_AI_FLAGS AI_ADDRCONFIG
|
||||||
|
#else // !AI_ADDRCONFIG
|
||||||
|
# define DEFAULT_AI_FLAGS 0
|
||||||
|
#endif // !AI_ADDRCONFIG
|
||||||
|
|
||||||
#endif // _D_A2NETCOMPAT_H_
|
#endif // _D_A2NETCOMPAT_H_
|
||||||
|
|
|
@ -24,6 +24,9 @@ int main(int argc, char* argv[]) {
|
||||||
// and latter cannot connect to former. To avoid this situation, we
|
// and latter cannot connect to former. To avoid this situation, we
|
||||||
// limit protocol family to AF_INET for unit tests.
|
// limit protocol family to AF_INET for unit tests.
|
||||||
aria2::SocketCore::setProtocolFamily(AF_INET);
|
aria2::SocketCore::setProtocolFamily(AF_INET);
|
||||||
|
// If AI_ADDRCONFIG is set, tests fail if IPv4 address is not
|
||||||
|
// configured.
|
||||||
|
aria2::setDefaultAIFlags(0);
|
||||||
|
|
||||||
CppUnit::Test* suite = CppUnit::TestFactoryRegistry::getRegistry().makeTest();
|
CppUnit::Test* suite = CppUnit::TestFactoryRegistry::getRegistry().makeTest();
|
||||||
CppUnit::TextUi::TestRunner runner;
|
CppUnit::TextUi::TestRunner runner;
|
||||||
|
|
Loading…
Reference in New Issue