]> git.stg.codes - stg.git/blob - include/raw_ip_packet.h
cd89126630ecbeab42f3077cd0adbada1d1f1ade
[stg.git] / include / raw_ip_packet.h
1 #ifndef RAW_IP_PACKET_H
2 #define RAW_IP_PACKET_H
3
4 #if defined(FREE_BSD) || defined(FREE_BSD5)
5 #include <netinet/in_systm.h> // n_long in netinet/ip.h
6 #endif
7
8 #include <netinet/in.h> // for htons
9 #include <netinet/ip.h> // for struct ip
10
11 #include <cstring>
12
13 #include "stg_const.h"
14 #include "common.h"
15
16 #define IPv4 (2)
17
18 enum { pcktSize = 68 }; //60(max) ip + 8 udp or tcp (part of tcp or udp header to ports)
19 //-----------------------------------------------------------------------------
20 struct RAW_PACKET
21 {
22     RAW_PACKET()
23         : dataLen(-1)
24     {
25     memset(pckt, 0, pcktSize);
26     }
27
28     RAW_PACKET(const RAW_PACKET & rp)
29         : dataLen(rp.dataLen)
30     {
31     memcpy(pckt, rp.pckt, pcktSize);
32     }
33
34 uint16_t    GetIPVersion() const;
35 uint8_t     GetHeaderLen() const;
36 uint8_t     GetProto() const;
37 uint32_t    GetLen() const;
38 uint32_t    GetSrcIP() const;
39 uint32_t    GetDstIP() const;
40 uint16_t    GetSrcPort() const;
41 uint16_t    GetDstPort() const;
42
43 bool        operator==(const RAW_PACKET & rvalue) const;
44 bool        operator!=(const RAW_PACKET & rvalue) const { return !(*this == rvalue); };
45 bool        operator<(const RAW_PACKET & rvalue) const;
46
47 union
48     {
49     uint8_t     pckt[pcktSize];         // îÁÞÁÌÏ ÐÁËÅÔÁ ÚÁÈ×ÁÞÅÎÎÏÇÏ ÉÚ ÓÅÔÉ
50     struct
51         {
52         struct ip   ipHeader;
53         // Only for packets without options field
54         uint16_t    sPort;
55         uint16_t    dPort;
56         } header __attribute__ ((packed));
57     };
58 int32_t     dataLen;                // äÌÉÎÁ IP ÐÁËÅÔÁ. åÓÌÉ -1, ÔÏ ÉÓÐÏÌØÚÏ×ÁÔØ ÄÌÉÎÕ ÉÚ ÚÁÇÏÌÏ×ËÁ ÓÁÍÏÇÏ ÐÁËÅÔÁ.
59 };
60 //-----------------------------------------------------------------------------
61 inline uint16_t RAW_PACKET::GetIPVersion() const
62 {
63 return header.ipHeader.ip_v;
64 }
65 //-----------------------------------------------------------------------------
66 inline uint8_t RAW_PACKET::GetHeaderLen() const
67 {
68 return header.ipHeader.ip_hl * 4;
69 }
70 //-----------------------------------------------------------------------------
71 inline uint8_t RAW_PACKET::GetProto() const
72 {
73 return header.ipHeader.ip_p;
74 }
75 //-----------------------------------------------------------------------------
76 inline uint32_t RAW_PACKET::GetLen() const
77 {
78 if (dataLen != -1)
79     return dataLen;
80 return ntohs(header.ipHeader.ip_len);
81 }
82 //-----------------------------------------------------------------------------
83 inline uint32_t RAW_PACKET::GetSrcIP() const
84 {
85 return header.ipHeader.ip_src.s_addr;
86 }
87 //-----------------------------------------------------------------------------
88 inline uint32_t RAW_PACKET::GetDstIP() const
89 {
90 return header.ipHeader.ip_dst.s_addr;
91 }
92 //-----------------------------------------------------------------------------
93 inline uint16_t RAW_PACKET::GetSrcPort() const
94 {
95 if (header.ipHeader.ip_p == 1) // for icmp proto return port 0
96     return 0;
97 return ntohs(*((uint16_t*)(pckt + header.ipHeader.ip_hl * 4)));
98 }
99 //-----------------------------------------------------------------------------
100 inline uint16_t RAW_PACKET::GetDstPort() const
101 {
102 if (header.ipHeader.ip_p == 1) // for icmp proto return port 0
103     return 0;
104 return ntohs(*((uint16_t*)(pckt + header.ipHeader.ip_hl * 4 + 2)));
105 }
106 //-----------------------------------------------------------------------------
107 inline bool RAW_PACKET::operator==(const RAW_PACKET & rvalue) const
108 {
109 if (header.ipHeader.ip_src.s_addr != rvalue.header.ipHeader.ip_src.s_addr)
110     return false;
111
112 if (header.ipHeader.ip_dst.s_addr != rvalue.header.ipHeader.ip_dst.s_addr)
113     return false;
114
115 if (header.ipHeader.ip_p != 1 && rvalue.header.ipHeader.ip_p != 1)
116     {
117     if (*((uint16_t *)(pckt + header.ipHeader.ip_hl * 4)) !=
118         *((uint16_t *)(rvalue.pckt + rvalue.header.ipHeader.ip_hl * 4)))
119         return false;
120
121     if (*((uint16_t *)(pckt + header.ipHeader.ip_hl * 4 + 2)) !=
122         *((uint16_t *)(rvalue.pckt + rvalue.header.ipHeader.ip_hl * 4 + 2)))
123         return false;
124     }
125
126 if (header.ipHeader.ip_p != rvalue.header.ipHeader.ip_p)
127     return false;
128
129 return true;
130 }
131 /*//-----------------------------------------------------------------------------
132 inline bool operator==(const RAW_PACKET & lhs, const RAW_PACKET & rhs) 
133 {
134 if (lhs.GetSrcIP() != rhs.GetSrcIP())
135     return false;
136
137 if (lhs.GetDstIP() != rhs.GetDstIP())
138     return false;
139
140 if (lhs.GetSrcPort() != rhs.GetSrcPort())
141     return false;
142
143 if (lhs.GetDstPort() != rhs.GetDstPort())
144     return false;
145
146 if (lhs.GetProto() != rhs.GetProto())
147     return false;
148
149 return true;
150 }*/
151 //-----------------------------------------------------------------------------
152 inline bool RAW_PACKET::operator<(const RAW_PACKET & rvalue) const
153 {
154 if (header.ipHeader.ip_src.s_addr < rvalue.header.ipHeader.ip_src.s_addr) 
155     return true;
156 if (header.ipHeader.ip_src.s_addr > rvalue.header.ipHeader.ip_src.s_addr) 
157     return false;
158
159 if (header.ipHeader.ip_dst.s_addr < rvalue.header.ipHeader.ip_dst.s_addr) 
160     return true;
161 if (header.ipHeader.ip_dst.s_addr > rvalue.header.ipHeader.ip_dst.s_addr) 
162     return false;
163
164 if (header.ipHeader.ip_p != 1 && rvalue.header.ipHeader.ip_p != 1)
165     {
166     if (*((uint16_t *)(pckt + header.ipHeader.ip_hl * 4)) <
167         *((uint16_t *)(rvalue.pckt + rvalue.header.ipHeader.ip_hl * 4))) 
168         return true;
169     if (*((uint16_t *)(pckt + header.ipHeader.ip_hl * 4)) >
170         *((uint16_t *)(rvalue.pckt + rvalue.header.ipHeader.ip_hl * 4))) 
171         return false;
172
173     if (*((uint16_t *)(pckt + header.ipHeader.ip_hl * 4 + 2)) <
174         *((uint16_t *)(rvalue.pckt + rvalue.header.ipHeader.ip_hl * 4 + 2))) 
175         return true;
176     if (*((uint16_t *)(pckt + header.ipHeader.ip_hl * 4 + 2)) >
177         *((uint16_t *)(rvalue.pckt + rvalue.header.ipHeader.ip_hl * 4 + 2))) 
178         return false;
179     }
180
181 if (header.ipHeader.ip_p < rvalue.header.ipHeader.ip_p) 
182     return true;
183
184 return false;
185 }
186 //-----------------------------------------------------------------------------
187 /*inline bool operator<(const RAW_PACKET & lhs, const RAW_PACKET & rhs)
188 {
189 if (lhs.GetSrcIP() < rhs.GetSrcIP()) 
190     return true;
191 if (lhs.GetSrcIP() > rhs.GetSrcIP()) 
192     return false;
193
194 if (lhs.GetDstIP() < rhs.GetDstIP()) 
195     return true;
196 if (lhs.GetDstIP() > rhs.GetDstIP()) 
197     return false;
198
199 if (lhs.GetSrcPort() < rhs.GetSrcPort()) 
200     return true;
201 if (lhs.GetSrcPort() > rhs.GetSrcPort()) 
202     return false;
203
204 if (lhs.GetDstPort() < rhs.GetDstPort()) 
205     return true;
206 if (lhs.GetDstPort() > rhs.GetDstPort()) 
207     return false;
208
209 if (lhs.GetProto() < rhs.GetProto()) 
210     return true;
211
212 return false;
213 }*/
214 //-----------------------------------------------------------------------------
215
216 #endif