/src/libzmq/src/udp_address.cpp
Line | Count | Source |
1 | | /* SPDX-License-Identifier: MPL-2.0 */ |
2 | | |
3 | | #include "precompiled.hpp" |
4 | | #include <string> |
5 | | #include <sstream> |
6 | | |
7 | | #include "macros.hpp" |
8 | | #include "udp_address.hpp" |
9 | | #include "stdint.hpp" |
10 | | #include "err.hpp" |
11 | | #include "ip.hpp" |
12 | | |
13 | | #ifndef ZMQ_HAVE_WINDOWS |
14 | | #include <sys/types.h> |
15 | | #include <arpa/inet.h> |
16 | | #include <netdb.h> |
17 | | #include <net/if.h> |
18 | | #include <ctype.h> |
19 | | #endif |
20 | | |
21 | | zmq::udp_address_t::udp_address_t () : |
22 | 0 | _bind_interface (-1), _is_multicast (false) |
23 | 0 | { |
24 | 0 | _bind_address = ip_addr_t::any (AF_INET); |
25 | 0 | _target_address = ip_addr_t::any (AF_INET); |
26 | 0 | } |
27 | | |
28 | | zmq::udp_address_t::~udp_address_t () |
29 | 0 | { |
30 | 0 | } |
31 | | |
32 | | int zmq::udp_address_t::resolve (const char *name_, bool bind_, bool ipv6_) |
33 | 0 | { |
34 | | // No IPv6 support yet |
35 | 0 | bool has_interface = false; |
36 | |
|
37 | 0 | _address = name_; |
38 | | |
39 | | // If we have a semicolon then we should have an interface specifier in the |
40 | | // URL |
41 | 0 | const char *src_delimiter = strrchr (name_, ';'); |
42 | 0 | if (src_delimiter) { |
43 | 0 | const std::string src_name (name_, src_delimiter - name_); |
44 | |
|
45 | 0 | ip_resolver_options_t src_resolver_opts; |
46 | |
|
47 | 0 | src_resolver_opts |
48 | 0 | .bindable (true) |
49 | | // Restrict hostname/service to literals to avoid any DNS |
50 | | // lookups or service-name irregularity due to |
51 | | // indeterminate socktype. |
52 | 0 | .allow_dns (false) |
53 | 0 | .allow_nic_name (true) |
54 | 0 | .ipv6 (ipv6_) |
55 | 0 | .expect_port (false); |
56 | |
|
57 | 0 | ip_resolver_t src_resolver (src_resolver_opts); |
58 | |
|
59 | 0 | const int rc = src_resolver.resolve (&_bind_address, src_name.c_str ()); |
60 | |
|
61 | 0 | if (rc != 0) { |
62 | 0 | return -1; |
63 | 0 | } |
64 | | |
65 | 0 | if (_bind_address.is_multicast ()) { |
66 | | // It doesn't make sense to have a multicast address as a source |
67 | 0 | errno = EINVAL; |
68 | 0 | return -1; |
69 | 0 | } |
70 | | |
71 | | // This is a hack because we need the interface index when binding |
72 | | // multicast IPv6, we can't do it by address. Unfortunately for the |
73 | | // time being we don't have a generic platform-independent function to |
74 | | // resolve an interface index from an address, so we only support it |
75 | | // when an actual interface name is provided. |
76 | 0 | if (src_name == "*") { |
77 | 0 | _bind_interface = 0; |
78 | 0 | } else { |
79 | 0 | #ifdef HAVE_IF_NAMETOINDEX |
80 | 0 | _bind_interface = if_nametoindex (src_name.c_str ()); |
81 | 0 | if (_bind_interface == 0) { |
82 | | // Error, probably not an interface name. |
83 | 0 | _bind_interface = -1; |
84 | 0 | } |
85 | 0 | #endif |
86 | 0 | } |
87 | |
|
88 | 0 | has_interface = true; |
89 | 0 | name_ = src_delimiter + 1; |
90 | 0 | } |
91 | | |
92 | 0 | ip_resolver_options_t resolver_opts; |
93 | |
|
94 | 0 | resolver_opts.bindable (bind_) |
95 | 0 | .allow_dns (true) |
96 | 0 | .allow_nic_name (bind_) |
97 | 0 | .expect_port (true) |
98 | 0 | .ipv6 (ipv6_); |
99 | |
|
100 | 0 | ip_resolver_t resolver (resolver_opts); |
101 | |
|
102 | 0 | const int rc = resolver.resolve (&_target_address, name_); |
103 | 0 | if (rc != 0) { |
104 | 0 | return -1; |
105 | 0 | } |
106 | | |
107 | 0 | _is_multicast = _target_address.is_multicast (); |
108 | 0 | const uint16_t port = _target_address.port (); |
109 | |
|
110 | 0 | if (has_interface) { |
111 | | // If we have an interface specifier then the target address must be a |
112 | | // multicast address |
113 | 0 | if (!_is_multicast) { |
114 | 0 | errno = EINVAL; |
115 | 0 | return -1; |
116 | 0 | } |
117 | | |
118 | 0 | _bind_address.set_port (port); |
119 | 0 | } else { |
120 | | // If we don't have an explicit interface specifier then the URL is |
121 | | // ambiguous: if the target address is multicast then it's the |
122 | | // destination address and the bind address is ANY, if it's unicast |
123 | | // then it's the bind address when 'bind_' is true and the destination |
124 | | // otherwise |
125 | 0 | if (_is_multicast || !bind_) { |
126 | 0 | _bind_address = ip_addr_t::any (_target_address.family ()); |
127 | 0 | _bind_address.set_port (port); |
128 | 0 | _bind_interface = 0; |
129 | 0 | } else { |
130 | | // If we were asked for a bind socket and the address |
131 | | // provided was not multicast then it was really meant as |
132 | | // a bind address and the target_address is useless. |
133 | 0 | _bind_address = _target_address; |
134 | 0 | } |
135 | 0 | } |
136 | | |
137 | 0 | if (_bind_address.family () != _target_address.family ()) { |
138 | 0 | errno = EINVAL; |
139 | 0 | return -1; |
140 | 0 | } |
141 | | |
142 | | // For IPv6 multicast we *must* have an interface index since we can't |
143 | | // bind by address. |
144 | 0 | if (ipv6_ && _is_multicast && _bind_interface < 0) { |
145 | 0 | errno = ENODEV; |
146 | 0 | return -1; |
147 | 0 | } |
148 | | |
149 | 0 | return 0; |
150 | 0 | } |
151 | | |
152 | | int zmq::udp_address_t::family () const |
153 | 0 | { |
154 | 0 | return _bind_address.family (); |
155 | 0 | } |
156 | | |
157 | | bool zmq::udp_address_t::is_mcast () const |
158 | 0 | { |
159 | 0 | return _is_multicast; |
160 | 0 | } |
161 | | |
162 | | const zmq::ip_addr_t *zmq::udp_address_t::bind_addr () const |
163 | 0 | { |
164 | 0 | return &_bind_address; |
165 | 0 | } |
166 | | |
167 | | int zmq::udp_address_t::bind_if () const |
168 | 0 | { |
169 | 0 | return _bind_interface; |
170 | 0 | } |
171 | | |
172 | | const zmq::ip_addr_t *zmq::udp_address_t::target_addr () const |
173 | 0 | { |
174 | 0 | return &_target_address; |
175 | 0 | } |
176 | | |
177 | | int zmq::udp_address_t::to_string (std::string &addr_) |
178 | 0 | { |
179 | | // XXX what do (factor TCP code?) |
180 | 0 | addr_ = _address; |
181 | 0 | return 0; |
182 | 0 | } |