-
Notifications
You must be signed in to change notification settings - Fork 0
/
ipv6.go
50 lines (41 loc) · 987 Bytes
/
ipv6.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
package packemon
import "net"
// https://atmarkit.itmedia.co.jp/ait/articles/1201/05/news113.html
type IPv6 struct {
Version uint8 // 4bit
TrafficClass uint8
FlowLabel uint32 // 20bit
PayloadLength uint16
NextHeader uint8
HopLimit uint8
SrcAddr []uint8
DstAddr []uint8
Option []uint8
Data []byte
}
func ParsedIPv6(payload []byte) *IPv6 {
return &IPv6{
Version: payload[0] >> 4,
TrafficClass: payload[0]<<4 | payload[1]>>4,
// FlowLabel: ,
// PayloadLength: ,
NextHeader: payload[6],
HopLimit: payload[7],
SrcAddr: payload[8:24],
DstAddr: payload[24:40],
}
}
const (
IPv6_NEXT_HEADER_UDP = 0x11
IPv6_NEXT_HEADER_ICMPv6 = 0x3a
)
func (i *IPv6) StrSrcIPAddr() string {
return uintsToStrIPv6Addr(i.SrcAddr)
}
func (i *IPv6) StrDstIPAddr() string {
return uintsToStrIPv6Addr(i.DstAddr)
}
func uintsToStrIPv6Addr(byteAddr []uint8) string {
ipv6Addr := net.IP(byteAddr)
return ipv6Addr.To16().String()
}