@@ -650,11 +650,6 @@ module Sockopt = struct
650650 let ip_ttl = 2 (* IP_TTL *)
651651 let ip_mtu = 14 (* IP_MTU *)
652652 let ip_mtu_discover = 10 (* IP_MTU_DISCOVER *)
653- (* IP_MTU_DISCOVER values *)
654- let ip_pmtudisc_want = 1 (* IP_PMTUDISC_WANT *)
655- let ip_pmtudisc_dont = 0 (* IP_PMTUDISC_DONT *)
656- let ip_pmtudisc_do = 2 (* IP_PMTUDISC_DO *)
657- let ip_pmtudisc_probe = 3 (* IP_PMTUDISC_PROBE *)
658653
659654 external setsockopt_int : Unix .file_descr -> int -> int -> int -> unit = " caml_eio_sockopt_int_set"
660655 external getsockopt_int : Unix .file_descr -> int -> int -> int = " caml_eio_sockopt_int_get"
@@ -683,77 +678,100 @@ module Sockopt = struct
683678 | IP_MTU : int Eio.Net.Sockopt .t
684679 | IP_MTU_DISCOVER : [`Want | `Dont | `Do | `Probe ] Eio.Net.Sockopt .t
685680
681+ let pp : type a. a Eio.Net.Sockopt.t -> Format.formatter -> a -> unit = fun opt f v ->
682+ match opt with
683+ | TCP_CORK -> Fmt. pf f " TCP_CORK = %b" v
684+ | TCP_KEEPIDLE -> Fmt. pf f " TCP_KEEPIDLE = %d" v
685+ | TCP_KEEPINTVL -> Fmt. pf f " TCP_KEEPINTVL = %d" v
686+ | TCP_KEEPCNT -> Fmt. pf f " TCP_KEEPCNT = %d" v
687+ | TCP_USER_TIMEOUT -> Fmt. pf f " TCP_USER_TIMEOUT = %d" v
688+ | TCP_MAXSEG -> Fmt. pf f " TCP_MAXSEG = %d" v
689+ | TCP_LINGER2 -> Fmt. (pf f " TCP_LINGER2 = %a" (option ~none: (any " <none>" ) int ) v)
690+ | TCP_DEFER_ACCEPT -> Fmt. pf f " TCP_DEFER_ACCEPT = %d" v
691+ | TCP_CONGESTION -> Fmt. pf f " TCP_CONGESTION = %s" v
692+ | TCP_SYNCNT -> Fmt. pf f " TCP_SYNCNT = %d" v
693+ | TCP_WINDOW_CLAMP -> Fmt. pf f " TCP_WINDOW_CLAMP = %d" v
694+ | TCP_QUICKACK -> Fmt. pf f " TCP_QUICKACK = %b" v
695+ | TCP_FASTOPEN -> Fmt. pf f " TCP_FASTOPEN = %d" v
696+ | IP_FREEBIND -> Fmt. pf f " IP_FREEBIND = %b" v
697+ | IP_BIND_ADDRESS_NO_PORT -> Fmt. pf f " IP_BIND_ADDRESS_NO_PORT = %b" v
698+ | IP_LOCAL_PORT_RANGE ->
699+ let (lower, upper) = v in
700+ Fmt. pf f " IP_LOCAL_PORT_RANGE = (%d, %d)" lower upper
701+ | IP_TTL -> Fmt. pf f " IP_TTL = %d" v
702+ | IP_MTU -> Fmt. pf f " IP_MTU = %d" v
703+ | IP_MTU_DISCOVER ->
704+ let s = match v with
705+ | `Want -> " Want"
706+ | `Dont -> " Dont"
707+ | `Do -> " Do"
708+ | `Probe -> " Probe"
709+ in
710+ Fmt. pf f " IP_MTU_DISCOVER = %s" s
711+ | _ -> Eio.Net.Sockopt. pp opt f v
712+
713+ let with_fd_set fd fn =
714+ Fd. use_exn " setsockopt" fd fn
715+
716+ let with_fd_get fd fn =
717+ Fd. use_exn " getsockopt" fd fn
718+
686719 let set : type a. Fd.t -> a Eio.Net.Sockopt.t -> a -> unit = fun fd opt v ->
687720 match opt with
688721 | TCP_CORK ->
689- Fd. use_exn " setsockopt" fd (fun fd ->
690- setsockopt_int fd ipproto_tcp tcp_cork (if v then 1 else 0 ))
722+ with_fd_set fd (fun fd -> setsockopt_int fd ipproto_tcp tcp_cork (if v then 1 else 0 ))
691723 | TCP_KEEPIDLE ->
692724 if v < 0 then
693725 invalid_arg (Printf. sprintf " TCP_KEEPIDLE must be non-negative, got %d" v);
694- Fd. use_exn " setsockopt" fd (fun fd ->
695- setsockopt_int fd ipproto_tcp tcp_keepidle v)
726+ with_fd_set fd (fun fd -> setsockopt_int fd ipproto_tcp tcp_keepidle v)
696727 | TCP_KEEPINTVL ->
697728 if v < 0 then
698729 invalid_arg (Printf. sprintf " TCP_KEEPINTVL must be non-negative, got %d" v);
699- Fd. use_exn " setsockopt" fd (fun fd ->
700- setsockopt_int fd ipproto_tcp tcp_keepintvl v)
730+ with_fd_set fd (fun fd -> setsockopt_int fd ipproto_tcp tcp_keepintvl v)
701731 | TCP_KEEPCNT ->
702732 if v < 0 then
703733 invalid_arg (Printf. sprintf " TCP_KEEPCNT must be non-negative, got %d" v);
704- Fd. use_exn " setsockopt" fd (fun fd ->
705- setsockopt_int fd ipproto_tcp tcp_keepcnt v)
734+ with_fd_set fd (fun fd -> setsockopt_int fd ipproto_tcp tcp_keepcnt v)
706735 | TCP_USER_TIMEOUT ->
707736 if v < 0 then
708737 invalid_arg (Printf. sprintf " TCP_USER_TIMEOUT must be non-negative, got %d" v);
709- Fd. use_exn " setsockopt" fd (fun fd ->
710- setsockopt_int fd ipproto_tcp tcp_user_timeout v)
738+ with_fd_set fd (fun fd -> setsockopt_int fd ipproto_tcp tcp_user_timeout v)
711739 | TCP_MAXSEG ->
712740 if v < 0 then
713741 invalid_arg (Printf. sprintf " TCP_MAXSEG must be non-negative, got %d" v);
714- Fd. use_exn " setsockopt" fd (fun fd ->
715- setsockopt_int fd ipproto_tcp tcp_maxseg v)
742+ with_fd_set fd (fun fd -> setsockopt_int fd ipproto_tcp tcp_maxseg v)
716743 | TCP_LINGER2 ->
717744 let v = match v with
718745 | None -> - 1
719746 | Some n when n < 0 ->
720747 invalid_arg (Printf. sprintf " TCP_LINGER2 must be non-negative, got %d" n);
721748 | Some n -> n
722749 in
723- Fd. use_exn " setsockopt" fd (fun fd ->
724- setsockopt_int fd ipproto_tcp tcp_linger2 v)
750+ with_fd_set fd (fun fd -> setsockopt_int fd ipproto_tcp tcp_linger2 v)
725751 | TCP_DEFER_ACCEPT ->
726752 if v < 0 then
727753 invalid_arg (Printf. sprintf " TCP_DEFER_ACCEPT must be non-negative, got %d" v);
728- Fd. use_exn " setsockopt" fd (fun fd ->
729- setsockopt_int fd ipproto_tcp tcp_defer_accept v)
754+ with_fd_set fd (fun fd -> setsockopt_int fd ipproto_tcp tcp_defer_accept v)
730755 | TCP_CONGESTION ->
731- Fd. use_exn " setsockopt" fd (fun fd ->
732- setsockopt_string fd ipproto_tcp tcp_congestion v)
756+ with_fd_set fd (fun fd -> setsockopt_string fd ipproto_tcp tcp_congestion v)
733757 | TCP_SYNCNT ->
734758 if v < 1 || v > 255 then
735759 invalid_arg (Printf. sprintf " TCP_SYNCNT must be between 1 and 255, got %d" v);
736- Fd. use_exn " setsockopt" fd (fun fd ->
737- setsockopt_int fd ipproto_tcp tcp_syncnt v)
760+ with_fd_set fd (fun fd -> setsockopt_int fd ipproto_tcp tcp_syncnt v)
738761 | TCP_WINDOW_CLAMP ->
739762 if v < 0 then
740763 invalid_arg (Printf. sprintf " TCP_WINDOW_CLAMP must be non-negative, got %d" v);
741- Fd. use_exn " setsockopt" fd (fun fd ->
742- setsockopt_int fd ipproto_tcp tcp_window_clamp v)
764+ with_fd_set fd (fun fd -> setsockopt_int fd ipproto_tcp tcp_window_clamp v)
743765 | TCP_QUICKACK ->
744- Fd. use_exn " setsockopt" fd (fun fd ->
745- setsockopt_int fd ipproto_tcp tcp_quickack (if v then 1 else 0 ))
766+ with_fd_set fd (fun fd -> setsockopt_int fd ipproto_tcp tcp_quickack (if v then 1 else 0 ))
746767 | TCP_FASTOPEN ->
747768 if v < 0 then
748769 invalid_arg (Printf. sprintf " TCP_FASTOPEN queue length must be non-negative, got %d" v);
749- Fd. use_exn " setsockopt" fd (fun fd ->
750- setsockopt_int fd ipproto_tcp tcp_fastopen v)
770+ with_fd_set fd (fun fd -> setsockopt_int fd ipproto_tcp tcp_fastopen v)
751771 | IP_FREEBIND ->
752- Fd. use_exn " setsockopt" fd (fun fd ->
753- setsockopt_int fd ipproto_ip ip_freebind (if v then 1 else 0 ))
772+ with_fd_set fd (fun fd -> setsockopt_int fd ipproto_ip ip_freebind (if v then 1 else 0 ))
754773 | IP_BIND_ADDRESS_NO_PORT ->
755- Fd. use_exn " setsockopt" fd (fun fd ->
756- setsockopt_int fd ipproto_ip ip_bind_address_no_port (if v then 1 else 0 ))
774+ with_fd_set fd (fun fd -> setsockopt_int fd ipproto_ip ip_bind_address_no_port (if v then 1 else 0 ))
757775 | IP_LOCAL_PORT_RANGE ->
758776 let (lower, upper) = v in
759777 if lower < 0 || lower > 65535 then
@@ -763,95 +781,73 @@ module Sockopt = struct
763781 if lower <> 0 && upper <> 0 && lower > upper then
764782 invalid_arg (Printf. sprintf " IP_LOCAL_PORT_RANGE lower bound (%d) must be <= upper bound (%d)" lower upper);
765783 let combined = (upper lsl 16 ) lor lower in
766- Fd. use_exn " setsockopt " fd (fun fd ->
784+ with_fd_set fd (fun fd ->
767785 setsockopt_int fd ipproto_ip ip_local_port_range combined)
768786 | IP_TTL ->
769787 if v < 1 || v > 255 then
770788 invalid_arg (Printf. sprintf " IP_TTL must be between 1 and 255, got %d" v);
771- Fd. use_exn " setsockopt" fd (fun fd ->
772- setsockopt_int fd ipproto_ip ip_ttl v)
789+ with_fd_set fd (fun fd -> setsockopt_int fd ipproto_ip ip_ttl v)
773790 | IP_MTU ->
774791 invalid_arg " IP_MTU is a read-only socket option"
775792 | IP_MTU_DISCOVER ->
776- let intval = match v with
777- | `Want -> ip_pmtudisc_want
778- | `Dont -> ip_pmtudisc_dont
779- | `Do -> ip_pmtudisc_do
780- | `Probe -> ip_pmtudisc_probe
781- in
782- Fd. use_exn " setsockopt" fd (fun fd ->
783- setsockopt_int fd ipproto_ip ip_mtu_discover intval)
793+ let i = match v with
794+ | `Dont -> 0
795+ | `Want -> 1
796+ | `Do -> 2
797+ | `Probe -> 3 in
798+ with_fd_set fd (fun fd -> setsockopt_int fd ipproto_ip ip_mtu_discover i)
784799 | _ -> Eio_unix.Net.Sockopt. set fd opt v
785800
786801 let get : type a. Fd.t -> a Eio.Net.Sockopt.t -> a = fun fd opt ->
787802 match opt with
788803 | TCP_CORK ->
789- Fd. use_exn " getsockopt" fd (fun fd ->
790- getsockopt_int fd ipproto_tcp tcp_cork <> 0 )
804+ with_fd_get fd (fun fd -> getsockopt_int fd ipproto_tcp tcp_cork <> 0 )
791805 | TCP_KEEPIDLE ->
792- Fd. use_exn " getsockopt" fd (fun fd ->
793- getsockopt_int fd ipproto_tcp tcp_keepidle)
806+ with_fd_get fd (fun fd -> getsockopt_int fd ipproto_tcp tcp_keepidle)
794807 | TCP_KEEPINTVL ->
795- Fd. use_exn " getsockopt" fd (fun fd ->
796- getsockopt_int fd ipproto_tcp tcp_keepintvl)
808+ with_fd_get fd (fun fd -> getsockopt_int fd ipproto_tcp tcp_keepintvl)
797809 | TCP_KEEPCNT ->
798- Fd. use_exn " getsockopt" fd (fun fd ->
799- getsockopt_int fd ipproto_tcp tcp_keepcnt)
810+ with_fd_get fd (fun fd -> getsockopt_int fd ipproto_tcp tcp_keepcnt)
800811 | TCP_USER_TIMEOUT ->
801- Fd. use_exn " getsockopt" fd (fun fd ->
802- getsockopt_int fd ipproto_tcp tcp_user_timeout)
812+ with_fd_get fd (fun fd -> getsockopt_int fd ipproto_tcp tcp_user_timeout)
803813 | TCP_MAXSEG ->
804- Fd. use_exn " getsockopt" fd (fun fd ->
805- getsockopt_int fd ipproto_tcp tcp_maxseg)
814+ with_fd_get fd (fun fd -> getsockopt_int fd ipproto_tcp tcp_maxseg)
806815 | TCP_LINGER2 ->
807- let v = Fd. use_exn " getsockopt" fd (fun fd ->
808- getsockopt_int fd ipproto_tcp tcp_linger2)
809- in
816+ let v = with_fd_get fd (fun fd -> getsockopt_int fd ipproto_tcp tcp_linger2) in
810817 if v = - 1 then None else Some v
811818 | TCP_DEFER_ACCEPT ->
812- Fd. use_exn " getsockopt" fd (fun fd ->
813- getsockopt_int fd ipproto_tcp tcp_defer_accept)
819+ with_fd_get fd (fun fd -> getsockopt_int fd ipproto_tcp tcp_defer_accept)
814820 | TCP_CONGESTION ->
815- Fd. use_exn " getsockopt" fd (fun fd ->
816- getsockopt_string fd ipproto_tcp tcp_congestion)
821+ with_fd_get fd (fun fd -> getsockopt_string fd ipproto_tcp tcp_congestion)
817822 | TCP_SYNCNT ->
818- Fd. use_exn " getsockopt" fd (fun fd ->
819- getsockopt_int fd ipproto_tcp tcp_syncnt)
823+ with_fd_get fd (fun fd -> getsockopt_int fd ipproto_tcp tcp_syncnt)
820824 | TCP_WINDOW_CLAMP ->
821- Fd. use_exn " getsockopt" fd (fun fd ->
822- getsockopt_int fd ipproto_tcp tcp_window_clamp)
825+ with_fd_get fd (fun fd -> getsockopt_int fd ipproto_tcp tcp_window_clamp)
823826 | TCP_QUICKACK ->
824- Fd. use_exn " getsockopt" fd (fun fd ->
825- getsockopt_int fd ipproto_tcp tcp_quickack <> 0 )
827+ with_fd_get fd (fun fd -> getsockopt_int fd ipproto_tcp tcp_quickack <> 0 )
826828 | TCP_FASTOPEN ->
827- Fd. use_exn " getsockopt" fd (fun fd ->
828- getsockopt_int fd ipproto_tcp tcp_fastopen)
829+ with_fd_get fd (fun fd -> getsockopt_int fd ipproto_tcp tcp_fastopen)
829830 | IP_FREEBIND ->
830- Fd. use_exn " getsockopt" fd (fun fd ->
831- getsockopt_int fd ipproto_ip ip_freebind <> 0 )
831+ with_fd_get fd (fun fd -> getsockopt_int fd ipproto_ip ip_freebind <> 0 )
832832 | IP_BIND_ADDRESS_NO_PORT ->
833- Fd. use_exn " getsockopt" fd (fun fd ->
834- getsockopt_int fd ipproto_ip ip_bind_address_no_port <> 0 )
833+ with_fd_get fd (fun fd -> getsockopt_int fd ipproto_ip ip_bind_address_no_port <> 0 )
835834 | IP_LOCAL_PORT_RANGE ->
836- Fd. use_exn " getsockopt " fd (fun fd ->
835+ with_fd_get fd (fun fd ->
837836 let combined = getsockopt_int fd ipproto_ip ip_local_port_range in
838837 let lower = combined land 0xFFFF in
839838 let upper = (combined lsr 16 ) land 0xFFFF in
840- ( lower, upper) )
839+ lower, upper)
841840 | IP_TTL ->
842- Fd. use_exn " getsockopt" fd (fun fd ->
843- getsockopt_int fd ipproto_ip ip_ttl)
841+ with_fd_get fd (fun fd -> getsockopt_int fd ipproto_ip ip_ttl)
844842 | IP_MTU ->
845- Fd. use_exn " getsockopt" fd (fun fd ->
846- getsockopt_int fd ipproto_ip ip_mtu)
847- | IP_MTU_DISCOVER ->
848- let intval = Fd. use_exn " getsockopt" fd (fun fd ->
849- getsockopt_int fd ipproto_ip ip_mtu_discover)
850- in
851- if intval = ip_pmtudisc_want then `Want
852- else if intval = ip_pmtudisc_dont then `Dont
853- else if intval = ip_pmtudisc_do then `Do
854- else if intval = ip_pmtudisc_probe then `Probe
855- else failwith (Printf. sprintf " Unknown IP_MTU_DISCOVER value: %d" intval)
843+ with_fd_get fd (fun fd -> getsockopt_int fd ipproto_ip ip_mtu)
844+ | IP_MTU_DISCOVER -> begin
845+ let i = with_fd_get fd (fun fd -> getsockopt_int fd ipproto_ip ip_mtu_discover) in
846+ match i with
847+ | 0 (* IP_PMTUDISC_DONT *) -> `Dont
848+ | 1 (* IP_PMTUDISC_WANT *) -> `Want
849+ | 2 (* IP_PMTUDISC_DO *) -> `Do
850+ | 3 (* IP_PMTUDISC_PROBE *) -> `Probe
851+ | i -> failwith (Printf. sprintf " Unknown IP_MTU_DISCOVER value: %d" i) end
856852 | _ -> Eio_unix.Net.Sockopt. get fd opt
857853end
0 commit comments