-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathraw_socket_builder_shared.go
More file actions
102 lines (87 loc) · 2.1 KB
/
Copy pathraw_socket_builder_shared.go
File metadata and controls
102 lines (87 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package rawsocket
import (
"encoding/binary"
"net"
"sync"
"time"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
"github.com/obeliskdev/fastrand"
)
var serializeOptions = gopacket.SerializeOptions{
FixLengths: true,
ComputeChecksums: true,
}
var decodeOptions = gopacket.DecodeOptions{
NoCopy: true,
}
var tcpOptionMSS1460 = []byte{0x05, 0xb4}
type udpBuildScratch struct {
buf gopacket.SerializeBuffer
ip4 layers.IPv4
ip6 layers.IPv6
udp layers.UDP
}
type icmpBuildScratch struct {
buf gopacket.SerializeBuffer
ip4 layers.IPv4
ip6 layers.IPv6
icmp layers.ICMPv4
}
type tcpBuildScratch struct {
buf gopacket.SerializeBuffer
ip4 layers.IPv4
ip6 layers.IPv6
tcp layers.TCP
options [8]layers.TCPOption
ws [1]byte
ts [8]byte
}
var udpBuildScratchPool = sync.Pool{
New: func() any {
return &udpBuildScratch{buf: gopacket.NewSerializeBuffer()}
},
}
var icmpBuildScratchPool = sync.Pool{
New: func() any {
return &icmpBuildScratch{buf: gopacket.NewSerializeBuffer()}
},
}
var tcpBuildScratchPool = sync.Pool{
New: func() any {
return &tcpBuildScratch{buf: gopacket.NewSerializeBuffer()}
},
}
func cloneSerializedBytes(buf gopacket.SerializeBuffer) []byte {
packet := buf.Bytes()
out := make([]byte, len(packet))
copy(out, packet)
return out
}
func fillTimestamp(dst []byte) {
binary.BigEndian.PutUint32(dst[:4], uint32(time.Now().UnixNano()))
binary.BigEndian.PutUint32(dst[4:], fastrand.Number[uint32](0, ^uint32(0)))
}
func prepareIPLayers(src, dest net.IP, protocol layers.IPProtocol, ip4 *layers.IPv4, ip6 *layers.IPv6) (gopacket.NetworkLayer, gopacket.SerializableLayer, bool) {
src4 := src.To4()
dest4 := dest.To4()
if src4 != nil && dest4 != nil {
*ip4 = layers.IPv4{
Version: 4,
Id: uint16(fastrand.Int(1, 65535)),
Flags: layers.IPv4DontFragment,
TTL: uint8(fastrand.Int(32, 255)),
Protocol: protocol,
SrcIP: src4,
DstIP: dest4,
}
return ip4, ip4, true
}
*ip6 = layers.IPv6{
Version: 6,
NextHeader: protocol,
SrcIP: src,
DstIP: dest,
}
return ip6, ip6, false
}