X-Git-Url: https://git.stg.codes/stg.git/blobdiff_plain/641204dfbdb9fc870cdd2e7f9e3169a44693e7bf..09c44c9a7e8c870e07827b3fc34811cd917b6115:/include/raw_ip_packet.h diff --git a/include/raw_ip_packet.h b/include/raw_ip_packet.h index af62d051..a1bdda71 100644 --- a/include/raw_ip_packet.h +++ b/include/raw_ip_packet.h @@ -1,6 +1,10 @@ #ifndef RAW_IP_PACKET_H #define RAW_IP_PACKET_H +#if defined(FREE_BSD) || defined(FREE_BSD5) +#include // n_long in netinet/ip.h +#endif + #include // for htons #include // for struct ip @@ -42,173 +46,123 @@ bool operator<(const RAW_PACKET & rvalue) const; union { - uint8_t pckt[pcktSize]; // îÁÞÁÌÏ ÐÁËÅÔÁ ÚÁÈ×ÁÞÅÎÎÏÇÏ ÉÚ ÓÅÔÉ + uint8_t pckt[pcktSize]; // Packet header as a raw data struct { struct ip ipHeader; // Only for packets without options field uint16_t sPort; uint16_t dPort; - } __attribute__ ((packed)); + } header __attribute__ ((packed)); }; -int32_t dataLen; // äÌÉÎÁ IP ÐÁËÅÔÁ. åÓÌÉ -1, ÔÏ ÉÓÐÏÌØÚÏ×ÁÔØ ÄÌÉÎÕ ÉÚ ÚÁÇÏÌÏ×ËÁ ÓÁÍÏÇÏ ÐÁËÅÔÁ. +int32_t dataLen; // IP packet length. Set to -1 to use length field from the header }; //----------------------------------------------------------------------------- inline uint16_t RAW_PACKET::GetIPVersion() const { -return ipHeader.ip_v; +return header.ipHeader.ip_v; } //----------------------------------------------------------------------------- inline uint8_t RAW_PACKET::GetHeaderLen() const { -return ipHeader.ip_hl * 4; +return header.ipHeader.ip_hl * 4; } //----------------------------------------------------------------------------- inline uint8_t RAW_PACKET::GetProto() const { -return ipHeader.ip_p; +return header.ipHeader.ip_p; } //----------------------------------------------------------------------------- inline uint32_t RAW_PACKET::GetLen() const { if (dataLen != -1) return dataLen; -return ntohs(ipHeader.ip_len); +return ntohs(header.ipHeader.ip_len); } //----------------------------------------------------------------------------- inline uint32_t RAW_PACKET::GetSrcIP() const { -return ipHeader.ip_src.s_addr; +return header.ipHeader.ip_src.s_addr; } //----------------------------------------------------------------------------- inline uint32_t RAW_PACKET::GetDstIP() const { -return ipHeader.ip_dst.s_addr; +return header.ipHeader.ip_dst.s_addr; } //----------------------------------------------------------------------------- inline uint16_t RAW_PACKET::GetSrcPort() const { -if (ipHeader.ip_p == 1) // for icmp proto return port 0 +if (header.ipHeader.ip_p == 1) // for icmp proto return port 0 return 0; -return ntohs(*((uint16_t*)(pckt + ipHeader.ip_hl * 4))); +return ntohs(*((uint16_t*)(pckt + header.ipHeader.ip_hl * 4))); } //----------------------------------------------------------------------------- inline uint16_t RAW_PACKET::GetDstPort() const { -if (ipHeader.ip_p == 1) // for icmp proto return port 0 +if (header.ipHeader.ip_p == 1) // for icmp proto return port 0 return 0; -return ntohs(*((uint16_t*)(pckt + ipHeader.ip_hl * 4 + 2))); +return ntohs(*((uint16_t*)(pckt + header.ipHeader.ip_hl * 4 + 2))); } //----------------------------------------------------------------------------- inline bool RAW_PACKET::operator==(const RAW_PACKET & rvalue) const { -if (ipHeader.ip_src.s_addr != rvalue.ipHeader.ip_src.s_addr) +if (header.ipHeader.ip_src.s_addr != rvalue.header.ipHeader.ip_src.s_addr) return false; -if (ipHeader.ip_dst.s_addr != rvalue.ipHeader.ip_dst.s_addr) +if (header.ipHeader.ip_dst.s_addr != rvalue.header.ipHeader.ip_dst.s_addr) return false; -if (ipHeader.ip_p != 1 && rvalue.ipHeader.ip_p != 1) +if (header.ipHeader.ip_p != 1 && rvalue.header.ipHeader.ip_p != 1) { - if (*((uint16_t *)(pckt + ipHeader.ip_hl * 4)) != - *((uint16_t *)(rvalue.pckt + rvalue.ipHeader.ip_hl * 4))) + if (*((uint16_t *)(pckt + header.ipHeader.ip_hl * 4)) != + *((uint16_t *)(rvalue.pckt + rvalue.header.ipHeader.ip_hl * 4))) return false; - if (*((uint16_t *)(pckt + ipHeader.ip_hl * 4 + 2)) != - *((uint16_t *)(rvalue.pckt + rvalue.ipHeader.ip_hl * 4 + 2))) + if (*((uint16_t *)(pckt + header.ipHeader.ip_hl * 4 + 2)) != + *((uint16_t *)(rvalue.pckt + rvalue.header.ipHeader.ip_hl * 4 + 2))) return false; } -if (ipHeader.ip_p != rvalue.ipHeader.ip_p) +if (header.ipHeader.ip_p != rvalue.header.ipHeader.ip_p) return false; return true; } -/*//----------------------------------------------------------------------------- -inline bool operator==(const RAW_PACKET & lhs, const RAW_PACKET & rhs) -{ -if (lhs.GetSrcIP() != rhs.GetSrcIP()) - return false; - -if (lhs.GetDstIP() != rhs.GetDstIP()) - return false; - -if (lhs.GetSrcPort() != rhs.GetSrcPort()) - return false; - -if (lhs.GetDstPort() != rhs.GetDstPort()) - return false; - -if (lhs.GetProto() != rhs.GetProto()) - return false; - -return true; -}*/ //----------------------------------------------------------------------------- inline bool RAW_PACKET::operator<(const RAW_PACKET & rvalue) const { -if (ipHeader.ip_src.s_addr < rvalue.ipHeader.ip_src.s_addr) +if (header.ipHeader.ip_src.s_addr < rvalue.header.ipHeader.ip_src.s_addr) return true; -if (ipHeader.ip_src.s_addr > rvalue.ipHeader.ip_src.s_addr) +if (header.ipHeader.ip_src.s_addr > rvalue.header.ipHeader.ip_src.s_addr) return false; -if (ipHeader.ip_dst.s_addr < rvalue.ipHeader.ip_dst.s_addr) +if (header.ipHeader.ip_dst.s_addr < rvalue.header.ipHeader.ip_dst.s_addr) return true; -if (ipHeader.ip_dst.s_addr > rvalue.ipHeader.ip_dst.s_addr) +if (header.ipHeader.ip_dst.s_addr > rvalue.header.ipHeader.ip_dst.s_addr) return false; -if (ipHeader.ip_p != 1 && rvalue.ipHeader.ip_p != 1) +if (header.ipHeader.ip_p != 1 && rvalue.header.ipHeader.ip_p != 1) { - if (*((uint16_t *)(pckt + ipHeader.ip_hl * 4)) < - *((uint16_t *)(rvalue.pckt + rvalue.ipHeader.ip_hl * 4))) + if (*((uint16_t *)(pckt + header.ipHeader.ip_hl * 4)) < + *((uint16_t *)(rvalue.pckt + rvalue.header.ipHeader.ip_hl * 4))) return true; - if (*((uint16_t *)(pckt + ipHeader.ip_hl * 4)) > - *((uint16_t *)(rvalue.pckt + rvalue.ipHeader.ip_hl * 4))) + if (*((uint16_t *)(pckt + header.ipHeader.ip_hl * 4)) > + *((uint16_t *)(rvalue.pckt + rvalue.header.ipHeader.ip_hl * 4))) return false; - if (*((uint16_t *)(pckt + ipHeader.ip_hl * 4 + 2)) < - *((uint16_t *)(rvalue.pckt + rvalue.ipHeader.ip_hl * 4 + 2))) + if (*((uint16_t *)(pckt + header.ipHeader.ip_hl * 4 + 2)) < + *((uint16_t *)(rvalue.pckt + rvalue.header.ipHeader.ip_hl * 4 + 2))) return true; - if (*((uint16_t *)(pckt + ipHeader.ip_hl * 4 + 2)) > - *((uint16_t *)(rvalue.pckt + rvalue.ipHeader.ip_hl * 4 + 2))) + if (*((uint16_t *)(pckt + header.ipHeader.ip_hl * 4 + 2)) > + *((uint16_t *)(rvalue.pckt + rvalue.header.ipHeader.ip_hl * 4 + 2))) return false; } -if (ipHeader.ip_p < rvalue.ipHeader.ip_p) +if (header.ipHeader.ip_p < rvalue.header.ipHeader.ip_p) return true; return false; } //----------------------------------------------------------------------------- -/*inline bool operator<(const RAW_PACKET & lhs, const RAW_PACKET & rhs) -{ -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.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.GetProto() < rhs.GetProto()) - return true; - -return false; -}*/ -//----------------------------------------------------------------------------- #endif - -