|
|
@ -12,19 +12,25 @@ import (
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// IP6 is an IPv6 address.
|
|
|
|
// IP6 is an IPv6 address.
|
|
|
|
type IP6 [16]byte // TODO: maybe 2x uint64 would be faster for the type of ops we do?
|
|
|
|
type IP6 struct {
|
|
|
|
|
|
|
|
Hi, Lo uint64
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// IP6FromNetaddr converts a netaddr.IP to an IP6. Panics if !ip.Is6.
|
|
|
|
// IP6FromNetaddr converts a netaddr.IP to an IP6. Panics if !ip.Is6.
|
|
|
|
func IP6FromNetaddr(ip netaddr.IP) IP6 {
|
|
|
|
func IP6FromNetaddr(ip netaddr.IP) IP6 {
|
|
|
|
if !ip.Is6() {
|
|
|
|
if !ip.Is6() {
|
|
|
|
panic(fmt.Sprintf("IP6FromNetaddr called with non-v6 addr %q", ip))
|
|
|
|
panic(fmt.Sprintf("IP6FromNetaddr called with non-v6 addr %q", ip))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return IP6(ip.As16())
|
|
|
|
b := ip.As16()
|
|
|
|
|
|
|
|
return IP6{binary.BigEndian.Uint64(b[:8]), binary.BigEndian.Uint64(b[8:])}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Netaddr converts ip to a netaddr.IP.
|
|
|
|
// Netaddr converts ip to a netaddr.IP.
|
|
|
|
func (ip IP6) Netaddr() netaddr.IP {
|
|
|
|
func (ip IP6) Netaddr() netaddr.IP {
|
|
|
|
return netaddr.IPFrom16(ip)
|
|
|
|
var b [16]byte
|
|
|
|
|
|
|
|
binary.BigEndian.PutUint64(b[:8], ip.Hi)
|
|
|
|
|
|
|
|
binary.BigEndian.PutUint64(b[8:], ip.Lo)
|
|
|
|
|
|
|
|
return netaddr.IPFrom16(b)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (ip IP6) String() string {
|
|
|
|
func (ip IP6) String() string {
|
|
|
@ -32,11 +38,11 @@ func (ip IP6) String() string {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (ip IP6) IsMulticast() bool {
|
|
|
|
func (ip IP6) IsMulticast() bool {
|
|
|
|
return ip[0] == 0xFF
|
|
|
|
return (ip.Hi >> 56) == 0xFF
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (ip IP6) IsLinkLocalUnicast() bool {
|
|
|
|
func (ip IP6) IsLinkLocalUnicast() bool {
|
|
|
|
return ip[0] == 0xFE && ip[1] == 0x80
|
|
|
|
return (ip.Hi >> 48) == 0xFE80
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ip6HeaderLength is the length of an IPv6 header with no IP options.
|
|
|
|
// ip6HeaderLength is the length of an IPv6 header with no IP options.
|
|
|
@ -69,8 +75,10 @@ func (h IP6Header) Marshal(buf []byte) error {
|
|
|
|
binary.BigEndian.PutUint16(buf[4:6], uint16(len(buf)-ip6HeaderLength)) // Total length
|
|
|
|
binary.BigEndian.PutUint16(buf[4:6], uint16(len(buf)-ip6HeaderLength)) // Total length
|
|
|
|
buf[6] = uint8(h.IPProto) // Inner protocol
|
|
|
|
buf[6] = uint8(h.IPProto) // Inner protocol
|
|
|
|
buf[7] = 64 // TTL
|
|
|
|
buf[7] = 64 // TTL
|
|
|
|
copy(buf[8:24], h.SrcIP[:])
|
|
|
|
binary.BigEndian.PutUint64(buf[8:16], h.SrcIP.Hi)
|
|
|
|
copy(buf[24:40], h.DstIP[:])
|
|
|
|
binary.BigEndian.PutUint64(buf[16:24], h.SrcIP.Lo)
|
|
|
|
|
|
|
|
binary.BigEndian.PutUint64(buf[24:32], h.DstIP.Hi)
|
|
|
|
|
|
|
|
binary.BigEndian.PutUint64(buf[32:40], h.DstIP.Lo)
|
|
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -92,8 +100,10 @@ func (h IP6Header) marshalPseudo(buf []byte) error {
|
|
|
|
return errLargePacket
|
|
|
|
return errLargePacket
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
copy(buf[:16], h.SrcIP[:])
|
|
|
|
binary.BigEndian.PutUint64(buf[:8], h.SrcIP.Hi)
|
|
|
|
copy(buf[16:32], h.DstIP[:])
|
|
|
|
binary.BigEndian.PutUint64(buf[8:16], h.SrcIP.Lo)
|
|
|
|
|
|
|
|
binary.BigEndian.PutUint64(buf[16:24], h.DstIP.Hi)
|
|
|
|
|
|
|
|
binary.BigEndian.PutUint64(buf[24:32], h.DstIP.Lo)
|
|
|
|
binary.BigEndian.PutUint32(buf[32:36], uint32(len(buf)-h.Len()))
|
|
|
|
binary.BigEndian.PutUint32(buf[32:36], uint32(len(buf)-h.Len()))
|
|
|
|
buf[36] = 0
|
|
|
|
buf[36] = 0
|
|
|
|
buf[37] = 0
|
|
|
|
buf[37] = 0
|
|
|
|