From d8f814d48b25e202065b5a2b829cd0c2c20d29d3 Mon Sep 17 00:00:00 2001 From: Minun Dragonation Date: Sun, 5 May 2019 21:34:28 +0800 Subject: fix bugs of setsockopt diff in win compact implementation --- sockcompat.c | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/sockcompat.c b/sockcompat.c index b52cbc6..047f8b6 100644 --- a/sockcompat.c +++ b/sockcompat.c @@ -189,13 +189,31 @@ int win32_connect(SOCKET sockfd, const struct sockaddr *addr, socklen_t addrlen) } int win32_getsockopt(SOCKET sockfd, int level, int optname, void *optval, socklen_t *optlen) { - int ret = getsockopt(sockfd, level, optname, (char*)optval, optlen); + int ret = 0; + if ((level == SOL_SOCKET) && ((optname == SO_RCVTIMEO) || (optname == SO_SNDTIMEO))) { + struct timeval *tv = (struct timeval *)optval; + DWORD timeout = 0; + socklen_t dwlen = 0; + ret = getsockopt(sockfd, level, optname, (char *)timeout, &dwlen); + tv->tv_sec = timeout / 1000; + tv->tv_usec = timeout * 1000; + *optlen = sizeof (struct timeval); + } else { + ret = getsockopt(sockfd, level, optname, (char*)optval, optlen); + } _updateErrno(ret != SOCKET_ERROR); return ret != SOCKET_ERROR ? ret : -1; } int win32_setsockopt(SOCKET sockfd, int level, int optname, const void *optval, socklen_t optlen) { - int ret = setsockopt(sockfd, level, optname, (const char*)optval, optlen); + int ret = 0; + if ((level == SOL_SOCKET) && ((optname == SO_RCVTIMEO) || (optname == SO_SNDTIMEO))) { + struct timeval *tv = (struct timeval *)optval; + DWORD timeout = tv->tv_sec * 1000 + tv->tv_usec / 1000; + ret = setsockopt(sockfd, level, optname, (const char*)&timeout, sizeof(DWORD)); + } else { + ret = setsockopt(sockfd, level, optname, (const char*)optval, optlen); + } _updateErrno(ret != SOCKET_ERROR); return ret != SOCKET_ERROR ? ret : -1; } -- cgit v1.2.3 From 82252440de09067d46772da727dbc69c2e83fcf1 Mon Sep 17 00:00:00 2001 From: Minun Dragonation Date: Sun, 5 May 2019 21:39:46 +0800 Subject: fix bugs on ref address incorrect on sockcompact with getsockopt --- sockcompat.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/sockcompat.c b/sockcompat.c index 047f8b6..7e5b6a7 100644 --- a/sockcompat.c +++ b/sockcompat.c @@ -192,9 +192,8 @@ int win32_getsockopt(SOCKET sockfd, int level, int optname, void *optval, sockle int ret = 0; if ((level == SOL_SOCKET) && ((optname == SO_RCVTIMEO) || (optname == SO_SNDTIMEO))) { struct timeval *tv = (struct timeval *)optval; - DWORD timeout = 0; - socklen_t dwlen = 0; - ret = getsockopt(sockfd, level, optname, (char *)timeout, &dwlen); + DWORD timeout = 0; socklen_t dwlen = 0; + ret = getsockopt(sockfd, level, optname, (char *)&timeout, &dwlen); tv->tv_sec = timeout / 1000; tv->tv_usec = timeout * 1000; *optlen = sizeof (struct timeval); -- cgit v1.2.3 From 4a94ce6326c51a2071025e929a3ab2a5263206f7 Mon Sep 17 00:00:00 2001 From: Minun Dragonation Date: Sun, 5 May 2019 21:46:34 +0800 Subject: fix bugs for optlen output on size not big enough for timeout events --- sockcompat.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/sockcompat.c b/sockcompat.c index 7e5b6a7..38cb9e5 100644 --- a/sockcompat.c +++ b/sockcompat.c @@ -191,11 +191,16 @@ int win32_connect(SOCKET sockfd, const struct sockaddr *addr, socklen_t addrlen) int win32_getsockopt(SOCKET sockfd, int level, int optname, void *optval, socklen_t *optlen) { int ret = 0; if ((level == SOL_SOCKET) && ((optname == SO_RCVTIMEO) || (optname == SO_SNDTIMEO))) { - struct timeval *tv = (struct timeval *)optval; - DWORD timeout = 0; socklen_t dwlen = 0; - ret = getsockopt(sockfd, level, optname, (char *)&timeout, &dwlen); - tv->tv_sec = timeout / 1000; - tv->tv_usec = timeout * 1000; + if (*optlen >= sizeof (struct timeval)) { + struct timeval *tv = (struct timeval *)optval; + DWORD timeout = 0; + socklen_t dwlen = 0; + ret = getsockopt(sockfd, level, optname, (char *)&timeout, &dwlen); + tv->tv_sec = timeout / 1000; + tv->tv_usec = timeout * 1000; + } else { + ret = WSAEFAULT; + } *optlen = sizeof (struct timeval); } else { ret = getsockopt(sockfd, level, optname, (char*)optval, optlen); -- cgit v1.2.3 From f5454d509f9eafef35714c4e5e23af066e80f4f6 Mon Sep 17 00:00:00 2001 From: Minun Dragonation Date: Sun, 5 May 2019 21:58:34 +0800 Subject: fix bugs on socket timeout tv usec calculation --- sockcompat.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sockcompat.c b/sockcompat.c index 38cb9e5..c3b6f66 100644 --- a/sockcompat.c +++ b/sockcompat.c @@ -197,7 +197,7 @@ int win32_getsockopt(SOCKET sockfd, int level, int optname, void *optval, sockle socklen_t dwlen = 0; ret = getsockopt(sockfd, level, optname, (char *)&timeout, &dwlen); tv->tv_sec = timeout / 1000; - tv->tv_usec = timeout * 1000; + tv->tv_usec = (timeout * 1000) % 1000000; } else { ret = WSAEFAULT; } -- cgit v1.2.3 From 76394f1be87359c88b13fc327f86da9e949ce3e3 Mon Sep 17 00:00:00 2001 From: Minun Dragonation Date: Mon, 13 May 2019 23:20:05 +0800 Subject: remove useless type casting --- sockcompat.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sockcompat.c b/sockcompat.c index c3b6f66..4cc2f41 100644 --- a/sockcompat.c +++ b/sockcompat.c @@ -192,7 +192,7 @@ int win32_getsockopt(SOCKET sockfd, int level, int optname, void *optval, sockle int ret = 0; if ((level == SOL_SOCKET) && ((optname == SO_RCVTIMEO) || (optname == SO_SNDTIMEO))) { if (*optlen >= sizeof (struct timeval)) { - struct timeval *tv = (struct timeval *)optval; + struct timeval *tv = optval; DWORD timeout = 0; socklen_t dwlen = 0; ret = getsockopt(sockfd, level, optname, (char *)&timeout, &dwlen); @@ -212,7 +212,7 @@ int win32_getsockopt(SOCKET sockfd, int level, int optname, void *optval, sockle int win32_setsockopt(SOCKET sockfd, int level, int optname, const void *optval, socklen_t optlen) { int ret = 0; if ((level == SOL_SOCKET) && ((optname == SO_RCVTIMEO) || (optname == SO_SNDTIMEO))) { - struct timeval *tv = (struct timeval *)optval; + struct timeval *tv = optval; DWORD timeout = tv->tv_sec * 1000 + tv->tv_usec / 1000; ret = setsockopt(sockfd, level, optname, (const char*)&timeout, sizeof(DWORD)); } else { -- cgit v1.2.3