1 // Copyright 2010 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
12 // BUG(mikio): On every POSIX platform, reads from the "ip4" network
13 // using the ReadFrom or ReadFromIP method might not return a complete
14 // IPv4 packet, including its header, even if there is space
15 // available. This can occur even in cases where Read or ReadMsgIP
16 // could return a complete packet. For this reason, it is recommended
17 // that you do not use these methods if it is important to receive a
20 // The Go 1 compatibility guidelines make it impossible for us to
21 // change the behavior of these methods; use Read or ReadMsgIP
24 // BUG(mikio): On JS, NaCl and Plan 9, methods and functions related
25 // to IPConn are not implemented.
27 // BUG(mikio): On Windows, the File method of IPConn is not
30 // IPAddr represents the address of an IP end point.
33 Zone string // IPv6 scoped addressing zone
36 // Network returns the address's network name, "ip".
37 func (a *IPAddr) Network() string { return "ip" }
39 func (a *IPAddr) String() string {
43 ip := ipEmptyString(a.IP)
45 return ip + "%" + a.Zone
50 func (a *IPAddr) isWildcard() bool {
51 if a == nil || a.IP == nil {
54 return a.IP.IsUnspecified()
57 func (a *IPAddr) opAddr() Addr {
64 // ResolveIPAddr returns an address of IP end point.
66 // The network must be an IP network name.
68 // If the host in the address parameter is not a literal IP address,
69 // ResolveIPAddr resolves the address to an address of IP end point.
70 // Otherwise, it parses the address as a literal IP address.
71 // The address parameter can use a host name, but this is not
72 // recommended, because it will return at most one of the host name's
75 // See func Dial for a description of the network and address
77 func ResolveIPAddr(network, address string) (*IPAddr, error) {
78 if network == "" { // a hint wildcard for Go 1.0 undocumented behavior
81 afnet, _, err := parseNetwork(context.Background(), network, false)
86 case "ip", "ip4", "ip6":
88 return nil, UnknownNetworkError(network)
90 addrs, err := DefaultResolver.internetAddrList(context.Background(), afnet, address)
94 return addrs.forResolve(network, address).(*IPAddr), nil
97 // IPConn is the implementation of the Conn and PacketConn interfaces
98 // for IP network connections.
103 // SyscallConn returns a raw network connection.
104 // This implements the syscall.Conn interface.
105 func (c *IPConn) SyscallConn() (syscall.RawConn, error) {
107 return nil, syscall.EINVAL
109 return newRawConn(c.fd)
112 // ReadFromIP acts like ReadFrom but returns an IPAddr.
113 func (c *IPConn) ReadFromIP(b []byte) (int, *IPAddr, error) {
115 return 0, nil, syscall.EINVAL
117 n, addr, err := c.readFrom(b)
119 err = &OpError{Op: "read", Net: c.fd.net, Source: c.fd.laddr, Addr: c.fd.raddr, Err: err}
124 // ReadFrom implements the PacketConn ReadFrom method.
125 func (c *IPConn) ReadFrom(b []byte) (int, Addr, error) {
127 return 0, nil, syscall.EINVAL
129 n, addr, err := c.readFrom(b)
131 err = &OpError{Op: "read", Net: c.fd.net, Source: c.fd.laddr, Addr: c.fd.raddr, Err: err}
139 // ReadMsgIP reads a message from c, copying the payload into b and
140 // the associated out-of-band data into oob. It returns the number of
141 // bytes copied into b, the number of bytes copied into oob, the flags
142 // that were set on the message and the source address of the message.
144 // The packages golang.org/x/net/ipv4 and golang.org/x/net/ipv6 can be
145 // used to manipulate IP-level socket options in oob.
146 func (c *IPConn) ReadMsgIP(b, oob []byte) (n, oobn, flags int, addr *IPAddr, err error) {
148 return 0, 0, 0, nil, syscall.EINVAL
150 n, oobn, flags, addr, err = c.readMsg(b, oob)
152 err = &OpError{Op: "read", Net: c.fd.net, Source: c.fd.laddr, Addr: c.fd.raddr, Err: err}
157 // WriteToIP acts like WriteTo but takes an IPAddr.
158 func (c *IPConn) WriteToIP(b []byte, addr *IPAddr) (int, error) {
160 return 0, syscall.EINVAL
162 n, err := c.writeTo(b, addr)
164 err = &OpError{Op: "write", Net: c.fd.net, Source: c.fd.laddr, Addr: addr.opAddr(), Err: err}
169 // WriteTo implements the PacketConn WriteTo method.
170 func (c *IPConn) WriteTo(b []byte, addr Addr) (int, error) {
172 return 0, syscall.EINVAL
174 a, ok := addr.(*IPAddr)
176 return 0, &OpError{Op: "write", Net: c.fd.net, Source: c.fd.laddr, Addr: addr, Err: syscall.EINVAL}
178 n, err := c.writeTo(b, a)
180 err = &OpError{Op: "write", Net: c.fd.net, Source: c.fd.laddr, Addr: a.opAddr(), Err: err}
185 // WriteMsgIP writes a message to addr via c, copying the payload from
186 // b and the associated out-of-band data from oob. It returns the
187 // number of payload and out-of-band bytes written.
189 // The packages golang.org/x/net/ipv4 and golang.org/x/net/ipv6 can be
190 // used to manipulate IP-level socket options in oob.
191 func (c *IPConn) WriteMsgIP(b, oob []byte, addr *IPAddr) (n, oobn int, err error) {
193 return 0, 0, syscall.EINVAL
195 n, oobn, err = c.writeMsg(b, oob, addr)
197 err = &OpError{Op: "write", Net: c.fd.net, Source: c.fd.laddr, Addr: addr.opAddr(), Err: err}
202 func newIPConn(fd *netFD) *IPConn { return &IPConn{conn{fd}} }
204 // DialIP acts like Dial for IP networks.
206 // The network must be an IP network name; see func Dial for details.
208 // If laddr is nil, a local address is automatically chosen.
209 // If the IP field of raddr is nil or an unspecified IP address, the
210 // local system is assumed.
211 func DialIP(network string, laddr, raddr *IPAddr) (*IPConn, error) {
213 return nil, &OpError{Op: "dial", Net: network, Source: laddr.opAddr(), Addr: nil, Err: errMissingAddress}
215 sd := &sysDialer{network: network, address: raddr.String()}
216 c, err := sd.dialIP(context.Background(), laddr, raddr)
218 return nil, &OpError{Op: "dial", Net: network, Source: laddr.opAddr(), Addr: raddr.opAddr(), Err: err}
223 // ListenIP acts like ListenPacket for IP networks.
225 // The network must be an IP network name; see func Dial for details.
227 // If the IP field of laddr is nil or an unspecified IP address,
228 // ListenIP listens on all available IP addresses of the local system
229 // except multicast IP addresses.
230 func ListenIP(network string, laddr *IPAddr) (*IPConn, error) {
234 sl := &sysListener{network: network, address: laddr.String()}
235 c, err := sl.listenIP(context.Background(), laddr)
237 return nil, &OpError{Op: "listen", Net: network, Source: nil, Addr: laddr.opAddr(), Err: err}