-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnetutil.go
More file actions
145 lines (123 loc) · 3.6 KB
/
netutil.go
File metadata and controls
145 lines (123 loc) · 3.6 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
package main
import (
"log"
"net"
"strings"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
"github.com/google/gopacket/pcap"
"time"
"errors"
"bytes"
)
// ARPパケットの作成
func CreateARPPacket(iface *net.Interface, srcAddr net.IP, dstAddr net.IP) []byte {
eth := layers.Ethernet{
SrcMAC: iface.HardwareAddr,
DstMAC: net.HardwareAddr{0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
EthernetType: layers.EthernetTypeARP,
}
arp := layers.ARP{
AddrType: layers.LinkTypeEthernet,
Protocol: layers.EthernetTypeIPv4,
HwAddressSize: 6,
ProtAddressSize: 4,
Operation: layers.ARPRequest,
SourceHwAddress: []byte(iface.HardwareAddr),
SourceProtAddress: srcAddr.To4(),
DstHwAddress: []byte{0, 0, 0, 0, 0, 0},
DstProtAddress: dstAddr.To4(),
}
buf := gopacket.NewSerializeBuffer()
opts := gopacket.SerializeOptions{
FixLengths: true,
ComputeChecksums: true,
}
gopacket.SerializeLayers(buf, opts, ð, &arp)
return buf.Bytes()
}
// ICMPリクエストパケットの作成
func CreateICMPEchoRequestPacket(iface *net.Interface, srcAddr net.IP, dstAddr net.IP, mac string) []byte {
dstMac, _ := net.ParseMAC(mac)
eth := layers.Ethernet{
SrcMAC: iface.HardwareAddr,
DstMAC: dstMac,
EthernetType: layers.EthernetTypeIPv4,
}
ip4 := layers.IPv4{
SrcIP: srcAddr.To4(),
DstIP: dstAddr.To4(),
Version: 4,
TTL: 64,
Protocol: layers.IPProtocolICMPv4,
}
icmp4 := layers.ICMPv4{
TypeCode: layers.CreateICMPv4TypeCode(8, 00),
Seq: 1,
}
buf := gopacket.NewSerializeBuffer()
opts := gopacket.SerializeOptions{
FixLengths: true,
ComputeChecksums: true,
}
gopacket.SerializeLayers(buf, opts, ð, &ip4, &icmp4)
return buf.Bytes()
}
// Read ARPパケット
func ReadARPPacket(handle *pcap.Handle, dstIP []byte) (net.HardwareAddr, error) {
start := time.Now()
for {
if time.Since(start) > time.Second*3 {
return nil, errors.New("timeout reading ARP reply")
}
if err := handle.SetBPFFilter("arp"); err != nil {
log.Fatal(err)
}
data, _, err := handle.ReadPacketData()
if err == pcap.NextErrorTimeoutExpired {
continue
} else if err != nil {
return nil, err
}
packet := gopacket.NewPacket(data, layers.LayerTypeEthernet, gopacket.NoCopy)
if arpLayer := packet.Layer(layers.LayerTypeARP); arpLayer != nil {
arp := arpLayer.(*layers.ARP)
if bytes.Equal(arp.SourceProtAddress, dstIP) {
return net.HardwareAddr(arp.SourceHwAddress), nil
}
}
}
}
func ResolveMACByIP(targetIP string, deviceName string) (string, error) {
var(
snapshot_len int32 = 512
promiscuous bool = false
err error
timeout time.Duration = 10 * time.Millisecond
handle *pcap.Handle
)
iface, _ := net.InterfaceByName(deviceName)
srcIP := net.ParseIP(GetDeviceIP(deviceName)[0])
dstIP := net.ParseIP(targetIP)
handle, err = pcap.OpenLive(deviceName, snapshot_len, promiscuous, timeout)
if err != nil {
log.Fatal(err)
handle.Close()
return "", err
}
defer handle.Close()
go handle.WritePacketData(CreateARPPacket(iface, srcIP, dstIP))
dstMAC, err := ReadARPPacket(handle, dstIP.To4())
if err != nil {
log.Fatal(err)
handle.Close()
return "", err
}
return dstMAC.String(), nil
}
// NIC(ex: eth0)のIPアドレスを取ってくる
func GetDeviceIP(device string) []string{
iface, _ := net.InterfaceByName(device)
ips, _ := iface.Addrs()
return strings.Split(ips[0].String(), "/")[:1]
}