-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathraw_socket_types.go
More file actions
105 lines (93 loc) · 2.69 KB
/
Copy pathraw_socket_types.go
File metadata and controls
105 lines (93 loc) · 2.69 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
package rawsocket
import (
"net"
"time"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
)
type ProtocolType int
// LinkType returns the corresponding gopacket.Decoder for the given ProtocolType.
func (t ProtocolType) LinkType() gopacket.Decoder {
switch t {
case IPPROTO_UDP:
return layers.LayerTypeUDP
case IPPROTO_ICMP:
if selfIPIsV4 {
return layers.LayerTypeICMPv4
}
return layers.LayerTypeICMPv6
case IPPROTO_IGMP:
return layers.LayerTypeIGMP
case IPPROTO_ESP:
return layers.LayerTypeIPSecESP
case IPPROTO_TCP:
return layers.LayerTypeTCP
case IPPROTO_IP, IPPROTO_RAW:
if selfIPIsV4 {
return layers.LayerTypeIPv4
}
return layers.LayerTypeIPv6
default:
return layers.LayerTypeEthernet
}
}
type TCP struct {
SYN, ACK, RST, PSH, FIN, URG, ECE, CWR, NS bool
Payload []byte
LegitOptions bool
Options []layers.TCPOption
Sequence uint32
Window uint16
AckNum uint32
}
type UDP struct {
Payload []byte
}
type ICMP struct {
Type layers.ICMPv4TypeCode
Payload []byte
}
type IGMP struct {
Type layers.IGMPType
MaxResponseTime time.Duration
GroupAddress net.IP
}
type ESP struct {
SPI uint32
Sequence uint32
Payload []byte
}
type RawIP struct {
Protocol layers.IPProtocol
Payload []byte
}
type WrappedPacket struct {
*net.IPAddr
gopacket.Packet
}
type RawSocket interface {
Write([]byte, net.Addr) (int, error)
Read([]byte) (int, net.Addr, error)
// SetReadDeadline sets the deadline for future Read calls. A
// zero value means no deadline. Returns an error if deadlines
// are not supported. Lets callers break out of a blocking Read
// on context cancellation without spawning a goroutine per
// packet or closing the socket.
SetReadDeadline(t time.Time) error
NextPacket() (gopacket.Packet, *net.IPAddr, error)
Iter() chan WrappedPacket
Close() error
// WriteRaw writes a pre-formatted frame directly to the wire
// without any gopacket re-parsing or re-serialization. The
// caller is responsible for including any required headers
// (ethernet, IP, transport). Returns the number of bytes written.
WriteRaw([]byte) (int, error)
// IsRawMode returns true when the socket operates in IP-raw mode
// (no ethernet framing needed). Returns false when ethernet
// framing is required (e.g. Windows pcap non-raw mode).
IsRawMode() bool
// MACs returns the source and router MAC addresses used for
// ethernet framing. Returns nil, nil in raw mode or when MACs
// are not available.
MACs() (src, dst net.HardwareAddr)
}