]> git.puffer.fish Git - matthieu/frr.git/commitdiff
lib: optimize sockunion_connect()
authorRenato Westphal <renato@opensourcerouting.org>
Mon, 23 Oct 2017 21:24:07 +0000 (19:24 -0200)
committerRenato Westphal <renato@opensourcerouting.org>
Tue, 24 Oct 2017 21:30:31 +0000 (19:30 -0200)
This function is only called with non-blocking sockets [1], so there's
no need to worry about setting O_NONBLOCK and unsetting it later if the
given fd was a blocking socket. This saves us 4 syscalls per connect,
which is not much but is something.

Also, remove an outdated comment about the return values of this
function. It returns a 'connect_result' enum now, whose values are
self-explanatory (connect_error, connect_success and connect_in_progress).

This also fixes a coverity scan warning where we weren't checking the
return value of the fcntl() syscall.

[1] bgp_connect() and pim_msdp_sock_connect().

Signed-off-by: Renato Westphal <renato@opensourcerouting.org>
lib/sockunion.c

index 559ae37ffb09130928d014525ca1dc0d8acf157e..ab8d8be3e54e38ed3eedf49d3bf6d4dead1e43cb 100644 (file)
@@ -175,15 +175,11 @@ static int sockunion_sizeof(const union sockunion *su)
        return ret;
 }
 
-/* sockunion_connect returns
-   -1 : error occured
-   0 : connect success
-   1 : connect is in progress */
+/* Performs a non-blocking connect().  */
 enum connect_result sockunion_connect(int fd, const union sockunion *peersu,
                                      unsigned short port, ifindex_t ifindex)
 {
        int ret;
-       int val;
        union sockunion su;
 
        memcpy(&su, peersu, sizeof(union sockunion));
@@ -203,18 +199,12 @@ enum connect_result sockunion_connect(int fd, const union sockunion *peersu,
                break;
        }
 
-       /* Make socket non-block. */
-       val = fcntl(fd, F_GETFL, 0);
-       fcntl(fd, F_SETFL, val | O_NONBLOCK);
-
        /* Call connect function. */
        ret = connect(fd, (struct sockaddr *)&su, sockunion_sizeof(&su));
 
        /* Immediate success */
-       if (ret == 0) {
-               fcntl(fd, F_SETFL, val);
+       if (ret == 0)
                return connect_success;
-       }
 
        /* If connect is in progress then return 1 else it's real error. */
        if (ret < 0) {
@@ -227,8 +217,6 @@ enum connect_result sockunion_connect(int fd, const union sockunion *peersu,
                }
        }
 
-       fcntl(fd, F_SETFL, val);
-
        return connect_in_progress;
 }