Skip to content

Commit

Permalink
Merge pull request #424 from hannesm/unix-icmp
Browse files Browse the repository at this point in the history
stack-unix: use a RAW socket for icmp (fixes #358)
  • Loading branch information
hannesm authored Feb 8, 2020
2 parents 8716023 + da105a7 commit 3b8de94
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 9 deletions.
3 changes: 1 addition & 2 deletions examples/ping/ping.ml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ let send_echo_requests ~stack ~payload ~dst () =
(* Return a thread and a receiver callback. The thread is woken up when we have
received [count] packets *)
let make_receiver ~count ~payload () =
let open Lwt.Infix in
let finished_t, finished_u = Lwt.task () in
let callback buf =
Log.debug (fun f -> f "Received IP %a" Cstruct.hexdump_pp buf);
Expand All @@ -70,7 +69,7 @@ let make_receiver ~count ~payload () =
| Next_hop_mtu _ | Pointer _ | Address _ | Unused ->
Log.err (fun f -> f "received an ICMP message which wasn't an echo-request or reply");
Lwt.return_unit
| Id_and_seq (id, seq) ->
| Id_and_seq (_id, seq) ->
if reply.code <> 0
then Log.err (fun f -> f "received an ICMP ECHO_REQUEST with reply.code=%d" reply.code);
if not(Cstruct.equal payload received_payload)
Expand Down
9 changes: 2 additions & 7 deletions src/stack-unix/icmpv4_socket.ml
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@ let pp_error ppf (`Ip s) = Fmt.string ppf s

let is_win32 = Sys.os_type = "Win32"

let sock_icmp =
(* Windows uses SOCK_RAW protocol 1 for ICMP
Unix uses SOCK_DGRAM protocol 1 for ICMP *)
if is_win32 then Lwt_unix.SOCK_RAW else Lwt_unix.SOCK_DGRAM

let ipproto_icmp = 1 (* according to BSD /etc/protocols *)
let port = 0 (* port isn't meaningful in this context *)

Expand Down Expand Up @@ -52,7 +47,7 @@ let write _t ~dst ?ttl:_ttl buf =
let flags = [] in
let ipproto_icmp = 1 in (* according to BSD /etc/protocols *)
let port = 0 in (* port isn't meaningful in this context *)
let fd = socket PF_INET sock_icmp ipproto_icmp in
let fd = socket PF_INET SOCK_RAW ipproto_icmp in
let in_addr = Unix.inet_addr_of_string (Ipaddr.V4.to_string dst) in
let sockaddr = ADDR_INET (in_addr, port) in
Lwt.catch (fun () ->
Expand Down Expand Up @@ -83,7 +78,7 @@ let input t ~src ~dst:_ buf =
| _, _ -> Lwt.return_unit

let listen _t addr fn =
let fd = Lwt_unix.socket PF_INET sock_icmp ipproto_icmp in
let fd = Lwt_unix.socket PF_INET SOCK_RAW ipproto_icmp in
Lwt.finalize
(fun () ->
let sa = Lwt_unix.ADDR_INET (Unix.inet_addr_of_string (Ipaddr.V4.to_string addr), port) in
Expand Down

0 comments on commit 3b8de94

Please sign in to comment.