Commit | Line | Data |
---|---|---|
0e23f538 TJ |
1 | /* |
2 | The software in this package is distributed under the GNU General | |
3 | Public License version 2 (with a special exception described below). | |
4 | ||
5 | A copy of GNU General Public License (GPL) is included in this distribution, | |
6 | in the file COPYING.GPL. | |
7 | ||
8 | As a special exception, if other files instantiate templates or use macros | |
9 | or inline functions from this file, or you compile this file and link it | |
10 | with other works to produce a work based on this file, this file | |
11 | does not by itself cause the resulting work to be covered | |
12 | by the GNU General Public License. | |
13 | ||
14 | However the source code for this file must still be made available | |
15 | in accordance with section (3) of the GNU General Public License. | |
16 | ||
17 | This exception does not invalidate any other reasons why a work based | |
18 | on this file might be covered by the GNU General Public License. | |
19 | */ | |
e93545dd GE |
20 | /*************************************************************************** |
21 | insocketstream.hxx - C++ streambuffer wrapper | |
22 | ------------------- | |
23 | begin : Sun Nov 10 2002 | |
24 | copyright : (C) 2002 by Intra2net AG | |
e93545dd GE |
25 | ***************************************************************************/ |
26 | ||
27 | #ifndef _INSOCKETSTREAM | |
28 | #define _INSOCKETSTREAM | |
29 | ||
30 | #include <string> | |
31 | #include <sstream> | |
32 | #include <streambuf> | |
33 | #include <stdio.h> | |
8d2c81e4 | 34 | #include <errno.h> |
e93545dd GE |
35 | |
36 | #include <sys/socket.h> | |
37 | #include <sys/un.h> | |
38 | #include <unistd.h> | |
39 | ||
40 | #include "exception.hxx" | |
41 | ||
9050e9f7 | 42 | // ATTENTION: A lot of mysterious STL bugs occurred |
e93545dd GE |
43 | // with a "real" buffer (buffer larger than 1 byte and up to 100 bytes) |
44 | // -> Keep it slow and working! | |
45 | ||
46 | class insocketstream : public std::streambuf | |
47 | { | |
48 | protected: | |
49 | char buffer; | |
50 | int sock; | |
51 | ||
52 | public: | |
53 | insocketstream(std::string unixsocket) | |
54 | { | |
55 | sock=socket(AF_UNIX,SOCK_STREAM,0); | |
56 | if (sock == -1) | |
57 | { | |
58 | std::ostringstream os; | |
59 | os << "can't open socket: " << strerror(errno); | |
60 | throw EXCEPTION (insocketstream_error, os.str()); | |
61 | } | |
62 | ||
63 | struct sockaddr_un server_adr; | |
64 | server_adr.sun_family=AF_UNIX; | |
65 | strncpy(server_adr.sun_path,unixsocket.c_str(),sizeof(server_adr.sun_path)); | |
9f85ad54 | 66 | server_adr.sun_path[sizeof(server_adr.sun_path)-1]=0; |
e93545dd GE |
67 | |
68 | if(connect(sock,(struct sockaddr *) &server_adr, sizeof(server_adr))) | |
69 | { | |
70 | std::ostringstream os; | |
71 | os << "can't connect to socket: " << strerror(errno); | |
72 | throw EXCEPTION (insocketstream_error, os.str()); | |
73 | } | |
74 | ||
75 | setg (&buffer, &buffer, &buffer); // force underflow | |
76 | } | |
77 | ||
78 | ~insocketstream() | |
79 | { | |
80 | if (sock != -1) | |
81 | { | |
82 | shutdown(sock,SHUT_RDWR); | |
83 | close(sock); | |
84 | } | |
85 | ||
86 | sock = -1; | |
87 | } | |
88 | ||
89 | protected: | |
90 | virtual int_type underflow() | |
91 | { | |
92 | if (gptr() < egptr()) | |
93 | return std::streambuf::traits_type::to_int_type(*gptr()); | |
94 | ||
95 | int nbytes=::read(sock,&buffer,1); | |
96 | ||
97 | if (nbytes==-1) | |
98 | { | |
99 | if (errno == EINTR) | |
100 | return std::streambuf::traits_type::to_int_type(*gptr()); | |
101 | else | |
102 | { | |
103 | std::ostringstream os; | |
104 | os << "error reading form socket: " << strerror(errno); | |
105 | throw EXCEPTION (insocketstream_error, os.str()); | |
106 | } | |
107 | } | |
108 | if (nbytes==0) | |
109 | return EOF; | |
110 | ||
111 | setg (&buffer, &buffer, &buffer+sizeof(char)); | |
112 | return std::streambuf::traits_type::to_int_type(*gptr()); | |
113 | } | |
114 | }; | |
115 | ||
116 | #endif |