From 8ab3c4a1bfbde44f45bd97eb40cbd27236239d10 Mon Sep 17 00:00:00 2001 From: Pieter Hintjens Date: Thu, 31 Jan 2013 21:52:30 +0100 Subject: [PATCH] Fixed issue #500 --- .gitignore | 3 ++ doc/zmq_ctx_get.txt | 6 +++- doc/zmq_ctx_set.txt | 11 ++++++++ src/ctx.cpp | 20 ++++++++++---- src/ctx.hpp | 3 ++ src/options.hpp | 1 - src/socket_base.cpp | 1 + tests/Makefile.am | 6 ++-- tests/test_ctx_options.cpp | 56 ++++++++++++++++++++++++++++++++++++++ 9 files changed, 97 insertions(+), 10 deletions(-) create mode 100644 tests/test_ctx_options.cpp diff --git a/.gitignore b/.gitignore index 75f34cf5..0a46750b 100644 --- a/.gitignore +++ b/.gitignore @@ -45,6 +45,7 @@ tests/test_router_mandatory tests/test_disconnect_inproc tests/test_raw_sock tests/test_disconnect_inproc +tests/test_ctx_options src/platform.hpp* src/stamp-h1 perf/local_lat @@ -73,3 +74,5 @@ foreign/openpgm/* !foreign/openpgm/Makefile.am zeromq-*.tar.gz zeromq-*.zip +core + diff --git a/doc/zmq_ctx_get.txt b/doc/zmq_ctx_get.txt index a6b3614b..ef4406ef 100644 --- a/doc/zmq_ctx_get.txt +++ b/doc/zmq_ctx_get.txt @@ -26,11 +26,15 @@ ZMQ_IO_THREADS: Get number of I/O threads The 'ZMQ_IO_THREADS' argument returns the size of the 0MQ thread pool for this context. -ZMQ_MAX_SOCKETS: Set maximum number of sockets +ZMQ_MAX_SOCKETS: Get maximum number of sockets ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ The 'ZMQ_MAX_SOCKETS' argument returns the maximum number of sockets allowed for this context. +ZMQ_IPV6: Set IPv6 option +~~~~~~~~~~~~~~~~~~~~~~~~~ +The 'ZMQ_IPV6' argument returns the IPv6 option for the context. + RETURN VALUE ------------ diff --git a/doc/zmq_ctx_set.txt b/doc/zmq_ctx_set.txt index 88fcc1e0..0d5e2872 100644 --- a/doc/zmq_ctx_set.txt +++ b/doc/zmq_ctx_set.txt @@ -40,6 +40,17 @@ on the context. [horizontal] Default value:: 1024 +ZMQ_IPV6: Set IPv6 option +~~~~~~~~~~~~~~~~~~~~~~~~~ +The 'ZMQ_IPV6' argument sets the IPv6 value for all sockets created in +the context from this point onwards. A value of `1` means IPv6 is +enabled, while `0` means the socket will use only IPv4. When IPv6 is +enabled, a socket will connect to, or accept connections from, both +IPv4 and IPv6 hosts. + +[horizontal] +Default value:: 0 + RETURN VALUE ------------ diff --git a/src/ctx.cpp b/src/ctx.cpp index 552ffec6..8726e8ef 100644 --- a/src/ctx.cpp +++ b/src/ctx.cpp @@ -45,7 +45,8 @@ zmq::ctx_t::ctx_t () : slot_count (0), slots (NULL), max_sockets (ZMQ_MAX_SOCKETS_DFLT), - io_thread_count (ZMQ_IO_THREADS_DFLT) + io_thread_count (ZMQ_IO_THREADS_DFLT), + ipv6 (false) { } @@ -139,6 +140,12 @@ int zmq::ctx_t::set (int option_, int optval_) io_thread_count = optval_; opt_sync.unlock (); } + else + if (option_ == ZMQ_IPV6 && optval_ >= 0) { + opt_sync.lock (); + ipv6 = optval_; + opt_sync.unlock (); + } else { errno = EINVAL; rc = -1; @@ -154,6 +161,9 @@ int zmq::ctx_t::get (int option_) else if (option_ == ZMQ_IO_THREADS) rc = io_thread_count; + else + if (option_ == ZMQ_IPV6) + rc = ipv6; else { errno = EINVAL; rc = -1; @@ -168,7 +178,7 @@ zmq::socket_base_t *zmq::ctx_t::create_socket (int type_) starting = false; // Initialise the array of mailboxes. Additional three slots are for - // zmq_term thread and reaper thread. + // zmq_ctx_term thread and reaper thread. opt_sync.lock (); int mazmq = max_sockets; int ios = io_thread_count; @@ -177,7 +187,7 @@ zmq::socket_base_t *zmq::ctx_t::create_socket (int type_) slots = (mailbox_t**) malloc (sizeof (mailbox_t*) * slot_count); alloc_assert (slots); - // Initialise the infrastructure for zmq_term thread. + // Initialise the infrastructure for zmq_ctx_term thread. slots [term_tid] = &term_mailbox; // Create the reaper thread. @@ -203,7 +213,7 @@ zmq::socket_base_t *zmq::ctx_t::create_socket (int type_) } } - // Once zmq_term() was called, we can't create new sockets. + // Once zmq_ctx_term() was called, we can't create new sockets. if (terminating) { slot_sync.unlock (); errno = ETERM; @@ -250,7 +260,7 @@ void zmq::ctx_t::destroy_socket (class socket_base_t *socket_) // Remove the socket from the list of sockets. sockets.erase (socket_); - // If zmq_term() was already called and there are no more socket + // If zmq_ctx_term() was already called and there are no more socket // we can ask reaper thread to terminate. if (terminating && sockets.empty ()) reaper->stop (); diff --git a/src/ctx.hpp b/src/ctx.hpp index 43e3237e..5eb9b51f 100644 --- a/src/ctx.hpp +++ b/src/ctx.hpp @@ -161,6 +161,9 @@ namespace zmq // Number of I/O threads to launch. int io_thread_count; + // Is IPv6 enabled on this context? + bool ipv6; + // Synchronisation of access to context options. mutex_t opt_sync; diff --git a/src/options.hpp b/src/options.hpp index 45d95191..ee77b579 100644 --- a/src/options.hpp +++ b/src/options.hpp @@ -33,7 +33,6 @@ namespace zmq { - struct options_t { options_t (); diff --git a/src/socket_base.cpp b/src/socket_base.cpp index 30339d22..b27a83a6 100644 --- a/src/socket_base.cpp +++ b/src/socket_base.cpp @@ -135,6 +135,7 @@ zmq::socket_base_t::socket_base_t (ctx_t *parent_, uint32_t tid_, int sid_) : monitor_events (0) { options.socket_id = sid_; + options.ipv6 = parent_->get (ZMQ_IPV6); } zmq::socket_base_t::~socket_base_t () diff --git a/tests/Makefile.am b/tests/Makefile.am index 72fe279e..90c910f0 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -19,8 +19,8 @@ noinst_PROGRAMS = test_pair_inproc \ test_monitor \ test_router_mandatory \ test_raw_sock \ - test_disconnect_inproc - + test_disconnect_inproc \ + test_ctx_options if !ON_MINGW noinst_PROGRAMS += test_shutdown_stress \ @@ -46,7 +46,7 @@ test_monitor_SOURCES = test_monitor.cpp test_router_mandatory_SOURCES = test_router_mandatory.cpp test_raw_sock_SOURCES = test_raw_sock.cpp test_disconnect_inproc_SOURCES = test_disconnect_inproc.cpp - +test_ctx_set_SOURCES = test_ctx_options.cpp if !ON_MINGW test_shutdown_stress_SOURCES = test_shutdown_stress.cpp test_pair_ipc_SOURCES = test_pair_ipc.cpp testutil.hpp diff --git a/tests/test_ctx_options.cpp b/tests/test_ctx_options.cpp new file mode 100644 index 00000000..43e19498 --- /dev/null +++ b/tests/test_ctx_options.cpp @@ -0,0 +1,56 @@ +/* + Copyright (c) 2007-2013 iMatix Corporation + Copyright (c) 2007-2012 Other contributors as noted in the AUTHORS file + + 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 + 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. + + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . +*/ + +#include "../include/zmq.h" +#include +#include +#undef NDEBUG +#include + +int main (void) +{ + int rc; + + // Set up our context and sockets + void *ctx = zmq_ctx_new (); + assert (ctx); + + assert (zmq_ctx_get (ctx, ZMQ_MAX_SOCKETS) == ZMQ_MAX_SOCKETS_DFLT); + assert (zmq_ctx_get (ctx, ZMQ_IO_THREADS) == ZMQ_IO_THREADS_DFLT); + assert (zmq_ctx_get (ctx, ZMQ_IPV6) == 0); + + rc = zmq_ctx_set (ctx, ZMQ_IPV6, true); + assert (zmq_ctx_get (ctx, ZMQ_IPV6) == true); + + void *router = zmq_socket (ctx, ZMQ_ROUTER); + int ipv6; + size_t optsize = sizeof (int); + rc = zmq_getsockopt (router, ZMQ_IPV6, &ipv6, &optsize); + assert (rc == 0); + assert (ipv6); + + rc = zmq_close (router); + assert (rc == 0); + + rc = zmq_ctx_term (ctx); + assert (rc == 0); + + return 0; +}