rippled
Message.cpp
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2012, 2013 Ripple Labs Inc.
5 
6  Permission to use, copy, modify, and/or distribute this software for any
7  purpose with or without fee is hereby granted, provided that the above
8  copyright notice and this permission notice appear in all copies.
9 
10  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18 //==============================================================================
19 
20 #include <ripple/overlay/Message.h>
21 #include <ripple/overlay/impl/TrafficCount.h>
22 #include <cstdint>
23 
24 namespace ripple {
25 
27  ::google::protobuf::Message const& message,
28  int type,
29  boost::optional<PublicKey> const& validator)
30  : category_(TrafficCount::categorize(message, type, false))
31  , validatorKey_(validator)
32 {
33  using namespace ripple::compression;
34 
35 #if defined(GOOGLE_PROTOBUF_VERSION) && (GOOGLE_PROTOBUF_VERSION >= 3011000)
36  auto const messageBytes = message.ByteSizeLong();
37 #else
38  unsigned const messageBytes = message.ByteSize();
39 #endif
40 
41  assert(messageBytes != 0);
42 
43  buffer_.resize(headerBytes + messageBytes);
44 
45  setHeader(buffer_.data(), messageBytes, type, Algorithm::None, 0);
46 
47  if (messageBytes != 0)
48  message.SerializeToArray(buffer_.data() + headerBytes, messageBytes);
49 }
50 
51 void
53 {
54  using namespace ripple::compression;
55  auto const messageBytes = buffer_.size() - headerBytes;
56 
57  auto type = getType(buffer_.data());
58 
59  bool const compressible = [&] {
60  if (messageBytes <= 70)
61  return false;
62  switch (type)
63  {
64  case protocol::mtMANIFESTS:
65  case protocol::mtENDPOINTS:
66  case protocol::mtTRANSACTION:
67  case protocol::mtGET_LEDGER:
68  case protocol::mtLEDGER_DATA:
69  case protocol::mtGET_OBJECTS:
70  case protocol::mtVALIDATORLIST:
71  return true;
72  case protocol::mtPING:
73  case protocol::mtCLUSTER:
74  case protocol::mtPROPOSE_LEDGER:
75  case protocol::mtSTATUS_CHANGE:
76  case protocol::mtHAVE_SET:
77  case protocol::mtVALIDATION:
78  case protocol::mtGET_SHARD_INFO:
79  case protocol::mtSHARD_INFO:
80  case protocol::mtGET_PEER_SHARD_INFO:
81  case protocol::mtPEER_SHARD_INFO:
82  break;
83  }
84  return false;
85  }();
86 
87  if (compressible)
88  {
89  auto payload = static_cast<void const*>(buffer_.data() + headerBytes);
90 
91  auto compressedSize = ripple::compression::compress(
92  payload,
93  messageBytes,
94  [&](std::size_t inSize) { // size of required compressed buffer
97  });
98 
99  if (compressedSize <
100  (messageBytes - (headerBytesCompressed - headerBytes)))
101  {
103  setHeader(
105  compressedSize,
106  type,
107  Algorithm::LZ4,
108  messageBytes);
109  }
110  else
112  }
113 }
114 
150 void
152  std::uint8_t* in,
153  std::uint32_t payloadBytes,
154  int type,
155  Algorithm compression,
156  std::uint32_t uncompressedBytes)
157 {
158  auto h = in;
159 
160  auto pack = [](std::uint8_t*& in, std::uint32_t size) {
161  *in++ = static_cast<std::uint8_t>(
162  (size >> 24) & 0x0F); // leftmost 4 are compression bits
163  *in++ = static_cast<std::uint8_t>((size >> 16) & 0xFF);
164  *in++ = static_cast<std::uint8_t>((size >> 8) & 0xFF);
165  *in++ = static_cast<std::uint8_t>(size & 0xFF);
166  };
167 
168  pack(in, payloadBytes);
169 
170  *in++ = static_cast<std::uint8_t>((type >> 8) & 0xFF);
171  *in++ = static_cast<std::uint8_t>(type & 0xFF);
172 
173  if (compression != Algorithm::None)
174  {
175  pack(in, uncompressedBytes);
176  *h |= static_cast<std::uint8_t>(compression);
177  }
178 }
179 
182 {
183  if (tryCompressed == Compressed::Off)
184  return buffer_;
185 
187 
188  if (bufferCompressed_.size() > 0)
189  return bufferCompressed_;
190  else
191  return buffer_;
192 }
193 
194 int
196 {
197  int type = (static_cast<int>(*(in + 4)) << 8) + *(in + 5);
198  return type;
199 }
200 
201 } // namespace ripple
std::call_once
T call_once(T... args)
std::vector::resize
T resize(T... args)
ripple::Message::getBuffer
std::vector< uint8_t > const & getBuffer(Compressed tryCompressed)
Retrieve the packed message data.
Definition: Message.cpp:181
ripple::Message::compress
void compress()
Try to compress the payload.
Definition: Message.cpp:52
ripple::TrafficCount
Definition: TrafficCount.h:32
std::vector< uint8_t >
std::vector::size
T size(T... args)
ripple::compression::headerBytes
constexpr std::size_t headerBytes
Definition: Compression.h:31
ripple::Message::getType
int getType(std::uint8_t const *in) const
Get the message type from the payload header.
Definition: Message.cpp:195
ripple::Message::setHeader
void setHeader(std::uint8_t *in, std::uint32_t payloadBytes, int type, Algorithm compression, std::uint32_t uncompressedBytes)
Set the payload header.
Definition: Message.cpp:151
ripple::Message::once_flag_
std::once_flag once_flag_
Definition: overlay/Message.h:94
ripple::QualityDirection::in
@ in
ripple::compression::compress
std::size_t compress(void const *in, std::size_t inSize, BufferFactory &&bf, Algorithm algorithm=Algorithm::LZ4)
Compress input data.
Definition: Compression.h:87
ripple::compression::Compressed::Off
@ Off
ripple::compression::Compressed
Compressed
Definition: Compression.h:38
cstdint
std::uint8_t
ripple::compression
Definition: Compression.h:29
ripple::compression::Algorithm
Algorithm
Definition: Compression.h:36
ripple::Message::buffer_
std::vector< uint8_t > buffer_
Definition: overlay/Message.h:91
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::Message::bufferCompressed_
std::vector< uint8_t > bufferCompressed_
Definition: overlay/Message.h:92
std::size_t
ripple::compression::headerBytesCompressed
constexpr std::size_t headerBytesCompressed
Definition: Compression.h:32
std::vector::data
T data(T... args)
ripple::Message::Message
Message(::google::protobuf::Message const &message, int type, boost::optional< PublicKey > const &validator={})
Constructor.
Definition: Message.cpp:26