Change in libosmocore[master]: sock: osmo_sock_init2_multiaddr: decouple addr resolution from socket...

This is merely a historical archive of years 2008-2021, before the migration to mailman3.

A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.

pespin gerrit-no-reply at lists.osmocom.org
Wed Aug 19 16:14:41 UTC 2020


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


Change subject: sock: osmo_sock_init2_multiaddr: decouple addr resolution from socket creation
......................................................................

sock: osmo_sock_init2_multiaddr: decouple addr resolution from socket creation

Address resolution is done first and once we have the information, it
proceeds to create the socket. This separation in steps will help when
adding support for IPv6+IPv4 sets, where AF_UNSEPC is passed and created
socket needs to be AF_INET6 in order to handle addr of both versions.

Change-Id: I03147e3033a0c1fd04c9ac61d2ffbd78a1bb784a
---
M src/socket.c
1 file changed, 42 insertions(+), 45 deletions(-)



  git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/23/19723/1

diff --git a/src/socket.c b/src/socket.c
index 9c60821..d92c670 100644
--- a/src/socket.c
+++ b/src/socket.c
@@ -483,7 +483,7 @@
 		   unsigned int flags)
 
 {
-	struct addrinfo *result[OSMO_SOCK_MAX_ADDRS];
+	struct addrinfo *res_loc[OSMO_SOCK_MAX_ADDRS], *res_rem[OSMO_SOCK_MAX_ADDRS];
 	int sfd = -1, rc, on = 1;
 	int i;
 	struct sockaddr_in addrs4[OSMO_SOCK_MAX_ADDRS];
@@ -519,21 +519,31 @@
 
 	/* figure out local side of socket */
 	if (flags & OSMO_SOCK_F_BIND) {
-		rc = addrinfo_helper_multi(result, family, type, proto, local_hosts,
+		rc = addrinfo_helper_multi(res_loc, family, type, proto, local_hosts,
 					       local_hosts_cnt, local_port, true);
 		if (rc < 0)
 			return -EINVAL;
+	}
+	/* figure out remote side of socket */
+	if (flags & OSMO_SOCK_F_CONNECT) {
+		rc = addrinfo_helper_multi(res_rem, family, type, proto, remote_hosts,
+					       remote_hosts_cnt, remote_port, false);
+		if (rc < 0)
+			return -EINVAL;
+	}
+
+	/* figure out local side of socket */
+	if (flags & OSMO_SOCK_F_BIND) {
 
 		/* Since addrinfo_helper sets ai_family, socktype and
 		   ai_protocol in hints, we know all results will use same
 		   values, so simply pick the first one and pass it to create
 		   the socket:
 		*/
-		sfd = socket_helper(result[0], flags);
+		sfd = socket_helper(res_loc[0], flags);
 		if (sfd < 0) {
-			for (i = 0; i < local_hosts_cnt; i++)
-				freeaddrinfo(result[i]);
-			return sfd;
+			rc = sfd;
+			goto ret_freeaddrinfo;
 		}
 
 		/* Since so far we only allow IPPROTO_SCTP in this function,
@@ -547,22 +557,17 @@
 			     " %s:%u: %s\n",
 			     strbuf, local_port,
 			     strerror(errno));
-			for (i = 0; i < local_hosts_cnt; i++)
-				freeaddrinfo(result[i]);
-			close(sfd);
-			return rc;
+			goto ret_close;
 		}
 
 		/* Build array of addresses taking first of same family for each host.
 		   TODO: Ideally we should use backtracking storing last used
 		   indexes and trying next combination if connect() fails .*/
-		rc = addrinfo_to_sockaddr(family, (const struct addrinfo **)result,
+		rc = addrinfo_to_sockaddr(family, (const struct addrinfo **)res_loc,
 					  local_hosts, local_hosts_cnt, addrs4, addrs6);
 		if (rc < 0) {
-			for (i = 0; i < local_hosts_cnt; i++)
-				freeaddrinfo(result[i]);
-			close(sfd);
-			return -ENODEV;
+			rc = -ENODEV;
+			goto ret_close;
 		}
 
 		if (family == AF_INET)
@@ -573,13 +578,9 @@
 			multiaddr_snprintf(strbuf, sizeof(strbuf), local_hosts, local_hosts_cnt);
 			LOGP(DLGLOBAL, LOGL_NOTICE, "unable to bind socket: %s:%u: %s\n",
 			     strbuf, local_port, strerror(errno));
-			for (i = 0; i < local_hosts_cnt; i++)
-			     freeaddrinfo(result[i]);
-			close(sfd);
-			return -ENODEV;
+			rc = -ENODEV;
+			goto ret_close;
 		}
-		for (i = 0; i < local_hosts_cnt; i++)
-			freeaddrinfo(result[i]);
 	}
 
 	/* Reached this point, if OSMO_SOCK_F_BIND then sfd is valid (>=0) or it
@@ -588,38 +589,27 @@
 
 	/* figure out remote side of socket */
 	if (flags & OSMO_SOCK_F_CONNECT) {
-		rc = addrinfo_helper_multi(result, family, type, proto, remote_hosts,
-					       remote_hosts_cnt, remote_port, false);
-		if (rc < 0) {
-			if (sfd >= 0)
-				close(sfd);
-			return -EINVAL;
-		}
-
 		if (sfd < 0) {
 			/* Since addrinfo_helper sets ai_family, socktype and
 			   ai_protocol in hints, we know all results will use same
 			   values, so simply pick the first one and pass it to create
 			   the socket:
 			*/
-			sfd = socket_helper(result[0], flags);
+			sfd = socket_helper(res_rem[0], flags);
 			if (sfd < 0) {
-				for (i = 0; i < remote_hosts_cnt; i++)
-					freeaddrinfo(result[i]);
-				return sfd;
+				rc = sfd;
+				goto ret_freeaddrinfo;
 			}
 		}
 
 		/* Build array of addresses taking first of same family for each host.
 		   TODO: Ideally we should use backtracking storing last used
 		   indexes and trying next combination if connect() fails .*/
-		rc = addrinfo_to_sockaddr(family, (const struct addrinfo **)result,
+		rc = addrinfo_to_sockaddr(family, (const struct addrinfo **)res_rem,
 					  remote_hosts, remote_hosts_cnt, addrs4, addrs6);
 		if (rc < 0) {
-			for (i = 0; i < remote_hosts_cnt; i++)
-				freeaddrinfo(result[i]);
-			close(sfd);
-			return -ENODEV;
+			rc = -ENODEV;
+			goto ret_close;
 		}
 
 		if (family == AF_INET)
@@ -631,13 +621,9 @@
 			multiaddr_snprintf(strbuf, sizeof(strbuf), remote_hosts, remote_hosts_cnt);
 			LOGP(DLGLOBAL, LOGL_ERROR, "unable to connect socket: %s:%u: %s\n",
 				strbuf, remote_port, strerror(errno));
-			for (i = 0; i < remote_hosts_cnt; i++)
-				freeaddrinfo(result[i]);
-			close(sfd);
-			return -ENODEV;
+			rc = -ENODEV;
+			goto ret_close;
 		}
-		for (i = 0; i < remote_hosts_cnt; i++)
-			freeaddrinfo(result[i]);
 	}
 
 	rc = osmo_sock_init_tail(sfd, type, flags);
@@ -646,7 +632,18 @@
 		sfd = -1;
 	}
 
-	return sfd;
+	rc = sfd;
+	goto ret_freeaddrinfo;
+
+ret_close:
+	if (sfd >= 0)
+		close(sfd);
+ret_freeaddrinfo:
+	for (i = 0; i < local_hosts_cnt; i++)
+		freeaddrinfo(res_loc[i]);
+	for (i = 0; i < remote_hosts_cnt; i++)
+		freeaddrinfo(res_rem[i]);
+	return rc;
 }
 #endif /* HAVE_LIBSCTP */
 

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

Gerrit-Project: libosmocore
Gerrit-Branch: master
Gerrit-Change-Id: I03147e3033a0c1fd04c9ac61d2ffbd78a1bb784a
Gerrit-Change-Number: 19723
Gerrit-PatchSet: 1
Gerrit-Owner: pespin <pespin at sysmocom.de>
Gerrit-MessageType: newchange
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20200819/be49f740/attachment.htm>


More information about the gerrit-log mailing list