-
Notifications
You must be signed in to change notification settings - Fork 1
/
udp.go
91 lines (82 loc) · 2.83 KB
/
udp.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
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
// Source: https://github.com/dimalinux/spoofsourceip/blob/e1554cd99d5fd7b5d3ba199fba4a3acc5308d5db/udpspoof/udp.go
package main
import (
"net"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
)
// UdpFrameOptions is a struct that holds all the information needed to create a UDP frame.
// @property sourceIP - The source IP address of the UDP packet.
// @property {uint16} sourcePort - The source port of the UDP packet.
// @property sourceMac - The MAC address of the sender.
// @property {bool} isIPv6 - Whether the frame is IPv6 or IPv4.
// @property {[]byte} payloadBytes - The bytes of the payload to be sent.
type UdpFrameOptions struct {
sourceIP, destIP net.IP
sourcePort, destPort uint16
sourceMac, destMac net.HardwareAddr
isIPv6 bool
payloadBytes []byte
}
// "A SerializableNetworkLayer is a gopacket.NetworkLayer that can be serialized to
// a buffer."
//
// The first thing to notice is that the type is an interface. This means that any
// type that implements the interface can be used as a SerializableNetworkLayer.
// The second thing to notice is that the interface has two methods:
// gopacket.NetworkLayer and SerializeTo. The first method is a method that is
// already defined in the gopacket library. The second method is a method that we
// are defining. This is the method that will be called by the
// serialization code to serialize the layer.
type SerializableNetworkLayer interface {
gopacket.NetworkLayer
SerializeTo(b gopacket.SerializeBuffer, opts gopacket.SerializeOptions) error
}
// CreateSerializedUDPFrame creates an Ethernet frame encapsulating our UDP
// packet for injection to the local network
func CreateSerializedUDPFrame(opts UdpFrameOptions) ([]byte, error) {
buf := gopacket.NewSerializeBuffer()
serializeOpts := gopacket.SerializeOptions{
FixLengths: true,
ComputeChecksums: true,
}
ethernetType := layers.EthernetTypeIPv4
if opts.isIPv6 {
ethernetType = layers.EthernetTypeIPv6
}
eth := &layers.Ethernet{
SrcMAC: opts.sourceMac,
DstMAC: opts.destMac,
EthernetType: ethernetType,
}
var ip SerializableNetworkLayer
if !opts.isIPv6 {
ip = &layers.IPv4{
SrcIP: opts.sourceIP,
DstIP: opts.destIP,
Protocol: layers.IPProtocolUDP,
Version: 4,
TTL: 32,
}
} else {
ip = &layers.IPv6{
SrcIP: opts.sourceIP,
DstIP: opts.destIP,
NextHeader: layers.IPProtocolUDP,
Version: 6,
HopLimit: 32,
}
ip.LayerType()
}
udp := &layers.UDP{
SrcPort: layers.UDPPort(opts.sourcePort),
DstPort: layers.UDPPort(opts.destPort),
// we configured "Length" and "Checksum" to be set for us
}
udp.SetNetworkLayerForChecksum(ip)
err := gopacket.SerializeLayers(buf, serializeOpts, eth, ip, udp, gopacket.Payload(opts.payloadBytes))
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}