Skip to content

Commit

Permalink
bug: fix the issue failing to listen on 0.0.0.0 on linux/arm64
Browse files Browse the repository at this point in the history
Fixes #392
  • Loading branch information
panjf2000 committed Aug 9, 2022
1 parent 3c3c519 commit c9eeca1
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 56 deletions.
89 changes: 89 additions & 0 deletions internal/socket/sock_posix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* Copyright 2009 The Go Authors. All rights reserved.
* Copyright (c) 2022 Andy Pan.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package socket

import (
"net"
"syscall"

"golang.org/x/sys/unix"
)

func ipToSockaddrInet4(ip net.IP, port int) (unix.SockaddrInet4, error) {
if len(ip) == 0 {
ip = net.IPv4zero
}
ip4 := ip.To4()
if ip4 == nil {
return unix.SockaddrInet4{}, &net.AddrError{Err: "non-IPv4 address", Addr: ip.String()}
}
sa := unix.SockaddrInet4{Port: port}
copy(sa.Addr[:], ip4)
return sa, nil
}

func ipToSockaddrInet6(ip net.IP, port int, zone string) (unix.SockaddrInet6, error) {
// In general, an IP wildcard address, which is either
// "0.0.0.0" or "::", means the entire IP addressing
// space. For some historical reason, it is used to
// specify "any available address" on some operations
// of IP node.
//
// When the IP node supports IPv4-mapped IPv6 address,
// we allow a listener to listen to the wildcard
// address of both IP addressing spaces by specifying
// IPv6 wildcard address.
if len(ip) == 0 || ip.Equal(net.IPv4zero) {
ip = net.IPv6zero
}
// We accept any IPv6 address including IPv4-mapped
// IPv6 address.
ip6 := ip.To16()
if ip6 == nil {
return unix.SockaddrInet6{}, &net.AddrError{Err: "non-IPv6 address", Addr: ip.String()}
}

sa := unix.SockaddrInet6{Port: port}
copy(sa.Addr[:], ip6)
iface, err := net.InterfaceByName(zone)
if err != nil {
return sa, nil
}
sa.ZoneId = uint32(iface.Index)

return sa, nil
}

func ipToSockaddr(family int, ip net.IP, port int, zone string) (unix.Sockaddr, error) {
switch family {
case syscall.AF_INET:
sa, err := ipToSockaddrInet4(ip, port)
if err != nil {
return nil, err
}
return &sa, nil
case syscall.AF_INET6:
sa, err := ipToSockaddrInet6(ip, port, zone)
if err != nil {
return nil, err
}
return &sa, nil
}
return nil, &net.AddrError{Err: "invalid address family", Addr: ip.String()}
}
32 changes: 4 additions & 28 deletions internal/socket/tcp_socket.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,38 +45,14 @@ func GetTCPSockAddr(proto, addr string) (sa unix.Sockaddr, family int, tcpAddr *

switch tcpVersion {
case "tcp4":
sa4 := &unix.SockaddrInet4{Port: tcpAddr.Port}

if tcpAddr.IP != nil {
if len(tcpAddr.IP) == 16 {
copy(sa4.Addr[:], tcpAddr.IP[12:16]) // copy last 4 bytes of slice to array
} else {
copy(sa4.Addr[:], tcpAddr.IP) // copy all bytes of slice to array
}
}

sa, family = sa4, unix.AF_INET
family = unix.AF_INET
sa, err = ipToSockaddr(family, tcpAddr.IP, tcpAddr.Port, "")
case "tcp6":
ipv6only = true
fallthrough
case "tcp":
sa6 := &unix.SockaddrInet6{Port: tcpAddr.Port}

if tcpAddr.IP != nil {
copy(sa6.Addr[:], tcpAddr.IP) // copy all bytes of slice to array
}

if tcpAddr.Zone != "" {
var iface *net.Interface
iface, err = net.InterfaceByName(tcpAddr.Zone)
if err != nil {
return
}

sa6.ZoneId = uint32(iface.Index)
}

sa, family = sa6, unix.AF_INET6
family = unix.AF_INET6
sa, err = ipToSockaddr(family, tcpAddr.IP, tcpAddr.Port, tcpAddr.Zone)
default:
err = errors.ErrUnsupportedProtocol
}
Expand Down
32 changes: 4 additions & 28 deletions internal/socket/udp_socket.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,38 +43,14 @@ func GetUDPSockAddr(proto, addr string) (sa unix.Sockaddr, family int, udpAddr *

switch udpVersion {
case "udp4":
sa4 := &unix.SockaddrInet4{Port: udpAddr.Port}

if udpAddr.IP != nil {
if len(udpAddr.IP) == 16 {
copy(sa4.Addr[:], udpAddr.IP[12:16]) // copy last 4 bytes of slice to array
} else {
copy(sa4.Addr[:], udpAddr.IP) // copy all bytes of slice to array
}
}

sa, family = sa4, unix.AF_INET
family = unix.AF_INET
sa, err = ipToSockaddr(family, udpAddr.IP, udpAddr.Port, "")
case "udp6":
ipv6only = true
fallthrough
case "udp":
sa6 := &unix.SockaddrInet6{Port: udpAddr.Port}

if udpAddr.IP != nil {
copy(sa6.Addr[:], udpAddr.IP) // copy all bytes of slice to array
}

if udpAddr.Zone != "" {
var iface *net.Interface
iface, err = net.InterfaceByName(udpAddr.Zone)
if err != nil {
return
}

sa6.ZoneId = uint32(iface.Index)
}

sa, family = sa6, unix.AF_INET6
family = unix.AF_INET6
sa, err = ipToSockaddr(family, udpAddr.IP, udpAddr.Port, udpAddr.Zone)
default:
err = errors.ErrUnsupportedProtocol
}
Expand Down

0 comments on commit c9eeca1

Please sign in to comment.