2016-03-01 15:57:46 -07:00
|
|
|
// Copyright 2010 The Go Authors. All rights reserved.
|
2010-05-21 18:30:40 -06:00
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package net
|
|
|
|
|
2016-04-14 18:47:25 -06:00
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"syscall"
|
|
|
|
)
|
2016-03-02 14:22:11 -07:00
|
|
|
|
2016-12-27 14:14:01 -07:00
|
|
|
// BUG(mikio): On every POSIX platform, reads from the "ip4" network
|
|
|
|
// using the ReadFrom or ReadFromIP method might not return a complete
|
|
|
|
// IPv4 packet, including its header, even if there is space
|
|
|
|
// available. This can occur even in cases where Read or ReadMsgIP
|
|
|
|
// could return a complete packet. For this reason, it is recommended
|
2017-01-05 18:49:08 -07:00
|
|
|
// that you do not use these methods if it is important to receive a
|
2016-12-27 14:14:01 -07:00
|
|
|
// full packet.
|
|
|
|
//
|
|
|
|
// The Go 1 compatibility guidelines make it impossible for us to
|
|
|
|
// change the behavior of these methods; use Read or ReadMsgIP
|
|
|
|
// instead.
|
|
|
|
|
2016-08-21 14:52:15 -06:00
|
|
|
// BUG(mikio): On NaCl, Plan 9 and Windows, the ReadMsgIP and
|
|
|
|
// WriteMsgIP methods of IPConn are not implemented.
|
|
|
|
|
|
|
|
// BUG(mikio): On Windows, the File method of IPConn is not
|
|
|
|
// implemented.
|
|
|
|
|
2012-07-19 17:32:25 -06:00
|
|
|
// IPAddr represents the address of an IP end point.
|
2010-05-21 18:30:40 -06:00
|
|
|
type IPAddr struct {
|
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
|
|
|
IP IP
|
|
|
|
Zone string // IPv6 scoped addressing zone
|
2010-05-21 18:30:40 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Network returns the address's network name, "ip".
|
|
|
|
func (a *IPAddr) Network() string { return "ip" }
|
|
|
|
|
2010-07-27 01:22:22 -06:00
|
|
|
func (a *IPAddr) String() string {
|
|
|
|
if a == nil {
|
|
|
|
return "<nil>"
|
|
|
|
}
|
2015-07-06 21:10:15 -06:00
|
|
|
ip := ipEmptyString(a.IP)
|
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 a.Zone != "" {
|
2015-07-06 21:10:15 -06:00
|
|
|
return ip + "%" + a.Zone
|
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
|
|
|
}
|
2015-07-06 21:10:15 -06:00
|
|
|
return ip
|
2010-07-27 01:22:22 -06:00
|
|
|
}
|
2010-05-21 18:30:40 -06:00
|
|
|
|
2015-03-26 08:26:45 -06:00
|
|
|
func (a *IPAddr) isWildcard() bool {
|
|
|
|
if a == nil || a.IP == nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return a.IP.IsUnspecified()
|
|
|
|
}
|
|
|
|
|
2015-05-29 16:33:16 -06:00
|
|
|
func (a *IPAddr) opAddr() Addr {
|
|
|
|
if a == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
2017-01-08 01:28:01 -07:00
|
|
|
// ResolveIPAddr returns an address of IP end point.
|
2016-10-24 18:49:22 -06:00
|
|
|
//
|
2017-01-08 01:28:01 -07:00
|
|
|
// The network must be an IP network name.
|
|
|
|
//
|
|
|
|
// If the host in the address parameter is not a literal IP address,
|
|
|
|
// ResolveIPAddr resolves the address to an address of IP end point.
|
|
|
|
// Otherwise, it parses the address as a literal IP address.
|
|
|
|
// The address parameter can use a host name, but this is not
|
|
|
|
// recommended, because it will return at most one of the host name's
|
|
|
|
// IP addresses.
|
|
|
|
//
|
|
|
|
// See func Dial for a description of the network and address
|
|
|
|
// parameters.
|
|
|
|
func ResolveIPAddr(network, address string) (*IPAddr, error) {
|
|
|
|
if network == "" { // a hint wildcard for Go 1.0 undocumented behavior
|
|
|
|
network = "ip"
|
2012-11-30 22:49:54 -07:00
|
|
|
}
|
2017-01-08 01:28:01 -07:00
|
|
|
afnet, _, err := parseNetwork(context.Background(), network, false)
|
2010-05-21 18:30:40 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2012-11-27 14:36:05 -07:00
|
|
|
switch afnet {
|
|
|
|
case "ip", "ip4", "ip6":
|
|
|
|
default:
|
2017-01-08 01:28:01 -07:00
|
|
|
return nil, UnknownNetworkError(network)
|
2010-05-21 18:30:40 -06:00
|
|
|
}
|
2017-01-08 01:28:01 -07:00
|
|
|
addrs, err := DefaultResolver.internetAddrList(context.Background(), afnet, address)
|
2012-11-27 16:45:23 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
net: don't return IPv4 unspecified addr for Resolve*Addr of [::] or [::]:n
ResolveTCPAddr, ResolveUDPAddr, and ResolveIPAddr return at most one
address. When given a name like "golang.org" to resolve that might
have more than 1 address, the net package has historically preferred
IPv4 addresses, with the assumption that many users don't yet have
IPv6 connectivity and randomly selecting between an IPv4 address and
an IPv6 address at runtime wouldn't be a good experience for IPv4-only
users.
In CL 45088 (78cf0e56) I modified the resolution of the
unspecified/empty address to internally resolve to both IPv6 "::" and
0.0.0.0 to fix issue #18806.
That code has 3 other callers I hadn't considered, though: the
Resolve*Addr functions. Since they preferred IPv4, any Resolve*Addr of
"[::]:port" or "::" (for ResolveIPAddr) would internally resolve both
"::" and 0.0.0.0 and then prefer 0.0.0.0, even though the user was
looking up an IPv6 literal.
Add tests and fix it, not by undoing the fix to #18806 but by
selecting the preference function for Resolve*Addr more explicitly: we
still prefer IPv4, but if the address being looked up was an IPv6
literal, prefer IPv6.
The tests are skipped on machines without IPv6.
Fixes #20911
Change-Id: Ib7036cc43182ae4118cd1390c254e17c04a251a3
Reviewed-on: https://go-review.googlesource.com/47554
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
Reviewed-by: Russ Cox <rsc@golang.org>
2017-07-05 23:00:21 -06:00
|
|
|
return addrs.forResolve(network, address).(*IPAddr), nil
|
2010-05-21 18:30:40 -06:00
|
|
|
}
|
2016-03-02 14:22:11 -07:00
|
|
|
|
|
|
|
// IPConn is the implementation of the Conn and PacketConn interfaces
|
|
|
|
// for IP network connections.
|
|
|
|
type IPConn struct {
|
|
|
|
conn
|
|
|
|
}
|
|
|
|
|
2017-02-13 14:38:10 -07:00
|
|
|
// SyscallConn returns a raw network connection.
|
|
|
|
// This implements the syscall.Conn interface.
|
|
|
|
func (c *IPConn) SyscallConn() (syscall.RawConn, error) {
|
|
|
|
if !c.ok() {
|
|
|
|
return nil, syscall.EINVAL
|
|
|
|
}
|
|
|
|
return newRawConn(c.fd)
|
|
|
|
}
|
|
|
|
|
2017-06-05 22:33:01 -06:00
|
|
|
// ReadFromIP acts like ReadFrom but returns an IPAddr.
|
2016-03-02 14:22:11 -07:00
|
|
|
func (c *IPConn) ReadFromIP(b []byte) (int, *IPAddr, error) {
|
|
|
|
if !c.ok() {
|
|
|
|
return 0, nil, syscall.EINVAL
|
|
|
|
}
|
|
|
|
n, addr, err := c.readFrom(b)
|
|
|
|
if err != nil {
|
|
|
|
err = &OpError{Op: "read", Net: c.fd.net, Source: c.fd.laddr, Addr: c.fd.raddr, Err: err}
|
|
|
|
}
|
|
|
|
return n, addr, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadFrom implements the PacketConn ReadFrom method.
|
|
|
|
func (c *IPConn) ReadFrom(b []byte) (int, Addr, error) {
|
|
|
|
if !c.ok() {
|
|
|
|
return 0, nil, syscall.EINVAL
|
|
|
|
}
|
|
|
|
n, addr, err := c.readFrom(b)
|
|
|
|
if err != nil {
|
|
|
|
err = &OpError{Op: "read", Net: c.fd.net, Source: c.fd.laddr, Addr: c.fd.raddr, Err: err}
|
|
|
|
}
|
|
|
|
if addr == nil {
|
|
|
|
return n, nil, err
|
|
|
|
}
|
|
|
|
return n, addr, err
|
|
|
|
}
|
|
|
|
|
2017-06-05 22:33:01 -06:00
|
|
|
// ReadMsgIP reads a message from c, copying the payload into b and
|
|
|
|
// the associated out-of-band data into oob. It returns the number of
|
2016-03-02 14:22:11 -07:00
|
|
|
// bytes copied into b, the number of bytes copied into oob, the flags
|
2017-06-05 22:33:01 -06:00
|
|
|
// that were set on the message and the source address of the message.
|
|
|
|
//
|
|
|
|
// The packages golang.org/x/net/ipv4 and golang.org/x/net/ipv6 can be
|
|
|
|
// used to manipulate IP-level socket options in oob.
|
2016-03-02 14:22:11 -07:00
|
|
|
func (c *IPConn) ReadMsgIP(b, oob []byte) (n, oobn, flags int, addr *IPAddr, err error) {
|
|
|
|
if !c.ok() {
|
|
|
|
return 0, 0, 0, nil, syscall.EINVAL
|
|
|
|
}
|
|
|
|
n, oobn, flags, addr, err = c.readMsg(b, oob)
|
|
|
|
if err != nil {
|
|
|
|
err = &OpError{Op: "read", Net: c.fd.net, Source: c.fd.laddr, Addr: c.fd.raddr, Err: err}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-06-05 22:33:01 -06:00
|
|
|
// WriteToIP acts like WriteTo but takes an IPAddr.
|
2016-03-02 14:22:11 -07:00
|
|
|
func (c *IPConn) WriteToIP(b []byte, addr *IPAddr) (int, error) {
|
|
|
|
if !c.ok() {
|
|
|
|
return 0, syscall.EINVAL
|
|
|
|
}
|
|
|
|
n, err := c.writeTo(b, addr)
|
|
|
|
if err != nil {
|
|
|
|
err = &OpError{Op: "write", Net: c.fd.net, Source: c.fd.laddr, Addr: addr.opAddr(), Err: err}
|
|
|
|
}
|
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteTo implements the PacketConn WriteTo method.
|
|
|
|
func (c *IPConn) WriteTo(b []byte, addr Addr) (int, error) {
|
|
|
|
if !c.ok() {
|
|
|
|
return 0, syscall.EINVAL
|
|
|
|
}
|
|
|
|
a, ok := addr.(*IPAddr)
|
|
|
|
if !ok {
|
|
|
|
return 0, &OpError{Op: "write", Net: c.fd.net, Source: c.fd.laddr, Addr: addr, Err: syscall.EINVAL}
|
|
|
|
}
|
|
|
|
n, err := c.writeTo(b, a)
|
|
|
|
if err != nil {
|
|
|
|
err = &OpError{Op: "write", Net: c.fd.net, Source: c.fd.laddr, Addr: a.opAddr(), Err: err}
|
|
|
|
}
|
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
|
2017-06-05 22:33:01 -06:00
|
|
|
// WriteMsgIP writes a message to addr via c, copying the payload from
|
2016-03-02 14:22:11 -07:00
|
|
|
// b and the associated out-of-band data from oob. It returns the
|
|
|
|
// number of payload and out-of-band bytes written.
|
2017-06-05 22:33:01 -06:00
|
|
|
//
|
|
|
|
// The packages golang.org/x/net/ipv4 and golang.org/x/net/ipv6 can be
|
|
|
|
// used to manipulate IP-level socket options in oob.
|
2016-03-02 14:22:11 -07:00
|
|
|
func (c *IPConn) WriteMsgIP(b, oob []byte, addr *IPAddr) (n, oobn int, err error) {
|
|
|
|
if !c.ok() {
|
|
|
|
return 0, 0, syscall.EINVAL
|
|
|
|
}
|
|
|
|
n, oobn, err = c.writeMsg(b, oob, addr)
|
|
|
|
if err != nil {
|
|
|
|
err = &OpError{Op: "write", Net: c.fd.net, Source: c.fd.laddr, Addr: addr.opAddr(), Err: err}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func newIPConn(fd *netFD) *IPConn { return &IPConn{conn{fd}} }
|
|
|
|
|
2017-01-08 01:18:20 -07:00
|
|
|
// DialIP acts like Dial for IP networks.
|
|
|
|
//
|
|
|
|
// The network must be an IP network name; see func Dial for details.
|
|
|
|
//
|
|
|
|
// If laddr is nil, a local address is automatically chosen.
|
|
|
|
// If the IP field of raddr is nil or an unspecified IP address, the
|
|
|
|
// local system is assumed.
|
|
|
|
func DialIP(network string, laddr, raddr *IPAddr) (*IPConn, error) {
|
|
|
|
c, err := dialIP(context.Background(), network, laddr, raddr)
|
2016-03-02 14:22:11 -07:00
|
|
|
if err != nil {
|
2017-01-08 01:18:20 -07:00
|
|
|
return nil, &OpError{Op: "dial", Net: network, Source: laddr.opAddr(), Addr: raddr.opAddr(), Err: err}
|
2016-03-02 14:22:11 -07:00
|
|
|
}
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
net: update documentation on Listen{TCP,UDP,MulticastUDP,IP,Unix,Unixgram}
This change clarifies the documentation on
Listen{TCP,UDP,MulticastUDP,IP,Unix,Unixgram} to avoid unnecessary
confusion about how the arguments for the connection setup functions
are used to make connections.
Change-Id: Ie269453ef49ec2db893391dc3ed2f7b641c14249
Reviewed-on: https://go-review.googlesource.com/34878
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2017-01-08 01:22:34 -07:00
|
|
|
// ListenIP acts like ListenPacket for IP networks.
|
|
|
|
//
|
|
|
|
// The network must be an IP network name; see func Dial for details.
|
|
|
|
//
|
|
|
|
// If the IP field of laddr is nil or an unspecified IP address,
|
|
|
|
// ListenIP listens on all available IP addresses of the local system
|
|
|
|
// except multicast IP addresses.
|
|
|
|
func ListenIP(network string, laddr *IPAddr) (*IPConn, error) {
|
|
|
|
c, err := listenIP(context.Background(), network, laddr)
|
2016-03-02 14:22:11 -07:00
|
|
|
if err != nil {
|
net: update documentation on Listen{TCP,UDP,MulticastUDP,IP,Unix,Unixgram}
This change clarifies the documentation on
Listen{TCP,UDP,MulticastUDP,IP,Unix,Unixgram} to avoid unnecessary
confusion about how the arguments for the connection setup functions
are used to make connections.
Change-Id: Ie269453ef49ec2db893391dc3ed2f7b641c14249
Reviewed-on: https://go-review.googlesource.com/34878
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2017-01-08 01:22:34 -07:00
|
|
|
return nil, &OpError{Op: "listen", Net: network, Source: nil, Addr: laddr.opAddr(), Err: err}
|
2016-03-02 14:22:11 -07:00
|
|
|
}
|
|
|
|
return c, nil
|
|
|
|
}
|