ws_address.cpp
Go to the documentation of this file.
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 "ws_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 <netinet/tcp.h>
17 #include <net/if.h>
18 #include <netdb.h>
19 #include <ctype.h>
20 #include <unistd.h>
21 #include <stdlib.h>
22 #endif
23 
24 #include <limits.h>
25 
27 {
28  memset (&_address, 0, sizeof (_address));
29 }
30 
31 zmq::ws_address_t::ws_address_t (const sockaddr *sa_, socklen_t sa_len_)
32 {
33  zmq_assert (sa_ && sa_len_ > 0);
34 
35  memset (&_address, 0, sizeof (_address));
36  if (sa_->sa_family == AF_INET
37  && sa_len_ >= static_cast<socklen_t> (sizeof (_address.ipv4)))
38  memcpy (&_address.ipv4, sa_, sizeof (_address.ipv4));
39  else if (sa_->sa_family == AF_INET6
40  && sa_len_ >= static_cast<socklen_t> (sizeof (_address.ipv6)))
41  memcpy (&_address.ipv6, sa_, sizeof (_address.ipv6));
42 
43  _path = std::string ("");
44 
45  char hbuf[NI_MAXHOST];
46  const int rc = getnameinfo (addr (), addrlen (), hbuf, sizeof (hbuf), NULL,
47  0, NI_NUMERICHOST);
48  if (rc != 0) {
49  _host = std::string ("localhost");
50  return;
51  }
52 
53  std::ostringstream os;
54 
55  if (_address.family () == AF_INET6)
56  os << std::string ("[");
57 
58  os << std::string (hbuf);
59 
60  if (_address.family () == AF_INET6)
61  os << std::string ("]");
62 
63  _host = os.str ();
64 }
65 
66 int zmq::ws_address_t::resolve (const char *name_, bool local_, bool ipv6_)
67 {
68  // find the host part, It's important to use str*r*chr to only get
69  // the latest colon since IPv6 addresses use colons as delemiters.
70  const char *delim = strrchr (name_, ':');
71  if (delim == NULL) {
72  errno = EINVAL;
73  return -1;
74  }
75  _host = std::string (name_, delim - name_);
76 
77  // find the path part, which is optional
78  delim = strrchr (name_, '/');
79  std::string host_name;
80  if (delim) {
81  _path = std::string (delim);
82  // remove the path, otherwise resolving the port will fail with wildcard
83  host_name = std::string (name_, delim - name_);
84  } else {
85  _path = std::string ("/");
86  host_name = name_;
87  }
88 
89  ip_resolver_options_t resolver_opts;
90  resolver_opts.bindable (local_)
91  .allow_dns (!local_)
92  .allow_nic_name (local_)
93  .ipv6 (ipv6_)
94  .allow_path (true)
95  .expect_port (true);
96 
97  ip_resolver_t resolver (resolver_opts);
98 
99  return resolver.resolve (&_address, host_name.c_str ());
100 }
101 
103 {
104  std::ostringstream os;
105  os << std::string ("ws://") << host () << std::string (":")
106  << _address.port () << _path;
107  addr_ = os.str ();
108 
109  return 0;
110 }
111 
112 const sockaddr *zmq::ws_address_t::addr () const
113 {
114  return _address.as_sockaddr ();
115 }
116 
117 socklen_t zmq::ws_address_t::addrlen () const
118 {
119  return _address.sockaddr_len ();
120 }
121 
122 const char *zmq::ws_address_t::host () const
123 {
124  return _host.c_str ();
125 }
126 
127 const char *zmq::ws_address_t::path () const
128 {
129  return _path.c_str ();
130 }
131 
132 #if defined ZMQ_HAVE_WINDOWS
133 unsigned short zmq::ws_address_t::family () const
134 #else
136 #endif
137 {
138  return _address.family ();
139 }
zmq::ws_address_t::family
sa_family_t family() const
Definition: ws_address.cpp:135
ip.hpp
zmq::ip_resolver_options_t::ipv6
ip_resolver_options_t & ipv6(bool ipv6_)
Definition: ip_resolver.cpp:121
benchmarks.python.py_benchmark.const
const
Definition: py_benchmark.py:14
zmq::ip_resolver_options_t::allow_path
ip_resolver_options_t & allow_path(bool allow_)
Definition: ip_resolver.cpp:145
NULL
NULL
Definition: test_security_zap.cpp:405
zmq::ws_address_t::resolve
int resolve(const char *name_, bool local_, bool ipv6_)
Definition: ws_address.cpp:66
EINVAL
#define EINVAL
Definition: errno.hpp:25
precompiled.hpp
zmq_assert
#define zmq_assert(x)
Definition: err.hpp:102
zmq::ws_address_t::path
const char * path() const
Definition: ws_address.cpp:127
string
GLsizei const GLchar *const * string
Definition: glcorearb.h:3083
errno
int errno
zmq::ip_resolver_options_t::bindable
ip_resolver_options_t & bindable(bool bindable_)
Definition: ip_resolver.cpp:106
NI_MAXHOST
#define NI_MAXHOST
Definition: vxworks/platform.hpp:305
macros.hpp
stdint.hpp
zmq::ws_address_t::addr
const sockaddr * addr() const
Definition: ws_address.cpp:112
zmq::ip_resolver_options_t::allow_dns
ip_resolver_options_t & allow_dns(bool allow_)
Definition: ip_resolver.cpp:138
name_
string name_
Definition: googletest.cc:182
zmq::ip_resolver_t::resolve
int resolve(ip_addr_t *ip_addr_, const char *name_)
Definition: ip_resolver.cpp:187
zmq::ip_resolver_options_t::expect_port
ip_resolver_options_t & expect_port(bool expect_)
Definition: ip_resolver.cpp:131
zmq::ip_resolver_t
Definition: ip_resolver.hpp:62
zmq::ws_address_t::host
const char * host() const
Definition: ws_address.cpp:122
zmq::ip_resolver_options_t::allow_nic_name
ip_resolver_options_t & allow_nic_name(bool allow_)
Definition: ip_resolver.cpp:114
zmq::ws_address_t::_address
ip_addr_t _address
Definition: ws_address.hpp:42
zmq::ip_resolver_options_t
Definition: ip_resolver.hpp:34
ws_address.hpp
err.hpp
zmq::ws_address_t::ws_address_t
ws_address_t()
Definition: ws_address.cpp:26
zmq::ws_address_t::to_string
int to_string(std::string &addr_) const
Definition: ws_address.cpp:102
zmq::ws_address_t::addrlen
socklen_t addrlen() const
Definition: ws_address.cpp:117


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:07:02