From 09624c6cec97dc729f3f3c0984d6289b115b846b Mon Sep 17 00:00:00 2001 From: Martin Willi Date: Thu, 6 Nov 2014 14:15:09 +0100 Subject: [PATCH] windows: Provide a read(2) wrapper that uses recv(2) on sockets --- src/libstrongswan/utils/windows.c | 16 ++++++++++++++++ src/libstrongswan/utils/windows.h | 9 +++++++++ 2 files changed, 25 insertions(+) diff --git a/src/libstrongswan/utils/windows.c b/src/libstrongswan/utils/windows.c index 6ce9d77c8..e511db026 100644 --- a/src/libstrongswan/utils/windows.c +++ b/src/libstrongswan/utils/windows.c @@ -643,6 +643,22 @@ ssize_t windows_sendto(int sockfd, const void *buf, size_t len, int flags, return outlen; } +/** + * See header + */ +#undef read +ssize_t windows_read(int fd, void *buf, size_t count) +{ + ssize_t ret; + + ret = recv(fd, buf, count, 0); + if (ret == -1 && WSAGetLastError() == WSAENOTSOCK) + { + ret = read(fd, buf, count); + } + return ret; +} + /** * See header */ diff --git a/src/libstrongswan/utils/windows.h b/src/libstrongswan/utils/windows.h index 80106ecdb..ffd95b3fb 100644 --- a/src/libstrongswan/utils/windows.h +++ b/src/libstrongswan/utils/windows.h @@ -362,6 +362,15 @@ ssize_t windows_send(int sockfd, const void *buf, size_t len, int flags); ssize_t windows_sendto(int sockfd, const void *buf, size_t len, int flags, const struct sockaddr *dest_addr, socklen_t addrlen); +/** + * read(2) working on files and sockets, cancellable on sockets only + * + * On Windows, there does not seem to be a way how a cancellable read can + * be implemented on Low level I/O functions for files, _pipe()s or stdio. + */ +#define read windows_read +ssize_t windows_read(int fd, void *buf, size_t count); + #if _WIN32_WINNT < 0x0600 /** * Define pollfd and flags on our own if not specified