Mercurial > hg > octave-lyh > gnulib-hg
changeset 11244:e8ad1520586c
setsockopt: Add support for timeouts on W32
Provide POSIX semantics for socket timeout options on W32.
* lib/setsockopt.c: Convert struct timeval to milliseconds on W32.
* lib/getsockopt.c: Convert milliseconds to struct timeval on W32.
* modules/setsockopt: Depend on sys_time module for struct timeval.
* modules/getsockopt: Depend on sys_time module for struct timeval.
Signed-off-by: Simon Josefsson <simon@josefsson.org>
author | Martin Lambers <marlam@marlam.de> |
---|---|
date | Tue, 03 Feb 2009 07:30:20 +0100 |
parents | 185ca1cc7308 |
children | ba40867f64e9 |
files | lib/getsockopt.c lib/setsockopt.c modules/getsockopt modules/setsockopt |
diffstat | 4 files changed, 46 insertions(+), 2 deletions(-) [+] |
line wrap: on
line diff
--- a/lib/getsockopt.c +++ b/lib/getsockopt.c @@ -23,6 +23,12 @@ /* Get winsock2.h. */ #include <sys/socket.h> +/* Get struct timeval. */ +#include <sys/time.h> + +/* Get memcpy. */ +#include <string.h> + /* Get set_winsock_errno, FD_TO_SOCKET etc. */ #include "w32sock.h" @@ -31,8 +37,29 @@ int rpl_getsockopt (int fd, int level, int optname, void *optval, int *optlen) { + int r; SOCKET sock = FD_TO_SOCKET (fd); - int r = getsockopt (sock, level, optname, optval, optlen); + + if (level == SOL_SOCKET && (optname == SO_RCVTIMEO || optname == SO_SNDTIMEO)) + { + int milliseconds; + int milliseconds_len = sizeof (int); + struct timeval tv; + size_t n; + r = getsockopt (sock, level, optname, &milliseconds, &milliseconds_len); + tv.tv_sec = milliseconds / 1000; + tv.tv_usec = (milliseconds - 1000 * tv.tv_sec) * 1000; + n = sizeof (struct timeval); + if (n > *optlen) + n = *optlen; + memcpy (optval, &tv, n); + *optlen = n; + } + else + { + r = getsockopt (sock, level, optname, optval, optlen); + } + if (r < 0) set_winsock_errno ();
--- a/lib/setsockopt.c +++ b/lib/setsockopt.c @@ -23,6 +23,9 @@ /* Get winsock2.h. */ #include <sys/socket.h> +/* Get struct timeval. */ +#include <sys/time.h> + /* Get set_winsock_errno, FD_TO_SOCKET etc. */ #include "w32sock.h" @@ -31,8 +34,20 @@ int rpl_setsockopt (int fd, int level, int optname, const void *optval, int optlen) { + int r; SOCKET sock = FD_TO_SOCKET (fd); - int r = setsockopt (sock, level, optname, optval, optlen); + + if (level == SOL_SOCKET && (optname == SO_RCVTIMEO || optname == SO_SNDTIMEO)) + { + const struct timeval *tv = optval; + int milliseconds = tv->tv_sec * 1000 + tv->tv_usec / 1000; + r = setsockopt (sock, level, optname, &milliseconds, sizeof (int)); + } + else + { + r = setsockopt (sock, level, optname, optval, optlen); + } + if (r < 0) set_winsock_errno ();