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
|
|
|
"os"
|
|
|
|
"reflect"
|
|
|
|
"syscall"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Boolean to int.
|
|
|
|
func boolint(b bool) int {
|
|
|
|
if b {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generic socket creation.
|
2011-12-03 03:17:34 +01:00
|
|
|
func socket(net string, f, p, t 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()
|
|
|
|
s, e := syscall.Socket(f, p, t)
|
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()
|
|
|
|
|
2011-05-20 02:18:15 +02:00
|
|
|
setKernelSpecificSockopt(s, f)
|
2010-12-03 05:34:57 +01:00
|
|
|
|
|
|
|
if la != nil {
|
|
|
|
e = syscall.Bind(s, la)
|
2011-12-13 00:40:51 +01:00
|
|
|
if e != nil {
|
2011-01-21 19:19:03 +01:00
|
|
|
closesocket(s)
|
2011-12-13 00:40:51 +01:00
|
|
|
return nil, e
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-30 17:33:16 +02:00
|
|
|
if fd, err = newFD(s, f, p, net); err != nil {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2011-12-03 03:17:34 +01:00
|
|
|
func setsockoptInt(fd *netFD, level, opt int, value int) error {
|
2011-09-16 17:47:21 +02:00
|
|
|
return os.NewSyscallError("setsockopt", syscall.SetsockoptInt(fd.sysfd, level, opt, value))
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
2011-12-03 03:17:34 +01:00
|
|
|
func setsockoptNsec(fd *netFD, level, opt int, nsec int64) error {
|
2010-12-03 05:34:57 +01:00
|
|
|
var tv = syscall.NsecToTimeval(nsec)
|
2011-09-16 17:47:21 +02:00
|
|
|
return os.NewSyscallError("setsockopt", syscall.SetsockoptTimeval(fd.sysfd, level, opt, &tv))
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
2011-12-03 03:17:34 +01:00
|
|
|
func setReadBuffer(fd *netFD, bytes int) error {
|
2010-12-03 05:34:57 +01:00
|
|
|
fd.incref()
|
|
|
|
defer fd.decref()
|
2011-09-16 17:47:21 +02:00
|
|
|
return setsockoptInt(fd, syscall.SOL_SOCKET, syscall.SO_RCVBUF, bytes)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
2011-12-03 03:17:34 +01:00
|
|
|
func setWriteBuffer(fd *netFD, bytes int) error {
|
2010-12-03 05:34:57 +01:00
|
|
|
fd.incref()
|
|
|
|
defer fd.decref()
|
2011-09-16 17:47:21 +02:00
|
|
|
return setsockoptInt(fd, syscall.SOL_SOCKET, syscall.SO_SNDBUF, bytes)
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
2011-12-03 03:17:34 +01:00
|
|
|
func setReadTimeout(fd *netFD, nsec int64) error {
|
2010-12-03 05:34:57 +01:00
|
|
|
fd.rdeadline_delta = nsec
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2011-12-03 03:17:34 +01:00
|
|
|
func setWriteTimeout(fd *netFD, nsec int64) error {
|
2010-12-03 05:34:57 +01:00
|
|
|
fd.wdeadline_delta = nsec
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2011-12-03 03:17:34 +01:00
|
|
|
func setTimeout(fd *netFD, nsec int64) error {
|
2010-12-03 05:34:57 +01:00
|
|
|
if e := setReadTimeout(fd, nsec); e != nil {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
return setWriteTimeout(fd, nsec)
|
|
|
|
}
|
|
|
|
|
2011-12-03 03:17:34 +01:00
|
|
|
func setReuseAddr(fd *netFD, reuse bool) error {
|
2010-12-03 05:34:57 +01:00
|
|
|
fd.incref()
|
|
|
|
defer fd.decref()
|
2011-09-16 17:47:21 +02:00
|
|
|
return setsockoptInt(fd, syscall.SOL_SOCKET, syscall.SO_REUSEADDR, boolint(reuse))
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
2011-12-03 03:17:34 +01:00
|
|
|
func setDontRoute(fd *netFD, dontroute bool) error {
|
2010-12-03 05:34:57 +01:00
|
|
|
fd.incref()
|
|
|
|
defer fd.decref()
|
2011-09-16 17:47:21 +02:00
|
|
|
return setsockoptInt(fd, syscall.SOL_SOCKET, syscall.SO_DONTROUTE, boolint(dontroute))
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
2011-12-03 03:17:34 +01:00
|
|
|
func setKeepAlive(fd *netFD, keepalive bool) error {
|
2010-12-03 05:34:57 +01:00
|
|
|
fd.incref()
|
|
|
|
defer fd.decref()
|
2011-09-16 17:47:21 +02:00
|
|
|
return setsockoptInt(fd, syscall.SOL_SOCKET, syscall.SO_KEEPALIVE, boolint(keepalive))
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
2011-12-03 03:17:34 +01:00
|
|
|
func setNoDelay(fd *netFD, noDelay bool) error {
|
2010-12-03 05:34:57 +01:00
|
|
|
fd.incref()
|
|
|
|
defer fd.decref()
|
2011-09-16 17:47:21 +02:00
|
|
|
return setsockoptInt(fd, syscall.IPPROTO_TCP, syscall.TCP_NODELAY, boolint(noDelay))
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
2011-12-03 03:17:34 +01:00
|
|
|
func setLinger(fd *netFD, sec int) error {
|
2010-12-03 05:34:57 +01:00
|
|
|
var l syscall.Linger
|
|
|
|
if sec >= 0 {
|
|
|
|
l.Onoff = 1
|
|
|
|
l.Linger = int32(sec)
|
|
|
|
} else {
|
|
|
|
l.Onoff = 0
|
|
|
|
l.Linger = 0
|
|
|
|
}
|
|
|
|
fd.incref()
|
|
|
|
defer fd.decref()
|
|
|
|
e := syscall.SetsockoptLinger(fd.sysfd, syscall.SOL_SOCKET, syscall.SO_LINGER, &l)
|
|
|
|
return os.NewSyscallError("setsockopt", e)
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|