rippled
TrafficCount.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/impl/TrafficCount.h>
21 
22 namespace ripple {
23 
26  ::google::protobuf::Message const& message,
27  int type,
28  bool inbound)
29 {
30  if ((type == protocol::mtPING) || (type == protocol::mtSTATUS_CHANGE))
31  return TrafficCount::category::base;
32 
33  if (type == protocol::mtCLUSTER)
34  return TrafficCount::category::cluster;
35 
36  if (type == protocol::mtMANIFESTS)
37  return TrafficCount::category::manifests;
38 
39  if (type == protocol::mtENDPOINTS)
40  return TrafficCount::category::overlay;
41 
42  if ((type == protocol::mtGET_PEER_SHARD_INFO) ||
43  (type == protocol::mtPEER_SHARD_INFO) ||
44  (type == protocol::mtGET_PEER_SHARD_INFO_V2) ||
45  (type == protocol::mtPEER_SHARD_INFO_V2))
46  return TrafficCount::category::shards;
47 
48  if (type == protocol::mtTRANSACTION)
49  return TrafficCount::category::transaction;
50 
51  if (type == protocol::mtVALIDATORLIST ||
52  type == protocol::mtVALIDATORLISTCOLLECTION)
53  return TrafficCount::category::validatorlist;
54 
55  if (type == protocol::mtVALIDATION)
56  return TrafficCount::category::validation;
57 
58  if (type == protocol::mtPROPOSE_LEDGER)
59  return TrafficCount::category::proposal;
60 
61  if (type == protocol::mtHAVE_SET)
62  return inbound ? TrafficCount::category::get_set
63  : TrafficCount::category::share_set;
64 
65  if (auto msg = dynamic_cast<protocol::TMLedgerData const*>(&message))
66  {
67  if (msg->type() == protocol::liTS_CANDIDATE)
68  return (inbound && !msg->has_requestcookie())
69  ? TrafficCount::category::ld_tsc_get
70  : TrafficCount::category::ld_tsc_share;
71 
72  if (msg->type() == protocol::liTX_NODE)
73  return (inbound && !msg->has_requestcookie())
74  ? TrafficCount::category::ld_txn_get
75  : TrafficCount::category::ld_txn_share;
76 
77  if (msg->type() == protocol::liAS_NODE)
78  return (inbound && !msg->has_requestcookie())
79  ? TrafficCount::category::ld_asn_get
80  : TrafficCount::category::ld_asn_share;
81 
82  return (inbound && !msg->has_requestcookie())
83  ? TrafficCount::category::ld_get
84  : TrafficCount::category::ld_share;
85  }
86 
87  if (auto msg = dynamic_cast<protocol::TMGetLedger const*>(&message))
88  {
89  if (msg->itype() == protocol::liTS_CANDIDATE)
90  return (inbound || msg->has_requestcookie())
91  ? TrafficCount::category::gl_tsc_share
92  : TrafficCount::category::gl_tsc_get;
93 
94  if (msg->itype() == protocol::liTX_NODE)
95  return (inbound || msg->has_requestcookie())
96  ? TrafficCount::category::gl_txn_share
97  : TrafficCount::category::gl_txn_get;
98 
99  if (msg->itype() == protocol::liAS_NODE)
100  return (inbound || msg->has_requestcookie())
101  ? TrafficCount::category::gl_asn_share
102  : TrafficCount::category::gl_asn_get;
103 
104  return (inbound || msg->has_requestcookie())
105  ? TrafficCount::category::gl_share
106  : TrafficCount::category::gl_get;
107  }
108 
109  if (auto msg = dynamic_cast<protocol::TMGetObjectByHash const*>(&message))
110  {
111  if (msg->type() == protocol::TMGetObjectByHash::otLEDGER)
112  return (msg->query() == inbound)
113  ? TrafficCount::category::share_hash_ledger
114  : TrafficCount::category::get_hash_ledger;
115 
116  if (msg->type() == protocol::TMGetObjectByHash::otTRANSACTION)
117  return (msg->query() == inbound)
118  ? TrafficCount::category::share_hash_tx
119  : TrafficCount::category::get_hash_tx;
120 
121  if (msg->type() == protocol::TMGetObjectByHash::otTRANSACTION_NODE)
122  return (msg->query() == inbound)
123  ? TrafficCount::category::share_hash_txnode
124  : TrafficCount::category::get_hash_txnode;
125 
126  if (msg->type() == protocol::TMGetObjectByHash::otSTATE_NODE)
127  return (msg->query() == inbound)
128  ? TrafficCount::category::share_hash_asnode
129  : TrafficCount::category::get_hash_asnode;
130 
131  if (msg->type() == protocol::TMGetObjectByHash::otCAS_OBJECT)
132  return (msg->query() == inbound)
133  ? TrafficCount::category::share_cas_object
134  : TrafficCount::category::get_cas_object;
135 
136  if (msg->type() == protocol::TMGetObjectByHash::otFETCH_PACK)
137  return (msg->query() == inbound)
138  ? TrafficCount::category::share_fetch_pack
139  : TrafficCount::category::get_fetch_pack;
140 
141  if (msg->type() == protocol::TMGetObjectByHash::otTRANSACTIONS)
142  return TrafficCount::category::get_transactions;
143 
144  return (msg->query() == inbound) ? TrafficCount::category::share_hash
145  : TrafficCount::category::get_hash;
146  }
147 
148  if (type == protocol::mtPROOF_PATH_REQ)
149  return TrafficCount::category::proof_path_request;
150 
151  if (type == protocol::mtPROOF_PATH_RESPONSE)
152  return TrafficCount::category::proof_path_response;
153 
154  if (type == protocol::mtREPLAY_DELTA_REQ)
155  return TrafficCount::category::replay_delta_request;
156 
157  if (type == protocol::mtREPLAY_DELTA_RESPONSE)
158  return TrafficCount::category::replay_delta_response;
159 
160  if (type == protocol::mtHAVE_TRANSACTIONS)
161  return TrafficCount::category::have_transactions;
162 
163  if (type == protocol::mtTRANSACTIONS)
164  return TrafficCount::category::requested_transactions;
165 
166  return TrafficCount::category::unknown;
167 }
168 
169 } // namespace ripple
ripple::TrafficCount::categorize
static category categorize(::google::protobuf::Message const &message, int type, bool inbound)
Given a protocol message, determine which traffic category it belongs to.
Definition: TrafficCount.cpp:25
ripple::TrafficCount::category
category
Definition: TrafficCount.h:67
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29