-
Notifications
You must be signed in to change notification settings - Fork 0
/
icmp.go
94 lines (80 loc) · 1.84 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package main
import (
"log"
"github.com/songgao/packets/ethernet"
"github.com/songgao/water"
)
type Icmp []byte
//Type
func (f Icmp) Type() byte {
//8 for echo message
//0 for echo reply message.
return f[0]
}
//Code
func (f Icmp) Code() byte {
//0 for ping/echo
return f[1]
}
//Checksum
func (f Icmp) Checksum() []byte {
//0 for ping/echo
return f[2:4]
}
//Identifier
func (f Icmp) Identifier() []byte {
return f[4:6]
}
//Sequence Number
func (f Icmp) SeqN() []byte {
return f[6:8]
}
//Sequence Number
func (f Icmp) Data() []byte {
return f[8:]
}
//checkSUM C
//codde from https://github.com/google/gopacket/blob/3aa782ce48d4a525acaebab344cedabfb561f870/layers/ip4.go#L158
func (f Icmp) CalcChecksum() uint16 {
// Clear checksum bytes
f[2] = 0
f[3] = 0
// Compute checksum
var csum uint32
for i := 0; i < len(f); i += 2 {
csum += uint32(f[i]) << 8
csum += uint32(f[i+1])
}
for {
// Break when sum is less or equals to 0xFFFF
if csum <= 65535 {
break
}
// Add carry to the sum
csum = (csum >> 16) + uint32(uint16(csum))
}
// Flip all the bits
return ^uint16(csum)
}
func handle_icmp(ifce *water.Interface, frame *ethernet.Frame, ipv4 *IPv4) {
var icmp Icmp = ipv4.Data()
log.Println("[ICMP] Type:", icmp.Type())
log.Println("[ICMP] Code:", icmp.Code())
log.Printf("[ICMP] Checksum: %x", icmp.Checksum())
log.Printf("[ICMP] Checksum: %x", icmp.CalcChecksum())
log.Printf("[ICMP] Identifier: %x", icmp.Identifier())
log.Printf("[ICMP] SeqN: %x", icmp.SeqN())
log.Printf("[ICMP] Data: % x \n", icmp.Data())
switch icmp.Type() {
case 8:
//ping
//swap address for IP
//change ICMP type to 0
var MacSrc = frame.Source()
var MacDst = frame.Destination()
var response ethernet.Frame
response.Prepare(MacSrc, MacDst, 0, ethernet.IPv4, len(*ipv4))
default:
log.Println("[ICMP] Uknown Code", icmp.Type())
}
}