2016-07-23 02:15:38 +08:00
|
|
|
// Copyright 2010 The Go Authors. All rights reserved.
|
2011-09-16 23:47:21 +08:00
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2021-07-31 05:28:58 +08:00
|
|
|
//go:build aix || darwin || dragonfly || freebsd || hurd || (js && wasm) || linux || netbsd || openbsd || solaris || windows
|
2011-10-27 07:57:58 +08:00
|
|
|
|
2011-09-16 23:47:21 +08:00
|
|
|
package net
|
|
|
|
|
|
|
|
import (
|
2016-07-23 02:15:38 +08:00
|
|
|
"context"
|
2011-09-16 23:47:21 +08:00
|
|
|
"syscall"
|
|
|
|
)
|
|
|
|
|
|
|
|
func sockaddrToIP(sa syscall.Sockaddr) Addr {
|
|
|
|
switch sa := sa.(type) {
|
|
|
|
case *syscall.SockaddrInet4:
|
2012-12-13 07:13:29 +08:00
|
|
|
return &IPAddr{IP: sa.Addr[0:]}
|
2011-09-16 23:47:21 +08:00
|
|
|
case *syscall.SockaddrInet6:
|
2017-09-15 01:11:35 +08:00
|
|
|
return &IPAddr{IP: sa.Addr[0:], Zone: zoneCache.name(int(sa.ZoneId))}
|
2011-09-16 23:47:21 +08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *IPAddr) family() int {
|
2011-10-27 07:57:58 +08:00
|
|
|
if a == nil || len(a.IP) <= IPv4len {
|
2011-09-16 23:47:21 +08:00
|
|
|
return syscall.AF_INET
|
|
|
|
}
|
|
|
|
if a.IP.To4() != nil {
|
|
|
|
return syscall.AF_INET
|
|
|
|
}
|
|
|
|
return syscall.AF_INET6
|
|
|
|
}
|
|
|
|
|
2011-12-03 10:17:34 +08:00
|
|
|
func (a *IPAddr) sockaddr(family int) (syscall.Sockaddr, error) {
|
2013-11-07 03:49:01 +08:00
|
|
|
if a == nil {
|
|
|
|
return nil, nil
|
2011-09-16 23:47:21 +08:00
|
|
|
}
|
2013-11-07 03:49:01 +08:00
|
|
|
return ipToSockaddr(family, a.IP, 0, a.Zone)
|
2011-09-16 23:47:21 +08:00
|
|
|
}
|
|
|
|
|
2017-01-14 08:05:42 +08:00
|
|
|
func (a *IPAddr) toLocal(net string) sockaddr {
|
|
|
|
return &IPAddr{loopbackIP(net), a.Zone}
|
|
|
|
}
|
|
|
|
|
2016-07-23 02:15:38 +08:00
|
|
|
func (c *IPConn) readFrom(b []byte) (int, *IPAddr, error) {
|
2011-09-16 23:47:21 +08:00
|
|
|
// TODO(cw,rsc): consider using readv if we know the family
|
|
|
|
// type to avoid the header trim/copy
|
2012-02-02 03:26:59 +08:00
|
|
|
var addr *IPAddr
|
2014-07-19 16:53:52 +08:00
|
|
|
n, sa, err := c.fd.readFrom(b)
|
2011-09-16 23:47:21 +08:00
|
|
|
switch sa := sa.(type) {
|
|
|
|
case *syscall.SockaddrInet4:
|
2012-12-13 07:13:29 +08:00
|
|
|
addr = &IPAddr{IP: sa.Addr[0:]}
|
2015-10-31 08:59:47 +08:00
|
|
|
n = stripIPv4Header(n, b)
|
2011-09-16 23:47:21 +08:00
|
|
|
case *syscall.SockaddrInet6:
|
2017-09-15 01:11:35 +08:00
|
|
|
addr = &IPAddr{IP: sa.Addr[0:], Zone: zoneCache.name(int(sa.ZoneId))}
|
2011-09-16 23:47:21 +08:00
|
|
|
}
|
2012-02-02 03:26:59 +08:00
|
|
|
return n, addr, err
|
2011-09-16 23:47:21 +08:00
|
|
|
}
|
|
|
|
|
2015-10-31 08:59:47 +08: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
|
|
|
|
}
|
|
|
|
|
2016-07-23 02:15:38 +08:00
|
|
|
func (c *IPConn) readMsg(b, oob []byte) (n, oobn, flags int, addr *IPAddr, err error) {
|
2012-10-23 12:31:11 +08:00
|
|
|
var sa syscall.Sockaddr
|
2021-07-31 05:28:58 +08:00
|
|
|
n, oobn, flags, sa, err = c.fd.readMsg(b, oob, 0)
|
2012-10-23 12:31:11 +08:00
|
|
|
switch sa := sa.(type) {
|
|
|
|
case *syscall.SockaddrInet4:
|
2012-12-13 07:13:29 +08:00
|
|
|
addr = &IPAddr{IP: sa.Addr[0:]}
|
2012-10-23 12:31:11 +08:00
|
|
|
case *syscall.SockaddrInet6:
|
2017-09-15 01:11:35 +08:00
|
|
|
addr = &IPAddr{IP: sa.Addr[0:], Zone: zoneCache.name(int(sa.ZoneId))}
|
2012-10-23 12:31:11 +08:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-07-23 02:15:38 +08:00
|
|
|
func (c *IPConn) writeTo(b []byte, addr *IPAddr) (int, error) {
|
2014-07-19 16:53:52 +08:00
|
|
|
if c.fd.isConnected {
|
2016-07-23 02:15:38 +08:00
|
|
|
return 0, ErrWriteToConnected
|
2014-07-19 16:53:52 +08:00
|
|
|
}
|
2013-11-07 03:49:01 +08:00
|
|
|
if addr == nil {
|
2016-07-23 02:15:38 +08:00
|
|
|
return 0, errMissingAddress
|
2013-11-07 03:49:01 +08:00
|
|
|
}
|
2012-02-02 03:26:59 +08:00
|
|
|
sa, err := addr.sockaddr(c.fd.family)
|
|
|
|
if err != nil {
|
2016-07-23 02:15:38 +08:00
|
|
|
return 0, err
|
2011-09-16 23:47:21 +08:00
|
|
|
}
|
2016-07-23 02:15:38 +08:00
|
|
|
return c.fd.writeTo(b, sa)
|
2011-09-16 23:47:21 +08:00
|
|
|
}
|
|
|
|
|
2016-07-23 02:15:38 +08:00
|
|
|
func (c *IPConn) writeMsg(b, oob []byte, addr *IPAddr) (n, oobn int, err error) {
|
2014-07-19 16:53:52 +08:00
|
|
|
if c.fd.isConnected {
|
2016-07-23 02:15:38 +08:00
|
|
|
return 0, 0, ErrWriteToConnected
|
2014-07-19 16:53:52 +08:00
|
|
|
}
|
2013-11-07 03:49:01 +08:00
|
|
|
if addr == nil {
|
2016-07-23 02:15:38 +08:00
|
|
|
return 0, 0, errMissingAddress
|
2012-10-23 12:31:11 +08:00
|
|
|
}
|
2016-07-23 02:15:38 +08:00
|
|
|
sa, err := addr.sockaddr(c.fd.family)
|
2015-10-31 08:59:47 +08:00
|
|
|
if err != nil {
|
2016-07-23 02:15:38 +08:00
|
|
|
return 0, 0, err
|
2015-10-31 08:59:47 +08:00
|
|
|
}
|
2016-07-23 02:15:38 +08:00
|
|
|
return c.fd.writeMsg(b, oob, sa)
|
2012-11-21 15:03:38 +08:00
|
|
|
}
|
|
|
|
|
2018-09-25 05:46:21 +08:00
|
|
|
func (sd *sysDialer) dialIP(ctx context.Context, laddr, raddr *IPAddr) (*IPConn, error) {
|
|
|
|
network, proto, err := parseNetwork(ctx, sd.network, true)
|
2011-09-16 23:47:21 +08:00
|
|
|
if err != nil {
|
2016-07-23 02:15:38 +08:00
|
|
|
return nil, err
|
2011-09-16 23:47:21 +08:00
|
|
|
}
|
2016-07-23 02:15:38 +08:00
|
|
|
switch network {
|
2011-09-16 23:47:21 +08:00
|
|
|
case "ip", "ip4", "ip6":
|
|
|
|
default:
|
2018-09-25 05:46:21 +08:00
|
|
|
return nil, UnknownNetworkError(sd.network)
|
2011-09-16 23:47:21 +08:00
|
|
|
}
|
2018-09-25 05:46:21 +08:00
|
|
|
fd, err := internetSocket(ctx, network, laddr, raddr, syscall.SOCK_RAW, proto, "dial", sd.Dialer.Control)
|
2012-02-02 03:26:59 +08:00
|
|
|
if err != nil {
|
2016-07-23 02:15:38 +08:00
|
|
|
return nil, err
|
2011-09-16 23:47:21 +08:00
|
|
|
}
|
|
|
|
return newIPConn(fd), nil
|
|
|
|
}
|
|
|
|
|
2018-09-25 05:46:21 +08:00
|
|
|
func (sl *sysListener) listenIP(ctx context.Context, laddr *IPAddr) (*IPConn, error) {
|
|
|
|
network, proto, err := parseNetwork(ctx, sl.network, true)
|
2011-09-16 23:47:21 +08:00
|
|
|
if err != nil {
|
2016-07-23 02:15:38 +08:00
|
|
|
return nil, err
|
2011-09-16 23:47:21 +08:00
|
|
|
}
|
2016-07-23 02:15:38 +08:00
|
|
|
switch network {
|
2011-09-16 23:47:21 +08:00
|
|
|
case "ip", "ip4", "ip6":
|
|
|
|
default:
|
2018-09-25 05:46:21 +08:00
|
|
|
return nil, UnknownNetworkError(sl.network)
|
2011-09-16 23:47:21 +08:00
|
|
|
}
|
2018-09-25 05:46:21 +08:00
|
|
|
fd, err := internetSocket(ctx, network, laddr, nil, syscall.SOCK_RAW, proto, "listen", sl.ListenConfig.Control)
|
2012-02-02 03:26:59 +08:00
|
|
|
if err != nil {
|
2016-07-23 02:15:38 +08:00
|
|
|
return nil, err
|
2011-09-16 23:47:21 +08:00
|
|
|
}
|
|
|
|
return newIPConn(fd), nil
|
|
|
|
}
|