]> git.stg.codes - stg.git/blob - tests/raw_ip_packet_old.h
Add async pool (to replace EVENT_LOOP).
[stg.git] / tests / raw_ip_packet_old.h
1 #pragma once
2
3 #include "stg/const.h"
4
5 #include <cstring>
6 #include <cstdint>
7
8 #include <netinet/in.h> // for htons
9
10 enum { pcktSizeOLD = 68 }; //60(max) ip + 8 udp or tcp (part of tcp or udp header to ports)
11 //-----------------------------------------------------------------------------
12 struct RawPacketOld
13 {
14     RawPacketOld()
15         : dataLen(-1)
16     {
17         memset(pckt, 0, pcktSizeOLD);
18     }
19
20     RawPacketOld(const RawPacketOld& rp)
21         : dataLen(rp.dataLen)
22     {
23         memcpy(pckt, rp.pckt, pcktSizeOLD);
24     }
25
26     uint16_t GetIPVersion() const
27     {
28         return pckt[0] >> 4;
29     }
30     uint8_t  GetHeaderLen() const
31     {
32         return (pckt[0] & 0x0F) * 4;
33     }
34     uint8_t  GetProto() const
35     {
36         return pckt[9];
37     }
38     uint32_t GetLen() const
39     {
40         if (dataLen != -1)
41             return dataLen;
42         return ntohs(*reinterpret_cast<const uint16_t*>(pckt + 2));
43     }
44     uint32_t GetSrcIP() const
45     {
46         return *reinterpret_cast<const uint32_t*>(pckt + 12);
47     }
48     uint32_t GetDstIP() const
49     {
50         return *reinterpret_cast<const uint32_t*>(pckt + 16);
51     }
52     uint16_t GetSrcPort() const
53     {
54         if (GetProto() == 1) // for icmp proto return port 0
55             return 0;
56         return ntohs(*reinterpret_cast<const uint16_t*>(pckt + GetHeaderLen()));
57     }
58     uint16_t GetDstPort() const
59     {
60         if (GetProto() == 1) // for icmp proto return port 0
61             return 0;
62         return ntohs(*reinterpret_cast<const uint16_t*>(pckt + GetHeaderLen() + 2));
63     }
64
65     uint8_t  pckt[pcktSizeOLD];         // îÁÞÁÌÏ ÐÁËÅÔÁ ÚÁÈ×ÁÞÅÎÎÏÇÏ ÉÚ ÓÅÔÉ
66     int32_t  dataLen;                // äÌÉÎÁ IP ÐÁËÅÔÁ. åÓÌÉ -1, ÔÏ ÉÓÐÏÌØÚÏ×ÁÔØ ÄÌÉÎÕ ÉÚ ÚÁÇÏÌÏ×ËÁ ÓÁÍÏÇÏ ÐÁËÅÔÁ.
67 };
68 //-----------------------------------------------------------------------------
69 inline bool operator==(const RawPacketOld& lhs, const RawPacketOld& rhs)
70 {
71     if (lhs.GetSrcIP() != rhs.GetSrcIP())
72         return false;
73
74     if (lhs.GetDstIP() != rhs.GetDstIP())
75         return false;
76
77     if (lhs.GetSrcPort() != rhs.GetSrcPort())
78         return false;
79
80     if (lhs.GetDstPort() != rhs.GetDstPort())
81         return false;
82
83     if (lhs.GetProto() != rhs.GetProto())
84         return false;
85
86     return true;
87 }
88 //-----------------------------------------------------------------------------
89 inline bool operator<(const RawPacketOld& lhs, const RawPacketOld& rhs)
90 {
91     if (lhs.GetSrcIP() < rhs.GetSrcIP())
92         return true;
93     if (lhs.GetSrcIP() > rhs.GetSrcIP())
94         return false;
95
96     if (lhs.GetDstIP() < rhs.GetDstIP())
97         return true;
98     if (lhs.GetDstIP() > rhs.GetDstIP())
99         return false;
100
101     if (lhs.GetSrcPort() < rhs.GetSrcPort())
102         return true;
103     if (lhs.GetSrcPort() > rhs.GetSrcPort())
104         return false;
105
106     if (lhs.GetDstPort() < rhs.GetDstPort())
107         return true;
108     if (lhs.GetDstPort() > rhs.GetDstPort())
109         return false;
110
111     if (lhs.GetProto() < rhs.GetProto())
112         return true;
113
114     /*
115     Last compare
116
117     if (lhs.GetProto() > rhs.GetProto())
118         return false;
119
120     don't needed
121     */
122
123     return false;
124 }