2011-08-18 17:40:42 +02:00
|
|
|
/*
|
2015-01-22 10:32:06 +01:00
|
|
|
Copyright (c) 2007-2015 Contributors as noted in the AUTHORS file
|
2011-08-18 17:40:42 +02:00
|
|
|
|
2015-06-02 22:33:55 +02:00
|
|
|
This file is part of libzmq, the ZeroMQ core engine in C++.
|
2011-08-18 17:40:42 +02:00
|
|
|
|
2015-06-02 22:33:55 +02:00
|
|
|
libzmq is free software; you can redistribute it and/or modify it under
|
|
|
|
the terms of the GNU Lesser General Public License (LGPL) as published
|
|
|
|
by the Free Software Foundation; either version 3 of the License, or
|
2011-08-18 17:40:42 +02:00
|
|
|
(at your option) any later version.
|
|
|
|
|
2015-06-02 22:33:55 +02:00
|
|
|
As a special exception, the Contributors give you permission to link
|
|
|
|
this library with independent modules to produce an executable,
|
|
|
|
regardless of the license terms of these independent modules, and to
|
|
|
|
copy and distribute the resulting executable under terms of your choice,
|
|
|
|
provided that you also meet, for each linked independent module, the
|
|
|
|
terms and conditions of the license of that module. An independent
|
|
|
|
module is a module which is not derived from or based on this library.
|
|
|
|
If you modify this library, you must extend this exception to your
|
|
|
|
version of the library.
|
|
|
|
|
|
|
|
libzmq 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.
|
2011-08-18 17:40:42 +02:00
|
|
|
|
|
|
|
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>
|
2012-04-18 23:42:11 +04:00
|
|
|
#include <sstream>
|
2011-08-18 17:40:42 +02:00
|
|
|
|
2015-08-21 16:12:22 -07:00
|
|
|
#include "macros.hpp"
|
2011-08-18 17:40:42 +02:00
|
|
|
#include "tcp_address.hpp"
|
|
|
|
#include "platform.hpp"
|
2011-08-18 17:58:46 +02:00
|
|
|
#include "stdint.hpp"
|
2011-08-18 17:40:42 +02:00
|
|
|
#include "err.hpp"
|
2011-09-02 15:34:12 +02:00
|
|
|
#include "ip.hpp"
|
2011-08-18 17:40:42 +02:00
|
|
|
|
|
|
|
#ifdef ZMQ_HAVE_WINDOWS
|
|
|
|
#include "windows.hpp"
|
|
|
|
#else
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <netinet/tcp.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#endif
|
|
|
|
|
2014-06-03 10:39:09 +02:00
|
|
|
#ifdef ZMQ_HAVE_SOLARIS
|
2011-08-18 17:40:42 +02:00
|
|
|
|
|
|
|
#include <sys/sockio.h>
|
|
|
|
#include <net/if.h>
|
|
|
|
#include <unistd.h>
|
2011-08-19 12:34:31 +02:00
|
|
|
#include <stdlib.h>
|
2011-08-18 17:40:42 +02:00
|
|
|
|
|
|
|
// On Solaris platform, network interface name can be queried by ioctl.
|
2014-04-30 14:43:37 +02:00
|
|
|
int zmq::tcp_address_t::resolve_nic_name (const char *nic_, bool ipv6_, bool is_src_)
|
2011-08-18 17:40:42 +02:00
|
|
|
{
|
|
|
|
// TODO: Unused parameter, IPv6 support not implemented for Solaris.
|
2015-08-22 00:31:25 +04:30
|
|
|
LIBZMQ_UNUSED(ipv6_);
|
2011-08-18 17:40:42 +02:00
|
|
|
|
|
|
|
// Create a socket.
|
2014-06-03 10:39:09 +02:00
|
|
|
const int fd = open_socket (AF_INET, SOCK_DGRAM, 0);
|
2012-05-28 23:13:09 +02:00
|
|
|
errno_assert (fd != -1);
|
2011-08-18 17:40:42 +02:00
|
|
|
|
|
|
|
// Retrieve number of interfaces.
|
|
|
|
lifnum ifn;
|
|
|
|
ifn.lifn_family = AF_INET;
|
|
|
|
ifn.lifn_flags = 0;
|
|
|
|
int rc = ioctl (fd, SIOCGLIFNUM, (char*) &ifn);
|
2012-05-28 23:13:09 +02:00
|
|
|
errno_assert (rc != -1);
|
2011-08-18 17:40:42 +02:00
|
|
|
|
|
|
|
// Allocate memory to get interface names.
|
2014-06-03 10:39:09 +02:00
|
|
|
const size_t ifr_size = sizeof (struct lifreq) * ifn.lifn_count;
|
2011-08-18 17:40:42 +02:00
|
|
|
char *ifr = (char*) malloc (ifr_size);
|
|
|
|
alloc_assert (ifr);
|
2012-04-13 13:26:57 +04:00
|
|
|
|
2011-08-18 17:40:42 +02:00
|
|
|
// 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);
|
2012-05-28 23:13:09 +02:00
|
|
|
errno_assert (rc != -1);
|
2011-08-18 17:40:42 +02:00
|
|
|
|
|
|
|
// Find the interface with the specified name and AF_INET family.
|
|
|
|
bool found = false;
|
|
|
|
lifreq *ifrp = ifc.lifc_req;
|
2015-06-13 22:08:14 +02:00
|
|
|
for (int n = 0; n < (int) (ifc.lifc_len / sizeof (lifreq));
|
2011-08-18 17:40:42 +02:00
|
|
|
n ++, ifrp ++) {
|
2011-08-19 12:34:31 +02:00
|
|
|
if (!strcmp (nic_, ifrp->lifr_name)) {
|
2011-08-18 17:40:42 +02:00
|
|
|
rc = ioctl (fd, SIOCGLIFADDR, (char*) ifrp);
|
2012-05-28 23:13:09 +02:00
|
|
|
errno_assert (rc != -1);
|
2011-08-18 17:40:42 +02:00
|
|
|
if (ifrp->lifr_addr.ss_family == AF_INET) {
|
2014-06-03 10:39:09 +02:00
|
|
|
if (is_src_)
|
2014-04-30 14:43:37 +02:00
|
|
|
source_address.ipv4 = *(sockaddr_in*) &ifrp->lifr_addr;
|
2014-06-03 10:39:09 +02:00
|
|
|
else
|
|
|
|
address.ipv4 = *(sockaddr_in*) &ifrp->lifr_addr;
|
2011-08-18 17:40:42 +02:00
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clean-up.
|
|
|
|
free (ifr);
|
|
|
|
close (fd);
|
|
|
|
|
|
|
|
if (!found) {
|
|
|
|
errno = ENODEV;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-11-15 08:40:46 +01:00
|
|
|
#elif defined ZMQ_HAVE_AIX || defined ZMQ_HAVE_HPUX || defined ZMQ_HAVE_ANDROID
|
2011-08-18 17:40:42 +02:00
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <net/if.h>
|
|
|
|
|
2014-04-30 14:43:37 +02:00
|
|
|
int zmq::tcp_address_t::resolve_nic_name (const char *nic_, bool ipv6_, bool is_src_)
|
2011-08-18 17:40:42 +02:00
|
|
|
{
|
|
|
|
// TODO: Unused parameter, IPv6 support not implemented for AIX or HP/UX.
|
2015-08-22 00:31:25 +04:30
|
|
|
LIBZMQ_UNUSED(ipv6_);
|
2011-08-18 17:40:42 +02:00
|
|
|
|
|
|
|
// Create a socket.
|
2014-07-01 09:17:19 +02:00
|
|
|
const int sd = open_socket (AF_INET, SOCK_DGRAM, 0);
|
2012-05-28 23:13:09 +02:00
|
|
|
errno_assert (sd != -1);
|
2011-08-18 17:40:42 +02:00
|
|
|
|
2012-06-08 11:46:45 -07:00
|
|
|
struct ifreq ifr;
|
2011-08-18 17:40:42 +02:00
|
|
|
|
|
|
|
// Copy interface name for ioctl get.
|
2014-06-03 10:39:09 +02:00
|
|
|
strncpy (ifr.ifr_name, nic_, sizeof ifr.ifr_name);
|
2011-08-18 17:40:42 +02:00
|
|
|
|
|
|
|
// Fetch interface address.
|
2014-07-01 09:17:19 +02:00
|
|
|
const int rc = ioctl (sd, SIOCGIFADDR, (caddr_t) &ifr, sizeof ifr);
|
2011-08-18 17:40:42 +02:00
|
|
|
|
|
|
|
// Clean up.
|
|
|
|
close (sd);
|
|
|
|
|
|
|
|
if (rc == -1) {
|
|
|
|
errno = ENODEV;
|
|
|
|
return -1;
|
|
|
|
}
|
2014-06-03 10:39:09 +02:00
|
|
|
if (is_src_)
|
2014-06-15 19:44:28 +02:00
|
|
|
memcpy (&source_address.ipv4.sin_addr,
|
2014-06-22 18:05:50 +08:00
|
|
|
&((sockaddr_in*) &ifr.ifr_addr)->sin_addr, sizeof (struct in_addr));
|
2014-06-03 10:39:09 +02:00
|
|
|
else
|
2014-06-15 19:44:28 +02:00
|
|
|
memcpy (&address.ipv4.sin_addr,
|
2014-06-22 18:05:50 +08:00
|
|
|
&((sockaddr_in*) &ifr.ifr_addr)->sin_addr, sizeof (struct in_addr));
|
2011-08-18 17:40:42 +02:00
|
|
|
|
2012-04-13 13:26:57 +04:00
|
|
|
return 0;
|
2011-08-18 17:40:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#elif ((defined ZMQ_HAVE_LINUX || defined ZMQ_HAVE_FREEBSD ||\
|
|
|
|
defined ZMQ_HAVE_OSX || defined ZMQ_HAVE_OPENBSD ||\
|
2015-09-03 08:58:12 +01:00
|
|
|
defined ZMQ_HAVE_QNXNTO || defined ZMQ_HAVE_NETBSD ||\
|
|
|
|
defined ZMQ_HAVE_DRAGONFLY)\
|
2011-08-18 17:40:42 +02:00
|
|
|
&& defined ZMQ_HAVE_IFADDRS)
|
|
|
|
|
|
|
|
#include <ifaddrs.h>
|
|
|
|
|
|
|
|
// On these platforms, network interface name can be queried
|
|
|
|
// using getifaddrs function.
|
2014-04-30 14:43:37 +02:00
|
|
|
int zmq::tcp_address_t::resolve_nic_name (const char *nic_, bool ipv6_, bool is_src_)
|
2011-08-18 17:40:42 +02:00
|
|
|
{
|
|
|
|
// Get the addresses.
|
2013-01-31 20:47:45 +01:00
|
|
|
ifaddrs *ifa = NULL;
|
2014-06-15 19:44:28 +02:00
|
|
|
const int rc = getifaddrs (&ifa);
|
2012-05-28 23:13:09 +02:00
|
|
|
errno_assert (rc == 0);
|
2011-08-18 17:40:42 +02:00
|
|
|
zmq_assert (ifa != NULL);
|
|
|
|
|
|
|
|
// Find the corresponding network interface.
|
|
|
|
bool found = false;
|
2014-06-03 10:39:09 +02:00
|
|
|
for (ifaddrs *ifp = ifa; ifp != NULL; ifp = ifp->ifa_next) {
|
2011-08-18 17:40:42 +02:00
|
|
|
if (ifp->ifa_addr == NULL)
|
|
|
|
continue;
|
|
|
|
|
2014-06-03 10:39:09 +02:00
|
|
|
const int family = ifp->ifa_addr->sa_family;
|
2013-01-31 20:47:45 +01:00
|
|
|
if ((family == AF_INET || (ipv6_ && family == AF_INET6))
|
|
|
|
&& !strcmp (nic_, ifp->ifa_name)) {
|
2014-06-03 10:39:09 +02:00
|
|
|
if (is_src_)
|
2014-04-30 14:43:37 +02:00
|
|
|
memcpy (&source_address, ifp->ifa_addr,
|
|
|
|
(family == AF_INET) ? sizeof (struct sockaddr_in)
|
|
|
|
: sizeof (struct sockaddr_in6));
|
2014-06-03 10:39:09 +02:00
|
|
|
else
|
2014-04-30 14:43:37 +02:00
|
|
|
memcpy (&address, ifp->ifa_addr,
|
|
|
|
(family == AF_INET) ? sizeof (struct sockaddr_in)
|
|
|
|
: sizeof (struct sockaddr_in6));
|
2011-08-18 17:40:42 +02:00
|
|
|
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.
|
2014-04-30 14:43:37 +02:00
|
|
|
int zmq::tcp_address_t::resolve_nic_name (const char *nic_, bool ipv6_, bool is_src_)
|
2011-08-18 17:40:42 +02:00
|
|
|
{
|
2015-08-22 00:31:25 +04:30
|
|
|
LIBZMQ_UNUSED(nic_);
|
|
|
|
LIBZMQ_UNUSED(ipv6_);
|
2011-08-18 17:40:42 +02:00
|
|
|
|
|
|
|
errno = ENODEV;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2014-04-30 14:43:37 +02:00
|
|
|
int zmq::tcp_address_t::resolve_interface (const char *interface_, bool ipv6_, bool is_src_)
|
2011-08-18 17:40:42 +02:00
|
|
|
{
|
|
|
|
// Initialize temporary output pointers with storage address.
|
|
|
|
sockaddr_storage ss;
|
2012-03-27 06:26:39 +02:00
|
|
|
sockaddr *out_addr = (sockaddr*) &ss;
|
|
|
|
size_t out_addrlen;
|
2011-08-18 17:40:42 +02:00
|
|
|
|
|
|
|
// Initialise IP-format family/port and populate temporary output pointers
|
|
|
|
// with the address.
|
2013-01-31 20:47:45 +01:00
|
|
|
if (ipv6_) {
|
2011-08-18 17:40:42 +02:00
|
|
|
sockaddr_in6 ip6_addr;
|
2014-06-03 10:39:09 +02:00
|
|
|
memset (&ip6_addr, 0, sizeof ip6_addr);
|
2011-08-18 17:40:42 +02:00
|
|
|
ip6_addr.sin6_family = AF_INET6;
|
2014-06-03 10:39:09 +02:00
|
|
|
memcpy (&ip6_addr.sin6_addr, &in6addr_any, sizeof in6addr_any);
|
2012-03-27 06:26:39 +02:00
|
|
|
out_addrlen = sizeof ip6_addr;
|
2011-08-18 17:40:42 +02:00
|
|
|
memcpy (out_addr, &ip6_addr, out_addrlen);
|
|
|
|
}
|
2013-01-31 20:47:45 +01:00
|
|
|
else {
|
|
|
|
sockaddr_in ip4_addr;
|
2014-06-03 10:39:09 +02:00
|
|
|
memset (&ip4_addr, 0, sizeof ip4_addr);
|
2013-01-31 20:47:45 +01:00
|
|
|
ip4_addr.sin_family = AF_INET;
|
|
|
|
ip4_addr.sin_addr.s_addr = htonl (INADDR_ANY);
|
|
|
|
out_addrlen = sizeof ip4_addr;
|
|
|
|
memcpy (out_addr, &ip4_addr, out_addrlen);
|
|
|
|
}
|
|
|
|
// "*" resolves to INADDR_ANY or in6addr_any.
|
2011-08-18 17:40:42 +02:00
|
|
|
if (strcmp (interface_, "*") == 0) {
|
2012-03-27 06:26:39 +02:00
|
|
|
zmq_assert (out_addrlen <= sizeof address);
|
2014-06-03 10:39:09 +02:00
|
|
|
if (is_src_)
|
2014-04-30 14:43:37 +02:00
|
|
|
memcpy (&source_address, out_addr, out_addrlen);
|
2014-06-03 10:39:09 +02:00
|
|
|
else
|
2014-04-30 14:43:37 +02:00
|
|
|
memcpy (&address, out_addr, out_addrlen);
|
2011-08-18 17:40:42 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to resolve the string as a NIC name.
|
2014-04-30 14:43:37 +02:00
|
|
|
int rc = resolve_nic_name (interface_, ipv6_, is_src_);
|
2014-06-03 10:39:09 +02:00
|
|
|
if (rc == 0 || errno != ENODEV)
|
2011-08-18 17:40:42 +02:00
|
|
|
return rc;
|
|
|
|
|
|
|
|
// 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
|
2014-06-03 10:39:09 +02:00
|
|
|
memset (&req, 0, sizeof req);
|
2011-08-18 17:40:42 +02:00
|
|
|
|
|
|
|
// Choose IPv4 or IPv6 protocol family. Note that IPv6 allows for
|
|
|
|
// IPv4-in-IPv6 addresses.
|
2013-01-31 20:47:45 +01:00
|
|
|
req.ai_family = ipv6_? AF_INET6: AF_INET;
|
2011-08-18 17:40:42 +02:00
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
2015-09-03 08:58:12 +01:00
|
|
|
#if defined AI_V4MAPPED && !defined ZMQ_HAVE_FREEBSD && !defined ZMQ_HAVE_DRAGONFLY
|
2012-03-25 23:51:51 +02:00
|
|
|
// 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.
|
|
|
|
// Note: While the AI_V4MAPPED flag is defined on FreeBSD system,
|
|
|
|
// it is not supported here. See libzmq issue #331.
|
2011-08-18 17:40:42 +02:00
|
|
|
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.
|
2012-08-24 16:38:46 -07:00
|
|
|
zmq_assert (res != NULL);
|
2014-06-03 10:39:09 +02:00
|
|
|
zmq_assert ((size_t) res->ai_addrlen <= sizeof address);
|
|
|
|
if (is_src_)
|
2014-04-30 14:43:37 +02:00
|
|
|
memcpy (&source_address, res->ai_addr, res->ai_addrlen);
|
2014-06-03 10:39:09 +02:00
|
|
|
else
|
2014-04-30 14:43:37 +02:00
|
|
|
memcpy (&address, res->ai_addr, res->ai_addrlen);
|
2011-08-18 17:40:42 +02:00
|
|
|
|
|
|
|
// Cleanup getaddrinfo after copying the possibly referenced result.
|
2012-08-24 16:38:46 -07:00
|
|
|
freeaddrinfo (res);
|
2011-08-18 17:40:42 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-04-30 14:43:37 +02:00
|
|
|
int zmq::tcp_address_t::resolve_hostname (const char *hostname_, bool ipv6_, bool is_src_)
|
2011-08-18 17:40:42 +02:00
|
|
|
{
|
|
|
|
// Set up the query.
|
2011-11-04 14:15:06 +01:00
|
|
|
#if defined ZMQ_HAVE_OPENVMS && defined __ia64 && __INITIAL_POINTER_SIZE == 64
|
|
|
|
__addrinfo64 req;
|
|
|
|
#else
|
2011-08-18 17:40:42 +02:00
|
|
|
addrinfo req;
|
2011-11-04 14:15:06 +01:00
|
|
|
#endif
|
2014-06-03 10:39:09 +02:00
|
|
|
memset (&req, 0, sizeof req);
|
2011-08-18 17:40:42 +02:00
|
|
|
|
|
|
|
// Choose IPv4 or IPv6 protocol family. Note that IPv6 allows for
|
|
|
|
// IPv4-in-IPv6 addresses.
|
2013-01-31 20:47:45 +01:00
|
|
|
req.ai_family = ipv6_? AF_INET6: AF_INET;
|
2011-08-18 17:40:42 +02:00
|
|
|
|
|
|
|
// 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;
|
2012-03-25 23:51:51 +02:00
|
|
|
|
2015-09-03 08:58:12 +01:00
|
|
|
#if defined AI_V4MAPPED && !defined ZMQ_HAVE_FREEBSD && !defined ZMQ_HAVE_DRAGONFLY
|
2012-03-25 23:51:51 +02:00
|
|
|
// 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.
|
|
|
|
// Note: While the AI_V4MAPPED flag is defined on FreeBSD system,
|
|
|
|
// it is not supported here. See libzmq issue #331.
|
2011-08-18 17:40:42 +02:00
|
|
|
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.
|
2011-11-04 14:15:06 +01:00
|
|
|
#if defined ZMQ_HAVE_OPENVMS && defined __ia64 && __INITIAL_POINTER_SIZE == 64
|
|
|
|
__addrinfo64 *res;
|
|
|
|
#else
|
2011-08-18 17:40:42 +02:00
|
|
|
addrinfo *res;
|
2011-11-04 14:15:06 +01:00
|
|
|
#endif
|
2014-06-03 10:39:09 +02:00
|
|
|
const int rc = getaddrinfo (hostname_, NULL, &req, &res);
|
2011-08-18 17:40:42 +02:00
|
|
|
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.
|
2014-06-03 10:39:09 +02:00
|
|
|
zmq_assert ((size_t) res->ai_addrlen <= sizeof address);
|
|
|
|
if (is_src_)
|
2014-04-30 14:43:37 +02:00
|
|
|
memcpy (&source_address, res->ai_addr, res->ai_addrlen);
|
2014-06-03 10:39:09 +02:00
|
|
|
else
|
2014-04-30 14:43:37 +02:00
|
|
|
memcpy (&address, res->ai_addr, res->ai_addrlen);
|
2012-04-13 13:26:57 +04:00
|
|
|
|
2011-08-18 17:40:42 +02:00
|
|
|
freeaddrinfo (res);
|
2012-04-13 13:26:57 +04:00
|
|
|
|
2011-08-18 17:40:42 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-05-03 13:59:37 +01:00
|
|
|
zmq::tcp_address_t::tcp_address_t () :
|
|
|
|
_has_src_addr (false)
|
2011-08-18 17:40:42 +02:00
|
|
|
{
|
2014-06-03 10:39:09 +02:00
|
|
|
memset (&address, 0, sizeof address);
|
|
|
|
memset (&source_address, 0, sizeof source_address);
|
2011-08-18 17:40:42 +02:00
|
|
|
}
|
|
|
|
|
2014-05-03 13:59:37 +01:00
|
|
|
zmq::tcp_address_t::tcp_address_t (const sockaddr *sa, socklen_t sa_len) :
|
|
|
|
_has_src_addr (false)
|
2012-04-18 23:42:11 +04:00
|
|
|
{
|
2014-06-03 10:39:09 +02:00
|
|
|
zmq_assert (sa && sa_len > 0);
|
|
|
|
|
|
|
|
memset (&address, 0, sizeof address);
|
|
|
|
memset (&source_address, 0, sizeof source_address);
|
|
|
|
if (sa->sa_family == AF_INET && sa_len >= (socklen_t) sizeof address.ipv4)
|
|
|
|
memcpy (&address.ipv4, sa, sizeof address.ipv4);
|
|
|
|
else
|
|
|
|
if (sa->sa_family == AF_INET6 && sa_len >= (socklen_t) sizeof address.ipv6)
|
|
|
|
memcpy (&address.ipv6, sa, sizeof address.ipv6);
|
2012-04-18 23:42:11 +04:00
|
|
|
}
|
|
|
|
|
2011-08-18 17:40:42 +02:00
|
|
|
zmq::tcp_address_t::~tcp_address_t ()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-04-30 14:43:37 +02:00
|
|
|
int zmq::tcp_address_t::resolve (const char *name_, bool local_, bool ipv6_, bool is_src_)
|
2011-08-18 17:40:42 +02:00
|
|
|
{
|
2014-04-30 14:43:37 +02:00
|
|
|
if (!is_src_) {
|
|
|
|
// Test the ';' to know if we have a source address in name_
|
|
|
|
const char *src_delimiter = strrchr (name_, ';');
|
|
|
|
if (src_delimiter) {
|
|
|
|
std::string src_name (name_, src_delimiter - name_);
|
2014-06-03 10:39:09 +02:00
|
|
|
const int rc = resolve (src_name.c_str (), local_, ipv6_, true);
|
2014-04-30 14:43:37 +02:00
|
|
|
if (rc != 0)
|
|
|
|
return -1;
|
|
|
|
name_ = src_delimiter + 1;
|
|
|
|
_has_src_addr = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-18 17:40:42 +02:00
|
|
|
// Find the ':' at end that separates address from the port number.
|
|
|
|
const char *delimiter = strrchr (name_, ':');
|
|
|
|
if (!delimiter) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
2014-04-30 14:43:37 +02:00
|
|
|
|
2011-08-18 17:40:42 +02:00
|
|
|
// Separate the address/port.
|
|
|
|
std::string addr_str (name_, delimiter - name_);
|
|
|
|
std::string port_str (delimiter + 1);
|
|
|
|
|
2014-06-25 14:37:54 +02:00
|
|
|
// Remove square brackets around the address, if any, as used in IPv6
|
2014-06-25 14:36:55 +02:00
|
|
|
if (addr_str.size () >= 2 && addr_str [0] == '[' &&
|
|
|
|
addr_str [addr_str.size () - 1] == ']')
|
|
|
|
addr_str = addr_str.substr (1, addr_str.size () - 2);
|
|
|
|
|
2012-03-27 06:26:39 +02:00
|
|
|
// Allow 0 specifically, to detect invalid port error in atoi if not
|
2013-01-31 20:47:45 +01:00
|
|
|
uint16_t port;
|
2012-03-27 06:45:03 +02:00
|
|
|
if (port_str == "*" || port_str == "0")
|
2012-03-27 06:26:39 +02:00
|
|
|
// Resolve wildcard to 0 to allow autoselection of port
|
2012-02-08 22:06:46 +00:00
|
|
|
port = 0;
|
2012-03-27 06:45:03 +02:00
|
|
|
else {
|
2012-02-08 22:06:46 +00:00
|
|
|
// Parse the port number (0 is not a valid port).
|
2012-03-27 06:26:39 +02:00
|
|
|
port = (uint16_t) atoi (port_str.c_str ());
|
2012-02-08 22:06:46 +00:00
|
|
|
if (port == 0) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
2011-08-18 17:40:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Resolve the IP address.
|
|
|
|
int rc;
|
|
|
|
if (local_)
|
2014-04-30 14:43:37 +02:00
|
|
|
rc = resolve_interface (addr_str.c_str (), ipv6_, is_src_);
|
2011-08-18 17:40:42 +02:00
|
|
|
else
|
2014-04-30 14:43:37 +02:00
|
|
|
rc = resolve_hostname (addr_str.c_str (), ipv6_, is_src_);
|
2011-08-18 17:40:42 +02:00
|
|
|
if (rc != 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
// Set the port into the address structure.
|
2014-04-30 14:43:37 +02:00
|
|
|
if (is_src_) {
|
|
|
|
if (source_address.generic.sa_family == AF_INET6)
|
|
|
|
source_address.ipv6.sin6_port = htons (port);
|
|
|
|
else
|
|
|
|
source_address.ipv4.sin_port = htons (port);
|
2014-06-03 10:39:09 +02:00
|
|
|
}
|
|
|
|
else {
|
2014-04-30 14:43:37 +02:00
|
|
|
if (address.generic.sa_family == AF_INET6)
|
|
|
|
address.ipv6.sin6_port = htons (port);
|
|
|
|
else
|
|
|
|
address.ipv4.sin_port = htons (port);
|
|
|
|
}
|
|
|
|
|
2011-08-18 17:40:42 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-04-18 23:42:11 +04:00
|
|
|
int zmq::tcp_address_t::to_string (std::string &addr_)
|
|
|
|
{
|
2013-01-31 20:47:45 +01:00
|
|
|
if (address.generic.sa_family != AF_INET
|
|
|
|
&& address.generic.sa_family != AF_INET6) {
|
2012-04-18 23:42:11 +04:00
|
|
|
addr_.clear ();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-06-24 14:31:28 +02:00
|
|
|
// Not using service resolv because of
|
|
|
|
// https://github.com/zeromq/libzmq/commit/1824574f9b5a8ce786853320e3ea09fe1f822bc4
|
2014-06-03 10:39:09 +02:00
|
|
|
char hbuf [NI_MAXHOST];
|
|
|
|
int rc = getnameinfo (addr (), addrlen (), hbuf, sizeof hbuf, NULL, 0, NI_NUMERICHOST);
|
2012-04-18 23:42:11 +04:00
|
|
|
if (rc != 0) {
|
|
|
|
addr_.clear ();
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (address.generic.sa_family == AF_INET6) {
|
|
|
|
std::stringstream s;
|
|
|
|
s << "tcp://[" << hbuf << "]:" << ntohs (address.ipv6.sin6_port);
|
|
|
|
addr_ = s.str ();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
std::stringstream s;
|
|
|
|
s << "tcp://" << hbuf << ":" << ntohs (address.ipv4.sin_port);
|
|
|
|
addr_ = s.str ();
|
2014-06-03 10:39:09 +02:00
|
|
|
}
|
2012-04-18 23:42:11 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-02-02 14:56:51 +01:00
|
|
|
const sockaddr *zmq::tcp_address_t::addr () const
|
2011-08-18 17:40:42 +02:00
|
|
|
{
|
|
|
|
return &address.generic;
|
|
|
|
}
|
|
|
|
|
2012-02-02 14:56:51 +01:00
|
|
|
socklen_t zmq::tcp_address_t::addrlen () const
|
2011-08-18 17:40:42 +02:00
|
|
|
{
|
|
|
|
if (address.generic.sa_family == AF_INET6)
|
2014-06-03 10:39:09 +02:00
|
|
|
return (socklen_t) sizeof address.ipv6;
|
2011-08-18 17:40:42 +02:00
|
|
|
else
|
2014-06-03 10:39:09 +02:00
|
|
|
return (socklen_t) sizeof address.ipv4;
|
2011-08-18 17:40:42 +02:00
|
|
|
}
|
|
|
|
|
2014-04-30 14:43:37 +02:00
|
|
|
const sockaddr *zmq::tcp_address_t::src_addr () const
|
|
|
|
{
|
|
|
|
return &source_address.generic;
|
|
|
|
}
|
|
|
|
|
|
|
|
socklen_t zmq::tcp_address_t::src_addrlen () const
|
|
|
|
{
|
|
|
|
if (address.generic.sa_family == AF_INET6)
|
2014-06-03 10:39:09 +02:00
|
|
|
return (socklen_t) sizeof source_address.ipv6;
|
2014-04-30 14:43:37 +02:00
|
|
|
else
|
2014-06-03 10:39:09 +02:00
|
|
|
return (socklen_t) sizeof source_address.ipv4;
|
2014-04-30 14:43:37 +02:00
|
|
|
}
|
|
|
|
|
2014-06-03 10:39:09 +02:00
|
|
|
bool zmq::tcp_address_t::has_src_addr () const
|
2014-04-30 14:43:37 +02:00
|
|
|
{
|
|
|
|
return _has_src_addr;
|
|
|
|
}
|
|
|
|
|
2011-08-18 17:58:46 +02:00
|
|
|
#if defined ZMQ_HAVE_WINDOWS
|
2012-02-02 14:56:51 +01:00
|
|
|
unsigned short zmq::tcp_address_t::family () const
|
2011-08-18 17:58:46 +02:00
|
|
|
#else
|
2012-02-02 14:56:51 +01:00
|
|
|
sa_family_t zmq::tcp_address_t::family () const
|
2011-08-18 17:58:46 +02:00
|
|
|
#endif
|
2011-08-18 17:40:42 +02:00
|
|
|
{
|
|
|
|
return address.generic.sa_family;
|
|
|
|
}
|
|
|
|
|
2012-04-12 18:37:14 +04:00
|
|
|
zmq::tcp_address_mask_t::tcp_address_mask_t () :
|
2014-06-03 10:39:09 +02:00
|
|
|
tcp_address_t (),
|
|
|
|
address_mask (-1)
|
2012-04-12 18:37:14 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-08-27 16:05:51 -07:00
|
|
|
int zmq::tcp_address_mask_t::mask () const
|
2012-04-12 18:37:14 +04:00
|
|
|
{
|
|
|
|
return address_mask;
|
|
|
|
}
|
|
|
|
|
2013-01-31 20:47:45 +01:00
|
|
|
int zmq::tcp_address_mask_t::resolve (const char *name_, bool ipv6_)
|
2012-04-12 18:37:14 +04:00
|
|
|
{
|
|
|
|
// Find '/' at the end that separates address from the cidr mask number.
|
2014-06-15 19:44:28 +02:00
|
|
|
// Allow empty mask clause and treat it like '/32' for ipv4 or '/128' for ipv6.
|
2012-04-13 13:26:57 +04:00
|
|
|
std::string addr_str, mask_str;
|
2012-04-12 18:37:14 +04:00
|
|
|
const char *delimiter = strrchr (name_, '/');
|
|
|
|
if (delimiter != NULL) {
|
|
|
|
addr_str.assign (name_, delimiter - name_);
|
|
|
|
mask_str.assign (delimiter + 1);
|
|
|
|
if (mask_str.empty ()) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2013-01-31 20:47:45 +01:00
|
|
|
else
|
2012-04-12 18:37:14 +04:00
|
|
|
addr_str.assign (name_);
|
|
|
|
|
|
|
|
// Parse address part using standard routines.
|
2014-06-03 10:39:09 +02:00
|
|
|
const int rc =
|
|
|
|
tcp_address_t::resolve_hostname (addr_str.c_str (), ipv6_);
|
2012-04-12 18:37:14 +04:00
|
|
|
if (rc != 0)
|
|
|
|
return rc;
|
|
|
|
|
|
|
|
// Parse the cidr mask number.
|
|
|
|
if (mask_str.empty ()) {
|
|
|
|
if (address.generic.sa_family == AF_INET6)
|
|
|
|
address_mask = 128;
|
|
|
|
else
|
|
|
|
address_mask = 32;
|
|
|
|
}
|
|
|
|
else
|
2014-06-03 10:39:09 +02:00
|
|
|
if (mask_str == "0")
|
2012-04-12 18:37:14 +04:00
|
|
|
address_mask = 0;
|
|
|
|
else {
|
2014-06-15 19:44:28 +02:00
|
|
|
const int mask = atoi (mask_str.c_str ());
|
2012-04-12 18:37:14 +04:00
|
|
|
if (
|
|
|
|
(mask < 1) ||
|
|
|
|
(address.generic.sa_family == AF_INET6 && mask > 128) ||
|
|
|
|
(address.generic.sa_family != AF_INET6 && mask > 32)
|
|
|
|
) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
address_mask = mask;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-04-18 23:42:11 +04:00
|
|
|
int zmq::tcp_address_mask_t::to_string (std::string &addr_)
|
|
|
|
{
|
2014-06-03 10:39:09 +02:00
|
|
|
if (address.generic.sa_family != AF_INET
|
|
|
|
&& address.generic.sa_family != AF_INET6) {
|
2012-04-18 23:42:11 +04:00
|
|
|
addr_.clear ();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (address_mask == -1) {
|
|
|
|
addr_.clear ();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-06-03 10:39:09 +02:00
|
|
|
char hbuf [NI_MAXHOST];
|
|
|
|
int rc = getnameinfo (addr (), addrlen (), hbuf, sizeof hbuf, NULL, 0, NI_NUMERICHOST);
|
2012-04-18 23:42:11 +04:00
|
|
|
if (rc != 0) {
|
|
|
|
addr_.clear ();
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (address.generic.sa_family == AF_INET6) {
|
|
|
|
std::stringstream s;
|
|
|
|
s << "[" << hbuf << "]/" << address_mask;
|
|
|
|
addr_ = s.str ();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
std::stringstream s;
|
|
|
|
s << hbuf << "/" << address_mask;
|
|
|
|
addr_ = s.str ();
|
2014-06-03 10:39:09 +02:00
|
|
|
}
|
2012-04-18 23:42:11 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-08-27 16:05:51 -07:00
|
|
|
bool zmq::tcp_address_mask_t::match_address (const struct sockaddr *ss, const socklen_t ss_len) const
|
2012-04-12 18:37:14 +04:00
|
|
|
{
|
2014-06-03 10:39:09 +02:00
|
|
|
zmq_assert (address_mask != -1
|
|
|
|
&& ss != NULL
|
|
|
|
&& ss_len >= (socklen_t) sizeof (struct sockaddr));
|
2012-04-12 18:37:14 +04:00
|
|
|
|
|
|
|
if (ss->sa_family != address.generic.sa_family)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (address_mask > 0) {
|
|
|
|
int mask;
|
2012-04-13 13:26:57 +04:00
|
|
|
const uint8_t *our_bytes, *their_bytes;
|
2012-04-12 18:37:14 +04:00
|
|
|
if (ss->sa_family == AF_INET6) {
|
|
|
|
zmq_assert (ss_len == sizeof (struct sockaddr_in6));
|
2012-04-13 13:26:57 +04:00
|
|
|
their_bytes = (const uint8_t *) &(((const struct sockaddr_in6 *) ss)->sin6_addr);
|
|
|
|
our_bytes = (const uint8_t *) &address.ipv6.sin6_addr;
|
2012-04-12 18:37:14 +04:00
|
|
|
mask = sizeof (struct in6_addr) * 8;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
zmq_assert (ss_len == sizeof (struct sockaddr_in));
|
2012-04-13 13:26:57 +04:00
|
|
|
their_bytes = (const uint8_t *) &(((const struct sockaddr_in *) ss)->sin_addr);
|
|
|
|
our_bytes = (const uint8_t *) &address.ipv4.sin_addr;
|
2012-04-12 18:37:14 +04:00
|
|
|
mask = sizeof (struct in_addr) * 8;
|
|
|
|
}
|
2014-06-03 10:39:09 +02:00
|
|
|
if (address_mask < mask)
|
|
|
|
mask = address_mask;
|
2012-04-12 18:37:14 +04:00
|
|
|
|
2014-06-03 10:39:09 +02:00
|
|
|
const size_t full_bytes = mask / 8;
|
|
|
|
if (memcmp (our_bytes, their_bytes, full_bytes))
|
2012-08-24 16:38:46 -07:00
|
|
|
return false;
|
2012-04-12 18:37:14 +04:00
|
|
|
|
2014-06-15 19:44:28 +02:00
|
|
|
const uint8_t last_byte_bits = 0xffU << (8 - mask % 8);
|
2012-04-12 18:37:14 +04:00
|
|
|
if (last_byte_bits) {
|
2014-06-03 10:39:09 +02:00
|
|
|
if ((their_bytes [full_bytes] & last_byte_bits) != (our_bytes [full_bytes] & last_byte_bits))
|
2012-04-12 18:37:14 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|