| 1 | /* |
| 2 | Copyright (c) 2007-2018 Contributors as noted in the AUTHORS file |
| 3 | |
| 4 | This file is part of libzmq, the ZeroMQ core engine in C++. |
| 5 | |
| 6 | libzmq is free software; you can redistribute it and/or modify it under |
| 7 | the terms of the GNU Lesser General Public License (LGPL) as published |
| 8 | by the Free Software Foundation; either version 3 of the License, or |
| 9 | (at your option) any later version. |
| 10 | |
| 11 | As a special exception, the Contributors give you permission to link |
| 12 | this library with independent modules to produce an executable, |
| 13 | regardless of the license terms of these independent modules, and to |
| 14 | copy and distribute the resulting executable under terms of your choice, |
| 15 | provided that you also meet, for each linked independent module, the |
| 16 | terms and conditions of the license of that module. An independent |
| 17 | module is a module which is not derived from or based on this library. |
| 18 | If you modify this library, you must extend this exception to your |
| 19 | version of the library. |
| 20 | |
| 21 | libzmq is distributed in the hope that it will be useful, but WITHOUT |
| 22 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 23 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public |
| 24 | License for more details. |
| 25 | |
| 26 | You should have received a copy of the GNU Lesser General Public License |
| 27 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 28 | */ |
| 29 | |
| 30 | #ifndef __ZMQ_IP_RESOLVER_HPP_INCLUDED__ |
| 31 | #define __ZMQ_IP_RESOLVER_HPP_INCLUDED__ |
| 32 | |
| 33 | #if !defined ZMQ_HAVE_WINDOWS |
| 34 | #include <sys/socket.h> |
| 35 | #include <netinet/in.h> |
| 36 | #include <netdb.h> |
| 37 | #endif |
| 38 | |
| 39 | #include "address.hpp" |
| 40 | |
| 41 | namespace zmq |
| 42 | { |
| 43 | union ip_addr_t |
| 44 | { |
| 45 | sockaddr generic; |
| 46 | sockaddr_in ipv4; |
| 47 | sockaddr_in6 ipv6; |
| 48 | |
| 49 | int family () const; |
| 50 | bool is_multicast () const; |
| 51 | uint16_t port () const; |
| 52 | |
| 53 | const struct sockaddr *as_sockaddr () const; |
| 54 | zmq_socklen_t sockaddr_len () const; |
| 55 | |
| 56 | void set_port (uint16_t); |
| 57 | |
| 58 | static ip_addr_t any (int family_); |
| 59 | }; |
| 60 | |
| 61 | class ip_resolver_options_t |
| 62 | { |
| 63 | public: |
| 64 | ip_resolver_options_t (); |
| 65 | |
| 66 | ip_resolver_options_t &bindable (bool bindable_); |
| 67 | ip_resolver_options_t &allow_nic_name (bool allow_); |
| 68 | ip_resolver_options_t &ipv6 (bool ipv6_); |
| 69 | ip_resolver_options_t &expect_port (bool expect_); |
| 70 | ip_resolver_options_t &allow_dns (bool allow_); |
| 71 | ip_resolver_options_t &allow_path (bool allow_); |
| 72 | |
| 73 | bool bindable (); |
| 74 | bool allow_nic_name (); |
| 75 | bool ipv6 (); |
| 76 | bool expect_port (); |
| 77 | bool allow_dns (); |
| 78 | bool allow_path (); |
| 79 | |
| 80 | private: |
| 81 | bool _bindable_wanted; |
| 82 | bool _nic_name_allowed; |
| 83 | bool _ipv6_wanted; |
| 84 | bool _port_expected; |
| 85 | bool _dns_allowed; |
| 86 | bool _path_allowed; |
| 87 | }; |
| 88 | |
| 89 | class ip_resolver_t |
| 90 | { |
| 91 | public: |
| 92 | ip_resolver_t (ip_resolver_options_t opts_); |
| 93 | |
| 94 | int resolve (ip_addr_t *ip_addr_, const char *name_); |
| 95 | |
| 96 | protected: |
| 97 | // Virtual functions that are overridden in tests |
| 98 | virtual int do_getaddrinfo (const char *node_, |
| 99 | const char *service_, |
| 100 | const struct addrinfo *hints_, |
| 101 | struct addrinfo **res_); |
| 102 | |
| 103 | virtual void do_freeaddrinfo (struct addrinfo *res_); |
| 104 | |
| 105 | virtual unsigned int do_if_nametoindex (const char *ifname_); |
| 106 | |
| 107 | private: |
| 108 | ip_resolver_options_t _options; |
| 109 | |
| 110 | int resolve_nic_name (ip_addr_t *ip_addr_, const char *nic_); |
| 111 | int resolve_getaddrinfo (ip_addr_t *ip_addr_, const char *addr_); |
| 112 | |
| 113 | #if defined ZMQ_HAVE_WINDOWS |
| 114 | int get_interface_name (unsigned long index_, char **dest_) const; |
| 115 | int wchar_to_utf8 (const WCHAR *src_, char **dest_) const; |
| 116 | #endif |
| 117 | }; |
| 118 | } |
| 119 | |
| 120 | #endif |
| 121 | |