2011-08-17 11:28:29 -06: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.
|
|
|
|
|
2011-12-21 03:44:47 -07:00
|
|
|
// +build darwin freebsd linux netbsd openbsd windows
|
build: add build comments to core packages
The go/build package already recognizes
system-specific file names like
mycode_darwin.go
mycode_darwin_386.go
mycode_386.s
However, it is also common to write files that
apply to multiple architectures, so a recent CL added
to go/build the ability to process comments
listing a set of conditions for building. For example:
// +build darwin freebsd openbsd/386
says that this file should be compiled only on
OS X, FreeBSD, or 32-bit x86 OpenBSD systems.
These conventions are not yet documented
(hence this long CL description).
This CL adds build comments to the multi-system
files in the core library, a step toward making it
possible to use go/build to build them.
With this change go/build can handle crypto/rand,
exec, net, path/filepath, os/user, and time.
os and syscall need additional adjustments.
R=golang-dev, r, gri, r, gustavo
CC=golang-dev
https://golang.org/cl/5011046
2011-09-15 14:48:57 -06:00
|
|
|
|
2011-08-17 11:28:29 -06:00
|
|
|
// (Raw) IP sockets
|
|
|
|
|
|
|
|
package net
|
|
|
|
|
|
|
|
import (
|
|
|
|
"syscall"
|
2012-11-08 09:35:16 -07:00
|
|
|
"time"
|
2011-08-17 11:28:29 -06: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-08-24 11:59:33 -06:00
|
|
|
if a == nil || len(a.IP) <= IPv4len {
|
2011-08-17 11:28:29 -06:00
|
|
|
return syscall.AF_INET
|
|
|
|
}
|
|
|
|
if a.IP.To4() != nil {
|
|
|
|
return syscall.AF_INET
|
|
|
|
}
|
|
|
|
return syscall.AF_INET6
|
|
|
|
}
|
|
|
|
|
2012-03-05 08:13:10 -07:00
|
|
|
func (a *IPAddr) isWildcard() bool {
|
|
|
|
if a == nil || a.IP == nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return a.IP.IsUnspecified()
|
|
|
|
}
|
|
|
|
|
2011-11-01 20:05:34 -06:00
|
|
|
func (a *IPAddr) sockaddr(family int) (syscall.Sockaddr, error) {
|
2011-08-17 11:28:29 -06: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 {
|
net: consolidate common socket functions
In resolving 3507, the fix had to be applied individually to
the four *Conn types, tcp, udp, rawip and unix, due to the
duplicate code in each Conn type.
This CL consolidates the common net.Conn methods that all four
*Conn types implement into a base conn type.
Pros:
* The fix for 3507 would have only needed to be applied to one
method. Further improvements, such as possibly removing the
c.fd != nil check in c.ok(), would benefit from this CL.
* Nearly 300 lines removed from the net package.
* The public interface and documentation are not changed.
* I think this is an excellent example of the power of embedding.
Cons:
* The net package is already distributed over many files, this
CL adds another place to look.
* The fix for 3507 was a total of 16 lines changed, this follow
up CL could be considered to be an overreaction as new Conn types
are unlikely to be added in the near future.
R=golang-dev, rsc
CC=golang-dev
https://golang.org/cl/6098047
2012-04-27 06:17:08 -06:00
|
|
|
conn
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
|
net: consolidate common socket functions
In resolving 3507, the fix had to be applied individually to
the four *Conn types, tcp, udp, rawip and unix, due to the
duplicate code in each Conn type.
This CL consolidates the common net.Conn methods that all four
*Conn types implement into a base conn type.
Pros:
* The fix for 3507 would have only needed to be applied to one
method. Further improvements, such as possibly removing the
c.fd != nil check in c.ok(), would benefit from this CL.
* Nearly 300 lines removed from the net package.
* The public interface and documentation are not changed.
* I think this is an excellent example of the power of embedding.
Cons:
* The net package is already distributed over many files, this
CL adds another place to look.
* The fix for 3507 was a total of 16 lines changed, this follow
up CL could be considered to be an overreaction as new Conn types
are unlikely to be added in the near future.
R=golang-dev, rsc
CC=golang-dev
https://golang.org/cl/6098047
2012-04-27 06:17:08 -06:00
|
|
|
func newIPConn(fd *netFD) *IPConn { return &IPConn{conn{fd}} }
|
2011-08-17 11:28:29 -06:00
|
|
|
|
|
|
|
// IP-specific methods.
|
|
|
|
|
2012-07-19 17:32:25 -06:00
|
|
|
// ReadFromIP reads an IP packet from c, copying the payload into b.
|
2011-08-17 11:28:29 -06: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-18 17:24:06 -07:00
|
|
|
// Timeout() == true after a fixed time limit; see SetDeadline and
|
|
|
|
// SetReadDeadline.
|
2012-01-21 05:51:53 -07:00
|
|
|
func (c *IPConn) ReadFromIP(b []byte) (int, *IPAddr, error) {
|
2011-08-17 11:28:29 -06:00
|
|
|
if !c.ok() {
|
2012-02-16 16:04:29 -07:00
|
|
|
return 0, nil, syscall.EINVAL
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
// TODO(cw,rsc): consider using readv if we know the family
|
|
|
|
// type to avoid the header trim/copy
|
2012-01-21 05:51:53 -07:00
|
|
|
var addr *IPAddr
|
2011-08-17 11:28:29 -06:00
|
|
|
n, sa, err := c.fd.ReadFrom(b)
|
|
|
|
switch sa := sa.(type) {
|
|
|
|
case *syscall.SockaddrInet4:
|
|
|
|
addr = &IPAddr{sa.Addr[0:]}
|
2011-08-24 11:59:33 -06:00
|
|
|
if len(b) >= IPv4len { // discard ipv4 header
|
2011-08-17 11:28:29 -06:00
|
|
|
hsize := (int(b[0]) & 0xf) * 4
|
|
|
|
copy(b, b[hsize:])
|
|
|
|
n -= hsize
|
|
|
|
}
|
|
|
|
case *syscall.SockaddrInet6:
|
|
|
|
addr = &IPAddr{sa.Addr[0:]}
|
|
|
|
}
|
2012-01-21 05:51:53 -07:00
|
|
|
return n, addr, err
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
|
2012-01-21 05:51:53 -07:00
|
|
|
// ReadFrom implements the PacketConn ReadFrom method.
|
|
|
|
func (c *IPConn) ReadFrom(b []byte) (int, Addr, error) {
|
2011-08-17 11:28:29 -06:00
|
|
|
if !c.ok() {
|
2012-02-16 16:04:29 -07:00
|
|
|
return 0, nil, syscall.EINVAL
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
n, uaddr, err := c.ReadFromIP(b)
|
|
|
|
return n, uaddr.toAddr(), err
|
|
|
|
}
|
|
|
|
|
2012-09-24 15:57:32 -06:00
|
|
|
// ReadMsgIP reads a packet from c, copying the payload into b and the
|
|
|
|
// associdated out-of-band data into oob. It returns the number of
|
|
|
|
// bytes copied into b, the number of bytes copied into oob, the flags
|
|
|
|
// that were set on the packet and the source address of the packet.
|
|
|
|
func (c *IPConn) ReadMsgIP(b, oob []byte) (n, oobn, flags int, addr *IPAddr, err error) {
|
|
|
|
if !c.ok() {
|
|
|
|
return 0, 0, 0, nil, syscall.EINVAL
|
|
|
|
}
|
|
|
|
var sa syscall.Sockaddr
|
|
|
|
n, oobn, flags, sa, err = c.fd.ReadMsg(b, oob)
|
|
|
|
switch sa := sa.(type) {
|
|
|
|
case *syscall.SockaddrInet4:
|
|
|
|
addr = &IPAddr{sa.Addr[0:]}
|
|
|
|
case *syscall.SockaddrInet6:
|
|
|
|
addr = &IPAddr{sa.Addr[0:]}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2012-07-19 17:32:25 -06:00
|
|
|
// WriteToIP writes an IP packet to addr via c, copying the payload from b.
|
2011-08-17 11:28:29 -06:00
|
|
|
//
|
|
|
|
// WriteToIP can be made to time out and return
|
|
|
|
// an error with Timeout() == true after a fixed time limit;
|
2012-01-18 17:24:06 -07:00
|
|
|
// see SetDeadline and SetWriteDeadline.
|
2011-08-17 11:28:29 -06:00
|
|
|
// On packet-oriented connections, write timeouts are rare.
|
2012-01-21 05:51:53 -07:00
|
|
|
func (c *IPConn) WriteToIP(b []byte, addr *IPAddr) (int, error) {
|
2011-08-17 11:28:29 -06:00
|
|
|
if !c.ok() {
|
2012-02-16 16:04:29 -07:00
|
|
|
return 0, syscall.EINVAL
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
2012-01-21 05:51:53 -07:00
|
|
|
sa, err := addr.sockaddr(c.fd.family)
|
|
|
|
if err != nil {
|
2012-01-23 10:59:43 -07:00
|
|
|
return 0, &OpError{"write", c.fd.net, addr, err}
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
return c.fd.WriteTo(b, sa)
|
|
|
|
}
|
|
|
|
|
2012-01-21 05:51:53 -07:00
|
|
|
// WriteTo implements the PacketConn WriteTo method.
|
|
|
|
func (c *IPConn) WriteTo(b []byte, addr Addr) (int, error) {
|
2011-08-17 11:28:29 -06:00
|
|
|
if !c.ok() {
|
2012-02-16 16:04:29 -07:00
|
|
|
return 0, syscall.EINVAL
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
a, ok := addr.(*IPAddr)
|
|
|
|
if !ok {
|
2012-02-16 16:04:29 -07:00
|
|
|
return 0, &OpError{"write", c.fd.net, addr, syscall.EINVAL}
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
return c.WriteToIP(b, a)
|
|
|
|
}
|
|
|
|
|
2012-09-24 15:57:32 -06:00
|
|
|
// WriteMsgIP writes a packet to addr via c, copying the payload from
|
|
|
|
// b and the associated out-of-band data from oob. It returns the
|
|
|
|
// number of payload and out-of-band bytes written.
|
|
|
|
func (c *IPConn) WriteMsgIP(b, oob []byte, addr *IPAddr) (n, oobn int, err error) {
|
|
|
|
if !c.ok() {
|
|
|
|
return 0, 0, syscall.EINVAL
|
|
|
|
}
|
|
|
|
sa, err := addr.sockaddr(c.fd.family)
|
|
|
|
if err != nil {
|
|
|
|
return 0, 0, &OpError{"write", c.fd.net, addr, err}
|
|
|
|
}
|
|
|
|
return c.fd.WriteMsg(b, oob, sa)
|
|
|
|
}
|
|
|
|
|
2012-01-04 21:18:08 -07: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-01-21 05:51:53 -07:00
|
|
|
func DialIP(netProto string, laddr, raddr *IPAddr) (*IPConn, error) {
|
2012-11-08 09:35:16 -07:00
|
|
|
return dialIP(netProto, laddr, raddr, noDeadline)
|
|
|
|
}
|
|
|
|
|
|
|
|
func dialIP(netProto string, laddr, raddr *IPAddr, deadline time.Time) (*IPConn, error) {
|
2012-01-21 05:51:53 -07:00
|
|
|
net, proto, err := parseDialNetwork(netProto)
|
2011-08-17 11:28:29 -06:00
|
|
|
if err != nil {
|
2012-01-21 05:51:53 -07:00
|
|
|
return nil, err
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
switch net {
|
|
|
|
case "ip", "ip4", "ip6":
|
|
|
|
default:
|
|
|
|
return nil, UnknownNetworkError(net)
|
|
|
|
}
|
|
|
|
if raddr == nil {
|
2012-01-23 10:59:43 -07:00
|
|
|
return nil, &OpError{"dial", netProto, nil, errMissingAddress}
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
2012-11-08 09:35:16 -07:00
|
|
|
fd, err := internetSocket(net, laddr.toAddr(), raddr.toAddr(), deadline, syscall.SOCK_RAW, proto, "dial", sockaddrToIP)
|
2012-01-21 05:51:53 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2011-08-17 11:28:29 -06: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-01-21 05:51:53 -07:00
|
|
|
func ListenIP(netProto string, laddr *IPAddr) (*IPConn, error) {
|
|
|
|
net, proto, err := parseDialNetwork(netProto)
|
2011-08-17 11:28:29 -06:00
|
|
|
if err != nil {
|
2012-01-21 05:51:53 -07:00
|
|
|
return nil, err
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
switch net {
|
|
|
|
case "ip", "ip4", "ip6":
|
|
|
|
default:
|
|
|
|
return nil, UnknownNetworkError(net)
|
|
|
|
}
|
2012-11-08 09:35:16 -07:00
|
|
|
fd, err := internetSocket(net, laddr.toAddr(), nil, noDeadline, syscall.SOCK_RAW, proto, "listen", sockaddrToIP)
|
2012-01-21 05:51:53 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
return newIPConn(fd), nil
|
|
|
|
}
|