[PATCH] osmo-pcap[master]: Use libosmocore osmo_sock_init2() instead of local implement...

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/.

Harald Welte gerrit-no-reply at lists.osmocom.org
Fri Jul 21 16:27:32 UTC 2017


Review at  https://gerrit.osmocom.org/3335

Use libosmocore osmo_sock_init2() instead of local implementation

A related function for "create a socket, bind it locally and connect
remotely" has meanwhile been introduced in libosmocore, so the local
implementation can go.

Change-Id: Ieda77ad8b3f7b89faa09882c0037562ce4d0fc89
---
M src/osmo_client_network.c
1 file changed, 2 insertions(+), 134 deletions(-)


  git pull ssh://gerrit.osmocom.org:29418/osmo-pcap refs/changes/35/3335/1

diff --git a/src/osmo_client_network.c b/src/osmo_client_network.c
index 27c649a..03cb281 100644
--- a/src/osmo_client_network.c
+++ b/src/osmo_client_network.c
@@ -43,138 +43,6 @@
 #include <unistd.h>
 
 
-/*
- * Move to libosmocore... if the api makes source
- */
-static int sock_src_init(uint16_t family, uint16_t type, uint8_t proto,
-		   const char *src, uint16_t src_port,
-		   const char *host, uint16_t port, unsigned int flags)
-{
-	struct addrinfo hints, *result, *rp;
-	struct addrinfo *src_result, *src_rp = NULL;
-	int sfd, rc, on = 1;
-	char portbuf[16];
-	char src_portbuf[16];
-
-	sprintf(portbuf, "%u", port);
-	sprintf(src_portbuf, "%u", src_port);
-	memset(&hints, 0, sizeof(struct addrinfo));
-	hints.ai_family = family;
-	if (type == SOCK_RAW) {
-		/* Workaround for glibc, that returns EAI_SERVICE (-8) if
-		 * SOCK_RAW and IPPROTO_GRE is used.
-		 */
-		hints.ai_socktype = SOCK_DGRAM;
-		hints.ai_protocol = IPPROTO_UDP;
-	} else {
-		hints.ai_socktype = type;
-		hints.ai_protocol = proto;
-	}
-
-	rc = getaddrinfo(host, portbuf, &hints, &result);
-	if (rc != 0) {
-		fprintf(stderr, "getaddrinfo returned NULL: %s:%u: %s\n",
-			host, port, strerror(errno));
-		return -EINVAL;
-	}
-
-	if (src) {
-		rc = getaddrinfo(src, src_portbuf, &hints, &src_result);
-		if (rc != 0) {
-			fprintf(stderr, "getaddrinfo returned NULL: %s:%u: %s\n",
-				src, src_port, strerror(errno));
-			freeaddrinfo(result);
-			return -EINVAL;
-		}
-
-		/* select an address */
-		for (src_rp = src_result; src_rp != NULL; src_rp = src_rp->ai_next) {
-			/* Workaround for glibc again */
-			if (type == SOCK_RAW) {
-				src_rp->ai_socktype = SOCK_RAW;
-				src_rp->ai_protocol = proto;
-			}
-			break;
-		}
-
-		if (!src_rp) {
-			fprintf(stderr, "Failed to get src: %s:%u %s\n",
-				src, src_port, strerror(errno));
-			freeaddrinfo(result);
-			freeaddrinfo(src_result);
-			return -EINVAL;
-		}
-	}
-
-
-	for (rp = result; rp != NULL; rp = rp->ai_next) {
-		/* Workaround for glibc again */
-		if (type == SOCK_RAW) {
-			rp->ai_socktype = SOCK_RAW;
-			rp->ai_protocol = proto;
-		}
-
-		sfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
-		if (sfd == -1)
-			continue;
-		if (flags & OSMO_SOCK_F_NONBLOCK) {
-			if (ioctl(sfd, FIONBIO, (unsigned char *)&on) < 0) {
-				fprintf(stderr,
-					"cannot set this socket unblocking:"
-					" %s:%u: %s\n",
-					host, port, strerror(errno));
-				close(sfd);
-				freeaddrinfo(result);
-				return -EINVAL;
-			}
-		}
-
-
-		if (src_rp) {
-			rc = bind(sfd, src_rp->ai_addr, src_rp->ai_addrlen);
-			if (rc != 0) {
-				fprintf(stderr,
-					"cannot bind socket:"
-					" %s:%u: %s\n",
-					src, src_port, strerror(errno));
-				close(sfd);
-				continue;
-			}
-		}
-
-		if (flags & OSMO_SOCK_F_CONNECT) {
-			rc = connect(sfd, rp->ai_addr, rp->ai_addrlen);
-			if (rc != -1 || (rc == -1 && errno == EINPROGRESS))
-				break;
-		} else {
-			rc = setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR,
-							&on, sizeof(on));
-			if (rc < 0) {
-				fprintf(stderr,
-					"cannot setsockopt socket:"
-					" %s:%u: %s\n",
-					host, port, strerror(errno));
-				break;
-			}
-			if (bind(sfd, rp->ai_addr, rp->ai_addrlen) != -1)
-				break;
-		}
-		close(sfd);
-	}
-	freeaddrinfo(result);
-	if (src)
-		freeaddrinfo(src_result);
-
-	if (rp == NULL) {
-		fprintf(stderr, "unable to connect/bind socket: %s:%u: %s\n",
-			host, port, strerror(errno));
-		return -ENODEV;
-	}
-
-	setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
-	return sfd;
-}
-
 static void _osmo_client_connect(void *_data)
 {
 	osmo_client_connect((struct osmo_pcap_client_conn *) _data);
@@ -369,10 +237,10 @@
 	conn->wqueue.bfd.when = BSC_FD_READ;
 	osmo_wqueue_clear(&conn->wqueue);
 
-	fd = sock_src_init(AF_INET, SOCK_STREAM, IPPROTO_TCP,
+	fd = osmo_sock_init2(AF_INET, SOCK_STREAM, IPPROTO_TCP,
 				conn->source_ip, 0,
 				conn->srv_ip, conn->srv_port,
-				OSMO_SOCK_F_CONNECT | OSMO_SOCK_F_NONBLOCK);
+				OSMO_SOCK_F_BIND | OSMO_SOCK_F_CONNECT | OSMO_SOCK_F_NONBLOCK);
 	if (fd < 0) {
 		LOGP(DCLIENT, LOGL_ERROR,
 		     "Failed to connect conn=%s to %s:%d\n",

-- 
To view, visit https://gerrit.osmocom.org/3335
To unsubscribe, visit https://gerrit.osmocom.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ieda77ad8b3f7b89faa09882c0037562ce4d0fc89
Gerrit-PatchSet: 1
Gerrit-Project: osmo-pcap
Gerrit-Branch: master
Gerrit-Owner: Harald Welte <laforge at gnumonks.org>



More information about the gerrit-log mailing list