ws_address.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 "ws_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 <netinet/tcp.h>
  36. #include <net/if.h>
  37. #include <netdb.h>
  38. #include <ctype.h>
  39. #include <unistd.h>
  40. #include <stdlib.h>
  41. #endif
  42. #include <limits.h>
  43. zmq::ws_address_t::ws_address_t ()
  44. {
  45. memset (&_address, 0, sizeof (_address));
  46. }
  47. zmq::ws_address_t::ws_address_t (const sockaddr *sa_, socklen_t sa_len_)
  48. {
  49. zmq_assert (sa_ && sa_len_ > 0);
  50. memset (&_address, 0, sizeof (_address));
  51. if (sa_->sa_family == AF_INET
  52. && sa_len_ >= static_cast<socklen_t> (sizeof (_address.ipv4)))
  53. memcpy (&_address.ipv4, sa_, sizeof (_address.ipv4));
  54. else if (sa_->sa_family == AF_INET6
  55. && sa_len_ >= static_cast<socklen_t> (sizeof (_address.ipv6)))
  56. memcpy (&_address.ipv6, sa_, sizeof (_address.ipv6));
  57. _path = std::string ("");
  58. char hbuf[NI_MAXHOST];
  59. const int rc = getnameinfo (addr (), addrlen (), hbuf, sizeof (hbuf), NULL,
  60. 0, NI_NUMERICHOST);
  61. if (rc != 0) {
  62. _host = std::string ("localhost");
  63. return;
  64. }
  65. std::ostringstream os;
  66. if (_address.family () == AF_INET6)
  67. os << std::string ("[");
  68. os << std::string (hbuf);
  69. if (_address.family () == AF_INET6)
  70. os << std::string ("]");
  71. _host = os.str ();
  72. }
  73. int zmq::ws_address_t::resolve (const char *name_, bool local_, bool ipv6_)
  74. {
  75. // find the host part, It's important to use str*r*chr to only get
  76. // the latest colon since IPv6 addresses use colons as delemiters.
  77. const char *delim = strrchr (name_, ':');
  78. if (delim == NULL) {
  79. errno = EINVAL;
  80. return -1;
  81. }
  82. _host = std::string (name_, delim - name_);
  83. // find the path part, which is optional
  84. delim = strrchr (name_, '/');
  85. std::string host_name;
  86. if (delim) {
  87. _path = std::string (delim);
  88. // remove the path, otherwise resolving the port will fail with wildcard
  89. host_name = std::string (name_, delim - name_);
  90. } else {
  91. _path = std::string ("/");
  92. host_name = name_;
  93. }
  94. ip_resolver_options_t resolver_opts;
  95. resolver_opts.bindable (local_)
  96. .allow_dns (!local_)
  97. .allow_nic_name (local_)
  98. .ipv6 (ipv6_)
  99. .allow_path (true)
  100. .expect_port (true);
  101. ip_resolver_t resolver (resolver_opts);
  102. return resolver.resolve (&_address, host_name.c_str ());
  103. }
  104. int zmq::ws_address_t::to_string (std::string &addr_) const
  105. {
  106. std::ostringstream os;
  107. os << std::string ("ws://") << host () << std::string (":")
  108. << _address.port () << _path;
  109. addr_ = os.str ();
  110. return 0;
  111. }
  112. const sockaddr *zmq::ws_address_t::addr () const
  113. {
  114. return _address.as_sockaddr ();
  115. }
  116. socklen_t zmq::ws_address_t::addrlen () const
  117. {
  118. return _address.sockaddr_len ();
  119. }
  120. const char *zmq::ws_address_t::host () const
  121. {
  122. return _host.c_str ();
  123. }
  124. const char *zmq::ws_address_t::path () const
  125. {
  126. return _path.c_str ();
  127. }
  128. #if defined ZMQ_HAVE_WINDOWS
  129. unsigned short zmq::ws_address_t::family () const
  130. #else
  131. sa_family_t zmq::ws_address_t::family () const
  132. #endif
  133. {
  134. return _address.family ();
  135. }