forked from mdlayher/arp
/
request.go
63 lines (51 loc) · 1.65 KB
/
request.go
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
package arp
import (
"errors"
"net"
"github.com/mdlayher/ethernet"
)
var (
// errInvalidARPPacket is returned when an ethernet frame does not
// indicate that an ARP packet is contained in its payload.
errInvalidARPPacket = errors.New("invalid ARP packet")
)
// A Request is a processed ARP request received by a server. Its fields
// contain information regarding the request's operation, sender information,
// and target information.
type Request struct {
// Operation specifies the ARP operation being performed, such as request
// or reply.
Operation Operation
// SenderHardwareAddr specifies the hardware address of the sender of this
// Request.
SenderHardwareAddr net.HardwareAddr
// SenderIP specifies the IPv4 address of the sender of this Request.
SenderIP net.IP
// TargetHardwareAddr specifies the hardware address of the target of this
// Request.
TargetHardwareAddr net.HardwareAddr
// TargetIP specifies the IPv4 address of the target of this Request.
TargetIP net.IP
}
// parseRequest unmarshals a raw ethernet frame and an ARP packet into a Request.
func parseRequest(buf []byte) (*Request, error) {
f := new(ethernet.Frame)
if err := f.UnmarshalBinary(buf); err != nil {
return nil, err
}
// Ignore frames which do not have ARP EtherType
if f.EtherType != ethernet.EtherTypeARP {
return nil, errInvalidARPPacket
}
p := new(Packet)
if err := p.UnmarshalBinary(f.Payload); err != nil {
return nil, err
}
return &Request{
Operation: p.Operation,
SenderHardwareAddr: p.SenderHardwareAddr,
SenderIP: p.SenderIP,
TargetHardwareAddr: p.TargetHardwareAddr,
TargetIP: p.TargetIP,
}, nil
}