Fix Idle count
[samplevnf.git] / VNFs / DPPD-PROX / tools / flow_extract / halfstream.cpp
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 #include <fstream>
18 #include <arpa/inet.h>
19
20 #include "halfstream.hpp"
21
22 HalfStream::Action::Part HalfStream::addPkt(const PcapPkt &pkt)
23 {
24         const uint32_t pktId = pkts.size();
25         const uint8_t *l5;
26         uint32_t l5Len;
27         uint16_t tmpHdrLen;
28
29         const struct PcapPkt::tcp_hdr *tcp;
30
31         struct pkt_tuple pt = pkt.parsePkt((const uint8_t **)&tcp, &tmpHdrLen, &l5, &l5Len);
32
33         if (pt.proto_id == IPPROTO_TCP) {
34                 if (tcp->tcp_flags & 0x02)
35                         tcpOpen = true;
36                 if (tcp->tcp_flags & 0x01)
37                         tcpClose = true;
38         }
39
40         if (pkts.empty()) {
41                 first = pkt.ts();
42                 hdrLen = tmpHdrLen;
43                 memcpy(hdr, pkt.payload(), hdrLen);
44         }
45         last = pkt.ts();
46         totLen += pkt.len();
47         contentLen += l5Len;
48
49         pkts.push_back(pkt);
50
51         return Action::Part(pktId, l5 - pkt.payload(), l5Len);
52 }
53
54 double HalfStream::getRate() const
55 {
56         if (pkts.empty())
57                 return 0;
58         if (first == last)
59                 return 1250000000;
60
61         return totLen / (last - first);
62 }
63
64 HalfStream::Action::Action(HalfStream* stream, const Part &p, bool isClient)
65         : halfStream(stream), m_isClient(isClient)
66 {
67         addPart(p);
68 }
69
70 void HalfStream::Action::addPart(const Part &p)
71 {
72         parts.push_back(p);
73 }
74
75 uint32_t HalfStream::Action::totLen() const
76 {
77         uint32_t ret = 0;
78
79         for (list<Part>::const_iterator i = parts.begin(); i != parts.end(); ++i) {
80                 ret += (*i).len;
81         }
82
83         return ret;
84 }
85
86 void HalfStream::Action::toFile(ofstream *f) const
87 {
88         for (list<Part>::const_iterator i = parts.begin(); i != parts.end(); ++i) {
89                 const PcapPkt &pkt = halfStream->pkts[i->pktId];
90                 const uint8_t *payload = &pkt.payload()[i->offset];
91                 const uint16_t len = i->len;
92
93                 f->write((const char *)payload, len);
94         }
95 }
96
97 HalfStream::HalfStream()
98         : first(0, 0), last(0, 0), totLen(0), hdrLen(0), contentLen(0), tcpOpen(false), tcpClose(false)
99 {
100
101 }