tcp_address.cpp 17.4 KB
Newer Older
1
/*
Martin Sustrik's avatar
Martin Sustrik committed
2
    Copyright (c) 2009-2011 250bpm s.r.o.
3
    Copyright (c) 2007-2009 iMatix Corporation
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
    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>
23
#include <sstream>
24 25 26

#include "tcp_address.hpp"
#include "platform.hpp"
Martin Sustrik's avatar
Martin Sustrik committed
27
#include "stdint.hpp"
28
#include "err.hpp"
29
#include "ip.hpp"
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51

#ifdef ZMQ_HAVE_WINDOWS
#include "windows.hpp"
#else
#include <sys/types.h>
#include <arpa/inet.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>
52
#include <stdlib.h>
53 54

//  On Solaris platform, network interface name can be queried by ioctl.
55
int zmq::tcp_address_t::resolve_nic_name (const char *nic_, bool ipv4only_)
56 57 58 59 60
{
    //  TODO: Unused parameter, IPv6 support not implemented for Solaris.
    (void) ipv4only_;

    //  Create a socket.
61
    int fd = open_socket (AF_INET, SOCK_DGRAM, 0);
62
    errno_assert (fd != -1);
63 64 65 66 67 68

    //  Retrieve number of interfaces.
    lifnum ifn;
    ifn.lifn_family = AF_INET;
    ifn.lifn_flags = 0;
    int rc = ioctl (fd, SIOCGLIFNUM, (char*) &ifn);
69
    errno_assert (rc != -1);
70 71 72 73 74

    //  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);
75

76 77 78 79 80 81 82
    //  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);
83
    errno_assert (rc != -1);
84 85 86 87 88 89

    //  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 ++) {
90
        if (!strcmp (nic_, ifrp->lifr_name)) {
91
            rc = ioctl (fd, SIOCGLIFADDR, (char*) ifrp);
92
            errno_assert (rc != -1);
93
            if (ifrp->lifr_addr.ss_family == AF_INET) {
94
                address.ipv4 = *(sockaddr_in*) &ifrp->lifr_addr;
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
                found = true;
                break;
            }
        }
    }

    //  Clean-up.
    free (ifr);
    close (fd);

    if (!found) {
        errno = ENODEV;
        return -1;
    }

    return 0;
}

113
#elif defined ZMQ_HAVE_AIX || defined ZMQ_HAVE_HPUX || defined ZMQ_HAVE_ANDROID
114 115 116 117 118 119

#include <sys/types.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <net/if.h>

120
int zmq::tcp_address_t::resolve_nic_name (const char *nic_, bool ipv4only_)
121 122 123 124 125
{
    //  TODO: Unused parameter, IPv6 support not implemented for AIX or HP/UX.
    (void) ipv4only_;

    //  Create a socket.
126
    int sd = open_socket (AF_INET, SOCK_DGRAM, 0);
127
    errno_assert (sd != -1);
128

129
    struct ifreq ifr;
130 131

    //  Copy interface name for ioctl get.
132
    strncpy (ifr.ifr_name, nic_, sizeof (ifr.ifr_name));
133 134 135 136 137 138 139 140 141 142 143 144

    //  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;
    }

145 146
    memcpy (&address.ipv4.sin_addr, &((sockaddr_in*) &ifr.ifr_addr)->sin_addr,
        sizeof (in_addr));
147

148
    return 0;
149 150 151 152 153 154 155 156 157 158 159
}

#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.
160
int zmq::tcp_address_t::resolve_nic_name (const char *nic_, bool ipv4only_)
161 162 163 164
{
    //  Get the addresses.
    ifaddrs* ifa = NULL;
    int rc = getifaddrs (&ifa);
165
    errno_assert (rc == 0);
166 167 168 169 170 171 172 173 174 175 176 177 178
    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))
179
            && !strcmp (nic_, ifp->ifa_name))
180
        {
181
            memcpy (&address, ifp->ifa_addr,
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
                    (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.
204
int zmq::tcp_address_t::resolve_nic_name (const char *nic_, bool ipv4only_)
205 206
{
    //  All unused parameters.
207
    (void) nic_;
208 209 210 211 212 213 214 215
    (void) ipv4only_;

    errno = ENODEV;
    return -1;
}

#endif

216
int zmq::tcp_address_t::resolve_interface (const char *interface_,
217 218 219 220
    bool ipv4only_)
{
    //  Initialize temporary output pointers with storage address.
    sockaddr_storage ss;
221 222
    sockaddr *out_addr = (sockaddr*) &ss;
    size_t out_addrlen;
223 224 225 226 227 228 229 230

    //  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);
231
        out_addrlen = sizeof ip4_addr;
232
        memcpy (out_addr, &ip4_addr, out_addrlen);
233 234
    }
    else {
235 236 237 238
        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));
239
        out_addrlen = sizeof ip6_addr;
240 241 242 243 244
        memcpy (out_addr, &ip6_addr, out_addrlen);
    }

    //  * resolves to INADDR_ANY or in6addr_any.
    if (strcmp (interface_, "*") == 0) {
245
        zmq_assert (out_addrlen <= sizeof address);
246 247 248 249 250
        memcpy (&address, out_addr, out_addrlen);
        return 0;
    }

    //  Try to resolve the string as a NIC name.
251
    int rc = resolve_nic_name (interface_, ipv4only_);
252 253
    if (rc != 0 && errno != ENODEV)
        return rc;
254
    if (rc == 0)
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
        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;

278 279 280 281 282 283
#if defined AI_V4MAPPED && !defined ZMQ_HAVE_FREEBSD
    //  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.
284 285 286 287 288 289 290 291 292 293 294 295 296
    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.
297
    zmq_assert (res != NULL);
298 299 300 301
    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.
302
    freeaddrinfo (res);
303 304 305 306 307 308 309

    return 0;
}

int zmq::tcp_address_t::resolve_hostname (const char *hostname_, bool ipv4only_)
{
    //  Set up the query.
310 311 312
#if defined ZMQ_HAVE_OPENVMS && defined __ia64 && __INITIAL_POINTER_SIZE == 64
    __addrinfo64 req;
#else
313
    addrinfo req;
314
#endif
315 316 317 318 319 320 321 322 323
    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;
324 325 326 327 328 329 330

#if defined AI_V4MAPPED && !defined ZMQ_HAVE_FREEBSD
    //  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.
331 332 333 334 335 336
    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.
337 338 339
#if defined ZMQ_HAVE_OPENVMS && defined __ia64 && __INITIAL_POINTER_SIZE == 64
    __addrinfo64 *res;
#else
340
    addrinfo *res;
341
#endif
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
    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);
358

359
    freeaddrinfo (res);
360

361 362 363 364 365 366 367 368
    return 0;
}

zmq::tcp_address_t::tcp_address_t ()
{
    memset (&address, 0, sizeof (address));
}

369 370 371 372 373
zmq::tcp_address_t::tcp_address_t (const sockaddr *sa, socklen_t sa_len)
{
    zmq_assert(sa && sa_len > 0);

    memset (&address, 0, sizeof (address));
374
    if (sa->sa_family == AF_INET && sa_len >= (socklen_t) sizeof (address.ipv4))
375
        memcpy(&address.ipv4, sa, sizeof (address.ipv4));
376
    else 
377
    if (sa->sa_family == AF_INET6 && sa_len >= (socklen_t) sizeof (address.ipv6))
378 379 380
        memcpy(&address.ipv6, sa, sizeof (address.ipv6));
}

381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
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);

398
    //  Remove square brackets around the address, if any.
399
    if (addr_str.size () >= 2 && addr_str [0] == '[' &&
400 401 402
          addr_str [addr_str.size () - 1] == ']')
        addr_str = addr_str.substr (1, addr_str.size () - 2);

403
    uint16_t port;
404
    //  Allow 0 specifically, to detect invalid port error in atoi if not
405
    if (port_str == "*" || port_str == "0")
406
        //  Resolve wildcard to 0 to allow autoselection of port
407
        port = 0;
408
    else {
409
        //  Parse the port number (0 is not a valid port).
410
        port = (uint16_t) atoi (port_str.c_str ());
411 412 413 414
        if (port == 0) {
            errno = EINVAL;
            return -1;
        }
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
    }

    //  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;
}

435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
int zmq::tcp_address_t::to_string (std::string &addr_)
{
    if (address.generic.sa_family != AF_INET && address.generic.sa_family != AF_INET6) {
        addr_.clear ();
        return -1;
    }

    // not using service resolv because of https://github.com/zeromq/libzmq/commit/1824574f9b5a8ce786853320e3ea09fe1f822bc4
    char hbuf[NI_MAXHOST];
    int rc = getnameinfo (addr (), addrlen (), hbuf, sizeof (hbuf), NULL, 0, NI_NUMERICHOST);
    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 ();
    };
    return 0;
}

463
const sockaddr *zmq::tcp_address_t::addr () const
464 465 466 467
{
    return &address.generic;
}

468
socklen_t zmq::tcp_address_t::addrlen () const
469 470 471 472 473 474 475
{
    if (address.generic.sa_family == AF_INET6)
        return (socklen_t) sizeof (address.ipv6);
    else
        return (socklen_t) sizeof (address.ipv4);
}

Martin Sustrik's avatar
Martin Sustrik committed
476
#if defined ZMQ_HAVE_WINDOWS
477
unsigned short zmq::tcp_address_t::family () const
Martin Sustrik's avatar
Martin Sustrik committed
478
#else
479
sa_family_t zmq::tcp_address_t::family () const
Martin Sustrik's avatar
Martin Sustrik committed
480
#endif
481 482 483 484
{
    return address.generic.sa_family;
}

485 486 487 488 489 490
zmq::tcp_address_mask_t::tcp_address_mask_t () :
    tcp_address_t ()
{
    address_mask = -1;
}

491
int zmq::tcp_address_mask_t::mask () const
492 493 494 495 496 497 498 499
{
    return address_mask;
}

int zmq::tcp_address_mask_t::resolve (const char *name_, bool ipv4only_)
{
    // Find '/' at the end that separates address from the cidr mask number.
    // Allow empty mask clause and threat it like '/32' for ipv4 or '/128' for ipv6.
500
    std::string addr_str, mask_str;
501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545
    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;
        }
    }
    else {
        addr_str.assign (name_);
    }

    // Parse address part using standard routines.
    int rc = tcp_address_t::resolve_hostname (addr_str.c_str (), ipv4only_);
    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
    if (mask_str == "0") {
        address_mask = 0;
    }
    else {
        int mask = atoi (mask_str.c_str ());
        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;
}

546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576
int zmq::tcp_address_mask_t::to_string (std::string &addr_)
{
    if (address.generic.sa_family != AF_INET && address.generic.sa_family != AF_INET6) {
        addr_.clear ();
        return -1;
    }
    if (address_mask == -1) {
        addr_.clear ();
        return -1;
    }

    char hbuf[NI_MAXHOST];
    int rc = getnameinfo (addr (), addrlen (), hbuf, sizeof (hbuf), NULL, 0, NI_NUMERICHOST);
    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 ();
    };
    return 0;
}

577
bool zmq::tcp_address_mask_t::match_address (const struct sockaddr *ss, const socklen_t ss_len) const
578
{
579
    zmq_assert (address_mask != -1 && ss != NULL && ss_len >= (socklen_t) sizeof (struct sockaddr));
580 581 582 583 584 585

    if (ss->sa_family != address.generic.sa_family)
        return false;

    if (address_mask > 0) {
        int mask;
586
        const uint8_t *our_bytes, *their_bytes;
587 588
        if (ss->sa_family == AF_INET6) {
            zmq_assert (ss_len == sizeof (struct sockaddr_in6));
589 590
            their_bytes = (const uint8_t *) &(((const struct sockaddr_in6 *) ss)->sin6_addr);
            our_bytes = (const uint8_t *) &address.ipv6.sin6_addr;
591 592 593 594
            mask = sizeof (struct in6_addr) * 8;
        }
        else {
            zmq_assert (ss_len == sizeof (struct sockaddr_in));
595 596
            their_bytes = (const uint8_t *) &(((const struct sockaddr_in *) ss)->sin_addr);
            our_bytes = (const uint8_t *) &address.ipv4.sin_addr;
597 598 599 600
            mask = sizeof (struct in_addr) * 8;
        }
        if (address_mask < mask) mask = address_mask;

601 602 603
        size_t full_bytes = mask / 8;
        if (memcmp(our_bytes, their_bytes, full_bytes))
            return false;
604

605
        uint8_t last_byte_bits = (0xffU << (8 - (mask % 8))) & 0xffU;
606 607 608 609 610 611 612 613
        if (last_byte_bits) {
            if ((their_bytes[full_bytes] & last_byte_bits) != (our_bytes[full_bytes] & last_byte_bits))
                return false;
        }
    }

    return true;
}