mirror of
https://github.com/golang/go
synced 2024-11-26 14:46:47 -07:00
syscall: reduce duplication between *bsd and linux
See discussion: https://groups.google.com/forum/#!topic/golang-dev/zSmH0lQxKAs Part 1 of several. Move identical types and functions to syscall_unix.go. R=rsc, mikioh.mikioh, r CC=golang-dev https://golang.org/cl/10392048
This commit is contained in:
parent
de47f68c99
commit
dd3a3cfa49
@ -151,20 +151,6 @@ func Wait4(pid int, wstatus *WaitStatus, options int, rusage *Rusage) (wpid int,
|
||||
//sysnb getsockname(fd int, rsa *RawSockaddrAny, addrlen *_Socklen) (err error)
|
||||
//sys Shutdown(s int, how int) (err error)
|
||||
|
||||
// For testing: clients can set this flag to force
|
||||
// creation of IPv6 sockets to return EAFNOSUPPORT.
|
||||
var SocketDisableIPv6 bool
|
||||
|
||||
type Sockaddr interface {
|
||||
sockaddr() (ptr uintptr, len _Socklen, err error) // lowercase; only we can define Sockaddrs
|
||||
}
|
||||
|
||||
type SockaddrInet4 struct {
|
||||
Port int
|
||||
Addr [4]byte
|
||||
raw RawSockaddrInet4
|
||||
}
|
||||
|
||||
func (sa *SockaddrInet4) sockaddr() (uintptr, _Socklen, error) {
|
||||
if sa.Port < 0 || sa.Port > 0xFFFF {
|
||||
return 0, 0, EINVAL
|
||||
@ -180,13 +166,6 @@ func (sa *SockaddrInet4) sockaddr() (uintptr, _Socklen, error) {
|
||||
return uintptr(unsafe.Pointer(&sa.raw)), _Socklen(sa.raw.Len), nil
|
||||
}
|
||||
|
||||
type SockaddrInet6 struct {
|
||||
Port int
|
||||
ZoneId uint32
|
||||
Addr [16]byte
|
||||
raw RawSockaddrInet6
|
||||
}
|
||||
|
||||
func (sa *SockaddrInet6) sockaddr() (uintptr, _Socklen, error) {
|
||||
if sa.Port < 0 || sa.Port > 0xFFFF {
|
||||
return 0, 0, EINVAL
|
||||
@ -203,11 +182,6 @@ func (sa *SockaddrInet6) sockaddr() (uintptr, _Socklen, error) {
|
||||
return uintptr(unsafe.Pointer(&sa.raw)), _Socklen(sa.raw.Len), nil
|
||||
}
|
||||
|
||||
type SockaddrUnix struct {
|
||||
Name string
|
||||
raw RawSockaddrUnix
|
||||
}
|
||||
|
||||
func (sa *SockaddrUnix) sockaddr() (uintptr, _Socklen, error) {
|
||||
name := sa.Name
|
||||
n := len(name)
|
||||
@ -344,42 +318,8 @@ func Getpeername(fd int) (sa Sockaddr, err error) {
|
||||
return anyToSockaddr(&rsa)
|
||||
}
|
||||
|
||||
func Bind(fd int, sa Sockaddr) (err error) {
|
||||
ptr, n, err := sa.sockaddr()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return bind(fd, ptr, n)
|
||||
}
|
||||
|
||||
func Connect(fd int, sa Sockaddr) (err error) {
|
||||
ptr, n, err := sa.sockaddr()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return connect(fd, ptr, n)
|
||||
}
|
||||
|
||||
func Socket(domain, typ, proto int) (fd int, err error) {
|
||||
if domain == AF_INET6 && SocketDisableIPv6 {
|
||||
return -1, EAFNOSUPPORT
|
||||
}
|
||||
fd, err = socket(domain, typ, proto)
|
||||
return
|
||||
}
|
||||
|
||||
//sysnb socketpair(domain int, typ int, proto int, fd *[2]int32) (err error)
|
||||
|
||||
func Socketpair(domain, typ, proto int) (fd [2]int, err error) {
|
||||
var fdx [2]int32
|
||||
err = socketpair(domain, typ, proto, &fdx)
|
||||
if err == nil {
|
||||
fd[0] = int(fdx[0])
|
||||
fd[1] = int(fdx[1])
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func GetsockoptByte(fd, level, opt int) (value byte, err error) {
|
||||
var n byte
|
||||
vallen := _Socklen(1)
|
||||
|
@ -230,20 +230,6 @@ func Mkfifo(path string, mode uint32) (err error) {
|
||||
return Mknod(path, mode|S_IFIFO, 0)
|
||||
}
|
||||
|
||||
// For testing: clients can set this flag to force
|
||||
// creation of IPv6 sockets to return EAFNOSUPPORT.
|
||||
var SocketDisableIPv6 bool
|
||||
|
||||
type Sockaddr interface {
|
||||
sockaddr() (ptr uintptr, len _Socklen, err error) // lowercase; only we can define Sockaddrs
|
||||
}
|
||||
|
||||
type SockaddrInet4 struct {
|
||||
Port int
|
||||
Addr [4]byte
|
||||
raw RawSockaddrInet4
|
||||
}
|
||||
|
||||
func (sa *SockaddrInet4) sockaddr() (uintptr, _Socklen, error) {
|
||||
if sa.Port < 0 || sa.Port > 0xFFFF {
|
||||
return 0, 0, EINVAL
|
||||
@ -258,13 +244,6 @@ func (sa *SockaddrInet4) sockaddr() (uintptr, _Socklen, error) {
|
||||
return uintptr(unsafe.Pointer(&sa.raw)), SizeofSockaddrInet4, nil
|
||||
}
|
||||
|
||||
type SockaddrInet6 struct {
|
||||
Port int
|
||||
ZoneId uint32
|
||||
Addr [16]byte
|
||||
raw RawSockaddrInet6
|
||||
}
|
||||
|
||||
func (sa *SockaddrInet6) sockaddr() (uintptr, _Socklen, error) {
|
||||
if sa.Port < 0 || sa.Port > 0xFFFF {
|
||||
return 0, 0, EINVAL
|
||||
@ -280,11 +259,6 @@ func (sa *SockaddrInet6) sockaddr() (uintptr, _Socklen, error) {
|
||||
return uintptr(unsafe.Pointer(&sa.raw)), SizeofSockaddrInet6, nil
|
||||
}
|
||||
|
||||
type SockaddrUnix struct {
|
||||
Name string
|
||||
raw RawSockaddrUnix
|
||||
}
|
||||
|
||||
func (sa *SockaddrUnix) sockaddr() (uintptr, _Socklen, error) {
|
||||
name := sa.Name
|
||||
n := len(name)
|
||||
@ -472,40 +446,6 @@ func Getpeername(fd int) (sa Sockaddr, err error) {
|
||||
return anyToSockaddr(&rsa)
|
||||
}
|
||||
|
||||
func Bind(fd int, sa Sockaddr) (err error) {
|
||||
ptr, n, err := sa.sockaddr()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return bind(fd, ptr, n)
|
||||
}
|
||||
|
||||
func Connect(fd int, sa Sockaddr) (err error) {
|
||||
ptr, n, err := sa.sockaddr()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return connect(fd, ptr, n)
|
||||
}
|
||||
|
||||
func Socket(domain, typ, proto int) (fd int, err error) {
|
||||
if domain == AF_INET6 && SocketDisableIPv6 {
|
||||
return -1, EAFNOSUPPORT
|
||||
}
|
||||
fd, err = socket(domain, typ, proto)
|
||||
return
|
||||
}
|
||||
|
||||
func Socketpair(domain, typ, proto int) (fd [2]int, err error) {
|
||||
var fdx [2]int32
|
||||
err = socketpair(domain, typ, proto, &fdx)
|
||||
if err == nil {
|
||||
fd[0] = int(fdx[0])
|
||||
fd[1] = int(fdx[1])
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func GetsockoptInt(fd, level, opt int) (value int, err error) {
|
||||
var n int32
|
||||
vallen := _Socklen(4)
|
||||
|
@ -152,6 +152,66 @@ func Write(fd int, p []byte) (n int, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
// For testing: clients can set this flag to force
|
||||
// creation of IPv6 sockets to return EAFNOSUPPORT.
|
||||
var SocketDisableIPv6 bool
|
||||
|
||||
type Sockaddr interface {
|
||||
sockaddr() (ptr uintptr, len _Socklen, err error) // lowercase; only we can define Sockaddrs
|
||||
}
|
||||
|
||||
type SockaddrInet4 struct {
|
||||
Port int
|
||||
Addr [4]byte
|
||||
raw RawSockaddrInet4
|
||||
}
|
||||
|
||||
type SockaddrInet6 struct {
|
||||
Port int
|
||||
ZoneId uint32
|
||||
Addr [16]byte
|
||||
raw RawSockaddrInet6
|
||||
}
|
||||
|
||||
type SockaddrUnix struct {
|
||||
Name string
|
||||
raw RawSockaddrUnix
|
||||
}
|
||||
|
||||
func Bind(fd int, sa Sockaddr) (err error) {
|
||||
ptr, n, err := sa.sockaddr()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return bind(fd, ptr, n)
|
||||
}
|
||||
|
||||
func Connect(fd int, sa Sockaddr) (err error) {
|
||||
ptr, n, err := sa.sockaddr()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return connect(fd, ptr, n)
|
||||
}
|
||||
|
||||
func Socket(domain, typ, proto int) (fd int, err error) {
|
||||
if domain == AF_INET6 && SocketDisableIPv6 {
|
||||
return -1, EAFNOSUPPORT
|
||||
}
|
||||
fd, err = socket(domain, typ, proto)
|
||||
return
|
||||
}
|
||||
|
||||
func Socketpair(domain, typ, proto int) (fd [2]int, err error) {
|
||||
var fdx [2]int32
|
||||
err = socketpair(domain, typ, proto, &fdx)
|
||||
if err == nil {
|
||||
fd[0] = int(fdx[0])
|
||||
fd[1] = int(fdx[1])
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func Sendfile(outfd int, infd int, offset *int64, count int) (written int, err error) {
|
||||
if raceenabled {
|
||||
raceReleaseMerge(unsafe.Pointer(&ioSync))
|
||||
|
Loading…
Reference in New Issue
Block a user