2009-12-08 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>

Fixed the bug that aria2 listens wrong port if --interface option
	is used.
	* src/SocketCore.cc
pull/1/head
Tatsuhiro Tsujikawa 2009-12-07 15:04:33 +00:00
parent 8cd1d845f2
commit cc4a14a1bc
2 changed files with 51 additions and 28 deletions

View File

@ -1,3 +1,9 @@
2009-12-08 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
Fixed the bug that aria2 listens wrong port if --interface option
is used.
* src/SocketCore.cc
2009-12-07 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net> 2009-12-07 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
Fixed the bug that query parameter in magnet URI is not decoded. Fixed the bug that query parameter in magnet URI is not decoded.

View File

@ -187,41 +187,58 @@ static sock_t bindInternal(int family, int socktype, int protocol,
return fd; return fd;
} }
void SocketCore::bind(uint16_t port, int flags) static sock_t bindTo
(const char* host, uint16_t port, int family, int sockType,
int getaddrinfoFlags, std::string& error)
{ {
closeConnection();
if(flags == 0 || _bindAddrs.empty()) {
struct addrinfo hints; struct addrinfo hints;
struct addrinfo* res; struct addrinfo* res;
memset(&hints, 0, sizeof(hints)); memset(&hints, 0, sizeof(hints));
hints.ai_family = _protocolFamily; hints.ai_family = family;
hints.ai_socktype = _sockType; hints.ai_socktype = sockType;
hints.ai_flags = flags; hints.ai_flags = getaddrinfoFlags;
hints.ai_protocol = 0; hints.ai_protocol = 0;
int s; int s = getaddrinfo(host, uitos(port).c_str(), &hints, &res);
s = getaddrinfo(0, uitos(port).c_str(), &hints, &res);
if(s) { if(s) {
throw DL_ABORT_EX(StringFormat(EX_SOCKET_BIND, gai_strerror(s)).str()); error = gai_strerror(s);
return -1;
} }
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 = bindInternal(rp->ai_family, rp->ai_socktype, rp->ai_protocol, sock_t fd = bindInternal(rp->ai_family, rp->ai_socktype, rp->ai_protocol,
rp->ai_addr, rp->ai_addrlen); rp->ai_addr, rp->ai_addrlen);
if(fd == (sock_t) -1) { if(fd != (sock_t)-1) {
continue; return fd;
} }
}
error = strerror(errno);
return -1;
}
void SocketCore::bind(uint16_t port, int flags)
{
closeConnection();
std::string error;
if(!(flags&AI_PASSIVE) || _bindAddrs.empty()) {
sock_t fd = bindTo(0, port, _protocolFamily, _sockType, flags, error);
if(fd != (sock_t) -1) {
sockfd = fd; sockfd = fd;
break;
} }
freeaddrinfo(res);
} else { } else {
for(std::vector<std::pair<struct sockaddr_storage, socklen_t> >:: for(std::vector<std::pair<struct sockaddr_storage, socklen_t> >::
const_iterator i = _bindAddrs.begin(); i != _bindAddrs.end(); ++i) { const_iterator i = _bindAddrs.begin(); i != _bindAddrs.end(); ++i) {
sock_t fd = bindInternal char host[NI_MAXHOST];
((*i).first.ss_family, _sockType, 0, int s;
reinterpret_cast<const struct sockaddr*> s = getnameinfo(reinterpret_cast<const struct sockaddr*>(&(*i).first),
(&(*i).first), (*i).second); (*i).second,
host, NI_MAXHOST, 0, NI_MAXSERV,
NI_NUMERICHOST);
if(s) {
error = gai_strerror(s);
continue;
}
sock_t fd = bindTo(host, port, _protocolFamily, _sockType, flags, error);
if(fd != (sock_t)-1) { if(fd != (sock_t)-1) {
sockfd = fd; sockfd = fd;
break; break;
@ -229,7 +246,7 @@ void SocketCore::bind(uint16_t port, int flags)
} }
} }
if(sockfd == (sock_t) -1) { if(sockfd == (sock_t) -1) {
throw DL_ABORT_EX(StringFormat(EX_SOCKET_BIND, strerror(errno)).str()); throw DL_ABORT_EX(StringFormat(EX_SOCKET_BIND, error.c_str()).str());
} }
} }