~ubuntu-branches/ubuntu/saucy/zeromq3/saucy

« back to all changes in this revision

Viewing changes to .pc/03_fix-test_shutdown_stress-segfault.patch/src/tcp_listener.cpp

  • Committer: Package Import Robot
  • Author(s): Alessandro Ghedini
  • Date: 2012-10-16 19:49:30 UTC
  • mfrom: (1.1.2)
  • Revision ID: package-import@ubuntu.com-20121016194930-98r0bi746eoaa4iv
Tags: 3.2.1~rc2+dfsg-1
* New upstream RC release (Closes: #690704)
* Bump Standards-Version to 3.9.4 (no changes needed)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
    Copyright (c) 2009-2011 250bpm s.r.o.
3
 
    Copyright (c) 2007-2010 iMatix Corporation
4
 
    Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file
5
 
 
6
 
    This file is part of 0MQ.
7
 
 
8
 
    0MQ is free software; you can redistribute it and/or modify it under
9
 
    the terms of the GNU Lesser General Public License as published by
10
 
    the Free Software Foundation; either version 3 of the License, or
11
 
    (at your option) any later version.
12
 
 
13
 
    0MQ is distributed in the hope that it will be useful,
14
 
    but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 
    GNU Lesser General Public License for more details.
17
 
 
18
 
    You should have received a copy of the GNU Lesser General Public License
19
 
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
 
*/
21
 
 
22
 
#include <new>
23
 
 
24
 
#include <string>
25
 
 
26
 
#include "platform.hpp"
27
 
#include "tcp_listener.hpp"
28
 
#include "stream_engine.hpp"
29
 
#include "io_thread.hpp"
30
 
#include "session_base.hpp"
31
 
#include "config.hpp"
32
 
#include "err.hpp"
33
 
#include "ip.hpp"
34
 
#include "socket_base.hpp"
35
 
 
36
 
#ifdef ZMQ_HAVE_WINDOWS
37
 
#include "windows.hpp"
38
 
#else
39
 
#include <unistd.h>
40
 
#include <sys/socket.h>
41
 
#include <arpa/inet.h>
42
 
#include <netinet/tcp.h>
43
 
#include <netinet/in.h>
44
 
#include <netdb.h>
45
 
#include <fcntl.h>
46
 
#endif
47
 
 
48
 
#ifdef ZMQ_HAVE_OPENVMS
49
 
#include <ioctl.h>
50
 
#endif
51
 
 
52
 
zmq::tcp_listener_t::tcp_listener_t (io_thread_t *io_thread_,
53
 
      socket_base_t *socket_, const options_t &options_) :
54
 
    own_t (io_thread_, options_),
55
 
    io_object_t (io_thread_),
56
 
    s (retired_fd),
57
 
    socket (socket_)
58
 
{
59
 
}
60
 
 
61
 
zmq::tcp_listener_t::~tcp_listener_t ()
62
 
{
63
 
    if (s != retired_fd)
64
 
        close ();
65
 
}
66
 
 
67
 
void zmq::tcp_listener_t::process_plug ()
68
 
{
69
 
    //  Start polling for incoming connections.
70
 
    handle = add_fd (s);
71
 
    set_pollin (handle);
72
 
}
73
 
 
74
 
void zmq::tcp_listener_t::process_term (int linger_)
75
 
{
76
 
    rm_fd (handle);
77
 
    close ();
78
 
    own_t::process_term (linger_);
79
 
}
80
 
 
81
 
void zmq::tcp_listener_t::in_event ()
82
 
{
83
 
    fd_t fd = accept ();
84
 
 
85
 
    //  If connection was reset by the peer in the meantime, just ignore it.
86
 
    //  TODO: Handle specific errors like ENFILE/EMFILE etc.
87
 
    if (fd == retired_fd) {
88
 
        socket->monitor_event (ZMQ_EVENT_ACCEPT_FAILED, endpoint.c_str(), zmq_errno());
89
 
        return;
90
 
    }
91
 
 
92
 
    tune_tcp_socket (fd);
93
 
    tune_tcp_keepalives (fd, options.tcp_keepalive, options.tcp_keepalive_cnt, options.tcp_keepalive_idle, options.tcp_keepalive_intvl);
94
 
 
95
 
    //  Create the engine object for this connection.
96
 
    stream_engine_t *engine = new (std::nothrow) stream_engine_t (fd, options);
97
 
    alloc_assert (engine);
98
 
 
99
 
    //  Choose I/O thread to run connecter in. Given that we are already
100
 
    //  running in an I/O thread, there must be at least one available.
101
 
    io_thread_t *io_thread = choose_io_thread (options.affinity);
102
 
    zmq_assert (io_thread);
103
 
 
104
 
    //  Create and launch a session object. 
105
 
    session_base_t *session = session_base_t::create (io_thread, false, socket,
106
 
        options, NULL);
107
 
    errno_assert (session);
108
 
    session->inc_seqnum ();
109
 
    launch_child (session);
110
 
    send_attach (session, engine, false);
111
 
    socket->monitor_event (ZMQ_EVENT_ACCEPTED, endpoint.c_str(), fd);
112
 
}
113
 
 
114
 
void zmq::tcp_listener_t::close ()
115
 
{
116
 
    zmq_assert (s != retired_fd);
117
 
#ifdef ZMQ_HAVE_WINDOWS
118
 
    int rc = closesocket (s);
119
 
    if (unlikely (rc != SOCKET_ERROR))
120
 
        socket->monitor_event (ZMQ_EVENT_CLOSE_FAILED, endpoint.c_str(), zmq_errno());
121
 
    wsa_assert (rc != SOCKET_ERROR);
122
 
#else
123
 
    int rc = ::close (s);
124
 
    if (unlikely (rc == 0))
125
 
        socket->monitor_event (ZMQ_EVENT_CLOSE_FAILED, endpoint.c_str(), zmq_errno());
126
 
    errno_assert (rc == 0);
127
 
#endif
128
 
    socket->monitor_event (ZMQ_EVENT_CLOSED, endpoint.c_str(), s);
129
 
    s = retired_fd;
130
 
}
131
 
 
132
 
int zmq::tcp_listener_t::get_address (std::string &addr_)
133
 
{
134
 
    // Get the details of the TCP socket
135
 
    struct sockaddr_storage ss;
136
 
    socklen_t sl = sizeof (ss);
137
 
    int rc = getsockname (s, (struct sockaddr *) &ss, &sl);
138
 
 
139
 
    if (rc != 0) {
140
 
        addr_.clear ();
141
 
        return rc;
142
 
    }
143
 
 
144
 
    tcp_address_t addr ((struct sockaddr *) &ss, sl);
145
 
    return addr.to_string (addr_);
146
 
}
147
 
 
148
 
int zmq::tcp_listener_t::set_address (const char *addr_)
149
 
{
150
 
    //  Convert the textual address into address structure.
151
 
    int rc = address.resolve (addr_, true, options.ipv4only ? true : false);
152
 
    if (rc != 0)
153
 
        return -1;
154
 
 
155
 
    //  Create a listening socket.
156
 
    s = open_socket (address.family (), SOCK_STREAM, IPPROTO_TCP);
157
 
#ifdef ZMQ_HAVE_WINDOWS
158
 
    if (s == INVALID_SOCKET)
159
 
        errno = wsa_error_to_errno (WSAGetLastError ());
160
 
#endif
161
 
 
162
 
    //  IPv6 address family not supported, try automatic downgrade to IPv4.
163
 
    if (address.family () == AF_INET6 && errno == EAFNOSUPPORT &&
164
 
          !options.ipv4only) {
165
 
        rc = address.resolve (addr_, true, true);
166
 
        if (rc != 0)
167
 
            return rc;
168
 
        s = ::socket (address.family (), SOCK_STREAM, IPPROTO_TCP);
169
 
    }
170
 
 
171
 
#ifdef ZMQ_HAVE_WINDOWS
172
 
    if (s == INVALID_SOCKET) {
173
 
        errno = wsa_error_to_errno (WSAGetLastError ());
174
 
        return -1;
175
 
    }
176
 
    //  On Windows, preventing sockets to be inherited by child processes.
177
 
    BOOL brc = SetHandleInformation ((HANDLE) s, HANDLE_FLAG_INHERIT, 0);
178
 
    win_assert (brc);
179
 
#else
180
 
    if (s == -1)
181
 
        return -1;
182
 
#endif
183
 
 
184
 
    //  On some systems, IPv4 mapping in IPv6 sockets is disabled by default.
185
 
    //  Switch it on in such cases.
186
 
    if (address.family () == AF_INET6)
187
 
        enable_ipv4_mapping (s);
188
 
 
189
 
    //  Allow reusing of the address.
190
 
    int flag = 1;
191
 
#ifdef ZMQ_HAVE_WINDOWS
192
 
    rc = setsockopt (s, SOL_SOCKET, SO_EXCLUSIVEADDRUSE,
193
 
        (const char*) &flag, sizeof (int));
194
 
    wsa_assert (rc != SOCKET_ERROR);
195
 
#else
196
 
    rc = setsockopt (s, SOL_SOCKET, SO_REUSEADDR, &flag, sizeof (int));
197
 
    errno_assert (rc == 0);
198
 
#endif
199
 
 
200
 
    address.to_string (endpoint);
201
 
 
202
 
    //  Bind the socket to the network interface and port.
203
 
    rc = bind (s, address.addr (), address.addrlen ());
204
 
#ifdef ZMQ_HAVE_WINDOWS
205
 
    if (rc == SOCKET_ERROR) {
206
 
        errno = wsa_error_to_errno (WSAGetLastError ());
207
 
        return -1;
208
 
    }
209
 
#else
210
 
    if (rc != 0)
211
 
        return -1;
212
 
#endif
213
 
 
214
 
    //  Listen for incomming connections.
215
 
    rc = listen (s, options.backlog);
216
 
#ifdef ZMQ_HAVE_WINDOWS
217
 
    if (rc == SOCKET_ERROR) {
218
 
        errno = wsa_error_to_errno (WSAGetLastError ());
219
 
        return -1;
220
 
    }
221
 
#else
222
 
    if (rc != 0)
223
 
        return -1;
224
 
#endif
225
 
 
226
 
    socket->monitor_event (ZMQ_EVENT_LISTENING, addr_, s);
227
 
    return 0;
228
 
}
229
 
 
230
 
zmq::fd_t zmq::tcp_listener_t::accept ()
231
 
{
232
 
    //  Accept one connection and deal with different failure modes.
233
 
    zmq_assert (s != retired_fd);
234
 
 
235
 
    struct sockaddr_storage ss = {0};
236
 
    socklen_t ss_len = sizeof (ss);
237
 
    fd_t sock = ::accept (s, (struct sockaddr *) &ss, &ss_len);
238
 
 
239
 
#ifdef ZMQ_HAVE_WINDOWS
240
 
    if (sock == INVALID_SOCKET) {
241
 
        wsa_assert (WSAGetLastError () == WSAEWOULDBLOCK ||
242
 
            WSAGetLastError () == WSAECONNRESET);
243
 
        return retired_fd;
244
 
    }
245
 
    //  On Windows, preventing sockets to be inherited by child processes.
246
 
    BOOL brc = SetHandleInformation ((HANDLE) sock, HANDLE_FLAG_INHERIT, 0);
247
 
    win_assert (brc);
248
 
#else
249
 
    if (sock == -1) {
250
 
        errno_assert (errno == EAGAIN || errno == EWOULDBLOCK ||
251
 
            errno == EINTR || errno == ECONNABORTED || errno == EPROTO ||
252
 
            errno == ENOBUFS);
253
 
        return retired_fd;
254
 
    }
255
 
#endif
256
 
 
257
 
    if (!options.tcp_accept_filters.empty ()) {
258
 
        bool matched = false;
259
 
        for (options_t::tcp_accept_filters_t::size_type i = 0; i != options.tcp_accept_filters.size (); ++i) {
260
 
            if (options.tcp_accept_filters[i].match_address ((struct sockaddr *) &ss, ss_len)) {
261
 
                matched = true;
262
 
                break;
263
 
            }
264
 
        }
265
 
        if (!matched) {
266
 
#ifdef ZMQ_HAVE_WINDOWS
267
 
            int rc = closesocket (sock);
268
 
            wsa_assert (rc != SOCKET_ERROR);
269
 
#else
270
 
            int rc = ::close (sock);
271
 
            errno_assert (rc == 0);
272
 
#endif
273
 
            return retired_fd;
274
 
        }
275
 
    }
276
 
 
277
 
    return sock;
278
 
}