Simplify the logic of tsocket_bsd_pending
[ira/wip.git] / lib / tsocket / tsocket_bsd.c
index 0ec47f846634f21927f437f85cebb5a594d7e2cb..054bb3de3a7cca0ad28bcb87bf236bba0e3e3c56 100644 (file)
@@ -149,40 +149,42 @@ static int tsocket_bsd_common_prepare_fd(int fd, bool high_fd)
 
 static ssize_t tsocket_bsd_pending(int fd)
 {
-       int ret;
+       int ret, error;
        int value = 0;
+       socklen_t len;
 
        ret = ioctl(fd, FIONREAD, &value);
        if (ret == -1) {
                return ret;
        }
 
-       if (ret == 0) {
-               if (value == 0) {
-                       int error=0;
-                       socklen_t len = sizeof(error);
-                       /*
-                        * if no data is available check if the socket
-                        * is in error state. For dgram sockets
-                        * it's the way to return ICMP error messages
-                        * of connected sockets to the caller.
-                        */
-                       ret = getsockopt(fd, SOL_SOCKET, SO_ERROR,
-                                        &error, &len);
-                       if (ret == -1) {
-                               return ret;
-                       }
-                       if (error != 0) {
-                               errno = error;
-                               return -1;
-                       }
-               }
+       if (ret != 0) {
+               /* this should not be reached */
+               errno = EIO;
+               return -1;
+       }
+
+       if (value != 0) {
                return value;
        }
 
-       /* this should not be reached */
-       errno = EIO;
-       return -1;
+       error = 0;
+       len = sizeof(error);
+
+       /*
+        * if no data is available check if the socket is in error state. For
+        * dgram sockets it's the way to return ICMP error messages of
+        * connected sockets to the caller.
+        */
+       ret = getsockopt(fd, SOL_SOCKET, SO_ERROR, &error, &len);
+       if (ret == -1) {
+               return ret;
+       }
+       if (error != 0) {
+               errno = error;
+               return -1;
+       }
+       return 0;
 }
 
 static const struct tsocket_address_ops tsocket_address_bsd_ops;