Add linux-386, linux-ppc64le targets (#56)
* Use golang.org/x/sys/unix for _linux.go sources To support builds on GOARCH=386 and possibly elsewhere, it's necessary to use the x/sys/unix package instead of the syscall package. This is because the syscall package is frozen and does not support SYS_GETSOCKNAME, SYS_RECVFROM, nor SYS_SENDTO for GOARCH=386. This commit alone doesn't add support for 386 builds, just gets things onto x/sys/unix so that it's possible. The remaining uses of the syscall package relate to signals, which cannot be switched to the x/sys/unix package at this time. Windows support breaks, so they can either continue using the syscall package (it's frozen, this is safe for Go 1.x at minimum), or something can be written to just use both windows- and unix-compatible signals. * Add linux-386, ppc64le targets to Makefile Because 'linux' is linux-amd64 already, just add linux-386 and linux-ppc64le targets to distinguish them. Would rename the linux target but that might break existing uses.
This commit is contained in:
52
udp_linux.go
52
udp_linux.go
@ -55,58 +55,58 @@ var x int
|
||||
|
||||
func NewListener(ip string, port int, multi bool) (*udpConn, error) {
|
||||
syscall.ForkLock.RLock()
|
||||
fd, err := syscall.Socket(syscall.AF_INET, syscall.SOCK_DGRAM, syscall.IPPROTO_UDP)
|
||||
fd, err := unix.Socket(unix.AF_INET, unix.SOCK_DGRAM, unix.IPPROTO_UDP)
|
||||
if err == nil {
|
||||
syscall.CloseOnExec(fd)
|
||||
unix.CloseOnExec(fd)
|
||||
}
|
||||
syscall.ForkLock.RUnlock()
|
||||
|
||||
if err != nil {
|
||||
syscall.Close(fd)
|
||||
unix.Close(fd)
|
||||
return nil, fmt.Errorf("unable to open socket: %s", err)
|
||||
}
|
||||
|
||||
var lip [4]byte
|
||||
copy(lip[:], net.ParseIP(ip).To4())
|
||||
|
||||
if err = syscall.SetsockoptInt(fd, syscall.SOL_SOCKET, unix.SO_REUSEPORT, 1); err != nil {
|
||||
if err = unix.SetsockoptInt(fd, unix.SOL_SOCKET, unix.SO_REUSEPORT, 1); err != nil {
|
||||
return nil, fmt.Errorf("unable to set SO_REUSEPORT: %s", err)
|
||||
}
|
||||
|
||||
if err = syscall.Bind(fd, &syscall.SockaddrInet4{Port: port}); err != nil {
|
||||
if err = unix.Bind(fd, &unix.SockaddrInet4{Port: port}); err != nil {
|
||||
return nil, fmt.Errorf("unable to bind to socket: %s", err)
|
||||
}
|
||||
|
||||
//TODO: this may be useful for forcing threads into specific cores
|
||||
//syscall.SetsockoptInt(fd, syscall.SOL_SOCKET, unix.SO_INCOMING_CPU, x)
|
||||
//v, err := syscall.GetsockoptInt(fd, syscall.SOL_SOCKET, unix.SO_INCOMING_CPU)
|
||||
//unix.SetsockoptInt(fd, unix.SOL_SOCKET, unix.SO_INCOMING_CPU, x)
|
||||
//v, err := unix.GetsockoptInt(fd, unix.SOL_SOCKET, unix.SO_INCOMING_CPU)
|
||||
//l.Println(v, err)
|
||||
|
||||
return &udpConn{sysFd: fd}, err
|
||||
}
|
||||
|
||||
func (u *udpConn) SetRecvBuffer(n int) error {
|
||||
return syscall.SetsockoptInt(u.sysFd, syscall.SOL_SOCKET, syscall.SO_RCVBUFFORCE, n)
|
||||
return unix.SetsockoptInt(u.sysFd, unix.SOL_SOCKET, unix.SO_RCVBUFFORCE, n)
|
||||
}
|
||||
|
||||
func (u *udpConn) SetSendBuffer(n int) error {
|
||||
return syscall.SetsockoptInt(u.sysFd, syscall.SOL_SOCKET, syscall.SO_SNDBUFFORCE, n)
|
||||
return unix.SetsockoptInt(u.sysFd, unix.SOL_SOCKET, unix.SO_SNDBUFFORCE, n)
|
||||
}
|
||||
|
||||
func (u *udpConn) GetRecvBuffer() (int, error) {
|
||||
return syscall.GetsockoptInt(int(u.sysFd), syscall.SOL_SOCKET, syscall.SO_RCVBUF)
|
||||
return unix.GetsockoptInt(int(u.sysFd), unix.SOL_SOCKET, unix.SO_RCVBUF)
|
||||
}
|
||||
|
||||
func (u *udpConn) GetSendBuffer() (int, error) {
|
||||
return syscall.GetsockoptInt(int(u.sysFd), syscall.SOL_SOCKET, syscall.SO_SNDBUF)
|
||||
return unix.GetsockoptInt(int(u.sysFd), unix.SOL_SOCKET, unix.SO_SNDBUF)
|
||||
}
|
||||
|
||||
func (u *udpConn) LocalAddr() (*udpAddr, error) {
|
||||
var rsa rawSockaddrAny
|
||||
var rLen = syscall.SizeofSockaddrAny
|
||||
var rLen = unix.SizeofSockaddrAny
|
||||
|
||||
_, _, err := syscall.Syscall(
|
||||
syscall.SYS_GETSOCKNAME,
|
||||
_, _, err := unix.Syscall(
|
||||
unix.SYS_GETSOCKNAME,
|
||||
uintptr(u.sysFd),
|
||||
uintptr(unsafe.Pointer(&rsa)),
|
||||
uintptr(unsafe.Pointer(&rLen)),
|
||||
@ -117,7 +117,7 @@ func (u *udpConn) LocalAddr() (*udpAddr, error) {
|
||||
}
|
||||
|
||||
addr := &udpAddr{}
|
||||
if rsa.Addr.Family == syscall.AF_INET {
|
||||
if rsa.Addr.Family == unix.AF_INET {
|
||||
addr.Port = uint16(rsa.Addr.Data[0])<<8 + uint16(rsa.Addr.Data[1])
|
||||
addr.IP = uint32(rsa.Addr.Data[2])<<24 + uint32(rsa.Addr.Data[3])<<16 + uint32(rsa.Addr.Data[4])<<8 + uint32(rsa.Addr.Data[5])
|
||||
} else {
|
||||
@ -157,11 +157,11 @@ func (u *udpConn) ListenOut(f *Interface) {
|
||||
|
||||
func (u *udpConn) Read(addr *udpAddr, b []byte) ([]byte, error) {
|
||||
var rsa rawSockaddrAny
|
||||
var rLen = syscall.SizeofSockaddrAny
|
||||
var rLen = unix.SizeofSockaddrAny
|
||||
|
||||
for {
|
||||
n, _, err := syscall.Syscall6(
|
||||
syscall.SYS_RECVFROM,
|
||||
n, _, err := unix.Syscall6(
|
||||
unix.SYS_RECVFROM,
|
||||
uintptr(u.sysFd),
|
||||
uintptr(unsafe.Pointer(&b[0])),
|
||||
uintptr(len(b)),
|
||||
@ -174,7 +174,7 @@ func (u *udpConn) Read(addr *udpAddr, b []byte) ([]byte, error) {
|
||||
return nil, &net.OpError{Op: "read", Err: err}
|
||||
}
|
||||
|
||||
if rsa.Addr.Family == syscall.AF_INET {
|
||||
if rsa.Addr.Family == unix.AF_INET {
|
||||
addr.Port = uint16(rsa.Addr.Data[0])<<8 + uint16(rsa.Addr.Data[1])
|
||||
addr.IP = uint32(rsa.Addr.Data[2])<<24 + uint32(rsa.Addr.Data[3])<<16 + uint32(rsa.Addr.Data[4])<<8 + uint32(rsa.Addr.Data[5])
|
||||
} else {
|
||||
@ -188,8 +188,8 @@ func (u *udpConn) Read(addr *udpAddr, b []byte) ([]byte, error) {
|
||||
|
||||
func (u *udpConn) ReadMulti(msgs []rawMessage) (int, error) {
|
||||
for {
|
||||
n, _, err := syscall.Syscall6(
|
||||
syscall.SYS_RECVMMSG,
|
||||
n, _, err := unix.Syscall6(
|
||||
unix.SYS_RECVMMSG,
|
||||
uintptr(u.sysFd),
|
||||
uintptr(unsafe.Pointer(&msgs[0])),
|
||||
uintptr(len(msgs)),
|
||||
@ -207,10 +207,10 @@ func (u *udpConn) ReadMulti(msgs []rawMessage) (int, error) {
|
||||
}
|
||||
|
||||
func (u *udpConn) WriteTo(b []byte, addr *udpAddr) error {
|
||||
var rsa syscall.RawSockaddrInet4
|
||||
var rsa unix.RawSockaddrInet4
|
||||
|
||||
//TODO: sometimes addr is nil!
|
||||
rsa.Family = syscall.AF_INET
|
||||
rsa.Family = unix.AF_INET
|
||||
p := (*[2]byte)(unsafe.Pointer(&rsa.Port))
|
||||
p[0] = byte(addr.Port >> 8)
|
||||
p[1] = byte(addr.Port)
|
||||
@ -221,14 +221,14 @@ func (u *udpConn) WriteTo(b []byte, addr *udpAddr) error {
|
||||
rsa.Addr[3] = byte(addr.IP & 0x000000ff)
|
||||
|
||||
for {
|
||||
_, _, err := syscall.Syscall6(
|
||||
syscall.SYS_SENDTO,
|
||||
_, _, err := unix.Syscall6(
|
||||
unix.SYS_SENDTO,
|
||||
uintptr(u.sysFd),
|
||||
uintptr(unsafe.Pointer(&b[0])),
|
||||
uintptr(len(b)),
|
||||
uintptr(0),
|
||||
uintptr(unsafe.Pointer(&rsa)),
|
||||
uintptr(syscall.SizeofSockaddrInet4),
|
||||
uintptr(unix.SizeofSockaddrInet4),
|
||||
)
|
||||
|
||||
if err != 0 {
|
||||
|
Reference in New Issue
Block a user