6 #include <netinet/in_systm.h> // n_long in netinet/ip.h
9 #include <netinet/in.h> // for htons
10 #include <netinet/ip.h> // for struct ip
17 enum { packetSize = 68 }; //60(max) ip + 8 udp or tcp (part of tcp or udp header to ports)
18 //-----------------------------------------------------------------------------
24 memset(rawPacket.data, 0, packetSize);
27 RawPacket(const RawPacket& rhs) noexcept
29 memcpy(rawPacket.data, rhs.rawPacket.data, packetSize);
31 RawPacket& operator=(const RawPacket& rhs) noexcept
33 memcpy(rawPacket.data, rhs.rawPacket.data, packetSize);
36 RawPacket(RawPacket&& rhs) noexcept
38 memcpy(rawPacket.data, rhs.rawPacket.data, packetSize);
40 RawPacket& operator=(RawPacket&& rhs) noexcept
42 memcpy(rawPacket.data, rhs.rawPacket.data, packetSize);
46 uint16_t GetIPVersion() const noexcept;
47 uint8_t GetHeaderLen() const noexcept;
48 uint8_t GetProto() const noexcept;
49 uint32_t GetLen() const noexcept;
50 uint32_t GetSrcIP() const noexcept;
51 uint32_t GetDstIP() const noexcept;
52 uint16_t GetSrcPort() const noexcept;
53 uint16_t GetDstPort() const noexcept;
55 bool operator==(const RawPacket& rhs) const noexcept;
56 bool operator!=(const RawPacket& rhs) const noexcept { return !(*this == rhs); }
57 bool operator<(const RawPacket& rhs) const noexcept;
61 uint8_t data[packetSize]; // Packet header as a raw data
65 // Only for packets without options field
70 int32_t dataLen; // IP packet length. Set to -1 to use length field from the header
72 //-----------------------------------------------------------------------------
73 inline uint16_t RawPacket::GetIPVersion() const noexcept
75 return rawPacket.header.ipHeader.ip_v;
77 //-----------------------------------------------------------------------------
78 inline uint8_t RawPacket::GetHeaderLen() const noexcept
80 return rawPacket.header.ipHeader.ip_hl * 4;
82 //-----------------------------------------------------------------------------
83 inline uint8_t RawPacket::GetProto() const noexcept
85 return rawPacket.header.ipHeader.ip_p;
87 //-----------------------------------------------------------------------------
88 inline uint32_t RawPacket::GetLen() const noexcept
92 return ntohs(rawPacket.header.ipHeader.ip_len);
94 //-----------------------------------------------------------------------------
95 inline uint32_t RawPacket::GetSrcIP() const noexcept
97 return rawPacket.header.ipHeader.ip_src.s_addr;
99 //-----------------------------------------------------------------------------
100 inline uint32_t RawPacket::GetDstIP() const noexcept
102 return rawPacket.header.ipHeader.ip_dst.s_addr;
104 //-----------------------------------------------------------------------------
105 inline uint16_t RawPacket::GetSrcPort() const noexcept
107 if (rawPacket.header.ipHeader.ip_p == 1) // for icmp proto return port 0
109 const uint8_t* pos = rawPacket.data + rawPacket.header.ipHeader.ip_hl * 4;
110 return ntohs(*reinterpret_cast<const uint16_t *>(pos));
112 //-----------------------------------------------------------------------------
113 inline uint16_t RawPacket::GetDstPort() const noexcept
115 if (rawPacket.header.ipHeader.ip_p == 1) // for icmp proto return port 0
117 const uint8_t * pos = rawPacket.data + rawPacket.header.ipHeader.ip_hl * 4 + 2;
118 return ntohs(*reinterpret_cast<const uint16_t *>(pos));
120 //-----------------------------------------------------------------------------
121 inline bool RawPacket::operator==(const RawPacket& rhs) const noexcept
123 if (rawPacket.header.ipHeader.ip_src.s_addr != rhs.rawPacket.header.ipHeader.ip_src.s_addr)
126 if (rawPacket.header.ipHeader.ip_dst.s_addr != rhs.rawPacket.header.ipHeader.ip_dst.s_addr)
129 if (rawPacket.header.ipHeader.ip_p != 1 && rhs.rawPacket.header.ipHeader.ip_p != 1)
131 const uint8_t * pos = rawPacket.data + rawPacket.header.ipHeader.ip_hl * 4;
132 const uint8_t * rpos = rhs.rawPacket.data + rhs.rawPacket.header.ipHeader.ip_hl * 4;
133 if (*reinterpret_cast<const uint16_t *>(pos) != *reinterpret_cast<const uint16_t *>(rpos))
138 if (*reinterpret_cast<const uint16_t *>(pos) != *reinterpret_cast<const uint16_t *>(rpos))
142 if (rawPacket.header.ipHeader.ip_p != rhs.rawPacket.header.ipHeader.ip_p)
147 //-----------------------------------------------------------------------------
148 inline bool RawPacket::operator<(const RawPacket& rhs) const noexcept
150 if (rawPacket.header.ipHeader.ip_src.s_addr < rhs.rawPacket.header.ipHeader.ip_src.s_addr)
152 if (rawPacket.header.ipHeader.ip_src.s_addr > rhs.rawPacket.header.ipHeader.ip_src.s_addr)
155 if (rawPacket.header.ipHeader.ip_dst.s_addr < rhs.rawPacket.header.ipHeader.ip_dst.s_addr)
157 if (rawPacket.header.ipHeader.ip_dst.s_addr > rhs.rawPacket.header.ipHeader.ip_dst.s_addr)
160 if (rawPacket.header.ipHeader.ip_p != 1 && rhs.rawPacket.header.ipHeader.ip_p != 1)
162 const uint8_t * pos = rawPacket.data + rawPacket.header.ipHeader.ip_hl * 4;
163 const uint8_t * rpos = rhs.rawPacket.data + rhs.rawPacket.header.ipHeader.ip_hl * 4;
164 if (*reinterpret_cast<const uint16_t *>(pos) < *reinterpret_cast<const uint16_t *>(rpos))
166 if (*reinterpret_cast<const uint16_t *>(pos) > *reinterpret_cast<const uint16_t *>(rpos))
171 if (*reinterpret_cast<const uint16_t *>(pos) < *reinterpret_cast<const uint16_t *>(rpos))
173 if (*reinterpret_cast<const uint16_t *>(pos) > *reinterpret_cast<const uint16_t *>(rpos))
177 if (rawPacket.header.ipHeader.ip_p < rhs.rawPacket.header.ipHeader.ip_p)
182 //-----------------------------------------------------------------------------