diff options
Diffstat (limited to 'llgo/third_party/gofrontend/libgo/go/net/ip.go')
| -rw-r--r-- | llgo/third_party/gofrontend/libgo/go/net/ip.go | 77 |
1 files changed, 32 insertions, 45 deletions
diff --git a/llgo/third_party/gofrontend/libgo/go/net/ip.go b/llgo/third_party/gofrontend/libgo/go/net/ip.go index 4a93e97b39d..cc004d60729 100644 --- a/llgo/third_party/gofrontend/libgo/go/net/ip.go +++ b/llgo/third_party/gofrontend/libgo/go/net/ip.go @@ -12,8 +12,6 @@ package net -import "errors" - // IP address lengths (bytes). const ( IPv4len = 4 @@ -108,58 +106,57 @@ var ( IPv6linklocalallrouters = IP{0xff, 0x02, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x02} ) -// IsUnspecified returns true if ip is an unspecified address. +// IsUnspecified reports whether ip is an unspecified address. func (ip IP) IsUnspecified() bool { - if ip.Equal(IPv4zero) || ip.Equal(IPv6unspecified) { - return true - } - return false + return ip.Equal(IPv4zero) || ip.Equal(IPv6unspecified) } -// IsLoopback returns true if ip is a loopback address. +// IsLoopback reports whether ip is a loopback address. func (ip IP) IsLoopback() bool { - if ip4 := ip.To4(); ip4 != nil && ip4[0] == 127 { - return true + if ip4 := ip.To4(); ip4 != nil { + return ip4[0] == 127 } return ip.Equal(IPv6loopback) } -// IsMulticast returns true if ip is a multicast address. +// IsMulticast reports whether ip is a multicast address. func (ip IP) IsMulticast() bool { - if ip4 := ip.To4(); ip4 != nil && ip4[0]&0xf0 == 0xe0 { - return true + if ip4 := ip.To4(); ip4 != nil { + return ip4[0]&0xf0 == 0xe0 } - return ip[0] == 0xff + return len(ip) == IPv6len && ip[0] == 0xff } -// IsInterfaceLinkLocalMulticast returns true if ip is +// IsInterfaceLocalMulticast reports whether ip is // an interface-local multicast address. func (ip IP) IsInterfaceLocalMulticast() bool { return len(ip) == IPv6len && ip[0] == 0xff && ip[1]&0x0f == 0x01 } -// IsLinkLocalMulticast returns true if ip is a link-local +// IsLinkLocalMulticast reports whether ip is a link-local // multicast address. func (ip IP) IsLinkLocalMulticast() bool { - if ip4 := ip.To4(); ip4 != nil && ip4[0] == 224 && ip4[1] == 0 && ip4[2] == 0 { - return true + if ip4 := ip.To4(); ip4 != nil { + return ip4[0] == 224 && ip4[1] == 0 && ip4[2] == 0 } - return ip[0] == 0xff && ip[1]&0x0f == 0x02 + return len(ip) == IPv6len && ip[0] == 0xff && ip[1]&0x0f == 0x02 } -// IsLinkLocalUnicast returns true if ip is a link-local +// IsLinkLocalUnicast reports whether ip is a link-local // unicast address. func (ip IP) IsLinkLocalUnicast() bool { - if ip4 := ip.To4(); ip4 != nil && ip4[0] == 169 && ip4[1] == 254 { - return true + if ip4 := ip.To4(); ip4 != nil { + return ip4[0] == 169 && ip4[1] == 254 } - return ip[0] == 0xfe && ip[1]&0xc0 == 0x80 + return len(ip) == IPv6len && ip[0] == 0xfe && ip[1]&0xc0 == 0x80 } -// IsGlobalUnicast returns true if ip is a global unicast +// IsGlobalUnicast reports whether ip is a global unicast // address. func (ip IP) IsGlobalUnicast() bool { - return !ip.IsUnspecified() && + return (len(ip) == IPv4len || len(ip) == IPv6len) && + !ip.Equal(IPv4bcast) && + !ip.IsUnspecified() && !ip.IsLoopback() && !ip.IsMulticast() && !ip.IsLinkLocalUnicast() @@ -267,10 +264,10 @@ func (ip IP) String() string { // If IPv4, use dotted notation. if p4 := p.To4(); len(p4) == IPv4len { - return itod(uint(p4[0])) + "." + - itod(uint(p4[1])) + "." + - itod(uint(p4[2])) + "." + - itod(uint(p4[3])) + return uitoa(uint(p4[0])) + "." + + uitoa(uint(p4[1])) + "." + + uitoa(uint(p4[2])) + "." + + uitoa(uint(p4[3])) } if len(p) != IPv6len { return "?" @@ -331,7 +328,7 @@ func (ip IP) MarshalText() ([]byte, error) { return []byte(""), nil } if len(ip) != IPv4len && len(ip) != IPv6len { - return nil, errors.New("invalid IP address") + return nil, &AddrError{Err: "invalid IP address", Addr: ip.String()} } return []byte(ip.String()), nil } @@ -346,13 +343,13 @@ func (ip *IP) UnmarshalText(text []byte) error { s := string(text) x := ParseIP(s) if x == nil { - return &ParseError{"IP address", s} + return &ParseError{Type: "IP address", Text: s} } *ip = x return nil } -// Equal returns true if ip and x are the same IP address. +// Equal reports whether ip and x are the same IP address. // An IPv4 address and that same address in IPv6 form are // considered to be equal. func (ip IP) Equal(x IP) bool { @@ -491,7 +488,7 @@ func (n *IPNet) String() string { if l == -1 { return nn.String() + "/" + m.String() } - return nn.String() + "/" + itod(uint(l)) + return nn.String() + "/" + uitoa(uint(l)) } // Parse IPv4 address (d.d.d.d). @@ -633,16 +630,6 @@ func parseIPv6(s string, zoneAllowed bool) (ip IP, zone string) { return ip, zone } -// A ParseError represents a malformed text string and the type of string that was expected. -type ParseError struct { - Type string - Text string -} - -func (e *ParseError) Error() string { - return "invalid " + e.Type + ": " + e.Text -} - // 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. @@ -671,7 +658,7 @@ func ParseIP(s string) IP { func ParseCIDR(s string) (IP, *IPNet, error) { i := byteIndex(s, '/') if i < 0 { - return nil, nil, &ParseError{"CIDR address", s} + return nil, nil, &ParseError{Type: "CIDR address", Text: s} } addr, mask := s[:i], s[i+1:] iplen := IPv4len @@ -682,7 +669,7 @@ func ParseCIDR(s string) (IP, *IPNet, error) { } n, i, ok := dtoi(mask, 0) if ip == nil || !ok || i != len(mask) || n < 0 || n > 8*iplen { - return nil, nil, &ParseError{"CIDR address", s} + return nil, nil, &ParseError{Type: "CIDR address", Text: s} } m := CIDRMask(n, 8*iplen) return ip, &IPNet{IP: ip.Mask(m), Mask: m}, nil |

