2008-09-16 14:42:47 -06:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
// IP address manipulations
|
|
|
|
//
|
|
|
|
// IPv4 addresses are 4 bytes; IPv6 addresses are 16 bytes.
|
|
|
|
// An IPv4 address can be converted to an IPv6 address by
|
|
|
|
// adding a canonical prefix (10 zeros, 2 0xFFs).
|
|
|
|
// This library accepts either size of byte array but always
|
|
|
|
// returns 16-byte addresses.
|
|
|
|
|
2008-09-26 15:11:26 -06:00
|
|
|
package net
|
2008-09-16 14:42:47 -06:00
|
|
|
|
2011-03-15 12:05:07 -06:00
|
|
|
import "os"
|
|
|
|
|
2009-03-05 16:48:12 -07:00
|
|
|
// IP address lengths (bytes).
|
2009-01-20 15:40:40 -07:00
|
|
|
const (
|
2009-12-15 16:35:38 -07:00
|
|
|
IPv4len = 4
|
|
|
|
IPv6len = 16
|
2008-09-16 14:42:47 -06:00
|
|
|
)
|
|
|
|
|
2009-03-05 16:48:12 -07:00
|
|
|
// An IP is a single IP address, an array of bytes.
|
|
|
|
// Functions in this package accept either 4-byte (IP v4)
|
|
|
|
// or 16-byte (IP v6) arrays as input. Unless otherwise
|
|
|
|
// specified, functions in this package always return
|
|
|
|
// IP addresses in 16-byte form using the canonical
|
|
|
|
// embedding.
|
|
|
|
//
|
|
|
|
// Note that in this documentation, referring to an
|
|
|
|
// IP address as an IPv4 address or an IPv6 address
|
|
|
|
// is a semantic property of the address, not just the
|
|
|
|
// length of the byte array: a 16-byte array can still
|
|
|
|
// be an IPv4 address.
|
2009-11-05 00:16:46 -07:00
|
|
|
type IP []byte
|
2009-03-05 16:48:12 -07:00
|
|
|
|
|
|
|
// An IP mask is an IP address.
|
2009-11-05 00:16:46 -07:00
|
|
|
type IPMask []byte
|
2009-03-05 16:48:12 -07:00
|
|
|
|
|
|
|
// IPv4 returns the IP address (in 16-byte form) of the
|
|
|
|
// IPv4 address a.b.c.d.
|
|
|
|
func IPv4(a, b, c, d byte) IP {
|
2009-12-15 16:35:38 -07:00
|
|
|
p := make(IP, IPv6len)
|
2011-03-15 12:05:07 -06:00
|
|
|
copy(p, v4InV6Prefix)
|
2009-12-15 16:35:38 -07:00
|
|
|
p[12] = a
|
|
|
|
p[13] = b
|
|
|
|
p[14] = c
|
|
|
|
p[15] = d
|
|
|
|
return p
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
|
|
|
|
2011-03-15 12:05:07 -06:00
|
|
|
var v4InV6Prefix = []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff}
|
|
|
|
|
2010-03-16 15:16:33 -06:00
|
|
|
// IPv4Mask returns the IP mask (in 16-byte form) of the
|
|
|
|
// IPv4 mask a.b.c.d.
|
|
|
|
func IPv4Mask(a, b, c, d byte) IPMask {
|
|
|
|
p := make(IPMask, IPv6len)
|
|
|
|
for i := 0; i < 12; i++ {
|
|
|
|
p[i] = 0xff
|
|
|
|
}
|
|
|
|
p[12] = a
|
|
|
|
p[13] = b
|
|
|
|
p[14] = c
|
|
|
|
p[15] = d
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2009-03-05 16:48:12 -07:00
|
|
|
// Well-known IPv4 addresses
|
|
|
|
var (
|
2009-12-15 16:35:38 -07:00
|
|
|
IPv4bcast = IPv4(255, 255, 255, 255) // broadcast
|
|
|
|
IPv4allsys = IPv4(224, 0, 0, 1) // all systems
|
|
|
|
IPv4allrouter = IPv4(224, 0, 0, 2) // all routers
|
|
|
|
IPv4zero = IPv4(0, 0, 0, 0) // all zeros
|
2009-03-05 16:48:12 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
// Well-known IPv6 addresses
|
|
|
|
var (
|
2011-05-16 15:03:06 -06:00
|
|
|
IPv6zero = IP{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
|
|
|
|
IPv6unspecified = IP{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
|
|
|
|
IPv6loopback = IP{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}
|
|
|
|
IPv6interfacelocalallnodes = IP{0xff, 0x01, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x01}
|
|
|
|
IPv6linklocalallnodes = IP{0xff, 0x02, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x01}
|
|
|
|
IPv6linklocalallrouters = IP{0xff, 0x02, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x02}
|
2009-03-05 16:48:12 -07:00
|
|
|
)
|
2008-09-16 14:42:47 -06:00
|
|
|
|
2011-05-16 21:21:13 -06:00
|
|
|
// IsUnspecified returns true if ip is an unspecified address.
|
|
|
|
func (ip IP) IsUnspecified() bool {
|
|
|
|
if ip.Equal(IPv4zero) || ip.Equal(IPv6unspecified) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsLoopback returns true if ip is a loopback address.
|
|
|
|
func (ip IP) IsLoopback() bool {
|
|
|
|
if ip4 := ip.To4(); ip4 != nil && ip4[0] == 127 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return ip.Equal(IPv6loopback)
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsMulticast returns true if ip is a multicast address.
|
|
|
|
func (ip IP) IsMulticast() bool {
|
|
|
|
if ip4 := ip.To4(); ip4 != nil && ip4[0]&0xf0 == 0xe0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return ip[0] == 0xff
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsInterfaceLinkLocalMulticast returns true if ip is
|
|
|
|
// an interface-local multicast address.
|
|
|
|
func (ip IP) IsInterfaceLocalMulticast() bool {
|
|
|
|
return len(ip) == IPv6len && ip[0] == 0xff && ip[1]&0x0f == 0x01
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsLinkLinkLocalMulticast returns true if ip is a link-local
|
|
|
|
// multicast address.
|
|
|
|
func (ip IP) IsLinkLocalMulticast() bool {
|
|
|
|
if ip4 := ip.To4(); ip4 != nil && ip4[0] == 224 && ip4[1] == 0 && ip4[2] == 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return ip[0] == 0xff && ip[1]&0x0f == 0x02
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsLinkLinkLocalUnicast returns true if ip is a link-local
|
|
|
|
// unicast address.
|
|
|
|
func (ip IP) IsLinkLocalUnicast() bool {
|
|
|
|
if ip4 := ip.To4(); ip4 != nil && ip4[0] == 169 && ip4[1] == 254 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return ip[0] == 0xfe && ip[1]&0xc0 == 0x80
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsGlobalUnicast returns true if ip is a global unicast
|
|
|
|
// address.
|
|
|
|
func (ip IP) IsGlobalUnicast() bool {
|
|
|
|
return !ip.IsUnspecified() &&
|
|
|
|
!ip.IsLoopback() &&
|
|
|
|
!ip.IsMulticast() &&
|
|
|
|
!ip.IsLinkLocalUnicast()
|
|
|
|
}
|
|
|
|
|
2008-09-16 14:42:47 -06:00
|
|
|
// Is p all zeros?
|
2009-05-07 18:36:29 -06:00
|
|
|
func isZeros(p IP) bool {
|
2008-09-16 14:42:47 -06:00
|
|
|
for i := 0; i < len(p); i++ {
|
|
|
|
if p[i] != 0 {
|
2009-11-09 13:07:39 -07:00
|
|
|
return false
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
return true
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
|
|
|
|
2009-03-05 16:48:12 -07:00
|
|
|
// To4 converts the IPv4 address ip to a 4-byte representation.
|
|
|
|
// If ip is not an IPv4 address, To4 returns nil.
|
|
|
|
func (ip IP) To4() IP {
|
|
|
|
if len(ip) == IPv4len {
|
2009-11-09 13:07:39 -07:00
|
|
|
return ip
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
2009-11-05 00:16:46 -07:00
|
|
|
if len(ip) == IPv6len &&
|
|
|
|
isZeros(ip[0:10]) &&
|
|
|
|
ip[10] == 0xff &&
|
|
|
|
ip[11] == 0xff {
|
2009-11-09 13:07:39 -07:00
|
|
|
return ip[12:16]
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
return nil
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
|
|
|
|
2009-03-05 16:48:12 -07:00
|
|
|
// To16 converts the IP address ip to a 16-byte representation.
|
|
|
|
// If ip is not an IP address (it is the wrong length), To16 returns nil.
|
|
|
|
func (ip IP) To16() IP {
|
|
|
|
if len(ip) == IPv4len {
|
2009-11-09 13:07:39 -07:00
|
|
|
return IPv4(ip[0], ip[1], ip[2], ip[3])
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
2009-03-05 16:48:12 -07:00
|
|
|
if len(ip) == IPv6len {
|
2009-11-09 13:07:39 -07:00
|
|
|
return ip
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
return nil
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Default route masks for IPv4.
|
2009-01-20 15:40:40 -07:00
|
|
|
var (
|
2010-03-16 15:16:33 -06:00
|
|
|
classAMask = IPv4Mask(0xff, 0, 0, 0)
|
|
|
|
classBMask = IPv4Mask(0xff, 0xff, 0, 0)
|
|
|
|
classCMask = IPv4Mask(0xff, 0xff, 0xff, 0)
|
2008-09-16 14:42:47 -06:00
|
|
|
)
|
|
|
|
|
2009-03-05 16:48:12 -07:00
|
|
|
// DefaultMask returns the default IP mask for the IP address ip.
|
|
|
|
// Only IPv4 addresses have default masks; DefaultMask returns
|
|
|
|
// nil if ip is not a valid IPv4 address.
|
2009-11-05 00:16:46 -07:00
|
|
|
func (ip IP) DefaultMask() IPMask {
|
2009-03-05 16:48:12 -07:00
|
|
|
if ip = ip.To4(); ip == nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
return nil
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
|
|
|
switch true {
|
2009-03-05 16:48:12 -07:00
|
|
|
case ip[0] < 0x80:
|
2009-11-09 13:07:39 -07:00
|
|
|
return classAMask
|
2009-03-05 16:48:12 -07:00
|
|
|
case ip[0] < 0xC0:
|
2009-11-09 13:07:39 -07:00
|
|
|
return classBMask
|
2008-09-16 14:42:47 -06:00
|
|
|
default:
|
2009-11-09 13:07:39 -07:00
|
|
|
return classCMask
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
return nil // not reached
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
|
|
|
|
2011-03-15 12:05:07 -06:00
|
|
|
func allFF(b []byte) bool {
|
|
|
|
for _, c := range b {
|
|
|
|
if c != 0xff {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2009-03-05 16:48:12 -07:00
|
|
|
// Mask returns the result of masking the IP address ip with mask.
|
|
|
|
func (ip IP) Mask(mask IPMask) IP {
|
2009-12-15 16:35:38 -07:00
|
|
|
n := len(ip)
|
2011-03-15 12:05:07 -06:00
|
|
|
if len(mask) == 16 && len(ip) == 4 && allFF(mask[:12]) {
|
|
|
|
mask = mask[12:]
|
|
|
|
}
|
|
|
|
if len(mask) == 4 && len(ip) == 16 && bytesEqual(ip[:12], v4InV6Prefix) {
|
|
|
|
ip = ip[12:]
|
|
|
|
}
|
2008-09-16 14:42:47 -06:00
|
|
|
if n != len(mask) {
|
2009-11-09 13:07:39 -07:00
|
|
|
return nil
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
out := make(IP, n)
|
2008-09-16 14:42:47 -06:00
|
|
|
for i := 0; i < n; i++ {
|
2009-11-09 22:23:52 -07:00
|
|
|
out[i] = ip[i] & mask[i]
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
return out
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Convert i to decimal string.
|
|
|
|
func itod(i uint) string {
|
|
|
|
if i == 0 {
|
2009-11-09 13:07:39 -07:00
|
|
|
return "0"
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Assemble decimal in reverse order.
|
2009-12-15 16:35:38 -07:00
|
|
|
var b [32]byte
|
|
|
|
bp := len(b)
|
2008-09-16 14:42:47 -06:00
|
|
|
for ; i > 0; i /= 10 {
|
2009-12-15 16:35:38 -07:00
|
|
|
bp--
|
|
|
|
b[bp] = byte(i%10) + '0'
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
|
|
|
|
2009-12-15 16:35:38 -07:00
|
|
|
return string(b[bp:])
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Convert i to hexadecimal string.
|
|
|
|
func itox(i uint) string {
|
|
|
|
if i == 0 {
|
2009-11-09 13:07:39 -07:00
|
|
|
return "0"
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Assemble hexadecimal in reverse order.
|
2009-12-15 16:35:38 -07:00
|
|
|
var b [32]byte
|
|
|
|
bp := len(b)
|
2008-09-16 14:42:47 -06:00
|
|
|
for ; i > 0; i /= 16 {
|
2009-12-15 16:35:38 -07:00
|
|
|
bp--
|
|
|
|
b[bp] = "0123456789abcdef"[byte(i%16)]
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
|
|
|
|
2009-12-15 16:35:38 -07:00
|
|
|
return string(b[bp:])
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
|
|
|
|
2009-03-05 16:48:12 -07:00
|
|
|
// String returns the string form of the IP address ip.
|
|
|
|
// If the address is an IPv4 address, the string representation
|
|
|
|
// is dotted decimal ("74.125.19.99"). Otherwise the representation
|
|
|
|
// is IPv6 ("2001:4860:0:2001::68").
|
|
|
|
func (ip IP) String() string {
|
2009-12-15 16:35:38 -07:00
|
|
|
p := ip
|
2009-03-05 16:48:12 -07:00
|
|
|
|
2009-11-02 19:37:30 -07:00
|
|
|
if len(ip) == 0 {
|
2009-11-09 13:07:39 -07:00
|
|
|
return ""
|
2009-11-02 19:37:30 -07:00
|
|
|
}
|
|
|
|
|
2008-09-16 14:42:47 -06:00
|
|
|
// If IPv4, use dotted notation.
|
2009-03-05 16:48:12 -07:00
|
|
|
if p4 := p.To4(); len(p4) == 4 {
|
2010-09-11 22:06:45 -06:00
|
|
|
return itod(uint(p4[0])) + "." +
|
|
|
|
itod(uint(p4[1])) + "." +
|
|
|
|
itod(uint(p4[2])) + "." +
|
|
|
|
itod(uint(p4[3]))
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
|
|
|
if len(p) != IPv6len {
|
2009-11-09 13:07:39 -07:00
|
|
|
return "?"
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Find longest run of zeros.
|
2009-12-15 16:35:38 -07:00
|
|
|
e0 := -1
|
|
|
|
e1 := -1
|
2009-11-05 00:16:46 -07:00
|
|
|
for i := 0; i < 16; i += 2 {
|
2009-12-15 16:35:38 -07:00
|
|
|
j := i
|
2008-09-16 14:42:47 -06:00
|
|
|
for j < 16 && p[j] == 0 && p[j+1] == 0 {
|
2009-11-09 13:07:39 -07:00
|
|
|
j += 2
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
2009-11-05 00:16:46 -07:00
|
|
|
if j > i && j-i > e1-e0 {
|
2009-12-15 16:35:38 -07:00
|
|
|
e0 = i
|
|
|
|
e1 = j
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
|
|
|
}
|
2010-08-12 01:03:01 -06:00
|
|
|
// The symbol "::" MUST NOT be used to shorten just one 16 bit 0 field.
|
|
|
|
if e1-e0 <= 2 {
|
|
|
|
e0 = -1
|
|
|
|
e1 = -1
|
|
|
|
}
|
2008-09-16 14:42:47 -06:00
|
|
|
|
|
|
|
// Print with possible :: in place of run of zeros
|
2009-12-15 16:35:38 -07:00
|
|
|
var s string
|
2008-09-16 14:42:47 -06:00
|
|
|
for i := 0; i < 16; i += 2 {
|
|
|
|
if i == e0 {
|
2009-12-15 16:35:38 -07:00
|
|
|
s += "::"
|
|
|
|
i = e1
|
2008-09-16 14:42:47 -06:00
|
|
|
if i >= 16 {
|
2009-11-09 13:07:39 -07:00
|
|
|
break
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
|
|
|
} else if i > 0 {
|
2009-11-09 13:07:39 -07:00
|
|
|
s += ":"
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
s += itox((uint(p[i]) << 8) | uint(p[i+1]))
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
return s
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
|
|
|
|
2011-03-15 12:05:07 -06:00
|
|
|
// Equal returns true if ip and x are the same IP address.
|
|
|
|
// An IPv4 address and that same address in IPv6 form are
|
|
|
|
// considered to be equal.
|
|
|
|
func (ip IP) Equal(x IP) bool {
|
|
|
|
if len(ip) == len(x) {
|
|
|
|
return bytesEqual(ip, x)
|
|
|
|
}
|
|
|
|
if len(ip) == 4 && len(x) == 16 {
|
|
|
|
return bytesEqual(x[0:12], v4InV6Prefix) && bytesEqual(ip, x[12:])
|
|
|
|
}
|
|
|
|
if len(ip) == 16 && len(x) == 4 {
|
|
|
|
return bytesEqual(ip[0:12], v4InV6Prefix) && bytesEqual(ip[12:], x)
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func bytesEqual(x, y []byte) bool {
|
|
|
|
if len(x) != len(y) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for i, b := range x {
|
|
|
|
if y[i] != b {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2008-09-16 14:42:47 -06:00
|
|
|
// If mask is a sequence of 1 bits followed by 0 bits,
|
|
|
|
// return the number of 1 bits.
|
2009-05-07 18:36:29 -06:00
|
|
|
func simpleMaskLength(mask IPMask) int {
|
2010-03-16 15:16:33 -06:00
|
|
|
var n int
|
|
|
|
for i, v := range mask {
|
|
|
|
if v == 0xff {
|
|
|
|
n += 8
|
|
|
|
continue
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
2010-03-16 15:16:33 -06:00
|
|
|
// found non-ff byte
|
|
|
|
// count 1 bits
|
|
|
|
for v&0x80 != 0 {
|
|
|
|
n++
|
|
|
|
v <<= 1
|
|
|
|
}
|
|
|
|
// rest must be 0 bits
|
|
|
|
if v != 0 {
|
2009-11-09 13:07:39 -07:00
|
|
|
return -1
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
2010-03-16 15:16:33 -06:00
|
|
|
for i++; i < len(mask); i++ {
|
|
|
|
if mask[i] != 0 {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
return n
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
|
|
|
|
2009-03-05 16:48:12 -07:00
|
|
|
// String returns the string representation of mask.
|
|
|
|
// If the mask is in the canonical form--ones followed by zeros--the
|
|
|
|
// string representation is just the decimal number of ones.
|
|
|
|
// If the mask is in a non-canonical form, it is formatted
|
|
|
|
// as an IP address.
|
|
|
|
func (mask IPMask) String() string {
|
2008-09-16 14:42:47 -06:00
|
|
|
switch len(mask) {
|
|
|
|
case 4:
|
2009-12-15 16:35:38 -07:00
|
|
|
n := simpleMaskLength(mask)
|
2008-09-16 14:42:47 -06:00
|
|
|
if n >= 0 {
|
2009-11-09 13:07:39 -07:00
|
|
|
return itod(uint(n + (IPv6len-IPv4len)*8))
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
|
|
|
case 16:
|
2009-12-15 16:35:38 -07:00
|
|
|
n := simpleMaskLength(mask)
|
2010-03-16 15:16:33 -06:00
|
|
|
if n >= 12*8 {
|
|
|
|
return itod(uint(n - 12*8))
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
return IP(mask).String()
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Parse IPv4 address (d.d.d.d).
|
2009-03-05 16:48:12 -07:00
|
|
|
func parseIPv4(s string) IP {
|
2009-12-15 16:35:38 -07:00
|
|
|
var p [IPv4len]byte
|
|
|
|
i := 0
|
2008-09-16 14:42:47 -06:00
|
|
|
for j := 0; j < IPv4len; j++ {
|
2010-01-15 14:43:14 -07:00
|
|
|
if i >= len(s) {
|
|
|
|
// Missing octets.
|
|
|
|
return nil
|
|
|
|
}
|
2008-09-16 14:42:47 -06:00
|
|
|
if j > 0 {
|
|
|
|
if s[i] != '.' {
|
2009-11-09 13:07:39 -07:00
|
|
|
return nil
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
i++
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
|
|
|
var (
|
2009-12-15 16:35:38 -07:00
|
|
|
n int
|
|
|
|
ok bool
|
2008-09-16 14:42:47 -06:00
|
|
|
)
|
2009-12-15 16:35:38 -07:00
|
|
|
n, i, ok = dtoi(s, i)
|
2008-09-16 14:42:47 -06:00
|
|
|
if !ok || n > 0xFF {
|
2009-11-09 13:07:39 -07:00
|
|
|
return nil
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
p[j] = byte(n)
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
|
|
|
if i != len(s) {
|
2009-11-09 13:07:39 -07:00
|
|
|
return nil
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
return IPv4(p[0], p[1], p[2], p[3])
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Parse IPv6 address. Many forms.
|
|
|
|
// The basic form is a sequence of eight colon-separated
|
|
|
|
// 16-bit hex numbers separated by colons,
|
|
|
|
// as in 0123:4567:89ab:cdef:0123:4567:89ab:cdef.
|
|
|
|
// Two exceptions:
|
|
|
|
// * A run of zeros can be replaced with "::".
|
|
|
|
// * The last 32 bits can be in IPv4 form.
|
|
|
|
// Thus, ::ffff:1.2.3.4 is the IPv4 address 1.2.3.4.
|
2009-03-05 16:48:12 -07:00
|
|
|
func parseIPv6(s string) IP {
|
2009-12-15 16:35:38 -07:00
|
|
|
p := make(IP, 16)
|
|
|
|
ellipsis := -1 // position of ellipsis in p
|
|
|
|
i := 0 // index in string s
|
2008-09-16 14:42:47 -06:00
|
|
|
|
|
|
|
// Might have leading ellipsis
|
|
|
|
if len(s) >= 2 && s[0] == ':' && s[1] == ':' {
|
2009-12-15 16:35:38 -07:00
|
|
|
ellipsis = 0
|
|
|
|
i = 2
|
2008-09-17 14:49:23 -06:00
|
|
|
// Might be only ellipsis
|
|
|
|
if i == len(s) {
|
2009-11-09 13:07:39 -07:00
|
|
|
return p
|
2008-09-17 14:49:23 -06:00
|
|
|
}
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Loop, parsing hex numbers followed by colon.
|
2009-12-15 16:35:38 -07:00
|
|
|
j := 0
|
2010-05-21 21:25:08 -06:00
|
|
|
for j < IPv6len {
|
2008-09-16 14:42:47 -06:00
|
|
|
// Hex number.
|
2009-12-15 16:35:38 -07:00
|
|
|
n, i1, ok := xtoi(s, i)
|
2008-09-17 14:49:23 -06:00
|
|
|
if !ok || n > 0xFFFF {
|
2009-11-09 13:07:39 -07:00
|
|
|
return nil
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// If followed by dot, might be in trailing IPv4.
|
2008-09-17 14:49:23 -06:00
|
|
|
if i1 < len(s) && s[i1] == '.' {
|
2009-11-05 00:16:46 -07:00
|
|
|
if ellipsis < 0 && j != IPv6len-IPv4len {
|
2008-09-16 14:42:47 -06:00
|
|
|
// Not the right place.
|
2009-11-09 13:07:39 -07:00
|
|
|
return nil
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
|
|
|
if j+IPv4len > IPv6len {
|
|
|
|
// Not enough room.
|
2009-11-09 13:07:39 -07:00
|
|
|
return nil
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
p4 := parseIPv4(s[i:])
|
2008-12-19 04:05:37 -07:00
|
|
|
if p4 == nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
return nil
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
p[j] = p4[12]
|
|
|
|
p[j+1] = p4[13]
|
|
|
|
p[j+2] = p4[14]
|
|
|
|
p[j+3] = p4[15]
|
|
|
|
i = len(s)
|
|
|
|
j += 4
|
|
|
|
break
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Save this 16-bit chunk.
|
2009-12-15 16:35:38 -07:00
|
|
|
p[j] = byte(n >> 8)
|
|
|
|
p[j+1] = byte(n)
|
|
|
|
j += 2
|
2008-09-16 14:42:47 -06:00
|
|
|
|
|
|
|
// Stop at end of string.
|
2009-12-15 16:35:38 -07:00
|
|
|
i = i1
|
2008-09-16 14:42:47 -06:00
|
|
|
if i == len(s) {
|
2009-11-09 13:07:39 -07:00
|
|
|
break
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise must be followed by colon and more.
|
2011-04-14 21:49:51 -06:00
|
|
|
if s[i] != ':' || i+1 == len(s) {
|
2009-11-09 13:07:39 -07:00
|
|
|
return nil
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
i++
|
2008-09-16 14:42:47 -06:00
|
|
|
|
|
|
|
// Look for ellipsis.
|
2008-09-17 14:49:23 -06:00
|
|
|
if s[i] == ':' {
|
2009-12-15 16:35:38 -07:00
|
|
|
if ellipsis >= 0 { // already have one
|
2009-11-09 13:07:39 -07:00
|
|
|
return nil
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
ellipsis = j
|
|
|
|
if i++; i == len(s) { // can be at end
|
2009-11-09 13:07:39 -07:00
|
|
|
break
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Must have used entire string.
|
|
|
|
if i != len(s) {
|
2009-11-09 13:07:39 -07:00
|
|
|
return nil
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// If didn't parse enough, expand ellipsis.
|
|
|
|
if j < IPv6len {
|
|
|
|
if ellipsis < 0 {
|
2009-11-09 13:07:39 -07:00
|
|
|
return nil
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
n := IPv6len - j
|
2009-11-09 22:23:52 -07:00
|
|
|
for k := j - 1; k >= ellipsis; k-- {
|
2009-11-09 13:07:39 -07:00
|
|
|
p[k+n] = p[k]
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
2009-11-09 22:23:52 -07:00
|
|
|
for k := ellipsis + n - 1; k >= ellipsis; k-- {
|
2009-11-09 13:07:39 -07:00
|
|
|
p[k] = 0
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
return p
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
|
|
|
|
2011-03-28 21:28:42 -06:00
|
|
|
// A ParseError represents a malformed text string and the type of string that was expected.
|
|
|
|
type ParseError struct {
|
2011-03-15 12:05:07 -06:00
|
|
|
Type string
|
|
|
|
Text string
|
|
|
|
}
|
|
|
|
|
2011-03-28 21:28:42 -06:00
|
|
|
func (e *ParseError) String() string {
|
2011-03-15 12:05:07 -06:00
|
|
|
return "invalid " + e.Type + ": " + e.Text
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseIP(s string) IP {
|
|
|
|
if p := parseIPv4(s); p != nil {
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
if p := parseIPv6(s); p != nil {
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2009-03-05 16:48:12 -07:00
|
|
|
// ParseIP parses s as an IP address, returning the result.
|
|
|
|
// The string s can be in dotted decimal ("74.125.19.99")
|
|
|
|
// or IPv6 ("2001:4860:0:2001::68") form.
|
|
|
|
// If s is not a valid textual representation of an IP address,
|
|
|
|
// ParseIP returns nil.
|
|
|
|
func ParseIP(s string) IP {
|
2011-03-15 12:05:07 -06:00
|
|
|
if p := parseIPv4(s); p != nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
return p
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
return parseIPv6(s)
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
2011-03-15 12:05:07 -06:00
|
|
|
|
|
|
|
// ParseCIDR parses s as a CIDR notation IP address and mask,
|
2011-03-28 21:28:42 -06:00
|
|
|
// like "192.168.100.1/24", "2001:DB8::/48", as defined in
|
|
|
|
// RFC 4632 and RFC 4291.
|
2011-03-15 12:05:07 -06:00
|
|
|
func ParseCIDR(s string) (ip IP, mask IPMask, err os.Error) {
|
|
|
|
i := byteIndex(s, '/')
|
|
|
|
if i < 0 {
|
2011-03-28 21:28:42 -06:00
|
|
|
return nil, nil, &ParseError{"CIDR address", s}
|
2011-03-15 12:05:07 -06:00
|
|
|
}
|
|
|
|
ipstr, maskstr := s[:i], s[i+1:]
|
2011-03-28 21:28:42 -06:00
|
|
|
iplen := 4
|
|
|
|
ip = parseIPv4(ipstr)
|
|
|
|
if ip == nil {
|
|
|
|
iplen = 16
|
|
|
|
ip = parseIPv6(ipstr)
|
|
|
|
}
|
2011-03-15 12:05:07 -06:00
|
|
|
nn, i, ok := dtoi(maskstr, 0)
|
2011-03-28 21:28:42 -06:00
|
|
|
if ip == nil || !ok || i != len(maskstr) || nn < 0 || nn > 8*iplen {
|
|
|
|
return nil, nil, &ParseError{"CIDR address", s}
|
2011-03-15 12:05:07 -06:00
|
|
|
}
|
|
|
|
n := uint(nn)
|
2011-03-28 21:28:42 -06:00
|
|
|
if iplen == 4 {
|
2011-03-15 12:05:07 -06:00
|
|
|
v4mask := ^uint32(0xffffffff >> n)
|
2011-03-28 21:28:42 -06:00
|
|
|
mask = IPv4Mask(byte(v4mask>>24), byte(v4mask>>16), byte(v4mask>>8), byte(v4mask))
|
|
|
|
} else {
|
|
|
|
mask = make(IPMask, 16)
|
|
|
|
for i := 0; i < 16; i++ {
|
|
|
|
if n >= 8 {
|
|
|
|
mask[i] = 0xff
|
|
|
|
n -= 8
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
mask[i] = ^byte(0xff >> n)
|
|
|
|
n = 0
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// address must not have any bits not in mask
|
|
|
|
for i := range ip {
|
|
|
|
if ip[i]&^mask[i] != 0 {
|
|
|
|
return nil, nil, &ParseError{"CIDR address", s}
|
2011-03-15 12:05:07 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return ip, mask, nil
|
|
|
|
}
|