summaryrefslogtreecommitdiffstats
path: root/lib/sockunion.c
diff options
context:
space:
mode:
authorRenato Westphal <renato@opensourcerouting.org>2017-10-23 23:24:07 +0200
committerRenato Westphal <renato@opensourcerouting.org>2017-10-24 23:30:31 +0200
commitb11b57723b963fb5d0ab11748b7957f0b7fa5d37 (patch)
tree55ddd237d50698bc0a53a42b41def519b337c935 /lib/sockunion.c
parent*: fix coverity warnings - error handling issues (diff)
downloadfrr-b11b57723b963fb5d0ab11748b7957f0b7fa5d37.tar.xz
frr-b11b57723b963fb5d0ab11748b7957f0b7fa5d37.zip
lib: optimize sockunion_connect()
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>
Diffstat (limited to 'lib/sockunion.c')
-rw-r--r--lib/sockunion.c16
1 files changed, 2 insertions, 14 deletions
diff --git a/lib/sockunion.c b/lib/sockunion.c
index 559ae37ff..ab8d8be3e 100644
--- a/lib/sockunion.c
+++ b/lib/sockunion.c
@@ -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;
}