udp_address.cpp 6.12 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
/*
    Copyright (c) 2007-2016 Contributors as noted in the AUTHORS file

    This file is part of libzmq, the ZeroMQ core engine in C++.

    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
    (at your option) any later version.

    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.

    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/>.
*/

30
#include "precompiled.hpp"
31 32 33 34 35 36 37 38 39
#include <string>
#include <sstream>

#include "macros.hpp"
#include "udp_address.hpp"
#include "stdint.hpp"
#include "err.hpp"
#include "ip.hpp"

40
#ifndef ZMQ_HAVE_WINDOWS
41 42 43
#include <sys/types.h>
#include <arpa/inet.h>
#include <netdb.h>
44
#include <net/if.h>
45 46 47
#include <ctype.h>
#endif

48 49 50
zmq::udp_address_t::udp_address_t () :
    _bind_interface (-1),
    _is_multicast (false)
51
{
52 53
    _bind_address = ip_addr_t::any (AF_INET);
    _target_address = ip_addr_t::any (AF_INET);
54 55 56 57 58 59
}

zmq::udp_address_t::~udp_address_t ()
{
}

60
int zmq::udp_address_t::resolve (const char *name_, bool bind_, bool ipv6_)
61
{
62 63
    //  No IPv6 support yet
    bool has_interface = false;
64

65
    _address = name_;
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81

    //  If we have a semicolon then we should have an interface specifier in the
    //  URL
    const char *src_delimiter = strrchr (name_, ';');
    if (src_delimiter) {
        std::string src_name (name_, src_delimiter - name_);

        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)
82
          .ipv6 (ipv6_)
83 84 85 86
          .expect_port (false);

        ip_resolver_t src_resolver (src_resolver_opts);

87
        const int rc = src_resolver.resolve (&_bind_address, src_name.c_str ());
88 89 90 91 92

        if (rc != 0) {
            return -1;
        }

93
        if (_bind_address.is_multicast ()) {
94 95 96 97 98
            //  It doesn't make sense to have a multicast address as a source
            errno = EINVAL;
            return -1;
        }

99 100 101 102 103 104
        //  This is a hack because we need the interface index when binding
        //  multicast IPv6, we can't do it by address. Unfortunately for the
        //  time being we don't have a generic platform-independent function to
        //  resolve an interface index from an address, so we only support it
        //  when an actual interface name is provided.
        if (src_name == "*") {
105
            _bind_interface = 0;
106
        } else {
107 108
            _bind_interface = if_nametoindex (src_name.c_str ());
            if (_bind_interface == 0) {
109
                //  Error, probably not an interface name.
110
                _bind_interface = -1;
111 112 113
            }
        }

114 115 116 117
        has_interface = true;
        name_ = src_delimiter + 1;
    }

118
    ip_resolver_options_t resolver_opts;
119

120 121 122 123
    resolver_opts.bindable (bind_)
      .allow_dns (!bind_)
      .allow_nic_name (bind_)
      .expect_port (true)
124
      .ipv6 (ipv6_);
125

126 127
    ip_resolver_t resolver (resolver_opts);

128
    int rc = resolver.resolve (&_target_address, name_);
129
    if (rc != 0) {
130 131 132
        return -1;
    }

133 134
    _is_multicast = _target_address.is_multicast ();
    uint16_t port = _target_address.port ();
135 136 137 138

    if (has_interface) {
        //  If we have an interface specifier then the target address must be a
        //  multicast address
139
        if (!_is_multicast) {
140 141 142 143
            errno = EINVAL;
            return -1;
        }

144
        _bind_address.set_port (port);
145 146 147 148 149 150
    } else {
        //  If we don't have an explicit interface specifier then the URL is
        //  ambiguous: if the target address is multicast then it's the
        //  destination address and the bind address is ANY, if it's unicast
        //  then it's the bind address when 'bind_' is true and the destination
        //  otherwise
151 152 153 154
        if (_is_multicast || !bind_) {
            _bind_address = ip_addr_t::any (_target_address.family ());
            _bind_address.set_port (port);
            _bind_interface = 0;
155
        } else {
156 157 158
            //  If we were asked for a bind socket and the address
            //  provided was not multicast then it was really meant as
            //  a bind address and the target_address is useless.
159
            _bind_address = _target_address;
160
        }
161 162
    }

163
    if (_bind_address.family () != _target_address.family ()) {
164 165
        errno = EINVAL;
        return -1;
166
    }
167

168 169
    //  For IPv6 multicast we *must* have an interface index since we can't
    //  bind by address.
170
    if (ipv6_ && _is_multicast && _bind_interface < 0) {
171 172 173
        errno = ENODEV;
        return -1;
    }
174 175 176 177

    return 0;
}

178
int zmq::udp_address_t::family () const
179
{
180
    return _bind_address.family ();
181 182 183 184
}

bool zmq::udp_address_t::is_mcast () const
{
185
    return _is_multicast;
186 187
}

188
const zmq::ip_addr_t *zmq::udp_address_t::bind_addr () const
189
{
190
    return &_bind_address;
191 192
}

193
int zmq::udp_address_t::bind_if () const
194
{
195
    return _bind_interface;
196 197
}

198
const zmq::ip_addr_t *zmq::udp_address_t::target_addr () const
199
{
200
    return &_target_address;
201 202
}

203
int zmq::udp_address_t::to_string (std::string &addr_)
204
{
205
    // XXX what do (factor TCP code?)
206
    addr_ = _address;
207
    return 0;
208
}