udp_engine.cpp 14.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
/*
Copyright (c) 2007-2016 Contributors as noted in the AUTHORS file

This file is part of libzmq, the ZeroMQ core engine in C++.

libzmq is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License (LGPL) as published
by the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.

As a special exception, the Contributors give you permission to link
this library with independent modules to produce an executable,
regardless of the license terms of these independent modules, and to
copy and distribute the resulting executable under terms of your choice,
provided that you also meet, for each linked independent module, the
terms and conditions of the license of that module. An independent
module is a module which is not derived from or based on this library.
If you modify this library, you must extend this exception to your
version of the library.

libzmq is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
License for more details.

You should have received a copy of the GNU Lesser General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "precompiled.hpp"
31

32
#if !defined ZMQ_HAVE_WINDOWS
33
#include <sys/types.h>
34
#include <unistd.h>
35 36 37
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
38 39 40
#ifdef ZMQ_HAVE_VXWORKS
#include <sockLib.h>
#endif
41 42 43 44 45 46 47 48
#endif

#include "udp_engine.hpp"
#include "session_base.hpp"
#include "v2_protocol.hpp"
#include "err.hpp"
#include "ip.hpp"

49 50 51 52 53
//  OSX uses a different name for this socket option
#ifndef IPV6_ADD_MEMBERSHIP
#define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP
#endif

54
zmq::udp_engine_t::udp_engine_t (const options_t &options_) :
55
    plugged (false),
56 57 58 59 60 61 62
    fd (-1),
    session (NULL),
    handle ((handle_t) NULL),
    address (NULL),
    options (options_),
    send_enabled (false),
    recv_enabled (false)
63 64 65
{
}

66
zmq::udp_engine_t::~udp_engine_t ()
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
{
    zmq_assert (!plugged);

    if (fd != retired_fd) {
#ifdef ZMQ_HAVE_WINDOWS
        int rc = closesocket (fd);
        wsa_assert (rc != SOCKET_ERROR);
#else
        int rc = close (fd);
        errno_assert (rc == 0);
#endif
        fd = retired_fd;
    }
}

82
int zmq::udp_engine_t::init (address_t *address_, bool send_, bool recv_)
83 84 85 86 87 88 89
{
    zmq_assert (address_);
    zmq_assert (send_ || recv_);
    send_enabled = send_;
    recv_enabled = recv_;
    address = address_;

90 91
    fd = open_socket (address->resolved.udp_addr->family (), SOCK_DGRAM,
                      IPPROTO_UDP);
92 93 94 95 96 97 98 99
    if (fd == retired_fd)
        return -1;

    unblock_socket (fd);

    return 0;
}

100
void zmq::udp_engine_t::plug (io_thread_t *io_thread_, session_base_t *session_)
101 102 103 104 105 106 107 108 109 110 111 112
{
    zmq_assert (!plugged);
    plugged = true;

    zmq_assert (!session);
    zmq_assert (session_);
    session = session_;

    //  Connect to I/O threads poller object.
    io_object_t::plug (io_thread_);
    handle = add_fd (fd);

113
    const udp_address_t *const udp_addr = address->resolved.udp_addr;
114

115 116 117 118
    // Bind the socket to a device if applicable
    if (!options.bound_device.empty ())
        bind_to_device (fd, options.bound_device);

119 120
    if (send_enabled) {
        if (!options.raw_socket) {
121
            const ip_addr_t *out = udp_addr->target_addr ();
122 123
            out_address = out->as_sockaddr ();
            out_addrlen = out->sockaddr_len ();
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140

            if (out->is_multicast ()) {
                int level;
                int optname;

                if (out->family () == AF_INET6) {
                    level = IPPROTO_IPV6;
                    optname = IPV6_MULTICAST_LOOP;
                } else {
                    level = IPPROTO_IP;
                    optname = IP_MULTICAST_LOOP;
                }

                int loop = options.multicast_loop;
                int rc = setsockopt (fd, level, optname, (char *) &loop,
                                     sizeof (loop));

141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
#ifdef ZMQ_HAVE_WINDOWS
                wsa_assert (rc != SOCKET_ERROR);
#else
                errno_assert (rc == 0);
#endif

                if (out->family () == AF_INET6) {
                    int bind_if = udp_addr->bind_if ();

                    if (bind_if > 0) {
                        //  If a bind interface is provided we tell the
                        //  kernel to use it to send multicast packets
                        rc = setsockopt (fd, IPPROTO_IPV6, IPV6_MULTICAST_IF,
                                         (char *) &bind_if, sizeof (bind_if));
                    } else {
                        rc = 0;
                    }
                } else {
                    struct in_addr bind_addr =
                      udp_addr->bind_addr ()->ipv4.sin_addr;

                    if (bind_addr.s_addr != INADDR_ANY) {
                        rc =
                          setsockopt (fd, IPPROTO_IP, IP_MULTICAST_IF,
                                      (char *) &bind_addr, sizeof (bind_addr));
                    } else {
                        rc = 0;
                    }
                }

171 172 173 174 175 176
#ifdef ZMQ_HAVE_WINDOWS
                wsa_assert (rc != SOCKET_ERROR);
#else
                errno_assert (rc == 0);
#endif
            }
177
        } else {
178
            /// XXX fixme ?
179 180 181 182
            out_address = (sockaddr *) &raw_address;
            out_addrlen = sizeof (sockaddr_in);
        }

183
        set_pollout (handle);
184
    }
185 186 187

    if (recv_enabled) {
        int on = 1;
188 189
        int rc =
          setsockopt (fd, SOL_SOCKET, SO_REUSEADDR, (char *) &on, sizeof (on));
190 191 192 193 194 195
#ifdef ZMQ_HAVE_WINDOWS
        wsa_assert (rc != SOCKET_ERROR);
#else
        errno_assert (rc == 0);
#endif

196
        const ip_addr_t *bind_addr = udp_addr->bind_addr ();
197 198 199
        ip_addr_t any = ip_addr_t::any (bind_addr->family ());
        const ip_addr_t *real_bind_addr;

200
        bool multicast = udp_addr->is_mcast ();
201 202 203 204 205 206 207 208 209 210 211

        if (multicast) {
            //  In multicast we should bind ANY and use the mreq struct to
            //  specify the interface
            any.set_port (bind_addr->port ());

            real_bind_addr = &any;
        } else {
            real_bind_addr = bind_addr;
        }

212
#ifdef ZMQ_HAVE_VXWORKS
213 214
        rc = bind (fd, (sockaddr *) real_bind_addr->as_sockaddr (),
                   real_bind_addr->sockaddr_len ());
215
#else
216 217 218
        rc = bind (fd, real_bind_addr->as_sockaddr (),
                   real_bind_addr->sockaddr_len ());

219
#endif
220 221 222 223 224
#ifdef ZMQ_HAVE_WINDOWS
        wsa_assert (rc != SOCKET_ERROR);
#else
        errno_assert (rc == 0);
#endif
225

226
        if (multicast) {
227
            const ip_addr_t *mcast_addr = udp_addr->target_addr ();
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255

            if (mcast_addr->family () == AF_INET) {
                struct ip_mreq mreq;
                mreq.imr_multiaddr = mcast_addr->ipv4.sin_addr;
                mreq.imr_interface = bind_addr->ipv4.sin_addr;

                rc = setsockopt (fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
                                 (char *) &mreq, sizeof (mreq));

                errno_assert (rc == 0);
            } else if (mcast_addr->family () == AF_INET6) {
                struct ipv6_mreq mreq;
                int iface = address->resolved.udp_addr->bind_if ();

                zmq_assert (iface >= -1);

                mreq.ipv6mr_multiaddr = mcast_addr->ipv6.sin6_addr;
                mreq.ipv6mr_interface = iface;

                rc = setsockopt (fd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP,
                                 (char *) &mreq, sizeof (mreq));

                errno_assert (rc == 0);
            } else {
                //  Shouldn't happen
                abort ();
            }

256 257 258 259 260 261 262 263 264 265 266 267 268
#ifdef ZMQ_HAVE_WINDOWS
            wsa_assert (rc != SOCKET_ERROR);
#else
            errno_assert (rc == 0);
#endif
        }
        set_pollin (handle);

        //  Call restart output to drop all join/leave commands
        restart_output ();
    }
}

269
void zmq::udp_engine_t::terminate ()
270 271 272 273 274 275 276 277 278 279 280 281
{
    zmq_assert (plugged);
    plugged = false;

    rm_fd (handle);

    //  Disconnect from I/O threads poller object.
    io_object_t::unplug ();

    delete this;
}

282
void zmq::udp_engine_t::sockaddr_to_msg (zmq::msg_t *msg, sockaddr_in *addr)
283
{
284
    char *name = inet_ntoa (addr->sin_addr);
285 286

    char port[6];
287
    sprintf (port, "%d", (int) ntohs (addr->sin_port));
288

289 290
    int size =
      (int) strlen (name) + (int) strlen (port) + 1 + 1; //  Colon + NULL
291 292 293
    int rc = msg->init_size (size);
    errno_assert (rc == 0);
    msg->set_flags (msg_t::more);
294
    char *address = (char *) msg->data ();
295 296 297 298 299 300

    strcpy (address, name);
    strcat (address, ":");
    strcat (address, port);
}

301
int zmq::udp_engine_t::resolve_raw_address (char *name_, size_t length_)
302
{
303 304 305 306 307 308
    memset (&raw_address, 0, sizeof raw_address);

    const char *delimiter = NULL;

    // Find delimiter, cannot use memrchr as it is not supported on windows
    if (length_ != 0) {
309
        int chars_left = (int) length_;
310 311 312 313 314 315 316 317 318
        char *current_char = name_ + length_;
        do {
            if (*(--current_char) == ':') {
                delimiter = current_char;
                break;
            }
        } while (--chars_left != 0);
    }

319 320 321 322 323 324
    if (!delimiter) {
        errno = EINVAL;
        return -1;
    }

    std::string addr_str (name_, delimiter - name_);
325
    std::string port_str (delimiter + 1, name_ + length_ - delimiter - 1);
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345

    //  Parse the port number (0 is not a valid port).
    uint16_t port = (uint16_t) atoi (port_str.c_str ());
    if (port == 0) {
        errno = EINVAL;
        return -1;
    }

    raw_address.sin_family = AF_INET;
    raw_address.sin_port = htons (port);
    raw_address.sin_addr.s_addr = inet_addr (addr_str.c_str ());

    if (raw_address.sin_addr.s_addr == INADDR_NONE) {
        errno = EINVAL;
        return -1;
    }

    return 0;
}

346
void zmq::udp_engine_t::out_event ()
347 348 349 350 351 352 353 354 355 356 357
{
    msg_t group_msg;
    int rc = session->pull_msg (&group_msg);
    errno_assert (rc == 0 || (rc == -1 && errno == EAGAIN));

    if (rc == 0) {
        msg_t body_msg;
        rc = session->pull_msg (&body_msg);

        size_t group_size = group_msg.size ();
        size_t body_size = body_msg.size ();
358
        size_t size;
359

360
        if (options.raw_socket) {
361
            rc = resolve_raw_address ((char *) group_msg.data (), group_size);
362 363 364 365 366 367 368 369 370 371

            //  We discard the message if address is not valid
            if (rc != 0) {
                rc = group_msg.close ();
                errno_assert (rc == 0);

                body_msg.close ();
                errno_assert (rc == 0);

                return;
372
            }
373 374 375

            size = body_size;

376
            memcpy (out_buffer, body_msg.data (), body_size);
377
        } else {
378 379
            size = group_size + body_size + 1;

380 381 382 383 384
            // TODO: check if larger than maximum size
            out_buffer[0] = (unsigned char) group_size;
            memcpy (out_buffer + 1, group_msg.data (), group_size);
            memcpy (out_buffer + 1 + group_size, body_msg.data (), body_size);
        }
385 386 387 388 389 390 391

        rc = group_msg.close ();
        errno_assert (rc == 0);

        body_msg.close ();
        errno_assert (rc == 0);

392
#ifdef ZMQ_HAVE_WINDOWS
393 394
        rc = sendto (fd, (const char *) out_buffer, (int) size, 0, out_address,
                     (int) out_addrlen);
395
        wsa_assert (rc != SOCKET_ERROR);
396 397 398 399
#elif defined ZMQ_HAVE_VXWORKS
        rc = sendto (fd, (caddr_t) out_buffer, size, 0,
                     (sockaddr *) out_address, (int) out_addrlen);
        errno_assert (rc != -1);
400
#else
401
        rc = sendto (fd, out_buffer, size, 0, out_address, out_addrlen);
402
        errno_assert (rc != -1);
403
#endif
404 405
    } else
        reset_pollout (handle);
406 407
}

408 409 410 411 412
const char *zmq::udp_engine_t::get_endpoint () const
{
    return "";
}

413
void zmq::udp_engine_t::restart_output ()
414 415 416 417 418 419
{
    //  If we don't support send we just drop all messages
    if (!send_enabled) {
        msg_t msg;
        while (session->pull_msg (&msg) == 0)
            msg.close ();
420 421
    } else {
        set_pollout (handle);
422 423 424 425
        out_event ();
    }
}

426
void zmq::udp_engine_t::in_event ()
427
{
428 429
    sockaddr_storage in_address;
    socklen_t in_addrlen = sizeof (sockaddr_storage);
430
#ifdef ZMQ_HAVE_WINDOWS
431 432 433
    int nbytes = recvfrom (fd, (char *) in_buffer, MAX_UDP_MSG, 0,
                           (sockaddr *) &in_address, &in_addrlen);
    const int last_error = WSAGetLastError ();
434
    if (nbytes == SOCKET_ERROR) {
435 436
        wsa_assert (last_error == WSAENETDOWN || last_error == WSAENETRESET
                    || last_error == WSAEWOULDBLOCK);
437 438
        return;
    }
439 440 441 442 443 444 445 446
#elif defined ZMQ_HAVE_VXWORKS
    int nbytes = recvfrom (fd, (char *) in_buffer, MAX_UDP_MSG, 0,
                           (sockaddr *) &in_address, (int *) &in_addrlen);
    if (nbytes == -1) {
        errno_assert (errno != EBADF && errno != EFAULT && errno != ENOMEM
                      && errno != ENOTSOCK);
        return;
    }
447
#else
448 449
    int nbytes = recvfrom (fd, in_buffer, MAX_UDP_MSG, 0,
                           (sockaddr *) &in_address, &in_addrlen);
450
    if (nbytes == -1) {
451 452
        errno_assert (errno != EBADF && errno != EFAULT && errno != ENOMEM
                      && errno != ENOTSOCK);
453 454 455
        return;
    }
#endif
456
    int rc;
457
    int body_size;
458
    int body_offset;
459
    msg_t msg;
460

461
    if (options.raw_socket) {
462 463
        zmq_assert (in_address.ss_family == AF_INET);
        sockaddr_to_msg (&msg, reinterpret_cast<sockaddr_in *> (&in_address));
464 465 466

        body_size = nbytes;
        body_offset = 0;
467 468
    } else {
        char *group_buffer = (char *) in_buffer + 1;
469 470 471 472 473 474
        int group_size = in_buffer[0];

        rc = msg.init_size (group_size);
        errno_assert (rc == 0);
        msg.set_flags (msg_t::more);
        memcpy (msg.data (), group_buffer, group_size);
475

476 477 478
        //  This doesn't fit, just ingore
        if (nbytes - 1 < group_size)
            return;
479

480
        body_size = nbytes - 1 - group_size;
481
        body_offset = 1 + group_size;
482
    }
483
    // Push group description to session
484 485
    rc = session->push_msg (&msg);
    errno_assert (rc == 0 || (rc == -1 && errno == EAGAIN));
486

487
    //  Group description message doesn't fit in the pipe, drop
488
    if (rc != 0) {
489 490
        rc = msg.close ();
        errno_assert (rc == 0);
491 492 493

        reset_pollin (handle);
        return;
494
    }
495 496 497 498 499

    rc = msg.close ();
    errno_assert (rc == 0);
    rc = msg.init_size (body_size);
    errno_assert (rc == 0);
500
    memcpy (msg.data (), in_buffer + body_offset, body_size);
501 502

    // Push message body to session
503
    rc = session->push_msg (&msg);
504
    // Message body doesn't fit in the pipe, drop and reset session state
505 506 507 508 509 510 511 512 513
    if (rc != 0) {
        rc = msg.close ();
        errno_assert (rc == 0);

        session->reset ();
        reset_pollin (handle);
        return;
    }

514 515 516
    rc = msg.close ();
    errno_assert (rc == 0);
    session->flush ();
517 518
}

519
void zmq::udp_engine_t::restart_input ()
520 521 522 523 524 525 526
{
    if (!recv_enabled)
        return;

    set_pollin (handle);
    in_event ();
}