Skip to content

Commit

Permalink
stack-socket: minimize diff -u udpv* ; diff -u tcpv*ml
Browse files Browse the repository at this point in the history
  • Loading branch information
hannesm committed Sep 12, 2020
1 parent fad44f5 commit dea044b
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 21 deletions.
12 changes: 6 additions & 6 deletions src/stack-unix/tcpv4_socket.ml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*)

open Lwt
open Lwt.Infix

type ipaddr = Ipaddr.V4.t
type flow = Lwt_unix.file_descr
Expand All @@ -26,13 +26,13 @@ type t = {

include Tcp_socket

let connect id =
let connect addr =
let t =
match id with
match addr with
| None -> { interface=None }
| Some ip -> { interface=Some (Ipaddr_unix.V4.to_inet_addr ip) }
in
return t
Lwt.return t

let dst fd =
match Lwt_unix.getpeername fd with
Expand All @@ -54,7 +54,7 @@ let create_connection ?keepalive _t (dst,dst_port) =
| None -> ()
| Some { Mirage_protocols.Keepalive.after; interval; probes } ->
Tcp_socket_options.enable_keepalive ~fd ~after ~interval ~probes );
return (Ok fd))
Lwt.return (Ok fd))
(fun exn ->
Lwt.catch (fun () -> Lwt_unix.close fd) (fun _ -> Lwt.return_unit) >>= fun () ->
return (Error (`Exn exn)))
Lwt.return (Error (`Exn exn)))
10 changes: 5 additions & 5 deletions src/stack-unix/tcpv6_socket.ml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ type t = {
interface: Unix.inet_addr option; (* source ip to bind to *)
}

include Tcp_socket

let connect addr =
let t =
match addr with
Expand All @@ -50,12 +52,10 @@ let create_connection ?keepalive _t (dst,dst_port) =
(Lwt_unix.ADDR_INET ((Ipaddr_unix.V6.to_inet_addr dst), dst_port))
>>= fun () ->
( match keepalive with
| None -> ()
| Some { Mirage_protocols.Keepalive.after; interval; probes } ->
Tcp_socket_options.enable_keepalive ~fd ~after ~interval ~probes );
| None -> ()
| Some { Mirage_protocols.Keepalive.after; interval; probes } ->
Tcp_socket_options.enable_keepalive ~fd ~after ~interval ~probes );
Lwt.return (Ok fd))
(fun exn ->
Lwt.catch (fun () -> Lwt_unix.close fd) (fun _ -> Lwt.return_unit) >>= fun () ->
Lwt.return (Error (`Exn exn)))

include Tcp_socket
17 changes: 7 additions & 10 deletions src/stack-unix/udpv6_socket.ml
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,13 @@
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*)

open Lwt
open Lwt.Infix

type buffer = Cstruct.t
type ipaddr = Ipaddr.V6.t
type flow = Lwt_unix.file_descr
type +'a io = 'a Lwt.t
type ip = Ipaddr.V6.t option (* source ip and port *)
type ipinput = unit Lwt.t
type callback = src:ipaddr -> dst:ipaddr -> src_port:int -> buffer -> unit io
type callback = src:ipaddr -> dst:ipaddr -> src_port:int -> Cstruct.t -> unit Lwt.t

type t = {
interface: Unix.inet_addr; (* source ip to bind to *)
Expand All @@ -35,12 +33,12 @@ let get_udpv6_listening_fd {listen_fds;interface} port =
Lwt.return @@ Hashtbl.find listen_fds (interface,port)
with Not_found ->
let fd = Lwt_unix.(socket PF_INET6 SOCK_DGRAM 0) in
Lwt_unix.bind fd (Lwt_unix.ADDR_INET (interface,port))
Lwt_unix.bind fd (Lwt_unix.ADDR_INET (interface, port))
>>= fun () ->
Hashtbl.add listen_fds (interface,port) fd;
Hashtbl.add listen_fds (interface, port) fd;
Lwt.return fd

(** IO operation errors *)

type error = [`Sendto_failed]

let pp_error ppf = function
Expand All @@ -54,10 +52,9 @@ let connect (id:ip) =
| None -> Ipaddr_unix.V6.to_inet_addr Ipaddr.V6.unspecified
| Some ip -> Ipaddr_unix.V6.to_inet_addr ip
in { interface; listen_fds }
in return t
in Lwt.return t

let disconnect _ =
return_unit
let disconnect _ = Lwt.return_unit

let id { interface; _ } =
Some (Ipaddr_unix.V6.of_inet_addr_exn interface)
Expand Down

0 comments on commit dea044b

Please sign in to comment.