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