Skip to content

Commit

Permalink
[server] add support for ipv6 addresses for --wait/--connect (HaxeFou…
Browse files Browse the repository at this point in the history
  • Loading branch information
kLabz authored and 0b1kn00b committed Jan 25, 2024
1 parent 88a9724 commit b0d2143
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 14 deletions.
1 change: 1 addition & 0 deletions haxe.opam
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,5 @@ depends: [
"conf-zlib"
"conf-neko"
"luv" {>= "0.5.12"}
"ipaddr"
]
6 changes: 3 additions & 3 deletions src/compiler/compilationContext.ml
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ type server_api = {
cache : CompilationCache.t;
callbacks : compilation_callbacks;
on_context_create : unit -> int;
init_wait_socket : string -> int -> server_accept;
init_wait_connect : string -> int -> server_accept;
init_wait_socket : (Ipaddr.V4.t, Ipaddr.V6.t) Ipaddr.v4v6 -> int -> server_accept;
init_wait_connect : (Ipaddr.V4.t, Ipaddr.V6.t) Ipaddr.v4v6 -> int -> server_accept;
init_wait_stdio : unit -> server_accept;
wait_loop : bool -> server_accept -> int;
do_connect : string -> int -> string list -> unit;
do_connect : (Ipaddr.V4.t, Ipaddr.V6.t) Ipaddr.v4v6 -> int -> string list -> unit;
}

let message ctx msg =
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/compiler.ml
Original file line number Diff line number Diff line change
Expand Up @@ -572,8 +572,8 @@ module HighLevel = struct
(* If we are already connected, ignore (issue #10813) *)
loop acc l
else begin
let host, port = (try ExtString.String.split hp ":" with _ -> "127.0.0.1", hp) in
server_api.do_connect host (try int_of_string port with _ -> raise (Arg.Bad "Invalid port")) ((List.rev acc) @ l);
let host, port = Helper.parse_host_port hp in
server_api.do_connect host port ((List.rev acc) @ l);
[],None
end
| "--server-connect" :: hp :: l ->
Expand Down
17 changes: 14 additions & 3 deletions src/compiler/helper.ml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
open Ipaddr
exception HelpMessage of string

let is_debug_run = try Sys.getenv "HAXEDEBUG" = "1" with _ -> false
Expand Down Expand Up @@ -52,6 +53,16 @@ let parse_hxml file =
parse_hxml_data data

let parse_host_port hp =
let host, port = (try ExtString.String.split hp ":" with _ -> "127.0.0.1", hp) in
let port = try int_of_string port with _ -> raise (Arg.Bad "Invalid port") in
host, port
match (Ipaddr.with_port_of_string ~default:(-1) hp) with
(* Short ipv6 notation will be mixed up with port; extract port and rebuild ipv6 *)
| Ok (V6 ip, -1) ->
let octets = ExtLib.String.split_on_char ':' (V6.to_string ip) in
(match (List.rev octets) with
| port :: octets -> (try V6 (V6.of_string_exn (ExtLib.String.join ":" (List.rev octets))), int_of_string port with _ -> raise (Arg.Bad "Invalid host/port"))
| _ -> raise (Arg.Bad "Invalid host/port")
)
| Ok (_, -1) -> raise (Arg.Bad "Invalid host/port: missing port")
| Ok (ip, port) -> ip, port
(* Default to 127.0.0.1 with given port if no host is provided *)
| Error _ when Str.string_match (Str.regexp "[0-9]+$") hp 0 -> V4 (V4.of_string_exn "127.0.0.1"), int_of_string hp
| Error _ -> raise (Arg.Bad "Invalid host/port")
23 changes: 18 additions & 5 deletions src/compiler/server.ml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ open CompilationCache
open Timer
open Type
open DisplayProcessingGlobals
open Ipaddr
open Json
open CompilationContext
open MessageReporting
Expand Down Expand Up @@ -542,8 +543,12 @@ let init_wait_stdio() =
mk_length_prefixed_communication false stdin stderr

(* The connect function to connect to [host] at [port] and send arguments [args]. *)
let do_connect host port args =
let sock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
let do_connect ip port args =
let (domain, host) = match ip with
| V4 ip -> (Unix.PF_INET, V4.to_string ip)
| V6 ip -> (Unix.PF_INET6, V6.to_string ip)
in
let sock = Unix.socket domain Unix.SOCK_STREAM 0 in
(try Unix.connect sock (Unix.ADDR_INET (Unix.inet_addr_of_string host,port)) with
| Unix.Unix_error(code,_,_) -> failwith("Couldn't connect on " ^ host ^ ":" ^ string_of_int port ^ " (" ^ (Unix.error_message code) ^ ")");
| _ -> failwith ("Couldn't connect on " ^ host ^ ":" ^ string_of_int port)
Expand Down Expand Up @@ -710,14 +715,22 @@ and wait_loop verbose accept =
0

(* Connect to given host/port and return accept function for communication *)
and init_wait_connect host port =
and init_wait_connect ip port =
let host = match ip with
| V4 ip -> V4.to_string ip
| V6 ip -> V6.to_string ip
in
let host = Unix.inet_addr_of_string host in
let chin, chout = Unix.open_connection (Unix.ADDR_INET (host,port)) in
mk_length_prefixed_communication true chin chout

(* The accept-function to wait for a socket connection. *)
and init_wait_socket host port =
let sock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
and init_wait_socket ip port =
let (domain, host) = match ip with
| V4 ip -> (Unix.PF_INET, V4.to_string ip)
| V6 ip -> (Unix.PF_INET6, V6.to_string ip)
in
let sock = Unix.socket domain Unix.SOCK_STREAM 0 in
(try Unix.setsockopt sock Unix.SO_REUSEADDR true with _ -> ());
(try Unix.bind sock (Unix.ADDR_INET (Unix.inet_addr_of_string host,port)) with _ -> failwith ("Couldn't wait on " ^ host ^ ":" ^ string_of_int port));
ServerMessage.socket_message ("Waiting on " ^ host ^ ":" ^ string_of_int port);
Expand Down
2 changes: 1 addition & 1 deletion src/dune
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
(libraries
extc extproc extlib_leftovers ilib javalib mbedtls neko objsize pcre2 swflib ttflib ziplib
json
unix str bigarray threads dynlink
unix ipaddr str bigarray threads dynlink
xml-light extlib sha
luv
)
Expand Down

0 comments on commit b0d2143

Please sign in to comment.