X-Git-Url: https://git.stg.codes/stg.git/blobdiff_plain/e5499c61083684b28bcbc6950aae66cbf0938703..e9ae1f101b5418c0ba2e6c9d86b23c12f0140982:/include/stg/raw_ip_packet.h diff --git a/include/stg/raw_ip_packet.h b/include/stg/raw_ip_packet.h index 30e1c8fe..3139cafe 100644 --- a/include/stg/raw_ip_packet.h +++ b/include/stg/raw_ip_packet.h @@ -1,5 +1,6 @@ -#ifndef RAW_IP_PACKET_H -#define RAW_IP_PACKET_H +#pragma once + +#include #if defined(FREE_BSD) #include // n_long in netinet/ip.h @@ -8,157 +9,176 @@ #include // for htons #include // for struct ip -#include - #define IPv4 (2) -enum { pcktSize = 68 }; //60(max) ip + 8 udp or tcp (part of tcp or udp header to ports) +namespace STG +{ + +enum { packetSize = 68 }; //60(max) ip + 8 udp or tcp (part of tcp or udp header to ports) //----------------------------------------------------------------------------- -struct RAW_PACKET +struct RawPacket { - RAW_PACKET() - : rawPacket(), - dataLen(-1) + RawPacket() + : dataLen(-1) + { + memset(rawPacket.data, 0, packetSize); + } + + RawPacket(const RawPacket& rhs) noexcept + { + memcpy(rawPacket.data, rhs.rawPacket.data, packetSize); + } + RawPacket& operator=(const RawPacket& rhs) noexcept + { + memcpy(rawPacket.data, rhs.rawPacket.data, packetSize); + return *this; + } + RawPacket(RawPacket&& rhs) noexcept + { + memcpy(rawPacket.data, rhs.rawPacket.data, packetSize); + } + RawPacket& operator=(RawPacket&& rhs) noexcept { - memset(rawPacket.pckt, 0, pcktSize); + memcpy(rawPacket.data, rhs.rawPacket.data, packetSize); + return *this; } -uint16_t GetIPVersion() const; -uint8_t GetHeaderLen() const; -uint8_t GetProto() const; -uint32_t GetLen() const; -uint32_t GetSrcIP() const; -uint32_t GetDstIP() const; -uint16_t GetSrcPort() const; -uint16_t GetDstPort() const; + uint16_t GetIPVersion() const noexcept; + uint8_t GetHeaderLen() const noexcept; + uint8_t GetProto() const noexcept; + uint32_t GetLen() const noexcept; + uint32_t GetSrcIP() const noexcept; + uint32_t GetDstIP() const noexcept; + uint16_t GetSrcPort() const noexcept; + uint16_t GetDstPort() const noexcept; -bool operator==(const RAW_PACKET & rvalue) const; -bool operator!=(const RAW_PACKET & rvalue) const { return !(*this == rvalue); } -bool operator<(const RAW_PACKET & rvalue) const; + bool operator==(const RawPacket& rhs) const noexcept; + bool operator!=(const RawPacket& rhs) const noexcept { return !(*this == rhs); } + bool operator<(const RawPacket& rhs) const noexcept; -union + union { - uint8_t pckt[pcktSize]; // Packet header as a raw data - struct + uint8_t data[packetSize]; // Packet header as a raw data + struct { - struct ip ipHeader; - // Only for packets without options field - uint16_t sPort; - uint16_t dPort; + struct ip ipHeader; + // Only for packets without options field + uint16_t sPort; + uint16_t dPort; } header; } rawPacket; -int32_t dataLen; // IP packet length. Set to -1 to use length field from the header + int32_t dataLen; // IP packet length. Set to -1 to use length field from the header }; //----------------------------------------------------------------------------- -inline uint16_t RAW_PACKET::GetIPVersion() const +inline uint16_t RawPacket::GetIPVersion() const noexcept { -return rawPacket.header.ipHeader.ip_v; + return rawPacket.header.ipHeader.ip_v; } //----------------------------------------------------------------------------- -inline uint8_t RAW_PACKET::GetHeaderLen() const +inline uint8_t RawPacket::GetHeaderLen() const noexcept { -return rawPacket.header.ipHeader.ip_hl * 4; + return rawPacket.header.ipHeader.ip_hl * 4; } //----------------------------------------------------------------------------- -inline uint8_t RAW_PACKET::GetProto() const +inline uint8_t RawPacket::GetProto() const noexcept { -return rawPacket.header.ipHeader.ip_p; + return rawPacket.header.ipHeader.ip_p; } //----------------------------------------------------------------------------- -inline uint32_t RAW_PACKET::GetLen() const +inline uint32_t RawPacket::GetLen() const noexcept { -if (dataLen != -1) - return dataLen; -return ntohs(rawPacket.header.ipHeader.ip_len); + if (dataLen != -1) + return dataLen; + return ntohs(rawPacket.header.ipHeader.ip_len); } //----------------------------------------------------------------------------- -inline uint32_t RAW_PACKET::GetSrcIP() const +inline uint32_t RawPacket::GetSrcIP() const noexcept { -return rawPacket.header.ipHeader.ip_src.s_addr; + return rawPacket.header.ipHeader.ip_src.s_addr; } //----------------------------------------------------------------------------- -inline uint32_t RAW_PACKET::GetDstIP() const +inline uint32_t RawPacket::GetDstIP() const noexcept { -return rawPacket.header.ipHeader.ip_dst.s_addr; + return rawPacket.header.ipHeader.ip_dst.s_addr; } //----------------------------------------------------------------------------- -inline uint16_t RAW_PACKET::GetSrcPort() const +inline uint16_t RawPacket::GetSrcPort() const noexcept { -if (rawPacket.header.ipHeader.ip_p == 1) // for icmp proto return port 0 - return 0; -const uint8_t * pos = rawPacket.pckt + rawPacket.header.ipHeader.ip_hl * 4; -return ntohs(*reinterpret_cast(pos)); + if (rawPacket.header.ipHeader.ip_p == 1) // for icmp proto return port 0 + return 0; + const uint8_t* pos = rawPacket.data + rawPacket.header.ipHeader.ip_hl * 4; + return ntohs(*reinterpret_cast(pos)); } //----------------------------------------------------------------------------- -inline uint16_t RAW_PACKET::GetDstPort() const +inline uint16_t RawPacket::GetDstPort() const noexcept { -if (rawPacket.header.ipHeader.ip_p == 1) // for icmp proto return port 0 - return 0; -const uint8_t * pos = rawPacket.pckt + rawPacket.header.ipHeader.ip_hl * 4 + 2; -return ntohs(*reinterpret_cast(pos)); + if (rawPacket.header.ipHeader.ip_p == 1) // for icmp proto return port 0 + return 0; + const uint8_t * pos = rawPacket.data + rawPacket.header.ipHeader.ip_hl * 4 + 2; + return ntohs(*reinterpret_cast(pos)); } //----------------------------------------------------------------------------- -inline bool RAW_PACKET::operator==(const RAW_PACKET & rvalue) const +inline bool RawPacket::operator==(const RawPacket& rhs) const noexcept { -if (rawPacket.header.ipHeader.ip_src.s_addr != rvalue.rawPacket.header.ipHeader.ip_src.s_addr) - return false; - -if (rawPacket.header.ipHeader.ip_dst.s_addr != rvalue.rawPacket.header.ipHeader.ip_dst.s_addr) - return false; - -if (rawPacket.header.ipHeader.ip_p != 1 && rvalue.rawPacket.header.ipHeader.ip_p != 1) - { - const uint8_t * pos = rawPacket.pckt + rawPacket.header.ipHeader.ip_hl * 4; - const uint8_t * rpos = rvalue.rawPacket.pckt + rvalue.rawPacket.header.ipHeader.ip_hl * 4; - if (*reinterpret_cast(pos) != *reinterpret_cast(rpos)) + if (rawPacket.header.ipHeader.ip_src.s_addr != rhs.rawPacket.header.ipHeader.ip_src.s_addr) return false; - pos += 2; - rpos += 2; - if (*reinterpret_cast(pos) != *reinterpret_cast(rpos)) + if (rawPacket.header.ipHeader.ip_dst.s_addr != rhs.rawPacket.header.ipHeader.ip_dst.s_addr) return false; + + if (rawPacket.header.ipHeader.ip_p != 1 && rhs.rawPacket.header.ipHeader.ip_p != 1) + { + const uint8_t * pos = rawPacket.data + rawPacket.header.ipHeader.ip_hl * 4; + const uint8_t * rpos = rhs.rawPacket.data + rhs.rawPacket.header.ipHeader.ip_hl * 4; + if (*reinterpret_cast(pos) != *reinterpret_cast(rpos)) + return false; + + pos += 2; + rpos += 2; + if (*reinterpret_cast(pos) != *reinterpret_cast(rpos)) + return false; } -if (rawPacket.header.ipHeader.ip_p != rvalue.rawPacket.header.ipHeader.ip_p) - return false; + if (rawPacket.header.ipHeader.ip_p != rhs.rawPacket.header.ipHeader.ip_p) + return false; -return true; + return true; } //----------------------------------------------------------------------------- -inline bool RAW_PACKET::operator<(const RAW_PACKET & rvalue) const +inline bool RawPacket::operator<(const RawPacket& rhs) const noexcept { -if (rawPacket.header.ipHeader.ip_src.s_addr < rvalue.rawPacket.header.ipHeader.ip_src.s_addr) - return true; -if (rawPacket.header.ipHeader.ip_src.s_addr > rvalue.rawPacket.header.ipHeader.ip_src.s_addr) - return false; - -if (rawPacket.header.ipHeader.ip_dst.s_addr < rvalue.rawPacket.header.ipHeader.ip_dst.s_addr) - return true; -if (rawPacket.header.ipHeader.ip_dst.s_addr > rvalue.rawPacket.header.ipHeader.ip_dst.s_addr) - return false; - -if (rawPacket.header.ipHeader.ip_p != 1 && rvalue.rawPacket.header.ipHeader.ip_p != 1) - { - const uint8_t * pos = rawPacket.pckt + rawPacket.header.ipHeader.ip_hl * 4; - const uint8_t * rpos = rvalue.rawPacket.pckt + rvalue.rawPacket.header.ipHeader.ip_hl * 4; - if (*reinterpret_cast(pos) < *reinterpret_cast(rpos)) + if (rawPacket.header.ipHeader.ip_src.s_addr < rhs.rawPacket.header.ipHeader.ip_src.s_addr) return true; - if (*reinterpret_cast(pos) > *reinterpret_cast(rpos)) + if (rawPacket.header.ipHeader.ip_src.s_addr > rhs.rawPacket.header.ipHeader.ip_src.s_addr) return false; - pos += 2; - rpos += 2; - if (*reinterpret_cast(pos) < *reinterpret_cast(rpos)) + if (rawPacket.header.ipHeader.ip_dst.s_addr < rhs.rawPacket.header.ipHeader.ip_dst.s_addr) return true; - if (*reinterpret_cast(pos) > *reinterpret_cast(rpos)) + if (rawPacket.header.ipHeader.ip_dst.s_addr > rhs.rawPacket.header.ipHeader.ip_dst.s_addr) return false; + + if (rawPacket.header.ipHeader.ip_p != 1 && rhs.rawPacket.header.ipHeader.ip_p != 1) + { + const uint8_t * pos = rawPacket.data + rawPacket.header.ipHeader.ip_hl * 4; + const uint8_t * rpos = rhs.rawPacket.data + rhs.rawPacket.header.ipHeader.ip_hl * 4; + if (*reinterpret_cast(pos) < *reinterpret_cast(rpos)) + return true; + if (*reinterpret_cast(pos) > *reinterpret_cast(rpos)) + return false; + + pos += 2; + rpos += 2; + if (*reinterpret_cast(pos) < *reinterpret_cast(rpos)) + return true; + if (*reinterpret_cast(pos) > *reinterpret_cast(rpos)) + return false; } -if (rawPacket.header.ipHeader.ip_p < rvalue.rawPacket.header.ipHeader.ip_p) - return true; + if (rawPacket.header.ipHeader.ip_p < rhs.rawPacket.header.ipHeader.ip_p) + return true; -return false; + return false; } //----------------------------------------------------------------------------- -#endif +}