1
Fork 0
mirror of https://git.savannah.gnu.org/git/guile.git synced 2025-04-30 03:40:34 +02:00

Define SO_RCVTIMEO and SO_SNDTIMEO.

These are important for reliable networking, since they prevent network
operations from hanging indefinitely.

* libguile/socket.c (scm_init_socket): Define SO_RCVTIMEO and
SO_SNDTIMEO.
(scm_getsockopt, scm_setsockopt): Include SO_RCVTIMEO and SO_SNDTIMEO in
docstring and handle them.
* doc/ref/posix.texi (Network Sockets and Communication): Document them.

Signed-off-by: Ludovic Courtès <ludo@gnu.org>
This commit is contained in:
Christopher Baines 2022-09-18 16:17:34 +02:00 committed by Ludovic Courtès
parent 328d6039d2
commit 6847365a9f
2 changed files with 46 additions and 0 deletions

View file

@ -3247,6 +3247,8 @@ Manual}, or @command{man 7 socket}.
@defvarx SO_NO_CHECK
@defvarx SO_PRIORITY
@defvarx SO_REUSEPORT
@defvarx SO_RCVTIMEO
@defvarx SO_SNDTIMEO
The @var{value} taken or returned is an integer.
@end defvar

View file

@ -35,6 +35,7 @@
#endif
#include <unistd.h>
#include <sys/types.h>
#include <sys/time.h>
#ifdef HAVE_WINSOCK2_H
#include <winsock2.h>
@ -502,6 +503,12 @@ SCM_DEFINE (scm_getsockopt, "getsockopt", 3, 0, 0,
"timeout support (ie.@: without @code{struct linger}), only\n"
"@var{enable} has an effect but the value in Guile is always a\n"
"pair.\n"
"@end defvar"
"\n"
"@defvar SO_RCVTIMEO\n"
"@defvarx SO_SNDTIMEO\n"
"@var{value} is a pair of integers @code{(@var{SECONDS}\n"
". @var{MICROSECONDS})}.\n"
"@end defvar")
#define FUNC_NAME s_scm_getsockopt
{
@ -522,6 +529,16 @@ SCM_DEFINE (scm_getsockopt, "getsockopt", 3, 0, 0,
if (getsockopt (fd, ilevel, ioptname, (void *) &optval, &optlen) == -1)
SCM_SYSERROR;
#if defined(SO_RCVTIMEO) && defined(SO_SNDTIMEO)
if (ioptname == SO_RCVTIMEO || ioptname == SO_SNDTIMEO)
{
struct timeval *opt_time = (struct timeval *) &optval;
return scm_cons (scm_from_long (opt_time->tv_sec),
scm_from_long (opt_time->tv_usec));
}
#endif
if (ilevel == SOL_SOCKET)
{
#ifdef SO_LINGER
@ -590,6 +607,12 @@ SCM_DEFINE (scm_setsockopt, "setsockopt", 4, 0, 0,
"(ie.@: without @code{struct linger}), only @var{ENABLE} has an\n"
"effect but the value in Guile is always a pair.\n"
"@end defvar\n"
"\n"
"@defvar SO_RCVTIMEO\n"
"@defvarx SO_SNDTIMEO\n"
"@var{value} is a pair of integers @code{(@var{SECONDS}\n"
". @var{MICROSECONDS})}.\n"
"@end defvar\n"
"\n"
"@c Note that we refer only to ``man ip'' here. On GNU/Linux it's\n"
"@c ``man 7 ip'' but on NetBSD it's ``man 4 ip''.\n"
@ -633,6 +656,8 @@ SCM_DEFINE (scm_setsockopt, "setsockopt", 4, 0, 0,
struct ip_mreq opt_mreq;
#endif
struct timeval opt_time;
const void *optval = NULL;
socklen_t optlen = 0;
@ -682,6 +707,19 @@ SCM_DEFINE (scm_setsockopt, "setsockopt", 4, 0, 0,
}
#endif
#if defined(SO_RCVTIMEO) && defined(SO_SNDTIMEO)
if (ioptname == SO_RCVTIMEO || ioptname == SO_SNDTIMEO)
{
SCM_ASSERT_TYPE (scm_is_pair (value), value, SCM_ARG4, FUNC_NAME,
"value");
opt_time.tv_sec = scm_to_ulong (SCM_CAR (value));
opt_time.tv_usec = scm_to_ulong (SCM_CDR (value));
optlen = sizeof (opt_time);
optval = &opt_time;
}
#endif
if (optval == NULL)
{
/* Most options take an int. */
@ -1768,6 +1806,12 @@ scm_init_socket ()
#ifdef SO_REUSEPORT /* new in Linux 3.9 */
scm_c_define ("SO_REUSEPORT", scm_from_int (SO_REUSEPORT));
#endif
#ifdef SO_RCVTIMEO
scm_c_define ("SO_RCVTIMEO", scm_from_int (SO_RCVTIMEO));
#endif
#ifdef SO_SNDTIMEO
scm_c_define ("SO_SNDTIMEO", scm_from_int (SO_SNDTIMEO));
#endif
/* recv/send options. */
#ifdef MSG_DONTWAIT