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 NaCl, Plan 9 and Windows, the ReadMsgIP and
25 // WriteMsgIP methods of 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 parses addr as an IP address of the form "host" or
65 // "ipv6-host%zone" and resolves the domain name on the network net,
66 // which must be "ip", "ip4" or "ip6".
68 // Resolving a hostname is not recommended because this returns at most
69 // one of its IP addresses.
70 func ResolveIPAddr(net
, addr
string) (*IPAddr
, error
) {
71 if net
== "" { // a hint wildcard for Go 1.0 undocumented behavior
74 afnet
, _
, err
:= parseNetwork(context
.Background(), net
)
79 case "ip", "ip4", "ip6":
81 return nil, UnknownNetworkError(net
)
83 addrs
, err
:= DefaultResolver
.internetAddrList(context
.Background(), afnet
, addr
)
87 return addrs
.first(isIPv4
).(*IPAddr
), nil
90 // IPConn is the implementation of the Conn and PacketConn interfaces
91 // for IP network connections.
96 // ReadFromIP reads an IP packet from c, copying the payload into b.
97 // It returns the number of bytes copied into b and the return address
98 // that was on the packet.
100 // ReadFromIP can be made to time out and return an error with
101 // Timeout() == true after a fixed time limit; see SetDeadline and
103 func (c
*IPConn
) ReadFromIP(b
[]byte) (int, *IPAddr
, error
) {
105 return 0, nil, syscall
.EINVAL
107 n
, addr
, err
:= c
.readFrom(b
)
109 err
= &OpError
{Op
: "read", Net
: c
.fd
.net
, Source
: c
.fd
.laddr
, Addr
: c
.fd
.raddr
, Err
: err
}
114 // ReadFrom implements the PacketConn ReadFrom method.
115 func (c
*IPConn
) ReadFrom(b
[]byte) (int, Addr
, error
) {
117 return 0, nil, syscall
.EINVAL
119 n
, addr
, err
:= c
.readFrom(b
)
121 err
= &OpError
{Op
: "read", Net
: c
.fd
.net
, Source
: c
.fd
.laddr
, Addr
: c
.fd
.raddr
, Err
: err
}
129 // ReadMsgIP reads a packet from c, copying the payload into b and the
130 // associated out-of-band data into oob. It returns the number of
131 // bytes copied into b, the number of bytes copied into oob, the flags
132 // that were set on the packet and the source address of the packet.
133 func (c
*IPConn
) ReadMsgIP(b
, oob
[]byte) (n
, oobn
, flags
int, addr
*IPAddr
, err error
) {
135 return 0, 0, 0, nil, syscall
.EINVAL
137 n
, oobn
, flags
, addr
, err
= c
.readMsg(b
, oob
)
139 err
= &OpError
{Op
: "read", Net
: c
.fd
.net
, Source
: c
.fd
.laddr
, Addr
: c
.fd
.raddr
, Err
: err
}
144 // WriteToIP writes an IP packet to addr via c, copying the payload
147 // WriteToIP can be made to time out and return an error with
148 // Timeout() == true after a fixed time limit; see SetDeadline and
149 // SetWriteDeadline. On packet-oriented connections, write timeouts
151 func (c
*IPConn
) WriteToIP(b
[]byte, addr
*IPAddr
) (int, error
) {
153 return 0, syscall
.EINVAL
155 n
, err
:= c
.writeTo(b
, addr
)
157 err
= &OpError
{Op
: "write", Net
: c
.fd
.net
, Source
: c
.fd
.laddr
, Addr
: addr
.opAddr(), Err
: err
}
162 // WriteTo implements the PacketConn WriteTo method.
163 func (c
*IPConn
) WriteTo(b
[]byte, addr Addr
) (int, error
) {
165 return 0, syscall
.EINVAL
167 a
, ok
:= addr
.(*IPAddr
)
169 return 0, &OpError
{Op
: "write", Net
: c
.fd
.net
, Source
: c
.fd
.laddr
, Addr
: addr
, Err
: syscall
.EINVAL
}
171 n
, err
:= c
.writeTo(b
, a
)
173 err
= &OpError
{Op
: "write", Net
: c
.fd
.net
, Source
: c
.fd
.laddr
, Addr
: a
.opAddr(), Err
: err
}
178 // WriteMsgIP writes a packet to addr via c, copying the payload from
179 // b and the associated out-of-band data from oob. It returns the
180 // number of payload and out-of-band bytes written.
181 func (c
*IPConn
) WriteMsgIP(b
, oob
[]byte, addr
*IPAddr
) (n
, oobn
int, err error
) {
183 return 0, 0, syscall
.EINVAL
185 n
, oobn
, err
= c
.writeMsg(b
, oob
, addr
)
187 err
= &OpError
{Op
: "write", Net
: c
.fd
.net
, Source
: c
.fd
.laddr
, Addr
: addr
.opAddr(), Err
: err
}
192 func newIPConn(fd
*netFD
) *IPConn
{ return &IPConn
{conn
{fd
}} }
194 // DialIP connects to the remote address raddr on the network protocol
195 // netProto, which must be "ip", "ip4", or "ip6" followed by a colon
196 // and a protocol number or name.
197 func DialIP(netProto
string, laddr
, raddr
*IPAddr
) (*IPConn
, error
) {
198 c
, err
:= dialIP(context
.Background(), netProto
, laddr
, raddr
)
200 return nil, &OpError
{Op
: "dial", Net
: netProto
, Source
: laddr
.opAddr(), Addr
: raddr
.opAddr(), Err
: err
}
205 // ListenIP listens for incoming IP packets addressed to the local
206 // address laddr. The returned connection's ReadFrom and WriteTo
207 // methods can be used to receive and send IP packets with per-packet
209 func ListenIP(netProto
string, laddr
*IPAddr
) (*IPConn
, error
) {
210 c
, err
:= listenIP(context
.Background(), netProto
, laddr
)
212 return nil, &OpError
{Op
: "listen", Net
: netProto
, Source
: nil, Addr
: laddr
.opAddr(), Err
: err
}