From 3615bbc1531ac876e0a237a57ecef1987b6e6a54 Mon Sep 17 00:00:00 2001
From: Steven McCoy <steven.mccoy@miru.hk>
Date: Sun, 17 Jul 2011 14:13:05 -0400
Subject: [PATCH] Prepare for IPv4/IPv4 preference socket options.


Signed-off-by: Steven McCoy <steven.mccoy@miru.hk>
---
 include/zmq.h   |    2 ++
 src/options.cpp |   38 +++++++++++++++++++++++++++++++++++++-
 src/options.hpp |   18 +++++++++++++++---
 3 files changed, 54 insertions(+), 4 deletions(-)

diff --git a/include/zmq.h b/include/zmq.h
index 73cefce..9af0598 100644
--- a/include/zmq.h
+++ b/include/zmq.h
@@ -186,6 +186,8 @@ ZMQ_EXPORT int zmq_term (void *context);
 #define ZMQ_SNDTIMEO 28
 #define ZMQ_RCVLABEL 29
 #define ZMQ_IPV6ONLY 30
+#define ZMQ_PREFER_IPV4_STACK 31
+#define ZMQ_PREFER_IPV6_ADDRESSES 32
 
 /*  Send/recv options.                                                        */
 #define ZMQ_DONTWAIT 1
diff --git a/src/options.cpp b/src/options.cpp
index 5c325d9..c90d0f1 100644
--- a/src/options.cpp
+++ b/src/options.cpp
@@ -44,7 +44,9 @@ zmq::options_t::options_t () :
     delay_on_close (true),
     delay_on_disconnect (true),
     filter (false),
-    ipv6_only (false)
+    ipv6_only (false),
+    prefer_ipv4_stack (false),
+    prefer_ipv6_addresses (false)
 {
 }
 
@@ -202,6 +204,22 @@ int zmq::options_t::setsockopt (int option_, const void *optval_,
         ipv6_only = *((int*) optval_);
         return 0;
 
+    case ZMQ_PREFER_IPV4_STACK:
+        if (optvallen_ != sizeof (int)) {
+            errno = EINVAL;
+            return -1;
+        }
+        prefer_ipv4_stack = *((int*) optval_);
+        return 0;
+
+    case ZMQ_PREFER_IPV6_ADDRESSES:
+        if (optvallen_ != sizeof (int)) {
+            errno = EINVAL;
+            return -1;
+        }
+        prefer_ipv6_addresses = *((int*) optval_);
+        return 0;
+
     }
 
     errno = EINVAL;
@@ -374,6 +392,24 @@ int zmq::options_t::getsockopt (int option_, void *optval_, size_t *optvallen_)
         *optvallen_ = sizeof (int);
         return 0;
 
+    case ZMQ_PREFER_IPV4_STACK:
+        if (*optvallen_ < sizeof (int)) {
+            errno = EINVAL;
+            return -1;
+        }
+        *((int*) optval_) = prefer_ipv4_stack;
+        *optvallen_ = sizeof (int);
+        return 0;
+
+    case ZMQ_PREFER_IPV6_ADDRESSES:
+        if (*optvallen_ < sizeof (int)) {
+            errno = EINVAL;
+            return -1;
+        }
+        *((int*) optval_) = prefer_ipv6_addresses;
+        *optvallen_ = sizeof (int);
+        return 0;
+
     }
 
     errno = EINVAL;
diff --git a/src/options.hpp b/src/options.hpp
index 0d42241..8be0222 100644
--- a/src/options.hpp
+++ b/src/options.hpp
@@ -93,12 +93,24 @@ namespace zmq
         //  them to the user when the peer terminates.
         bool delay_on_disconnect;
 
-        //  If 1, (X)SUB socket should filter the messages. If 0, it should not.
+        //  If true, (X)SUB socket should filter the messages. If false, it
+        //  should not.
         bool filter;
 
-        //  If 1, indicates a socket created for the AF_INET6 family is 
-        //  restricted to IPv6 communications only. If 0, it should not.
+        //  If true, indicates a socket created for the AF_INET6 family is 
+        //  restricted to IPv6 communications only. If false, it should not.
         bool ipv6_only;
+
+        //  If true, set a preference to use only IPv4 sockets.  The
+        //  application will not be able to communicate with IPv6 hosts. If
+        //  false, underlying native socket will be an IPv6 socket and 
+        //  applications can connect to and accept connections from both IPv4
+        //  and IPv6 hosts.
+        bool prefer_ipv4_stack;
+
+        //  If true, prefer IPv6 addresses over IPv4 addresses.  If false,
+        //  IPv4 mapped addresses may be used.
+        bool prefer_ipv6_addresses;
     };
 
 }
-- 
1.7.5.1

