Skip to content

Commit d0796ef

Browse files
authored
Merge pull request #217 from CESNET/fix-wg-ipfix-byte-order
Fix wg ipfix byte order
2 parents b775de3 + 6d58647 commit d0796ef

File tree

4 files changed

+24
-8
lines changed

4 files changed

+24
-8
lines changed

include/ipfixprobe/utils.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,11 @@ std::string vec2str(const std::vector<T> &vec) {
171171
return ss.str();
172172
}
173173

174+
/**
175+
* @brief Copy uint32 in little endian byte order to destination in host byte order
176+
*/
177+
void memcpy_le32toh(uint32_t* dest, const uint32_t* src);
178+
174179
}
175180

176181
#endif /* IPXP_UTILS_HPP */

process/wg.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
#include <iostream>
3030
#include <cstring>
3131

32+
#include <ipfixprobe/utils.hpp>
33+
3234
#include "wg.hpp"
3335

3436
namespace ipxp {
@@ -145,23 +147,23 @@ bool WGPlugin::parse_wg(const char *data, unsigned int payload_len, bool source_
145147
// compare the current dst_peer and see if it matches the original source.
146148
// If not, the flow flush may be needed to create a new flow.
147149
cmp_peer = source_pkt ? ext->src_peer : ext->dst_peer;
148-
memcpy(&cmp_new_peer, (data + 4), sizeof(uint32_t));
150+
memcpy_le32toh(&cmp_new_peer, reinterpret_cast<const uint32_t*>(data + 4));
149151

150152
if (cmp_peer != 0 && cmp_peer != cmp_new_peer) {
151153
flow_flush = true;
152154
return false;
153155
}
154156

155-
memcpy(source_pkt ? &(ext->src_peer) : &(ext->dst_peer), (data + 4), sizeof(uint32_t));
157+
memcpy_le32toh(source_pkt ? &(ext->src_peer) : &(ext->dst_peer), reinterpret_cast<const uint32_t*>(data + 4));
156158
break;
157159

158160
case WG_PACKETTYPE_RESP_TO_INIT:
159161
if (payload_len != WG_PACKETLEN_RESP_TO_INIT) {
160162
return false;
161163
}
162164

163-
memcpy(&(ext->src_peer), (data + 4), sizeof(uint32_t));
164-
memcpy(&(ext->dst_peer), (data + 8), sizeof(uint32_t));
165+
memcpy_le32toh(&(ext->src_peer), reinterpret_cast<const uint32_t*>(data + 4));
166+
memcpy_le32toh(&(ext->dst_peer), reinterpret_cast<const uint32_t*>(data + 8));
165167

166168
// let's swap for the opposite direction
167169
if (!source_pkt) {
@@ -174,15 +176,15 @@ bool WGPlugin::parse_wg(const char *data, unsigned int payload_len, bool source_
174176
return false;
175177
}
176178

177-
memcpy(source_pkt ? &(ext->dst_peer) : &(ext->src_peer), (data + 4), sizeof(uint32_t));
179+
memcpy_le32toh(source_pkt ? &(ext->dst_peer) : &(ext->src_peer), reinterpret_cast<const uint32_t*>(data + 4));
178180
break;
179181

180182
case WG_PACKETTYPE_TRANSPORT_DATA:
181183
if (payload_len < WG_PACKETLEN_MIN_TRANSPORT_DATA) {
182184
return false;
183185
}
184186

185-
memcpy(source_pkt ? &(ext->dst_peer) : &(ext->src_peer), (data + 4), sizeof(uint32_t));
187+
memcpy_le32toh(source_pkt ? &(ext->dst_peer) : &(ext->src_peer), reinterpret_cast<const uint32_t*>(data + 4));
186188
break;
187189
}
188190

process/wg.hpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#ifndef IPXP_PROCESS_WG_HPP
3030
#define IPXP_PROCESS_WG_HPP
3131

32+
#include <endian.h>
3233
#include <string>
3334
#include <sstream>
3435

@@ -112,9 +113,11 @@ struct RecordExtWG : public RecordExt {
112113

113114
memcpy(buffer, &possible_wg, sizeof(possible_wg));
114115
buffer += sizeof(possible_wg);
115-
memcpy(buffer, &src_peer, sizeof(src_peer));
116+
uint32_t src_peer_be = htobe32(src_peer);
117+
memcpy(buffer, &src_peer_be, sizeof(src_peer));
116118
buffer += sizeof(src_peer);
117-
memcpy(buffer, &dst_peer, sizeof(dst_peer));
119+
uint32_t dst_peer_be = htobe32(dst_peer);
120+
memcpy(buffer, &dst_peer_be, sizeof(dst_peer));
118121
buffer += sizeof(dst_peer);
119122

120123
return requiredLen;

utils.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
#include <arpa/inet.h>
3131
#include <cstring>
32+
#include <endian.h>
3233
#include <string>
3334
#include <utility>
3435

@@ -138,4 +139,9 @@ uint64_t timeval2usec(const struct timeval& tv)
138139
return tv.tv_sec * usec_in_sec + tv.tv_usec;
139140
}
140141

142+
void memcpy_le32toh(uint32_t* dest, const uint32_t* src)
143+
{
144+
*dest = le32toh(*src);
145+
}
146+
141147
} // namespace ipxp

0 commit comments

Comments
 (0)