1 #ifndef RAW_IP_PACKET_H
2 #define RAW_IP_PACKET_H
5 #include <netinet/in_systm.h> // n_long in netinet/ip.h
8 #include <netinet/in.h> // for htons
9 #include <netinet/ip.h> // for struct ip
15 enum { pcktSize = 68 }; //60(max) ip + 8 udp or tcp (part of tcp or udp header to ports)
16 //-----------------------------------------------------------------------------
23 memset(rawPacket.pckt, 0, pcktSize);
26 uint16_t GetIPVersion() const;
27 uint8_t GetHeaderLen() const;
28 uint8_t GetProto() const;
29 uint32_t GetLen() const;
30 uint32_t GetSrcIP() const;
31 uint32_t GetDstIP() const;
32 uint16_t GetSrcPort() const;
33 uint16_t GetDstPort() const;
35 bool operator==(const RAW_PACKET & rvalue) const;
36 bool operator!=(const RAW_PACKET & rvalue) const { return !(*this == rvalue); }
37 bool operator<(const RAW_PACKET & rvalue) const;
41 uint8_t pckt[pcktSize]; // Packet header as a raw data
45 // Only for packets without options field
50 int32_t dataLen; // IP packet length. Set to -1 to use length field from the header
52 //-----------------------------------------------------------------------------
53 inline uint16_t RAW_PACKET::GetIPVersion() const
55 return rawPacket.header.ipHeader.ip_v;
57 //-----------------------------------------------------------------------------
58 inline uint8_t RAW_PACKET::GetHeaderLen() const
60 return rawPacket.header.ipHeader.ip_hl * 4;
62 //-----------------------------------------------------------------------------
63 inline uint8_t RAW_PACKET::GetProto() const
65 return rawPacket.header.ipHeader.ip_p;
67 //-----------------------------------------------------------------------------
68 inline uint32_t RAW_PACKET::GetLen() const
72 return ntohs(rawPacket.header.ipHeader.ip_len);
74 //-----------------------------------------------------------------------------
75 inline uint32_t RAW_PACKET::GetSrcIP() const
77 return rawPacket.header.ipHeader.ip_src.s_addr;
79 //-----------------------------------------------------------------------------
80 inline uint32_t RAW_PACKET::GetDstIP() const
82 return rawPacket.header.ipHeader.ip_dst.s_addr;
84 //-----------------------------------------------------------------------------
85 inline uint16_t RAW_PACKET::GetSrcPort() const
87 if (rawPacket.header.ipHeader.ip_p == 1) // for icmp proto return port 0
89 const uint8_t * pos = rawPacket.pckt + rawPacket.header.ipHeader.ip_hl * 4;
90 return ntohs(*reinterpret_cast<const uint16_t *>(pos));
92 //-----------------------------------------------------------------------------
93 inline uint16_t RAW_PACKET::GetDstPort() const
95 if (rawPacket.header.ipHeader.ip_p == 1) // for icmp proto return port 0
97 const uint8_t * pos = rawPacket.pckt + rawPacket.header.ipHeader.ip_hl * 4 + 2;
98 return ntohs(*reinterpret_cast<const uint16_t *>(pos));
100 //-----------------------------------------------------------------------------
101 inline bool RAW_PACKET::operator==(const RAW_PACKET & rvalue) const
103 if (rawPacket.header.ipHeader.ip_src.s_addr != rvalue.rawPacket.header.ipHeader.ip_src.s_addr)
106 if (rawPacket.header.ipHeader.ip_dst.s_addr != rvalue.rawPacket.header.ipHeader.ip_dst.s_addr)
109 if (rawPacket.header.ipHeader.ip_p != 1 && rvalue.rawPacket.header.ipHeader.ip_p != 1)
111 const uint8_t * pos = rawPacket.pckt + rawPacket.header.ipHeader.ip_hl * 4;
112 const uint8_t * rpos = rvalue.rawPacket.pckt + rvalue.rawPacket.header.ipHeader.ip_hl * 4;
113 if (*reinterpret_cast<const uint16_t *>(pos) != *reinterpret_cast<const uint16_t *>(rpos))
118 if (*reinterpret_cast<const uint16_t *>(pos) != *reinterpret_cast<const uint16_t *>(rpos))
122 if (rawPacket.header.ipHeader.ip_p != rvalue.rawPacket.header.ipHeader.ip_p)
127 //-----------------------------------------------------------------------------
128 inline bool RAW_PACKET::operator<(const RAW_PACKET & rvalue) const
130 if (rawPacket.header.ipHeader.ip_src.s_addr < rvalue.rawPacket.header.ipHeader.ip_src.s_addr)
132 if (rawPacket.header.ipHeader.ip_src.s_addr > rvalue.rawPacket.header.ipHeader.ip_src.s_addr)
135 if (rawPacket.header.ipHeader.ip_dst.s_addr < rvalue.rawPacket.header.ipHeader.ip_dst.s_addr)
137 if (rawPacket.header.ipHeader.ip_dst.s_addr > rvalue.rawPacket.header.ipHeader.ip_dst.s_addr)
140 if (rawPacket.header.ipHeader.ip_p != 1 && rvalue.rawPacket.header.ipHeader.ip_p != 1)
142 const uint8_t * pos = rawPacket.pckt + rawPacket.header.ipHeader.ip_hl * 4;
143 const uint8_t * rpos = rvalue.rawPacket.pckt + rvalue.rawPacket.header.ipHeader.ip_hl * 4;
144 if (*reinterpret_cast<const uint16_t *>(pos) < *reinterpret_cast<const uint16_t *>(rpos))
146 if (*reinterpret_cast<const uint16_t *>(pos) > *reinterpret_cast<const uint16_t *>(rpos))
151 if (*reinterpret_cast<const uint16_t *>(pos) < *reinterpret_cast<const uint16_t *>(rpos))
153 if (*reinterpret_cast<const uint16_t *>(pos) > *reinterpret_cast<const uint16_t *>(rpos))
157 if (rawPacket.header.ipHeader.ip_p < rvalue.rawPacket.header.ipHeader.ip_p)
162 //-----------------------------------------------------------------------------