Fix Idle count
[samplevnf.git] / VNFs / DPPD-PROX / tools / flow_extract / pcappkt.hpp
1 /*
2 // Copyright (c) 2010-2017 Intel Corporation
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //     http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 */
16
17 #ifndef _PCAPPKT_H_
18 #define _PCAPPKT_H_
19
20 #include <inttypes.h>
21 #include <pcap.h>
22 #include <string>
23 #include <cstring>
24
25 using namespace std;
26
27 struct pkt_tuple {
28         uint32_t src_addr;
29         uint32_t dst_addr;
30         uint8_t proto_id;
31         uint16_t src_port;
32         uint16_t dst_port;
33         bool operator!=(const pkt_tuple& other) const
34         {
35                 return src_addr != other.src_addr ||
36                         dst_addr != other.dst_addr ||
37                         proto_id != other.proto_id ||
38                         src_port != other.src_port ||
39                         dst_port != other.dst_port;
40         }
41         bool operator==(const pkt_tuple& other) const
42         {
43                 return src_addr == other.src_addr &&
44                         dst_addr == other.dst_addr &&
45                         proto_id == other.proto_id &&
46                         src_port == other.src_port &&
47                         dst_port == other.dst_port;
48         }
49         friend ostream& operator<<(ostream& stream, const pkt_tuple &other);
50         struct pkt_tuple flip() const
51         {
52                 struct pkt_tuple ret;
53
54                 ret = *this;
55                 ret.src_addr = dst_addr;
56                 ret.src_port = dst_port;
57                 ret.dst_addr = src_addr;
58                 ret.dst_port = src_port;
59                 return ret;
60         }
61
62 } __attribute__((packed));
63
64 class Allocator;
65
66 class PcapPkt {
67         friend class PcapReader;
68 public:
69         struct tcp_hdr {
70                 uint16_t src_port;  /**< TCP source port. */
71                 uint16_t dst_port;  /**< TCP destination port. */
72                 uint32_t sent_seq;  /**< TX data sequence number. */
73                 uint32_t recv_ack;  /**< RX data acknowledgement sequence number. */
74                 uint8_t  data_off;  /**< Data offset. */
75                 uint8_t  tcp_flags; /**< TCP flags */
76                 uint16_t rx_win;    /**< RX flow control window. */
77                 uint16_t cksum;     /**< TCP checksum. */
78                 uint16_t tcp_urp;   /**< TCP urgent pointer, if any. */
79         } __attribute__((__packed__));
80
81         static Allocator *allocator;
82         enum L4Proto {PROTO_TCP, PROTO_UDP};
83         PcapPkt();
84         void* operator new(size_t size);
85         static void operator delete(void *pointer);
86         PcapPkt(const PcapPkt& other);
87         PcapPkt(uint8_t *mem);
88         void toMem(uint8_t *mem) const;
89         void fromMem(uint8_t *mem);
90         void toFile(ofstream *file) const;
91         size_t memSize() const;
92         const struct timeval &ts() const {return header.ts;}
93         const uint16_t len() const {return header.len;}
94         pkt_tuple parsePkt(const uint8_t **l4_hdr = NULL, uint16_t *hdr_len = NULL, const uint8_t **l5 = NULL, uint32_t *l5_len = NULL) const;
95         const struct pcap_pkthdr &hdr() const {return header;}
96         const uint8_t *payload() const {return buf;}
97         enum L4Proto getProto() const;
98         ~PcapPkt();
99 private:
100         struct pcap_pkthdr header;
101         uint8_t *buf;
102 };
103
104 #endif /* _PCAPPKT_H_ */