gofmt-ify net

R=rsc
http://go/go-review/1017045
This commit is contained in:
Robert Griesemer 2009-11-04 23:16:46 -08:00
parent 7df45566db
commit 5d37705416
23 changed files with 569 additions and 570 deletions

View file

@ -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.
@ -30,17 +30,17 @@ const (
// 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.
type IP []byte;
type IP []byte
// An IP mask is an IP address.
type IPMask []byte;
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);
for i := 0; i < 10; i++ {
p[i] = 0
p[i] = 0;
}
p[10] = 0xff;
p[11] = 0xff;
@ -48,15 +48,15 @@ func IPv4(a, b, c, d byte) IP {
p[13] = b;
p[14] = c;
p[15] = d;
return p
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
@ -68,52 +68,52 @@ var (
func isZeros(p IP) bool {
for i := 0; i < len(p); i++ {
if p[i] != 0 {
return false
return false;
}
}
return true
return true;
}
// 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 {
return ip
return ip;
}
if len(ip) == IPv6len
&& isZeros(ip[0:10])
&& ip[10] == 0xff
&& ip[11] == 0xff {
return ip[12:16]
if len(ip) == IPv6len &&
isZeros(ip[0:10]) &&
ip[10] == 0xff &&
ip[11] == 0xff {
return ip[12:16];
}
return nil
return nil;
}
// 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 {
return IPv4(ip[0], ip[1], ip[2], ip[3])
return IPv4(ip[0], ip[1], ip[2], ip[3]);
}
if len(ip) == IPv6len {
return ip
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.
// Only IPv4 addresses have default masks; DefaultMask returns
// nil if ip is not a valid IPv4 address.
func (ip IP) DefaultMask() IPMask {
func (ip IP) DefaultMask() IPMask {
if ip = ip.To4(); ip == nil {
return nil
return nil;
}
switch true {
case ip[0] < 0x80:
@ -130,19 +130,19 @@ func (ip IP) DefaultMask() IPMask {
func (ip IP) Mask(mask IPMask) IP {
n := len(ip);
if n != len(mask) {
return nil
return nil;
}
out := make(IP, n);
for i := 0; i < n; i++ {
out[i] = ip[i] & mask[i];
out[i] = ip[i]&mask[i];
}
return out
return out;
}
// Convert i to decimal string.
func itod(i uint) string {
if i == 0 {
return "0"
return "0";
}
// Assemble decimal in reverse order.
@ -150,16 +150,16 @@ func itod(i uint) string {
bp := len(b);
for ; i > 0; i /= 10 {
bp--;
b[bp] = byte(i%10) + '0'
b[bp] = byte(i%10)+'0';
}
return string(b[bp:len(b)])
return string(b[bp:len(b)]);
}
// Convert i to hexadecimal string.
func itox(i uint) string {
if i == 0 {
return "0"
return "0";
}
// Assemble hexadecimal in reverse order.
@ -167,10 +167,10 @@ func itox(i uint) string {
bp := len(b);
for ; i > 0; i /= 16 {
bp--;
b[bp] = "0123456789abcdef"[byte(i%16)]
b[bp] = "0123456789abcdef"[byte(i%16)];
}
return string(b[bp:len(b)])
return string(b[bp:len(b)]);
}
// String returns the string form of the IP address ip.
@ -186,26 +186,26 @@ func (ip IP) String() string {
// If IPv4, use dotted notation.
if p4 := p.To4(); len(p4) == 4 {
return itod(uint(p4[0]))+"."
+itod(uint(p4[1]))+"."
+itod(uint(p4[2]))+"."
+itod(uint(p4[3]))
return itod(uint(p4[0])) + "." +
itod(uint(p4[1])) + "." +
itod(uint(p4[2])) + "." +
itod(uint(p4[3]));
}
if len(p) != IPv6len {
return "?"
return "?";
}
// Find longest run of zeros.
e0 := -1;
e1 := -1;
for i := 0; i < 16; i+=2 {
for i := 0; i < 16; i += 2 {
j := i;
for j < 16 && p[j] == 0 && p[j+1] == 0 {
j += 2
j += 2;
}
if j > i && j - i > e1 - e0 {
if j > i && j-i > e1-e0 {
e0 = i;
e1 = j
e1 = j;
}
}
@ -216,14 +216,14 @@ func (ip IP) String() string {
s += "::";
i = e1;
if i >= 16 {
break
break;
}
} else if i > 0 {
s += ":"
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,
@ -232,24 +232,24 @@ func simpleMaskLength(mask IPMask) int {
var i int;
for i = 0; i < len(mask); i++ {
if mask[i] != 0xFF {
break
break;
}
}
n := 8*i;
v := mask[i];
for v & 0x80 != 0 {
for v&0x80 != 0 {
n++;
v <<= 1
v <<= 1;
}
if v != 0 {
return -1
return -1;
}
for i++; i < len(mask); i++ {
if mask[i] != 0 {
return -1
return -1;
}
}
return n
return n;
}
// String returns the string representation of mask.
@ -262,12 +262,12 @@ func (mask IPMask) String() string {
case 4:
n := simpleMaskLength(mask);
if n >= 0 {
return itod(uint(n+(IPv6len-IPv4len)*8))
return itod(uint(n + (IPv6len-IPv4len)*8));
}
case 16:
n := simpleMaskLength(mask);
if n >= 0 {
return itod(uint(n))
return itod(uint(n));
}
}
return IP(mask).String();
@ -280,24 +280,24 @@ func parseIPv4(s string) IP {
for j := 0; j < IPv4len; j++ {
if j > 0 {
if s[i] != '.' {
return nil
return nil;
}
i++;
}
var (
n int;
ok bool
n int;
ok bool;
)
n, i, ok = dtoi(s, i);
if !ok || n > 0xFF {
return nil
return nil;
}
p[j] = byte(n)
p[j] = byte(n);
}
if i != len(s) {
return nil
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.
@ -311,7 +311,7 @@ func parseIPv4(s string) IP {
func parseIPv6(s string) IP {
p := make(IP, 16);
ellipsis := -1; // position of ellipsis in p
i := 0; // index in string s
i := 0; // index in string s
// Might have leading ellipsis
if len(s) >= 2 && s[0] == ':' && s[1] == ':' {
@ -319,7 +319,7 @@ func parseIPv6(s string) IP {
i = 2;
// Might be only ellipsis
if i == len(s) {
return p
return p;
}
}
@ -329,22 +329,22 @@ L: for j < IPv6len {
// Hex number.
n, i1, ok := xtoi(s, i);
if !ok || n > 0xFFFF {
return nil
return nil;
}
// If followed by dot, might be in trailing IPv4.
if i1 < len(s) && s[i1] == '.' {
if ellipsis < 0 && j != IPv6len - IPv4len {
if ellipsis < 0 && j != IPv6len-IPv4len {
// Not the right place.
return nil
return nil;
}
if j+IPv4len > IPv6len {
// Not enough room.
return nil
return nil;
}
p4 := parseIPv4(s[i:len(s)]);
if p4 == nil {
return nil
return nil;
}
p[j] = p4[12];
p[j+1] = p4[13];
@ -352,7 +352,7 @@ L: for j < IPv6len {
p[j+3] = p4[15];
i = len(s);
j += 4;
break
break;
}
// Save this 16-bit chunk.
@ -363,46 +363,46 @@ L: for j < IPv6len {
// Stop at end of string.
i = i1;
if i == len(s) {
break
break;
}
// Otherwise must be followed by colon and more.
if s[i] != ':' && i+1 == len(s) {
return nil
return nil;
}
i++;
// Look for ellipsis.
if s[i] == ':' {
if ellipsis >= 0 { // already have one
return nil
return nil;
}
ellipsis = j;
if i++; i == len(s) { // can be at end
break
break;
}
}
}
// Must have used entire string.
if i != len(s) {
return nil
return nil;
}
// If didn't parse enough, expand ellipsis.
if j < IPv6len {
if ellipsis < 0 {
return nil
return nil;
}
n := IPv6len - j;
n := IPv6len-j;
for k := j-1; k >= ellipsis; k-- {
p[k+n] = p[k]
p[k+n] = p[k];
}
for k := ellipsis+n-1; k>=ellipsis; k-- {
p[k] = 0
for k := ellipsis+n-1; k >= ellipsis; k-- {
p[k] = 0;
}
}
return p
return p;
}
// ParseIP parses s as an IP address, returning the result.
@ -413,8 +413,7 @@ L: for j < IPv6len {
func ParseIP(s string) IP {
p := parseIPv4(s);
if p != nil {
return p
return p;
}
return parseIPv6(s)
return parseIPv6(s);
}