Commit b01a8e17 authored by Martin Sustrik's avatar Martin Sustrik

IPC address related functionality refactored into ipc_address_t class

Signed-off-by: 's avatarMartin Sustrik <sustrik@250bpm.com>
parent 3488af04
...@@ -24,6 +24,7 @@ libzmq_la_SOURCES = \ ...@@ -24,6 +24,7 @@ libzmq_la_SOURCES = \
io_object.hpp \ io_object.hpp \
io_thread.hpp \ io_thread.hpp \
ip.hpp \ ip.hpp \
ipc_address.hpp \
ipc_connecter.hpp \ ipc_connecter.hpp \
ipc_listener.hpp \ ipc_listener.hpp \
i_engine.hpp \ i_engine.hpp \
...@@ -88,6 +89,7 @@ libzmq_la_SOURCES = \ ...@@ -88,6 +89,7 @@ libzmq_la_SOURCES = \
io_object.cpp \ io_object.cpp \
io_thread.cpp \ io_thread.cpp \
ip.cpp \ ip.cpp \
ipc_address.cpp \
ipc_connecter.cpp \ ipc_connecter.cpp \
ipc_listener.cpp \ ipc_listener.cpp \
kqueue.cpp \ kqueue.cpp \
......
...@@ -28,10 +28,6 @@ ...@@ -28,10 +28,6 @@
#include "platform.hpp" #include "platform.hpp"
#include "stdint.hpp" #include "stdint.hpp"
#if !defined ZMQ_HAVE_WINDOWS && !defined ZMQ_HAVE_OPENVMS
#include <sys/un.h>
#endif
#if !defined ZMQ_HAVE_WINDOWS #if !defined ZMQ_HAVE_WINDOWS
#include <fcntl.h> #include <fcntl.h>
#endif #endif
...@@ -396,26 +392,6 @@ int zmq::resolve_ip_hostname (sockaddr_storage *addr_, socklen_t *addr_len_, ...@@ -396,26 +392,6 @@ int zmq::resolve_ip_hostname (sockaddr_storage *addr_, socklen_t *addr_len_,
return 0; 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_) void zmq::tune_tcp_socket (fd_t s_)
{ {
// Disable Nagle's algorithm. We are doing data batching on 0MQ level, // Disable Nagle's algorithm. We are doing data batching on 0MQ level,
......
...@@ -58,10 +58,6 @@ namespace zmq ...@@ -58,10 +58,6 @@ namespace zmq
int resolve_ip_hostname (sockaddr_storage *addr_, socklen_t *addr_len_, int resolve_ip_hostname (sockaddr_storage *addr_, socklen_t *addr_len_,
const char *hostname_, bool ipv4only_); 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. // Tunes the supplied TCP socket for the best latency.
void tune_tcp_socket (fd_t s_); void tune_tcp_socket (fd_t s_);
......
/*
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
/*
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
...@@ -48,9 +48,6 @@ zmq::ipc_connecter_t::ipc_connecter_t (class io_thread_t *io_thread_, ...@@ -48,9 +48,6 @@ zmq::ipc_connecter_t::ipc_connecter_t (class io_thread_t *io_thread_,
session (session_), session (session_),
current_reconnect_ivl(options.reconnect_ivl) 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 // TODO: set_addess should be called separately, so that the error
// can be propagated. // can be propagated.
int rc = set_address (address_); int rc = set_address (address_);
...@@ -169,16 +166,14 @@ int zmq::ipc_connecter_t::get_new_reconnect_ivl () ...@@ -169,16 +166,14 @@ int zmq::ipc_connecter_t::get_new_reconnect_ivl ()
int zmq::ipc_connecter_t::set_address (const char *addr_) 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 () int zmq::ipc_connecter_t::open ()
{ {
zmq_assert (s == retired_fd); zmq_assert (s == retired_fd);
struct sockaddr *sa = (struct sockaddr*) &addr;
// Create the socket. // Create the socket.
zmq_assert (AF_UNIX == sa->sa_family);
s = socket (AF_UNIX, SOCK_STREAM, 0); s = socket (AF_UNIX, SOCK_STREAM, 0);
if (s == -1) if (s == -1)
return -1; return -1;
...@@ -187,7 +182,7 @@ int zmq::ipc_connecter_t::open () ...@@ -187,7 +182,7 @@ int zmq::ipc_connecter_t::open ()
unblock_socket (s); unblock_socket (s);
// Connect to the remote peer. // 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. // Connect was successfull immediately.
if (rc == 0) if (rc == 0)
......
...@@ -26,10 +26,10 @@ ...@@ -26,10 +26,10 @@
#if !defined ZMQ_HAVE_WINDOWS && !defined ZMQ_HAVE_OPENVMS #if !defined ZMQ_HAVE_WINDOWS && !defined ZMQ_HAVE_OPENVMS
#include "fd.hpp" #include "fd.hpp"
#include "ip.hpp"
#include "own.hpp" #include "own.hpp"
#include "io_object.hpp"
#include "stdint.hpp" #include "stdint.hpp"
#include "io_object.hpp"
#include "ipc_address.hpp"
namespace zmq namespace zmq
{ {
...@@ -85,8 +85,7 @@ namespace zmq ...@@ -85,8 +85,7 @@ namespace zmq
fd_t connect (); fd_t connect ();
// Address to connect to. // Address to connect to.
sockaddr_storage addr; ipc_address_t address;
socklen_t addr_len;
// Underlying socket. // Underlying socket.
fd_t s; fd_t s;
......
...@@ -27,6 +27,7 @@ ...@@ -27,6 +27,7 @@
#include <string.h> #include <string.h>
#include "stream_engine.hpp" #include "stream_engine.hpp"
#include "ipc_address.hpp"
#include "io_thread.hpp" #include "io_thread.hpp"
#include "session.hpp" #include "session.hpp"
#include "config.hpp" #include "config.hpp"
...@@ -45,8 +46,6 @@ zmq::ipc_listener_t::ipc_listener_t (io_thread_t *io_thread_, ...@@ -45,8 +46,6 @@ zmq::ipc_listener_t::ipc_listener_t (io_thread_t *io_thread_,
s (retired_fd), s (retired_fd),
socket (socket_) socket (socket_)
{ {
memset (&addr, 0, sizeof (addr));
addr_len = 0;
} }
zmq::ipc_listener_t::~ipc_listener_t () zmq::ipc_listener_t::~ipc_listener_t ()
...@@ -100,9 +99,11 @@ int zmq::ipc_listener_t::set_address (const char *addr_) ...@@ -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 // Get rid of the file associated with the UNIX domain socket that
// may have been left behind by the previous run of the application. // may have been left behind by the previous run of the application.
::unlink (addr_); ::unlink (addr_);
filename.clear ();
// Convert the address into sockaddr_un structure. // Initialise the address structure.
int rc = resolve_local_path (&addr, &addr_len, addr_); ipc_address_t address;
int rc = address.resolve (addr_);
if (rc != 0) if (rc != 0)
return -1; return -1;
...@@ -112,7 +113,7 @@ int zmq::ipc_listener_t::set_address (const char *addr_) ...@@ -112,7 +113,7 @@ int zmq::ipc_listener_t::set_address (const char *addr_)
return -1; return -1;
// Bind the socket to the file path. // 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) if (rc != 0)
return -1; return -1;
...@@ -136,9 +137,8 @@ int zmq::ipc_listener_t::close () ...@@ -136,9 +137,8 @@ int zmq::ipc_listener_t::close ()
// If there's an underlying UNIX domain socket, get rid of the file it // If there's an underlying UNIX domain socket, get rid of the file it
// is associated with. // is associated with.
struct sockaddr_un *su = (struct sockaddr_un*) &addr; if (has_file && !filename.empty ()) {
if (AF_UNIX == su->sun_family && has_file) { rc = ::unlink(filename.c_str ());
rc = ::unlink(su->sun_path);
if (rc != 0) if (rc != 0)
return -1; return -1;
} }
......
...@@ -25,11 +25,12 @@ ...@@ -25,11 +25,12 @@
#if !defined ZMQ_HAVE_WINDOWS && !defined ZMQ_HAVE_OPENVMS #if !defined ZMQ_HAVE_WINDOWS && !defined ZMQ_HAVE_OPENVMS
#include <string>
#include "fd.hpp" #include "fd.hpp"
#include "ip.hpp"
#include "own.hpp" #include "own.hpp"
#include "io_object.hpp"
#include "stdint.hpp" #include "stdint.hpp"
#include "io_object.hpp"
namespace zmq namespace zmq
{ {
...@@ -62,13 +63,12 @@ namespace zmq ...@@ -62,13 +63,12 @@ namespace zmq
// if the connection was dropped while waiting in the listen backlog. // if the connection was dropped while waiting in the listen backlog.
fd_t accept (); fd_t accept ();
// Address to listen on.
sockaddr_storage addr;
socklen_t addr_len;
// True, if the undelying file for UNIX domain socket exists. // True, if the undelying file for UNIX domain socket exists.
bool has_file; bool has_file;
// Name of the file associated with the UNIX domain address.
std::string filename;
// Underlying socket. // Underlying socket.
fd_t s; fd_t s;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment