1 #ifndef RAW_IP_PACKET_H
2 #define RAW_IP_PACKET_H
4 #if defined(FREE_BSD) || defined(FREE_BSD5)
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 RAW_PACKET(const RAW_PACKET & rp)
30 memcpy(rawPacket.pckt, rp.rawPacket.pckt, pcktSize);
33 uint16_t GetIPVersion() const;
34 uint8_t GetHeaderLen() const;
35 uint8_t GetProto() const;
36 uint32_t GetLen() const;
37 uint32_t GetSrcIP() const;
38 uint32_t GetDstIP() const;
39 uint16_t GetSrcPort() const;
40 uint16_t GetDstPort() const;
42 bool operator==(const RAW_PACKET & rvalue) const;
43 bool operator!=(const RAW_PACKET & rvalue) const { return !(*this == rvalue); };
44 bool operator<(const RAW_PACKET & rvalue) const;
48 uint8_t pckt[pcktSize]; // Packet header as a raw data
52 // Only for packets without options field
55 } header __attribute__ ((packed));
57 int32_t dataLen; // IP packet length. Set to -1 to use length field from the header
59 //-----------------------------------------------------------------------------
60 inline uint16_t RAW_PACKET::GetIPVersion() const
62 return rawPacket.header.ipHeader.ip_v;
64 //-----------------------------------------------------------------------------
65 inline uint8_t RAW_PACKET::GetHeaderLen() const
67 return rawPacket.header.ipHeader.ip_hl * 4;
69 //-----------------------------------------------------------------------------
70 inline uint8_t RAW_PACKET::GetProto() const
72 return rawPacket.header.ipHeader.ip_p;
74 //-----------------------------------------------------------------------------
75 inline uint32_t RAW_PACKET::GetLen() const
79 return ntohs(rawPacket.header.ipHeader.ip_len);
81 //-----------------------------------------------------------------------------
82 inline uint32_t RAW_PACKET::GetSrcIP() const
84 return rawPacket.header.ipHeader.ip_src.s_addr;
86 //-----------------------------------------------------------------------------
87 inline uint32_t RAW_PACKET::GetDstIP() const
89 return rawPacket.header.ipHeader.ip_dst.s_addr;
91 //-----------------------------------------------------------------------------
92 inline uint16_t RAW_PACKET::GetSrcPort() const
94 if (rawPacket.header.ipHeader.ip_p == 1) // for icmp proto return port 0
96 return ntohs(*((uint16_t*)(rawPacket.pckt + rawPacket.header.ipHeader.ip_hl * 4)));
98 //-----------------------------------------------------------------------------
99 inline uint16_t RAW_PACKET::GetDstPort() const
101 if (rawPacket.header.ipHeader.ip_p == 1) // for icmp proto return port 0
103 return ntohs(*((uint16_t*)(rawPacket.pckt + rawPacket.header.ipHeader.ip_hl * 4 + 2)));
105 //-----------------------------------------------------------------------------
106 inline bool RAW_PACKET::operator==(const RAW_PACKET & rvalue) const
108 if (rawPacket.header.ipHeader.ip_src.s_addr != rvalue.rawPacket.header.ipHeader.ip_src.s_addr)
111 if (rawPacket.header.ipHeader.ip_dst.s_addr != rvalue.rawPacket.header.ipHeader.ip_dst.s_addr)
114 if (rawPacket.header.ipHeader.ip_p != 1 && rvalue.rawPacket.header.ipHeader.ip_p != 1)
116 if (*((uint16_t *)(rawPacket.pckt + rawPacket.header.ipHeader.ip_hl * 4)) !=
117 *((uint16_t *)(rvalue.rawPacket.pckt + rvalue.rawPacket.header.ipHeader.ip_hl * 4)))
120 if (*((uint16_t *)(rawPacket.pckt + rawPacket.header.ipHeader.ip_hl * 4 + 2)) !=
121 *((uint16_t *)(rvalue.rawPacket.pckt + rvalue.rawPacket.header.ipHeader.ip_hl * 4 + 2)))
125 if (rawPacket.header.ipHeader.ip_p != rvalue.rawPacket.header.ipHeader.ip_p)
130 //-----------------------------------------------------------------------------
131 inline bool RAW_PACKET::operator<(const RAW_PACKET & rvalue) const
133 if (rawPacket.header.ipHeader.ip_src.s_addr < rvalue.rawPacket.header.ipHeader.ip_src.s_addr)
135 if (rawPacket.header.ipHeader.ip_src.s_addr > rvalue.rawPacket.header.ipHeader.ip_src.s_addr)
138 if (rawPacket.header.ipHeader.ip_dst.s_addr < rvalue.rawPacket.header.ipHeader.ip_dst.s_addr)
140 if (rawPacket.header.ipHeader.ip_dst.s_addr > rvalue.rawPacket.header.ipHeader.ip_dst.s_addr)
143 if (rawPacket.header.ipHeader.ip_p != 1 && rvalue.rawPacket.header.ipHeader.ip_p != 1)
145 if (*((uint16_t *)(rawPacket.pckt + rawPacket.header.ipHeader.ip_hl * 4)) <
146 *((uint16_t *)(rvalue.rawPacket.pckt + rvalue.rawPacket.header.ipHeader.ip_hl * 4)))
148 if (*((uint16_t *)(rawPacket.pckt + rawPacket.header.ipHeader.ip_hl * 4)) >
149 *((uint16_t *)(rvalue.rawPacket.pckt + rvalue.rawPacket.header.ipHeader.ip_hl * 4)))
152 if (*((uint16_t *)(rawPacket.pckt + rawPacket.header.ipHeader.ip_hl * 4 + 2)) <
153 *((uint16_t *)(rvalue.rawPacket.pckt + rvalue.rawPacket.header.ipHeader.ip_hl * 4 + 2)))
155 if (*((uint16_t *)(rawPacket.pckt + rawPacket.header.ipHeader.ip_hl * 4 + 2)) >
156 *((uint16_t *)(rvalue.rawPacket.pckt + rvalue.rawPacket.header.ipHeader.ip_hl * 4 + 2)))
160 if (rawPacket.header.ipHeader.ip_p < rvalue.rawPacket.header.ipHeader.ip_p)
165 //-----------------------------------------------------------------------------