2016-03-01 15:57:46 -07:00
|
|
|
// Copyright 2010 The Go Authors. All rights reserved.
|
2011-08-17 11:28:29 -06:00
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
all: merge NaCl branch (part 1)
See golang.org/s/go13nacl for design overview.
This CL is the mostly mechanical changes from rsc's Go 1.2 based NaCl branch, specifically 39cb35750369 to 500771b477cf from https://code.google.com/r/rsc-go13nacl. This CL does not include working NaCl support, there are probably two or three more large merges to come.
CL 15750044 is not included as it involves more invasive changes to the linker which will need to be merged separately.
The exact change lists included are
15050047: syscall: support for Native Client
15360044: syscall: unzip implementation for Native Client
15370044: syscall: Native Client SRPC implementation
15400047: cmd/dist, cmd/go, go/build, test: support for Native Client
15410048: runtime: support for Native Client
15410049: syscall: file descriptor table for Native Client
15410050: syscall: in-memory file system for Native Client
15440048: all: update +build lines for Native Client port
15540045: cmd/6g, cmd/8g, cmd/gc: support for Native Client
15570045: os: support for Native Client
15680044: crypto/..., hash/crc32, reflect, sync/atomic: support for amd64p32
15690044: net: support for Native Client
15690048: runtime: support for fake time like on Go Playground
15690051: build: disable various tests on Native Client
LGTM=rsc
R=rsc
CC=golang-codereviews
https://golang.org/cl/68150047
2014-02-25 07:47:42 -07:00
|
|
|
// +build darwin dragonfly freebsd linux nacl netbsd openbsd solaris 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
|
|
|
package net
|
|
|
|
|
|
|
|
import (
|
|
|
|
"syscall"
|
2012-11-08 09:35:16 -07:00
|
|
|
"time"
|
2011-08-17 11:28:29 -06:00
|
|
|
)
|
|
|
|
|
2013-08-28 04:49:17 -06:00
|
|
|
// BUG(mikio): On every POSIX platform, reads from the "ip4" network
|
|
|
|
// using the ReadFrom or ReadFromIP method might not return a complete
|
|
|
|
// IPv4 packet, including its header, even if there is space
|
|
|
|
// available. This can occur even in cases where Read or ReadMsgIP
|
|
|
|
// could return a complete packet. For this reason, it is recommended
|
|
|
|
// that you do not uses these methods if it is important to receive a
|
|
|
|
// full packet.
|
|
|
|
//
|
2013-12-31 02:52:37 -07:00
|
|
|
// The Go 1 compatibility guidelines make it impossible for us to
|
2013-08-28 04:49:17 -06:00
|
|
|
// change the behavior of these methods; use Read or ReadMsgIP
|
|
|
|
// instead.
|
|
|
|
|
2011-08-17 11:28:29 -06:00
|
|
|
func sockaddrToIP(sa syscall.Sockaddr) Addr {
|
|
|
|
switch sa := sa.(type) {
|
|
|
|
case *syscall.SockaddrInet4:
|
net, cmd/fix: add IPv6 scoped addressing zone to INET, INET6 address structs
This CL starts to introduce IPv6 scoped addressing capability
into the net package.
The Public API changes are:
+pkg net, type IPAddr struct, Zone string
+pkg net, type IPNet struct, Zone string
+pkg net, type TCPAddr struct, Zone string
+pkg net, type UDPAddr struct, Zone string
Update #4234.
R=rsc, bradfitz, iant
CC=golang-dev
https://golang.org/cl/6849045
2012-11-26 08:45:42 -07:00
|
|
|
return &IPAddr{IP: sa.Addr[0:]}
|
2011-08-17 11:28:29 -06:00
|
|
|
case *syscall.SockaddrInet6:
|
net, cmd/fix: add IPv6 scoped addressing zone to INET, INET6 address structs
This CL starts to introduce IPv6 scoped addressing capability
into the net package.
The Public API changes are:
+pkg net, type IPAddr struct, Zone string
+pkg net, type IPNet struct, Zone string
+pkg net, type TCPAddr struct, Zone string
+pkg net, type UDPAddr struct, Zone string
Update #4234.
R=rsc, bradfitz, iant
CC=golang-dev
https://golang.org/cl/6849045
2012-11-26 08:45:42 -07:00
|
|
|
return &IPAddr{IP: sa.Addr[0:], Zone: zoneToString(int(sa.ZoneId))}
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2011-11-01 20:05:34 -06:00
|
|
|
func (a *IPAddr) sockaddr(family int) (syscall.Sockaddr, error) {
|
2013-08-02 22:32:22 -06:00
|
|
|
if a == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
net, cmd/fix: add IPv6 scoped addressing zone to INET, INET6 address structs
This CL starts to introduce IPv6 scoped addressing capability
into the net package.
The Public API changes are:
+pkg net, type IPAddr struct, Zone string
+pkg net, type IPNet struct, Zone string
+pkg net, type TCPAddr struct, Zone string
+pkg net, type UDPAddr struct, Zone string
Update #4234.
R=rsc, bradfitz, iant
CC=golang-dev
https://golang.org/cl/6849045
2012-11-26 08:45:42 -07:00
|
|
|
return ipToSockaddr(family, a.IP, 0, a.Zone)
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
|
2013-03-31 01:46:59 -06:00
|
|
|
// IPConn is the implementation of the Conn and PacketConn interfaces
|
|
|
|
// for IP network connections.
|
2011-08-17 11:28:29 -06:00
|
|
|
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
|
|
|
|
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
|
2014-04-02 18:06:51 -06:00
|
|
|
n, sa, err := c.fd.readFrom(b)
|
2011-08-17 11:28:29 -06:00
|
|
|
switch sa := sa.(type) {
|
|
|
|
case *syscall.SockaddrInet4:
|
net, cmd/fix: add IPv6 scoped addressing zone to INET, INET6 address structs
This CL starts to introduce IPv6 scoped addressing capability
into the net package.
The Public API changes are:
+pkg net, type IPAddr struct, Zone string
+pkg net, type IPNet struct, Zone string
+pkg net, type TCPAddr struct, Zone string
+pkg net, type UDPAddr struct, Zone string
Update #4234.
R=rsc, bradfitz, iant
CC=golang-dev
https://golang.org/cl/6849045
2012-11-26 08:45:42 -07:00
|
|
|
addr = &IPAddr{IP: sa.Addr[0:]}
|
2015-01-18 00:28:15 -07:00
|
|
|
n = stripIPv4Header(n, b)
|
2011-08-17 11:28:29 -06:00
|
|
|
case *syscall.SockaddrInet6:
|
net, cmd/fix: add IPv6 scoped addressing zone to INET, INET6 address structs
This CL starts to introduce IPv6 scoped addressing capability
into the net package.
The Public API changes are:
+pkg net, type IPAddr struct, Zone string
+pkg net, type IPNet struct, Zone string
+pkg net, type TCPAddr struct, Zone string
+pkg net, type UDPAddr struct, Zone string
Update #4234.
R=rsc, bradfitz, iant
CC=golang-dev
https://golang.org/cl/6849045
2012-11-26 08:45:42 -07:00
|
|
|
addr = &IPAddr{IP: sa.Addr[0:], Zone: zoneToString(int(sa.ZoneId))}
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
2015-04-16 08:10:56 -06:00
|
|
|
if err != nil {
|
2015-04-21 07:53:47 -06:00
|
|
|
err = &OpError{Op: "read", Net: c.fd.net, Source: c.fd.laddr, Addr: c.fd.raddr, Err: err}
|
2015-04-16 08:10:56 -06:00
|
|
|
}
|
2012-01-21 05:51:53 -07:00
|
|
|
return n, addr, err
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
|
2015-01-18 00:28:15 -07:00
|
|
|
func stripIPv4Header(n int, b []byte) int {
|
|
|
|
if len(b) < 20 {
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
l := int(b[0]&0x0f) << 2
|
|
|
|
if 20 > l || l > len(b) {
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
if b[0]>>4 != 4 {
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
copy(b, b[l:])
|
|
|
|
return n - l
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
net, cmd/fix: add IPv6 scoped addressing zone to INET, INET6 address structs
This CL starts to introduce IPv6 scoped addressing capability
into the net package.
The Public API changes are:
+pkg net, type IPAddr struct, Zone string
+pkg net, type IPNet struct, Zone string
+pkg net, type TCPAddr struct, Zone string
+pkg net, type UDPAddr struct, Zone string
Update #4234.
R=rsc, bradfitz, iant
CC=golang-dev
https://golang.org/cl/6849045
2012-11-26 08:45:42 -07:00
|
|
|
n, addr, err := c.ReadFromIP(b)
|
2015-04-01 16:17:09 -06:00
|
|
|
if addr == nil {
|
|
|
|
return n, nil, err
|
|
|
|
}
|
|
|
|
return n, addr, err
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
|
2012-09-24 15:57:32 -06:00
|
|
|
// ReadMsgIP reads a packet from c, copying the payload into b and the
|
2016-03-01 16:21:55 -07:00
|
|
|
// associated out-of-band data into oob. It returns the number of
|
2012-09-24 15:57:32 -06:00
|
|
|
// 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
|
2014-04-02 18:06:51 -06:00
|
|
|
n, oobn, flags, sa, err = c.fd.readMsg(b, oob)
|
2012-09-24 15:57:32 -06:00
|
|
|
switch sa := sa.(type) {
|
|
|
|
case *syscall.SockaddrInet4:
|
net, cmd/fix: add IPv6 scoped addressing zone to INET, INET6 address structs
This CL starts to introduce IPv6 scoped addressing capability
into the net package.
The Public API changes are:
+pkg net, type IPAddr struct, Zone string
+pkg net, type IPNet struct, Zone string
+pkg net, type TCPAddr struct, Zone string
+pkg net, type UDPAddr struct, Zone string
Update #4234.
R=rsc, bradfitz, iant
CC=golang-dev
https://golang.org/cl/6849045
2012-11-26 08:45:42 -07:00
|
|
|
addr = &IPAddr{IP: sa.Addr[0:]}
|
2012-09-24 15:57:32 -06:00
|
|
|
case *syscall.SockaddrInet6:
|
net, cmd/fix: add IPv6 scoped addressing zone to INET, INET6 address structs
This CL starts to introduce IPv6 scoped addressing capability
into the net package.
The Public API changes are:
+pkg net, type IPAddr struct, Zone string
+pkg net, type IPNet struct, Zone string
+pkg net, type TCPAddr struct, Zone string
+pkg net, type UDPAddr struct, Zone string
Update #4234.
R=rsc, bradfitz, iant
CC=golang-dev
https://golang.org/cl/6849045
2012-11-26 08:45:42 -07:00
|
|
|
addr = &IPAddr{IP: sa.Addr[0:], Zone: zoneToString(int(sa.ZoneId))}
|
2012-09-24 15:57:32 -06:00
|
|
|
}
|
2015-04-16 08:10:56 -06:00
|
|
|
if err != nil {
|
2015-04-21 07:53:47 -06:00
|
|
|
err = &OpError{Op: "read", Net: c.fd.net, Source: c.fd.laddr, Addr: c.fd.raddr, Err: err}
|
2015-04-16 08:10:56 -06:00
|
|
|
}
|
2012-09-24 15:57:32 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-03-31 01:46:59 -06:00
|
|
|
// WriteToIP writes an IP packet to addr via c, copying the payload
|
|
|
|
// from b.
|
2011-08-17 11:28:29 -06:00
|
|
|
//
|
2013-03-31 01:46:59 -06:00
|
|
|
// WriteToIP can be made to time out and return an error with
|
|
|
|
// Timeout() == true after a fixed time limit; see SetDeadline and
|
2016-03-01 16:21:55 -07:00
|
|
|
// SetWriteDeadline. On packet-oriented connections, write timeouts
|
2013-03-31 01:46:59 -06:00
|
|
|
// 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
|
|
|
}
|
2014-04-28 21:37:16 -06:00
|
|
|
if c.fd.isConnected {
|
2015-05-29 16:33:16 -06:00
|
|
|
return 0, &OpError{Op: "write", Net: c.fd.net, Source: c.fd.laddr, Addr: addr.opAddr(), Err: ErrWriteToConnected}
|
2014-04-28 21:37:16 -06:00
|
|
|
}
|
2013-08-18 04:19:36 -06:00
|
|
|
if addr == nil {
|
2015-05-29 16:33:16 -06:00
|
|
|
return 0, &OpError{Op: "write", Net: c.fd.net, Source: c.fd.laddr, Addr: nil, Err: errMissingAddress}
|
2013-08-18 04:19:36 -06:00
|
|
|
}
|
2012-01-21 05:51:53 -07:00
|
|
|
sa, err := addr.sockaddr(c.fd.family)
|
|
|
|
if err != nil {
|
2015-05-29 16:33:16 -06:00
|
|
|
return 0, &OpError{Op: "write", Net: c.fd.net, Source: c.fd.laddr, Addr: addr.opAddr(), Err: err}
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
2015-04-15 20:26:44 -06:00
|
|
|
n, err := c.fd.writeTo(b, sa)
|
|
|
|
if err != nil {
|
2015-05-29 16:33:16 -06:00
|
|
|
err = &OpError{Op: "write", Net: c.fd.net, Source: c.fd.laddr, Addr: addr.opAddr(), Err: err}
|
2015-04-15 20:26:44 -06:00
|
|
|
}
|
|
|
|
return n, err
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
|
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 {
|
2015-04-21 07:53:47 -06:00
|
|
|
return 0, &OpError{Op: "write", Net: c.fd.net, Source: c.fd.laddr, Addr: addr, Err: 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
|
2016-03-01 16:21:55 -07:00
|
|
|
// b and the associated out-of-band data from oob. It returns the
|
2012-09-24 15:57:32 -06:00
|
|
|
// 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
|
|
|
|
}
|
2014-04-28 21:37:16 -06:00
|
|
|
if c.fd.isConnected {
|
2015-05-29 16:33:16 -06:00
|
|
|
return 0, 0, &OpError{Op: "write", Net: c.fd.net, Source: c.fd.laddr, Addr: addr.opAddr(), Err: ErrWriteToConnected}
|
2014-04-28 21:37:16 -06:00
|
|
|
}
|
2013-08-18 04:19:36 -06:00
|
|
|
if addr == nil {
|
2015-05-29 16:33:16 -06:00
|
|
|
return 0, 0, &OpError{Op: "write", Net: c.fd.net, Source: c.fd.laddr, Addr: nil, Err: errMissingAddress}
|
2013-08-18 04:19:36 -06:00
|
|
|
}
|
2015-04-15 20:26:44 -06:00
|
|
|
var sa syscall.Sockaddr
|
|
|
|
sa, err = addr.sockaddr(c.fd.family)
|
2012-09-24 15:57:32 -06:00
|
|
|
if err != nil {
|
2015-05-29 16:33:16 -06:00
|
|
|
return 0, 0, &OpError{Op: "write", Net: c.fd.net, Source: c.fd.laddr, Addr: addr.opAddr(), Err: err}
|
2012-09-24 15:57:32 -06:00
|
|
|
}
|
2015-04-15 20:26:44 -06:00
|
|
|
n, oobn, err = c.fd.writeMsg(b, oob, sa)
|
|
|
|
if err != nil {
|
2015-05-29 16:33:16 -06:00
|
|
|
err = &OpError{Op: "write", Net: c.fd.net, Source: c.fd.laddr, Addr: addr.opAddr(), Err: err}
|
2015-04-15 20:26:44 -06:00
|
|
|
}
|
|
|
|
return
|
2012-09-24 15:57:32 -06:00
|
|
|
}
|
|
|
|
|
2013-03-31 01:46:59 -06: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) {
|
2016-03-02 04:08:18 -07:00
|
|
|
c, err := dialIP(netProto, laddr, raddr, noDeadline)
|
|
|
|
if err != nil {
|
|
|
|
return nil, &OpError{Op: "dial", Net: netProto, Source: laddr.opAddr(), Addr: raddr.opAddr(), Err: err}
|
|
|
|
}
|
|
|
|
return c, nil
|
2012-11-08 09:35:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func dialIP(netProto string, laddr, raddr *IPAddr, deadline time.Time) (*IPConn, error) {
|
2013-02-08 05:53:10 -07:00
|
|
|
net, proto, err := parseNetwork(netProto)
|
2011-08-17 11:28:29 -06:00
|
|
|
if err != nil {
|
2016-03-02 04:08:18 -07:00
|
|
|
return nil, err
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
switch net {
|
|
|
|
case "ip", "ip4", "ip6":
|
|
|
|
default:
|
2016-03-02 04:08:18 -07:00
|
|
|
return nil, UnknownNetworkError(netProto)
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
if raddr == nil {
|
2016-03-02 04:08:18 -07:00
|
|
|
return nil, errMissingAddress
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
2015-12-14 15:21:48 -07:00
|
|
|
fd, err := internetSocket(net, laddr, raddr, deadline, syscall.SOCK_RAW, proto, "dial", noCancel)
|
2012-01-21 05:51:53 -07:00
|
|
|
if err != nil {
|
2016-03-02 04:08:18 -07:00
|
|
|
return nil, err
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
return newIPConn(fd), nil
|
|
|
|
}
|
|
|
|
|
2013-03-31 01:46:59 -06:00
|
|
|
// ListenIP listens for incoming IP packets addressed to the local
|
2016-03-01 16:21:55 -07:00
|
|
|
// address laddr. The returned connection's ReadFrom and WriteTo
|
2013-03-31 01:46:59 -06:00
|
|
|
// 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) {
|
2013-02-08 05:53:10 -07:00
|
|
|
net, proto, err := parseNetwork(netProto)
|
2011-08-17 11:28:29 -06:00
|
|
|
if err != nil {
|
2015-05-29 16:33:16 -06:00
|
|
|
return nil, &OpError{Op: "listen", Net: netProto, Source: nil, Addr: laddr.opAddr(), Err: err}
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
switch net {
|
|
|
|
case "ip", "ip4", "ip6":
|
|
|
|
default:
|
2015-05-29 16:33:16 -06:00
|
|
|
return nil, &OpError{Op: "listen", Net: netProto, Source: nil, Addr: laddr.opAddr(), Err: UnknownNetworkError(netProto)}
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
2015-12-14 15:21:48 -07:00
|
|
|
fd, err := internetSocket(net, laddr, nil, noDeadline, syscall.SOCK_RAW, proto, "listen", noCancel)
|
2012-01-21 05:51:53 -07:00
|
|
|
if err != nil {
|
2015-05-29 16:33:16 -06:00
|
|
|
return nil, &OpError{Op: "listen", Net: netProto, Source: nil, Addr: laddr.opAddr(), Err: err}
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
return newIPConn(fd), nil
|
|
|
|
}
|