Skip to content

Commit a622a76

Browse files
authored
Merge pull request #74 from CESNET/fix_variable_len_ipfix_fields
Added new function for variable-length IE IPFIX export
2 parents 30cfb48 + be785d6 commit a622a76

File tree

4 files changed

+45
-37
lines changed

4 files changed

+45
-37
lines changed

include/ipfixprobe/utils.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ namespace ipxp {
5858
void parse_range(const std::string &arg, std::string &from, std::string &to, const std::string &delim = "-");
5959
bool str2bool(std::string str);
6060
void trim_str(std::string &str);
61+
uint32_t variable2ipfix_buffer(uint8_t* buffer2write, uint8_t* buffer2read, uint16_t len);
6162

6263
template<typename T> constexpr
6364
T const& max(const T &a, const T &b) {

process/http.hpp

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
#include <ipfixprobe/flowifc.hpp>
6060
#include <ipfixprobe/packet.hpp>
6161
#include <ipfixprobe/ipfix-elements.hpp>
62+
#include <ipfixprobe/utils.hpp>
6263

6364
namespace ipxp {
6465

@@ -132,56 +133,45 @@ struct RecordExtHTTP : public RecordExt {
132133

133134
virtual int fill_ipfix(uint8_t *buffer, int size)
134135
{
135-
int length, total_length = 0;
136+
uint16_t length = 0;
137+
uint32_t total_length = 0;
136138

137139
length = strlen(user_agent);
138-
if (length + 1 > size) {
140+
if (uint32_t (length + 1) > (uint32_t) size) {
139141
return -1;
140142
}
141-
buffer[0] = length;
142-
memcpy(buffer + 1, user_agent, length);
143-
total_length = length + 1;
143+
total_length += variable2ipfix_buffer(buffer + total_length, (uint8_t*) user_agent, length);
144144

145145
length = strlen(method);
146-
if (total_length + length + 1 > size) {
146+
if (total_length + length + 3 > (uint32_t)size) {
147147
return -1;
148148
}
149-
buffer[total_length] = length;
150-
memcpy(buffer + total_length + 1, method, length);
151-
total_length += length + 1;
149+
total_length += variable2ipfix_buffer(buffer + total_length, (uint8_t*) method, length);
152150

153151
length = strlen(host);
154-
if (total_length + length + 1 > size) {
152+
if (total_length + length + 3 > (uint32_t)size) {
155153
return -1;
156154
}
157-
buffer[total_length] = length;
158-
memcpy(buffer + total_length + 1, host, length);
159-
total_length += length + 1;
155+
total_length += variable2ipfix_buffer(buffer + total_length, (uint8_t*) host, length);
160156

161157
length = strlen(referer);
162-
if (total_length + length + 1 > size) {
158+
if (total_length + length + 3 > (uint32_t)size) {
163159
return -1;
164160
}
165-
buffer[total_length] = length;
166-
memcpy(buffer + total_length + 1, referer, length);
167-
total_length += length + 1;
161+
total_length += variable2ipfix_buffer(buffer + total_length, (uint8_t*) referer, length);
168162

169163
length = strlen(uri);
170-
if (total_length + length + 4 > size) {
164+
if (total_length + length + 4 > (uint32_t)size) {
171165
return -1;
172166
}
173-
buffer[total_length] = length;
174-
memcpy(buffer + total_length + 1, uri, length);
175-
total_length += length + 1;
167+
total_length += variable2ipfix_buffer(buffer + total_length, (uint8_t*) uri, length);
176168

177169
length = strlen(content_type);
178-
if (total_length + length + 3 > size) {
170+
if (total_length + length + 3 > (uint32_t)size) {
179171
return -1;
180172
}
181-
buffer[total_length] = length;
173+
total_length += variable2ipfix_buffer(buffer + total_length, (uint8_t*) content_type, length);
182174

183-
memcpy(buffer + total_length + 1, content_type, length);
184-
total_length += length + 1;
185175
*(uint16_t *) (buffer + total_length) = ntohs(code);
186176
total_length += 2;
187177

process/tls.hpp

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
* \file tls.hpp
33
* \brief Plugin for parsing https traffic.
44
* \author Jiri Havranek <[email protected]>
5-
* \date 2018
5+
* \author Karel Hynek <[email protected]>
6+
* \date 2021
67
*/
78
/*
89
* Copyright (C) 2018 CESNET
@@ -47,6 +48,7 @@
4748
#include <string>
4849
#include <cstring>
4950
#include <arpa/inet.h>
51+
5052
#include <sstream>
5153
#include <iomanip>
5254

@@ -58,6 +60,7 @@
5860
#include <ipfixprobe/flowifc.hpp>
5961
#include <ipfixprobe/packet.hpp>
6062
#include <ipfixprobe/ipfix-elements.hpp>
63+
#include <ipfixprobe/utils.hpp>
6164

6265
namespace ipxp {
6366

@@ -109,24 +112,20 @@ struct RecordExtTLS : public RecordExt {
109112

110113
virtual int fill_ipfix(uint8_t *buffer, int size)
111114
{
112-
int sni_len = strlen(sni);
113-
int alpn_len = strlen(alpn);
114-
int pos = 0;
115+
uint16_t sni_len = strlen(sni);
116+
uint16_t alpn_len = strlen(alpn);
115117

116-
if (sni_len + alpn_len + 2 + 16 + 3 > size) {
118+
uint32_t pos = 0;
119+
uint32_t req_buff_len = (sni_len + 3) + (alpn_len + 3) + (2) + (16 + 3); // (SNI) + (ALPN) + (VERSION) + (JA3)
120+
if (req_buff_len > size) {
117121
return -1;
118122
}
119123

120124
*(uint16_t *) buffer = ntohs(version);
121125
pos += 2;
122126

123-
buffer[pos++] = sni_len;
124-
memcpy(buffer + pos, sni, sni_len);
125-
pos += sni_len;
126-
127-
buffer[pos++] = alpn_len;
128-
memcpy(buffer + pos, alpn, alpn_len);
129-
pos += alpn_len;
127+
pos += variable2ipfix_buffer(buffer + pos, (uint8_t*) sni, sni_len);
128+
pos += variable2ipfix_buffer(buffer + pos, (uint8_t*) alpn, alpn_len);
130129

131130
buffer[pos++] = 16;
132131
memcpy(buffer + pos, ja3_hash_bin, 16);

utils.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* \file utils.cpp
33
* \brief Utility functions source
44
* \author Jiri Havranek <[email protected]>
5+
* \author Karel Hynek <[email protected]>
56
* \date 2021
67
*/
78
/*
@@ -43,6 +44,8 @@
4344

4445
#include <string>
4546
#include <utility>
47+
#include <cstring>
48+
#include <arpa/inet.h>
4649

4750
#include <ipfixprobe/utils.hpp>
4851

@@ -105,4 +108,19 @@ uint64_t pntoh64(const void *p)
105108
return buffer;
106109
}
107110

111+
112+
uint32_t variable2ipfix_buffer(uint8_t* buffer2write, uint8_t* buffer2read, uint16_t len)
113+
{
114+
uint32_t ptr = 0;
115+
if (len >= 255) {
116+
buffer2write[ptr++] = 255;
117+
*(uint16_t *)(buffer2write + ptr) = ntohs(len);
118+
ptr += sizeof(uint16_t);
119+
} else {
120+
buffer2write[ptr++] = len;
121+
}
122+
std::memcpy(buffer2write + ptr, buffer2read, len);
123+
return ptr + len;
124+
}
125+
108126
}

0 commit comments

Comments
 (0)