This repository has been 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
/
send.cc
158 lines (142 loc) · 5.13 KB
/
send.cc
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// 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.
#include "send.h"
#include <sys/socket.h>
#include <glog/logging.h>
#include "stringpiece.h"
#include "util.h"
namespace clerk {
namespace ipfix {
static inline void WriteChars(char** buffer, uint8_t a, uint8_t b, uint8_t c,
uint8_t d) {
char* buf = *buffer;
buf[0] = a;
buf[1] = b;
buf[2] = c;
buf[3] = d;
*buffer += 4;
}
static inline void WriteBE32(char** buffer, uint32_t data) {
WriteChars(buffer, data >> 24, data >> 16, data >> 8, data);
}
static inline void WriteBE16s(char** buffer, uint16_t first, uint16_t second) {
WriteChars(buffer, first >> 8, first, second >> 8, second);
}
static inline void WriteBE64(char** buffer, uint64_t v) {
WriteBE32(buffer, v >> 32);
WriteBE32(buffer, v & 0xFFFFFFFF);
}
IPFIXPacket::IPFIXPacket(uint32_t unix_secs) : unix_secs_(unix_secs) {}
void IPFIXPacket::Reset(PacketType t, uint32_t seq) {
count_ = 0;
type_ = t;
memset(buffer_, 0, kMaxPacketSize);
start_ = &buffer_[0];
current_ = &buffer_[0];
limit_ = start_ + kMaxPacketSize;
CHECK_LE(current_ + kHeaderSize, limit_);
char* want = current_ + kHeaderSize;
WriteBE16s(¤t_, 0xffff, 0xffff); // Will rewrite in SendTo.
WriteBE32(¤t_, unix_secs_);
WriteBE32(¤t_, seq);
WriteBE32(¤t_, 12345); // Source ID
record_buf_ = current_;
WriteBE16s(¤t_, 0xffff, 0xffff); // Will rewrite in SendTo.
CHECK_EQ(current_, want) << "diff: " << current_ - buffer_;
}
StringPiece IPFIXPacket::PacketData() {
CHECK(record_buf_ != nullptr);
WriteBE16s(&record_buf_, type_, current_ - record_buf_);
char* first = start_;
WriteBE16s(&first, 10, current_ - start_);
return StringPiece(buffer_, current_ - buffer_);
}
void IPFIXPacket::SendTo(int sock_fd) {
auto data = PacketData();
if (send(sock_fd, data.data(), data.size(), 0) < 0) {
PLOG(ERROR) << "Sending packet to socket failed";
}
}
int IPFIXPacket::count() const { return count_; }
bool IPFIXPacket::AddToBuffer(const flow::Key& k, const flow::Stats& f,
uint8_t end_reason) {
CHECK_LE(current_ + kSingleRecordSize, limit_);
char* want = current_ + kSingleRecordSize;
count_++;
switch (type_) {
case ipfix::PT_V4:
CHECK_EQ(k.network, 4);
WriteBE32(¤t_, k.get_src_ip4());
WriteBE32(¤t_, k.get_dst_ip4());
break;
case ipfix::PT_V6:
CHECK_EQ(k.network, 6);
memcpy(current_, k.src_ip, 16);
current_ += 16;
memcpy(current_, k.dst_ip, 16);
current_ += 16;
break;
case ipfix::PT_TEMPLATE:
LOG(FATAL) << "Adding to template";
default:
LOG(FATAL) << "Bad packet type " << type_;
}
WriteBE16s(¤t_, k.src_port, k.dst_port);
WriteChars(¤t_, k.protocol, f.tcp_flags, k.icmp_type, k.icmp_code);
WriteBE32(¤t_, f.src_asn);
WriteBE32(¤t_, f.dst_asn);
WriteBE64(¤t_, f.bytes);
WriteBE64(¤t_, f.packets);
// Note that even though we have nanoseconds, we write out milliseconds. This
// is because IPFIX says that micros/nanos should be in stupid NTP format
// (https://tools.ietf.org/html/rfc5905#section-6) and I'm too lazy to compute
// it.
WriteBE64(¤t_, f.first_ns / kNumNanosPerMilli);
WriteBE64(¤t_, f.last_ns / kNumNanosPerMilli);
WriteChars(¤t_, k.tos, end_reason, k.vlan >> 8, k.vlan);
CHECK_LE(current_, want);
return current_ + kSingleRecordSize >= limit_;
}
void IPFIXPacket::WriteFlowSet(bool v4) {
count_++;
CHECK_EQ(type_, ipfix::PT_TEMPLATE);
CHECK_LE(current_ + kFlowSetSize, limit_);
char* want = current_ + kFlowSetSize;
WriteBE16s(¤t_, v4 ? ipfix::PT_V4 : ipfix::PT_V6,
kFieldCount); // template ID, field count
if (v4) {
WriteBE16s(¤t_, IPV4_SRC_ADDR, 4);
WriteBE16s(¤t_, IPV4_DST_ADDR, 4);
} else {
WriteBE16s(¤t_, IPV6_SRC_ADDR, 16);
WriteBE16s(¤t_, IPV6_DST_ADDR, 16);
}
WriteBE16s(¤t_, L4_SRC_PORT, 2);
WriteBE16s(¤t_, L4_DST_PORT, 2);
WriteBE16s(¤t_, PROTOCOL, 1);
WriteBE16s(¤t_, TCP_FLAGS, 1);
WriteBE16s(¤t_, ICMP_TYPE, 2);
WriteBE16s(¤t_, BGP_SOURCE_AS_NUMBER, 4);
WriteBE16s(¤t_, BGP_DESTINATION_AS_NUMBER, 4);
WriteBE16s(¤t_, IN_BYTES, 8);
WriteBE16s(¤t_, IN_PKTS, 8);
WriteBE16s(¤t_, FLOW_START_MILLISECONDS, 8);
WriteBE16s(¤t_, FLOW_END_MILLISECONDS, 8);
WriteBE16s(¤t_, IP_CLASS_OF_SERVICE, 1);
WriteBE16s(¤t_, FLOW_END_REASON, 1);
WriteBE16s(¤t_, VLAN_ID, 2);
CHECK_EQ(current_, want);
}
} // namespace ipfix
} // namespace clerk