6 #include <sys/types.h> // u_char, u_int32_t, etc.
7 #include <netinet/in_systm.h> // n_long in netinet/ip.h
10 #include <netinet/in.h> // for htons
11 #include <netinet/ip.h> // for struct ip
18 enum { packetSize = 68 }; //60(max) ip + 8 udp or tcp (part of tcp or udp header to ports)
19 //-----------------------------------------------------------------------------
25 memset(rawPacket.data, 0, packetSize);
28 RawPacket(const RawPacket& rhs) noexcept
30 memcpy(rawPacket.data, rhs.rawPacket.data, packetSize);
32 RawPacket& operator=(const RawPacket& rhs) noexcept
34 memcpy(rawPacket.data, rhs.rawPacket.data, packetSize);
37 RawPacket(RawPacket&& rhs) noexcept
39 memcpy(rawPacket.data, rhs.rawPacket.data, packetSize);
41 RawPacket& operator=(RawPacket&& rhs) noexcept
43 memcpy(rawPacket.data, rhs.rawPacket.data, packetSize);
47 uint16_t GetIPVersion() const noexcept;
48 uint8_t GetHeaderLen() const noexcept;
49 uint8_t GetProto() const noexcept;
50 uint32_t GetLen() const noexcept;
51 uint32_t GetSrcIP() const noexcept;
52 uint32_t GetDstIP() const noexcept;
53 uint16_t GetSrcPort() const noexcept;
54 uint16_t GetDstPort() const noexcept;
56 bool operator==(const RawPacket& rhs) const noexcept;
57 bool operator!=(const RawPacket& rhs) const noexcept { return !(*this == rhs); }
58 bool operator<(const RawPacket& rhs) const noexcept;
62 uint8_t data[packetSize]; // Packet header as a raw data
66 // Only for packets without options field
71 int32_t dataLen; // IP packet length. Set to -1 to use length field from the header
73 //-----------------------------------------------------------------------------
74 inline uint16_t RawPacket::GetIPVersion() const noexcept
76 return rawPacket.header.ipHeader.ip_v;
78 //-----------------------------------------------------------------------------
79 inline uint8_t RawPacket::GetHeaderLen() const noexcept
81 return rawPacket.header.ipHeader.ip_hl * 4;
83 //-----------------------------------------------------------------------------
84 inline uint8_t RawPacket::GetProto() const noexcept
86 return rawPacket.header.ipHeader.ip_p;
88 //-----------------------------------------------------------------------------
89 inline uint32_t RawPacket::GetLen() const noexcept
93 return ntohs(rawPacket.header.ipHeader.ip_len);
95 //-----------------------------------------------------------------------------
96 inline uint32_t RawPacket::GetSrcIP() const noexcept
98 return rawPacket.header.ipHeader.ip_src.s_addr;
100 //-----------------------------------------------------------------------------
101 inline uint32_t RawPacket::GetDstIP() const noexcept
103 return rawPacket.header.ipHeader.ip_dst.s_addr;
105 //-----------------------------------------------------------------------------
106 inline uint16_t RawPacket::GetSrcPort() const noexcept
108 if (rawPacket.header.ipHeader.ip_p == 1) // for icmp proto return port 0
110 const uint8_t* pos = rawPacket.data + rawPacket.header.ipHeader.ip_hl * 4;
111 return ntohs(*reinterpret_cast<const uint16_t *>(pos));
113 //-----------------------------------------------------------------------------
114 inline uint16_t RawPacket::GetDstPort() const noexcept
116 if (rawPacket.header.ipHeader.ip_p == 1) // for icmp proto return port 0
118 const uint8_t * pos = rawPacket.data + rawPacket.header.ipHeader.ip_hl * 4 + 2;
119 return ntohs(*reinterpret_cast<const uint16_t *>(pos));
121 //-----------------------------------------------------------------------------
122 inline bool RawPacket::operator==(const RawPacket& rhs) const noexcept
124 if (rawPacket.header.ipHeader.ip_src.s_addr != rhs.rawPacket.header.ipHeader.ip_src.s_addr)
127 if (rawPacket.header.ipHeader.ip_dst.s_addr != rhs.rawPacket.header.ipHeader.ip_dst.s_addr)
130 if (rawPacket.header.ipHeader.ip_p != 1 && rhs.rawPacket.header.ipHeader.ip_p != 1)
132 const uint8_t * pos = rawPacket.data + rawPacket.header.ipHeader.ip_hl * 4;
133 const uint8_t * rpos = rhs.rawPacket.data + rhs.rawPacket.header.ipHeader.ip_hl * 4;
134 if (*reinterpret_cast<const uint16_t *>(pos) != *reinterpret_cast<const uint16_t *>(rpos))
139 if (*reinterpret_cast<const uint16_t *>(pos) != *reinterpret_cast<const uint16_t *>(rpos))
143 if (rawPacket.header.ipHeader.ip_p != rhs.rawPacket.header.ipHeader.ip_p)
148 //-----------------------------------------------------------------------------
149 inline bool RawPacket::operator<(const RawPacket& rhs) const noexcept
151 if (rawPacket.header.ipHeader.ip_src.s_addr < rhs.rawPacket.header.ipHeader.ip_src.s_addr)
153 if (rawPacket.header.ipHeader.ip_src.s_addr > rhs.rawPacket.header.ipHeader.ip_src.s_addr)
156 if (rawPacket.header.ipHeader.ip_dst.s_addr < rhs.rawPacket.header.ipHeader.ip_dst.s_addr)
158 if (rawPacket.header.ipHeader.ip_dst.s_addr > rhs.rawPacket.header.ipHeader.ip_dst.s_addr)
161 if (rawPacket.header.ipHeader.ip_p != 1 && rhs.rawPacket.header.ipHeader.ip_p != 1)
163 const uint8_t * pos = rawPacket.data + rawPacket.header.ipHeader.ip_hl * 4;
164 const uint8_t * rpos = rhs.rawPacket.data + rhs.rawPacket.header.ipHeader.ip_hl * 4;
165 if (*reinterpret_cast<const uint16_t *>(pos) < *reinterpret_cast<const uint16_t *>(rpos))
167 if (*reinterpret_cast<const uint16_t *>(pos) > *reinterpret_cast<const uint16_t *>(rpos))
172 if (*reinterpret_cast<const uint16_t *>(pos) < *reinterpret_cast<const uint16_t *>(rpos))
174 if (*reinterpret_cast<const uint16_t *>(pos) > *reinterpret_cast<const uint16_t *>(rpos))
178 if (rawPacket.header.ipHeader.ip_p < rhs.rawPacket.header.ipHeader.ip_p)
183 //-----------------------------------------------------------------------------