From 3679ea8fb2e8323f0493357503d82ea0ce34aace Mon Sep 17 00:00:00 2001 From: Harald Welte Date: Sat, 26 Nov 2016 14:35:46 +0100 Subject: 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 --- openbsc/include/openbsc/Makefile.am | 1 - openbsc/include/openbsc/socket.h | 14 ----- openbsc/src/ipaccess/ipaccess-proxy.c | 43 ++++++++----- openbsc/src/libcommon/Makefile.am | 1 - openbsc/src/libcommon/socket.c | 111 ---------------------------------- openbsc/src/osmo-bsc_nat/bsc_nat.c | 9 ++- openbsc/src/osmo-bsc_nat/bsc_ussd.c | 8 ++- 7 files changed, 40 insertions(+), 147 deletions(-) delete mode 100644 openbsc/include/openbsc/socket.h delete mode 100644 openbsc/src/libcommon/socket.c 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 - -#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 #include #include +#include #include #include #include @@ -47,7 +48,6 @@ #include #include #include -#include #include /* 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 - * (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 . - * - */ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include -#include - -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 #include #include -#include #include #include @@ -59,6 +58,7 @@ #include #include #include +#include #include #include @@ -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 #include #include -#include #include #include +#include #include #include #include @@ -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) -- cgit v1.2.3