On Mon, 02. Feb 2009, 22:27:03 +0100, Paolo Bonzini wrote: > > Simon, Paolo, do you have any comments on the patches suggested in this > > thread? > > I didn't see anything wrong -- on the contrary. If you resend it, I'll > apply it.
OK, I resend the part that I wrote (provide POSIX semantics for socket timeout options on W32). Bruno changed the type of the 'optlen' argument from int to socklen_t. Bruno, would you resend this patch or apply it? Another suggestion was to add the 'restrict' keyword to the prototypes, but I had the impression that it is not yet decided whether to use 'restrict' or not. Martin 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. --- lib/getsockopt.c | 29 ++++++++++++++++++++++++++++- lib/setsockopt.c | 17 ++++++++++++++++- modules/getsockopt | 1 + modules/setsockopt | 1 + 4 files changed, 46 insertions(+), 2 deletions(-) diff --git a/lib/getsockopt.c b/lib/getsockopt.c index 7be8bed..3da37a9 100644 --- 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 (); diff --git a/lib/setsockopt.c b/lib/setsockopt.c index 09f048e..64f4a81 100644 --- 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 (); diff --git a/modules/getsockopt b/modules/getsockopt index efb762a..40ff649 100644 --- a/modules/getsockopt +++ b/modules/getsockopt @@ -7,6 +7,7 @@ lib/w32sock.h Depends-on: sys_socket +sys_time errno configure.ac: diff --git a/modules/setsockopt b/modules/setsockopt index d665f8e..b429510 100644 --- a/modules/setsockopt +++ b/modules/setsockopt @@ -7,6 +7,7 @@ lib/w32sock.h Depends-on: sys_socket +sys_time errno configure.ac: -- 1.5.6.5