Try all found address set to bind sockets with --multiple-interface

Fixes GH-523
pull/538/head
Tatsuhiro Tsujikawa 2016-01-09 14:06:09 +09:00
parent e92027b9f0
commit d2d06395d9
1 changed files with 25 additions and 18 deletions

View File

@ -306,7 +306,7 @@ void SocketCore::bind(const char* addr, uint16_t port, int family, int flags)
else { else {
addrp = nullptr; addrp = nullptr;
} }
if (!(flags & AI_PASSIVE) || bindAddrs_.empty()) { if (addrp || !(flags & AI_PASSIVE) || bindAddrsList_.empty()) {
sock_t fd = bindTo(addrp, port, family, sockType_, flags, error); sock_t fd = bindTo(addrp, port, family, sockType_, flags, error);
if (fd == (sock_t)-1) { if (fd == (sock_t)-1) {
throw DL_ABORT_EX(fmt(EX_SOCKET_BIND, error.c_str())); throw DL_ABORT_EX(fmt(EX_SOCKET_BIND, error.c_str()));
@ -315,25 +315,30 @@ void SocketCore::bind(const char* addr, uint16_t port, int family, int flags)
return; return;
} }
for (const auto& a : bindAddrs_) { std::array<char, NI_MAXHOST> host;
char host[NI_MAXHOST]; for (const auto& bindAddrs : bindAddrsList_) {
int s; for (const auto& a : bindAddrs) {
s = getnameinfo(&a.first.sa, a.second, host, NI_MAXHOST, nullptr, 0, if (family != AF_UNSPEC && family != a.first.storage.ss_family) {
NI_NUMERICHOST); continue;
}
auto s = getnameinfo(&a.first.sa, a.second, host.data(), NI_MAXHOST,
nullptr, 0, NI_NUMERICHOST);
if (s) { if (s) {
error = gai_strerror(s); error = gai_strerror(s);
continue; continue;
} }
if (addrp && strcmp(host, addrp) != 0) { if (addrp && strcmp(host.data(), addrp) != 0) {
error = "Given address and resolved address do not match."; error = "Given address and resolved address do not match.";
continue; continue;
} }
sock_t fd = bindTo(host, port, family, sockType_, flags, error); auto fd = bindTo(host.data(), port, family, sockType_, flags, error);
if (fd != (sock_t)-1) { if (fd != (sock_t)-1) {
sockfd_ = fd; sockfd_ = fd;
break; return;
} }
} }
}
if (sockfd_ == (sock_t)-1) { if (sockfd_ == (sock_t)-1) {
throw DL_ABORT_EX(fmt(EX_SOCKET_BIND, error.c_str())); throw DL_ABORT_EX(fmt(EX_SOCKET_BIND, error.c_str()));
} }
@ -1287,6 +1292,8 @@ void SocketCore::bindAddress(const std::string& iface)
A2_LOG_DEBUG(fmt("Sockets will bind to %s", host)); A2_LOG_DEBUG(fmt("Sockets will bind to %s", host));
} }
} }
bindAddrsList_.push_back(bindAddrs_);
bindAddrsListIt_ = std::begin(bindAddrsList_);
} }
void SocketCore::bindAllAddress(const std::string& ifaces) void SocketCore::bindAllAddress(const std::string& ifaces)