1 /* This file is part of Metaproxy.
2 Copyright (C) 2005-2008 Index Data
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
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
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
30 #include <netinet/in.h>
32 #include <arpa/inet.h>
33 #include <netinet/tcp.h>
39 #include <sys/socket.h>
42 #include <sys/select.h>
45 #include <boost/thread/thread.hpp>
46 #include <boost/thread/mutex.hpp>
47 #include <boost/thread/condition.hpp>
53 #include <yazpp/socket-observer.h>
58 namespace mp = metaproxy_1;
60 namespace metaproxy_1 {
61 class Pipe::Rep : public boost::noncopyable {
73 void Pipe::Rep::close(int &fd)
87 m_fd[0] = m_fd[1] = -1;
91 bool Pipe::Rep::nonblock(int s)
94 unsigned long tru = 1;
95 if (ioctlsocket(s, FIONBIO, &tru) < 0)
98 if (fcntl(s, F_SETFL, O_NONBLOCK) < 0)
101 signal (SIGPIPE, SIG_IGN);
107 Pipe::Pipe(int port_to_use) : m_p(new Rep)
111 WORD wVersionRequested = MAKEWORD(2, 0);
112 if (WSAStartup( wVersionRequested, &wsaData ))
113 throw Pipe::Error("WSAStartup failed");
115 port_to_use = 0; // we'll just use pipe on Unix
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");
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");
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;
136 if (bind(m_p->m_socket, addr, sizeof(struct sockaddr_in)))
137 throw Pipe::Error("could not bind on socket");
139 if (listen(m_p->m_socket, 3) < 0)
140 throw Pipe::Error("could not listen on socket");
144 tmpadd = (unsigned) inet_addr("127.0.0.1");
146 memcpy(&add.sin_addr.s_addr, &tmpadd, sizeof(struct in_addr));
148 throw Pipe::Error("inet_addr failed");
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");
154 m_p->nonblock(m_p->m_fd[1]);
156 if (connect(m_p->m_fd[1], addr, sizeof(*addr)) < 0)
159 if (WSAGetLastError() != WSAEWOULDBLOCK)
160 throw Pipe::Error("could not connect to socket");
162 if (errno != EINPROGRESS)
163 throw Pipe::Error("could not connect to socket");
168 struct sockaddr caddr;
170 int caddr_len = sizeof(caddr);
172 socklen_t caddr_len = sizeof(caddr);
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");
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);
184 throw Pipe::Error("could not complete connect");
186 m_p->close(m_p->m_socket);
192 throw Pipe::Error("pipe failed");
195 assert(m_p->m_fd[0] >= 0);
196 assert(m_p->m_fd[1] >= 0);
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);
212 int &Pipe::read_fd() const
217 int &Pipe::write_fd() const
225 * indent-tabs-mode: nil
226 * c-file-style: "stroustrup"
228 * vim: shiftwidth=4 tabstop=8 expandtab