guile-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH] Define SO_RCVTIMEO and SO_SNDTIMEO.


From: Christopher Baines
Subject: [PATCH] Define SO_RCVTIMEO and SO_SNDTIMEO.
Date: Sat, 17 Sep 2022 10:05:12 +0200

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  | 24 ++++++++++++++++++++++++
 2 files changed, 26 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..547dd1d83 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>
@@ -493,6 +494,8 @@ SCM_DEFINE (scm_getsockopt, "getsockopt", 3, 0, 0,
            "@defvarx SO_NO_CHECK\n"
            "@defvarx SO_PRIORITY\n"
            "@defvarx SO_REUSEPORT\n"
+           "@defvarx SO_RCVTIMEO\n"
+           "@defvarx SO_SNDTIMEO\n"
            "The value returned is an integer.\n"
            "@end defvar\n"
            "\n"
@@ -581,6 +584,8 @@ SCM_DEFINE (scm_setsockopt, "setsockopt", 4, 0, 0,
            "@defvarx SO_NO_CHECK\n"
            "@defvarx SO_PRIORITY\n"
            "@defvarx SO_REUSEPORT\n"
+           "@defvarx SO_RCVTIMEO\n"
+           "@defvarx SO_SNDTIMEO\n"
            "@var{value} is an integer.\n"
            "@end defvar\n"
            "\n"
@@ -633,6 +638,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 +689,17 @@ SCM_DEFINE (scm_setsockopt, "setsockopt", 4, 0, 0,
     }
 #endif
 
+  if (ioptname == SO_RCVTIMEO || ioptname == SO_SNDTIMEO)
+    {
+      SCM_ASSERT (scm_is_pair (value), value, SCM_ARG4, FUNC_NAME);
+
+      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;
+    }
+
   if (optval == NULL)
     {
       /* Most options take an int.  */
@@ -1768,6 +1786,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




reply via email to

[Prev in Thread] Current Thread [Next in Thread]