/* 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 . */ #include "precompiled.hpp" #include #include #include "macros.hpp" #include "udp_address.hpp" #include "stdint.hpp" #include "err.hpp" #include "ip.hpp" #ifndef ZMQ_HAVE_WINDOWS #include #include #include #include #endif #include "ip_resolver.hpp" zmq::udp_address_t::udp_address_t () : is_multicast (false) { memset (&bind_address, 0, sizeof bind_address); memset (&dest_address, 0, sizeof dest_address); } zmq::udp_address_t::~udp_address_t () { } int zmq::udp_address_t::resolve (const char *name_, bool bind_) { ip_resolver_options_t resolver_opts; resolver_opts.bindable (bind_) .allow_dns (!bind_) .allow_nic_name (bind_) .expect_port (true) .ipv6 (false); ip_resolver_t resolver (resolver_opts); ip_addr_t addr; int rc = resolver.resolve (&addr, name_); if (rc != 0) { return -1; } if (addr.generic.sa_family != AF_INET) { // Shouldn't happen return -1; } dest_address = addr.ipv4; // we will check only first byte of IP // and if it from 224 to 239, then it can // represent multicast IP. int i = dest_address.sin_addr.s_addr & 0xFF; if (i >= 224 && i <= 239) { multicast = dest_address.sin_addr; is_multicast = true; } else is_multicast = false; iface.s_addr = htonl (INADDR_ANY); if (iface.s_addr == INADDR_NONE) { errno = EINVAL; return -1; } // If a should bind and not a multicast, the dest address // is actually the bind address if (bind_ && !is_multicast) bind_address = dest_address; else { bind_address.sin_family = AF_INET; bind_address.sin_port = dest_address.sin_port; bind_address.sin_addr.s_addr = htonl (INADDR_ANY); } address = name_; return 0; } int zmq::udp_address_t::to_string (std::string &addr_) { addr_ = address; return 0; } bool zmq::udp_address_t::is_mcast () const { return is_multicast; } const sockaddr *zmq::udp_address_t::bind_addr () const { return (sockaddr *) &bind_address; } socklen_t zmq::udp_address_t::bind_addrlen () const { return sizeof (sockaddr_in); } const sockaddr *zmq::udp_address_t::dest_addr () const { return (sockaddr *) &dest_address; } socklen_t zmq::udp_address_t::dest_addrlen () const { return sizeof (sockaddr_in); } const in_addr zmq::udp_address_t::multicast_ip () const { return multicast; } const in_addr zmq::udp_address_t::interface_ip () const { return iface; } #if defined ZMQ_HAVE_WINDOWS unsigned short zmq::udp_address_t::family () const #else sa_family_t zmq::udp_address_t::family () const #endif { return AF_INET; }