conn: try harder to have v4 and v6 ports agree
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
parent
78ebce6932
commit
30b96ba083
|
@ -99,7 +99,9 @@ func extractErrno(err error) error {
|
||||||
func createBind(uport uint16) (Bind, uint16, error) {
|
func createBind(uport uint16) (Bind, uint16, error) {
|
||||||
var err error
|
var err error
|
||||||
var bind nativeBind
|
var bind nativeBind
|
||||||
|
var tries int
|
||||||
|
|
||||||
|
again:
|
||||||
port := int(uport)
|
port := int(uport)
|
||||||
|
|
||||||
bind.ipv4, port, err = listenNet("udp4", port)
|
bind.ipv4, port, err = listenNet("udp4", port)
|
||||||
|
@ -108,6 +110,10 @@ func createBind(uport uint16) (Bind, uint16, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
bind.ipv6, port, err = listenNet("udp6", port)
|
bind.ipv6, port, err = listenNet("udp6", port)
|
||||||
|
if uport == 0 && err != nil && extractErrno(err) == syscall.EADDRINUSE && tries < 100 {
|
||||||
|
tries++
|
||||||
|
goto again
|
||||||
|
}
|
||||||
if err != nil && extractErrno(err) != syscall.EAFNOSUPPORT {
|
if err != nil && extractErrno(err) != syscall.EAFNOSUPPORT {
|
||||||
bind.ipv4.Close()
|
bind.ipv4.Close()
|
||||||
bind.ipv4 = nil
|
bind.ipv4 = nil
|
||||||
|
|
|
@ -104,7 +104,11 @@ func createBind(port uint16) (Bind, uint16, error) {
|
||||||
var err error
|
var err error
|
||||||
var bind nativeBind
|
var bind nativeBind
|
||||||
var newPort uint16
|
var newPort uint16
|
||||||
|
var tries int
|
||||||
|
originalPort := port
|
||||||
|
|
||||||
|
again:
|
||||||
|
port = originalPort
|
||||||
// Attempt ipv6 bind, update port if successful.
|
// Attempt ipv6 bind, update port if successful.
|
||||||
bind.sock6, newPort, err = create6(port)
|
bind.sock6, newPort, err = create6(port)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -118,6 +122,10 @@ func createBind(port uint16) (Bind, uint16, error) {
|
||||||
// Attempt ipv4 bind, update port if successful.
|
// Attempt ipv4 bind, update port if successful.
|
||||||
bind.sock4, newPort, err = create4(port)
|
bind.sock4, newPort, err = create4(port)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
if originalPort == 0 && err == syscall.EADDRINUSE && tries < 100 {
|
||||||
|
tries++
|
||||||
|
goto again
|
||||||
|
}
|
||||||
if err != syscall.EAFNOSUPPORT {
|
if err != syscall.EAFNOSUPPORT {
|
||||||
unix.Close(bind.sock6)
|
unix.Close(bind.sock6)
|
||||||
return nil, 0, err
|
return nil, 0, err
|
||||||
|
|
Loading…
Reference in a new issue