-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathaf_inet_syscall.go
59 lines (51 loc) · 1.35 KB
/
af_inet_syscall.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
// pkg/pktgen/af_inet.go
package pktgen
import (
"context"
"log"
"net"
"syscall"
)
// AFInetSyscallSender is a Sender that uses the syscall package to send packets
type AFInetSyscallSender struct {
dstIP net.IP
dstPort uint16
payloadSize int
}
// NewAFInetSyscallSender creates a new AFInetSyscallSender
func NewAFInetSyscallSender(dstIP net.IP, dstPort, payloadSize int) *AFInetSyscallSender {
return &AFInetSyscallSender{
dstIP: dstIP,
dstPort: uint16(dstPort),
payloadSize: payloadSize,
}
}
// Send sends packets to the destination IP address
func (s *AFInetSyscallSender) Send(ctx context.Context) error {
fd, err := syscall.Socket(syscall.AF_INET, syscall.SOCK_DGRAM, syscall.IPPROTO_UDP)
if err != nil {
log.Fatalf("Failed to create socket: %v", err)
}
defer syscall.Close(fd)
// Assuming parsedDstIP is already an IPv4 address
ipv4DstIP := s.dstIP.To4()
if ipv4DstIP == nil {
log.Fatalf("Destination IP address is not an IPv4 address: %s", s.dstIP)
}
dstAddr := &syscall.SockaddrInet4{
Port: int(s.dstPort),
Addr: [4]byte{ipv4DstIP[0], ipv4DstIP[1], ipv4DstIP[2], ipv4DstIP[3]},
}
payload := buildPayload(s.payloadSize)
for {
select {
case <-ctx.Done():
return nil
default:
err = syscall.Sendto(fd, payload, 0, dstAddr)
if err != nil {
log.Fatalf("Failed to send packet: %v", err)
}
}
}
}