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
|
|
|
|
2008-12-18 16:42:39 -07:00
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
)
|
|
|
|
|
2008-09-16 14:42:47 -06:00
|
|
|
export const (
|
|
|
|
IPv4len = 4;
|
|
|
|
IPv6len = 16
|
|
|
|
)
|
|
|
|
|
|
|
|
// Make the 4 bytes into an IPv4 address (in IPv6 form)
|
2008-12-18 23:37:22 -07:00
|
|
|
func MakeIPv4(a, b, c, d byte) []byte {
|
2008-10-07 13:31:31 -06:00
|
|
|
p := new([]byte, IPv6len);
|
2008-09-16 14:42:47 -06:00
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
p[i] = 0
|
|
|
|
}
|
|
|
|
p[10] = 0xff;
|
|
|
|
p[11] = 0xff;
|
|
|
|
p[12] = a;
|
|
|
|
p[13] = b;
|
|
|
|
p[14] = c;
|
2008-10-07 13:31:31 -06:00
|
|
|
p[15] = d;
|
2008-09-16 14:42:47 -06:00
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
// Well-known IP addresses
|
2008-12-18 23:37:22 -07:00
|
|
|
export var IPv4bcast, IPv4allsys, IPv4allrouter, IPv4prefix, IPallbits, IPnoaddr []byte
|
|
|
|
|
|
|
|
var NIL []byte // TODO(rsc)
|
2008-09-16 14:42:47 -06:00
|
|
|
|
|
|
|
func init() {
|
|
|
|
IPv4bcast = MakeIPv4(0xff, 0xff, 0xff, 0xff);
|
|
|
|
IPv4allsys = MakeIPv4(0xe0, 0x00, 0x00, 0x01);
|
|
|
|
IPv4allrouter = MakeIPv4(0xe0, 0x00, 0x00, 0x02);
|
|
|
|
IPv4prefix = MakeIPv4(0, 0, 0, 0);
|
|
|
|
IPallbits = new([]byte, IPv6len);
|
|
|
|
for i := 0; i < IPv6len; i++ {
|
|
|
|
IPallbits[i] = 0xff
|
|
|
|
}
|
|
|
|
IPnoaddr = new([]byte, IPv6len); // zeroed
|
|
|
|
}
|
|
|
|
|
|
|
|
// Is p all zeros?
|
2008-12-18 23:37:22 -07:00
|
|
|
func IsZeros(p []byte) bool {
|
2008-09-16 14:42:47 -06:00
|
|
|
for i := 0; i < len(p); i++ {
|
|
|
|
if p[i] != 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Is p an IPv4 address (perhaps in IPv6 form)?
|
|
|
|
// If so, return the 4-byte V4 array.
|
2008-12-18 23:37:22 -07:00
|
|
|
export func ToIPv4(p []byte) []byte {
|
2008-09-16 14:42:47 -06:00
|
|
|
if len(p) == IPv4len {
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
if len(p) == IPv6len
|
|
|
|
&& IsZeros(p[0:10])
|
|
|
|
&& p[10] == 0xff
|
|
|
|
&& p[11] == 0xff {
|
|
|
|
return p[12:16]
|
|
|
|
}
|
2008-12-18 23:37:22 -07:00
|
|
|
return NIL
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Convert p to IPv6 form.
|
2008-12-18 23:37:22 -07:00
|
|
|
export func ToIPv6(p []byte) []byte {
|
2008-09-16 14:42:47 -06:00
|
|
|
if len(p) == IPv4len {
|
|
|
|
return MakeIPv4(p[0], p[1], p[2], p[3])
|
|
|
|
}
|
|
|
|
if len(p) == IPv6len {
|
|
|
|
return p
|
|
|
|
}
|
2008-12-18 23:37:22 -07:00
|
|
|
return NIL
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Default route masks for IPv4.
|
|
|
|
export var (
|
|
|
|
ClassAMask = MakeIPv4(0xff, 0, 0, 0);
|
|
|
|
ClassBMask = MakeIPv4(0xff, 0xff, 0, 0);
|
|
|
|
ClassCMask = MakeIPv4(0xff, 0xff, 0xff, 0);
|
|
|
|
)
|
|
|
|
|
2008-12-18 23:37:22 -07:00
|
|
|
export func DefaultMask(p []byte) []byte {
|
|
|
|
if p = ToIPv4(p); len(p) == 0 {
|
|
|
|
return NIL
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
|
|
|
switch true {
|
|
|
|
case p[0] < 0x80:
|
|
|
|
return ClassAMask;
|
|
|
|
case p[0] < 0xC0:
|
|
|
|
return ClassBMask;
|
|
|
|
default:
|
|
|
|
return ClassCMask;
|
|
|
|
}
|
2008-12-18 23:37:22 -07:00
|
|
|
return NIL; // not reached
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Apply mask to ip, returning new address.
|
2008-12-18 23:37:22 -07:00
|
|
|
export func Mask(ip []byte, mask []byte) []byte {
|
2008-10-07 13:31:31 -06:00
|
|
|
n := len(ip);
|
2008-09-16 14:42:47 -06:00
|
|
|
if n != len(mask) {
|
2008-12-18 23:37:22 -07:00
|
|
|
return NIL
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
2008-10-07 13:31:31 -06:00
|
|
|
out := new([]byte, n);
|
2008-09-16 14:42:47 -06:00
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
out[i] = ip[i] & mask[i];
|
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert i to decimal string.
|
|
|
|
func itod(i uint) string {
|
|
|
|
if i == 0 {
|
|
|
|
return "0"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assemble decimal in reverse order.
|
|
|
|
var b [32]byte;
|
|
|
|
bp := len(b);
|
|
|
|
for ; i > 0; i /= 10 {
|
|
|
|
bp--;
|
|
|
|
b[bp] = byte(i%10) + '0'
|
|
|
|
}
|
|
|
|
|
2008-12-18 23:37:22 -07:00
|
|
|
return string(b[bp:len(b)])
|
|
|
|
// return string((&b)[bp:len(b)])
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Convert i to hexadecimal string.
|
|
|
|
func itox(i uint) string {
|
|
|
|
if i == 0 {
|
|
|
|
return "0"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assemble hexadecimal in reverse order.
|
|
|
|
var b [32]byte;
|
|
|
|
bp := len(b);
|
|
|
|
for ; i > 0; i /= 16 {
|
|
|
|
bp--;
|
|
|
|
b[bp] = "0123456789abcdef"[byte(i%16)]
|
|
|
|
}
|
|
|
|
|
2008-12-18 23:37:22 -07:00
|
|
|
return string(b[bp:len(b)])
|
|
|
|
// return string((&b)[bp:len(b)])
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Convert IP address to string.
|
2008-12-18 23:37:22 -07:00
|
|
|
export func IPToString(p []byte) string {
|
2008-09-16 14:42:47 -06:00
|
|
|
// If IPv4, use dotted notation.
|
2008-12-18 23:37:22 -07:00
|
|
|
if p4 := ToIPv4(p); len(p4) == 4 {
|
2008-09-16 14:42:47 -06:00
|
|
|
return itod(uint(p4[0]))+"."
|
|
|
|
+itod(uint(p4[1]))+"."
|
|
|
|
+itod(uint(p4[2]))+"."
|
|
|
|
+itod(uint(p4[3]))
|
|
|
|
}
|
|
|
|
if len(p) != IPv6len {
|
|
|
|
return "?"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find longest run of zeros.
|
|
|
|
e0 := -1;
|
2008-10-07 13:31:31 -06:00
|
|
|
e1 := -1;
|
2008-09-16 14:42:47 -06:00
|
|
|
for i := 0; i < 16; i+=2 {
|
2008-10-07 13:31:31 -06:00
|
|
|
j := i;
|
2008-09-16 14:42:47 -06:00
|
|
|
for j < 16 && p[j] == 0 && p[j+1] == 0 {
|
|
|
|
j += 2
|
|
|
|
}
|
|
|
|
if j > i && j - i > e1 - e0 {
|
|
|
|
e0 = i;
|
|
|
|
e1 = j
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Print with possible :: in place of run of zeros
|
|
|
|
var s string;
|
|
|
|
for i := 0; i < 16; i += 2 {
|
|
|
|
if i == e0 {
|
|
|
|
s += "::";
|
2008-10-07 13:31:31 -06:00
|
|
|
i = e1;
|
2008-09-16 14:42:47 -06:00
|
|
|
if i >= 16 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
} else if i > 0 {
|
|
|
|
s += ":"
|
|
|
|
}
|
|
|
|
s += itox((uint(p[i])<<8) | uint(p[i+1]))
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
// If mask is a sequence of 1 bits followed by 0 bits,
|
|
|
|
// return the number of 1 bits.
|
2008-12-18 23:37:22 -07:00
|
|
|
func SimpleMaskLength(mask []byte) int {
|
2008-10-07 13:31:31 -06:00
|
|
|
var i int;
|
2008-09-16 14:42:47 -06:00
|
|
|
for i = 0; i < len(mask); i++ {
|
|
|
|
if mask[i] != 0xFF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
n := 8*i;
|
2008-10-07 13:31:31 -06:00
|
|
|
v := mask[i];
|
2008-09-16 14:42:47 -06:00
|
|
|
for v & 0x80 != 0 {
|
2008-10-07 13:31:31 -06:00
|
|
|
n++;
|
2008-09-16 14:42:47 -06:00
|
|
|
v <<= 1
|
|
|
|
}
|
|
|
|
if v != 0 {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
for i++; i < len(mask); i++ {
|
|
|
|
if mask[i] != 0 {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
2008-12-18 23:37:22 -07:00
|
|
|
export func MaskToString(mask []byte) string {
|
2008-09-16 14:42:47 -06:00
|
|
|
switch len(mask) {
|
|
|
|
case 4:
|
2008-10-07 13:31:31 -06:00
|
|
|
n := SimpleMaskLength(mask);
|
2008-09-16 14:42:47 -06:00
|
|
|
if n >= 0 {
|
|
|
|
return itod(uint(n+(IPv6len-IPv4len)*8))
|
|
|
|
}
|
|
|
|
case 16:
|
2008-10-07 13:31:31 -06:00
|
|
|
n := SimpleMaskLength(mask);
|
2008-09-16 14:42:47 -06:00
|
|
|
if n >= 0 {
|
|
|
|
return itod(uint(n))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return IPToString(mask)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse IPv4 address (d.d.d.d).
|
2008-12-18 23:37:22 -07:00
|
|
|
func ParseIPv4(s string) []byte {
|
2008-10-07 13:31:31 -06:00
|
|
|
var p [IPv4len]byte;
|
|
|
|
i := 0;
|
2008-09-16 14:42:47 -06:00
|
|
|
for j := 0; j < IPv4len; j++ {
|
|
|
|
if j > 0 {
|
|
|
|
if s[i] != '.' {
|
2008-12-18 23:37:22 -07:00
|
|
|
return NIL
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
2008-10-07 13:31:31 -06:00
|
|
|
i++;
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
|
|
|
var (
|
|
|
|
n int;
|
|
|
|
ok bool
|
|
|
|
)
|
2008-12-18 16:42:39 -07:00
|
|
|
n, i, ok = Dtoi(s, i);
|
2008-09-16 14:42:47 -06:00
|
|
|
if !ok || n > 0xFF {
|
2008-12-18 23:37:22 -07:00
|
|
|
return NIL
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
|
|
|
p[j] = byte(n)
|
|
|
|
}
|
|
|
|
if i != len(s) {
|
2008-12-18 23:37:22 -07:00
|
|
|
return NIL
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
|
|
|
return MakeIPv4(p[0], p[1], p[2], p[3])
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2008-12-18 23:37:22 -07:00
|
|
|
func ParseIPv6(s string) []byte {
|
2008-09-16 14:42:47 -06:00
|
|
|
p := new([]byte, 16);
|
|
|
|
ellipsis := -1; // position of ellipsis in p
|
|
|
|
i := 0; // index in string s
|
|
|
|
|
|
|
|
// Might have leading ellipsis
|
|
|
|
if len(s) >= 2 && s[0] == ':' && s[1] == ':' {
|
|
|
|
ellipsis = 0;
|
2008-10-07 13:31:31 -06:00
|
|
|
i = 2;
|
2008-09-17 14:49:23 -06:00
|
|
|
// Might be only ellipsis
|
|
|
|
if i == len(s) {
|
|
|
|
return p
|
|
|
|
}
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Loop, parsing hex numbers followed by colon.
|
|
|
|
j := 0;
|
|
|
|
L: for j < IPv6len {
|
|
|
|
// Hex number.
|
2008-12-18 16:42:39 -07:00
|
|
|
n, i1, ok := Xtoi(s, i);
|
2008-09-17 14:49:23 -06:00
|
|
|
if !ok || n > 0xFFFF {
|
2008-12-18 23:37:22 -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] == '.' {
|
2008-09-16 14:42:47 -06:00
|
|
|
if ellipsis < 0 && j != IPv6len - IPv4len {
|
|
|
|
// Not the right place.
|
2008-12-18 23:37:22 -07:00
|
|
|
return NIL
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
|
|
|
if j+IPv4len > IPv6len {
|
|
|
|
// Not enough room.
|
2008-12-18 23:37:22 -07:00
|
|
|
return NIL
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
|
|
|
p4 := ParseIPv4(s[i:len(s)]);
|
2008-12-18 23:37:22 -07:00
|
|
|
if len(p4) == 0 {
|
|
|
|
return NIL
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
|
|
|
// BUG: p[j:j+4] = p4
|
|
|
|
p[j] = p4[12];
|
|
|
|
p[j+1] = p4[13];
|
|
|
|
p[j+2] = p4[14];
|
|
|
|
p[j+3] = p4[15];
|
|
|
|
i = len(s);
|
2008-10-07 13:31:31 -06:00
|
|
|
j += 4;
|
2008-09-16 14:42:47 -06:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save this 16-bit chunk.
|
|
|
|
p[j] = byte(n>>8);
|
|
|
|
p[j+1] = byte(n);
|
|
|
|
j += 2;
|
|
|
|
|
|
|
|
// Stop at end of string.
|
2008-10-07 13:31:31 -06:00
|
|
|
i = i1;
|
2008-09-16 14:42:47 -06:00
|
|
|
if i == len(s) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise must be followed by colon and more.
|
|
|
|
if s[i] != ':' && i+1 == len(s) {
|
2008-12-18 23:37:22 -07:00
|
|
|
return NIL
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
2008-10-07 13:31:31 -06:00
|
|
|
i++;
|
2008-09-16 14:42:47 -06:00
|
|
|
|
|
|
|
// Look for ellipsis.
|
2008-09-17 14:49:23 -06:00
|
|
|
if s[i] == ':' {
|
2008-09-16 14:42:47 -06:00
|
|
|
if ellipsis >= 0 { // already have one
|
2008-12-18 23:37:22 -07:00
|
|
|
return NIL
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
|
|
|
ellipsis = j;
|
|
|
|
if i++; i == len(s) { // can be at end
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Must have used entire string.
|
|
|
|
if i != len(s) {
|
2008-12-18 23:37:22 -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 {
|
2008-12-18 23:37:22 -07:00
|
|
|
return NIL
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
2008-10-07 13:31:31 -06:00
|
|
|
n := IPv6len - j;
|
2008-09-17 14:49:23 -06:00
|
|
|
for k := j-1; k >= ellipsis; k-- {
|
2008-09-16 14:42:47 -06:00
|
|
|
p[k+n] = p[k]
|
|
|
|
}
|
|
|
|
for k := ellipsis+n-1; k>=ellipsis; k-- {
|
|
|
|
p[k] = 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2008-12-18 23:37:22 -07:00
|
|
|
export func ParseIP(s string) []byte {
|
2008-10-07 13:31:31 -06:00
|
|
|
p := ParseIPv4(s);
|
2008-12-18 23:37:22 -07:00
|
|
|
if len(p) != 0 {
|
2008-09-16 14:42:47 -06:00
|
|
|
return p
|
|
|
|
}
|
|
|
|
return ParseIPv6(s)
|
|
|
|
}
|
|
|
|
|