2010-12-03 05:34:57 +01:00
|
|
|
// Copyright 2009 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
|
|
|
|
2010-12-03 05:34:57 +01:00
|
|
|
// Sockets
|
|
|
|
|
|
|
|
package net
|
|
|
|
|
|
|
|
import (
|
2011-09-16 17:47:21 +02:00
|
|
|
"io"
|
2010-12-03 05:34:57 +01:00
|
|
|
"syscall"
|
|
|
|
)
|
|
|
|
|
2012-01-25 21:56:26 +01:00
|
|
|
var listenerBacklog = maxListenerBacklog()
|
2010-12-03 05:34:57 +01:00
|
|
|
|
|
|
|
// Generic socket creation.
|
2012-10-23 06:31:11 +02:00
|
|
|
func socket(net string, f, t, p int, ipv6only bool, ulsa, ursa syscall.Sockaddr, toAddr func(syscall.Sockaddr) Addr) (fd *netFD, err error) {
|
|
|
|
// See ../syscall/exec_unix.go for description of ForkLock.
|
2010-12-03 05:34:57 +01:00
|
|
|
syscall.ForkLock.RLock()
|
2012-01-25 22:54:22 +01:00
|
|
|
s, err := syscall.Socket(f, t, p)
|
2011-12-13 00:40:51 +01:00
|
|
|
if err != nil {
|
2010-12-03 05:34:57 +01:00
|
|
|
syscall.ForkLock.RUnlock()
|
2011-12-13 00:40:51 +01:00
|
|
|
return nil, err
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
syscall.CloseOnExec(s)
|
|
|
|
syscall.ForkLock.RUnlock()
|
|
|
|
|
2012-10-23 06:31:11 +02:00
|
|
|
if err = setDefaultSockopts(s, f, t, ipv6only); err != nil {
|
2012-02-09 09:19:58 +01:00
|
|
|
closesocket(s)
|
|
|
|
return nil, err
|
|
|
|
}
|
2010-12-03 05:34:57 +01:00
|
|
|
|
2012-10-23 06:31:11 +02:00
|
|
|
var blsa syscall.Sockaddr
|
|
|
|
if ulsa != nil {
|
|
|
|
if blsa, err = listenerSockaddr(s, f, ulsa, toAddr); err != nil {
|
2012-02-09 09:19:58 +01:00
|
|
|
closesocket(s)
|
|
|
|
return nil, err
|
|
|
|
}
|
2012-10-23 06:31:11 +02:00
|
|
|
if err = syscall.Bind(s, blsa); err != nil {
|
2011-01-21 19:19:03 +01:00
|
|
|
closesocket(s)
|
2012-01-25 22:54:22 +01:00
|
|
|
return nil, err
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-25 22:54:22 +01:00
|
|
|
if fd, err = newFD(s, f, t, net); err != nil {
|
2011-03-30 17:33:16 +02:00
|
|
|
closesocket(s)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2012-10-23 06:31:11 +02:00
|
|
|
if ursa != nil {
|
|
|
|
if err = fd.connect(ursa); err != nil {
|
2011-10-27 01:57:58 +02:00
|
|
|
closesocket(s)
|
2011-09-16 17:47:21 +02:00
|
|
|
fd.Close()
|
2011-03-30 17:33:16 +02:00
|
|
|
return nil, err
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2012-02-01 20:26:59 +01:00
|
|
|
fd.isConnected = true
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
2012-10-23 06:31:11 +02:00
|
|
|
lsa, _ := syscall.Getsockname(s)
|
2012-03-02 21:01:37 +01:00
|
|
|
var laddr Addr
|
2012-10-23 06:31:11 +02:00
|
|
|
if ulsa != nil && blsa != ulsa {
|
|
|
|
laddr = toAddr(ulsa)
|
2012-03-02 21:01:37 +01:00
|
|
|
} else {
|
2012-10-23 06:31:11 +02:00
|
|
|
laddr = toAddr(lsa)
|
2012-03-02 21:01:37 +01:00
|
|
|
}
|
2012-10-23 06:31:11 +02:00
|
|
|
rsa, _ := syscall.Getpeername(s)
|
|
|
|
raddr := toAddr(rsa)
|
2011-03-30 17:33:16 +02:00
|
|
|
fd.setAddr(laddr, raddr)
|
2010-12-03 05:34:57 +01:00
|
|
|
return fd, nil
|
|
|
|
}
|
|
|
|
|
2011-09-16 17:47:21 +02:00
|
|
|
type writerOnly struct {
|
|
|
|
io.Writer
|
|
|
|
}
|
2010-12-03 05:34:57 +01:00
|
|
|
|
2011-09-16 17:47:21 +02:00
|
|
|
// Fallback implementation of io.ReaderFrom's ReadFrom, when sendfile isn't
|
|
|
|
// applicable.
|
2011-12-03 03:17:34 +01:00
|
|
|
func genericReadFrom(w io.Writer, r io.Reader) (n int64, err error) {
|
2011-09-16 17:47:21 +02:00
|
|
|
// Use wrapper to hide existing r.ReadFrom from io.Copy.
|
|
|
|
return io.Copy(writerOnly{w}, r)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|