psocksxx  1.0.0
sockstreambuf.h
1 /*
2 * psocksxx - A C++ wrapper for POSIX sockets
3 * Copyright (C) 2013 Uditha Atukorala
4 *
5 * This software library is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This software library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this software library. If not, see <http://www.gnu.org/licenses/>.
17 *
18 */
19 
20 #ifndef PSOCKSXX_SOCKSTREAMBUF_H
21 #define PSOCKSXX_SOCKSTREAMBUF_H
22 
23 #include <psocksxx/socktimeoutexception.h>
24 #include <psocksxx/sockaddr.h>
25 
26 #include <streambuf>
27 #include <sys/socket.h>
28 #include <netinet/in.h>
29 #include <unistd.h>
30 
31 #ifndef SOCKSTREAMBUF_SIZE
32 #define SOCKSTREAMBUF_SIZE 1024
33 #endif
34 
35 #ifndef SOCKSTREAMBUF_PUTBACK_SIZE
36 #define SOCKSTREAMBUF_PUTBACK_SIZE 8
37 #endif
38 
39 
40 namespace psocksxx {
41 
48  class sockstreambuf : public std::streambuf {
49  public:
50 
52  typedef int socket_t;
53 
55  enum eof_t {
56  eof = -1
57  };
58 
61  pf_local = PF_LOCAL,
62  pf_inet = PF_INET,
63  pf_route = PF_ROUTE,
64  pf_key = PF_KEY,
65  pf_inet6 = PF_INET6
66  };
67 
70  sock_stream = SOCK_STREAM,
71  sock_dgram = SOCK_DGRAM,
72  sock_raw = SOCK_RAW,
73  sock_rdm = SOCK_RDM,
74  sock_seqpacket = SOCK_SEQPACKET
75  };
76 
80  ipproto_ip = IPPROTO_IP,
81  ipproto_ipv6 = IPPROTO_IPV6,
82  ipproto_icmp = IPPROTO_ICMP,
83  ipproto_raw = IPPROTO_RAW,
84  ipproto_tcp = IPPROTO_TCP,
85  ipproto_udp = IPPROTO_UDP
86  };
87 
88 
89  sockstreambuf() throw();
90  virtual ~sockstreambuf();
91 
101  sockstreambuf( socket_t socket ) throw();
102 
111  const socket_t & socket() const throw();
112 
123  void open( socket_domain_t domain, socket_type_t type, socket_protocol_t proto = proto_unspec ) throw( sockexception );
124 
132  void close() throw();
133 
144  virtual int flush() throw( socktimeoutexception );
145 
146 
160  void connect( const sockaddr * dest_addr, unsigned int timeout = 0 ) throw( sockexception, socktimeoutexception );
161 
176  void connect( const sockaddr * dest_addr, timeval * timeout ) throw( sockexception, socktimeoutexception );
177 
190  void bind( const sockaddr * bind_addr, bool reuse_addr = false ) throw( sockexception );
191 
204  void listen( int backlog = SOMAXCONN ) throw( sockexception );
205 
218  socket_t accept() throw( sockexception );
219 
231  const timeval * timeout( time_t sec, suseconds_t usec ) throw();
232 
242  void * clear_timeout() throw();
243 
252  bool timedout() const throw();
253 
254 
255  protected:
256 
260  void init_buffers() throw();
261 
265  void cleanup_buffers() throw();
266 
275  virtual int sync() throw();
276 
293  virtual int overflow( int c = eof ) throw( socktimeoutexception );
294 
309  virtual int underflow() throw( socktimeoutexception );
310 
323  bool ready( timeval * timeout, bool chk_read = true, bool chk_write = true ) throw( sockexception );
324 
325 
326  private:
327 
329  socket_t _socket;
330 
331  size_t _bufsize;
332  size_t _putbacksize;
333 
334  timeval * _timeout;
335  bool _timed_out;
336 
337 
338  void init_defaults() throw();
339 
340  };
341 
342 } /* end of namespace psocksxx */
343 
344 #endif /* !PSOCKSXX_SOCKSTREAMBUF_H */
345 
bool timedout() const
get the timed-out status
Definition: sockstreambuf.cpp:282
Socket exception.
Definition: sockexception.h:35
Definition: sockstreambuf.h:81
void connect(const sockaddr *dest_addr, unsigned int timeout=0)
initiate a connection on a socket
Definition: sockstreambuf.cpp:129
Definition: sockstreambuf.h:84
void listen(int backlog=SOMAXCONN)
make the socket passive and capable of accepting connections
Definition: sockstreambuf.cpp:225
Definition: sockstreambuf.h:61
socket_type_t
Definition: sockstreambuf.h:69
Definition: sockstreambuf.h:82
Definition: sockstreambuf.h:85
Definition: sockstreambuf.h:65
virtual int underflow()
read more data into the buffer from the socket
Definition: sockstreambuf.cpp:391
virtual int sync()
sync data with the socket
Definition: sockstreambuf.cpp:351
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
void bind(const sockaddr *bind_addr, bool reuse_addr=false)
bind the socket to a specified address
Definition: sockstreambuf.cpp:205
virtual int flush()
flush the socket output buffer
Definition: sockstreambuf.cpp:315
Socket stream buffer class.
Definition: sockstreambuf.h:48
void cleanup_buffers()
cleanup internal buffers
Definition: sockstreambuf.cpp:304
int socket_t
Definition: sockstreambuf.h:52
virtual int overflow(int c=eof)
consumes the buffer by writing the contents to the socket
Definition: sockstreambuf.cpp:370
sockstreambuf()
constructor
Definition: sockstreambuf.cpp:33
socket_protocol_t
Definition: sockstreambuf.h:78
Definition: sockstreambuf.h:63
Definition: iosocks.cpp:23
Definition: sockstreambuf.h:56
const socket_t & socket() const
get internal socket data
Definition: sockstreambuf.cpp:247
Definition: sockstreambuf.h:62
Definition: sockstreambuf.h:79
eof_t
Definition: sockstreambuf.h:55
void close()
close open sockets
Definition: sockstreambuf.cpp:110
Socket timeout exception.
Definition: socktimeoutexception.h:36
Definition: sockstreambuf.h:64
Socket address base class.
Definition: sockaddr.h:35
Definition: sockstreambuf.h:80
const timeval * timeout(time_t sec, suseconds_t usec)
set the timeout value for the socket
Definition: sockstreambuf.cpp:252
void init_buffers()
initialise internal buffers
Definition: sockstreambuf.cpp:287
void * clear_timeout()
clear the timeout value for the socket
Definition: sockstreambuf.cpp:264
socket_domain_t
Definition: sockstreambuf.h:60
socket_t accept()
accept a connection on a listening (passive) socket
Definition: sockstreambuf.cpp:234
void open(socket_domain_t domain, socket_type_t type, socket_protocol_t proto=proto_unspec)
open a socket
Definition: sockstreambuf.cpp:89
Definition: sockstreambuf.h:83