socket: reduce code duplication, introduce socket_helper_tail()
Common bits shared by various functions (currently setting
non-blocking) should not be copy+pasted around.
Change-Id: I95056940ddc26b65f63eedaeaab6882edaef6317
diff --git a/src/socket.c b/src/socket.c
index 6b8c34a..b44bbc6 100644
--- a/src/socket.c
+++ b/src/socket.c
@@ -132,9 +132,26 @@
}
#endif /* HAVE_LIBSCTP*/
+static int socket_helper_tail(int sfd, unsigned int flags)
+{
+ int on = 1;
+
+ if (flags & OSMO_SOCK_F_NONBLOCK) {
+ if (ioctl(sfd, FIONBIO, (unsigned char *)&on) < 0) {
+ LOGP(DLGLOBAL, LOGL_ERROR,
+ "cannot set this socket unblocking: %s\n",
+ strerror(errno));
+ close(sfd);
+ return -EINVAL;
+ }
+ }
+
+ return 0;
+}
+
static int socket_helper(const struct addrinfo *rp, unsigned int flags)
{
- int sfd, on = 1;
+ int sfd, rc;
sfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
if (sfd == -1) {
@@ -142,21 +159,17 @@
"unable to create socket: %s\n", strerror(errno));
return sfd;
}
- if (flags & OSMO_SOCK_F_NONBLOCK) {
- if (ioctl(sfd, FIONBIO, (unsigned char *)&on) < 0) {
- LOGP(DLGLOBAL, LOGL_ERROR,
- "cannot set this socket unblocking: %s\n",
- strerror(errno));
- close(sfd);
- sfd = -EINVAL;
- }
- }
+
+ rc = socket_helper_tail(sfd, flags);
+ if (rc < 0)
+ return rc;
+
return sfd;
}
static int socket_helper_osa(const struct osmo_sockaddr *addr, uint16_t type, uint8_t proto, unsigned int flags)
{
- int sfd, on = 1;
+ int sfd, rc;
sfd = socket(addr->u.sa.sa_family, type, proto);
if (sfd == -1) {
@@ -164,15 +177,11 @@
"unable to create socket: %s\n", strerror(errno));
return sfd;
}
- if (flags & OSMO_SOCK_F_NONBLOCK) {
- if (ioctl(sfd, FIONBIO, (unsigned char *)&on) < 0) {
- LOGP(DLGLOBAL, LOGL_ERROR,
- "cannot set this socket unblocking: %s\n",
- strerror(errno));
- close(sfd);
- sfd = -EINVAL;
- }
- }
+
+ rc = socket_helper_tail(sfd, flags);
+ if (rc < 0)
+ return rc;
+
return sfd;
}
@@ -621,7 +630,7 @@
static int socket_helper_multiaddr(uint16_t family, uint16_t type, uint8_t proto, unsigned int flags)
{
- int sfd, on = 1;
+ int sfd, rc;
sfd = socket(family, type, proto);
if (sfd == -1) {
@@ -629,15 +638,11 @@
"Unable to create socket: %s\n", strerror(errno));
return sfd;
}
- if (flags & OSMO_SOCK_F_NONBLOCK) {
- if (ioctl(sfd, FIONBIO, (unsigned char *)&on) < 0) {
- LOGP(DLGLOBAL, LOGL_ERROR,
- "Cannot set this socket unblocking: %s\n",
- strerror(errno));
- close(sfd);
- sfd = -EINVAL;
- }
- }
+
+ rc = socket_helper_tail(sfd, flags);
+ if (rc < 0)
+ return rc;
+
return sfd;
}
@@ -1218,7 +1223,7 @@
const char *socket_path, unsigned int flags)
{
struct sockaddr_un local;
- int sfd, rc, on = 1;
+ int sfd, rc;
unsigned int namelen;
if ((flags & (OSMO_SOCK_F_BIND | OSMO_SOCK_F_CONNECT)) ==
@@ -1258,15 +1263,9 @@
goto err;
}
- if (flags & OSMO_SOCK_F_NONBLOCK) {
- if (ioctl(sfd, FIONBIO, (unsigned char *)&on) < 0) {
- LOGP(DLGLOBAL, LOGL_ERROR,
- "cannot set this socket unblocking: %s\n",
- strerror(errno));
- close(sfd);
- return -EINVAL;
- }
- }
+ rc = socket_helper_tail(sfd, flags);
+ if (rc < 0)
+ return rc;
rc = osmo_sock_init_tail(sfd, type, flags);
if (rc < 0) {