From 55fdfc223ea0584ec88fb392f729e205c021ace2 Mon Sep 17 00:00:00 2001 From: Eric Date: Fri, 13 Aug 2021 00:14:18 +0200 Subject: [PATCH] globally lock the portrange when trying to grab a port to prep for multithreading Change-Id: I78ae737b829bb428372f34db7d5bc601b5088b78 --- configure.ac | 3 +++ include/osmocom/mgcp/mgcp.h | 2 ++ src/libosmo-mgcp/mgcp_protocol.c | 6 +++++- 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/configure.ac b/configure.ac index 192c012ac..b996e87b0 100644 --- a/configure.ac +++ b/configure.ac @@ -57,6 +57,9 @@ PKG_CHECK_MODULES(LIBOSMONETIF, libosmo-netif >= 1.1.0) PKG_CHECK_MODULES(LIBOSMOABIS, libosmoabis >= 1.1.0) PKG_CHECK_MODULES(LIBOSMOTRAU, libosmotrau >= 1.1.0) +CFLAGS="$CFLAGS -pthread" +LDFLAGS="$LDFLAGS -pthread" + AC_ARG_ENABLE(sanitize, [AS_HELP_STRING( [--enable-sanitize], diff --git a/include/osmocom/mgcp/mgcp.h b/include/osmocom/mgcp/mgcp.h index b3f2eb5b0..228b0b3c5 100644 --- a/include/osmocom/mgcp/mgcp.h +++ b/include/osmocom/mgcp/mgcp.h @@ -34,6 +34,7 @@ #include #include #include +#include #include "mgcp_ratectr.h" @@ -92,6 +93,7 @@ typedef void (*mgcp_get_format)(struct mgcp_endpoint *endp, * This holds information on how to allocate ports */ struct mgcp_port_range { + pthread_mutex_t lock; /* addr or NULL to fall-back to default */ char *bind_addr_v4; char *bind_addr_v6; diff --git a/src/libosmo-mgcp/mgcp_protocol.c b/src/libosmo-mgcp/mgcp_protocol.c index ba80d7d79..16b7dab1a 100644 --- a/src/libosmo-mgcp/mgcp_protocol.c +++ b/src/libosmo-mgcp/mgcp_protocol.c @@ -28,6 +28,7 @@ #include #include #include +#include #include #include @@ -478,6 +479,7 @@ static int allocate_port(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn) range = &endp->cfg->net_ports; + pthread_mutex_lock(&range->lock); /* attempt to find a port */ tries = (range->range_end - range->range_start) / 2; for (i = 0; i < tries; ++i) { @@ -490,11 +492,12 @@ static int allocate_port(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn) range->last_port += 2; if (rc == 0) { + pthread_mutex_unlock(&range->lock); return 0; } } - + pthread_mutex_unlock(&range->lock); LOGPENDP(endp, DLMGCP, LOGL_ERROR, "Allocating a RTP/RTCP port failed %u times.\n", tries); @@ -1606,6 +1609,7 @@ struct mgcp_config *mgcp_config_alloc(void) osmo_strlcpy(cfg->domain, "mgw", sizeof(cfg->domain)); + cfg->net_ports.lock = (pthread_mutex_t)PTHREAD_MUTEX_INITIALIZER; cfg->net_ports.range_start = RTP_PORT_DEFAULT_RANGE_START; cfg->net_ports.range_end = RTP_PORT_DEFAULT_RANGE_END; cfg->net_ports.last_port = cfg->net_ports.range_start;