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_SHARD_INFO) ||
43  (type == protocol::mtSHARD_INFO) ||
44  (type == protocol::mtGET_PEER_SHARD_INFO) ||
45  (type == protocol::mtPEER_SHARD_INFO))
46  return TrafficCount::category::shards;
47 
48  if (type == protocol::mtTRANSACTION)
49  return TrafficCount::category::transaction;
50 
51  if (type == protocol::mtVALIDATORLIST)
52  return TrafficCount::category::validatorlist;
53 
54  if (type == protocol::mtVALIDATION)
55  return TrafficCount::category::validation;
56 
57  if (type == protocol::mtPROPOSE_LEDGER)
58  return TrafficCount::category::proposal;
59 
60  if (type == protocol::mtHAVE_SET)
61  return inbound ? TrafficCount::category::get_set
62  : TrafficCount::category::share_set;
63 
64  if (auto msg = dynamic_cast<protocol::TMLedgerData const*>(&message))
65  {
66  if (msg->type() == protocol::liTS_CANDIDATE)
67  return (inbound && !msg->has_requestcookie())
68  ? TrafficCount::category::ld_tsc_get
69  : TrafficCount::category::ld_tsc_share;
70 
71  if (msg->type() == protocol::liTX_NODE)
72  return (inbound && !msg->has_requestcookie())
73  ? TrafficCount::category::ld_txn_get
74  : TrafficCount::category::ld_txn_share;
75 
76  if (msg->type() == protocol::liAS_NODE)
77  return (inbound && !msg->has_requestcookie())
78  ? TrafficCount::category::ld_asn_get
79  : TrafficCount::category::ld_asn_share;
80 
81  return (inbound && !msg->has_requestcookie())
82  ? TrafficCount::category::ld_get
83  : TrafficCount::category::ld_share;
84  }
85 
86  if (auto msg = dynamic_cast<protocol::TMGetLedger const*>(&message))
87  {
88  if (msg->itype() == protocol::liTS_CANDIDATE)
89  return (inbound || msg->has_requestcookie())
90  ? TrafficCount::category::gl_tsc_share
91  : TrafficCount::category::gl_tsc_get;
92 
93  if (msg->itype() == protocol::liTX_NODE)
94  return (inbound || msg->has_requestcookie())
95  ? TrafficCount::category::gl_txn_share
96  : TrafficCount::category::gl_txn_get;
97 
98  if (msg->itype() == protocol::liAS_NODE)
99  return (inbound || msg->has_requestcookie())
100  ? TrafficCount::category::gl_asn_share
101  : TrafficCount::category::gl_asn_get;
102 
103  return (inbound || msg->has_requestcookie())
104  ? TrafficCount::category::gl_share
105  : TrafficCount::category::gl_get;
106  }
107 
108  if (auto msg = dynamic_cast<protocol::TMGetObjectByHash const*>(&message))
109  {
110  if (msg->type() == protocol::TMGetObjectByHash::otLEDGER)
111  return (msg->query() == inbound)
112  ? TrafficCount::category::share_hash_ledger
113  : TrafficCount::category::get_hash_ledger;
114 
115  if (msg->type() == protocol::TMGetObjectByHash::otTRANSACTION)
116  return (msg->query() == inbound)
117  ? TrafficCount::category::share_hash_tx
118  : TrafficCount::category::get_hash_tx;
119 
120  if (msg->type() == protocol::TMGetObjectByHash::otTRANSACTION_NODE)
121  return (msg->query() == inbound)
122  ? TrafficCount::category::share_hash_txnode
123  : TrafficCount::category::get_hash_txnode;
124 
125  if (msg->type() == protocol::TMGetObjectByHash::otSTATE_NODE)
126  return (msg->query() == inbound)
127  ? TrafficCount::category::share_hash_asnode
128  : TrafficCount::category::get_hash_asnode;
129 
130  if (msg->type() == protocol::TMGetObjectByHash::otCAS_OBJECT)
131  return (msg->query() == inbound)
132  ? TrafficCount::category::share_cas_object
133  : TrafficCount::category::get_cas_object;
134 
135  if (msg->type() == protocol::TMGetObjectByHash::otFETCH_PACK)
136  return (msg->query() == inbound)
137  ? TrafficCount::category::share_fetch_pack
138  : TrafficCount::category::get_fetch_pack;
139 
140  return (msg->query() == inbound) ? TrafficCount::category::share_hash
141  : TrafficCount::category::get_hash;
142  }
143 
144  return TrafficCount::category::unknown;
145 }
146 
147 } // 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