2009-09-11 17:58:37 +02:00
|
|
|
/*
|
2010-01-05 08:29:35 +01:00
|
|
|
Copyright (c) 2007-2010 iMatix Corporation
|
2009-09-11 17:58:37 +02:00
|
|
|
|
|
|
|
This file is part of 0MQ.
|
|
|
|
|
|
|
|
0MQ is free software; you can redistribute it and/or modify it under
|
|
|
|
the terms of the Lesser GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
0MQ 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
|
|
|
|
Lesser GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the Lesser GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "platform.hpp"
|
|
|
|
|
2009-09-24 16:23:49 +02:00
|
|
|
#ifdef ZMQ_HAVE_OPENPGM
|
2009-09-11 17:58:37 +02:00
|
|
|
|
2009-10-06 12:57:24 +02:00
|
|
|
#ifdef ZMQ_HAVE_WINDOWS
|
|
|
|
#include "windows.hpp"
|
|
|
|
#endif
|
|
|
|
|
2009-09-11 17:58:37 +02:00
|
|
|
#ifdef ZMQ_HAVE_LINUX
|
2009-10-06 12:57:24 +02:00
|
|
|
#include <poll.h>
|
2009-09-11 17:58:37 +02:00
|
|
|
#endif
|
|
|
|
|
2009-12-28 11:51:06 +01:00
|
|
|
#include <stdlib.h>
|
2010-02-04 09:39:57 +01:00
|
|
|
#include <string.h>
|
2009-09-11 17:58:37 +02:00
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include "options.hpp"
|
|
|
|
#include "pgm_socket.hpp"
|
|
|
|
#include "config.hpp"
|
|
|
|
#include "err.hpp"
|
2009-09-22 15:12:51 +02:00
|
|
|
#include "uuid.hpp"
|
2009-12-28 11:51:06 +01:00
|
|
|
#include "stdint.hpp"
|
2009-09-11 17:58:37 +02:00
|
|
|
|
|
|
|
zmq::pgm_socket_t::pgm_socket_t (bool receiver_, const options_t &options_) :
|
2010-09-28 16:35:29 +02:00
|
|
|
sock (NULL),
|
2009-09-11 17:58:37 +02:00
|
|
|
options (options_),
|
|
|
|
receiver (receiver_),
|
|
|
|
pgm_msgv (NULL),
|
2009-12-31 16:18:45 +01:00
|
|
|
pgm_msgv_len (0),
|
2009-09-11 17:58:37 +02:00
|
|
|
nbytes_rec (0),
|
|
|
|
nbytes_processed (0),
|
2009-12-31 16:18:45 +01:00
|
|
|
pgm_msgv_processed (0)
|
2009-09-11 17:58:37 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2010-09-28 16:58:51 +02:00
|
|
|
// Create, bind and connect PGM socket.
|
|
|
|
// network_ of the form <interface & multicast group decls>:<IP port>
|
|
|
|
// e.g. eth0;239.192.0.1:7500
|
|
|
|
// link-local;224.250.0.1,224.250.0.2;224.250.0.3:8000
|
|
|
|
// ;[fe80::1%en0]:7500
|
2009-09-16 15:36:38 +02:00
|
|
|
int zmq::pgm_socket_t::init (bool udp_encapsulation_, const char *network_)
|
2009-09-11 17:58:37 +02:00
|
|
|
{
|
2009-12-28 11:51:06 +01:00
|
|
|
// Can not open transport before destroying old one.
|
2010-09-28 16:35:29 +02:00
|
|
|
zmq_assert (sock == NULL);
|
2009-09-11 17:58:37 +02:00
|
|
|
|
2010-09-28 16:35:29 +02:00
|
|
|
// Parse port number, start from end for IPv6
|
|
|
|
const char *port_delim = strrchr (network_, ':');
|
2009-09-11 17:58:37 +02:00
|
|
|
if (!port_delim) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-12-28 11:51:06 +01:00
|
|
|
uint16_t port_number = atoi (port_delim + 1);
|
2009-09-11 17:58:37 +02:00
|
|
|
|
2009-12-28 11:51:06 +01:00
|
|
|
char network [256];
|
2009-09-16 15:36:38 +02:00
|
|
|
if (port_delim - network_ >= (int) sizeof (network) - 1) {
|
2009-09-11 17:58:37 +02:00
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
memset (network, '\0', sizeof (network));
|
2009-09-16 15:36:38 +02:00
|
|
|
memcpy (network, network_, port_delim - network_);
|
2010-09-28 16:35:29 +02:00
|
|
|
|
|
|
|
// Validate socket options
|
|
|
|
// Data rate is in [B/s]. options.rate is in [kb/s].
|
|
|
|
if (options.rate <= 0) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
2009-09-22 15:12:51 +02:00
|
|
|
}
|
2010-09-28 16:35:29 +02:00
|
|
|
// Recovery interval [s].
|
|
|
|
if (options.recovery_ivl <= 0) {
|
2009-09-11 17:58:37 +02:00
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-09-28 16:35:29 +02:00
|
|
|
// Zero counter used in msgrecv.
|
|
|
|
nbytes_rec = 0;
|
|
|
|
nbytes_processed = 0;
|
|
|
|
pgm_msgv_processed = 0;
|
2009-12-21 18:50:24 +01:00
|
|
|
|
2010-09-28 16:35:29 +02:00
|
|
|
bool rc;
|
|
|
|
pgm_error_t *pgm_error = NULL;
|
|
|
|
struct pgm_addrinfo_t hints, *res = NULL;
|
|
|
|
sa_family_t sa_family;
|
|
|
|
|
|
|
|
memset (&hints, 0, sizeof (hints));
|
|
|
|
hints.ai_family = AF_UNSPEC;
|
|
|
|
if (!pgm_getaddrinfo (network, NULL, &res, &pgm_error)) {
|
2010-09-28 16:58:51 +02:00
|
|
|
// Invalid parameters don't set pgm_error_t
|
|
|
|
zmq_assert (pgm_error != NULL);
|
2010-09-28 16:35:29 +02:00
|
|
|
if (pgm_error->domain == PGM_ERROR_DOMAIN_IF && (
|
2010-09-28 16:58:51 +02:00
|
|
|
// NB: cannot catch EAI_BADFLAGS
|
|
|
|
pgm_error->code != PGM_ERROR_SERVICE &&
|
|
|
|
pgm_error->code != PGM_ERROR_SOCKTNOSUPPORT))
|
|
|
|
// User, host, or network configuration or transient error
|
2010-09-28 16:35:29 +02:00
|
|
|
goto err_abort;
|
|
|
|
|
2010-09-28 16:58:51 +02:00
|
|
|
// Fatal OpenPGM internal error
|
2009-12-21 18:50:24 +01:00
|
|
|
zmq_assert (false);
|
2009-09-24 16:23:49 +02:00
|
|
|
}
|
|
|
|
|
2010-09-28 16:58:51 +02:00
|
|
|
zmq_assert (res != NULL);
|
|
|
|
|
2010-09-28 16:35:29 +02:00
|
|
|
// Pick up detected IP family
|
|
|
|
sa_family = res->ai_send_addrs[0].gsr_group.ss_family;
|
2009-09-24 16:23:49 +02:00
|
|
|
|
2010-09-28 16:35:29 +02:00
|
|
|
// Create IP/PGM or UDP/PGM socket
|
2009-12-28 11:51:06 +01:00
|
|
|
if (udp_encapsulation_) {
|
2010-09-28 16:35:29 +02:00
|
|
|
if (!pgm_socket (&sock, sa_family, SOCK_SEQPACKET, IPPROTO_UDP, &pgm_error)) {
|
2010-09-28 16:58:51 +02:00
|
|
|
// Invalid parameters don't set pgm_error_t
|
|
|
|
zmq_assert (pgm_error != NULL);
|
2010-09-28 16:35:29 +02:00
|
|
|
if (pgm_error->domain == PGM_ERROR_DOMAIN_SOCKET && (
|
2010-09-28 16:58:51 +02:00
|
|
|
pgm_error->code != PGM_ERROR_BADF &&
|
|
|
|
pgm_error->code != PGM_ERROR_FAULT &&
|
|
|
|
pgm_error->code != PGM_ERROR_NOPROTOOPT &&
|
|
|
|
pgm_error->code != PGM_ERROR_FAILED))
|
|
|
|
// User, host, or network configuration or transient error
|
2010-09-28 16:35:29 +02:00
|
|
|
goto err_abort;
|
|
|
|
|
2010-09-28 16:58:51 +02:00
|
|
|
// Fatal OpenPGM internal error
|
2010-09-28 16:35:29 +02:00
|
|
|
zmq_assert (false);
|
2009-12-21 18:50:24 +01:00
|
|
|
}
|
|
|
|
|
2010-09-28 16:35:29 +02:00
|
|
|
// All options are of data type int
|
|
|
|
const int encapsulation_port = port_number;
|
|
|
|
if (!pgm_setsockopt (sock, IPPROTO_PGM, PGM_UDP_ENCAP_UCAST_PORT, &encapsulation_port, sizeof (encapsulation_port)) ||
|
2010-09-28 16:58:51 +02:00
|
|
|
!pgm_setsockopt (sock, IPPROTO_PGM, PGM_UDP_ENCAP_MCAST_PORT, &encapsulation_port, sizeof (encapsulation_port)))
|
2010-09-28 16:35:29 +02:00
|
|
|
goto err_abort;
|
|
|
|
} else {
|
|
|
|
if (!pgm_socket (&sock, sa_family, SOCK_SEQPACKET, IPPROTO_PGM, &pgm_error)) {
|
2010-09-28 16:58:51 +02:00
|
|
|
// Invalid parameters don't set pgm_error_t
|
|
|
|
zmq_assert (pgm_error != NULL);
|
2010-09-28 16:35:29 +02:00
|
|
|
if (pgm_error->domain == PGM_ERROR_DOMAIN_SOCKET && (
|
2010-09-28 16:58:51 +02:00
|
|
|
pgm_error->code != PGM_ERROR_BADF &&
|
|
|
|
pgm_error->code != PGM_ERROR_FAULT &&
|
|
|
|
pgm_error->code != PGM_ERROR_NOPROTOOPT &&
|
|
|
|
pgm_error->code != PGM_ERROR_FAILED))
|
|
|
|
// User, host, or network configuration or transient error
|
2010-09-28 16:35:29 +02:00
|
|
|
goto err_abort;
|
|
|
|
|
2010-09-28 16:58:51 +02:00
|
|
|
// Fatal OpenPGM internal error
|
2010-09-28 16:35:29 +02:00
|
|
|
zmq_assert (false);
|
|
|
|
}
|
2009-09-24 16:23:49 +02:00
|
|
|
}
|
|
|
|
|
2010-09-28 16:35:29 +02:00
|
|
|
{
|
|
|
|
const int rcvbuf = (int) options.rcvbuf,
|
|
|
|
sndbuf = (int) options.sndbuf,
|
|
|
|
max_tpdu = (int) pgm_max_tpdu;
|
|
|
|
if (rcvbuf) {
|
|
|
|
if (!pgm_setsockopt (sock, SOL_SOCKET, SO_RCVBUF, &rcvbuf, sizeof (rcvbuf)))
|
|
|
|
goto err_abort;
|
|
|
|
}
|
|
|
|
if (sndbuf) {
|
2010-09-28 16:58:51 +02:00
|
|
|
if (!pgm_setsockopt (sock, SOL_SOCKET, SO_SNDBUF, &sndbuf, sizeof (sndbuf)))
|
2010-09-28 16:35:29 +02:00
|
|
|
goto err_abort;
|
|
|
|
}
|
2009-09-11 17:58:37 +02:00
|
|
|
|
|
|
|
// Set maximum transport protocol data unit size (TPDU).
|
2010-09-28 16:35:29 +02:00
|
|
|
if (!pgm_setsockopt (sock, IPPROTO_PGM, PGM_MTU, &max_tpdu, sizeof (max_tpdu)))
|
|
|
|
goto err_abort;
|
2009-09-25 17:50:12 +02:00
|
|
|
}
|
|
|
|
|
2009-09-11 17:58:37 +02:00
|
|
|
if (receiver) {
|
2010-09-28 16:35:29 +02:00
|
|
|
const int recv_only = 1,
|
|
|
|
rxw_max_rte = options.rate * 1000 / 8,
|
|
|
|
rxw_secs = options.recovery_ivl,
|
|
|
|
peer_expiry = 5 * pgm_msecs (8192),
|
|
|
|
spmr_expiry = pgm_msecs (25),
|
|
|
|
nak_bo_ivl = pgm_msecs (50),
|
|
|
|
nak_rpt_ivl = pgm_msecs (200),
|
|
|
|
nak_rdata_ivl = pgm_msecs (200),
|
|
|
|
nak_data_retries = 5,
|
|
|
|
nak_ncf_retries = 2;
|
|
|
|
|
|
|
|
if (!pgm_setsockopt (sock, IPPROTO_PGM, PGM_RECV_ONLY, &recv_only, sizeof (recv_only)) ||
|
|
|
|
!pgm_setsockopt (sock, IPPROTO_PGM, PGM_RXW_MAX_RTE, &rxw_max_rte, sizeof (rxw_max_rte)) ||
|
|
|
|
!pgm_setsockopt (sock, IPPROTO_PGM, PGM_RXW_SECS, &rxw_secs, sizeof (rxw_secs)) ||
|
|
|
|
!pgm_setsockopt (sock, IPPROTO_PGM, PGM_PEER_EXPIRY, &peer_expiry, sizeof (peer_expiry)) ||
|
|
|
|
!pgm_setsockopt (sock, IPPROTO_PGM, PGM_SPMR_EXPIRY, &spmr_expiry, sizeof (spmr_expiry)) ||
|
|
|
|
!pgm_setsockopt (sock, IPPROTO_PGM, PGM_NAK_BO_IVL, &nak_bo_ivl, sizeof (nak_bo_ivl)) ||
|
|
|
|
!pgm_setsockopt (sock, IPPROTO_PGM, PGM_NAK_RPT_IVL, &nak_rpt_ivl, sizeof (nak_rpt_ivl)) ||
|
|
|
|
!pgm_setsockopt (sock, IPPROTO_PGM, PGM_NAK_RDATA_IVL, &nak_rdata_ivl, sizeof (nak_rdata_ivl)) ||
|
|
|
|
!pgm_setsockopt (sock, IPPROTO_PGM, PGM_NAK_DATA_RETRIES, &nak_data_retries, sizeof (nak_data_retries)) ||
|
|
|
|
!pgm_setsockopt (sock, IPPROTO_PGM, PGM_NAK_NCF_RETRIES, &nak_ncf_retries, sizeof (nak_ncf_retries)))
|
|
|
|
goto err_abort;
|
2009-09-11 17:58:37 +02:00
|
|
|
} else {
|
2010-09-28 16:35:29 +02:00
|
|
|
const int send_only = 1,
|
|
|
|
txw_max_rte = options.rate * 1000 / 8,
|
|
|
|
txw_secs = options.recovery_ivl,
|
|
|
|
ambient_spm = pgm_msecs (8192),
|
|
|
|
heartbeat_spm[] = { pgm_msecs (4),
|
|
|
|
pgm_msecs (4),
|
|
|
|
pgm_msecs (8),
|
|
|
|
pgm_msecs (16),
|
|
|
|
pgm_msecs (32),
|
|
|
|
pgm_msecs (64),
|
|
|
|
pgm_msecs (128),
|
|
|
|
pgm_msecs (256),
|
|
|
|
pgm_msecs (512),
|
|
|
|
pgm_msecs (1024),
|
|
|
|
pgm_msecs (2048),
|
|
|
|
pgm_msecs (4096),
|
|
|
|
pgm_msecs (8192) };
|
|
|
|
|
|
|
|
if (!pgm_setsockopt (sock, IPPROTO_PGM, PGM_SEND_ONLY, &send_only, sizeof (send_only)) ||
|
|
|
|
!pgm_setsockopt (sock, IPPROTO_PGM, PGM_TXW_MAX_RTE, &txw_max_rte, sizeof (txw_max_rte)) ||
|
|
|
|
!pgm_setsockopt (sock, IPPROTO_PGM, PGM_TXW_SECS, &txw_secs, sizeof (txw_secs)) ||
|
|
|
|
!pgm_setsockopt (sock, IPPROTO_PGM, PGM_AMBIENT_SPM, &ambient_spm, sizeof (ambient_spm)) ||
|
|
|
|
!pgm_setsockopt (sock, IPPROTO_PGM, PGM_HEARTBEAT_SPM, &heartbeat_spm, sizeof (heartbeat_spm)))
|
|
|
|
goto err_abort;
|
|
|
|
}
|
2009-09-11 17:58:37 +02:00
|
|
|
|
2010-09-28 16:35:29 +02:00
|
|
|
// PGM transport GSI.
|
|
|
|
struct pgm_sockaddr_t addr;
|
2009-12-13 09:11:08 +01:00
|
|
|
|
2010-09-28 16:35:29 +02:00
|
|
|
memset (&addr, 0, sizeof(addr));
|
|
|
|
addr.sa_port = port_number;
|
|
|
|
addr.sa_addr.sport = DEFAULT_DATA_SOURCE_PORT;
|
2009-09-11 17:58:37 +02:00
|
|
|
|
2010-09-28 16:35:29 +02:00
|
|
|
if (options.identity.size () > 0) {
|
2009-12-14 11:46:30 +01:00
|
|
|
|
2010-09-28 16:35:29 +02:00
|
|
|
// Create gsi from identity.
|
|
|
|
if (!pgm_gsi_create_from_data (&addr.sa_addr.gsi, options.identity.data (), options.identity.size ()))
|
|
|
|
goto err_abort;
|
|
|
|
} else {
|
2009-09-11 17:58:37 +02:00
|
|
|
|
2010-09-28 16:35:29 +02:00
|
|
|
// Generate random gsi.
|
|
|
|
std::string gsi_base = uuid_t ().to_string ();
|
|
|
|
if (!pgm_gsi_create_from_string (&addr.sa_addr.gsi, gsi_base.c_str (), -1))
|
|
|
|
goto err_abort;
|
|
|
|
}
|
2009-09-11 17:58:37 +02:00
|
|
|
|
2010-09-28 16:35:29 +02:00
|
|
|
// Bind a transport to the specified network devices.
|
|
|
|
struct pgm_interface_req_t if_req;
|
|
|
|
memset (&if_req, 0, sizeof(if_req));
|
|
|
|
if_req.ir_interface = res->ai_recv_addrs[0].gsr_interface;
|
|
|
|
if_req.ir_scope_id = 0;
|
|
|
|
if (AF_INET6 == sa_family) {
|
|
|
|
struct sockaddr_in6 sa6;
|
|
|
|
memcpy (&sa6, &res->ai_recv_addrs[0].gsr_group, sizeof (sa6));
|
|
|
|
if_req.ir_scope_id = sa6.sin6_scope_id;
|
2009-09-11 17:58:37 +02:00
|
|
|
}
|
2010-09-28 16:35:29 +02:00
|
|
|
if (!pgm_bind3 (sock, &addr, sizeof (addr), &if_req, sizeof (if_req), &if_req, sizeof (if_req), &pgm_error)) {
|
2010-09-28 16:58:51 +02:00
|
|
|
// Invalid parameters don't set pgm_error_t
|
|
|
|
zmq_assert (pgm_error != NULL);
|
|
|
|
if ((pgm_error->domain == PGM_ERROR_DOMAIN_SOCKET ||
|
|
|
|
pgm_error->domain == PGM_ERROR_DOMAIN_IF) && (
|
|
|
|
pgm_error->code != PGM_ERROR_INVAL &&
|
|
|
|
pgm_error->code != PGM_ERROR_BADF &&
|
|
|
|
pgm_error->code != PGM_ERROR_FAULT))
|
|
|
|
// User, host, or network configuration or transient error
|
2010-09-28 16:35:29 +02:00
|
|
|
goto err_abort;
|
|
|
|
|
2010-09-28 16:58:51 +02:00
|
|
|
// Fatal OpenPGM internal error
|
2010-09-28 16:35:29 +02:00
|
|
|
zmq_assert (false);
|
2009-09-11 17:58:37 +02:00
|
|
|
}
|
|
|
|
|
2010-09-28 16:35:29 +02:00
|
|
|
// Join IP multicast groups
|
|
|
|
for (unsigned i = 0; i < res->ai_recv_addrs_len; i++)
|
|
|
|
{
|
|
|
|
if (!pgm_setsockopt (sock, IPPROTO_PGM, PGM_JOIN_GROUP, &res->ai_recv_addrs[i], sizeof (struct group_req)))
|
|
|
|
goto err_abort;
|
|
|
|
}
|
|
|
|
if (!pgm_setsockopt (sock, IPPROTO_PGM, PGM_SEND_GROUP, &res->ai_send_addrs[0], sizeof (struct group_req)))
|
|
|
|
goto err_abort;
|
2010-09-28 16:58:51 +02:00
|
|
|
|
2010-09-28 16:35:29 +02:00
|
|
|
pgm_freeaddrinfo (res);
|
2010-09-28 16:58:51 +02:00
|
|
|
res = NULL;
|
2009-12-21 18:50:24 +01:00
|
|
|
|
2010-09-28 16:35:29 +02:00
|
|
|
// Set IP level parameters
|
|
|
|
{
|
|
|
|
const int nonblocking = 1,
|
|
|
|
multicast_loop = options.use_multicast_loop ? 1 : 0,
|
|
|
|
multicast_hops = 16,
|
|
|
|
dscp = 0x2e << 2; /* Expedited Forwarding PHB for network elements, no ECN. */
|
|
|
|
|
|
|
|
if (!pgm_setsockopt (sock, IPPROTO_PGM, PGM_MULTICAST_LOOP, &multicast_loop, sizeof (multicast_loop)) ||
|
|
|
|
!pgm_setsockopt (sock, IPPROTO_PGM, PGM_MULTICAST_HOPS, &multicast_hops, sizeof (multicast_hops)))
|
|
|
|
goto err_abort;
|
|
|
|
if (AF_INET6 != sa_family &&
|
2010-09-28 16:58:51 +02:00
|
|
|
!pgm_setsockopt (sock, IPPROTO_PGM, PGM_TOS, &dscp, sizeof (dscp)))
|
2010-09-28 16:35:29 +02:00
|
|
|
goto err_abort;
|
|
|
|
if (!pgm_setsockopt (sock, IPPROTO_PGM, PGM_NOBLOCK, &nonblocking, sizeof (nonblocking)))
|
|
|
|
goto err_abort;
|
2009-12-13 09:56:02 +01:00
|
|
|
}
|
2009-09-11 17:58:37 +02:00
|
|
|
|
2010-09-28 16:58:51 +02:00
|
|
|
// Connect PGM transport to start state machine.
|
|
|
|
if (!pgm_connect (sock, &pgm_error)) {
|
|
|
|
// Invalid parameters don't set pgm_error_t
|
|
|
|
zmq_assert (pgm_error != NULL);
|
|
|
|
goto err_abort;
|
|
|
|
}
|
|
|
|
|
2009-12-28 11:51:06 +01:00
|
|
|
// For receiver transport preallocate pgm_msgv array.
|
|
|
|
if (receiver) {
|
|
|
|
zmq_assert (in_batch_size > 0);
|
|
|
|
size_t max_tsdu_size = get_max_tsdu_size ();
|
|
|
|
pgm_msgv_len = (int) in_batch_size / max_tsdu_size;
|
|
|
|
if ((int) in_batch_size % max_tsdu_size)
|
|
|
|
pgm_msgv_len++;
|
|
|
|
zmq_assert (pgm_msgv_len);
|
|
|
|
|
|
|
|
pgm_msgv = (pgm_msgv_t*) malloc (sizeof (pgm_msgv_t) * pgm_msgv_len);
|
|
|
|
}
|
|
|
|
|
2009-09-11 17:58:37 +02:00
|
|
|
return 0;
|
2010-09-28 16:35:29 +02:00
|
|
|
|
|
|
|
err_abort:
|
|
|
|
if (sock != NULL) {
|
|
|
|
pgm_close (sock, FALSE);
|
|
|
|
sock = NULL;
|
|
|
|
}
|
|
|
|
if (res != NULL) {
|
|
|
|
pgm_freeaddrinfo (res);
|
|
|
|
res = NULL;
|
|
|
|
}
|
|
|
|
if (pgm_error != NULL) {
|
|
|
|
pgm_error_free (pgm_error);
|
|
|
|
pgm_error = NULL;
|
|
|
|
}
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
2009-09-11 17:58:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
zmq::pgm_socket_t::~pgm_socket_t ()
|
|
|
|
{
|
2009-12-28 11:51:06 +01:00
|
|
|
if (pgm_msgv)
|
|
|
|
free (pgm_msgv);
|
2010-09-28 16:35:29 +02:00
|
|
|
if (sock)
|
|
|
|
pgm_close (sock, TRUE);
|
2009-09-11 17:58:37 +02:00
|
|
|
}
|
|
|
|
|
2010-09-28 16:35:29 +02:00
|
|
|
// Get receiver fds. receive_fd_ is signaled for incoming
|
|
|
|
// packets, waiting_pipe_fd_ is signaled for state driven
|
|
|
|
// events and data.
|
2009-12-13 11:27:43 +01:00
|
|
|
void zmq::pgm_socket_t::get_receiver_fds (int *receive_fd_,
|
2009-09-11 17:58:37 +02:00
|
|
|
int *waiting_pipe_fd_)
|
|
|
|
{
|
2010-09-28 16:35:29 +02:00
|
|
|
socklen_t socklen;
|
|
|
|
bool rc;
|
|
|
|
|
2009-11-03 14:11:53 +01:00
|
|
|
zmq_assert (receive_fd_);
|
|
|
|
zmq_assert (waiting_pipe_fd_);
|
|
|
|
|
2010-09-28 16:35:29 +02:00
|
|
|
socklen = sizeof (*receive_fd_);
|
|
|
|
rc = pgm_getsockopt (sock, IPPROTO_PGM, PGM_RECV_SOCK, receive_fd_, &socklen);
|
|
|
|
zmq_assert (rc);
|
|
|
|
zmq_assert (socklen == sizeof (*receive_fd_));
|
2009-11-03 14:11:53 +01:00
|
|
|
|
2010-09-28 16:35:29 +02:00
|
|
|
socklen = sizeof (*waiting_pipe_fd_);
|
|
|
|
rc = pgm_getsockopt (sock, IPPROTO_PGM, PGM_PENDING_SOCK, waiting_pipe_fd_, &socklen);
|
|
|
|
zmq_assert (rc);
|
|
|
|
zmq_assert (socklen == sizeof (*waiting_pipe_fd_));
|
2009-09-11 17:58:37 +02:00
|
|
|
}
|
|
|
|
|
2009-11-03 14:11:53 +01:00
|
|
|
// Get fds and store them into user allocated memory.
|
2010-09-28 16:35:29 +02:00
|
|
|
// send_fd is for non-blocking send wire notifications.
|
|
|
|
// receive_fd_ is for incoming back-channel protocol packets.
|
|
|
|
// rdata_notify_fd_ is raised for waiting repair transmissions.
|
|
|
|
// pending_notify_fd_ is for state driven events.
|
2009-12-13 11:27:43 +01:00
|
|
|
void zmq::pgm_socket_t::get_sender_fds (int *send_fd_, int *receive_fd_,
|
2010-01-05 11:22:14 +01:00
|
|
|
int *rdata_notify_fd_, int *pending_notify_fd_)
|
2009-09-11 17:58:37 +02:00
|
|
|
{
|
2010-09-28 16:35:29 +02:00
|
|
|
socklen_t socklen;
|
|
|
|
bool rc;
|
|
|
|
|
2009-09-28 18:06:06 +02:00
|
|
|
zmq_assert (send_fd_);
|
|
|
|
zmq_assert (receive_fd_);
|
2009-11-03 14:11:53 +01:00
|
|
|
zmq_assert (rdata_notify_fd_);
|
2010-01-05 11:22:14 +01:00
|
|
|
zmq_assert (pending_notify_fd_);
|
2009-11-03 14:11:53 +01:00
|
|
|
|
2010-09-28 16:35:29 +02:00
|
|
|
socklen = sizeof (*send_fd_);
|
|
|
|
rc = pgm_getsockopt (sock, IPPROTO_PGM, PGM_SEND_SOCK, send_fd_, &socklen);
|
|
|
|
zmq_assert (rc);
|
|
|
|
zmq_assert (socklen == sizeof (*receive_fd_));
|
|
|
|
|
|
|
|
socklen = sizeof (*receive_fd_);
|
|
|
|
rc = pgm_getsockopt (sock, IPPROTO_PGM, PGM_RECV_SOCK, receive_fd_, &socklen);
|
|
|
|
zmq_assert (rc);
|
|
|
|
zmq_assert (socklen == sizeof (*receive_fd_));
|
|
|
|
|
|
|
|
socklen = sizeof (*rdata_notify_fd_);
|
|
|
|
rc = pgm_getsockopt (sock, IPPROTO_PGM, PGM_REPAIR_SOCK, rdata_notify_fd_, &socklen);
|
|
|
|
zmq_assert (rc);
|
|
|
|
zmq_assert (socklen == sizeof (*rdata_notify_fd_));
|
|
|
|
|
|
|
|
socklen = sizeof (*pending_notify_fd_);
|
|
|
|
rc = pgm_getsockopt (sock, IPPROTO_PGM, PGM_PENDING_SOCK, pending_notify_fd_, &socklen);
|
|
|
|
zmq_assert (rc);
|
|
|
|
zmq_assert (socklen == sizeof (*pending_notify_fd_));
|
2009-09-11 17:58:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Send one APDU, transmit window owned memory.
|
2010-09-28 16:35:29 +02:00
|
|
|
// data_len_ must be less than one TPDU.
|
2009-09-11 17:58:37 +02:00
|
|
|
size_t zmq::pgm_socket_t::send (unsigned char *data_, size_t data_len_)
|
|
|
|
{
|
2009-09-28 18:06:06 +02:00
|
|
|
size_t nbytes = 0;
|
|
|
|
|
2010-09-28 16:35:29 +02:00
|
|
|
const int status = pgm_send (sock, data_, data_len_, &nbytes);
|
2009-09-28 18:06:06 +02:00
|
|
|
|
2009-09-11 17:58:37 +02:00
|
|
|
// We have to write all data as one packet.
|
2010-09-28 22:46:56 +02:00
|
|
|
if (nbytes > 0) {
|
|
|
|
zmq_assert (status == PGM_IO_STATUS_NORMAL);
|
2009-09-28 18:06:06 +02:00
|
|
|
zmq_assert ((ssize_t) nbytes == (ssize_t) data_len_);
|
2010-09-28 22:46:56 +02:00
|
|
|
} else {
|
|
|
|
zmq_assert (status == PGM_IO_STATUS_RATE_LIMITED || status == PGM_IO_STATUS_WOULD_BLOCK);
|
|
|
|
|
|
|
|
if (status == PGM_IO_STATUS_RATE_LIMITED)
|
|
|
|
errno = ENOMEM;
|
|
|
|
else
|
|
|
|
errno = EBUSY;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save return value.
|
|
|
|
last_tx_status = status;
|
2009-09-11 17:58:37 +02:00
|
|
|
|
|
|
|
return nbytes;
|
|
|
|
}
|
|
|
|
|
2010-09-28 22:46:56 +02:00
|
|
|
long zmq::pgm_socket_t::get_rx_timeout ()
|
|
|
|
{
|
|
|
|
if (last_rx_status != PGM_IO_STATUS_RATE_LIMITED && last_rx_status != PGM_IO_STATUS_TIMER_PENDING)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
struct timeval tv;
|
|
|
|
socklen_t optlen = sizeof (tv);
|
|
|
|
const bool rc = pgm_getsockopt (sock, IPPROTO_PGM, last_rx_status == PGM_IO_STATUS_RATE_LIMITED ? PGM_RATE_REMAIN : PGM_TIME_REMAIN, &tv, &optlen);
|
|
|
|
zmq_assert (rc);
|
|
|
|
|
|
|
|
const long timeout = (tv.tv_sec * 1000) + (tv.tv_usec / 1000);
|
|
|
|
|
|
|
|
return timeout;
|
|
|
|
}
|
|
|
|
|
|
|
|
long zmq::pgm_socket_t::get_tx_timeout ()
|
|
|
|
{
|
|
|
|
if (last_tx_status != PGM_IO_STATUS_RATE_LIMITED)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
struct timeval tv;
|
|
|
|
socklen_t optlen = sizeof (tv);
|
|
|
|
const bool rc = pgm_getsockopt (sock, IPPROTO_PGM, PGM_RATE_REMAIN, &tv, &optlen);
|
|
|
|
zmq_assert (rc);
|
|
|
|
|
|
|
|
const long timeout = (tv.tv_sec * 1000) + (tv.tv_usec / 1000);
|
|
|
|
|
|
|
|
return timeout;
|
|
|
|
}
|
|
|
|
|
2009-09-11 17:58:37 +02:00
|
|
|
// Return max TSDU size without fragmentation from current PGM transport.
|
2009-12-13 11:27:43 +01:00
|
|
|
size_t zmq::pgm_socket_t::get_max_tsdu_size ()
|
2009-09-11 17:58:37 +02:00
|
|
|
{
|
2010-09-28 16:35:29 +02:00
|
|
|
int max_tsdu = 0;
|
|
|
|
socklen_t optlen = sizeof (max_tsdu);
|
|
|
|
|
|
|
|
bool rc = pgm_getsockopt (sock, IPPROTO_PGM, PGM_MSS, &max_tsdu, &optlen);
|
|
|
|
zmq_assert (rc);
|
|
|
|
zmq_assert (optlen == sizeof (max_tsdu));
|
|
|
|
return (size_t) max_tsdu;
|
2009-09-25 17:50:12 +02:00
|
|
|
}
|
|
|
|
|
2010-09-28 16:35:29 +02:00
|
|
|
// pgm_recvmsgv is called to fill the pgm_msgv array up to
|
2009-09-11 17:58:37 +02:00
|
|
|
// pgm_msgv_len. In subsequent calls data from pgm_msgv structure are
|
|
|
|
// returned.
|
2009-09-22 15:12:51 +02:00
|
|
|
ssize_t zmq::pgm_socket_t::receive (void **raw_data_, const pgm_tsi_t **tsi_)
|
2009-09-11 17:58:37 +02:00
|
|
|
{
|
2009-09-24 16:23:49 +02:00
|
|
|
size_t raw_data_len = 0;
|
|
|
|
|
2009-09-11 17:58:37 +02:00
|
|
|
// We just sent all data from pgm_transport_recvmsgv up
|
|
|
|
// and have to return 0 that another engine in this thread is scheduled.
|
|
|
|
if (nbytes_rec == nbytes_processed && nbytes_rec > 0) {
|
|
|
|
|
|
|
|
// Reset all the counters.
|
|
|
|
nbytes_rec = 0;
|
|
|
|
nbytes_processed = 0;
|
|
|
|
pgm_msgv_processed = 0;
|
2010-09-28 16:58:51 +02:00
|
|
|
errno = EAGAIN;
|
2010-09-28 22:46:56 +02:00
|
|
|
return 0;
|
2009-09-11 17:58:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// If we have are going first time or if we have processed all pgm_msgv_t
|
2009-09-16 10:11:01 +02:00
|
|
|
// structure previously read from the pgm socket.
|
2009-09-11 17:58:37 +02:00
|
|
|
if (nbytes_rec == nbytes_processed) {
|
|
|
|
|
|
|
|
// Check program flow.
|
|
|
|
zmq_assert (pgm_msgv_processed == 0);
|
|
|
|
zmq_assert (nbytes_processed == 0);
|
|
|
|
zmq_assert (nbytes_rec == 0);
|
|
|
|
|
|
|
|
// Receive a vector of Application Protocol Domain Unit's (APDUs)
|
|
|
|
// from the transport.
|
2010-09-28 16:35:29 +02:00
|
|
|
pgm_error_t *pgm_error = NULL;
|
2009-09-25 17:50:12 +02:00
|
|
|
|
2010-09-28 16:35:29 +02:00
|
|
|
const int status = pgm_recvmsgv (sock, pgm_msgv,
|
|
|
|
pgm_msgv_len, MSG_ERRQUEUE, &nbytes_rec, &pgm_error);
|
2009-12-21 18:50:24 +01:00
|
|
|
|
2010-09-28 16:58:51 +02:00
|
|
|
// Invalid parameters
|
2009-12-28 11:51:06 +01:00
|
|
|
zmq_assert (status != PGM_IO_STATUS_ERROR);
|
2009-12-21 18:50:24 +01:00
|
|
|
|
2010-09-28 22:46:56 +02:00
|
|
|
last_rx_status = status;
|
|
|
|
|
2009-09-25 17:50:12 +02:00
|
|
|
// In a case when no ODATA/RDATA fired POLLIN event (SPM...)
|
2010-09-28 16:35:29 +02:00
|
|
|
// pgm_recvmsg returns PGM_IO_STATUS_TIMER_PENDING.
|
2009-11-03 19:54:43 +01:00
|
|
|
if (status == PGM_IO_STATUS_TIMER_PENDING) {
|
|
|
|
|
2009-09-29 13:56:19 +02:00
|
|
|
zmq_assert (nbytes_rec == 0);
|
|
|
|
|
2009-09-25 17:50:12 +02:00
|
|
|
// In case if no RDATA/ODATA caused POLLIN 0 is
|
|
|
|
// returned.
|
|
|
|
nbytes_rec = 0;
|
2010-09-28 16:58:51 +02:00
|
|
|
errno = EBUSY;
|
2010-09-28 22:46:56 +02:00
|
|
|
return 0;
|
2009-09-25 17:50:12 +02:00
|
|
|
}
|
2009-11-03 19:54:43 +01:00
|
|
|
|
2010-09-28 16:35:29 +02:00
|
|
|
// Send SPMR, NAK, ACK is rate limited.
|
|
|
|
if (status == PGM_IO_STATUS_RATE_LIMITED) {
|
|
|
|
|
|
|
|
zmq_assert (nbytes_rec == 0);
|
|
|
|
|
|
|
|
// In case if no RDATA/ODATA caused POLLIN 0 is
|
|
|
|
// returned.
|
|
|
|
nbytes_rec = 0;
|
2010-09-28 22:46:56 +02:00
|
|
|
errno = ENOMEM;
|
|
|
|
return 0;
|
2010-09-28 16:35:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// No peers and hence no incoming packets.
|
|
|
|
if (status == PGM_IO_STATUS_WOULD_BLOCK) {
|
|
|
|
|
|
|
|
zmq_assert (nbytes_rec == 0);
|
|
|
|
|
|
|
|
// In case if no RDATA/ODATA caused POLLIN 0 is
|
|
|
|
// returned.
|
|
|
|
nbytes_rec = 0;
|
2010-09-28 16:58:51 +02:00
|
|
|
errno = EAGAIN;
|
2010-09-28 22:46:56 +02:00
|
|
|
return 0;
|
2010-09-28 16:35:29 +02:00
|
|
|
}
|
|
|
|
|
2009-09-29 13:56:19 +02:00
|
|
|
// Data loss.
|
|
|
|
if (status == PGM_IO_STATUS_RESET) {
|
|
|
|
|
2010-09-28 16:35:29 +02:00
|
|
|
struct pgm_sk_buff_t* skb = pgm_msgv[0].msgv_skb[0];
|
2009-09-29 13:56:19 +02:00
|
|
|
|
|
|
|
// Save lost data TSI.
|
2010-09-28 16:35:29 +02:00
|
|
|
*tsi_ = &skb->tsi;
|
2009-09-29 13:56:19 +02:00
|
|
|
nbytes_rec = 0;
|
|
|
|
|
|
|
|
// In case of dala loss -1 is returned.
|
2009-12-18 14:19:28 +01:00
|
|
|
errno = EINVAL;
|
2010-09-28 16:35:29 +02:00
|
|
|
pgm_free_skb (skb);
|
2009-09-29 13:56:19 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-12-13 11:27:43 +01:00
|
|
|
zmq_assert (status == PGM_IO_STATUS_NORMAL);
|
2009-09-11 17:58:37 +02:00
|
|
|
}
|
2010-05-13 12:41:20 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
zmq_assert (pgm_msgv_processed <= pgm_msgv_len);
|
|
|
|
}
|
2009-09-11 17:58:37 +02:00
|
|
|
|
2010-09-28 22:46:56 +02:00
|
|
|
// Zero byte payloads are valid in PGM, but not 0MQ protocol.
|
2009-09-11 17:58:37 +02:00
|
|
|
zmq_assert (nbytes_rec > 0);
|
|
|
|
|
2009-09-25 17:50:12 +02:00
|
|
|
// Only one APDU per pgm_msgv_t structure is allowed.
|
|
|
|
zmq_assert (pgm_msgv [pgm_msgv_processed].msgv_len == 1);
|
|
|
|
|
|
|
|
struct pgm_sk_buff_t* skb =
|
|
|
|
pgm_msgv [pgm_msgv_processed].msgv_skb [0];
|
|
|
|
|
|
|
|
// Take pointers from pgm_msgv_t structure.
|
|
|
|
*raw_data_ = skb->data;
|
|
|
|
raw_data_len = skb->len;
|
|
|
|
|
|
|
|
// Save current TSI.
|
|
|
|
*tsi_ = &skb->tsi;
|
2009-09-11 17:58:37 +02:00
|
|
|
|
|
|
|
// Move the the next pgm_msgv_t structure.
|
|
|
|
pgm_msgv_processed++;
|
2010-05-13 12:43:58 +02:00
|
|
|
zmq_assert (pgm_msgv_processed <= pgm_msgv_len);
|
2009-09-11 17:58:37 +02:00
|
|
|
nbytes_processed +=raw_data_len;
|
|
|
|
|
|
|
|
return raw_data_len;
|
|
|
|
}
|
|
|
|
|
2009-12-13 11:27:43 +01:00
|
|
|
void zmq::pgm_socket_t::process_upstream ()
|
2009-09-11 17:58:37 +02:00
|
|
|
{
|
2009-09-28 18:06:06 +02:00
|
|
|
pgm_msgv_t dummy_msg;
|
2009-09-24 16:23:49 +02:00
|
|
|
|
2009-09-28 18:06:06 +02:00
|
|
|
size_t dummy_bytes = 0;
|
2010-09-28 16:35:29 +02:00
|
|
|
pgm_error_t *pgm_error = NULL;
|
2009-09-28 18:06:06 +02:00
|
|
|
|
2010-09-28 16:35:29 +02:00
|
|
|
const int status = pgm_recvmsgv (sock, &dummy_msg,
|
|
|
|
1, MSG_ERRQUEUE, &dummy_bytes, &pgm_error);
|
2009-09-28 18:06:06 +02:00
|
|
|
|
2010-09-28 16:58:51 +02:00
|
|
|
// Invalid parameters
|
2009-12-28 11:51:06 +01:00
|
|
|
zmq_assert (status != PGM_IO_STATUS_ERROR);
|
2009-12-21 18:50:24 +01:00
|
|
|
|
2009-09-28 18:06:06 +02:00
|
|
|
// No data should be returned.
|
2009-11-04 18:59:19 +01:00
|
|
|
zmq_assert (dummy_bytes == 0 && (status == PGM_IO_STATUS_TIMER_PENDING ||
|
2010-09-28 16:35:29 +02:00
|
|
|
status == PGM_IO_STATUS_RATE_LIMITED || status == PGM_IO_STATUS_WOULD_BLOCK));
|
2010-09-28 22:46:56 +02:00
|
|
|
|
|
|
|
last_rx_status = status;
|
|
|
|
|
|
|
|
if (status == PGM_IO_STATUS_TIMER_PENDING)
|
|
|
|
errno = EBUSY;
|
|
|
|
else if (status == PGM_IO_STATUS_RATE_LIMITED)
|
|
|
|
errno = ENOMEM;
|
|
|
|
else
|
|
|
|
errno = EAGAIN;
|
2009-09-11 17:58:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|