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
18 enum { pcktSize = 68 }; //60(max) ip + 8 udp or tcp (part of tcp or udp header to ports)
19 //-----------------------------------------------------------------------------
26 memset(rawPacket.pckt, 0, pcktSize);
29 RAW_PACKET(const RAW_PACKET & rp)
33 memcpy(rawPacket.pckt, rp.rawPacket.pckt, pcktSize);
36 uint16_t GetIPVersion() const;
37 uint8_t GetHeaderLen() const;
38 uint8_t GetProto() const;
39 uint32_t GetLen() const;
40 uint32_t GetSrcIP() const;
41 uint32_t GetDstIP() const;
42 uint16_t GetSrcPort() const;
43 uint16_t GetDstPort() const;
45 bool operator==(const RAW_PACKET & rvalue) const;
46 bool operator!=(const RAW_PACKET & rvalue) const { return !(*this == rvalue); };
47 bool operator<(const RAW_PACKET & rvalue) const;
51 uint8_t pckt[pcktSize]; // Packet header as a raw data
55 // Only for packets without options field
58 } header __attribute__ ((packed));
60 int32_t dataLen; // IP packet length. Set to -1 to use length field from the header
62 //-----------------------------------------------------------------------------
63 inline uint16_t RAW_PACKET::GetIPVersion() const
65 return rawPacket.header.ipHeader.ip_v;
67 //-----------------------------------------------------------------------------
68 inline uint8_t RAW_PACKET::GetHeaderLen() const
70 return rawPacket.header.ipHeader.ip_hl * 4;
72 //-----------------------------------------------------------------------------
73 inline uint8_t RAW_PACKET::GetProto() const
75 return rawPacket.header.ipHeader.ip_p;
77 //-----------------------------------------------------------------------------
78 inline uint32_t RAW_PACKET::GetLen() const
82 return ntohs(rawPacket.header.ipHeader.ip_len);
84 //-----------------------------------------------------------------------------
85 inline uint32_t RAW_PACKET::GetSrcIP() const
87 return rawPacket.header.ipHeader.ip_src.s_addr;
89 //-----------------------------------------------------------------------------
90 inline uint32_t RAW_PACKET::GetDstIP() const
92 return rawPacket.header.ipHeader.ip_dst.s_addr;
94 //-----------------------------------------------------------------------------
95 inline uint16_t RAW_PACKET::GetSrcPort() const
97 if (rawPacket.header.ipHeader.ip_p == 1) // for icmp proto return port 0
99 return ntohs(*((uint16_t*)(rawPacket.pckt + rawPacket.header.ipHeader.ip_hl * 4)));
101 //-----------------------------------------------------------------------------
102 inline uint16_t RAW_PACKET::GetDstPort() const
104 if (rawPacket.header.ipHeader.ip_p == 1) // for icmp proto return port 0
106 return ntohs(*((uint16_t*)(rawPacket.pckt + rawPacket.header.ipHeader.ip_hl * 4 + 2)));
108 //-----------------------------------------------------------------------------
109 inline bool RAW_PACKET::operator==(const RAW_PACKET & rvalue) const
111 if (rawPacket.header.ipHeader.ip_src.s_addr != rvalue.rawPacket.header.ipHeader.ip_src.s_addr)
114 if (rawPacket.header.ipHeader.ip_dst.s_addr != rvalue.rawPacket.header.ipHeader.ip_dst.s_addr)
117 if (rawPacket.header.ipHeader.ip_p != 1 && rvalue.rawPacket.header.ipHeader.ip_p != 1)
119 if (*((uint16_t *)(rawPacket.pckt + rawPacket.header.ipHeader.ip_hl * 4)) !=
120 *((uint16_t *)(rvalue.rawPacket.pckt + rvalue.rawPacket.header.ipHeader.ip_hl * 4)))
123 if (*((uint16_t *)(rawPacket.pckt + rawPacket.header.ipHeader.ip_hl * 4 + 2)) !=
124 *((uint16_t *)(rvalue.rawPacket.pckt + rvalue.rawPacket.header.ipHeader.ip_hl * 4 + 2)))
128 if (rawPacket.header.ipHeader.ip_p != rvalue.rawPacket.header.ipHeader.ip_p)
133 //-----------------------------------------------------------------------------
134 inline bool RAW_PACKET::operator<(const RAW_PACKET & rvalue) const
136 if (rawPacket.header.ipHeader.ip_src.s_addr < rvalue.rawPacket.header.ipHeader.ip_src.s_addr)
138 if (rawPacket.header.ipHeader.ip_src.s_addr > rvalue.rawPacket.header.ipHeader.ip_src.s_addr)
141 if (rawPacket.header.ipHeader.ip_dst.s_addr < rvalue.rawPacket.header.ipHeader.ip_dst.s_addr)
143 if (rawPacket.header.ipHeader.ip_dst.s_addr > rvalue.rawPacket.header.ipHeader.ip_dst.s_addr)
146 if (rawPacket.header.ipHeader.ip_p != 1 && rvalue.rawPacket.header.ipHeader.ip_p != 1)
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)))
151 if (*((uint16_t *)(rawPacket.pckt + rawPacket.header.ipHeader.ip_hl * 4)) >
152 *((uint16_t *)(rvalue.rawPacket.pckt + rvalue.rawPacket.header.ipHeader.ip_hl * 4)))
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)))
158 if (*((uint16_t *)(rawPacket.pckt + rawPacket.header.ipHeader.ip_hl * 4 + 2)) >
159 *((uint16_t *)(rvalue.rawPacket.pckt + rvalue.rawPacket.header.ipHeader.ip_hl * 4 + 2)))
163 if (rawPacket.header.ipHeader.ip_p < rvalue.rawPacket.header.ipHeader.ip_p)
168 //-----------------------------------------------------------------------------