From: Pablo Neira Ayuso pablo@gnumonks.org
This patchset is the first in the series to cleanup redundant code in ipaccess tools.
They are available in the pablo/cleanups branch in openbsc git tree.
Please, merge it! Thanks.
Pablo Neira Ayuso (3): ipaccess-config: exit if no network interface is specified libcommon: socket: extend make_sock() prototype ipaccess-proxy: get rid of internal make_sock() implementation
openbsc/include/openbsc/socket.h | 5 ++- openbsc/src/ipaccess/ipaccess-find.c | 1 + openbsc/src/ipaccess/ipaccess-proxy.c | 42 ++------------------------------ openbsc/src/libabis/input/hsl.c | 4 +- openbsc/src/libabis/input/ipaccess.c | 8 +++--- openbsc/src/libcommon/socket.c | 10 ++++--- openbsc/src/libgb/gprs_ns.c | 2 +- openbsc/src/libgb/gprs_ns_frgre.c | 2 +- openbsc/src/osmo-bsc_nat/bsc_ussd.c | 2 +- 9 files changed, 22 insertions(+), 54 deletions(-)
From: Pablo Neira Ayuso pablo@gnumonks.org
ipaccess-config has to exit if no network interface is specified, otherwise it uses argv[1] which has a uninitialized value. --- openbsc/src/ipaccess/ipaccess-find.c | 1 + 1 files changed, 1 insertions(+), 0 deletions(-)
diff --git a/openbsc/src/ipaccess/ipaccess-find.c b/openbsc/src/ipaccess/ipaccess-find.c index 6dc2f06..a273609 100644 --- a/openbsc/src/ipaccess/ipaccess-find.c +++ b/openbsc/src/ipaccess/ipaccess-find.c @@ -196,6 +196,7 @@ int main(int argc, char **argv) if (argc < 2) { fprintf(stdout, "you might need to specify the outgoing\n" " network interface, e.g. ``%s eth0''\n", argv[0]); + exit(EXIT_FAILURE); }
ifname = argv[1];
From: Pablo Neira Ayuso pablo@gnumonks.org
This patch extends the make_sock() prototype so you can fully set the fields priv_nr and data of the bsc_fd structure.
This is the first step to get rid of the internal make_sock() implementation that ipaccess-proxy uses.
This patch includes a minor cleanup to pass INADDR_ANY instead of zero, if you do not want to bind the socket to one specific address. --- openbsc/include/openbsc/socket.h | 5 +++-- openbsc/src/libabis/input/hsl.c | 4 ++-- openbsc/src/libabis/input/ipaccess.c | 8 ++++---- openbsc/src/libcommon/socket.c | 10 ++++++---- openbsc/src/libgb/gprs_ns.c | 2 +- openbsc/src/libgb/gprs_ns_frgre.c | 2 +- openbsc/src/osmo-bsc_nat/bsc_ussd.c | 2 +- 7 files changed, 18 insertions(+), 15 deletions(-)
diff --git a/openbsc/include/openbsc/socket.h b/openbsc/include/openbsc/socket.h index faca764..dac4ca7 100644 --- a/openbsc/include/openbsc/socket.h +++ b/openbsc/include/openbsc/socket.h @@ -8,7 +8,8 @@ #define IPPROTO_GRE 47 #endif
-int make_sock(struct bsc_fd *bfd, int proto, u_int32_t ip, u_int16_t port, - int (*cb)(struct bsc_fd *fd, unsigned int what)); +int make_sock(struct bsc_fd *bfd, int proto, + u_int32_t ip, u_int16_t port, int priv_nr, + int (*cb)(struct bsc_fd *fd, unsigned int what), void *data);
#endif /* _BSC_SOCKET_H */ diff --git a/openbsc/src/libabis/input/hsl.c b/openbsc/src/libabis/input/hsl.c index d8f284e..2b9ab1d 100644 --- a/openbsc/src/libabis/input/hsl.c +++ b/openbsc/src/libabis/input/hsl.c @@ -451,8 +451,8 @@ int hsl_setup(struct gsm_network *gsmnet) e1h->gsmnet = gsmnet;
/* Listen for connections */ - ret = make_sock(&e1h->listen_fd, IPPROTO_TCP, 0, HSL_TCP_PORT, - listen_fd_cb); + ret = make_sock(&e1h->listen_fd, IPPROTO_TCP, INADDR_ANY, HSL_TCP_PORT, + 0, listen_fd_cb, NULL); if (ret < 0) return ret;
diff --git a/openbsc/src/libabis/input/ipaccess.c b/openbsc/src/libabis/input/ipaccess.c index f074616..ab1d41d 100644 --- a/openbsc/src/libabis/input/ipaccess.c +++ b/openbsc/src/libabis/input/ipaccess.c @@ -782,14 +782,14 @@ int ipaccess_setup(struct gsm_network *gsmnet) e1h->gsmnet = gsmnet;
/* Listen for OML connections */ - ret = make_sock(&e1h->listen_fd, IPPROTO_TCP, 0, IPA_TCP_PORT_OML, - listen_fd_cb); + ret = make_sock(&e1h->listen_fd, IPPROTO_TCP, INADDR_ANY, + IPA_TCP_PORT_OML, 0, listen_fd_cb, NULL); if (ret < 0) return ret;
/* Listen for RSL connections */ - ret = make_sock(&e1h->rsl_listen_fd, IPPROTO_TCP, 0, - IPA_TCP_PORT_RSL, rsl_listen_fd_cb); + ret = make_sock(&e1h->rsl_listen_fd, IPPROTO_TCP, INADDR_ANY, + IPA_TCP_PORT_RSL, 0, rsl_listen_fd_cb, NULL); if (ret < 0) return ret;
diff --git a/openbsc/src/libcommon/socket.c b/openbsc/src/libcommon/socket.c index 42d7b48..5ca7ec9 100644 --- a/openbsc/src/libcommon/socket.c +++ b/openbsc/src/libcommon/socket.c @@ -40,8 +40,9 @@ #include <openbsc/gsm_data.h> #include <osmocom/core/talloc.h>
-int make_sock(struct bsc_fd *bfd, int proto, u_int32_t ip, u_int16_t port, - int (*cb)(struct bsc_fd *fd, unsigned int what)) +int make_sock(struct bsc_fd *bfd, int proto, + u_int32_t ip, u_int16_t port, int priv_nr, + int (*cb)(struct bsc_fd *fd, unsigned int what), void *data) { struct sockaddr_in addr; int ret, on = 1; @@ -64,7 +65,8 @@ int make_sock(struct bsc_fd *bfd, int proto, u_int32_t ip, u_int16_t port, bfd->fd = socket(AF_INET, type, proto); bfd->cb = cb; bfd->when = BSC_FD_READ; - //bfd->data = line; + bfd->data = data; + bfd->priv_nr = priv_nr;
if (bfd->fd < 0) { LOGP(DINP, LOGL_ERROR, "could not create socket.\n"); @@ -74,7 +76,7 @@ int make_sock(struct bsc_fd *bfd, int proto, u_int32_t ip, u_int16_t port, memset(&addr, 0, sizeof(addr)); addr.sin_family = AF_INET; addr.sin_port = htons(port); - if (ip) + if (ip != INADDR_ANY) addr.sin_addr.s_addr = htonl(ip); else addr.sin_addr.s_addr = INADDR_ANY; diff --git a/openbsc/src/libgb/gprs_ns.c b/openbsc/src/libgb/gprs_ns.c index 877a065..95e5a55 100644 --- a/openbsc/src/libgb/gprs_ns.c +++ b/openbsc/src/libgb/gprs_ns.c @@ -946,7 +946,7 @@ int gprs_ns_nsip_listen(struct gprs_ns_inst *nsi) int ret;
ret = make_sock(&nsi->nsip.fd, IPPROTO_UDP, nsi->nsip.local_ip, - nsi->nsip.local_port, nsip_fd_cb); + nsi->nsip.local_port, 0, nsip_fd_cb, NULL); if (ret < 0) return ret;
diff --git a/openbsc/src/libgb/gprs_ns_frgre.c b/openbsc/src/libgb/gprs_ns_frgre.c index 98b1ad6..85019e1 100644 --- a/openbsc/src/libgb/gprs_ns_frgre.c +++ b/openbsc/src/libgb/gprs_ns_frgre.c @@ -292,7 +292,7 @@ int gprs_ns_frgre_listen(struct gprs_ns_inst *nsi) return 0;
rc = make_sock(&nsi->frgre.fd, IPPROTO_GRE, nsi->frgre.local_ip, - 0, nsfrgre_fd_cb); + 0, 0, nsfrgre_fd_cb, NULL); if (rc < 0) { LOGP(DNS, LOGL_ERROR, "Error creating GRE socket (%s)\n", strerror(errno)); diff --git a/openbsc/src/osmo-bsc_nat/bsc_ussd.c b/openbsc/src/osmo-bsc_nat/bsc_ussd.c index 0dd0d87..50c50ed 100644 --- a/openbsc/src/osmo-bsc_nat/bsc_ussd.c +++ b/openbsc/src/osmo-bsc_nat/bsc_ussd.c @@ -251,7 +251,7 @@ int bsc_ussd_init(struct bsc_nat *nat)
nat->ussd_listen.data = nat; return make_sock(&nat->ussd_listen, IPPROTO_TCP, - ntohl(addr.s_addr), 5001, ussd_listen_cb); + ntohl(addr.s_addr), 5001, 0, ussd_listen_cb, NULL); }
static int forward_ussd(struct sccp_connections *con, const struct ussd_request *req,
From: Pablo Neira Ayuso pablo@gnumonks.org
With this patch, we use the implementation available in libcommon. --- openbsc/src/ipaccess/ipaccess-proxy.c | 42 ++------------------------------ 1 files changed, 3 insertions(+), 39 deletions(-)
diff --git a/openbsc/src/ipaccess/ipaccess-proxy.c b/openbsc/src/ipaccess/ipaccess-proxy.c index d98eb54..a6b433f 100644 --- a/openbsc/src/ipaccess/ipaccess-proxy.c +++ b/openbsc/src/ipaccess/ipaccess-proxy.c @@ -44,6 +44,7 @@ #include <osmocom/core/msgb.h> #include <openbsc/debug.h> #include <openbsc/ipaccess.h> +#include <openbsc/socket.h> #include <osmocom/core/talloc.h>
static struct log_target *stderr_target; @@ -288,43 +289,6 @@ static void _logp_ipbc_uid(unsigned int ss, unsigned int lvl, char *file, int li logp2(ss, lvl, file, line, 0, "unknown "); }
-/* UDP socket handling */ - -static int make_sock(struct bsc_fd *bfd, u_int16_t port, int proto, int priv_nr, - int (*cb)(struct bsc_fd *fd, unsigned int what), - void *data) -{ - struct sockaddr_in addr; - int ret, on = 1; - - bfd->fd = socket(AF_INET, SOCK_DGRAM, proto); - bfd->cb = cb; - bfd->when = BSC_FD_READ; - bfd->data = data; - bfd->priv_nr = priv_nr; - - memset(&addr, 0, sizeof(addr)); - addr.sin_family = AF_INET; - addr.sin_port = htons(port); - 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(DINP, LOGL_ERROR, "could not bind socket: %s\n", - strerror(errno)); - return -EIO; - } - - ret = bsc_register_fd(bfd); - if (ret < 0) { - perror("register UDP fd"); - return ret; - } - return 0; -} - static int handle_udp_read(struct bsc_fd *bfd) { struct ipa_bts_conn *ipbc = bfd->data; @@ -494,7 +458,7 @@ static int ipbc_alloc_connect(struct ipa_proxy_conn *ipc, struct bsc_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, udp_port, IPPROTO_UDP, + ret = make_sock(&ipbc->udp_bts_fd, IPPROTO_UDP, INADDR_ANY, udp_port, UDP_TO_BTS, udp_fd_cb, ipbc); if (ret < 0) goto err_udp_bts; @@ -503,7 +467,7 @@ static int ipbc_alloc_connect(struct ipa_proxy_conn *ipc, struct bsc_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, udp_port, IPPROTO_UDP, + ret = make_sock(&ipbc->udp_bsc_fd, IPPROTO_UDP, INADDR_ANY, udp_port, UDP_TO_BSC, udp_fd_cb, ipbc); if (ret < 0) goto err_udp_bsc;
Hi Pablo,
On Tue, Apr 05, 2011 at 06:45:16PM +0200, pablo@gnumonks.org wrote:
This patchset is the first in the series to cleanup redundant code in ipaccess tools.
Thanks, I've merged your cleanups branch.