IPv4_5TupleL2Ident.hpp
Go to the documentation of this file.
1 #ifndef IPV4_5TUPLEL2IDENT_HPP
2 #define IPV4_5TUPLEL2IDENT_HPP
3 
4 #include <cstdint>
5 #include <exception>
6 #include <functional>
7 #include <iostream>
8 #include <sstream>
9 #include <stdexcept>
10 #include <string>
11 
12 #include "common.hpp"
13 #include "headers.hpp"
14 
15 #include "exceptions.hpp"
16 
17 template <class Packet> class IPv4_5TupleL2Ident {
18 public:
19  struct Hasher;
20  struct ConnectionID {
21  uint32_t dstIP;
22  uint32_t srcIP;
23  uint16_t dstPort;
24  uint16_t srcPort;
25  uint8_t proto;
26 
27  bool operator==(const ConnectionID &c) const {
28  if ((dstIP == c.dstIP) && (srcIP == c.srcIP) && (dstPort == c.dstPort) &&
29  (srcPort == c.srcPort) && (proto == c.proto)) {
30  return true;
31  } else {
32  return false;
33  }
34  };
35 
36  bool operator<(const ConnectionID &c) const {
37  Hasher hasher;
38  if (hasher(*this) < hasher(c)) {
39  return true;
40  } else {
41  return false;
42  }
43  };
44 
45  operator std::string() const {
46  std::stringstream sstream;
47  sstream << "DstIP: " << std::hex << ntohl(dstIP) << ", ";
48  sstream << "SrcIP: " << std::hex << ntohl(srcIP) << ", ";
49  sstream << "DstPort: " << std::dec << ntohs(dstPort) << ", ";
50  sstream << "SrcPort: " << std::dec << ntohs(srcPort);
51  return sstream.str();
52  }
53 
56  proto(c.proto){};
57  ConnectionID() : dstIP(0), srcIP(0), dstPort(0), srcPort(0), proto(0){};
58  };
59 
60  struct Hasher {
61  // In the future, maybe we find something more random...
62  uint64_t operator()(const ConnectionID &c) const {
63  uint64_t res = c.dstPort;
64  res |= c.srcPort << 16;
65  res ^= c.dstIP;
66  res += c.srcIP;
67  res += c.proto;
68  return res;
69  }
70  };
71 
72  static ConnectionID identify(Packet *pkt) {
73  ConnectionID id;
74  struct Headers::IPv4 *ip = reinterpret_cast<struct Headers::IPv4 *>(
75  reinterpret_cast<uint8_t *>(pkt->getData()) + 14);
76  id.dstIP = ip->dstIP;
77  id.srcIP = ip->srcIP;
78  id.proto = ip->proto;
79 
80  if (id.proto == IPPROTO_UDP) {
81  struct Headers::Udp *udp =
82  reinterpret_cast<struct Headers::Udp *>(ip->getPayload());
83  id.dstPort = udp->dstPort;
84  id.srcPort = udp->srcPort;
85  } else if (id.proto == IPPROTO_TCP) {
86  struct Headers::Tcp *tcp =
87  reinterpret_cast<struct Headers::Tcp *>(ip->getPayload());
88  id.dstPort = tcp->dstPort;
89  id.srcPort = tcp->srcPort;
90  } else {
91 #ifdef DEBUG
92  std::cout << "IPv4_5TupleL2Ident::indentify() failed" << std::endl;
93  std::cout << "IPv4_5TupleL2Ident::indentify() id.proto="
94  << static_cast<unsigned int>(id.proto) << std::endl;
95 #endif
96  hexdump(pkt->getData(), pkt->getDataLen());
97  throw new PacketNotIdentified();
98  }
99 
100  return id;
101  };
102 };
103 
104 #endif /* IPV4_5TUPLEL2IDENT_HPP */
bool operator<(const ConnectionID &c) const
Representation of the IPv4 header.
Definition: headers.hpp:65
uint32_t dstIP
destination IPv4 address
Definition: headers.hpp:124
void * getPayload()
Get the SDU.
Definition: headers.hpp:149
uint64_t operator()(const ConnectionID &c) const
bool operator==(const ConnectionID &c) const
uint16_t dstPort
Destination port.
Definition: headers.hpp:190
uint32_t srcIP
source IPv4 address
Definition: headers.hpp:123
void hexdump(const void *data, int dataLen)
Dump hex data.
Definition: hexdump.cpp:12
Representation of the UDP header.
Definition: headers.hpp:209
uint16_t srcPort
Source port.
Definition: headers.hpp:210
uint8_t proto
next protocol
Definition: headers.hpp:107
static ConnectionID identify(Packet *pkt)
uint16_t dstPort
Destination port.
Definition: headers.hpp:211
uint16_t srcPort
Source port.
Definition: headers.hpp:189
Representation of the TCP header.
Definition: headers.hpp:188