|
4 | 4 |
|
5 | 5 | package net
|
6 | 6 |
|
| 7 | +import "syscall" |
| 8 | + |
7 | 9 | // IPAddr represents the address of an IP end point.
|
8 | 10 | type IPAddr struct {
|
9 | 11 | IP IP
|
@@ -60,3 +62,130 @@ func ResolveIPAddr(net, addr string) (*IPAddr, error) {
|
60 | 62 | }
|
61 | 63 | return addrs.first(isIPv4).(*IPAddr), nil
|
62 | 64 | }
|
| 65 | + |
| 66 | +// IPConn is the implementation of the Conn and PacketConn interfaces |
| 67 | +// for IP network connections. |
| 68 | +type IPConn struct { |
| 69 | + conn |
| 70 | +} |
| 71 | + |
| 72 | +// ReadFromIP reads an IP packet from c, copying the payload into b. |
| 73 | +// It returns the number of bytes copied into b and the return address |
| 74 | +// that was on the packet. |
| 75 | +// |
| 76 | +// ReadFromIP can be made to time out and return an error with |
| 77 | +// Timeout() == true after a fixed time limit; see SetDeadline and |
| 78 | +// SetReadDeadline. |
| 79 | +func (c *IPConn) ReadFromIP(b []byte) (int, *IPAddr, error) { |
| 80 | + if !c.ok() { |
| 81 | + return 0, nil, syscall.EINVAL |
| 82 | + } |
| 83 | + n, addr, err := c.readFrom(b) |
| 84 | + if err != nil { |
| 85 | + err = &OpError{Op: "read", Net: c.fd.net, Source: c.fd.laddr, Addr: c.fd.raddr, Err: err} |
| 86 | + } |
| 87 | + return n, addr, err |
| 88 | +} |
| 89 | + |
| 90 | +// ReadFrom implements the PacketConn ReadFrom method. |
| 91 | +func (c *IPConn) ReadFrom(b []byte) (int, Addr, error) { |
| 92 | + if !c.ok() { |
| 93 | + return 0, nil, syscall.EINVAL |
| 94 | + } |
| 95 | + n, addr, err := c.readFrom(b) |
| 96 | + if err != nil { |
| 97 | + err = &OpError{Op: "read", Net: c.fd.net, Source: c.fd.laddr, Addr: c.fd.raddr, Err: err} |
| 98 | + } |
| 99 | + if addr == nil { |
| 100 | + return n, nil, err |
| 101 | + } |
| 102 | + return n, addr, err |
| 103 | +} |
| 104 | + |
| 105 | +// ReadMsgIP reads a packet from c, copying the payload into b and the |
| 106 | +// associated out-of-band data into oob. It returns the number of |
| 107 | +// bytes copied into b, the number of bytes copied into oob, the flags |
| 108 | +// that were set on the packet and the source address of the packet. |
| 109 | +func (c *IPConn) ReadMsgIP(b, oob []byte) (n, oobn, flags int, addr *IPAddr, err error) { |
| 110 | + if !c.ok() { |
| 111 | + return 0, 0, 0, nil, syscall.EINVAL |
| 112 | + } |
| 113 | + n, oobn, flags, addr, err = c.readMsg(b, oob) |
| 114 | + if err != nil { |
| 115 | + err = &OpError{Op: "read", Net: c.fd.net, Source: c.fd.laddr, Addr: c.fd.raddr, Err: err} |
| 116 | + } |
| 117 | + return |
| 118 | +} |
| 119 | + |
| 120 | +// WriteToIP writes an IP packet to addr via c, copying the payload |
| 121 | +// from b. |
| 122 | +// |
| 123 | +// WriteToIP can be made to time out and return an error with |
| 124 | +// Timeout() == true after a fixed time limit; see SetDeadline and |
| 125 | +// SetWriteDeadline. On packet-oriented connections, write timeouts |
| 126 | +// are rare. |
| 127 | +func (c *IPConn) WriteToIP(b []byte, addr *IPAddr) (int, error) { |
| 128 | + if !c.ok() { |
| 129 | + return 0, syscall.EINVAL |
| 130 | + } |
| 131 | + n, err := c.writeTo(b, addr) |
| 132 | + if err != nil { |
| 133 | + err = &OpError{Op: "write", Net: c.fd.net, Source: c.fd.laddr, Addr: addr.opAddr(), Err: err} |
| 134 | + } |
| 135 | + return n, err |
| 136 | +} |
| 137 | + |
| 138 | +// WriteTo implements the PacketConn WriteTo method. |
| 139 | +func (c *IPConn) WriteTo(b []byte, addr Addr) (int, error) { |
| 140 | + if !c.ok() { |
| 141 | + return 0, syscall.EINVAL |
| 142 | + } |
| 143 | + a, ok := addr.(*IPAddr) |
| 144 | + if !ok { |
| 145 | + return 0, &OpError{Op: "write", Net: c.fd.net, Source: c.fd.laddr, Addr: addr, Err: syscall.EINVAL} |
| 146 | + } |
| 147 | + n, err := c.writeTo(b, a) |
| 148 | + if err != nil { |
| 149 | + err = &OpError{Op: "write", Net: c.fd.net, Source: c.fd.laddr, Addr: a.opAddr(), Err: err} |
| 150 | + } |
| 151 | + return n, err |
| 152 | +} |
| 153 | + |
| 154 | +// WriteMsgIP writes a packet to addr via c, copying the payload from |
| 155 | +// b and the associated out-of-band data from oob. It returns the |
| 156 | +// number of payload and out-of-band bytes written. |
| 157 | +func (c *IPConn) WriteMsgIP(b, oob []byte, addr *IPAddr) (n, oobn int, err error) { |
| 158 | + if !c.ok() { |
| 159 | + return 0, 0, syscall.EINVAL |
| 160 | + } |
| 161 | + n, oobn, err = c.writeMsg(b, oob, addr) |
| 162 | + if err != nil { |
| 163 | + err = &OpError{Op: "write", Net: c.fd.net, Source: c.fd.laddr, Addr: addr.opAddr(), Err: err} |
| 164 | + } |
| 165 | + return |
| 166 | +} |
| 167 | + |
| 168 | +func newIPConn(fd *netFD) *IPConn { return &IPConn{conn{fd}} } |
| 169 | + |
| 170 | +// DialIP connects to the remote address raddr on the network protocol |
| 171 | +// netProto, which must be "ip", "ip4", or "ip6" followed by a colon |
| 172 | +// and a protocol number or name. |
| 173 | +func DialIP(netProto string, laddr, raddr *IPAddr) (*IPConn, error) { |
| 174 | + c, err := dialIP(netProto, laddr, raddr, noDeadline) |
| 175 | + if err != nil { |
| 176 | + return nil, &OpError{Op: "dial", Net: netProto, Source: laddr.opAddr(), Addr: raddr.opAddr(), Err: err} |
| 177 | + } |
| 178 | + return c, nil |
| 179 | +} |
| 180 | + |
| 181 | +// ListenIP listens for incoming IP packets addressed to the local |
| 182 | +// address laddr. The returned connection's ReadFrom and WriteTo |
| 183 | +// methods can be used to receive and send IP packets with per-packet |
| 184 | +// addressing. |
| 185 | +func ListenIP(netProto string, laddr *IPAddr) (*IPConn, error) { |
| 186 | + c, err := listenIP(netProto, laddr) |
| 187 | + if err != nil { |
| 188 | + return nil, &OpError{Op: "listen", Net: netProto, Source: nil, Addr: laddr.opAddr(), Err: err} |
| 189 | + } |
| 190 | + return c, nil |
| 191 | +} |
0 commit comments