diff --git a/qemu-kernel.go b/qemu-kernel.go index fd54e85..1d56eef 100644 --- a/qemu-kernel.go +++ b/qemu-kernel.go @@ -12,6 +12,7 @@ import ( "net" "os" "os/exec" + "runtime" "syscall" "time" @@ -117,14 +118,29 @@ func getRandomAddrPort() (addr string) { return fmt.Sprintf("%s:%d", ip, port) } +func getRandomPort(ip string) (addr string) { + // ip:1024-65535 + port := rand.Int()%(65535-1024) + 1024 + return fmt.Sprintf("%s:%d", ip, port) +} + func getFreeAddrPort() (addrPort string) { + timeout := time.Now().Add(time.Second) for { - addrPort = getRandomAddrPort() + if runtime.GOOS == "linux" { + addrPort = getRandomAddrPort() + } else { + addrPort = getRandomPort("127.0.0.1") + } ln, err := net.Listen("tcp", addrPort) if err == nil { ln.Close() return } + + if time.Now().After(timeout) { + panic("Can't found free address:port on localhost") + } } }