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).
|
2013-01-02 20:25:13 -07:00
|
|
|
// This library accepts either size of byte slice but always
|
2008-09-16 14:42:47 -06:00
|
|
|
// returns 16-byte addresses.
|
|
|
|
|
2008-09-26 15:11:26 -06:00
|
|
|
package net
|
2008-09-16 14:42:47 -06:00
|
|
|
|
2009-03-05 16:48:12 -07:00
|
|
|
// IP address lengths (bytes).
|
2009-01-20 15:40:40 -07:00
|
|
|
const (
|
2009-12-15 16:35:38 -07:00
|
|
|
IPv4len = 4
|
|
|
|
IPv6len = 16
|
2008-09-16 14:42:47 -06:00
|
|
|
)
|
|
|
|
|
2013-01-02 20:25:13 -07:00
|
|
|
// An IP is a single IP address, a slice of bytes.
|
2011-08-29 08:49:03 -06:00
|
|
|
// Functions in this package accept either 4-byte (IPv4)
|
2013-01-02 20:25:13 -07:00
|
|
|
// or 16-byte (IPv6) slices as input.
|
2009-03-05 16:48:12 -07:00
|
|
|
//
|
|
|
|
// Note that in this documentation, referring to an
|
|
|
|
// IP address as an IPv4 address or an IPv6 address
|
|
|
|
// is a semantic property of the address, not just the
|
2013-01-02 20:25:13 -07:00
|
|
|
// length of the byte slice: a 16-byte slice can still
|
2009-03-05 16:48:12 -07:00
|
|
|
// be an IPv4 address.
|
2009-11-05 00:16:46 -07:00
|
|
|
type IP []byte
|
2009-03-05 16:48:12 -07:00
|
|
|
|
|
|
|
// An IP mask is an IP address.
|
2009-11-05 00:16:46 -07:00
|
|
|
type IPMask []byte
|
2009-03-05 16:48:12 -07:00
|
|
|
|
2011-09-07 12:01:12 -06:00
|
|
|
// An IPNet represents an IP network.
|
|
|
|
type IPNet struct {
|
|
|
|
IP IP // network number
|
|
|
|
Mask IPMask // network mask
|
|
|
|
}
|
|
|
|
|
2009-03-05 16:48:12 -07:00
|
|
|
// 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 {
|
2009-12-15 16:35:38 -07:00
|
|
|
p := make(IP, IPv6len)
|
2011-03-15 12:05:07 -06:00
|
|
|
copy(p, v4InV6Prefix)
|
2009-12-15 16:35:38 -07:00
|
|
|
p[12] = a
|
|
|
|
p[13] = b
|
|
|
|
p[14] = c
|
|
|
|
p[15] = d
|
|
|
|
return p
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
|
|
|
|
2011-03-15 12:05:07 -06:00
|
|
|
var v4InV6Prefix = []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff}
|
|
|
|
|
2011-08-29 08:49:03 -06:00
|
|
|
// IPv4Mask returns the IP mask (in 4-byte form) of the
|
2010-03-16 15:16:33 -06:00
|
|
|
// IPv4 mask a.b.c.d.
|
|
|
|
func IPv4Mask(a, b, c, d byte) IPMask {
|
2011-08-29 08:49:03 -06:00
|
|
|
p := make(IPMask, IPv4len)
|
|
|
|
p[0] = a
|
|
|
|
p[1] = b
|
|
|
|
p[2] = c
|
|
|
|
p[3] = d
|
2010-03-16 15:16:33 -06:00
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2011-09-07 13:50:07 -06:00
|
|
|
// CIDRMask returns an IPMask consisting of `ones' 1 bits
|
|
|
|
// followed by 0s up to a total length of `bits' bits.
|
|
|
|
// For a mask of this form, CIDRMask is the inverse of IPMask.Size.
|
2011-09-07 12:01:12 -06:00
|
|
|
func CIDRMask(ones, bits int) IPMask {
|
|
|
|
if bits != 8*IPv4len && bits != 8*IPv6len {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if ones < 0 || ones > bits {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
l := bits / 8
|
|
|
|
m := make(IPMask, l)
|
|
|
|
n := uint(ones)
|
|
|
|
for i := 0; i < l; i++ {
|
|
|
|
if n >= 8 {
|
|
|
|
m[i] = 0xff
|
|
|
|
n -= 8
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
m[i] = ^byte(0xff >> n)
|
|
|
|
n = 0
|
|
|
|
}
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2009-03-05 16:48:12 -07:00
|
|
|
// Well-known IPv4 addresses
|
|
|
|
var (
|
2009-12-15 16:35:38 -07:00
|
|
|
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
|
2009-03-05 16:48:12 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
// Well-known IPv6 addresses
|
|
|
|
var (
|
2011-05-16 15:03:06 -06:00
|
|
|
IPv6zero = IP{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
|
|
|
|
IPv6unspecified = IP{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
|
|
|
|
IPv6loopback = IP{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}
|
|
|
|
IPv6interfacelocalallnodes = IP{0xff, 0x01, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x01}
|
|
|
|
IPv6linklocalallnodes = IP{0xff, 0x02, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x01}
|
|
|
|
IPv6linklocalallrouters = IP{0xff, 0x02, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x02}
|
2009-03-05 16:48:12 -07:00
|
|
|
)
|
2008-09-16 14:42:47 -06:00
|
|
|
|
2015-02-17 16:44:42 -07:00
|
|
|
// IsUnspecified reports whether ip is an unspecified address.
|
2011-05-16 21:21:13 -06:00
|
|
|
func (ip IP) IsUnspecified() bool {
|
2015-07-03 22:49:47 -06:00
|
|
|
return ip.Equal(IPv4zero) || ip.Equal(IPv6unspecified)
|
2011-05-16 21:21:13 -06:00
|
|
|
}
|
|
|
|
|
2015-02-17 16:44:42 -07:00
|
|
|
// IsLoopback reports whether ip is a loopback address.
|
2011-05-16 21:21:13 -06:00
|
|
|
func (ip IP) IsLoopback() bool {
|
2015-07-03 22:49:47 -06:00
|
|
|
if ip4 := ip.To4(); ip4 != nil {
|
|
|
|
return ip4[0] == 127
|
2011-05-16 21:21:13 -06:00
|
|
|
}
|
|
|
|
return ip.Equal(IPv6loopback)
|
|
|
|
}
|
|
|
|
|
2015-02-17 16:44:42 -07:00
|
|
|
// IsMulticast reports whether ip is a multicast address.
|
2011-05-16 21:21:13 -06:00
|
|
|
func (ip IP) IsMulticast() bool {
|
2015-07-03 22:49:47 -06:00
|
|
|
if ip4 := ip.To4(); ip4 != nil {
|
|
|
|
return ip4[0]&0xf0 == 0xe0
|
2011-05-16 21:21:13 -06:00
|
|
|
}
|
2015-07-03 22:49:47 -06:00
|
|
|
return len(ip) == IPv6len && ip[0] == 0xff
|
2011-05-16 21:21:13 -06:00
|
|
|
}
|
|
|
|
|
2015-02-17 16:44:42 -07:00
|
|
|
// IsInterfaceLocalMulticast reports whether ip is
|
2011-05-16 21:21:13 -06:00
|
|
|
// an interface-local multicast address.
|
|
|
|
func (ip IP) IsInterfaceLocalMulticast() bool {
|
|
|
|
return len(ip) == IPv6len && ip[0] == 0xff && ip[1]&0x0f == 0x01
|
|
|
|
}
|
|
|
|
|
2015-02-17 16:44:42 -07:00
|
|
|
// IsLinkLocalMulticast reports whether ip is a link-local
|
2011-05-16 21:21:13 -06:00
|
|
|
// multicast address.
|
|
|
|
func (ip IP) IsLinkLocalMulticast() bool {
|
2015-07-03 22:49:47 -06:00
|
|
|
if ip4 := ip.To4(); ip4 != nil {
|
|
|
|
return ip4[0] == 224 && ip4[1] == 0 && ip4[2] == 0
|
2011-05-16 21:21:13 -06:00
|
|
|
}
|
2015-07-03 22:49:47 -06:00
|
|
|
return len(ip) == IPv6len && ip[0] == 0xff && ip[1]&0x0f == 0x02
|
2011-05-16 21:21:13 -06:00
|
|
|
}
|
|
|
|
|
2015-02-17 16:44:42 -07:00
|
|
|
// IsLinkLocalUnicast reports whether ip is a link-local
|
2011-05-16 21:21:13 -06:00
|
|
|
// unicast address.
|
|
|
|
func (ip IP) IsLinkLocalUnicast() bool {
|
2015-07-03 22:49:47 -06:00
|
|
|
if ip4 := ip.To4(); ip4 != nil {
|
|
|
|
return ip4[0] == 169 && ip4[1] == 254
|
2011-05-16 21:21:13 -06:00
|
|
|
}
|
2015-07-03 22:49:47 -06:00
|
|
|
return len(ip) == IPv6len && ip[0] == 0xfe && ip[1]&0xc0 == 0x80
|
2011-05-16 21:21:13 -06:00
|
|
|
}
|
|
|
|
|
2015-02-17 16:44:42 -07:00
|
|
|
// IsGlobalUnicast reports whether ip is a global unicast
|
2011-05-16 21:21:13 -06:00
|
|
|
// address.
|
|
|
|
func (ip IP) IsGlobalUnicast() bool {
|
2015-07-03 22:49:47 -06:00
|
|
|
return (len(ip) == IPv4len || len(ip) == IPv6len) &&
|
|
|
|
!ip.Equal(IPv4bcast) &&
|
|
|
|
!ip.IsUnspecified() &&
|
2011-05-16 21:21:13 -06:00
|
|
|
!ip.IsLoopback() &&
|
|
|
|
!ip.IsMulticast() &&
|
|
|
|
!ip.IsLinkLocalUnicast()
|
|
|
|
}
|
|
|
|
|
2008-09-16 14:42:47 -06:00
|
|
|
// Is p all zeros?
|
2009-05-07 18:36:29 -06:00
|
|
|
func isZeros(p IP) bool {
|
2008-09-16 14:42:47 -06:00
|
|
|
for i := 0; i < len(p); i++ {
|
|
|
|
if p[i] != 0 {
|
2009-11-09 13:07:39 -07:00
|
|
|
return false
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
return true
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
|
|
|
|
2009-03-05 16:48:12 -07:00
|
|
|
// 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 {
|
2009-11-09 13:07:39 -07:00
|
|
|
return ip
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
2009-11-05 00:16:46 -07:00
|
|
|
if len(ip) == IPv6len &&
|
|
|
|
isZeros(ip[0:10]) &&
|
|
|
|
ip[10] == 0xff &&
|
|
|
|
ip[11] == 0xff {
|
2009-11-09 13:07:39 -07:00
|
|
|
return ip[12:16]
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
return nil
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
|
|
|
|
2009-03-05 16:48:12 -07:00
|
|
|
// 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 {
|
2009-11-09 13:07:39 -07:00
|
|
|
return IPv4(ip[0], ip[1], ip[2], ip[3])
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
2009-03-05 16:48:12 -07:00
|
|
|
if len(ip) == IPv6len {
|
2009-11-09 13:07:39 -07:00
|
|
|
return ip
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
return nil
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Default route masks for IPv4.
|
2009-01-20 15:40:40 -07:00
|
|
|
var (
|
2010-03-16 15:16:33 -06:00
|
|
|
classAMask = IPv4Mask(0xff, 0, 0, 0)
|
|
|
|
classBMask = IPv4Mask(0xff, 0xff, 0, 0)
|
|
|
|
classCMask = IPv4Mask(0xff, 0xff, 0xff, 0)
|
2008-09-16 14:42:47 -06:00
|
|
|
)
|
|
|
|
|
2009-03-05 16:48:12 -07:00
|
|
|
// 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.
|
2009-11-05 00:16:46 -07:00
|
|
|
func (ip IP) DefaultMask() IPMask {
|
2009-03-05 16:48:12 -07:00
|
|
|
if ip = ip.To4(); ip == nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
return nil
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
|
|
|
switch true {
|
2009-03-05 16:48:12 -07:00
|
|
|
case ip[0] < 0x80:
|
2009-11-09 13:07:39 -07:00
|
|
|
return classAMask
|
2009-03-05 16:48:12 -07:00
|
|
|
case ip[0] < 0xC0:
|
2009-11-09 13:07:39 -07:00
|
|
|
return classBMask
|
2008-09-16 14:42:47 -06:00
|
|
|
default:
|
2009-11-09 13:07:39 -07:00
|
|
|
return classCMask
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-15 12:05:07 -06:00
|
|
|
func allFF(b []byte) bool {
|
|
|
|
for _, c := range b {
|
|
|
|
if c != 0xff {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2009-03-05 16:48:12 -07:00
|
|
|
// Mask returns the result of masking the IP address ip with mask.
|
|
|
|
func (ip IP) Mask(mask IPMask) IP {
|
2011-08-25 17:00:04 -06:00
|
|
|
if len(mask) == IPv6len && len(ip) == IPv4len && allFF(mask[:12]) {
|
2011-03-15 12:05:07 -06:00
|
|
|
mask = mask[12:]
|
|
|
|
}
|
2011-08-25 17:00:04 -06:00
|
|
|
if len(mask) == IPv4len && len(ip) == IPv6len && bytesEqual(ip[:12], v4InV6Prefix) {
|
2011-03-15 12:05:07 -06:00
|
|
|
ip = ip[12:]
|
|
|
|
}
|
2011-08-29 08:49:03 -06:00
|
|
|
n := len(ip)
|
2008-09-16 14:42:47 -06:00
|
|
|
if n != len(mask) {
|
2009-11-09 13:07:39 -07:00
|
|
|
return nil
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
out := make(IP, n)
|
2008-09-16 14:42:47 -06:00
|
|
|
for i := 0; i < n; i++ {
|
2009-11-09 22:23:52 -07:00
|
|
|
out[i] = ip[i] & mask[i]
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
return out
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
|
|
|
|
2009-03-05 16:48:12 -07:00
|
|
|
// 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").
|
|
|
|
func (ip IP) String() string {
|
2009-12-15 16:35:38 -07:00
|
|
|
p := ip
|
2009-03-05 16:48:12 -07:00
|
|
|
|
2009-11-02 19:37:30 -07:00
|
|
|
if len(ip) == 0 {
|
2011-08-29 08:49:03 -06:00
|
|
|
return "<nil>"
|
2009-11-02 19:37:30 -07:00
|
|
|
}
|
|
|
|
|
2008-09-16 14:42:47 -06:00
|
|
|
// If IPv4, use dotted notation.
|
2011-08-25 17:00:04 -06:00
|
|
|
if p4 := p.To4(); len(p4) == IPv4len {
|
2014-12-31 10:45:05 -07:00
|
|
|
return uitoa(uint(p4[0])) + "." +
|
|
|
|
uitoa(uint(p4[1])) + "." +
|
|
|
|
uitoa(uint(p4[2])) + "." +
|
|
|
|
uitoa(uint(p4[3]))
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
|
|
|
if len(p) != IPv6len {
|
2009-11-09 13:07:39 -07:00
|
|
|
return "?"
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Find longest run of zeros.
|
2009-12-15 16:35:38 -07:00
|
|
|
e0 := -1
|
|
|
|
e1 := -1
|
2011-08-25 17:00:04 -06:00
|
|
|
for i := 0; i < IPv6len; i += 2 {
|
2009-12-15 16:35:38 -07:00
|
|
|
j := i
|
2011-08-25 17:00:04 -06:00
|
|
|
for j < IPv6len && p[j] == 0 && p[j+1] == 0 {
|
2009-11-09 13:07:39 -07:00
|
|
|
j += 2
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
2009-11-05 00:16:46 -07:00
|
|
|
if j > i && j-i > e1-e0 {
|
2009-12-15 16:35:38 -07:00
|
|
|
e0 = i
|
|
|
|
e1 = j
|
2014-06-11 21:40:00 -06:00
|
|
|
i = j
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
|
|
|
}
|
2010-08-12 01:03:01 -06:00
|
|
|
// The symbol "::" MUST NOT be used to shorten just one 16 bit 0 field.
|
|
|
|
if e1-e0 <= 2 {
|
|
|
|
e0 = -1
|
|
|
|
e1 = -1
|
|
|
|
}
|
2008-09-16 14:42:47 -06:00
|
|
|
|
2014-06-11 21:40:00 -06:00
|
|
|
const maxLen = len("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")
|
|
|
|
b := make([]byte, 0, maxLen)
|
|
|
|
|
2008-09-16 14:42:47 -06:00
|
|
|
// Print with possible :: in place of run of zeros
|
2011-08-25 17:00:04 -06:00
|
|
|
for i := 0; i < IPv6len; i += 2 {
|
2008-09-16 14:42:47 -06:00
|
|
|
if i == e0 {
|
2014-06-11 21:40:00 -06:00
|
|
|
b = append(b, ':', ':')
|
2009-12-15 16:35:38 -07:00
|
|
|
i = e1
|
2011-08-25 17:00:04 -06:00
|
|
|
if i >= IPv6len {
|
2009-11-09 13:07:39 -07:00
|
|
|
break
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
|
|
|
} else if i > 0 {
|
2014-06-11 21:40:00 -06:00
|
|
|
b = append(b, ':')
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
2014-06-11 21:40:00 -06:00
|
|
|
b = appendHex(b, (uint32(p[i])<<8)|uint32(p[i+1]))
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
2014-06-11 21:40:00 -06:00
|
|
|
return string(b)
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
|
|
|
|
2013-09-23 20:40:24 -06:00
|
|
|
// ipEmptyString is like ip.String except that it returns
|
|
|
|
// an empty string when ip is unset.
|
|
|
|
func ipEmptyString(ip IP) string {
|
|
|
|
if len(ip) == 0 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return ip.String()
|
|
|
|
}
|
|
|
|
|
2013-08-13 22:33:20 -06:00
|
|
|
// MarshalText implements the encoding.TextMarshaler interface.
|
|
|
|
// The encoding is the same as returned by String.
|
|
|
|
func (ip IP) MarshalText() ([]byte, error) {
|
2013-09-06 16:29:09 -06:00
|
|
|
if len(ip) == 0 {
|
|
|
|
return []byte(""), nil
|
|
|
|
}
|
2013-08-13 22:33:20 -06:00
|
|
|
if len(ip) != IPv4len && len(ip) != IPv6len {
|
net: fix inconsistent errors
These a series of changes fix inconsistent errors on the package net
APIs. Now almost all the APIs return OpError as a common error type
except Lookup, Resolve and Parse APIs. The Lookup, Resolve and Parse
APIs return more specific errors such as DNSError, AddrError or
ParseError.
An OpError may contain nested error information. For example, Dial may
return an OpError containing a DNSError, AddrError, unexposed type/value
or other package's type/value like the following:
OpError{/* dial info */, Err: &DNSError{}}
OpError{/* dial info */, Err: &AddrError{}}
OpError{/* dial info */, Err: <unexposed type or value>}
OpError{/* dial info */, Err: <other package's type or value>}
and Read and Write may return an OpError containing other OpError when
an application uses io.Copy or similar:
OpError{/* for io.Reader */, Err: &OpError{/* for io.Writer */}}
When an endpoint is created for connection-oriented byte-stream
protocols, Read may return an io.EOF when the connection is closed by
remote endpoint.
Fixes #4856.
A series of changes:
- net: fix inconsistent error values on Dial, Listen partially
https://go.googlesource.com/go/+/89b7c66d0d14462fd7893be4290bdfe5f9063ae1
- net: fix inconsistent error values on Read
https://go.googlesource.com/go/+/ec1144423f45e010c72363fe59291d43214b6e31
- net: fix inconsistent error values on Write
https://go.googlesource.com/go/+/11b5f98bf0d5eb8854f735cc332c912725070214
- net: fix inconsistent error values on Close
https://go.googlesource.com/go/+/310db63c5bc121e7bfccb494c01a6b91a257e7fc
- net: fix inconsistent error values on Accept
https://go.googlesource.com/go/+/4540e162b1aefda8157372764ad3d290a414ef1d
- net: fix inconsistent error values on File
https://go.googlesource.com/go/+/885111365ba0a74421059bfbd18f4c57c1e70332
- net: fix inconsistent error values on setters
https://go.googlesource.com/go/+/2173a27903897c481b0a0daf3ca3e0a0685701db
- net: fix inconsistent error values on Interface
https://go.googlesource.com/go/+/456cf0f22c93e1a6654980f4a48a564555f6c8a2
- net: fix inconsistent error values on Lookup
https://go.googlesource.com/go/+/0fc582e87942b2e52bed751b6c56660ba99e9a7d
- net: add Source field to OpError
https://go.googlesource.com/go/+/afd2d2b6df3ebfe99faf347030f15adfdf422fa0
Change-Id: Id678e369088dc9fbe9073cfe7ff8a8754a57d61f
Reviewed-on: https://go-review.googlesource.com/9236
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2015-04-21 06:20:15 -06:00
|
|
|
return nil, &AddrError{Err: "invalid IP address", Addr: ip.String()}
|
2013-08-13 22:33:20 -06:00
|
|
|
}
|
|
|
|
return []byte(ip.String()), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalText implements the encoding.TextUnmarshaler interface.
|
|
|
|
// The IP address is expected in a form accepted by ParseIP.
|
|
|
|
func (ip *IP) UnmarshalText(text []byte) error {
|
2013-09-06 16:29:09 -06:00
|
|
|
if len(text) == 0 {
|
|
|
|
*ip = nil
|
|
|
|
return nil
|
|
|
|
}
|
2013-08-13 22:33:20 -06:00
|
|
|
s := string(text)
|
|
|
|
x := ParseIP(s)
|
|
|
|
if x == nil {
|
net: fix inconsistent errors
These a series of changes fix inconsistent errors on the package net
APIs. Now almost all the APIs return OpError as a common error type
except Lookup, Resolve and Parse APIs. The Lookup, Resolve and Parse
APIs return more specific errors such as DNSError, AddrError or
ParseError.
An OpError may contain nested error information. For example, Dial may
return an OpError containing a DNSError, AddrError, unexposed type/value
or other package's type/value like the following:
OpError{/* dial info */, Err: &DNSError{}}
OpError{/* dial info */, Err: &AddrError{}}
OpError{/* dial info */, Err: <unexposed type or value>}
OpError{/* dial info */, Err: <other package's type or value>}
and Read and Write may return an OpError containing other OpError when
an application uses io.Copy or similar:
OpError{/* for io.Reader */, Err: &OpError{/* for io.Writer */}}
When an endpoint is created for connection-oriented byte-stream
protocols, Read may return an io.EOF when the connection is closed by
remote endpoint.
Fixes #4856.
A series of changes:
- net: fix inconsistent error values on Dial, Listen partially
https://go.googlesource.com/go/+/89b7c66d0d14462fd7893be4290bdfe5f9063ae1
- net: fix inconsistent error values on Read
https://go.googlesource.com/go/+/ec1144423f45e010c72363fe59291d43214b6e31
- net: fix inconsistent error values on Write
https://go.googlesource.com/go/+/11b5f98bf0d5eb8854f735cc332c912725070214
- net: fix inconsistent error values on Close
https://go.googlesource.com/go/+/310db63c5bc121e7bfccb494c01a6b91a257e7fc
- net: fix inconsistent error values on Accept
https://go.googlesource.com/go/+/4540e162b1aefda8157372764ad3d290a414ef1d
- net: fix inconsistent error values on File
https://go.googlesource.com/go/+/885111365ba0a74421059bfbd18f4c57c1e70332
- net: fix inconsistent error values on setters
https://go.googlesource.com/go/+/2173a27903897c481b0a0daf3ca3e0a0685701db
- net: fix inconsistent error values on Interface
https://go.googlesource.com/go/+/456cf0f22c93e1a6654980f4a48a564555f6c8a2
- net: fix inconsistent error values on Lookup
https://go.googlesource.com/go/+/0fc582e87942b2e52bed751b6c56660ba99e9a7d
- net: add Source field to OpError
https://go.googlesource.com/go/+/afd2d2b6df3ebfe99faf347030f15adfdf422fa0
Change-Id: Id678e369088dc9fbe9073cfe7ff8a8754a57d61f
Reviewed-on: https://go-review.googlesource.com/9236
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2015-04-21 06:20:15 -06:00
|
|
|
return &ParseError{Type: "IP address", Text: s}
|
2013-08-13 22:33:20 -06:00
|
|
|
}
|
|
|
|
*ip = x
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-02-17 16:44:42 -07:00
|
|
|
// Equal reports whether ip and x are the same IP address.
|
2011-03-15 12:05:07 -06:00
|
|
|
// An IPv4 address and that same address in IPv6 form are
|
|
|
|
// considered to be equal.
|
|
|
|
func (ip IP) Equal(x IP) bool {
|
|
|
|
if len(ip) == len(x) {
|
|
|
|
return bytesEqual(ip, x)
|
|
|
|
}
|
2011-08-25 17:00:04 -06:00
|
|
|
if len(ip) == IPv4len && len(x) == IPv6len {
|
2011-03-15 12:05:07 -06:00
|
|
|
return bytesEqual(x[0:12], v4InV6Prefix) && bytesEqual(ip, x[12:])
|
|
|
|
}
|
2011-08-25 17:00:04 -06:00
|
|
|
if len(ip) == IPv6len && len(x) == IPv4len {
|
2011-03-15 12:05:07 -06:00
|
|
|
return bytesEqual(ip[0:12], v4InV6Prefix) && bytesEqual(ip[12:], x)
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func bytesEqual(x, y []byte) bool {
|
|
|
|
if len(x) != len(y) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for i, b := range x {
|
|
|
|
if y[i] != b {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2016-03-14 19:00:12 -06:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2008-09-16 14:42:47 -06:00
|
|
|
// If mask is a sequence of 1 bits followed by 0 bits,
|
|
|
|
// return the number of 1 bits.
|
2009-05-07 18:36:29 -06:00
|
|
|
func simpleMaskLength(mask IPMask) int {
|
2010-03-16 15:16:33 -06:00
|
|
|
var n int
|
|
|
|
for i, v := range mask {
|
|
|
|
if v == 0xff {
|
|
|
|
n += 8
|
|
|
|
continue
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
2010-03-16 15:16:33 -06:00
|
|
|
// found non-ff byte
|
|
|
|
// count 1 bits
|
|
|
|
for v&0x80 != 0 {
|
|
|
|
n++
|
|
|
|
v <<= 1
|
|
|
|
}
|
|
|
|
// rest must be 0 bits
|
|
|
|
if v != 0 {
|
2009-11-09 13:07:39 -07:00
|
|
|
return -1
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
2010-03-16 15:16:33 -06:00
|
|
|
for i++; i < len(mask); i++ {
|
|
|
|
if mask[i] != 0 {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
return n
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
|
|
|
|
2011-08-29 08:49:03 -06:00
|
|
|
// Size returns the number of leading ones and total bits in the mask.
|
|
|
|
// If the mask is not in the canonical form--ones followed by zeros--then
|
|
|
|
// Size returns 0, 0.
|
|
|
|
func (m IPMask) Size() (ones, bits int) {
|
|
|
|
ones, bits = simpleMaskLength(m), len(m)*8
|
|
|
|
if ones == -1 {
|
|
|
|
return 0, 0
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// String returns the hexadecimal form of m, with no punctuation.
|
|
|
|
func (m IPMask) String() string {
|
2014-06-11 21:40:00 -06:00
|
|
|
if len(m) == 0 {
|
2011-08-29 08:49:03 -06:00
|
|
|
return "<nil>"
|
|
|
|
}
|
2014-06-11 21:40:00 -06:00
|
|
|
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)
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
|
|
|
|
2011-09-07 12:01:12 -06:00
|
|
|
func networkNumberAndMask(n *IPNet) (ip IP, m IPMask) {
|
|
|
|
if ip = n.IP.To4(); ip == nil {
|
|
|
|
ip = n.IP
|
|
|
|
if len(ip) != IPv6len {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
m = n.Mask
|
|
|
|
switch len(m) {
|
|
|
|
case IPv4len:
|
|
|
|
if len(ip) != IPv4len {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
case IPv6len:
|
|
|
|
if len(ip) == IPv4len {
|
|
|
|
m = m[12:]
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Contains reports whether the network includes ip.
|
|
|
|
func (n *IPNet) Contains(ip IP) bool {
|
|
|
|
nn, m := networkNumberAndMask(n)
|
|
|
|
if x := ip.To4(); x != nil {
|
|
|
|
ip = x
|
|
|
|
}
|
|
|
|
l := len(ip)
|
|
|
|
if l != len(nn) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for i := 0; i < l; i++ {
|
|
|
|
if nn[i]&m[i] != ip[i]&m[i] {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
net: support IPv6 scoped addressing zone
This CL provides IPv6 scoped addressing zone support as defined
in RFC 4007 for internet protocol family connection setups.
Follwoing types and functions allow a literal IPv6 address with
zone identifer as theirs parameter.
pkg net, func Dial(string, string) (Conn, error)
pkg net, func DialOpt(string, ...DialOption) (Conn, error)
pkg net, func DialTimeout(string, string, time.Duration) (Conn, error)
pkg net, func Listen(string, string) (Listener, error)
pkg net, func ListenPacket(string, string) (PacketConn, error)
pkg net, func ResolveIPAddr(string, string) (*IPAddr, error)
pkg net, func ResolveTCPAddr(string, string) (*TCPAddr, error)
pkg net, func ResolveUDPAddr(string, string) (*UDPAddr, error)
pkg net, type IPAddr struct, Zone string
pkg net, type TCPAddr struct, Zone string
pkg net, type UDPAddr struct, Zone string
Also follwoing methods return a literal IPv6 address with zone
identifier string if possible.
pkg net, method (*IPAddr) String() string
pkg net, method (*TCPAddr) String() string
pkg net, method (*UDPAddr) String() string
Fixes #4234.
Fixes #4501.
Update #5081.
R=rsc, iant
CC=golang-dev
https://golang.org/cl/6816116
2013-03-22 18:57:40 -06:00
|
|
|
// Network returns the address's network name, "ip+net".
|
|
|
|
func (n *IPNet) Network() string { return "ip+net" }
|
|
|
|
|
2011-09-07 12:01:12 -06:00
|
|
|
// 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.
|
|
|
|
// 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".
|
|
|
|
func (n *IPNet) String() string {
|
|
|
|
nn, m := networkNumberAndMask(n)
|
|
|
|
if nn == nil || m == nil {
|
|
|
|
return "<nil>"
|
|
|
|
}
|
|
|
|
l := simpleMaskLength(m)
|
|
|
|
if l == -1 {
|
|
|
|
return nn.String() + "/" + m.String()
|
|
|
|
}
|
2014-12-31 10:45:05 -07:00
|
|
|
return nn.String() + "/" + uitoa(uint(l))
|
2011-09-07 12:01:12 -06:00
|
|
|
}
|
|
|
|
|
2008-09-16 14:42:47 -06:00
|
|
|
// Parse IPv4 address (d.d.d.d).
|
2009-03-05 16:48:12 -07:00
|
|
|
func parseIPv4(s string) IP {
|
2009-12-15 16:35:38 -07:00
|
|
|
var p [IPv4len]byte
|
|
|
|
i := 0
|
2008-09-16 14:42:47 -06:00
|
|
|
for j := 0; j < IPv4len; j++ {
|
2010-01-15 14:43:14 -07:00
|
|
|
if i >= len(s) {
|
|
|
|
// Missing octets.
|
|
|
|
return nil
|
|
|
|
}
|
2008-09-16 14:42:47 -06:00
|
|
|
if j > 0 {
|
|
|
|
if s[i] != '.' {
|
2009-11-09 13:07:39 -07:00
|
|
|
return nil
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
i++
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
|
|
|
var (
|
2009-12-15 16:35:38 -07:00
|
|
|
n int
|
|
|
|
ok bool
|
2008-09-16 14:42:47 -06:00
|
|
|
)
|
2009-12-15 16:35:38 -07:00
|
|
|
n, i, ok = dtoi(s, i)
|
2008-09-16 14:42:47 -06:00
|
|
|
if !ok || n > 0xFF {
|
2009-11-09 13:07:39 -07:00
|
|
|
return nil
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
p[j] = byte(n)
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
|
|
|
if i != len(s) {
|
2009-11-09 13:07:39 -07:00
|
|
|
return nil
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
return IPv4(p[0], p[1], p[2], p[3])
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
|
|
|
|
net: support IPv6 scoped addressing zone
This CL provides IPv6 scoped addressing zone support as defined
in RFC 4007 for internet protocol family connection setups.
Follwoing types and functions allow a literal IPv6 address with
zone identifer as theirs parameter.
pkg net, func Dial(string, string) (Conn, error)
pkg net, func DialOpt(string, ...DialOption) (Conn, error)
pkg net, func DialTimeout(string, string, time.Duration) (Conn, error)
pkg net, func Listen(string, string) (Listener, error)
pkg net, func ListenPacket(string, string) (PacketConn, error)
pkg net, func ResolveIPAddr(string, string) (*IPAddr, error)
pkg net, func ResolveTCPAddr(string, string) (*TCPAddr, error)
pkg net, func ResolveUDPAddr(string, string) (*UDPAddr, error)
pkg net, type IPAddr struct, Zone string
pkg net, type TCPAddr struct, Zone string
pkg net, type UDPAddr struct, Zone string
Also follwoing methods return a literal IPv6 address with zone
identifier string if possible.
pkg net, method (*IPAddr) String() string
pkg net, method (*TCPAddr) String() string
pkg net, method (*UDPAddr) String() string
Fixes #4234.
Fixes #4501.
Update #5081.
R=rsc, iant
CC=golang-dev
https://golang.org/cl/6816116
2013-03-22 18:57:40 -06:00
|
|
|
// parseIPv6 parses s as a literal IPv6 address described in RFC 4291
|
|
|
|
// and RFC 5952. It can also parse a literal scoped IPv6 address with
|
|
|
|
// zone identifier which is described in RFC 4007 when zoneAllowed is
|
|
|
|
// true.
|
|
|
|
func parseIPv6(s string, zoneAllowed bool) (ip IP, zone string) {
|
|
|
|
ip = make(IP, IPv6len)
|
2009-12-15 16:35:38 -07:00
|
|
|
ellipsis := -1 // position of ellipsis in p
|
|
|
|
i := 0 // index in string s
|
2008-09-16 14:42:47 -06:00
|
|
|
|
net: support IPv6 scoped addressing zone
This CL provides IPv6 scoped addressing zone support as defined
in RFC 4007 for internet protocol family connection setups.
Follwoing types and functions allow a literal IPv6 address with
zone identifer as theirs parameter.
pkg net, func Dial(string, string) (Conn, error)
pkg net, func DialOpt(string, ...DialOption) (Conn, error)
pkg net, func DialTimeout(string, string, time.Duration) (Conn, error)
pkg net, func Listen(string, string) (Listener, error)
pkg net, func ListenPacket(string, string) (PacketConn, error)
pkg net, func ResolveIPAddr(string, string) (*IPAddr, error)
pkg net, func ResolveTCPAddr(string, string) (*TCPAddr, error)
pkg net, func ResolveUDPAddr(string, string) (*UDPAddr, error)
pkg net, type IPAddr struct, Zone string
pkg net, type TCPAddr struct, Zone string
pkg net, type UDPAddr struct, Zone string
Also follwoing methods return a literal IPv6 address with zone
identifier string if possible.
pkg net, method (*IPAddr) String() string
pkg net, method (*TCPAddr) String() string
pkg net, method (*UDPAddr) String() string
Fixes #4234.
Fixes #4501.
Update #5081.
R=rsc, iant
CC=golang-dev
https://golang.org/cl/6816116
2013-03-22 18:57:40 -06:00
|
|
|
if zoneAllowed {
|
|
|
|
s, zone = splitHostZone(s)
|
|
|
|
}
|
|
|
|
|
2008-09-16 14:42:47 -06:00
|
|
|
// Might have leading ellipsis
|
|
|
|
if len(s) >= 2 && s[0] == ':' && s[1] == ':' {
|
2009-12-15 16:35:38 -07:00
|
|
|
ellipsis = 0
|
|
|
|
i = 2
|
2008-09-17 14:49:23 -06:00
|
|
|
// Might be only ellipsis
|
|
|
|
if i == len(s) {
|
net: support IPv6 scoped addressing zone
This CL provides IPv6 scoped addressing zone support as defined
in RFC 4007 for internet protocol family connection setups.
Follwoing types and functions allow a literal IPv6 address with
zone identifer as theirs parameter.
pkg net, func Dial(string, string) (Conn, error)
pkg net, func DialOpt(string, ...DialOption) (Conn, error)
pkg net, func DialTimeout(string, string, time.Duration) (Conn, error)
pkg net, func Listen(string, string) (Listener, error)
pkg net, func ListenPacket(string, string) (PacketConn, error)
pkg net, func ResolveIPAddr(string, string) (*IPAddr, error)
pkg net, func ResolveTCPAddr(string, string) (*TCPAddr, error)
pkg net, func ResolveUDPAddr(string, string) (*UDPAddr, error)
pkg net, type IPAddr struct, Zone string
pkg net, type TCPAddr struct, Zone string
pkg net, type UDPAddr struct, Zone string
Also follwoing methods return a literal IPv6 address with zone
identifier string if possible.
pkg net, method (*IPAddr) String() string
pkg net, method (*TCPAddr) String() string
pkg net, method (*UDPAddr) String() string
Fixes #4234.
Fixes #4501.
Update #5081.
R=rsc, iant
CC=golang-dev
https://golang.org/cl/6816116
2013-03-22 18:57:40 -06:00
|
|
|
return ip, zone
|
2008-09-17 14:49:23 -06:00
|
|
|
}
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Loop, parsing hex numbers followed by colon.
|
2009-12-15 16:35:38 -07:00
|
|
|
j := 0
|
2010-05-21 21:25:08 -06:00
|
|
|
for j < IPv6len {
|
2008-09-16 14:42:47 -06:00
|
|
|
// Hex number.
|
2009-12-15 16:35:38 -07:00
|
|
|
n, i1, ok := xtoi(s, i)
|
2008-09-17 14:49:23 -06:00
|
|
|
if !ok || n > 0xFFFF {
|
net: support IPv6 scoped addressing zone
This CL provides IPv6 scoped addressing zone support as defined
in RFC 4007 for internet protocol family connection setups.
Follwoing types and functions allow a literal IPv6 address with
zone identifer as theirs parameter.
pkg net, func Dial(string, string) (Conn, error)
pkg net, func DialOpt(string, ...DialOption) (Conn, error)
pkg net, func DialTimeout(string, string, time.Duration) (Conn, error)
pkg net, func Listen(string, string) (Listener, error)
pkg net, func ListenPacket(string, string) (PacketConn, error)
pkg net, func ResolveIPAddr(string, string) (*IPAddr, error)
pkg net, func ResolveTCPAddr(string, string) (*TCPAddr, error)
pkg net, func ResolveUDPAddr(string, string) (*UDPAddr, error)
pkg net, type IPAddr struct, Zone string
pkg net, type TCPAddr struct, Zone string
pkg net, type UDPAddr struct, Zone string
Also follwoing methods return a literal IPv6 address with zone
identifier string if possible.
pkg net, method (*IPAddr) String() string
pkg net, method (*TCPAddr) String() string
pkg net, method (*UDPAddr) String() string
Fixes #4234.
Fixes #4501.
Update #5081.
R=rsc, iant
CC=golang-dev
https://golang.org/cl/6816116
2013-03-22 18:57:40 -06:00
|
|
|
return nil, zone
|
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] == '.' {
|
2009-11-05 00:16:46 -07:00
|
|
|
if ellipsis < 0 && j != IPv6len-IPv4len {
|
2008-09-16 14:42:47 -06:00
|
|
|
// Not the right place.
|
net: support IPv6 scoped addressing zone
This CL provides IPv6 scoped addressing zone support as defined
in RFC 4007 for internet protocol family connection setups.
Follwoing types and functions allow a literal IPv6 address with
zone identifer as theirs parameter.
pkg net, func Dial(string, string) (Conn, error)
pkg net, func DialOpt(string, ...DialOption) (Conn, error)
pkg net, func DialTimeout(string, string, time.Duration) (Conn, error)
pkg net, func Listen(string, string) (Listener, error)
pkg net, func ListenPacket(string, string) (PacketConn, error)
pkg net, func ResolveIPAddr(string, string) (*IPAddr, error)
pkg net, func ResolveTCPAddr(string, string) (*TCPAddr, error)
pkg net, func ResolveUDPAddr(string, string) (*UDPAddr, error)
pkg net, type IPAddr struct, Zone string
pkg net, type TCPAddr struct, Zone string
pkg net, type UDPAddr struct, Zone string
Also follwoing methods return a literal IPv6 address with zone
identifier string if possible.
pkg net, method (*IPAddr) String() string
pkg net, method (*TCPAddr) String() string
pkg net, method (*UDPAddr) String() string
Fixes #4234.
Fixes #4501.
Update #5081.
R=rsc, iant
CC=golang-dev
https://golang.org/cl/6816116
2013-03-22 18:57:40 -06:00
|
|
|
return nil, zone
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
|
|
|
if j+IPv4len > IPv6len {
|
|
|
|
// Not enough room.
|
net: support IPv6 scoped addressing zone
This CL provides IPv6 scoped addressing zone support as defined
in RFC 4007 for internet protocol family connection setups.
Follwoing types and functions allow a literal IPv6 address with
zone identifer as theirs parameter.
pkg net, func Dial(string, string) (Conn, error)
pkg net, func DialOpt(string, ...DialOption) (Conn, error)
pkg net, func DialTimeout(string, string, time.Duration) (Conn, error)
pkg net, func Listen(string, string) (Listener, error)
pkg net, func ListenPacket(string, string) (PacketConn, error)
pkg net, func ResolveIPAddr(string, string) (*IPAddr, error)
pkg net, func ResolveTCPAddr(string, string) (*TCPAddr, error)
pkg net, func ResolveUDPAddr(string, string) (*UDPAddr, error)
pkg net, type IPAddr struct, Zone string
pkg net, type TCPAddr struct, Zone string
pkg net, type UDPAddr struct, Zone string
Also follwoing methods return a literal IPv6 address with zone
identifier string if possible.
pkg net, method (*IPAddr) String() string
pkg net, method (*TCPAddr) String() string
pkg net, method (*UDPAddr) String() string
Fixes #4234.
Fixes #4501.
Update #5081.
R=rsc, iant
CC=golang-dev
https://golang.org/cl/6816116
2013-03-22 18:57:40 -06:00
|
|
|
return nil, zone
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
net: support IPv6 scoped addressing zone
This CL provides IPv6 scoped addressing zone support as defined
in RFC 4007 for internet protocol family connection setups.
Follwoing types and functions allow a literal IPv6 address with
zone identifer as theirs parameter.
pkg net, func Dial(string, string) (Conn, error)
pkg net, func DialOpt(string, ...DialOption) (Conn, error)
pkg net, func DialTimeout(string, string, time.Duration) (Conn, error)
pkg net, func Listen(string, string) (Listener, error)
pkg net, func ListenPacket(string, string) (PacketConn, error)
pkg net, func ResolveIPAddr(string, string) (*IPAddr, error)
pkg net, func ResolveTCPAddr(string, string) (*TCPAddr, error)
pkg net, func ResolveUDPAddr(string, string) (*UDPAddr, error)
pkg net, type IPAddr struct, Zone string
pkg net, type TCPAddr struct, Zone string
pkg net, type UDPAddr struct, Zone string
Also follwoing methods return a literal IPv6 address with zone
identifier string if possible.
pkg net, method (*IPAddr) String() string
pkg net, method (*TCPAddr) String() string
pkg net, method (*UDPAddr) String() string
Fixes #4234.
Fixes #4501.
Update #5081.
R=rsc, iant
CC=golang-dev
https://golang.org/cl/6816116
2013-03-22 18:57:40 -06:00
|
|
|
ip4 := parseIPv4(s[i:])
|
|
|
|
if ip4 == nil {
|
|
|
|
return nil, zone
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
net: support IPv6 scoped addressing zone
This CL provides IPv6 scoped addressing zone support as defined
in RFC 4007 for internet protocol family connection setups.
Follwoing types and functions allow a literal IPv6 address with
zone identifer as theirs parameter.
pkg net, func Dial(string, string) (Conn, error)
pkg net, func DialOpt(string, ...DialOption) (Conn, error)
pkg net, func DialTimeout(string, string, time.Duration) (Conn, error)
pkg net, func Listen(string, string) (Listener, error)
pkg net, func ListenPacket(string, string) (PacketConn, error)
pkg net, func ResolveIPAddr(string, string) (*IPAddr, error)
pkg net, func ResolveTCPAddr(string, string) (*TCPAddr, error)
pkg net, func ResolveUDPAddr(string, string) (*UDPAddr, error)
pkg net, type IPAddr struct, Zone string
pkg net, type TCPAddr struct, Zone string
pkg net, type UDPAddr struct, Zone string
Also follwoing methods return a literal IPv6 address with zone
identifier string if possible.
pkg net, method (*IPAddr) String() string
pkg net, method (*TCPAddr) String() string
pkg net, method (*UDPAddr) String() string
Fixes #4234.
Fixes #4501.
Update #5081.
R=rsc, iant
CC=golang-dev
https://golang.org/cl/6816116
2013-03-22 18:57:40 -06:00
|
|
|
ip[j] = ip4[12]
|
|
|
|
ip[j+1] = ip4[13]
|
|
|
|
ip[j+2] = ip4[14]
|
|
|
|
ip[j+3] = ip4[15]
|
2009-12-15 16:35:38 -07:00
|
|
|
i = len(s)
|
2011-08-25 17:00:04 -06:00
|
|
|
j += IPv4len
|
2009-12-15 16:35:38 -07:00
|
|
|
break
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Save this 16-bit chunk.
|
net: support IPv6 scoped addressing zone
This CL provides IPv6 scoped addressing zone support as defined
in RFC 4007 for internet protocol family connection setups.
Follwoing types and functions allow a literal IPv6 address with
zone identifer as theirs parameter.
pkg net, func Dial(string, string) (Conn, error)
pkg net, func DialOpt(string, ...DialOption) (Conn, error)
pkg net, func DialTimeout(string, string, time.Duration) (Conn, error)
pkg net, func Listen(string, string) (Listener, error)
pkg net, func ListenPacket(string, string) (PacketConn, error)
pkg net, func ResolveIPAddr(string, string) (*IPAddr, error)
pkg net, func ResolveTCPAddr(string, string) (*TCPAddr, error)
pkg net, func ResolveUDPAddr(string, string) (*UDPAddr, error)
pkg net, type IPAddr struct, Zone string
pkg net, type TCPAddr struct, Zone string
pkg net, type UDPAddr struct, Zone string
Also follwoing methods return a literal IPv6 address with zone
identifier string if possible.
pkg net, method (*IPAddr) String() string
pkg net, method (*TCPAddr) String() string
pkg net, method (*UDPAddr) String() string
Fixes #4234.
Fixes #4501.
Update #5081.
R=rsc, iant
CC=golang-dev
https://golang.org/cl/6816116
2013-03-22 18:57:40 -06:00
|
|
|
ip[j] = byte(n >> 8)
|
|
|
|
ip[j+1] = byte(n)
|
2009-12-15 16:35:38 -07:00
|
|
|
j += 2
|
2008-09-16 14:42:47 -06:00
|
|
|
|
|
|
|
// Stop at end of string.
|
2009-12-15 16:35:38 -07:00
|
|
|
i = i1
|
2008-09-16 14:42:47 -06:00
|
|
|
if i == len(s) {
|
2009-11-09 13:07:39 -07:00
|
|
|
break
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise must be followed by colon and more.
|
2011-04-14 21:49:51 -06:00
|
|
|
if s[i] != ':' || i+1 == len(s) {
|
net: support IPv6 scoped addressing zone
This CL provides IPv6 scoped addressing zone support as defined
in RFC 4007 for internet protocol family connection setups.
Follwoing types and functions allow a literal IPv6 address with
zone identifer as theirs parameter.
pkg net, func Dial(string, string) (Conn, error)
pkg net, func DialOpt(string, ...DialOption) (Conn, error)
pkg net, func DialTimeout(string, string, time.Duration) (Conn, error)
pkg net, func Listen(string, string) (Listener, error)
pkg net, func ListenPacket(string, string) (PacketConn, error)
pkg net, func ResolveIPAddr(string, string) (*IPAddr, error)
pkg net, func ResolveTCPAddr(string, string) (*TCPAddr, error)
pkg net, func ResolveUDPAddr(string, string) (*UDPAddr, error)
pkg net, type IPAddr struct, Zone string
pkg net, type TCPAddr struct, Zone string
pkg net, type UDPAddr struct, Zone string
Also follwoing methods return a literal IPv6 address with zone
identifier string if possible.
pkg net, method (*IPAddr) String() string
pkg net, method (*TCPAddr) String() string
pkg net, method (*UDPAddr) String() string
Fixes #4234.
Fixes #4501.
Update #5081.
R=rsc, iant
CC=golang-dev
https://golang.org/cl/6816116
2013-03-22 18:57:40 -06:00
|
|
|
return nil, zone
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
i++
|
2008-09-16 14:42:47 -06:00
|
|
|
|
|
|
|
// Look for ellipsis.
|
2008-09-17 14:49:23 -06:00
|
|
|
if s[i] == ':' {
|
2009-12-15 16:35:38 -07:00
|
|
|
if ellipsis >= 0 { // already have one
|
net: support IPv6 scoped addressing zone
This CL provides IPv6 scoped addressing zone support as defined
in RFC 4007 for internet protocol family connection setups.
Follwoing types and functions allow a literal IPv6 address with
zone identifer as theirs parameter.
pkg net, func Dial(string, string) (Conn, error)
pkg net, func DialOpt(string, ...DialOption) (Conn, error)
pkg net, func DialTimeout(string, string, time.Duration) (Conn, error)
pkg net, func Listen(string, string) (Listener, error)
pkg net, func ListenPacket(string, string) (PacketConn, error)
pkg net, func ResolveIPAddr(string, string) (*IPAddr, error)
pkg net, func ResolveTCPAddr(string, string) (*TCPAddr, error)
pkg net, func ResolveUDPAddr(string, string) (*UDPAddr, error)
pkg net, type IPAddr struct, Zone string
pkg net, type TCPAddr struct, Zone string
pkg net, type UDPAddr struct, Zone string
Also follwoing methods return a literal IPv6 address with zone
identifier string if possible.
pkg net, method (*IPAddr) String() string
pkg net, method (*TCPAddr) String() string
pkg net, method (*UDPAddr) String() string
Fixes #4234.
Fixes #4501.
Update #5081.
R=rsc, iant
CC=golang-dev
https://golang.org/cl/6816116
2013-03-22 18:57:40 -06:00
|
|
|
return nil, zone
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
ellipsis = j
|
|
|
|
if i++; i == len(s) { // can be at end
|
2009-11-09 13:07:39 -07:00
|
|
|
break
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Must have used entire string.
|
|
|
|
if i != len(s) {
|
net: support IPv6 scoped addressing zone
This CL provides IPv6 scoped addressing zone support as defined
in RFC 4007 for internet protocol family connection setups.
Follwoing types and functions allow a literal IPv6 address with
zone identifer as theirs parameter.
pkg net, func Dial(string, string) (Conn, error)
pkg net, func DialOpt(string, ...DialOption) (Conn, error)
pkg net, func DialTimeout(string, string, time.Duration) (Conn, error)
pkg net, func Listen(string, string) (Listener, error)
pkg net, func ListenPacket(string, string) (PacketConn, error)
pkg net, func ResolveIPAddr(string, string) (*IPAddr, error)
pkg net, func ResolveTCPAddr(string, string) (*TCPAddr, error)
pkg net, func ResolveUDPAddr(string, string) (*UDPAddr, error)
pkg net, type IPAddr struct, Zone string
pkg net, type TCPAddr struct, Zone string
pkg net, type UDPAddr struct, Zone string
Also follwoing methods return a literal IPv6 address with zone
identifier string if possible.
pkg net, method (*IPAddr) String() string
pkg net, method (*TCPAddr) String() string
pkg net, method (*UDPAddr) String() string
Fixes #4234.
Fixes #4501.
Update #5081.
R=rsc, iant
CC=golang-dev
https://golang.org/cl/6816116
2013-03-22 18:57:40 -06:00
|
|
|
return nil, zone
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// If didn't parse enough, expand ellipsis.
|
|
|
|
if j < IPv6len {
|
|
|
|
if ellipsis < 0 {
|
net: support IPv6 scoped addressing zone
This CL provides IPv6 scoped addressing zone support as defined
in RFC 4007 for internet protocol family connection setups.
Follwoing types and functions allow a literal IPv6 address with
zone identifer as theirs parameter.
pkg net, func Dial(string, string) (Conn, error)
pkg net, func DialOpt(string, ...DialOption) (Conn, error)
pkg net, func DialTimeout(string, string, time.Duration) (Conn, error)
pkg net, func Listen(string, string) (Listener, error)
pkg net, func ListenPacket(string, string) (PacketConn, error)
pkg net, func ResolveIPAddr(string, string) (*IPAddr, error)
pkg net, func ResolveTCPAddr(string, string) (*TCPAddr, error)
pkg net, func ResolveUDPAddr(string, string) (*UDPAddr, error)
pkg net, type IPAddr struct, Zone string
pkg net, type TCPAddr struct, Zone string
pkg net, type UDPAddr struct, Zone string
Also follwoing methods return a literal IPv6 address with zone
identifier string if possible.
pkg net, method (*IPAddr) String() string
pkg net, method (*TCPAddr) String() string
pkg net, method (*UDPAddr) String() string
Fixes #4234.
Fixes #4501.
Update #5081.
R=rsc, iant
CC=golang-dev
https://golang.org/cl/6816116
2013-03-22 18:57:40 -06:00
|
|
|
return nil, zone
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
n := IPv6len - j
|
2009-11-09 22:23:52 -07:00
|
|
|
for k := j - 1; k >= ellipsis; k-- {
|
net: support IPv6 scoped addressing zone
This CL provides IPv6 scoped addressing zone support as defined
in RFC 4007 for internet protocol family connection setups.
Follwoing types and functions allow a literal IPv6 address with
zone identifer as theirs parameter.
pkg net, func Dial(string, string) (Conn, error)
pkg net, func DialOpt(string, ...DialOption) (Conn, error)
pkg net, func DialTimeout(string, string, time.Duration) (Conn, error)
pkg net, func Listen(string, string) (Listener, error)
pkg net, func ListenPacket(string, string) (PacketConn, error)
pkg net, func ResolveIPAddr(string, string) (*IPAddr, error)
pkg net, func ResolveTCPAddr(string, string) (*TCPAddr, error)
pkg net, func ResolveUDPAddr(string, string) (*UDPAddr, error)
pkg net, type IPAddr struct, Zone string
pkg net, type TCPAddr struct, Zone string
pkg net, type UDPAddr struct, Zone string
Also follwoing methods return a literal IPv6 address with zone
identifier string if possible.
pkg net, method (*IPAddr) String() string
pkg net, method (*TCPAddr) String() string
pkg net, method (*UDPAddr) String() string
Fixes #4234.
Fixes #4501.
Update #5081.
R=rsc, iant
CC=golang-dev
https://golang.org/cl/6816116
2013-03-22 18:57:40 -06:00
|
|
|
ip[k+n] = ip[k]
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
2009-11-09 22:23:52 -07:00
|
|
|
for k := ellipsis + n - 1; k >= ellipsis; k-- {
|
net: support IPv6 scoped addressing zone
This CL provides IPv6 scoped addressing zone support as defined
in RFC 4007 for internet protocol family connection setups.
Follwoing types and functions allow a literal IPv6 address with
zone identifer as theirs parameter.
pkg net, func Dial(string, string) (Conn, error)
pkg net, func DialOpt(string, ...DialOption) (Conn, error)
pkg net, func DialTimeout(string, string, time.Duration) (Conn, error)
pkg net, func Listen(string, string) (Listener, error)
pkg net, func ListenPacket(string, string) (PacketConn, error)
pkg net, func ResolveIPAddr(string, string) (*IPAddr, error)
pkg net, func ResolveTCPAddr(string, string) (*TCPAddr, error)
pkg net, func ResolveUDPAddr(string, string) (*UDPAddr, error)
pkg net, type IPAddr struct, Zone string
pkg net, type TCPAddr struct, Zone string
pkg net, type UDPAddr struct, Zone string
Also follwoing methods return a literal IPv6 address with zone
identifier string if possible.
pkg net, method (*IPAddr) String() string
pkg net, method (*TCPAddr) String() string
pkg net, method (*UDPAddr) String() string
Fixes #4234.
Fixes #4501.
Update #5081.
R=rsc, iant
CC=golang-dev
https://golang.org/cl/6816116
2013-03-22 18:57:40 -06:00
|
|
|
ip[k] = 0
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
2013-12-20 05:29:28 -07:00
|
|
|
} else if ellipsis >= 0 {
|
|
|
|
// Ellipsis must represent at least one 0 group.
|
|
|
|
return nil, zone
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
net: support IPv6 scoped addressing zone
This CL provides IPv6 scoped addressing zone support as defined
in RFC 4007 for internet protocol family connection setups.
Follwoing types and functions allow a literal IPv6 address with
zone identifer as theirs parameter.
pkg net, func Dial(string, string) (Conn, error)
pkg net, func DialOpt(string, ...DialOption) (Conn, error)
pkg net, func DialTimeout(string, string, time.Duration) (Conn, error)
pkg net, func Listen(string, string) (Listener, error)
pkg net, func ListenPacket(string, string) (PacketConn, error)
pkg net, func ResolveIPAddr(string, string) (*IPAddr, error)
pkg net, func ResolveTCPAddr(string, string) (*TCPAddr, error)
pkg net, func ResolveUDPAddr(string, string) (*UDPAddr, error)
pkg net, type IPAddr struct, Zone string
pkg net, type TCPAddr struct, Zone string
pkg net, type UDPAddr struct, Zone string
Also follwoing methods return a literal IPv6 address with zone
identifier string if possible.
pkg net, method (*IPAddr) String() string
pkg net, method (*TCPAddr) String() string
pkg net, method (*UDPAddr) String() string
Fixes #4234.
Fixes #4501.
Update #5081.
R=rsc, iant
CC=golang-dev
https://golang.org/cl/6816116
2013-03-22 18:57:40 -06:00
|
|
|
return ip, zone
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
|
|
|
|
2009-03-05 16:48:12 -07:00
|
|
|
// 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.
|
|
|
|
// If s is not a valid textual representation of an IP address,
|
|
|
|
// ParseIP returns nil.
|
|
|
|
func ParseIP(s string) IP {
|
2014-06-11 21:40:00 -06:00
|
|
|
for i := 0; i < len(s); i++ {
|
|
|
|
switch s[i] {
|
|
|
|
case '.':
|
|
|
|
return parseIPv4(s)
|
|
|
|
case ':':
|
|
|
|
ip, _ := parseIPv6(s, false)
|
|
|
|
return ip
|
|
|
|
}
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
2014-06-11 21:40:00 -06:00
|
|
|
return nil
|
2008-09-16 14:42:47 -06:00
|
|
|
}
|
2011-03-15 12:05:07 -06:00
|
|
|
|
|
|
|
// ParseCIDR parses s as a CIDR notation IP address and mask,
|
2011-08-29 08:49:03 -06:00
|
|
|
// like "192.168.100.1/24" or "2001:DB8::/48", as defined in
|
2011-03-28 21:28:42 -06:00
|
|
|
// RFC 4632 and RFC 4291.
|
2011-09-07 12:01:12 -06:00
|
|
|
//
|
|
|
|
// It returns the IP address and the network implied by the IP
|
2016-03-01 16:21:55 -07:00
|
|
|
// and mask. For example, ParseCIDR("192.168.100.1/16") returns
|
2011-09-07 12:01:12 -06:00
|
|
|
// the IP address 192.168.100.1 and the network 192.168.0.0/16.
|
2011-11-01 20:05:34 -06:00
|
|
|
func ParseCIDR(s string) (IP, *IPNet, error) {
|
2011-03-15 12:05:07 -06:00
|
|
|
i := byteIndex(s, '/')
|
|
|
|
if i < 0 {
|
net: fix inconsistent errors
These a series of changes fix inconsistent errors on the package net
APIs. Now almost all the APIs return OpError as a common error type
except Lookup, Resolve and Parse APIs. The Lookup, Resolve and Parse
APIs return more specific errors such as DNSError, AddrError or
ParseError.
An OpError may contain nested error information. For example, Dial may
return an OpError containing a DNSError, AddrError, unexposed type/value
or other package's type/value like the following:
OpError{/* dial info */, Err: &DNSError{}}
OpError{/* dial info */, Err: &AddrError{}}
OpError{/* dial info */, Err: <unexposed type or value>}
OpError{/* dial info */, Err: <other package's type or value>}
and Read and Write may return an OpError containing other OpError when
an application uses io.Copy or similar:
OpError{/* for io.Reader */, Err: &OpError{/* for io.Writer */}}
When an endpoint is created for connection-oriented byte-stream
protocols, Read may return an io.EOF when the connection is closed by
remote endpoint.
Fixes #4856.
A series of changes:
- net: fix inconsistent error values on Dial, Listen partially
https://go.googlesource.com/go/+/89b7c66d0d14462fd7893be4290bdfe5f9063ae1
- net: fix inconsistent error values on Read
https://go.googlesource.com/go/+/ec1144423f45e010c72363fe59291d43214b6e31
- net: fix inconsistent error values on Write
https://go.googlesource.com/go/+/11b5f98bf0d5eb8854f735cc332c912725070214
- net: fix inconsistent error values on Close
https://go.googlesource.com/go/+/310db63c5bc121e7bfccb494c01a6b91a257e7fc
- net: fix inconsistent error values on Accept
https://go.googlesource.com/go/+/4540e162b1aefda8157372764ad3d290a414ef1d
- net: fix inconsistent error values on File
https://go.googlesource.com/go/+/885111365ba0a74421059bfbd18f4c57c1e70332
- net: fix inconsistent error values on setters
https://go.googlesource.com/go/+/2173a27903897c481b0a0daf3ca3e0a0685701db
- net: fix inconsistent error values on Interface
https://go.googlesource.com/go/+/456cf0f22c93e1a6654980f4a48a564555f6c8a2
- net: fix inconsistent error values on Lookup
https://go.googlesource.com/go/+/0fc582e87942b2e52bed751b6c56660ba99e9a7d
- net: add Source field to OpError
https://go.googlesource.com/go/+/afd2d2b6df3ebfe99faf347030f15adfdf422fa0
Change-Id: Id678e369088dc9fbe9073cfe7ff8a8754a57d61f
Reviewed-on: https://go-review.googlesource.com/9236
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2015-04-21 06:20:15 -06:00
|
|
|
return nil, nil, &ParseError{Type: "CIDR address", Text: s}
|
2011-03-15 12:05:07 -06:00
|
|
|
}
|
net: support IPv6 scoped addressing zone
This CL provides IPv6 scoped addressing zone support as defined
in RFC 4007 for internet protocol family connection setups.
Follwoing types and functions allow a literal IPv6 address with
zone identifer as theirs parameter.
pkg net, func Dial(string, string) (Conn, error)
pkg net, func DialOpt(string, ...DialOption) (Conn, error)
pkg net, func DialTimeout(string, string, time.Duration) (Conn, error)
pkg net, func Listen(string, string) (Listener, error)
pkg net, func ListenPacket(string, string) (PacketConn, error)
pkg net, func ResolveIPAddr(string, string) (*IPAddr, error)
pkg net, func ResolveTCPAddr(string, string) (*TCPAddr, error)
pkg net, func ResolveUDPAddr(string, string) (*UDPAddr, error)
pkg net, type IPAddr struct, Zone string
pkg net, type TCPAddr struct, Zone string
pkg net, type UDPAddr struct, Zone string
Also follwoing methods return a literal IPv6 address with zone
identifier string if possible.
pkg net, method (*IPAddr) String() string
pkg net, method (*TCPAddr) String() string
pkg net, method (*UDPAddr) String() string
Fixes #4234.
Fixes #4501.
Update #5081.
R=rsc, iant
CC=golang-dev
https://golang.org/cl/6816116
2013-03-22 18:57:40 -06:00
|
|
|
addr, mask := s[:i], s[i+1:]
|
2011-08-25 17:00:04 -06:00
|
|
|
iplen := IPv4len
|
net: support IPv6 scoped addressing zone
This CL provides IPv6 scoped addressing zone support as defined
in RFC 4007 for internet protocol family connection setups.
Follwoing types and functions allow a literal IPv6 address with
zone identifer as theirs parameter.
pkg net, func Dial(string, string) (Conn, error)
pkg net, func DialOpt(string, ...DialOption) (Conn, error)
pkg net, func DialTimeout(string, string, time.Duration) (Conn, error)
pkg net, func Listen(string, string) (Listener, error)
pkg net, func ListenPacket(string, string) (PacketConn, error)
pkg net, func ResolveIPAddr(string, string) (*IPAddr, error)
pkg net, func ResolveTCPAddr(string, string) (*TCPAddr, error)
pkg net, func ResolveUDPAddr(string, string) (*UDPAddr, error)
pkg net, type IPAddr struct, Zone string
pkg net, type TCPAddr struct, Zone string
pkg net, type UDPAddr struct, Zone string
Also follwoing methods return a literal IPv6 address with zone
identifier string if possible.
pkg net, method (*IPAddr) String() string
pkg net, method (*TCPAddr) String() string
pkg net, method (*UDPAddr) String() string
Fixes #4234.
Fixes #4501.
Update #5081.
R=rsc, iant
CC=golang-dev
https://golang.org/cl/6816116
2013-03-22 18:57:40 -06:00
|
|
|
ip := parseIPv4(addr)
|
2011-03-28 21:28:42 -06:00
|
|
|
if ip == nil {
|
2011-08-25 17:00:04 -06:00
|
|
|
iplen = IPv6len
|
net: support IPv6 scoped addressing zone
This CL provides IPv6 scoped addressing zone support as defined
in RFC 4007 for internet protocol family connection setups.
Follwoing types and functions allow a literal IPv6 address with
zone identifer as theirs parameter.
pkg net, func Dial(string, string) (Conn, error)
pkg net, func DialOpt(string, ...DialOption) (Conn, error)
pkg net, func DialTimeout(string, string, time.Duration) (Conn, error)
pkg net, func Listen(string, string) (Listener, error)
pkg net, func ListenPacket(string, string) (PacketConn, error)
pkg net, func ResolveIPAddr(string, string) (*IPAddr, error)
pkg net, func ResolveTCPAddr(string, string) (*TCPAddr, error)
pkg net, func ResolveUDPAddr(string, string) (*UDPAddr, error)
pkg net, type IPAddr struct, Zone string
pkg net, type TCPAddr struct, Zone string
pkg net, type UDPAddr struct, Zone string
Also follwoing methods return a literal IPv6 address with zone
identifier string if possible.
pkg net, method (*IPAddr) String() string
pkg net, method (*TCPAddr) String() string
pkg net, method (*UDPAddr) String() string
Fixes #4234.
Fixes #4501.
Update #5081.
R=rsc, iant
CC=golang-dev
https://golang.org/cl/6816116
2013-03-22 18:57:40 -06:00
|
|
|
ip, _ = parseIPv6(addr, false)
|
2011-03-28 21:28:42 -06:00
|
|
|
}
|
net: support IPv6 scoped addressing zone
This CL provides IPv6 scoped addressing zone support as defined
in RFC 4007 for internet protocol family connection setups.
Follwoing types and functions allow a literal IPv6 address with
zone identifer as theirs parameter.
pkg net, func Dial(string, string) (Conn, error)
pkg net, func DialOpt(string, ...DialOption) (Conn, error)
pkg net, func DialTimeout(string, string, time.Duration) (Conn, error)
pkg net, func Listen(string, string) (Listener, error)
pkg net, func ListenPacket(string, string) (PacketConn, error)
pkg net, func ResolveIPAddr(string, string) (*IPAddr, error)
pkg net, func ResolveTCPAddr(string, string) (*TCPAddr, error)
pkg net, func ResolveUDPAddr(string, string) (*UDPAddr, error)
pkg net, type IPAddr struct, Zone string
pkg net, type TCPAddr struct, Zone string
pkg net, type UDPAddr struct, Zone string
Also follwoing methods return a literal IPv6 address with zone
identifier string if possible.
pkg net, method (*IPAddr) String() string
pkg net, method (*TCPAddr) String() string
pkg net, method (*UDPAddr) String() string
Fixes #4234.
Fixes #4501.
Update #5081.
R=rsc, iant
CC=golang-dev
https://golang.org/cl/6816116
2013-03-22 18:57:40 -06:00
|
|
|
n, i, ok := dtoi(mask, 0)
|
|
|
|
if ip == nil || !ok || i != len(mask) || n < 0 || n > 8*iplen {
|
net: fix inconsistent errors
These a series of changes fix inconsistent errors on the package net
APIs. Now almost all the APIs return OpError as a common error type
except Lookup, Resolve and Parse APIs. The Lookup, Resolve and Parse
APIs return more specific errors such as DNSError, AddrError or
ParseError.
An OpError may contain nested error information. For example, Dial may
return an OpError containing a DNSError, AddrError, unexposed type/value
or other package's type/value like the following:
OpError{/* dial info */, Err: &DNSError{}}
OpError{/* dial info */, Err: &AddrError{}}
OpError{/* dial info */, Err: <unexposed type or value>}
OpError{/* dial info */, Err: <other package's type or value>}
and Read and Write may return an OpError containing other OpError when
an application uses io.Copy or similar:
OpError{/* for io.Reader */, Err: &OpError{/* for io.Writer */}}
When an endpoint is created for connection-oriented byte-stream
protocols, Read may return an io.EOF when the connection is closed by
remote endpoint.
Fixes #4856.
A series of changes:
- net: fix inconsistent error values on Dial, Listen partially
https://go.googlesource.com/go/+/89b7c66d0d14462fd7893be4290bdfe5f9063ae1
- net: fix inconsistent error values on Read
https://go.googlesource.com/go/+/ec1144423f45e010c72363fe59291d43214b6e31
- net: fix inconsistent error values on Write
https://go.googlesource.com/go/+/11b5f98bf0d5eb8854f735cc332c912725070214
- net: fix inconsistent error values on Close
https://go.googlesource.com/go/+/310db63c5bc121e7bfccb494c01a6b91a257e7fc
- net: fix inconsistent error values on Accept
https://go.googlesource.com/go/+/4540e162b1aefda8157372764ad3d290a414ef1d
- net: fix inconsistent error values on File
https://go.googlesource.com/go/+/885111365ba0a74421059bfbd18f4c57c1e70332
- net: fix inconsistent error values on setters
https://go.googlesource.com/go/+/2173a27903897c481b0a0daf3ca3e0a0685701db
- net: fix inconsistent error values on Interface
https://go.googlesource.com/go/+/456cf0f22c93e1a6654980f4a48a564555f6c8a2
- net: fix inconsistent error values on Lookup
https://go.googlesource.com/go/+/0fc582e87942b2e52bed751b6c56660ba99e9a7d
- net: add Source field to OpError
https://go.googlesource.com/go/+/afd2d2b6df3ebfe99faf347030f15adfdf422fa0
Change-Id: Id678e369088dc9fbe9073cfe7ff8a8754a57d61f
Reviewed-on: https://go-review.googlesource.com/9236
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2015-04-21 06:20:15 -06:00
|
|
|
return nil, nil, &ParseError{Type: "CIDR address", Text: s}
|
2011-03-15 12:05:07 -06:00
|
|
|
}
|
2011-09-07 12:01:12 -06:00
|
|
|
m := CIDRMask(n, 8*iplen)
|
net, cmd/fix: add IPv6 scoped addressing zone to INET, INET6 address structs
This CL starts to introduce IPv6 scoped addressing capability
into the net package.
The Public API changes are:
+pkg net, type IPAddr struct, Zone string
+pkg net, type IPNet struct, Zone string
+pkg net, type TCPAddr struct, Zone string
+pkg net, type UDPAddr struct, Zone string
Update #4234.
R=rsc, bradfitz, iant
CC=golang-dev
https://golang.org/cl/6849045
2012-11-26 08:45:42 -07:00
|
|
|
return ip, &IPNet{IP: ip.Mask(m), Mask: m}, nil
|
2011-03-15 12:05:07 -06:00
|
|
|
}
|