libzmq/src/options.cpp

497 lines
13 KiB
C++
Raw Normal View History

2009-08-12 09:40:16 +02:00
/*
Copyright (c) 2007-2013 Contributors as noted in the AUTHORS file
2009-08-12 09:40:16 +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 GNU Lesser General Public License as published by
2009-08-12 09:40:16 +02:00
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
GNU Lesser General Public License for more details.
2009-08-12 09:40:16 +02:00
You should have received a copy of the GNU Lesser General Public License
2009-08-12 09:40:16 +02:00
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2010-04-09 13:04:15 +02:00
#include <string.h>
2009-08-12 09:40:16 +02:00
#include "options.hpp"
2009-09-21 14:39:59 +02:00
#include "err.hpp"
2009-08-12 09:40:16 +02:00
zmq::options_t::options_t () :
sndhwm (1000),
rcvhwm (1000),
2009-09-11 17:58:37 +02:00
affinity (0),
identity_size (0),
rate (100),
recovery_ivl (10000),
multicast_hops (1),
sndbuf (0),
rcvbuf (0),
2010-09-28 15:27:45 +02:00
type (-1),
linger (-1),
reconnect_ivl (100),
reconnect_ivl_max (0),
backlog (100),
maxmsgsize (-1),
rcvtimeo (-1),
sndtimeo (-1),
2013-01-31 20:47:45 +01:00
ipv6 (0),
delay_attach_on_connect (0),
delay_on_close (true),
delay_on_disconnect (true),
filter (false),
recv_identity (false),
2012-11-09 14:12:11 +01:00
raw_sock (false),
tcp_keepalive (-1),
tcp_keepalive_cnt (-1),
tcp_keepalive_idle (-1),
tcp_keepalive_intvl (-1),
socket_id (0)
2009-08-12 09:40:16 +02:00
{
}
2009-09-21 14:39:59 +02:00
int zmq::options_t::setsockopt (int option_, const void *optval_,
size_t optvallen_)
{
bool valid = true;
bool is_int = (optvallen_ == sizeof (int));
int value = is_int? *((int *) optval_): 0;
2009-09-21 14:39:59 +02:00
switch (option_) {
case ZMQ_SNDHWM:
if (is_int && value >= 0)
sndhwm = value;
else
valid = false;
break;
case ZMQ_RCVHWM:
if (is_int && value >= 0)
rcvhwm = value;
else
valid = false;
break;
2009-09-21 14:39:59 +02:00
case ZMQ_AFFINITY:
if (optvallen_ == sizeof (uint64_t))
affinity = *((uint64_t*) optval_);
else
valid = false;
break;
case ZMQ_IDENTITY:
// Empty identity is invalid as well as identity longer than
// 255 bytes. Identity starting with binary zero is invalid
// as these are used for auto-generated identities.
if (optvallen_ > 0 && optvallen_ < 256
&& *((const unsigned char *) optval_) != 0) {
identity_size = optvallen_;
memcpy (identity, optval_, identity_size);
}
else
valid = false;
break;
2009-09-21 14:39:59 +02:00
case ZMQ_RATE:
if (is_int && value > 0)
rate = value;
else
valid = false;
break;
2009-09-21 14:39:59 +02:00
case ZMQ_RECOVERY_IVL:
if (is_int && value >= 0)
recovery_ivl = value;
else
valid = false;
case ZMQ_SNDBUF:
if (is_int && value >= 0)
sndbuf = value;
else
valid = false;
break;
case ZMQ_RCVBUF:
if (is_int && value >= 0)
rcvbuf = value;
else
valid = false;
break;
case ZMQ_LINGER:
if (is_int && value >= -1)
linger = value;
else
valid = false;
break;
2009-09-21 14:39:59 +02:00
case ZMQ_RECONNECT_IVL:
if (is_int && value >= -1)
reconnect_ivl = value;
else
valid = false;
break;
case ZMQ_RECONNECT_IVL_MAX:
if (is_int && value >= 0)
reconnect_ivl_max = value;
else
valid = false;
break;
case ZMQ_BACKLOG:
if (is_int && value >= 0)
backlog = value;
else
valid = false;
break;
case ZMQ_MAXMSGSIZE:
if (optvallen_ == sizeof (int64_t))
maxmsgsize = *((int64_t *) optval_);
else
valid = false;
break;
case ZMQ_MULTICAST_HOPS:
if (is_int && value > 0)
multicast_hops = value;
else
valid = false;
break;
case ZMQ_RCVTIMEO:
if (is_int && value >= -1)
rcvtimeo = value;
else
valid = false;
break;
case ZMQ_SNDTIMEO:
if (is_int && value >= -1)
sndtimeo = value;
else
valid = false;
break;
2013-01-31 20:47:45 +01:00
/* Deprecated in favor of ZMQ_IPV6 */
case ZMQ_IPV4ONLY:
if (is_int && (value == 0 || value == 1))
ipv6 = (value == 0);
else
valid = false;
break;
/* To replace the somewhat surprising IPV4ONLY */
case ZMQ_IPV6:
if (is_int && (value == 0 || value == 1))
ipv6 = (value != 0);
else
valid = false;
break;
case ZMQ_TCP_KEEPALIVE:
if (is_int && (value >= -1 || value <= 1))
tcp_keepalive = value;
else
valid = false;
break;
case ZMQ_TCP_KEEPALIVE_CNT:
if (is_int && (value == -1 || value >= 0))
tcp_keepalive_cnt = value;
else
valid = false;
break;
2009-09-21 14:39:59 +02:00
case ZMQ_TCP_KEEPALIVE_IDLE:
if (is_int && (value == -1 || value >= 0))
tcp_keepalive_idle = value;
else
valid = false;
break;
case ZMQ_TCP_KEEPALIVE_INTVL:
if (is_int && (value == -1 || value >= 0))
tcp_keepalive_intvl = value;
else
valid = false;
break;
case ZMQ_DELAY_ATTACH_ON_CONNECT:
if (is_int && (value == 0 || value == 1))
delay_attach_on_connect = value;
else
valid = false;
break;
case ZMQ_TCP_ACCEPT_FILTER:
if (optvallen_ == 0 && optval_ == NULL)
tcp_accept_filters.clear ();
else
if (optvallen_ < 1 || optvallen_ > 255 || optval_ == NULL || *((const char*) optval_) == 0)
valid = false;
else {
std::string filter_str ((const char *) optval_, optvallen_);
2013-01-01 10:24:51 +01:00
tcp_address_mask_t mask;
2013-01-31 20:47:45 +01:00
int rc = mask.resolve (filter_str.c_str (), ipv6);
if (rc == 0)
tcp_accept_filters.push_back (mask);
else
valid = false;
}
break;
default:
valid = false;
break;
}
if (valid)
return 0;
else {
errno = EINVAL;
return -1;
}
2009-09-21 14:39:59 +02:00
}
2010-04-09 13:04:15 +02:00
int zmq::options_t::getsockopt (int option_, void *optval_, size_t *optvallen_)
{
switch (option_) {
case ZMQ_SNDHWM:
if (*optvallen_ < sizeof (int)) {
2010-04-09 13:04:15 +02:00
errno = EINVAL;
return -1;
}
*((int*) optval_) = sndhwm;
*optvallen_ = sizeof (int);
return 0;
case ZMQ_RCVHWM:
if (*optvallen_ < sizeof (int)) {
errno = EINVAL;
return -1;
}
*((int*) optval_) = rcvhwm;
*optvallen_ = sizeof (int);
2010-04-09 13:04:15 +02:00
return 0;
case ZMQ_AFFINITY:
if (*optvallen_ < sizeof (uint64_t)) {
errno = EINVAL;
return -1;
}
*((uint64_t*) optval_) = affinity;
*optvallen_ = sizeof (uint64_t);
return 0;
case ZMQ_IDENTITY:
if (*optvallen_ < identity_size) {
errno = EINVAL;
return -1;
}
memcpy (optval_, identity, identity_size);
*optvallen_ = identity_size;
return 0;
2010-04-09 13:04:15 +02:00
case ZMQ_RATE:
if (*optvallen_ < sizeof (int)) {
2010-04-09 13:04:15 +02:00
errno = EINVAL;
return -1;
}
*((int*) optval_) = rate;
*optvallen_ = sizeof (int);
2010-04-09 13:04:15 +02:00
return 0;
2010-04-09 13:04:15 +02:00
case ZMQ_RECOVERY_IVL:
if (*optvallen_ < sizeof (int)) {
2010-04-09 13:04:15 +02:00
errno = EINVAL;
return -1;
}
*((int*) optval_) = recovery_ivl;
*optvallen_ = sizeof (int);
2010-04-09 13:04:15 +02:00
return 0;
case ZMQ_SNDBUF:
if (*optvallen_ < sizeof (int)) {
2010-04-09 13:04:15 +02:00
errno = EINVAL;
return -1;
}
*((int*) optval_) = sndbuf;
*optvallen_ = sizeof (int);
2010-04-09 13:04:15 +02:00
return 0;
case ZMQ_RCVBUF:
if (*optvallen_ < sizeof (int)) {
2010-04-09 13:04:15 +02:00
errno = EINVAL;
return -1;
}
*((int*) optval_) = rcvbuf;
*optvallen_ = sizeof (int);
2010-04-09 13:04:15 +02:00
return 0;
case ZMQ_TYPE:
if (*optvallen_ < sizeof (int)) {
errno = EINVAL;
return -1;
}
*((int*) optval_) = type;
*optvallen_ = sizeof (int);
return 0;
case ZMQ_LINGER:
if (*optvallen_ < sizeof (int)) {
errno = EINVAL;
return -1;
}
*((int*) optval_) = linger;
*optvallen_ = sizeof (int);
return 0;
case ZMQ_RECONNECT_IVL:
if (*optvallen_ < sizeof (int)) {
errno = EINVAL;
return -1;
}
*((int*) optval_) = reconnect_ivl;
*optvallen_ = sizeof (int);
return 0;
case ZMQ_RECONNECT_IVL_MAX:
if (*optvallen_ < sizeof (int)) {
errno = EINVAL;
return -1;
}
*((int*) optval_) = reconnect_ivl_max;
*optvallen_ = sizeof (int);
return 0;
case ZMQ_BACKLOG:
if (*optvallen_ < sizeof (int)) {
errno = EINVAL;
return -1;
}
*((int*) optval_) = backlog;
*optvallen_ = sizeof (int);
return 0;
case ZMQ_MAXMSGSIZE:
if (*optvallen_ < sizeof (int64_t)) {
errno = EINVAL;
return -1;
}
*((int64_t*) optval_) = maxmsgsize;
*optvallen_ = sizeof (int64_t);
return 0;
case ZMQ_MULTICAST_HOPS:
if (*optvallen_ < sizeof (int)) {
errno = EINVAL;
return -1;
}
*((int*) optval_) = multicast_hops;
*optvallen_ = sizeof (int);
return 0;
case ZMQ_RCVTIMEO:
if (*optvallen_ < sizeof (int)) {
errno = EINVAL;
return -1;
}
*((int*) optval_) = rcvtimeo;
*optvallen_ = sizeof (int);
return 0;
case ZMQ_SNDTIMEO:
if (*optvallen_ < sizeof (int)) {
errno = EINVAL;
return -1;
}
*((int*) optval_) = sndtimeo;
*optvallen_ = sizeof (int);
return 0;
case ZMQ_IPV4ONLY:
if (*optvallen_ < sizeof (int)) {
errno = EINVAL;
return -1;
}
2013-01-31 20:47:45 +01:00
*((int*) optval_) = 1 - ipv6;
*optvallen_ = sizeof (int);
return 0;
case ZMQ_IPV6:
if (*optvallen_ < sizeof (int)) {
errno = EINVAL;
return -1;
}
2013-01-31 20:47:45 +01:00
*((int*) optval_) = ipv6;
*optvallen_ = sizeof (int);
return 0;
case ZMQ_DELAY_ATTACH_ON_CONNECT:
2013-01-31 20:47:45 +01:00
if (*optvallen_ < sizeof (int)) {
errno = EINVAL;
return -1;
}
*((int*) optval_) = delay_attach_on_connect;
*optvallen_ = sizeof (int);
return 0;
case ZMQ_TCP_KEEPALIVE:
if (*optvallen_ < sizeof (int)) {
errno = EINVAL;
return -1;
}
*((int*) optval_) = tcp_keepalive;
*optvallen_ = sizeof (int);
return 0;
case ZMQ_TCP_KEEPALIVE_CNT:
if (*optvallen_ < sizeof (int)) {
errno = EINVAL;
return -1;
}
*((int*) optval_) = tcp_keepalive_cnt;
*optvallen_ = sizeof (int);
return 0;
case ZMQ_TCP_KEEPALIVE_IDLE:
if (*optvallen_ < sizeof (int)) {
errno = EINVAL;
return -1;
}
*((int*) optval_) = tcp_keepalive_idle;
*optvallen_ = sizeof (int);
return 0;
case ZMQ_TCP_KEEPALIVE_INTVL:
if (*optvallen_ < sizeof (int)) {
errno = EINVAL;
return -1;
}
*((int*) optval_) = tcp_keepalive_intvl;
*optvallen_ = sizeof (int);
return 0;
case ZMQ_LAST_ENDPOINT:
2013-01-31 20:47:45 +01:00
/* don't allow string which cannot contain the entire message */
2012-02-15 00:10:06 +01:00
if (*optvallen_ < last_endpoint.size() + 1) {
errno = EINVAL;
return -1;
}
2012-02-15 00:10:06 +01:00
memcpy (optval_, last_endpoint.c_str(), last_endpoint.size()+1);
*optvallen_ = last_endpoint.size()+1;
return 0;
2010-04-09 13:04:15 +02:00
}
errno = EINVAL;
return -1;
}