2009-08-12 09:40:16 +02:00
|
|
|
/*
|
2011-03-02 16:30:40 +01:00
|
|
|
Copyright (c) 2007-2011 iMatix Corporation
|
|
|
|
Copyright (c) 2007-2011 Other 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"
|
2009-08-12 09:40:16 +02:00
|
|
|
|
|
|
|
zmq::options_t::options_t () :
|
2011-03-24 16:47:33 +01:00
|
|
|
sndhwm (0),
|
|
|
|
rcvhwm (0),
|
2009-09-11 17:58:37 +02:00
|
|
|
affinity (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),
|
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-12 15:24:08 +02:00
|
|
|
filter (1),
|
2010-03-20 10:58:59 +01:00
|
|
|
immediate_connect (true)
|
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_)
|
|
|
|
{
|
|
|
|
switch (option_) {
|
|
|
|
|
2011-03-24 16:47:33 +01:00
|
|
|
case ZMQ_SNDHWM:
|
2011-03-24 15:43:03 +01:00
|
|
|
if (optvallen_ != sizeof (int) || *((int*) optval_) < 0) {
|
2009-09-21 14:39:59 +02:00
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
2011-03-24 16:47:33 +01:00
|
|
|
sndhwm = *((int*) optval_);
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case ZMQ_RCVHWM:
|
|
|
|
if (optvallen_ != sizeof (int) || *((int*) optval_) < 0) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
rcvhwm = *((int*) optval_);
|
2009-09-21 14:39:59 +02:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
case ZMQ_AFFINITY:
|
2010-04-09 13:04:15 +02:00
|
|
|
if (optvallen_ != sizeof (uint64_t)) {
|
2009-09-21 14:39:59 +02:00
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
2010-04-09 13:04:15 +02:00
|
|
|
affinity = *((uint64_t*) optval_);
|
2009-09-21 14:39:59 +02:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
case ZMQ_IDENTITY:
|
2010-02-19 15:24:43 +01:00
|
|
|
|
|
|
|
// 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_ < 1 || optvallen_ > 255 ||
|
|
|
|
*((const unsigned char*) optval_) == 0) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
2010-02-13 15:30:03 +01:00
|
|
|
identity.assign ((const unsigned char*) optval_, optvallen_);
|
2009-09-21 14:39:59 +02:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
case ZMQ_RATE:
|
2011-03-24 15:18:20 +01:00
|
|
|
if (optvallen_ != sizeof (int) || *((int*) optval_) <= 0) {
|
2009-09-21 14:39:59 +02:00
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
2011-03-24 15:18:20 +01:00
|
|
|
rate = *((int*) optval_);
|
2009-09-21 14:39:59 +02:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
case ZMQ_RECOVERY_IVL:
|
2011-03-24 15:18:20 +01:00
|
|
|
if (optvallen_ != sizeof (int) || *((int*) optval_) < 0) {
|
2009-09-21 14:39:59 +02:00
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
2011-03-24 15:18:20 +01:00
|
|
|
recovery_ivl = *((int*) optval_);
|
2009-09-21 14:39:59 +02:00
|
|
|
return 0;
|
|
|
|
|
2009-12-10 09:47:24 +01:00
|
|
|
case ZMQ_SNDBUF:
|
2011-03-24 14:48:50 +01:00
|
|
|
if (optvallen_ != sizeof (int) || *((int*) optval_) < 0) {
|
2009-12-10 09:47:24 +01:00
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
2011-03-24 14:48:50 +01:00
|
|
|
sndbuf = *((int*) optval_);
|
2009-12-10 09:47:24 +01:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
case ZMQ_RCVBUF:
|
2011-03-24 14:48:50 +01:00
|
|
|
if (optvallen_ != sizeof (int) || *((int*) optval_) < 0) {
|
2009-12-10 09:47:24 +01:00
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
2011-03-24 14:48:50 +01:00
|
|
|
rcvbuf = *((int*) optval_);
|
2009-12-10 09:47:24 +01:00
|
|
|
return 0;
|
2010-10-16 10:53:29 +02:00
|
|
|
|
|
|
|
case ZMQ_LINGER:
|
|
|
|
if (optvallen_ != sizeof (int)) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
linger = *((int*) optval_);
|
|
|
|
return 0;
|
2010-10-17 09:54:12 +02:00
|
|
|
|
|
|
|
case ZMQ_RECONNECT_IVL:
|
|
|
|
if (optvallen_ != sizeof (int)) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (*((int*) optval_) < 0) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
reconnect_ivl = *((int*) optval_);
|
|
|
|
return 0;
|
2010-10-17 10:23:58 +02:00
|
|
|
|
2011-01-26 07:01:06 +01:00
|
|
|
case ZMQ_RECONNECT_IVL_MAX:
|
|
|
|
if (optvallen_ != sizeof (int)) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (*((int*) optval_) < 0) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
reconnect_ivl_max = *((int*) optval_);
|
|
|
|
return 0;
|
|
|
|
|
2010-10-17 10:23:58 +02:00
|
|
|
case ZMQ_BACKLOG:
|
|
|
|
if (optvallen_ != sizeof (int)) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
backlog = *((int*) optval_);
|
|
|
|
return 0;
|
|
|
|
|
2011-03-02 09:00:36 +01:00
|
|
|
case ZMQ_MAXMSGSIZE:
|
|
|
|
if (optvallen_ != sizeof (int64_t)) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
maxmsgsize = *((int64_t*) optval_);
|
|
|
|
return 0;
|
|
|
|
|
2011-05-15 18:25:43 +02:00
|
|
|
case ZMQ_MULTICAST_HOPS:
|
|
|
|
if (optvallen_ != sizeof (int) || *((int*) optval_) <= 0) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
multicast_hops = *((int*) optval_);
|
|
|
|
return 0;
|
|
|
|
|
2011-06-12 15:24:08 +02:00
|
|
|
case ZMQ_FILTER:
|
|
|
|
if (optvallen_ != sizeof (int) || (*((int*) optval_) != 0 &&
|
|
|
|
*((int*) optval_) != 1)) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
filter = *((int*) optval_);
|
|
|
|
return 0;
|
|
|
|
|
2009-09-21 14:39:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
2010-04-09 13:04:15 +02:00
|
|
|
|
|
|
|
int zmq::options_t::getsockopt (int option_, void *optval_, size_t *optvallen_)
|
|
|
|
{
|
|
|
|
switch (option_) {
|
|
|
|
|
2011-03-24 16:47:33 +01:00
|
|
|
case ZMQ_SNDHWM:
|
2011-03-24 15:43:03 +01:00
|
|
|
if (*optvallen_ < sizeof (int)) {
|
2010-04-09 13:04:15 +02:00
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
2011-03-24 16:47:33 +01:00
|
|
|
*((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.data (), identity.size ());
|
|
|
|
*optvallen_ = identity.size ();
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case ZMQ_RATE:
|
2011-03-24 15:18:20 +01:00
|
|
|
if (*optvallen_ < sizeof (int)) {
|
2010-04-09 13:04:15 +02:00
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
2011-03-24 15:18:20 +01:00
|
|
|
*((int*) optval_) = rate;
|
|
|
|
*optvallen_ = sizeof (int);
|
2010-04-09 13:04:15 +02:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
case ZMQ_RECOVERY_IVL:
|
2011-03-24 15:18:20 +01:00
|
|
|
if (*optvallen_ < sizeof (int)) {
|
2010-04-09 13:04:15 +02:00
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
2011-03-24 15:18:20 +01:00
|
|
|
*((int*) optval_) = recovery_ivl;
|
|
|
|
*optvallen_ = sizeof (int);
|
2010-04-09 13:04:15 +02:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
case ZMQ_SNDBUF:
|
2011-03-24 14:48:50 +01:00
|
|
|
if (*optvallen_ < sizeof (int)) {
|
2010-04-09 13:04:15 +02:00
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
2011-03-24 14:48:50 +01:00
|
|
|
*((int*) optval_) = sndbuf;
|
|
|
|
*optvallen_ = sizeof (int);
|
2010-04-09 13:04:15 +02:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
case ZMQ_RCVBUF:
|
2011-03-24 14:48:50 +01:00
|
|
|
if (*optvallen_ < sizeof (int)) {
|
2010-04-09 13:04:15 +02:00
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
2011-03-24 14:48:50 +01:00
|
|
|
*((int*) optval_) = rcvbuf;
|
|
|
|
*optvallen_ = sizeof (int);
|
2010-04-09 13:04:15 +02:00
|
|
|
return 0;
|
2010-10-17 09:54:12 +02:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2011-01-26 07:01:06 +01:00
|
|
|
case ZMQ_RECONNECT_IVL_MAX:
|
|
|
|
if (*optvallen_ < sizeof (int)) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
*((int*) optval_) = reconnect_ivl_max;
|
|
|
|
*optvallen_ = sizeof (int);
|
|
|
|
return 0;
|
|
|
|
|
2010-10-17 10:23:58 +02:00
|
|
|
case ZMQ_BACKLOG:
|
|
|
|
if (*optvallen_ < sizeof (int)) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
*((int*) optval_) = backlog;
|
|
|
|
*optvallen_ = sizeof (int);
|
|
|
|
return 0;
|
|
|
|
|
2011-03-02 09:00:36 +01:00
|
|
|
case ZMQ_MAXMSGSIZE:
|
|
|
|
if (*optvallen_ < sizeof (int64_t)) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
*((int64_t*) optval_) = maxmsgsize;
|
|
|
|
*optvallen_ = sizeof (int64_t);
|
|
|
|
return 0;
|
|
|
|
|
2011-05-15 18:25:43 +02:00
|
|
|
case ZMQ_MULTICAST_HOPS:
|
|
|
|
if (*optvallen_ < sizeof (int)) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
*((int*) optval_) = multicast_hops;
|
|
|
|
*optvallen_ = sizeof (int);
|
|
|
|
return 0;
|
|
|
|
|
2011-06-12 15:24:08 +02:00
|
|
|
case ZMQ_FILTER:
|
|
|
|
if (*optvallen_ < sizeof (int)) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
*((int*) optval_) = filter;
|
|
|
|
*optvallen_ = sizeof (int);
|
|
|
|
return 0;
|
|
|
|
|
2010-04-09 13:04:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|