[PATCH 1/2] IPC address related functionality refactored into ipc_address_t class

Martin Sustrik sustrik at 250bpm.com
Thu Aug 18 11:08:22 CEST 2011


Signed-off-by: Martin Sustrik <sustrik at 250bpm.com>
---
 src/Makefile.am       |    2 +
 src/ip.cpp            |   24 -------------------
 src/ip.hpp            |    4 ---
 src/ipc_address.cpp   |   60 ++++++++++++++++++++++++++++++++++++++++++++++++
 src/ipc_address.hpp   |   61 +++++++++++++++++++++++++++++++++++++++++++++++++
 src/ipc_connecter.cpp |    9 +-----
 src/ipc_connecter.hpp |    7 ++---
 src/ipc_listener.cpp  |   16 ++++++------
 src/ipc_listener.hpp  |   12 +++++-----
 9 files changed, 142 insertions(+), 53 deletions(-)
 create mode 100644 src/ipc_address.cpp
 create mode 100644 src/ipc_address.hpp

diff --git a/src/Makefile.am b/src/Makefile.am
index 93c51ed..db6bb5e 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -24,6 +24,7 @@ libzmq_la_SOURCES = \
     io_object.hpp \
     io_thread.hpp \
     ip.hpp \
+    ipc_address.hpp \
     ipc_connecter.hpp \
     ipc_listener.hpp \
     i_engine.hpp \
@@ -88,6 +89,7 @@ libzmq_la_SOURCES = \
     io_object.cpp \
     io_thread.cpp \
     ip.cpp \
+    ipc_address.cpp \
     ipc_connecter.cpp \
     ipc_listener.cpp \
     kqueue.cpp \
diff --git a/src/ip.cpp b/src/ip.cpp
index 953640d..e03ba04 100644
--- a/src/ip.cpp
+++ b/src/ip.cpp
@@ -28,10 +28,6 @@
 #include "platform.hpp"
 #include "stdint.hpp"
 
-#if !defined ZMQ_HAVE_WINDOWS && !defined ZMQ_HAVE_OPENVMS
-#include <sys/un.h>
-#endif
-
 #if !defined ZMQ_HAVE_WINDOWS
 #include <fcntl.h>
 #endif
@@ -396,26 +392,6 @@ int zmq::resolve_ip_hostname (sockaddr_storage *addr_, socklen_t *addr_len_,
     return 0;
 }
 
