b433754c83
Fixes bug where we would infinite-loop trying to find an HTTP server port when all ports were unavailable (e.g. range [80,80] and no root permission) Also fixes bug where SSH port forwarding would try ports up to but not including HostPortMax. Also add clarification in error messages for which port range to expand
26 lines
631 B
Go
26 lines
631 B
Go
package xenserver
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"net"
|
|
)
|
|
|
|
// FindPort finds and starts listening on a port in the range [portMin, portMax]
|
|
// returns the listener and the port number on success, or nil, 0 on failure
|
|
func FindPort(portMin uint, portMax uint) (net.Listener, uint) {
|
|
log.Printf("Looking for an available port between %d and %d", portMin, portMax)
|
|
|
|
for port := portMin; port <= portMax; port++ {
|
|
log.Printf("Trying port: %d", port)
|
|
l, err := net.Listen("tcp", fmt.Sprintf(":%d", port))
|
|
if err == nil {
|
|
return l, port
|
|
} else {
|
|
log.Printf("Port %d unavailable: %s", port, err.Error())
|
|
}
|
|
}
|
|
|
|
return nil, 0
|
|
}
|