Skip to content

Commit 530313d

Browse files
authored
JuliaLang/julia#34302 add ipv6 square bracket support to launch_on_machine (JuliaLang/julia#34494)
1 parent 025e86e commit 530313d

File tree

2 files changed

+54
-19
lines changed

2 files changed

+54
-19
lines changed

src/managers.jl

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,44 @@ end
147147
show(io::IO, manager::SSHManager) = println(io, "SSHManager(machines=", manager.machines, ")")
148148

149149

150-
function launch_on_machine(manager::SSHManager, machine, cnt, params, launched, launch_ntfy::Condition)
150+
function parse_machine(machine::AbstractString)
151+
hoststr = ""
152+
portnum = nothing
153+
154+
if machine[begin] == '[' # ipv6 bracket notation (RFC 2732)
155+
ipv6_end = findlast(']', machine)
156+
if ipv6_end == nothing
157+
throw(ArgumentError("invalid machine definition format string: invalid port format \"$machine_def\""))
158+
end
159+
hoststr = machine[begin+1 : prevind(machine,ipv6_end)]
160+
machine_def = split(machine[ipv6_end : end] , ':')
161+
else # ipv4
162+
machine_def = split(machine, ':')
163+
hoststr = machine_def[1]
164+
end
165+
166+
if length(machine_def) > 2
167+
throw(ArgumentError("invalid machine definition format string: invalid port format \"$machine_def\""))
168+
end
169+
170+
if length(machine_def) == 2
171+
portstr = machine_def[2]
172+
173+
portnum = tryparse(Int, portstr)
174+
if portnum == nothing
175+
msg = "invalid machine definition format string: invalid port format \"$machine_def\""
176+
throw(ArgumentError(msg))
177+
end
178+
179+
if portnum < 1 || portnum > 65535
180+
msg = "invalid machine definition format string: invalid port number \"$machine_def\""
181+
throw(ArgumentError(msg))
182+
end
183+
end
184+
(hoststr, portnum)
185+
end
186+
187+
function launch_on_machine(manager::SSHManager, machine::AbstractString, cnt, params::Dict, launched::Array, launch_ntfy::Condition)
151188
dir = params[:dir]
152189
exename = params[:exename]
153190
exeflags = params[:exeflags]
@@ -165,21 +202,8 @@ function launch_on_machine(manager::SSHManager, machine, cnt, params, launched,
165202
end
166203
exeflags = `$exeflags --worker`
167204

168-
machine_def = split(machine_bind[1], ':')
169-
# if this machine def has a port number, add the port information to the ssh flags
170-
if length(machine_def) > 2
171-
throw(ArgumentError("invalid machine definition format string: invalid port format \"$machine_def\""))
172-
end
173-
host = machine_def[1]
174-
portopt = ``
175-
if length(machine_def) == 2
176-
portstr = machine_def[2]
177-
if !all(isdigit, portstr) || (p = parse(Int,portstr); p < 1 || p > 65535)
178-
msg = "invalid machine definition format string: invalid port format \"$machine_def\""
179-
throw(ArgumentError(msg))
180-
end
181-
portopt = ` -p $(machine_def[2]) `
182-
end
205+
host, portnum = parse_machine(machine_bind[1])
206+
portopt = portnum === nothing ? `` : `-p $portnum`
183207
sshflags = `$(params[:sshflags]) $portopt`
184208

185209
if tunnel

test/managers.jl

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,24 @@
11
using Test
2-
3-
using Distributed: bind_client_port
2+
using Distributed
43
using Sockets
4+
using Distributed: parse_machine, bind_client_port
5+
6+
@test parse_machine("127.0.0.1") == ("127.0.0.1", nothing)
7+
@test parse_machine("127.0.0.1:80") == ("127.0.0.1", 80)
8+
@test parse_machine("[2001:db8::1]") == ("2001:db8::1", nothing)
9+
@test parse_machine("[2001:db8::1]:443") == ("2001:db8::1", 443)
10+
11+
@test parse_machine("127.0.0.1:90") == ("127.0.0.1", 90)
12+
@test parse_machine("127.0.0.1:1") == ("127.0.0.1", 1)
13+
@test parse_machine("127.0.0.1:65535") == ("127.0.0.1", 65535)
14+
@test_throws ArgumentError parse_machine("127.0.0.1:-1")
15+
@test_throws ArgumentError parse_machine("127.0.0.1:0")
16+
@test_throws ArgumentError parse_machine("127.0.0.1:65536")
517

618
sock = bind_client_port(TCPSocket(), typeof(IPv4(0)))
719
addr, port = getsockname(sock)
820
@test addr == ip"0.0.0.0"
921

10-
1122
sock = bind_client_port(TCPSocket(), typeof(IPv6(0)))
1223
addr, port = getsockname(sock)
1324
@test addr == ip"::"

0 commit comments

Comments
 (0)