X-Git-Url: https://git.stg.codes/stg.git/blobdiff_plain/90e389f6ec12e60a62c362296ffcf314feb5b03d..73bee151e048054bde9a39c5ad2efec25af9c77b:/tests/raw_ip_packet_old.h diff --git a/tests/raw_ip_packet_old.h b/tests/raw_ip_packet_old.h index 1fbfe5f3..7c4a2a47 100644 --- a/tests/raw_ip_packet_old.h +++ b/tests/raw_ip_packet_old.h @@ -1,146 +1,124 @@ -#ifndef RAW_IP_PACKET_OLD_H -#define RAW_IP_PACKET_OLD_H +#pragma once -#include // for htons -//#include // for struct ip +#include "stg/const.h" #include +#include -#include "const.h" - -#define IPv4 (2) +#include // for htons enum { pcktSizeOLD = 68 }; //60(max) ip + 8 udp or tcp (part of tcp or udp header to ports) //----------------------------------------------------------------------------- -struct RAW_PACKET_OLD +struct RawPacketOld { - RAW_PACKET_OLD() + RawPacketOld() : dataLen(-1) { - memset(pckt, 0, pcktSizeOLD); + memset(pckt, 0, pcktSizeOLD); } - RAW_PACKET_OLD(const RAW_PACKET_OLD & rp) + RawPacketOld(const RawPacketOld& rp) : dataLen(rp.dataLen) { - memcpy(pckt, rp.pckt, pcktSizeOLD); + memcpy(pckt, rp.pckt, pcktSizeOLD); + } + + uint16_t GetIPVersion() const + { + return pckt[0] >> 4; + } + uint8_t GetHeaderLen() const + { + return (pckt[0] & 0x0F) * 4; + } + uint8_t GetProto() const + { + return pckt[9]; + } + uint32_t GetLen() const + { + if (dataLen != -1) + return dataLen; + return ntohs(*reinterpret_cast(pckt + 2)); + } + uint32_t GetSrcIP() const + { + return *reinterpret_cast(pckt + 12); + } + uint32_t GetDstIP() const + { + return *reinterpret_cast(pckt + 16); + } + uint16_t GetSrcPort() const + { + if (GetProto() == 1) // for icmp proto return port 0 + return 0; + return ntohs(*reinterpret_cast(pckt + GetHeaderLen())); + } + uint16_t GetDstPort() const + { + if (GetProto() == 1) // for icmp proto return port 0 + return 0; + return ntohs(*reinterpret_cast(pckt + GetHeaderLen() + 2)); } -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; - -uint8_t pckt[pcktSizeOLD]; // îÁÞÁÌÏ ÐÁËÅÔÁ ÚÁÈ×ÁÞÅÎÎÏÇÏ ÉÚ ÓÅÔÉ -int32_t dataLen; // äÌÉÎÁ IP ÐÁËÅÔÁ. åÓÌÉ -1, ÔÏ ÉÓÐÏÌØÚÏ×ÁÔØ ÄÌÉÎÕ ÉÚ ÚÁÇÏÌÏ×ËÁ ÓÁÍÏÇÏ ÐÁËÅÔÁ. + uint8_t pckt[pcktSizeOLD]; // îÁÞÁÌÏ ÐÁËÅÔÁ ÚÁÈ×ÁÞÅÎÎÏÇÏ ÉÚ ÓÅÔÉ + int32_t dataLen; // äÌÉÎÁ IP ÐÁËÅÔÁ. åÓÌÉ -1, ÔÏ ÉÓÐÏÌØÚÏ×ÁÔØ ÄÌÉÎÕ ÉÚ ÚÁÇÏÌÏ×ËÁ ÓÁÍÏÇÏ ÐÁËÅÔÁ. }; //----------------------------------------------------------------------------- -inline uint16_t RAW_PACKET_OLD::GetIPVersion() const -{ -return pckt[0] >> 4; -} -//----------------------------------------------------------------------------- -inline uint8_t RAW_PACKET_OLD::GetHeaderLen() const -{ -return (pckt[0] & 0x0F) * 4; -} -//----------------------------------------------------------------------------- -inline uint8_t RAW_PACKET_OLD::GetProto() const -{ -return pckt[9]; -} -//----------------------------------------------------------------------------- -inline uint32_t RAW_PACKET_OLD::GetLen() const -{ -if (dataLen != -1) - return dataLen; -return ntohs(*(uint16_t*)(pckt + 2)); -} -//----------------------------------------------------------------------------- -inline uint32_t RAW_PACKET_OLD::GetSrcIP() const +inline bool operator==(const RawPacketOld& lhs, const RawPacketOld& rhs) { -return *(uint32_t*)(pckt + 12); -} -//----------------------------------------------------------------------------- -inline uint32_t RAW_PACKET_OLD::GetDstIP() const -{ -return *(uint32_t*)(pckt + 16); -} -//----------------------------------------------------------------------------- -inline uint16_t RAW_PACKET_OLD::GetSrcPort() const -{ -if (GetProto() == 1) // for icmp proto return port 0 - return 0; -return ntohs(*((uint16_t*)(pckt + GetHeaderLen()))); -} -//----------------------------------------------------------------------------- -inline uint16_t RAW_PACKET_OLD::GetDstPort() const -{ -if (GetProto() == 1) // for icmp proto return port 0 - return 0; -return ntohs(*((uint16_t*)(pckt + GetHeaderLen() + 2))); -} -//----------------------------------------------------------------------------- -inline bool operator==(const RAW_PACKET_OLD & lhs, const RAW_PACKET_OLD & rhs) -{ -if (lhs.GetSrcIP() != rhs.GetSrcIP()) - return false; + if (lhs.GetSrcIP() != rhs.GetSrcIP()) + return false; -if (lhs.GetDstIP() != rhs.GetDstIP()) - return false; + if (lhs.GetDstIP() != rhs.GetDstIP()) + return false; -if (lhs.GetSrcPort() != rhs.GetSrcPort()) - return false; + if (lhs.GetSrcPort() != rhs.GetSrcPort()) + return false; -if (lhs.GetDstPort() != rhs.GetDstPort()) - return false; + if (lhs.GetDstPort() != rhs.GetDstPort()) + return false; -if (lhs.GetProto() != rhs.GetProto()) - return false; + if (lhs.GetProto() != rhs.GetProto()) + return false; -return true; + return true; } //----------------------------------------------------------------------------- -inline bool operator<(const RAW_PACKET_OLD & lhs, const RAW_PACKET_OLD & rhs) +inline bool operator<(const RawPacketOld& lhs, const RawPacketOld& rhs) { -if (lhs.GetSrcIP() < rhs.GetSrcIP()) - return true; -if (lhs.GetSrcIP() > rhs.GetSrcIP()) - return false; + if (lhs.GetSrcIP() < rhs.GetSrcIP()) + return true; + if (lhs.GetSrcIP() > rhs.GetSrcIP()) + return false; -if (lhs.GetDstIP() < rhs.GetDstIP()) - return true; -if (lhs.GetDstIP() > rhs.GetDstIP()) - return false; + if (lhs.GetDstIP() < rhs.GetDstIP()) + return true; + if (lhs.GetDstIP() > rhs.GetDstIP()) + return false; -if (lhs.GetSrcPort() < rhs.GetSrcPort()) - return true; -if (lhs.GetSrcPort() > rhs.GetSrcPort()) - return false; + if (lhs.GetSrcPort() < rhs.GetSrcPort()) + return true; + if (lhs.GetSrcPort() > rhs.GetSrcPort()) + return false; -if (lhs.GetDstPort() < rhs.GetDstPort()) - return true; -if (lhs.GetDstPort() > rhs.GetDstPort()) - return false; + if (lhs.GetDstPort() < rhs.GetDstPort()) + return true; + if (lhs.GetDstPort() > rhs.GetDstPort()) + return false; -if (lhs.GetProto() < rhs.GetProto()) - return true; + if (lhs.GetProto() < rhs.GetProto()) + return true; -/* -Last compare + /* + Last compare -if (lhs.GetProto() > rhs.GetProto()) - return false; + if (lhs.GetProto() > rhs.GetProto()) + return false; -don't needed -*/ + don't needed + */ -return false; + return false; } -//----------------------------------------------------------------------------- - -#endif