createNetcheckBindString determines the netcheck socket bind "address:port" string based on the CLI args and environment variable values used to invoke the netcheck CLI. Arguments cliAddressIsSet and cliPortIsSet explicitly indicate whether the corresponding cliAddress and cliPort were set in CLI ar
(cliAddress string, cliAddressIsSet bool, cliPort int, cliPortIsSet bool, envBind string)
| 297 | // corresponding cliAddress and cliPort were set in CLI args, instead of relying |
| 298 | // on in-band sentinel values. |
| 299 | func createNetcheckBindString(cliAddress string, cliAddressIsSet bool, cliPort int, cliPortIsSet bool, envBind string) (string, error) { |
| 300 | // Default to port number 0 but overwrite with a valid CLI value, if set. |
| 301 | var port uint16 = 0 |
| 302 | if cliPortIsSet { |
| 303 | // 0 is valid, results in OS picking port. |
| 304 | if cliPort >= 0 && cliPort <= math.MaxUint16 { |
| 305 | port = uint16(cliPort) |
| 306 | } else { |
| 307 | return "", fmt.Errorf("invalid bind port number: %d", cliPort) |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | // Use CLI address, if set. |
| 312 | if cliAddressIsSet { |
| 313 | addr, err := netip.ParseAddr(cliAddress) |
| 314 | if err != nil { |
| 315 | return "", fmt.Errorf("invalid bind address: %q", cliAddress) |
| 316 | } |
| 317 | return netip.AddrPortFrom(addr, port).String(), nil |
| 318 | } else { |
| 319 | // No CLI address set, but port is set. |
| 320 | if cliPortIsSet { |
| 321 | return fmt.Sprintf(":%d", port), nil |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | // Fall back to the environment variable. |
| 326 | // Intentionally skipping input validation here to avoid breaking legacy usage method. |
| 327 | if envBind != "" { |
| 328 | return envBind, nil |
| 329 | } |
| 330 | |
| 331 | // OS picks both address and port. |
| 332 | return ":0", nil |
| 333 | } |
searching dependent graphs…