libzmq  master
ZeroMQ C++ Core Engine (LIBZMQ)
vmci_address.cpp
Go to the documentation of this file.
1 /*
2  Copyright (c) 2007-2016 Contributors as noted in the AUTHORS file
3 
4  This file is part of libzmq, the ZeroMQ core engine in C++.
5 
6  libzmq is free software; you can redistribute it and/or modify it under
7  the terms of the GNU Lesser General Public License (LGPL) as published
8  by the Free Software Foundation; either version 3 of the License, or
9  (at your option) any later version.
10 
11  As a special exception, the Contributors give you permission to link
12  this library with independent modules to produce an executable,
13  regardless of the license terms of these independent modules, and to
14  copy and distribute the resulting executable under terms of your choice,
15  provided that you also meet, for each linked independent module, the
16  terms and conditions of the license of that module. An independent
17  module is a module which is not derived from or based on this library.
18  If you modify this library, you must extend this exception to your
19  version of the library.
20 
21  libzmq is distributed in the hope that it will be useful, but WITHOUT
22  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
23  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
24  License for more details.
25 
26  You should have received a copy of the GNU Lesser General Public License
27  along with this program. If not, see <http://www.gnu.org/licenses/>.
28 */
29 
30 #include "vmci_address.hpp"
31 
32 #if defined(ZMQ_HAVE_VMCI)
33 
34 #include <climits>
35 #include <string>
36 #include <sstream>
37 
38 #include "err.hpp"
39 
40 zmq::vmci_address_t::vmci_address_t(ctx_t *parent_) :
41  parent(parent_)
42 {
43  memset (&address, 0, sizeof address);
44 }
45 
46 zmq::vmci_address_t::vmci_address_t(const sockaddr *sa, socklen_t sa_len, ctx_t *parent_) :
47  parent(parent_)
48 {
49  zmq_assert (sa && sa_len > 0);
50 
51  memset (&address, 0, sizeof address);
52  if (sa->sa_family == parent->get_vmci_socket_family())
53  memcpy(&address, sa, sa_len);
54 }
55 
56 zmq::vmci_address_t::~vmci_address_t ()
57 {
58 }
59 
60 int zmq::vmci_address_t::resolve(const char *path_)
61 {
62  // Find the ':' at end that separates address from the port number.
63  const char *delimiter = strrchr (path_, ':');
64  if (!delimiter) {
65  errno = EINVAL;
66  return -1;
67  }
68 
69  // Separate the address/port.
70  std::string addr_str (path_, delimiter - path_);
71  std::string port_str (delimiter + 1);
72 
73  unsigned int cid = VMADDR_CID_ANY;
74  unsigned int port = VMADDR_PORT_ANY;
75 
76  if (!addr_str.length()) {
77  errno = EINVAL;
78  return -1;
79  }
80  else if (addr_str == "@") {
81  cid = VMCISock_GetLocalCID();
82 
83  if (cid == VMADDR_CID_ANY) {
84  errno = ENODEV;
85  return -1;
86  }
87  }
88  else if (addr_str != "*" && addr_str != "-1") {
89  const char *begin = addr_str.c_str();
90  char *end = NULL;
91  unsigned long l = strtoul(begin, &end, 10);
92 
93  if ((l == 0 && end == begin) || (l == ULONG_MAX && errno == ERANGE) || l > UINT_MAX)
94  {
95  errno = EINVAL;
96  return -1;
97  }
98 
99  cid = static_cast<unsigned int> (l);
100  }
101 
102  if (!port_str.length()) {
103  errno = EINVAL;
104  return -1;
105  }
106  else if (port_str != "*" && port_str != "-1") {
107  const char *begin = port_str.c_str();
108  char *end = NULL;
109  unsigned long l = strtoul(begin, &end, 10);
110 
111  if ((l == 0 && end == begin) || (l == ULONG_MAX && errno == ERANGE) || l > UINT_MAX) {
112  errno = EINVAL;
113  return -1;
114  }
115 
116  port = static_cast<unsigned int> (l);
117  }
118 
119  address.svm_family = static_cast<sa_family_t> (parent->get_vmci_socket_family());
120  address.svm_cid = cid;
121  address.svm_port = port;
122 
123  return 0;
124 }
125 
126 int zmq::vmci_address_t::to_string (std::string &addr_)
127 {
128  if (address.svm_family != parent->get_vmci_socket_family()) {
129  addr_.clear ();
130  return -1;
131  }
132 
133  std::stringstream s;
134 
135  s << "vmci://";
136 
137  if (address.svm_cid == VMADDR_CID_ANY) {
138  s << "*";
139  }
140  else
141  {
142  s << address.svm_cid;
143  }
144 
145  s << ":";
146 
147  if (address.svm_port == VMADDR_PORT_ANY) {
148  s << "*";
149  }
150  else {
151  s << address.svm_port;
152  }
153 
154  addr_ = s.str ();
155  return 0;
156 }
157 
158 const sockaddr *zmq::vmci_address_t::addr () const
159 {
160  return reinterpret_cast<const sockaddr*> (&address);
161 }
162 
163 socklen_t zmq::vmci_address_t::addrlen () const
164 {
165  return static_cast<socklen_t> (sizeof address);
166 }
167 
168 #endif
#define zmq_assert(x)
Definition: err.hpp:119
const char * address
Definition: test_fork.cpp:32