[esnacc-dev] [PATCH 05/11] windows/asn-buf: Fix the ioctl

Aaron Conole aconole at bytheb.org
Tue Dec 6 19:47:03 UTC 2016


There were a few problems with the ioctl call:
1) It used the return value, instead of the number of bytes.  This
   caused it to block when no data was available - the opposite of what
   was desired.

2) Under windows, it didn't properly cast the argument to ioctlsocket.

3) Windows doesn't support ioctl(..FIONREAD..) to see if any bytes are
   available.

Fixes 43b074e6d7c0 ("asn-buf: Add available helper to the fdbuf")
Signed-off-by: Aaron Conole <aconole at bytheb.org>
---
 cxx-lib/src/asn-buf.cpp | 17 ++++++++++++-----
 1 file changed, 12 insertions(+), 5 deletions(-)

diff --git a/cxx-lib/src/asn-buf.cpp b/cxx-lib/src/asn-buf.cpp
index a24c1b4..62d61df 100644
--- a/cxx-lib/src/asn-buf.cpp
+++ b/cxx-lib/src/asn-buf.cpp
@@ -906,6 +906,9 @@ static int ioctlsocket(int fd, long request, void *data )
 {
 	return ioctl(fd, request, data);
 }
+#define CAST void *
+#else
+#define CAST u_long *
 #endif
 
 std::streamsize
@@ -919,11 +922,11 @@ AsnFDBuf::showmanyc()
         tv.tv_usec = 0;
 
         FD_ZERO(&fds);
-        FD_SET(
 #ifdef WIN32 
-            (unsigned int)
+        FD_SET((unsigned int) fd, &fds);
+#else
+        FD_SET(fd, &fds);
 #endif
-            fd, &fds);
 
         if (select(fd+1, &fds, NULL, NULL, &tv) > 0) {
             return 1; // at least 1 available...
@@ -931,17 +934,21 @@ AsnFDBuf::showmanyc()
 
         // black arts... try an ioctl
         size_t numBytes = 0;
-        int i = ioctlsocket(fd, FIONREAD, &numBytes);
+        int i = ioctlsocket(fd, FIONREAD, (CAST) &numBytes);
         if (i > 0) {
-            return i;
+            return numBytes;
         }
     } else {
+#ifndef WIN32
         // we can check for file length
         size_t numBytes = 0;
         int i = ioctl(fd, FIONREAD, &numBytes);
         if (i > 0) {
             return i;
         }
+#else
+        return 0;
+#endif
     }
     return 0;
 }
-- 
2.7.4




More information about the dev mailing list