-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add Eio_unix.Sockopt for setting socket options
- Loading branch information
Showing
7 changed files
with
142 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
#include <stdlib.h> | ||
#include <unistd.h> | ||
#include <sys/socket.h> | ||
|
||
#ifdef __linux__ | ||
#include <netinet/in.h> | ||
#include <netinet/tcp.h> | ||
#endif | ||
|
||
#include <caml/mlvalues.h> | ||
#include <caml/unixsupport.h> | ||
#include <caml/socketaddr.h> | ||
|
||
#ifndef TCP_CORK | ||
#define TCP_CORK (-1) | ||
#endif | ||
|
||
#ifndef TCP_KEEPCNT | ||
#define TCP_KEEPCNT (-1) | ||
#endif | ||
|
||
#ifndef TCP_KEEPIDLE | ||
#define TCP_KEEPIDLE (-1) | ||
#endif | ||
|
||
#ifndef TCP_KEEPINTVL | ||
#define TCP_KEEPINTVL (-1) | ||
#endif | ||
|
||
struct socket_option { | ||
int level; | ||
int option; | ||
}; | ||
|
||
/* Not exported by caml/sockaddr.h */ | ||
CAMLexport value caml_unix_getsockopt_aux(char *, int, int, int, value); | ||
CAMLexport value caml_unix_setsockopt_aux(char *, int, int, int, value, value); | ||
|
||
static struct socket_option sockopt_int[] = { | ||
{ IPPROTO_TCP, TCP_CORK }, | ||
{ IPPROTO_TCP, TCP_KEEPCNT }, | ||
{ IPPROTO_TCP, TCP_KEEPIDLE }, | ||
{ IPPROTO_TCP, TCP_KEEPINTVL } | ||
}; | ||
|
||
CAMLprim value eio_unix_getsockopt_int(value vsocket, value voption) | ||
{ | ||
struct socket_option *opt = &(sockopt_int[Int_val(voption)]); | ||
return caml_unix_getsockopt_aux("eio_unix_getsockopt_int", | ||
1, /* TYPE_INT */ | ||
opt->level, | ||
opt->option, | ||
vsocket); | ||
} | ||
|
||
CAMLprim value eio_unix_setsockopt_int(value vsocket, value voption, value val) | ||
{ | ||
struct socket_option *opt = &(sockopt_int[Int_val(voption)]); | ||
return caml_unix_setsockopt_aux("eio_unix_setsockopt_int", | ||
1, /* TYPE_INT */ | ||
opt->level, | ||
opt->option, | ||
vsocket, | ||
val); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
type socket_int_option = | ||
EIO_TCP_CORK | ||
| EIO_TCP_KEEPCNT | ||
| EIO_TCP_KEEPIDLE | ||
| EIO_TCP_KEEPINTVL | ||
|
||
external setsockopt_int : Unix.file_descr -> socket_int_option -> int -> unit = | ||
"eio_unix_setsockopt_int" | ||
external getsockopt_int : Unix.file_descr -> socket_int_option -> int = | ||
"eio_unix_getsockopt_int" | ||
|
||
type _ t = | ||
| SO_KEEPALIVE : bool t | ||
| SO_REUSEADDR : bool t | ||
| SO_REUSEPORT : bool t | ||
| TCP_CORK : int t | ||
| TCP_KEEPCNT : int t | ||
| TCP_KEEPIDLE : int t | ||
| TCP_KEEPINTVL : int t | ||
|
||
let set : type a . Fd.t -> a t -> a -> unit = fun sock k v -> | ||
Fd.use_exn "Sockaddr.set" sock @@ fun fd -> | ||
match k with | ||
| TCP_CORK -> setsockopt_int fd EIO_TCP_CORK v | ||
| TCP_KEEPCNT -> setsockopt_int fd EIO_TCP_KEEPCNT v | ||
| TCP_KEEPIDLE -> setsockopt_int fd EIO_TCP_KEEPIDLE v | ||
| TCP_KEEPINTVL -> setsockopt_int fd EIO_TCP_KEEPINTVL v | ||
| SO_KEEPALIVE -> Unix.(setsockopt fd SO_KEEPALIVE v) | ||
| SO_REUSEADDR -> Unix.(setsockopt fd SO_REUSEADDR v) | ||
| SO_REUSEPORT -> Unix.(setsockopt fd SO_REUSEPORT v) | ||
|
||
let get_descr : type a . Unix.file_descr -> a t -> a = fun fd k -> | ||
match k with | ||
| TCP_CORK -> getsockopt_int fd EIO_TCP_CORK | ||
| TCP_KEEPCNT -> getsockopt_int fd EIO_TCP_KEEPCNT | ||
| TCP_KEEPIDLE -> getsockopt_int fd EIO_TCP_KEEPIDLE | ||
| TCP_KEEPINTVL -> getsockopt_int fd EIO_TCP_KEEPINTVL | ||
| SO_KEEPALIVE -> Unix.(getsockopt fd SO_KEEPALIVE) | ||
| SO_REUSEADDR -> Unix.(getsockopt fd SO_REUSEADDR) | ||
| SO_REUSEPORT -> Unix.(getsockopt fd SO_REUSEPORT) | ||
|
||
let get : type a . Fd.t -> a t -> a = fun sock k -> | ||
Fd.use_exn "Sockaddr.get" sock (fun fd -> get_descr fd k) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
type _ t = | ||
| SO_KEEPALIVE : bool t | ||
| SO_REUSEADDR : bool t | ||
| SO_REUSEPORT : bool t | ||
| TCP_CORK : int t | ||
| TCP_KEEPCNT : int t | ||
| TCP_KEEPIDLE : int t | ||
| TCP_KEEPINTVL : int t | ||
|
||
val set : Fd.t -> 'a t -> 'a -> unit | ||
val get : Fd.t -> 'a t -> 'a |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters