X-Git-Url: https://git.stg.codes/stg.git/blobdiff_plain/a258b9f408911f1a0e6f221180ee9993d100f58b..783ed451825ea18f1bec2502cb3fcecda653234a:/include/stg/raw_ip_packet.h?ds=sidebyside diff --git a/include/stg/raw_ip_packet.h b/include/stg/raw_ip_packet.h index 16e43402..30e1c8fe 100644 --- a/include/stg/raw_ip_packet.h +++ b/include/stg/raw_ip_packet.h @@ -1,7 +1,7 @@ #ifndef RAW_IP_PACKET_H #define RAW_IP_PACKET_H -#if defined(FREE_BSD) || defined(FREE_BSD5) +#if defined(FREE_BSD) #include // n_long in netinet/ip.h #endif @@ -10,9 +10,6 @@ #include -#include "const.h" -#include "common.h" - #define IPv4 (2) enum { pcktSize = 68 }; //60(max) ip + 8 udp or tcp (part of tcp or udp header to ports) @@ -26,13 +23,6 @@ struct RAW_PACKET memset(rawPacket.pckt, 0, pcktSize); } - RAW_PACKET(const RAW_PACKET & rp) - : rawPacket(), - dataLen(rp.dataLen) - { - memcpy(rawPacket.pckt, rp.rawPacket.pckt, pcktSize); - } - uint16_t GetIPVersion() const; uint8_t GetHeaderLen() const; uint8_t GetProto() const; @@ -43,7 +33,7 @@ uint16_t GetSrcPort() const; uint16_t GetDstPort() const; bool operator==(const RAW_PACKET & rvalue) const; -bool operator!=(const RAW_PACKET & rvalue) const { return !(*this == rvalue); }; +bool operator!=(const RAW_PACKET & rvalue) const { return !(*this == rvalue); } bool operator<(const RAW_PACKET & rvalue) const; union @@ -55,7 +45,7 @@ union // Only for packets without options field uint16_t sPort; uint16_t dPort; - } header __attribute__ ((packed)); + } header; } rawPacket; int32_t dataLen; // IP packet length. Set to -1 to use length field from the header }; @@ -96,14 +86,16 @@ inline uint16_t RAW_PACKET::GetSrcPort() const { if (rawPacket.header.ipHeader.ip_p == 1) // for icmp proto return port 0 return 0; -return ntohs(*((uint16_t*)(rawPacket.pckt + rawPacket.header.ipHeader.ip_hl * 4))); +const uint8_t * pos = rawPacket.pckt + rawPacket.header.ipHeader.ip_hl * 4; +return ntohs(*reinterpret_cast(pos)); } //----------------------------------------------------------------------------- inline uint16_t RAW_PACKET::GetDstPort() const { if (rawPacket.header.ipHeader.ip_p == 1) // for icmp proto return port 0 return 0; -return ntohs(*((uint16_t*)(rawPacket.pckt + rawPacket.header.ipHeader.ip_hl * 4 + 2))); +const uint8_t * pos = rawPacket.pckt + rawPacket.header.ipHeader.ip_hl * 4 + 2; +return ntohs(*reinterpret_cast(pos)); } //----------------------------------------------------------------------------- inline bool RAW_PACKET::operator==(const RAW_PACKET & rvalue) const @@ -116,12 +108,14 @@ if (rawPacket.header.ipHeader.ip_dst.s_addr != rvalue.rawPacket.header.ipHeader. if (rawPacket.header.ipHeader.ip_p != 1 && rvalue.rawPacket.header.ipHeader.ip_p != 1) { - if (*((uint16_t *)(rawPacket.pckt + rawPacket.header.ipHeader.ip_hl * 4)) != - *((uint16_t *)(rvalue.rawPacket.pckt + rvalue.rawPacket.header.ipHeader.ip_hl * 4))) + 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)) return false; - if (*((uint16_t *)(rawPacket.pckt + rawPacket.header.ipHeader.ip_hl * 4 + 2)) != - *((uint16_t *)(rvalue.rawPacket.pckt + rvalue.rawPacket.header.ipHeader.ip_hl * 4 + 2))) + pos += 2; + rpos += 2; + if (*reinterpret_cast(pos) != *reinterpret_cast(rpos)) return false; } @@ -145,18 +139,18 @@ if (rawPacket.header.ipHeader.ip_dst.s_addr > rvalue.rawPacket.header.ipHeader.i if (rawPacket.header.ipHeader.ip_p != 1 && rvalue.rawPacket.header.ipHeader.ip_p != 1) { - if (*((uint16_t *)(rawPacket.pckt + rawPacket.header.ipHeader.ip_hl * 4)) < - *((uint16_t *)(rvalue.rawPacket.pckt + rvalue.rawPacket.header.ipHeader.ip_hl * 4))) + 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)) return true; - if (*((uint16_t *)(rawPacket.pckt + rawPacket.header.ipHeader.ip_hl * 4)) > - *((uint16_t *)(rvalue.rawPacket.pckt + rvalue.rawPacket.header.ipHeader.ip_hl * 4))) + if (*reinterpret_cast(pos) > *reinterpret_cast(rpos)) return false; - if (*((uint16_t *)(rawPacket.pckt + rawPacket.header.ipHeader.ip_hl * 4 + 2)) < - *((uint16_t *)(rvalue.rawPacket.pckt + rvalue.rawPacket.header.ipHeader.ip_hl * 4 + 2))) + pos += 2; + rpos += 2; + if (*reinterpret_cast(pos) < *reinterpret_cast(rpos)) return true; - if (*((uint16_t *)(rawPacket.pckt + rawPacket.header.ipHeader.ip_hl * 4 + 2)) > - *((uint16_t *)(rvalue.rawPacket.pckt + rvalue.rawPacket.header.ipHeader.ip_hl * 4 + 2))) + if (*reinterpret_cast(pos) > *reinterpret_cast(rpos)) return false; }