1996-09-04 07:17:43 -07:00
|
|
|
/***********************************************************************/
|
|
|
|
/* */
|
|
|
|
/* Objective Caml */
|
|
|
|
/* */
|
|
|
|
/* 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
|
|
|
/* */
|
|
|
|
/***********************************************************************/
|
|
|
|
|
|
|
|
/* $Id$ */
|
|
|
|
|
|
|
|
#include <mlvalues.h>
|
2002-06-07 02:49:45 -07:00
|
|
|
#include <alloc.h>
|
1996-09-05 06:32:25 -07:00
|
|
|
#include "unixsupport.h"
|
1996-09-04 07:17:43 -07:00
|
|
|
|
2001-07-27 05:52:05 -07:00
|
|
|
static int sockopt_bool[] = {
|
|
|
|
SO_DEBUG, SO_BROADCAST, SO_REUSEADDR, SO_KEEPALIVE,
|
|
|
|
SO_DONTROUTE, SO_OOBINLINE, SO_ACCEPTCONN };
|
1996-09-04 07:17:43 -07:00
|
|
|
|
2001-07-27 05:52:05 -07:00
|
|
|
static int sockopt_int[] = {
|
|
|
|
SO_SNDBUF, SO_RCVBUF, SO_ERROR, SO_TYPE, SO_RCVLOWAT, SO_SNDLOWAT };
|
|
|
|
|
|
|
|
static int sockopt_optint[] = { SO_LINGER };
|
|
|
|
|
|
|
|
static int sockopt_float[] = { SO_RCVTIMEO, SO_SNDTIMEO };
|
|
|
|
|
2001-10-09 06:36:33 -07:00
|
|
|
CAMLprim value getsockopt_int(int *sockopt, value socket,
|
|
|
|
int level, value option)
|
1996-09-04 07:17:43 -07:00
|
|
|
{
|
2001-07-27 05:52:05 -07:00
|
|
|
int optval;
|
|
|
|
int optsize;
|
|
|
|
|
1996-09-04 07:17:43 -07:00
|
|
|
optsize = sizeof(optval);
|
2002-04-30 08:00:48 -07:00
|
|
|
if (getsockopt(Socket_val(socket),
|
2001-10-09 06:36:33 -07:00
|
|
|
level, sockopt[Int_val(option)],
|
2001-07-27 05:52:05 -07:00
|
|
|
(void *) &optval, &optsize) == -1)
|
|
|
|
uerror("getsockopt", Nothing);
|
1996-09-04 07:17:43 -07:00
|
|
|
return Val_int(optval);
|
|
|
|
}
|
|
|
|
|
2001-10-09 06:36:33 -07:00
|
|
|
CAMLprim value setsockopt_int(int *sockopt, value socket, int level,
|
|
|
|
value option, value status)
|
1996-09-04 07:17:43 -07:00
|
|
|
{
|
|
|
|
int optval = Int_val(status);
|
2002-04-30 08:00:48 -07:00
|
|
|
if (setsockopt(Socket_val(socket),
|
2001-10-09 06:36:33 -07:00
|
|
|
level, sockopt[Int_val(option)],
|
2001-07-27 05:52:05 -07:00
|
|
|
(void *) &optval, sizeof(optval)) == -1)
|
|
|
|
uerror("setsockopt", Nothing);
|
|
|
|
return Val_unit;
|
|
|
|
}
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLprim value unix_getsockopt_bool(value socket, value option) {
|
2001-10-09 06:36:33 -07:00
|
|
|
return getsockopt_int(sockopt_bool, socket, SOL_SOCKET, option);
|
2001-07-27 05:52:05 -07:00
|
|
|
}
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLprim value unix_setsockopt_bool(value socket, value option, value status)
|
2001-07-27 05:52:05 -07:00
|
|
|
{
|
2001-10-09 06:36:33 -07:00
|
|
|
return setsockopt_int(sockopt_bool, socket, SOL_SOCKET, option, status);
|
2001-07-27 05:52:05 -07:00
|
|
|
}
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLprim value unix_getsockopt_int(value socket, value option) {
|
2001-10-09 06:36:33 -07:00
|
|
|
return getsockopt_int(sockopt_int, socket, SOL_SOCKET, option);
|
2001-07-27 05:52:05 -07:00
|
|
|
}
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLprim value unix_setsockopt_int(value socket, value option, value status)
|
2001-07-27 05:52:05 -07:00
|
|
|
{
|
2001-10-09 06:36:33 -07:00
|
|
|
return setsockopt_int(sockopt_int, socket, SOL_SOCKET, option, status);
|
2001-07-27 05:52:05 -07:00
|
|
|
}
|
|
|
|
|
2001-10-09 06:36:33 -07:00
|
|
|
CAMLprim value getsockopt_optint(int *sockopt, value socket,
|
|
|
|
int level, value option)
|
2001-07-27 05:52:05 -07:00
|
|
|
{
|
|
|
|
struct linger optval;
|
|
|
|
int optsize;
|
|
|
|
value res = Val_int(0); /* None */
|
|
|
|
|
|
|
|
optsize = sizeof(optval);
|
2002-04-30 08:00:48 -07:00
|
|
|
if (getsockopt(Socket_val(socket),
|
2001-10-09 06:36:33 -07:00
|
|
|
level, sockopt[Int_val(option)],
|
2001-07-27 05:52:05 -07:00
|
|
|
(void *) &optval, &optsize) == -1)
|
|
|
|
uerror("getsockopt_optint", Nothing);
|
|
|
|
if (optval.l_onoff != 0) {
|
|
|
|
res = alloc_small(1, 0);
|
|
|
|
Field(res, 0) = Val_int(optval.l_linger);
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2001-10-09 06:36:33 -07:00
|
|
|
CAMLprim value setsockopt_optint(int *sockopt, value socket, int level,
|
|
|
|
value option, value status)
|
2001-07-27 05:52:05 -07:00
|
|
|
{
|
|
|
|
struct linger optval;
|
|
|
|
|
|
|
|
optval.l_onoff = Is_block (status);
|
|
|
|
if (optval.l_onoff)
|
|
|
|
optval.l_linger = Int_val (Field (status, 0));
|
2002-04-30 08:00:48 -07:00
|
|
|
if (setsockopt(Socket_val(socket),
|
2001-10-09 06:36:33 -07:00
|
|
|
level, sockopt[Int_val(option)],
|
2001-07-27 05:52:05 -07:00
|
|
|
(void *) &optval, sizeof(optval)) == -1)
|
|
|
|
uerror("setsockopt_optint", Nothing);
|
1996-09-04 07:17:43 -07:00
|
|
|
return Val_unit;
|
|
|
|
}
|
2001-07-27 05:52:05 -07:00
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLprim value unix_getsockopt_optint(value socket, value option)
|
2001-07-27 05:52:05 -07:00
|
|
|
{
|
2001-10-09 06:36:33 -07:00
|
|
|
return getsockopt_optint(sockopt_optint, socket, SOL_SOCKET, option);
|
2001-07-27 05:52:05 -07:00
|
|
|
}
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLprim value unix_setsockopt_optint(value socket, value option, value status)
|
2001-07-27 05:52:05 -07:00
|
|
|
{
|
2001-10-09 06:36:33 -07:00
|
|
|
return setsockopt_optint(sockopt_optint, socket, SOL_SOCKET, option, status);
|
2001-07-27 05:52:05 -07:00
|
|
|
}
|
|
|
|
|
2001-10-09 06:36:33 -07:00
|
|
|
CAMLprim value getsockopt_float(int *sockopt, value socket,
|
|
|
|
int level, value option)
|
2001-07-27 05:52:05 -07:00
|
|
|
{
|
|
|
|
struct timeval tv;
|
|
|
|
int optsize;
|
|
|
|
|
|
|
|
optsize = sizeof(tv);
|
2002-04-30 08:00:48 -07:00
|
|
|
if (getsockopt(Socket_val(socket),
|
2001-10-09 06:36:33 -07:00
|
|
|
level, sockopt[Int_val(option)],
|
2001-07-27 05:52:05 -07:00
|
|
|
(void *) &tv, &optsize) == -1)
|
|
|
|
uerror("getsockopt_float", Nothing);
|
|
|
|
return copy_double((double) tv.tv_sec + (double) tv.tv_usec / 1e6);
|
|
|
|
}
|
|
|
|
|
2001-10-09 06:36:33 -07:00
|
|
|
CAMLprim value setsockopt_float(int *sockopt, value socket, int level,
|
|
|
|
value option, value status)
|
2001-07-27 05:52:05 -07:00
|
|
|
{
|
|
|
|
struct timeval tv;
|
|
|
|
double tv_f;
|
|
|
|
|
|
|
|
tv_f = Double_val(status);
|
|
|
|
tv.tv_sec = (int)tv_f;
|
|
|
|
tv.tv_usec = (int) (1e6 * (tv_f - tv.tv_sec));
|
2002-04-30 08:00:48 -07:00
|
|
|
if (setsockopt(Socket_val(socket),
|
2001-10-09 06:36:33 -07:00
|
|
|
level, sockopt[Int_val(option)],
|
2001-07-27 05:52:05 -07:00
|
|
|
(void *) &tv, sizeof(tv)) == -1)
|
|
|
|
uerror("setsockopt_float", Nothing);
|
|
|
|
return Val_unit;
|
|
|
|
}
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLprim value unix_getsockopt_float(value socket, value option)
|
2001-07-27 05:52:05 -07:00
|
|
|
{
|
2001-10-09 06:36:33 -07:00
|
|
|
return getsockopt_float(sockopt_float, socket, SOL_SOCKET, option);
|
2001-07-27 05:52:05 -07:00
|
|
|
}
|
|
|
|
|
2001-08-28 07:47:48 -07:00
|
|
|
CAMLprim value unix_setsockopt_float(value socket, value option, value status)
|
2001-07-27 05:52:05 -07:00
|
|
|
{
|
2001-10-09 06:36:33 -07:00
|
|
|
return setsockopt_float(sockopt_float, socket, SOL_SOCKET, option, status);
|
2001-07-27 05:52:05 -07:00
|
|
|
}
|
|
|
|
|