summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/Makefile.am2
-rw-r--r--src/ip.cpp369
-rw-r--r--src/ip.hpp32
-rw-r--r--src/tcp_address.cpp422
-rw-r--r--src/tcp_address.hpp68
-rw-r--r--src/tcp_connecter.cpp12
-rw-r--r--src/tcp_connecter.hpp7
-rw-r--r--src/tcp_listener.cpp19
-rw-r--r--src/tcp_listener.hpp7
-rw-r--r--src/vtcp_connecter.cpp10
-rw-r--r--src/vtcp_connecter.hpp7
11 files changed, 522 insertions, 433 deletions
diff --git a/src/Makefile.am b/src/Makefile.am
index db6bb5e..7992ab8 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -63,6 +63,7 @@ libzmq_la_SOURCES = \
stdint.hpp \
stream_engine.hpp \
sub.hpp \
+ tcp_address.hpp \
tcp_connecter.hpp \
tcp_listener.hpp \
thread.hpp \
@@ -121,6 +122,7 @@ libzmq_la_SOURCES = \
socket_base.cpp \
stream_engine.cpp \
sub.cpp \
+ tcp_address.cpp \
tcp_connecter.cpp \
tcp_listener.cpp \
thread.cpp \
diff --git a/src/ip.cpp b/src/ip.cpp
index e03ba04..5d81f26 100644
--- a/src/ip.cpp
+++ b/src/ip.cpp
@@ -18,380 +18,23 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-#include <stdlib.h>
-#include <string.h>
-#include <stdlib.h>
-#include <string>
-
#include "ip.hpp"
#include "err.hpp"
#include "platform.hpp"
-#include "stdint.hpp"
-#if !defined ZMQ_HAVE_WINDOWS
+#if defined ZMQ_HAVE_WINDOWS
+#include "windows.hpp"
+#else
#include <fcntl.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/tcp.h>
#endif
#if defined ZMQ_HAVE_OPENVMS
#include <ioctl.h>
#endif
-#if defined ZMQ_HAVE_SOLARIS
-
-#include <sys/sockio.h>
-#include <net/if.h>
-#include <unistd.h>
-
-// On Solaris platform, network interface name can be queried by ioctl.
-static int resolve_nic_name (struct sockaddr* addr_, char const *interface_,
- bool ipv4only_)
-{
- // TODO: Unused parameter, IPv6 support not implemented for Solaris.
- (void) ipv4only_;
-
- // Create a socket.
- int fd = socket (AF_INET, SOCK_DGRAM, 0);
- zmq_assert (fd != -1);
-
- // Retrieve number of interfaces.
- lifnum ifn;
- ifn.lifn_family = AF_INET;
- ifn.lifn_flags = 0;
- int rc = ioctl (fd, SIOCGLIFNUM, (char*) &ifn);
- zmq_assert (rc != -1);
-
- // Allocate memory to get interface names.
- size_t ifr_size = sizeof (struct lifreq) * ifn.lifn_count;
- char *ifr = (char*) malloc (ifr_size);
- alloc_assert (ifr);
-
- // Retrieve interface names.
- lifconf ifc;
- ifc.lifc_family = AF_INET;
- ifc.lifc_flags = 0;
- ifc.lifc_len = ifr_size;
- ifc.lifc_buf = ifr;
- rc = ioctl (fd, SIOCGLIFCONF, (char*) &ifc);
- zmq_assert (rc != -1);
-
- // Find the interface with the specified name and AF_INET family.
- bool found = false;
- lifreq *ifrp = ifc.lifc_req;
- for (int n = 0; n < (int) (ifc.lifc_len / sizeof (lifreq));
- n ++, ifrp ++) {
- if (!strcmp (interface_, ifrp->lifr_name)) {
- rc = ioctl (fd, SIOCGLIFADDR, (char*) ifrp);
- zmq_assert (rc != -1);
- if (ifrp->lifr_addr.ss_family == AF_INET) {
- *(sockaddr_in*) addr_ = *(sockaddr_in*) &ifrp->lifr_addr;
- found = true;
- break;
- }
- }
- }
-
- // Clean-up.
- free (ifr);
- close (fd);
-
- if (!found) {
- errno = ENODEV;
- return -1;
- }
-
- return 0;
-}
-
-#elif defined ZMQ_HAVE_AIX || ZMQ_HAVE_HPUX || ZMQ_HAVE_ANDROID
-
-#include <sys/types.h>
-#include <unistd.h>
-#include <sys/ioctl.h>
-#include <net/if.h>
-
-static int resolve_nic_name (struct sockaddr* addr_, char const *interface_,
- bool ipv4only_)
-{
- // TODO: Unused parameter, IPv6 support not implemented for AIX or HP/UX.
- (void) ipv4only_;
-
- // Create a socket.
- int sd = socket (AF_INET, SOCK_DGRAM, 0);
- zmq_assert (sd != -1);
-
- struct ifreq ifr;
-
- // Copy interface name for ioctl get.
- strncpy (ifr.ifr_name, interface_, sizeof (ifr.ifr_name));
-
- // Fetch interface address.
- int rc = ioctl (sd, SIOCGIFADDR, (caddr_t) &ifr, sizeof (struct ifreq));
-
- // Clean up.
- close (sd);
-
- if (rc == -1) {
- errno = ENODEV;
- return -1;
- }
-
- memcpy (&((sockaddr_in*) addr_)->sin_addr,
- &((sockaddr_in*) &ifr.ifr_addr)->sin_addr, sizeof (in_addr));
-
- return 0;
-}
-
-#elif ((defined ZMQ_HAVE_LINUX || defined ZMQ_HAVE_FREEBSD ||\
- defined ZMQ_HAVE_OSX || defined ZMQ_HAVE_OPENBSD ||\
- defined ZMQ_HAVE_QNXNTO || defined ZMQ_HAVE_NETBSD)\
- && defined ZMQ_HAVE_IFADDRS)
-
-#include <ifaddrs.h>
-
-// On these platforms, network interface name can be queried
-// using getifaddrs function.
-static int resolve_nic_name (struct sockaddr* addr_, char const *interface_,
- bool ipv4only_)
-{
- // Get the addresses.
- ifaddrs* ifa = NULL;
- int rc = getifaddrs (&ifa);
- zmq_assert (rc == 0);
- zmq_assert (ifa != NULL);
-
- // Find the corresponding network interface.
- bool found = false;
- for (ifaddrs *ifp = ifa; ifp != NULL ;ifp = ifp->ifa_next)
- {
- if (ifp->ifa_addr == NULL)
- continue;
-
- int family = ifp->ifa_addr->sa_family;
-
- if ((family == AF_INET
- || (!ipv4only_ && family == AF_INET6))
- && !strcmp (interface_, ifp->ifa_name))
- {
- memcpy (addr_, ifp->ifa_addr,
- (family == AF_INET) ? sizeof (struct sockaddr_in)
- : sizeof (struct sockaddr_in6));
- found = true;
- break;
- }
- }
-
- // Clean-up;
- freeifaddrs (ifa);
-
- if (!found) {
- errno = ENODEV;
- return -1;
- }
-
- return 0;
-}
-
-#else
-
-// On other platforms we assume there are no sane interface names.
-// This is true especially of Windows.
-static int resolve_nic_name (struct sockaddr* addr_, char const *interface_,
- bool ipv4only_)
-{
- // All unused parameters.
- (void) addr_;
- (void) interface_;
- (void) ipv4only_;
-
- errno = ENODEV;
- return -1;
-}
-
-#endif
-
-int zmq::resolve_ip_interface (sockaddr_storage* addr_, socklen_t *addr_len_,
- char const *interface_, bool ipv4only_)
-{
- // Find the ':' at end that separates NIC name from service.
- const char *delimiter = strrchr (interface_, ':');
- if (!delimiter) {
- errno = EINVAL;
- return -1;
- }
-
- // Separate the name/port.
- std::string iface (interface_, delimiter - interface_);
- std::string service (delimiter + 1);
-
- // 0 is not a valid port.
- uint16_t sin_port = htons ((uint16_t) atoi (service.c_str()));
- if (!sin_port) {
- errno = EINVAL;
- return -1;
- }
-
- // Initialize the output parameter.
- memset (addr_, 0, sizeof (*addr_));
-
- // Initialize temporary output pointers with storage address.
- sockaddr_storage ss;
- sockaddr *out_addr = (sockaddr *) &ss;
- socklen_t out_addrlen;
-
- // Initialise IP-format family/port and populate temporary output pointers
- // with the address.
- if (ipv4only_) {
- sockaddr_in ip4_addr;
- memset (&ip4_addr, 0, sizeof (ip4_addr));
- ip4_addr.sin_family = AF_INET;
- ip4_addr.sin_port = sin_port;
- ip4_addr.sin_addr.s_addr = htonl (INADDR_ANY);
- out_addrlen = (socklen_t) sizeof (ip4_addr);
- memcpy (out_addr, &ip4_addr, out_addrlen);
- } else {
- sockaddr_in6 ip6_addr;
- memset (&ip6_addr, 0, sizeof (ip6_addr));
- ip6_addr.sin6_family = AF_INET6;
- ip6_addr.sin6_port = sin_port;
- memcpy (&ip6_addr.sin6_addr, &in6addr_any, sizeof (in6addr_any));
- out_addrlen = (socklen_t) sizeof (ip6_addr);
- memcpy (out_addr, &ip6_addr, out_addrlen);
- }
-
- // * resolves to INADDR_ANY or in6addr_any.
- if (iface.compare("*") == 0) {
- zmq_assert (out_addrlen <= (socklen_t) sizeof (*addr_));
- memcpy (addr_, out_addr, out_addrlen);
- *addr_len_ = out_addrlen;
- return 0;
- }
-
- // Try to resolve the string as a NIC name.
- int rc = resolve_nic_name (out_addr, iface.c_str(), ipv4only_);
- if (rc != 0 && errno != ENODEV)
- return rc;
- if (rc == 0) {
- zmq_assert (out_addrlen <= (socklen_t) sizeof (*addr_));
- memcpy (addr_, out_addr, out_addrlen);
- *addr_len_ = out_addrlen;
- return 0;
- }
-
- // There's no such interface name. Assume literal address.
-#if defined ZMQ_HAVE_OPENVMS && defined __ia64
- __addrinfo64 *res = NULL;
- __addrinfo64 req;
-#else
- addrinfo *res = NULL;
- addrinfo req;
-#endif
- memset (&req, 0, sizeof (req));
-
- // Choose IPv4 or IPv6 protocol family. Note that IPv6 allows for
- // IPv4-in-IPv6 addresses.
- req.ai_family = ipv4only_ ? AF_INET : AF_INET6;
-
- // Arbitrary, not used in the output, but avoids duplicate results.
- req.ai_socktype = SOCK_STREAM;
-
- // Restrict hostname/service to literals to avoid any DNS lookups or
- // service-name irregularity due to indeterminate socktype.
- req.ai_flags = AI_PASSIVE | AI_NUMERICHOST | AI_NUMERICSERV;
-
-#ifndef ZMQ_HAVE_WINDOWS
- // Windows by default maps IPv4 addresses into IPv6. In this API we only
- // require IPv4-mapped addresses when no native IPv6 interfaces are
- // available (~AI_ALL). This saves an additional DNS roundtrip for IPv4
- // addresses.
- if (req.ai_family == AF_INET6)
- req.ai_flags |= AI_V4MAPPED;
-#endif
-
- // Resolve the literal address. Some of the error info is lost in case
- // of error, however, there's no way to report EAI errors via errno.
- rc = getaddrinfo (iface.c_str(), service.c_str(), &req, &res);
- if (rc) {
- errno = ENODEV;
- return -1;
- }
-
- // Use the first result.
- zmq_assert ((size_t) (res->ai_addrlen) <= sizeof (*addr_));
- memcpy (addr_, res->ai_addr, res->ai_addrlen);
- *addr_len_ = (socklen_t) res->ai_addrlen;
-
- // Cleanup getaddrinfo after copying the possibly referenced result.
- if (res)
- freeaddrinfo (res);
-
- return 0;
-}
-
-int zmq::resolve_ip_hostname (sockaddr_storage *addr_, socklen_t *addr_len_,
- const char *hostname_, bool ipv4only_)
-{
- // Find the ':' that separates hostname name from service.
- const char *delimiter = strrchr (hostname_, ':');
- if (!delimiter) {
- errno = EINVAL;
- return -1;
- }
-
- // Separate the hostname and service.
- std::string hostname (hostname_, delimiter - hostname_);
- std::string service (delimiter + 1);
-
- // Set up the query.
- addrinfo req;
- memset (&req, 0, sizeof (req));
-
- // Choose IPv4 or IPv6 protocol family. Note that IPv6 allows for
- // IPv4-in-IPv6 addresses.
- req.ai_family = ipv4only_ ? AF_INET : AF_INET6;
-
- // Need to choose one to avoid duplicate results from getaddrinfo() - this
- // doesn't really matter, since it's not included in the addr-output.
- req.ai_socktype = SOCK_STREAM;
-
- // Avoid named services due to unclear socktype.
- req.ai_flags = AI_NUMERICSERV;
-
-#ifndef ZMQ_HAVE_WINDOWS
- // Windows by default maps IPv4 addresses into IPv6. In this API we only
- // require IPv4-mapped addresses when no native IPv6 interfaces are
- // available. This saves an additional DNS roundtrip for IPv4 addresses.
- if (req.ai_family == AF_INET6)
- req.ai_flags |= AI_V4MAPPED;
-#endif
-
- // Resolve host name. Some of the error info is lost in case of error,
- // however, there's no way to report EAI errors via errno.
- addrinfo *res;
- int rc = getaddrinfo (hostname.c_str (), service.c_str (), &req, &res);
- if (rc) {
-
- switch (rc) {
- case EAI_MEMORY:
- errno = ENOMEM;
- break;
- default:
- errno = EINVAL;
- break;
- }
-
- return -1;
- }
-
- // Copy first result to output addr with hostname and service.
- zmq_assert ((size_t) (res->ai_addrlen) <= sizeof (*addr_));
- memcpy (addr_, res->ai_addr, res->ai_addrlen);
- *addr_len_ = (socklen_t) res->ai_addrlen;
-
- freeaddrinfo (res);
-
- return 0;
-}
-
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 cbff0c2..f1a1668 100644
--- a/src/ip.hpp
+++ b/src/ip.hpp
@@ -21,43 +21,11 @@
#ifndef __ZMQ_IP_HPP_INCLUDED__
#define __ZMQ_IP_HPP_INCLUDED__
-#include "platform.hpp"
#include "fd.hpp"
-#ifdef ZMQ_HAVE_WINDOWS
-#include "windows.hpp"
-#else
-#include <sys/types.h>
-#include <sys/socket.h>
-#include <arpa/inet.h>
-#include <netinet/in.h>
-#include <netinet/tcp.h>
-#include <netdb.h>
-#endif
-
-// Some platforms (notably Darwin/OSX and NetBSD) do not define all AI_
-// flags for getaddrinfo(). This can be worked around safely by defining
-// these to 0.
-#ifndef AI_ADDRCONFIG
-#define AI_ADDRCONFIG 0
-#endif
-#ifndef AI_NUMERICSERV
-#define AI_NUMERICSERV 0
-#endif
-
namespace zmq
{
- // Resolves network interface name in <nic-name>:<port> format. Symbol "*"
- // (asterisk) resolves to INADDR_ANY (all network interfaces).
- int resolve_ip_interface (sockaddr_storage *addr_, socklen_t *addr_len_,
- char const *interface_, bool ipv4only_);
-
- // This function resolves a string in <hostname>:<port-number> format.
- // Hostname can be either the name of the host or its IP address.
- int resolve_ip_hostname (sockaddr_storage *addr_, socklen_t *addr_len_,
- const char *hostname_, bool ipv4only_);
-
// Tunes the supplied TCP socket for the best latency.
void tune_tcp_socket (fd_t s_);
diff --git a/src/tcp_address.cpp b/src/tcp_address.cpp
new file mode 100644
index 0000000..358c847
--- /dev/null
+++ b/src/tcp_address.cpp
@@ -0,0 +1,422 @@
+/*
+ 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 <string.h>
+#include <string>
+
+#include "tcp_address.hpp"
+#include "platform.hpp"
+#include "err.hpp"
+
+#ifdef ZMQ_HAVE_WINDOWS
+#include "windows.hpp"
+#else
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <arpa/inet.h>
+#include <netinet/in.h>
+#include <netinet/tcp.h>
+#include <netdb.h>
+#endif
+
+// Some platforms (notably Darwin/OSX and NetBSD) do not define all AI_
+// flags for getaddrinfo(). This can be worked around safely by defining
+// these to 0.
+#ifndef AI_ADDRCONFIG
+#define AI_ADDRCONFIG 0
+#endif
+
+#if defined ZMQ_HAVE_SOLARIS
+
+#include <sys/sockio.h>
+#include <net/if.h>
+#include <unistd.h>
+
+// On Solaris platform, network interface name can be queried by ioctl.
+static int resolve_nic_name (struct sockaddr* addr_, char const *interface_,
+ bool ipv4only_)
+{
+ // TODO: Unused parameter, IPv6 support not implemented for Solaris.
+ (void) ipv4only_;
+
+ // Create a socket.
+ int fd = socket (AF_INET, SOCK_DGRAM, 0);
+ zmq_assert (fd != -1);
+
+ // Retrieve number of interfaces.
+ lifnum ifn;
+ ifn.lifn_family = AF_INET;
+ ifn.lifn_flags = 0;
+ int rc = ioctl (fd, SIOCGLIFNUM, (char*) &ifn);
+ zmq_assert (rc != -1);
+
+ // Allocate memory to get interface names.
+ size_t ifr_size = sizeof (struct lifreq) * ifn.lifn_count;
+ char *ifr = (char*) malloc (ifr_size);
+ alloc_assert (ifr);
+
+ // Retrieve interface names.
+ lifconf ifc;
+ ifc.lifc_family = AF_INET;
+ ifc.lifc_flags = 0;
+ ifc.lifc_len = ifr_size;
+ ifc.lifc_buf = ifr;
+ rc = ioctl (fd, SIOCGLIFCONF, (char*) &ifc);
+ zmq_assert (rc != -1);
+
+ // Find the interface with the specified name and AF_INET family.
+ bool found = false;
+ lifreq *ifrp = ifc.lifc_req;
+ for (int n = 0; n < (int) (ifc.lifc_len / sizeof (lifreq));
+ n ++, ifrp ++) {
+ if (!strcmp (interface_, ifrp->lifr_name)) {
+ rc = ioctl (fd, SIOCGLIFADDR, (char*) ifrp);
+ zmq_assert (rc != -1);
+ if (ifrp->lifr_addr.ss_family == AF_INET) {
+ *(sockaddr_in*) addr_ = *(sockaddr_in*) &ifrp->lifr_addr;
+ found = true;
+ break;
+ }
+ }
+ }
+
+ // Clean-up.
+ free (ifr);
+ close (fd);
+
+ if (!found) {
+ errno = ENODEV;
+ return -1;
+ }
+
+ return 0;
+}
+
+#elif defined ZMQ_HAVE_AIX || ZMQ_HAVE_HPUX || ZMQ_HAVE_ANDROID
+
+#include <sys/types.h>
+#include <unistd.h>
+#include <sys/ioctl.h>
+#include <net/if.h>
+
+static int resolve_nic_name (struct sockaddr* addr_, char const *interface_,
+ bool ipv4only_)
+{
+ // TODO: Unused parameter, IPv6 support not implemented for AIX or HP/UX.
+ (void) ipv4only_;
+
+ // Create a socket.
+ int sd = socket (AF_INET, SOCK_DGRAM, 0);
+ zmq_assert (sd != -1);
+
+ struct ifreq ifr;
+
+ // Copy interface name for ioctl get.
+ strncpy (ifr.ifr_name, interface_, sizeof (ifr.ifr_name));
+
+ // Fetch interface address.
+ int rc = ioctl (sd, SIOCGIFADDR, (caddr_t) &ifr, sizeof (struct ifreq));
+
+ // Clean up.
+ close (sd);
+
+ if (rc == -1) {
+ errno = ENODEV;
+ return -1;
+ }
+
+ memcpy (&((sockaddr_in*) addr_)->sin_addr,
+ &((sockaddr_in*) &ifr.ifr_addr)->sin_addr, sizeof (in_addr));
+
+ return 0;
+}
+
+#elif ((defined ZMQ_HAVE_LINUX || defined ZMQ_HAVE_FREEBSD ||\
+ defined ZMQ_HAVE_OSX || defined ZMQ_HAVE_OPENBSD ||\
+ defined ZMQ_HAVE_QNXNTO || defined ZMQ_HAVE_NETBSD)\
+ && defined ZMQ_HAVE_IFADDRS)
+
+#include <ifaddrs.h>
+
+// On these platforms, network interface name can be queried
+// using getifaddrs function.
+static int resolve_nic_name (struct sockaddr* addr_, char const *interface_,
+ bool ipv4only_)
+{
+ // Get the addresses.
+ ifaddrs* ifa = NULL;
+ int rc = getifaddrs (&ifa);
+ zmq_assert (rc == 0);
+ zmq_assert (ifa != NULL);
+
+ // Find the corresponding network interface.
+ bool found = false;
+ for (ifaddrs *ifp = ifa; ifp != NULL ;ifp = ifp->ifa_next)
+ {
+ if (ifp->ifa_addr == NULL)
+ continue;
+
+ int family = ifp->ifa_addr->sa_family;
+
+ if ((family == AF_INET
+ || (!ipv4only_ && family == AF_INET6))
+ && !strcmp (interface_, ifp->ifa_name))
+ {
+ memcpy (addr_, ifp->ifa_addr,
+ (family == AF_INET) ? sizeof (struct sockaddr_in)
+ : sizeof (struct sockaddr_in6));
+ found = true;
+ break;
+ }
+ }
+
+ // Clean-up;
+ freeifaddrs (ifa);
+
+ if (!found) {
+ errno = ENODEV;
+ return -1;
+ }
+
+ return 0;
+}
+
+#else
+
+// On other platforms we assume there are no sane interface names.
+// This is true especially of Windows.
+static int resolve_nic_name (struct sockaddr* addr_, char const *interface_,
+ bool ipv4only_)
+{
+ // All unused parameters.
+ (void) addr_;
+ (void) interface_;
+ (void) ipv4only_;
+
+ errno = ENODEV;
+ return -1;
+}
+
+#endif
+
+int zmq::tcp_address_t::resolve_interface (char const *interface_,
+ bool ipv4only_)
+{
+ // Initialize temporary output pointers with storage address.
+ sockaddr_storage ss;
+ sockaddr *out_addr = (sockaddr *) &ss;
+ socklen_t out_addrlen;
+
+ // Initialise IP-format family/port and populate temporary output pointers
+ // with the address.
+ if (ipv4only_) {
+ sockaddr_in ip4_addr;
+ memset (&ip4_addr, 0, sizeof (ip4_addr));
+ ip4_addr.sin_family = AF_INET;
+ ip4_addr.sin_addr.s_addr = htonl (INADDR_ANY);
+ out_addrlen = (socklen_t) sizeof (ip4_addr);
+ memcpy (out_addr, &ip4_addr, out_addrlen);
+ } else {
+ sockaddr_in6 ip6_addr;
+ memset (&ip6_addr, 0, sizeof (ip6_addr));
+ ip6_addr.sin6_family = AF_INET6;
+ memcpy (&ip6_addr.sin6_addr, &in6addr_any, sizeof (in6addr_any));
+ out_addrlen = (socklen_t) sizeof (ip6_addr);
+ memcpy (out_addr, &ip6_addr, out_addrlen);
+ }
+
+ // * resolves to INADDR_ANY or in6addr_any.
+ if (strcmp (interface_, "*") == 0) {
+ zmq_assert (out_addrlen <= (socklen_t) sizeof (address));
+ memcpy (&address, out_addr, out_addrlen);
+ return 0;
+ }
+
+ // Try to resolve the string as a NIC name.
+ int rc = resolve_nic_name (out_addr, interface_, ipv4only_);
+ if (rc != 0 && errno != ENODEV)
+ return rc;
+ if (rc == 0) {
+ zmq_assert (out_addrlen <= (socklen_t) sizeof (address));
+ memcpy (&address, out_addr, out_addrlen);
+ return 0;
+ }
+
+ // There's no such interface name. Assume literal address.
+#if defined ZMQ_HAVE_OPENVMS && defined __ia64
+ __addrinfo64 *res = NULL;
+ __addrinfo64 req;
+#else
+ addrinfo *res = NULL;
+ addrinfo req;
+#endif
+ memset (&req, 0, sizeof (req));
+
+ // Choose IPv4 or IPv6 protocol family. Note that IPv6 allows for
+ // IPv4-in-IPv6 addresses.
+ req.ai_family = ipv4only_ ? AF_INET : AF_INET6;
+
+ // Arbitrary, not used in the output, but avoids duplicate results.
+ req.ai_socktype = SOCK_STREAM;
+
+ // Restrict hostname/service to literals to avoid any DNS lookups or
+ // service-name irregularity due to indeterminate socktype.
+ req.ai_flags = AI_PASSIVE | AI_NUMERICHOST;
+
+#ifndef ZMQ_HAVE_WINDOWS
+ // Windows by default maps IPv4 addresses into IPv6. In this API we only
+ // require IPv4-mapped addresses when no native IPv6 interfaces are
+ // available (~AI_ALL). This saves an additional DNS roundtrip for IPv4
+ // addresses.
+ if (req.ai_family == AF_INET6)
+ req.ai_flags |= AI_V4MAPPED;
+#endif
+
+ // Resolve the literal address. Some of the error info is lost in case
+ // of error, however, there's no way to report EAI errors via errno.
+ rc = getaddrinfo (interface_, NULL, &req, &res);
+ if (rc) {
+ errno = ENODEV;
+ return -1;
+ }
+
+ // Use the first result.
+ zmq_assert ((size_t) (res->ai_addrlen) <= sizeof (address));
+ memcpy (&address, res->ai_addr, res->ai_addrlen);
+
+ // Cleanup getaddrinfo after copying the possibly referenced result.
+ if (res)
+ freeaddrinfo (res);
+
+ return 0;
+}
+
+int zmq::tcp_address_t::resolve_hostname (const char *hostname_, bool ipv4only_)
+{
+ // Set up the query.
+ addrinfo req;
+ memset (&req, 0, sizeof (req));
+
+ // Choose IPv4 or IPv6 protocol family. Note that IPv6 allows for
+ // IPv4-in-IPv6 addresses.
+ req.ai_family = ipv4only_ ? AF_INET : AF_INET6;
+
+ // Need to choose one to avoid duplicate results from getaddrinfo() - this
+ // doesn't really matter, since it's not included in the addr-output.
+ req.ai_socktype = SOCK_STREAM;
+
+#ifndef ZMQ_HAVE_WINDOWS
+ // Windows by default maps IPv4 addresses into IPv6. In this API we only
+ // require IPv4-mapped addresses when no native IPv6 interfaces are
+ // available. This saves an additional DNS roundtrip for IPv4 addresses.
+ if (req.ai_family == AF_INET6)
+ req.ai_flags |= AI_V4MAPPED;
+#endif
+
+ // Resolve host name. Some of the error info is lost in case of error,
+ // however, there's no way to report EAI errors via errno.
+ addrinfo *res;
+ int rc = getaddrinfo (hostname_, NULL, &req, &res);
+ if (rc) {
+ switch (rc) {
+ case EAI_MEMORY:
+ errno = ENOMEM;
+ break;
+ default:
+ errno = EINVAL;
+ break;
+ }
+ return -1;
+ }
+
+ // Copy first result to output addr with hostname and service.
+ zmq_assert ((size_t) (res->ai_addrlen) <= sizeof (address));
+ memcpy (&address, res->ai_addr, res->ai_addrlen);
+
+ freeaddrinfo (res);
+
+ return 0;
+}
+
+zmq::tcp_address_t::tcp_address_t ()
+{
+ memset (&address, 0, sizeof (address));
+}
+
+zmq::tcp_address_t::~tcp_address_t ()
+{
+}
+
+int zmq::tcp_address_t::resolve (const char *name_, bool local_, bool ipv4only_)
+{
+ // Find the ':' at end that separates address from the port number.
+ const char *delimiter = strrchr (name_, ':');
+ if (!delimiter) {
+ errno = EINVAL;
+ return -1;
+ }
+
+ // Separate the address/port.
+ std::string addr_str (name_, delimiter - name_);
+ std::string port_str (delimiter + 1);
+
+ // Parse the port number (0 is not a valid port).
+ uint16_t port = (uint16_t) atoi (port_str.c_str());
+ if (port == 0) {
+ errno = EINVAL;
+ return -1;
+ }
+
+ // Resolve the IP address.
+ int rc;
+ if (local_)
+ rc = resolve_interface (addr_str.c_str (), ipv4only_);
+ else
+ rc = resolve_hostname (addr_str.c_str (), ipv4only_);
+ if (rc != 0)
+ return -1;
+
+ // Set the port into the address structure.
+ if (address.generic.sa_family == AF_INET6)
+ address.ipv6.sin6_port = htons (port);
+ else
+ address.ipv4.sin_port = htons (port);
+
+ return 0;
+}
+
+sockaddr *zmq::tcp_address_t::addr ()
+{
+ return &address.generic;
+}
+
+socklen_t zmq::tcp_address_t::addrlen ()
+{
+ if (address.generic.sa_family == AF_INET6)
+ return (socklen_t) sizeof (address.ipv6);
+ else
+ return (socklen_t) sizeof (address.ipv4);
+}
+
+sa_family_t zmq::tcp_address_t::family ()
+{
+ return address.generic.sa_family;
+}
+
diff --git a/src/tcp_address.hpp b/src/tcp_address.hpp
new file mode 100644
index 0000000..7b848f9
--- /dev/null
+++ b/src/tcp_address.hpp
@@ -0,0 +1,68 @@
+/*
+ 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_TCP_ADDRESS_HPP_INCLUDED__
+#define __ZMQ_TCP_ADDRESS_HPP_INCLUDED__
+
+#include "platform.hpp"
+
+#include <sys/socket.h>
+#include <netinet/in.h>
+
+namespace zmq
+{
+
+ class tcp_address_t
+ {
+ public:
+
+ tcp_address_t ();
+ ~tcp_address_t ();
+
+ // This function translates textual TCP address into an address
+ // strcuture. If 'local' is true, names are resolved as local interface
+ // names. If it is false, names are resolved as remote hostnames.
+ // If 'ipv4only' is true, the name will never resolve to IPv6 address.
+ int resolve (const char* name_, bool local_, bool ipv4only_);
+
+ sa_family_t family ();
+ sockaddr *addr ();
+ socklen_t addrlen ();
+
+ private:
+
+ int resolve_interface (char const *interface_, bool ipv4only_);
+ int resolve_hostname (const char *hostname_, bool ipv4only_);
+
+ union {
+ sockaddr generic;
+ sockaddr_in ipv4;
+ sockaddr_in6 ipv6;
+ char padding [1024];
+ } address;
+
+ tcp_address_t (const tcp_address_t&);
+ const tcp_address_t &operator = (const tcp_address_t&);
+ };
+
+}
+
+#endif
+
diff --git a/src/tcp_connecter.cpp b/src/tcp_connecter.cpp
index 10e1be6..ac27181 100644
--- a/src/tcp_connecter.cpp
+++ b/src/tcp_connecter.cpp
@@ -56,9 +56,6 @@ zmq::tcp_connecter_t::tcp_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_);
@@ -179,8 +176,7 @@ int zmq::tcp_connecter_t::get_new_reconnect_ivl ()
int zmq::tcp_connecter_t::set_address (const char *addr_)
{
- return resolve_ip_hostname (&addr, &addr_len, addr_,
- options.ipv4only ? true : false);
+ return address.resolve (addr_, false, options.ipv4only ? true : false);
}
int zmq::tcp_connecter_t::open ()
@@ -188,7 +184,7 @@ int zmq::tcp_connecter_t::open ()
zmq_assert (s == retired_fd);
// Create the socket.
- s = socket (addr.ss_family, SOCK_STREAM, IPPROTO_TCP);
+ s = socket (address.family (), SOCK_STREAM, IPPROTO_TCP);
#ifdef ZMQ_HAVE_WINDOWS
if (s == INVALID_SOCKET) {
wsa_error_to_errno ();
@@ -201,14 +197,14 @@ int zmq::tcp_connecter_t::open ()
// On some systems, IPv4 mapping in IPv6 sockets is disabled by default.
// Switch it on in such cases.
- if (addr.ss_family == AF_INET6)
+ if (address.family () == AF_INET6)
enable_ipv4_mapping (s);
// Set the socket to non-blocking mode so that we get async connect().
unblock_socket (s);
// Connect to the remote peer.
- int rc = ::connect (s, (struct sockaddr*) &addr, addr_len);
+ int rc = ::connect (s, address.addr (), address.addrlen ());
// Connect was successfull immediately.
if (rc == 0)
diff --git a/src/tcp_connecter.hpp b/src/tcp_connecter.hpp
index b5b39e8..17f8a70 100644
--- a/src/tcp_connecter.hpp
+++ b/src/tcp_connecter.hpp
@@ -22,10 +22,10 @@
#define __TCP_CONNECTER_HPP_INCLUDED__
#include "fd.hpp"
-#include "ip.hpp"
#include "own.hpp"
-#include "io_object.hpp"
#include "stdint.hpp"
+#include "io_object.hpp"
+#include "tcp_address.hpp"
namespace zmq
{
@@ -81,8 +81,7 @@ namespace zmq
fd_t connect ();
// Address to connect to.
- sockaddr_storage addr;
- socklen_t addr_len;
+ tcp_address_t address;
// Underlying socket.
fd_t s;
diff --git a/src/tcp_listener.cpp b/src/tcp_listener.cpp
index dd654b2..e78a4a3 100644
--- a/src/tcp_listener.cpp
+++ b/src/tcp_listener.cpp
@@ -55,8 +55,6 @@ zmq::tcp_listener_t::tcp_listener_t (io_thread_t *io_thread_,
s (retired_fd),
socket (socket_)
{
- memset (&addr, 0, sizeof (addr));
- addr_len = 0;
}
zmq::tcp_listener_t::~tcp_listener_t ()
@@ -122,26 +120,25 @@ void zmq::tcp_listener_t::close ()
int zmq::tcp_listener_t::set_address (const char *addr_)
{
- // Convert the interface into sockaddr_in structure.
- int rc = resolve_ip_interface (&addr, &addr_len, addr_,
- options.ipv4only ? true : false);
+ // Convert the textual address into address structure.
+ int rc = address.resolve (addr_, true, options.ipv4only ? true : false);
if (rc != 0)
return -1;
// Create a listening socket.
- s = ::socket (addr.ss_family, SOCK_STREAM, IPPROTO_TCP);
+ s = ::socket (address.family (), SOCK_STREAM, IPPROTO_TCP);
#ifdef ZMQ_HAVE_WINDOWS
if (s == INVALID_SOCKET)
wsa_error_to_errno ();
#endif
// IPv6 address family not supported, try automatic downgrade to IPv4.
- if (addr.ss_family == AF_INET6 && errno == EAFNOSUPPORT &&
+ if (address.family () == AF_INET6 && errno == EAFNOSUPPORT &&
!options.ipv4only) {
- rc = resolve_ip_interface (&addr, &addr_len, addr_, true);
+ rc = address.resolve (addr_, true, true);
if (rc != 0)
return rc;
- s = ::socket (addr.ss_family, SOCK_STREAM, IPPROTO_TCP);
+ s = ::socket (address.family (), SOCK_STREAM, IPPROTO_TCP);
}
#ifdef ZMQ_HAVE_WINDOWS
@@ -156,7 +153,7 @@ int zmq::tcp_listener_t::set_address (const char *addr_)
// On some systems, IPv4 mapping in IPv6 sockets is disabled by default.
// Switch it on in such cases.
- if (addr.ss_family == AF_INET6)
+ if (address.family () == AF_INET6)
enable_ipv4_mapping (s);
// Allow reusing of the address.
@@ -171,7 +168,7 @@ int zmq::tcp_listener_t::set_address (const char *addr_)
#endif
// Bind the socket to the network interface and port.
- rc = bind (s, (struct sockaddr*) &addr, addr_len);
+ rc = bind (s, address.addr (), address.addrlen ());
#ifdef ZMQ_HAVE_WINDOWS
if (rc == SOCKET_ERROR) {
wsa_error_to_errno ();
diff --git a/src/tcp_listener.hpp b/src/tcp_listener.hpp
index 69fde37..60713e3 100644
--- a/src/tcp_listener.hpp
+++ b/src/tcp_listener.hpp
@@ -22,10 +22,10 @@
#define __ZMQ_TCP_LISTENER_HPP_INCLUDED__
#include "fd.hpp"
-#include "ip.hpp"
#include "own.hpp"
-#include "io_object.hpp"
#include "stdint.hpp"
+#include "io_object.hpp"
+#include "tcp_address.hpp"
namespace zmq
{
@@ -59,8 +59,7 @@ namespace zmq
fd_t accept ();
// Address to listen on.
- sockaddr_storage addr;
- socklen_t addr_len;
+ tcp_address_t address;
// True, if the undelying file for UNIX domain socket exists.
bool has_file;
diff --git a/src/vtcp_connecter.cpp b/src/vtcp_connecter.cpp
index 61e7e84..3d2900e 100644
--- a/src/vtcp_connecter.cpp
+++ b/src/vtcp_connecter.cpp
@@ -60,8 +60,6 @@ zmq::vtcp_connecter_t::vtcp_connecter_t (class io_thread_t *io_thread_,
session (session_),
current_reconnect_ivl(options.reconnect_ivl)
{
- memset (&addr, 0, sizeof (addr));
- addr_len = 0;
subport = 0;
int rc = set_address (address_);
@@ -92,8 +90,7 @@ int zmq::vtcp_connecter_t::set_address (const char *addr_)
addr_str += ":9220";
std::string subport_str (delimiter + 1);
subport = (vtcp_subport_t) atoi (subport_str.c_str ());
- int rc = resolve_ip_hostname (&addr, &addr_len, addr_str.c_str (),
- true);
+ int rc = address.resolve (addr_str.c_str (), false, true);
if (rc != 0)
return -1;
}
@@ -101,8 +98,7 @@ int zmq::vtcp_connecter_t::set_address (const char *addr_)
std::string addr_str (addr_, delimiter - addr_);
std::string subport_str (delimiter + 1);
subport = (vtcp_subport_t) atoi (subport_str.c_str ());
- int rc = resolve_ip_hostname (&addr, &addr_len, addr_str.c_str (),
- true);
+ int rc = address.resolve (addr_str.c_str (), false, true);
if (rc != 0)
return -1;
}
@@ -207,7 +203,7 @@ int zmq::vtcp_connecter_t::open ()
zmq_assert (s == retired_fd);
// Start the connection procedure.
- sockaddr_in *paddr = (sockaddr_in*) &addr;
+ sockaddr_in *paddr = (sockaddr_in*) address.addr ();
s = vtcp_connect (paddr->sin_addr.s_addr, ntohs (paddr->sin_port));
// Connect was successfull immediately.
diff --git a/src/vtcp_connecter.hpp b/src/vtcp_connecter.hpp
index 61a592f..f467b5f 100644
--- a/src/vtcp_connecter.hpp
+++ b/src/vtcp_connecter.hpp
@@ -28,10 +28,10 @@
#include <vtcp.h>
#include "fd.hpp"
-#include "ip.hpp"
#include "own.hpp"
-#include "io_object.hpp"
#include "stdint.hpp"
+#include "io_object.hpp"
+#include "tcp_address.hpp"
namespace zmq
{
@@ -87,8 +87,7 @@ namespace zmq
fd_t connect ();
// Address to connect to.
- sockaddr_storage addr;
- socklen_t addr_len;
+ tcp_address_t address;
vtcp_subport_t subport;
// Underlying socket.