Change in libosmocore[master]: socket: reduce code duplication, introduce socket_helper_tail()

laforge gerrit-no-reply at lists.osmocom.org
Wed Apr 28 11:15:33 UTC 2021


laforge has uploaded this change for review. ( https://gerrit.osmocom.org/c/libosmocore/+/23933 )


Change subject: socket: reduce code duplication, introduce socket_helper_tail()
......................................................................

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
---
M src/socket.c
1 file changed, 39 insertions(+), 40 deletions(-)



  git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/33/23933/1

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) {

-- 
To view, visit https://gerrit.osmocom.org/c/libosmocore/+/23933
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings

Gerrit-Project: libosmocore
Gerrit-Branch: master
Gerrit-Change-Id: I95056940ddc26b65f63eedaeaab6882edaef6317
Gerrit-Change-Number: 23933
Gerrit-PatchSet: 1
Gerrit-Owner: laforge <laforge at osmocom.org>
Gerrit-MessageType: newchange
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20210428/23d33bd3/attachment.htm>


More information about the gerrit-log mailing list