sockstreambuf.h
/*
* psocksxx - A C++ wrapper for POSIX sockets
* Copyright (C) 2013 Uditha Atukorala
*
* This software library is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This software library 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this software library. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef PSOCKSXX_SOCKSTREAMBUF_H
#define PSOCKSXX_SOCKSTREAMBUF_H
#include <psocksxx/socktimeoutexception.h>
#include <psocksxx/sockaddr.h>
#include <streambuf>
#include <sys/socket.h>
#if defined(__GNU__) || defined(__gnu_hurd__)
#include <hurd/socket.h>
#endif
#include <netinet/in.h>
#include <unistd.h>
#ifndef SOCKSTREAMBUF_SIZE
#define SOCKSTREAMBUF_SIZE 1024
#endif
#ifndef SOCKSTREAMBUF_PUTBACK_SIZE
#define SOCKSTREAMBUF_PUTBACK_SIZE 8
#endif
namespace psocksxx {
class sockstreambuf : public std::streambuf {
public:
typedef int socket_t;
enum eof_t {
eof = -1
};
enum socket_domain_t {
pf_local = PF_LOCAL,
pf_inet = PF_INET,
pf_route = PF_ROUTE,
pf_key = PF_KEY,
pf_inet6 = PF_INET6
};
enum socket_type_t {
sock_stream = SOCK_STREAM,
sock_dgram = SOCK_DGRAM,
sock_raw = SOCK_RAW,
sock_rdm = SOCK_RDM,
sock_seqpacket = SOCK_SEQPACKET
};
enum socket_protocol_t {
proto_unspec = 0,
ipproto_ip = IPPROTO_IP,
ipproto_ipv6 = IPPROTO_IPV6,
ipproto_icmp = IPPROTO_ICMP,
ipproto_raw = IPPROTO_RAW,
ipproto_tcp = IPPROTO_TCP,
ipproto_udp = IPPROTO_UDP
};
sockstreambuf() throw();
virtual ~sockstreambuf();
sockstreambuf( socket_t socket ) throw();
const socket_t & socket() const throw();
void open( socket_domain_t domain, socket_type_t type, socket_protocol_t proto = proto_unspec ) throw( sockexception );
void close() throw();
virtual int flush() throw( socktimeoutexception );
void connect( const sockaddr * dest_addr, unsigned int timeout = 0 ) throw( sockexception, socktimeoutexception );
void connect( const sockaddr * dest_addr, timeval * timeout ) throw( sockexception, socktimeoutexception );
void bind( const sockaddr * bind_addr, bool reuse_addr = false ) throw( sockexception );
void listen( int backlog = SOMAXCONN ) throw( sockexception );
socket_t accept() throw( sockexception );
const timeval * timeout( time_t sec, suseconds_t usec ) throw();
void * clear_timeout() throw();
bool timedout() const throw();
protected:
void init_buffers() throw();
void cleanup_buffers() throw();
virtual int sync() throw();
virtual int overflow( int c = eof ) throw( socktimeoutexception );
virtual int underflow() throw( socktimeoutexception );
bool ready( timeval * timeout, bool chk_read = true, bool chk_write = true ) throw( sockexception );
private:
socket_t _socket;
size_t _bufsize;
size_t _putbacksize;
timeval * _timeout;
bool _timed_out;
void init_defaults() throw();
};
} /* end of namespace psocksxx */
#endif /* !PSOCKSXX_SOCKSTREAMBUF_H */
psocksxx::sockaddr
Socket address base class.
Definition: sockaddr.h:35
psocksxx::sockexception
Socket exception.
Definition: sockexception.h:35
psocksxx::sockstreambuf
Socket stream buffer class.
Definition: sockstreambuf.h:53
psocksxx::sockstreambuf::timeout
const timeval * timeout(time_t sec, suseconds_t usec)
set the timeout value for the socket
Definition: sockstreambuf.cpp:252
psocksxx::sockstreambuf::eof_t
eof_t
Definition: sockstreambuf.h:60
psocksxx::sockstreambuf::eof
@ eof
Definition: sockstreambuf.h:61
psocksxx::sockstreambuf::socket_protocol_t
socket_protocol_t
Definition: sockstreambuf.h:83
psocksxx::sockstreambuf::ipproto_ipv6
@ ipproto_ipv6
Definition: sockstreambuf.h:86
psocksxx::sockstreambuf::ipproto_ip
@ ipproto_ip
Definition: sockstreambuf.h:85
psocksxx::sockstreambuf::ipproto_icmp
@ ipproto_icmp
Definition: sockstreambuf.h:87
psocksxx::sockstreambuf::ipproto_tcp
@ ipproto_tcp
Definition: sockstreambuf.h:89
psocksxx::sockstreambuf::ipproto_udp
@ ipproto_udp
Definition: sockstreambuf.h:90
psocksxx::sockstreambuf::ipproto_raw
@ ipproto_raw
Definition: sockstreambuf.h:88
psocksxx::sockstreambuf::proto_unspec
@ proto_unspec
Definition: sockstreambuf.h:84
psocksxx::sockstreambuf::underflow
virtual int underflow()
read more data into the buffer from the socket
Definition: sockstreambuf.cpp:391
psocksxx::sockstreambuf::connect
void connect(const sockaddr *dest_addr, unsigned int timeout=0)
initiate a connection on a socket
Definition: sockstreambuf.cpp:129
psocksxx::sockstreambuf::overflow
virtual int overflow(int c=eof)
consumes the buffer by writing the contents to the socket
Definition: sockstreambuf.cpp:370
psocksxx::sockstreambuf::socket_domain_t
socket_domain_t
Definition: sockstreambuf.h:65
psocksxx::sockstreambuf::pf_key
@ pf_key
Definition: sockstreambuf.h:69
psocksxx::sockstreambuf::pf_route
@ pf_route
Definition: sockstreambuf.h:68
psocksxx::sockstreambuf::pf_inet
@ pf_inet
Definition: sockstreambuf.h:67
psocksxx::sockstreambuf::pf_local
@ pf_local
Definition: sockstreambuf.h:66
psocksxx::sockstreambuf::pf_inet6
@ pf_inet6
Definition: sockstreambuf.h:70
psocksxx::sockstreambuf::accept
socket_t accept()
accept a connection on a listening (passive) socket
Definition: sockstreambuf.cpp:234
psocksxx::sockstreambuf::socket_t
int socket_t
Definition: sockstreambuf.h:57
psocksxx::sockstreambuf::socket
const socket_t & socket() const
get internal socket data
Definition: sockstreambuf.cpp:247
psocksxx::sockstreambuf::close
void close()
close open sockets
Definition: sockstreambuf.cpp:110
psocksxx::sockstreambuf::flush
virtual int flush()
flush the socket output buffer
Definition: sockstreambuf.cpp:315
psocksxx::sockstreambuf::socket_type_t
socket_type_t
Definition: sockstreambuf.h:74
psocksxx::sockstreambuf::open
void open(socket_domain_t domain, socket_type_t type, socket_protocol_t proto=proto_unspec)
open a socket
Definition: sockstreambuf.cpp:89
psocksxx::sockstreambuf::sockstreambuf
sockstreambuf()
constructor
Definition: sockstreambuf.cpp:33
psocksxx::sockstreambuf::ready
bool ready(timeval *timeout, bool chk_read=true, bool chk_write=true)
check for the read/write availability on the socket
Definition: sockstreambuf.cpp:451
psocksxx::sockstreambuf::sync
virtual int sync()
sync data with the socket
Definition: sockstreambuf.cpp:351
psocksxx::sockstreambuf::timedout
bool timedout() const
get the timed-out status
Definition: sockstreambuf.cpp:282
psocksxx::sockstreambuf::listen
void listen(int backlog=SOMAXCONN)
make the socket passive and capable of accepting connections
Definition: sockstreambuf.cpp:225
psocksxx::sockstreambuf::cleanup_buffers
void cleanup_buffers()
cleanup internal buffers
Definition: sockstreambuf.cpp:304
psocksxx::sockstreambuf::bind
void bind(const sockaddr *bind_addr, bool reuse_addr=false)
bind the socket to a specified address
Definition: sockstreambuf.cpp:205
psocksxx::sockstreambuf::clear_timeout
void * clear_timeout()
clear the timeout value for the socket
Definition: sockstreambuf.cpp:264
psocksxx::sockstreambuf::init_buffers
void init_buffers()
initialise internal buffers
Definition: sockstreambuf.cpp:287
psocksxx::socktimeoutexception
Socket timeout exception.
Definition: socktimeoutexception.h:36