Debian Codegraph @

src/net/zmq.cpp

raw symbols SCIP
1 // Copyright (c) 2019-2022, The Monero Project
2 //
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without modification, are
6 // permitted provided that the following conditions are met:
7 //
8 // 1. Redistributions of source code must retain the above copyright notice, this list of
9 // conditions and the following disclaimer.
10 //
11 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 // of conditions and the following disclaimer in the documentation and/or other
13 // materials provided with the distribution.
14 //
15 // 3. Neither the name of the copyright holder nor the names of its contributors may be
16 // used to endorse or promote products derived from this software without specific
17 // prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22 // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
27 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29 #include "net/zmq.h"
30
31 #include <cassert>
32 #include <cerrno>
33 #include <limits>
34 #include <utility>
35
36 #include "byte_slice.h"
37
38 namespace net
39 {
40 namespace zmq
41 {
42 const std::error_category& error_category() noexcept
43 {
44 struct category final : std::error_category
45 {
46 virtual const char* name() const noexcept override final
47 {
48 return "error::error_category()";
49 }
50
51 virtual std::string message(int value) const override final
52 {
53 char const* const msg = zmq_strerror(value);
54 if (msg)
55 return msg;
56 return "zmq_strerror failure";
57 }
58
59 virtual std::error_condition default_error_condition(int value) const noexcept override final
60 {
61 // maps specific errors to generic `std::errc` cases.
62 switch (value)
63 {
64 case EFSM:
65 case ETERM:
66 break;
67 default:
68 /* zmq is using cerrno errors. C++ spec indicates that
69 `std::errc` values must be identical to the cerrno value.
70 So just map every zmq specific error to the generic errc
71 equivalent. zmq extensions must be in the switch or they
72 map to a non-existent errc enum value. */
73 return std::errc(value);
74 }
75 return std::error_condition{value, *this};
76 }
77
78 };
79 static const category instance{};
80 return instance;
81 }
82
83 void terminate::call(void* ptr) noexcept
84 {
85 assert(ptr != nullptr); // see header
86 while (zmq_term(ptr))
87 {
88 if (zmq_errno() != EINTR)
89 break;
90 }
91 }
92
93 namespace
94 {
95 //! RAII wrapper for `zmq_msg_t`.
96 class message
97 {
98 zmq_msg_t handle_;
99
100 public:
101 message() noexcept
102 : handle_()
103 {
104 zmq_msg_init(handle());
105 }
106
107 message(message&& rhs) = delete;
108 message(const message& rhs) = delete;
109 message& operator=(message&& rhs) = delete;
110 message& operator=(const message& rhs) = delete;
111
112 ~message() noexcept
113 {
114 zmq_msg_close(handle());
115 }
116
117 zmq_msg_t* handle() noexcept
118 {
119 return std::addressof(handle_);
120 }
121
122 const char* data() noexcept
123 {
124 return static_cast<const char*>(zmq_msg_data(handle()));
125 }
126
127 std::size_t size() noexcept
128 {
129 return zmq_msg_size(handle());
130 }
131 };
132
133 struct do_receive
134 {
135 /* ZMQ documentation states that message parts are atomic - either
136 all are received or none are. Looking through ZMQ code and
137 GitHub discussions indicates that after part 1 is returned,
138 `EAGAIN` cannot be returned to meet these guarantees. Unit tests
139 verify (for the `inproc://` case) that this is the behavior.
140 Therefore, read errors after the first part are treated as a
141 failure for the entire message (probably `ETERM`). */
142 int operator()(std::string& payload, void* const socket, const int flags) const
143 {
144 static constexpr const int max_out = std::numeric_limits<int>::max();
145 const std::string::size_type initial = payload.size();
146 message part{};
147 for (;;)
148 {
149 int last = 0;
150 if ((last = zmq_msg_recv(part.handle(), socket, flags)) < 0)
151 return last;
152
153 payload.append(part.data(), part.size());
154 if (!zmq_msg_more(part.handle()))
155 break;
156 }
157 const std::string::size_type added = payload.size() - initial;
158 return unsigned(max_out) < added ? max_out : int(added);
159 }
160 };
161 } // anonymous
162
163 expect<std::string> receive(void* const socket, const int flags)
164 {
165 std::string payload{};
166 MONERO_CHECK(retry_op(do_receive{}, payload, socket, flags));
167 return {std::move(payload)};
168 }
169
170 expect<void> send(const epee::span<const std::uint8_t> payload, void* const socket, const int flags) noexcept
171 {
172 return retry_op(zmq_send, socket, payload.data(), payload.size(), flags);
173 }
174
175 expect<void> send(epee::byte_slice&& payload, void* socket, int flags) noexcept
176 {
177 void* const data = const_cast<std::uint8_t*>(payload.data());
178 const std::size_t size = payload.size();
179 auto buffer = payload.take_buffer(); // clears `payload` from callee
180
181 zmq_msg_t msg{};
182 MONERO_ZMQ_CHECK(zmq_msg_init_data(std::addressof(msg), data, size, epee::release_byte_slice::call, buffer.get()));
183 buffer.release(); // zmq will now decrement byte_slice ref-count
184
185 expect<void> sent = retry_op(zmq_msg_send, std::addressof(msg), socket, flags);
186 if (!sent) // beware if removing `noexcept` from this function - possible leak here
187 zmq_msg_close(std::addressof(msg));
188 return sent;
189 }
190 } // zmq
191 } // net
192