| 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/levin_base.h" |
| 30 | |
| 31 | #include "int-util.h" |
| 32 | |
| 33 | namespace epee |
| 34 | { |
| 35 | namespace levin |
| 36 | { |
| 37 | message_writer::message_writer(const std::size_t reserve) |
| 38 | : buffer() |
| 39 | { |
| 40 | buffer.reserve(reserve); |
| 41 | buffer.put_n(0, sizeof(header)); |
| 42 | } |
| 43 | |
| 44 | byte_slice message_writer::finalize(const uint32_t command, const uint32_t flags, const uint32_t return_code, const bool expect_response) |
| 45 | { |
| 46 | if (buffer.size() < sizeof(header)) |
| 47 | throw std::runtime_error{"levin_writer::finalize already called"}; |
| 48 | |
| 49 | header head = make_header(command, payload_size(), flags, expect_response); |
| 50 | head.m_return_code = SWAP32LE(return_code); |
| 51 | |
| 52 | std::memcpy(buffer.tellp() - buffer.size(), std::addressof(head), sizeof(head)); |
| 53 | return byte_slice{std::move(buffer)}; |
| 54 | } |
| 55 | |
| 56 | bucket_head2 make_header(uint32_t command, uint64_t msg_size, uint32_t flags, bool expect_response) noexcept |
| 57 | { |
| 58 | bucket_head2 head = {0}; |
| 59 | head.m_signature = SWAP64LE(LEVIN_SIGNATURE); |
| 60 | head.m_have_to_return_data = expect_response; |
| 61 | head.m_cb = SWAP64LE(msg_size); |
| 62 | |
| 63 | head.m_command = SWAP32LE(command); |
| 64 | head.m_protocol_version = SWAP32LE(LEVIN_PROTOCOL_VER_1); |
| 65 | head.m_flags = SWAP32LE(flags); |
| 66 | return head; |
| 67 | } |
| 68 | |
| 69 | byte_slice make_noise_notify(const std::size_t noise_bytes) |
| 70 | { |
| 71 | static constexpr const std::uint32_t flags = |
| 72 | LEVIN_PACKET_BEGIN | LEVIN_PACKET_END; |
| 73 | |
| 74 | if (noise_bytes < sizeof(bucket_head2)) |
| 75 | return nullptr; |
| 76 | |
| 77 | std::string buffer(noise_bytes, char(0)); |
| 78 | const bucket_head2 head = make_header(0, noise_bytes - sizeof(bucket_head2), flags, false); |
| 79 | std::memcpy(std::addressof(buffer[0]), std::addressof(head), sizeof(head)); |
| 80 | |
| 81 | return byte_slice{std::move(buffer)}; |
| 82 | } |
| 83 | |
| 84 | byte_slice make_fragmented_notify(const std::size_t noise_size, const int command, message_writer message) |
| 85 | { |
| 86 | if (noise_size < sizeof(bucket_head2) * 2) |
| 87 | return nullptr; |
| 88 | |
| 89 | if (message.buffer.size() <= noise_size) |
| 90 | { |
| 91 | /* The entire message can be sent at once, and the levin binary parser |
| 92 | will ignore extra bytes. So just pad with zeroes and otherwise send |
| 93 | a "normal", not fragmented message. */ |
| 94 | |
| 95 | message.buffer.put_n(0, noise_size - message.buffer.size()); |
| 96 | return message.finalize_notify(command); |
| 97 | } |
| 98 | |
| 99 | // fragment message |
| 100 | const byte_slice payload_bytes = message.finalize_notify(command); |
| 101 | span<const std::uint8_t> payload = to_span(payload_bytes); |
| 102 | |
| 103 | const size_t payload_space = noise_size - sizeof(bucket_head2); |
| 104 | const size_t expected_fragments = ((payload.size() - 2) / payload_space) + 1; |
| 105 | |
| 106 | byte_stream buffer{}; |
| 107 | buffer.reserve(expected_fragments * noise_size); |
| 108 | |
| 109 | bucket_head2 head = make_header(0, payload_space, LEVIN_PACKET_BEGIN, false); |
| 110 | buffer.write(as_byte_span(head)); |
| 111 | |
| 112 | // internal levin header is in payload already |
| 113 | |
| 114 | size_t copy_size = payload.remove_prefix(payload_space); |
| 115 | buffer.write(payload.data() - copy_size, copy_size); |
| 116 | |
| 117 | head.m_flags = 0; |
| 118 | while (!payload.empty()) |
| 119 | { |
| 120 | copy_size = payload.remove_prefix(payload_space); |
| 121 | |
| 122 | if (payload.empty()) |
| 123 | head.m_flags = LEVIN_PACKET_END; |
| 124 | |
| 125 | buffer.write(as_byte_span(head)); |
| 126 | buffer.write(payload.data() - copy_size, copy_size); |
| 127 | } |
| 128 | |
| 129 | const size_t padding = noise_size - copy_size - sizeof(bucket_head2); |
| 130 | buffer.put_n(0, padding); |
| 131 | |
| 132 | return byte_slice{std::move(buffer)}; |
| 133 | } |
| 134 | } // levin |
| 135 | } // epee |