1996-09-04 07:17:43 -07:00
|
|
|
/***********************************************************************/
|
|
|
|
/* */
|
2011-07-27 07:17:02 -07:00
|
|
|
/* OCaml */
|
1996-09-04 07:17:43 -07:00
|
|
|
/* */
|
|
|
|
/* Xavier Leroy and Pascal Cuoq, projet Cristal, INRIA Rocquencourt */
|
|
|
|
/* */
|
|
|
|
/* Copyright 1996 Institut National de Recherche en Informatique et */
|
1999-11-17 10:59:06 -08:00
|
|
|
/* en Automatique. All rights reserved. This file is distributed */
|
2001-12-07 05:41:02 -08:00
|
|
|
/* under the terms of the GNU Library General Public License, with */
|
|
|
|
/* the special exception on linking described in file ../../LICENSE. */
|
1996-09-04 07:17:43 -07:00
|
|
|
/* */
|
|
|
|
/***********************************************************************/
|
|
|
|
|
2008-07-14 00:45:03 -07:00
|
|
|
#include <errno.h>
|
1996-09-04 07:17:43 -07:00
|
|
|
#include <mlvalues.h>
|
2008-07-29 05:13:24 -07:00
|
|
|
#include <memory.h>
|
2002-06-07 02:49:45 -07:00
|
|
|
#include <alloc.h>
|
2008-07-29 05:13:24 -07:00
|
|
|
#include <fail.h>
|
1996-09-05 06:32:25 -07:00
|
|
|
#include "unixsupport.h"
|
2008-07-23 22:55:21 -07:00
|
|
|
#include "socketaddr.h"
|
1996-09-04 07:17:43 -07:00
|
|
|
|
2008-08-01 06:46:08 -07:00
|
|
|
#ifndef IPPROTO_IPV6
|
|
|
|
#define IPPROTO_IPV6 (-1)
|
|
|
|
#endif
|
|
|
|
#ifndef IPV6_V6ONLY
|
|
|
|
#define IPV6_V6ONLY (-1)
|
|
|
|
#endif
|
|
|
|
|
2008-07-14 00:45:03 -07:00
|
|
|
enum option_type {
|
|
|
|
TYPE_BOOL = 0,
|
|
|
|
TYPE_INT = 1,
|
|
|
|
TYPE_LINGER = 2,
|
|
|
|
TYPE_TIMEVAL = 3,
|
|
|
|
TYPE_UNIX_ERROR = 4
|
|
|
|
};
|
|
|
|
|
|
|
|
struct socket_option {
|
|
|
|
int level;
|
|
|
|
int option;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Table of options, indexed by type */
|
|
|
|
|
|
|
|
static struct socket_option sockopt_bool[] = {
|
|
|
|
{ SOL_SOCKET, SO_DEBUG },
|
|
|
|
{ SOL_SOCKET, SO_BROADCAST },
|
|
|
|
{ SOL_SOCKET, SO_REUSEADDR },
|
|
|
|
{ SOL_SOCKET, SO_KEEPALIVE },
|
|
|
|
{ SOL_SOCKET, SO_DONTROUTE },
|
|
|
|
{ SOL_SOCKET, SO_OOBINLINE },
|
|
|
|
{ SOL_SOCKET, SO_ACCEPTCONN },
|
2008-08-01 06:46:08 -07:00
|
|
|
{ IPPROTO_TCP, TCP_NODELAY },
|
|
|
|
{ IPPROTO_IPV6, IPV6_V6ONLY}
|
2008-07-14 00:45:03 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct socket_option sockopt_int[] = {
|
|
|
|
{ SOL_SOCKET, SO_SNDBUF },
|
|
|
|
{ SOL_SOCKET, SO_RCVBUF },
|
|
|
|
{ SOL_SOCKET, SO_ERROR },
|
|
|
|
{ SOL_SOCKET, SO_TYPE },
|
|
|
|
{ SOL_SOCKET, SO_RCVLOWAT },
|
|
|
|
{ SOL_SOCKET, SO_SNDLOWAT } };
|
|
|
|
|
|
|
|
static struct socket_option sockopt_linger[] = {
|
2008-07-23 22:55:21 -07:00
|
|
|
{ SOL_SOCKET, SO_LINGER }
|
2008-07-14 00:45:03 -07:00
|
|
|
};
|
|
|
|
|
2008-07-23 22:55:21 -07:00
|
|
|
static struct socket_option sockopt_timeval[] = {
|
2008-07-14 00:45:03 -07:00
|
|
|
{ SOL_SOCKET, SO_RCVTIMEO },
|
|
|
|
{ SOL_SOCKET, SO_SNDTIMEO }
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct socket_option sockopt_unix_error[] = {
|
|
|
|
{ SOL_SOCKET, SO_ERROR }
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct socket_option * sockopt_table[] = {
|
|
|
|
sockopt_bool,
|
|
|
|
sockopt_int,
|
|
|
|
sockopt_linger,
|
|
|
|
sockopt_timeval,
|
|
|
|
sockopt_unix_error
|
|
|
|
};
|
|
|
|
|
|
|
|
static char * getsockopt_fun_name[] = {
|
|
|
|
"getsockopt",
|
|
|
|
"getsockopt_int",
|
|
|
|
"getsockopt_optint",
|
|
|
|
"getsockopt_float",
|
|
|
|
"getsockopt_error"
|
|
|
|
};
|
|
|
|
|
|
|
|
static char * setsockopt_fun_name[] = {
|
|
|
|
"setsockopt",
|
|
|
|
"setsockopt_int",
|
|
|
|
"setsockopt_optint",
|
|
|
|
"setsockopt_float",
|
|
|
|
"setsockopt_error"
|
|
|
|
};
|
|
|
|
|
|
|
|
union option_value {
|
|
|
|
int i;
|
|
|
|
struct linger lg;
|
|
|
|
struct timeval tv;
|
|
|
|
};
|
2001-07-27 05:52:05 -07:00
|
|
|
|
2008-07-14 00:45:03 -07:00
|
|
|
CAMLexport value
|
|
|
|
unix_getsockopt_aux(char * name,
|
|
|
|
enum option_type ty, int level, int option,
|
|
|
|
value socket)
|
2001-07-27 05:52:05 -07:00
|
|
|
{
|
2008-07-14 00:45:03 -07:00
|
|
|
union option_value optval;
|
|
|
|
socklen_param_type optsize;
|
|
|
|
|
|
|
|
|
|
|
|
switch (ty) {
|
|
|
|
case TYPE_BOOL:
|
|
|
|
case TYPE_INT:
|
|
|
|
case TYPE_UNIX_ERROR:
|
|
|
|
optsize = sizeof(optval.i); break;
|
|
|
|
case TYPE_LINGER:
|
|
|
|
optsize = sizeof(optval.lg); break;
|
|
|
|
case TYPE_TIMEVAL:
|
|
|
|
optsize = sizeof(optval.tv); break;
|
|
|
|
default:
|
|
|
|
unix_error(EINVAL, name, Nothing);
|
|
|
|
}
|
2001-07-27 05:52:05 -07:00
|
|
|
|
2008-07-14 00:45:03 -07:00
|
|
|
if (getsockopt(Socket_val(socket), level, option,
|
2002-07-23 07:12:03 -07:00
|
|
|
(void *) &optval, &optsize) == -1)
|
2008-07-14 00:45:03 -07:00
|
|
|
uerror(name, Nothing);
|
|
|
|
|
|
|
|
switch (ty) {
|
|
|
|
case TYPE_BOOL:
|
|
|
|
case TYPE_INT:
|
|
|
|
return Val_int(optval.i);
|
|
|
|
case TYPE_LINGER:
|
|
|
|
if (optval.lg.l_onoff == 0) {
|
|
|
|
return Val_int(0); /* None */
|
|
|
|
} else {
|
|
|
|
value res = alloc_small(1, 0); /* Some */
|
|
|
|
Field(res, 0) = Val_int(optval.lg.l_linger);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
case TYPE_TIMEVAL:
|
|
|
|
return copy_double((double) optval.tv.tv_sec
|
|
|
|
+ (double) optval.tv.tv_usec / 1e6);
|
|
|
|
case TYPE_UNIX_ERROR:
|
|
|
|
if (optval.i == 0) {
|
|
|
|
return Val_int(0); /* None */
|
|
|
|
} else {
|
|
|
|
value err, res;
|
|
|
|
err = unix_error_of_code(optval.i);
|
|
|
|
Begin_root(err);
|
|
|
|
res = alloc_small(1, 0); /* Some */
|
|
|
|
Field(res, 0) = err;
|
|
|
|
End_roots();
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
unix_error(EINVAL, name, Nothing);
|
2008-07-23 22:55:21 -07:00
|
|
|
return Val_unit; /* Avoid warning */
|
2001-07-27 05:52:05 -07:00
|
|
|
}
|
1996-09-04 07:17:43 -07:00
|
|
|
}
|
2001-07-27 05:52:05 -07:00
|
|
|
|
2008-07-23 22:55:21 -07:00
|
|
|
CAMLexport value
|
2008-07-14 00:45:03 -07:00
|
|
|
unix_setsockopt_aux(char * name,
|
|
|
|
enum option_type ty, int level, int option,
|
|
|
|
value socket, value val)
|
2001-07-27 05:52:05 -07:00
|
|
|
{
|
2008-07-14 00:45:03 -07:00
|
|
|
union option_value optval;
|
|
|
|
socklen_param_type optsize;
|
|
|
|
double f;
|
|
|
|
|
|
|
|
switch (ty) {
|
|
|
|
case TYPE_BOOL:
|
|
|
|
case TYPE_INT:
|
|
|
|
optsize = sizeof(optval.i);
|
|
|
|
optval.i = Int_val(val);
|
|
|
|
break;
|
|
|
|
case TYPE_LINGER:
|
|
|
|
optsize = sizeof(optval.lg);
|
|
|
|
optval.lg.l_onoff = Is_block (val);
|
|
|
|
if (optval.lg.l_onoff)
|
|
|
|
optval.lg.l_linger = Int_val (Field (val, 0));
|
|
|
|
break;
|
|
|
|
case TYPE_TIMEVAL:
|
|
|
|
f = Double_val(val);
|
|
|
|
optsize = sizeof(optval.tv);
|
|
|
|
optval.tv.tv_sec = (int) f;
|
|
|
|
optval.tv.tv_usec = (int) (1e6 * (f - optval.tv.tv_sec));
|
|
|
|
break;
|
|
|
|
case TYPE_UNIX_ERROR:
|
|
|
|
default:
|
|
|
|
unix_error(EINVAL, name, Nothing);
|
|
|
|
}
|
2001-07-27 05:52:05 -07:00
|
|
|
|
2008-07-14 00:45:03 -07:00
|
|
|
if (setsockopt(Socket_val(socket), level, option,
|
|
|
|
(void *) &optval, optsize) == -1)
|
|
|
|
uerror(name, Nothing);
|
2001-07-27 05:52:05 -07:00
|
|
|
|
|
|
|
return Val_unit;
|
|
|
|
}
|
|
|
|
|
2008-07-14 00:45:03 -07:00
|
|
|
CAMLprim value unix_getsockopt(value vty, value vsocket, value voption)
|
2001-07-27 05:52:05 -07:00
|
|
|
{
|
2008-07-14 00:45:03 -07:00
|
|
|
enum option_type ty = Int_val(vty);
|
|
|
|
struct socket_option * opt = &(sockopt_table[ty][Int_val(voption)]);
|
|
|
|
return unix_getsockopt_aux(getsockopt_fun_name[ty],
|
|
|
|
ty,
|
|
|
|
opt->level,
|
|
|
|
opt->option,
|
|
|
|
vsocket);
|
2001-07-27 05:52:05 -07:00
|
|
|
}
|
|
|
|
|
2008-07-14 00:45:03 -07:00
|
|
|
CAMLprim value unix_setsockopt(value vty, value vsocket, value voption,
|
|
|
|
value val)
|
2001-07-27 05:52:05 -07:00
|
|
|
{
|
2008-07-14 00:45:03 -07:00
|
|
|
enum option_type ty = Int_val(vty);
|
|
|
|
struct socket_option * opt = &(sockopt_table[ty][Int_val(voption)]);
|
|
|
|
return unix_setsockopt_aux(setsockopt_fun_name[ty],
|
|
|
|
ty,
|
|
|
|
opt->level,
|
|
|
|
opt->option,
|
|
|
|
vsocket,
|
|
|
|
val);
|
2001-07-27 05:52:05 -07:00
|
|
|
}
|