-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathicmp.go
76 lines (66 loc) · 1.72 KB
/
icmp.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
package tcpip
import (
"fmt"
"log"
"syscall"
)
type ICMP struct {
Type []byte
Code []byte
CheckSum []byte
Identification []byte
SequenceNumber []byte
Data []byte
}
func NewICMP() ICMP {
// https://www.infraexpert.com/study/tcpip4.html
icmp := ICMP{
// ping request
Type: []byte{0x08},
Code: []byte{0x00},
CheckSum: []byte{0x00, 0x00},
Identification: []byte{0x00, 0x10},
SequenceNumber: []byte{0x00, 0x01},
Data: []byte{0x01, 0x02},
}
icmpsum := sumByteArr(toByteArr(icmp))
icmp.CheckSum = checksum(icmpsum)
return icmp
}
func (*ICMP) Send(ifindex int, packet []byte) ICMP {
addr := syscall.SockaddrLinklayer{
Protocol: syscall.ETH_P_IP,
Ifindex: ifindex,
}
sendfd, err := syscall.Socket(syscall.AF_PACKET, syscall.SOCK_RAW, int(htons(syscall.ETH_P_ALL)))
if err != nil {
log.Fatalf("create icmp sendfd err : %v\n", err)
}
err = syscall.Sendto(sendfd, packet, 0, &addr)
if err != nil {
log.Fatalf("Send to err : %v\n", err)
}
fmt.Println("send icmp packet")
for {
recvBuf := make([]byte, 1500)
_, _, err := syscall.Recvfrom(sendfd, recvBuf, 0)
if err != nil {
log.Fatalf("read err : %v", err)
}
// IPヘッダのProtocolがICMPであることをチェック
if recvBuf[23] == 0x01 {
// Ethernetが14byte, IPヘッダが20byteなので34byte目からがICMPパケット
return parseICMP(recvBuf[34:])
}
}
}
func parseICMP(packet []byte) ICMP {
return ICMP{
Type: []byte{packet[0]},
Code: []byte{packet[1]},
CheckSum: []byte{packet[2], packet[3]},
Identification: []byte{packet[4], packet[5]},
SequenceNumber: []byte{packet[6], packet[7]},
Data: packet[8:],
}
}