2003-04-22 22:16:22 +08:00
|
|
|
//
|
|
|
|
// ZoneMinder Communications Class Implementation, $Date$, $Revision$
|
2008-07-25 17:33:23 +08:00
|
|
|
// Copyright (C) 2001-2008 Philip Coombes
|
2003-04-22 22:16:22 +08:00
|
|
|
//
|
|
|
|
// This program is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU General Public License
|
|
|
|
// as published by the Free Software Foundation; either version 2
|
|
|
|
// of the License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program; if not, write to the Free Software
|
|
|
|
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
//
|
|
|
|
|
2008-07-16 16:35:59 +08:00
|
|
|
#include "zm_comms.h"
|
|
|
|
|
2011-06-21 17:19:10 +08:00
|
|
|
#include "zm_logger.h"
|
2008-07-16 16:35:59 +08:00
|
|
|
|
2003-04-22 22:16:22 +08:00
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdarg.h>
|
2008-07-16 16:35:59 +08:00
|
|
|
//#include <memory.h>
|
2015-01-03 06:08:51 +08:00
|
|
|
#if defined(BSD)
|
|
|
|
#include <stdlib.h>
|
|
|
|
#else
|
2003-04-22 22:16:22 +08:00
|
|
|
#include <alloca.h>
|
2015-01-03 06:08:51 +08:00
|
|
|
#endif
|
2015-01-02 06:18:12 +08:00
|
|
|
|
2008-07-16 16:35:59 +08:00
|
|
|
//#include <unistd.h>
|
2003-04-22 22:16:22 +08:00
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <netinet/tcp.h>
|
|
|
|
|
2015-05-18 08:18:54 +08:00
|
|
|
#ifdef SOLARIS
|
|
|
|
#include <sys/filio.h> // define FIONREAD
|
|
|
|
#endif
|
|
|
|
|
2008-07-16 16:35:59 +08:00
|
|
|
int CommsBase::readV( int iovcnt, /* const void *, int, */ ... )
|
2003-04-22 22:16:22 +08:00
|
|
|
{
|
|
|
|
va_list arg_ptr;
|
|
|
|
//struct iovec iov[iovcnt];
|
|
|
|
struct iovec *iov = (struct iovec *)alloca( sizeof(struct iovec)*iovcnt );
|
|
|
|
|
|
|
|
va_start( arg_ptr, iovcnt );
|
|
|
|
for ( int i = 0; i < iovcnt; i++ )
|
|
|
|
{
|
|
|
|
iov[i].iov_base = va_arg( arg_ptr, void * );
|
|
|
|
iov[i].iov_len = va_arg( arg_ptr, int );
|
|
|
|
}
|
|
|
|
va_end( arg_ptr );
|
|
|
|
|
2008-07-16 16:35:59 +08:00
|
|
|
int nBytes = ::readv( mRd, iov, iovcnt );
|
|
|
|
if ( nBytes < 0 )
|
|
|
|
Debug( 1, "Readv of %d buffers max on rd %d failed: %s", iovcnt, mRd, strerror(errno) );
|
|
|
|
return( nBytes );
|
2003-04-22 22:16:22 +08:00
|
|
|
}
|
|
|
|
|
2008-07-16 16:35:59 +08:00
|
|
|
int CommsBase::writeV( int iovcnt, /* const void *, int, */ ... )
|
2003-04-22 22:16:22 +08:00
|
|
|
{
|
|
|
|
va_list arg_ptr;
|
|
|
|
//struct iovec iov[iovcnt];
|
|
|
|
struct iovec *iov = (struct iovec *)alloca( sizeof(struct iovec)*iovcnt );
|
|
|
|
|
|
|
|
va_start( arg_ptr, iovcnt );
|
|
|
|
for ( int i = 0; i < iovcnt; i++ )
|
|
|
|
{
|
|
|
|
iov[i].iov_base = va_arg( arg_ptr, void * );
|
|
|
|
iov[i].iov_len = va_arg( arg_ptr, int );
|
|
|
|
}
|
|
|
|
va_end( arg_ptr );
|
|
|
|
|
2008-07-16 16:35:59 +08:00
|
|
|
ssize_t nBytes = ::writev( mWd, iov, iovcnt );
|
|
|
|
if ( nBytes < 0 )
|
|
|
|
Debug( 1, "Writev of %d buffers on wd %d failed: %s", iovcnt, mWd, strerror(errno) );
|
|
|
|
return( nBytes );
|
2003-04-22 22:16:22 +08:00
|
|
|
}
|
|
|
|
|
2008-07-16 16:35:59 +08:00
|
|
|
bool Pipe::open()
|
2003-04-22 22:16:22 +08:00
|
|
|
{
|
2008-07-16 16:35:59 +08:00
|
|
|
if ( ::pipe( mFd ) < 0 )
|
2003-04-22 22:16:22 +08:00
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Error( "pipe(), errno = %d, error = %s", errno, strerror(errno) );
|
2003-04-22 22:16:22 +08:00
|
|
|
return( false );
|
|
|
|
}
|
|
|
|
|
|
|
|
return( true );
|
|
|
|
}
|
|
|
|
|
2008-07-16 16:35:59 +08:00
|
|
|
bool Pipe::close()
|
2003-04-22 22:16:22 +08:00
|
|
|
{
|
2008-07-16 16:35:59 +08:00
|
|
|
if ( mFd[0] > -1 ) ::close( mFd[0] );
|
|
|
|
mFd[0] = -1;
|
|
|
|
if ( mFd[1] > -1 ) ::close( mFd[1] );
|
|
|
|
mFd[1] = -1;
|
2003-04-22 22:16:22 +08:00
|
|
|
return( true );
|
|
|
|
}
|
|
|
|
|
2008-07-16 16:35:59 +08:00
|
|
|
bool Pipe::setBlocking( bool blocking )
|
2003-04-22 22:16:22 +08:00
|
|
|
{
|
|
|
|
int flags;
|
|
|
|
|
|
|
|
/* Now set it for non-blocking I/O */
|
2008-07-16 16:35:59 +08:00
|
|
|
if ( (flags = fcntl( mFd[1], F_GETFL )) < 0 )
|
2003-04-22 22:16:22 +08:00
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Error( "fcntl(), errno = %d, error = %s", errno, strerror(errno) );
|
2003-04-22 22:16:22 +08:00
|
|
|
return( false );
|
|
|
|
}
|
|
|
|
if ( blocking )
|
|
|
|
{
|
|
|
|
flags &= ~O_NONBLOCK;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
flags |= O_NONBLOCK;
|
|
|
|
}
|
2008-07-16 16:35:59 +08:00
|
|
|
if ( fcntl( mFd[1], F_SETFL, flags ) < 0 )
|
2003-04-22 22:16:22 +08:00
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Error( "fcntl(), errno = %d, error = %s", errno, strerror(errno) );
|
2003-04-22 22:16:22 +08:00
|
|
|
return( false );
|
|
|
|
}
|
|
|
|
|
|
|
|
return( true );
|
|
|
|
}
|
|
|
|
|
2008-07-16 16:35:59 +08:00
|
|
|
SockAddr::SockAddr( const struct sockaddr *addr ) : mAddr( addr )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
SockAddr *SockAddr::newSockAddr( const struct sockaddr &addr, socklen_t len )
|
|
|
|
{
|
|
|
|
if ( addr.sa_family == AF_INET && len == SockAddrInet::addrSize() )
|
|
|
|
{
|
|
|
|
return( new SockAddrInet( (const struct sockaddr_in *)&addr ) );
|
|
|
|
}
|
|
|
|
else if ( addr.sa_family == AF_UNIX && len == SockAddrUnix::addrSize() )
|
|
|
|
{
|
|
|
|
return( new SockAddrUnix( (const struct sockaddr_un *)&addr ) );
|
|
|
|
}
|
|
|
|
Error( "Unable to create new SockAddr from addr family %d with size %d", addr.sa_family, len );
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
SockAddr *SockAddr::newSockAddr( const SockAddr *addr )
|
|
|
|
{
|
|
|
|
if ( !addr )
|
|
|
|
return( 0 );
|
|
|
|
|
|
|
|
if ( addr->getDomain() == AF_INET )
|
|
|
|
{
|
|
|
|
return( new SockAddrInet( *(SockAddrInet *)addr ) );
|
|
|
|
}
|
|
|
|
else if ( addr->getDomain() == AF_UNIX )
|
|
|
|
{
|
|
|
|
return( new SockAddrUnix( *(SockAddrUnix *)addr ) );
|
|
|
|
}
|
|
|
|
Error( "Unable to create new SockAddr from addr family %d", addr->getDomain() );
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
SockAddrInet::SockAddrInet() : SockAddr( (struct sockaddr *)&mAddrIn )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SockAddrInet::resolve( const char *host, const char *serv, const char *proto )
|
2003-04-22 22:16:22 +08:00
|
|
|
{
|
2008-07-16 16:35:59 +08:00
|
|
|
memset( &mAddrIn, 0, sizeof(mAddrIn) );
|
|
|
|
|
|
|
|
struct hostent *hostent=0;
|
|
|
|
if ( !(hostent = ::gethostbyname( host ) ) )
|
|
|
|
{
|
|
|
|
Error( "gethostbyname( %s ), h_errno = %d", host, h_errno );
|
|
|
|
return( false );
|
|
|
|
}
|
|
|
|
|
|
|
|
struct servent *servent=0;
|
|
|
|
if ( !(servent = ::getservbyname( serv, proto ) ) )
|
|
|
|
{
|
|
|
|
Error( "getservbyname( %s ), errno = %d, error = %s", serv, errno, strerror(errno) );
|
|
|
|
return( false );
|
|
|
|
}
|
|
|
|
|
|
|
|
mAddrIn.sin_port = servent->s_port;
|
|
|
|
mAddrIn.sin_family = AF_INET;
|
|
|
|
mAddrIn.sin_addr.s_addr = ((struct in_addr *)(hostent->h_addr))->s_addr;
|
|
|
|
|
|
|
|
return( true );
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SockAddrInet::resolve( const char *host, int port, const char *proto )
|
|
|
|
{
|
|
|
|
memset( &mAddrIn, 0, sizeof(mAddrIn) );
|
|
|
|
|
|
|
|
struct hostent *hostent=0;
|
|
|
|
if ( !(hostent = ::gethostbyname( host ) ) )
|
|
|
|
{
|
|
|
|
Error( "gethostbyname( %s ), h_errno = %d", host, h_errno );
|
|
|
|
return( false );
|
|
|
|
}
|
|
|
|
|
|
|
|
mAddrIn.sin_port = htons(port);
|
|
|
|
mAddrIn.sin_family = AF_INET;
|
|
|
|
mAddrIn.sin_addr.s_addr = ((struct in_addr *)(hostent->h_addr))->s_addr;
|
|
|
|
return( true );
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SockAddrInet::resolve( const char *serv, const char *proto )
|
|
|
|
{
|
|
|
|
memset( &mAddrIn, 0, sizeof(mAddrIn) );
|
|
|
|
|
|
|
|
struct servent *servent=0;
|
|
|
|
if ( !(servent = ::getservbyname( serv, proto ) ) )
|
|
|
|
{
|
|
|
|
Error( "getservbyname( %s ), errno = %d, error = %s", serv, errno, strerror(errno) );
|
|
|
|
return( false );
|
|
|
|
}
|
|
|
|
|
|
|
|
mAddrIn.sin_port = servent->s_port;
|
|
|
|
mAddrIn.sin_family = AF_INET;
|
|
|
|
mAddrIn.sin_addr.s_addr = INADDR_ANY;
|
|
|
|
|
|
|
|
return( true );
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SockAddrInet::resolve( int port, const char *proto )
|
|
|
|
{
|
|
|
|
memset( &mAddrIn, 0, sizeof(mAddrIn) );
|
|
|
|
|
|
|
|
mAddrIn.sin_port = htons(port);
|
|
|
|
mAddrIn.sin_family = AF_INET;
|
|
|
|
mAddrIn.sin_addr.s_addr = INADDR_ANY;
|
|
|
|
|
|
|
|
return( true );
|
|
|
|
}
|
|
|
|
|
|
|
|
SockAddrUnix::SockAddrUnix() : SockAddr( (struct sockaddr *)&mAddrUn )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SockAddrUnix::resolve( const char *path, const char *proto )
|
|
|
|
{
|
|
|
|
memset( &mAddrUn, 0, sizeof(mAddrUn) );
|
|
|
|
|
|
|
|
strncpy( mAddrUn.sun_path, path, sizeof(mAddrUn.sun_path) );
|
|
|
|
mAddrUn.sun_family = AF_UNIX;
|
|
|
|
|
|
|
|
return( true );
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Socket::socket()
|
|
|
|
{
|
|
|
|
if ( mSd >= 0 )
|
|
|
|
return( true );
|
|
|
|
|
|
|
|
if ( (mSd = ::socket( getDomain(), getType(), 0 ) ) < 0 )
|
2003-04-22 22:16:22 +08:00
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Error( "socket(), errno = %d, error = %s", errno, strerror(errno) );
|
2003-04-22 22:16:22 +08:00
|
|
|
return( false );
|
|
|
|
}
|
|
|
|
|
|
|
|
int val = 1;
|
|
|
|
|
2008-07-16 16:35:59 +08:00
|
|
|
(void)::setsockopt( mSd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val) );
|
|
|
|
(void)::setsockopt( mSd, SOL_SOCKET, SO_KEEPALIVE, &val, sizeof(val) );
|
|
|
|
|
|
|
|
mState = DISCONNECTED;
|
2003-04-22 22:16:22 +08:00
|
|
|
|
|
|
|
return( true );
|
|
|
|
}
|
|
|
|
|
2008-07-16 16:35:59 +08:00
|
|
|
bool Socket::connect()
|
2003-04-22 22:16:22 +08:00
|
|
|
{
|
2008-07-16 16:35:59 +08:00
|
|
|
if ( !socket() )
|
|
|
|
return( false );
|
|
|
|
|
|
|
|
if ( ::connect( mSd, mRemoteAddr->getAddr(), getAddrSize() ) == -1 )
|
|
|
|
{
|
|
|
|
Error( "connect(), errno = %d, error = %s", errno, strerror(errno) );
|
|
|
|
close();
|
|
|
|
return( false );
|
|
|
|
}
|
|
|
|
|
|
|
|
mState = CONNECTED;
|
|
|
|
|
2003-04-22 22:16:22 +08:00
|
|
|
return( true );
|
|
|
|
}
|
|
|
|
|
2008-07-16 16:35:59 +08:00
|
|
|
bool Socket::bind()
|
|
|
|
{
|
|
|
|
if ( !socket() )
|
|
|
|
return( false );
|
|
|
|
|
|
|
|
if ( ::bind( mSd, mLocalAddr->getAddr(), getAddrSize() ) == -1 )
|
|
|
|
{
|
|
|
|
Error( "bind(), errno = %d, error = %s", errno, strerror(errno) );
|
|
|
|
close();
|
|
|
|
return( false );
|
|
|
|
}
|
|
|
|
return( true );
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Socket::listen()
|
|
|
|
{
|
|
|
|
if ( ::listen( mSd, SOMAXCONN ) == -1 )
|
|
|
|
{
|
|
|
|
Error( "listen(), errno = %d, error = %s", errno, strerror(errno) );
|
|
|
|
close();
|
|
|
|
return( false );
|
|
|
|
}
|
|
|
|
|
|
|
|
mState = LISTENING;
|
|
|
|
|
|
|
|
return( true );
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Socket::accept()
|
|
|
|
{
|
|
|
|
struct sockaddr *rem_addr = mLocalAddr->getTempAddr();
|
|
|
|
socklen_t rem_addr_size = getAddrSize();
|
|
|
|
|
|
|
|
int newSd = -1;
|
|
|
|
if ( (newSd = ::accept( mSd, rem_addr, &rem_addr_size )) == -1 )
|
|
|
|
{
|
|
|
|
Error( "accept(), errno = %d, error = %s", errno, strerror(errno) );
|
|
|
|
close();
|
|
|
|
return( false );
|
|
|
|
}
|
|
|
|
|
|
|
|
::close( mSd );
|
|
|
|
mSd = newSd;
|
|
|
|
|
|
|
|
mState = CONNECTED;
|
|
|
|
|
|
|
|
return( true );
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Socket::accept( int &newSd )
|
|
|
|
{
|
|
|
|
struct sockaddr *rem_addr = mLocalAddr->getTempAddr();
|
|
|
|
socklen_t rem_addr_size = getAddrSize();
|
|
|
|
|
|
|
|
newSd = -1;
|
|
|
|
if ( (newSd = ::accept( mSd, rem_addr, &rem_addr_size )) == -1 )
|
|
|
|
{
|
|
|
|
Error( "accept(), errno = %d, error = %s", errno, strerror(errno) );
|
|
|
|
close();
|
|
|
|
return( false );
|
|
|
|
}
|
|
|
|
|
|
|
|
return( true );
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Socket::close()
|
|
|
|
{
|
|
|
|
if ( mSd > -1 ) ::close( mSd );
|
|
|
|
mSd = -1;
|
|
|
|
mState = CLOSED;
|
|
|
|
return( true );
|
|
|
|
}
|
|
|
|
|
|
|
|
int Socket::bytesToRead() const
|
2003-04-22 22:16:22 +08:00
|
|
|
{
|
|
|
|
int bytes_to_read = 0;
|
|
|
|
|
2008-07-16 16:35:59 +08:00
|
|
|
if ( ioctl( mSd, FIONREAD, &bytes_to_read ) < 0 )
|
2003-04-22 22:16:22 +08:00
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Error( "ioctl(), errno = %d, error = %s", errno, strerror(errno) );
|
2003-04-22 22:16:22 +08:00
|
|
|
return( -1 );
|
|
|
|
}
|
|
|
|
return( bytes_to_read );
|
|
|
|
}
|
|
|
|
|
2008-07-16 16:35:59 +08:00
|
|
|
bool Socket::getBlocking( bool &blocking )
|
2003-04-22 22:16:22 +08:00
|
|
|
{
|
|
|
|
int flags;
|
|
|
|
|
2008-07-16 16:35:59 +08:00
|
|
|
if ( (flags = fcntl( mSd, F_GETFL )) < 0 )
|
2003-04-22 22:16:22 +08:00
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Error( "fcntl(), errno = %d, error = %s", errno, strerror(errno) );
|
2003-04-22 22:16:22 +08:00
|
|
|
return( false );
|
|
|
|
}
|
|
|
|
blocking = (flags & O_NONBLOCK);
|
|
|
|
return( true );
|
|
|
|
}
|
|
|
|
|
2008-07-16 16:35:59 +08:00
|
|
|
bool Socket::setBlocking( bool blocking )
|
2003-04-22 22:16:22 +08:00
|
|
|
{
|
|
|
|
#if 0
|
|
|
|
// ioctl is apparently not recommended
|
|
|
|
int ioctl_arg = !blocking;
|
2008-07-16 16:35:59 +08:00
|
|
|
if ( ioctl( mSd, FIONBIO, &ioctl_arg ) < 0 )
|
2003-04-22 22:16:22 +08:00
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Error( "ioctl(), errno = %d, error = %s", errno, strerror(errno) );
|
2003-04-22 22:16:22 +08:00
|
|
|
return( false );
|
|
|
|
}
|
|
|
|
return( true );
|
|
|
|
#endif
|
|
|
|
|
|
|
|
int flags;
|
|
|
|
|
|
|
|
/* Now set it for non-blocking I/O */
|
2008-07-16 16:35:59 +08:00
|
|
|
if ( (flags = fcntl( mSd, F_GETFL )) < 0 )
|
2003-04-22 22:16:22 +08:00
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Error( "fcntl(), errno = %d, error = %s", errno, strerror(errno) );
|
2003-04-22 22:16:22 +08:00
|
|
|
return( false );
|
|
|
|
}
|
|
|
|
if ( blocking )
|
|
|
|
{
|
|
|
|
flags &= ~O_NONBLOCK;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
flags |= O_NONBLOCK;
|
|
|
|
}
|
2008-07-16 16:35:59 +08:00
|
|
|
if ( fcntl( mSd, F_SETFL, flags ) < 0 )
|
2003-04-22 22:16:22 +08:00
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Error( "fcntl(), errno = %d, error = %s", errno, strerror(errno) );
|
2003-04-22 22:16:22 +08:00
|
|
|
return( false );
|
|
|
|
}
|
|
|
|
|
|
|
|
return( true );
|
|
|
|
}
|
|
|
|
|
2008-07-16 16:35:59 +08:00
|
|
|
bool Socket::getSendBufferSize( int &buffersize ) const
|
2003-04-22 22:16:22 +08:00
|
|
|
{
|
|
|
|
socklen_t optlen = sizeof(buffersize);
|
2008-07-16 16:35:59 +08:00
|
|
|
if ( getsockopt( mSd, SOL_SOCKET, SO_SNDBUF, &buffersize, &optlen ) < 0 )
|
2003-04-22 22:16:22 +08:00
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Error( "getsockopt(), errno = %d, error = %s", errno, strerror(errno) );
|
2003-04-22 22:16:22 +08:00
|
|
|
return( -1 );
|
|
|
|
}
|
|
|
|
return( buffersize );
|
|
|
|
}
|
|
|
|
|
2008-07-16 16:35:59 +08:00
|
|
|
bool Socket::getRecvBufferSize( int &buffersize ) const
|
2003-04-22 22:16:22 +08:00
|
|
|
{
|
|
|
|
socklen_t optlen = sizeof(buffersize);
|
2008-07-16 16:35:59 +08:00
|
|
|
if ( getsockopt( mSd, SOL_SOCKET, SO_RCVBUF, &buffersize, &optlen ) < 0 )
|
2003-04-22 22:16:22 +08:00
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Error( "getsockopt(), errno = %d, error = %s", errno, strerror(errno) );
|
2003-04-22 22:16:22 +08:00
|
|
|
return( -1 );
|
|
|
|
}
|
|
|
|
return( buffersize );
|
|
|
|
}
|
|
|
|
|
2008-07-16 16:35:59 +08:00
|
|
|
bool Socket::setSendBufferSize( int buffersize )
|
2003-04-22 22:16:22 +08:00
|
|
|
{
|
2008-07-16 16:35:59 +08:00
|
|
|
if ( setsockopt( mSd, SOL_SOCKET, SO_SNDBUF, (char *)&buffersize, sizeof(buffersize)) < 0 )
|
2003-04-22 22:16:22 +08:00
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Error( "setsockopt(), errno = %d, error = %s", errno, strerror(errno) );
|
2003-04-22 22:16:22 +08:00
|
|
|
return( false );
|
|
|
|
}
|
|
|
|
return( true );
|
|
|
|
}
|
|
|
|
|
2008-07-16 16:35:59 +08:00
|
|
|
bool Socket::setRecvBufferSize( int buffersize )
|
2003-04-22 22:16:22 +08:00
|
|
|
{
|
2008-07-16 16:35:59 +08:00
|
|
|
if ( setsockopt( mSd, SOL_SOCKET, SO_RCVBUF, (char *)&buffersize, sizeof(buffersize)) < 0 )
|
2003-04-22 22:16:22 +08:00
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Error( "setsockopt(), errno = %d, error = %s", errno, strerror(errno) );
|
2003-04-22 22:16:22 +08:00
|
|
|
return( false );
|
|
|
|
}
|
|
|
|
return( true );
|
|
|
|
}
|
|
|
|
|
2008-07-16 16:35:59 +08:00
|
|
|
bool Socket::getRouting( bool &route ) const
|
2003-04-22 22:16:22 +08:00
|
|
|
{
|
2008-07-16 16:35:59 +08:00
|
|
|
int dontRoute;
|
|
|
|
socklen_t optlen = sizeof(dontRoute);
|
|
|
|
if ( getsockopt( mSd, SOL_SOCKET, SO_DONTROUTE, &dontRoute, &optlen ) < 0 )
|
2003-04-22 22:16:22 +08:00
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Error( "getsockopt(), errno = %d, error = %s", errno, strerror(errno) );
|
2003-04-22 22:16:22 +08:00
|
|
|
return( false );
|
|
|
|
}
|
2008-07-16 16:35:59 +08:00
|
|
|
route = !dontRoute;
|
2003-04-22 22:16:22 +08:00
|
|
|
return( true );
|
|
|
|
}
|
|
|
|
|
2008-07-16 16:35:59 +08:00
|
|
|
bool Socket::setRouting( bool route )
|
2003-04-22 22:16:22 +08:00
|
|
|
{
|
2008-07-16 16:35:59 +08:00
|
|
|
int dontRoute = !route;
|
|
|
|
if ( setsockopt( mSd, SOL_SOCKET, SO_DONTROUTE, (char *)&dontRoute, sizeof(dontRoute)) < 0 )
|
2003-04-22 22:16:22 +08:00
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Error( "setsockopt(), errno = %d, error = %s", errno, strerror(errno) );
|
2003-04-22 22:16:22 +08:00
|
|
|
return( false );
|
|
|
|
}
|
|
|
|
return( true );
|
|
|
|
}
|
|
|
|
|
2008-07-16 16:35:59 +08:00
|
|
|
bool Socket::getNoDelay( bool &nodelay ) const
|
2003-04-22 22:16:22 +08:00
|
|
|
{
|
|
|
|
int int_nodelay;
|
|
|
|
socklen_t optlen = sizeof(int_nodelay);
|
2008-07-16 16:35:59 +08:00
|
|
|
if ( getsockopt( mSd, IPPROTO_TCP, TCP_NODELAY, &int_nodelay, &optlen ) < 0 )
|
2003-04-22 22:16:22 +08:00
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Error( "getsockopt(), errno = %d, error = %s", errno, strerror(errno) );
|
2003-04-22 22:16:22 +08:00
|
|
|
return( false );
|
|
|
|
}
|
|
|
|
nodelay = int_nodelay;
|
|
|
|
return( true );
|
|
|
|
}
|
|
|
|
|
2008-07-16 16:35:59 +08:00
|
|
|
bool Socket::setNoDelay( bool nodelay )
|
2003-04-22 22:16:22 +08:00
|
|
|
{
|
|
|
|
int int_nodelay = nodelay;
|
|
|
|
|
2008-07-16 16:35:59 +08:00
|
|
|
if ( setsockopt( mSd, IPPROTO_TCP, TCP_NODELAY, (char *)&int_nodelay, sizeof(int_nodelay)) < 0 )
|
2003-04-22 22:16:22 +08:00
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
Error( "setsockopt(), errno = %d, error = %s", errno, strerror(errno) );
|
2003-04-22 22:16:22 +08:00
|
|
|
return( false );
|
|
|
|
}
|
|
|
|
return( true );
|
|
|
|
}
|
|
|
|
|
2008-07-16 16:35:59 +08:00
|
|
|
bool TcpInetServer::listen()
|
2003-04-22 22:16:22 +08:00
|
|
|
{
|
2008-07-16 16:35:59 +08:00
|
|
|
return( Socket::listen() );
|
|
|
|
}
|
2003-04-22 22:16:22 +08:00
|
|
|
|
2008-07-16 16:35:59 +08:00
|
|
|
bool TcpInetServer::accept()
|
|
|
|
{
|
|
|
|
return( Socket::accept() );
|
|
|
|
}
|
2003-04-22 22:16:22 +08:00
|
|
|
|
2008-07-16 16:35:59 +08:00
|
|
|
bool TcpInetServer::accept( TcpInetSocket *&newSocket )
|
|
|
|
{
|
|
|
|
int newSd = -1;
|
|
|
|
newSocket = 0;
|
2003-04-22 22:16:22 +08:00
|
|
|
|
2008-07-16 16:35:59 +08:00
|
|
|
if ( !Socket::accept( newSd ) )
|
|
|
|
return( false );
|
2003-04-22 22:16:22 +08:00
|
|
|
|
2008-07-16 16:35:59 +08:00
|
|
|
newSocket = new TcpInetSocket( *this, newSd );
|
|
|
|
|
|
|
|
return( true );
|
2003-04-22 22:16:22 +08:00
|
|
|
}
|
|
|
|
|
2008-07-16 16:35:59 +08:00
|
|
|
bool TcpUnixServer::accept( TcpUnixSocket *&newSocket )
|
2003-04-22 22:16:22 +08:00
|
|
|
{
|
2008-07-16 16:35:59 +08:00
|
|
|
int newSd = -1;
|
|
|
|
newSocket = 0;
|
2003-04-22 22:16:22 +08:00
|
|
|
|
2008-07-16 16:35:59 +08:00
|
|
|
if ( !Socket::accept( newSd ) )
|
|
|
|
return( false );
|
2003-04-22 22:16:22 +08:00
|
|
|
|
2008-07-16 16:35:59 +08:00
|
|
|
newSocket = new TcpUnixSocket( *this, newSd );
|
2003-04-22 22:16:22 +08:00
|
|
|
|
2008-07-16 16:35:59 +08:00
|
|
|
return( true );
|
2003-04-22 22:16:22 +08:00
|
|
|
}
|
|
|
|
|
2008-07-16 16:35:59 +08:00
|
|
|
Select::Select() : mHasTimeout( false ), mMaxFd( -1 )
|
2003-04-22 22:16:22 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2008-07-16 16:35:59 +08:00
|
|
|
Select::Select( struct timeval timeout ) : mMaxFd( -1 )
|
2003-04-22 22:16:22 +08:00
|
|
|
{
|
2008-07-16 16:35:59 +08:00
|
|
|
setTimeout( timeout );
|
2003-04-22 22:16:22 +08:00
|
|
|
}
|
|
|
|
|
2008-07-16 16:35:59 +08:00
|
|
|
Select::Select( int timeout ) : mMaxFd( -1 )
|
2003-04-22 22:16:22 +08:00
|
|
|
{
|
2008-07-16 16:35:59 +08:00
|
|
|
setTimeout( timeout );
|
2003-04-22 22:16:22 +08:00
|
|
|
}
|
|
|
|
|
2008-07-16 16:35:59 +08:00
|
|
|
Select::Select( double timeout ) : mMaxFd( -1 )
|
2003-04-22 22:16:22 +08:00
|
|
|
{
|
2008-07-16 16:35:59 +08:00
|
|
|
setTimeout( timeout );
|
2003-04-22 22:16:22 +08:00
|
|
|
}
|
|
|
|
|
2008-07-16 16:35:59 +08:00
|
|
|
void Select::setTimeout( int timeout )
|
2003-04-22 22:16:22 +08:00
|
|
|
{
|
2008-07-16 16:35:59 +08:00
|
|
|
mTimeout.tv_sec = timeout;
|
|
|
|
mTimeout.tv_usec = 0;
|
|
|
|
mHasTimeout = true;
|
2003-04-22 22:16:22 +08:00
|
|
|
}
|
|
|
|
|
2008-07-16 16:35:59 +08:00
|
|
|
void Select::setTimeout( double timeout )
|
2003-04-22 22:16:22 +08:00
|
|
|
{
|
2008-07-16 16:35:59 +08:00
|
|
|
mTimeout.tv_sec = int(timeout);
|
2008-10-09 16:27:10 +08:00
|
|
|
mTimeout.tv_usec = suseconds_t((timeout-mTimeout.tv_sec)*1000000.0);
|
2008-07-16 16:35:59 +08:00
|
|
|
mHasTimeout = true;
|
2003-04-22 22:16:22 +08:00
|
|
|
}
|
|
|
|
|
2008-07-16 16:35:59 +08:00
|
|
|
void Select::setTimeout( struct timeval timeout )
|
2003-04-22 22:16:22 +08:00
|
|
|
{
|
2008-07-16 16:35:59 +08:00
|
|
|
mTimeout = timeout;
|
|
|
|
mHasTimeout = true;
|
2003-04-22 22:16:22 +08:00
|
|
|
}
|
|
|
|
|
2008-07-16 16:35:59 +08:00
|
|
|
void Select::clearTimeout()
|
2003-04-22 22:16:22 +08:00
|
|
|
{
|
2008-07-16 16:35:59 +08:00
|
|
|
mHasTimeout = false;
|
2003-04-22 22:16:22 +08:00
|
|
|
}
|
|
|
|
|
2008-07-16 16:35:59 +08:00
|
|
|
void Select::calcMaxFd()
|
2003-04-22 22:16:22 +08:00
|
|
|
{
|
2008-07-16 16:35:59 +08:00
|
|
|
mMaxFd = -1;
|
|
|
|
for ( CommsSet::iterator iter = mReaders.begin(); iter != mReaders.end(); iter++ )
|
|
|
|
if ( (*iter)->getMaxDesc() > mMaxFd )
|
|
|
|
mMaxFd = (*iter)->getMaxDesc();
|
|
|
|
for ( CommsSet::iterator iter = mWriters.begin(); iter != mWriters.end(); iter++ )
|
|
|
|
if ( (*iter)->getMaxDesc() > mMaxFd )
|
|
|
|
mMaxFd = (*iter)->getMaxDesc();
|
2003-04-22 22:16:22 +08:00
|
|
|
}
|
|
|
|
|
2008-07-16 16:35:59 +08:00
|
|
|
bool Select::addReader( CommsBase *comms )
|
2003-04-22 22:16:22 +08:00
|
|
|
{
|
2008-07-16 16:35:59 +08:00
|
|
|
if ( !comms->isOpen() )
|
|
|
|
{
|
|
|
|
Error( "Unable to add closed reader" );
|
|
|
|
return( false );
|
|
|
|
}
|
|
|
|
std::pair<CommsSet::iterator,bool> result = mReaders.insert( comms );
|
|
|
|
if ( result.second )
|
|
|
|
if ( comms->getMaxDesc() > mMaxFd )
|
|
|
|
mMaxFd = comms->getMaxDesc();
|
|
|
|
return( result.second );
|
2003-04-22 22:16:22 +08:00
|
|
|
}
|
|
|
|
|
2008-07-16 16:35:59 +08:00
|
|
|
bool Select::deleteReader( CommsBase *comms )
|
2003-04-22 22:16:22 +08:00
|
|
|
{
|
2008-07-16 16:35:59 +08:00
|
|
|
if ( !comms->isOpen() )
|
|
|
|
{
|
|
|
|
Error( "Unable to add closed reader" );
|
|
|
|
return( false );
|
|
|
|
}
|
|
|
|
if ( mReaders.erase( comms ) )
|
|
|
|
{
|
|
|
|
calcMaxFd();
|
|
|
|
return( true );
|
|
|
|
}
|
|
|
|
return( false );
|
2003-04-22 22:16:22 +08:00
|
|
|
}
|
|
|
|
|
2008-07-16 16:35:59 +08:00
|
|
|
void Select::clearReaders()
|
2003-04-22 22:16:22 +08:00
|
|
|
{
|
2008-07-16 16:35:59 +08:00
|
|
|
mReaders.clear();
|
|
|
|
mMaxFd = -1;
|
2003-04-22 22:16:22 +08:00
|
|
|
}
|
|
|
|
|
2008-07-16 16:35:59 +08:00
|
|
|
bool Select::addWriter( CommsBase *comms )
|
2003-04-22 22:16:22 +08:00
|
|
|
{
|
2008-07-16 16:35:59 +08:00
|
|
|
std::pair<CommsSet::iterator,bool> result = mWriters.insert( comms );
|
|
|
|
if ( result.second )
|
|
|
|
if ( comms->getMaxDesc() > mMaxFd )
|
|
|
|
mMaxFd = comms->getMaxDesc();
|
|
|
|
return( result.second );
|
2003-04-22 22:16:22 +08:00
|
|
|
}
|
|
|
|
|
2008-07-16 16:35:59 +08:00
|
|
|
bool Select::deleteWriter( CommsBase *comms )
|
2003-04-22 22:16:22 +08:00
|
|
|
{
|
2008-07-16 16:35:59 +08:00
|
|
|
if ( mWriters.erase( comms ) )
|
|
|
|
{
|
|
|
|
calcMaxFd();
|
|
|
|
return( true );
|
|
|
|
}
|
|
|
|
return( false );
|
2003-04-22 22:16:22 +08:00
|
|
|
}
|
|
|
|
|
2008-07-16 16:35:59 +08:00
|
|
|
void Select::clearWriters()
|
2003-04-22 22:16:22 +08:00
|
|
|
{
|
2008-07-16 16:35:59 +08:00
|
|
|
mWriters.clear();
|
|
|
|
mMaxFd = -1;
|
2003-04-22 22:16:22 +08:00
|
|
|
}
|
|
|
|
|
2008-07-16 16:35:59 +08:00
|
|
|
int Select::wait()
|
2003-04-22 22:16:22 +08:00
|
|
|
{
|
2008-07-16 16:35:59 +08:00
|
|
|
struct timeval tempTimeout = mTimeout;
|
|
|
|
struct timeval *selectTimeout = mHasTimeout?&tempTimeout:NULL;
|
2003-04-22 22:16:22 +08:00
|
|
|
|
2008-07-16 16:35:59 +08:00
|
|
|
fd_set rfds;
|
|
|
|
fd_set wfds;
|
2003-04-22 22:16:22 +08:00
|
|
|
|
2008-07-16 16:35:59 +08:00
|
|
|
mReadable.clear();
|
|
|
|
FD_ZERO(&rfds);
|
|
|
|
for ( CommsSet::iterator iter = mReaders.begin(); iter != mReaders.end(); iter++ )
|
|
|
|
FD_SET((*iter)->getReadDesc(),&rfds);
|
2003-04-22 22:16:22 +08:00
|
|
|
|
2008-07-16 16:35:59 +08:00
|
|
|
mWriteable.clear();
|
|
|
|
FD_ZERO(&wfds);
|
|
|
|
for ( CommsSet::iterator iter = mWriters.begin(); iter != mWriters.end(); iter++ )
|
|
|
|
FD_SET((*iter)->getWriteDesc(),&wfds);
|
2003-04-22 22:16:22 +08:00
|
|
|
|
2008-07-16 16:35:59 +08:00
|
|
|
int nFound = select( mMaxFd+1, &rfds, &wfds, NULL, selectTimeout );
|
|
|
|
if( nFound == 0 )
|
|
|
|
{
|
|
|
|
Debug( 1, "Select timed out" );
|
|
|
|
}
|
|
|
|
else if ( nFound < 0)
|
|
|
|
{
|
|
|
|
Error( "Select error: %s", strerror(errno) );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for ( CommsSet::iterator iter = mReaders.begin(); iter != mReaders.end(); iter++ )
|
|
|
|
if ( FD_ISSET((*iter)->getReadDesc(),&rfds) )
|
|
|
|
mReadable.push_back( *iter );
|
|
|
|
for ( CommsSet::iterator iter = mWriters.begin(); iter != mWriters.end(); iter++ )
|
|
|
|
if ( FD_ISSET((*iter)->getWriteDesc(),&rfds) )
|
|
|
|
mWriteable.push_back( *iter );
|
|
|
|
}
|
|
|
|
return( nFound );
|
2003-04-22 22:16:22 +08:00
|
|
|
}
|
|
|
|
|
2008-07-16 16:35:59 +08:00
|
|
|
const Select::CommsList &Select::getReadable() const
|
2003-04-22 22:16:22 +08:00
|
|
|
{
|
2008-07-16 16:35:59 +08:00
|
|
|
return( mReadable );
|
2003-04-22 22:16:22 +08:00
|
|
|
}
|
|
|
|
|
2008-07-16 16:35:59 +08:00
|
|
|
const Select::CommsList &Select::getWriteable() const
|
|
|
|
{
|
|
|
|
return( mWriteable );
|
|
|
|
}
|