2009-08-12 09:40:16 +02:00
|
|
|
/*
|
2014-01-02 12:00:57 +01:00
|
|
|
Copyright (c) 2007-2014 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
|
2010-10-30 15:08:28 +02:00
|
|
|
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
|
2010-10-30 15:08:28 +02:00
|
|
|
GNU Lesser General Public License for more details.
|
2009-08-12 09:40:16 +02:00
|
|
|
|
2010-10-30 15:08:28 +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"
|
2013-09-15 20:07:33 +02:00
|
|
|
#include "../include/zmq_utils.h"
|
2009-08-12 09:40:16 +02:00
|
|
|
|
|
|
|
zmq::options_t::options_t () :
|
2011-10-06 13:12:49 +02:00
|
|
|
sndhwm (1000),
|
|
|
|
rcvhwm (1000),
|
2009-09-11 17:58:37 +02:00
|
|
|
affinity (0),
|
2011-11-02 14:33:58 +01:00
|
|
|
identity_size (0),
|
2009-09-14 11:25:57 +02:00
|
|
|
rate (100),
|
2011-03-24 14:36:40 +01:00
|
|
|
recovery_ivl (10000),
|
2011-05-15 18:25:43 +02:00
|
|
|
multicast_hops (1),
|
2009-12-10 09:47:24 +01:00
|
|
|
sndbuf (0),
|
|
|
|
rcvbuf (0),
|
2013-11-25 04:01:21 +01:00
|
|
|
tos (0),
|
2010-09-28 15:27:45 +02:00
|
|
|
type (-1),
|
2010-10-16 10:53:29 +02:00
|
|
|
linger (-1),
|
2010-10-17 09:54:12 +02:00
|
|
|
reconnect_ivl (100),
|
2011-01-26 07:01:06 +01:00
|
|
|
reconnect_ivl_max (0),
|
2010-10-17 10:23:58 +02:00
|
|
|
backlog (100),
|
2011-03-02 09:00:36 +01:00
|
|
|
maxmsgsize (-1),
|
2011-06-17 12:22:02 +02:00
|
|
|
rcvtimeo (-1),
|
|
|
|
sndtimeo (-1),
|
2013-01-31 20:47:45 +01:00
|
|
|
ipv6 (0),
|
2013-03-12 16:11:19 +01:00
|
|
|
immediate (0),
|
2011-11-04 08:00:47 +01:00
|
|
|
filter (false),
|
2012-03-20 01:41:20 +01:00
|
|
|
recv_identity (false),
|
2012-11-09 14:12:11 +01:00
|
|
|
raw_sock (false),
|
2012-04-05 17:39:53 +02:00
|
|
|
tcp_keepalive (-1),
|
|
|
|
tcp_keepalive_cnt (-1),
|
|
|
|
tcp_keepalive_idle (-1),
|
|
|
|
tcp_keepalive_intvl (-1),
|
2013-05-15 17:54:03 +02:00
|
|
|
mechanism (ZMQ_NULL),
|
2013-05-17 17:46:30 +02:00
|
|
|
as_server (0),
|
2014-04-23 21:45:13 +02:00
|
|
|
gss_plaintext (false),
|
2013-08-17 21:59:07 +02:00
|
|
|
socket_id (0),
|
2014-04-23 21:45:13 +02:00
|
|
|
conflate (false)
|
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_)
|
|
|
|
{
|
2013-01-30 23:53:09 +01:00
|
|
|
bool is_int = (optvallen_ == sizeof (int));
|
|
|
|
int value = is_int? *((int *) optval_): 0;
|
2013-09-09 20:40:34 +02:00
|
|
|
|
2009-09-21 14:39:59 +02:00
|
|
|
switch (option_) {
|
2013-01-30 23:53:09 +01:00
|
|
|
case ZMQ_SNDHWM:
|
2013-05-15 17:54:03 +02:00
|
|
|
if (is_int && value >= 0) {
|
2013-01-30 23:53:09 +01:00
|
|
|
sndhwm = value;
|
2013-05-15 17:54:03 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2013-01-30 23:53:09 +01:00
|
|
|
break;
|
2013-09-09 20:40:34 +02:00
|
|
|
|
2013-01-30 23:53:09 +01:00
|
|
|
case ZMQ_RCVHWM:
|
2013-05-15 17:54:03 +02:00
|
|
|
if (is_int && value >= 0) {
|
2013-01-30 23:53:09 +01:00
|
|
|
rcvhwm = value;
|
2013-05-15 17:54:03 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2013-01-30 23:53:09 +01:00
|
|
|
break;
|
2009-09-21 14:39:59 +02:00
|
|
|
|
2013-01-30 23:53:09 +01:00
|
|
|
case ZMQ_AFFINITY:
|
2013-05-15 17:54:03 +02:00
|
|
|
if (optvallen_ == sizeof (uint64_t)) {
|
2013-01-30 23:53:09 +01:00
|
|
|
affinity = *((uint64_t*) optval_);
|
2013-05-15 17:54:03 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2013-01-30 23:53:09 +01:00
|
|
|
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);
|
2013-05-15 17:54:03 +02:00
|
|
|
return 0;
|
2013-01-30 23:53:09 +01:00
|
|
|
}
|
|
|
|
break;
|
2009-09-21 14:39:59 +02:00
|
|
|
|
2013-01-30 23:53:09 +01:00
|
|
|
case ZMQ_RATE:
|
2013-05-15 17:54:03 +02:00
|
|
|
if (is_int && value > 0) {
|
2013-01-30 23:53:09 +01:00
|
|
|
rate = value;
|
2013-05-15 17:54:03 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2013-01-30 23:53:09 +01:00
|
|
|
break;
|
2009-09-21 14:39:59 +02:00
|
|
|
|
2013-01-30 23:53:09 +01:00
|
|
|
case ZMQ_RECOVERY_IVL:
|
2013-05-15 17:54:03 +02:00
|
|
|
if (is_int && value >= 0) {
|
2013-01-30 23:53:09 +01:00
|
|
|
recovery_ivl = value;
|
2013-05-15 17:54:03 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
break;
|
2011-11-02 14:33:58 +01:00
|
|
|
|
2013-01-30 23:53:09 +01:00
|
|
|
case ZMQ_SNDBUF:
|
2013-05-15 17:54:03 +02:00
|
|
|
if (is_int && value >= 0) {
|
2013-01-30 23:53:09 +01:00
|
|
|
sndbuf = value;
|
2013-05-15 17:54:03 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2013-01-30 23:53:09 +01:00
|
|
|
break;
|
2011-11-02 14:33:58 +01:00
|
|
|
|
2013-01-30 23:53:09 +01:00
|
|
|
case ZMQ_RCVBUF:
|
2013-05-15 17:54:03 +02:00
|
|
|
if (is_int && value >= 0) {
|
2013-01-30 23:53:09 +01:00
|
|
|
rcvbuf = value;
|
2013-05-15 17:54:03 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2013-01-30 23:53:09 +01:00
|
|
|
break;
|
2012-04-13 11:26:57 +02:00
|
|
|
|
2013-11-25 04:01:21 +01:00
|
|
|
case ZMQ_TOS:
|
|
|
|
if (is_int && value >= 0) {
|
|
|
|
tos = value;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2013-01-30 23:53:09 +01:00
|
|
|
case ZMQ_LINGER:
|
2013-05-15 17:54:03 +02:00
|
|
|
if (is_int && value >= -1) {
|
2013-01-30 23:53:09 +01:00
|
|
|
linger = value;
|
2013-05-15 17:54:03 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2013-01-30 23:53:09 +01:00
|
|
|
break;
|
2009-09-21 14:39:59 +02:00
|
|
|
|
2013-01-30 23:53:09 +01:00
|
|
|
case ZMQ_RECONNECT_IVL:
|
2013-05-15 17:54:03 +02:00
|
|
|
if (is_int && value >= -1) {
|
2013-01-30 23:53:09 +01:00
|
|
|
reconnect_ivl = value;
|
2013-05-15 17:54:03 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2013-01-30 23:53:09 +01:00
|
|
|
break;
|
2009-12-10 09:47:24 +01:00
|
|
|
|
2013-01-30 23:53:09 +01:00
|
|
|
case ZMQ_RECONNECT_IVL_MAX:
|
2013-05-15 17:54:03 +02:00
|
|
|
if (is_int && value >= 0) {
|
2013-01-30 23:53:09 +01:00
|
|
|
reconnect_ivl_max = value;
|
2013-05-15 17:54:03 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2013-01-30 23:53:09 +01:00
|
|
|
break;
|
2010-10-16 10:53:29 +02:00
|
|
|
|
2013-01-30 23:53:09 +01:00
|
|
|
case ZMQ_BACKLOG:
|
2013-05-15 17:54:03 +02:00
|
|
|
if (is_int && value >= 0) {
|
2013-01-30 23:53:09 +01:00
|
|
|
backlog = value;
|
2013-05-15 17:54:03 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2013-01-30 23:53:09 +01:00
|
|
|
break;
|
2010-10-17 09:54:12 +02:00
|
|
|
|
2013-01-30 23:53:09 +01:00
|
|
|
case ZMQ_MAXMSGSIZE:
|
2013-05-15 17:54:03 +02:00
|
|
|
if (optvallen_ == sizeof (int64_t)) {
|
2013-01-30 23:53:09 +01:00
|
|
|
maxmsgsize = *((int64_t *) optval_);
|
2013-05-15 17:54:03 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2013-02-20 14:05:55 +01:00
|
|
|
break;
|
2010-10-17 10:23:58 +02:00
|
|
|
|
2013-01-30 23:53:09 +01:00
|
|
|
case ZMQ_MULTICAST_HOPS:
|
2013-05-15 17:54:03 +02:00
|
|
|
if (is_int && value > 0) {
|
2013-01-30 23:53:09 +01:00
|
|
|
multicast_hops = value;
|
2013-05-15 17:54:03 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2013-01-30 23:53:09 +01:00
|
|
|
break;
|
2011-01-26 07:01:06 +01:00
|
|
|
|
2013-01-30 23:53:09 +01:00
|
|
|
case ZMQ_RCVTIMEO:
|
2013-05-15 17:54:03 +02:00
|
|
|
if (is_int && value >= -1) {
|
2013-01-30 23:53:09 +01:00
|
|
|
rcvtimeo = value;
|
2013-05-15 17:54:03 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2013-01-30 23:53:09 +01:00
|
|
|
break;
|
2010-10-17 10:23:58 +02:00
|
|
|
|
2013-01-30 23:53:09 +01:00
|
|
|
case ZMQ_SNDTIMEO:
|
2013-05-15 17:54:03 +02:00
|
|
|
if (is_int && value >= -1) {
|
2013-01-30 23:53:09 +01:00
|
|
|
sndtimeo = value;
|
2013-05-15 17:54:03 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2013-01-30 23:53:09 +01:00
|
|
|
break;
|
2011-03-02 09:00:36 +01:00
|
|
|
|
2013-01-31 20:47:45 +01:00
|
|
|
/* Deprecated in favor of ZMQ_IPV6 */
|
2013-01-30 23:53:09 +01:00
|
|
|
case ZMQ_IPV4ONLY:
|
2013-05-15 17:54:03 +02:00
|
|
|
if (is_int && (value == 0 || value == 1)) {
|
2013-02-19 14:18:50 +01:00
|
|
|
ipv6 = (value == 0);
|
2013-05-15 17:54:03 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2013-01-30 23:53:09 +01:00
|
|
|
break;
|
2011-05-15 18:25:43 +02:00
|
|
|
|
2013-01-30 23:53:09 +01:00
|
|
|
/* To replace the somewhat surprising IPV4ONLY */
|
|
|
|
case ZMQ_IPV6:
|
2013-05-15 17:54:03 +02:00
|
|
|
if (is_int && (value == 0 || value == 1)) {
|
2013-02-19 14:18:50 +01:00
|
|
|
ipv6 = (value != 0);
|
2013-05-15 17:54:03 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2013-01-30 23:53:09 +01:00
|
|
|
break;
|
2011-06-17 12:22:02 +02:00
|
|
|
|
2013-01-30 23:53:09 +01:00
|
|
|
case ZMQ_TCP_KEEPALIVE:
|
2014-01-31 12:02:42 +01:00
|
|
|
if (is_int && (value == -1 || value == 0 || value == 1)) {
|
2013-01-30 23:53:09 +01:00
|
|
|
tcp_keepalive = value;
|
2013-05-15 17:54:03 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2013-01-30 23:53:09 +01:00
|
|
|
break;
|
2011-06-17 12:22:02 +02:00
|
|
|
|
2013-01-30 23:53:09 +01:00
|
|
|
case ZMQ_TCP_KEEPALIVE_CNT:
|
2013-05-15 17:54:03 +02:00
|
|
|
if (is_int && (value == -1 || value >= 0)) {
|
2013-01-30 23:53:09 +01:00
|
|
|
tcp_keepalive_cnt = value;
|
2013-05-15 17:54:03 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2013-01-30 23:53:09 +01:00
|
|
|
break;
|
2009-09-21 14:39:59 +02:00
|
|
|
|
2013-01-30 23:53:09 +01:00
|
|
|
case ZMQ_TCP_KEEPALIVE_IDLE:
|
2013-05-15 17:54:03 +02:00
|
|
|
if (is_int && (value == -1 || value >= 0)) {
|
2013-01-30 23:53:09 +01:00
|
|
|
tcp_keepalive_idle = value;
|
2013-05-15 17:54:03 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2013-01-30 23:53:09 +01:00
|
|
|
break;
|
2012-04-05 17:39:53 +02:00
|
|
|
|
2013-01-30 23:53:09 +01:00
|
|
|
case ZMQ_TCP_KEEPALIVE_INTVL:
|
2013-05-15 17:54:03 +02:00
|
|
|
if (is_int && (value == -1 || value >= 0)) {
|
2013-01-30 23:53:09 +01:00
|
|
|
tcp_keepalive_intvl = value;
|
2013-05-15 17:54:03 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2013-01-30 23:53:09 +01:00
|
|
|
break;
|
2013-09-09 20:40:34 +02:00
|
|
|
|
2013-03-12 16:11:19 +01:00
|
|
|
case ZMQ_IMMEDIATE:
|
2013-05-15 17:54:03 +02:00
|
|
|
if (is_int && (value == 0 || value == 1)) {
|
2013-03-12 16:11:19 +01:00
|
|
|
immediate = value;
|
2013-05-15 17:54:03 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2013-01-30 23:53:09 +01:00
|
|
|
break;
|
2012-04-12 16:37:14 +02:00
|
|
|
|
2013-01-30 23:53:09 +01:00
|
|
|
case ZMQ_TCP_ACCEPT_FILTER:
|
2013-05-15 17:54:03 +02:00
|
|
|
if (optvallen_ == 0 && optval_ == NULL) {
|
2012-04-12 16:37:14 +02:00
|
|
|
tcp_accept_filters.clear ();
|
2013-05-15 17:54:03 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2012-04-12 16:37:14 +02:00
|
|
|
else
|
2013-05-15 17:54:03 +02:00
|
|
|
if (optvallen_ > 0 && optvallen_ < 256 && optval_ != NULL && *((const char*) optval_) != 0) {
|
2013-01-30 23:53:09 +01:00
|
|
|
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);
|
2013-05-15 17:54:03 +02:00
|
|
|
if (rc == 0) {
|
2013-01-30 23:53:09 +01:00
|
|
|
tcp_accept_filters.push_back (mask);
|
2013-05-15 17:54:03 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2013-09-09 20:40:34 +02:00
|
|
|
|
2013-12-04 23:17:39 +01:00
|
|
|
# if defined ZMQ_HAVE_SO_PEERCRED || defined ZMQ_HAVE_LOCAL_PEERCRED
|
2013-12-06 18:58:10 +01:00
|
|
|
case ZMQ_IPC_FILTER_UID:
|
2013-12-04 23:17:39 +01:00
|
|
|
if (optvallen_ == 0 && optval_ == NULL) {
|
|
|
|
ipc_uid_accept_filters.clear ();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
if (optvallen_ == sizeof (uid_t) && optval_ != NULL) {
|
|
|
|
ipc_uid_accept_filters.insert (*((uid_t *) optval_));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2013-12-06 18:58:10 +01:00
|
|
|
case ZMQ_IPC_FILTER_GID:
|
2013-12-04 23:17:39 +01:00
|
|
|
if (optvallen_ == 0 && optval_ == NULL) {
|
|
|
|
ipc_gid_accept_filters.clear ();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
if (optvallen_ == sizeof (gid_t) && optval_ != NULL) {
|
|
|
|
ipc_gid_accept_filters.insert (*((gid_t *) optval_));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
# endif
|
|
|
|
|
|
|
|
# if defined ZMQ_HAVE_SO_PEERCRED
|
2013-12-06 18:58:10 +01:00
|
|
|
case ZMQ_IPC_FILTER_PID:
|
2013-12-04 23:17:39 +01:00
|
|
|
if (optvallen_ == 0 && optval_ == NULL) {
|
|
|
|
ipc_pid_accept_filters.clear ();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
if (optvallen_ == sizeof (pid_t) && optval_ != NULL) {
|
|
|
|
ipc_pid_accept_filters.insert (*((pid_t *) optval_));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
# endif
|
|
|
|
|
2013-09-17 14:05:41 +02:00
|
|
|
case ZMQ_PLAIN_SERVER:
|
|
|
|
if (is_int && (value == 0 || value == 1)) {
|
2013-05-17 17:46:30 +02:00
|
|
|
as_server = value;
|
2013-09-17 14:05:41 +02:00
|
|
|
mechanism = value? ZMQ_PLAIN: ZMQ_NULL;
|
2013-05-15 17:54:03 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
break;
|
2013-09-09 20:40:34 +02:00
|
|
|
|
2013-05-15 17:54:03 +02:00
|
|
|
case ZMQ_PLAIN_USERNAME:
|
|
|
|
if (optvallen_ == 0 && optval_ == NULL) {
|
|
|
|
mechanism = ZMQ_NULL;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else
|
2013-07-03 22:30:48 +02:00
|
|
|
if (optvallen_ > 0 && optvallen_ < 256 && optval_ != NULL) {
|
2013-05-15 17:54:03 +02:00
|
|
|
plain_username.assign ((const char *) optval_, optvallen_);
|
2013-09-17 14:05:41 +02:00
|
|
|
as_server = 0;
|
2013-05-15 17:54:03 +02:00
|
|
|
mechanism = ZMQ_PLAIN;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
break;
|
2013-09-09 20:40:34 +02:00
|
|
|
|
2013-05-15 17:54:03 +02:00
|
|
|
case ZMQ_PLAIN_PASSWORD:
|
|
|
|
if (optvallen_ == 0 && optval_ == NULL) {
|
|
|
|
mechanism = ZMQ_NULL;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else
|
2013-07-03 22:30:48 +02:00
|
|
|
if (optvallen_ > 0 && optvallen_ < 256 && optval_ != NULL) {
|
2013-05-15 17:54:03 +02:00
|
|
|
plain_password.assign ((const char *) optval_, optvallen_);
|
2013-09-17 14:05:41 +02:00
|
|
|
as_server = 0;
|
2013-05-15 17:54:03 +02:00
|
|
|
mechanism = ZMQ_PLAIN;
|
|
|
|
return 0;
|
2012-04-12 16:37:14 +02:00
|
|
|
}
|
2013-01-30 23:53:09 +01:00
|
|
|
break;
|
2013-09-09 20:40:34 +02:00
|
|
|
|
|
|
|
case ZMQ_ZAP_DOMAIN:
|
2013-09-26 11:33:44 +02:00
|
|
|
if (optvallen_ < 256) {
|
2013-09-09 20:40:34 +02:00
|
|
|
zap_domain.assign ((const char *) optval_, optvallen_);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
Added Z85 support
The use of binary for CURVE keys is painful; you cannot easily copy
these in e.g. email, or use them directly in source code. There are
various encoding possibilities. Base16 and Base64 are not optimal.
Ascii85 is not safe for source (it generates quotes and escapes).
So, I've designed a new Base85 encoding, Z85, which is safe to use
in code and elsewhere, and I've modified libzmq to use this where
it also uses binary keys (in get/setsockopt).
Very simply, if you use a 32-byte value, it's Base256 (binary),
and if you use a 40-byte value, it's Base85 (Z85).
I've put the Z85 codec into z85_codec.hpp, it's not elegant C++
but it is minimal and it works. Feel free to rewrap as a real class
if this annoys you.
2013-06-28 22:10:22 +02:00
|
|
|
// If libsodium isn't installed, these options provoke EINVAL
|
2013-06-28 12:04:01 +02:00
|
|
|
# ifdef HAVE_LIBSODIUM
|
2013-09-17 14:05:41 +02:00
|
|
|
case ZMQ_CURVE_SERVER:
|
2013-09-17 14:05:55 +02:00
|
|
|
if (is_int && (value == 0 || value == 1)) {
|
2013-06-20 18:09:12 +02:00
|
|
|
as_server = value;
|
2013-09-17 14:05:55 +02:00
|
|
|
mechanism = value? ZMQ_CURVE: ZMQ_NULL;
|
2013-06-20 18:09:12 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
break;
|
2013-09-09 20:40:34 +02:00
|
|
|
|
2013-09-17 14:05:55 +02:00
|
|
|
case ZMQ_CURVE_PUBLICKEY:
|
|
|
|
if (optvallen_ == CURVE_KEYSIZE) {
|
|
|
|
memcpy (curve_public_key, optval_, CURVE_KEYSIZE);
|
|
|
|
mechanism = ZMQ_CURVE;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
if (optvallen_ == CURVE_KEYSIZE_Z85) {
|
|
|
|
zmq_z85_decode (curve_public_key, (char *) optval_);
|
|
|
|
mechanism = ZMQ_CURVE;
|
|
|
|
return 0;
|
|
|
|
}
|
2013-06-20 18:09:12 +02:00
|
|
|
break;
|
2013-09-09 20:40:34 +02:00
|
|
|
|
2013-09-17 14:05:55 +02:00
|
|
|
case ZMQ_CURVE_SECRETKEY:
|
|
|
|
if (optvallen_ == CURVE_KEYSIZE) {
|
|
|
|
memcpy (curve_secret_key, optval_, CURVE_KEYSIZE);
|
|
|
|
mechanism = ZMQ_CURVE;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
if (optvallen_ == CURVE_KEYSIZE_Z85) {
|
|
|
|
zmq_z85_decode (curve_secret_key, (char *) optval_);
|
|
|
|
mechanism = ZMQ_CURVE;
|
|
|
|
return 0;
|
|
|
|
}
|
2013-09-17 12:44:14 +02:00
|
|
|
break;
|
|
|
|
|
2013-09-17 14:05:55 +02:00
|
|
|
case ZMQ_CURVE_SERVERKEY:
|
|
|
|
if (optvallen_ == CURVE_KEYSIZE) {
|
|
|
|
memcpy (curve_server_key, optval_, CURVE_KEYSIZE);
|
|
|
|
as_server = 0;
|
|
|
|
mechanism = ZMQ_CURVE;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
if (optvallen_ == CURVE_KEYSIZE_Z85) {
|
|
|
|
zmq_z85_decode (curve_server_key, (char *) optval_);
|
|
|
|
as_server = 0;
|
|
|
|
mechanism = ZMQ_CURVE;
|
|
|
|
return 0;
|
|
|
|
}
|
2013-06-20 18:09:12 +02:00
|
|
|
break;
|
2013-06-28 12:04:01 +02:00
|
|
|
# endif
|
2013-09-25 05:20:01 +02:00
|
|
|
|
2013-08-17 21:59:07 +02:00
|
|
|
case ZMQ_CONFLATE:
|
|
|
|
if (is_int && (value == 0 || value == 1)) {
|
|
|
|
conflate = (value != 0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
break;
|
2013-09-25 05:20:01 +02:00
|
|
|
|
|
|
|
case ZMQ_GSSAPI_SERVER:
|
|
|
|
if (is_int && (value == 0 || value == 1)) {
|
|
|
|
as_server = value;
|
|
|
|
mechanism = ZMQ_GSSAPI;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2014-04-23 19:20:22 +02:00
|
|
|
case ZMQ_GSSAPI_PRINCIPAL:
|
2013-11-20 23:01:16 +01:00
|
|
|
if (optvallen_ > 0 && optvallen_ < 256 && optval_ != NULL) {
|
2014-04-23 19:20:22 +02:00
|
|
|
gss_principal.assign ((const char *) optval_, optvallen_);
|
2013-11-20 23:01:16 +01:00
|
|
|
mechanism = ZMQ_GSSAPI;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2014-04-23 19:20:22 +02:00
|
|
|
case ZMQ_GSSAPI_SERVICE_PRINCIPAL:
|
2013-11-20 23:01:16 +01:00
|
|
|
if (optvallen_ > 0 && optvallen_ < 256 && optval_ != NULL) {
|
2014-04-23 19:20:22 +02:00
|
|
|
gss_service_principal.assign ((const char *) optval_, optvallen_);
|
2013-09-25 05:20:01 +02:00
|
|
|
mechanism = ZMQ_GSSAPI;
|
2013-11-21 19:46:23 +01:00
|
|
|
as_server = 0;
|
2013-09-25 05:20:01 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
break;
|
2013-08-17 21:59:07 +02:00
|
|
|
|
2014-04-23 20:01:54 +02:00
|
|
|
case ZMQ_GSSAPI_PLAINTEXT:
|
|
|
|
if (is_int && (value == 0 || value == 1)) {
|
|
|
|
gss_plaintext = (value != 0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
2013-08-17 21:59:07 +02:00
|
|
|
default:
|
|
|
|
break;
|
2013-01-30 23:53:09 +01:00
|
|
|
}
|
2013-05-15 17:54:03 +02:00
|
|
|
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_)
|
|
|
|
{
|
2013-05-15 17:54:03 +02:00
|
|
|
bool is_int = (*optvallen_ == sizeof (int));
|
|
|
|
int *value = (int *) optval_;
|
2013-09-09 20:40:34 +02:00
|
|
|
|
2010-04-09 13:04:15 +02:00
|
|
|
switch (option_) {
|
2013-05-15 17:54:03 +02:00
|
|
|
case ZMQ_SNDHWM:
|
|
|
|
if (is_int) {
|
|
|
|
*value = sndhwm;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ZMQ_RCVHWM:
|
|
|
|
if (is_int) {
|
|
|
|
*value = rcvhwm;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ZMQ_AFFINITY:
|
|
|
|
if (*optvallen_ == sizeof (uint64_t)) {
|
|
|
|
*((uint64_t *) optval_) = affinity;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ZMQ_IDENTITY:
|
|
|
|
if (*optvallen_ >= identity_size) {
|
|
|
|
memcpy (optval_, identity, identity_size);
|
|
|
|
*optvallen_ = identity_size;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ZMQ_RATE:
|
|
|
|
if (is_int) {
|
|
|
|
*value = rate;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ZMQ_RECOVERY_IVL:
|
|
|
|
if (is_int) {
|
|
|
|
*value = recovery_ivl;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ZMQ_SNDBUF:
|
|
|
|
if (is_int) {
|
|
|
|
*value = sndbuf;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ZMQ_RCVBUF:
|
|
|
|
if (is_int) {
|
|
|
|
*value = rcvbuf;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2013-11-25 04:01:21 +01:00
|
|
|
case ZMQ_TOS:
|
|
|
|
if (is_int) {
|
|
|
|
*value = tos;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
break;
|
2013-05-15 17:54:03 +02:00
|
|
|
case ZMQ_TYPE:
|
|
|
|
if (is_int) {
|
|
|
|
*value = type;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ZMQ_LINGER:
|
|
|
|
if (is_int) {
|
|
|
|
*value = linger;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
break;
|
2010-04-09 13:04:15 +02:00
|
|
|
|
2013-05-15 17:54:03 +02:00
|
|
|
case ZMQ_RECONNECT_IVL:
|
|
|
|
if (is_int) {
|
|
|
|
*value = reconnect_ivl;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ZMQ_RECONNECT_IVL_MAX:
|
|
|
|
if (is_int) {
|
|
|
|
*value = reconnect_ivl_max;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ZMQ_BACKLOG:
|
|
|
|
if (is_int) {
|
|
|
|
*value = backlog;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ZMQ_MAXMSGSIZE:
|
|
|
|
if (*optvallen_ == sizeof (int64_t)) {
|
|
|
|
*((int64_t *) optval_) = maxmsgsize;
|
|
|
|
*optvallen_ = sizeof (int64_t);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ZMQ_MULTICAST_HOPS:
|
|
|
|
if (is_int) {
|
|
|
|
*value = multicast_hops;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ZMQ_RCVTIMEO:
|
|
|
|
if (is_int) {
|
|
|
|
*value = rcvtimeo;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ZMQ_SNDTIMEO:
|
|
|
|
if (is_int) {
|
|
|
|
*value = sndtimeo;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ZMQ_IPV4ONLY:
|
|
|
|
if (is_int) {
|
|
|
|
*value = 1 - ipv6;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
break;
|
2013-09-09 20:40:34 +02:00
|
|
|
|
2013-05-15 17:54:03 +02:00
|
|
|
case ZMQ_IPV6:
|
|
|
|
if (is_int) {
|
|
|
|
*value = ipv6;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ZMQ_IMMEDIATE:
|
|
|
|
if (is_int) {
|
|
|
|
*value = immediate;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ZMQ_TCP_KEEPALIVE:
|
|
|
|
if (is_int) {
|
|
|
|
*value = tcp_keepalive;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ZMQ_TCP_KEEPALIVE_CNT:
|
|
|
|
if (is_int) {
|
|
|
|
*value = tcp_keepalive_cnt;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ZMQ_TCP_KEEPALIVE_IDLE:
|
|
|
|
if (is_int) {
|
|
|
|
*value = tcp_keepalive_idle;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ZMQ_TCP_KEEPALIVE_INTVL:
|
|
|
|
if (is_int) {
|
|
|
|
*value = tcp_keepalive_intvl;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ZMQ_MECHANISM:
|
|
|
|
if (is_int) {
|
|
|
|
*value = mechanism;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
break;
|
2013-09-09 20:40:34 +02:00
|
|
|
|
2013-09-17 14:05:41 +02:00
|
|
|
case ZMQ_PLAIN_SERVER:
|
2013-05-15 17:54:03 +02:00
|
|
|
if (is_int) {
|
2013-09-17 14:05:41 +02:00
|
|
|
*value = as_server && mechanism == ZMQ_PLAIN;
|
2013-05-15 17:54:03 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
break;
|
2013-09-09 20:40:34 +02:00
|
|
|
|
2013-05-15 17:54:03 +02:00
|
|
|
case ZMQ_PLAIN_USERNAME:
|
|
|
|
if (*optvallen_ >= plain_username.size () + 1) {
|
|
|
|
memcpy (optval_, plain_username.c_str (), plain_username.size () + 1);
|
|
|
|
*optvallen_ = plain_username.size () + 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
break;
|
2013-09-09 20:40:34 +02:00
|
|
|
|
2013-05-15 17:54:03 +02:00
|
|
|
case ZMQ_PLAIN_PASSWORD:
|
|
|
|
if (*optvallen_ >= plain_password.size () + 1) {
|
|
|
|
memcpy (optval_, plain_password.c_str (), plain_password.size () + 1);
|
|
|
|
*optvallen_ = plain_password.size () + 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
break;
|
2013-09-09 20:40:34 +02:00
|
|
|
|
|
|
|
case ZMQ_ZAP_DOMAIN:
|
|
|
|
if (*optvallen_ >= zap_domain.size () + 1) {
|
|
|
|
memcpy (optval_, zap_domain.c_str (), zap_domain.size () + 1);
|
|
|
|
*optvallen_ = zap_domain.size () + 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
Added Z85 support
The use of binary for CURVE keys is painful; you cannot easily copy
these in e.g. email, or use them directly in source code. There are
various encoding possibilities. Base16 and Base64 are not optimal.
Ascii85 is not safe for source (it generates quotes and escapes).
So, I've designed a new Base85 encoding, Z85, which is safe to use
in code and elsewhere, and I've modified libzmq to use this where
it also uses binary keys (in get/setsockopt).
Very simply, if you use a 32-byte value, it's Base256 (binary),
and if you use a 40-byte value, it's Base85 (Z85).
I've put the Z85 codec into z85_codec.hpp, it's not elegant C++
but it is minimal and it works. Feel free to rewrap as a real class
if this annoys you.
2013-06-28 22:10:22 +02:00
|
|
|
// If libsodium isn't installed, these options provoke EINVAL
|
2013-06-28 12:04:01 +02:00
|
|
|
# ifdef HAVE_LIBSODIUM
|
2013-09-17 14:05:41 +02:00
|
|
|
case ZMQ_CURVE_SERVER:
|
2013-06-20 18:09:12 +02:00
|
|
|
if (is_int) {
|
2013-09-17 14:05:55 +02:00
|
|
|
*value = as_server && mechanism == ZMQ_CURVE;
|
2013-06-20 18:09:12 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
break;
|
2013-09-09 20:40:34 +02:00
|
|
|
|
2013-09-17 14:05:55 +02:00
|
|
|
case ZMQ_CURVE_PUBLICKEY:
|
|
|
|
if (*optvallen_ == CURVE_KEYSIZE) {
|
|
|
|
memcpy (optval_, curve_public_key, CURVE_KEYSIZE);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
if (*optvallen_ == CURVE_KEYSIZE_Z85 + 1) {
|
|
|
|
zmq_z85_encode ((char *) optval_, curve_public_key, CURVE_KEYSIZE);
|
|
|
|
return 0;
|
|
|
|
}
|
2013-06-20 18:09:12 +02:00
|
|
|
break;
|
2013-09-09 20:40:34 +02:00
|
|
|
|
2013-09-17 14:05:55 +02:00
|
|
|
case ZMQ_CURVE_SECRETKEY:
|
|
|
|
if (*optvallen_ == CURVE_KEYSIZE) {
|
|
|
|
memcpy (optval_, curve_secret_key, CURVE_KEYSIZE);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
if (*optvallen_ == CURVE_KEYSIZE_Z85 + 1) {
|
|
|
|
zmq_z85_encode ((char *) optval_, curve_secret_key, CURVE_KEYSIZE);
|
|
|
|
return 0;
|
|
|
|
}
|
2013-09-17 12:44:14 +02:00
|
|
|
break;
|
|
|
|
|
2013-09-17 14:05:55 +02:00
|
|
|
case ZMQ_CURVE_SERVERKEY:
|
|
|
|
if (*optvallen_ == CURVE_KEYSIZE) {
|
|
|
|
memcpy (optval_, curve_server_key, CURVE_KEYSIZE);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
if (*optvallen_ == CURVE_KEYSIZE_Z85 + 1) {
|
|
|
|
zmq_z85_encode ((char *) optval_, curve_server_key, CURVE_KEYSIZE);
|
|
|
|
return 0;
|
|
|
|
}
|
2013-06-20 18:09:12 +02:00
|
|
|
break;
|
2013-06-28 12:04:01 +02:00
|
|
|
# endif
|
2013-08-17 21:59:07 +02:00
|
|
|
|
|
|
|
case ZMQ_CONFLATE:
|
|
|
|
if (is_int) {
|
|
|
|
*value = conflate;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
break;
|
2013-09-25 05:20:01 +02:00
|
|
|
|
|
|
|
case ZMQ_GSSAPI_SERVER:
|
|
|
|
if (is_int) {
|
|
|
|
*value = as_server && mechanism == ZMQ_GSSAPI;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
break;
|
2013-11-20 23:01:16 +01:00
|
|
|
|
2014-04-23 19:20:22 +02:00
|
|
|
case ZMQ_GSSAPI_PRINCIPAL:
|
|
|
|
if (*optvallen_ >= gss_principal.size () + 1) {
|
|
|
|
memcpy (optval_, gss_principal.c_str (), gss_principal.size () + 1);
|
|
|
|
*optvallen_ = gss_principal.size () + 1;
|
2013-09-25 05:20:01 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
break;
|
2013-08-17 21:59:07 +02:00
|
|
|
|
2014-04-23 19:20:22 +02:00
|
|
|
case ZMQ_GSSAPI_SERVICE_PRINCIPAL:
|
|
|
|
if (*optvallen_ >= gss_service_principal.size () + 1) {
|
|
|
|
memcpy (optval_, gss_service_principal.c_str (), gss_service_principal.size () + 1);
|
|
|
|
*optvallen_ = gss_service_principal.size () + 1;
|
2013-11-20 23:01:16 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
break;
|
2013-09-25 05:20:01 +02:00
|
|
|
|
2014-04-23 20:01:54 +02:00
|
|
|
case ZMQ_GSSAPI_PLAINTEXT:
|
|
|
|
if (is_int) {
|
|
|
|
*value = gss_plaintext;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
2013-09-25 05:20:01 +02:00
|
|
|
}
|
2010-04-09 13:04:15 +02:00
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|