2011-09-16 17:47:21 +02:00
|
|
|
// Copyright 2010 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2012-01-13 06:11:45 +01:00
|
|
|
// +build darwin freebsd linux netbsd openbsd windows
|
2011-10-27 01:57:58 +02:00
|
|
|
|
2011-09-16 17:47:21 +02:00
|
|
|
// (Raw) IP sockets
|
|
|
|
|
|
|
|
package net
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"syscall"
|
2012-01-25 22:54:22 +01:00
|
|
|
"time"
|
2011-09-16 17:47:21 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func sockaddrToIP(sa syscall.Sockaddr) Addr {
|
|
|
|
switch sa := sa.(type) {
|
|
|
|
case *syscall.SockaddrInet4:
|
|
|
|
return &IPAddr{sa.Addr[0:]}
|
|
|
|
case *syscall.SockaddrInet6:
|
|
|
|
return &IPAddr{sa.Addr[0:]}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *IPAddr) family() int {
|
2011-10-27 01:57:58 +02:00
|
|
|
if a == nil || len(a.IP) <= IPv4len {
|
2011-09-16 17:47:21 +02:00
|
|
|
return syscall.AF_INET
|
|
|
|
}
|
|
|
|
if a.IP.To4() != nil {
|
|
|
|
return syscall.AF_INET
|
|
|
|
}
|
|
|
|
return syscall.AF_INET6
|
|
|
|
}
|
|
|
|
|
2012-03-30 23:27:11 +02:00
|
|
|
func (a *IPAddr) isWildcard() bool {
|
|
|
|
if a == nil || a.IP == nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return a.IP.IsUnspecified()
|
|
|
|
}
|
|
|
|
|
2011-12-03 03:17:34 +01:00
|
|
|
func (a *IPAddr) sockaddr(family int) (syscall.Sockaddr, error) {
|
2011-09-16 17:47:21 +02:00
|
|
|
return ipToSockaddr(family, a.IP, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *IPAddr) toAddr() sockaddr {
|
|
|
|
if a == nil { // nil *IPAddr
|
|
|
|
return nil // nil interface
|
|
|
|
}
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
|
|
|
// IPConn is the implementation of the Conn and PacketConn
|
|
|
|
// interfaces for IP network connections.
|
|
|
|
type IPConn struct {
|
|
|
|
fd *netFD
|
|
|
|
}
|
|
|
|
|
|
|
|
func newIPConn(fd *netFD) *IPConn { return &IPConn{fd} }
|
|
|
|
|
|
|
|
func (c *IPConn) ok() bool { return c != nil && c.fd != nil }
|
|
|
|
|
|
|
|
// Implementation of the Conn interface - see Conn for documentation.
|
|
|
|
|
2012-02-01 20:26:59 +01:00
|
|
|
// Read implements the Conn Read method.
|
|
|
|
func (c *IPConn) Read(b []byte) (int, error) {
|
|
|
|
n, _, err := c.ReadFrom(b)
|
|
|
|
return n, err
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
|
|
|
|
2012-02-01 20:26:59 +01:00
|
|
|
// Write implements the Conn Write method.
|
|
|
|
func (c *IPConn) Write(b []byte) (int, error) {
|
2011-09-16 17:47:21 +02:00
|
|
|
if !c.ok() {
|
2012-03-02 21:01:37 +01:00
|
|
|
return 0, syscall.EINVAL
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
|
|
|
return c.fd.Write(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes the IP connection.
|
2011-12-03 03:17:34 +01:00
|
|
|
func (c *IPConn) Close() error {
|
2011-09-16 17:47:21 +02:00
|
|
|
if !c.ok() {
|
2012-03-02 21:01:37 +01:00
|
|
|
return syscall.EINVAL
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
2012-05-04 17:01:11 +02:00
|
|
|
return c.fd.Close()
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// LocalAddr returns the local network address.
|
|
|
|
func (c *IPConn) LocalAddr() Addr {
|
|
|
|
if !c.ok() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return c.fd.laddr
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoteAddr returns the remote network address, a *IPAddr.
|
|
|
|
func (c *IPConn) RemoteAddr() Addr {
|
|
|
|
if !c.ok() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return c.fd.raddr
|
|
|
|
}
|
|
|
|
|
2012-02-01 20:26:59 +01:00
|
|
|
// SetDeadline implements the Conn SetDeadline method.
|
2012-01-25 22:54:22 +01:00
|
|
|
func (c *IPConn) SetDeadline(t time.Time) error {
|
2011-09-16 17:47:21 +02:00
|
|
|
if !c.ok() {
|
2012-03-02 21:01:37 +01:00
|
|
|
return syscall.EINVAL
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
2012-01-25 22:54:22 +01:00
|
|
|
return setDeadline(c.fd, t)
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
|
|
|
|
2012-02-01 20:26:59 +01:00
|
|
|
// SetReadDeadline implements the Conn SetReadDeadline method.
|
2012-01-25 22:54:22 +01:00
|
|
|
func (c *IPConn) SetReadDeadline(t time.Time) error {
|
2011-09-16 17:47:21 +02:00
|
|
|
if !c.ok() {
|
2012-03-02 21:01:37 +01:00
|
|
|
return syscall.EINVAL
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
2012-01-25 22:54:22 +01:00
|
|
|
return setReadDeadline(c.fd, t)
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
|
|
|
|
2012-02-01 20:26:59 +01:00
|
|
|
// SetWriteDeadline implements the Conn SetWriteDeadline method.
|
2012-01-25 22:54:22 +01:00
|
|
|
func (c *IPConn) SetWriteDeadline(t time.Time) error {
|
2011-09-16 17:47:21 +02:00
|
|
|
if !c.ok() {
|
2012-03-02 21:01:37 +01:00
|
|
|
return syscall.EINVAL
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
2012-01-25 22:54:22 +01:00
|
|
|
return setWriteDeadline(c.fd, t)
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetReadBuffer sets the size of the operating system's
|
|
|
|
// receive buffer associated with the connection.
|
2011-12-03 03:17:34 +01:00
|
|
|
func (c *IPConn) SetReadBuffer(bytes int) error {
|
2011-09-16 17:47:21 +02:00
|
|
|
if !c.ok() {
|
2012-03-02 21:01:37 +01:00
|
|
|
return syscall.EINVAL
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
|
|
|
return setReadBuffer(c.fd, bytes)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetWriteBuffer sets the size of the operating system's
|
|
|
|
// transmit buffer associated with the connection.
|
2011-12-03 03:17:34 +01:00
|
|
|
func (c *IPConn) SetWriteBuffer(bytes int) error {
|
2011-09-16 17:47:21 +02:00
|
|
|
if !c.ok() {
|
2012-03-02 21:01:37 +01:00
|
|
|
return syscall.EINVAL
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
|
|
|
return setWriteBuffer(c.fd, bytes)
|
|
|
|
}
|
|
|
|
|
|
|
|
// IP-specific methods.
|
|
|
|
|
2012-10-03 07:27:36 +02:00
|
|
|
// ReadFromIP reads an IP packet from c, copying the payload into b.
|
2011-09-16 17:47:21 +02:00
|
|
|
// It returns the number of bytes copied into b and the return address
|
|
|
|
// that was on the packet.
|
|
|
|
//
|
|
|
|
// ReadFromIP can be made to time out and return an error with
|
2012-01-25 22:54:22 +01:00
|
|
|
// Timeout() == true after a fixed time limit; see SetDeadline and
|
|
|
|
// SetReadDeadline.
|
2012-02-01 20:26:59 +01:00
|
|
|
func (c *IPConn) ReadFromIP(b []byte) (int, *IPAddr, error) {
|
2011-09-16 17:47:21 +02:00
|
|
|
if !c.ok() {
|
2012-03-02 21:01:37 +01:00
|
|
|
return 0, nil, syscall.EINVAL
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
|
|
|
// TODO(cw,rsc): consider using readv if we know the family
|
|
|
|
// type to avoid the header trim/copy
|
2012-02-01 20:26:59 +01:00
|
|
|
var addr *IPAddr
|
2011-09-16 17:47:21 +02:00
|
|
|
n, sa, err := c.fd.ReadFrom(b)
|
|
|
|
switch sa := sa.(type) {
|
|
|
|
case *syscall.SockaddrInet4:
|
|
|
|
addr = &IPAddr{sa.Addr[0:]}
|
2011-10-27 01:57:58 +02:00
|
|
|
if len(b) >= IPv4len { // discard ipv4 header
|
2011-09-16 17:47:21 +02:00
|
|
|
hsize := (int(b[0]) & 0xf) * 4
|
|
|
|
copy(b, b[hsize:])
|
|
|
|
n -= hsize
|
|
|
|
}
|
|
|
|
case *syscall.SockaddrInet6:
|
|
|
|
addr = &IPAddr{sa.Addr[0:]}
|
|
|
|
}
|
2012-02-01 20:26:59 +01:00
|
|
|
return n, addr, err
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
|
|
|
|
2012-02-01 20:26:59 +01:00
|
|
|
// ReadFrom implements the PacketConn ReadFrom method.
|
|
|
|
func (c *IPConn) ReadFrom(b []byte) (int, Addr, error) {
|
2011-09-16 17:47:21 +02:00
|
|
|
if !c.ok() {
|
2012-03-02 21:01:37 +01:00
|
|
|
return 0, nil, syscall.EINVAL
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
|
|
|
n, uaddr, err := c.ReadFromIP(b)
|
|
|
|
return n, uaddr.toAddr(), err
|
|
|
|
}
|
|
|
|
|
2012-10-03 07:27:36 +02:00
|
|
|
// WriteToIP writes an IP packet to addr via c, copying the payload from b.
|
2011-09-16 17:47:21 +02:00
|
|
|
//
|
|
|
|
// WriteToIP can be made to time out and return
|
|
|
|
// an error with Timeout() == true after a fixed time limit;
|
2012-01-25 22:54:22 +01:00
|
|
|
// see SetDeadline and SetWriteDeadline.
|
2011-09-16 17:47:21 +02:00
|
|
|
// On packet-oriented connections, write timeouts are rare.
|
2012-02-01 20:26:59 +01:00
|
|
|
func (c *IPConn) WriteToIP(b []byte, addr *IPAddr) (int, error) {
|
2011-09-16 17:47:21 +02:00
|
|
|
if !c.ok() {
|
2012-03-02 21:01:37 +01:00
|
|
|
return 0, syscall.EINVAL
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
2012-02-01 20:26:59 +01:00
|
|
|
sa, err := addr.sockaddr(c.fd.family)
|
|
|
|
if err != nil {
|
|
|
|
return 0, &OpError{"write", c.fd.net, addr, err}
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
|
|
|
return c.fd.WriteTo(b, sa)
|
|
|
|
}
|
|
|
|
|
2012-02-01 20:26:59 +01:00
|
|
|
// WriteTo implements the PacketConn WriteTo method.
|
|
|
|
func (c *IPConn) WriteTo(b []byte, addr Addr) (int, error) {
|
2011-09-16 17:47:21 +02:00
|
|
|
if !c.ok() {
|
2012-03-02 21:01:37 +01:00
|
|
|
return 0, syscall.EINVAL
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
|
|
|
a, ok := addr.(*IPAddr)
|
|
|
|
if !ok {
|
2012-03-02 21:01:37 +01:00
|
|
|
return 0, &OpError{"write", c.fd.net, addr, syscall.EINVAL}
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
|
|
|
return c.WriteToIP(b, a)
|
|
|
|
}
|
|
|
|
|
2012-01-25 21:56:26 +01:00
|
|
|
// DialIP connects to the remote address raddr on the network protocol netProto,
|
|
|
|
// which must be "ip", "ip4", or "ip6" followed by a colon and a protocol number or name.
|
2012-02-01 20:26:59 +01:00
|
|
|
func DialIP(netProto string, laddr, raddr *IPAddr) (*IPConn, error) {
|
|
|
|
net, proto, err := parseDialNetwork(netProto)
|
2011-09-16 17:47:21 +02:00
|
|
|
if err != nil {
|
2012-02-01 20:26:59 +01:00
|
|
|
return nil, err
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
|
|
|
switch net {
|
|
|
|
case "ip", "ip4", "ip6":
|
|
|
|
default:
|
|
|
|
return nil, UnknownNetworkError(net)
|
|
|
|
}
|
|
|
|
if raddr == nil {
|
2012-02-01 20:26:59 +01:00
|
|
|
return nil, &OpError{"dial", netProto, nil, errMissingAddress}
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
2012-02-01 20:26:59 +01:00
|
|
|
fd, err := internetSocket(net, laddr.toAddr(), raddr.toAddr(), syscall.SOCK_RAW, proto, "dial", sockaddrToIP)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
|
|
|
return newIPConn(fd), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListenIP listens for incoming IP packets addressed to the
|
|
|
|
// local address laddr. The returned connection c's ReadFrom
|
|
|
|
// and WriteTo methods can be used to receive and send IP
|
|
|
|
// packets with per-packet addressing.
|
2012-02-01 20:26:59 +01:00
|
|
|
func ListenIP(netProto string, laddr *IPAddr) (*IPConn, error) {
|
|
|
|
net, proto, err := parseDialNetwork(netProto)
|
2011-09-16 17:47:21 +02:00
|
|
|
if err != nil {
|
2012-02-01 20:26:59 +01:00
|
|
|
return nil, err
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
|
|
|
switch net {
|
|
|
|
case "ip", "ip4", "ip6":
|
|
|
|
default:
|
|
|
|
return nil, UnknownNetworkError(net)
|
|
|
|
}
|
2012-02-01 20:26:59 +01:00
|
|
|
fd, err := internetSocket(net, laddr.toAddr(), nil, syscall.SOCK_RAW, proto, "listen", sockaddrToIP)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
|
|
|
return newIPConn(fd), nil
|
|
|
|
}
|
|
|
|
|
2011-10-27 01:57:58 +02:00
|
|
|
// File returns a copy of the underlying os.File, set to blocking mode.
|
|
|
|
// It is the caller's responsibility to close f when finished.
|
|
|
|
// Closing c does not affect f, and closing f does not affect c.
|
2011-12-03 03:17:34 +01:00
|
|
|
func (c *IPConn) File() (f *os.File, err error) { return c.fd.dup() }
|