|
| 1 | +#include <stdlib.h> |
| 2 | +#include <unistd.h> |
| 3 | +#include <sys/socket.h> |
| 4 | + |
| 5 | +#ifdef __linux__ |
| 6 | +#include <netinet/in.h> |
| 7 | +#include <netinet/tcp.h> |
| 8 | +#endif |
| 9 | + |
| 10 | +#include <caml/mlvalues.h> |
| 11 | +#include <caml/unixsupport.h> |
| 12 | +#include <caml/socketaddr.h> |
| 13 | + |
| 14 | +#ifndef TCP_CORK |
| 15 | +#define TCP_CORK (-1) |
| 16 | +#endif |
| 17 | + |
| 18 | +#ifndef TCP_KEEPCNT |
| 19 | +#define TCP_KEEPCNT (-1) |
| 20 | +#endif |
| 21 | + |
| 22 | +#ifndef TCP_KEEPIDLE |
| 23 | +#define TCP_KEEPIDLE (-1) |
| 24 | +#endif |
| 25 | + |
| 26 | +#ifndef TCP_KEEPINTVL |
| 27 | +#define TCP_KEEPINTVL (-1) |
| 28 | +#endif |
| 29 | + |
| 30 | +struct socket_option { |
| 31 | + int level; |
| 32 | + int option; |
| 33 | +}; |
| 34 | + |
| 35 | +/* Not exported by caml/sockaddr.h */ |
| 36 | +CAMLexport value caml_unix_getsockopt_aux(char *, int, int, int, value); |
| 37 | +CAMLexport value caml_unix_setsockopt_aux(char *, int, int, int, value, value); |
| 38 | + |
| 39 | +static struct socket_option sockopt_int[] = { |
| 40 | + { IPPROTO_TCP, TCP_CORK }, |
| 41 | + { IPPROTO_TCP, TCP_KEEPCNT }, |
| 42 | + { IPPROTO_TCP, TCP_KEEPIDLE }, |
| 43 | + { IPPROTO_TCP, TCP_KEEPINTVL } |
| 44 | +}; |
| 45 | + |
| 46 | +CAMLprim value eio_unix_getsockopt_int(value vsocket, value voption) |
| 47 | +{ |
| 48 | + struct socket_option *opt = &(sockopt_int[Int_val(voption)]); |
| 49 | + return caml_unix_getsockopt_aux("eio_unix_getsockopt_int", |
| 50 | + 1, /* TYPE_INT */ |
| 51 | + opt->level, |
| 52 | + opt->option, |
| 53 | + vsocket); |
| 54 | +} |
| 55 | + |
| 56 | +CAMLprim value eio_unix_setsockopt_int(value vsocket, value voption, value val) |
| 57 | +{ |
| 58 | + struct socket_option *opt = &(sockopt_int[Int_val(voption)]); |
| 59 | + return caml_unix_setsockopt_aux("eio_unix_setsockopt_int", |
| 60 | + 1, /* TYPE_INT */ |
| 61 | + opt->level, |
| 62 | + opt->option, |
| 63 | + vsocket, |
| 64 | + val); |
| 65 | +} |
0 commit comments