Windows: use Boost 1.59, msvc 14.0
[metaproxy-moved-to-github.git] / src / pipe.cpp
1 /* This file is part of Metaproxy.
2    Copyright (C) Index Data
3
4 Metaproxy is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
7 version.
8
9 Metaproxy is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17 */
18
19 #include "config.hpp"
20
21 #if HAVE_UNISTD_H
22 #include <unistd.h>
23 #endif
24
25 #include <signal.h>
26 #include <errno.h>
27 #ifdef WIN32
28 #include <winsock.h>
29 #else
30 #include <netinet/in.h>
31 #include <netdb.h>
32 #include <arpa/inet.h>
33 #include <netinet/tcp.h>
34
35 #include <fcntl.h>
36 #endif
37
38 #if HAVE_SYS_SOCKET_H
39 #include <sys/socket.h>
40 #endif
41 #if HAVE_SYS_SELECT_H
42 #include <sys/select.h>
43 #endif
44
45 #include <boost/thread/thread.hpp>
46 #include <boost/thread/mutex.hpp>
47 #include <boost/thread/condition.hpp>
48
49 #include <stdio.h>
50 #include <string.h>
51
52 #include <deque>
53
54 #include <yazpp/socket-observer.h>
55 #include <yaz/log.h>
56
57 #include "pipe.hpp"
58
59 namespace mp = metaproxy_1;
60
61 namespace metaproxy_1 {
62     class Pipe::Rep : public boost::noncopyable {
63         friend class Pipe;
64         Rep();
65         int m_fd[2];
66         int m_socket;
67         bool nonblock(int s);
68         void close(int &fd);
69     };
70 }
71
72 using namespace mp;
73
74 void Pipe::Rep::close(int &fd)
75 {
76 #ifdef WIN32
77     if (fd != -1)
78         ::closesocket(fd);
79 #else
80     if (fd != -1)
81         ::close(fd);
82 #endif
83     fd = -1;
84 }
85
86 Pipe::Rep::Rep()
87 {
88     m_fd[0] = m_fd[1] = -1;
89     m_socket = -1;
90 }
91
92 bool Pipe::Rep::nonblock(int s)
93 {
94 #ifdef WIN32
95     unsigned long tru = 1;
96     if (ioctlsocket(s, FIONBIO, &tru) < 0)
97         return false;
98 #else
99     if (fcntl(s, F_SETFL, O_NONBLOCK) < 0)
100         return false;
101 #ifndef MSG_NOSIGNAL
102     signal (SIGPIPE, SIG_IGN);
103 #endif
104 #endif
105     return true;
106 }
107
108 Pipe::Pipe(int port_to_use) : m_p(new Rep)
109 {
110 #ifdef WIN32
111     WSADATA wsaData;
112     WORD wVersionRequested = MAKEWORD(2, 0);
113     if (WSAStartup( wVersionRequested, &wsaData ))
114         throw Pipe::Error("WSAStartup failed");
115 #else
116     port_to_use = 0;  // we'll just use pipe on Unix
117 #endif
118     if (port_to_use)
119     {
120         // create server socket
121         m_p->m_socket = socket(AF_INET, SOCK_STREAM, 0);
122         if (m_p->m_socket < 0)
123             throw Pipe::Error("could not create socket");
124 #ifndef WIN32
125         unsigned long one = 1;
126         if (setsockopt(m_p->m_socket, SOL_SOCKET, SO_REUSEADDR, (char*)
127                        &one, sizeof(one)) < 0)
128             throw Pipe::Error("setsockopt error");
129 #endif
130         // bind server socket
131         struct sockaddr_in add;
132         add.sin_family = AF_INET;
133         add.sin_port = htons(port_to_use);
134         add.sin_addr.s_addr = INADDR_ANY;
135         struct sockaddr *addr = ( struct sockaddr *) &add;
136
137         if (bind(m_p->m_socket, addr, sizeof(struct sockaddr_in)))
138             throw Pipe::Error("could not bind on socket");
139
140         if (listen(m_p->m_socket, 3) < 0)
141             throw Pipe::Error("could not listen on socket");
142
143         // client socket
144         unsigned int tmpadd;
145         tmpadd = (unsigned) inet_addr("127.0.0.1");
146         if (tmpadd)
147             memcpy(&add.sin_addr.s_addr, &tmpadd, sizeof(struct in_addr));
148         else
149             throw Pipe::Error("inet_addr failed");
150
151         m_p->m_fd[1] = socket(AF_INET, SOCK_STREAM, 0);
152         if (m_p->m_fd[1] < 0)
153             throw Pipe::Error("could not create socket");
154
155         m_p->nonblock(m_p->m_fd[1]);
156
157         if (connect(m_p->m_fd[1], addr, sizeof(*addr)) < 0)
158         {
159 #ifdef WIN32
160             if (WSAGetLastError() != WSAEWOULDBLOCK)
161                 throw Pipe::Error("could not connect to socket");
162 #else
163             if (errno != EINPROGRESS)
164                 throw Pipe::Error("could not connect to socket");
165 #endif
166         }
167
168         // server accept
169         struct sockaddr caddr;
170 #ifdef WIN32
171         int caddr_len = sizeof(caddr);
172 #else
173         socklen_t caddr_len = sizeof(caddr);
174 #endif
175         m_p->m_fd[0] = accept(m_p->m_socket, &caddr, &caddr_len);
176         if (m_p->m_fd[0] < 0)
177             throw Pipe::Error("could not accept on socket");
178
179         // complete connect
180         fd_set write_set;
181         FD_ZERO(&write_set);
182         FD_SET(m_p->m_fd[1], &write_set);
183         int r = select(m_p->m_fd[1]+1, 0, &write_set, 0, 0);
184         if (r != 1)
185             throw Pipe::Error("could not complete connect");
186
187         m_p->close(m_p->m_socket);
188     }
189     else
190     {
191 #ifndef WIN32
192         if (pipe(m_p->m_fd))
193             throw Pipe::Error("pipe failed");
194         else
195         {
196             assert(m_p->m_fd[0] >= 0);
197             assert(m_p->m_fd[1] >= 0);
198         }
199 #endif
200     }
201 }
202
203 Pipe::~Pipe()
204 {
205     m_p->close(m_p->m_fd[0]);
206     m_p->close(m_p->m_fd[1]);
207     m_p->close(m_p->m_socket);
208 #ifdef WIN32
209     WSACleanup();
210 #endif
211 }
212
213 int &Pipe::read_fd() const
214 {
215     return m_p->m_fd[0];
216 }
217
218 int &Pipe::write_fd() const
219 {
220     return m_p->m_fd[1];
221 }
222
223 /*
224  * Local variables:
225  * c-basic-offset: 4
226  * c-file-style: "Stroustrup"
227  * indent-tabs-mode: nil
228  * End:
229  * vim: shiftwidth=4 tabstop=8 expandtab
230  */
231