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

Added debug logs.
	* src/SocketCore.cc
pull/1/head
Tatsuhiro Tsujikawa 2009-12-04 07:37:16 +00:00
parent 5bb9eaec17
commit 6a546813ba
2 changed files with 36 additions and 21 deletions

View File

@ -1,3 +1,8 @@
2009-12-04 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
Added debug logs.
* src/SocketCore.cc
2009-12-03 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net> 2009-12-03 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
Fixed type of tail to uint64_t Fixed type of tail to uint64_t

View File

@ -170,22 +170,22 @@ std::string uitos(T value)
static sock_t bindInternal(int family, int socktype, int protocol, static sock_t bindInternal(int family, int socktype, int protocol,
const struct sockaddr* addr, socklen_t addrlen) const struct sockaddr* addr, socklen_t addrlen)
{ {
sock_t fd = socket(family, socktype, protocol); sock_t fd = socket(family, socktype, protocol);
if(fd == (sock_t) -1) { if(fd == (sock_t) -1) {
return -1; return -1;
} }
int sockopt = 1; int sockopt = 1;
if(setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (a2_sockopt_t) &sockopt, if(setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (a2_sockopt_t) &sockopt,
sizeof(sockopt)) < 0) { sizeof(sockopt)) < 0) {
CLOSE(fd); CLOSE(fd);
return -1; return -1;
} }
if(::bind(fd, addr, addrlen) == -1) { if(::bind(fd, addr, addrlen) == -1) {
LogFactory::getInstance()->debug("%s", strerror(errno)); LogFactory::getInstance()->debug(EX_SOCKET_BIND, strerror(errno));
CLOSE(fd); CLOSE(fd);
return -1; return -1;
} }
return fd; return fd;
} }
void SocketCore::bind(uint16_t port, int flags) void SocketCore::bind(uint16_t port, int flags)
@ -225,7 +225,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, "all addresses failed").str()); throw DL_ABORT_EX(StringFormat(EX_SOCKET_BIND, strerror(errno)).str());
} }
} }
@ -236,8 +236,7 @@ void SocketCore::bind(const struct sockaddr* addr, socklen_t addrlen)
sock_t fd = bindInternal(addr->sa_family, _sockType, 0, addr, addrlen); sock_t fd = bindInternal(addr->sa_family, _sockType, 0, addr, addrlen);
if(fd != (sock_t)-1) { if(fd != (sock_t)-1) {
sockfd = fd; sockfd = fd;
} } else {
if(sockfd == (sock_t) -1) {
throw DL_ABORT_EX(StringFormat(EX_SOCKET_BIND, strerror(errno)).str()); throw DL_ABORT_EX(StringFormat(EX_SOCKET_BIND, strerror(errno)).str());
} }
} }
@ -315,6 +314,7 @@ void SocketCore::establishConnection(const std::string& host, uint16_t port)
if(!_bindAddr.isNull()) { if(!_bindAddr.isNull()) {
if(::bind(fd, reinterpret_cast<const struct sockaddr*>(_bindAddr.get()), if(::bind(fd, reinterpret_cast<const struct sockaddr*>(_bindAddr.get()),
_bindAddrLen) == -1) { _bindAddrLen) == -1) {
LogFactory::getInstance()->debug(EX_SOCKET_BIND, strerror(errno));
CLOSE(fd); CLOSE(fd);
continue; continue;
} }
@ -336,7 +336,7 @@ void SocketCore::establishConnection(const std::string& host, uint16_t port)
freeaddrinfo(res); 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(),
"all addresses failed").str()); strerror(errno)).str());
} }
} }
@ -1131,6 +1131,7 @@ void SocketCore::bindAddress(const std::string& interface)
socklen_t bindAddrLen = 0; socklen_t bindAddrLen = 0;
memset(bindAddr.get(), 0, sizeof(struct sockaddr_storage)); memset(bindAddr.get(), 0, sizeof(struct sockaddr_storage));
bool found = false; bool found = false;
LogFactory::getInstance()->debug("Finding interface %s", interface.c_str());
#ifdef HAVE_GETIFADDRS #ifdef HAVE_GETIFADDRS
// First find interface in interface addresses // First find interface in interface addresses
struct ifaddrs* ifaddr = 0; struct ifaddrs* ifaddr = 0;
@ -1201,7 +1202,7 @@ void SocketCore::bindAddress(const std::string& interface)
} catch(RecoverableException& e) { } catch(RecoverableException& e) {
throw DL_ABORT_EX2 throw DL_ABORT_EX2
(StringFormat(MSG_INTERFACE_NOT_FOUND, (StringFormat(MSG_INTERFACE_NOT_FOUND,
interface.c_str()).str(), e); interface.c_str(), e.what()).str(), e);
} }
found = true; found = true;
break; break;
@ -1209,6 +1210,15 @@ void SocketCore::bindAddress(const std::string& interface)
} }
} }
if(found) { if(found) {
char host[NI_MAXHOST];
int s;
s = getnameinfo(reinterpret_cast<struct sockaddr*>(bindAddr.get()),
bindAddrLen,
host, NI_MAXHOST, 0, NI_MAXSERV,
NI_NUMERICHOST);
if(s == 0) {
LogFactory::getInstance()->debug("Sockets will bind to %s", host);
}
_bindAddr = bindAddr; _bindAddr = bindAddr;
_bindAddrLen = bindAddrLen; _bindAddrLen = bindAddrLen;
} }