summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHarald Welte <laforge@gnumonks.org>2016-11-26 14:35:46 +0100
committerHarald Welte <laforge@gnumonks.org>2016-11-26 17:10:30 +0100
commit3679ea8fb2e8323f0493357503d82ea0ce34aace (patch)
treec06341fed479bf64c7f39b1d306b7fa6654c17a5
parent3d7230b2a2bb5e49192ee316418b0e1572882555 (diff)
Replace local make_sock() function with libosmocore osmo_fd_init_ofd()laforge/coverity
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
-rw-r--r--openbsc/include/openbsc/Makefile.am1
-rw-r--r--openbsc/include/openbsc/socket.h14
-rw-r--r--openbsc/src/ipaccess/ipaccess-proxy.c43
-rw-r--r--openbsc/src/libcommon/Makefile.am1
-rw-r--r--openbsc/src/libcommon/socket.c111
-rw-r--r--openbsc/src/osmo-bsc_nat/bsc_nat.c9
-rw-r--r--openbsc/src/osmo-bsc_nat/bsc_ussd.c8
7 files changed, 40 insertions, 147 deletions
diff --git a/openbsc/include/openbsc/Makefile.am b/openbsc/include/openbsc/Makefile.am
index 2b54c4345..c5a2e911b 100644
--- a/openbsc/include/openbsc/Makefile.am
+++ b/openbsc/include/openbsc/Makefile.am
@@ -74,7 +74,6 @@ noinst_HEADERS = \
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 0fd85f104..000000000
--- 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 9e8ec88e3..bb52a4811 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 @@ static int ipbc_alloc_connect(struct ipa_proxy_conn *ipc, struct osmo_fd *bfd,
/* 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 @@ static int ipbc_alloc_connect(struct ipa_proxy_conn *ipc, struct osmo_fd *bfd,
/* 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 @@ static int ipbc_alloc_connect(struct ipa_proxy_conn *ipc, struct osmo_fd *bfd,
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 @@ static int ipaccess_proxy_setup(void)
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 6cfebc2da..93b188da2 100644
--- a/openbsc/src/libcommon/Makefile.am
+++ b/openbsc/src/libcommon/Makefile.am
@@ -23,7 +23,6 @@ libcommon_a_SOURCES = \
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 2a64767f8..000000000
--- 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@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 a4dd67901..933cfeb59 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 @@ int main(int argc, char **argv)
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 2f68381ac..b052eb86f 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 @@ int bsc_ussd_init(struct bsc_nat *nat)
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)