fix net-local-discovery

This commit is contained in:
Vincent Bernardoff 2016-10-10 20:04:28 +02:00 committed by Grégoire Henry
parent 1ac018188e
commit 668ebd7c5f

View File

@ -505,10 +505,10 @@ let discovery_answerer my_gid disco_port cancelation callback =
(* init a UDP listening socket on the broadcast canal *) (* init a UDP listening socket on the broadcast canal *)
catch catch
(fun () -> (fun () ->
let main_socket = LU.(socket PF_INET6 SOCK_DGRAM 0) in let main_socket = LU.(socket PF_INET SOCK_DGRAM 0) in
LU.(setsockopt main_socket SO_BROADCAST true) ; LU.(setsockopt main_socket SO_BROADCAST true) ;
LU.(setsockopt main_socket SO_REUSEADDR true) ; LU.(setsockopt main_socket SO_REUSEADDR true) ;
LU.(bind main_socket (ADDR_INET (Unix.inet6_addr_any, disco_port))) ; LU.(bind main_socket (ADDR_INET (Unix.inet_addr_any, disco_port))) ;
return (Some main_socket)) return (Some main_socket))
(fun exn -> (fun exn ->
debug "(%a) will not listen to discovery requests (%s)" debug "(%a) will not listen to discovery requests (%s)"
@ -523,18 +523,18 @@ let discovery_answerer my_gid disco_port cancelation callback =
pick [ (cancelation () >>= fun () -> return None) ; pick [ (cancelation () >>= fun () -> return None) ;
(Lwt_bytes.recvfrom main_socket buffer 0 len [] >>= fun r -> (Lwt_bytes.recvfrom main_socket buffer 0 len [] >>= fun r ->
return (Some r)) ] >>= function return (Some r)) ] >>= function
| Some (len, LU.ADDR_INET (addr, _)) -> | Some (len', LU.ADDR_INET (addr, _)) ->
if len <> len then if len' <> len then
step () (* drop bytes, better luck next time ! *) step () (* drop bytes, better luck next time ! *)
else else
answerable_discovery_message (Netbits.of_raw buffer) my_gid answerable_discovery_message (Netbits.of_raw buffer) my_gid
(fun _ port -> (fun _ port ->
catch catch
(fun () -> (fun () ->
let socket = LU.(socket PF_INET6 SOCK_STREAM 0) in let ipaddr = Ipaddr_unix.of_inet_addr addr in
let socket = LU.(socket (match ipaddr with Ipaddr.V4 _ -> PF_INET | V6 _ -> PF_INET6) SOCK_STREAM 0) in
LU.connect socket LU.(ADDR_INET (addr, port)) >>= fun () -> LU.connect socket LU.(ADDR_INET (addr, port)) >>= fun () ->
let addr = Ipaddr_unix.of_inet_addr addr in callback ipaddr port socket >>= fun () ->
callback addr port socket >>= fun () ->
return ()) return ())
(fun _ -> (* ignore errors *) return ()) >>= fun () -> (fun _ -> (* ignore errors *) return ()) >>= fun () ->
step ()) step ())
@ -551,9 +551,9 @@ let discovery_sender my_gid disco_port inco_port cancelation restart =
let rec loop delay n = let rec loop delay n =
catch catch
(fun () -> (fun () ->
let socket = LU.(socket PF_INET6 SOCK_DGRAM 0) in let socket = LU.(socket PF_INET SOCK_DGRAM 0) in
LU.setsockopt socket LU.SO_BROADCAST true ; LU.setsockopt socket LU.SO_BROADCAST true ;
LU.connect socket LU.(ADDR_INET (Unix.inet6_addr_any, disco_port)) >>= fun () -> LU.connect socket LU.(ADDR_INET (Unix.inet_addr_of_string "255.255.255.255", disco_port)) >>= fun () ->
Netbits.(write socket message) >>= fun _ -> Netbits.(write socket message) >>= fun _ ->
LU.close socket) LU.close socket)
(fun _ -> (fun _ ->