1 /* This file is part of Metaproxy.
2 Copyright (C) 2005-2010 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>
54 #include <yazpp/socket-observer.h>
59 namespace mp = metaproxy_1;
61 namespace metaproxy_1 {
62 class Pipe::Rep : public boost::noncopyable {
74 void Pipe::Rep::close(int &fd)
88 m_fd[0] = m_fd[1] = -1;
92 bool Pipe::Rep::nonblock(int s)
95 unsigned long tru = 1;
96 if (ioctlsocket(s, FIONBIO, &tru) < 0)
99 if (fcntl(s, F_SETFL, O_NONBLOCK) < 0)
102 signal (SIGPIPE, SIG_IGN);
108 Pipe::Pipe(int port_to_use) : m_p(new Rep)
112 WORD wVersionRequested = MAKEWORD(2, 0);
113 if (WSAStartup( wVersionRequested, &wsaData ))
114 throw Pipe::Error("WSAStartup failed");
116 port_to_use = 0; // we'll just use pipe on Unix
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");
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");
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;
137 if (bind(m_p->m_socket, addr, sizeof(struct sockaddr_in)))
138 throw Pipe::Error("could not bind on socket");
140 if (listen(m_p->m_socket, 3) < 0)
141 throw Pipe::Error("could not listen on socket");
145 tmpadd = (unsigned) inet_addr("127.0.0.1");
147 memcpy(&add.sin_addr.s_addr, &tmpadd, sizeof(struct in_addr));
149 throw Pipe::Error("inet_addr failed");
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");
155 m_p->nonblock(m_p->m_fd[1]);
157 if (connect(m_p->m_fd[1], addr, sizeof(*addr)) < 0)
160 if (WSAGetLastError() != WSAEWOULDBLOCK)
161 throw Pipe::Error("could not connect to socket");
163 if (errno != EINPROGRESS)
164 throw Pipe::Error("could not connect to socket");
169 struct sockaddr caddr;
171 int caddr_len = sizeof(caddr);
173 socklen_t caddr_len = sizeof(caddr);
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");
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);
185 throw Pipe::Error("could not complete connect");
187 m_p->close(m_p->m_socket);
193 throw Pipe::Error("pipe failed");
196 assert(m_p->m_fd[0] >= 0);
197 assert(m_p->m_fd[1] >= 0);
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);
213 int &Pipe::read_fd() const
218 int &Pipe::write_fd() const
226 * c-file-style: "Stroustrup"
227 * indent-tabs-mode: nil
229 * vim: shiftwidth=4 tabstop=8 expandtab