libcommon: eliminate socket.c

Replace calls to make_sock() with osmo_sock_init_ofd().
Shame on me for not testing every single one in practice, I hope for peer
review to confirm that this should be correct... Read closely please!

The IPPROTO_GRE define seems to be unused (at least in osmo-bsc.git), drop it
completely.

Change-Id: Ia6e4e0e1eed3328fa25b3b90be376d532ad0e56b
This commit is contained in:
Neels Hofmeyr 2018-02-13 17:37:39 +01:00
parent 7997bf4bed
commit 978f58cd2e
7 changed files with 37 additions and 158 deletions

View File

@ -48,7 +48,6 @@ noinst_HEADERS = \
rest_octets.h \
rs232.h \
signal.h \
socket.h \
system_information.h \
ussd.h \
vty.h \

View File

@ -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 */

View File

@ -47,7 +47,7 @@
#include <osmocom/abis/ipaccess.h>
#include <osmocom/bsc/debug.h>
#include <osmocom/bsc/ipaccess.h>
#include <osmocom/bsc/socket.h>
#include <osmocom/core/socket.h>
#include <osmocom/core/talloc.h>
/* one instance of an ip.access protocol proxy */
@ -369,8 +369,11 @@ 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.priv_nr = UDP_TO_BTS;
ipbc->udp_bts_fd.cb = udp_fd_cb;
ipbc->udp_bts_fd.data = ipbc;
ret = osmo_sock_init_ofd(&ipbc->udp_bts_fd, AF_INET, SOCK_DGRAM, IPPROTO_UDP,
NULL, udp_port, OSMO_SOCK_F_BIND);
if (ret < 0)
goto err_udp_bts;
DEBUGP(DLINP, "(%u/%u/%u) Created UDP socket for injection "
@ -378,8 +381,11 @@ 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.priv_nr = UDP_TO_BSC;
ipbc->udp_bsc_fd.cb = udp_fd_cb;
ipbc->udp_bsc_fd.data = ipbc;
ret = osmo_sock_init_ofd(&ipbc->udp_bsc_fd, AF_INET, SOCK_DGRAM, IPPROTO_UDP,
NULL, udp_port, OSMO_SOCK_F_BIND);
if (ret < 0)
goto err_udp_bsc;
DEBUGP(DLINP, "(%u/%u/%u) Created UDP socket for injection "
@ -390,13 +396,12 @@ static int ipbc_alloc_connect(struct ipa_proxy_conn *ipc, struct osmo_fd *bfd,
if (gprs_ns_ipaddr) {
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.priv_nr = 0;
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,
listen_ipaddr, 0, OSMO_SOCK_F_BIND);
if (ret < 0) {
LOGP(DLINP, LOGL_ERROR, "Creating the GPRS socket failed.\n");
goto err_udp_bsc;
@ -1062,15 +1067,18 @@ static int ipaccess_proxy_setup(void)
osmo_timer_setup(&ipp->reconn_timer, reconn_tmr_cb, 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.priv_nr = OML_FROM_BTS;
ipp->oml_listen_fd.cb = listen_fd_cb;
ret = osmo_sock_init_ofd(&ipp->oml_listen_fd, AF_INET, SOCK_STREAM, IPPROTO_TCP,
NULL, IPA_TCP_PORT_OML, OSMO_SOCK_F_BIND);
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.priv_nr = RSL_FROM_BTS;
ipp->rsl_listen_fd.cb = listen_fd_cb;
ret = osmo_sock_init_ofd(&ipp->rsl_listen_fd, AF_INET, SOCK_STREAM, IPPROTO_TCP,
NULL, IPA_TCP_PORT_RSL, OSMO_SOCK_F_BIND);
if (ret < 0)
return ret;

View File

@ -22,7 +22,6 @@ noinst_LIBRARIES = \
libcommon_a_SOURCES = \
gsm_data.c \
gsm_data_shared.c \
socket.c \
talloc_ctx.c \
handover_cfg.c \
$(NULL)

View File

@ -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 <osmocom/bsc/debug.h>
#include <osmocom/bsc/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;
}

View File

@ -46,7 +46,6 @@
#include <osmocom/bsc/bsc_msg_filter.h>
#include <osmocom/bsc/ipaccess.h>
#include <osmocom/bsc/abis_nm.h>
#include <osmocom/bsc/socket.h>
#include <osmocom/bsc/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>
@ -80,7 +80,7 @@
#define SCCP_CLOSE_TIME_TIMEOUT 19
static const char *config_file = "bsc-nat.cfg";
static struct in_addr local_addr;
static const char *local_addr;
static struct osmo_fd bsc_listen;
static const char *msc_ip = NULL;
static struct osmo_timer_list sccp_close;
@ -1507,7 +1507,7 @@ static void handle_options(int argc, char **argv)
msc_ip = optarg;
break;
case 'l':
inet_aton(optarg, &local_addr);
local_addr = optarg;
break;
default:
/* ignore */
@ -1693,7 +1693,7 @@ int main(int argc, char **argv)
/* parse options */
local_addr.s_addr = INADDR_ANY;
local_addr = NULL;
handle_options(argc, argv);
nat->include_base = dirname(talloc_strdup(tall_bsc_ctx, config_file));
@ -1757,8 +1757,10 @@ 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,
local_addr, 5000, OSMO_SOCK_F_BIND);
if (rc != 0) {
fprintf(stderr, "Failed to listen for BSC.\n");
exit(1);

View File

@ -20,11 +20,12 @@
*
*/
#include <osmocom/core/socket.h>
#include <osmocom/bsc/bsc_nat.h>
#include <osmocom/bsc/bsc_nat_sccp.h>
#include <osmocom/bsc/bsc_msg_filter.h>
#include <osmocom/bsc/ipaccess.h>
#include <osmocom/bsc/socket.h>
#include <osmocom/bsc/debug.h>
#include <osmocom/gsm/protocol/gsm_08_08.h>
@ -282,15 +283,10 @@ static int ussd_listen_cb(struct osmo_fd *bfd, unsigned int what)
int bsc_ussd_init(struct bsc_nat *nat)
{
struct in_addr addr;
addr.s_addr = INADDR_ANY;
if (nat->ussd_local)
inet_aton(nat->ussd_local, &addr);
nat->ussd_listen.cb = ussd_listen_cb;
nat->ussd_listen.data = nat;
return make_sock(&nat->ussd_listen, IPPROTO_TCP,
ntohl(addr.s_addr), 5001, 0, ussd_listen_cb, nat);
return osmo_sock_init_ofd(&nat->ussd_listen, AF_INET, SOCK_STREAM, IPPROTO_TCP,
nat->ussd_local, 5001, OSMO_SOCK_F_BIND);
}
static int forward_ussd_simple(struct nat_sccp_connection *con, struct msgb *input)