2016-03-01 15:57:46 -07:00
|
|
|
// Copyright 2009 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 (
|
2016-04-14 18:47:25 -06:00
|
|
|
"context"
|
2012-12-15 19:51:47 -07:00
|
|
|
"errors"
|
2011-08-17 11:28:29 -06:00
|
|
|
"os"
|
|
|
|
"syscall"
|
|
|
|
)
|
|
|
|
|
2016-04-14 18:47:25 -06:00
|
|
|
func unixSocket(ctx context.Context, net string, laddr, raddr sockaddr, mode string) (*netFD, error) {
|
2012-01-19 15:31:13 -07:00
|
|
|
var sotype int
|
2011-08-17 11:28:29 -06:00
|
|
|
switch net {
|
|
|
|
case "unix":
|
2012-01-19 15:31:13 -07:00
|
|
|
sotype = syscall.SOCK_STREAM
|
2011-08-17 11:28:29 -06:00
|
|
|
case "unixgram":
|
2012-01-19 15:31:13 -07:00
|
|
|
sotype = syscall.SOCK_DGRAM
|
2011-08-17 11:28:29 -06:00
|
|
|
case "unixpacket":
|
2012-01-19 15:31:13 -07:00
|
|
|
sotype = syscall.SOCK_SEQPACKET
|
2012-12-15 19:51:47 -07:00
|
|
|
default:
|
|
|
|
return nil, UnknownNetworkError(net)
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
switch mode {
|
|
|
|
case "dial":
|
2013-08-02 22:32:22 -06:00
|
|
|
if laddr != nil && laddr.isWildcard() {
|
|
|
|
laddr = nil
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
2013-08-02 22:32:22 -06:00
|
|
|
if raddr != nil && raddr.isWildcard() {
|
|
|
|
raddr = nil
|
|
|
|
}
|
|
|
|
if raddr == nil && (sotype != syscall.SOCK_DGRAM || laddr == nil) {
|
2013-08-28 04:51:02 -06:00
|
|
|
return nil, errMissingAddress
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
case "listen":
|
2012-12-15 19:51:47 -07:00
|
|
|
default:
|
|
|
|
return nil, errors.New("unknown mode: " + mode)
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
|
2016-04-14 18:47:25 -06:00
|
|
|
fd, err := socket(ctx, net, syscall.AF_UNIX, sotype, 0, false, laddr, raddr)
|
2012-02-22 03:08:19 -07:00
|
|
|
if err != nil {
|
2013-08-28 04:51:02 -06:00
|
|
|
return nil, err
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
return fd, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func sockaddrToUnix(sa syscall.Sockaddr) Addr {
|
|
|
|
if s, ok := sa.(*syscall.SockaddrUnix); ok {
|
2013-03-23 07:32:19 -06:00
|
|
|
return &UnixAddr{Name: s.Name, Net: "unix"}
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func sockaddrToUnixgram(sa syscall.Sockaddr) Addr {
|
|
|
|
if s, ok := sa.(*syscall.SockaddrUnix); ok {
|
2013-03-23 07:32:19 -06:00
|
|
|
return &UnixAddr{Name: s.Name, Net: "unixgram"}
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func sockaddrToUnixpacket(sa syscall.Sockaddr) Addr {
|
|
|
|
if s, ok := sa.(*syscall.SockaddrUnix); ok {
|
2013-03-23 07:32:19 -06:00
|
|
|
return &UnixAddr{Name: s.Name, Net: "unixpacket"}
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2012-01-19 15:31:13 -07:00
|
|
|
func sotypeToNet(sotype int) string {
|
|
|
|
switch sotype {
|
2011-08-17 11:28:29 -06:00
|
|
|
case syscall.SOCK_STREAM:
|
|
|
|
return "unix"
|
|
|
|
case syscall.SOCK_DGRAM:
|
|
|
|
return "unixgram"
|
2013-03-23 07:32:19 -06:00
|
|
|
case syscall.SOCK_SEQPACKET:
|
|
|
|
return "unixpacket"
|
2011-08-17 11:28:29 -06:00
|
|
|
default:
|
2012-01-19 15:31:13 -07:00
|
|
|
panic("sotypeToNet unknown socket type")
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-28 01:15:07 -06:00
|
|
|
func (a *UnixAddr) family() int {
|
|
|
|
return syscall.AF_UNIX
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *UnixAddr) sockaddr(family int) (syscall.Sockaddr, error) {
|
|
|
|
if a == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return &syscall.SockaddrUnix{Name: a.Name}, nil
|
|
|
|
}
|
|
|
|
|
2016-10-26 19:07:52 -06:00
|
|
|
func (a *UnixAddr) toLocal(net string) sockaddr {
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
2016-03-02 14:22:11 -07:00
|
|
|
func (c *UnixConn) readFrom(b []byte) (int, *UnixAddr, error) {
|
2015-04-16 08:10:56 -06:00
|
|
|
var addr *UnixAddr
|
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.SockaddrUnix:
|
2013-01-30 11:02:01 -07:00
|
|
|
if sa.Name != "" {
|
2013-03-23 07:32:19 -06:00
|
|
|
addr = &UnixAddr{Name: sa.Name, Net: sotypeToNet(c.fd.sotype)}
|
2013-01-30 11:02:01 -07:00
|
|
|
}
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
2015-04-01 16:17:09 -06:00
|
|
|
return n, addr, err
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
|
2016-03-02 14:22:11 -07:00
|
|
|
func (c *UnixConn) readMsg(b, oob []byte) (n, oobn, flags int, addr *UnixAddr, err error) {
|
|
|
|
var sa syscall.Sockaddr
|
|
|
|
n, oobn, flags, sa, err = c.fd.readMsg(b, oob)
|
2012-12-15 19:51:47 -07:00
|
|
|
switch sa := sa.(type) {
|
|
|
|
case *syscall.SockaddrUnix:
|
2013-01-30 11:02:01 -07:00
|
|
|
if sa.Name != "" {
|
2013-03-23 07:32:19 -06:00
|
|
|
addr = &UnixAddr{Name: sa.Name, Net: sotypeToNet(c.fd.sotype)}
|
2013-01-30 11:02:01 -07:00
|
|
|
}
|
2012-12-15 19:51:47 -07:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-03-02 14:22:11 -07:00
|
|
|
func (c *UnixConn) writeTo(b []byte, addr *UnixAddr) (int, error) {
|
2014-04-02 04:42:05 -06:00
|
|
|
if c.fd.isConnected {
|
2016-03-02 14:22:11 -07:00
|
|
|
return 0, ErrWriteToConnected
|
2014-04-02 04:42:05 -06:00
|
|
|
}
|
2013-08-18 04:19:36 -06:00
|
|
|
if addr == nil {
|
2016-03-02 14:22:11 -07:00
|
|
|
return 0, errMissingAddress
|
2013-08-18 04:19:36 -06:00
|
|
|
}
|
2012-01-19 15:31:13 -07:00
|
|
|
if addr.Net != sotypeToNet(c.fd.sotype) {
|
2016-03-02 14:22:11 -07:00
|
|
|
return 0, syscall.EAFNOSUPPORT
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
sa := &syscall.SockaddrUnix{Name: addr.Name}
|
2016-03-02 14:22:11 -07:00
|
|
|
return c.fd.writeTo(b, sa)
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
|
2016-03-02 14:22:11 -07:00
|
|
|
func (c *UnixConn) writeMsg(b, oob []byte, addr *UnixAddr) (n, oobn int, err error) {
|
2014-04-02 04:42:05 -06:00
|
|
|
if c.fd.sotype == syscall.SOCK_DGRAM && c.fd.isConnected {
|
2016-03-02 14:22:11 -07:00
|
|
|
return 0, 0, ErrWriteToConnected
|
2014-04-02 04:42:05 -06:00
|
|
|
}
|
2015-04-15 20:26:44 -06:00
|
|
|
var sa syscall.Sockaddr
|
2011-08-17 11:28:29 -06:00
|
|
|
if addr != nil {
|
2012-01-19 15:31:13 -07:00
|
|
|
if addr.Net != sotypeToNet(c.fd.sotype) {
|
2016-03-02 14:22:11 -07:00
|
|
|
return 0, 0, syscall.EAFNOSUPPORT
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
2015-04-15 20:26:44 -06:00
|
|
|
sa = &syscall.SockaddrUnix{Name: addr.Name}
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
2016-03-02 14:22:11 -07:00
|
|
|
return c.fd.writeMsg(b, oob, sa)
|
net: remove redundant argument check from Dial on udp, unix networks
The net package consists of thin three layers like the follwoing;
- Exposed API, that contains net.Dial, net.DialUDP, net.DialUnix
- Socket and network file descriptor, that contains net.netFD and
its methods, helper functions such as dialUDP, dialUnix
- Network pollster, that contains net.pollDesc and its methods
This CL removes redundant argument check which is already done by
API layer.
R=golang-dev, dave, bradfitz
CC=golang-dev
https://golang.org/cl/13092043
2013-08-21 19:33:06 -06:00
|
|
|
}
|
|
|
|
|
2016-04-14 18:47:25 -06:00
|
|
|
func dialUnix(ctx context.Context, net string, laddr, raddr *UnixAddr) (*UnixConn, error) {
|
|
|
|
fd, err := unixSocket(ctx, net, laddr, raddr, "dial")
|
2012-01-31 08:36:45 -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 newUnixConn(fd), nil
|
|
|
|
}
|
|
|
|
|
2016-03-02 14:22:11 -07:00
|
|
|
func (ln *UnixListener) accept() (*UnixConn, error) {
|
|
|
|
fd, err := ln.fd.accept()
|
2011-08-17 11:28:29 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-03-02 14:22:11 -07:00
|
|
|
return newUnixConn(fd), nil
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
|
2016-03-02 14:22:11 -07:00
|
|
|
func (ln *UnixListener) close() error {
|
2011-08-17 11:28:29 -06:00
|
|
|
// The operating system doesn't clean up
|
|
|
|
// the file that announcing created, so
|
|
|
|
// we have to clean it up ourselves.
|
|
|
|
// There's a race here--we can't know for
|
|
|
|
// sure whether someone else has come along
|
|
|
|
// and replaced our socket name already--
|
|
|
|
// but this sequence (remove then close)
|
|
|
|
// is at least compatible with the auto-remove
|
2016-03-01 16:21:55 -07:00
|
|
|
// sequence in ListenUnix. It's only non-Go
|
2011-08-17 11:28:29 -06:00
|
|
|
// programs that can mess us up.
|
2016-10-26 18:13:33 -06:00
|
|
|
// Even if there are racy calls to Close, we want to unlink only for the first one.
|
|
|
|
ln.unlinkOnce.Do(func() {
|
|
|
|
if ln.path[0] != '@' && ln.unlink {
|
|
|
|
syscall.Unlink(ln.path)
|
|
|
|
}
|
|
|
|
})
|
2016-03-02 14:22:11 -07:00
|
|
|
return ln.fd.Close()
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
|
2016-03-02 14:22:11 -07:00
|
|
|
func (ln *UnixListener) file() (*os.File, error) {
|
|
|
|
f, err := ln.fd.dup()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2015-04-19 04:01:49 -06:00
|
|
|
}
|
2016-03-02 14:22:11 -07:00
|
|
|
return f, nil
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
|
|
|
|
2016-10-26 18:23:03 -06:00
|
|
|
// SetUnlinkOnClose sets whether the underlying socket file should be removed
|
|
|
|
// from the file system when the listener is closed.
|
|
|
|
//
|
|
|
|
// The default behavior is to unlink the socket file only when package net created it.
|
|
|
|
// That is, when the listener and the underlying socket file were created by a call to
|
|
|
|
// Listen or ListenUnix, then by default closing the listener will remove the socket file.
|
|
|
|
// but if the listener was created by a call to FileListener to use an already existing
|
|
|
|
// socket file, then by default closing the listener will not remove the socket file.
|
|
|
|
func (l *UnixListener) SetUnlinkOnClose(unlink bool) {
|
|
|
|
l.unlink = unlink
|
|
|
|
}
|
|
|
|
|
2016-04-14 18:47:25 -06:00
|
|
|
func listenUnix(ctx context.Context, network string, laddr *UnixAddr) (*UnixListener, error) {
|
|
|
|
fd, err := unixSocket(ctx, network, laddr, nil, "listen")
|
2015-04-18 01:53:55 -06:00
|
|
|
if err != nil {
|
2016-03-02 14:22:11 -07:00
|
|
|
return nil, err
|
2015-04-18 01:53:55 -06:00
|
|
|
}
|
2016-03-02 14:22:11 -07:00
|
|
|
return &UnixListener{fd: fd, path: fd.laddr.String(), unlink: true}, nil
|
2015-04-18 01:53:55 -06:00
|
|
|
}
|
2011-08-17 11:28:29 -06:00
|
|
|
|
2016-04-14 18:47:25 -06:00
|
|
|
func listenUnixgram(ctx context.Context, network string, laddr *UnixAddr) (*UnixConn, error) {
|
|
|
|
fd, err := unixSocket(ctx, network, laddr, nil, "listen")
|
2012-01-31 08:36:45 -07:00
|
|
|
if err != nil {
|
2016-03-02 14:22:11 -07:00
|
|
|
return nil, err
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|
2012-12-15 19:51:47 -07:00
|
|
|
return newUnixConn(fd), nil
|
2011-08-17 11:28:29 -06:00
|
|
|
}
|