unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
From: Christopher Baines <mail@cbaines.net>
To: guile-devel@gnu.org
Subject: [PATCH v2] Define SO_RCVTIMEO and SO_SNDTIMEO.
Date: Sun, 18 Sep 2022 15:56:31 +0200	[thread overview]
Message-ID: <20220918135631.15078-1-mail@cbaines.net> (raw)
In-Reply-To: <20220917080512.17824-1-mail@cbaines.net>

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): Include SO_RCVTIMEO and SO_SNDTIMEO in docstring.
(scm_setsockopt): Include SO_RCVTIMEO and SO_SNDTIMEO in docstring and
handle these operations.
* doc/ref/posix.texi (Network Sockets and Communication): Document them.
---
 doc/ref/posix.texi |  2 ++
 libguile/socket.c  | 34 ++++++++++++++++++++++++++++++++++
 2 files changed, 36 insertions(+)

diff --git a/doc/ref/posix.texi b/doc/ref/posix.texi
index 19911a427..5c7dce90d 100644
--- a/doc/ref/posix.texi
+++ b/doc/ref/posix.texi
@@ -3229,6 +3229,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
 
diff --git a/libguile/socket.c b/libguile/socket.c
index b3482c8f3..0c6e1078e 100644
--- a/libguile/socket.c
+++ b/libguile/socket.c
@@ -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
 {
@@ -590,6 +597,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 +646,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 +697,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 +1796,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
-- 
2.37.3




  parent reply	other threads:[~2022-09-18 13:56 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-09-17  8:05 [PATCH] Define SO_RCVTIMEO and SO_SNDTIMEO Christopher Baines
2022-09-17  9:40 ` Maxime Devos
2022-09-18 14:11   ` Christopher Baines
2022-09-18 13:56 ` Christopher Baines [this message]
2022-09-18 14:17 ` [PATCH v3] " Christopher Baines
2022-10-12 20:28   ` Ludovic Courtès
2022-10-13 10:28     ` Christopher Baines

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220918135631.15078-1-mail@cbaines.net \
    --to=mail@cbaines.net \
    --cc=guile-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).