diff options
Diffstat (limited to 'libgo/go/net/ip.go')
-rw-r--r-- | libgo/go/net/ip.go | 46 |
1 files changed, 28 insertions, 18 deletions
diff --git a/libgo/go/net/ip.go b/libgo/go/net/ip.go index cc004d6..d0c8263 100644 --- a/libgo/go/net/ip.go +++ b/libgo/go/net/ip.go @@ -252,9 +252,11 @@ func (ip IP) Mask(mask IPMask) IP { } // 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"). +// It returns one of 4 forms: +// - "<nil>", if ip has length 0 +// - dotted decimal ("192.0.2.1"), if ip is an IPv4 or IP4-mapped IPv6 address +// - IPv6 ("2001:db8::1"), if ip is a valid IPv6 address +// - the hexadecimal form of ip, without punctuation, if no other cases apply func (ip IP) String() string { p := ip @@ -270,7 +272,7 @@ func (ip IP) String() string { uitoa(uint(p4[3])) } if len(p) != IPv6len { - return "?" + return "?" + hexString(ip) } // Find longest run of zeros. @@ -312,6 +314,14 @@ func (ip IP) String() string { return string(b) } +func hexString(b []byte) string { + s := make([]byte, len(b)*2) + for i, tn := range b { + s[i*2], s[i*2+1] = hexDigit[tn>>4], hexDigit[tn&0xf] + } + return string(s) +} + // ipEmptyString is like ip.String except that it returns // an empty string when ip is unset. func ipEmptyString(ip IP) string { @@ -328,7 +338,7 @@ func (ip IP) MarshalText() ([]byte, error) { return []byte(""), nil } if len(ip) != IPv4len && len(ip) != IPv6len { - return nil, &AddrError{Err: "invalid IP address", Addr: ip.String()} + return nil, &AddrError{Err: "invalid IP address", Addr: hexString(ip)} } return []byte(ip.String()), nil } @@ -377,6 +387,10 @@ func bytesEqual(x, y []byte) bool { return true } +func (ip IP) matchAddrFamily(x IP) bool { + return ip.To4() != nil && x.To4() != nil || ip.To16() != nil && ip.To4() == nil && x.To16() != nil && x.To4() == nil +} + // If mask is a sequence of 1 bits followed by 0 bits, // return the number of 1 bits. func simpleMaskLength(mask IPMask) int { @@ -422,11 +436,7 @@ func (m IPMask) String() string { if len(m) == 0 { return "<nil>" } - buf := make([]byte, len(m)*2) - for i, b := range m { - buf[i*2], buf[i*2+1] = hexDigit[b>>4], hexDigit[b&0xf] - } - return string(buf) + return hexString(m) } func networkNumberAndMask(n *IPNet) (ip IP, m IPMask) { @@ -473,12 +483,12 @@ func (n *IPNet) Contains(ip IP) bool { // Network returns the address's network name, "ip+net". func (n *IPNet) Network() string { return "ip+net" } -// String returns the CIDR notation of n like "192.168.100.1/24" -// or "2001:DB8::/48" as defined in RFC 4632 and RFC 4291. +// String returns the CIDR notation of n like "192.0.2.1/24" +// or "2001:db8::/48" as defined in RFC 4632 and RFC 4291. // If the mask is not in the canonical form, it returns the // string which consists of an IP address, followed by a slash // character and a mask expressed as hexadecimal form with no -// punctuation like "192.168.100.1/c000ff00". +// punctuation like "198.51.100.1/c000ff00". func (n *IPNet) String() string { nn, m := networkNumberAndMask(n) if nn == nil || m == nil { @@ -631,8 +641,8 @@ func parseIPv6(s string, zoneAllowed bool) (ip IP, zone string) { } // 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. +// The string s can be in dotted decimal ("192.0.2.1") +// or IPv6 ("2001:db8::68") form. // If s is not a valid textual representation of an IP address, // ParseIP returns nil. func ParseIP(s string) IP { @@ -649,12 +659,12 @@ func ParseIP(s string) IP { } // ParseCIDR parses s as a CIDR notation IP address and mask, -// like "192.168.100.1/24" or "2001:DB8::/48", as defined in +// like "192.0.2.0/24" or "2001:db8::/32", as defined in // RFC 4632 and RFC 4291. // // It returns the IP address and the network implied by the IP -// and mask. For example, ParseCIDR("192.168.100.1/16") returns -// the IP address 192.168.100.1 and the network 192.168.0.0/16. +// and mask. For example, ParseCIDR("198.51.100.1/24") returns +// the IP address 198.51.100.1 and the network 198.51.100.0/24. func ParseCIDR(s string) (IP, *IPNet, error) { i := byteIndex(s, '/') if i < 0 { |