-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bug: fix the issue failing to listen on 0.0.0.0 on linux/arm64
Fixes #392
- Loading branch information
Showing
3 changed files
with
97 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters