Netwhere
 All Classes Pages
flow.hpp
1 /*
2  * Copyright (c) 2017, Ben Smith
3  * All rights reserved.
4  *
5  */
6 
7 #ifndef __FLOW_H__
8 #define __FLOW_H__
9 
10 #include <tins/tins.h>
11 #include <boost/functional/hash.hpp>
12 
13 #include "object_set.hpp"
14 
22 struct Flow {
23  Flow(const Tins::EthernetII::address_type& src_hw,
24  const Tins::IPv4Address& src_ip,
25  const Tins::EthernetII::address_type& dst_hw,
26  const Tins::IPv4Address& dst_ip,
27  int dst_port,
28  uint8_t ip_protocol)
29  : src_hw(src_hw), src_ip(src_ip),
30  dst_hw(dst_hw), dst_ip(dst_ip), dst_port(dst_port), ip_protocol(ip_protocol) {}
31 
32  bool operator == (const Flow& other) const {
33  return src_hw == other.src_hw
34  && src_ip == other.src_ip
35  && dst_hw == other.dst_hw
36  && dst_ip == other.dst_ip
37  && dst_port == other.dst_port
38  && ip_protocol == other.ip_protocol;
39  }
40 
41  const Tins::EthernetII::address_type src_hw;
42  const Tins::IPv4Address src_ip;
43  const Tins::EthernetII::address_type dst_hw;
44  const Tins::IPv4Address dst_ip;
45  const int dst_port;
46  const uint8_t ip_protocol;
47 };
48 
49 namespace std {
50  template <>
51  struct hash<Flow>
52  {
53  size_t operator()(const Flow& flow) const {
54  size_t seed = 0;
55  for(auto && value : flow.src_hw) {
56  boost::hash_combine<uint8_t>(seed, value);
57  }
58  boost::hash_combine<uint32_t>(seed, flow.src_ip);
59  for(auto && value : flow.dst_hw) {
60  boost::hash_combine<uint8_t>(seed, value);
61  }
62  boost::hash_combine<uint32_t>(seed, flow.dst_ip);
63  boost::hash_combine<uint32_t>(seed, flow.dst_port);
64  boost::hash_combine<uint8_t>(seed, flow.ip_protocol);
65 
66  return seed;
67  }
68  };
69 }
70 
74 class FlowCounter {
75 public:
76  FlowCounter(const Flow& flow) : _flow(flow), _bytes_to_src(0), _bytes_to_dst(0), _modified_at(0) {}
77 
78  void incr_src(time_t current_time, size_t bytes) {
79  _bytes_to_src += bytes;
80  _modified_at = current_time;
81  }
82 
83  void incr_dst(time_t current_time, size_t bytes) {
84  _bytes_to_dst += bytes;
85  _modified_at = current_time;
86  }
87 
88  const Flow& flow() const { return _flow; }
89  size_t bytes_to_src() const { return _bytes_to_src; }
90  size_t bytes_to_dst() const { return _bytes_to_dst; }
91  time_t modified_at() const { return _modified_at; }
92 
93 private:
94  const Flow _flow;
95  size_t _bytes_to_src;
96  size_t _bytes_to_dst;
97  time_t _modified_at;
98 };
99 
101 
102 #endif
representation of a packet flow
Definition: flow.hpp:22
Definition: flow.hpp:49
counts bytes associated with a Flow
Definition: flow.hpp:74