options.cpp 14.2 KB
Newer Older
1
/*
2
    Copyright (c) 2007-2012 iMatix Corporation
Martin Sustrik's avatar
Martin Sustrik committed
3
    Copyright (c) 2009-2011 250bpm s.r.o.
4
    Copyright (c) 2011 VMware, Inc.
5
    Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file
6 7 8 9

    This file is part of 0MQ.

    0MQ is free software; you can redistribute it and/or modify it under
10
    the terms of the GNU Lesser General Public License as published by
11 12 13 14 15 16
    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
17
    GNU Lesser General Public License for more details.
18

19
    You should have received a copy of the GNU Lesser General Public License
20 21 22
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

23 24
#include <string.h>

25
#include "options.hpp"
26
#include "err.hpp"
27 28

zmq::options_t::options_t () :
29 30
    sndhwm (1000),
    rcvhwm (1000),
malosek's avatar
malosek committed
31
    affinity (0),
32
    identity_size (0),
33
    rate (100),
34
    recovery_ivl (10000),
35
    multicast_hops (1),
36 37
    sndbuf (0),
    rcvbuf (0),
38
    type (-1),
39
    linger (-1),
40
    reconnect_ivl (100),
41
    reconnect_ivl_max (0),
42
    backlog (100),
43
    maxmsgsize (-1),
44 45
    rcvtimeo (-1),
    sndtimeo (-1),
Steven McCoy's avatar
Steven McCoy committed
46
    ipv4only (1),
47
    delay_attach_on_connect (0),
48
    delay_on_close (true),
49
    delay_on_disconnect (true),
50 51
    filter (false),
    send_identity (false),
52
    recv_identity (false),
53 54 55 56
    tcp_keepalive (-1),
    tcp_keepalive_cnt (-1),
    tcp_keepalive_idle (-1),
    tcp_keepalive_intvl (-1),
57
    socket_id (0)
58 59
{
}
60 61 62 63 64 65

int zmq::options_t::setsockopt (int option_, const void *optval_,
    size_t optvallen_)
{
    switch (option_) {

66
    case ZMQ_SNDHWM:
67
        if (optvallen_ != sizeof (int) || *((int*) optval_) < 0) {
68 69 70
            errno = EINVAL;
            return -1;
        }
71 72 73 74 75 76 77 78 79
        sndhwm = *((int*) optval_);
        return 0;

    case ZMQ_RCVHWM:
        if (optvallen_ != sizeof (int) || *((int*) optval_) < 0) {
            errno = EINVAL;
            return -1;
        }
        rcvhwm = *((int*) optval_);
80 81 82
        return 0;

    case ZMQ_AFFINITY:
83
        if (optvallen_ != sizeof (uint64_t)) {
84 85 86
            errno = EINVAL;
            return -1;
        }
87
        affinity = *((uint64_t*) optval_);
88 89
        return 0;

90 91 92 93 94 95 96 97 98 99 100 101 102 103
    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_ < 1 || optvallen_ > 255 ||
              *((const unsigned char*) optval_) == 0) {
            errno = EINVAL;
            return -1;
        }
        identity_size = optvallen_;
        memcpy (identity, optval_, identity_size);
        return 0;

104
    case ZMQ_RATE:
105
        if (optvallen_ != sizeof (int) || *((int*) optval_) <= 0) {
106 107 108
            errno = EINVAL;
            return -1;
        }
109
        rate = *((int*) optval_);
110
        return 0;
111

112
    case ZMQ_RECOVERY_IVL:
113
        if (optvallen_ != sizeof (int) || *((int*) optval_) < 0) {
114 115 116
            errno = EINVAL;
            return -1;
        }
117
        recovery_ivl = *((int*) optval_);
118 119
        return 0;

120
    case ZMQ_SNDBUF:
121
        if (optvallen_ != sizeof (int) || *((int*) optval_) < 0) {
122 123 124
            errno = EINVAL;
            return -1;
        }
125
        sndbuf = *((int*) optval_);
126 127 128
        return 0;

    case ZMQ_RCVBUF:
129
        if (optvallen_ != sizeof (int) || *((int*) optval_) < 0) {
130 131 132
            errno = EINVAL;
            return -1;
        }
133
        rcvbuf = *((int*) optval_);
134
        return 0;
135 136 137 138 139 140 141 142

    case ZMQ_LINGER:
        if (optvallen_ != sizeof (int)) {
            errno = EINVAL;
            return -1;
        }
        linger = *((int*) optval_);
        return 0;
143 144 145 146 147 148

    case ZMQ_RECONNECT_IVL:
        if (optvallen_ != sizeof (int)) {
            errno = EINVAL;
            return -1;
        }
149
        if (*((int*) optval_) < -1) {
150 151 152 153 154
            errno = EINVAL;
            return -1;
        }
        reconnect_ivl = *((int*) optval_);
        return 0;
155

156 157 158 159 160 161 162 163 164 165 166 167
    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;

168 169 170 171 172 173 174 175
    case ZMQ_BACKLOG:
        if (optvallen_ != sizeof (int)) {
            errno = EINVAL;
            return -1;
        }
        backlog = *((int*) optval_);
        return 0;

176 177 178 179 180 181 182 183
    case ZMQ_MAXMSGSIZE:
        if (optvallen_ != sizeof (int64_t)) {
            errno = EINVAL;
            return -1;
        }
        maxmsgsize = *((int64_t*) optval_);
        return 0;

184 185 186 187 188 189 190 191
    case ZMQ_MULTICAST_HOPS:
        if (optvallen_ != sizeof (int) || *((int*) optval_) <= 0) {
            errno = EINVAL;
            return -1;
        }
        multicast_hops = *((int*) optval_);
        return 0;

192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
    case ZMQ_RCVTIMEO:
        if (optvallen_ != sizeof (int)) {
            errno = EINVAL;
            return -1;
        }
        rcvtimeo = *((int*) optval_);
        return 0;

    case ZMQ_SNDTIMEO:
        if (optvallen_ != sizeof (int)) {
            errno = EINVAL;
            return -1;
        }
        sndtimeo = *((int*) optval_);
        return 0;

Steven McCoy's avatar
Steven McCoy committed
208 209 210 211 212 213 214 215 216 217 218 219 220 221
    case ZMQ_IPV4ONLY:
        {
            if (optvallen_ != sizeof (int)) {
                errno = EINVAL;
                return -1;
            }
            int val = *((int*) optval_);
            if (val != 0 && val != 1) {
                errno = EINVAL;
                return -1;
            }
            ipv4only = val;
            return 0;
        }
222

223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
    case ZMQ_DELAY_ATTACH_ON_CONNECT:
    {
        if (optvallen_ != sizeof (int)) {
            errno = EINVAL;
            return -1;
        }
        int val = *((int*) optval_);
        if (val != 0 && val != 1) {
            errno = EINVAL;
            return -1;
        }
        delay_attach_on_connect = val;
        return 0;
    }

238 239 240 241 242 243 244 245 246 247 248
    case ZMQ_TCP_KEEPALIVE:
        {
            if (optvallen_ != sizeof (int)) {
                errno = EINVAL;
                return -1;
            }
            int val = *((int*) optval_);
            if (val != -1 && val != 0 && val != 1) {
                errno = EINVAL;
                return -1;
            }
249
#if defined ZMQ_HAVE_SO_KEEPALIVE
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
            tcp_keepalive = val;
#endif
            return 0;
        }

    case ZMQ_TCP_KEEPALIVE_CNT:
        {
            if (optvallen_ != sizeof (int)) {
                errno = EINVAL;
                return -1;
            }
            int val = *((int*) optval_);
            if (val <= 0 && val != -1) {
                errno = EINVAL;
                return -1;
            }
266
#if defined ZMQ_HAVE_SO_KEEPALIVE && defined ZMQ_HAVE_TCP_KEEPCNT
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
            tcp_keepalive_cnt = val;
#endif
            return 0;
        }

    case ZMQ_TCP_KEEPALIVE_IDLE:
        {
            if (optvallen_ != sizeof (int)) {
                errno = EINVAL;
                return -1;
            }
            int val = *((int*) optval_);
            if (val <= 0 && val != -1) {
                errno = EINVAL;
                return -1;
            }
283 284

#if defined ZMQ_HAVE_SO_KEEPALIVE && (defined ZMQ_HAVE_TCP_KEEPIDLE || defined ZMQ_HAVE_TCP_KEEPALIVE)
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
            tcp_keepalive_idle = val;
#endif
            return 0;
        }

    case ZMQ_TCP_KEEPALIVE_INTVL:
        {
            if (optvallen_ != sizeof (int)) {
                errno = EINVAL;
                return -1;
            }
            int val = *((int*) optval_);
            if (val <= 0 && val != -1) {
                errno = EINVAL;
                return -1;
            }
301
#if defined ZMQ_HAVE_SO_KEEPALIVE && defined ZMQ_HAVE_TCP_KEEPINTVL
302 303 304 305
            tcp_keepalive_intvl = val;
#endif
            return 0;
        }
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331

    case ZMQ_TCP_ACCEPT_FILTER:
        {
            if (optvallen_ == 0 && optval_ == NULL) {
                tcp_accept_filters.clear ();
                return 0;
            }
            else
            if (optvallen_ < 1 || optvallen_ > 255 || optval_ == NULL || *((const char*) optval_) == 0) {
                errno = EINVAL;
                return -1;
            }
            else {
                std::string filter_str ((const char*) optval_, optvallen_);

                tcp_address_mask_t filter;
                int rc = filter.resolve (filter_str.c_str (), ipv4only ? true : false);
                if (rc != 0) {
                    errno = EINVAL;
                    return -1;
                }
                tcp_accept_filters.push_back(filter);

                return 0;
            }
        }
332
    }
333 334 335
    errno = EINVAL;
    return -1;
}
336 337 338 339 340

int zmq::options_t::getsockopt (int option_, void *optval_, size_t *optvallen_)
{
    switch (option_) {

341
    case ZMQ_SNDHWM:
342
        if (*optvallen_ < sizeof (int)) {
343 344 345
            errno = EINVAL;
            return -1;
        }
346 347 348 349 350 351 352 353 354 355 356
        *((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);
357 358 359 360 361 362 363 364 365 366 367
        return 0;

    case ZMQ_AFFINITY:
        if (*optvallen_ < sizeof (uint64_t)) {
            errno = EINVAL;
            return -1;
        }
        *((uint64_t*) optval_) = affinity;
        *optvallen_ = sizeof (uint64_t);
        return 0;

368 369 370 371 372 373 374 375 376
    case ZMQ_IDENTITY:
        if (*optvallen_ < identity_size) {
            errno = EINVAL;
            return -1;
        }
        memcpy (optval_, identity, identity_size);
        *optvallen_ = identity_size;
        return 0;

377
    case ZMQ_RATE:
378
        if (*optvallen_ < sizeof (int)) {
379 380 381
            errno = EINVAL;
            return -1;
        }
382 383
        *((int*) optval_) = rate;
        *optvallen_ = sizeof (int);
384
        return 0;
385

386
    case ZMQ_RECOVERY_IVL:
387
        if (*optvallen_ < sizeof (int)) {
388 389 390
            errno = EINVAL;
            return -1;
        }
391 392
        *((int*) optval_) = recovery_ivl;
        *optvallen_ = sizeof (int);
393 394 395
        return 0;

    case ZMQ_SNDBUF:
396
        if (*optvallen_ < sizeof (int)) {
397 398 399
            errno = EINVAL;
            return -1;
        }
400 401
        *((int*) optval_) = sndbuf;
        *optvallen_ = sizeof (int);
402 403 404
        return 0;

    case ZMQ_RCVBUF:
405
        if (*optvallen_ < sizeof (int)) {
406 407 408
            errno = EINVAL;
            return -1;
        }
409 410
        *((int*) optval_) = rcvbuf;
        *optvallen_ = sizeof (int);
411
        return 0;
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439

    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;

440 441 442 443 444 445 446 447 448
    case ZMQ_RECONNECT_IVL_MAX:
        if (*optvallen_ < sizeof (int)) {
            errno = EINVAL;
            return -1;
        }
        *((int*) optval_) = reconnect_ivl_max;
        *optvallen_ = sizeof (int);
        return 0;

449 450 451 452 453 454 455 456 457
    case ZMQ_BACKLOG:
        if (*optvallen_ < sizeof (int)) {
            errno = EINVAL;
            return -1;
        }
        *((int*) optval_) = backlog;
        *optvallen_ = sizeof (int);
        return 0;

458 459 460 461 462 463 464 465 466
    case ZMQ_MAXMSGSIZE:
        if (*optvallen_ < sizeof (int64_t)) {
            errno = EINVAL;
            return -1;
        }
        *((int64_t*) optval_) = maxmsgsize;
        *optvallen_ = sizeof (int64_t);
        return 0;

467 468 469 470 471 472 473 474 475
    case ZMQ_MULTICAST_HOPS:
        if (*optvallen_ < sizeof (int)) {
            errno = EINVAL;
            return -1;
        }
        *((int*) optval_) = multicast_hops;
        *optvallen_ = sizeof (int);
        return 0;

476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493
    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;

Steven McCoy's avatar
Steven McCoy committed
494 495 496 497 498 499 500 501
    case ZMQ_IPV4ONLY:
        if (*optvallen_ < sizeof (int)) {
            errno = EINVAL;
            return -1;
        }
        *((int*) optval_) = ipv4only;
        *optvallen_ = sizeof (int);
        return 0;
502

503 504 505 506 507 508 509 510 511
    case ZMQ_DELAY_ATTACH_ON_CONNECT:
        if (*optvallen_ < sizeof (int)) {
            errno = EINVAL;
            return -1;
        }
        *((int*) optval_) = delay_attach_on_connect;
        *optvallen_ = sizeof (int);
        return 0;

512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547
    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;

548
    case ZMQ_LAST_ENDPOINT:
549 550
        // don't allow string which cannot contain the entire message
        if (*optvallen_ < last_endpoint.size() + 1) {
551 552 553
            errno = EINVAL;
            return -1;
        }
554 555
        memcpy (optval_, last_endpoint.c_str(), last_endpoint.size()+1);
        *optvallen_ = last_endpoint.size()+1;
556
        return 0;
557 558 559 560 561
    }

    errno = EINVAL;
    return -1;
}