55
// IPConn is the implementation of the Conn and PacketConn
56
// interfaces for IP network connections.
52
// IPConn is the implementation of the Conn and PacketConn interfaces
53
// for IP network connections.
57
54
type IPConn struct {
61
func newIPConn(fd *netFD) *IPConn { return &IPConn{fd} }
63
func (c *IPConn) ok() bool { return c != nil && c.fd != nil }
65
// Implementation of the Conn interface - see Conn for documentation.
67
// Read implements the Conn Read method.
68
func (c *IPConn) Read(b []byte) (int, error) {
69
n, _, err := c.ReadFrom(b)
73
// Write implements the Conn Write method.
74
func (c *IPConn) Write(b []byte) (int, error) {
76
return 0, syscall.EINVAL
81
// Close closes the IP connection.
82
func (c *IPConn) Close() error {
89
// LocalAddr returns the local network address.
90
func (c *IPConn) LocalAddr() Addr {
97
// RemoteAddr returns the remote network address, a *IPAddr.
98
func (c *IPConn) RemoteAddr() Addr {
105
// SetDeadline implements the Conn SetDeadline method.
106
func (c *IPConn) SetDeadline(t time.Time) error {
108
return syscall.EINVAL
110
return setDeadline(c.fd, t)
113
// SetReadDeadline implements the Conn SetReadDeadline method.
114
func (c *IPConn) SetReadDeadline(t time.Time) error {
116
return syscall.EINVAL
118
return setReadDeadline(c.fd, t)
121
// SetWriteDeadline implements the Conn SetWriteDeadline method.
122
func (c *IPConn) SetWriteDeadline(t time.Time) error {
124
return syscall.EINVAL
126
return setWriteDeadline(c.fd, t)
129
// SetReadBuffer sets the size of the operating system's
130
// receive buffer associated with the connection.
131
func (c *IPConn) SetReadBuffer(bytes int) error {
133
return syscall.EINVAL
135
return setReadBuffer(c.fd, bytes)
138
// SetWriteBuffer sets the size of the operating system's
139
// transmit buffer associated with the connection.
140
func (c *IPConn) SetWriteBuffer(bytes int) error {
142
return syscall.EINVAL
144
return setWriteBuffer(c.fd, bytes)
147
// IP-specific methods.
149
// ReadFromIP reads a IP packet from c, copying the payload into b.
58
func newIPConn(fd *netFD) *IPConn { return &IPConn{conn{fd}} }
60
// ReadFromIP reads an IP packet from c, copying the payload into b.
150
61
// It returns the number of bytes copied into b and the return address
151
62
// that was on the packet.
181
92
return 0, nil, syscall.EINVAL
183
n, uaddr, err := c.ReadFromIP(b)
184
return n, uaddr.toAddr(), err
187
// WriteToIP writes a IP packet to addr via c, copying the payload from b.
94
n, addr, err := c.ReadFromIP(b)
95
return n, addr.toAddr(), err
98
// ReadMsgIP reads a packet from c, copying the payload into b and the
99
// associated out-of-band data into oob. It returns the number of
100
// bytes copied into b, the number of bytes copied into oob, the flags
101
// that were set on the packet and the source address of the packet.
102
func (c *IPConn) ReadMsgIP(b, oob []byte) (n, oobn, flags int, addr *IPAddr, err error) {
104
return 0, 0, 0, nil, syscall.EINVAL
106
var sa syscall.Sockaddr
107
n, oobn, flags, sa, err = c.fd.ReadMsg(b, oob)
108
switch sa := sa.(type) {
109
case *syscall.SockaddrInet4:
110
addr = &IPAddr{IP: sa.Addr[0:]}
111
case *syscall.SockaddrInet6:
112
addr = &IPAddr{IP: sa.Addr[0:], Zone: zoneToString(int(sa.ZoneId))}
117
// WriteToIP writes an IP packet to addr via c, copying the payload
189
// WriteToIP can be made to time out and return
190
// an error with Timeout() == true after a fixed time limit;
191
// see SetDeadline and SetWriteDeadline.
192
// On packet-oriented connections, write timeouts are rare.
120
// WriteToIP can be made to time out and return an error with
121
// Timeout() == true after a fixed time limit; see SetDeadline and
122
// SetWriteDeadline. On packet-oriented connections, write timeouts
193
124
func (c *IPConn) WriteToIP(b []byte, addr *IPAddr) (int, error) {
195
126
return 0, syscall.EINVAL
213
144
return c.WriteToIP(b, a)
216
// DialIP connects to the remote address raddr on the network protocol netProto,
217
// which must be "ip", "ip4", or "ip6" followed by a colon and a protocol number or name.
147
// WriteMsgIP writes a packet to addr via c, copying the payload from
148
// b and the associated out-of-band data from oob. It returns the
149
// number of payload and out-of-band bytes written.
150
func (c *IPConn) WriteMsgIP(b, oob []byte, addr *IPAddr) (n, oobn int, err error) {
152
return 0, 0, syscall.EINVAL
154
sa, err := addr.sockaddr(c.fd.family)
156
return 0, 0, &OpError{"write", c.fd.net, addr, err}
158
return c.fd.WriteMsg(b, oob, sa)
161
// DialIP connects to the remote address raddr on the network protocol
162
// netProto, which must be "ip", "ip4", or "ip6" followed by a colon
163
// and a protocol number or name.
218
164
func DialIP(netProto string, laddr, raddr *IPAddr) (*IPConn, error) {
219
net, proto, err := parseDialNetwork(netProto)
165
return dialIP(netProto, laddr, raddr, noDeadline)
168
func dialIP(netProto string, laddr, raddr *IPAddr, deadline time.Time) (*IPConn, error) {
169
net, proto, err := parseNetwork(netProto)
224
174
case "ip", "ip4", "ip6":
226
return nil, UnknownNetworkError(net)
176
return nil, UnknownNetworkError(netProto)
228
178
if raddr == nil {
229
179
return nil, &OpError{"dial", netProto, nil, errMissingAddress}
231
fd, err := internetSocket(net, laddr.toAddr(), raddr.toAddr(), syscall.SOCK_RAW, proto, "dial", sockaddrToIP)
181
fd, err := internetSocket(net, laddr.toAddr(), raddr.toAddr(), deadline, syscall.SOCK_RAW, proto, "dial", sockaddrToIP)
235
185
return newIPConn(fd), nil
238
// ListenIP listens for incoming IP packets addressed to the
239
// local address laddr. The returned connection c's ReadFrom
240
// and WriteTo methods can be used to receive and send IP
241
// packets with per-packet addressing.
188
// ListenIP listens for incoming IP packets addressed to the local
189
// address laddr. The returned connection's ReadFrom and WriteTo
190
// methods can be used to receive and send IP packets with per-packet
242
192
func ListenIP(netProto string, laddr *IPAddr) (*IPConn, error) {
243
net, proto, err := parseDialNetwork(netProto)
193
net, proto, err := parseNetwork(netProto)
248
198
case "ip", "ip4", "ip6":
250
return nil, UnknownNetworkError(net)
200
return nil, UnknownNetworkError(netProto)
252
fd, err := internetSocket(net, laddr.toAddr(), nil, syscall.SOCK_RAW, proto, "listen", sockaddrToIP)
202
fd, err := internetSocket(net, laddr.toAddr(), nil, noDeadline, syscall.SOCK_RAW, proto, "listen", sockaddrToIP)
256
206
return newIPConn(fd), nil
259
// File returns a copy of the underlying os.File, set to blocking mode.
260
// It is the caller's responsibility to close f when finished.
261
// Closing c does not affect f, and closing f does not affect c.
262
func (c *IPConn) File() (f *os.File, err error) { return c.fd.dup() }