mirror of
https://github.com/golang/go
synced 2024-11-22 01:34:41 -07:00
net: fix IPMask.String not to crash on all-0xff mask
R=r CC=golang-dev https://golang.org/cl/438042
This commit is contained in:
parent
7f4c2caea5
commit
0b986316b8
@ -51,6 +51,20 @@ func IPv4(a, b, c, d byte) IP {
|
|||||||
return p
|
return p
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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
|
||||||
|
}
|
||||||
|
|
||||||
// Well-known IPv4 addresses
|
// Well-known IPv4 addresses
|
||||||
var (
|
var (
|
||||||
IPv4bcast = IPv4(255, 255, 255, 255) // broadcast
|
IPv4bcast = IPv4(255, 255, 255, 255) // broadcast
|
||||||
@ -103,9 +117,9 @@ func (ip IP) To16() IP {
|
|||||||
|
|
||||||
// Default route masks for IPv4.
|
// Default route masks for IPv4.
|
||||||
var (
|
var (
|
||||||
classAMask = IPMask(IPv4(0xff, 0, 0, 0))
|
classAMask = IPv4Mask(0xff, 0, 0, 0)
|
||||||
classBMask = IPMask(IPv4(0xff, 0xff, 0, 0))
|
classBMask = IPv4Mask(0xff, 0xff, 0, 0)
|
||||||
classCMask = IPMask(IPv4(0xff, 0xff, 0xff, 0))
|
classCMask = IPv4Mask(0xff, 0xff, 0xff, 0)
|
||||||
)
|
)
|
||||||
|
|
||||||
// DefaultMask returns the default IP mask for the IP address ip.
|
// DefaultMask returns the default IP mask for the IP address ip.
|
||||||
@ -229,18 +243,19 @@ func (ip IP) String() string {
|
|||||||
// If mask is a sequence of 1 bits followed by 0 bits,
|
// If mask is a sequence of 1 bits followed by 0 bits,
|
||||||
// return the number of 1 bits.
|
// return the number of 1 bits.
|
||||||
func simpleMaskLength(mask IPMask) int {
|
func simpleMaskLength(mask IPMask) int {
|
||||||
var i int
|
var n int
|
||||||
for i = 0; i < len(mask); i++ {
|
for i, v := range mask {
|
||||||
if mask[i] != 0xFF {
|
if v == 0xff {
|
||||||
break
|
n += 8
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
}
|
// found non-ff byte
|
||||||
n := 8 * i
|
// count 1 bits
|
||||||
v := mask[i]
|
|
||||||
for v&0x80 != 0 {
|
for v&0x80 != 0 {
|
||||||
n++
|
n++
|
||||||
v <<= 1
|
v <<= 1
|
||||||
}
|
}
|
||||||
|
// rest must be 0 bits
|
||||||
if v != 0 {
|
if v != 0 {
|
||||||
return -1
|
return -1
|
||||||
}
|
}
|
||||||
@ -249,6 +264,8 @@ func simpleMaskLength(mask IPMask) int {
|
|||||||
return -1
|
return -1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
break
|
||||||
|
}
|
||||||
return n
|
return n
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -266,8 +283,8 @@ func (mask IPMask) String() string {
|
|||||||
}
|
}
|
||||||
case 16:
|
case 16:
|
||||||
n := simpleMaskLength(mask)
|
n := simpleMaskLength(mask)
|
||||||
if n >= 0 {
|
if n >= 12*8 {
|
||||||
return itod(uint(n))
|
return itod(uint(n - 12*8))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return IP(mask).String()
|
return IP(mask).String()
|
||||||
|
Loading…
Reference in New Issue
Block a user