vmci_address.cpp
Go to the documentation of this file.
1 /* SPDX-License-Identifier: MPL-2.0 */
2 
3 #include "precompiled.hpp"
4 
5 #include "vmci_address.hpp"
6 
7 #if defined(ZMQ_HAVE_VMCI)
8 
9 #include <climits>
10 #include <string>
11 #include <sstream>
12 
13 #include "err.hpp"
14 
15 zmq::vmci_address_t::vmci_address_t ()
16 {
17  memset (&address, 0, sizeof address);
18 }
19 
20 zmq::vmci_address_t::vmci_address_t (ctx_t *parent_) : parent (parent_)
21 {
22  memset (&address, 0, sizeof address);
23 }
24 
25 zmq::vmci_address_t::vmci_address_t (const sockaddr *sa,
26  socklen_t sa_len,
27  ctx_t *parent_) :
28  parent (parent_)
29 {
30  zmq_assert (sa && sa_len > 0);
31 
32  memset (&address, 0, sizeof address);
33  if (sa->sa_family == parent->get_vmci_socket_family ())
34  memcpy (&address, sa, sa_len);
35 }
36 
37 int zmq::vmci_address_t::resolve (const char *path_)
38 {
39  // Find the ':' at end that separates address from the port number.
40  const char *delimiter = strrchr (path_, ':');
41  if (!delimiter) {
42  errno = EINVAL;
43  return -1;
44  }
45 
46  // Separate the address/port.
47  std::string addr_str (path_, delimiter - path_);
48  std::string port_str (delimiter + 1);
49 
50  unsigned int cid = VMADDR_CID_ANY;
51  unsigned int port = VMADDR_PORT_ANY;
52 
53  if (!addr_str.length ()) {
54  errno = EINVAL;
55  return -1;
56  } else if (addr_str == "@") {
57  cid = VMCISock_GetLocalCID ();
58 
59  if (cid == VMADDR_CID_ANY) {
60  errno = ENODEV;
61  return -1;
62  }
63  } else if (addr_str != "*" && addr_str != "-1") {
64  const char *begin = addr_str.c_str ();
65  char *end = NULL;
66  unsigned long l = strtoul (begin, &end, 10);
67 
68  if ((l == 0 && end == begin) || (l == ULONG_MAX && errno == ERANGE)
69  || l > UINT_MAX) {
70  errno = EINVAL;
71  return -1;
72  }
73 
74  cid = static_cast<unsigned int> (l);
75  }
76 
77  if (!port_str.length ()) {
78  errno = EINVAL;
79  return -1;
80  } else if (port_str != "*" && port_str != "-1") {
81  const char *begin = port_str.c_str ();
82  char *end = NULL;
83  unsigned long l = strtoul (begin, &end, 10);
84 
85  if ((l == 0 && end == begin) || (l == ULONG_MAX && errno == ERANGE)
86  || l > UINT_MAX) {
87  errno = EINVAL;
88  return -1;
89  }
90 
91  port = static_cast<unsigned int> (l);
92  }
93 
94  address.svm_family =
95  static_cast<sa_family_t> (parent->get_vmci_socket_family ());
96  address.svm_cid = cid;
97  address.svm_port = port;
98 
99  return 0;
100 }
101 
102 int zmq::vmci_address_t::to_string (std::string &addr_) const
103 {
104  if (address.svm_family != parent->get_vmci_socket_family ()) {
105  addr_.clear ();
106  return -1;
107  }
108 
109  std::stringstream s;
110 
111  s << "vmci://";
112 
113  if (address.svm_cid == VMADDR_CID_ANY) {
114  s << "*";
115  } else {
116  s << address.svm_cid;
117  }
118 
119  s << ":";
120 
121  if (address.svm_port == VMADDR_PORT_ANY) {
122  s << "*";
123  } else {
124  s << address.svm_port;
125  }
126 
127  addr_ = s.str ();
128  return 0;
129 }
130 
131 const sockaddr *zmq::vmci_address_t::addr () const
132 {
133  return reinterpret_cast<const sockaddr *> (&address);
134 }
135 
136 socklen_t zmq::vmci_address_t::addrlen () const
137 {
138  return static_cast<socklen_t> (sizeof address);
139 }
140 
141 #if defined ZMQ_HAVE_WINDOWS
142 unsigned short zmq::vmci_address_t::family () const
143 #else
144 sa_family_t zmq::vmci_address_t::family () const
145 #endif
146 {
147  return parent->get_vmci_socket_family ();
148 }
149 
150 #endif
benchmarks.python.py_benchmark.const
const
Definition: py_benchmark.py:14
end
GLuint GLuint end
Definition: glcorearb.h:2858
NULL
NULL
Definition: test_security_zap.cpp:405
EINVAL
#define EINVAL
Definition: errno.hpp:25
s
XmlRpcServer s
precompiled.hpp
zmq_assert
#define zmq_assert(x)
Definition: err.hpp:102
string
GLsizei const GLchar *const * string
Definition: glcorearb.h:3083
errno
int errno
address
const char * address
Definition: builds/zos/test_fork.cpp:6
begin
static size_t begin(const upb_table *t)
Definition: php/ext/google/protobuf/upb.c:4898
err.hpp
vmci_address.hpp


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