mirror of
https://github.com/golang/go
synced 2024-10-04 15:11:20 -06:00
055ecb7be5
These a series of changes fix inconsistent errors on the package net APIs. Now almost all the APIs return OpError as a common error type except Lookup, Resolve and Parse APIs. The Lookup, Resolve and Parse APIs return more specific errors such as DNSError, AddrError or ParseError. An OpError may contain nested error information. For example, Dial may return an OpError containing a DNSError, AddrError, unexposed type/value or other package's type/value like the following: OpError{/* dial info */, Err: &DNSError{}} OpError{/* dial info */, Err: &AddrError{}} OpError{/* dial info */, Err: <unexposed type or value>} OpError{/* dial info */, Err: <other package's type or value>} and Read and Write may return an OpError containing other OpError when an application uses io.Copy or similar: OpError{/* for io.Reader */, Err: &OpError{/* for io.Writer */}} When an endpoint is created for connection-oriented byte-stream protocols, Read may return an io.EOF when the connection is closed by remote endpoint. Fixes #4856. A series of changes: - net: fix inconsistent error values on Dial, Listen partially https://go.googlesource.com/go/+/89b7c66d0d14462fd7893be4290bdfe5f9063ae1 - net: fix inconsistent error values on Read https://go.googlesource.com/go/+/ec1144423f45e010c72363fe59291d43214b6e31 - net: fix inconsistent error values on Write https://go.googlesource.com/go/+/11b5f98bf0d5eb8854f735cc332c912725070214 - net: fix inconsistent error values on Close https://go.googlesource.com/go/+/310db63c5bc121e7bfccb494c01a6b91a257e7fc - net: fix inconsistent error values on Accept https://go.googlesource.com/go/+/4540e162b1aefda8157372764ad3d290a414ef1d - net: fix inconsistent error values on File https://go.googlesource.com/go/+/885111365ba0a74421059bfbd18f4c57c1e70332 - net: fix inconsistent error values on setters https://go.googlesource.com/go/+/2173a27903897c481b0a0daf3ca3e0a0685701db - net: fix inconsistent error values on Interface https://go.googlesource.com/go/+/456cf0f22c93e1a6654980f4a48a564555f6c8a2 - net: fix inconsistent error values on Lookup https://go.googlesource.com/go/+/0fc582e87942b2e52bed751b6c56660ba99e9a7d - net: add Source field to OpError https://go.googlesource.com/go/+/afd2d2b6df3ebfe99faf347030f15adfdf422fa0 Change-Id: Id678e369088dc9fbe9073cfe7ff8a8754a57d61f Reviewed-on: https://go-review.googlesource.com/9236 Reviewed-by: Ian Lance Taylor <iant@golang.org>
229 lines
4.5 KiB
Go
229 lines
4.5 KiB
Go
// 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.
|
|
|
|
package net
|
|
|
|
import (
|
|
"io"
|
|
"os"
|
|
"syscall"
|
|
"time"
|
|
)
|
|
|
|
// Network file descriptor.
|
|
type netFD struct {
|
|
// locking/lifetime of sysfd + serialize access to Read and Write methods
|
|
fdmu fdMutex
|
|
|
|
// immutable until Close
|
|
net string
|
|
n string
|
|
dir string
|
|
ctl, data *os.File
|
|
laddr, raddr Addr
|
|
}
|
|
|
|
var (
|
|
netdir string // default network
|
|
)
|
|
|
|
func sysInit() {
|
|
netdir = "/net"
|
|
}
|
|
|
|
func dial(net string, ra Addr, dialer func(time.Time) (Conn, error), deadline time.Time) (Conn, error) {
|
|
// On plan9, use the relatively inefficient
|
|
// goroutine-racing implementation.
|
|
return dialChannel(net, ra, dialer, deadline)
|
|
}
|
|
|
|
func newFD(net, name string, ctl, data *os.File, laddr, raddr Addr) (*netFD, error) {
|
|
return &netFD{net: net, n: name, dir: netdir + "/" + net + "/" + name, ctl: ctl, data: data, laddr: laddr, raddr: raddr}, nil
|
|
}
|
|
|
|
func (fd *netFD) init() error {
|
|
// stub for future fd.pd.Init(fd)
|
|
return nil
|
|
}
|
|
|
|
func (fd *netFD) name() string {
|
|
var ls, rs string
|
|
if fd.laddr != nil {
|
|
ls = fd.laddr.String()
|
|
}
|
|
if fd.raddr != nil {
|
|
rs = fd.raddr.String()
|
|
}
|
|
return fd.net + ":" + ls + "->" + rs
|
|
}
|
|
|
|
func (fd *netFD) ok() bool { return fd != nil && fd.ctl != nil }
|
|
|
|
func (fd *netFD) destroy() {
|
|
if !fd.ok() {
|
|
return
|
|
}
|
|
err := fd.ctl.Close()
|
|
if fd.data != nil {
|
|
if err1 := fd.data.Close(); err1 != nil && err == nil {
|
|
err = err1
|
|
}
|
|
}
|
|
fd.ctl = nil
|
|
fd.data = nil
|
|
}
|
|
|
|
// Add a reference to this fd.
|
|
// Returns an error if the fd cannot be used.
|
|
func (fd *netFD) incref() error {
|
|
if !fd.fdmu.Incref() {
|
|
return errClosing
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Remove a reference to this FD and close if we've been asked to do so
|
|
// (and there are no references left).
|
|
func (fd *netFD) decref() {
|
|
if fd.fdmu.Decref() {
|
|
fd.destroy()
|
|
}
|
|
}
|
|
|
|
// Add a reference to this fd and lock for reading.
|
|
// Returns an error if the fd cannot be used.
|
|
func (fd *netFD) readLock() error {
|
|
if !fd.fdmu.RWLock(true) {
|
|
return errClosing
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Unlock for reading and remove a reference to this FD.
|
|
func (fd *netFD) readUnlock() {
|
|
if fd.fdmu.RWUnlock(true) {
|
|
fd.destroy()
|
|
}
|
|
}
|
|
|
|
// Add a reference to this fd and lock for writing.
|
|
// Returns an error if the fd cannot be used.
|
|
func (fd *netFD) writeLock() error {
|
|
if !fd.fdmu.RWLock(false) {
|
|
return errClosing
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Unlock for writing and remove a reference to this FD.
|
|
func (fd *netFD) writeUnlock() {
|
|
if fd.fdmu.RWUnlock(false) {
|
|
fd.destroy()
|
|
}
|
|
}
|
|
|
|
func (fd *netFD) Read(b []byte) (n int, err error) {
|
|
if !fd.ok() || fd.data == nil {
|
|
return 0, syscall.EINVAL
|
|
}
|
|
if err := fd.readLock(); err != nil {
|
|
return 0, err
|
|
}
|
|
defer fd.readUnlock()
|
|
n, err = fd.data.Read(b)
|
|
if fd.net == "udp" && err == io.EOF {
|
|
n = 0
|
|
err = nil
|
|
}
|
|
return
|
|
}
|
|
|
|
func (fd *netFD) Write(b []byte) (n int, err error) {
|
|
if !fd.ok() || fd.data == nil {
|
|
return 0, syscall.EINVAL
|
|
}
|
|
if err := fd.writeLock(); err != nil {
|
|
return 0, err
|
|
}
|
|
defer fd.writeUnlock()
|
|
return fd.data.Write(b)
|
|
}
|
|
|
|
func (fd *netFD) closeRead() error {
|
|
if !fd.ok() {
|
|
return syscall.EINVAL
|
|
}
|
|
return syscall.EPLAN9
|
|
}
|
|
|
|
func (fd *netFD) closeWrite() error {
|
|
if !fd.ok() {
|
|
return syscall.EINVAL
|
|
}
|
|
return syscall.EPLAN9
|
|
}
|
|
|
|
func (fd *netFD) Close() error {
|
|
if !fd.fdmu.IncrefAndClose() {
|
|
return errClosing
|
|
}
|
|
if !fd.ok() {
|
|
return syscall.EINVAL
|
|
}
|
|
err := fd.ctl.Close()
|
|
if fd.data != nil {
|
|
if err1 := fd.data.Close(); err1 != nil && err == nil {
|
|
err = err1
|
|
}
|
|
}
|
|
fd.ctl = nil
|
|
fd.data = nil
|
|
return err
|
|
}
|
|
|
|
// This method is only called via Conn.
|
|
func (fd *netFD) dup() (*os.File, error) {
|
|
if !fd.ok() || fd.data == nil {
|
|
return nil, syscall.EINVAL
|
|
}
|
|
return fd.file(fd.data, fd.dir+"/data")
|
|
}
|
|
|
|
func (l *TCPListener) dup() (*os.File, error) {
|
|
if !l.fd.ok() {
|
|
return nil, syscall.EINVAL
|
|
}
|
|
return l.fd.file(l.fd.ctl, l.fd.dir+"/ctl")
|
|
}
|
|
|
|
func (fd *netFD) file(f *os.File, s string) (*os.File, error) {
|
|
syscall.ForkLock.RLock()
|
|
dfd, err := syscall.Dup(int(f.Fd()), -1)
|
|
syscall.ForkLock.RUnlock()
|
|
if err != nil {
|
|
return nil, os.NewSyscallError("dup", err)
|
|
}
|
|
return os.NewFile(uintptr(dfd), s), nil
|
|
}
|
|
|
|
func (fd *netFD) setDeadline(t time.Time) error {
|
|
return syscall.EPLAN9
|
|
}
|
|
|
|
func (fd *netFD) setReadDeadline(t time.Time) error {
|
|
return syscall.EPLAN9
|
|
}
|
|
|
|
func (fd *netFD) setWriteDeadline(t time.Time) error {
|
|
return syscall.EPLAN9
|
|
}
|
|
|
|
func setReadBuffer(fd *netFD, bytes int) error {
|
|
return syscall.EPLAN9
|
|
}
|
|
|
|
func setWriteBuffer(fd *netFD, bytes int) error {
|
|
return syscall.EPLAN9
|
|
}
|