[PATCH] openbsc[master]: Replace local make_sock() function with libosmocore osmo_fd_...

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 Dec 2 11:58:10 UTC 2016


Hello Jenkins Builder,

I'd like you to reexamine a change.  Please visit

    https://gerrit.osmocom.org/1317

to look at the new patch set (#4).

Replace local make_sock() function with libosmocore osmo_fd_init_ofd()

The local 'make_sock()' function should have been deprecated since 2011,
when we started to have general socket related utility functions in
libosmocore.

Fixes: Coverity CID 57645
Change-Id: I2329da82d2b6612e281086ca67c7836b97e03d3d
---
M openbsc/include/openbsc/Makefile.am
D openbsc/include/openbsc/socket.h
M openbsc/src/ipaccess/ipaccess-proxy.c
M openbsc/src/libcommon/Makefile.am
D openbsc/src/libcommon/socket.c
M openbsc/src/osmo-bsc_nat/bsc_nat.c
M openbsc/src/osmo-bsc_nat/bsc_ussd.c
7 files changed, 40 insertions(+), 147 deletions(-)


  git pull ssh://gerrit.osmocom.org:29418/openbsc refs/changes/17/1317/4

diff --git a/openbsc/include/openbsc/Makefile.am b/openbsc/include/openbsc/Makefile.am
index 2b54c43..c5a2e91 100644
--- a/openbsc/include/openbsc/Makefile.am
+++ b/openbsc/include/openbsc/Makefile.am
@@ -74,7 +74,6 @@
 	slhc.h \
 	smpp.h \
 	sms_queue.h \
-	socket.h \
 	system_information.h \
 	token_auth.h \
 	transaction.h \
diff --git a/openbsc/include/openbsc/socket.h b/openbsc/include/openbsc/socket.h
deleted file mode 100644
index 0fd85f1..0000000
--- a/openbsc/include/openbsc/socket.h
+++ /dev/null
@@ -1,14 +0,0 @@
-#ifndef _BSC_SOCKET_H
-#define _BSC_SOCKET_H
-
-#include <osmocom/core/select.h>
-
-#ifndef IPPROTO_GRE
-#define IPPROTO_GRE 47
-#endif
-
-int make_sock(struct osmo_fd *bfd, int proto,
-	      uint32_t ip, uint16_t port, int priv_nr,
-	      int (*cb)(struct osmo_fd *fd, unsigned int what), void *data);
-
-#endif /* _BSC_SOCKET_H */
diff --git a/openbsc/src/ipaccess/ipaccess-proxy.c b/openbsc/src/ipaccess/ipaccess-proxy.c
index 9e8ec88..bb52a48 100644
--- a/openbsc/src/ipaccess/ipaccess-proxy.c
+++ b/openbsc/src/ipaccess/ipaccess-proxy.c
@@ -40,6 +40,7 @@
 #include <openbsc/gsm_data.h>
 #include <osmocom/core/application.h>
 #include <osmocom/core/select.h>
+#include <osmocom/core/socket.h>
 #include <osmocom/gsm/tlv.h>
 #include <osmocom/core/msgb.h>
 #include <osmocom/gsm/ipa.h>
@@ -47,7 +48,6 @@
 #include <osmocom/abis/ipaccess.h>
 #include <openbsc/debug.h>
 #include <openbsc/ipaccess.h>
-#include <openbsc/socket.h>
 #include <osmocom/core/talloc.h>
 
 /* one instance of an ip.access protocol proxy */
@@ -369,8 +369,12 @@
 
 	/* Create UDP socket for BTS packet injection */
 	udp_port = 10000 + (site_id % 1000)*100 + (bts_id % 100);
-	ret = make_sock(&ipbc->udp_bts_fd, IPPROTO_UDP, INADDR_ANY, udp_port,
-			UDP_TO_BTS, udp_fd_cb, ipbc);
+	ipbc->udp_bts_fd.cb = udp_fd_cb;
+	ipbc->udp_bts_fd.data = ipbc;
+	ipbc->udp_bts_fd.priv_nr = UDP_TO_BTS;
+	ret = osmo_sock_init_ofd(&ipbc->udp_bts_fd, AF_INET, SOCK_DGRAM,
+				 IPPROTO_UDP, "0.0.0.0", udp_port,
+				 OSMO_SOCK_F_BIND|OSMO_SOCK_F_NONBLOCK);
 	if (ret < 0)
 		goto err_udp_bts;
 	DEBUGP(DLINP, "(%u/%u/%u) Created UDP socket for injection "
@@ -378,8 +382,12 @@
 
 	/* Create UDP socket for BSC packet injection */
 	udp_port = 20000 + (site_id % 1000)*100 + (bts_id % 100);
-	ret = make_sock(&ipbc->udp_bsc_fd, IPPROTO_UDP, INADDR_ANY, udp_port,
-			UDP_TO_BSC, udp_fd_cb, ipbc);
+	ipbc->udp_bsc_fd.cb = udp_fd_cb;
+	ipbc->udp_bsc_fd.data = ipbc;
+	ipbc->udp_bsc_fd.priv_nr = UDP_TO_BSC;
+	ret = osmo_sock_init_ofd(&ipbc->udp_bsc_fd, AF_INET, SOCK_DGRAM,
+				 IPPROTO_UDP, "0.0.0.0", udp_port,
+				 OSMO_SOCK_F_BIND|OSMO_SOCK_F_NONBLOCK);
 	if (ret < 0)
 		goto err_udp_bsc;
 	DEBUGP(DLINP, "(%u/%u/%u) Created UDP socket for injection "
@@ -391,12 +399,14 @@
 		struct sockaddr_in sock;
 		socklen_t len = sizeof(sock);
 		struct in_addr addr;
-		uint32_t ip;
 
 		inet_aton(listen_ipaddr, &addr);
-		ip = ntohl(addr.s_addr); /* make_sock() needs host byte order */
-		ret = make_sock(&ipbc->gprs_ns_fd, IPPROTO_UDP, ip, 0, 0,
-				gprs_ns_cb, ipbc);
+
+		ipbc->gprs_ns_fd.cb = gprs_ns_cb;
+		ipbc->gprs_ns_fd.data = ipbc;
+		ret = osmo_sock_init_ofd(&ipbc->gprs_ns_fd, AF_INET, SOCK_DGRAM,
+					 IPPROTO_UDP, inet_ntoa(addr), 0,
+					 OSMO_SOCK_F_BIND|OSMO_SOCK_F_NONBLOCK);
 		if (ret < 0) {
 			LOGP(DLINP, LOGL_ERROR, "Creating the GPRS socket failed.\n");
 			goto err_udp_bsc;
@@ -1063,15 +1073,20 @@
 	ipp->reconn_timer.data = ipp;
 
 	/* Listen for OML connections */
-	ret = make_sock(&ipp->oml_listen_fd, IPPROTO_TCP, INADDR_ANY,
-			IPA_TCP_PORT_OML, OML_FROM_BTS, listen_fd_cb, NULL);
+	ipp->oml_listen_fd.cb = listen_fd_cb;
+	ipp->oml_listen_fd.priv_nr = OML_FROM_BTS;
+	ret = osmo_sock_init_ofd(&ipp->oml_listen_fd, AF_INET, SOCK_STREAM,
+				 IPPROTO_TCP, "0.0.0.0", IPA_TCP_PORT_OML,
+				 OSMO_SOCK_F_BIND|OSMO_SOCK_F_NONBLOCK);
 	if (ret < 0)
 		return ret;
 
 	/* Listen for RSL connections */
-	ret = make_sock(&ipp->rsl_listen_fd, IPPROTO_TCP, INADDR_ANY,
-			IPA_TCP_PORT_RSL, RSL_FROM_BTS, listen_fd_cb, NULL);
-
+	ipp->rsl_listen_fd.cb =listen_fd_cb;
+	ipp->rsl_listen_fd.priv_nr = RSL_FROM_BTS;
+	ret = osmo_sock_init_ofd(&ipp->rsl_listen_fd, AF_INET, SOCK_STREAM,
+				 IPPROTO_TCP, "0.0.0.0", IPA_TCP_PORT_RSL,
+				 OSMO_SOCK_F_BIND|OSMO_SOCK_F_NONBLOCK);
 	if (ret < 0)
 		return ret;
 
diff --git a/openbsc/src/libcommon/Makefile.am b/openbsc/src/libcommon/Makefile.am
index 6cfebc2..93b188d 100644
--- a/openbsc/src/libcommon/Makefile.am
+++ b/openbsc/src/libcommon/Makefile.am
@@ -23,7 +23,6 @@
 	debug.c \
 	gsm_data.c \
 	gsm_data_shared.c \
-	socket.c \
 	talloc_ctx.c \
 	gsm_subscriber_base.c \
 	$(NULL)
diff --git a/openbsc/src/libcommon/socket.c b/openbsc/src/libcommon/socket.c
deleted file mode 100644
index 2a64767..0000000
--- a/openbsc/src/libcommon/socket.c
+++ /dev/null
@@ -1,111 +0,0 @@
-/* OpenBSC sokcet code, taken from Abis input driver for ip.access */
-
-/* (C) 2009 by Harald Welte <laforge at gnumonks.org>
- * (C) 2010 by Holger Hans Peter Freyther
- * (C) 2010 by On-Waves
- *
- * All Rights Reserved
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as published by
- * the Free Software Foundation; either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program.  If not, see <http://www.gnu.org/licenses/>.
- *
- */
-
-#include <stdio.h>
-#include <unistd.h>
-#include <stdlib.h>
-#include <errno.h>
-#include <string.h>
-#include <time.h>
-#include <sys/fcntl.h>
-#include <sys/socket.h>
-#include <sys/ioctl.h>
-#include <arpa/inet.h>
-
-#include <osmocom/core/select.h>
-#include <osmocom/gsm/tlv.h>
-#include <osmocom/core/msgb.h>
-#include <openbsc/debug.h>
-#include <openbsc/gsm_data.h>
-#include <osmocom/core/talloc.h>
-
-int make_sock(struct osmo_fd *bfd, int proto,
-	      uint32_t ip, uint16_t port, int priv_nr,
-	      int (*cb)(struct osmo_fd *fd, unsigned int what), void *data)
-{
-	struct sockaddr_in addr;
-	int ret, on = 1;
-	int type = SOCK_STREAM;
-
-	switch (proto) {
-	case IPPROTO_TCP:
-		type = SOCK_STREAM;
-		break;
-	case IPPROTO_UDP:
-		type = SOCK_DGRAM;
-		break;
-#ifdef IPPROTO_GRE
-	case IPPROTO_GRE:
-		type = SOCK_RAW;
-		break;
-#endif
-	default:
-		return -EINVAL;
-	}
-
-	bfd->fd = socket(AF_INET, type, proto);
-	bfd->cb = cb;
-	bfd->when = BSC_FD_READ;
-	bfd->data = data;
-	bfd->priv_nr = priv_nr;
-
-	if (bfd->fd < 0) {
-		LOGP(DLINP, LOGL_ERROR, "could not create socket.\n");
-		return -EIO;
-	}
-
-	memset(&addr, 0, sizeof(addr));
-	addr.sin_family = AF_INET;
-	addr.sin_port = htons(port);
-	if (ip != INADDR_ANY)
-		addr.sin_addr.s_addr = htonl(ip);
-	else
-		addr.sin_addr.s_addr = INADDR_ANY;
-
-	setsockopt(bfd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
-
-	ret = bind(bfd->fd, (struct sockaddr *) &addr, sizeof(addr));
-	if (ret < 0) {
-		LOGP(DLINP, LOGL_ERROR, "could not bind socket %s\n",
-			strerror(errno));
-		close(bfd->fd);
-		return -EIO;
-	}
-
-	if (proto == IPPROTO_TCP) {
-		ret = listen(bfd->fd, 1);
-		if (ret < 0) {
-			perror("listen");
-			close(bfd->fd);
-			return ret;
-		}
-	}
-
-	ret = osmo_fd_register(bfd);
-	if (ret < 0) {
-		perror("register_listen_fd");
-		close(bfd->fd);
-		return ret;
-	}
-	return 0;
-}
diff --git a/openbsc/src/osmo-bsc_nat/bsc_nat.c b/openbsc/src/osmo-bsc_nat/bsc_nat.c
index a4dd679..933cfeb 100644
--- a/openbsc/src/osmo-bsc_nat/bsc_nat.c
+++ b/openbsc/src/osmo-bsc_nat/bsc_nat.c
@@ -46,7 +46,6 @@
 #include <openbsc/bsc_msg_filter.h>
 #include <openbsc/ipaccess.h>
 #include <openbsc/abis_nm.h>
-#include <openbsc/socket.h>
 #include <openbsc/vty.h>
 
 #include <osmocom/ctrl/control_cmd.h>
@@ -59,6 +58,7 @@
 #include <osmocom/core/application.h>
 #include <osmocom/core/talloc.h>
 #include <osmocom/core/stats.h>
+#include <osmocom/core/socket.h>
 
 #include <osmocom/gsm/tlv.h>
 #include <osmocom/gsm/gsm0808.h>
@@ -1684,8 +1684,11 @@
 	bsc_msc_connect(nat->msc_con);
 
 	/* wait for the BSC */
-	rc = make_sock(&bsc_listen, IPPROTO_TCP, ntohl(local_addr.s_addr),
-		       5000, 0, ipaccess_listen_bsc_cb, nat);
+	bsc_listen.cb = ipaccess_listen_bsc_cb;
+	bsc_listen.data = nat;
+	rc = osmo_sock_init_ofd(&bsc_listen, AF_INET, SOCK_STREAM, IPPROTO_TCP,
+				 inet_ntoa(local_addr), 5000,
+				 OSMO_SOCK_F_BIND|OSMO_SOCK_F_NONBLOCK);
 	if (rc != 0) {
 		fprintf(stderr, "Failed to listen for BSC.\n");
 		exit(1);
diff --git a/openbsc/src/osmo-bsc_nat/bsc_ussd.c b/openbsc/src/osmo-bsc_nat/bsc_ussd.c
index 2f68381..b052eb8 100644
--- a/openbsc/src/osmo-bsc_nat/bsc_ussd.c
+++ b/openbsc/src/osmo-bsc_nat/bsc_ussd.c
@@ -24,10 +24,10 @@
 #include <openbsc/bsc_nat_sccp.h>
 #include <openbsc/bsc_msg_filter.h>
 #include <openbsc/ipaccess.h>
-#include <openbsc/socket.h>
 
 #include <osmocom/gsm/protocol/gsm_08_08.h>
 #include <osmocom/gsm/gsm0480.h>
+#include <osmocom/core/socket.h>
 #include <osmocom/core/talloc.h>
 #include <osmocom/gsm/tlv.h>
 #include <osmocom/gsm/ipa.h>
@@ -287,8 +287,10 @@
 		inet_aton(nat->ussd_local, &addr);
 
 	nat->ussd_listen.data = nat;
-	return make_sock(&nat->ussd_listen, IPPROTO_TCP,
-			 ntohl(addr.s_addr), 5001, 0, ussd_listen_cb, nat);
+	nat->ussd_listen.cb = ussd_listen_cb;
+	return osmo_sock_init_ofd(&nat->ussd_listen, AF_INET, SOCK_STREAM,
+				  IPPROTO_TCP, inet_ntoa(addr), 5001,
+				  OSMO_SOCK_F_BIND|OSMO_SOCK_F_NONBLOCK);
 }
 
 static int forward_ussd_simple(struct nat_sccp_connection *con, struct msgb *input)

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

Gerrit-MessageType: newpatchset
Gerrit-Change-Id: I2329da82d2b6612e281086ca67c7836b97e03d3d
Gerrit-PatchSet: 4
Gerrit-Project: openbsc
Gerrit-Branch: master
Gerrit-Owner: Harald Welte <laforge at gnumonks.org>
Gerrit-Reviewer: Harald Welte <laforge at gnumonks.org>
Gerrit-Reviewer: Jenkins Builder



More information about the gerrit-log mailing list