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.
5 // +build darwin freebsd linux netbsd openbsd windows
17 func sockaddrToIP(sa syscall.Sockaddr) Addr {
18 switch sa := sa.(type) {
19 case *syscall.SockaddrInet4:
20 return &IPAddr{sa.Addr[0:]}
21 case *syscall.SockaddrInet6:
22 return &IPAddr{sa.Addr[0:]}
27 func (a *IPAddr) family() int {
28 if a == nil || len(a.IP) <= IPv4len {
29 return syscall.AF_INET
31 if a.IP.To4() != nil {
32 return syscall.AF_INET
34 return syscall.AF_INET6
37 func (a *IPAddr) isWildcard() bool {
38 if a == nil || a.IP == nil {
41 return a.IP.IsUnspecified()
44 func (a *IPAddr) sockaddr(family int) (syscall.Sockaddr, error) {
45 return ipToSockaddr(family, a.IP, 0)
48 func (a *IPAddr) toAddr() sockaddr {
49 if a == nil { // nil *IPAddr
50 return nil // nil interface
55 // IPConn is the implementation of the Conn and PacketConn
56 // interfaces for IP network connections.
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.
150 // It returns the number of bytes copied into b and the return address
151 // that was on the packet.
153 // ReadFromIP can be made to time out and return an error with
154 // Timeout() == true after a fixed time limit; see SetDeadline and
156 func (c *IPConn) ReadFromIP(b []byte) (int, *IPAddr, error) {
158 return 0, nil, syscall.EINVAL
160 // TODO(cw,rsc): consider using readv if we know the family
161 // type to avoid the header trim/copy
163 n, sa, err := c.fd.ReadFrom(b)
164 switch sa := sa.(type) {
165 case *syscall.SockaddrInet4:
166 addr = &IPAddr{sa.Addr[0:]}
167 if len(b) >= IPv4len { // discard ipv4 header
168 hsize := (int(b[0]) & 0xf) * 4
172 case *syscall.SockaddrInet6:
173 addr = &IPAddr{sa.Addr[0:]}
178 // ReadFrom implements the PacketConn ReadFrom method.
179 func (c *IPConn) ReadFrom(b []byte) (int, Addr, error) {
181 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.
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.
193 func (c *IPConn) WriteToIP(b []byte, addr *IPAddr) (int, error) {
195 return 0, syscall.EINVAL
197 sa, err := addr.sockaddr(c.fd.family)
199 return 0, &OpError{"write", c.fd.net, addr, err}
201 return c.fd.WriteTo(b, sa)
204 // WriteTo implements the PacketConn WriteTo method.
205 func (c *IPConn) WriteTo(b []byte, addr Addr) (int, error) {
207 return 0, syscall.EINVAL
209 a, ok := addr.(*IPAddr)
211 return 0, &OpError{"write", c.fd.net, addr, syscall.EINVAL}
213 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.
218 func DialIP(netProto string, laddr, raddr *IPAddr) (*IPConn, error) {
219 net, proto, err := parseDialNetwork(netProto)
224 case "ip", "ip4", "ip6":
226 return nil, UnknownNetworkError(net)
229 return nil, &OpError{"dial", netProto, nil, errMissingAddress}
231 fd, err := internetSocket(net, laddr.toAddr(), raddr.toAddr(), syscall.SOCK_RAW, proto, "dial", sockaddrToIP)
235 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.
242 func ListenIP(netProto string, laddr *IPAddr) (*IPConn, error) {
243 net, proto, err := parseDialNetwork(netProto)
248 case "ip", "ip4", "ip6":
250 return nil, UnknownNetworkError(net)
252 fd, err := internetSocket(net, laddr.toAddr(), nil, syscall.SOCK_RAW, proto, "listen", sockaddrToIP)
256 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() }