aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHarald Welte <laforge@gnumonks.org>2017-07-13 16:13:26 +0200
committerHarald Welte <laforge@gnumonks.org>2017-07-13 16:53:41 +0200
commitc47bbda78ce62d3390343b39d3bdfd0d6c9609df (patch)
treedd29ece3582f6c1cf07b0d00c5e0606ee65f653d
parente30d7e6018e2690e7f51c182ab2a4fbaca8dd7cc (diff)
socket: Unify listen() calls and check for erroneous returns
We had three places at the end of socket initialization functions calling listen(). Let's unify that and fix some bugs: * close + return error in case of bad listen() result * don't call listen() on AF_UNIX SOCK_DGRAM sockets Change-Id: I7e8dbe3c0486bb3b9810b0add1331e93fc106d82
-rw-r--r--src/socket.c53
1 files changed, 33 insertions, 20 deletions
diff --git a/src/socket.c b/src/socket.c
index d18f756f..d0e4c243 100644
--- a/src/socket.c
+++ b/src/socket.c
@@ -104,6 +104,25 @@ static int socket_helper(const struct addrinfo *rp, unsigned int flags)
}
+static int osmo_sock_init_tail(int fd, uint16_t type, unsigned int flags)
+{
+ int rc = 0;
+
+ /* Make sure to call 'listen' on a bound, connection-oriented sock */
+ if ((flags & (OSMO_SOCK_F_BIND|OSMO_SOCK_F_CONNECT)) == OSMO_SOCK_F_BIND) {
+ switch (type) {
+ case SOCK_STREAM:
+ case SOCK_SEQPACKET:
+ rc = listen(fd, 10);
+ }
+ }
+
+ if (rc < 0)
+ LOGP(DLGLOBAL, LOGL_ERROR, "unable to listen on socket: %s\n", strerror(errno));
+
+ return rc;
+}
+
/*! Initialize a socket (including bind and/or connect)
* \param[in] family Address Family like AF_INET, AF_INET6, AF_UNSPEC
* \param[in] type Socket type like SOCK_DGRAM, SOCK_STREAM
@@ -219,15 +238,12 @@ int osmo_sock_init2(uint16_t family, uint16_t type, uint8_t proto,
}
}
- /* Make sure to call 'listen' on a bound, connection-oriented sock */
- if ((flags & (OSMO_SOCK_F_BIND|OSMO_SOCK_F_CONNECT)) == OSMO_SOCK_F_BIND) {
- switch (type) {
- case SOCK_STREAM:
- case SOCK_SEQPACKET:
- listen(sfd, 10);
- break;
- }
+ rc = osmo_sock_init_tail(sfd, type, flags);
+ if (rc < 0) {
+ close(sfd);
+ sfd = -1;
}
+
return sfd;
}
@@ -305,15 +321,12 @@ int osmo_sock_init(uint16_t family, uint16_t type, uint8_t proto,
setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
- /* Make sure to call 'listen' on a bound, connection-oriented sock */
- if (flags & OSMO_SOCK_F_BIND) {
- switch (type) {
- case SOCK_STREAM:
- case SOCK_SEQPACKET:
- listen(sfd, 10);
- break;
- }
+ rc = osmo_sock_init_tail(sfd, type, flags);
+ if (rc < 0) {
+ close(sfd);
+ sfd = -1;
}
+
return sfd;
}
@@ -545,10 +558,10 @@ int osmo_sock_unix_init(uint16_t type, uint8_t proto,
}
}
- if (flags & OSMO_SOCK_F_BIND) {
- rc = listen(sfd, 10);
- if (rc < 0)
- goto err;
+ rc = osmo_sock_init_tail(sfd, type, flags);
+ if (rc < 0) {
+ close(sfd);
+ sfd = -1;
}
return sfd;