Skip to content

Commit

Permalink
Change getaddrinfo(host::AbstractString) to work with ipv6 localhost …
Browse files Browse the repository at this point in the history
…string "::1" (JuliaLang#36029)

* fix getaddrinfo(host::AbstractString) to work for ipv6 hostname

* fix getaddrinfo add tests

* clean up getaddrinfo

* when getaddrinfo gets passed localhost choosing the ipv6 instead ipv4 result causes the tests to fail

* fix downstream test
  • Loading branch information
ssikdar1 committed May 30, 2020
1 parent 018f0bb commit 2ab654c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
8 changes: 7 additions & 1 deletion stdlib/Sockets/src/addrinfo.jl
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,13 @@ function getaddrinfo(host::String, T::Type{<:IPAddr})
throw(DNSError(host, UV_EAI_NONAME))
end
getaddrinfo(host::AbstractString, T::Type{<:IPAddr}) = getaddrinfo(String(host), T)
getaddrinfo(host::AbstractString) = getaddrinfo(String(host), IPv4)
function getaddrinfo(host::AbstractString)
addrs = getalladdrinfo(String(host))
if !isempty(addrs)
return addrs[begin]
end
throw(DNSError(host, UV_EAI_NONAME))
end

function uv_getnameinfocb(req::Ptr{Cvoid}, status::Cint, hostname::Cstring, service::Cstring)
data = uv_req_data(req)
Expand Down
6 changes: 4 additions & 2 deletions stdlib/Sockets/test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,8 @@ end
end

@testset "getaddrinfo" begin
@test getaddrinfo("127.0.0.1") == ip"127.0.0.1"
@test getaddrinfo("::1") == ip"::1"
let localhost = getnameinfo(ip"127.0.0.1")::String
@test !isempty(localhost) && localhost != "127.0.0.1"
@test !isempty(getalladdrinfo(localhost)::Vector{IPAddr})
Expand Down Expand Up @@ -255,10 +257,10 @@ end
end

# test connecting to a named port
let localhost = getaddrinfo("localhost")
let localhost = ip"127.0.0.1"
global randport
randport, server = listenany(localhost, defaultport)
@async connect("localhost", randport)
@async connect(localhost, randport)
s1 = accept(server)
@test_throws ErrorException("client TCPSocket is not in initialization state") accept(server, s1)
@test_throws Base._UVError("listen", Base.UV_EADDRINUSE) listen(randport)
Expand Down

0 comments on commit 2ab654c

Please sign in to comment.