udp_address.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. Copyright (c) 2007-2016 Contributors as noted in the AUTHORS file
  3. This file is part of libzmq, the ZeroMQ core engine in C++.
  4. libzmq is free software; you can redistribute it and/or modify it under
  5. the terms of the GNU Lesser General Public License (LGPL) as published
  6. by the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. As a special exception, the Contributors give you permission to link
  9. this library with independent modules to produce an executable,
  10. regardless of the license terms of these independent modules, and to
  11. copy and distribute the resulting executable under terms of your choice,
  12. provided that you also meet, for each linked independent module, the
  13. terms and conditions of the license of that module. An independent
  14. module is a module which is not derived from or based on this library.
  15. If you modify this library, you must extend this exception to your
  16. version of the library.
  17. libzmq is distributed in the hope that it will be useful, but WITHOUT
  18. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  19. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  20. License for more details.
  21. You should have received a copy of the GNU Lesser General Public License
  22. along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. */
  24. #include "precompiled.hpp"
  25. #include <string>
  26. #include <sstream>
  27. #include "macros.hpp"
  28. #include "udp_address.hpp"
  29. #include "stdint.hpp"
  30. #include "err.hpp"
  31. #include "ip.hpp"
  32. #ifndef ZMQ_HAVE_WINDOWS
  33. #include <sys/types.h>
  34. #include <arpa/inet.h>
  35. #include <netdb.h>
  36. #include <net/if.h>
  37. #include <ctype.h>
  38. #endif
  39. zmq::udp_address_t::udp_address_t () :
  40. _bind_interface (-1),
  41. _is_multicast (false)
  42. {
  43. _bind_address = ip_addr_t::any (AF_INET);
  44. _target_address = ip_addr_t::any (AF_INET);
  45. }
  46. zmq::udp_address_t::~udp_address_t ()
  47. {
  48. }
  49. int zmq::udp_address_t::resolve (const char *name_, bool bind_, bool ipv6_)
  50. {
  51. // No IPv6 support yet
  52. bool has_interface = false;
  53. _address = name_;
  54. // If we have a semicolon then we should have an interface specifier in the
  55. // URL
  56. const char *src_delimiter = strrchr (name_, ';');
  57. if (src_delimiter) {
  58. const std::string src_name (name_, src_delimiter - name_);
  59. ip_resolver_options_t src_resolver_opts;
  60. src_resolver_opts
  61. .bindable (true)
  62. // Restrict hostname/service to literals to avoid any DNS
  63. // lookups or service-name irregularity due to
  64. // indeterminate socktype.
  65. .allow_dns (false)
  66. .allow_nic_name (true)
  67. .ipv6 (ipv6_)
  68. .expect_port (false);
  69. ip_resolver_t src_resolver (src_resolver_opts);
  70. const int rc = src_resolver.resolve (&_bind_address, src_name.c_str ());
  71. if (rc != 0) {
  72. return -1;
  73. }
  74. if (_bind_address.is_multicast ()) {
  75. // It doesn't make sense to have a multicast address as a source
  76. errno = EINVAL;
  77. return -1;
  78. }
  79. // This is a hack because we need the interface index when binding
  80. // multicast IPv6, we can't do it by address. Unfortunately for the
  81. // time being we don't have a generic platform-independent function to
  82. // resolve an interface index from an address, so we only support it
  83. // when an actual interface name is provided.
  84. if (src_name == "*") {
  85. _bind_interface = 0;
  86. } else {
  87. #if _WIN32_WINNT > _WIN32_WINNT_WINXP && !defined ZMQ_HAVE_WINDOWS_UWP \
  88. && !defined ZMQ_HAVE_VXWORKS
  89. _bind_interface = if_nametoindex (src_name.c_str ());
  90. if (_bind_interface == 0) {
  91. // Error, probably not an interface name.
  92. _bind_interface = -1;
  93. }
  94. #endif
  95. }
  96. has_interface = true;
  97. name_ = src_delimiter + 1;
  98. }
  99. ip_resolver_options_t resolver_opts;
  100. resolver_opts.bindable (bind_)
  101. .allow_dns (!bind_)
  102. .allow_nic_name (bind_)
  103. .expect_port (true)
  104. .ipv6 (ipv6_);
  105. ip_resolver_t resolver (resolver_opts);
  106. const int rc = resolver.resolve (&_target_address, name_);
  107. if (rc != 0) {
  108. return -1;
  109. }
  110. _is_multicast = _target_address.is_multicast ();
  111. const uint16_t port = _target_address.port ();
  112. if (has_interface) {
  113. // If we have an interface specifier then the target address must be a
  114. // multicast address
  115. if (!_is_multicast) {
  116. errno = EINVAL;
  117. return -1;
  118. }
  119. _bind_address.set_port (port);
  120. } else {
  121. // If we don't have an explicit interface specifier then the URL is
  122. // ambiguous: if the target address is multicast then it's the
  123. // destination address and the bind address is ANY, if it's unicast
  124. // then it's the bind address when 'bind_' is true and the destination
  125. // otherwise
  126. if (_is_multicast || !bind_) {
  127. _bind_address = ip_addr_t::any (_target_address.family ());
  128. _bind_address.set_port (port);
  129. _bind_interface = 0;
  130. } else {
  131. // If we were asked for a bind socket and the address
  132. // provided was not multicast then it was really meant as
  133. // a bind address and the target_address is useless.
  134. _bind_address = _target_address;
  135. }
  136. }
  137. if (_bind_address.family () != _target_address.family ()) {
  138. errno = EINVAL;
  139. return -1;
  140. }
  141. // For IPv6 multicast we *must* have an interface index since we can't
  142. // bind by address.
  143. if (ipv6_ && _is_multicast && _bind_interface < 0) {
  144. errno = ENODEV;
  145. return -1;
  146. }
  147. return 0;
  148. }
  149. int zmq::udp_address_t::family () const
  150. {
  151. return _bind_address.family ();
  152. }
  153. bool zmq::udp_address_t::is_mcast () const
  154. {
  155. return _is_multicast;
  156. }
  157. const zmq::ip_addr_t *zmq::udp_address_t::bind_addr () const
  158. {
  159. return &_bind_address;
  160. }
  161. int zmq::udp_address_t::bind_if () const
  162. {
  163. return _bind_interface;
  164. }
  165. const zmq::ip_addr_t *zmq::udp_address_t::target_addr () const
  166. {
  167. return &_target_address;
  168. }
  169. int zmq::udp_address_t::to_string (std::string &addr_)
  170. {
  171. // XXX what do (factor TCP code?)
  172. addr_ = _address;
  173. return 0;
  174. }