Skip to content

Commit d8f814d

Browse files
committed
fix bugs of setsockopt diff in win compact implementation
1 parent f5f855c commit d8f814d

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

sockcompat.c

+20-2
Original file line numberDiff line numberDiff line change
@@ -189,13 +189,31 @@ int win32_connect(SOCKET sockfd, const struct sockaddr *addr, socklen_t addrlen)
189189
}
190190

191191
int win32_getsockopt(SOCKET sockfd, int level, int optname, void *optval, socklen_t *optlen) {
192-
int ret = getsockopt(sockfd, level, optname, (char*)optval, optlen);
192+
int ret = 0;
193+
if ((level == SOL_SOCKET) && ((optname == SO_RCVTIMEO) || (optname == SO_SNDTIMEO))) {
194+
struct timeval *tv = (struct timeval *)optval;
195+
DWORD timeout = 0;
196+
socklen_t dwlen = 0;
197+
ret = getsockopt(sockfd, level, optname, (char *)timeout, &dwlen);
198+
tv->tv_sec = timeout / 1000;
199+
tv->tv_usec = timeout * 1000;
200+
*optlen = sizeof (struct timeval);
201+
} else {
202+
ret = getsockopt(sockfd, level, optname, (char*)optval, optlen);
203+
}
193204
_updateErrno(ret != SOCKET_ERROR);
194205
return ret != SOCKET_ERROR ? ret : -1;
195206
}
196207

197208
int win32_setsockopt(SOCKET sockfd, int level, int optname, const void *optval, socklen_t optlen) {
198-
int ret = setsockopt(sockfd, level, optname, (const char*)optval, optlen);
209+
int ret = 0;
210+
if ((level == SOL_SOCKET) && ((optname == SO_RCVTIMEO) || (optname == SO_SNDTIMEO))) {
211+
struct timeval *tv = (struct timeval *)optval;
212+
DWORD timeout = tv->tv_sec * 1000 + tv->tv_usec / 1000;
213+
ret = setsockopt(sockfd, level, optname, (const char*)&timeout, sizeof(DWORD));
214+
} else {
215+
ret = setsockopt(sockfd, level, optname, (const char*)optval, optlen);
216+
}
199217
_updateErrno(ret != SOCKET_ERROR);
200218
return ret != SOCKET_ERROR ? ret : -1;
201219
}

0 commit comments

Comments
 (0)