2009-08-12 09:40:16 +02:00
|
|
|
/*
|
2010-01-05 08:29:35 +01:00
|
|
|
Copyright (c) 2007-2010 iMatix Corporation
|
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 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/>.
|
|
|
|
*/
|
|
|
|
|
2010-04-09 13:04:15 +02:00
|
|
|
#include <string.h>
|
|
|
|
|
2010-03-11 20:33:27 +01:00
|
|
|
#include "../include/zmq.h"
|
2009-09-21 14:39:59 +02:00
|
|
|
|
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 () :
|
|
|
|
hwm (0),
|
|
|
|
swap (0),
|
2009-09-11 17:58:37 +02:00
|
|
|
affinity (0),
|
2009-09-14 11:25:57 +02:00
|
|
|
rate (100),
|
2009-09-16 10:11:01 +02:00
|
|
|
recovery_ivl (10),
|
2009-09-23 10:22:39 +02:00
|
|
|
use_multicast_loop (true),
|
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),
|
2009-09-21 17:20:13 +02:00
|
|
|
requires_in (false),
|
2010-02-12 19:42:35 +01:00
|
|
|
requires_out (false),
|
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_) {
|
|
|
|
|
|
|
|
case ZMQ_HWM:
|
2010-03-01 10:13:26 +01:00
|
|
|
if (optvallen_ != sizeof (uint64_t)) {
|
2009-09-21 14:39:59 +02:00
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
2010-03-01 10:13:26 +01:00
|
|
|
hwm = *((uint64_t*) optval_);
|
2009-09-21 14:39:59 +02:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
case ZMQ_SWAP:
|
2010-09-15 16:44:57 +02:00
|
|
|
if (optvallen_ != sizeof (int64_t) || *((int64_t*) optval_) < 0) {
|
2009-09-21 14:39:59 +02:00
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
swap = *((int64_t*) optval_);
|
|
|
|
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:
|
2010-09-15 16:44:57 +02:00
|
|
|
if (optvallen_ != sizeof (int64_t) || *((int64_t*) optval_) < 0) {
|
2009-09-21 14:39:59 +02:00
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
rate = (uint32_t) *((int64_t*) optval_);
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case ZMQ_RECOVERY_IVL:
|
2010-09-15 16:44:57 +02:00
|
|
|
if (optvallen_ != sizeof (int64_t) || *((int64_t*) optval_) < 0) {
|
2009-09-21 14:39:59 +02:00
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
recovery_ivl = (uint32_t) *((int64_t*) optval_);
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case ZMQ_MCAST_LOOP:
|
|
|
|
if (optvallen_ != sizeof (int64_t)) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if ((int64_t) *((int64_t*) optval_) == 0)
|
|
|
|
use_multicast_loop = false;
|
|
|
|
else if ((int64_t) *((int64_t*) optval_) == 1)
|
|
|
|
use_multicast_loop = true;
|
|
|
|
else {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
2009-12-10 09:47:24 +01:00
|
|
|
|
|
|
|
case ZMQ_SNDBUF:
|
|
|
|
if (optvallen_ != sizeof (uint64_t)) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
sndbuf = *((uint64_t*) optval_);
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case ZMQ_RCVBUF:
|
|
|
|
if (optvallen_ != sizeof (uint64_t)) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
rcvbuf = *((uint64_t*) optval_);
|
|
|
|
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;
|
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_) {
|
|
|
|
|
2010-10-16 10:53:29 +02:00
|
|
|
case ZMQ_LINGER:
|
|
|
|
if (*optvallen_ < sizeof (int)) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
*((int*) optval_) = linger;
|
|
|
|
*optvallen_ = sizeof (int);
|
|
|
|
return 0;
|
|
|
|
|
2010-09-28 15:27:45 +02:00
|
|
|
case ZMQ_TYPE:
|
|
|
|
if (*optvallen_ < sizeof (int)) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
*((int*) optval_) = type;
|
|
|
|
*optvallen_ = sizeof (int);
|
|
|
|
return 0;
|
|
|
|
|
2010-04-09 13:04:15 +02:00
|
|
|
case ZMQ_HWM:
|
|
|
|
if (*optvallen_ < sizeof (uint64_t)) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
*((uint64_t*) optval_) = hwm;
|
|
|
|
*optvallen_ = sizeof (uint64_t);
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case ZMQ_SWAP:
|
|
|
|
if (*optvallen_ < sizeof (int64_t)) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
*((int64_t*) optval_) = swap;
|
|
|
|
*optvallen_ = sizeof (int64_t);
|
|
|
|
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:
|
|
|
|
if (*optvallen_ < sizeof (int64_t)) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
*((int64_t*) optval_) = rate;
|
|
|
|
*optvallen_ = sizeof (int64_t);
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case ZMQ_RECOVERY_IVL:
|
|
|
|
if (*optvallen_ < sizeof (int64_t)) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
*((int64_t*) optval_) = recovery_ivl;
|
|
|
|
*optvallen_ = sizeof (int64_t);
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case ZMQ_MCAST_LOOP:
|
|
|
|
if (*optvallen_ < sizeof (int64_t)) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
*((int64_t*) optval_) = use_multicast_loop ? 1 : 0;
|
|
|
|
*optvallen_ = sizeof (int64_t);
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case ZMQ_SNDBUF:
|
|
|
|
if (*optvallen_ < sizeof (uint64_t)) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
*((uint64_t*) optval_) = sndbuf;
|
|
|
|
*optvallen_ = sizeof (uint64_t);
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case ZMQ_RCVBUF:
|
|
|
|
if (*optvallen_ < sizeof (uint64_t)) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
*((uint64_t*) optval_) = rcvbuf;
|
|
|
|
*optvallen_ = sizeof (uint64_t);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|