2011-03-30 17:33:16 +02:00
|
|
|
// Copyright 2011 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
|
2011-10-27 01:57:58 +02:00
|
|
|
|
2011-03-30 17:33:16 +02:00
|
|
|
package net
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"syscall"
|
|
|
|
)
|
|
|
|
|
2012-02-09 09:19:58 +01:00
|
|
|
func newFileFD(f *os.File) (*netFD, error) {
|
2012-03-02 17:38:43 +01:00
|
|
|
fd, err := syscall.Dup(int(f.Fd()))
|
2012-02-09 09:19:58 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, os.NewSyscallError("dup", err)
|
2011-03-30 17:33:16 +02:00
|
|
|
}
|
|
|
|
|
2012-02-09 09:19:58 +01:00
|
|
|
proto, err := syscall.GetsockoptInt(fd, syscall.SOL_SOCKET, syscall.SO_TYPE)
|
|
|
|
if err != nil {
|
|
|
|
return nil, os.NewSyscallError("getsockopt", err)
|
2011-03-30 17:33:16 +02:00
|
|
|
}
|
|
|
|
|
2011-10-27 01:57:58 +02:00
|
|
|
family := syscall.AF_UNSPEC
|
2011-03-30 17:33:16 +02:00
|
|
|
toAddr := sockaddrToTCP
|
|
|
|
sa, _ := syscall.Getsockname(fd)
|
|
|
|
switch sa.(type) {
|
|
|
|
default:
|
|
|
|
closesocket(fd)
|
2012-03-02 21:01:37 +01:00
|
|
|
return nil, syscall.EINVAL
|
2011-03-30 17:33:16 +02:00
|
|
|
case *syscall.SockaddrInet4:
|
2011-10-27 01:57:58 +02:00
|
|
|
family = syscall.AF_INET
|
2011-03-30 17:33:16 +02:00
|
|
|
if proto == syscall.SOCK_DGRAM {
|
|
|
|
toAddr = sockaddrToUDP
|
|
|
|
} else if proto == syscall.SOCK_RAW {
|
|
|
|
toAddr = sockaddrToIP
|
|
|
|
}
|
|
|
|
case *syscall.SockaddrInet6:
|
2011-10-27 01:57:58 +02:00
|
|
|
family = syscall.AF_INET6
|
2011-03-30 17:33:16 +02:00
|
|
|
if proto == syscall.SOCK_DGRAM {
|
|
|
|
toAddr = sockaddrToUDP
|
|
|
|
} else if proto == syscall.SOCK_RAW {
|
|
|
|
toAddr = sockaddrToIP
|
|
|
|
}
|
|
|
|
case *syscall.SockaddrUnix:
|
2011-10-27 01:57:58 +02:00
|
|
|
family = syscall.AF_UNIX
|
2011-03-30 17:33:16 +02:00
|
|
|
toAddr = sockaddrToUnix
|
|
|
|
if proto == syscall.SOCK_DGRAM {
|
|
|
|
toAddr = sockaddrToUnixgram
|
|
|
|
} else if proto == syscall.SOCK_SEQPACKET {
|
|
|
|
toAddr = sockaddrToUnixpacket
|
|
|
|
}
|
|
|
|
}
|
|
|
|
laddr := toAddr(sa)
|
|
|
|
sa, _ = syscall.Getpeername(fd)
|
|
|
|
raddr := toAddr(sa)
|
|
|
|
|
2012-02-09 09:19:58 +01:00
|
|
|
netfd, err := newFD(fd, family, proto, laddr.Network())
|
|
|
|
if err != nil {
|
2011-03-30 17:33:16 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
2012-02-09 09:19:58 +01:00
|
|
|
netfd.setAddr(laddr, raddr)
|
|
|
|
return netfd, nil
|
2011-03-30 17:33:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2011-12-03 03:17:34 +01:00
|
|
|
func FileConn(f *os.File) (c Conn, err error) {
|
2011-03-30 17:33:16 +02:00
|
|
|
fd, err := newFileFD(f)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
switch fd.laddr.(type) {
|
|
|
|
case *TCPAddr:
|
|
|
|
return newTCPConn(fd), nil
|
|
|
|
case *UDPAddr:
|
|
|
|
return newUDPConn(fd), nil
|
|
|
|
case *UnixAddr:
|
|
|
|
return newUnixConn(fd), nil
|
|
|
|
case *IPAddr:
|
|
|
|
return newIPConn(fd), nil
|
|
|
|
}
|
|
|
|
fd.Close()
|
2012-03-02 21:01:37 +01:00
|
|
|
return nil, syscall.EINVAL
|
2011-03-30 17:33:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2011-12-03 03:17:34 +01:00
|
|
|
func FileListener(f *os.File) (l Listener, err error) {
|
2011-03-30 17:33:16 +02:00
|
|
|
fd, err := newFileFD(f)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
switch laddr := fd.laddr.(type) {
|
|
|
|
case *TCPAddr:
|
|
|
|
return &TCPListener{fd}, nil
|
|
|
|
case *UnixAddr:
|
|
|
|
return &UnixListener{fd, laddr.Name}, nil
|
|
|
|
}
|
|
|
|
fd.Close()
|
2012-03-02 21:01:37 +01:00
|
|
|
return nil, syscall.EINVAL
|
2011-03-30 17:33:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2011-12-03 03:17:34 +01:00
|
|
|
func FilePacketConn(f *os.File) (c PacketConn, err error) {
|
2011-03-30 17:33:16 +02:00
|
|
|
fd, err := newFileFD(f)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
switch fd.laddr.(type) {
|
|
|
|
case *UDPAddr:
|
|
|
|
return newUDPConn(fd), nil
|
|
|
|
case *UnixAddr:
|
|
|
|
return newUnixConn(fd), nil
|
|
|
|
}
|
|
|
|
fd.Close()
|
2012-03-02 21:01:37 +01:00
|
|
|
return nil, syscall.EINVAL
|
2011-03-30 17:33:16 +02:00
|
|
|
}
|