diff options
Diffstat (limited to 'src/pkg/net/file.go')
-rw-r--r-- | src/pkg/net/file.go | 41 |
1 files changed, 24 insertions, 17 deletions
diff --git a/src/pkg/net/file.go b/src/pkg/net/file.go index 0e411a192..c95d16d64 100644 --- a/src/pkg/net/file.go +++ b/src/pkg/net/file.go @@ -2,6 +2,8 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. +// +build darwin freebsd linux netbsd openbsd + package net import ( @@ -9,36 +11,40 @@ import ( "syscall" ) -func newFileFD(f *os.File) (nfd *netFD, err os.Error) { - fd, errno := syscall.Dup(f.Fd()) - if errno != 0 { - return nil, os.NewSyscallError("dup", errno) +func newFileFD(f *os.File) (*netFD, error) { + fd, err := syscall.Dup(int(f.Fd())) + if err != nil { + return nil, os.NewSyscallError("dup", err) } - proto, errno := syscall.GetsockoptInt(fd, syscall.SOL_SOCKET, syscall.SO_TYPE) - if errno != 0 { - return nil, os.NewSyscallError("getsockopt", errno) + proto, err := syscall.GetsockoptInt(fd, syscall.SOL_SOCKET, syscall.SO_TYPE) + if err != nil { + return nil, os.NewSyscallError("getsockopt", err) } + family := syscall.AF_UNSPEC toAddr := sockaddrToTCP sa, _ := syscall.Getsockname(fd) switch sa.(type) { default: closesocket(fd) - return nil, os.EINVAL + return nil, syscall.EINVAL case *syscall.SockaddrInet4: + family = syscall.AF_INET if proto == syscall.SOCK_DGRAM { toAddr = sockaddrToUDP } else if proto == syscall.SOCK_RAW { toAddr = sockaddrToIP } case *syscall.SockaddrInet6: + family = syscall.AF_INET6 if proto == syscall.SOCK_DGRAM { toAddr = sockaddrToUDP } else if proto == syscall.SOCK_RAW { toAddr = sockaddrToIP } case *syscall.SockaddrUnix: + family = syscall.AF_UNIX toAddr = sockaddrToUnix if proto == syscall.SOCK_DGRAM { toAddr = sockaddrToUnixgram @@ -50,18 +56,19 @@ func newFileFD(f *os.File) (nfd *netFD, err os.Error) { sa, _ = syscall.Getpeername(fd) raddr := toAddr(sa) - if nfd, err = newFD(fd, 0, proto, laddr.Network()); err != nil { + netfd, err := newFD(fd, family, proto, laddr.Network()) + if err != nil { return nil, err } - nfd.setAddr(laddr, raddr) - return nfd, nil + netfd.setAddr(laddr, raddr) + return netfd, nil } // FileConn returns a copy of the network connection corresponding to // the open file f. It is the caller's responsibility to close f when // finished. Closing c does not affect f, and closing f does not // affect c. -func FileConn(f *os.File) (c Conn, err os.Error) { +func FileConn(f *os.File) (c Conn, err error) { fd, err := newFileFD(f) if err != nil { return nil, err @@ -77,14 +84,14 @@ func FileConn(f *os.File) (c Conn, err os.Error) { return newIPConn(fd), nil } fd.Close() - return nil, os.EINVAL + return nil, syscall.EINVAL } // FileListener returns a copy of the network listener corresponding // to the open file f. It is the caller's responsibility to close l // when finished. Closing c does not affect l, and closing l does not // affect c. -func FileListener(f *os.File) (l Listener, err os.Error) { +func FileListener(f *os.File) (l Listener, err error) { fd, err := newFileFD(f) if err != nil { return nil, err @@ -96,14 +103,14 @@ func FileListener(f *os.File) (l Listener, err os.Error) { return &UnixListener{fd, laddr.Name}, nil } fd.Close() - return nil, os.EINVAL + return nil, syscall.EINVAL } // FilePacketConn returns a copy of the packet network connection // corresponding to the open file f. It is the caller's // responsibility to close f when finished. Closing c does not affect // f, and closing f does not affect c. -func FilePacketConn(f *os.File) (c PacketConn, err os.Error) { +func FilePacketConn(f *os.File) (c PacketConn, err error) { fd, err := newFileFD(f) if err != nil { return nil, err @@ -115,5 +122,5 @@ func FilePacketConn(f *os.File) (c PacketConn, err os.Error) { return newUnixConn(fd), nil } fd.Close() - return nil, os.EINVAL + return nil, syscall.EINVAL } |