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 {
91 // LocalAddr returns the local network address.
92 func (c *IPConn) LocalAddr() Addr {
99 // RemoteAddr returns the remote network address, a *IPAddr.
100 func (c *IPConn) RemoteAddr() Addr {
107 // SetDeadline implements the Conn SetDeadline method.
108 func (c *IPConn) SetDeadline(t time.Time) error {
110 return syscall.EINVAL
112 return setDeadline(c.fd, t)
115 // SetReadDeadline implements the Conn SetReadDeadline method.
116 func (c *IPConn) SetReadDeadline(t time.Time) error {
118 return syscall.EINVAL
120 return setReadDeadline(c.fd, t)
123 // SetWriteDeadline implements the Conn SetWriteDeadline method.
124 func (c *IPConn) SetWriteDeadline(t time.Time) error {
126 return syscall.EINVAL
128 return setWriteDeadline(c.fd, t)
131 // SetReadBuffer sets the size of the operating system's
132 // receive buffer associated with the connection.
133 func (c *IPConn) SetReadBuffer(bytes int) error {
135 return syscall.EINVAL
137 return setReadBuffer(c.fd, bytes)
140 // SetWriteBuffer sets the size of the operating system's
141 // transmit buffer associated with the connection.
142 func (c *IPConn) SetWriteBuffer(bytes int) error {
144 return syscall.EINVAL
146 return setWriteBuffer(c.fd, bytes)
149 // IP-specific methods.
151 // ReadFromIP reads a IP packet from c, copying the payload into b.
152 // It returns the number of bytes copied into b and the return address
153 // that was on the packet.
155 // ReadFromIP can be made to time out and return an error with
156 // Timeout() == true after a fixed time limit; see SetDeadline and
158 func (c *IPConn) ReadFromIP(b []byte) (int, *IPAddr, error) {
160 return 0, nil, syscall.EINVAL
162 // TODO(cw,rsc): consider using readv if we know the family
163 // type to avoid the header trim/copy
165 n, sa, err := c.fd.ReadFrom(b)
166 switch sa := sa.(type) {
167 case *syscall.SockaddrInet4:
168 addr = &IPAddr{sa.Addr[0:]}
169 if len(b) >= IPv4len { // discard ipv4 header
170 hsize := (int(b[0]) & 0xf) * 4
174 case *syscall.SockaddrInet6:
175 addr = &IPAddr{sa.Addr[0:]}
180 // ReadFrom implements the PacketConn ReadFrom method.
181 func (c *IPConn) ReadFrom(b []byte) (int, Addr, error) {
183 return 0, nil, syscall.EINVAL
185 n, uaddr, err := c.ReadFromIP(b)
186 return n, uaddr.toAddr(), err
189 // WriteToIP writes a IP packet to addr via c, copying the payload from b.
191 // WriteToIP can be made to time out and return
192 // an error with Timeout() == true after a fixed time limit;
193 // see SetDeadline and SetWriteDeadline.
194 // On packet-oriented connections, write timeouts are rare.
195 func (c *IPConn) WriteToIP(b []byte, addr *IPAddr) (int, error) {
197 return 0, syscall.EINVAL
199 sa, err := addr.sockaddr(c.fd.family)
201 return 0, &OpError{"write", c.fd.net, addr, err}
203 return c.fd.WriteTo(b, sa)
206 // WriteTo implements the PacketConn WriteTo method.
207 func (c *IPConn) WriteTo(b []byte, addr Addr) (int, error) {
209 return 0, syscall.EINVAL
211 a, ok := addr.(*IPAddr)
213 return 0, &OpError{"write", c.fd.net, addr, syscall.EINVAL}
215 return c.WriteToIP(b, a)
218 // DialIP connects to the remote address raddr on the network protocol netProto,
219 // which must be "ip", "ip4", or "ip6" followed by a colon and a protocol number or name.
220 func DialIP(netProto string, laddr, raddr *IPAddr) (*IPConn, error) {
221 net, proto, err := parseDialNetwork(netProto)
226 case "ip", "ip4", "ip6":
228 return nil, UnknownNetworkError(net)
231 return nil, &OpError{"dial", netProto, nil, errMissingAddress}
233 fd, err := internetSocket(net, laddr.toAddr(), raddr.toAddr(), syscall.SOCK_RAW, proto, "dial", sockaddrToIP)
237 return newIPConn(fd), nil
240 // ListenIP listens for incoming IP packets addressed to the
241 // local address laddr. The returned connection c's ReadFrom
242 // and WriteTo methods can be used to receive and send IP
243 // packets with per-packet addressing.
244 func ListenIP(netProto string, laddr *IPAddr) (*IPConn, error) {
245 net, proto, err := parseDialNetwork(netProto)
250 case "ip", "ip4", "ip6":
252 return nil, UnknownNetworkError(net)
254 fd, err := internetSocket(net, laddr.toAddr(), nil, syscall.SOCK_RAW, proto, "listen", sockaddrToIP)
258 return newIPConn(fd), nil
261 // File returns a copy of the underlying os.File, set to blocking mode.
262 // It is the caller's responsibility to close f when finished.
263 // Closing c does not affect f, and closing f does not affect c.
264 func (c *IPConn) File() (f *os.File, err error) { return c.fd.dup() }