]> git.stg.codes - stg.git/blobdiff - include/stg/raw_ip_packet.h
Port to CMake, get rid of os_int.h.
[stg.git] / include / stg / raw_ip_packet.h
index a1bdda7170205d76cc3fbc1eef6ac114ad26134b..30e1c8fe186d72cefbf3a2e50983a3292039b1d9 100644 (file)
@@ -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 <netinet/in_systm.h> // n_long in netinet/ip.h
 #endif
 
@@ -10,9 +10,6 @@
 
 #include <cstring>
 
-#include "stg_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)
@@ -20,15 +17,10 @@ enum { pcktSize = 68 }; //60(max) ip + 8 udp or tcp (part of tcp or udp header t
 struct RAW_PACKET
 {
     RAW_PACKET()
-        : dataLen(-1)
-    {
-    memset(pckt, 0, pcktSize);
-    }
-
-    RAW_PACKET(const RAW_PACKET & rp)
-        : dataLen(rp.dataLen)
+        : rawPacket(),
+          dataLen(-1)
     {
-    memcpy(pckt, rp.pckt, pcktSize);
+    memset(rawPacket.pckt, 0, pcktSize);
     }
 
 uint16_t    GetIPVersion() const;
@@ -41,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
@@ -53,77 +45,81 @@ 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
 };
 //-----------------------------------------------------------------------------
 inline uint16_t RAW_PACKET::GetIPVersion() const
 {
-return header.ipHeader.ip_v;
+return rawPacket.header.ipHeader.ip_v;
 }
 //-----------------------------------------------------------------------------
 inline uint8_t RAW_PACKET::GetHeaderLen() const
 {
-return header.ipHeader.ip_hl * 4;
+return rawPacket.header.ipHeader.ip_hl * 4;
 }
 //-----------------------------------------------------------------------------
 inline uint8_t RAW_PACKET::GetProto() const
 {
-return header.ipHeader.ip_p;
+return rawPacket.header.ipHeader.ip_p;
 }
 //-----------------------------------------------------------------------------
 inline uint32_t RAW_PACKET::GetLen() const
 {
 if (dataLen != -1)
     return dataLen;
-return ntohs(header.ipHeader.ip_len);
+return ntohs(rawPacket.header.ipHeader.ip_len);
 }
 //-----------------------------------------------------------------------------
 inline uint32_t RAW_PACKET::GetSrcIP() const
 {
-return header.ipHeader.ip_src.s_addr;
+return rawPacket.header.ipHeader.ip_src.s_addr;
 }
 //-----------------------------------------------------------------------------
 inline uint32_t RAW_PACKET::GetDstIP() const
 {
-return header.ipHeader.ip_dst.s_addr;
+return rawPacket.header.ipHeader.ip_dst.s_addr;
 }
 //-----------------------------------------------------------------------------
 inline uint16_t RAW_PACKET::GetSrcPort() const
 {
-if (header.ipHeader.ip_p == 1) // for icmp proto return port 0
+if (rawPacket.header.ipHeader.ip_p == 1) // for icmp proto return port 0
     return 0;
-return ntohs(*((uint16_t*)(pckt + header.ipHeader.ip_hl * 4)));
+const uint8_t * pos = rawPacket.pckt + rawPacket.header.ipHeader.ip_hl * 4;
+return ntohs(*reinterpret_cast<const uint16_t *>(pos));
 }
 //-----------------------------------------------------------------------------
 inline uint16_t RAW_PACKET::GetDstPort() const
 {
-if (header.ipHeader.ip_p == 1) // for icmp proto return port 0
+if (rawPacket.header.ipHeader.ip_p == 1) // for icmp proto return port 0
     return 0;
-return ntohs(*((uint16_t*)(pckt + header.ipHeader.ip_hl * 4 + 2)));
+const uint8_t * pos = rawPacket.pckt + rawPacket.header.ipHeader.ip_hl * 4 + 2;
+return ntohs(*reinterpret_cast<const uint16_t *>(pos));
 }
 //-----------------------------------------------------------------------------
 inline bool RAW_PACKET::operator==(const RAW_PACKET & rvalue) const
 {
-if (header.ipHeader.ip_src.s_addr != rvalue.header.ipHeader.ip_src.s_addr)
+if (rawPacket.header.ipHeader.ip_src.s_addr != rvalue.rawPacket.header.ipHeader.ip_src.s_addr)
     return false;
 
-if (header.ipHeader.ip_dst.s_addr != rvalue.header.ipHeader.ip_dst.s_addr)
+if (rawPacket.header.ipHeader.ip_dst.s_addr != rvalue.rawPacket.header.ipHeader.ip_dst.s_addr)
     return false;
 
-if (header.ipHeader.ip_p != 1 && rvalue.header.ipHeader.ip_p != 1)
+if (rawPacket.header.ipHeader.ip_p != 1 && rvalue.rawPacket.header.ipHeader.ip_p != 1)
     {
-    if (*((uint16_t *)(pckt + header.ipHeader.ip_hl * 4)) !=
-        *((uint16_t *)(rvalue.pckt + rvalue.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<const uint16_t *>(pos) != *reinterpret_cast<const uint16_t *>(rpos))
         return false;
 
-    if (*((uint16_t *)(pckt + header.ipHeader.ip_hl * 4 + 2)) !=
-        *((uint16_t *)(rvalue.pckt + rvalue.header.ipHeader.ip_hl * 4 + 2)))
+    pos += 2;
+    rpos += 2;
+    if (*reinterpret_cast<const uint16_t *>(pos) != *reinterpret_cast<const uint16_t *>(rpos))
         return false;
     }
 
-if (header.ipHeader.ip_p != rvalue.header.ipHeader.ip_p)
+if (rawPacket.header.ipHeader.ip_p != rvalue.rawPacket.header.ipHeader.ip_p)
     return false;
 
 return true;
@@ -131,34 +127,34 @@ return true;
 //-----------------------------------------------------------------------------
 inline bool RAW_PACKET::operator<(const RAW_PACKET & rvalue) const
 {
-if (header.ipHeader.ip_src.s_addr < rvalue.header.ipHeader.ip_src.s_addr) 
+if (rawPacket.header.ipHeader.ip_src.s_addr < rvalue.rawPacket.header.ipHeader.ip_src.s_addr) 
     return true;
-if (header.ipHeader.ip_src.s_addr > rvalue.header.ipHeader.ip_src.s_addr) 
+if (rawPacket.header.ipHeader.ip_src.s_addr > rvalue.rawPacket.header.ipHeader.ip_src.s_addr) 
     return false;
 
-if (header.ipHeader.ip_dst.s_addr < rvalue.header.ipHeader.ip_dst.s_addr) 
+if (rawPacket.header.ipHeader.ip_dst.s_addr < rvalue.rawPacket.header.ipHeader.ip_dst.s_addr) 
     return true;
-if (header.ipHeader.ip_dst.s_addr > rvalue.header.ipHeader.ip_dst.s_addr) 
+if (rawPacket.header.ipHeader.ip_dst.s_addr > rvalue.rawPacket.header.ipHeader.ip_dst.s_addr) 
     return false;
 
-if (header.ipHeader.ip_p != 1 && rvalue.header.ipHeader.ip_p != 1)
+if (rawPacket.header.ipHeader.ip_p != 1 && rvalue.rawPacket.header.ipHeader.ip_p != 1)
     {
-    if (*((uint16_t *)(pckt + header.ipHeader.ip_hl * 4)) <
-        *((uint16_t *)(rvalue.pckt + rvalue.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<const uint16_t *>(pos) < *reinterpret_cast<const uint16_t *>(rpos))
         return true;
-    if (*((uint16_t *)(pckt + header.ipHeader.ip_hl * 4)) >
-        *((uint16_t *)(rvalue.pckt + rvalue.header.ipHeader.ip_hl * 4))) 
+    if (*reinterpret_cast<const uint16_t *>(pos) > *reinterpret_cast<const uint16_t *>(rpos))
         return false;
 
-    if (*((uint16_t *)(pckt + header.ipHeader.ip_hl * 4 + 2)) <
-        *((uint16_t *)(rvalue.pckt + rvalue.header.ipHeader.ip_hl * 4 + 2))) 
+    pos += 2;
+    rpos += 2;
+    if (*reinterpret_cast<const uint16_t *>(pos) < *reinterpret_cast<const uint16_t *>(rpos))
         return true;
-    if (*((uint16_t *)(pckt + header.ipHeader.ip_hl * 4 + 2)) >
-        *((uint16_t *)(rvalue.pckt + rvalue.header.ipHeader.ip_hl * 4 + 2))) 
+    if (*reinterpret_cast<const uint16_t *>(pos) > *reinterpret_cast<const uint16_t *>(rpos))
         return false;
     }
 
-if (header.ipHeader.ip_p < rvalue.header.ipHeader.ip_p) 
+if (rawPacket.header.ipHeader.ip_p < rvalue.rawPacket.header.ipHeader.ip_p) 
     return true;
 
 return false;