diff --git a/NEWS.md b/NEWS.md index 04ee2158d70d3d..9e16b31de775fa 100644 --- a/NEWS.md +++ b/NEWS.md @@ -46,6 +46,11 @@ Standard library changes * Fixed `repr` such that it displays `Time` as it would be entered in Julia ([#32103]). +#### Sockets + +* `getipaddrs` returns IP addresses in the order provided by libuv ([#32260]). +* `getipaddr` prefers to return the first `IPv4` interface address provided by libuv ([#32260]). + #### Statistics * `mean` now accepts both a function argument and a `dims` keyword ([#31576]). diff --git a/stdlib/Sockets/src/addrinfo.jl b/stdlib/Sockets/src/addrinfo.jl index 8e1e0f084db592..a799a731cbe955 100644 --- a/stdlib/Sockets/src/addrinfo.jl +++ b/stdlib/Sockets/src/addrinfo.jl @@ -234,10 +234,14 @@ ip"fe80::9731:35af:e1c5:6e49" """ function getipaddr(addr_type::Type{T}) where T<:IPAddr addrs = getipaddrs(addr_type) + if length(addrs) == 0 error("No networking interface available") end - return addrs[1] + + # Prefer the first IPv4 address + i = something(findfirst(ip -> ip isa IPv4, addrs), 1) + return addrs[i] end getipaddr() = getipaddr(IPv4) @@ -295,14 +299,5 @@ function getipaddrs(addr_type::Type{T}=IPAddr; loopback::Bool=false) where T<:IP end end ccall(:uv_free_interface_addresses, Cvoid, (Ptr{UInt8}, Int32), addr, count) - sort!(addresses, lt=(addr1,addr2) -> begin - if addr1 isa IPv4 && addr2 isa IPv6 - return true - elseif addr1 isa IPv6 && addr2 isa IPv4 - return false - else - return addr1 < addr2 - end - end) return addresses end