summaryrefslogtreecommitdiff
path: root/src/pkg/net/ip.go
diff options
context:
space:
mode:
Diffstat (limited to 'src/pkg/net/ip.go')
-rw-r--r--src/pkg/net/ip.go182
1 files changed, 91 insertions, 91 deletions
diff --git a/src/pkg/net/ip.go b/src/pkg/net/ip.go
index db1552890..e090d3aa6 100644
--- a/src/pkg/net/ip.go
+++ b/src/pkg/net/ip.go
@@ -14,8 +14,8 @@ package net
// IP address lengths (bytes).
const (
- IPv4len = 4;
- IPv6len = 16;
+ IPv4len = 4
+ IPv6len = 16
)
// An IP is a single IP address, an array of bytes.
@@ -38,30 +38,30 @@ type IPMask []byte
// 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 {
- p := make(IP, IPv6len);
+ p := make(IP, IPv6len)
for i := 0; i < 10; i++ {
p[i] = 0
}
- p[10] = 0xff;
- p[11] = 0xff;
- p[12] = a;
- p[13] = b;
- p[14] = c;
- p[15] = d;
- return p;
+ p[10] = 0xff
+ p[11] = 0xff
+ p[12] = a
+ p[13] = b
+ p[14] = c
+ p[15] = d
+ return p
}
// Well-known IPv4 addresses
var (
- 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
+ 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
)
// Well-known IPv6 addresses
var (
- IPzero = make(IP, IPv6len); // all zeros
+ IPzero = make(IP, IPv6len) // all zeros
)
// Is p all zeros?
@@ -71,7 +71,7 @@ func isZeros(p IP) bool {
return false
}
}
- return true;
+ return true
}
// To4 converts the IPv4 address ip to a 4-byte representation.
@@ -86,7 +86,7 @@ func (ip IP) To4() IP {
ip[11] == 0xff {
return ip[12:16]
}
- return nil;
+ return nil
}
// To16 converts the IP address ip to a 16-byte representation.
@@ -98,14 +98,14 @@ func (ip IP) To16() IP {
if len(ip) == IPv6len {
return ip
}
- return nil;
+ return nil
}
// Default route masks for IPv4.
var (
- classAMask = IPMask(IPv4(0xff, 0, 0, 0));
- classBMask = IPMask(IPv4(0xff, 0xff, 0, 0));
- classCMask = IPMask(IPv4(0xff, 0xff, 0xff, 0));
+ classAMask = IPMask(IPv4(0xff, 0, 0, 0))
+ classBMask = IPMask(IPv4(0xff, 0xff, 0, 0))
+ classCMask = IPMask(IPv4(0xff, 0xff, 0xff, 0))
)
// DefaultMask returns the default IP mask for the IP address ip.
@@ -123,20 +123,20 @@ func (ip IP) DefaultMask() IPMask {
default:
return classCMask
}
- return nil; // not reached
+ return nil // not reached
}
// Mask returns the result of masking the IP address ip with mask.
func (ip IP) Mask(mask IPMask) IP {
- n := len(ip);
+ n := len(ip)
if n != len(mask) {
return nil
}
- out := make(IP, n);
+ out := make(IP, n)
for i := 0; i < n; i++ {
out[i] = ip[i] & mask[i]
}
- return out;
+ return out
}
// Convert i to decimal string.
@@ -146,14 +146,14 @@ func itod(i uint) string {
}
// Assemble decimal in reverse order.
- var b [32]byte;
- bp := len(b);
+ var b [32]byte
+ bp := len(b)
for ; i > 0; i /= 10 {
- bp--;
- b[bp] = byte(i%10) + '0';
+ bp--
+ b[bp] = byte(i%10) + '0'
}
- return string(b[bp:]);
+ return string(b[bp:])
}
// Convert i to hexadecimal string.
@@ -163,14 +163,14 @@ func itox(i uint) string {
}
// Assemble hexadecimal in reverse order.
- var b [32]byte;
- bp := len(b);
+ var b [32]byte
+ bp := len(b)
for ; i > 0; i /= 16 {
- bp--;
- b[bp] = "0123456789abcdef"[byte(i%16)];
+ bp--
+ b[bp] = "0123456789abcdef"[byte(i%16)]
}
- return string(b[bp:]);
+ return string(b[bp:])
}
// String returns the string form of the IP address ip.
@@ -178,7 +178,7 @@ func itox(i uint) string {
// is dotted decimal ("74.125.19.99"). Otherwise the representation
// is IPv6 ("2001:4860:0:2001::68").
func (ip IP) String() string {
- p := ip;
+ p := ip
if len(ip) == 0 {
return ""
@@ -196,50 +196,50 @@ func (ip IP) String() string {
}
// Find longest run of zeros.
- e0 := -1;
- e1 := -1;
+ e0 := -1
+ e1 := -1
for i := 0; i < 16; i += 2 {
- j := i;
+ j := i
for j < 16 && p[j] == 0 && p[j+1] == 0 {
j += 2
}
if j > i && j-i > e1-e0 {
- e0 = i;
- e1 = j;
+ e0 = i
+ e1 = j
}
}
// Print with possible :: in place of run of zeros
- var s string;
+ var s string
for i := 0; i < 16; i += 2 {
if i == e0 {
- s += "::";
- i = e1;
+ s += "::"
+ i = e1
if i >= 16 {
break
}
} else if i > 0 {
s += ":"
}
- s += itox((uint(p[i]) << 8) | uint(p[i+1]));
+ s += itox((uint(p[i]) << 8) | uint(p[i+1]))
}
- return s;
+ return s
}
// If mask is a sequence of 1 bits followed by 0 bits,
// return the number of 1 bits.
func simpleMaskLength(mask IPMask) int {
- var i int;
+ var i int
for i = 0; i < len(mask); i++ {
if mask[i] != 0xFF {
break
}
}
- n := 8 * i;
- v := mask[i];
+ n := 8 * i
+ v := mask[i]
for v&0x80 != 0 {
- n++;
- v <<= 1;
+ n++
+ v <<= 1
}
if v != 0 {
return -1
@@ -249,7 +249,7 @@ func simpleMaskLength(mask IPMask) int {
return -1
}
}
- return n;
+ return n
}
// String returns the string representation of mask.
@@ -260,44 +260,44 @@ func simpleMaskLength(mask IPMask) int {
func (mask IPMask) String() string {
switch len(mask) {
case 4:
- n := simpleMaskLength(mask);
+ n := simpleMaskLength(mask)
if n >= 0 {
return itod(uint(n + (IPv6len-IPv4len)*8))
}
case 16:
- n := simpleMaskLength(mask);
+ n := simpleMaskLength(mask)
if n >= 0 {
return itod(uint(n))
}
}
- return IP(mask).String();
+ return IP(mask).String()
}
// Parse IPv4 address (d.d.d.d).
func parseIPv4(s string) IP {
- var p [IPv4len]byte;
- i := 0;
+ var p [IPv4len]byte
+ i := 0
for j := 0; j < IPv4len; j++ {
if j > 0 {
if s[i] != '.' {
return nil
}
- i++;
+ i++
}
var (
- n int;
- ok bool;
+ n int
+ ok bool
)
- n, i, ok = dtoi(s, i);
+ n, i, ok = dtoi(s, i)
if !ok || n > 0xFF {
return nil
}
- p[j] = byte(n);
+ p[j] = byte(n)
}
if i != len(s) {
return nil
}
- return IPv4(p[0], p[1], p[2], p[3]);
+ return IPv4(p[0], p[1], p[2], p[3])
}
// Parse IPv6 address. Many forms.
@@ -309,14 +309,14 @@ func parseIPv4(s string) IP {
// * The last 32 bits can be in IPv4 form.
// Thus, ::ffff:1.2.3.4 is the IPv4 address 1.2.3.4.
func parseIPv6(s string) IP {
- p := make(IP, 16);
- ellipsis := -1; // position of ellipsis in p
- i := 0; // index in string s
+ p := make(IP, 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;
- i = 2;
+ ellipsis = 0
+ i = 2
// Might be only ellipsis
if i == len(s) {
return p
@@ -324,10 +324,10 @@ func parseIPv6(s string) IP {
}
// Loop, parsing hex numbers followed by colon.
- j := 0;
-L: for j < IPv6len {
+ j := 0
+L: for j < IPv6len {
// Hex number.
- n, i1, ok := xtoi(s, i);
+ n, i1, ok := xtoi(s, i)
if !ok || n > 0xFFFF {
return nil
}
@@ -342,26 +342,26 @@ L: for j < IPv6len {
// Not enough room.
return nil
}
- p4 := parseIPv4(s[i:]);
+ p4 := parseIPv4(s[i:])
if p4 == nil {
return nil
}
- 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;
+ 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
}
// Save this 16-bit chunk.
- p[j] = byte(n >> 8);
- p[j+1] = byte(n);
- j += 2;
+ p[j] = byte(n >> 8)
+ p[j+1] = byte(n)
+ j += 2
// Stop at end of string.
- i = i1;
+ i = i1
if i == len(s) {
break
}
@@ -370,15 +370,15 @@ L: for j < IPv6len {
if s[i] != ':' && i+1 == len(s) {
return nil
}
- i++;
+ i++
// Look for ellipsis.
if s[i] == ':' {
- if ellipsis >= 0 { // already have one
+ if ellipsis >= 0 { // already have one
return nil
}
- ellipsis = j;
- if i++; i == len(s) { // can be at end
+ ellipsis = j
+ if i++; i == len(s) { // can be at end
break
}
}
@@ -394,7 +394,7 @@ L: for j < IPv6len {
if ellipsis < 0 {
return nil
}
- n := IPv6len - j;
+ n := IPv6len - j
for k := j - 1; k >= ellipsis; k-- {
p[k+n] = p[k]
}
@@ -402,7 +402,7 @@ L: for j < IPv6len {
p[k] = 0
}
}
- return p;
+ return p
}
// ParseIP parses s as an IP address, returning the result.
@@ -411,9 +411,9 @@ L: for j < IPv6len {
// If s is not a valid textual representation of an IP address,
// ParseIP returns nil.
func ParseIP(s string) IP {
- p := parseIPv4(s);
+ p := parseIPv4(s)
if p != nil {
return p
}
- return parseIPv6(s);
+ return parseIPv6(s)
}