This repository was archived by the owner on Sep 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathsend.h
120 lines (107 loc) · 3.49 KB
/
send.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// Copyright 2016 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef CLERK_SEND_H_
#define CLERK_SEND_H_
#include <stdint.h> // uint32_t, etc.
#include <stdlib.h> // size_t
#include "flow.h"
#include "util.h"
#include "stringpiece.h"
namespace clerk {
namespace ipfix {
const size_t kMaxPacketSize = 1400;
const size_t kSingleRecordSize =
16 + 16 + // IPv6 addresses for ipv6. IPv4 is 4+4, so this overestimates
// in that case.
2 + 2 + // Ports
1 + // Protocol
1 + // TCP flags
2 + // ICMP code
4 + // Src AS
4 + // Dst AS
8 + // Bytes
8 + // Packets
8 + // First millis since epoch, uint64
8 + // Last millis since epoch, uint64
1 + // IP TOS
1 + // Flow end reason
2 + // VLAN ID
0;
const uint16_t kFieldCount = 16;
const size_t kHeaderSize = 20;
const size_t kFlowSetSize = 2 * 2 + // Template ID, field count
kFieldCount * 4; // fields' type/length
// Pulled from http://www.ietf.org/rfc/rfc3954.txt
enum IpfixTypes {
IN_BYTES = 1,
IN_PKTS = 2,
PROTOCOL = 4,
IP_CLASS_OF_SERVICE = 5,
TCP_FLAGS = 6,
L4_SRC_PORT = 7,
IPV4_SRC_ADDR = 8,
L4_DST_PORT = 11,
IPV4_DST_ADDR = 12,
BGP_SOURCE_AS_NUMBER = 16,
BGP_DESTINATION_AS_NUMBER = 17,
IPV6_SRC_ADDR = 27,
IPV6_DST_ADDR = 28,
ICMP_TYPE = 32,
VLAN_ID = 58,
FLOW_END_REASON = 136,
FLOW_START_MILLISECONDS = 152,
FLOW_END_MILLISECONDS = 153,
};
enum PacketType {
PT_V4 = 256,
PT_V6 = 257,
PT_TEMPLATE = 2,
};
// IPFIXPacket is a helper to build an IPFIX (netflow v10) packet to send over
// the network. It's a little tricky, so read all the fine print... or just use
// IPFIX::Send instead ;)
class IPFIXPacket {
public:
// Creates a new packet with the given uptime and current time.
explicit IPFIXPacket(uint32_t unix_secs);
// Reset this pcket to a packet type. If that packet type is PT_TEMPLATE, the
// packet is immediately sendable, and AddToBuffer will CHECK-fail.
// Otherwise, AddToBuffer must be called before SendTo.
void Reset(PacketType t, uint32_t seq);
// Get the packet data to send.
StringPiece PacketData();
// Send packet data to socket.
void SendTo(int sock_fd);
// Number of entries added to the buffer.
int count() const;
// AddToBuffer adds the given key/flow to the packet. If the packet is full
// and must be immediately sent, returns true.
bool AddToBuffer(const flow::Key& k, const flow::Stats& f,
uint8_t end_reason);
// Writes the template for v4 or v6 to the packet. Should be called only once
// on a single packet, packet type must be PT_TEMPLATE.
void WriteFlowSet(bool v4);
private:
char buffer_[kMaxPacketSize];
char* start_;
char* record_buf_;
char* current_;
char* limit_;
uint16_t count_;
PacketType type_;
uint32_t unix_secs_;
};
} // namespace ipfix
} // namespace clerk
#endif // CLERK_SEND_H_