psocksxx 1.1.1
Loading...
Searching...
No Matches
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
29#if defined(__GNU__) || defined(__gnu_hurd__)
30#include <hurd/socket.h>
31#endif
32
33#include <netinet/in.h>
34#include <unistd.h>
35
36#ifndef SOCKSTREAMBUF_SIZE
37#define SOCKSTREAMBUF_SIZE 1024
38#endif
39
40#ifndef SOCKSTREAMBUF_PUTBACK_SIZE
41#define SOCKSTREAMBUF_PUTBACK_SIZE 8
42#endif
43
44
45namespace psocksxx {
46
53 class sockstreambuf : public std::streambuf {
54 public:
55
57 typedef int socket_t;
58
60 enum eof_t {
61 eof = -1
62 };
63
66 pf_local = PF_LOCAL,
67 pf_inet = PF_INET,
68 pf_route = PF_ROUTE,
69 pf_key = PF_KEY,
70 pf_inet6 = PF_INET6
71 };
72
75 sock_stream = SOCK_STREAM,
76 sock_dgram = SOCK_DGRAM,
77 sock_raw = SOCK_RAW,
78 sock_rdm = SOCK_RDM,
79 sock_seqpacket = SOCK_SEQPACKET
80 };
81
85 ipproto_ip = IPPROTO_IP,
86 ipproto_ipv6 = IPPROTO_IPV6,
87 ipproto_icmp = IPPROTO_ICMP,
88 ipproto_raw = IPPROTO_RAW,
89 ipproto_tcp = IPPROTO_TCP,
90 ipproto_udp = IPPROTO_UDP
91 };
92
93
94 sockstreambuf() throw();
95 virtual ~sockstreambuf();
96
106 sockstreambuf( socket_t socket ) throw();
107
116 const socket_t & socket() const throw();
117
128 void open( socket_domain_t domain, socket_type_t type, socket_protocol_t proto = proto_unspec ) throw( sockexception );
129
137 void close() throw();
138
149 virtual int flush() throw( socktimeoutexception );
150
151
165 void connect( const sockaddr * dest_addr, unsigned int timeout = 0 ) throw( sockexception, socktimeoutexception );
166
181 void connect( const sockaddr * dest_addr, timeval * timeout ) throw( sockexception, socktimeoutexception );
182
195 void bind( const sockaddr * bind_addr, bool reuse_addr = false ) throw( sockexception );
196
209 void listen( int backlog = SOMAXCONN ) throw( sockexception );
210
223 socket_t accept() throw( sockexception );
224
236 const timeval * timeout( time_t sec, suseconds_t usec ) throw();
237
247 void * clear_timeout() throw();
248
257 bool timedout() const throw();
258
259
260 protected:
261
265 void init_buffers() throw();
266
270 void cleanup_buffers() throw();
271
280 virtual int sync() throw();
281
298 virtual int overflow( int c = eof ) throw( socktimeoutexception );
299
314 virtual int underflow() throw( socktimeoutexception );
315
328 bool ready( timeval * timeout, bool chk_read = true, bool chk_write = true ) throw( sockexception );
329
330
331 private:
332
334 socket_t _socket;
335
336 size_t _bufsize;
337 size_t _putbacksize;
338
339 timeval * _timeout;
340 bool _timed_out;
341
342
343 void init_defaults() throw();
344
345 };
346
347} /* end of namespace psocksxx */
348
349#endif /* !PSOCKSXX_SOCKSTREAMBUF_H */
350
Socket address base class.
Definition sockaddr.h:35
Socket exception.
Definition sockexception.h:35
Socket stream buffer class.
Definition sockstreambuf.h:53
const timeval * timeout(time_t sec, suseconds_t usec)
set the timeout value for the socket
Definition sockstreambuf.cpp:252
eof_t
Definition sockstreambuf.h:60
@ eof
Definition sockstreambuf.h:61
socket_protocol_t
Definition sockstreambuf.h:83
@ ipproto_ipv6
Definition sockstreambuf.h:86
@ ipproto_ip
Definition sockstreambuf.h:85
@ ipproto_icmp
Definition sockstreambuf.h:87
@ ipproto_tcp
Definition sockstreambuf.h:89
@ ipproto_udp
Definition sockstreambuf.h:90
@ ipproto_raw
Definition sockstreambuf.h:88
@ proto_unspec
Definition sockstreambuf.h:84
virtual int underflow()
read more data into the buffer from the socket
Definition sockstreambuf.cpp:391
void connect(const sockaddr *dest_addr, unsigned int timeout=0)
initiate a connection on a socket
Definition sockstreambuf.cpp:129
virtual int overflow(int c=eof)
consumes the buffer by writing the contents to the socket
Definition sockstreambuf.cpp:370
socket_domain_t
Definition sockstreambuf.h:65
@ pf_key
Definition sockstreambuf.h:69
@ pf_route
Definition sockstreambuf.h:68
@ pf_inet
Definition sockstreambuf.h:67
@ pf_local
Definition sockstreambuf.h:66
@ pf_inet6
Definition sockstreambuf.h:70
socket_t accept()
accept a connection on a listening (passive) socket
Definition sockstreambuf.cpp:234
int socket_t
Definition sockstreambuf.h:57
const socket_t & socket() const
get internal socket data
Definition sockstreambuf.cpp:247
void close()
close open sockets
Definition sockstreambuf.cpp:110
virtual int flush()
flush the socket output buffer
Definition sockstreambuf.cpp:315
socket_type_t
Definition sockstreambuf.h:74
void open(socket_domain_t domain, socket_type_t type, socket_protocol_t proto=proto_unspec)
open a socket
Definition sockstreambuf.cpp:89
sockstreambuf()
constructor
Definition sockstreambuf.cpp:33
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
virtual int sync()
sync data with the socket
Definition sockstreambuf.cpp:351
bool timedout() const
get the timed-out status
Definition sockstreambuf.cpp:282
void listen(int backlog=SOMAXCONN)
make the socket passive and capable of accepting connections
Definition sockstreambuf.cpp:225
void cleanup_buffers()
cleanup internal buffers
Definition sockstreambuf.cpp:304
void bind(const sockaddr *bind_addr, bool reuse_addr=false)
bind the socket to a specified address
Definition sockstreambuf.cpp:205
void * clear_timeout()
clear the timeout value for the socket
Definition sockstreambuf.cpp:264
void init_buffers()
initialise internal buffers
Definition sockstreambuf.cpp:287
Socket timeout exception.
Definition socktimeoutexception.h:36