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
|
|
|
"reflect"
|
|
|
|
"syscall"
|
|
|
|
)
|
|
|
|
|
2012-01-25 21:56:26 +01:00
|
|
|
var listenerBacklog = maxListenerBacklog()
|
2010-12-03 05:34:57 +01:00
|
|
|
|
|
|
|
// Generic socket creation.
|
2012-01-25 22:54:22 +01:00
|
|
|
func socket(net string, f, t, p int, la, ra syscall.Sockaddr, toAddr func(syscall.Sockaddr) Addr) (fd *netFD, err error) {
|
2010-12-03 05:34:57 +01:00
|
|
|
// See ../syscall/exec.go for description of ForkLock.
|
|
|
|
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-01-25 22:54:22 +01:00
|
|
|
setDefaultSockopts(s, f, t)
|
2010-12-03 05:34:57 +01:00
|
|
|
|
|
|
|
if la != nil {
|
2012-01-25 22:54:22 +01:00
|
|
|
err = syscall.Bind(s, la)
|
|
|
|
if 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
|
|
|
|
}
|
|
|
|
|
2010-12-03 05:34:57 +01:00
|
|
|
if ra != nil {
|
2011-03-30 17:33:16 +02:00
|
|
|
if err = fd.connect(ra); 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sa, _ := syscall.Getsockname(s)
|
|
|
|
laddr := toAddr(sa)
|
|
|
|
sa, _ = syscall.Getpeername(s)
|
|
|
|
raddr := toAddr(sa)
|
|
|
|
|
2011-03-30 17:33:16 +02:00
|
|
|
fd.setAddr(laddr, raddr)
|
2010-12-03 05:34:57 +01:00
|
|
|
return fd, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type UnknownSocketError struct {
|
|
|
|
sa syscall.Sockaddr
|
|
|
|
}
|
|
|
|
|
2011-12-03 03:17:34 +01:00
|
|
|
func (e *UnknownSocketError) Error() string {
|
2011-05-20 02:18:15 +02:00
|
|
|
return "unknown socket address type " + reflect.TypeOf(e.sa).String()
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|