Skip to content

Commit

Permalink
wg - fix parsing and exporting byte order
Browse files Browse the repository at this point in the history
  • Loading branch information
TheSableCZ committed Sep 13, 2024
1 parent f352e83 commit 1437299
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
14 changes: 8 additions & 6 deletions process/wg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
#include <iostream>
#include <cstring>

#include <ipfixprobe/utils.hpp>

#include "wg.hpp"

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

if (cmp_peer != 0 && cmp_peer != cmp_new_peer) {
flow_flush = true;
return false;
}

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

case WG_PACKETTYPE_RESP_TO_INIT:
if (payload_len != WG_PACKETLEN_RESP_TO_INIT) {
return false;
}

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

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

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

case WG_PACKETTYPE_TRANSPORT_DATA:
if (payload_len < WG_PACKETLEN_MIN_TRANSPORT_DATA) {
return false;
}

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

Expand Down
6 changes: 4 additions & 2 deletions process/wg.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,11 @@ struct RecordExtWG : public RecordExt {

memcpy(buffer, &possible_wg, sizeof(possible_wg));
buffer += sizeof(possible_wg);
memcpy(buffer, &src_peer, sizeof(src_peer));
uint32_t src_peer_be = htobe32(src_peer);
memcpy(buffer, &src_peer_be, sizeof(src_peer));
buffer += sizeof(src_peer);
memcpy(buffer, &dst_peer, sizeof(dst_peer));
uint32_t dst_peer_be = htobe32(dst_peer);
memcpy(buffer, &dst_peer_be, sizeof(dst_peer));
buffer += sizeof(dst_peer);

return requiredLen;
Expand Down

0 comments on commit 1437299

Please sign in to comment.