-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathraw_socket_options.go
More file actions
191 lines (156 loc) · 4.5 KB
/
Copy pathraw_socket_options.go
File metadata and controls
191 lines (156 loc) · 4.5 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package rawsocket
import (
"net"
"time"
"github.com/google/gopacket/layers"
)
// TCPOpt configures a TCP packet builder.
type TCPOpt func(*TCP)
// UDPOpt configures a UDP packet builder.
type UDPOpt func(*UDP)
// ICMPOpt configures an ICMP packet builder.
type ICMPOpt func(*ICMP)
type IGMPOpt func(*IGMP)
type ESPOpt func(*ESP)
type RawIPOpt func(*RawIP)
func NewTCP(opts ...TCPOpt) *TCP {
tcp := &TCP{Window: 65535}
for _, opt := range opts {
if opt != nil {
opt(tcp)
}
}
return tcp
}
func NewUDP(opts ...UDPOpt) *UDP {
udp := &UDP{}
for _, opt := range opts {
if opt != nil {
opt(udp)
}
}
return udp
}
func NewICMP(opts ...ICMPOpt) *ICMP {
icmp := &ICMP{Type: layers.CreateICMPv4TypeCode(layers.ICMPv4TypeEchoRequest, 0)}
for _, opt := range opts {
if opt != nil {
opt(icmp)
}
}
return icmp
}
func NewIGMP(opts ...IGMPOpt) *IGMP {
igmp := &IGMP{
Type: layers.IGMPMembershipQuery,
MaxResponseTime: 10 * time.Second,
GroupAddress: net.IPv4zero,
}
for _, opt := range opts {
if opt != nil {
opt(igmp)
}
}
return igmp
}
func NewESP(opts ...ESPOpt) *ESP {
esp := &ESP{
SPI: 1,
Sequence: 1,
}
for _, opt := range opts {
if opt != nil {
opt(esp)
}
}
return esp
}
func NewRawIP(opts ...RawIPOpt) *RawIP {
rawIP := &RawIP{
Protocol: layers.IPProtocol(255),
}
for _, opt := range opts {
if opt != nil {
opt(rawIP)
}
}
return rawIP
}
func WithTCPSYN(enabled bool) TCPOpt { return func(t *TCP) { t.SYN = enabled } }
func WithTCPACK(enabled bool) TCPOpt { return func(t *TCP) { t.ACK = enabled } }
func WithTCPRST(enabled bool) TCPOpt { return func(t *TCP) { t.RST = enabled } }
func WithTCPPSH(enabled bool) TCPOpt { return func(t *TCP) { t.PSH = enabled } }
func WithTCPFIN(enabled bool) TCPOpt { return func(t *TCP) { t.FIN = enabled } }
func WithTCPURG(enabled bool) TCPOpt { return func(t *TCP) { t.URG = enabled } }
func WithTCPECE(enabled bool) TCPOpt { return func(t *TCP) { t.ECE = enabled } }
func WithTCPCWR(enabled bool) TCPOpt { return func(t *TCP) { t.CWR = enabled } }
func WithTCPNS(enabled bool) TCPOpt { return func(t *TCP) { t.NS = enabled } }
func WithTCPSequence(seq uint32) TCPOpt {
return func(t *TCP) { t.Sequence = seq }
}
func WithTCPAckNumber(ack uint32) TCPOpt {
return func(t *TCP) { t.AckNum = ack }
}
func WithTCPWindow(window uint16) TCPOpt {
return func(t *TCP) { t.Window = window }
}
func WithTCPPayload(payload []byte) TCPOpt {
return func(t *TCP) { t.Payload = payload }
}
func WithTCPOptions(options ...layers.TCPOption) TCPOpt {
return func(t *TCP) { t.Options = append(t.Options, options...) }
}
func WithTCPLegitOptions(enabled bool) TCPOpt {
return func(t *TCP) { t.LegitOptions = enabled }
}
func WithUDPPayload(payload []byte) UDPOpt {
return func(u *UDP) { u.Payload = payload }
}
func WithICMPType(typeCode layers.ICMPv4TypeCode) ICMPOpt {
return func(i *ICMP) { i.Type = typeCode }
}
func WithICMPPayload(payload []byte) ICMPOpt {
return func(i *ICMP) { i.Payload = payload }
}
func WithIGMPType(t layers.IGMPType) IGMPOpt {
return func(i *IGMP) { i.Type = t }
}
func WithIGMPMaxResponseTime(d time.Duration) IGMPOpt {
return func(i *IGMP) { i.MaxResponseTime = d }
}
func WithIGMPGroupAddress(group net.IP) IGMPOpt {
return func(i *IGMP) { i.GroupAddress = group }
}
func WithESPSPI(spi uint32) ESPOpt {
return func(e *ESP) { e.SPI = spi }
}
func WithESPSequence(seq uint32) ESPOpt {
return func(e *ESP) { e.Sequence = seq }
}
func WithESPPayload(payload []byte) ESPOpt {
return func(e *ESP) { e.Payload = payload }
}
func WithRawIPProtocol(protocol layers.IPProtocol) RawIPOpt {
return func(r *RawIP) { r.Protocol = protocol }
}
func WithRawIPPayload(payload []byte) RawIPOpt {
return func(r *RawIP) { r.Payload = payload }
}
func BuildTCPPacket(src, dest net.TCPAddr, opts ...TCPOpt) ([]byte, error) {
return NewTCP(opts...).BuildWithError(src, dest)
}
func BuildUDPPacket(src, dest net.UDPAddr, opts ...UDPOpt) ([]byte, error) {
return NewUDP(opts...).BuildWithError(src, dest)
}
func BuildICMPPacket(src, dest net.IPAddr, opts ...ICMPOpt) ([]byte, error) {
return NewICMP(opts...).BuildWithError(src, dest)
}
func BuildIGMPPacket(src, dest net.IPAddr, opts ...IGMPOpt) ([]byte, error) {
return NewIGMP(opts...).BuildWithError(src, dest)
}
func BuildESPPacket(src, dest net.IPAddr, opts ...ESPOpt) ([]byte, error) {
return NewESP(opts...).BuildWithError(src, dest)
}
func BuildRawIPPacket(src, dest net.IPAddr, opts ...RawIPOpt) ([]byte, error) {
return NewRawIP(opts...).BuildWithError(src, dest)
}