Updated footer comment
[metaproxy-moved-to-github.git] / src / pipe.cpp
1 /* This file is part of Metaproxy.
2    Copyright (C) 2005-2008 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
51 #include <deque>
52
53 #include <yazpp/socket-observer.h>
54 #include <yaz/log.h>
55
56 #include "pipe.hpp"
57
58 namespace mp = metaproxy_1;
59
60 namespace metaproxy_1 {
61     class Pipe::Rep : public boost::noncopyable {
62         friend class Pipe;
63         Rep();
64         int m_fd[2];
65         int m_socket;
66         bool nonblock(int s);
67         void close(int &fd);
68     };
69 }
70
71 using namespace mp;
72
73 void Pipe::Rep::close(int &fd)
74 {
75 #ifdef WIN32
76     if (fd != -1)
77         ::closesocket(fd);
78 #else
79     if (fd != -1)
80         ::close(fd);
81 #endif
82     fd = -1;
83 }
84
85 Pipe::Rep::Rep()
86 {
87     m_fd[0] = m_fd[1] = -1;
88     m_socket = -1;
89 }
90
91 bool Pipe::Rep::nonblock(int s)
92 {
93 #ifdef WIN32
94     unsigned long tru = 1;
95     if (ioctlsocket(s, FIONBIO, &tru) < 0)
96         return false;
97 #else
98     if (fcntl(s, F_SETFL, O_NONBLOCK) < 0)
99         return false;
100 #ifndef MSG_NOSIGNAL
101     signal (SIGPIPE, SIG_IGN);
102 #endif
103 #endif
104     return true;
105 }
106
107 Pipe::Pipe(int port_to_use) : m_p(new Rep)
108 {
109 #ifdef WIN32
110     WSADATA wsaData;
111     WORD wVersionRequested = MAKEWORD(2, 0);
112     if (WSAStartup( wVersionRequested, &wsaData ))
113         throw Pipe::Error("WSAStartup failed");
114 #else
115     port_to_use = 0;  // we'll just use pipe on Unix
116 #endif
117     if (port_to_use)
118     {
119         // create server socket
120         m_p->m_socket = socket(AF_INET, SOCK_STREAM, 0);
121         if (m_p->m_socket < 0)
122             throw Pipe::Error("could not create socket");
123 #ifndef WIN32
124         unsigned long one = 1;
125         if (setsockopt(m_p->m_socket, SOL_SOCKET, SO_REUSEADDR, (char*) 
126                        &one, sizeof(one)) < 0)
127             throw Pipe::Error("setsockopt error");
128 #endif
129         // bind server socket
130         struct sockaddr_in add;
131         add.sin_family = AF_INET;
132         add.sin_port = htons(port_to_use);
133         add.sin_addr.s_addr = INADDR_ANY;
134         struct sockaddr *addr = ( struct sockaddr *) &add;
135       
136         if (bind(m_p->m_socket, addr, sizeof(struct sockaddr_in)))
137             throw Pipe::Error("could not bind on socket");
138         
139         if (listen(m_p->m_socket, 3) < 0)
140             throw Pipe::Error("could not listen on socket");
141
142         // client socket
143         unsigned int tmpadd;
144         tmpadd = (unsigned) inet_addr("127.0.0.1");
145         if (tmpadd)
146             memcpy(&add.sin_addr.s_addr, &tmpadd, sizeof(struct in_addr));
147         else
148             throw Pipe::Error("inet_addr failed");
149             
150         m_p->m_fd[1] = socket(AF_INET, SOCK_STREAM, 0);
151         if (m_p->m_fd[1] < 0)
152             throw Pipe::Error("could not create socket");
153         
154         m_p->nonblock(m_p->m_fd[1]);
155
156         if (connect(m_p->m_fd[1], addr, sizeof(*addr)) < 0)
157         {
158 #ifdef WIN32
159             if (WSAGetLastError() != WSAEWOULDBLOCK)
160                 throw Pipe::Error("could not connect to socket");
161 #else
162             if (errno != EINPROGRESS)
163                 throw Pipe::Error("could not connect to socket");
164 #endif
165         }
166
167         // server accept
168         struct sockaddr caddr;
169 #ifdef WIN32
170         int caddr_len = sizeof(caddr);
171 #else
172         socklen_t caddr_len = sizeof(caddr);
173 #endif
174         m_p->m_fd[0] = accept(m_p->m_socket, &caddr, &caddr_len);
175         if (m_p->m_fd[0] < 0)
176             throw Pipe::Error("could not accept on socket");
177
178         // complete connect
179         fd_set write_set;
180         FD_ZERO(&write_set);
181         FD_SET(m_p->m_fd[1], &write_set);
182         int r = select(m_p->m_fd[1]+1, 0, &write_set, 0, 0);
183         if (r != 1)
184             throw Pipe::Error("could not complete connect");
185
186         m_p->close(m_p->m_socket);
187     }
188     else
189     {
190 #ifndef WIN32
191         if (pipe(m_p->m_fd))
192             throw Pipe::Error("pipe failed");
193         else
194         {
195             assert(m_p->m_fd[0] >= 0);
196             assert(m_p->m_fd[1] >= 0);
197         }
198 #endif
199     }
200 }
201
202 Pipe::~Pipe()
203 {
204     m_p->close(m_p->m_fd[0]);
205     m_p->close(m_p->m_fd[1]);
206     m_p->close(m_p->m_socket);
207 #ifdef WIN32
208     WSACleanup();
209 #endif
210 }
211
212 int &Pipe::read_fd() const
213 {
214     return m_p->m_fd[0];
215 }
216
217 int &Pipe::write_fd() const
218 {
219     return m_p->m_fd[1];
220 }
221
222 /*
223  * Local variables:
224  * c-basic-offset: 4
225  * c-file-style: "Stroustrup"
226  * indent-tabs-mode: nil
227  * End:
228  * vim: shiftwidth=4 tabstop=8 expandtab
229  */
230