]> git.stg.codes - stg.git/blob - tests/raw_ip_packet_old.h
Some minor changes in bf stream.
[stg.git] / tests / raw_ip_packet_old.h
1 #ifndef RAW_IP_PACKET_OLD_H
2 #define RAW_IP_PACKET_OLD_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
11 #define IPv4 (2)
12
13 enum { pcktSizeOLD = 68 }; //60(max) ip + 8 udp or tcp (part of tcp or udp header to ports)
14 //-----------------------------------------------------------------------------
15 struct RAW_PACKET_OLD
16 {
17     RAW_PACKET_OLD()
18         : dataLen(-1)
19     {
20     memset(pckt, 0, pcktSizeOLD);
21     }
22
23     RAW_PACKET_OLD(const RAW_PACKET_OLD & rp)
24         : dataLen(rp.dataLen)
25     {
26     memcpy(pckt, rp.pckt, pcktSizeOLD);
27     }
28
29 uint16_t    GetIPVersion() const;
30 uint8_t     GetHeaderLen() const;
31 uint8_t     GetProto() const;
32 uint32_t    GetLen() const;
33 uint32_t    GetSrcIP() const;
34 uint32_t    GetDstIP() const;
35 uint16_t    GetSrcPort() const;
36 uint16_t    GetDstPort() const;
37
38 uint8_t     pckt[pcktSizeOLD];         // îÁÞÁÌÏ ÐÁËÅÔÁ ÚÁÈ×ÁÞÅÎÎÏÇÏ ÉÚ ÓÅÔÉ
39 int32_t     dataLen;                // äÌÉÎÁ IP ÐÁËÅÔÁ. åÓÌÉ -1, ÔÏ ÉÓÐÏÌØÚÏ×ÁÔØ ÄÌÉÎÕ ÉÚ ÚÁÇÏÌÏ×ËÁ ÓÁÍÏÇÏ ÐÁËÅÔÁ.
40 };
41 //-----------------------------------------------------------------------------
42 inline uint16_t RAW_PACKET_OLD::GetIPVersion() const
43 {
44 return pckt[0] >> 4;
45 }
46 //-----------------------------------------------------------------------------
47 inline uint8_t RAW_PACKET_OLD::GetHeaderLen() const
48 {
49 return (pckt[0] & 0x0F) * 4;
50 }
51 //-----------------------------------------------------------------------------
52 inline uint8_t RAW_PACKET_OLD::GetProto() const
53 {
54 return pckt[9];
55 }
56 //-----------------------------------------------------------------------------
57 inline uint32_t RAW_PACKET_OLD::GetLen() const
58 {
59 if (dataLen != -1)
60     return dataLen;
61 return ntohs(*(uint16_t*)(pckt + 2));
62 }
63 //-----------------------------------------------------------------------------
64 inline uint32_t RAW_PACKET_OLD::GetSrcIP() const
65 {
66 return *(uint32_t*)(pckt + 12);
67 }
68 //-----------------------------------------------------------------------------
69 inline uint32_t RAW_PACKET_OLD::GetDstIP() const
70 {
71 return *(uint32_t*)(pckt + 16);
72 }
73 //-----------------------------------------------------------------------------
74 inline uint16_t RAW_PACKET_OLD::GetSrcPort() const
75 {
76 if (GetProto() == 1) // for icmp proto return port 0
77     return 0;
78 return ntohs(*((uint16_t*)(pckt + GetHeaderLen())));
79 }
80 //-----------------------------------------------------------------------------
81 inline uint16_t RAW_PACKET_OLD::GetDstPort() const
82 {
83 if (GetProto() == 1) // for icmp proto return port 0
84     return 0;
85 return ntohs(*((uint16_t*)(pckt + GetHeaderLen() + 2)));
86 }
87 //-----------------------------------------------------------------------------
88 inline bool operator==(const RAW_PACKET_OLD & lhs, const RAW_PACKET_OLD & rhs) 
89 {
90 if (lhs.GetSrcIP() != rhs.GetSrcIP())
91     return false;
92
93 if (lhs.GetDstIP() != rhs.GetDstIP())
94     return false;
95
96 if (lhs.GetSrcPort() != rhs.GetSrcPort())
97     return false;
98
99 if (lhs.GetDstPort() != rhs.GetDstPort())
100     return false;
101
102 if (lhs.GetProto() != rhs.GetProto())
103     return false;
104
105 return true;
106 }
107 //-----------------------------------------------------------------------------
108 inline bool operator<(const RAW_PACKET_OLD & lhs, const RAW_PACKET_OLD & rhs)
109 {
110 if (lhs.GetSrcIP() < rhs.GetSrcIP()) 
111     return true;
112 if (lhs.GetSrcIP() > rhs.GetSrcIP()) 
113     return false;
114
115 if (lhs.GetDstIP() < rhs.GetDstIP()) 
116     return true;
117 if (lhs.GetDstIP() > rhs.GetDstIP()) 
118     return false;
119
120 if (lhs.GetSrcPort() < rhs.GetSrcPort()) 
121     return true;
122 if (lhs.GetSrcPort() > rhs.GetSrcPort()) 
123     return false;
124
125 if (lhs.GetDstPort() < rhs.GetDstPort()) 
126     return true;
127 if (lhs.GetDstPort() > rhs.GetDstPort()) 
128     return false;
129
130 if (lhs.GetProto() < rhs.GetProto()) 
131     return true;
132
133 /*
134 Last compare
135
136 if (lhs.GetProto() > rhs.GetProto())
137     return false;
138
139 don't needed
140 */
141
142 return false;
143 }
144 //-----------------------------------------------------------------------------
145
146 #endif