mirror of https://github.com/aria2/aria2
2010-06-12 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
Renamed member variables * src/SocketCore.cc * src/SocketCore.hpull/1/head
parent
8d1da8bd3e
commit
6b2eeaa9e1
|
@ -1,3 +1,9 @@
|
||||||
|
2010-06-12 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
|
||||||
|
|
||||||
|
Renamed member variables
|
||||||
|
* src/SocketCore.cc
|
||||||
|
* src/SocketCore.h
|
||||||
|
|
||||||
2010-06-12 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
|
2010-06-12 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
|
||||||
|
|
||||||
Made public member variable in Metalinker private
|
Made public member variable in Metalinker private
|
||||||
|
|
|
@ -132,18 +132,18 @@ void SocketCore::setTLSContext(const SharedHandle<TLSContext>& tlsContext)
|
||||||
}
|
}
|
||||||
#endif // ENABLE_SSL
|
#endif // ENABLE_SSL
|
||||||
|
|
||||||
SocketCore::SocketCore(int sockType):_sockType(sockType), sockfd(-1) {
|
SocketCore::SocketCore(int sockType):_sockType(sockType), _sockfd(-1) {
|
||||||
init();
|
init();
|
||||||
}
|
}
|
||||||
|
|
||||||
SocketCore::SocketCore(sock_t sockfd, int sockType):_sockType(sockType), sockfd(sockfd) {
|
SocketCore::SocketCore(sock_t sockfd, int sockType):_sockType(sockType), _sockfd(sockfd) {
|
||||||
init();
|
init();
|
||||||
}
|
}
|
||||||
|
|
||||||
void SocketCore::init()
|
void SocketCore::init()
|
||||||
{
|
{
|
||||||
blocking = true;
|
_blocking = true;
|
||||||
secure = 0;
|
_secure = 0;
|
||||||
|
|
||||||
_wantRead = false;
|
_wantRead = false;
|
||||||
_wantWrite = false;
|
_wantWrite = false;
|
||||||
|
@ -153,17 +153,17 @@ void SocketCore::init()
|
||||||
ssl = NULL;
|
ssl = NULL;
|
||||||
#endif // HAVE_LIBSSL
|
#endif // HAVE_LIBSSL
|
||||||
#ifdef HAVE_LIBGNUTLS
|
#ifdef HAVE_LIBGNUTLS
|
||||||
sslSession = NULL;
|
_sslSession = 0;
|
||||||
peekBufMax = 4096;
|
_peekBufMax = 4096;
|
||||||
peekBuf = 0;
|
_peekBuf = 0;
|
||||||
peekBufLength = 0;
|
_peekBufLength = 0;
|
||||||
#endif //HAVE_LIBGNUTLS
|
#endif //HAVE_LIBGNUTLS
|
||||||
}
|
}
|
||||||
|
|
||||||
SocketCore::~SocketCore() {
|
SocketCore::~SocketCore() {
|
||||||
closeConnection();
|
closeConnection();
|
||||||
#ifdef HAVE_LIBGNUTLS
|
#ifdef HAVE_LIBGNUTLS
|
||||||
delete [] peekBuf;
|
delete [] _peekBuf;
|
||||||
#endif // HAVE_LIBGNUTLS
|
#endif // HAVE_LIBGNUTLS
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -198,7 +198,7 @@ void SocketCore::create(int family, int protocol)
|
||||||
throw DL_ABORT_EX
|
throw DL_ABORT_EX
|
||||||
(StringFormat("Failed to create socket. Cause:%s", errorMsg()).str());
|
(StringFormat("Failed to create socket. Cause:%s", errorMsg()).str());
|
||||||
}
|
}
|
||||||
sockfd = fd;
|
_sockfd = fd;
|
||||||
}
|
}
|
||||||
|
|
||||||
static sock_t bindInternal(int family, int socktype, int protocol,
|
static sock_t bindInternal(int family, int socktype, int protocol,
|
||||||
|
@ -254,7 +254,7 @@ void SocketCore::bindWithFamily(uint16_t port, int family, int flags)
|
||||||
if(fd == (sock_t) -1) {
|
if(fd == (sock_t) -1) {
|
||||||
throw DL_ABORT_EX(StringFormat(EX_SOCKET_BIND, error.c_str()).str());
|
throw DL_ABORT_EX(StringFormat(EX_SOCKET_BIND, error.c_str()).str());
|
||||||
} else {
|
} else {
|
||||||
sockfd = fd;
|
_sockfd = fd;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -267,7 +267,7 @@ void SocketCore::bind(const std::string& addr, uint16_t port, int flags)
|
||||||
if(fd == (sock_t)-1) {
|
if(fd == (sock_t)-1) {
|
||||||
throw DL_ABORT_EX(StringFormat(EX_SOCKET_BIND, error.c_str()).str());
|
throw DL_ABORT_EX(StringFormat(EX_SOCKET_BIND, error.c_str()).str());
|
||||||
} else {
|
} else {
|
||||||
sockfd = fd;
|
_sockfd = fd;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -278,7 +278,7 @@ void SocketCore::bind(uint16_t port, int flags)
|
||||||
if(!(flags&AI_PASSIVE) || _bindAddrs.empty()) {
|
if(!(flags&AI_PASSIVE) || _bindAddrs.empty()) {
|
||||||
sock_t fd = bindTo(0, port, _protocolFamily, _sockType, flags, error);
|
sock_t fd = bindTo(0, port, _protocolFamily, _sockType, flags, error);
|
||||||
if(fd != (sock_t) -1) {
|
if(fd != (sock_t) -1) {
|
||||||
sockfd = fd;
|
_sockfd = fd;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
for(std::vector<std::pair<struct sockaddr_storage, socklen_t> >::
|
for(std::vector<std::pair<struct sockaddr_storage, socklen_t> >::
|
||||||
|
@ -296,12 +296,12 @@ void SocketCore::bind(uint16_t port, int flags)
|
||||||
}
|
}
|
||||||
sock_t fd = bindTo(host, port, _protocolFamily, _sockType, flags, error);
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(sockfd == (sock_t) -1) {
|
if(_sockfd == (sock_t) -1) {
|
||||||
throw DL_ABORT_EX(StringFormat(EX_SOCKET_BIND, error.c_str()).str());
|
throw DL_ABORT_EX(StringFormat(EX_SOCKET_BIND, error.c_str()).str());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -312,7 +312,7 @@ void SocketCore::bind(const struct sockaddr* addr, socklen_t addrlen)
|
||||||
std::string error;
|
std::string error;
|
||||||
sock_t fd = bindInternal(addr->sa_family, _sockType, 0, addr, addrlen, error);
|
sock_t fd = bindInternal(addr->sa_family, _sockType, 0, addr, addrlen, error);
|
||||||
if(fd != (sock_t)-1) {
|
if(fd != (sock_t)-1) {
|
||||||
sockfd = fd;
|
_sockfd = fd;
|
||||||
} else {
|
} else {
|
||||||
throw DL_ABORT_EX(StringFormat(EX_SOCKET_BIND, error.c_str()).str());
|
throw DL_ABORT_EX(StringFormat(EX_SOCKET_BIND, error.c_str()).str());
|
||||||
}
|
}
|
||||||
|
@ -320,7 +320,7 @@ void SocketCore::bind(const struct sockaddr* addr, socklen_t addrlen)
|
||||||
|
|
||||||
void SocketCore::beginListen()
|
void SocketCore::beginListen()
|
||||||
{
|
{
|
||||||
if(listen(sockfd, 1) == -1) {
|
if(listen(_sockfd, 1) == -1) {
|
||||||
throw DL_ABORT_EX(StringFormat(EX_SOCKET_LISTEN, errorMsg()).str());
|
throw DL_ABORT_EX(StringFormat(EX_SOCKET_LISTEN, errorMsg()).str());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -330,7 +330,7 @@ SocketCore* SocketCore::acceptConnection() const
|
||||||
struct sockaddr_storage sockaddr;
|
struct sockaddr_storage sockaddr;
|
||||||
socklen_t len = sizeof(sockaddr);
|
socklen_t len = sizeof(sockaddr);
|
||||||
sock_t fd;
|
sock_t fd;
|
||||||
while((fd = accept(sockfd, reinterpret_cast<struct sockaddr*>(&sockaddr), &len)) == (sock_t) -1 && SOCKET_ERRNO == A2_EINTR);
|
while((fd = accept(_sockfd, reinterpret_cast<struct sockaddr*>(&sockaddr), &len)) == (sock_t) -1 && SOCKET_ERRNO == A2_EINTR);
|
||||||
if(fd == (sock_t) -1) {
|
if(fd == (sock_t) -1) {
|
||||||
throw DL_ABORT_EX(StringFormat(EX_SOCKET_ACCEPT, errorMsg()).str());
|
throw DL_ABORT_EX(StringFormat(EX_SOCKET_ACCEPT, errorMsg()).str());
|
||||||
}
|
}
|
||||||
|
@ -342,7 +342,7 @@ void SocketCore::getAddrInfo(std::pair<std::string, uint16_t>& addrinfo) const
|
||||||
struct sockaddr_storage sockaddr;
|
struct sockaddr_storage sockaddr;
|
||||||
socklen_t len = sizeof(sockaddr);
|
socklen_t len = sizeof(sockaddr);
|
||||||
struct sockaddr* addrp = reinterpret_cast<struct sockaddr*>(&sockaddr);
|
struct sockaddr* addrp = reinterpret_cast<struct sockaddr*>(&sockaddr);
|
||||||
if(getsockname(sockfd, addrp, &len) == -1) {
|
if(getsockname(_sockfd, addrp, &len) == -1) {
|
||||||
throw DL_ABORT_EX(StringFormat(EX_SOCKET_GET_NAME, errorMsg()).str());
|
throw DL_ABORT_EX(StringFormat(EX_SOCKET_GET_NAME, errorMsg()).str());
|
||||||
}
|
}
|
||||||
addrinfo = util::getNumericNameInfo(addrp, len);
|
addrinfo = util::getNumericNameInfo(addrp, len);
|
||||||
|
@ -353,7 +353,7 @@ void SocketCore::getPeerInfo(std::pair<std::string, uint16_t>& peerinfo) const
|
||||||
struct sockaddr_storage sockaddr;
|
struct sockaddr_storage sockaddr;
|
||||||
socklen_t len = sizeof(sockaddr);
|
socklen_t len = sizeof(sockaddr);
|
||||||
struct sockaddr* addrp = reinterpret_cast<struct sockaddr*>(&sockaddr);
|
struct sockaddr* addrp = reinterpret_cast<struct sockaddr*>(&sockaddr);
|
||||||
if(getpeername(sockfd, addrp, &len) == -1) {
|
if(getpeername(_sockfd, addrp, &len) == -1) {
|
||||||
throw DL_ABORT_EX(StringFormat(EX_SOCKET_GET_NAME, errorMsg()).str());
|
throw DL_ABORT_EX(StringFormat(EX_SOCKET_GET_NAME, errorMsg()).str());
|
||||||
}
|
}
|
||||||
peerinfo = util::getNumericNameInfo(addrp, len);
|
peerinfo = util::getNumericNameInfo(addrp, len);
|
||||||
|
@ -407,21 +407,21 @@ void SocketCore::establishConnection(const std::string& host, uint16_t port)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
sockfd = fd;
|
_sockfd = fd;
|
||||||
// make socket non-blocking mode
|
// make socket non-blocking mode
|
||||||
setNonBlockingMode();
|
setNonBlockingMode();
|
||||||
if(connect(fd, rp->ai_addr, rp->ai_addrlen) == -1 &&
|
if(connect(fd, rp->ai_addr, rp->ai_addrlen) == -1 &&
|
||||||
SOCKET_ERRNO != A2_EINPROGRESS) {
|
SOCKET_ERRNO != A2_EINPROGRESS) {
|
||||||
error = errorMsg();
|
error = errorMsg();
|
||||||
CLOSE(sockfd);
|
CLOSE(_sockfd);
|
||||||
sockfd = (sock_t) -1;
|
_sockfd = (sock_t) -1;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
// TODO at this point, connection may not be established and it may fail
|
// TODO at this point, connection may not be established and it may fail
|
||||||
// later. In such case, next ai_addr should be tried.
|
// later. In such case, next ai_addr should be tried.
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
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(),
|
||||||
error.c_str()).str());
|
error.c_str()).str());
|
||||||
}
|
}
|
||||||
|
@ -430,7 +430,7 @@ void SocketCore::establishConnection(const std::string& host, uint16_t port)
|
||||||
void SocketCore::setSockOpt
|
void SocketCore::setSockOpt
|
||||||
(int level, int optname, void* optval, socklen_t optlen)
|
(int level, int optname, void* optval, socklen_t optlen)
|
||||||
{
|
{
|
||||||
if(setsockopt(sockfd, level, optname, (a2_sockopt_t)optval, optlen) < 0) {
|
if(setsockopt(_sockfd, level, optname, (a2_sockopt_t)optval, optlen) < 0) {
|
||||||
throw DL_ABORT_EX(StringFormat(EX_SOCKET_SET_OPT, errorMsg()).str());
|
throw DL_ABORT_EX(StringFormat(EX_SOCKET_SET_OPT, errorMsg()).str());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -488,60 +488,60 @@ void SocketCore::setNonBlockingMode()
|
||||||
{
|
{
|
||||||
#ifdef __MINGW32__
|
#ifdef __MINGW32__
|
||||||
static u_long flag = 1;
|
static u_long flag = 1;
|
||||||
if (::ioctlsocket(sockfd, FIONBIO, &flag) == -1) {
|
if (::ioctlsocket(_sockfd, FIONBIO, &flag) == -1) {
|
||||||
throw DL_ABORT_EX(StringFormat(EX_SOCKET_NONBLOCKING, errorMsg()).str());
|
throw DL_ABORT_EX(StringFormat(EX_SOCKET_NONBLOCKING, errorMsg()).str());
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
int flags;
|
int flags;
|
||||||
while((flags = fcntl(sockfd, F_GETFL, 0)) == -1 && errno == EINTR);
|
while((flags = fcntl(_sockfd, F_GETFL, 0)) == -1 && errno == EINTR);
|
||||||
// TODO add error handling
|
// TODO add error handling
|
||||||
while(fcntl(sockfd, F_SETFL, flags|O_NONBLOCK) == -1 && errno == EINTR);
|
while(fcntl(_sockfd, F_SETFL, flags|O_NONBLOCK) == -1 && errno == EINTR);
|
||||||
#endif // __MINGW32__
|
#endif // __MINGW32__
|
||||||
blocking = false;
|
_blocking = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void SocketCore::setBlockingMode()
|
void SocketCore::setBlockingMode()
|
||||||
{
|
{
|
||||||
#ifdef __MINGW32__
|
#ifdef __MINGW32__
|
||||||
static u_long flag = 0;
|
static u_long flag = 0;
|
||||||
if (::ioctlsocket(sockfd, FIONBIO, &flag) == -1) {
|
if (::ioctlsocket(_sockfd, FIONBIO, &flag) == -1) {
|
||||||
throw DL_ABORT_EX(StringFormat(EX_SOCKET_BLOCKING, errorMsg()).str());
|
throw DL_ABORT_EX(StringFormat(EX_SOCKET_BLOCKING, errorMsg()).str());
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
int flags;
|
int flags;
|
||||||
while((flags = fcntl(sockfd, F_GETFL, 0)) == -1 && errno == EINTR);
|
while((flags = fcntl(_sockfd, F_GETFL, 0)) == -1 && errno == EINTR);
|
||||||
// TODO add error handling
|
// TODO add error handling
|
||||||
while(fcntl(sockfd, F_SETFL, flags&(~O_NONBLOCK)) == -1 && errno == EINTR);
|
while(fcntl(_sockfd, F_SETFL, flags&(~O_NONBLOCK)) == -1 && errno == EINTR);
|
||||||
#endif // __MINGW32__
|
#endif // __MINGW32__
|
||||||
blocking = true;
|
_blocking = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void SocketCore::closeConnection()
|
void SocketCore::closeConnection()
|
||||||
{
|
{
|
||||||
#ifdef HAVE_LIBSSL
|
#ifdef HAVE_LIBSSL
|
||||||
// for SSL
|
// for SSL
|
||||||
if(secure) {
|
if(_secure) {
|
||||||
SSL_shutdown(ssl);
|
SSL_shutdown(ssl);
|
||||||
}
|
}
|
||||||
#endif // HAVE_LIBSSL
|
#endif // HAVE_LIBSSL
|
||||||
#ifdef HAVE_LIBGNUTLS
|
#ifdef HAVE_LIBGNUTLS
|
||||||
if(secure) {
|
if(_secure) {
|
||||||
gnutls_bye(sslSession, GNUTLS_SHUT_RDWR);
|
gnutls_bye(_sslSession, GNUTLS_SHUT_RDWR);
|
||||||
}
|
}
|
||||||
#endif // HAVE_LIBGNUTLS
|
#endif // HAVE_LIBGNUTLS
|
||||||
if(sockfd != (sock_t) -1) {
|
if(_sockfd != (sock_t) -1) {
|
||||||
CLOSE(sockfd);
|
CLOSE(_sockfd);
|
||||||
sockfd = -1;
|
_sockfd = -1;
|
||||||
}
|
}
|
||||||
#ifdef HAVE_LIBSSL
|
#ifdef HAVE_LIBSSL
|
||||||
// for SSL
|
// for SSL
|
||||||
if(secure) {
|
if(_secure) {
|
||||||
SSL_free(ssl);
|
SSL_free(ssl);
|
||||||
}
|
}
|
||||||
#endif // HAVE_LIBSSL
|
#endif // HAVE_LIBSSL
|
||||||
#ifdef HAVE_LIBGNUTLS
|
#ifdef HAVE_LIBGNUTLS
|
||||||
if(secure) {
|
if(_secure) {
|
||||||
gnutls_deinit(sslSession);
|
gnutls_deinit(_sslSession);
|
||||||
}
|
}
|
||||||
#endif // HAVE_LIBGNUTLS
|
#endif // HAVE_LIBGNUTLS
|
||||||
}
|
}
|
||||||
|
@ -559,7 +559,7 @@ bool SocketCore::isWritable(time_t timeout)
|
||||||
{
|
{
|
||||||
#ifdef HAVE_POLL
|
#ifdef HAVE_POLL
|
||||||
struct pollfd p;
|
struct pollfd p;
|
||||||
p.fd = sockfd;
|
p.fd = _sockfd;
|
||||||
p.events = POLLOUT;
|
p.events = POLLOUT;
|
||||||
int r;
|
int r;
|
||||||
while((r = poll(&p, 1, timeout*1000)) == -1 && errno == EINTR);
|
while((r = poll(&p, 1, timeout*1000)) == -1 && errno == EINTR);
|
||||||
|
@ -573,17 +573,17 @@ bool SocketCore::isWritable(time_t timeout)
|
||||||
}
|
}
|
||||||
#else // !HAVE_POLL
|
#else // !HAVE_POLL
|
||||||
# ifndef __MINGW32__
|
# ifndef __MINGW32__
|
||||||
CHECK_FD(sockfd);
|
CHECK_FD(_sockfd);
|
||||||
# endif // !__MINGW32__
|
# endif // !__MINGW32__
|
||||||
fd_set fds;
|
fd_set fds;
|
||||||
FD_ZERO(&fds);
|
FD_ZERO(&fds);
|
||||||
FD_SET(sockfd, &fds);
|
FD_SET(_sockfd, &fds);
|
||||||
|
|
||||||
struct timeval tv;
|
struct timeval tv;
|
||||||
tv.tv_sec = timeout;
|
tv.tv_sec = timeout;
|
||||||
tv.tv_usec = 0;
|
tv.tv_usec = 0;
|
||||||
|
|
||||||
int r = select(sockfd+1, NULL, &fds, NULL, &tv);
|
int r = select(_sockfd+1, NULL, &fds, NULL, &tv);
|
||||||
if(r == 1) {
|
if(r == 1) {
|
||||||
return true;
|
return true;
|
||||||
} else if(r == 0) {
|
} else if(r == 0) {
|
||||||
|
@ -603,13 +603,13 @@ bool SocketCore::isWritable(time_t timeout)
|
||||||
bool SocketCore::isReadable(time_t timeout)
|
bool SocketCore::isReadable(time_t timeout)
|
||||||
{
|
{
|
||||||
#ifdef HAVE_LIBGNUTLS
|
#ifdef HAVE_LIBGNUTLS
|
||||||
if(secure && peekBufLength > 0) {
|
if(_secure && _peekBufLength > 0) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
#endif // HAVE_LIBGNUTLS
|
#endif // HAVE_LIBGNUTLS
|
||||||
#ifdef HAVE_POLL
|
#ifdef HAVE_POLL
|
||||||
struct pollfd p;
|
struct pollfd p;
|
||||||
p.fd = sockfd;
|
p.fd = _sockfd;
|
||||||
p.events = POLLIN;
|
p.events = POLLIN;
|
||||||
int r;
|
int r;
|
||||||
while((r = poll(&p, 1, timeout*1000)) == -1 && errno == EINTR);
|
while((r = poll(&p, 1, timeout*1000)) == -1 && errno == EINTR);
|
||||||
|
@ -623,17 +623,17 @@ bool SocketCore::isReadable(time_t timeout)
|
||||||
}
|
}
|
||||||
#else // !HAVE_POLL
|
#else // !HAVE_POLL
|
||||||
# ifndef __MINGW32__
|
# ifndef __MINGW32__
|
||||||
CHECK_FD(sockfd);
|
CHECK_FD(_sockfd);
|
||||||
# endif // !__MINGW32__
|
# endif // !__MINGW32__
|
||||||
fd_set fds;
|
fd_set fds;
|
||||||
FD_ZERO(&fds);
|
FD_ZERO(&fds);
|
||||||
FD_SET(sockfd, &fds);
|
FD_SET(_sockfd, &fds);
|
||||||
|
|
||||||
struct timeval tv;
|
struct timeval tv;
|
||||||
tv.tv_sec = timeout;
|
tv.tv_sec = timeout;
|
||||||
tv.tv_usec = 0;
|
tv.tv_usec = 0;
|
||||||
|
|
||||||
int r = select(sockfd+1, &fds, NULL, NULL, &tv);
|
int r = select(_sockfd+1, &fds, NULL, NULL, &tv);
|
||||||
if(r == 1) {
|
if(r == 1) {
|
||||||
return true;
|
return true;
|
||||||
} else if(r == 0) {
|
} else if(r == 0) {
|
||||||
|
@ -669,7 +669,7 @@ int SocketCore::sslHandleEAGAIN(int ret)
|
||||||
#ifdef HAVE_LIBGNUTLS
|
#ifdef HAVE_LIBGNUTLS
|
||||||
void SocketCore::gnutlsRecordCheckDirection()
|
void SocketCore::gnutlsRecordCheckDirection()
|
||||||
{
|
{
|
||||||
int direction = gnutls_record_get_direction(sslSession);
|
int direction = gnutls_record_get_direction(_sslSession);
|
||||||
if(direction == 0) {
|
if(direction == 0) {
|
||||||
_wantRead = true;
|
_wantRead = true;
|
||||||
} else { // if(direction == 1) {
|
} else { // if(direction == 1) {
|
||||||
|
@ -684,8 +684,8 @@ ssize_t SocketCore::writeData(const char* data, size_t len)
|
||||||
_wantRead = false;
|
_wantRead = false;
|
||||||
_wantWrite = false;
|
_wantWrite = false;
|
||||||
|
|
||||||
if(!secure) {
|
if(!_secure) {
|
||||||
while((ret = send(sockfd, data, len, 0)) == -1 && SOCKET_ERRNO == A2_EINTR);
|
while((ret = send(_sockfd, data, len, 0)) == -1 && SOCKET_ERRNO == A2_EINTR);
|
||||||
if(ret == -1) {
|
if(ret == -1) {
|
||||||
if(A2_WOULDBLOCK(SOCKET_ERRNO)) {
|
if(A2_WOULDBLOCK(SOCKET_ERRNO)) {
|
||||||
_wantWrite = true;
|
_wantWrite = true;
|
||||||
|
@ -707,7 +707,7 @@ ssize_t SocketCore::writeData(const char* data, size_t len)
|
||||||
}
|
}
|
||||||
#endif // HAVE_LIBSSL
|
#endif // HAVE_LIBSSL
|
||||||
#ifdef HAVE_LIBGNUTLS
|
#ifdef HAVE_LIBGNUTLS
|
||||||
while((ret = gnutls_record_send(sslSession, data, len)) ==
|
while((ret = gnutls_record_send(_sslSession, data, len)) ==
|
||||||
GNUTLS_E_INTERRUPTED);
|
GNUTLS_E_INTERRUPTED);
|
||||||
if(ret == GNUTLS_E_AGAIN) {
|
if(ret == GNUTLS_E_AGAIN) {
|
||||||
gnutlsRecordCheckDirection();
|
gnutlsRecordCheckDirection();
|
||||||
|
@ -727,8 +727,8 @@ void SocketCore::readData(char* data, size_t& len)
|
||||||
_wantRead = false;
|
_wantRead = false;
|
||||||
_wantWrite = false;
|
_wantWrite = false;
|
||||||
|
|
||||||
if(!secure) {
|
if(!_secure) {
|
||||||
while((ret = recv(sockfd, data, len, 0)) == -1 && SOCKET_ERRNO == A2_EINTR);
|
while((ret = recv(_sockfd, data, len, 0)) == -1 && SOCKET_ERRNO == A2_EINTR);
|
||||||
|
|
||||||
if(ret == -1) {
|
if(ret == -1) {
|
||||||
if(A2_WOULDBLOCK(SOCKET_ERRNO)) {
|
if(A2_WOULDBLOCK(SOCKET_ERRNO)) {
|
||||||
|
@ -773,8 +773,8 @@ void SocketCore::peekData(char* data, size_t& len)
|
||||||
_wantRead = false;
|
_wantRead = false;
|
||||||
_wantWrite = false;
|
_wantWrite = false;
|
||||||
|
|
||||||
if(!secure) {
|
if(!_secure) {
|
||||||
while((ret = recv(sockfd, data, len, MSG_PEEK)) == -1 &&
|
while((ret = recv(_sockfd, data, len, MSG_PEEK)) == -1 &&
|
||||||
SOCKET_ERRNO == A2_EINTR);
|
SOCKET_ERRNO == A2_EINTR);
|
||||||
if(ret == -1) {
|
if(ret == -1) {
|
||||||
if(A2_WOULDBLOCK(SOCKET_ERRNO)) {
|
if(A2_WOULDBLOCK(SOCKET_ERRNO)) {
|
||||||
|
@ -816,18 +816,15 @@ void SocketCore::peekData(char* data, size_t& len)
|
||||||
#ifdef HAVE_LIBGNUTLS
|
#ifdef HAVE_LIBGNUTLS
|
||||||
size_t SocketCore::shiftPeekData(char* data, size_t len)
|
size_t SocketCore::shiftPeekData(char* data, size_t len)
|
||||||
{
|
{
|
||||||
if(peekBufLength <= len) {
|
if(_peekBufLength <= len) {
|
||||||
memcpy(data, peekBuf, peekBufLength);
|
memcpy(data, _peekBuf, _peekBufLength);
|
||||||
size_t ret = peekBufLength;
|
size_t ret = _peekBufLength;
|
||||||
peekBufLength = 0;
|
_peekBufLength = 0;
|
||||||
return ret;
|
return ret;
|
||||||
} else {
|
} else {
|
||||||
memcpy(data, peekBuf, len);
|
memcpy(data, _peekBuf, len);
|
||||||
char* temp = new char[peekBufMax];
|
_peekBufLength -= len;
|
||||||
memcpy(temp, peekBuf+len, peekBufLength-len);
|
memmove(_peekBuf, _peekBuf+len, _peekBufLength);
|
||||||
delete [] peekBuf;
|
|
||||||
peekBuf = temp;
|
|
||||||
peekBufLength -= len;
|
|
||||||
return len;
|
return len;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -835,15 +832,15 @@ size_t SocketCore::shiftPeekData(char* data, size_t len)
|
||||||
|
|
||||||
void SocketCore::addPeekData(char* data, size_t len)
|
void SocketCore::addPeekData(char* data, size_t len)
|
||||||
{
|
{
|
||||||
if(peekBufLength+len > peekBufMax) {
|
if(_peekBufLength+len > _peekBufMax) {
|
||||||
char* temp = new char[peekBufMax+len];
|
char* temp = new char[_peekBufMax+len];
|
||||||
memcpy(temp, peekBuf, peekBufLength);
|
memcpy(temp, _peekBuf, _peekBufLength);
|
||||||
delete [] peekBuf;
|
delete [] _peekBuf;
|
||||||
peekBuf = temp;
|
_peekBuf = temp;
|
||||||
peekBufMax = peekBufLength+len;
|
_peekBufMax = _peekBufLength+len;
|
||||||
}
|
}
|
||||||
memcpy(peekBuf+peekBufLength, data, len);
|
memcpy(_peekBuf+_peekBufLength, data, len);
|
||||||
peekBufLength += len;
|
_peekBufLength += len;
|
||||||
}
|
}
|
||||||
|
|
||||||
static ssize_t GNUTLS_RECORD_RECV_NO_INTERRUPT
|
static ssize_t GNUTLS_RECORD_RECV_NO_INTERRUPT
|
||||||
|
@ -864,7 +861,7 @@ ssize_t SocketCore::gnutlsRecv(char* data, size_t len)
|
||||||
size_t plen = shiftPeekData(data, len);
|
size_t plen = shiftPeekData(data, len);
|
||||||
if(plen < len) {
|
if(plen < len) {
|
||||||
ssize_t ret = GNUTLS_RECORD_RECV_NO_INTERRUPT
|
ssize_t ret = GNUTLS_RECORD_RECV_NO_INTERRUPT
|
||||||
(sslSession, data+plen, len-plen);
|
(_sslSession, data+plen, len-plen);
|
||||||
if(ret == GNUTLS_E_AGAIN) {
|
if(ret == GNUTLS_E_AGAIN) {
|
||||||
return GNUTLS_E_AGAIN;
|
return GNUTLS_E_AGAIN;
|
||||||
}
|
}
|
||||||
|
@ -876,25 +873,25 @@ ssize_t SocketCore::gnutlsRecv(char* data, size_t len)
|
||||||
|
|
||||||
ssize_t SocketCore::gnutlsPeek(char* data, size_t len)
|
ssize_t SocketCore::gnutlsPeek(char* data, size_t len)
|
||||||
{
|
{
|
||||||
if(peekBufLength >= len) {
|
if(_peekBufLength >= len) {
|
||||||
memcpy(data, peekBuf, len);
|
memcpy(data, _peekBuf, len);
|
||||||
return len;
|
return len;
|
||||||
} else {
|
} else {
|
||||||
memcpy(data, peekBuf, peekBufLength);
|
memcpy(data, _peekBuf, _peekBufLength);
|
||||||
ssize_t ret = GNUTLS_RECORD_RECV_NO_INTERRUPT
|
ssize_t ret = GNUTLS_RECORD_RECV_NO_INTERRUPT
|
||||||
(sslSession, data+peekBufLength, len-peekBufLength);
|
(_sslSession, data+_peekBufLength, len-_peekBufLength);
|
||||||
if(ret == GNUTLS_E_AGAIN) {
|
if(ret == GNUTLS_E_AGAIN) {
|
||||||
return GNUTLS_E_AGAIN;
|
return GNUTLS_E_AGAIN;
|
||||||
}
|
}
|
||||||
addPeekData(data+peekBufLength, ret);
|
addPeekData(data+_peekBufLength, ret);
|
||||||
return peekBufLength;
|
return _peekBufLength;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif // HAVE_LIBGNUTLS
|
#endif // HAVE_LIBGNUTLS
|
||||||
|
|
||||||
void SocketCore::prepareSecureConnection()
|
void SocketCore::prepareSecureConnection()
|
||||||
{
|
{
|
||||||
if(!secure) {
|
if(!_secure) {
|
||||||
#ifdef HAVE_LIBSSL
|
#ifdef HAVE_LIBSSL
|
||||||
// for SSL
|
// for SSL
|
||||||
ssl = SSL_new(_tlsContext->getSSLCtx());
|
ssl = SSL_new(_tlsContext->getSSLCtx());
|
||||||
|
@ -903,7 +900,7 @@ void SocketCore::prepareSecureConnection()
|
||||||
(StringFormat(EX_SSL_INIT_FAILURE,
|
(StringFormat(EX_SSL_INIT_FAILURE,
|
||||||
ERR_error_string(ERR_get_error(), 0)).str());
|
ERR_error_string(ERR_get_error(), 0)).str());
|
||||||
}
|
}
|
||||||
if(SSL_set_fd(ssl, sockfd) == 0) {
|
if(SSL_set_fd(ssl, _sockfd) == 0) {
|
||||||
throw DL_ABORT_EX
|
throw DL_ABORT_EX
|
||||||
(StringFormat(EX_SSL_INIT_FAILURE,
|
(StringFormat(EX_SSL_INIT_FAILURE,
|
||||||
ERR_error_string(ERR_get_error(), 0)).str());
|
ERR_error_string(ERR_get_error(), 0)).str());
|
||||||
|
@ -911,29 +908,29 @@ void SocketCore::prepareSecureConnection()
|
||||||
#endif // HAVE_LIBSSL
|
#endif // HAVE_LIBSSL
|
||||||
#ifdef HAVE_LIBGNUTLS
|
#ifdef HAVE_LIBGNUTLS
|
||||||
int r;
|
int r;
|
||||||
gnutls_init(&sslSession, GNUTLS_CLIENT);
|
gnutls_init(&_sslSession, GNUTLS_CLIENT);
|
||||||
// It seems err is not error message, but the argument string
|
// It seems err is not error message, but the argument string
|
||||||
// which causes syntax error.
|
// which causes syntax error.
|
||||||
const char* err;
|
const char* err;
|
||||||
// Disables TLS1.1 here because there are servers that don't
|
// Disables TLS1.1 here because there are servers that don't
|
||||||
// understand TLS1.1.
|
// understand TLS1.1.
|
||||||
r = gnutls_priority_set_direct(sslSession, "NORMAL:!VERS-TLS1.1", &err);
|
r = gnutls_priority_set_direct(_sslSession, "NORMAL:!VERS-TLS1.1", &err);
|
||||||
if(r != GNUTLS_E_SUCCESS) {
|
if(r != GNUTLS_E_SUCCESS) {
|
||||||
throw DL_ABORT_EX
|
throw DL_ABORT_EX
|
||||||
(StringFormat(EX_SSL_INIT_FAILURE, gnutls_strerror(r)).str());
|
(StringFormat(EX_SSL_INIT_FAILURE, gnutls_strerror(r)).str());
|
||||||
}
|
}
|
||||||
// put the x509 credentials to the current session
|
// put the x509 credentials to the current session
|
||||||
gnutls_credentials_set(sslSession, GNUTLS_CRD_CERTIFICATE,
|
gnutls_credentials_set(_sslSession, GNUTLS_CRD_CERTIFICATE,
|
||||||
_tlsContext->getCertCred());
|
_tlsContext->getCertCred());
|
||||||
gnutls_transport_set_ptr(sslSession, (gnutls_transport_ptr_t)sockfd);
|
gnutls_transport_set_ptr(_sslSession, (gnutls_transport_ptr_t)_sockfd);
|
||||||
#endif // HAVE_LIBGNUTLS
|
#endif // HAVE_LIBGNUTLS
|
||||||
secure = 1;
|
_secure = 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool SocketCore::initiateSecureConnection(const std::string& hostname)
|
bool SocketCore::initiateSecureConnection(const std::string& hostname)
|
||||||
{
|
{
|
||||||
if(secure == 1) {
|
if(_secure == 1) {
|
||||||
_wantRead = false;
|
_wantRead = false;
|
||||||
_wantWrite = false;
|
_wantWrite = false;
|
||||||
#ifdef HAVE_LIBSSL
|
#ifdef HAVE_LIBSSL
|
||||||
|
@ -953,7 +950,7 @@ bool SocketCore::initiateSecureConnection(const std::string& hostname)
|
||||||
return false;
|
return false;
|
||||||
case SSL_ERROR_WANT_X509_LOOKUP:
|
case SSL_ERROR_WANT_X509_LOOKUP:
|
||||||
case SSL_ERROR_ZERO_RETURN:
|
case SSL_ERROR_ZERO_RETURN:
|
||||||
if (blocking) {
|
if (_blocking) {
|
||||||
throw DL_ABORT_EX
|
throw DL_ABORT_EX
|
||||||
(StringFormat(EX_SSL_CONNECT_ERROR, ssl_error).str());
|
(StringFormat(EX_SSL_CONNECT_ERROR, ssl_error).str());
|
||||||
}
|
}
|
||||||
|
@ -1015,7 +1012,7 @@ bool SocketCore::initiateSecureConnection(const std::string& hostname)
|
||||||
}
|
}
|
||||||
#endif // HAVE_LIBSSL
|
#endif // HAVE_LIBSSL
|
||||||
#ifdef HAVE_LIBGNUTLS
|
#ifdef HAVE_LIBGNUTLS
|
||||||
int ret = gnutls_handshake(sslSession);
|
int ret = gnutls_handshake(_sslSession);
|
||||||
if(ret == GNUTLS_E_AGAIN) {
|
if(ret == GNUTLS_E_AGAIN) {
|
||||||
gnutlsRecordCheckDirection();
|
gnutlsRecordCheckDirection();
|
||||||
return false;
|
return false;
|
||||||
|
@ -1027,7 +1024,7 @@ bool SocketCore::initiateSecureConnection(const std::string& hostname)
|
||||||
if(_tlsContext->peerVerificationEnabled()) {
|
if(_tlsContext->peerVerificationEnabled()) {
|
||||||
// verify peer
|
// verify peer
|
||||||
unsigned int status;
|
unsigned int status;
|
||||||
ret = gnutls_certificate_verify_peers2(sslSession, &status);
|
ret = gnutls_certificate_verify_peers2(_sslSession, &status);
|
||||||
if(ret < 0) {
|
if(ret < 0) {
|
||||||
throw DL_ABORT_EX
|
throw DL_ABORT_EX
|
||||||
(StringFormat("gnutls_certificate_verify_peer2() failed. Cause: %s",
|
(StringFormat("gnutls_certificate_verify_peer2() failed. Cause: %s",
|
||||||
|
@ -1050,13 +1047,13 @@ bool SocketCore::initiateSecureConnection(const std::string& hostname)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// certificate type: only X509 is allowed.
|
// certificate type: only X509 is allowed.
|
||||||
if(gnutls_certificate_type_get(sslSession) != GNUTLS_CRT_X509) {
|
if(gnutls_certificate_type_get(_sslSession) != GNUTLS_CRT_X509) {
|
||||||
throw DL_ABORT_EX("Certificate type is not X509.");
|
throw DL_ABORT_EX("Certificate type is not X509.");
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned int peerCertsLength;
|
unsigned int peerCertsLength;
|
||||||
const gnutls_datum_t* peerCerts = gnutls_certificate_get_peers
|
const gnutls_datum_t* peerCerts = gnutls_certificate_get_peers
|
||||||
(sslSession, &peerCertsLength);
|
(_sslSession, &peerCertsLength);
|
||||||
if(!peerCerts) {
|
if(!peerCerts) {
|
||||||
throw DL_ABORT_EX(MSG_NO_CERT_FOUND);
|
throw DL_ABORT_EX(MSG_NO_CERT_FOUND);
|
||||||
}
|
}
|
||||||
|
@ -1098,9 +1095,9 @@ bool SocketCore::initiateSecureConnection(const std::string& hostname)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
peekBuf = new char[peekBufMax];
|
_peekBuf = new char[_peekBufMax];
|
||||||
#endif // HAVE_LIBGNUTLS
|
#endif // HAVE_LIBGNUTLS
|
||||||
secure = 2;
|
_secure = 2;
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
return true;
|
return true;
|
||||||
|
@ -1124,7 +1121,7 @@ ssize_t SocketCore::writeData(const char* data, size_t len,
|
||||||
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) {
|
||||||
while((r = sendto(sockfd, data, len, 0, rp->ai_addr, rp->ai_addrlen)) == -1
|
while((r = sendto(_sockfd, data, len, 0, rp->ai_addr, rp->ai_addrlen)) == -1
|
||||||
&& A2_EINTR == SOCKET_ERRNO);
|
&& A2_EINTR == SOCKET_ERRNO);
|
||||||
if(r == static_cast<ssize_t>(len)) {
|
if(r == static_cast<ssize_t>(len)) {
|
||||||
break;
|
break;
|
||||||
|
@ -1151,7 +1148,7 @@ ssize_t SocketCore::readDataFrom(char* data, size_t len,
|
||||||
socklen_t sockaddrlen = sizeof(struct sockaddr_storage);
|
socklen_t sockaddrlen = sizeof(struct sockaddr_storage);
|
||||||
struct sockaddr* addrp = reinterpret_cast<struct sockaddr*>(&sockaddr);
|
struct sockaddr* addrp = reinterpret_cast<struct sockaddr*>(&sockaddr);
|
||||||
ssize_t r;
|
ssize_t r;
|
||||||
while((r = recvfrom(sockfd, data, len, 0, addrp, &sockaddrlen)) == -1 &&
|
while((r = recvfrom(_sockfd, data, len, 0, addrp, &sockaddrlen)) == -1 &&
|
||||||
A2_EINTR == SOCKET_ERRNO);
|
A2_EINTR == SOCKET_ERRNO);
|
||||||
if(r == -1) {
|
if(r == -1) {
|
||||||
if(A2_WOULDBLOCK(SOCKET_ERRNO)) {
|
if(A2_WOULDBLOCK(SOCKET_ERRNO)) {
|
||||||
|
@ -1172,7 +1169,7 @@ std::string SocketCore::getSocketError() const
|
||||||
int error;
|
int error;
|
||||||
socklen_t optlen = sizeof(error);
|
socklen_t optlen = sizeof(error);
|
||||||
|
|
||||||
if(getsockopt(sockfd, SOL_SOCKET, SO_ERROR, (a2_sockopt_t) &error, &optlen) == -1) {
|
if(getsockopt(_sockfd, SOL_SOCKET, SO_ERROR, (a2_sockopt_t) &error, &optlen) == -1) {
|
||||||
throw DL_ABORT_EX(StringFormat("Failed to get socket error: %s",
|
throw DL_ABORT_EX(StringFormat("Failed to get socket error: %s",
|
||||||
errorMsg()).str());
|
errorMsg()).str());
|
||||||
}
|
}
|
||||||
|
|
|
@ -72,14 +72,14 @@ private:
|
||||||
// socket type defined in <sys/socket.h>
|
// socket type defined in <sys/socket.h>
|
||||||
int _sockType;
|
int _sockType;
|
||||||
// socket endpoint descriptor
|
// socket endpoint descriptor
|
||||||
sock_t sockfd;
|
sock_t _sockfd;
|
||||||
|
|
||||||
static int _protocolFamily;
|
static int _protocolFamily;
|
||||||
|
|
||||||
static std::vector<std::pair<struct sockaddr_storage, socklen_t> > _bindAddrs;
|
static std::vector<std::pair<struct sockaddr_storage, socklen_t> > _bindAddrs;
|
||||||
|
|
||||||
bool blocking;
|
bool _blocking;
|
||||||
int secure;
|
int _secure;
|
||||||
|
|
||||||
bool _wantRead;
|
bool _wantRead;
|
||||||
bool _wantWrite;
|
bool _wantWrite;
|
||||||
|
@ -95,10 +95,10 @@ private:
|
||||||
int sslHandleEAGAIN(int ret);
|
int sslHandleEAGAIN(int ret);
|
||||||
#endif // HAVE_LIBSSL
|
#endif // HAVE_LIBSSL
|
||||||
#ifdef HAVE_LIBGNUTLS
|
#ifdef HAVE_LIBGNUTLS
|
||||||
gnutls_session_t sslSession;
|
gnutls_session_t _sslSession;
|
||||||
char* peekBuf;
|
char* _peekBuf;
|
||||||
size_t peekBufLength;
|
size_t _peekBufLength;
|
||||||
size_t peekBufMax;
|
size_t _peekBufMax;
|
||||||
|
|
||||||
size_t shiftPeekData(char* data, size_t len);
|
size_t shiftPeekData(char* data, size_t len);
|
||||||
void addPeekData(char* data, size_t len);
|
void addPeekData(char* data, size_t len);
|
||||||
|
@ -119,9 +119,9 @@ public:
|
||||||
SocketCore(int sockType = SOCK_STREAM);
|
SocketCore(int sockType = SOCK_STREAM);
|
||||||
~SocketCore();
|
~SocketCore();
|
||||||
|
|
||||||
sock_t getSockfd() const { return sockfd; }
|
sock_t getSockfd() const { return _sockfd; }
|
||||||
|
|
||||||
bool isOpen() const { return sockfd != (sock_t) -1; }
|
bool isOpen() const { return _sockfd != (sock_t) -1; }
|
||||||
|
|
||||||
void setMulticastInterface(const std::string& localAddr);
|
void setMulticastInterface(const std::string& localAddr);
|
||||||
|
|
||||||
|
@ -306,7 +306,7 @@ public:
|
||||||
void prepareSecureConnection();
|
void prepareSecureConnection();
|
||||||
|
|
||||||
bool operator==(const SocketCore& s) {
|
bool operator==(const SocketCore& s) {
|
||||||
return sockfd == s.sockfd;
|
return _sockfd == s._sockfd;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool operator!=(const SocketCore& s) {
|
bool operator!=(const SocketCore& s) {
|
||||||
|
@ -314,7 +314,7 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
bool operator<(const SocketCore& s) {
|
bool operator<(const SocketCore& s) {
|
||||||
return sockfd < s.sockfd;
|
return _sockfd < s._sockfd;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string getSocketError() const;
|
std::string getSocketError() const;
|
||||||
|
|
Loading…
Reference in New Issue