2011-08-18 17:40:42 +02:00
|
|
|
/*
|
2016-01-28 15:07:31 +01:00
|
|
|
Copyright (c) 2007-2016 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/>.
|
|
|
|
*/
|
|
|
|
|
2016-02-18 10:56:52 -06:00
|
|
|
#include "precompiled.hpp"
|
2011-08-18 17:40:42 +02:00
|
|
|
#include <string>
|
|
|
|
|
2015-08-21 16:12:22 -07:00
|
|
|
#include "macros.hpp"
|
2011-08-18 17:40:42 +02:00
|
|
|
#include "tcp_address.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
|
|
|
|
2016-02-12 11:01:52 +01:00
|
|
|
#ifndef ZMQ_HAVE_WINDOWS
|
2011-08-18 17:40:42 +02:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <netinet/tcp.h>
|
2016-02-12 11:01:52 +01:00
|
|
|
#include <net/if.h>
|
2011-08-18 17:40:42 +02:00
|
|
|
#include <netdb.h>
|
2016-01-21 13:44:01 +06:00
|
|
|
#include <ctype.h>
|
2016-02-12 11:01:52 +01:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
2011-08-18 17:40:42 +02:00
|
|
|
#endif
|
|
|
|
|
2018-05-28 09:58:39 +02:00
|
|
|
#include <limits.h>
|
|
|
|
|
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
|
|
|
{
|
2018-05-27 11:10:39 +02:00
|
|
|
memset (&_address, 0, sizeof (_address));
|
|
|
|
memset (&_source_address, 0, sizeof (_source_address));
|
2011-08-18 17:40:42 +02:00
|
|
|
}
|
|
|
|
|
2018-05-24 17:58:30 +02:00
|
|
|
zmq::tcp_address_t::tcp_address_t (const sockaddr *sa_, socklen_t sa_len_) :
|
2014-05-03 13:59:37 +01:00
|
|
|
_has_src_addr (false)
|
2012-04-18 23:42:11 +04:00
|
|
|
{
|
2018-05-24 17:58:30 +02:00
|
|
|
zmq_assert (sa_ && sa_len_ > 0);
|
2014-06-03 10:39:09 +02:00
|
|
|
|
2018-05-27 11:10:39 +02:00
|
|
|
memset (&_address, 0, sizeof (_address));
|
|
|
|
memset (&_source_address, 0, sizeof (_source_address));
|
2018-05-24 17:58:30 +02:00
|
|
|
if (sa_->sa_family == AF_INET
|
2018-05-27 11:10:39 +02:00
|
|
|
&& sa_len_ >= static_cast<socklen_t> (sizeof (_address.ipv4)))
|
|
|
|
memcpy (&_address.ipv4, sa_, sizeof (_address.ipv4));
|
2018-05-24 17:58:30 +02:00
|
|
|
else if (sa_->sa_family == AF_INET6
|
2018-05-27 11:10:39 +02:00
|
|
|
&& sa_len_ >= static_cast<socklen_t> (sizeof (_address.ipv6)))
|
|
|
|
memcpy (&_address.ipv6, sa_, sizeof (_address.ipv6));
|
2012-04-18 23:42:11 +04:00
|
|
|
}
|
|
|
|
|
2018-04-30 16:56:17 +02:00
|
|
|
int zmq::tcp_address_t::resolve (const char *name_, bool local_, bool ipv6_)
|
2011-08-18 17:40:42 +02:00
|
|
|
{
|
2018-04-30 16:56:17 +02:00
|
|
|
// Test the ';' to know if we have a source address in name_
|
|
|
|
const char *src_delimiter = strrchr (name_, ';');
|
|
|
|
if (src_delimiter) {
|
2019-12-25 13:51:21 +01:00
|
|
|
const std::string src_name (name_, src_delimiter - name_);
|
2018-04-30 16:56:17 +02:00
|
|
|
|
|
|
|
ip_resolver_options_t src_resolver_opts;
|
|
|
|
|
|
|
|
src_resolver_opts
|
|
|
|
.bindable (true)
|
|
|
|
// Restrict hostname/service to literals to avoid any DNS
|
|
|
|
// lookups or service-name irregularity due to
|
|
|
|
// indeterminate socktype.
|
|
|
|
.allow_dns (false)
|
|
|
|
.allow_nic_name (true)
|
|
|
|
.ipv6 (ipv6_)
|
|
|
|
.expect_port (true);
|
|
|
|
|
|
|
|
ip_resolver_t src_resolver (src_resolver_opts);
|
|
|
|
|
|
|
|
const int rc =
|
2018-05-27 11:10:39 +02:00
|
|
|
src_resolver.resolve (&_source_address, src_name.c_str ());
|
2018-04-30 16:56:17 +02:00
|
|
|
if (rc != 0)
|
2015-12-09 16:26:34 -06:00
|
|
|
return -1;
|
2018-04-30 16:56:17 +02:00
|
|
|
name_ = src_delimiter + 1;
|
|
|
|
_has_src_addr = true;
|
2015-12-09 16:26:34 -06:00
|
|
|
}
|
|
|
|
|
2018-04-30 16:56:17 +02:00
|
|
|
ip_resolver_options_t resolver_opts;
|
2011-08-18 17:40:42 +02:00
|
|
|
|
2018-04-30 16:56:17 +02:00
|
|
|
resolver_opts.bindable (local_)
|
|
|
|
.allow_dns (!local_)
|
|
|
|
.allow_nic_name (local_)
|
|
|
|
.ipv6 (ipv6_)
|
|
|
|
.expect_port (true);
|
2011-08-18 17:40:42 +02:00
|
|
|
|
2018-04-30 16:56:17 +02:00
|
|
|
ip_resolver_t resolver (resolver_opts);
|
2014-04-30 14:43:37 +02:00
|
|
|
|
2018-05-27 11:10:39 +02:00
|
|
|
return resolver.resolve (&_address, name_);
|
2011-08-18 17:40:42 +02:00
|
|
|
}
|
|
|
|
|
2019-02-05 04:02:55 -05:00
|
|
|
template <size_t N1, size_t N2>
|
2019-12-08 14:21:43 +01:00
|
|
|
static std::string make_address_string (const char *hbuf_,
|
|
|
|
uint16_t port_,
|
|
|
|
const char (&ipv6_prefix_)[N1],
|
|
|
|
const char (&ipv6_suffix_)[N2])
|
2019-02-05 04:02:55 -05:00
|
|
|
{
|
|
|
|
const size_t max_port_str_length = 5;
|
2019-12-08 14:21:43 +01:00
|
|
|
char buf[NI_MAXHOST + sizeof ipv6_prefix_ + sizeof ipv6_suffix_
|
2019-02-05 04:02:55 -05:00
|
|
|
+ max_port_str_length];
|
|
|
|
char *pos = buf;
|
2019-12-08 14:21:43 +01:00
|
|
|
memcpy (pos, ipv6_prefix_, sizeof ipv6_prefix_ - 1);
|
|
|
|
pos += sizeof ipv6_prefix_ - 1;
|
|
|
|
const size_t hbuf_len = strlen (hbuf_);
|
|
|
|
memcpy (pos, hbuf_, hbuf_len);
|
2019-02-05 04:02:55 -05:00
|
|
|
pos += hbuf_len;
|
2019-12-08 14:21:43 +01:00
|
|
|
memcpy (pos, ipv6_suffix_, sizeof ipv6_suffix_ - 1);
|
|
|
|
pos += sizeof ipv6_suffix_ - 1;
|
|
|
|
pos += sprintf (pos, "%d", ntohs (port_));
|
2019-02-05 04:02:55 -05:00
|
|
|
return std::string (buf, pos - buf);
|
|
|
|
}
|
|
|
|
|
2018-05-30 10:28:14 +02:00
|
|
|
int zmq::tcp_address_t::to_string (std::string &addr_) const
|
2012-04-18 23:42:11 +04:00
|
|
|
{
|
2018-05-27 11:10:39 +02:00
|
|
|
if (_address.family () != AF_INET && _address.family () != AF_INET6) {
|
2012-04-18 23:42:11 +04:00
|
|
|
addr_.clear ();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-09-06 18:46:32 +02:00
|
|
|
// Not using service resolving because of
|
2014-06-24 14:31:28 +02:00
|
|
|
// https://github.com/zeromq/libzmq/commit/1824574f9b5a8ce786853320e3ea09fe1f822bc4
|
2014-06-03 10:39:09 +02:00
|
|
|
char hbuf[NI_MAXHOST];
|
2018-05-30 10:28:14 +02:00
|
|
|
const 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;
|
|
|
|
}
|
|
|
|
|
2019-02-05 04:02:55 -05:00
|
|
|
const char ipv4_prefix[] = "tcp://";
|
|
|
|
const char ipv4_suffix[] = ":";
|
|
|
|
const char ipv6_prefix[] = "tcp://[";
|
|
|
|
const char ipv6_suffix[] = "]:";
|
2018-05-27 11:10:39 +02:00
|
|
|
if (_address.family () == AF_INET6) {
|
2019-02-05 04:02:55 -05:00
|
|
|
addr_ = make_address_string (hbuf, _address.ipv6.sin6_port, ipv6_prefix,
|
|
|
|
ipv6_suffix);
|
2012-04-18 23:42:11 +04:00
|
|
|
} else {
|
2019-02-05 04:02:55 -05:00
|
|
|
addr_ = make_address_string (hbuf, _address.ipv4.sin_port, ipv4_prefix,
|
|
|
|
ipv4_suffix);
|
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
|
|
|
{
|
2018-05-30 10:28:14 +02:00
|
|
|
return _address.as_sockaddr ();
|
2011-08-18 17:40:42 +02:00
|
|
|
}
|
|
|
|
|
2012-02-02 14:56:51 +01:00
|
|
|
socklen_t zmq::tcp_address_t::addrlen () const
|
2011-08-18 17:40:42 +02:00
|
|
|
{
|
2018-05-30 10:28:14 +02:00
|
|
|
return _address.sockaddr_len ();
|
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
|
|
|
|
{
|
2018-05-30 10:28:14 +02:00
|
|
|
return _source_address.as_sockaddr ();
|
2014-04-30 14:43:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
socklen_t zmq::tcp_address_t::src_addrlen () const
|
|
|
|
{
|
2018-05-30 10:28:14 +02:00
|
|
|
return _source_address.sockaddr_len ();
|
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
|
|
|
{
|
2018-05-27 11:10:39 +02:00
|
|
|
return _address.family ();
|
2011-08-18 17:40:42 +02:00
|
|
|
}
|
|
|
|
|
2018-05-30 10:28:14 +02:00
|
|
|
zmq::tcp_address_mask_t::tcp_address_mask_t () : _address_mask (-1)
|
2012-04-12 18:37:14 +04:00
|
|
|
{
|
2018-05-30 10:28:14 +02:00
|
|
|
memset (&_network_address, 0, sizeof (_network_address));
|
2012-04-12 18:37:14 +04:00
|
|
|
}
|
|
|
|
|
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.
|
2018-04-30 16:56:17 +02:00
|
|
|
ip_resolver_options_t resolver_opts;
|
|
|
|
|
|
|
|
resolver_opts.bindable (false)
|
|
|
|
.allow_dns (false)
|
|
|
|
.allow_nic_name (false)
|
|
|
|
.ipv6 (ipv6_)
|
|
|
|
.expect_port (false);
|
|
|
|
|
|
|
|
ip_resolver_t resolver (resolver_opts);
|
|
|
|
|
2018-05-30 10:28:14 +02:00
|
|
|
const int rc = resolver.resolve (&_network_address, addr_str.c_str ());
|
2012-04-12 18:37:14 +04:00
|
|
|
if (rc != 0)
|
|
|
|
return rc;
|
|
|
|
|
|
|
|
// Parse the cidr mask number.
|
2018-05-30 10:28:14 +02:00
|
|
|
const int full_mask_ipv4 =
|
|
|
|
sizeof (_network_address.ipv4.sin_addr) * CHAR_BIT;
|
|
|
|
const int full_mask_ipv6 =
|
|
|
|
sizeof (_network_address.ipv6.sin6_addr) * CHAR_BIT;
|
2012-04-12 18:37:14 +04:00
|
|
|
if (mask_str.empty ()) {
|
2018-05-30 10:28:14 +02:00
|
|
|
_address_mask = _network_address.family () == AF_INET6 ? full_mask_ipv6
|
|
|
|
: full_mask_ipv4;
|
2014-06-03 10:39:09 +02:00
|
|
|
} else if (mask_str == "0")
|
2018-05-27 11:10:39 +02:00
|
|
|
_address_mask = 0;
|
2012-04-12 18:37:14 +04:00
|
|
|
else {
|
2018-05-30 10:35:01 +02:00
|
|
|
const long mask = strtol (mask_str.c_str (), NULL, 10);
|
2018-05-28 09:58:39 +02:00
|
|
|
if ((mask < 1)
|
2018-05-30 10:28:14 +02:00
|
|
|
|| (_network_address.family () == AF_INET6 && mask > full_mask_ipv6)
|
|
|
|
|| (_network_address.family () != AF_INET6
|
|
|
|
&& mask > full_mask_ipv4)) {
|
2012-04-12 18:37:14 +04:00
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
2018-05-30 10:35:01 +02:00
|
|
|
_address_mask = static_cast<int> (mask);
|
2012-04-12 18:37:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-05-24 17:58:30 +02: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
|
|
|
{
|
2018-05-27 11:10:39 +02:00
|
|
|
zmq_assert (_address_mask != -1 && ss_ != NULL
|
2018-05-30 10:28:14 +02:00
|
|
|
&& ss_len_
|
|
|
|
>= static_cast<socklen_t> (sizeof (struct sockaddr)));
|
2012-04-12 18:37:14 +04:00
|
|
|
|
2018-05-30 10:28:14 +02:00
|
|
|
if (ss_->sa_family != _network_address.generic.sa_family)
|
2012-04-12 18:37:14 +04:00
|
|
|
return false;
|
|
|
|
|
2018-05-27 11:10:39 +02:00
|
|
|
if (_address_mask > 0) {
|
2012-04-12 18:37:14 +04:00
|
|
|
int mask;
|
2012-04-13 13:26:57 +04:00
|
|
|
const uint8_t *our_bytes, *their_bytes;
|
2018-05-24 17:58:30 +02:00
|
|
|
if (ss_->sa_family == AF_INET6) {
|
|
|
|
zmq_assert (ss_len_ == sizeof (struct sockaddr_in6));
|
|
|
|
their_bytes = reinterpret_cast<const uint8_t *> (
|
|
|
|
&((reinterpret_cast<const struct sockaddr_in6 *> (ss_))
|
|
|
|
->sin6_addr));
|
2018-05-30 10:28:14 +02:00
|
|
|
our_bytes = reinterpret_cast<const uint8_t *> (
|
|
|
|
&_network_address.ipv6.sin6_addr);
|
2012-04-12 18:37:14 +04:00
|
|
|
mask = sizeof (struct in6_addr) * 8;
|
|
|
|
} else {
|
2018-05-24 17:58:30 +02:00
|
|
|
zmq_assert (ss_len_ == sizeof (struct sockaddr_in));
|
|
|
|
their_bytes = reinterpret_cast<const uint8_t *> (&(
|
|
|
|
(reinterpret_cast<const struct sockaddr_in *> (ss_))->sin_addr));
|
2018-05-30 10:28:14 +02:00
|
|
|
our_bytes = reinterpret_cast<const uint8_t *> (
|
|
|
|
&_network_address.ipv4.sin_addr);
|
2012-04-12 18:37:14 +04:00
|
|
|
mask = sizeof (struct in_addr) * 8;
|
|
|
|
}
|
2018-05-27 11:10:39 +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;
|
2018-08-09 17:30:17 +02:00
|
|
|
if (memcmp (our_bytes, their_bytes, full_bytes) != 0)
|
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;
|
|
|
|
}
|