Netwhere
 All Classes Pages
host.hpp
1 /*
2  * Copyright (c) 2017, Ben Smith
3  * All rights reserved.
4  *
5  */
6 
7 #ifndef __HOST_H__
8 #define __HOST_H__
9 
10 #include <unordered_map>
11 #include <tuple>
12 #include <tins/tins.h>
13 
14 #include "flow.hpp"
15 #include "object_set.hpp"
16 
21 struct Host {
22  Host(const Tins::ARP::hwaddress_type hw, const Tins::IPv4Address ip)
23  : hw(hw), ip(ip) {
24  }
25 
26  bool operator == (const Host& other) const {
27  return hw == other.hw
28  && ip == other.ip;
29  }
30 
31  const Tins::ARP::hwaddress_type hw;
32  const Tins::IPv4Address ip;
33 };
34 
35 namespace std {
36  template <>
37  struct hash<Host>
38  {
39  size_t operator()(const Host& host) const {
40  size_t seed = 0;
41  boost::hash_combine<std::string>(seed, host.hw.to_string());
42  boost::hash_combine<uint32_t>(seed, host.ip);
43 
44  return seed;
45  }
46  };
47 }
48 
49 class HostFlows {
50 public:
51  HostFlows(const Host& host) : _host(host), _bytes_in(0), _bytes_out(0) {}
52 
53  void add_flow(const FlowCounter* summary) {
54  _flows.push_back(summary);
55  }
56 
57  void remove_flow(const Flow& flow) {
58  for (auto flow_summary = _flows.begin(); flow_summary != _flows.end(); flow_summary++) {
59  if ((*flow_summary)->flow() == flow) {
60  _flows.erase(flow_summary);
61  break;
62  }
63  }
64  }
65 
66  void update_host_stats(const Flow& flow, size_t bytes) {
67  if (flow.src_ip == _host.ip && flow.src_hw == _host.hw)
68  _bytes_out += bytes;
69  else
70  _bytes_in += bytes;
71  }
72 
73  const Host& host() const { return _host; }
74  const std::vector<const FlowCounter*>& flows() const { return _flows; }
75  size_t bytes_in() const { return _bytes_in; }
76  size_t bytes_out() const { return _bytes_out; }
77 
78 private:
79  const Host _host;
80  std::vector<const FlowCounter*> _flows;
81 
82  size_t _bytes_in;
83  size_t _bytes_out;
84 };
85 
87 
88 #endif
representation of a packet flow
Definition: flow.hpp:22
Definition: flow.hpp:49
Definition: host.hpp:49
counts bytes associated with a Flow
Definition: flow.hpp:74
IP and hardware address for a network host.
Definition: host.hpp:21