2016-02-18 10:56:52 -06:00
|
|
|
/*
|
|
|
|
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"
|
2016-01-29 21:17:11 +02:00
|
|
|
|
2016-05-13 20:41:26 -07:00
|
|
|
#if !defined ZMQ_HAVE_WINDOWS
|
2016-01-29 21:17:11 +02:00
|
|
|
#include <sys/types.h>
|
2016-02-02 01:17:12 +05:00
|
|
|
#include <unistd.h>
|
2016-01-29 21:17:11 +02:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
2018-03-10 03:03:02 -08:00
|
|
|
#ifdef ZMQ_HAVE_VXWORKS
|
|
|
|
#include <sockLib.h>
|
|
|
|
#endif
|
2016-01-29 21:17:11 +02:00
|
|
|
#endif
|
|
|
|
|
2018-05-18 17:47:47 +02:00
|
|
|
#include "udp_address.hpp"
|
2016-01-29 21:17:11 +02:00
|
|
|
#include "udp_engine.hpp"
|
|
|
|
#include "session_base.hpp"
|
|
|
|
#include "err.hpp"
|
|
|
|
#include "ip.hpp"
|
|
|
|
|
2018-05-05 00:44:13 +02:00
|
|
|
// OSX uses a different name for this socket option
|
|
|
|
#ifndef IPV6_ADD_MEMBERSHIP
|
|
|
|
#define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP
|
|
|
|
#endif
|
|
|
|
|
2018-04-17 12:33:18 +10:00
|
|
|
#ifdef __APPLE__
|
|
|
|
#include <TargetConditionals.h>
|
|
|
|
#endif
|
|
|
|
|
2018-02-01 11:46:09 +01:00
|
|
|
zmq::udp_engine_t::udp_engine_t (const options_t &options_) :
|
2016-01-29 21:17:11 +02:00
|
|
|
plugged (false),
|
2018-02-01 11:46:09 +01:00
|
|
|
fd (-1),
|
|
|
|
session (NULL),
|
2018-05-18 15:54:00 +02:00
|
|
|
handle (static_cast<handle_t> (NULL)),
|
2018-02-01 11:46:09 +01:00
|
|
|
address (NULL),
|
|
|
|
options (options_),
|
|
|
|
send_enabled (false),
|
|
|
|
recv_enabled (false)
|
2016-01-29 21:17:11 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-02-01 11:46:09 +01:00
|
|
|
zmq::udp_engine_t::~udp_engine_t ()
|
2016-01-29 21:17:11 +02:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-07 12:10:17 +01:00
|
|
|
int zmq::udp_engine_t::init (address_t *address_, bool send_, bool recv_)
|
2016-01-29 21:17:11 +02:00
|
|
|
{
|
|
|
|
zmq_assert (address_);
|
|
|
|
zmq_assert (send_ || recv_);
|
|
|
|
send_enabled = send_;
|
|
|
|
recv_enabled = recv_;
|
|
|
|
address = address_;
|
|
|
|
|
2018-02-01 11:46:09 +01:00
|
|
|
fd = open_socket (address->resolved.udp_addr->family (), SOCK_DGRAM,
|
|
|
|
IPPROTO_UDP);
|
2016-01-29 21:17:11 +02:00
|
|
|
if (fd == retired_fd)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
unblock_socket (fd);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-02-01 11:46:09 +01:00
|
|
|
void zmq::udp_engine_t::plug (io_thread_t *io_thread_, session_base_t *session_)
|
2016-01-29 21:17:11 +02:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
|
2018-05-14 16:46:06 +02:00
|
|
|
const udp_address_t *const udp_addr = address->resolved.udp_addr;
|
2018-05-13 18:03:47 +02:00
|
|
|
|
2017-07-31 16:03:55 +01:00
|
|
|
// Bind the socket to a device if applicable
|
|
|
|
if (!options.bound_device.empty ())
|
|
|
|
bind_to_device (fd, options.bound_device);
|
|
|
|
|
2016-05-16 12:04:08 +03:00
|
|
|
if (send_enabled) {
|
|
|
|
if (!options.raw_socket) {
|
2018-05-13 18:03:47 +02:00
|
|
|
const ip_addr_t *out = udp_addr->target_addr ();
|
2018-05-05 00:44:13 +02:00
|
|
|
out_address = out->as_sockaddr ();
|
|
|
|
out_addrlen = out->sockaddr_len ();
|
2018-05-10 16:56:49 +02:00
|
|
|
|
|
|
|
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;
|
2018-05-18 15:54:00 +02:00
|
|
|
int rc =
|
|
|
|
setsockopt (fd, level, optname,
|
|
|
|
reinterpret_cast<char *> (&loop), sizeof (loop));
|
2018-05-10 16:56:49 +02:00
|
|
|
|
2018-05-13 18:03:47 +02:00
|
|
|
#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,
|
2018-05-18 15:54:00 +02:00
|
|
|
reinterpret_cast<char *> (&bind_if),
|
|
|
|
sizeof (bind_if));
|
2018-05-13 18:03:47 +02:00
|
|
|
} else {
|
|
|
|
rc = 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
struct in_addr bind_addr =
|
|
|
|
udp_addr->bind_addr ()->ipv4.sin_addr;
|
|
|
|
|
|
|
|
if (bind_addr.s_addr != INADDR_ANY) {
|
2018-05-18 15:54:00 +02:00
|
|
|
rc = setsockopt (fd, IPPROTO_IP, IP_MULTICAST_IF,
|
|
|
|
reinterpret_cast<char *> (&bind_addr),
|
|
|
|
sizeof (bind_addr));
|
2018-05-13 18:03:47 +02:00
|
|
|
} else {
|
|
|
|
rc = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-10 16:56:49 +02:00
|
|
|
#ifdef ZMQ_HAVE_WINDOWS
|
|
|
|
wsa_assert (rc != SOCKET_ERROR);
|
|
|
|
#else
|
|
|
|
errno_assert (rc == 0);
|
|
|
|
#endif
|
|
|
|
}
|
2018-02-01 11:46:09 +01:00
|
|
|
} else {
|
2018-05-05 00:44:13 +02:00
|
|
|
/// XXX fixme ?
|
2018-05-18 15:54:00 +02:00
|
|
|
out_address = reinterpret_cast<sockaddr *> (&raw_address);
|
2016-05-16 12:04:08 +03:00
|
|
|
out_addrlen = sizeof (sockaddr_in);
|
|
|
|
}
|
|
|
|
|
2016-01-29 21:17:11 +02:00
|
|
|
set_pollout (handle);
|
2016-05-16 12:04:08 +03:00
|
|
|
}
|
2016-01-29 21:17:11 +02:00
|
|
|
|
|
|
|
if (recv_enabled) {
|
|
|
|
int on = 1;
|
2018-05-18 15:54:00 +02:00
|
|
|
int rc = setsockopt (fd, SOL_SOCKET, SO_REUSEADDR,
|
|
|
|
reinterpret_cast<char *> (&on), sizeof (on));
|
2016-02-07 12:10:17 +01:00
|
|
|
#ifdef ZMQ_HAVE_WINDOWS
|
|
|
|
wsa_assert (rc != SOCKET_ERROR);
|
|
|
|
#else
|
|
|
|
errno_assert (rc == 0);
|
|
|
|
#endif
|
|
|
|
|
2018-05-13 18:03:47 +02:00
|
|
|
const ip_addr_t *bind_addr = udp_addr->bind_addr ();
|
2018-05-05 00:44:13 +02:00
|
|
|
ip_addr_t any = ip_addr_t::any (bind_addr->family ());
|
|
|
|
const ip_addr_t *real_bind_addr;
|
|
|
|
|
2018-05-13 18:03:47 +02:00
|
|
|
bool multicast = udp_addr->is_mcast ();
|
2018-05-05 00:44:13 +02:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-03-10 03:03:02 -08:00
|
|
|
#ifdef ZMQ_HAVE_VXWORKS
|
2018-05-05 00:44:13 +02:00
|
|
|
rc = bind (fd, (sockaddr *) real_bind_addr->as_sockaddr (),
|
|
|
|
real_bind_addr->sockaddr_len ());
|
2018-03-10 03:03:02 -08:00
|
|
|
#else
|
2018-05-05 00:44:13 +02:00
|
|
|
rc = bind (fd, real_bind_addr->as_sockaddr (),
|
|
|
|
real_bind_addr->sockaddr_len ());
|
|
|
|
|
2018-03-10 03:03:02 -08:00
|
|
|
#endif
|
2016-02-07 12:10:17 +01:00
|
|
|
#ifdef ZMQ_HAVE_WINDOWS
|
|
|
|
wsa_assert (rc != SOCKET_ERROR);
|
|
|
|
#else
|
|
|
|
errno_assert (rc == 0);
|
|
|
|
#endif
|
2016-01-29 21:17:11 +02:00
|
|
|
|
2018-05-05 00:44:13 +02:00
|
|
|
if (multicast) {
|
2018-05-13 18:03:47 +02:00
|
|
|
const ip_addr_t *mcast_addr = udp_addr->target_addr ();
|
2018-05-05 00:44:13 +02:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2018-05-18 15:54:00 +02:00
|
|
|
rc =
|
|
|
|
setsockopt (fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
|
|
|
|
reinterpret_cast<char *> (&mreq), sizeof (mreq));
|
2018-05-05 00:44:13 +02:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2018-05-18 15:54:00 +02:00
|
|
|
rc =
|
|
|
|
setsockopt (fd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP,
|
|
|
|
reinterpret_cast<char *> (&mreq), sizeof (mreq));
|
2018-05-05 00:44:13 +02:00
|
|
|
|
|
|
|
errno_assert (rc == 0);
|
|
|
|
} else {
|
|
|
|
// Shouldn't happen
|
|
|
|
abort ();
|
|
|
|
}
|
|
|
|
|
2016-01-29 21:17:11 +02:00
|
|
|
#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 ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-01 11:46:09 +01:00
|
|
|
void zmq::udp_engine_t::terminate ()
|
2016-01-29 21:17:11 +02:00
|
|
|
{
|
|
|
|
zmq_assert (plugged);
|
|
|
|
plugged = false;
|
|
|
|
|
|
|
|
rm_fd (handle);
|
|
|
|
|
|
|
|
// Disconnect from I/O threads poller object.
|
|
|
|
io_object_t::unplug ();
|
|
|
|
|
|
|
|
delete this;
|
|
|
|
}
|
|
|
|
|
2018-02-01 11:46:09 +01:00
|
|
|
void zmq::udp_engine_t::sockaddr_to_msg (zmq::msg_t *msg, sockaddr_in *addr)
|
2016-05-16 12:04:08 +03:00
|
|
|
{
|
2018-05-18 18:05:25 +02:00
|
|
|
const char *const name = inet_ntoa (addr->sin_addr);
|
2016-05-16 12:04:08 +03:00
|
|
|
|
|
|
|
char port[6];
|
2018-05-18 15:54:00 +02:00
|
|
|
sprintf (port, "%d", static_cast<int> (ntohs (addr->sin_port)));
|
2016-05-16 12:04:08 +03:00
|
|
|
|
2018-05-18 18:05:25 +02:00
|
|
|
const int size = static_cast<int> (strlen (name))
|
|
|
|
+ static_cast<int> (strlen (port)) + 1
|
|
|
|
+ 1; // Colon + NULL
|
|
|
|
const int rc = msg->init_size (size);
|
2016-05-16 12:04:08 +03:00
|
|
|
errno_assert (rc == 0);
|
|
|
|
msg->set_flags (msg_t::more);
|
2018-05-18 15:54:00 +02:00
|
|
|
char *address = static_cast<char *> (msg->data ());
|
2016-05-16 12:04:08 +03:00
|
|
|
|
|
|
|
strcpy (address, name);
|
|
|
|
strcat (address, ":");
|
|
|
|
strcat (address, port);
|
|
|
|
}
|
|
|
|
|
2016-05-16 13:51:39 +03:00
|
|
|
int zmq::udp_engine_t::resolve_raw_address (char *name_, size_t length_)
|
2016-05-16 12:04:08 +03:00
|
|
|
{
|
2016-05-16 13:51:39 +03:00
|
|
|
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) {
|
2018-05-18 15:54:00 +02:00
|
|
|
int chars_left = static_cast<int> (length_);
|
2016-05-16 13:51:39 +03:00
|
|
|
char *current_char = name_ + length_;
|
|
|
|
do {
|
|
|
|
if (*(--current_char) == ':') {
|
|
|
|
delimiter = current_char;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while (--chars_left != 0);
|
|
|
|
}
|
|
|
|
|
2016-05-16 12:04:08 +03:00
|
|
|
if (!delimiter) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string addr_str (name_, delimiter - name_);
|
2016-05-16 13:51:39 +03:00
|
|
|
std::string port_str (delimiter + 1, name_ + length_ - delimiter - 1);
|
2016-05-16 12:04:08 +03:00
|
|
|
|
|
|
|
// Parse the port number (0 is not a valid port).
|
2018-05-18 15:54:00 +02:00
|
|
|
uint16_t port = static_cast<uint16_t> (atoi (port_str.c_str ()));
|
2016-05-16 12:04:08 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-02-01 11:46:09 +01:00
|
|
|
void zmq::udp_engine_t::out_event ()
|
2016-01-29 21:17:11 +02:00
|
|
|
{
|
|
|
|
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 ();
|
2016-05-16 12:04:08 +03:00
|
|
|
size_t size;
|
2016-01-29 21:17:11 +02:00
|
|
|
|
2016-05-12 22:20:31 -03:00
|
|
|
if (options.raw_socket) {
|
2018-05-18 15:54:00 +02:00
|
|
|
rc = resolve_raw_address (static_cast<char *> (group_msg.data ()),
|
|
|
|
group_size);
|
2016-05-16 12:04:08 +03:00
|
|
|
|
|
|
|
// 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;
|
2016-05-12 22:20:31 -03:00
|
|
|
}
|
2016-05-16 12:04:08 +03:00
|
|
|
|
|
|
|
size = body_size;
|
|
|
|
|
2016-05-12 22:20:31 -03:00
|
|
|
memcpy (out_buffer, body_msg.data (), body_size);
|
2018-02-01 11:46:09 +01:00
|
|
|
} else {
|
2016-05-16 12:04:08 +03:00
|
|
|
size = group_size + body_size + 1;
|
|
|
|
|
2016-05-12 22:20:31 -03:00
|
|
|
// TODO: check if larger than maximum size
|
2018-05-18 15:54:00 +02:00
|
|
|
out_buffer[0] = static_cast<unsigned char> (group_size);
|
2016-05-12 22:20:31 -03:00
|
|
|
memcpy (out_buffer + 1, group_msg.data (), group_size);
|
|
|
|
memcpy (out_buffer + 1 + group_size, body_msg.data (), body_size);
|
|
|
|
}
|
2016-01-29 21:17:11 +02:00
|
|
|
|
|
|
|
rc = group_msg.close ();
|
|
|
|
errno_assert (rc == 0);
|
|
|
|
|
|
|
|
body_msg.close ();
|
|
|
|
errno_assert (rc == 0);
|
|
|
|
|
2016-01-30 13:21:08 +02:00
|
|
|
#ifdef ZMQ_HAVE_WINDOWS
|
2018-05-18 15:54:00 +02:00
|
|
|
rc = sendto (fd, reinterpret_cast<const char *> (out_buffer),
|
|
|
|
static_cast<int> (size), 0, out_address,
|
|
|
|
static_cast<int> (out_addrlen));
|
2016-02-07 12:10:17 +01:00
|
|
|
wsa_assert (rc != SOCKET_ERROR);
|
2018-03-10 03:03:02 -08:00
|
|
|
#elif defined ZMQ_HAVE_VXWORKS
|
|
|
|
rc = sendto (fd, (caddr_t) out_buffer, size, 0,
|
|
|
|
(sockaddr *) out_address, (int) out_addrlen);
|
|
|
|
errno_assert (rc != -1);
|
2016-01-30 13:21:08 +02:00
|
|
|
#else
|
2016-05-12 22:20:31 -03:00
|
|
|
rc = sendto (fd, out_buffer, size, 0, out_address, out_addrlen);
|
2016-01-29 21:17:11 +02:00
|
|
|
errno_assert (rc != -1);
|
2016-01-30 13:21:08 +02:00
|
|
|
#endif
|
2018-02-01 11:46:09 +01:00
|
|
|
} else
|
|
|
|
reset_pollout (handle);
|
2016-01-29 21:17:11 +02:00
|
|
|
}
|
|
|
|
|
2017-08-08 14:10:20 +02:00
|
|
|
const char *zmq::udp_engine_t::get_endpoint () const
|
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2018-02-01 11:46:09 +01:00
|
|
|
void zmq::udp_engine_t::restart_output ()
|
2016-01-29 21:17:11 +02:00
|
|
|
{
|
|
|
|
// 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 ();
|
2018-02-01 11:46:09 +01:00
|
|
|
} else {
|
|
|
|
set_pollout (handle);
|
2016-01-29 21:17:11 +02:00
|
|
|
out_event ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-01 11:46:09 +01:00
|
|
|
void zmq::udp_engine_t::in_event ()
|
2016-01-29 21:17:11 +02:00
|
|
|
{
|
2018-05-15 14:53:22 +02:00
|
|
|
sockaddr_storage in_address;
|
|
|
|
socklen_t in_addrlen = sizeof (sockaddr_storage);
|
2016-01-30 13:21:08 +02:00
|
|
|
#ifdef ZMQ_HAVE_WINDOWS
|
2018-05-18 15:54:00 +02:00
|
|
|
int nbytes =
|
|
|
|
recvfrom (fd, reinterpret_cast<char *> (in_buffer), MAX_UDP_MSG, 0,
|
|
|
|
reinterpret_cast<sockaddr *> (&in_address), &in_addrlen);
|
2018-02-01 11:46:09 +01:00
|
|
|
const int last_error = WSAGetLastError ();
|
2016-01-30 13:21:08 +02:00
|
|
|
if (nbytes == SOCKET_ERROR) {
|
2018-02-01 11:46:09 +01:00
|
|
|
wsa_assert (last_error == WSAENETDOWN || last_error == WSAENETRESET
|
|
|
|
|| last_error == WSAEWOULDBLOCK);
|
2016-01-30 13:21:08 +02:00
|
|
|
return;
|
|
|
|
}
|
2018-03-10 03:03:02 -08:00
|
|
|
#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;
|
|
|
|
}
|
2016-01-30 13:21:08 +02:00
|
|
|
#else
|
2018-02-01 11:46:09 +01:00
|
|
|
int nbytes = recvfrom (fd, in_buffer, MAX_UDP_MSG, 0,
|
|
|
|
(sockaddr *) &in_address, &in_addrlen);
|
2016-01-30 13:21:08 +02:00
|
|
|
if (nbytes == -1) {
|
2018-04-17 12:33:18 +10:00
|
|
|
#if !defined(TARGET_OS_IPHONE) || !TARGET_OS_IPHONE
|
|
|
|
errno_assert (errno != EBADF && errno != EFAULT && errno != ENOMEM
|
|
|
|
&& errno != ENOTSOCK);
|
|
|
|
#else
|
2018-02-01 11:46:09 +01:00
|
|
|
errno_assert (errno != EBADF && errno != EFAULT && errno != ENOMEM
|
|
|
|
&& errno != ENOTSOCK);
|
2018-04-17 12:33:18 +10:00
|
|
|
#endif
|
2016-01-30 13:21:08 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
2016-05-16 12:04:08 +03:00
|
|
|
int rc;
|
2016-05-12 22:20:31 -03:00
|
|
|
int body_size;
|
2016-05-16 12:04:08 +03:00
|
|
|
int body_offset;
|
2016-05-12 22:20:31 -03:00
|
|
|
msg_t msg;
|
2016-05-16 12:04:08 +03:00
|
|
|
|
2016-05-12 22:20:31 -03:00
|
|
|
if (options.raw_socket) {
|
2018-05-15 14:53:22 +02:00
|
|
|
zmq_assert (in_address.ss_family == AF_INET);
|
|
|
|
sockaddr_to_msg (&msg, reinterpret_cast<sockaddr_in *> (&in_address));
|
2016-05-16 12:04:08 +03:00
|
|
|
|
|
|
|
body_size = nbytes;
|
|
|
|
body_offset = 0;
|
2018-02-01 11:46:09 +01:00
|
|
|
} else {
|
2018-05-18 18:05:25 +02:00
|
|
|
const char *group_buffer =
|
|
|
|
reinterpret_cast<const char *> (in_buffer) + 1;
|
2016-05-16 12:04:08 +03:00
|
|
|
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);
|
2016-01-29 21:17:11 +02:00
|
|
|
|
2016-05-12 22:20:31 -03:00
|
|
|
// This doesn't fit, just ingore
|
|
|
|
if (nbytes - 1 < group_size)
|
|
|
|
return;
|
2016-01-29 21:17:11 +02:00
|
|
|
|
2016-05-12 22:20:31 -03:00
|
|
|
body_size = nbytes - 1 - group_size;
|
2016-05-16 12:04:08 +03:00
|
|
|
body_offset = 1 + group_size;
|
2016-05-12 22:20:31 -03:00
|
|
|
}
|
2018-03-19 17:46:29 +01:00
|
|
|
// Push group description to session
|
2016-01-30 13:21:08 +02:00
|
|
|
rc = session->push_msg (&msg);
|
|
|
|
errno_assert (rc == 0 || (rc == -1 && errno == EAGAIN));
|
2016-01-29 21:17:11 +02:00
|
|
|
|
2018-03-19 17:46:29 +01:00
|
|
|
// Group description message doesn't fit in the pipe, drop
|
2016-01-30 13:21:08 +02:00
|
|
|
if (rc != 0) {
|
2016-01-29 21:17:11 +02:00
|
|
|
rc = msg.close ();
|
|
|
|
errno_assert (rc == 0);
|
2016-01-30 13:21:08 +02:00
|
|
|
|
|
|
|
reset_pollin (handle);
|
|
|
|
return;
|
2016-01-29 21:17:11 +02:00
|
|
|
}
|
2016-01-30 13:21:08 +02:00
|
|
|
|
|
|
|
rc = msg.close ();
|
|
|
|
errno_assert (rc == 0);
|
|
|
|
rc = msg.init_size (body_size);
|
|
|
|
errno_assert (rc == 0);
|
2016-05-16 12:04:08 +03:00
|
|
|
memcpy (msg.data (), in_buffer + body_offset, body_size);
|
2018-03-19 17:46:29 +01:00
|
|
|
|
|
|
|
// Push message body to session
|
2016-01-30 13:21:08 +02:00
|
|
|
rc = session->push_msg (&msg);
|
2018-03-20 14:35:24 +01:00
|
|
|
// Message body doesn't fit in the pipe, drop and reset session state
|
2018-03-19 17:46:29 +01:00
|
|
|
if (rc != 0) {
|
|
|
|
rc = msg.close ();
|
|
|
|
errno_assert (rc == 0);
|
|
|
|
|
|
|
|
session->reset ();
|
|
|
|
reset_pollin (handle);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-01-30 13:21:08 +02:00
|
|
|
rc = msg.close ();
|
|
|
|
errno_assert (rc == 0);
|
|
|
|
session->flush ();
|
2016-01-29 21:17:11 +02:00
|
|
|
}
|
|
|
|
|
2018-02-01 11:46:09 +01:00
|
|
|
void zmq::udp_engine_t::restart_input ()
|
2016-01-29 21:17:11 +02:00
|
|
|
{
|
|
|
|
if (!recv_enabled)
|
|
|
|
return;
|
|
|
|
|
|
|
|
set_pollin (handle);
|
|
|
|
in_event ();
|
|
|
|
}
|