-int zmq::resolve_local_path (sockaddr_storage *addr_, socklen_t *addr_len_,
-    const char *path_)
-{
-#if defined ZMQ_HAVE_WINDOWS || defined ZMQ_HAVE_OPENVMS
-    errno = EPROTONOSUPPORT;
-    return -1;
-#else
-    sockaddr_un *un = (sockaddr_un*) addr_;
-    if (strlen (path_) >= sizeof (un->sun_path))
-    {
-        errno = ENAMETOOLONG;
-        return -1;
-    }
-    strcpy (un->sun_path, path_);
-    un->sun_family = AF_UNIX;
-    *addr_len_ = sizeof (sockaddr_un);
-    return 0;
-#endif
-}
-
 void zmq::tune_tcp_socket (fd_t s_)
 {
     //  Disable Nagle's algorithm. We are doing data batching on 0MQ level,
diff --git a/src/ip.hpp b/src/ip.hpp
index a8b6eb1..cbff0c2 100644
--- a/src/ip.hpp
+++ b/src/ip.hpp
@@ -58,10 +58,6 @@ namespace zmq
     int resolve_ip_hostname (sockaddr_storage *addr_, socklen_t *addr_len_,
         const char *hostname_, bool ipv4only_);
 
-    //  This function sets up address for UNIX domain transport.
-    int resolve_local_path (sockaddr_storage *addr_, socklen_t *addr_len_,
-        const char* pathname_);
-
     //  Tunes the supplied TCP socket for the best latency.
     void tune_tcp_socket (fd_t s_);
 
diff --git a/src/ipc_address.cpp b/src/ipc_address.cpp
new file mode 100644
index 0000000..f7252c5
--- /dev/null
+++ b/src/ipc_address.cpp
@@ -0,0 +1,60 @@
+/*
+    Copyright (c) 2007-2011 iMatix Corporation
+    Copyright (c) 2007-2011 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 <http://www.gnu.org/licenses/>.
+*/
+
+#include "ipc_address.hpp"
+
+#if !defined ZMQ_HAVE_WINDOWS || !defined ZMQ_HAVE_OPENVMS
+
+#include "err.hpp"
+
+#include <string.h>
+
+zmq::ipc_address_t::ipc_address_t ()
+{
+    memset (&address, 0, sizeof (address));
+}
+
+zmq::ipc_address_t::~ipc_address_t ()
+{
+}
+
+int zmq::ipc_address_t::resolve (const char *path_)
+{
+    if (strlen (path_) >= sizeof (address.sun_path)) {
+        errno = ENAMETOOLONG;
+        return -1;
+    }
+
+    address.sun_family = AF_UNIX;
+    strcpy (address.sun_path, path_);
+    return 0;
+}
+
+sockaddr *zmq::ipc_address_t::addr ()
+{
+    return (sockaddr*) &address;
+}
+
+socklen_t zmq::ipc_address_t::addrlen ()
+{
+    return (socklen_t) sizeof (address);
+}
+
+#endif
diff --git a/src/ipc_address.hpp b/src/ipc_address.hpp
new file mode 100644
index 0000000..453f5fd
--- /dev/null
+++ b/src/ipc_address.hpp
@@ -0,0 +1,61 @@
+/*
+    Copyright (c) 2007-2011 iMatix Corporation
+    Copyright (c) 2007-2011 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 <http://www.gnu.org/licenses/>.
+*/
+
+#ifndef __ZMQ_IPC_ADDRESS_HPP_INCLUDED__
+#define __ZMQ_IPC_ADDRESS_HPP_INCLUDED__
+
+#include "platform.hpp"
+
+#if !defined ZMQ_HAVE_WINDOWS && !defined ZMQ_HAVE_OPENVMS
+
+#include <sys/socket.h>
+#include <sys/un.h>
+
+namespace zmq
+{
+
+    class ipc_address_t
+    {
+    public:
+
+        ipc_address_t ();
+        ~ipc_address_t ();
+
+        //  This function sets up the address for UNIX domain transport.
+        int resolve (const char* path_);
+
+        sockaddr *addr ();
+        socklen_t addrlen ();
+
+    private:
+
+        struct sockaddr_un address;
+
+        ipc_address_t (const ipc_address_t&);
+        const ipc_address_t &operator = (const ipc_address_t&);
+    };
+    
+}
+
+#endif
+
+#endif
+
+
diff --git a/src/ipc_connecter.cpp b/src/ipc_connecter.cpp
index 2862db4..54def6e 100644
--- a/src/ipc_connecter.cpp
+++ b/src/ipc_connecter.cpp
@@ -48,9 +48,6 @@ zmq::ipc_connecter_t::ipc_connecter_t (class io_thread_t *io_thread_,
     session (session_),
     current_reconnect_ivl(options.reconnect_ivl)
 {
-    memset (&addr, 0, sizeof (addr));
-    addr_len = 0;
-
     //  TODO: set_addess should be called separately, so that the error
     //  can be propagated.
     int rc = set_address (address_);
@@ -169,16 +166,14 @@ int zmq::ipc_connecter_t::get_new_reconnect_ivl ()
 
 int zmq::ipc_connecter_t::set_address (const char *addr_)
 {
-    return resolve_local_path (&addr, &addr_len, addr_);
+    return address.resolve (addr_);
 }
 
 int zmq::ipc_connecter_t::open ()
 {
     zmq_assert (s == retired_fd);
-    struct sockaddr *sa = (struct sockaddr*) &addr;
 
     //  Create the socket.
-    zmq_assert (AF_UNIX == sa->sa_family);
     s = socket (AF_UNIX, SOCK_STREAM, 0);
     if (s == -1)
         return -1;
@@ -187,7 +182,7 @@ int zmq::ipc_connecter_t::open ()
     unblock_socket (s);
 
     //  Connect to the remote peer.
-    int rc = ::connect (s, (struct sockaddr*) &addr, sizeof (sockaddr_un));
+    int rc = ::connect (s, address.addr (), address.addrlen ());
 
     //  Connect was successfull immediately.
     if (rc == 0)
diff --git a/src/ipc_connecter.hpp b/src/ipc_connecter.hpp
index ee57c12..0bb9d69 100644
--- a/src/ipc_connecter.hpp
+++ b/src/ipc_connecter.hpp
@@ -26,10 +26,10 @@
 #if !defined ZMQ_HAVE_WINDOWS && !defined ZMQ_HAVE_OPENVMS
 
 #include "fd.hpp"
-#include "ip.hpp"
 #include "own.hpp"
-#include "io_object.hpp"
 #include "stdint.hpp"
+#include "io_object.hpp"
+#include "ipc_address.hpp"
 
 namespace zmq
 {
@@ -85,8 +85,7 @@ namespace zmq
         fd_t connect ();
 
         //  Address to connect to.
-        sockaddr_storage addr;
-        socklen_t addr_len;
+        ipc_address_t address;
 
         //  Underlying socket.
         fd_t s;
diff --git a/src/ipc_listener.cpp b/src/ipc_listener.cpp
index 9e35af3..1457349 100644
--- a/src/ipc_listener.cpp
+++ b/src/ipc_listener.cpp
@@ -27,6 +27,7 @@
 #include <string.h>
 
 #include "stream_engine.hpp"
+#include "ipc_address.hpp"
 #include "io_thread.hpp"
 #include "session.hpp"
 #include "config.hpp"
@@ -45,8 +46,6 @@ zmq::ipc_listener_t::ipc_listener_t (io_thread_t *io_thread_,
     s (retired_fd),
     socket (socket_)
 {
-    memset (&addr, 0, sizeof (addr));
-    addr_len = 0;
 }
 
 zmq::ipc_listener_t::~ipc_listener_t ()
@@ -100,9 +99,11 @@ int zmq::ipc_listener_t::set_address (const char *addr_)
     //  Get rid of the file associated with the UNIX domain socket that
     //  may have been left behind by the previous run of the application.
     ::unlink (addr_);
+    filename.clear ();
 
-    //  Convert the address into sockaddr_un structure.
-    int rc = resolve_local_path (&addr, &addr_len, addr_);
+    //  Initialise the address structure.
+    ipc_address_t address;
+    int rc = address.resolve (addr_);
     if (rc != 0)
         return -1;
 
@@ -112,7 +113,7 @@ int zmq::ipc_listener_t::set_address (const char *addr_)
         return -1;
 
     //  Bind the socket to the file path.
-    rc = bind (s, (struct sockaddr*) &addr, addr_len);
+    rc = bind (s, address.addr (), address.addrlen ());
     if (rc != 0)
         return -1;
 
@@ -136,9 +137,8 @@ int zmq::ipc_listener_t::close ()
 
     //  If there's an underlying UNIX domain socket, get rid of the file it
     //  is associated with.
-    struct sockaddr_un *su = (struct sockaddr_un*) &addr;
-    if (AF_UNIX == su->sun_family && has_file) {
-        rc = ::unlink(su->sun_path);
+    if (has_file && !filename.empty ()) {
+        rc = ::unlink(filename.c_str ());
         if (rc != 0)
             return -1;
     }
diff --git a/src/ipc_listener.hpp b/src/ipc_listener.hpp
index ce1e20d..4cd881b 100644
--- a/src/ipc_listener.hpp
+++ b/src/ipc_listener.hpp
@@ -25,11 +25,12 @@
 
 #if !defined ZMQ_HAVE_WINDOWS && !defined ZMQ_HAVE_OPENVMS
 
+#include <string>
+
 #include "fd.hpp"
-#include "ip.hpp"
 #include "own.hpp"
-#include "io_object.hpp"
 #include "stdint.hpp"
+#include "io_object.hpp"
 
 namespace zmq
 {
@@ -62,13 +63,12 @@ namespace zmq
         //  if the connection was dropped while waiting in the listen backlog.
         fd_t accept ();
 
-        //  Address to listen on.
-        sockaddr_storage addr;
-        socklen_t addr_len;
-
         //  True, if the undelying file for UNIX domain socket exists.
         bool has_file;
 
+        //  Name of the file associated with the UNIX domain address.
+        std::string filename;
+
         //  Underlying socket.
         fd_t s;
 
-- 
1.7.0.4


--------------090404000807070504080503
Content-Type: text/x-patch;
 name="0002-MSVC-build-fixed.patch"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
 filename="0002-MSVC-build-fixed.patch"



More information about the zeromq-dev mailing list