Initially implement the new osmo-mgw and libosmo-mgcp
Leave the old osmo-bsc_mgcp and libosmo-legacy-mgcp as it is; on a copy thereof
(added by a previous commit), apply changes to initially implement the new
osmo-mgw.
Adjust build system and debian packaging to accomodate the new libosmo-mgcp and
osmo-mgw.
The main differences:
*) use a list to manage rtp connections.
Aggregate all rtp related information inside a single struct.
Use a linked list to manage the both connections (net and bts).
The idea behind using a list is that we might support conference
calls at some later point.
Store the linked list in struct mgcp_endpoint, have a private linked
list for each endpoint. The list contains connection items which are
implemented in struct mgcp_conn. A connection is allocated and freed
using the functions in mgcp_conn.c. A connection is allocated on the
reception of a CRCX command and freed with the reception of a DLCX
command.
*) remove external transcoder feature
Fortunatelly the external transcoder feature is not needed
anymore. This patch removes the related code.
*) vty: get rid of CONN_BTS and CONN_NET
Since the new connection model does not make a difference
between BTS and NET connections the VTY should not use
the fixed CONN_BTS and CONN_NET constants.
- Handle the conns list inside the endpoint directly
- introduce function to dump basic rtp connection info
- introduce human readable names for connections
Parts of the code adjusted to use generalized connections instead of explicit
BTS/NET ones:
- teach mgcp_send_dummy() to send dummy packets to any RTP connection
- network: generalize mgcp_bind_net/bts_rtp_port()
- network: generalize mgcp_send()
- tap: generalize call tapping feature
- stat: generalize statistics
- Replace rtp_data_net() and rtp_data_bts() with generalized rtp_data_rx()
*) mgcp_protocol.c fixes:
- check ci string before it is converted:
In case of missing ci, a nullpointer is delivered to strtoul().
Add a function that takes ci, checks it and converts it to an
uint32_t. Use the return code to react on missing ci.
- output error message on missing CI.
- when parsing the mode, print log message when mode is missing.
- use mode_orig when mode is missing.
- fix ptime formatstring to use %u rather than %d.
- cosmetic: log when connection is deleted on DLCX.
- change loglevels of CRCX, MDCX, DLCX events from DEBUG to NOTICE.
*) mgcp_test
- apply rename of strline_r() to mgcp_strline().
- MGCP command macros:
- Add 'I: 1' parameters.
- Use proper port numbers:
from m=audio 0 RTP/AVP 126
to m=audio 16002 RTP/AVP 128
- Change ptime to 'a=ptime:40' because this is what the MGW currently
returns. CRCX generally feed a ptime:40 and this is expected to be
returned.
- struct mgcp_test: Use only one ptype, there are no explicit BTS and NET
endpoints anymore.
Hence remove one column from tests[].
- test_messages():
- Enable: remove '#if 0'
- Remove concept of BTS and NET endpoints: test only one conn, as they are
now interchangeable anyway.
- remove endpoint init, now done internally.
- add false asserts in error cases.
- test_retransmission():
- remove endpoint init, now done internally.
- add false asserts in error cases.
- test_packet_error_detection():
- Remove concept of BTS and NET endpoints: test only one conn, as they are
now interchangeable anyway. Use arbitrary conn ids (e.g. 4711).
- remove endpoint init, now done internally.
- add false assert in error case.
- Assert that a conn really vanishes on DLCX, previously the conn would
remain and just be unused, now it is actually discarded.
- test_no_cycle()
- Remove concept of BTS and NET endpoints: test only one conn, as they are
now interchangeable anyway. Use arbitrary conn ids (e.g. 4711).
- test_no_name()
- Enable: remove '#if 0'.
- remove endpoint init, now done internally.
- add false assert in error case.
- mgcp_test.ok: adjust expected results to status quo:
- We now see two dummy packets instead of one, now sent to both sides because
we don't know of BTS or NET side. (maybe drop dummy packets later...)
- packet duration, conn mode: now sane defaults show instead of unset.
- various whitespace and formatting changes from lindent.
Change-Id: Ie008599136c7ed8a0dfbb0cf803188975a499fc5
diff --git a/src/Makefile.am b/src/Makefile.am
index 922bbda..fab1bf0 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -23,9 +23,11 @@
SUBDIRS = \
libosmo-legacy-mgcp \
libosmo-mgcp-client \
+ libosmo-mgcp \
$(NULL)
# Programs
SUBDIRS += \
osmo-bsc_mgcp \
+ osmo-mgw \
$(NULL)
diff --git a/src/libosmo-mgcp/Makefile.am b/src/libosmo-mgcp/Makefile.am
index bcf6242..20959b8 100644
--- a/src/libosmo-mgcp/Makefile.am
+++ b/src/libosmo-mgcp/Makefile.am
@@ -10,7 +10,6 @@
$(LIBOSMOVTY_CFLAGS) \
$(LIBOSMONETIF_CFLAGS) \
$(COVERAGE_CFLAGS) \
- $(LIBBCG729_CFLAGS) \
$(NULL)
AM_LDFLAGS = \
@@ -18,33 +17,31 @@
$(LIBOSMOGSM_LIBS) \
$(LIBOSMONETIF_LIBS) \
$(COVERAGE_LDFLAGS) \
- $(LIBBCG729_LIBS) \
$(NULL)
# This is not at all related to the release version, but a range of supported
# API versions. Read TODO_RELEASE in the source tree's root!
-LEGACY_MGCP_LIBVERSION=0:0:0
+MGCP_LIBVERSION=0:0:0
lib_LTLIBRARIES = \
- libosmo-legacy-mgcp.la \
+ libosmo-mgcp.la \
$(NULL)
noinst_HEADERS = \
g711common.h \
$(NULL)
-libosmo_legacy_mgcp_la_SOURCES = \
+libosmo_mgcp_la_SOURCES = \
mgcp_common.c \
mgcp_protocol.c \
mgcp_network.c \
mgcp_vty.c \
mgcp_osmux.c \
mgcp_sdp.c \
+ mgcp_msg.c \
+ mgcp_conn.c \
+ mgcp_stat.c \
+ mgcp_ep.c \
$(NULL)
-if BUILD_MGCP_TRANSCODING
-libosmo_legacy_mgcp_la_SOURCES += \
- mgcp_transcode.c \
- $(NULL)
-endif
-libosmo_legacy_mgcp_la_LDFLAGS = $(AM_LDFLAGS) -version-info $(LEGACY_MGCP_LIBVERSION)
+libosmo_mgcp_la_LDFLAGS = $(AM_LDFLAGS) -version-info $(MGCP_LIBVERSION)
diff --git a/src/libosmo-mgcp/mgcp_common.c b/src/libosmo-mgcp/mgcp_common.c
index 444a060..c246742 100644
--- a/src/libosmo-mgcp/mgcp_common.c
+++ b/src/libosmo-mgcp/mgcp_common.c
@@ -23,7 +23,7 @@
#include <errno.h>
#include <osmocom/core/utils.h>
-#include <osmocom/legacy_mgcp/mgcp.h>
+#include <osmocom/mgcp/mgcp.h>
const struct value_string mgcp_connection_mode_strs[] = {
{ MGCP_CONN_NONE, "none" },
diff --git a/src/libosmo-mgcp/mgcp_conn.c b/src/libosmo-mgcp/mgcp_conn.c
new file mode 100644
index 0000000..1a295e7
--- /dev/null
+++ b/src/libosmo-mgcp/mgcp_conn.c
@@ -0,0 +1,286 @@
+/* Message connection list handling */
+
+/*
+ * (C) 2017 by sysmocom s.f.m.c. GmbH <info@sysmocom.de>
+ * All Rights Reserved
+ *
+ * Author: Philipp Maier
+ *
+ * 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 <osmocom/mgcp/mgcp_conn.h>
+#include <osmocom/mgcp/mgcp_internal.h>
+#include <osmocom/mgcp/mgcp_ep.h>
+
+/* Reset codec state and free memory */
+static void mgcp_rtp_codec_reset(struct mgcp_rtp_codec *codec)
+{
+ codec->payload_type = -1;
+ codec->subtype_name = NULL;
+ codec->audio_name = NULL;
+ codec->frame_duration_num = DEFAULT_RTP_AUDIO_FRAME_DUR_NUM;
+ codec->frame_duration_den = DEFAULT_RTP_AUDIO_FRAME_DUR_DEN;
+ codec->rate = DEFAULT_RTP_AUDIO_DEFAULT_RATE;
+ codec->channels = DEFAULT_RTP_AUDIO_DEFAULT_CHANNELS;
+
+ /* see also mgcp_sdp.c, mgcp_set_audio_info() */
+ talloc_free(codec->subtype_name);
+ talloc_free(codec->audio_name);
+}
+
+/* Reset states, free memory, set defaults and reset codec state */
+static void mgcp_rtp_conn_reset(struct mgcp_conn_rtp *conn)
+{
+ struct mgcp_rtp_end *end = &conn->end;
+
+ conn->type = MGCP_RTP_DEFAULT;
+ conn->osmux.allocated_cid = -1;
+
+ end->rtp.fd = -1;
+ end->rtcp.fd = -1;
+ end->local_port = 0;
+ end->packets_rx = 0;
+ end->octets_rx = 0;
+ end->packets_tx = 0;
+ end->octets_tx = 0;
+ end->dropped_packets = 0;
+ end->rtp_port = end->rtcp_port = 0;
+ talloc_free(end->fmtp_extra);
+ end->fmtp_extra = NULL;
+
+ /* Set default values */
+ end->frames_per_packet = 0; /* unknown */
+ end->packet_duration_ms = DEFAULT_RTP_AUDIO_PACKET_DURATION_MS;
+ end->output_enabled = 0;
+
+ mgcp_rtp_codec_reset(&end->codec);
+ mgcp_rtp_codec_reset(&end->alt_codec);
+}
+
+/*! allocate a new connection list entry.
+ * \param[in] ctx talloc context
+ * \param[in] endp associated endpoint
+ * \param[in] id identification number of the connection
+ * \param[in] type connection type (e.g. MGCP_CONN_TYPE_RTP)
+ * \returns pointer to allocated connection, NULL on error */
+struct mgcp_conn *mgcp_conn_alloc(void *ctx, struct mgcp_endpoint *endp,
+ uint32_t id, enum mgcp_conn_type type,
+ char *name)
+{
+ struct mgcp_conn *conn;
+ OSMO_ASSERT(endp);
+ OSMO_ASSERT(endp->conns.next != NULL && endp->conns.prev != NULL);
+ OSMO_ASSERT(strlen(name) < sizeof(conn->name));
+
+ /* Do not allow more then two connections */
+ if (llist_count(&endp->conns) >= endp->type->max_conns)
+ return NULL;
+
+ /* Prevent duplicate connection IDs */
+ if (mgcp_conn_get(endp, id))
+ return NULL;
+
+ /* Create new connection and add it to the list */
+ conn = talloc_zero(ctx, struct mgcp_conn);
+ if (!conn)
+ return NULL;
+ conn->endp = endp;
+ conn->type = type;
+ conn->mode = MGCP_CONN_NONE;
+ conn->mode_orig = MGCP_CONN_NONE;
+ conn->id = id;
+ conn->u.rtp.conn = conn;
+ strcpy(conn->name, name);
+
+ switch (type) {
+ case MGCP_CONN_TYPE_RTP:
+ mgcp_rtp_conn_reset(&conn->u.rtp);
+ break;
+ default:
+ /* NOTE: This should never be called with an
+ * invalid type, its up to the programmer
+ * to ensure propery types */
+ OSMO_ASSERT(false);
+ }
+
+ llist_add(&conn->entry, &endp->conns);
+
+ return conn;
+}
+
+/*! find a connection by its ID.
+ * \param[in] endp associated endpoint
+ * \param[in] id identification number of the connection
+ * \returns pointer to allocated connection, NULL if not found */
+struct mgcp_conn *mgcp_conn_get(struct mgcp_endpoint *endp, uint32_t id)
+{
+ OSMO_ASSERT(endp);
+ OSMO_ASSERT(endp->conns.next != NULL && endp->conns.prev != NULL);
+
+ struct mgcp_conn *conn;
+
+ llist_for_each_entry(conn, &endp->conns, entry) {
+ if (conn->id == id)
+ return conn;
+ }
+
+ return NULL;
+}
+
+/*! find an RTP connection by its ID.
+ * \param[in] endp associated endpoint
+ * \param[in] id identification number of the connection
+ * \returns pointer to allocated connection, NULL if not found */
+struct mgcp_conn_rtp *mgcp_conn_get_rtp(struct mgcp_endpoint *endp, uint32_t id)
+{
+ OSMO_ASSERT(endp);
+ OSMO_ASSERT(endp->conns.next != NULL && endp->conns.prev != NULL);
+
+ struct mgcp_conn *conn;
+
+ conn = mgcp_conn_get(endp, id);
+ if (!conn)
+ return NULL;
+
+ if (conn->type == MGCP_CONN_TYPE_RTP)
+ return &conn->u.rtp;
+
+ return NULL;
+}
+
+/*! free a connection by its ID.
+ * \param[in] endp associated endpoint
+ * \param[in] id identification number of the connection */
+void mgcp_conn_free(struct mgcp_endpoint *endp, uint32_t id)
+{
+ OSMO_ASSERT(endp);
+ OSMO_ASSERT(endp->conns.next != NULL && endp->conns.prev != NULL);
+
+ struct mgcp_conn *conn;
+
+ conn = mgcp_conn_get(endp, id);
+ if (!conn)
+ return;
+
+ switch (conn->type) {
+ case MGCP_CONN_TYPE_RTP:
+ osmux_disable_conn(&conn->u.rtp);
+ osmux_release_cid(&conn->u.rtp);
+ mgcp_free_rtp_port(&conn->u.rtp.end);
+ break;
+ default:
+ /* NOTE: This should never be called with an
+ * invalid type, its up to the programmer
+ * to ensure propery types */
+ OSMO_ASSERT(false);
+ }
+
+ llist_del(&conn->entry);
+ talloc_free(conn);
+}
+
+/*! free oldest connection in the list.
+ * \param[in] endp associated endpoint */
+void mgcp_conn_free_oldest(struct mgcp_endpoint *endp)
+{
+ OSMO_ASSERT(endp);
+ OSMO_ASSERT(endp->conns.next != NULL && endp->conns.prev != NULL);
+
+ struct mgcp_conn *conn;
+
+ if (llist_empty(&endp->conns))
+ return;
+
+ conn = llist_last_entry(&endp->conns, struct mgcp_conn, entry);
+ if (!conn)
+ return;
+
+ mgcp_conn_free(endp, conn->id);
+}
+
+/*! free all connections at once.
+ * \param[in] endp associated endpoint */
+void mgcp_conn_free_all(struct mgcp_endpoint *endp)
+{
+ OSMO_ASSERT(endp);
+ OSMO_ASSERT(endp->conns.next != NULL && endp->conns.prev != NULL);
+
+ struct mgcp_conn *conn;
+ struct mgcp_conn *conn_tmp;
+
+ /* Drop all items in the list */
+ llist_for_each_entry_safe(conn, conn_tmp, &endp->conns, entry) {
+ mgcp_conn_free(endp, conn->id);
+ }
+
+ return;
+}
+
+/*! dump basic connection information to human readble string.
+ * \param[in] conn to dump
+ * \returns human readble string */
+char *mgcp_conn_dump(struct mgcp_conn *conn)
+{
+ static char str[256];
+
+ if (!conn) {
+ snprintf(str, sizeof(str), "(null connection)");
+ return str;
+ }
+
+ switch (conn->type) {
+ case MGCP_CONN_TYPE_RTP:
+ /* Dump RTP connection */
+ snprintf(str, sizeof(str), "(%s/rtp, id:%u, ip:%s, "
+ "rtp:%u rtcp:%u)",
+ conn->name,
+ conn->id,
+ inet_ntoa(conn->u.rtp.end.addr),
+ ntohs(conn->u.rtp.end.rtp_port),
+ ntohs(conn->u.rtp.end.rtcp_port));
+ break;
+
+ default:
+ /* Should not happen, we should be able to dump
+ * every possible connection type. */
+ snprintf(str, sizeof(str), "(unknown connection type)");
+ break;
+ }
+
+ return str;
+}
+
+/*! find destination connection on a specific endpoint.
+ * \param[in] conn to search a destination for
+ * \returns destination connection, NULL on failure */
+struct mgcp_conn *mgcp_find_dst_conn(struct mgcp_conn *conn)
+{
+ struct mgcp_endpoint *endp;
+ struct mgcp_conn *partner_conn;
+ endp = conn->endp;
+
+ /*! NOTE: This simply works by grabbing the first connection that is
+ * not the supplied connection, which is suitable for endpoints that
+ * do not serve more than two connections. */
+
+ llist_for_each_entry(partner_conn, &endp->conns, entry) {
+ if (conn != partner_conn) {
+ return partner_conn;
+ }
+ }
+
+ return NULL;
+}
diff --git a/src/libosmo-mgcp/mgcp_ep.c b/src/libosmo-mgcp/mgcp_ep.c
new file mode 100644
index 0000000..72ca691
--- /dev/null
+++ b/src/libosmo-mgcp/mgcp_ep.c
@@ -0,0 +1,32 @@
+/* Endpoint types */
+
+/*
+ * (C) 2017 by sysmocom s.f.m.c. GmbH <info@sysmocom.de>
+ * All Rights Reserved
+ *
+ * Author: Philipp Maier
+ *
+ * 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 <osmocom/mgcp/mgcp_ep.h>
+#include <osmocom/mgcp/mgcp_internal.h>
+
+/* Endpoint typeset definition */
+const struct mgcp_endpoint_typeset ep_typeset = {
+ /* Specify endpoint properties for RTP endpoint */
+ .rtp.max_conns = 2,
+ .rtp.dispatch_rtp_cb = mgcp_dispatch_rtp_bridge_cb
+};
diff --git a/src/libosmo-mgcp/mgcp_msg.c b/src/libosmo-mgcp/mgcp_msg.c
new file mode 100644
index 0000000..f4ecb03
--- /dev/null
+++ b/src/libosmo-mgcp/mgcp_msg.c
@@ -0,0 +1,404 @@
+/* A Media Gateway Control Protocol Media Gateway: RFC 3435 */
+/* Message parser/generator utilities */
+
+/*
+ * (C) 2009-2012 by Holger Hans Peter Freyther <zecke@selfish.org>
+ * (C) 2009-2012 by On-Waves
+ * (C) 2017 by sysmocom s.f.m.c. GmbH <info@sysmocom.de>
+ * 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 <limits.h>
+
+#include <osmocom/mgcp/mgcp_internal.h>
+#include <osmocom/mgcp/mgcp_msg.h>
+#include <osmocom/mgcp/mgcp_conn.h>
+
+/*! Display an mgcp message on the log output.
+ * \param[in] message mgcp message string
+ * \param[in] len message mgcp message string length
+ * \param[in] preamble string to display in logtext in front of each line */
+void mgcp_disp_msg(unsigned char *message, unsigned int len, char *preamble)
+{
+ unsigned char line[80];
+ unsigned char *ptr;
+ unsigned int consumed = 0;
+ unsigned int consumed_line = 0;
+ unsigned int line_count = 0;
+
+ if (!log_check_level(DLMGCP, LOGL_DEBUG))
+ return;
+
+ while (1) {
+ memset(line, 0, sizeof(line));
+ ptr = line;
+ consumed_line = 0;
+ do {
+ if (*message != '\n' && *message != '\r') {
+ *ptr = *message;
+ ptr++;
+ }
+ message++;
+ consumed++;
+ consumed_line++;
+ } while (*message != '\n' && consumed < len
+ && consumed_line < sizeof(line));
+
+ if (strlen((const char *)line)) {
+ LOGP(DLMGCP, LOGL_DEBUG, "%s: line #%02u: %s\n",
+ preamble, line_count, line);
+ line_count++;
+ }
+
+ if (consumed >= len)
+ return;
+ }
+}
+
+/*! Parse connection mode.
+ * \param[in] mode as string (recvonly, sendrecv, sendonly or loopback)
+ * \param[in] endp pointer to endpoint (only used for log output)
+ * \param[out] associated connection to be modified accordingly
+ * \returns 0 on success, -1 on error */
+int mgcp_parse_conn_mode(const char *mode, struct mgcp_endpoint *endp,
+ struct mgcp_conn *conn)
+{
+ int ret = 0;
+
+ if (!mode) {
+ LOGP(DLMGCP, LOGL_ERROR,
+ "endpoint:%x missing connection mode\n",
+ ENDPOINT_NUMBER(endp));
+ return -1;
+ }
+ if (!conn)
+ return -1;
+ if (!endp)
+ return -1;
+
+ if (strcmp(mode, "recvonly") == 0)
+ conn->mode = MGCP_CONN_RECV_ONLY;
+ else if (strcmp(mode, "sendrecv") == 0)
+ conn->mode = MGCP_CONN_RECV_SEND;
+ else if (strcmp(mode, "sendonly") == 0)
+ conn->mode = MGCP_CONN_SEND_ONLY;
+ else if (strcmp(mode, "loopback") == 0)
+ conn->mode = MGCP_CONN_LOOPBACK;
+ else {
+ LOGP(DLMGCP, LOGL_ERROR,
+ "endpoint:%x unknown connection mode: '%s'\n",
+ ENDPOINT_NUMBER(endp), mode);
+ ret = -1;
+ }
+
+ /* Special handling für RTP connections */
+ if (conn->type == MGCP_CONN_TYPE_RTP) {
+ conn->u.rtp.end.output_enabled =
+ conn->mode & MGCP_CONN_SEND_ONLY ? 1 : 0;
+ }
+
+ LOGP(DLMGCP, LOGL_DEBUG,
+ "endpoint:%x conn:%s\n",
+ ENDPOINT_NUMBER(endp), mgcp_conn_dump(conn));
+
+ LOGP(DLMGCP, LOGL_DEBUG,
+ "endpoint:%x connection mode '%s' %d\n",
+ ENDPOINT_NUMBER(endp), mode, conn->mode);
+
+ /* Special handling für RTP connections */
+ if (conn->type == MGCP_CONN_TYPE_RTP) {
+ LOGP(DLMGCP, LOGL_DEBUG, "endpoint:%x output_enabled %d\n",
+ ENDPOINT_NUMBER(endp), conn->u.rtp.end.output_enabled);
+ }
+
+ /* The VTY might change the connection mode at any time, so we have
+ * to hold a copy of the original connection mode */
+ conn->mode_orig = conn->mode;
+
+ return ret;
+}
+
+/* We have a null terminated string with the endpoint name here. We only
+ * support two kinds. Simple ones as seen on the BSC level and the ones
+ * seen on the trunk side. (helper function for find_endpoint()) */
+static struct mgcp_endpoint *find_e1_endpoint(struct mgcp_config *cfg,
+ const char *mgcp)
+{
+ char *rest = NULL;
+ struct mgcp_trunk_config *tcfg;
+ int trunk, endp;
+
+ trunk = strtoul(mgcp + 6, &rest, 10);
+ if (rest == NULL || rest[0] != '/' || trunk < 1) {
+ LOGP(DLMGCP, LOGL_ERROR, "Wrong trunk name '%s'\n", mgcp);
+ return NULL;
+ }
+
+ endp = strtoul(rest + 1, &rest, 10);
+ if (rest == NULL || rest[0] != '@') {
+ LOGP(DLMGCP, LOGL_ERROR, "Wrong endpoint name '%s'\n", mgcp);
+ return NULL;
+ }
+
+ /* signalling is on timeslot 1 */
+ if (endp == 1)
+ return NULL;
+
+ tcfg = mgcp_trunk_num(cfg, trunk);
+ if (!tcfg) {
+ LOGP(DLMGCP, LOGL_ERROR, "The trunk %d is not declared.\n",
+ trunk);
+ return NULL;
+ }
+
+ if (!tcfg->endpoints) {
+ LOGP(DLMGCP, LOGL_ERROR,
+ "Endpoints of trunk %d not allocated.\n", trunk);
+ return NULL;
+ }
+
+ if (endp < 1 || endp >= tcfg->number_endpoints) {
+ LOGP(DLMGCP, LOGL_ERROR, "Failed to find endpoint '%s'\n",
+ mgcp);
+ return NULL;
+ }
+
+ return &tcfg->endpoints[endp];
+}
+
+/* Search the endpoint pool for the endpoint that had been selected via the
+ * MGCP message (helper function for mgcp_analyze_header()) */
+static struct mgcp_endpoint *find_endpoint(struct mgcp_config *cfg,
+ const char *mgcp)
+{
+ char *endptr = NULL;
+ unsigned int gw = INT_MAX;
+
+ if (strncmp(mgcp, "ds/e1", 5) == 0)
+ return find_e1_endpoint(cfg, mgcp);
+
+ gw = strtoul(mgcp, &endptr, 16);
+ if (gw > 0 && gw < cfg->trunk.number_endpoints && endptr[0] == '@')
+ return &cfg->trunk.endpoints[gw];
+
+ LOGP(DLMGCP, LOGL_ERROR, "Not able to find the endpoint: '%s'\n", mgcp);
+ return NULL;
+}
+
+/*! Analyze and parse the the hader of an MGCP messeage string.
+ * \param[out] pdata caller provided memory to store the parsing results
+ * \param[in] data mgcp message string
+ * \returns when the status line was complete and transaction_id and
+ * endp out parameters are set, -1 on error */
+int mgcp_parse_header(struct mgcp_parse_data *pdata, char *data)
+{
+ int i = 0;
+ char *elem, *save = NULL;
+
+ /*! This function will parse the header part of the received
+ * MGCP message. The parsing results are stored in pdata.
+ * The function will also automatically search the pool with
+ * available endpoints in order to find an endpoint that matches
+ * the endpoint string in in the header */
+
+ OSMO_ASSERT(data);
+ pdata->trans = "000000";
+
+ for (elem = strtok_r(data, " ", &save); elem;
+ elem = strtok_r(NULL, " ", &save)) {
+ switch (i) {
+ case 0:
+ pdata->trans = elem;
+ break;
+ case 1:
+ pdata->endp = find_endpoint(pdata->cfg, elem);
+ if (!pdata->endp) {
+ LOGP(DLMGCP, LOGL_ERROR,
+ "Unable to find Endpoint `%s'\n", elem);
+ return -1;
+ }
+ break;
+ case 2:
+ if (strcmp("MGCP", elem)) {
+ LOGP(DLMGCP, LOGL_ERROR,
+ "MGCP header parsing error\n");
+ return -1;
+ }
+ break;
+ case 3:
+ if (strcmp("1.0", elem)) {
+ LOGP(DLMGCP, LOGL_ERROR, "MGCP version `%s' "
+ "not supported\n", elem);
+ return -1;
+ }
+ break;
+ }
+ i++;
+ }
+
+ if (i != 4) {
+ LOGP(DLMGCP, LOGL_ERROR, "MGCP status line too short.\n");
+ pdata->trans = "000000";
+ pdata->endp = NULL;
+ return -1;
+ }
+
+ return 0;
+}
+
+/*! Extract OSMUX CID from an MGCP parameter line (string).
+ * \param[in] line single parameter line from the MGCP message
+ * \returns OSMUX CID, -1 on error */
+int mgcp_parse_osmux_cid(const char *line)
+{
+ int osmux_cid;
+
+ if (sscanf(line + 2, "Osmux: %u", &osmux_cid) != 1)
+ return -1;
+
+ if (osmux_cid > OSMUX_CID_MAX) {
+ LOGP(DLMGCP, LOGL_ERROR, "Osmux ID too large: %u > %u\n",
+ osmux_cid, OSMUX_CID_MAX);
+ return -1;
+ }
+ LOGP(DLMGCP, LOGL_DEBUG, "bsc-nat offered Osmux CID %u\n", osmux_cid);
+
+ return osmux_cid;
+}
+
+/*! Check MGCP parameter line (string) for plausibility.
+ * \param[in] endp pointer to endpoint (only used for log output)
+ * \param[in] line single parameter line from the MGCP message
+ * \returns 1 when line seems plausible, 0 on error */
+int mgcp_check_param(const struct mgcp_endpoint *endp, const char *line)
+{
+ const size_t line_len = strlen(line);
+ if (line[0] != '\0' && line_len < 2) {
+ LOGP(DLMGCP, LOGL_ERROR,
+ "Wrong MGCP option format: '%s' on 0x%x\n",
+ line, ENDPOINT_NUMBER(endp));
+ return 0;
+ }
+
+ /* FIXME: A couple more checks wouldn't hurt... */
+
+ return 1;
+}
+
+/*! Check if the specified callid seems plausible.
+ * \param[in] endp pointer to endpoint
+ * \param{in] callid to verify
+ * \returns 1 when callid seems plausible, 0 on error */
+int mgcp_verify_call_id(struct mgcp_endpoint *endp, const char *callid)
+{
+ /*! This function compares the supplied callid with the called that is
+ * stored in the endpoint structure. */
+
+ if (!endp)
+ return -1;
+ if (!callid)
+ return -1;
+ if (!endp->callid)
+ return -1;
+
+ if (strcmp(endp->callid, callid) != 0) {
+ LOGP(DLMGCP, LOGL_ERROR,
+ "endpoint:%x CallIDs does not match '%s' != '%s'\n",
+ ENDPOINT_NUMBER(endp), endp->callid, callid);
+ return -1;
+ }
+
+ return 0;
+}
+
+/*! Check if the specified connection id seems plausible.
+ * \param[in] endp pointer to endpoint
+ * \param{in] connection id to verify
+ * \returns 1 when connection id seems plausible, 0 on error */
+int mgcp_verify_ci(struct mgcp_endpoint *endp, const char *ci)
+{
+ uint32_t id;
+
+ if (!endp)
+ return -1;
+
+ id = strtoul(ci, NULL, 10);
+
+ if (mgcp_conn_get(endp, id))
+ return 0;
+
+ LOGP(DLMGCP, LOGL_ERROR,
+ "endpoint:%x No connection found under ConnectionIdentifier %u\n",
+ ENDPOINT_NUMBER(endp), id);
+
+ return -1;
+}
+
+/*! Extract individual lines from MCGP message.
+ * \param[in] str MGCP message string, consisting of multiple lines
+ * \param{in] saveptr pointer to next line in str
+ * \returns line, NULL when done */
+char *mgcp_strline(char *str, char **saveptr)
+{
+ char *result;
+
+ /*! The function must be called with *str set to the input string
+ * for the first line. After that saveptr will be initalized.
+ * all consecutive lines are extracted by calling the function
+ * with str set to NULL. When done, the function will return NULL
+ * to indicate that all lines have been parsed. */
+
+ if (str)
+ *saveptr = str;
+
+ result = *saveptr;
+
+ if (*saveptr != NULL) {
+ *saveptr = strpbrk(*saveptr, "\r\n");
+
+ if (*saveptr != NULL) {
+ char *eos = *saveptr;
+
+ if ((*saveptr)[0] == '\r' && (*saveptr)[1] == '\n')
+ (*saveptr)++;
+ (*saveptr)++;
+ if ((*saveptr)[0] == '\0')
+ *saveptr = NULL;
+
+ *eos = '\0';
+ }
+ }
+
+ return result;
+}
+
+/*! Parse CI from a given string.
+ * \param[out] caller provided memory to store the result
+ * \param{in] string containing the connection id
+ * \returns 0 on success, -1 on error */
+int mgcp_parse_ci(uint32_t *conn_id, const char *ci)
+{
+
+ OSMO_ASSERT(conn_id);
+
+ if (!ci)
+ return -1;
+
+ *conn_id = strtoul(ci, NULL, 10);
+
+ return 0;
+}
diff --git a/src/libosmo-mgcp/mgcp_network.c b/src/libosmo-mgcp/mgcp_network.c
index 8ccfb42..4a1eb74 100644
--- a/src/libosmo-mgcp/mgcp_network.c
+++ b/src/libosmo-mgcp/mgcp_network.c
@@ -27,22 +27,18 @@
#include <errno.h>
#include <time.h>
#include <limits.h>
-
#include <sys/socket.h>
#include <arpa/inet.h>
#include <osmocom/core/msgb.h>
#include <osmocom/core/select.h>
-
#include <osmocom/netif/rtp.h>
-
-#include <osmocom/legacy_mgcp/mgcp.h>
-#include <osmocom/legacy_mgcp/mgcp_internal.h>
-
-#include <osmocom/legacy_mgcp/osmux.h>
-
-#warning "Make use of the rtp proxy code"
-
+#include <osmocom/mgcp/mgcp.h>
+#include <osmocom/mgcp/mgcp_internal.h>
+#include <osmocom/mgcp/mgcp_stat.h>
+#include <osmocom/mgcp/osmux.h>
+#include <osmocom/mgcp/mgcp_conn.h>
+#include <osmocom/mgcp/mgcp_ep.h>
#define RTP_SEQ_MOD (1 << 16)
#define RTP_MAX_DROPOUT 3000
@@ -54,35 +50,44 @@
MGCP_PROTO_RTCP,
};
-/**
- * This does not need to be a precision timestamp and
+/* This does not need to be a precision timestamp and
* is allowed to wrap quite fast. The returned value is
- * 1/unit seconds.
- */
-static uint32_t get_current_ts(unsigned unit)
+ * 1/codec_rate seconds. */
+static uint32_t get_current_ts(unsigned codec_rate)
{
struct timespec tp;
uint64_t ret;
- if (!unit)
+ if (!codec_rate)
return 0;
memset(&tp, 0, sizeof(tp));
if (clock_gettime(CLOCK_MONOTONIC, &tp) != 0)
- LOGP(DLMGCP, LOGL_NOTICE,
- "Getting the clock failed.\n");
+ LOGP(DLMGCP, LOGL_NOTICE, "Getting the clock failed.\n");
/* convert it to 1/unit seconds */
ret = tp.tv_sec;
- ret *= unit;
- ret += (int64_t)tp.tv_nsec * unit / 1000 / 1000 / 1000;
+ ret *= codec_rate;
+ ret += (int64_t) tp.tv_nsec * codec_rate / 1000 / 1000 / 1000;
return ret;
}
+/*! send udp packet.
+ * \param[in] fd associated file descriptor
+ * \param[in] addr destination ip-address
+ * \param[in] port destination UDP port
+ * \param[in] buf buffer that holds the data to be send
+ * \param[in] len length of the data to be sent
+ * \returns bytes sent, -1 on error */
int mgcp_udp_send(int fd, struct in_addr *addr, int port, char *buf, int len)
{
struct sockaddr_in out;
+
+ LOGP(DLMGCP, LOGL_DEBUG,
+ "sending %i bytes length packet to %s:%u ...\n",
+ len, inet_ntoa(*addr), ntohs(port));
+
out.sin_family = AF_INET;
out.sin_port = port;
memcpy(&out.sin_addr, addr, sizeof(*addr));
@@ -90,14 +95,26 @@
return sendto(fd, buf, len, 0, (struct sockaddr *)&out, sizeof(out));
}
-int mgcp_send_dummy(struct mgcp_endpoint *endp)
+/*! send RTP dummy packet (to keep NAT connection open).
+ * \param[in] endp mcgp endpoint that holds the RTP connection
+ * \param[in] conn associated RTP connection
+ * \returns bytes sent, -1 on error */
+int mgcp_send_dummy(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn)
{
static char buf[] = { MGCP_DUMMY_LOAD };
int rc;
int was_rtcp = 0;
- rc = mgcp_udp_send(endp->net_end.rtp.fd, &endp->net_end.addr,
- endp->net_end.rtp_port, buf, 1);
+ OSMO_ASSERT(endp);
+ OSMO_ASSERT(conn);
+
+ LOGP(DLMGCP, LOGL_DEBUG,
+ "endpoint:%x sending dummy packet...\n", ENDPOINT_NUMBER(endp));
+ LOGP(DLMGCP, LOGL_DEBUG, "endpoint:%x conn:%s\n",
+ ENDPOINT_NUMBER(endp), mgcp_conn_dump(conn->conn));
+
+ rc = mgcp_udp_send(conn->end.rtp.fd, &conn->end.addr,
+ conn->end.rtp_port, buf, 1);
if (rc == -1)
goto failed;
@@ -106,24 +123,23 @@
return rc;
was_rtcp = 1;
- rc = mgcp_udp_send(endp->net_end.rtcp.fd, &endp->net_end.addr,
- endp->net_end.rtcp_port, buf, 1);
+ rc = mgcp_udp_send(conn->end.rtcp.fd, &conn->end.addr,
+ conn->end.rtcp_port, buf, 1);
if (rc >= 0)
return rc;
failed:
LOGP(DLMGCP, LOGL_ERROR,
- "Failed to send dummy %s packet: %s on: 0x%x to %s:%d\n",
- was_rtcp ? "RTCP" : "RTP",
- strerror(errno), ENDPOINT_NUMBER(endp), inet_ntoa(endp->net_end.addr),
- was_rtcp ? endp->net_end.rtcp_port : endp->net_end.rtp_port);
+ "endpoint:%x Failed to send dummy %s packet.\n",
+ ENDPOINT_NUMBER(endp), was_rtcp ? "RTCP" : "RTP");
return -1;
}
-static int32_t compute_timestamp_aligment_error(struct mgcp_rtp_stream_state *sstate,
- int ptime, uint32_t timestamp)
+/* Compute timestamp alignment error */
+static int32_t ts_alignment_error(struct mgcp_rtp_stream_state *sstate,
+ int ptime, uint32_t timestamp)
{
int32_t timestamp_delta;
@@ -136,13 +152,14 @@
return timestamp_delta % ptime;
}
+/* Check timestamp and sequence number for plausibility */
static int check_rtp_timestamp(struct mgcp_endpoint *endp,
struct mgcp_rtp_state *state,
struct mgcp_rtp_stream_state *sstate,
struct mgcp_rtp_end *rtp_end,
struct sockaddr_in *addr,
uint16_t seq, uint32_t timestamp,
- const char *text, int32_t *tsdelta_out)
+ const char *text, int32_t * tsdelta_out)
{
int32_t tsdelta;
int32_t timestamp_error;
@@ -159,30 +176,28 @@
"number %d is the same, "
"TS offset: %d, SeqNo offset: %d "
"on 0x%x SSRC: %u timestamp: %u "
- "from %s:%d in %d\n",
+ "from %s:%d\n",
text, seq,
state->timestamp_offset, state->seq_offset,
ENDPOINT_NUMBER(endp), sstate->ssrc, timestamp,
- inet_ntoa(addr->sin_addr), ntohs(addr->sin_port),
- endp->conn_mode);
+ inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
}
return 0;
}
tsdelta =
- (int32_t)(timestamp - sstate->last_timestamp) /
- (int16_t)(seq - sstate->last_seq);
+ (int32_t)(timestamp - sstate->last_timestamp) /
+ (int16_t)(seq - sstate->last_seq);
if (tsdelta == 0) {
/* Don't update *tsdelta_out */
LOGP(DLMGCP, LOGL_NOTICE,
"The %s timestamp delta is %d "
"on 0x%x SSRC: %u timestamp: %u "
- "from %s:%d in %d\n",
+ "from %s:%d\n",
text, tsdelta,
ENDPOINT_NUMBER(endp), sstate->ssrc, timestamp,
- inet_ntoa(addr->sin_addr), ntohs(addr->sin_port),
- endp->conn_mode);
+ inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
return 0;
}
@@ -191,11 +206,10 @@
if (sstate->last_tsdelta) {
LOGP(DLMGCP, LOGL_INFO,
"The %s timestamp delta changes from %d to %d "
- "on 0x%x SSRC: %u timestamp: %u from %s:%d in %d\n",
+ "on 0x%x SSRC: %u timestamp: %u from %s:%d\n",
text, sstate->last_tsdelta, tsdelta,
ENDPOINT_NUMBER(endp), sstate->ssrc, timestamp,
- inet_ntoa(addr->sin_addr), ntohs(addr->sin_port),
- endp->conn_mode);
+ inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
}
}
@@ -203,8 +217,7 @@
*tsdelta_out = tsdelta;
timestamp_error =
- compute_timestamp_aligment_error(sstate, state->packet_duration,
- timestamp);
+ ts_alignment_error(sstate, state->packet_duration, timestamp);
if (timestamp_error) {
sstate->err_ts_counter += 1;
@@ -212,14 +225,14 @@
"The %s timestamp has an alignment error of %d "
"on 0x%x SSRC: %u "
"SeqNo delta: %d, TS delta: %d, dTS/dSeq: %d "
- "from %s:%d in mode %d. ptime: %d\n",
+ "from %s:%d. ptime: %d\n",
text, timestamp_error,
ENDPOINT_NUMBER(endp), sstate->ssrc,
(int16_t)(seq - sstate->last_seq),
(int32_t)(timestamp - sstate->last_timestamp),
tsdelta,
inet_ntoa(addr->sin_addr), ntohs(addr->sin_port),
- endp->conn_mode, state->packet_duration);
+ state->packet_duration);
}
return 1;
}
@@ -241,20 +254,18 @@
LOGP(DLMGCP, LOGL_NOTICE,
"A fixed packet duration is not available on 0x%x, "
"using last output timestamp delta instead: %d "
- "from %s:%d in %d\n",
+ "from %s:%d\n",
ENDPOINT_NUMBER(endp), tsdelta,
- inet_ntoa(addr->sin_addr), ntohs(addr->sin_port),
- endp->conn_mode);
+ inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
} else {
tsdelta = rtp_end->codec.rate * 20 / 1000;
LOGP(DLMGCP, LOGL_NOTICE,
"Fixed packet duration and last timestamp delta "
"are not available on 0x%x, "
"using fixed 20ms instead: %d "
- "from %s:%d in %d\n",
+ "from %s:%d\n",
ENDPOINT_NUMBER(endp), tsdelta,
- inet_ntoa(addr->sin_addr), ntohs(addr->sin_port),
- endp->conn_mode);
+ inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
}
}
@@ -267,11 +278,10 @@
LOGP(DLMGCP, LOGL_NOTICE,
"Timestamp offset change on 0x%x SSRC: %u "
"SeqNo delta: %d, TS offset: %d, "
- "from %s:%d in %d\n",
+ "from %s:%d\n",
ENDPOINT_NUMBER(endp), state->in_stream.ssrc,
delta_seq, state->timestamp_offset,
- inet_ntoa(addr->sin_addr), ntohs(addr->sin_port),
- endp->conn_mode);
+ inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
}
return timestamp_offset;
@@ -284,64 +294,89 @@
struct sockaddr_in *addr,
uint32_t timestamp)
{
- int timestamp_error = 0;
+ int ts_error = 0;
+ int ts_check = 0;
int ptime = state->packet_duration;
/* Align according to: T + Toffs - Tlast = k * Tptime */
- timestamp_error = compute_timestamp_aligment_error(
- &state->out_stream, ptime,
- timestamp + state->timestamp_offset);
+ ts_error = ts_alignment_error(&state->out_stream, ptime,
+ timestamp + state->timestamp_offset);
- if (timestamp_error) {
- state->timestamp_offset += ptime - timestamp_error;
+ /* If there is an alignment error, we have to compensate it */
+ if (ts_error) {
+ state->timestamp_offset += ptime - ts_error;
LOGP(DLMGCP, LOGL_NOTICE,
"Corrected timestamp alignment error of %d on 0x%x SSRC: %u "
"new TS offset: %d, "
- "from %s:%d in %d\n",
- timestamp_error,
+ "from %s:%d\n",
+ ts_error,
ENDPOINT_NUMBER(endp), state->in_stream.ssrc,
state->timestamp_offset, inet_ntoa(addr->sin_addr),
- ntohs(addr->sin_port), endp->conn_mode);
+ ntohs(addr->sin_port));
}
- OSMO_ASSERT(compute_timestamp_aligment_error(&state->out_stream, ptime,
- timestamp + state->timestamp_offset) == 0);
+ /* Check we really managed to compensate the timestamp
+ * offset. There should not be any remaining error, failing
+ * here would point to a serous problem with the alingnment
+ * error computation fuction */
+ ts_check = ts_alignment_error(&state->out_stream, ptime,
+ timestamp + state->timestamp_offset);
+ OSMO_ASSERT(ts_check == 0);
- return timestamp_error;
+ /* Return alignment error before compensation */
+ return ts_error;
}
-int mgcp_rtp_processing_default(struct mgcp_endpoint *endp, struct mgcp_rtp_end *dst_end,
+/*! dummy callback to disable transcoding (see also cfg->rtp_processing_cb).
+ * \param[in] associated endpoint
+ * \param[in] destination RTP end
+ * \param[in,out] pointer to buffer with voice data
+ * \param[in] voice data length
+ * \param[in] maxmimum size of caller provided voice data buffer
+ * \returns ignores input parameters, return always 0 */
+int mgcp_rtp_processing_default(struct mgcp_endpoint *endp,
+ struct mgcp_rtp_end *dst_end,
char *data, int *len, int buf_size)
{
+ LOGP(DLMGCP, LOGL_DEBUG, "endpoint:%x transcoding disabled\n",
+ ENDPOINT_NUMBER(endp));
return 0;
}
+/*! dummy callback to disable transcoding (see also cfg->setup_rtp_processing_cb).
+ * \param[in] associated endpoint
+ * \param[in] destination RTP end
+ * \param[in] source RTP end
+ * \returns ignores input parameters, return always 0 */
int mgcp_setup_rtp_processing_default(struct mgcp_endpoint *endp,
struct mgcp_rtp_end *dst_end,
struct mgcp_rtp_end *src_end)
{
+ LOGP(DLMGCP, LOGL_DEBUG, "endpoint:%x transcoding disabled\n",
+ ENDPOINT_NUMBER(endp));
return 0;
}
void mgcp_get_net_downlink_format_default(struct mgcp_endpoint *endp,
int *payload_type,
- const char**audio_name,
- const char**fmtp_extra)
+ const char **audio_name,
+ const char **fmtp_extra,
+ struct mgcp_conn_rtp *conn)
{
- /* Use the BTS side parameters when passing the SDP data (for
- * downlink) to the net peer.
- */
- *payload_type = endp->bts_end.codec.payload_type;
- *audio_name = endp->bts_end.codec.audio_name;
- *fmtp_extra = endp->bts_end.fmtp_extra;
+ LOGP(DLMGCP, LOGL_DEBUG,
+ "endpoint:%x conn:%s using format defaults\n",
+ ENDPOINT_NUMBER(endp), mgcp_conn_dump(conn->conn));
+
+ *payload_type = conn->end.codec.payload_type;
+ *audio_name = conn->end.codec.audio_name;
+ *fmtp_extra = conn->end.fmtp_extra;
}
-
-void mgcp_rtp_annex_count(struct mgcp_endpoint *endp, struct mgcp_rtp_state *state,
- const uint16_t seq, const int32_t transit,
- const uint32_t ssrc)
+void mgcp_rtp_annex_count(struct mgcp_endpoint *endp,
+ struct mgcp_rtp_state *state, const uint16_t seq,
+ const int32_t transit, const uint32_t ssrc)
{
int32_t d;
@@ -357,32 +392,28 @@
} else {
uint16_t udelta;
- /*
- * The below takes the shape of the validation of
+ /* The below takes the shape of the validation of
* Appendix A. Check if there is something weird with
* the sequence number, otherwise check for a wrap
* around in the sequence number.
* It can't wrap during the initialization so let's
* skip it here. The Appendix A probably doesn't have
- * this issue because of the probation.
- */
+ * this issue because of the probation. */
udelta = seq - state->stats_max_seq;
if (udelta < RTP_MAX_DROPOUT) {
if (seq < state->stats_max_seq)
state->stats_cycles += RTP_SEQ_MOD;
} else if (udelta <= RTP_SEQ_MOD - RTP_MAX_MISORDER) {
LOGP(DLMGCP, LOGL_NOTICE,
- "RTP seqno made a very large jump on 0x%x delta: %u\n",
- ENDPOINT_NUMBER(endp), udelta);
+ "RTP seqno made a very large jump on 0x%x delta: %u\n",
+ ENDPOINT_NUMBER(endp), udelta);
}
}
- /*
- * Calculate the jitter between the two packages. The TS should be
+ /* Calculate the jitter between the two packages. The TS should be
* taken closer to the read function. This was taken from the
* Appendix A of RFC 3550. Timestamp and arrival_time have a 1/rate
- * resolution.
- */
+ * resolution. */
d = transit - state->stats_transit;
state->stats_transit = transit;
if (d < 0)
@@ -391,19 +422,16 @@
state->stats_max_seq = seq;
}
-
-
-/**
- * The RFC 3550 Appendix A assumes there are multiple sources but
+/* The RFC 3550 Appendix A assumes there are multiple sources but
* some of the supported endpoints (e.g. the nanoBTS) can only handle
* one source and this code will patch RTP header to appear as if there
* is only one source.
* There is also no probation period for new sources. Every RTP header
- * we receive will be seen as a switch in streams.
- */
-void mgcp_patch_and_count(struct mgcp_endpoint *endp, struct mgcp_rtp_state *state,
- struct mgcp_rtp_end *rtp_end, struct sockaddr_in *addr,
- char *data, int len)
+ * we receive will be seen as a switch in streams. */
+void mgcp_patch_and_count(struct mgcp_endpoint *endp,
+ struct mgcp_rtp_state *state,
+ struct mgcp_rtp_end *rtp_end,
+ struct sockaddr_in *addr, char *data, int len)
{
uint32_t arrival_time;
int32_t transit;
@@ -415,7 +443,7 @@
if (len < sizeof(*rtp_hdr))
return;
- rtp_hdr = (struct rtp_hdr *) data;
+ rtp_hdr = (struct rtp_hdr *)data;
seq = ntohs(rtp_hdr->sequence);
timestamp = ntohl(rtp_hdr->timestamp);
arrival_time = get_current_ts(rtp_end->codec.rate);
@@ -429,34 +457,33 @@
state->in_stream.last_seq = seq - 1;
state->in_stream.ssrc = state->orig_ssrc = ssrc;
state->in_stream.last_tsdelta = 0;
- state->packet_duration = mgcp_rtp_packet_duration(endp, rtp_end);
+ state->packet_duration =
+ mgcp_rtp_packet_duration(endp, rtp_end);
state->out_stream = state->in_stream;
state->out_stream.last_timestamp = timestamp;
- state->out_stream.ssrc = ssrc - 1; /* force output SSRC change */
+ state->out_stream.ssrc = ssrc - 1; /* force output SSRC change */
LOGP(DLMGCP, LOGL_INFO,
- "Initializing stream on 0x%x SSRC: %u timestamp: %u "
- "pkt-duration: %d, from %s:%d in %d\n",
- ENDPOINT_NUMBER(endp), state->in_stream.ssrc,
- state->seq_offset, state->packet_duration,
- inet_ntoa(addr->sin_addr), ntohs(addr->sin_port),
- endp->conn_mode);
+ "endpoint:%x initializing stream, SSRC: %u timestamp: %u "
+ "pkt-duration: %d, from %s:%d\n",
+ ENDPOINT_NUMBER(endp), state->in_stream.ssrc,
+ state->seq_offset, state->packet_duration,
+ inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
if (state->packet_duration == 0) {
- state->packet_duration = rtp_end->codec.rate * 20 / 1000;
+ state->packet_duration =
+ rtp_end->codec.rate * 20 / 1000;
LOGP(DLMGCP, LOGL_NOTICE,
- "Fixed packet duration is not available on 0x%x, "
- "using fixed 20ms instead: %d from %s:%d in %d\n",
+ "endpoint:%x fixed packet duration is not available, "
+ "using fixed 20ms instead: %d from %s:%d\n",
ENDPOINT_NUMBER(endp), state->packet_duration,
- inet_ntoa(addr->sin_addr), ntohs(addr->sin_port),
- endp->conn_mode);
+ inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
}
} else if (state->in_stream.ssrc != ssrc) {
LOGP(DLMGCP, LOGL_NOTICE,
- "The SSRC changed on 0x%x: %u -> %u "
- "from %s:%d in %d\n",
- ENDPOINT_NUMBER(endp),
- state->in_stream.ssrc, rtp_hdr->ssrc,
- inet_ntoa(addr->sin_addr), ntohs(addr->sin_port),
- endp->conn_mode);
+ "endpoint:%x SSRC changed: %u -> %u "
+ "from %s:%d\n",
+ ENDPOINT_NUMBER(endp),
+ state->in_stream.ssrc, rtp_hdr->ssrc,
+ inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
state->in_stream.ssrc = ssrc;
if (rtp_end->force_constant_ssrc) {
@@ -464,13 +491,13 @@
/* Always increment seqno by 1 */
state->seq_offset =
- (state->out_stream.last_seq + 1) - seq;
+ (state->out_stream.last_seq + 1) - seq;
/* Estimate number of packets that would have been sent */
delta_seq =
- (arrival_time - state->in_stream.last_arrival_time
- + state->packet_duration/2) /
- state->packet_duration;
+ (arrival_time - state->in_stream.last_arrival_time
+ + state->packet_duration / 2) /
+ state->packet_duration;
adjust_rtp_timestamp_offset(endp, state, rtp_end, addr,
delta_seq, timestamp);
@@ -481,20 +508,19 @@
rtp_end->force_constant_ssrc -= 1;
LOGP(DLMGCP, LOGL_NOTICE,
- "SSRC patching enabled on 0x%x SSRC: %u "
+ "endpoint:%x SSRC patching enabled, SSRC: %u "
"SeqNo offset: %d, TS offset: %d "
- "from %s:%d in %d\n",
+ "from %s:%d\n",
ENDPOINT_NUMBER(endp), state->in_stream.ssrc,
state->seq_offset, state->timestamp_offset,
- inet_ntoa(addr->sin_addr), ntohs(addr->sin_port),
- endp->conn_mode);
+ inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
}
state->in_stream.last_tsdelta = 0;
} else {
/* Compute current per-packet timestamp delta */
- check_rtp_timestamp(endp, state, &state->in_stream, rtp_end, addr,
- seq, timestamp, "input",
+ check_rtp_timestamp(endp, state, &state->in_stream, rtp_end,
+ addr, seq, timestamp, "input",
&state->in_stream.last_tsdelta);
if (state->patch_ssrc)
@@ -509,7 +535,8 @@
if (rtp_end->force_aligned_timing &&
state->out_stream.ssrc == ssrc && state->packet_duration)
/* Align the timestamp offset */
- align_rtp_timestamp_offset(endp, state, rtp_end, addr, timestamp);
+ align_rtp_timestamp_offset(endp, state, rtp_end, addr,
+ timestamp);
/* Store the updated SSRC back to the packet */
if (state->patch_ssrc)
@@ -538,17 +565,17 @@
return;
#if 0
- DEBUGP(DLMGCP, "Payload hdr payload %u -> endp payload %u\n",
- rtp_hdr->payload_type, payload);
+ DEBUGP(DLMGCP,
+ "endpoint:%x payload hdr payload %u -> endp payload %u\n",
+ ENDPOINT_NUMBER(endp), rtp_hdr->payload_type, payload);
rtp_hdr->payload_type = payload;
#endif
}
-/*
- * The below code is for dispatching. We have a dedicated port for
- * the data coming from the net and one to discover the BTS.
- */
-static int forward_data(int fd, struct mgcp_rtp_tap *tap, const char *buf, int len)
+/* Forward data to a debug tap. This is debug function that is intended for
+ * debugging the voice traffic with tools like gstreamer */
+static int forward_data(int fd, struct mgcp_rtp_tap *tap, const char *buf,
+ int len)
{
if (!tap->enabled)
return 0;
@@ -557,433 +584,470 @@
(struct sockaddr *)&tap->forward, sizeof(tap->forward));
}
-static int mgcp_send_transcoder(struct mgcp_rtp_end *end,
- struct mgcp_config *cfg, int is_rtp,
- const char *buf, int len)
+/*! Send RTP/RTCP data to a specified destination connection.
+ * \param[in] endp associated endpoint (for configuration, logging)
+ * \param[in] is_rtp flag to specify if the packet is of type RTP or RTCP
+ * \param[in] spoofed source address (set to NULL to disable)
+ * \param[in] buf buffer that contains the RTP/RTCP data
+ * \param[in] len length of the buffer that contains the RTP/RTCP data
+ * \param[in] conn_src associated source connection
+ * \param[in] conn_dst associated destination connection
+ * \returns 0 on success, -1 on ERROR */
+int mgcp_send(struct mgcp_endpoint *endp, int is_rtp, struct sockaddr_in *addr,
+ char *buf, int len, struct mgcp_conn_rtp *conn_src,
+ struct mgcp_conn_rtp *conn_dst)
{
- int rc;
- int port;
- struct sockaddr_in addr;
+ /*! When no destination connection is available (e.g. when only one
+ * connection in loopback mode exists), then the source connection
+ * shall be specified as destination connection */
- port = is_rtp ? end->rtp_port : end->rtcp_port;
-
- addr.sin_family = AF_INET;
- addr.sin_addr = cfg->transcoder_in;
- addr.sin_port = port;
-
- rc = sendto(is_rtp ?
- end->rtp.fd :
- end->rtcp.fd, buf, len, 0,
- (struct sockaddr *) &addr, sizeof(addr));
-
- if (rc != len)
- LOGP(DLMGCP, LOGL_ERROR,
- "Failed to send data to the transcoder: %s\n",
- strerror(errno));
-
- return rc;
-}
-
-int mgcp_send(struct mgcp_endpoint *endp, int dest, int is_rtp,
- struct sockaddr_in *addr, char *buf, int rc)
-{
struct mgcp_trunk_config *tcfg = endp->tcfg;
struct mgcp_rtp_end *rtp_end;
struct mgcp_rtp_state *rtp_state;
- int tap_idx;
+ char *dest_name;
- LOGP(DLMGCP, LOGL_DEBUG,
- "endpoint %x dest %s tcfg->audio_loop %d endp->conn_mode %d (== loopback: %d)\n",
- ENDPOINT_NUMBER(endp),
- dest == MGCP_DEST_NET? "net" : "bts",
- tcfg->audio_loop,
- endp->conn_mode,
- endp->conn_mode == MGCP_CONN_LOOPBACK);
+ OSMO_ASSERT(conn_src);
+ OSMO_ASSERT(conn_dst);
- /* For loop toggle the destination and then dispatch. */
- if (tcfg->audio_loop)
- dest = !dest;
-
- /* Loop based on the conn_mode, maybe undoing the above */
- if (endp->conn_mode == MGCP_CONN_LOOPBACK)
- dest = !dest;
-
- if (dest == MGCP_DEST_NET) {
- rtp_end = &endp->net_end;
- rtp_state = &endp->bts_state;
- tap_idx = MGCP_TAP_NET_OUT;
+ if (is_rtp) {
+ LOGP(DLMGCP, LOGL_DEBUG,
+ "endpoint:%x delivering RTP packet...\n",
+ ENDPOINT_NUMBER(endp));
} else {
- rtp_end = &endp->bts_end;
- rtp_state = &endp->net_state;
- tap_idx = MGCP_TAP_BTS_OUT;
+ LOGP(DLMGCP, LOGL_DEBUG,
+ "endpoint:%x delivering RTCP packet...\n",
+ ENDPOINT_NUMBER(endp));
}
+
LOGP(DLMGCP, LOGL_DEBUG,
- "endpoint %x dest %s net_end %s %d %d bts_end %s %d %d rtp_end %s %d %d\n",
- ENDPOINT_NUMBER(endp),
- dest == MGCP_DEST_NET? "net" : "bts",
+ "endpoint:%x loop:%d, mode:%d ",
+ ENDPOINT_NUMBER(endp), tcfg->audio_loop, conn_src->conn->mode);
+ if (conn_src->conn->mode == MGCP_CONN_LOOPBACK)
+ LOGPC(DLMGCP, LOGL_DEBUG, "(loopback)\n");
+ else
+ LOGPC(DLMGCP, LOGL_DEBUG, "\n");
- inet_ntoa(endp->net_end.addr),
- ntohs(endp->net_end.rtp_port),
- ntohs(endp->net_end.rtcp_port),
-
- inet_ntoa(endp->bts_end.addr),
- ntohs(endp->bts_end.rtp_port),
- ntohs(endp->bts_end.rtcp_port),
-
- inet_ntoa(rtp_end->addr),
- ntohs(rtp_end->rtp_port),
- ntohs(rtp_end->rtcp_port)
- );
+ /* Note: In case of loopback configuration, both, the source and the
+ * destination will point to the same connection. */
+ rtp_end = &conn_dst->end;
+ rtp_state = &conn_src->state;
+ dest_name = conn_dst->conn->name;
if (!rtp_end->output_enabled) {
rtp_end->dropped_packets += 1;
LOGP(DLMGCP, LOGL_DEBUG,
- "endpoint %x output disabled, drop to %s %s %d %d\n",
+ "endpoint:%x output disabled, drop to %s %s "
+ "rtp_port:%u rtcp_port:%u\n",
ENDPOINT_NUMBER(endp),
- dest == MGCP_DEST_NET? "net" : "bts",
+ dest_name,
inet_ntoa(rtp_end->addr),
- ntohs(rtp_end->rtp_port),
- ntohs(rtp_end->rtcp_port)
+ ntohs(rtp_end->rtp_port), ntohs(rtp_end->rtcp_port)
);
} else if (is_rtp) {
int cont;
int nbytes = 0;
- int len = rc;
+ int buflen = len;
do {
+ /* Run transcoder */
cont = endp->cfg->rtp_processing_cb(endp, rtp_end,
- buf, &len, RTP_BUF_SIZE);
+ buf, &buflen,
+ RTP_BUF_SIZE);
if (cont < 0)
break;
- mgcp_patch_and_count(endp, rtp_state, rtp_end, addr, buf, len);
- LOGP(DLMGCP, LOGL_DEBUG,
- "endpoint %x process/send to %s %s %d %d\n",
- ENDPOINT_NUMBER(endp),
- (dest == MGCP_DEST_NET)? "net" : "bts",
- inet_ntoa(rtp_end->addr),
- ntohs(rtp_end->rtp_port),
- ntohs(rtp_end->rtcp_port)
- );
- forward_data(rtp_end->rtp.fd, &endp->taps[tap_idx],
- buf, len);
+ if (addr)
+ mgcp_patch_and_count(endp, rtp_state, rtp_end,
+ addr, buf, buflen);
+ LOGP(DLMGCP, LOGL_DEBUG,
+ "endpoint:%x process/send to %s %s "
+ "rtp_port:%u rtcp_port:%u\n",
+ ENDPOINT_NUMBER(endp), dest_name,
+ inet_ntoa(rtp_end->addr), ntohs(rtp_end->rtp_port),
+ ntohs(rtp_end->rtcp_port)
+ );
+
+ /* Forward a copy of the RTP data to a debug ip/port */
+ forward_data(rtp_end->rtp.fd, &conn_src->tap_out,
+ buf, buflen);
/* FIXME: HACK HACK HACK. See OS#2459.
* The ip.access nano3G needs the first RTP payload's first two bytes to read hex
* 'e400', or it will reject the RAB assignment. It seems to not harm other femto
* cells (as long as we patch only the first RTP payload in each stream).
*/
- if (tap_idx == MGCP_TAP_BTS_OUT
- && !rtp_state->patched_first_rtp_payload) {
- uint8_t *data = (uint8_t*)&buf[12];
+ if (!rtp_state->patched_first_rtp_payload) {
+ uint8_t *data = (uint8_t *) & buf[12];
data[0] = 0xe4;
data[1] = 0x00;
rtp_state->patched_first_rtp_payload = true;
}
- rc = mgcp_udp_send(rtp_end->rtp.fd,
- &rtp_end->addr,
- rtp_end->rtp_port, buf, len);
+ len = mgcp_udp_send(rtp_end->rtp.fd,
+ &rtp_end->addr,
+ rtp_end->rtp_port, buf, buflen);
- if (rc <= 0)
- return rc;
- nbytes += rc;
- len = cont;
- } while (len > 0);
+ if (len <= 0)
+ return len;
+
+ conn_dst->end.packets_tx += 1;
+ conn_dst->end.octets_tx += len;
+
+ nbytes += len;
+ buflen = cont;
+ } while (buflen > 0);
return nbytes;
} else if (!tcfg->omit_rtcp) {
LOGP(DLMGCP, LOGL_DEBUG,
- "endpoint %x send to %s %s %d %d\n",
+ "endpoint:%x send to %s %s rtp_port:%u rtcp_port:%u\n",
ENDPOINT_NUMBER(endp),
- dest == MGCP_DEST_NET? "net" : "bts",
+ dest_name,
inet_ntoa(rtp_end->addr),
- ntohs(rtp_end->rtp_port),
- ntohs(rtp_end->rtcp_port)
+ ntohs(rtp_end->rtp_port), ntohs(rtp_end->rtcp_port)
);
- return mgcp_udp_send(rtp_end->rtcp.fd,
- &rtp_end->addr,
- rtp_end->rtcp_port, buf, rc);
+ len = mgcp_udp_send(rtp_end->rtcp.fd,
+ &rtp_end->addr,
+ rtp_end->rtcp_port, buf, len);
+
+ conn_dst->end.packets_tx += 1;
+ conn_dst->end.octets_tx += len;
+
+ return len;
}
return 0;
}
-static int receive_from(struct mgcp_endpoint *endp, int fd, struct sockaddr_in *addr,
- char *buf, int bufsize)
+/* Helper function for mgcp_recv(),
+ Receive one RTP Packet + Originating address from file descriptor */
+static int receive_from(struct mgcp_endpoint *endp, int fd,
+ struct sockaddr_in *addr, char *buf, int bufsize)
{
int rc;
socklen_t slen = sizeof(*addr);
+ struct sockaddr_in addr_sink;
+ char buf_sink[RTP_BUF_SIZE];
+ bool tossed = false;
- rc = recvfrom(fd, buf, bufsize, 0,
- (struct sockaddr *) addr, &slen);
+ if (!addr)
+ addr = &addr_sink;
+ if (!buf) {
+ tossed = true;
+ buf = buf_sink;
+ bufsize = sizeof(buf_sink);
+ }
+
+ rc = recvfrom(fd, buf, bufsize, 0, (struct sockaddr *)addr, &slen);
+
+ LOGP(DLMGCP, LOGL_DEBUG,
+ "receiving %u bytes length packet from %s:%u ...\n",
+ rc, inet_ntoa(addr->sin_addr), ntohs(addr->sin_port));
+
if (rc < 0) {
- LOGP(DLMGCP, LOGL_ERROR, "Failed to receive message on: 0x%x errno: %d/%s\n",
- ENDPOINT_NUMBER(endp), errno, strerror(errno));
+ LOGP(DLMGCP, LOGL_ERROR,
+ "endpoint:%x failed to receive packet, errno: %d/%s\n",
+ ENDPOINT_NUMBER(endp), errno, strerror(errno));
return -1;
}
- /* do not forward aynthing... maybe there is a packet from the bts */
- if (!endp->allocated)
- return -1;
-
- #warning "Slight spec violation. With connection mode recvonly we should attempt to forward."
+ if (tossed) {
+ LOGP(DLMGCP, LOGL_ERROR, "endpoint:%x packet tossed\n",
+ ENDPOINT_NUMBER(endp));
+ }
return rc;
}
-static int rtp_data_net(struct osmo_fd *fd, unsigned int what)
+/* Check if the origin (addr) matches the address/port data of the RTP
+ * connections. */
+static int check_rtp_origin(struct mgcp_conn_rtp *conn,
+ struct sockaddr_in *addr)
{
- char buf[RTP_BUF_SIZE];
- struct sockaddr_in addr;
struct mgcp_endpoint *endp;
- int rc, proto;
+ endp = conn->conn->endp;
- endp = (struct mgcp_endpoint *) fd->data;
-
- rc = receive_from(endp, fd->fd, &addr, buf, sizeof(buf));
- if (rc <= 0)
- return -1;
-
- LOGP(DLMGCP, LOGL_DEBUG,
- "endpoint %x",
- ENDPOINT_NUMBER(endp));
- LOGPC(DLMGCP, LOGL_DEBUG,
- " from net %s %d",
- inet_ntoa(addr.sin_addr),
- ntohs(addr.sin_port));
- LOGPC(DLMGCP, LOGL_DEBUG,
- " net_end %s %d %d",
- inet_ntoa(endp->net_end.addr),
- ntohs(endp->net_end.rtp_port),
- ntohs(endp->net_end.rtcp_port));
- LOGPC(DLMGCP, LOGL_DEBUG,
- " bts_end %s %d %d\n",
- inet_ntoa(endp->bts_end.addr),
- ntohs(endp->bts_end.rtp_port),
- ntohs(endp->bts_end.rtcp_port)
- );
-
- if (memcmp(&addr.sin_addr, &endp->net_end.addr, sizeof(addr.sin_addr)) != 0) {
+ /* Note: Check if the inbound RTP data comes from the same host to
+ * which we send our outgoing RTP traffic. */
+ if (memcmp(&addr->sin_addr, &conn->end.addr, sizeof(addr->sin_addr))
+ != 0) {
LOGP(DLMGCP, LOGL_ERROR,
- "rtp_data_net: Endpoint 0x%x data from wrong address %s vs. ",
- ENDPOINT_NUMBER(endp), inet_ntoa(addr.sin_addr));
- LOGPC(DLMGCP, LOGL_ERROR,
- "%s\n", inet_ntoa(endp->net_end.addr));
+ "endpoint:%x data from wrong address: %s, ",
+ ENDPOINT_NUMBER(endp), inet_ntoa(addr->sin_addr));
+ LOGPC(DLMGCP, LOGL_ERROR, "expected: %s\n",
+ inet_ntoa(conn->end.addr));
+ LOGP(DLMGCP, LOGL_ERROR, "endpoint:%x packet tossed\n",
+ ENDPOINT_NUMBER(endp));
return -1;
}
- switch(endp->type) {
- case MGCP_RTP_DEFAULT:
- case MGCP_RTP_TRANSCODED:
- if (endp->net_end.rtp_port != addr.sin_port &&
- endp->net_end.rtcp_port != addr.sin_port) {
- LOGP(DLMGCP, LOGL_ERROR,
- "rtp_data_net: Data from wrong source port %d on 0x%x\n",
- ntohs(addr.sin_port), ENDPOINT_NUMBER(endp));
- return -1;
- }
- break;
- case MGCP_OSMUX_BSC:
- case MGCP_OSMUX_BSC_NAT:
- break;
+ /* Note: Usually the remote remote port of the data we receive will be
+ * the same as the remote port where we transmit outgoing RTP traffic
+ * to (set by MDCX). We use this to check the origin of the data for
+ * plausibility. */
+ if (conn->end.rtp_port != addr->sin_port &&
+ conn->end.rtcp_port != addr->sin_port) {
+ LOGP(DLMGCP, LOGL_ERROR,
+ "endpoint:%x data from wrong source port: %d, ",
+ ENDPOINT_NUMBER(endp), ntohs(addr->sin_port));
+ LOGPC(DLMGCP, LOGL_ERROR,
+ "expected: %d for RTP or %d for RTCP\n",
+ ntohs(conn->end.rtp_port), ntohs(conn->end.rtcp_port));
+ LOGP(DLMGCP, LOGL_ERROR, "endpoint:%x packet tossed\n",
+ ENDPOINT_NUMBER(endp));
+ return -1;
}
- LOGP(DLMGCP, LOGL_DEBUG,
- "rtp_data_net: Endpoint %x data from %s %d\n",
- ENDPOINT_NUMBER(endp),
- inet_ntoa(addr.sin_addr),
- ntohs(addr.sin_port));
-
- /* throw away the dummy message */
- if (rc == 1 && buf[0] == MGCP_DUMMY_LOAD) {
- LOGP(DLMGCP, LOGL_NOTICE, "Filtered dummy from network on 0x%x\n",
- ENDPOINT_NUMBER(endp));
- return 0;
- }
-
- proto = fd == &endp->net_end.rtp ? MGCP_PROTO_RTP : MGCP_PROTO_RTCP;
- endp->net_end.packets += 1;
- endp->net_end.octets += rc;
-
- forward_data(fd->fd, &endp->taps[MGCP_TAP_NET_IN], buf, rc);
-
- switch (endp->type) {
- case MGCP_RTP_DEFAULT:
- return mgcp_send(endp, MGCP_DEST_BTS, proto == MGCP_PROTO_RTP,
- &addr, buf, rc);
- case MGCP_RTP_TRANSCODED:
- return mgcp_send_transcoder(&endp->trans_net, endp->cfg,
- proto == MGCP_PROTO_RTP, buf, rc);
- case MGCP_OSMUX_BSC_NAT:
- return osmux_xfrm_to_osmux(MGCP_DEST_BTS, buf, rc, endp);
- case MGCP_OSMUX_BSC: /* Should not happen */
- break;
- }
-
- LOGP(DLMGCP, LOGL_ERROR, "Bad MGCP type %u on endpoint %u\n",
- endp->type, ENDPOINT_NUMBER(endp));
return 0;
}
-static void discover_bts(struct mgcp_endpoint *endp, int proto, struct sockaddr_in *addr)
+/* Check the if the destination address configuration of an RTP connection
+ * makes sense */
+static int check_rtp_destin(struct mgcp_conn_rtp *conn)
{
- struct mgcp_config *cfg = endp->cfg;
+ struct mgcp_endpoint *endp;
+ endp = conn->conn->endp;
- if (proto == MGCP_PROTO_RTP && endp->bts_end.rtp_port == 0) {
- if (!cfg->bts_ip ||
- memcmp(&addr->sin_addr,
- &cfg->bts_in, sizeof(cfg->bts_in)) == 0 ||
- memcmp(&addr->sin_addr,
- &endp->bts_end.addr, sizeof(endp->bts_end.addr)) == 0) {
-
- endp->bts_end.rtp_port = addr->sin_port;
- endp->bts_end.addr = addr->sin_addr;
-
- LOGP(DLMGCP, LOGL_NOTICE,
- "Found BTS for endpoint: 0x%x on port: %d/%d of %s\n",
- ENDPOINT_NUMBER(endp), ntohs(endp->bts_end.rtp_port),
- ntohs(endp->bts_end.rtcp_port), inet_ntoa(addr->sin_addr));
- }
- } else if (proto == MGCP_PROTO_RTCP && endp->bts_end.rtcp_port == 0) {
- if (memcmp(&endp->bts_end.addr, &addr->sin_addr,
- sizeof(endp->bts_end.addr)) == 0) {
- endp->bts_end.rtcp_port = addr->sin_port;
- }
+ if (strcmp(inet_ntoa(conn->end.addr), "0.0.0.0") == 0) {
+ LOGP(DLMGCP, LOGL_ERROR,
+ "endpoint:%x destination IP-address is invalid\n",
+ ENDPOINT_NUMBER(endp));
+ return -1;
}
+
+ if (conn->end.rtp_port == 0) {
+ LOGP(DLMGCP, LOGL_ERROR,
+ "endpoint:%x destination rtp port is invalid\n",
+ ENDPOINT_NUMBER(endp));
+ return -1;
+ }
+
+ return 0;
}
-static int rtp_data_bts(struct osmo_fd *fd, unsigned int what)
+/* Receive RTP data from a specified source connection and dispatch it to a
+ * destination connection. */
+static int mgcp_recv(int *proto, struct sockaddr_in *addr, char *buf,
+ unsigned int buf_size, struct osmo_fd *fd)
{
- char buf[RTP_BUF_SIZE];
- struct sockaddr_in addr;
struct mgcp_endpoint *endp;
- int rc, proto;
+ struct mgcp_conn_rtp *conn;
+ struct mgcp_trunk_config *tcfg;
+ int rc;
- endp = (struct mgcp_endpoint *) fd->data;
+ conn = (struct mgcp_conn_rtp*) fd->data;
+ endp = conn->conn->endp;
+ tcfg = endp->tcfg;
- rc = receive_from(endp, fd->fd, &addr, buf, sizeof(buf));
+ LOGP(DLMGCP, LOGL_DEBUG, "endpoint:%x receiving RTP/RTCP packet...\n",
+ ENDPOINT_NUMBER(endp));
+
+ rc = receive_from(endp, fd->fd, addr, buf, buf_size);
if (rc <= 0)
return -1;
+ *proto = fd == &conn->end.rtp ? MGCP_PROTO_RTP : MGCP_PROTO_RTCP;
- proto = fd == &endp->bts_end.rtp ? MGCP_PROTO_RTP : MGCP_PROTO_RTCP;
+ LOGP(DLMGCP, LOGL_DEBUG, "endpoint:%x ", ENDPOINT_NUMBER(endp));
+ LOGPC(DLMGCP, LOGL_DEBUG, "receiveing from %s %s %d\n",
+ conn->conn->name, inet_ntoa(addr->sin_addr),
+ ntohs(addr->sin_port));
+ LOGP(DLMGCP, LOGL_DEBUG, "endpoint:%x conn:%s\n", ENDPOINT_NUMBER(endp),
+ mgcp_conn_dump(conn->conn));
- /* We have no idea who called us, maybe it is the BTS. */
- /* it was the BTS... */
- discover_bts(endp, proto, &addr);
-
- if (memcmp(&endp->bts_end.addr, &addr.sin_addr, sizeof(addr.sin_addr)) != 0) {
- LOGP(DLMGCP, LOGL_ERROR,
- "rtp_data_bts: Data from wrong bts %s on 0x%x\n",
- inet_ntoa(addr.sin_addr), ENDPOINT_NUMBER(endp));
- return -1;
+ /* Check if the origin of the RTP packet seems plausible */
+ if (tcfg->rtp_accept_all == 0) {
+ if (check_rtp_origin(conn, addr) != 0)
+ return -1;
}
- if (endp->bts_end.rtp_port != addr.sin_port &&
- endp->bts_end.rtcp_port != addr.sin_port) {
- LOGP(DLMGCP, LOGL_ERROR,
- "rtp_data_bts: ata from wrong bts source port %d on 0x%x\n",
- ntohs(addr.sin_port), ENDPOINT_NUMBER(endp));
- return -1;
- }
-
- LOGP(DLMGCP, LOGL_DEBUG,
- "rtp_data_bts: Endpoint %x data from %s %d\n",
- ENDPOINT_NUMBER(endp),
- inet_ntoa(addr.sin_addr),
- ntohs(addr.sin_port));
-
- /* throw away the dummy message */
+ /* Filter out dummy message */
if (rc == 1 && buf[0] == MGCP_DUMMY_LOAD) {
- LOGP(DLMGCP, LOGL_NOTICE, "Filtered dummy from bts on 0x%x\n",
- ENDPOINT_NUMBER(endp));
+ LOGP(DLMGCP, LOGL_NOTICE,
+ "endpoint:%x dummy message received\n",
+ ENDPOINT_NUMBER(endp));
+ LOGP(DLMGCP, LOGL_ERROR,
+ "endpoint:%x packet tossed\n", ENDPOINT_NUMBER(endp));
return 0;
}
- /* do this before the loop handling */
- endp->bts_end.packets += 1;
- endp->bts_end.octets += rc;
+ /* Increment RX statistics */
+ conn->end.packets_rx += 1;
+ conn->end.octets_rx += rc;
- forward_data(fd->fd, &endp->taps[MGCP_TAP_BTS_IN], buf, rc);
+ /* Forward a copy of the RTP data to a debug ip/port */
+ forward_data(fd->fd, &conn->tap_in, buf, rc);
- switch (endp->type) {
+ return rc;
+}
+
+/* Send RTP data. Possible options are standard RTP packet
+ * transmission or trsmission via an osmux connection */
+static int mgcp_send_rtp(int proto, struct sockaddr_in *addr, char *buf,
+ unsigned int buf_size,
+ struct mgcp_conn_rtp *conn_src,
+ struct mgcp_conn_rtp *conn_dst)
+{
+ struct mgcp_endpoint *endp;
+ endp = conn_src->conn->endp;
+
+ LOGP(DLMGCP, LOGL_DEBUG, "endpoint:%x destin conn:%s\n",
+ ENDPOINT_NUMBER(endp), mgcp_conn_dump(conn_dst->conn));
+
+ /* Before we try to deliver the packet, we check if the destination
+ * port and IP-Address make sense at all. If not, we will be unable
+ * to deliver the packet. */
+ if (check_rtp_destin(conn_dst) != 0)
+ return -1;
+
+ /* Depending on the RTP connection type, deliver the RTP packet to the
+ * destination connection. */
+ switch (conn_dst->type) {
case MGCP_RTP_DEFAULT:
LOGP(DLMGCP, LOGL_DEBUG,
- "rtp_data_bts: Endpoint %x MGCP_RTP_DEFAULT\n",
+ "endpoint:%x endpoint type is MGCP_RTP_DEFAULT, "
+ "using mgcp_send() to forward data directly\n",
ENDPOINT_NUMBER(endp));
- return mgcp_send(endp, MGCP_DEST_NET, proto == MGCP_PROTO_RTP,
- &addr, buf, rc);
- case MGCP_RTP_TRANSCODED:
- return mgcp_send_transcoder(&endp->trans_bts, endp->cfg,
- proto == MGCP_PROTO_RTP, buf, rc);
- case MGCP_OSMUX_BSC:
- /* OSMUX translation: BTS -> BSC */
- return osmux_xfrm_to_osmux(MGCP_DEST_NET, buf, rc, endp);
+ return mgcp_send(endp, proto == MGCP_PROTO_RTP,
+ addr, buf, buf_size, conn_src, conn_dst);
case MGCP_OSMUX_BSC_NAT:
- break; /* Should not happen */
+ case MGCP_OSMUX_BSC:
+ LOGP(DLMGCP, LOGL_DEBUG,
+ "endpoint:%x endpoint type is MGCP_OSMUX_BSC_NAT, "
+ "using osmux_xfrm_to_osmux() to forward data through OSMUX\n",
+ ENDPOINT_NUMBER(endp));
+ return osmux_xfrm_to_osmux(buf, buf_size, conn_dst);
}
- LOGP(DLMGCP, LOGL_ERROR, "Bad MGCP type %u on endpoint %u\n",
- endp->type, ENDPOINT_NUMBER(endp));
+ /* If the data has not been handled/forwarded until here, it will
+ * be discarded, this should not happen, normally the MGCP type
+ * should be properly set */
+ LOGP(DLMGCP, LOGL_ERROR,
+ "endpoint:%x bad MGCP type -- data discarded!\n",
+ ENDPOINT_NUMBER(endp));
+
+ return -1;
+}
+
+/*! dispatch incoming RTP packet to opposite RTP connection.
+ * \param[in] proto protocol (MGCP_CONN_TYPE_RTP or MGCP_CONN_TYPE_RTCP)
+ * \param[in] addr socket address where the RTP packet has been received from
+ * \param[in] buf buffer that hold the RTP payload
+ * \param[in] buf_size size data length of buf
+ * \param[in] conn originating connection
+ * \returns 0 on success, -1 on ERROR */
+int mgcp_dispatch_rtp_bridge_cb(int proto, struct sockaddr_in *addr, char *buf,
+ unsigned int buf_size, struct mgcp_conn *conn)
+{
+ struct mgcp_conn *conn_dst;
+ struct mgcp_endpoint *endp;
+ endp = conn->endp;
+
+ /*! NOTE: This callback function implements the endpoint specific
+ * dispatch bahviour of an rtp bridge/proxy endpoint. It is assumed
+ * that the endpoint will hold only two connections. This premise
+ * is used to determine the opposite connection (it is always the
+ * connection that is not the originating connection). Once the
+ * destination connection is known the RTP packet is sent via
+ * the destination connection. */
+
+ /* Find a destination connection. */
+ /* NOTE: This code path runs every time an RTP packet is received. The
+ * function mgcp_find_dst_conn() we use to determine the detination
+ * connection will iterate the connection list inside the endpoint.
+ * Since list iterations are quite costly, we will figure out the
+ * destination only once and use the optional private data pointer of
+ * the connection to cache the destination connection pointer. */
+ if (!conn->priv) {
+ conn_dst = mgcp_find_dst_conn(conn);
+ conn->priv = conn_dst;
+ } else {
+ conn_dst = (struct mgcp_conn *)conn->priv;
+ }
+
+ /* There is no destination conn, stop here */
+ if (!conn_dst) {
+ LOGP(DLMGCP, LOGL_ERROR,
+ "endpoint:%x unable to find destination conn\n",
+ ENDPOINT_NUMBER(endp));
+ return -1;
+ }
+
+ /* The destination conn is not an RTP connection */
+ if (conn_dst->type != MGCP_CONN_TYPE_RTP) {
+ LOGP(DLMGCP, LOGL_ERROR,
+ "endpoint:%x unable to find suitable destination conn\n",
+ ENDPOINT_NUMBER(endp));
+ return -1;
+ }
+
+ /* Dispatch RTP packet to destination RTP connection */
+ return mgcp_send_rtp(proto, addr, buf,
+ buf_size, &conn->u.rtp, &conn_dst->u.rtp);
+
+}
+
+/* Handle incoming RTP data from NET */
+static int rtp_data_net(struct osmo_fd *fd, unsigned int what)
+{
+ /* NOTE: This is a generic implementation. RTP data is received. In
+ * case of loopback the data is just sent back to its origin. All
+ * other cases implement endpoint specific behaviour (e.g. how is the
+ * destination connection determined?). That specific behaviour is
+ * implemented by the callback function that is called at the end of
+ * the function */
+
+ struct mgcp_conn_rtp *conn_src;
+ struct mgcp_endpoint *endp;
+ struct sockaddr_in addr;
+
+ char buf[RTP_BUF_SIZE];
+ int proto;
+ int rc;
+
+ conn_src = (struct mgcp_conn_rtp *)fd->data;
+ OSMO_ASSERT(conn_src);
+ endp = conn_src->conn->endp;
+ OSMO_ASSERT(endp);
+
+ LOGP(DLMGCP, LOGL_DEBUG, "endpoint:%x source conn:%s\n",
+ ENDPOINT_NUMBER(endp), mgcp_conn_dump(conn_src->conn));
+
+ /* Receive packet */
+ rc = mgcp_recv(&proto, &addr, buf, sizeof(buf), fd);
+ if (rc < 0)
+ return -1;
+
+ /* Check if the connection is in loopback mode, if yes, just send the
+ * incoming data back to the origin */
+ if (conn_src->conn->mode == MGCP_CONN_LOOPBACK) {
+ return mgcp_send_rtp(proto, &addr, buf,
+ sizeof(buf), conn_src, conn_src);
+ }
+
+ /* Execute endpoint specific implementation that handles the
+ * dispatching of the RTP data */
+ return endp->type->dispatch_rtp_cb(proto, &addr, buf, sizeof(buf),
+ conn_src->conn);
+}
+
+/*! set IP Type of Service parameter.
+ * \param[in] fd associated file descriptor
+ * \param[in] tos dscp value
+ * \returns 0 on success, -1 on ERROR */
+int mgcp_set_ip_tos(int fd, int tos)
+{
+ int ret;
+ ret = setsockopt(fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
+
+ if (ret < 0)
+ return -1;
return 0;
}
-static int rtp_data_transcoder(struct mgcp_rtp_end *end, struct mgcp_endpoint *_endp,
- int dest, struct osmo_fd *fd)
-{
- char buf[RTP_BUF_SIZE];
- struct sockaddr_in addr;
- struct mgcp_config *cfg;
- int rc, proto;
-
- cfg = _endp->cfg;
- rc = receive_from(_endp, fd->fd, &addr, buf, sizeof(buf));
- if (rc <= 0)
- return -1;
-
- proto = fd == &end->rtp ? MGCP_PROTO_RTP : MGCP_PROTO_RTCP;
-
- if (memcmp(&addr.sin_addr, &cfg->transcoder_in, sizeof(addr.sin_addr)) != 0) {
- LOGP(DLMGCP, LOGL_ERROR,
- "Data not coming from transcoder dest: %d %s on 0x%x\n",
- dest, inet_ntoa(addr.sin_addr), ENDPOINT_NUMBER(_endp));
- return -1;
- }
-
- if (end->rtp_port != addr.sin_port &&
- end->rtcp_port != addr.sin_port) {
- LOGP(DLMGCP, LOGL_ERROR,
- "Data from wrong transcoder dest %d source port %d on 0x%x\n",
- dest, ntohs(addr.sin_port), ENDPOINT_NUMBER(_endp));
- return -1;
- }
-
- /* throw away the dummy message */
- if (rc == 1 && buf[0] == MGCP_DUMMY_LOAD) {
- LOGP(DLMGCP, LOGL_NOTICE, "Filtered dummy from transcoder dest %d on 0x%x\n",
- dest, ENDPOINT_NUMBER(_endp));
- return 0;
- }
-
- end->packets += 1;
- return mgcp_send(_endp, dest, proto == MGCP_PROTO_RTP, &addr, buf, rc);
-}
-
-static int rtp_data_trans_net(struct osmo_fd *fd, unsigned int what)
-{
- struct mgcp_endpoint *endp;
- endp = (struct mgcp_endpoint *) fd->data;
-
- return rtp_data_transcoder(&endp->trans_net, endp, MGCP_DEST_NET, fd);
-}
-
-static int rtp_data_trans_bts(struct osmo_fd *fd, unsigned int what)
-{
- struct mgcp_endpoint *endp;
- endp = (struct mgcp_endpoint *) fd->data;
-
- return rtp_data_transcoder(&endp->trans_bts, endp, MGCP_DEST_BTS, fd);
-}
-
+/*! bind RTP port to osmo_fd.
+ * \param[in] source_addr source (local) address to bind on
+ * \param[in] fd associated file descriptor
+ * \param[in] port to bind on
+ * \returns 0 on success, -1 on ERROR */
int mgcp_create_bind(const char *source_addr, struct osmo_fd *fd, int port)
{
struct sockaddr_in addr;
@@ -991,64 +1055,80 @@
fd->fd = socket(AF_INET, SOCK_DGRAM, 0);
if (fd->fd < 0) {
- LOGP(DLMGCP, LOGL_ERROR, "Failed to create UDP port.\n");
+ LOGP(DLMGCP, LOGL_ERROR, "failed to create UDP port (%s:%i).\n",
+ source_addr, port);
+ return -1;
+ } else {
+ LOGP(DLMGCP, LOGL_DEBUG,
+ "created UDP port (%s:%i).\n", source_addr, port);
+ }
+
+ if (setsockopt(fd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) != 0) {
+ LOGP(DLMGCP, LOGL_ERROR,
+ "failed to set socket options (%s:%i).\n", source_addr,
+ port);
return -1;
}
- setsockopt(fd->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
inet_aton(source_addr, &addr.sin_addr);
- if (bind(fd->fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
+ if (bind(fd->fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
close(fd->fd);
fd->fd = -1;
+ LOGP(DLMGCP, LOGL_ERROR, "failed to bind UDP port (%s:%i).\n",
+ source_addr, port);
return -1;
+ } else {
+ LOGP(DLMGCP, LOGL_DEBUG,
+ "bound UDP port (%s:%i).\n", source_addr, port);
}
return 0;
}
-int mgcp_set_ip_tos(int fd, int tos)
-{
- int ret;
- ret = setsockopt(fd, IPPROTO_IP, IP_TOS,
- &tos, sizeof(tos));
- return ret != 0;
-}
-
+/* Bind RTP and RTCP port (helper function for mgcp_bind_net_rtp_port()) */
static int bind_rtp(struct mgcp_config *cfg, const char *source_addr,
- struct mgcp_rtp_end *rtp_end, int endpno)
+ struct mgcp_rtp_end *rtp_end, int endpno)
{
+ /* NOTE: The port that is used for RTCP is the RTP port incremented by one
+ * (e.g. RTP-Port = 16000 ==> RTCP-Port = 16001) */
+
if (mgcp_create_bind(source_addr, &rtp_end->rtp,
rtp_end->local_port) != 0) {
- LOGP(DLMGCP, LOGL_ERROR, "Failed to create RTP port: %s:%d on 0x%x\n",
- source_addr, rtp_end->local_port, endpno);
+ LOGP(DLMGCP, LOGL_ERROR,
+ "endpoint:%x failed to create RTP port: %s:%d\n", endpno,
+ source_addr, rtp_end->local_port);
goto cleanup0;
}
if (mgcp_create_bind(source_addr, &rtp_end->rtcp,
rtp_end->local_port + 1) != 0) {
- LOGP(DLMGCP, LOGL_ERROR, "Failed to create RTCP port: %s:%d on 0x%x\n",
- source_addr, rtp_end->local_port + 1, endpno);
+ LOGP(DLMGCP, LOGL_ERROR,
+ "endpoint:%x failed to create RTCP port: %s:%d\n", endpno,
+ source_addr, rtp_end->local_port + 1);
goto cleanup1;
}
+ /* Set Type of Service (DSCP-Value) as configured via VTY */
mgcp_set_ip_tos(rtp_end->rtp.fd, cfg->endp_dscp);
mgcp_set_ip_tos(rtp_end->rtcp.fd, cfg->endp_dscp);
rtp_end->rtp.when = BSC_FD_READ;
if (osmo_fd_register(&rtp_end->rtp) != 0) {
- LOGP(DLMGCP, LOGL_ERROR, "Failed to register RTP port %d on 0x%x\n",
- rtp_end->local_port, endpno);
+ LOGP(DLMGCP, LOGL_ERROR,
+ "endpoint:%x failed to register RTP port %d\n", endpno,
+ rtp_end->local_port);
goto cleanup2;
}
rtp_end->rtcp.when = BSC_FD_READ;
if (osmo_fd_register(&rtp_end->rtcp) != 0) {
- LOGP(DLMGCP, LOGL_ERROR, "Failed to register RTCP port %d on 0x%x\n",
- rtp_end->local_port + 1, endpno);
+ LOGP(DLMGCP, LOGL_ERROR,
+ "endpoint:%x failed to register RTCP port %d\n", endpno,
+ rtp_end->local_port + 1);
goto cleanup3;
}
@@ -1066,54 +1146,47 @@
return -1;
}
-static int int_bind(const char *port,
- struct mgcp_rtp_end *end, int (*cb)(struct osmo_fd *, unsigned),
- struct mgcp_endpoint *_endp,
- const char *source_addr, int rtp_port)
+/*! bind RTP port to endpoint/connection.
+ * \param[in] endp endpoint that holds the RTP connection
+ * \param[in] rtp_port port number to bind on
+ * \param[in] conn associated RTP connection
+ * \returns 0 on success, -1 on ERROR */
+int mgcp_bind_net_rtp_port(struct mgcp_endpoint *endp, int rtp_port,
+ struct mgcp_conn_rtp *conn)
{
+ char name[512];
+ struct mgcp_rtp_end *end;
+
+ snprintf(name, sizeof(name), "%s-%u", conn->conn->name, conn->conn->id);
+ end = &conn->end;
+
if (end->rtp.fd != -1 || end->rtcp.fd != -1) {
- LOGP(DLMGCP, LOGL_ERROR, "Previous %s was still bound on %d\n",
- port, ENDPOINT_NUMBER(_endp));
- mgcp_free_rtp_port(end);
+ LOGP(DLMGCP, LOGL_ERROR,
+ "endpoint:%x %u was already bound on conn:%s\n",
+ ENDPOINT_NUMBER(endp), rtp_port,
+ mgcp_conn_dump(conn->conn));
+
+ /* Double bindings should never occour! Since we always allocate
+ * connections dynamically and free them when they are not
+ * needed anymore, there must be no previous binding leftover.
+ * Should there be a connection bound twice, we have a serious
+ * problem and must exit immediately! */
+ OSMO_ASSERT(false);
}
end->local_port = rtp_port;
- end->rtp.cb = cb;
- end->rtp.data = _endp;
- end->rtcp.data = _endp;
- end->rtcp.cb = cb;
- return bind_rtp(_endp->cfg, source_addr, end, ENDPOINT_NUMBER(_endp));
+ end->rtp.cb = rtp_data_net;
+ end->rtp.data = conn;
+ end->rtcp.data = conn;
+ end->rtcp.cb = rtp_data_net;
+
+ return bind_rtp(endp->cfg, mgcp_net_src_addr(endp), end,
+ ENDPOINT_NUMBER(endp));
}
-int mgcp_bind_bts_rtp_port(struct mgcp_endpoint *endp, int rtp_port)
-{
- return int_bind("bts-port", &endp->bts_end,
- rtp_data_bts, endp,
- mgcp_bts_src_addr(endp), rtp_port);
-}
-
-int mgcp_bind_net_rtp_port(struct mgcp_endpoint *endp, int rtp_port)
-{
- return int_bind("net-port", &endp->net_end,
- rtp_data_net, endp,
- mgcp_net_src_addr(endp), rtp_port);
-}
-
-int mgcp_bind_trans_net_rtp_port(struct mgcp_endpoint *endp, int rtp_port)
-{
- return int_bind("trans-net", &endp->trans_net,
- rtp_data_trans_net, endp,
- endp->cfg->source_addr, rtp_port);
-}
-
-int mgcp_bind_trans_bts_rtp_port(struct mgcp_endpoint *endp, int rtp_port)
-{
- return int_bind("trans-bts", &endp->trans_bts,
- rtp_data_trans_bts, endp,
- endp->cfg->source_addr, rtp_port);
-}
-
-int mgcp_free_rtp_port(struct mgcp_rtp_end *end)
+/*! free allocated RTP and RTCP ports.
+ * \param[in] end RTP end */
+void mgcp_free_rtp_port(struct mgcp_rtp_end *end)
{
if (end->rtp.fd != -1) {
close(end->rtp.fd);
@@ -1126,41 +1199,4 @@
end->rtcp.fd = -1;
osmo_fd_unregister(&end->rtcp);
}
-
- return 0;
-}
-
-
-void mgcp_state_calc_loss(struct mgcp_rtp_state *state,
- struct mgcp_rtp_end *end, uint32_t *expected,
- int *loss)
-{
- *expected = state->stats_cycles + state->stats_max_seq;
- *expected = *expected - state->stats_base_seq + 1;
-
- if (!state->stats_initialized) {
- *expected = 0;
- *loss = 0;
- return;
- }
-
- /*
- * Make sure the sign is correct and use the biggest
- * positive/negative number that fits.
- */
- *loss = *expected - end->packets;
- if (*expected < end->packets) {
- if (*loss > 0)
- *loss = INT_MIN;
- } else {
- if (*loss < 0)
- *loss = INT_MAX;
- }
-}
-
-uint32_t mgcp_state_calc_jitter(struct mgcp_rtp_state *state)
-{
- if (!state->stats_initialized)
- return 0;
- return state->stats_jitter >> 4;
}
diff --git a/src/libosmo-mgcp/mgcp_osmux.c b/src/libosmo-mgcp/mgcp_osmux.c
index 743d3f9..60ffe06 100644
--- a/src/libosmo-mgcp/mgcp_osmux.c
+++ b/src/libosmo-mgcp/mgcp_osmux.c
@@ -20,9 +20,10 @@
#include <osmocom/netif/osmux.h>
#include <osmocom/netif/rtp.h>
-#include <osmocom/legacy_mgcp/mgcp.h>
-#include <osmocom/legacy_mgcp/mgcp_internal.h>
-#include <osmocom/legacy_mgcp/osmux.h>
+#include <osmocom/mgcp/mgcp.h>
+#include <osmocom/mgcp/mgcp_internal.h>
+#include <osmocom/mgcp/osmux.h>
+#include <osmocom/mgcp/mgcp_conn.h>
static struct osmo_fd osmux_fd;
@@ -38,7 +39,8 @@
static void *osmux;
-static void osmux_deliver(struct msgb *batch_msg, void *data)
+/* Deliver OSMUX batch to the remote end */
+static void osmux_deliver_cb(struct msgb *batch_msg, void *data)
{
struct osmux_handle *handle = data;
struct sockaddr_in out = {
@@ -52,12 +54,12 @@
msgb_free(batch_msg);
}
+/* Lookup existing OSMUX handle for specified destination address. */
static struct osmux_handle *
osmux_handle_find_get(struct in_addr *addr, int rem_port)
{
struct osmux_handle *h;
- /* Lookup for existing OSMUX handle for this destination address. */
llist_for_each_entry(h, &osmux_handle_list, head) {
if (memcmp(&h->rem_addr, addr, sizeof(struct in_addr)) == 0 &&
h->rem_port == rem_port) {
@@ -72,11 +74,11 @@
return NULL;
}
+/* Put down no longer needed OSMUX handle */
static void osmux_handle_put(struct osmux_in_handle *in)
{
struct osmux_handle *h;
- /* Lookup for existing OSMUX handle for this destination address. */
llist_for_each_entry(h, &osmux_handle_list, head) {
if (h->in == in) {
if (--h->refcnt == 0) {
@@ -101,6 +103,7 @@
LOGP(DLMGCP, LOGL_ERROR, "cannot find Osmux input handle %p\n", in);
}
+/* Allocate free OSMUX handle */
static struct osmux_handle *
osmux_handle_alloc(struct mgcp_config *cfg, struct in_addr *addr, int rem_port)
{
@@ -119,11 +122,14 @@
return NULL;
}
- h->in->osmux_seq = 0; /* sequence number to start OSmux message from */
+ /* sequence number to start OSMUX message from */
+ h->in->osmux_seq = 0;
+
h->in->batch_factor = cfg->osmux_batch;
+
/* If batch size is zero, the library defaults to 1470 bytes. */
h->in->batch_size = cfg->osmux_batch_size;
- h->in->deliver = osmux_deliver;
+ h->in->deliver = osmux_deliver_cb;
osmux_xfrm_input_init(h->in);
h->in->data = h;
@@ -135,6 +141,8 @@
return h;
}
+/* Lookup existing handle for a specified address, if the handle can not be
+ * foud a the function will automatically allocate one */
static struct osmux_in_handle *
osmux_handle_lookup(struct mgcp_config *cfg, struct in_addr *addr, int rem_port)
{
@@ -151,47 +159,60 @@
return h->in;
}
-int osmux_xfrm_to_osmux(int type, char *buf, int rc, struct mgcp_endpoint *endp)
+/*! send RTP packet through OSMUX connection.
+ * \param[in] buf rtp data
+ * \param[in] buf_len length of rtp data
+ * \param[in] conn associated RTP connection
+ * \returns 0 on success, -1 on ERROR */
+int osmux_xfrm_to_osmux(char *buf, int buf_len, struct mgcp_conn_rtp *conn)
{
int ret;
struct msgb *msg;
msg = msgb_alloc(4096, "RTP");
if (!msg)
- return 0;
+ return -1;
- memcpy(msg->data, buf, rc);
- msgb_put(msg, rc);
+ memcpy(msg->data, buf, buf_len);
+ msgb_put(msg, buf_len);
- while ((ret = osmux_xfrm_input(endp->osmux.in, msg, endp->osmux.cid)) > 0) {
+ while ((ret = osmux_xfrm_input(conn->osmux.in, msg, conn->osmux.cid)) > 0) {
/* batch full, build and deliver it */
- osmux_xfrm_input_deliver(endp->osmux.in);
+ osmux_xfrm_input_deliver(conn->osmux.in);
}
return 0;
}
+/* Lookup the endpoint that corresponds to the specified address (port) */
static struct mgcp_endpoint *
endpoint_lookup(struct mgcp_config *cfg, int cid,
struct in_addr *from_addr, int type)
{
- struct mgcp_endpoint *tmp = NULL;
+ struct mgcp_endpoint *endp = NULL;
int i;
+ struct mgcp_conn_rtp *conn_net = NULL;
+ struct mgcp_conn_rtp *conn_bts = NULL;
- /* Lookup for the endpoint that corresponds to this port */
for (i=0; i<cfg->trunk.number_endpoints; i++) {
struct in_addr *this;
- tmp = &cfg->trunk.endpoints[i];
+ endp = &cfg->trunk.endpoints[i];
+#if 0
if (!tmp->allocated)
continue;
+#endif
switch(type) {
case MGCP_DEST_NET:
- this = &tmp->net_end.addr;
+ /* FIXME: Get rid of CONN_ID_XXX! */
+ conn_net = mgcp_conn_get_rtp(endp, CONN_ID_NET);
+ this = &conn_net->end.addr;
break;
case MGCP_DEST_BTS:
- this = &tmp->bts_end.addr;
+ /* FIXME: Get rid of CONN_ID_XXX! */
+ conn_bts = mgcp_conn_get_rtp(endp, CONN_ID_BTS);
+ this = &conn_bts->end.addr;
break;
default:
/* Should not ever happen */
@@ -199,8 +220,10 @@
return NULL;
}
- if (tmp->osmux.cid == cid && this->s_addr == from_addr->s_addr)
- return tmp;
+ /* FIXME: Get rid of CONN_ID_XXX! */
+ conn_net = mgcp_conn_get_rtp(endp, CONN_ID_NET);
+ if (conn_net->osmux.cid == cid && this->s_addr == from_addr->s_addr)
+ return endp;
}
LOGP(DLMGCP, LOGL_ERROR, "Cannot find endpoint with cid=%d\n", cid);
@@ -211,30 +234,54 @@
static void scheduled_tx_net_cb(struct msgb *msg, void *data)
{
struct mgcp_endpoint *endp = data;
+ struct mgcp_conn_rtp *conn_net = NULL;
+ struct mgcp_conn_rtp *conn_bts = NULL;
+
+ /* FIXME: Get rid of CONN_ID_XXX! */
+ conn_bts = mgcp_conn_get_rtp(endp, CONN_ID_BTS);
+ conn_net = mgcp_conn_get_rtp(endp, CONN_ID_NET);
+ if (!conn_bts || !conn_net)
+ return;
+
struct sockaddr_in addr = {
- .sin_addr = endp->net_end.addr,
- .sin_port = endp->net_end.rtp_port,
+ .sin_addr = conn_net->end.addr,
+ .sin_port = conn_net->end.rtp_port,
};
- endp->bts_end.octets += msg->len;
- endp->bts_end.packets++;
+ conn_bts->end.octets_tx += msg->len;
+ conn_bts->end.packets_tx++;
- mgcp_send(endp, MGCP_DEST_NET, 1, &addr, (char *)msg->data, msg->len);
+ /* Send RTP data to NET */
+ /* FIXME: Get rid of conn_bts and conn_net! */
+ mgcp_send(endp, 1, &addr, (char *)msg->data, msg->len,
+ conn_bts, conn_net);
msgb_free(msg);
}
static void scheduled_tx_bts_cb(struct msgb *msg, void *data)
{
struct mgcp_endpoint *endp = data;
+ struct mgcp_conn_rtp *conn_net = NULL;
+ struct mgcp_conn_rtp *conn_bts = NULL;
+
+ /* FIXME: Get rid of CONN_ID_XXX! */
+ conn_bts = mgcp_conn_get_rtp(endp, CONN_ID_BTS);
+ conn_net = mgcp_conn_get_rtp(endp, CONN_ID_NET);
+ if (!conn_bts || !conn_net)
+ return;
+
struct sockaddr_in addr = {
- .sin_addr = endp->bts_end.addr,
- .sin_port = endp->bts_end.rtp_port,
+ .sin_addr = conn_bts->end.addr,
+ .sin_port = conn_bts->end.rtp_port,
};
- endp->net_end.octets += msg->len;
- endp->net_end.packets++;
+ conn_net->end.octets_tx += msg->len;
+ conn_net->end.packets_tx++;
- mgcp_send(endp, MGCP_DEST_BTS, 1, &addr, (char *)msg->data, msg->len);
+ /* Send RTP data to BTS */
+ /* FIXME: Get rid of conn_bts and conn_net! */
+ mgcp_send(endp, 1, &addr, (char *)msg->data, msg->len,
+ conn_net, conn_bts);
msgb_free(msg);
}
@@ -271,6 +318,7 @@
struct sockaddr_in addr;
struct mgcp_config *cfg = ofd->data;
uint32_t rem;
+ struct mgcp_conn_rtp *conn_net = NULL;
msg = osmux_recv(ofd, &addr);
if (!msg)
@@ -287,17 +335,23 @@
/* Yes, we use MGCP_DEST_NET to locate the origin */
endp = endpoint_lookup(cfg, osmuxh->circuit_id,
&addr.sin_addr, MGCP_DEST_NET);
+
+ /* FIXME: Get rid of CONN_ID_XXX! */
+ conn_net = mgcp_conn_get_rtp(endp, CONN_ID_NET);
+ if (!conn_net)
+ goto out;
+
if (!endp) {
LOGP(DLMGCP, LOGL_ERROR,
"Cannot find an endpoint for circuit_id=%d\n",
osmuxh->circuit_id);
goto out;
}
- endp->osmux.stats.octets += osmux_chunk_length(msg, rem);
- endp->osmux.stats.chunks++;
+ conn_net->osmux.stats.octets += osmux_chunk_length(msg, rem);
+ conn_net->osmux.stats.chunks++;
rem = msg->len;
- osmux_xfrm_output(osmuxh, &endp->osmux.out, &list);
+ osmux_xfrm_output(osmuxh, &conn_net->osmux.out, &list);
osmux_tx_sched(&list, scheduled_tx_bts_cb, endp);
}
out:
@@ -311,6 +365,7 @@
{
struct mgcp_endpoint *endp;
uint8_t osmux_cid;
+ struct mgcp_conn_rtp *conn_net = NULL;
if (msg->len < 1 + sizeof(osmux_cid)) {
LOGP(DLMGCP, LOGL_ERROR,
@@ -337,10 +392,14 @@
goto out;
}
- if (endp->osmux.state == OSMUX_STATE_ENABLED)
+ conn_net = mgcp_conn_get_rtp(endp, CONN_ID_NET);
+ if (!conn_net)
goto out;
- if (osmux_enable_endpoint(endp, &addr->sin_addr, addr->sin_port) < 0 ) {
+ if (conn_net->osmux.state == OSMUX_STATE_ENABLED)
+ goto out;
+
+ if (osmux_enable_conn(endp, conn_net, &addr->sin_addr, addr->sin_port) < 0 ) {
LOGP(DLMGCP, LOGL_ERROR,
"Could not enable osmux in endpoint %d\n",
ENDPOINT_NUMBER(endp));
@@ -363,6 +422,7 @@
struct sockaddr_in addr;
struct mgcp_config *cfg = ofd->data;
uint32_t rem;
+ struct mgcp_conn_rtp *conn_net = NULL;
msg = osmux_recv(ofd, &addr);
if (!msg)
@@ -379,17 +439,23 @@
/* Yes, we use MGCP_DEST_BTS to locate the origin */
endp = endpoint_lookup(cfg, osmuxh->circuit_id,
&addr.sin_addr, MGCP_DEST_BTS);
+
+ /* FIXME: Get rid of CONN_ID_XXX! */
+ conn_net = mgcp_conn_get_rtp(endp, CONN_ID_NET);
+ if (!conn_net)
+ goto out;
+
if (!endp) {
LOGP(DLMGCP, LOGL_ERROR,
"Cannot find an endpoint for circuit_id=%d\n",
osmuxh->circuit_id);
goto out;
}
- endp->osmux.stats.octets += osmux_chunk_length(msg, rem);
- endp->osmux.stats.chunks++;
+ conn_net->osmux.stats.octets += osmux_chunk_length(msg, rem);
+ conn_net->osmux.stats.chunks++;
rem = msg->len;
- osmux_xfrm_output(osmuxh, &endp->osmux.out, &list);
+ osmux_xfrm_output(osmuxh, &conn_net->osmux.out, &list);
osmux_tx_sched(&list, scheduled_tx_net_cb, endp);
}
out:
@@ -432,116 +498,155 @@
return 0;
}
-int osmux_enable_endpoint(struct mgcp_endpoint *endp, struct in_addr *addr, uint16_t port)
+/*! enable OSXMUX circuit for a specified connection.
+ * \param[in] endp mgcp endpoint (configuration)
+ * \param[in] conn connection to disable
+ * \param[in] addr IP address of remote OSMUX endpoint
+ * \param[in] port portnumber of the remote OSMUX endpoint
+ * \returns 0 on success, -1 on ERROR */
+int osmux_enable_conn(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn,
+ struct in_addr *addr, uint16_t port)
{
- /* If osmux is enabled, initialize the output handler. This handler is
- * used to reconstruct the RTP flow from osmux. The RTP SSRC is
- * allocated based on the circuit ID (endp->osmux.cid), which is unique
- * in the local scope to the BSC/BSC-NAT. We use it to divide the RTP
- * SSRC space (2^32) by the 256 possible circuit IDs, then randomly
- * select one value from that window. Thus, we have no chance to have
- * overlapping RTP SSRC traveling to the BTSes behind the BSC,
- * similarly, for flows traveling to the MSC.
+ /*! If osmux is enabled, initialize the output handler. This handler is
+ * used to reconstruct the RTP flow from osmux. The RTP SSRC is
+ * allocated based on the circuit ID (conn_net->osmux.cid), which is unique
+ * in the local scope to the BSC/BSC-NAT. We use it to divide the RTP
+ * SSRC space (2^32) by the 256 possible circuit IDs, then randomly
+ * select one value from that window. Thus, we have no chance to have
+ * overlapping RTP SSRC traveling to the BTSes behind the BSC,
+ * similarly, for flows traveling to the MSC.
*/
static const uint32_t rtp_ssrc_winlen = UINT32_MAX / 256;
+ uint16_t osmux_dummy = endp->cfg->osmux_dummy;
- if (endp->osmux.state == OSMUX_STATE_DISABLED) {
- LOGP(DLMGCP, LOGL_ERROR, "Endpoint %u didn't request Osmux\n",
- ENDPOINT_NUMBER(endp));
+ /* Check if osmux is enabled for the specified connection */
+ if (conn->osmux.state == OSMUX_STATE_DISABLED) {
+ LOGP(DLMGCP, LOGL_ERROR, "OSMUX not enabled for conn:%s\n",
+ mgcp_conn_dump(conn->conn));
return -1;
}
- osmux_xfrm_output_init(&endp->osmux.out,
- (endp->osmux.cid * rtp_ssrc_winlen) +
+ osmux_xfrm_output_init(&conn->osmux.out,
+ (conn->osmux.cid * rtp_ssrc_winlen) +
(random() % rtp_ssrc_winlen));
- endp->osmux.in = osmux_handle_lookup(endp->cfg, addr, port);
- if (!endp->osmux.in) {
- LOGP(DLMGCP, LOGL_ERROR, "Cannot allocate input osmux handle\n");
+ conn->osmux.in = osmux_handle_lookup(endp->cfg, addr, port);
+ if (!conn->osmux.in) {
+ LOGP(DLMGCP, LOGL_ERROR, "Cannot allocate input osmux handle for conn:%s\n",
+ mgcp_conn_dump(conn->conn));
return -1;
}
- if (!osmux_xfrm_input_open_circuit(endp->osmux.in, endp->osmux.cid,
- endp->cfg->osmux_dummy)) {
- LOGP(DLMGCP, LOGL_ERROR, "Cannot open osmux circuit %u\n",
- endp->osmux.cid);
+ if (!osmux_xfrm_input_open_circuit(conn->osmux.in, conn->osmux.cid, osmux_dummy)) {
+ LOGP(DLMGCP, LOGL_ERROR, "Cannot open osmux circuit %u for conn:%s\n",
+ conn->osmux.cid, mgcp_conn_dump(conn->conn));
return -1;
}
switch (endp->cfg->role) {
case MGCP_BSC_NAT:
- endp->type = MGCP_OSMUX_BSC_NAT;
+ conn->type = MGCP_OSMUX_BSC_NAT;
break;
case MGCP_BSC:
- endp->type = MGCP_OSMUX_BSC;
+ conn->type = MGCP_OSMUX_BSC;
break;
}
- endp->osmux.state = OSMUX_STATE_ENABLED;
+
+ conn->osmux.state = OSMUX_STATE_ENABLED;
return 0;
}
-void osmux_disable_endpoint(struct mgcp_endpoint *endp)
+/*! disable OSXMUX circuit for a specified connection.
+ * \param[in] conn connection to disable */
+void osmux_disable_conn(struct mgcp_conn_rtp *conn)
{
- LOGP(DLMGCP, LOGL_INFO, "Releasing endpoint %u using Osmux CID %u\n",
- ENDPOINT_NUMBER(endp), endp->osmux.cid);
- osmux_xfrm_input_close_circuit(endp->osmux.in, endp->osmux.cid);
- endp->osmux.state = OSMUX_STATE_DISABLED;
- endp->osmux.cid = -1;
- osmux_handle_put(endp->osmux.in);
+ if (!conn)
+ return;
+
+ if (conn->osmux.state != OSMUX_STATE_ENABLED)
+ return;
+
+ LOGP(DLMGCP, LOGL_INFO, "Releasing connection %u using Osmux CID %u\n",
+ conn->conn->id, conn->osmux.cid);
+ osmux_xfrm_input_close_circuit(conn->osmux.in, conn->osmux.cid);
+ conn->osmux.state = OSMUX_STATE_DISABLED;
+ conn->osmux.cid = -1;
+ osmux_handle_put(conn->osmux.in);
}
-void osmux_release_cid(struct mgcp_endpoint *endp)
+/*! relase OSXMUX cid, that had been allocated to this connection.
+ * \param[in] conn connection with OSMUX cid to release */
+void osmux_release_cid(struct mgcp_conn_rtp *conn)
{
- if (endp->osmux.allocated_cid >= 0)
- osmux_put_cid(endp->osmux.allocated_cid);
- endp->osmux.allocated_cid = -1;
+ if (!conn)
+ return;
+
+ if (conn->osmux.state != OSMUX_STATE_ENABLED)
+ return;
+
+ if (conn->osmux.allocated_cid >= 0)
+ osmux_put_cid(conn->osmux.allocated_cid);
+ conn->osmux.allocated_cid = -1;
}
-void osmux_allocate_cid(struct mgcp_endpoint *endp)
+/*! allocate OSXMUX cid to connection.
+ * \param[in] conn connection for which we allocate the OSMUX cid*/
+void osmux_allocate_cid(struct mgcp_conn_rtp *conn)
{
- osmux_release_cid(endp);
- endp->osmux.allocated_cid = osmux_get_cid();
+ osmux_release_cid(conn);
+ conn->osmux.allocated_cid = osmux_get_cid();
}
-/* We don't need to send the dummy load for osmux so often as another endpoint
- * may have already punched the hole in the firewall. This approach is simple
- * though.
- */
-int osmux_send_dummy(struct mgcp_endpoint *endp)
+/*! send RTP dummy packet to OSMUX connection port.
+ * \param[in] endp mcgp endpoint that holds the RTP connection
+ * \param[in] conn associated RTP connection
+ * \returns bytes sent, -1 on error */
+int osmux_send_dummy(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn)
{
char buf[1 + sizeof(uint8_t)];
struct in_addr addr_unset = {};
+ /*! The dummy packet will not be sent via the actual OSMUX connection,
+ * instead it is sent out of band to port where the remote OSMUX
+ * multplexer is listening. The goal is to ensure that the connection
+ * is kept open */
+
+ /*! We don't need to send the dummy load for osmux so often as another
+ * endpoint may have already punched the hole in the firewall. This
+ * approach is simple though. */
+
buf[0] = MGCP_DUMMY_LOAD;
- memcpy(&buf[1], &endp->osmux.cid, sizeof(endp->osmux.cid));
+ memcpy(&buf[1], &conn->osmux.cid, sizeof(conn->osmux.cid));
/* Wait until we have the connection information from MDCX */
- if (memcmp(&endp->net_end.addr, &addr_unset, sizeof(addr_unset)) == 0)
+ if (memcmp(&conn->end.addr, &addr_unset, sizeof(addr_unset)) == 0)
return 0;
- if (endp->osmux.state == OSMUX_STATE_ACTIVATING) {
- if (osmux_enable_endpoint(endp, &endp->net_end.addr,
- htons(endp->cfg->osmux_port)) < 0) {
+ if (conn->osmux.state == OSMUX_STATE_ACTIVATING) {
+ if (osmux_enable_conn(endp, conn, &conn->end.addr,
+ htons(endp->cfg->osmux_port)) < 0) {
LOGP(DLMGCP, LOGL_ERROR,
- "Could not activate osmux in endpoint %d\n",
- ENDPOINT_NUMBER(endp));
+ "Could not activate osmux for conn:%s\n",
+ mgcp_conn_dump(conn->conn));
}
LOGP(DLMGCP, LOGL_ERROR,
"Osmux CID %u for %s:%u is now enabled\n",
- endp->osmux.cid, inet_ntoa(endp->net_end.addr),
+ conn->osmux.cid, inet_ntoa(conn->end.addr),
endp->cfg->osmux_port);
}
LOGP(DLMGCP, LOGL_DEBUG,
"sending OSMUX dummy load to %s CID %u\n",
- inet_ntoa(endp->net_end.addr), endp->osmux.cid);
+ inet_ntoa(conn->end.addr), conn->osmux.cid);
- return mgcp_udp_send(osmux_fd.fd, &endp->net_end.addr,
+ return mgcp_udp_send(osmux_fd.fd, &conn->end.addr,
htons(endp->cfg->osmux_port), buf, sizeof(buf));
}
-/* bsc-nat allocates/releases the Osmux circuit ID */
+/*! bsc-nat allocates/releases the OSMUX cids (Circuit IDs). */
static uint8_t osmux_cid_bitmap[(OSMUX_CID_MAX + 1) / 8];
+/*! count the number of taken OSMUX cids.
+ * \returns number of OSMUX cids in use */
int osmux_used_cid(void)
{
int i, j, used = 0;
@@ -556,6 +661,8 @@
return used;
}
+/*! take a free OSMUX cid.
+ * \returns OSMUX cid */
int osmux_get_cid(void)
{
int i, j;
@@ -576,6 +683,8 @@
return -1;
}
+/*! put back a no longer used OSMUX cid.
+ * \param[in] osmux_cid OSMUX cid */
void osmux_put_cid(uint8_t osmux_cid)
{
LOGP(DLMGCP, LOGL_DEBUG, "Osmux CID %u is back to the pool\n", osmux_cid);
diff --git a/src/libosmo-mgcp/mgcp_protocol.c b/src/libosmo-mgcp/mgcp_protocol.c
index bc1ec0d..ac25894 100644
--- a/src/libosmo-mgcp/mgcp_protocol.c
+++ b/src/libosmo-mgcp/mgcp_protocol.c
@@ -33,19 +33,15 @@
#include <osmocom/core/talloc.h>
#include <osmocom/core/select.h>
-#include <osmocom/legacy_mgcp/mgcp.h>
-#include <osmocom/legacy_mgcp/mgcp_internal.h>
-
-#define for_each_non_empty_line(line, save) \
- for (line = strtok_r(NULL, "\r\n", &save); line;\
- line = strtok_r(NULL, "\r\n", &save))
-
-
-static void mgcp_rtp_end_reset(struct mgcp_rtp_end *end);
+#include <osmocom/mgcp/mgcp.h>
+#include <osmocom/mgcp/mgcp_internal.h>
+#include <osmocom/mgcp/mgcp_stat.h>
+#include <osmocom/mgcp/mgcp_msg.h>
+#include <osmocom/mgcp/mgcp_ep.h>
struct mgcp_request {
char *name;
- struct msgb *(*handle_request) (struct mgcp_parse_data *data);
+ struct msgb *(*handle_request) (struct mgcp_parse_data * data);
char *debug_name;
};
@@ -59,111 +55,67 @@
static struct msgb *handle_rsip(struct mgcp_parse_data *data);
static struct msgb *handle_noti_req(struct mgcp_parse_data *data);
-static void create_transcoder(struct mgcp_endpoint *endp);
-static void delete_transcoder(struct mgcp_endpoint *endp);
-
-static int setup_rtp_processing(struct mgcp_endpoint *endp);
-
-static int mgcp_analyze_header(struct mgcp_parse_data *parse, char *data);
-
-/* Display an mgcp message on the log output */
-void display_mgcp_message(unsigned char *message, unsigned int len,
- char *preamble)
+/* Initalize transcoder */
+static int setup_rtp_processing(struct mgcp_endpoint *endp,
+ struct mgcp_conn_rtp *conn)
{
- unsigned char line[80];
- unsigned char *ptr;
- unsigned int consumed = 0;
- unsigned int consumed_line = 0;
- unsigned int line_count = 0;
+ struct mgcp_config *cfg = endp->cfg;
+ struct mgcp_conn_rtp *conn_src = NULL;
+ struct mgcp_conn_rtp *conn_dst = conn;
+ struct mgcp_conn *_conn;
- if (!log_check_level(DLMGCP, LOGL_DEBUG))
- return;
-
- while (1) {
- memset(line, 0, sizeof(line));
- ptr = line;
- consumed_line = 0;
- do {
- if (*message != '\n' && *message != '\r') {
- *ptr = *message;
- ptr++;
- }
- message++;
- consumed++;
- consumed_line++;
- } while (*message != '\n' && consumed < len
- && consumed_line < sizeof(line));
-
- if (strlen((const char *)line)) {
- LOGP(DLMGCP, LOGL_DEBUG, "%s: line #%02u: %s\n",
- preamble, line_count, line);
- line_count++;
- }
-
- if (consumed >= len)
- return;
- }
-}
-
-static int mgcp_check_param(const struct mgcp_endpoint *endp, const char *line)
-{
- const size_t line_len = strlen(line);
- if (line[0] != '\0' && line_len < 2) {
- LOGP(DLMGCP, LOGL_ERROR,
- "Wrong MGCP option format: '%s' on 0x%x\n",
- line, ENDPOINT_NUMBER(endp));
+ if (conn->type != MGCP_RTP_DEFAULT) {
+ LOGP(DLMGCP, LOGL_NOTICE,
+ "endpoint:%x RTP-setup: Endpoint is not configured as RTP default, stopping here!\n",
+ ENDPOINT_NUMBER(endp));
return 0;
}
- return 1;
+ if (conn->conn->mode == MGCP_CONN_LOOPBACK) {
+ LOGP(DLMGCP, LOGL_NOTICE,
+ "endpoint:%x RTP-setup: Endpoint is in loopback mode, stopping here!\n",
+ ENDPOINT_NUMBER(endp));
+ return 0;
+ }
+
+ /* Find the "sister" connection */
+ llist_for_each_entry(_conn, &endp->conns, entry) {
+ if (_conn->id != conn->conn->id) {
+ conn_src = &_conn->u.rtp;
+ break;
+ }
+ }
+
+ return cfg->setup_rtp_processing_cb(endp, &conn_dst->end,
+ &conn_src->end);
}
-static uint32_t generate_call_id(struct mgcp_config *cfg)
-{
- int i;
-
- /* use the call id */
- ++cfg->last_call_id;
-
- /* handle wrap around */
- if (cfg->last_call_id == CI_UNUSED)
- ++cfg->last_call_id;
-
- /* callstack can only be of size number_of_endpoints */
- /* verify that the call id is free, e.g. in case of overrun */
- for (i = 1; i < cfg->trunk.number_endpoints; ++i)
- if (cfg->trunk.endpoints[i].ci == cfg->last_call_id)
- return generate_call_id(cfg);
-
- return cfg->last_call_id;
-}
-
-/*
- * array of function pointers for handling various
+/* array of function pointers for handling various
* messages. In the future this might be binary sorted
- * for performance reasons.
- */
-static const struct mgcp_request mgcp_requests [] = {
+ * for performance reasons. */
+static const struct mgcp_request mgcp_requests[] = {
MGCP_REQUEST("AUEP", handle_audit_endpoint, "AuditEndpoint")
- MGCP_REQUEST("CRCX", handle_create_con, "CreateConnection")
- MGCP_REQUEST("DLCX", handle_delete_con, "DeleteConnection")
- MGCP_REQUEST("MDCX", handle_modify_con, "ModifiyConnection")
- MGCP_REQUEST("RQNT", handle_noti_req, "NotificationRequest")
+ MGCP_REQUEST("CRCX", handle_create_con, "CreateConnection")
+ MGCP_REQUEST("DLCX", handle_delete_con, "DeleteConnection")
+ MGCP_REQUEST("MDCX", handle_modify_con, "ModifiyConnection")
+ MGCP_REQUEST("RQNT", handle_noti_req, "NotificationRequest")
- /* SPEC extension */
- MGCP_REQUEST("RSIP", handle_rsip, "ReSetInProgress")
+ /* SPEC extension */
+ MGCP_REQUEST("RSIP", handle_rsip, "ReSetInProgress")
};
+/* Helper function to allocate some memory for responses and retransmissions */
static struct msgb *mgcp_msgb_alloc(void)
{
struct msgb *msg;
msg = msgb_alloc_headroom(4096, 128, "MGCP msg");
if (!msg)
- LOGP(DLMGCP, LOGL_ERROR, "Failed to msgb for MGCP data.\n");
+ LOGP(DLMGCP, LOGL_ERROR, "Failed to msgb for MGCP data.\n");
return msg;
}
+/* Helper function for do_retransmission() and create_resp() */
static struct msgb *do_retransmission(const struct mgcp_endpoint *endp)
{
struct msgb *msg = mgcp_msgb_alloc();
@@ -172,7 +124,7 @@
msg->l2h = msgb_put(msg, strlen(endp->last_response));
memcpy(msg->l2h, endp->last_response, msgb_l2len(msg));
- display_mgcp_message(msg->l2h, msgb_l2len(msg), "Retransmitted response");
+ mgcp_disp_msg(msg->l2h, msgb_l2len(msg), "Retransmitted response");
return msg;
}
@@ -188,8 +140,8 @@
if (!res)
return NULL;
- len = snprintf((char *) res->data, 2048, "%d %s%s%s\r\n%s",
- code, trans, txt, param ? param : "", sdp ? sdp : "");
+ len = snprintf((char *)res->data, 2048, "%d %s%s%s\r\n%s",
+ code, trans, txt, param ? param : "", sdp ? sdp : "");
if (len < 0) {
LOGP(DLMGCP, LOGL_ERROR, "Failed to sprintf MGCP response.\n");
msgb_free(res);
@@ -198,7 +150,7 @@
res->l2h = msgb_put(res, len);
LOGP(DLMGCP, LOGL_DEBUG, "Generated response: code=%d\n", code);
- display_mgcp_message(res->l2h, msgb_l2len(res), "Generated response");
+ mgcp_disp_msg(res->l2h, msgb_l2len(res), "Generated response");
/*
* Remember the last transmission per endpoint.
@@ -209,33 +161,37 @@
talloc_free(endp->last_trans);
endp->last_trans = talloc_strdup(tcfg->endpoints, trans);
endp->last_response = talloc_strndup(tcfg->endpoints,
- (const char *) res->l2h,
- msgb_l2len(res));
+ (const char *)res->l2h,
+ msgb_l2len(res));
}
return res;
}
static struct msgb *create_ok_resp_with_param(struct mgcp_endpoint *endp,
- int code, const char *msg,
- const char *trans, const char *param)
+ int code, const char *msg,
+ const char *trans,
+ const char *param)
{
return create_resp(endp, code, " OK", msg, trans, param, NULL);
}
static struct msgb *create_ok_response(struct mgcp_endpoint *endp,
- int code, const char *msg, const char *trans)
+ int code, const char *msg,
+ const char *trans)
{
return create_ok_resp_with_param(endp, code, msg, trans, NULL);
}
static struct msgb *create_err_response(struct mgcp_endpoint *endp,
- int code, const char *msg, const char *trans)
+ int code, const char *msg,
+ const char *trans)
{
return create_resp(endp, code, " FAIL", msg, trans, NULL, NULL);
}
static int write_response_sdp(struct mgcp_endpoint *endp,
+ struct mgcp_conn_rtp *conn,
char *sdp_record, size_t size, const char *addr)
{
const char *fmtp_extra;
@@ -244,16 +200,18 @@
int len;
int nchars;
+ if (!conn)
+ return -1;
+
endp->cfg->get_net_downlink_format_cb(endp, &payload_type,
- &audio_name, &fmtp_extra);
+ &audio_name, &fmtp_extra, conn);
len = snprintf(sdp_record, size,
- "v=0\r\n"
- "o=- %u 23 IN IP4 %s\r\n"
- "s=-\r\n"
- "c=IN IP4 %s\r\n"
- "t=0 0\r\n",
- endp->ci, addr, addr);
+ "v=0\r\n"
+ "o=- %u 23 IN IP4 %s\r\n"
+ "s=-\r\n"
+ "c=IN IP4 %s\r\n"
+ "t=0 0\r\n", conn->conn->id, addr, addr);
if (len < 0 || len >= size)
goto buffer_too_small;
@@ -261,7 +219,7 @@
if (payload_type >= 0) {
nchars = snprintf(sdp_record + len, size - len,
"m=audio %d RTP/AVP %d\r\n",
- endp->net_end.local_port, payload_type);
+ conn->end.local_port, payload_type);
if (nchars < 0 || nchars >= size - len)
goto buffer_too_small;
@@ -288,10 +246,10 @@
len += nchars;
}
}
- if (endp->bts_end.packet_duration_ms > 0 && endp->tcfg->audio_send_ptime) {
+ if (conn->end.packet_duration_ms > 0 && endp->tcfg->audio_send_ptime) {
nchars = snprintf(sdp_record + len, size - len,
- "a=ptime:%d\r\n",
- endp->bts_end.packet_duration_ms);
+ "a=ptime:%u\r\n",
+ conn->end.packet_duration_ms);
if (nchars < 0 || nchars >= size - len)
goto buffer_too_small;
@@ -306,8 +264,11 @@
return -1;
}
+/* Format MGCP response string (with SDP attached) */
static struct msgb *create_response_with_sdp(struct mgcp_endpoint *endp,
- const char *msg, const char *trans_id)
+ struct mgcp_conn_rtp *conn,
+ const char *msg,
+ const char *trans_id)
{
const char *addr = endp->cfg->local_ip;
char sdp_record[4096];
@@ -318,19 +279,19 @@
if (!addr)
addr = mgcp_net_src_addr(endp);
- if (endp->osmux.state == OSMUX_STATE_NEGOTIATING) {
- sprintf(osmux_extension, "\nX-Osmux: %u", endp->osmux.cid);
- endp->osmux.state = OSMUX_STATE_ACTIVATING;
+ if (conn->osmux.state == OSMUX_STATE_NEGOTIATING) {
+ sprintf(osmux_extension, "\nX-Osmux: %u", conn->osmux.cid);
+ conn->osmux.state = OSMUX_STATE_ACTIVATING;
} else {
osmux_extension[0] = '\0';
}
len = snprintf(sdp_record, sizeof(sdp_record),
- "I: %u%s\n\n", endp->ci, osmux_extension);
+ "I: %u%s\n\n", conn->conn->id, osmux_extension);
if (len < 0)
return NULL;
- nchars = write_response_sdp(endp, sdp_record + len,
+ nchars = write_response_sdp(endp, conn, sdp_record + len,
sizeof(sdp_record) - len - 1, addr);
if (nchars < 0)
return NULL;
@@ -342,19 +303,19 @@
return create_resp(endp, 200, " OK", msg, trans_id, NULL, sdp_record);
}
-static void send_dummy(struct mgcp_endpoint *endp)
+/* Send out dummy packet to keep the connection open, if the connection is an
+ * osmux connection, send the dummy packet via OSMUX */
+static void send_dummy(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn)
{
- if (endp->osmux.state != OSMUX_STATE_DISABLED)
- osmux_send_dummy(endp);
+ if (conn->osmux.state != OSMUX_STATE_DISABLED)
+ osmux_send_dummy(endp, conn);
else
- mgcp_send_dummy(endp);
+ mgcp_send_dummy(endp, conn);
}
-/*
- * handle incoming messages:
+/* handle incoming messages:
* - this can be a command (four letters, space, transaction id)
- * - or a response (three numbers, space, transaction id)
- */
+ * - or a response (three numbers, space, transaction id) */
struct msgb *mgcp_handle_message(struct mgcp_config *cfg, struct msgb *msg)
{
struct mgcp_parse_data pdata;
@@ -370,32 +331,33 @@
if (mgcp_msg_terminate_nul(msg))
return NULL;
- display_mgcp_message(msg->l2h, msgb_l2len(msg), "Received message");
+ mgcp_disp_msg(msg->l2h, msgb_l2len(msg), "Received message");
- /* attempt to treat it as a response */
- if (sscanf((const char *)&msg->l2h[0], "%3d %*s", &code) == 1) {
+ /* attempt to treat it as a response */
+ if (sscanf((const char *)&msg->l2h[0], "%3d %*s", &code) == 1) {
LOGP(DLMGCP, LOGL_DEBUG, "Response: Code: %d\n", code);
return NULL;
}
msg->l3h = &msg->l2h[4];
-
/*
* Check for a duplicate message and respond.
*/
memset(&pdata, 0, sizeof(pdata));
pdata.cfg = cfg;
- data = strline_r((char *) msg->l3h, &pdata.save);
- pdata.found = mgcp_analyze_header(&pdata, data);
+ data = mgcp_strline((char *)msg->l3h, &pdata.save);
+ pdata.found = mgcp_parse_header(&pdata, data);
if (pdata.endp && pdata.trans
- && pdata.endp->last_trans
- && strcmp(pdata.endp->last_trans, pdata.trans) == 0) {
+ && pdata.endp->last_trans
+ && strcmp(pdata.endp->last_trans, pdata.trans) == 0) {
return do_retransmission(pdata.endp);
}
for (i = 0; i < ARRAY_SIZE(mgcp_requests); ++i) {
- if (strncmp(mgcp_requests[i].name, (const char *) &msg->l2h[0], 4) == 0) {
+ if (strncmp
+ (mgcp_requests[i].name, (const char *)&msg->l2h[0],
+ 4) == 0) {
handled = 1;
resp = mgcp_requests[i].handle_request(&pdata);
break;
@@ -403,201 +365,40 @@
}
if (!handled)
- LOGP(DLMGCP, LOGL_NOTICE, "MSG with type: '%.4s' not handled\n", &msg->l2h[0]);
+ LOGP(DLMGCP, LOGL_NOTICE, "MSG with type: '%.4s' not handled\n",
+ &msg->l2h[0]);
return resp;
}
-/**
- * We have a null terminated string with the endpoint name here. We only
- * support two kinds. Simple ones as seen on the BSC level and the ones
- * seen on the trunk side.
- */
-static struct mgcp_endpoint *find_e1_endpoint(struct mgcp_config *cfg,
- const char *mgcp)
-{
- char *rest = NULL;
- struct mgcp_trunk_config *tcfg;
- int trunk, endp;
-
- trunk = strtoul(mgcp + 6, &rest, 10);
- if (rest == NULL || rest[0] != '/' || trunk < 1) {
- LOGP(DLMGCP, LOGL_ERROR, "Wrong trunk name '%s'\n", mgcp);
- return NULL;
- }
-
- endp = strtoul(rest + 1, &rest, 10);
- if (rest == NULL || rest[0] != '@') {
- LOGP(DLMGCP, LOGL_ERROR, "Wrong endpoint name '%s'\n", mgcp);
- return NULL;
- }
-
- /* signalling is on timeslot 1 */
- if (endp == 1)
- return NULL;
-
- tcfg = mgcp_trunk_num(cfg, trunk);
- if (!tcfg) {
- LOGP(DLMGCP, LOGL_ERROR, "The trunk %d is not declared.\n", trunk);
- return NULL;
- }
-
- if (!tcfg->endpoints) {
- LOGP(DLMGCP, LOGL_ERROR, "Endpoints of trunk %d not allocated.\n", trunk);
- return NULL;
- }
-
- if (endp < 1 || endp >= tcfg->number_endpoints) {
- LOGP(DLMGCP, LOGL_ERROR, "Failed to find endpoint '%s'\n", mgcp);
- return NULL;
- }
-
- return &tcfg->endpoints[endp];
-}
-
-static struct mgcp_endpoint *find_endpoint(struct mgcp_config *cfg, const char *mgcp)
-{
- char *endptr = NULL;
- unsigned int gw = INT_MAX;
-
- if (strncmp(mgcp, "ds/e1", 5) == 0)
- return find_e1_endpoint(cfg, mgcp);
-
- gw = strtoul(mgcp, &endptr, 16);
- if (gw > 0 && gw < cfg->trunk.number_endpoints && endptr[0] == '@')
- return &cfg->trunk.endpoints[gw];
-
- LOGP(DLMGCP, LOGL_ERROR, "Not able to find the endpoint: '%s'\n", mgcp);
- return NULL;
-}
-
-/**
- * @returns 0 when the status line was complete and transaction_id and
- * endp out parameters are set.
- */
-static int mgcp_analyze_header(struct mgcp_parse_data *pdata, char *data)
-{
- int i = 0;
- char *elem, *save = NULL;
-
- OSMO_ASSERT(data);
- pdata->trans = "000000";
-
- for (elem = strtok_r(data, " ", &save); elem;
- elem = strtok_r(NULL, " ", &save)) {
- switch (i) {
- case 0:
- pdata->trans = elem;
- break;
- case 1:
- pdata->endp = find_endpoint(pdata->cfg, elem);
- if (!pdata->endp) {
- LOGP(DLMGCP, LOGL_ERROR,
- "Unable to find Endpoint `%s'\n", elem);
- return -1;
- }
- break;
- case 2:
- if (strcmp("MGCP", elem)) {
- LOGP(DLMGCP, LOGL_ERROR,
- "MGCP header parsing error\n");
- return -1;
- }
- break;
- case 3:
- if (strcmp("1.0", elem)) {
- LOGP(DLMGCP, LOGL_ERROR, "MGCP version `%s' "
- "not supported\n", elem);
- return -1;
- }
- break;
- }
- i++;
- }
-
- if (i != 4) {
- LOGP(DLMGCP, LOGL_ERROR, "MGCP status line too short.\n");
- pdata->trans = "000000";
- pdata->endp = NULL;
- return -1;
- }
-
- return 0;
-}
-
-static int verify_call_id(const struct mgcp_endpoint *endp,
- const char *callid)
-{
- if (strcmp(endp->callid, callid) != 0) {
- LOGP(DLMGCP, LOGL_ERROR, "CallIDs does not match on 0x%x. '%s' != '%s'\n",
- ENDPOINT_NUMBER(endp), endp->callid, callid);
- return -1;
- }
-
- return 0;
-}
-
-static int verify_ci(const struct mgcp_endpoint *endp,
- const char *_ci)
-{
- uint32_t ci = strtoul(_ci, NULL, 10);
-
- if (ci != endp->ci) {
- LOGP(DLMGCP, LOGL_ERROR, "ConnectionIdentifiers do not match on 0x%x. %u != %s\n",
- ENDPOINT_NUMBER(endp), endp->ci, _ci);
- return -1;
- }
-
- return 0;
-}
-
+/* AUEP command handler, processes the received command */
static struct msgb *handle_audit_endpoint(struct mgcp_parse_data *p)
{
- if (p->found != 0)
+ LOGP(DLMGCP, LOGL_NOTICE, "AUEP: auditing endpoint ...\n");
+
+ if (p->found != 0) {
+ LOGP(DLMGCP, LOGL_ERROR,
+ "AUEP: failed to find the endpoint.\n");
return create_err_response(NULL, 500, "AUEP", p->trans);
- else
+ } else
return create_ok_response(p->endp, 200, "AUEP", p->trans);
}
-static int parse_conn_mode(const char *msg, struct mgcp_endpoint *endp)
-{
- int ret = 0;
- if (strcmp(msg, "recvonly") == 0)
- endp->conn_mode = MGCP_CONN_RECV_ONLY;
- else if (strcmp(msg, "sendrecv") == 0)
- endp->conn_mode = MGCP_CONN_RECV_SEND;
- else if (strcmp(msg, "sendonly") == 0)
- endp->conn_mode = MGCP_CONN_SEND_ONLY;
- else if (strcmp(msg, "loopback") == 0)
- endp->conn_mode = MGCP_CONN_LOOPBACK;
- else {
- LOGP(DLMGCP, LOGL_ERROR, "Unknown connection mode: '%s'\n", msg);
- ret = -1;
- }
-
- endp->net_end.output_enabled =
- endp->conn_mode & MGCP_CONN_SEND_ONLY ? 1 : 0;
- endp->bts_end.output_enabled =
- endp->conn_mode & MGCP_CONN_RECV_ONLY ? 1 : 0;
-
- LOGP(DLMGCP, LOGL_DEBUG, "endpoint %x connection mode '%s' %d output_enabled net %d bts %d\n",
- ENDPOINT_NUMBER(endp),
- msg, endp->conn_mode, endp->net_end.output_enabled,
- endp->bts_end.output_enabled);
-
- return ret;
-}
-
-static int allocate_port(struct mgcp_endpoint *endp, struct mgcp_rtp_end *end,
- struct mgcp_port_range *range,
- int (*alloc)(struct mgcp_endpoint *endp, int port))
+/* Try to find a free port by attemting to bind on it. Also handle the
+ * counter that points on the next free port. Since we have a pointer
+ * to the next free port, binding should work on the first attemt,
+ * neverless, try at least the next 200 ports before giving up */
+static int allocate_port(struct mgcp_endpoint *endp, struct mgcp_conn_rtp *conn)
{
int i;
+ struct mgcp_rtp_end *end;
+ struct mgcp_port_range *range;
- if (range->mode == PORT_ALLOC_STATIC) {
- end->local_alloc = PORT_ALLOC_STATIC;
- return 0;
- }
+ OSMO_ASSERT(conn);
+ end = &conn->end;
+ OSMO_ASSERT(end);
+
+ range = &endp->cfg->net_ports;
/* attempt to find a port */
for (i = 0; i < 200; ++i) {
@@ -606,60 +407,23 @@
if (range->last_port >= range->range_end)
range->last_port = range->range_start;
- rc = alloc(endp, range->last_port);
+ rc = mgcp_bind_net_rtp_port(endp, range->last_port, conn);
range->last_port += 2;
if (rc == 0) {
- end->local_alloc = PORT_ALLOC_DYNAMIC;
return 0;
}
}
- LOGP(DLMGCP, LOGL_ERROR, "Allocating a RTP/RTCP port failed 200 times 0x%x.\n",
+ LOGP(DLMGCP, LOGL_ERROR,
+ "Allocating a RTP/RTCP port failed 200 times 0x%x.\n",
ENDPOINT_NUMBER(endp));
return -1;
}
-static int allocate_ports(struct mgcp_endpoint *endp)
-{
- if (allocate_port(endp, &endp->net_end, &endp->cfg->net_ports,
- mgcp_bind_net_rtp_port) != 0)
- return -1;
-
- if (allocate_port(endp, &endp->bts_end, &endp->cfg->bts_ports,
- mgcp_bind_bts_rtp_port) != 0) {
- mgcp_rtp_end_reset(&endp->net_end);
- return -1;
- }
-
- if (endp->cfg->transcoder_ip && endp->tcfg->trunk_type == MGCP_TRUNK_VIRTUAL) {
- if (allocate_port(endp, &endp->trans_net,
- &endp->cfg->transcoder_ports,
- mgcp_bind_trans_net_rtp_port) != 0) {
- mgcp_rtp_end_reset(&endp->net_end);
- mgcp_rtp_end_reset(&endp->bts_end);
- return -1;
- }
-
- if (allocate_port(endp, &endp->trans_bts,
- &endp->cfg->transcoder_ports,
- mgcp_bind_trans_bts_rtp_port) != 0) {
- mgcp_rtp_end_reset(&endp->net_end);
- mgcp_rtp_end_reset(&endp->bts_end);
- mgcp_rtp_end_reset(&endp->trans_net);
- return -1;
- }
-
- /* remember that we have set up transcoding */
- endp->type = MGCP_RTP_TRANSCODED;
- }
-
- return 0;
-}
-
/* Set the LCO from a string (see RFC 3435).
- * The string is stored in the 'string' field. A NULL string is handled excatly
+ * The string is stored in the 'string' field. A NULL string is handled excatlyy
* like an empty string, the 'string' field is never NULL after this function
* has been called. */
static void set_local_cx_options(void *ctx, struct mgcp_lco *lco,
@@ -682,6 +446,10 @@
a_opt = strstr(lco->string, "a:");
if (a_opt && sscanf(a_opt, "a:%8[^,]", codec) == 1)
lco->codec = talloc_strdup(ctx, codec);
+
+ LOGP(DLMGCP, LOGL_DEBUG,
+ "local CX options: lco->pkt_period_max: %i, lco->codec: %s\n",
+ lco->pkt_period_max, lco->codec);
}
void mgcp_rtp_end_config(struct mgcp_endpoint *endp, int expect_ssrc_change,
@@ -711,28 +479,13 @@
f = rtp->frames_per_packet;
else if (rtp->packet_duration_ms && rtp->codec.frame_duration_num) {
int den = 1000 * rtp->codec.frame_duration_num;
- f = (rtp->packet_duration_ms * rtp->codec.frame_duration_den + den/2)
- / den;
+ f = (rtp->packet_duration_ms * rtp->codec.frame_duration_den +
+ den / 2)
+ / den;
}
- return rtp->codec.rate * f * rtp->codec.frame_duration_num / rtp->codec.frame_duration_den;
-}
-
-static int mgcp_parse_osmux_cid(const char *line)
-{
- int osmux_cid;
-
- if (sscanf(line + 2, "Osmux: %u", &osmux_cid) != 1)
- return -1;
-
- if (osmux_cid > OSMUX_CID_MAX) {
- LOGP(DLMGCP, LOGL_ERROR, "Osmux ID too large: %u > %u\n",
- osmux_cid, OSMUX_CID_MAX);
- return -1;
- }
- LOGP(DLMGCP, LOGL_DEBUG, "bsc-nat offered Osmux CID %u\n", osmux_cid);
-
- return osmux_cid;
+ return rtp->codec.rate * f * rtp->codec.frame_duration_num /
+ rtp->codec.frame_duration_den;
}
static int mgcp_osmux_setup(struct mgcp_endpoint *endp, const char *line)
@@ -748,6 +501,7 @@
return mgcp_parse_osmux_cid(line);
}
+/* CRCX command handler, processes the received command */
static struct msgb *handle_create_con(struct mgcp_parse_data *p)
{
struct mgcp_trunk_config *tcfg;
@@ -756,9 +510,15 @@
const char *local_options = NULL;
const char *callid = NULL;
+ const char *ci = NULL;
const char *mode = NULL;
char *line;
int have_sdp = 0, osmux_cid = -1;
+ struct mgcp_conn_rtp *conn = NULL;
+ uint32_t conn_id;
+ char conn_name[512];
+
+ LOGP(DLMGCP, LOGL_NOTICE, "CRCX: creating new connection ...\n");
if (p->found != 0)
return create_err_response(NULL, 510, "CRCX", p->trans);
@@ -770,30 +530,32 @@
switch (line[0]) {
case 'L':
- local_options = (const char *) line + 3;
+ local_options = (const char *)line + 3;
break;
case 'C':
- callid = (const char *) line + 3;
+ callid = (const char *)line + 3;
+ break;
+ case 'I':
+ ci = (const char *)line + 3;
break;
case 'M':
- mode = (const char *) line + 3;
+ mode = (const char *)line + 3;
break;
case 'X':
- /* Osmux is not enabled in this bsc, ignore it so the
- * bsc-nat knows that we don't want to use Osmux.
- */
+ /* If osmoux is disabled, just skip setting it up */
if (!p->endp->cfg->osmux)
break;
-
- if (strncmp("Osmux: ", line + 2, strlen("Osmux: ")) == 0)
+ if (strncmp("Osmux: ", line + 2, strlen("Osmux: ")) ==
+ 0)
osmux_cid = mgcp_osmux_setup(endp, line);
break;
case '\0':
have_sdp = 1;
goto mgcp_header_done;
default:
- LOGP(DLMGCP, LOGL_NOTICE, "Unhandled option: '%c'/%d on 0x%x\n",
- *line, *line, ENDPOINT_NUMBER(endp));
+ LOGP(DLMGCP, LOGL_NOTICE,
+ "CRCX: endpoint:%x unhandled option: '%c'/%d\n",
+ ENDPOINT_NUMBER(endp), *line, *line);
break;
}
}
@@ -801,103 +563,165 @@
mgcp_header_done:
tcfg = p->endp->tcfg;
- /* Check required data */
- if (!callid || !mode) {
- LOGP(DLMGCP, LOGL_ERROR, "Missing callid and mode in CRCX on 0x%x\n",
+ /* Check parameters */
+ if (!callid) {
+ LOGP(DLMGCP, LOGL_ERROR,
+ "CRCX: endpoint:%x insufficient parameters, missing callid\n",
ENDPOINT_NUMBER(endp));
return create_err_response(endp, 400, "CRCX", p->trans);
}
- if (endp->allocated) {
+ if (!mode) {
+ LOGP(DLMGCP, LOGL_ERROR,
+ "CRCX: endpoint:%x insufficient parameters, missing mode\n",
+ ENDPOINT_NUMBER(endp));
+ return create_err_response(endp, 400, "CRCX", p->trans);
+ }
+
+ if (!ci) {
+ LOGP(DLMGCP, LOGL_ERROR,
+ "CRCX: endpoint:%x insufficient parameters, missing connection id\n",
+ ENDPOINT_NUMBER(endp));
+ return create_err_response(endp, 400, "CRCX", p->trans);
+ }
+
+ /* Check if we are able to accept the creation of another connection */
+ if (llist_count(&endp->conns) >= endp->type->max_conns) {
+ LOGP(DLMGCP, LOGL_ERROR,
+ "CRCX: endpoint:%x endpoint full, max. %i connections allowed!\n",
+ endp->type->max_conns, ENDPOINT_NUMBER(endp));
if (tcfg->force_realloc) {
- LOGP(DLMGCP, LOGL_NOTICE, "Endpoint 0x%x already allocated. Forcing realloc.\n",
- ENDPOINT_NUMBER(endp));
- mgcp_release_endp(endp);
- if (p->cfg->realloc_cb)
- p->cfg->realloc_cb(tcfg, ENDPOINT_NUMBER(endp));
+ /* There is no more room for a connection, make some
+ * room by blindly tossing the oldest of the two two
+ * connections */
+ mgcp_conn_free_oldest(endp);
} else {
- LOGP(DLMGCP, LOGL_ERROR, "Endpoint is already used. 0x%x\n",
- ENDPOINT_NUMBER(endp));
+ /* There is no more room for a connection, leave
+ * everything as it is and return with an error */
return create_err_response(endp, 400, "CRCX", p->trans);
}
}
- /* copy some parameters */
+ /* Check if this endpoint already serves a call, if so, check if the
+ * callids match up so that we are sure that this is our call */
+ if (endp->callid && mgcp_verify_call_id(endp, callid)) {
+ LOGP(DLMGCP, LOGL_ERROR,
+ "CRCX: endpoint:%x allready seized by other call (%s)\n",
+ ENDPOINT_NUMBER(endp), endp->callid);
+ if (tcfg->force_realloc)
+ /* This is not our call, toss everything by releasing
+ * the entire endpoint. (rude!) */
+ mgcp_release_endp(endp);
+ else {
+ /* This is not our call, leave everything as it is and
+ * return with an error. */
+ return create_err_response(endp, 400, "CRCX", p->trans);
+ }
+ }
+
+ /* Set the callid, creation of another connection will only be possible
+ * when the callid matches up. (Connections are distinuished by their
+ * connection ids) */
endp->callid = talloc_strdup(tcfg->endpoints, callid);
+ /* Extract audio codec information */
set_local_cx_options(endp->tcfg->endpoints, &endp->local_options,
local_options);
- if (parse_conn_mode(mode, endp) != 0) {
- error_code = 517;
- goto error2;
+ if (mgcp_parse_ci(&conn_id, ci)) {
+ LOGP(DLMGCP, LOGL_ERROR,
+ "CRCX: endpoint:%x insufficient parameters, missing ci (connectionIdentifier)\n",
+ ENDPOINT_NUMBER(endp));
+ return create_err_response(endp, 400, "CRCX", p->trans);
}
- /* initialize */
- endp->net_end.rtp_port = endp->net_end.rtcp_port = endp->bts_end.rtp_port = endp->bts_end.rtcp_port = 0;
- mgcp_rtp_end_config(endp, 0, &endp->net_end);
- mgcp_rtp_end_config(endp, 0, &endp->bts_end);
+ /* Only accept another connection when the connection ID is different. */
+ if (mgcp_conn_get_rtp(endp, conn_id)) {
+ LOGP(DLMGCP, LOGL_ERROR,
+ "CRCX: endpoint:%x there is already a connection with id %u present!\n",
+ conn_id, ENDPOINT_NUMBER(endp));
+ if (tcfg->force_realloc) {
+ /* Ignore the existing connection by just freeing it */
+ mgcp_conn_free(endp, conn_id);
+ } else {
+ /* There is already a connection with that ID present,
+ * leave everything as it is and return with an error. */
+ return create_err_response(endp, 400, "CRCX", p->trans);
+ }
+ }
- /* set to zero until we get the info */
- memset(&endp->net_end.addr, 0, sizeof(endp->net_end.addr));
-
- /* bind to the port now */
- if (allocate_ports(endp) != 0)
+ snprintf(conn_name, sizeof(conn_name), "%s-%u", callid, conn_id);
+ mgcp_conn_alloc(NULL, endp, conn_id, MGCP_CONN_TYPE_RTP,
+ conn_name);
+ conn = mgcp_conn_get_rtp(endp, conn_id);
+ if (!conn) {
+ LOGP(DLMGCP, LOGL_ERROR,
+ "CRCX: endpoint:%x unable to allocate RTP connection\n",
+ ENDPOINT_NUMBER(endp));
goto error2;
- /* assign a local call identifier or fail */
- endp->ci = generate_call_id(p->cfg);
- if (endp->ci == CI_UNUSED)
+ }
+
+ if (mgcp_parse_conn_mode(mode, endp, conn->conn) != 0) {
+ error_code = 517;
goto error2;
+ }
+
+ mgcp_rtp_end_config(endp, 0, &conn->end);
+
+ if (allocate_port(endp, conn) != 0) {
+ goto error2;
+ }
/* Annotate Osmux circuit ID and set it to negotiating state until this
- * is fully set up from the dummy load.
- */
- endp->osmux.state = OSMUX_STATE_DISABLED;
+ * is fully set up from the dummy load. */
+ conn->osmux.state = OSMUX_STATE_DISABLED;
if (osmux_cid >= 0) {
- endp->osmux.cid = osmux_cid;
- endp->osmux.state = OSMUX_STATE_NEGOTIATING;
+ conn->osmux.cid = osmux_cid;
+ conn->osmux.state = OSMUX_STATE_NEGOTIATING;
} else if (endp->cfg->osmux == OSMUX_USAGE_ONLY) {
LOGP(DLMGCP, LOGL_ERROR,
- "Osmux only and no osmux offered on 0x%x\n", ENDPOINT_NUMBER(endp));
+ "CRCX: endpoint:%x osmux only and no osmux offered\n",
+ ENDPOINT_NUMBER(endp));
goto error2;
}
- endp->allocated = 1;
-
/* set up RTP media parameters */
- mgcp_set_audio_info(p->cfg, &endp->bts_end.codec, tcfg->audio_payload, tcfg->audio_name);
- endp->bts_end.fmtp_extra = talloc_strdup(tcfg->endpoints,
- tcfg->audio_fmtp_extra);
if (have_sdp)
- mgcp_parse_sdp_data(endp, &endp->net_end, p);
+ mgcp_parse_sdp_data(endp, &conn->end, p);
else if (endp->local_options.codec)
- mgcp_set_audio_info(p->cfg, &endp->net_end.codec,
- PTYPE_UNDEFINED, endp->local_options.codec);
+ mgcp_set_audio_info(p->cfg, &conn->end.codec,
+ PTYPE_UNDEFINED, endp->local_options.codec);
+ conn->end.fmtp_extra = talloc_strdup(tcfg->endpoints,
+ tcfg->audio_fmtp_extra);
- if (p->cfg->bts_force_ptime) {
- endp->bts_end.packet_duration_ms = p->cfg->bts_force_ptime;
- endp->bts_end.force_output_ptime = 1;
+ if (p->cfg->force_ptime) {
+ conn->end.packet_duration_ms = p->cfg->force_ptime;
+ conn->end.force_output_ptime = 1;
}
- if (setup_rtp_processing(endp) != 0)
+ if (setup_rtp_processing(endp, conn) != 0) {
+ LOGP(DLMGCP, LOGL_ERROR,
+ "CRCX: endpoint:%x could not start RTP processing!\n",
+ ENDPOINT_NUMBER(endp));
goto error2;
+ }
/* policy CB */
if (p->cfg->policy_cb) {
int rc;
rc = p->cfg->policy_cb(tcfg, ENDPOINT_NUMBER(endp),
- MGCP_ENDP_CRCX, p->trans);
+ MGCP_ENDP_CRCX, p->trans);
switch (rc) {
case MGCP_POLICY_REJECT:
- LOGP(DLMGCP, LOGL_NOTICE, "CRCX rejected by policy on 0x%x\n",
+ LOGP(DLMGCP, LOGL_NOTICE,
+ "CRCX: endpoint:%x CRCX rejected by policy\n",
ENDPOINT_NUMBER(endp));
mgcp_release_endp(endp);
return create_err_response(endp, 400, "CRCX", p->trans);
break;
case MGCP_POLICY_DEFER:
/* stop processing */
- create_transcoder(endp);
return NULL;
break;
case MGCP_POLICY_CONT:
@@ -906,24 +730,30 @@
}
}
- LOGP(DLMGCP, LOGL_DEBUG, "Creating endpoint on: 0x%x CI: %u port: %u/%u\n",
- ENDPOINT_NUMBER(endp), endp->ci,
- endp->net_end.local_port, endp->bts_end.local_port);
+ LOGP(DLMGCP, LOGL_DEBUG,
+ "CRCX: endpoint:%x Creating connection: CI: %u port: %u\n",
+ ENDPOINT_NUMBER(endp), conn->conn->id, conn->end.local_port);
if (p->cfg->change_cb)
p->cfg->change_cb(tcfg, ENDPOINT_NUMBER(endp), MGCP_ENDP_CRCX);
- if (endp->conn_mode & MGCP_CONN_RECV_ONLY && tcfg->keepalive_interval != 0) {
- send_dummy(endp);
+ if (conn->conn->mode & MGCP_CONN_RECV_ONLY
+ && tcfg->keepalive_interval != 0) {
+ send_dummy(endp, conn);
}
- create_transcoder(endp);
- return create_response_with_sdp(endp, "CRCX", p->trans);
+ LOGP(DLMGCP, LOGL_NOTICE,
+ "CRCX: endpoint:%x connection successfully created\n",
+ ENDPOINT_NUMBER(endp));
+ return create_response_with_sdp(endp, conn, "CRCX", p->trans);
error2:
mgcp_release_endp(endp);
- LOGP(DLMGCP, LOGL_NOTICE, "Resource error on 0x%x\n", ENDPOINT_NUMBER(endp));
+ LOGP(DLMGCP, LOGL_NOTICE,
+ "CRCX: endpoint:%x unable to create connection resource error\n",
+ ENDPOINT_NUMBER(endp));
return create_err_response(endp, error_code, "CRCX", p->trans);
}
+/* MDCX command handler, processes the received command */
static struct msgb *handle_modify_con(struct mgcp_parse_data *p)
{
struct mgcp_endpoint *endp = p->endp;
@@ -931,14 +761,21 @@
int silent = 0;
int have_sdp = 0;
char *line;
+ const char *ci = NULL;
const char *local_options = NULL;
+ const char *mode = NULL;
+ struct mgcp_conn_rtp *conn = NULL;
+ uint32_t conn_id;
+
+ LOGP(DLMGCP, LOGL_NOTICE, "MDCX: modifying existing connection ...\n");
if (p->found != 0)
return create_err_response(NULL, 510, "MDCX", p->trans);
- if (endp->ci == CI_UNUSED) {
- LOGP(DLMGCP, LOGL_ERROR, "Endpoint is not "
- "holding a connection. 0x%x\n", ENDPOINT_NUMBER(endp));
+ if (llist_count(&endp->conns) <= 0) {
+ LOGP(DLMGCP, LOGL_ERROR,
+ "MDCX: endpoint:%x endpoint is not holding a connection.\n",
+ ENDPOINT_NUMBER(endp));
return create_err_response(endp, 400, "MDCX", p->trans);
}
@@ -947,62 +784,79 @@
continue;
switch (line[0]) {
- case 'C': {
- if (verify_call_id(endp, line + 3) != 0)
+ case 'C':
+ if (mgcp_verify_call_id(endp, line + 3) != 0)
goto error3;
break;
- }
- case 'I': {
- if (verify_ci(endp, line + 3) != 0)
+ case 'I':
+ ci = (const char *)line + 3;
+ if (mgcp_verify_ci(endp, ci) != 0)
goto error3;
break;
- }
case 'L':
- local_options = (const char *) line + 3;
+ local_options = (const char *)line + 3;
break;
case 'M':
- if (parse_conn_mode(line + 3, endp) != 0) {
- error_code = 517;
- goto error3;
- }
- endp->orig_mode = endp->conn_mode;
+ mode = (const char *)line + 3;
break;
case 'Z':
silent = strcmp("noanswer", line + 3) == 0;
break;
case '\0':
- /* SDP file begins */
have_sdp = 1;
- mgcp_parse_sdp_data(endp, &endp->net_end, p);
- /* This will exhaust p->save, so the loop will
- * terminate next time.
- */
+ goto mgcp_header_done;
break;
default:
- LOGP(DLMGCP, LOGL_NOTICE, "Unhandled MGCP option: '%c'/%d on 0x%x\n",
- line[0], line[0], ENDPOINT_NUMBER(endp));
+ LOGP(DLMGCP, LOGL_NOTICE,
+ "MDCX: endpoint:%x Unhandled MGCP option: '%c'/%d\n",
+ ENDPOINT_NUMBER(endp), line[0], line[0]);
break;
}
}
+mgcp_header_done:
+ if (mgcp_parse_ci(&conn_id, ci)) {
+ LOGP(DLMGCP, LOGL_ERROR,
+ "MDCX: endpoint:%x insufficient parameters, missing ci (connectionIdentifier)\n",
+ ENDPOINT_NUMBER(endp));
+ return create_err_response(endp, 400, "MDCX", p->trans);
+ }
+
+ conn = mgcp_conn_get_rtp(endp, conn_id);
+ if (!conn)
+ return create_err_response(endp, 400, "MDCX", p->trans);
+
+ if (mode) {
+ if (mgcp_parse_conn_mode(mode, endp, conn->conn) != 0) {
+ error_code = 517;
+ goto error3;
+ }
+ } else
+ conn->conn->mode = conn->conn->mode_orig;
+
+ if (have_sdp)
+ mgcp_parse_sdp_data(endp, &conn->end, p);
+
set_local_cx_options(endp->tcfg->endpoints, &endp->local_options,
local_options);
if (!have_sdp && endp->local_options.codec)
- mgcp_set_audio_info(p->cfg, &endp->net_end.codec,
- PTYPE_UNDEFINED, endp->local_options.codec);
+ mgcp_set_audio_info(p->cfg, &conn->end.codec,
+ PTYPE_UNDEFINED, endp->local_options.codec);
- if (setup_rtp_processing(endp) != 0)
+ if (setup_rtp_processing(endp, conn) != 0)
goto error3;
+
/* policy CB */
if (p->cfg->policy_cb) {
int rc;
rc = p->cfg->policy_cb(endp->tcfg, ENDPOINT_NUMBER(endp),
- MGCP_ENDP_MDCX, p->trans);
+ MGCP_ENDP_MDCX, p->trans);
switch (rc) {
case MGCP_POLICY_REJECT:
- LOGP(DLMGCP, LOGL_NOTICE, "MDCX rejected by policy on 0x%x\n",
+ LOGP(DLMGCP, LOGL_NOTICE,
+ "MDCX: endpoint:%x rejected by policy\n",
ENDPOINT_NUMBER(endp));
if (silent)
goto out_silent;
@@ -1010,7 +864,8 @@
break;
case MGCP_POLICY_DEFER:
/* stop processing */
- LOGP(DLMGCP, LOGL_DEBUG, "endp %x MDCX defer\n",
+ LOGP(DLMGCP, LOGL_DEBUG,
+ "MDCX: endpoint:%x defered by policy\n",
ENDPOINT_NUMBER(endp));
return NULL;
break;
@@ -1020,34 +875,37 @@
}
}
- mgcp_rtp_end_config(endp, 1, &endp->net_end);
- mgcp_rtp_end_config(endp, 1, &endp->bts_end);
+ mgcp_rtp_end_config(endp, 1, &conn->end);
/* modify */
- LOGP(DLMGCP, LOGL_DEBUG, "Modified endpoint on: 0x%x Server: %s:%u\n",
- ENDPOINT_NUMBER(endp), inet_ntoa(endp->net_end.addr), ntohs(endp->net_end.rtp_port));
+ LOGP(DLMGCP, LOGL_DEBUG,
+ "MDCX: endpoint:%x modified conn:%s\n",
+ ENDPOINT_NUMBER(endp), mgcp_conn_dump(conn->conn));
if (p->cfg->change_cb)
- p->cfg->change_cb(endp->tcfg, ENDPOINT_NUMBER(endp), MGCP_ENDP_MDCX);
+ p->cfg->change_cb(endp->tcfg, ENDPOINT_NUMBER(endp),
+ MGCP_ENDP_MDCX);
- if (endp->conn_mode & MGCP_CONN_RECV_ONLY &&
+ if (conn->conn->mode & MGCP_CONN_RECV_ONLY &&
endp->tcfg->keepalive_interval != 0)
- send_dummy(endp);
+ send_dummy(endp, conn);
if (silent)
goto out_silent;
- return create_response_with_sdp(endp, "MDCX", p->trans);
-
+ LOGP(DLMGCP, LOGL_NOTICE,
+ "MDCX: endpoint:%x connection successfully modified\n",
+ ENDPOINT_NUMBER(endp));
+ return create_response_with_sdp(endp, conn, "MDCX", p->trans);
error3:
return create_err_response(endp, error_code, "MDCX", p->trans);
-
out_silent:
- LOGP(DLMGCP, LOGL_DEBUG, "endp %x Modify endpoint: silent exit\n",
+ LOGP(DLMGCP, LOGL_DEBUG, "MDCX: endpoint:%x silent exit\n",
ENDPOINT_NUMBER(endp));
return NULL;
}
+/* DLCX command handler, processes the received command */
static struct msgb *handle_delete_con(struct mgcp_parse_data *p)
{
struct mgcp_endpoint *endp = p->endp;
@@ -1055,13 +913,21 @@
int silent = 0;
char *line;
char stats[1048];
+ const char *ci = NULL;
+ struct mgcp_conn_rtp *conn = NULL;
+ uint32_t conn_id;
+
+ LOGP(DLMGCP, LOGL_NOTICE,
+ "DLCX: endpoint:%x deleting connection ...\n",
+ ENDPOINT_NUMBER(endp));
if (p->found != 0)
return create_err_response(NULL, error_code, "DLCX", p->trans);
- if (!p->endp->allocated) {
- LOGP(DLMGCP, LOGL_ERROR, "Endpoint is not used. 0x%x\n",
- ENDPOINT_NUMBER(endp));
+ if (llist_count(&endp->conns) <= 0) {
+ LOGP(DLMGCP, LOGL_ERROR,
+ "DLCX: endpoint:%x endpoint is not holding a connection.\n",
+ ENDPOINT_NUMBER(endp));
return create_err_response(endp, 400, "DLCX", p->trans);
}
@@ -1071,19 +937,21 @@
switch (line[0]) {
case 'C':
- if (verify_call_id(endp, line + 3) != 0)
+ if (mgcp_verify_call_id(endp, line + 3) != 0)
goto error3;
break;
case 'I':
- if (verify_ci(endp, line + 3) != 0)
+ ci = (const char *)line + 3;
+ if (mgcp_verify_ci(endp, ci) != 0)
goto error3;
break;
case 'Z':
silent = strcmp("noanswer", line + 3) == 0;
break;
default:
- LOGP(DLMGCP, LOGL_NOTICE, "Unhandled option: '%c'/%d on 0x%x\n",
- line[0], line[0], ENDPOINT_NUMBER(endp));
+ LOGP(DLMGCP, LOGL_NOTICE,
+ "DLCX: endpoint:%x Unhandled MGCP option: '%c'/%d\n",
+ ENDPOINT_NUMBER(endp), line[0], line[0]);
break;
}
}
@@ -1092,10 +960,11 @@
if (p->cfg->policy_cb) {
int rc;
rc = p->cfg->policy_cb(endp->tcfg, ENDPOINT_NUMBER(endp),
- MGCP_ENDP_DLCX, p->trans);
+ MGCP_ENDP_DLCX, p->trans);
switch (rc) {
case MGCP_POLICY_REJECT:
- LOGP(DLMGCP, LOGL_NOTICE, "DLCX rejected by policy on 0x%x\n",
+ LOGP(DLMGCP, LOGL_NOTICE,
+ "DLCX: endpoint:%x rejected by policy\n",
ENDPOINT_NUMBER(endp));
if (silent)
goto out_silent;
@@ -1103,7 +972,6 @@
break;
case MGCP_POLICY_DEFER:
/* stop processing */
- delete_transcoder(endp);
return NULL;
break;
case MGCP_POLICY_CONT:
@@ -1112,17 +980,41 @@
}
}
- /* free the connection */
- LOGP(DLMGCP, LOGL_DEBUG, "Deleted endpoint on: 0x%x Server: %s:%u\n",
- ENDPOINT_NUMBER(endp), inet_ntoa(endp->net_end.addr), ntohs(endp->net_end.rtp_port));
+ /* find the connection */
+ if (mgcp_parse_ci(&conn_id, ci)) {
+ LOGP(DLMGCP, LOGL_ERROR,
+ "DLCX: endpoint:%x insufficient parameters, missing ci (connectionIdentifier)\n",
+ ENDPOINT_NUMBER(endp));
+ return create_err_response(endp, 400, "DLCX", p->trans);
+ }
- /* save the statistics of the current call */
- mgcp_format_stats(endp, stats, sizeof(stats));
+ conn = mgcp_conn_get_rtp(endp, conn_id);
+ if (!conn)
+ goto error3;
- delete_transcoder(endp);
- mgcp_release_endp(endp);
+ /* save the statistics of the current connection */
+ mgcp_format_stats(stats, sizeof(stats), conn->conn);
+
+ /* delete connection */
+ LOGP(DLMGCP, LOGL_DEBUG, "DLCX: endpoint:%x deleting conn:%s\n",
+ ENDPOINT_NUMBER(endp), mgcp_conn_dump(conn->conn));
+ mgcp_conn_free(endp, conn_id);
+ LOGP(DLMGCP, LOGL_NOTICE,
+ "DLCX: endpoint:%x connection successfully deleted\n",
+ ENDPOINT_NUMBER(endp));
+
+ /* When all connections are closed, the endpoint will be released
+ * in order to be ready to be used by another call. */
+ if (llist_count(&endp->conns) <= 0) {
+ mgcp_release_endp(endp);
+ LOGP(DLMGCP, LOGL_DEBUG,
+ "DLCX: endpoint:%x endpoint released\n",
+ ENDPOINT_NUMBER(endp));
+ }
+
if (p->cfg->change_cb)
- p->cfg->change_cb(endp->tcfg, ENDPOINT_NUMBER(endp), MGCP_ENDP_DLCX);
+ p->cfg->change_cb(endp->tcfg, ENDPOINT_NUMBER(endp),
+ MGCP_ENDP_DLCX);
if (silent)
goto out_silent;
@@ -1132,13 +1024,27 @@
return create_err_response(endp, error_code, "DLCX", p->trans);
out_silent:
+ LOGP(DLMGCP, LOGL_DEBUG, "DLCX: endpoint:%x silent exit\n",
+ ENDPOINT_NUMBER(endp));
return NULL;
}
+/* RSIP command handler, processes the received command */
static struct msgb *handle_rsip(struct mgcp_parse_data *p)
{
+ /* TODO: Also implement the resetting of a specific endpoint
+ * to make mgcp_send_reset_ep() work. Currently this will call
+ * mgcp_rsip_cb() in mgw_main.c, which sets reset_endpoints=1
+ * to make read_call_agent() reset all endpoints when called
+ * next time. In order to selectively reset endpoints some
+ * mechanism to distinguish which endpoint shall be resetted
+ * is needed */
+
+ LOGP(DLMGCP, LOGL_NOTICE, "RSIP: resetting all endpoints ...\n");
+
if (p->found != 0) {
- LOGP(DLMGCP, LOGL_ERROR, "Failed to find the endpoint.\n");
+ LOGP(DLMGCP, LOGL_ERROR,
+ "RSIP: failed to find the endpoint.\n");
return NULL;
}
@@ -1156,17 +1062,17 @@
return str[2];
}
-/*
- * This can request like DTMF detection and forward, fax detection... it
+/* This can request like DTMF detection and forward, fax detection... it
* can also request when the notification should be send and such. We don't
- * do this right now.
- */
+ * do this right now. */
static struct msgb *handle_noti_req(struct mgcp_parse_data *p)
{
int res = 0;
char *line;
char tone = CHAR_MAX;
+ LOGP(DLMGCP, LOGL_NOTICE, "RQNT: processing request for notification ...\n");
+
if (p->found != 0)
return create_err_response(NULL, 400, "RQNT", p->trans);
@@ -1186,29 +1092,38 @@
res = p->cfg->rqnt_cb(p->endp, tone);
return res == 0 ?
- create_ok_response(p->endp, 200, "RQNT", p->trans) :
- create_err_response(p->endp, res, "RQNT", p->trans);
+ create_ok_response(p->endp, 200, "RQNT", p->trans) :
+ create_err_response(p->endp, res, "RQNT", p->trans);
}
+/* Connection keepalive timer, will take care that dummy packets are send
+ * regulary, so that NAT connections stay open */
static void mgcp_keepalive_timer_cb(void *_tcfg)
{
struct mgcp_trunk_config *tcfg = _tcfg;
+ struct mgcp_conn *conn;
int i;
+
LOGP(DLMGCP, LOGL_DEBUG, "Triggered trunk %d keepalive timer.\n",
tcfg->trunk_nr);
if (tcfg->keepalive_interval <= 0)
return;
+ /* Send walk over all endpoints and send out dummy packets through
+ * every connection present on each endpoint */
for (i = 1; i < tcfg->number_endpoints; ++i) {
struct mgcp_endpoint *endp = &tcfg->endpoints[i];
- if (endp->conn_mode == MGCP_CONN_RECV_ONLY)
- send_dummy(endp);
+ llist_for_each_entry(conn, &endp->conns, entry) {
+ if (conn->mode == MGCP_CONN_RECV_ONLY)
+ send_dummy(endp, &conn->u.rtp);
+ }
}
LOGP(DLMGCP, LOGL_DEBUG, "Rescheduling trunk %d keepalive timer.\n",
tcfg->trunk_nr);
- osmo_timer_schedule(&tcfg->keepalive_timer, tcfg->keepalive_interval, 0);
+ osmo_timer_schedule(&tcfg->keepalive_timer, tcfg->keepalive_interval,
+ 0);
}
void mgcp_trunk_set_keepalive(struct mgcp_trunk_config *tcfg, int interval)
@@ -1223,6 +1138,8 @@
tcfg->keepalive_interval, 0);
}
+/*! allocate configuration with default values.
+ * (called once at startup by main function) */
struct mgcp_config *mgcp_config_alloc(void)
{
struct mgcp_config *cfg;
@@ -1233,15 +1150,14 @@
return NULL;
}
+ 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;
+
cfg->source_port = 2427;
cfg->source_addr = talloc_strdup(cfg, "0.0.0.0");
cfg->osmux_addr = talloc_strdup(cfg, "0.0.0.0");
- cfg->transcoder_remote_base = 4000;
-
- cfg->bts_ports.base_port = RTP_PORT_DEFAULT;
- cfg->net_ports.base_port = RTP_PORT_NET_DEFAULT;
-
cfg->rtp_processing_cb = &mgcp_rtp_processing_default;
cfg->setup_rtp_processing_cb = &mgcp_setup_rtp_processing_default;
@@ -1263,6 +1179,11 @@
return cfg;
}
+/*! allocate configuration with default values.
+ * (called once at startup by VTY)
+ * \param[in] cfg mgcp configuration
+ * \param[in] nr trunk number
+ * \returns pointer to allocated trunk configuration */
struct mgcp_trunk_config *mgcp_trunk_alloc(struct mgcp_config *cfg, int nr)
{
struct mgcp_trunk_config *trunk;
@@ -1287,185 +1208,69 @@
return trunk;
}
+/*! get trunk configuration by trunk number (index).
+ * \param[in] cfg mgcp configuration
+ * \param[in] index trunk number
+ * \returns pointer to trunk configuration, NULL on error */
struct mgcp_trunk_config *mgcp_trunk_num(struct mgcp_config *cfg, int index)
{
struct mgcp_trunk_config *trunk;
llist_for_each_entry(trunk, &cfg->trunks, entry)
- if (trunk->trunk_nr == index)
- return trunk;
+ if (trunk->trunk_nr == index)
+ return trunk;
return NULL;
}
-static void mgcp_rtp_codec_reset(struct mgcp_rtp_codec *codec)
-{
- codec->payload_type = -1;
- talloc_free(codec->subtype_name);
- codec->subtype_name = NULL;
- talloc_free(codec->audio_name);
- codec->audio_name = NULL;
- codec->frame_duration_num = DEFAULT_RTP_AUDIO_FRAME_DUR_NUM;
- codec->frame_duration_den = DEFAULT_RTP_AUDIO_FRAME_DUR_DEN;
- codec->rate = DEFAULT_RTP_AUDIO_DEFAULT_RATE;
- codec->channels = DEFAULT_RTP_AUDIO_DEFAULT_CHANNELS;
-}
-
-static void mgcp_rtp_end_reset(struct mgcp_rtp_end *end)
-{
- if (end->local_alloc == PORT_ALLOC_DYNAMIC) {
- mgcp_free_rtp_port(end);
- end->local_port = 0;
- }
-
- end->packets = 0;
- end->octets = 0;
- end->dropped_packets = 0;
- memset(&end->addr, 0, sizeof(end->addr));
- end->rtp_port = end->rtcp_port = 0;
- end->local_alloc = -1;
- talloc_free(end->fmtp_extra);
- end->fmtp_extra = NULL;
- talloc_free(end->rtp_process_data);
- end->rtp_process_data = NULL;
-
- /* Set default values */
- end->frames_per_packet = 0; /* unknown */
- end->packet_duration_ms = DEFAULT_RTP_AUDIO_PACKET_DURATION_MS;
- end->output_enabled = 0;
-
- mgcp_rtp_codec_reset(&end->codec);
- mgcp_rtp_codec_reset(&end->alt_codec);
-}
-
-static void mgcp_rtp_end_init(struct mgcp_rtp_end *end)
-{
- mgcp_rtp_end_reset(end);
- end->rtp.fd = -1;
- end->rtcp.fd = -1;
-}
-
+/*! allocate endpoints and set default values.
+ * (called once at startup by VTY)
+ * \param[in] tcfg trunk configuration
+ * \returns 0 on success, -1 on failure */
int mgcp_endpoints_allocate(struct mgcp_trunk_config *tcfg)
{
int i;
- /* Initialize all endpoints */
tcfg->endpoints = _talloc_zero_array(tcfg->cfg,
- sizeof(struct mgcp_endpoint),
- tcfg->number_endpoints, "endpoints");
+ sizeof(struct mgcp_endpoint),
+ tcfg->number_endpoints,
+ "endpoints");
if (!tcfg->endpoints)
return -1;
for (i = 0; i < tcfg->number_endpoints; ++i) {
- tcfg->endpoints[i].osmux.allocated_cid = -1;
- tcfg->endpoints[i].ci = CI_UNUSED;
+ INIT_LLIST_HEAD(&tcfg->endpoints[i].conns);
tcfg->endpoints[i].cfg = tcfg->cfg;
tcfg->endpoints[i].tcfg = tcfg;
- mgcp_rtp_end_init(&tcfg->endpoints[i].net_end);
- mgcp_rtp_end_init(&tcfg->endpoints[i].bts_end);
- mgcp_rtp_end_init(&tcfg->endpoints[i].trans_net);
- mgcp_rtp_end_init(&tcfg->endpoints[i].trans_bts);
+
+ /* NOTE: Currently all endpoints are of type RTP, this will
+ * change when new variations are implemented */
+ tcfg->endpoints[i].type = &ep_typeset.rtp;
}
return 0;
}
+/*! relase endpoint, all open connections are closed.
+ * \param[in] endp endpoint to release */
void mgcp_release_endp(struct mgcp_endpoint *endp)
{
- LOGP(DLMGCP, LOGL_DEBUG, "Releasing endpoint on: 0x%x\n", ENDPOINT_NUMBER(endp));
- endp->ci = CI_UNUSED;
- endp->allocated = 0;
+ LOGP(DLMGCP, LOGL_DEBUG, "Releasing endpoint:%x\n",
+ ENDPOINT_NUMBER(endp));
+ /* Normally this function should only be called wehen
+ * all connections have been removed already. In case
+ * that there are still connections open (e.g. when
+ * RSIP is executed), free them all at once. */
+ mgcp_conn_free_all(endp);
+
+ /* Reset endpoint parameters and states */
talloc_free(endp->callid);
endp->callid = NULL;
-
talloc_free(endp->local_options.string);
endp->local_options.string = NULL;
talloc_free(endp->local_options.codec);
endp->local_options.codec = NULL;
-
- mgcp_rtp_end_reset(&endp->bts_end);
- mgcp_rtp_end_reset(&endp->net_end);
- mgcp_rtp_end_reset(&endp->trans_net);
- mgcp_rtp_end_reset(&endp->trans_bts);
- endp->type = MGCP_RTP_DEFAULT;
-
- memset(&endp->net_state, 0, sizeof(endp->net_state));
- memset(&endp->bts_state, 0, sizeof(endp->bts_state));
-
- endp->conn_mode = endp->orig_mode = MGCP_CONN_NONE;
-
- if (endp->osmux.state == OSMUX_STATE_ENABLED)
- osmux_disable_endpoint(endp);
-
- /* release the circuit ID if it had been allocated */
- osmux_release_cid(endp);
-
- memset(&endp->taps, 0, sizeof(endp->taps));
-}
-
-void mgcp_initialize_endp(struct mgcp_endpoint *endp)
-{
- return mgcp_release_endp(endp);
-}
-
-static int send_trans(struct mgcp_config *cfg, const char *buf, int len)
-{
- struct sockaddr_in addr;
-
- memset(&addr, 0, sizeof(addr));
- addr.sin_family = AF_INET;
- addr.sin_addr = cfg->transcoder_in;
- addr.sin_port = htons(2427);
- return sendto(cfg->gw_fd.bfd.fd, buf, len, 0,
- (struct sockaddr *) &addr, sizeof(addr));
-}
-
-static void send_msg(struct mgcp_endpoint *endp, int endpoint, int port,
- const char *msg, const char *mode)
-{
- char buf[2096];
- int len;
- int nchars;
-
- /* hardcoded to AMR right now, we do not know the real type at this point */
- len = snprintf(buf, sizeof(buf),
- "%s 42 %x@mgw MGCP 1.0\r\n"
- "C: 4256\r\n"
- "M: %s\r\n"
- "\r\n",
- msg, endpoint, mode);
-
- if (len < 0)
- return;
-
- nchars = write_response_sdp(endp, buf + len, sizeof(buf) + len - 1, NULL);
- if (nchars < 0)
- return;
-
- len += nchars;
-
- buf[sizeof(buf) - 1] = '\0';
-
- send_trans(endp->cfg, buf, len);
-}
-
-static void send_dlcx(struct mgcp_endpoint *endp, int endpoint)
-{
- char buf[2096];
- int len;
-
- len = snprintf(buf, sizeof(buf),
- "DLCX 43 %x@mgw MGCP 1.0\r\n"
- "C: 4256\r\n"
- , endpoint);
-
- if (len < 0)
- return;
-
- buf[sizeof(buf) - 1] = '\0';
-
- send_trans(endp->cfg, buf, len);
}
static int send_agent(struct mgcp_config *cfg, const char *buf, int len)
@@ -1473,168 +1278,47 @@
return write(cfg->gw_fd.bfd.fd, buf, len);
}
+/*! Reset all endpoints by sending RSIP message to self.
+ * (called by VTY)
+ * \param[in] endp trunk endpoint
+ * \param[in] endpoint number
+ * \returns 0 on success, -1 on error */
int mgcp_send_reset_all(struct mgcp_config *cfg)
{
+ int rc;
+
static const char mgcp_reset[] = {
- "RSIP 1 *@mgw MGCP 1.0\r\n"
+ "RSIP 1 *@mgw MGCP 1.0\r\n"
};
- return send_agent(cfg, mgcp_reset, sizeof mgcp_reset -1);
+ rc = send_agent(cfg, mgcp_reset, sizeof mgcp_reset - 1);
+ if (rc <= 0)
+ return -1;
+
+ return 0;
}
+/*! Reset a single endpoint by sending RSIP message to self.
+ * (called by VTY)
+ * \param[in] endp trunk endpoint
+ * \param[in] endpoint number
+ * \returns 0 on success, -1 on error */
int mgcp_send_reset_ep(struct mgcp_endpoint *endp, int endpoint)
{
char buf[128];
int len;
+ int rc;
len = snprintf(buf, sizeof(buf),
- "RSIP 39 %x@mgw MGCP 1.0\r\n"
- , endpoint);
+ "RSIP 39 %x@mgw MGCP 1.0\r\n", endpoint);
if (len < 0)
- return len;
+ return -1;
buf[sizeof(buf) - 1] = '\0';
- return send_agent(endp->cfg, buf, len);
-}
-
-static int setup_rtp_processing(struct mgcp_endpoint *endp)
-{
- int rc = 0;
- struct mgcp_config *cfg = endp->cfg;
-
- if (endp->type != MGCP_RTP_DEFAULT)
- return 0;
-
- if (endp->conn_mode == MGCP_CONN_LOOPBACK)
- return 0;
-
- if (endp->conn_mode & MGCP_CONN_SEND_ONLY)
- rc |= cfg->setup_rtp_processing_cb(endp, &endp->net_end, &endp->bts_end);
- else
- rc |= cfg->setup_rtp_processing_cb(endp, &endp->net_end, NULL);
-
- if (endp->conn_mode & MGCP_CONN_RECV_ONLY)
- rc |= cfg->setup_rtp_processing_cb(endp, &endp->bts_end, &endp->net_end);
- else
- rc |= cfg->setup_rtp_processing_cb(endp, &endp->bts_end, NULL);
- return rc;
-}
-
-static void create_transcoder(struct mgcp_endpoint *endp)
-{
- int port;
- int in_endp = ENDPOINT_NUMBER(endp);
- int out_endp = endp_back_channel(in_endp);
-
- if (endp->type != MGCP_RTP_TRANSCODED)
- return;
-
- send_msg(endp, in_endp, endp->trans_bts.local_port, "CRCX", "sendrecv");
- send_msg(endp, in_endp, endp->trans_bts.local_port, "MDCX", "sendrecv");
- send_msg(endp, out_endp, endp->trans_net.local_port, "CRCX", "sendrecv");
- send_msg(endp, out_endp, endp->trans_net.local_port, "MDCX", "sendrecv");
-
- port = rtp_calculate_port(in_endp, endp->cfg->transcoder_remote_base);
- endp->trans_bts.rtp_port = htons(port);
- endp->trans_bts.rtcp_port = htons(port + 1);
-
- port = rtp_calculate_port(out_endp, endp->cfg->transcoder_remote_base);
- endp->trans_net.rtp_port = htons(port);
- endp->trans_net.rtcp_port = htons(port + 1);
-}
-
-static void delete_transcoder(struct mgcp_endpoint *endp)
-{
- int in_endp = ENDPOINT_NUMBER(endp);
- int out_endp = endp_back_channel(in_endp);
-
- if (endp->type != MGCP_RTP_TRANSCODED)
- return;
-
- send_dlcx(endp, in_endp);
- send_dlcx(endp, out_endp);
-}
-
-int mgcp_reset_transcoder(struct mgcp_config *cfg)
-{
- if (!cfg->transcoder_ip)
- return 0;
-
- static const char mgcp_reset[] = {
- "RSIP 1 13@mgw MGCP 1.0\r\n"
- };
-
- return send_trans(cfg, mgcp_reset, sizeof mgcp_reset -1);
-}
-
-void mgcp_format_stats(struct mgcp_endpoint *endp, char *msg, size_t size)
-{
- uint32_t expected, jitter;
- int ploss;
- int nchars;
- mgcp_state_calc_loss(&endp->net_state, &endp->net_end,
- &expected, &ploss);
- jitter = mgcp_state_calc_jitter(&endp->net_state);
-
- nchars = snprintf(msg, size,
- "\r\nP: PS=%u, OS=%u, PR=%u, OR=%u, PL=%d, JI=%u",
- endp->bts_end.packets, endp->bts_end.octets,
- endp->net_end.packets, endp->net_end.octets,
- ploss, jitter);
- if (nchars < 0 || nchars >= size)
- goto truncate;
-
- msg += nchars;
- size -= nchars;
-
- /* Error Counter */
- nchars = snprintf(msg, size,
- "\r\nX-Osmo-CP: EC TIS=%u, TOS=%u, TIR=%u, TOR=%u",
- endp->net_state.in_stream.err_ts_counter,
- endp->net_state.out_stream.err_ts_counter,
- endp->bts_state.in_stream.err_ts_counter,
- endp->bts_state.out_stream.err_ts_counter);
- if (nchars < 0 || nchars >= size)
- goto truncate;
-
- msg += nchars;
- size -= nchars;
-
- if (endp->osmux.state == OSMUX_STATE_ENABLED) {
- snprintf(msg, size,
- "\r\nX-Osmux-ST: CR=%u, BR=%u",
- endp->osmux.stats.chunks,
- endp->osmux.stats.octets);
- }
-truncate:
- msg[size - 1] = '\0';
-}
-
-int mgcp_parse_stats(struct msgb *msg, uint32_t *ps, uint32_t *os,
- uint32_t *pr, uint32_t *_or, int *loss, uint32_t *jitter)
-{
- char *line, *save;
- int rc;
-
- /* initialize with bad values */
- *ps = *os = *pr = *_or = *jitter = UINT_MAX;
- *loss = INT_MAX;
-
-
- line = strtok_r((char *) msg->l2h, "\r\n", &save);
- if (!line)
+ rc = send_agent(endp->cfg, buf, len);
+ if (rc <= 0)
return -1;
- /* this can only parse the message that is created above... */
- for_each_non_empty_line(line, save) {
- switch (line[0]) {
- case 'P':
- rc = sscanf(line, "P: PS=%u, OS=%u, PR=%u, OR=%u, PL=%d, JI=%u",
- ps, os, pr, _or, loss, jitter);
- return rc == 6 ? 0 : -1;
- }
- }
-
- return -1;
+ return 0;
}
diff --git a/src/libosmo-mgcp/mgcp_sdp.c b/src/libosmo-mgcp/mgcp_sdp.c
index 055e1af..7568351 100644
--- a/src/libosmo-mgcp/mgcp_sdp.c
+++ b/src/libosmo-mgcp/mgcp_sdp.c
@@ -20,8 +20,9 @@
*
*/
-#include <osmocom/legacy_mgcp/mgcp.h>
-#include <osmocom/legacy_mgcp/mgcp_internal.h>
+#include <osmocom/mgcp/mgcp.h>
+#include <osmocom/mgcp/mgcp_internal.h>
+#include <osmocom/mgcp/mgcp_msg.h>
#include <errno.h>
diff --git a/src/libosmo-mgcp/mgcp_stat.c b/src/libosmo-mgcp/mgcp_stat.c
new file mode 100644
index 0000000..b84f5f2
--- /dev/null
+++ b/src/libosmo-mgcp/mgcp_stat.c
@@ -0,0 +1,128 @@
+/* A Media Gateway Control Protocol Media Gateway: RFC 3435 */
+/* The statistics generator */
+
+/*
+ * (C) 2009-2012 by Holger Hans Peter Freyther <zecke@selfish.org>
+ * (C) 2009-2012 by On-Waves
+ * (C) 2017 by sysmocom s.f.m.c. GmbH <info@sysmocom.de>
+ * 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 <osmocom/mgcp/mgcp_stat.h>
+#include <limits.h>
+
+/* Helper function for mgcp_format_stats_rtp() to calculate packet loss */
+void calc_loss(struct mgcp_rtp_state *state,
+ struct mgcp_rtp_end *end, uint32_t *expected,
+ int *loss)
+{
+ *expected = state->stats_cycles + state->stats_max_seq;
+ *expected = *expected - state->stats_base_seq + 1;
+
+ if (!state->stats_initialized) {
+ *expected = 0;
+ *loss = 0;
+ return;
+ }
+
+ /*
+ * Make sure the sign is correct and use the biggest
+ * positive/negative number that fits.
+ */
+ *loss = *expected - end->packets_rx;
+ if (*expected < end->packets_rx) {
+ if (*loss > 0)
+ *loss = INT_MIN;
+ } else {
+ if (*loss < 0)
+ *loss = INT_MAX;
+ }
+}
+
+/* Helper function for mgcp_format_stats_rtp() to calculate jitter */
+uint32_t calc_jitter(struct mgcp_rtp_state *state)
+{
+ if (!state->stats_initialized)
+ return 0;
+ return state->stats_jitter >> 4;
+}
+
+/* Generate statistics for an RTP connection */
+static void mgcp_format_stats_rtp(char *str, size_t str_len,
+ struct mgcp_conn_rtp *conn)
+{
+ uint32_t expected, jitter;
+ int ploss;
+ int nchars;
+
+ calc_loss(&conn->state, &conn->end, &expected, &ploss);
+ jitter = calc_jitter(&conn->state);
+
+ nchars = snprintf(str, str_len,
+ "\r\nP: PS=%u, OS=%u, PR=%u, OR=%u, PL=%d, JI=%u",
+ conn->end.packets_tx, conn->end.octets_tx,
+ conn->end.packets_rx, conn->end.octets_rx,
+ ploss, jitter);
+ if (nchars < 0 || nchars >= str_len)
+ goto truncate;
+
+ str += nchars;
+ str_len -= nchars;
+
+ /* Error Counter */
+ nchars = snprintf(str, str_len,
+ "\r\nX-Osmo-CP: EC TI=%u, TO=%u",
+ conn->state.in_stream.err_ts_counter,
+ conn->state.out_stream.err_ts_counter);
+ if (nchars < 0 || nchars >= str_len)
+ goto truncate;
+
+ str += nchars;
+ str_len -= nchars;
+
+ if (conn->osmux.state == OSMUX_STATE_ENABLED) {
+ snprintf(str, str_len,
+ "\r\nX-Osmux-ST: CR=%u, BR=%u",
+ conn->osmux.stats.chunks, conn->osmux.stats.octets);
+ }
+
+truncate:
+ str[str_len - 1] = '\0';
+}
+
+/*! format statistics into an mgcp parameter string.
+ * \param[out] str resulting string
+ * \param[in] str_len length of the string buffer
+ * \param[in] conn connection to evaluate */
+void mgcp_format_stats(char *str, size_t str_len, struct mgcp_conn *conn)
+{
+ memset(str, 0, str_len);
+ if (!conn)
+ return;
+
+ /* NOTE: At the moment we only support generating statistics for
+ * RTP connections. However, in the future we may also want to
+ * generate statistics for other connection types as well. Lets
+ * keep this option open: */
+ switch (conn->type) {
+ case MGCP_CONN_TYPE_RTP:
+ mgcp_format_stats_rtp(str, str_len, &conn->u.rtp);
+ break;
+ default:
+ break;
+ }
+}
diff --git a/src/libosmo-mgcp/mgcp_transcode.c b/src/libosmo-mgcp/mgcp_transcode.c
deleted file mode 100644
index cbabdd1..0000000
--- a/src/libosmo-mgcp/mgcp_transcode.c
+++ /dev/null
@@ -1,611 +0,0 @@
-/*
- * (C) 2014 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 <stdlib.h>
-#include <string.h>
-#include <errno.h>
-
-
-#include "g711common.h"
-
-#include <osmocom/legacy_mgcp/mgcp.h>
-#include <osmocom/legacy_mgcp/mgcp_internal.h>
-#include <osmocom/legacy_mgcp/mgcp_transcode.h>
-
-#include <osmocom/core/talloc.h>
-#include <osmocom/netif/rtp.h>
-
-int mgcp_transcoding_get_frame_size(void *state_, int nsamples, int dst)
-{
- struct mgcp_process_rtp_state *state = state_;
- if (dst)
- return (nsamples >= 0 ?
- nsamples / state->dst_samples_per_frame :
- 1) * state->dst_frame_size;
- else
- return (nsamples >= 0 ?
- nsamples / state->src_samples_per_frame :
- 1) * state->src_frame_size;
-}
-
-static enum audio_format get_audio_format(const struct mgcp_rtp_codec *codec)
-{
- if (codec->subtype_name) {
- if (!strcasecmp("GSM", codec->subtype_name))
- return AF_GSM;
- if (!strcasecmp("PCMA", codec->subtype_name))
- return AF_PCMA;
- if (!strcasecmp("PCMU", codec->subtype_name))
- return AF_PCMU;
-#ifdef HAVE_BCG729
- if (!strcasecmp("G729", codec->subtype_name))
- return AF_G729;
-#endif
- if (!strcasecmp("L16", codec->subtype_name))
- return AF_L16;
- }
-
- switch (codec->payload_type) {
- case 0 /* PCMU */:
- return AF_PCMU;
- case 3 /* GSM */:
- return AF_GSM;
- case 8 /* PCMA */:
- return AF_PCMA;
-#ifdef HAVE_BCG729
- case 18 /* G.729 */:
- return AF_G729;
-#endif
- case 11 /* L16 */:
- return AF_L16;
- default:
- return AF_INVALID;
- }
-}
-
-static void l16_encode(short *sample, unsigned char *buf, size_t n)
-{
- for (; n > 0; --n, ++sample, buf += 2) {
- buf[0] = sample[0] >> 8;
- buf[1] = sample[0] & 0xff;
- }
-}
-
-static void l16_decode(unsigned char *buf, short *sample, size_t n)
-{
- for (; n > 0; --n, ++sample, buf += 2)
- sample[0] = ((short)buf[0] << 8) | buf[1];
-}
-
-static void alaw_encode(short *sample, unsigned char *buf, size_t n)
-{
- for (; n > 0; --n)
- *(buf++) = s16_to_alaw(*(sample++));
-}
-
-static void alaw_decode(unsigned char *buf, short *sample, size_t n)
-{
- for (; n > 0; --n)
- *(sample++) = alaw_to_s16(*(buf++));
-}
-
-static void ulaw_encode(short *sample, unsigned char *buf, size_t n)
-{
- for (; n > 0; --n)
- *(buf++) = s16_to_ulaw(*(sample++));
-}
-
-static void ulaw_decode(unsigned char *buf, short *sample, size_t n)
-{
- for (; n > 0; --n)
- *(sample++) = ulaw_to_s16(*(buf++));
-}
-
-static int processing_state_destructor(struct mgcp_process_rtp_state *state)
-{
- switch (state->src_fmt) {
- case AF_GSM:
- if (state->src.gsm_handle)
- gsm_destroy(state->src.gsm_handle);
- break;
-#ifdef HAVE_BCG729
- case AF_G729:
- if (state->src.g729_dec)
- closeBcg729DecoderChannel(state->src.g729_dec);
- break;
-#endif
- default:
- break;
- }
- switch (state->dst_fmt) {
- case AF_GSM:
- if (state->dst.gsm_handle)
- gsm_destroy(state->dst.gsm_handle);
- break;
-#ifdef HAVE_BCG729
- case AF_G729:
- if (state->dst.g729_enc)
- closeBcg729EncoderChannel(state->dst.g729_enc);
- break;
-#endif
- default:
- break;
- }
- return 0;
-}
-
-int mgcp_transcoding_setup(struct mgcp_endpoint *endp,
- struct mgcp_rtp_end *dst_end,
- struct mgcp_rtp_end *src_end)
-{
- struct mgcp_process_rtp_state *state;
- enum audio_format src_fmt, dst_fmt;
- const struct mgcp_rtp_codec *dst_codec = &dst_end->codec;
-
- /* cleanup first */
- if (dst_end->rtp_process_data) {
- talloc_free(dst_end->rtp_process_data);
- dst_end->rtp_process_data = NULL;
- }
-
- if (!src_end)
- return 0;
-
- const struct mgcp_rtp_codec *src_codec = &src_end->codec;
-
- if (endp->tcfg->no_audio_transcoding) {
- LOGP(DLMGCP, LOGL_NOTICE,
- "Transcoding disabled on endpoint 0x%x\n",
- ENDPOINT_NUMBER(endp));
- return 0;
- }
-
- src_fmt = get_audio_format(src_codec);
- dst_fmt = get_audio_format(dst_codec);
-
- LOGP(DLMGCP, LOGL_ERROR,
- "Checking transcoding: %s (%d) -> %s (%d)\n",
- src_codec->subtype_name, src_codec->payload_type,
- dst_codec->subtype_name, dst_codec->payload_type);
-
- if (src_fmt == AF_INVALID || dst_fmt == AF_INVALID) {
- if (!src_codec->subtype_name || !dst_codec->subtype_name)
- /* Not enough info, do nothing */
- return 0;
-
- if (strcasecmp(src_codec->subtype_name, dst_codec->subtype_name) == 0)
- /* Nothing to do */
- return 0;
-
- LOGP(DLMGCP, LOGL_ERROR,
- "Cannot transcode: %s codec not supported (%s -> %s).\n",
- src_fmt != AF_INVALID ? "destination" : "source",
- src_codec->audio_name, dst_codec->audio_name);
- return -EINVAL;
- }
-
- if (src_codec->rate && dst_codec->rate && src_codec->rate != dst_codec->rate) {
- LOGP(DLMGCP, LOGL_ERROR,
- "Cannot transcode: rate conversion (%d -> %d) not supported.\n",
- src_codec->rate, dst_codec->rate);
- return -EINVAL;
- }
-
- state = talloc_zero(endp->tcfg->cfg, struct mgcp_process_rtp_state);
- talloc_set_destructor(state, processing_state_destructor);
- dst_end->rtp_process_data = state;
-
- state->src_fmt = src_fmt;
-
- switch (state->src_fmt) {
- case AF_L16:
- case AF_S16:
- state->src_frame_size = 80 * sizeof(short);
- state->src_samples_per_frame = 80;
- break;
- case AF_GSM:
- state->src_frame_size = sizeof(gsm_frame);
- state->src_samples_per_frame = 160;
- state->src.gsm_handle = gsm_create();
- if (!state->src.gsm_handle) {
- LOGP(DLMGCP, LOGL_ERROR,
- "Failed to initialize GSM decoder.\n");
- return -EINVAL;
- }
- break;
-#ifdef HAVE_BCG729
- case AF_G729:
- state->src_frame_size = 10;
- state->src_samples_per_frame = 80;
- state->src.g729_dec = initBcg729DecoderChannel();
- if (!state->src.g729_dec) {
- LOGP(DLMGCP, LOGL_ERROR,
- "Failed to initialize G.729 decoder.\n");
- return -EINVAL;
- }
- break;
-#endif
- case AF_PCMU:
- case AF_PCMA:
- state->src_frame_size = 80;
- state->src_samples_per_frame = 80;
- break;
- default:
- break;
- }
-
- state->dst_fmt = dst_fmt;
-
- switch (state->dst_fmt) {
- case AF_L16:
- case AF_S16:
- state->dst_frame_size = 80*sizeof(short);
- state->dst_samples_per_frame = 80;
- break;
- case AF_GSM:
- state->dst_frame_size = sizeof(gsm_frame);
- state->dst_samples_per_frame = 160;
- state->dst.gsm_handle = gsm_create();
- if (!state->dst.gsm_handle) {
- LOGP(DLMGCP, LOGL_ERROR,
- "Failed to initialize GSM encoder.\n");
- return -EINVAL;
- }
- break;
-#ifdef HAVE_BCG729
- case AF_G729:
- state->dst_frame_size = 10;
- state->dst_samples_per_frame = 80;
- state->dst.g729_enc = initBcg729EncoderChannel();
- if (!state->dst.g729_enc) {
- LOGP(DLMGCP, LOGL_ERROR,
- "Failed to initialize G.729 decoder.\n");
- return -EINVAL;
- }
- break;
-#endif
- case AF_PCMU:
- case AF_PCMA:
- state->dst_frame_size = 80;
- state->dst_samples_per_frame = 80;
- break;
- default:
- break;
- }
-
- if (dst_end->force_output_ptime)
- state->dst_packet_duration = mgcp_rtp_packet_duration(endp, dst_end);
-
- LOGP(DLMGCP, LOGL_INFO,
- "Initialized RTP processing on: 0x%x "
- "conv: %d (%d, %d, %s) -> %d (%d, %d, %s)\n",
- ENDPOINT_NUMBER(endp),
- src_fmt, src_codec->payload_type, src_codec->rate, src_end->fmtp_extra,
- dst_fmt, dst_codec->payload_type, dst_codec->rate, dst_end->fmtp_extra);
-
- return 0;
-}
-
-void mgcp_transcoding_net_downlink_format(struct mgcp_endpoint *endp,
- int *payload_type,
- const char**audio_name,
- const char**fmtp_extra)
-{
- struct mgcp_process_rtp_state *state = endp->net_end.rtp_process_data;
- struct mgcp_rtp_codec *net_codec = &endp->net_end.codec;
- struct mgcp_rtp_codec *bts_codec = &endp->bts_end.codec;
-
- if (!state || net_codec->payload_type < 0) {
- *payload_type = bts_codec->payload_type;
- *audio_name = bts_codec->audio_name;
- *fmtp_extra = endp->bts_end.fmtp_extra;
- return;
- }
-
- *payload_type = net_codec->payload_type;
- *audio_name = net_codec->audio_name;
- *fmtp_extra = endp->net_end.fmtp_extra;
-}
-
-static int decode_audio(struct mgcp_process_rtp_state *state,
- uint8_t **src, size_t *nbytes)
-{
- while (*nbytes >= state->src_frame_size) {
- if (state->sample_cnt + state->src_samples_per_frame > ARRAY_SIZE(state->samples)) {
- LOGP(DLMGCP, LOGL_ERROR,
- "Sample buffer too small: %zu > %zu.\n",
- state->sample_cnt + state->src_samples_per_frame,
- ARRAY_SIZE(state->samples));
- return -ENOSPC;
- }
- switch (state->src_fmt) {
- case AF_GSM:
- if (gsm_decode(state->src.gsm_handle,
- (gsm_byte *)*src, state->samples + state->sample_cnt) < 0) {
- LOGP(DLMGCP, LOGL_ERROR,
- "Failed to decode GSM.\n");
- return -EINVAL;
- }
- break;
-#ifdef HAVE_BCG729
- case AF_G729:
- bcg729Decoder(state->src.g729_dec, *src, 0, state->samples + state->sample_cnt);
- break;
-#endif
- case AF_PCMU:
- ulaw_decode(*src, state->samples + state->sample_cnt,
- state->src_samples_per_frame);
- break;
- case AF_PCMA:
- alaw_decode(*src, state->samples + state->sample_cnt,
- state->src_samples_per_frame);
- break;
- case AF_S16:
- memmove(state->samples + state->sample_cnt, *src,
- state->src_frame_size);
- break;
- case AF_L16:
- l16_decode(*src, state->samples + state->sample_cnt,
- state->src_samples_per_frame);
- break;
- default:
- break;
- }
- *src += state->src_frame_size;
- *nbytes -= state->src_frame_size;
- state->sample_cnt += state->src_samples_per_frame;
- }
- return 0;
-}
-
-static int encode_audio(struct mgcp_process_rtp_state *state,
- uint8_t *dst, size_t buf_size, size_t max_samples)
-{
- int nbytes = 0;
- size_t nsamples = 0;
- /* Encode samples into dst */
- while (nsamples + state->dst_samples_per_frame <= max_samples) {
- if (nbytes + state->dst_frame_size > buf_size) {
- if (nbytes > 0)
- break;
-
- /* Not even one frame fits into the buffer */
- LOGP(DLMGCP, LOGL_INFO,
- "Encoding (RTP) buffer too small: %zu > %zu.\n",
- nbytes + state->dst_frame_size, buf_size);
- return -ENOSPC;
- }
- switch (state->dst_fmt) {
- case AF_GSM:
- gsm_encode(state->dst.gsm_handle,
- state->samples + state->sample_offs, dst);
- break;
-#ifdef HAVE_BCG729
- case AF_G729:
- bcg729Encoder(state->dst.g729_enc,
- state->samples + state->sample_offs, dst);
- break;
-#endif
- case AF_PCMU:
- ulaw_encode(state->samples + state->sample_offs, dst,
- state->src_samples_per_frame);
- break;
- case AF_PCMA:
- alaw_encode(state->samples + state->sample_offs, dst,
- state->src_samples_per_frame);
- break;
- case AF_S16:
- memmove(dst, state->samples + state->sample_offs,
- state->dst_frame_size);
- break;
- case AF_L16:
- l16_encode(state->samples + state->sample_offs, dst,
- state->src_samples_per_frame);
- break;
- default:
- break;
- }
- dst += state->dst_frame_size;
- nbytes += state->dst_frame_size;
- state->sample_offs += state->dst_samples_per_frame;
- nsamples += state->dst_samples_per_frame;
- }
- state->sample_cnt -= nsamples;
- return nbytes;
-}
-
-static struct mgcp_rtp_end *source_for_dest(struct mgcp_endpoint *endp,
- struct mgcp_rtp_end *dst_end)
-{
- if (&endp->bts_end == dst_end)
- return &endp->net_end;
- else if (&endp->net_end == dst_end)
- return &endp->bts_end;
- OSMO_ASSERT(0);
-}
-
-/*
- * With some modems we get offered multiple codecs
- * and we have selected one of them. It might not
- * be the right one and we need to detect this with
- * the first audio packets. One difficulty is that
- * we patch the rtp payload type in place, so we
- * need to discuss this.
- */
-struct mgcp_process_rtp_state *check_transcode_state(
- struct mgcp_endpoint *endp,
- struct mgcp_rtp_end *dst_end,
- struct rtp_hdr *rtp_hdr)
-{
- struct mgcp_rtp_end *src_end;
-
- /* Only deal with messages from net to bts */
- if (&endp->bts_end != dst_end)
- goto done;
-
- src_end = source_for_dest(endp, dst_end);
-
- /* Already patched */
- if (rtp_hdr->payload_type == dst_end->codec.payload_type)
- goto done;
- /* The payload we expect */
- if (rtp_hdr->payload_type == src_end->codec.payload_type)
- goto done;
- /* The matching alternate payload type? Then switch */
- if (rtp_hdr->payload_type == src_end->alt_codec.payload_type) {
- struct mgcp_config *cfg = endp->cfg;
- struct mgcp_rtp_codec tmp_codec = src_end->alt_codec;
- src_end->alt_codec = src_end->codec;
- src_end->codec = tmp_codec;
- cfg->setup_rtp_processing_cb(endp, &endp->net_end, &endp->bts_end);
- cfg->setup_rtp_processing_cb(endp, &endp->bts_end, &endp->net_end);
- }
-
-done:
- return dst_end->rtp_process_data;
-}
-
-int mgcp_transcoding_process_rtp(struct mgcp_endpoint *endp,
- struct mgcp_rtp_end *dst_end,
- char *data, int *len, int buf_size)
-{
- struct mgcp_process_rtp_state *state;
- const size_t rtp_hdr_size = sizeof(struct rtp_hdr);
- struct rtp_hdr *rtp_hdr = (struct rtp_hdr *) data;
- char *payload_data = (char *) &rtp_hdr->data[0];
- int payload_len = *len - rtp_hdr_size;
- uint8_t *src = (uint8_t *)payload_data;
- uint8_t *dst = (uint8_t *)payload_data;
- size_t nbytes = payload_len;
- size_t nsamples;
- size_t max_samples;
- uint32_t ts_no;
- int rc;
-
- state = check_transcode_state(endp, dst_end, rtp_hdr);
- if (!state)
- return 0;
-
- if (state->src_fmt == state->dst_fmt) {
- if (!state->dst_packet_duration)
- return 0;
-
- /* TODO: repackage without transcoding */
- }
-
- /* If the remaining samples do not fit into a fixed ptime,
- * a) discard them, if the next packet is much later
- * b) add silence and * send it, if the current packet is not
- * yet too late
- * c) append the sample data, if the timestamp matches exactly
- */
-
- /* TODO: check payload type (-> G.711 comfort noise) */
-
- if (payload_len > 0) {
- ts_no = ntohl(rtp_hdr->timestamp);
- if (!state->is_running) {
- state->next_seq = ntohs(rtp_hdr->sequence);
- state->next_time = ts_no;
- state->is_running = 1;
- }
-
-
- if (state->sample_cnt > 0) {
- int32_t delta = ts_no - state->next_time;
- /* TODO: check sequence? reordering? packet loss? */
-
- if (delta > state->sample_cnt) {
- /* There is a time gap between the last packet
- * and the current one. Just discard the
- * partial data that is left in the buffer.
- * TODO: This can be improved by adding silence
- * instead if the delta is small enough.
- */
- LOGP(DLMGCP, LOGL_NOTICE,
- "0x%x dropping sample buffer due delta=%d sample_cnt=%zu\n",
- ENDPOINT_NUMBER(endp), delta, state->sample_cnt);
- state->sample_cnt = 0;
- state->next_time = ts_no;
- } else if (delta < 0) {
- LOGP(DLMGCP, LOGL_NOTICE,
- "RTP time jumps backwards, delta = %d, "
- "discarding buffered samples\n",
- delta);
- state->sample_cnt = 0;
- state->sample_offs = 0;
- return -EAGAIN;
- }
-
- /* Make sure the samples start without offset */
- if (state->sample_offs && state->sample_cnt)
- memmove(&state->samples[0],
- &state->samples[state->sample_offs],
- state->sample_cnt *
- sizeof(state->samples[0]));
- }
-
- state->sample_offs = 0;
-
- /* Append decoded audio to samples */
- decode_audio(state, &src, &nbytes);
-
- if (nbytes > 0)
- LOGP(DLMGCP, LOGL_NOTICE,
- "Skipped audio frame in RTP packet: %zu octets\n",
- nbytes);
- } else
- ts_no = state->next_time;
-
- if (state->sample_cnt < state->dst_packet_duration)
- return -EAGAIN;
-
- max_samples =
- state->dst_packet_duration ?
- state->dst_packet_duration : state->sample_cnt;
-
- nsamples = state->sample_cnt;
-
- rc = encode_audio(state, dst, buf_size, max_samples);
- /*
- * There were no samples to encode?
- * TODO: how does this work for comfort noise?
- */
- if (rc == 0)
- return -ENOMSG;
- /* Any other error during the encoding */
- if (rc < 0)
- return rc;
-
- nsamples -= state->sample_cnt;
-
- *len = rtp_hdr_size + rc;
- rtp_hdr->sequence = htons(state->next_seq);
- rtp_hdr->timestamp = htonl(ts_no);
-
- state->next_seq += 1;
- state->next_time = ts_no + nsamples;
-
- /*
- * XXX: At this point we should always have consumed
- * samples. So doing OSMO_ASSERT(nsamples > 0) and returning
- * rtp_hdr_size should be fine.
- */
- return nsamples ? rtp_hdr_size : 0;
-}
diff --git a/src/libosmo-mgcp/mgcp_vty.c b/src/libosmo-mgcp/mgcp_vty.c
index a48e205..aaa0429 100644
--- a/src/libosmo-mgcp/mgcp_vty.c
+++ b/src/libosmo-mgcp/mgcp_vty.c
@@ -21,12 +21,11 @@
*
*/
-
#include <osmocom/core/talloc.h>
-
-#include <osmocom/legacy_mgcp/mgcp.h>
-#include <osmocom/legacy_mgcp/mgcp_internal.h>
-#include <osmocom/legacy_mgcp/vty.h>
+#include <osmocom/mgcp/mgcp.h>
+#include <osmocom/mgcp/mgcp_internal.h>
+#include <osmocom/mgcp/vty.h>
+#include <osmocom/mgcp/mgcp_conn.h>
#include <string.h>
@@ -48,9 +47,6 @@
return trunk;
}
-/*
- * vty code for mgcp below
- */
struct cmd_node mgcp_node = {
MGCP_NODE,
"%s(config-mgcp)# ",
@@ -68,26 +64,15 @@
vty_out(vty, "mgcp%s", VTY_NEWLINE);
if (g_cfg->local_ip)
vty_out(vty, " local ip %s%s", g_cfg->local_ip, VTY_NEWLINE);
- if (g_cfg->bts_ip && strlen(g_cfg->bts_ip) != 0)
- vty_out(vty, " bts ip %s%s", g_cfg->bts_ip, VTY_NEWLINE);
vty_out(vty, " bind ip %s%s", g_cfg->source_addr, VTY_NEWLINE);
vty_out(vty, " bind port %u%s", g_cfg->source_port, VTY_NEWLINE);
+ vty_out(vty, " rtp net-range %u %u%s",
+ g_cfg->net_ports.range_start, g_cfg->net_ports.range_end,
+ VTY_NEWLINE);
- if (g_cfg->bts_ports.mode == PORT_ALLOC_STATIC)
- vty_out(vty, " rtp bts-base %u%s", g_cfg->bts_ports.base_port, VTY_NEWLINE);
- else
- vty_out(vty, " rtp bts-range %u %u%s",
- g_cfg->bts_ports.range_start, g_cfg->bts_ports.range_end, VTY_NEWLINE);
- if (g_cfg->bts_ports.bind_addr)
- vty_out(vty, " rtp bts-bind-ip %s%s", g_cfg->bts_ports.bind_addr, VTY_NEWLINE);
-
- if (g_cfg->net_ports.mode == PORT_ALLOC_STATIC)
- vty_out(vty, " rtp net-base %u%s", g_cfg->net_ports.base_port, VTY_NEWLINE);
- else
- vty_out(vty, " rtp net-range %u %u%s",
- g_cfg->net_ports.range_start, g_cfg->net_ports.range_end, VTY_NEWLINE);
if (g_cfg->net_ports.bind_addr)
- vty_out(vty, " rtp net-bind-ip %s%s", g_cfg->net_ports.bind_addr, VTY_NEWLINE);
+ vty_out(vty, " rtp net-bind-ip %s%s",
+ g_cfg->net_ports.bind_addr, VTY_NEWLINE);
vty_out(vty, " rtp ip-dscp %d%s", g_cfg->endp_dscp, VTY_NEWLINE);
if (g_cfg->trunk.keepalive_interval == MGCP_KEEPALIVE_ONCE)
@@ -102,11 +87,14 @@
vty_out(vty, " rtcp-omit%s", VTY_NEWLINE);
else
vty_out(vty, " no rtcp-omit%s", VTY_NEWLINE);
- if (g_cfg->trunk.force_constant_ssrc || g_cfg->trunk.force_aligned_timing) {
+ if (g_cfg->trunk.force_constant_ssrc
+ || g_cfg->trunk.force_aligned_timing) {
vty_out(vty, " %srtp-patch ssrc%s",
- g_cfg->trunk.force_constant_ssrc ? "" : "no ", VTY_NEWLINE);
+ g_cfg->trunk.force_constant_ssrc ? "" : "no ",
+ VTY_NEWLINE);
vty_out(vty, " %srtp-patch timestamp%s",
- g_cfg->trunk.force_aligned_timing ? "" : "no ", VTY_NEWLINE);
+ g_cfg->trunk.force_aligned_timing ? "" : "no ",
+ VTY_NEWLINE);
} else
vty_out(vty, " no rtp-patch%s", VTY_NEWLINE);
if (g_cfg->trunk.audio_payload != -1)
@@ -122,23 +110,17 @@
g_cfg->trunk.audio_send_ptime ? "" : "no ", VTY_NEWLINE);
vty_out(vty, " %ssdp audio-payload send-name%s",
g_cfg->trunk.audio_send_name ? "" : "no ", VTY_NEWLINE);
- vty_out(vty, " loop %u%s", !!g_cfg->trunk.audio_loop, VTY_NEWLINE);
- vty_out(vty, " number endpoints %u%s", g_cfg->trunk.number_endpoints - 1, VTY_NEWLINE);
+ vty_out(vty, " loop %u%s", ! !g_cfg->trunk.audio_loop, VTY_NEWLINE);
+ vty_out(vty, " number endpoints %u%s",
+ g_cfg->trunk.number_endpoints - 1, VTY_NEWLINE);
vty_out(vty, " %sallow-transcoding%s",
g_cfg->trunk.no_audio_transcoding ? "no " : "", VTY_NEWLINE);
if (g_cfg->call_agent_addr)
- vty_out(vty, " call-agent ip %s%s", g_cfg->call_agent_addr, VTY_NEWLINE);
- if (g_cfg->transcoder_ip)
- vty_out(vty, " transcoder-mgw %s%s", g_cfg->transcoder_ip, VTY_NEWLINE);
-
- if (g_cfg->transcoder_ports.mode == PORT_ALLOC_STATIC)
- vty_out(vty, " rtp transcoder-base %u%s", g_cfg->transcoder_ports.base_port, VTY_NEWLINE);
- else
- vty_out(vty, " rtp transcoder-range %u %u%s",
- g_cfg->transcoder_ports.range_start, g_cfg->transcoder_ports.range_end, VTY_NEWLINE);
- if (g_cfg->bts_force_ptime > 0)
- vty_out(vty, " rtp force-ptime %d%s", g_cfg->bts_force_ptime, VTY_NEWLINE);
- vty_out(vty, " transcoder-remote-base %u%s", g_cfg->transcoder_remote_base, VTY_NEWLINE);
+ vty_out(vty, " call-agent ip %s%s", g_cfg->call_agent_addr,
+ VTY_NEWLINE);
+ if (g_cfg->force_ptime > 0)
+ vty_out(vty, " rtp force-ptime %d%s", g_cfg->force_ptime,
+ VTY_NEWLINE);
switch (g_cfg->osmux) {
case OSMUX_USAGE_ON:
@@ -167,13 +149,12 @@
return CMD_SUCCESS;
}
-static void dump_rtp_end(const char *end_name, struct vty *vty,
- struct mgcp_rtp_state *state, struct mgcp_rtp_end *end)
+static void dump_rtp_end(struct vty *vty, struct mgcp_rtp_state *state,
+ struct mgcp_rtp_end *end)
{
struct mgcp_rtp_codec *codec = &end->codec;
vty_out(vty,
- " %s%s"
" Timestamp Errs: %d->%d%s"
" Dropped Packets: %d%s"
" Payload Type: %d Rate: %u Channels: %d %s"
@@ -181,20 +162,22 @@
" FPP: %d Packet Duration: %u%s"
" FMTP-Extra: %s Audio-Name: %s Sub-Type: %s%s"
" Output-Enabled: %d Force-PTIME: %d%s",
- end_name, VTY_NEWLINE,
state->in_stream.err_ts_counter,
state->out_stream.err_ts_counter, VTY_NEWLINE,
end->dropped_packets, VTY_NEWLINE,
codec->payload_type, codec->rate, codec->channels, VTY_NEWLINE,
- codec->frame_duration_num, codec->frame_duration_den, VTY_NEWLINE,
- end->frames_per_packet, end->packet_duration_ms, VTY_NEWLINE,
- end->fmtp_extra, codec->audio_name, codec->subtype_name, VTY_NEWLINE,
- end->output_enabled, end->force_output_ptime, VTY_NEWLINE);
+ codec->frame_duration_num, codec->frame_duration_den,
+ VTY_NEWLINE, end->frames_per_packet, end->packet_duration_ms,
+ VTY_NEWLINE, end->fmtp_extra, codec->audio_name,
+ codec->subtype_name, VTY_NEWLINE, end->output_enabled,
+ end->force_output_ptime, VTY_NEWLINE);
}
-static void dump_trunk(struct vty *vty, struct mgcp_trunk_config *cfg, int verbose)
+static void dump_trunk(struct vty *vty, struct mgcp_trunk_config *cfg,
+ int verbose)
{
int i;
+ struct mgcp_conn *conn;
vty_out(vty, "%s trunk nr %d with %d endpoints:%s",
cfg->trunk_type == MGCP_TRUNK_VIRTUAL ? "Virtual" : "E1",
@@ -207,20 +190,22 @@
for (i = 1; i < cfg->number_endpoints; ++i) {
struct mgcp_endpoint *endp = &cfg->endpoints[i];
- vty_out(vty,
- " Endpoint 0x%.2x: CI: %d net: %u/%u bts: %u/%u on %s "
- "traffic received bts: %u remote: %u transcoder: %u/%u%s",
- i, endp->ci,
- ntohs(endp->net_end.rtp_port), ntohs(endp->net_end.rtcp_port),
- ntohs(endp->bts_end.rtp_port), ntohs(endp->bts_end.rtcp_port),
- inet_ntoa(endp->bts_end.addr),
- endp->bts_end.packets, endp->net_end.packets,
- endp->trans_net.packets, endp->trans_bts.packets,
- VTY_NEWLINE);
- if (verbose && endp->allocated) {
- dump_rtp_end("Net->BTS", vty, &endp->bts_state, &endp->bts_end);
- dump_rtp_end("BTS->Net", vty, &endp->net_state, &endp->net_end);
+ vty_out(vty, "Endpoint 0x%.2x:%s", i, VTY_NEWLINE);
+
+ llist_for_each_entry(conn, &endp->conns, entry) {
+ vty_out(vty, " CONN: %s%s",
+ mgcp_conn_dump(conn), VTY_NEWLINE);
+
+ if (verbose) {
+ /* FIXME: Also add verbosity for other
+ * connection types (E1) as soon as
+ * the implementation is available */
+ if (conn->type == MGCP_CONN_TYPE_RTP) {
+ dump_rtp_end(vty, &conn->u.rtp.state,
+ &conn->u.rtp.end);
+ }
+ }
}
}
}
@@ -237,18 +222,16 @@
dump_trunk(vty, &g_cfg->trunk, show_stats);
llist_for_each_entry(trunk, &g_cfg->trunks, entry)
- dump_trunk(vty, trunk, show_stats);
+ dump_trunk(vty, trunk, show_stats);
if (g_cfg->osmux)
- vty_out(vty, "Osmux used CID: %d%s", osmux_used_cid(), VTY_NEWLINE);
+ vty_out(vty, "Osmux used CID: %d%s", osmux_used_cid(),
+ VTY_NEWLINE);
return CMD_SUCCESS;
}
-DEFUN(cfg_mgcp,
- cfg_mgcp_cmd,
- "mgcp",
- "Configure the MGCP")
+DEFUN(cfg_mgcp, cfg_mgcp_cmd, "mgcp", "Configure the MGCP")
{
vty->node = MGCP_NODE;
return CMD_SUCCESS;
@@ -258,32 +241,16 @@
cfg_mgcp_local_ip_cmd,
"local ip A.B.C.D",
"Local options for the SDP record\n"
- IP_STR
- "IPv4 Address to use in SDP record\n")
+ IP_STR "IPv4 Address to use in SDP record\n")
{
osmo_talloc_replace_string(g_cfg, &g_cfg->local_ip, argv[0]);
return CMD_SUCCESS;
}
-DEFUN(cfg_mgcp_bts_ip,
- cfg_mgcp_bts_ip_cmd,
- "bts ip A.B.C.D",
- "BTS Audio source/destination options\n"
- IP_STR
- "IPv4 Address of the BTS\n")
-{
- osmo_talloc_replace_string(g_cfg, &g_cfg->bts_ip, argv[0]);
- inet_aton(g_cfg->bts_ip, &g_cfg->bts_in);
- return CMD_SUCCESS;
-}
-
#define BIND_STR "Listen/Bind related socket option\n"
DEFUN(cfg_mgcp_bind_ip,
cfg_mgcp_bind_ip_cmd,
- "bind ip A.B.C.D",
- BIND_STR
- IP_STR
- "IPv4 Address to bind to\n")
+ "bind ip A.B.C.D", BIND_STR IP_STR "IPv4 Address to bind to\n")
{
osmo_talloc_replace_string(g_cfg, &g_cfg->source_addr, argv[0]);
return CMD_SUCCESS;
@@ -292,9 +259,7 @@
DEFUN(cfg_mgcp_bind_port,
cfg_mgcp_bind_port_cmd,
"bind port <0-65534>",
- BIND_STR
- "Port information\n"
- "UDP port to listen for MGCP messages\n")
+ BIND_STR "Port information\n" "UDP port to listen for MGCP messages\n")
{
unsigned int port = atoi(argv[0]);
g_cfg->source_port = port;
@@ -305,55 +270,24 @@
cfg_mgcp_bind_early_cmd,
"bind early (0|1)",
BIND_STR
- "Bind local ports on start up\n"
- "Bind on demand\n" "Bind on startup\n")
+ "Bind local ports on start up\n" "Bind on demand\n" "Bind on startup\n")
{
vty_out(vty, "bind early is deprecated, remove it from the config.\n");
return CMD_WARNING;
}
-static void parse_base(struct mgcp_port_range *range, const char **argv)
-{
- unsigned int port = atoi(argv[0]);
- range->mode = PORT_ALLOC_STATIC;
- range->base_port = port;
-}
-
static void parse_range(struct mgcp_port_range *range, const char **argv)
{
- range->mode = PORT_ALLOC_DYNAMIC;
range->range_start = atoi(argv[0]);
range->range_end = atoi(argv[1]);
- range->last_port = g_cfg->bts_ports.range_start;
+ range->last_port = g_cfg->net_ports.range_start;
}
-
#define RTP_STR "RTP configuration\n"
-#define BTS_START_STR "First UDP port allocated for the BTS side\n"
-#define NET_START_STR "First UDP port allocated for the NET side\n"
#define UDP_PORT_STR "UDP Port number\n"
-DEFUN(cfg_mgcp_rtp_bts_base_port,
- cfg_mgcp_rtp_bts_base_port_cmd,
- "rtp bts-base <0-65534>",
- RTP_STR
- BTS_START_STR
- UDP_PORT_STR)
-{
- parse_base(&g_cfg->bts_ports, argv);
- return CMD_SUCCESS;
-}
-
+#define NET_START_STR "First UDP port allocated\n"
#define RANGE_START_STR "Start of the range of ports\n"
#define RANGE_END_STR "End of the range of ports\n"
-DEFUN(cfg_mgcp_rtp_bts_range,
- cfg_mgcp_rtp_bts_range_cmd,
- "rtp bts-range <0-65534> <0-65534>",
- RTP_STR "Range of ports to use for the BTS side\n"
- RANGE_START_STR RANGE_END_STR)
-{
- parse_range(&g_cfg->bts_ports, argv);
- return CMD_SUCCESS;
-}
DEFUN(cfg_mgcp_rtp_net_range,
cfg_mgcp_rtp_net_range_cmd,
@@ -365,58 +299,6 @@
return CMD_SUCCESS;
}
-DEFUN(cfg_mgcp_rtp_net_base_port,
- cfg_mgcp_rtp_net_base_port_cmd,
- "rtp net-base <0-65534>",
- RTP_STR NET_START_STR UDP_PORT_STR)
-{
- parse_base(&g_cfg->net_ports, argv);
- return CMD_SUCCESS;
-}
-
-ALIAS_DEPRECATED(cfg_mgcp_rtp_bts_base_port, cfg_mgcp_rtp_base_port_cmd,
- "rtp base <0-65534>",
- RTP_STR BTS_START_STR UDP_PORT_STR)
-
-DEFUN(cfg_mgcp_rtp_transcoder_range,
- cfg_mgcp_rtp_transcoder_range_cmd,
- "rtp transcoder-range <0-65534> <0-65534>",
- RTP_STR "Range of ports to use for the Transcoder\n"
- RANGE_START_STR RANGE_END_STR)
-{
- parse_range(&g_cfg->transcoder_ports, argv);
- return CMD_SUCCESS;
-}
-
-DEFUN(cfg_mgcp_rtp_transcoder_base,
- cfg_mgcp_rtp_transcoder_base_cmd,
- "rtp transcoder-base <0-65534>",
- RTP_STR "First UDP port allocated for the Transcoder side\n"
- UDP_PORT_STR)
-{
- parse_base(&g_cfg->transcoder_ports, argv);
- return CMD_SUCCESS;
-}
-
-DEFUN(cfg_mgcp_rtp_bts_bind_ip,
- cfg_mgcp_rtp_bts_bind_ip_cmd,
- "rtp bts-bind-ip A.B.C.D",
- RTP_STR "Bind endpoints facing the BTS\n" "Address to bind to\n")
-{
- osmo_talloc_replace_string(g_cfg, &g_cfg->bts_ports.bind_addr, argv[0]);
- return CMD_SUCCESS;
-}
-
-DEFUN(cfg_mgcp_rtp_no_bts_bind_ip,
- cfg_mgcp_rtp_no_bts_bind_ip_cmd,
- "no rtp bts-bind-ip",
- NO_STR RTP_STR "Bind endpoints facing the BTS\n" "Address to bind to\n")
-{
- talloc_free(g_cfg->bts_ports.bind_addr);
- g_cfg->bts_ports.bind_addr = NULL;
- return CMD_SUCCESS;
-}
-
DEFUN(cfg_mgcp_rtp_net_bind_ip,
cfg_mgcp_rtp_net_bind_ip_cmd,
"rtp net-bind-ip A.B.C.D",
@@ -429,7 +311,8 @@
DEFUN(cfg_mgcp_rtp_no_net_bind_ip,
cfg_mgcp_rtp_no_net_bind_ip_cmd,
"no rtp net-bind-ip",
- NO_STR RTP_STR "Bind endpoints facing the Network\n" "Address to bind to\n")
+ NO_STR RTP_STR "Bind endpoints facing the Network\n"
+ "Address to bind to\n")
{
talloc_free(g_cfg->net_ports.bind_addr);
g_cfg->net_ports.bind_addr = NULL;
@@ -448,28 +331,25 @@
}
ALIAS_DEPRECATED(cfg_mgcp_rtp_ip_dscp, cfg_mgcp_rtp_ip_tos_cmd,
- "rtp ip-tos <0-255>",
- RTP_STR
- "Apply IP_TOS to the audio stream\n" "The DSCP value\n")
-
-#define FORCE_PTIME_STR "Force a fixed ptime for packets sent to the BTS"
-DEFUN(cfg_mgcp_rtp_force_ptime,
+ "rtp ip-tos <0-255>",
+ RTP_STR
+ "Apply IP_TOS to the audio stream\n" "The DSCP value\n")
+#define FORCE_PTIME_STR "Force a fixed ptime for packets sent"
+ DEFUN(cfg_mgcp_rtp_force_ptime,
cfg_mgcp_rtp_force_ptime_cmd,
"rtp force-ptime (10|20|40)",
RTP_STR FORCE_PTIME_STR
- "The required ptime (packet duration) in ms\n"
- "10 ms\n20 ms\n40 ms\n")
+ "The required ptime (packet duration) in ms\n" "10 ms\n20 ms\n40 ms\n")
{
- g_cfg->bts_force_ptime = atoi(argv[0]);
+ g_cfg->force_ptime = atoi(argv[0]);
return CMD_SUCCESS;
}
DEFUN(cfg_mgcp_no_rtp_force_ptime,
cfg_mgcp_no_rtp_force_ptime_cmd,
- "no rtp force-ptime",
- NO_STR RTP_STR FORCE_PTIME_STR)
+ "no rtp force-ptime", NO_STR RTP_STR FORCE_PTIME_STR)
{
- g_cfg->bts_force_ptime = 0;
+ g_cfg->force_ptime = 0;
return CMD_SUCCESS;
}
@@ -490,8 +370,7 @@
DEFUN(cfg_mgcp_allow_transcoding,
cfg_mgcp_allow_transcoding_cmd,
- "allow-transcoding",
- "Allow transcoding\n")
+ "allow-transcoding", "Allow transcoding\n")
{
g_cfg->trunk.no_audio_transcoding = 0;
return CMD_SUCCESS;
@@ -499,8 +378,7 @@
DEFUN(cfg_mgcp_no_allow_transcoding,
cfg_mgcp_no_allow_transcoding_cmd,
- "no allow-transcoding",
- NO_STR "Allow transcoding\n")
+ "no allow-transcoding", NO_STR "Allow transcoding\n")
{
g_cfg->trunk.no_audio_transcoding = 1;
return CMD_SUCCESS;
@@ -511,20 +389,19 @@
DEFUN(cfg_mgcp_sdp_payload_number,
cfg_mgcp_sdp_payload_number_cmd,
"sdp audio-payload number <0-255>",
- SDP_STR AUDIO_STR
- "Number\n" "Payload number\n")
+ SDP_STR AUDIO_STR "Number\n" "Payload number\n")
{
unsigned int payload = atoi(argv[0]);
g_cfg->trunk.audio_payload = payload;
return CMD_SUCCESS;
}
-ALIAS_DEPRECATED(cfg_mgcp_sdp_payload_number, cfg_mgcp_sdp_payload_number_cmd_old,
- "sdp audio payload number <0-255>",
- SDP_STR AUDIO_STR AUDIO_STR "Number\n" "Payload number\n")
-
+ALIAS_DEPRECATED(cfg_mgcp_sdp_payload_number,
+ cfg_mgcp_sdp_payload_number_cmd_old,
+ "sdp audio payload number <0-255>",
+ SDP_STR AUDIO_STR AUDIO_STR "Number\n" "Payload number\n")
-DEFUN(cfg_mgcp_sdp_payload_name,
+ DEFUN(cfg_mgcp_sdp_payload_name,
cfg_mgcp_sdp_payload_name_cmd,
"sdp audio-payload name NAME",
SDP_STR AUDIO_STR "Name\n" "Payload name\n")
@@ -534,14 +411,13 @@
}
ALIAS_DEPRECATED(cfg_mgcp_sdp_payload_name, cfg_mgcp_sdp_payload_name_cmd_old,
- "sdp audio payload name NAME",
- SDP_STR AUDIO_STR AUDIO_STR "Name\n" "Payload name\n")
+ "sdp audio payload name NAME",
+ SDP_STR AUDIO_STR AUDIO_STR "Name\n" "Payload name\n")
-DEFUN(cfg_mgcp_sdp_payload_send_ptime,
+ DEFUN(cfg_mgcp_sdp_payload_send_ptime,
cfg_mgcp_sdp_payload_send_ptime_cmd,
"sdp audio-payload send-ptime",
- SDP_STR AUDIO_STR
- "Send SDP ptime (packet duration) attribute\n")
+ SDP_STR AUDIO_STR "Send SDP ptime (packet duration) attribute\n")
{
g_cfg->trunk.audio_send_ptime = 1;
return CMD_SUCCESS;
@@ -550,8 +426,7 @@
DEFUN(cfg_mgcp_no_sdp_payload_send_ptime,
cfg_mgcp_no_sdp_payload_send_ptime_cmd,
"no sdp audio-payload send-ptime",
- NO_STR SDP_STR AUDIO_STR
- "Send SDP ptime (packet duration) attribute\n")
+ NO_STR SDP_STR AUDIO_STR "Send SDP ptime (packet duration) attribute\n")
{
g_cfg->trunk.audio_send_ptime = 0;
return CMD_SUCCESS;
@@ -560,8 +435,7 @@
DEFUN(cfg_mgcp_sdp_payload_send_name,
cfg_mgcp_sdp_payload_send_name_cmd,
"sdp audio-payload send-name",
- SDP_STR AUDIO_STR
- "Send SDP rtpmap with the audio name\n")
+ SDP_STR AUDIO_STR "Send SDP rtpmap with the audio name\n")
{
g_cfg->trunk.audio_send_name = 1;
return CMD_SUCCESS;
@@ -570,8 +444,7 @@
DEFUN(cfg_mgcp_no_sdp_payload_send_name,
cfg_mgcp_no_sdp_payload_send_name_cmd,
"no sdp audio-payload send-name",
- NO_STR SDP_STR AUDIO_STR
- "Send SDP rtpmap with the audio name\n")
+ NO_STR SDP_STR AUDIO_STR "Send SDP rtpmap with the audio name\n")
{
g_cfg->trunk.audio_send_name = 0;
return CMD_SUCCESS;
@@ -580,8 +453,7 @@
DEFUN(cfg_mgcp_loop,
cfg_mgcp_loop_cmd,
"loop (0|1)",
- "Loop audio for all endpoints on main trunk\n"
- "Don't Loop\n" "Loop\n")
+ "Loop audio for all endpoints on main trunk\n" "Don't Loop\n" "Loop\n")
{
if (g_cfg->osmux) {
vty_out(vty, "Cannot use `loop' with `osmux'.%s", VTY_NEWLINE);
@@ -601,6 +473,16 @@
return CMD_SUCCESS;
}
+DEFUN(cfg_mgcp_rtp_accept_all,
+ cfg_mgcp_rtp_accept_all_cmd,
+ "rtp-accept-all (0|1)",
+ "Accept all RTP packets, even when the originating IP/Port does not match\n"
+ "enable filter\n" "disable filter\n")
+{
+ g_cfg->trunk.rtp_accept_all = atoi(argv[0]);
+ return CMD_SUCCESS;
+}
+
DEFUN(cfg_mgcp_number_endp,
cfg_mgcp_number_endp_cmd,
"number endpoints <0-65534>",
@@ -611,19 +493,14 @@
return CMD_SUCCESS;
}
-DEFUN(cfg_mgcp_omit_rtcp,
- cfg_mgcp_omit_rtcp_cmd,
- "rtcp-omit",
- RTCP_OMIT_STR)
+DEFUN(cfg_mgcp_omit_rtcp, cfg_mgcp_omit_rtcp_cmd, "rtcp-omit", RTCP_OMIT_STR)
{
g_cfg->trunk.omit_rtcp = 1;
return CMD_SUCCESS;
}
DEFUN(cfg_mgcp_no_omit_rtcp,
- cfg_mgcp_no_omit_rtcp_cmd,
- "no rtcp-omit",
- NO_STR RTCP_OMIT_STR)
+ cfg_mgcp_no_omit_rtcp_cmd, "no rtcp-omit", NO_STR RTCP_OMIT_STR)
{
g_cfg->trunk.omit_rtcp = 0;
return CMD_SUCCESS;
@@ -631,10 +508,7 @@
DEFUN(cfg_mgcp_patch_rtp_ssrc,
cfg_mgcp_patch_rtp_ssrc_cmd,
- "rtp-patch ssrc",
- RTP_PATCH_STR
- "Force a fixed SSRC\n"
- )
+ "rtp-patch ssrc", RTP_PATCH_STR "Force a fixed SSRC\n")
{
g_cfg->trunk.force_constant_ssrc = 1;
return CMD_SUCCESS;
@@ -642,10 +516,7 @@
DEFUN(cfg_mgcp_no_patch_rtp_ssrc,
cfg_mgcp_no_patch_rtp_ssrc_cmd,
- "no rtp-patch ssrc",
- NO_STR RTP_PATCH_STR
- "Force a fixed SSRC\n"
- )
+ "no rtp-patch ssrc", NO_STR RTP_PATCH_STR "Force a fixed SSRC\n")
{
g_cfg->trunk.force_constant_ssrc = 0;
return CMD_SUCCESS;
@@ -653,10 +524,7 @@
DEFUN(cfg_mgcp_patch_rtp_ts,
cfg_mgcp_patch_rtp_ts_cmd,
- "rtp-patch timestamp",
- RTP_PATCH_STR
- "Adjust RTP timestamp\n"
- )
+ "rtp-patch timestamp", RTP_PATCH_STR "Adjust RTP timestamp\n")
{
g_cfg->trunk.force_aligned_timing = 1;
return CMD_SUCCESS;
@@ -664,19 +532,14 @@
DEFUN(cfg_mgcp_no_patch_rtp_ts,
cfg_mgcp_no_patch_rtp_ts_cmd,
- "no rtp-patch timestamp",
- NO_STR RTP_PATCH_STR
- "Adjust RTP timestamp\n"
- )
+ "no rtp-patch timestamp", NO_STR RTP_PATCH_STR "Adjust RTP timestamp\n")
{
g_cfg->trunk.force_aligned_timing = 0;
return CMD_SUCCESS;
}
DEFUN(cfg_mgcp_no_patch_rtp,
- cfg_mgcp_no_patch_rtp_cmd,
- "no rtp-patch",
- NO_STR RTP_PATCH_STR)
+ cfg_mgcp_no_patch_rtp_cmd, "no rtp-patch", NO_STR RTP_PATCH_STR)
{
g_cfg->trunk.force_constant_ssrc = 0;
g_cfg->trunk.force_aligned_timing = 0;
@@ -686,9 +549,7 @@
DEFUN(cfg_mgcp_rtp_keepalive,
cfg_mgcp_rtp_keepalive_cmd,
"rtp keep-alive <1-120>",
- RTP_STR RTP_KEEPALIVE_STR
- "Keep alive interval in secs\n"
- )
+ RTP_STR RTP_KEEPALIVE_STR "Keep alive interval in secs\n")
{
mgcp_trunk_set_keepalive(&g_cfg->trunk, atoi(argv[0]));
return CMD_SUCCESS;
@@ -697,9 +558,7 @@
DEFUN(cfg_mgcp_rtp_keepalive_once,
cfg_mgcp_rtp_keepalive_once_cmd,
"rtp keep-alive once",
- RTP_STR RTP_KEEPALIVE_STR
- "Send dummy packet only once after CRCX/MDCX\n"
- )
+ RTP_STR RTP_KEEPALIVE_STR "Send dummy packet only once after CRCX/MDCX\n")
{
mgcp_trunk_set_keepalive(&g_cfg->trunk, MGCP_KEEPALIVE_ONCE);
return CMD_SUCCESS;
@@ -707,71 +566,29 @@
DEFUN(cfg_mgcp_no_rtp_keepalive,
cfg_mgcp_no_rtp_keepalive_cmd,
- "no rtp keep-alive",
- NO_STR RTP_STR RTP_KEEPALIVE_STR
- )
+ "no rtp keep-alive", NO_STR RTP_STR RTP_KEEPALIVE_STR)
{
mgcp_trunk_set_keepalive(&g_cfg->trunk, 0);
return CMD_SUCCESS;
}
-
-
#define CALL_AGENT_STR "Callagent information\n"
DEFUN(cfg_mgcp_agent_addr,
cfg_mgcp_agent_addr_cmd,
"call-agent ip A.B.C.D",
- CALL_AGENT_STR IP_STR
- "IPv4 Address of the callagent\n")
+ CALL_AGENT_STR IP_STR "IPv4 Address of the callagent\n")
{
osmo_talloc_replace_string(g_cfg, &g_cfg->call_agent_addr, argv[0]);
return CMD_SUCCESS;
}
ALIAS_DEPRECATED(cfg_mgcp_agent_addr, cfg_mgcp_agent_addr_cmd_old,
- "call agent ip A.B.C.D",
- CALL_AGENT_STR CALL_AGENT_STR IP_STR
- "IPv4 Address of the callagent\n")
-
+ "call agent ip A.B.C.D",
+ CALL_AGENT_STR CALL_AGENT_STR IP_STR
+ "IPv4 Address of the callagent\n")
-DEFUN(cfg_mgcp_transcoder,
- cfg_mgcp_transcoder_cmd,
- "transcoder-mgw A.B.C.D",
- "Use a MGW to detranscoder RTP\n"
- "The IP address of the MGW")
-{
- osmo_talloc_replace_string(g_cfg, &g_cfg->transcoder_ip, argv[0]);
- inet_aton(g_cfg->transcoder_ip, &g_cfg->transcoder_in);
-
- return CMD_SUCCESS;
-}
-
-DEFUN(cfg_mgcp_no_transcoder,
- cfg_mgcp_no_transcoder_cmd,
- "no transcoder-mgw",
- NO_STR "Disable the transcoding\n")
-{
- if (g_cfg->transcoder_ip) {
- LOGP(DLMGCP, LOGL_NOTICE, "Disabling transcoding on future calls.\n");
- talloc_free(g_cfg->transcoder_ip);
- g_cfg->transcoder_ip = NULL;
- }
-
- return CMD_SUCCESS;
-}
-
-DEFUN(cfg_mgcp_transcoder_remote_base,
- cfg_mgcp_transcoder_remote_base_cmd,
- "transcoder-remote-base <0-65534>",
- "Set the base port for the transcoder\n" "The RTP base port on the transcoder")
-{
- g_cfg->transcoder_remote_base = atoi(argv[0]);
- return CMD_SUCCESS;
-}
-
-DEFUN(cfg_mgcp_trunk, cfg_mgcp_trunk_cmd,
- "trunk <1-64>",
- "Configure a SS7 trunk\n" "Trunk Nr\n")
+ DEFUN(cfg_mgcp_trunk, cfg_mgcp_trunk_cmd,
+ "trunk <1-64>", "Configure a SS7 trunk\n" "Trunk Nr\n")
{
struct mgcp_trunk_config *trunk;
int index = atoi(argv[0]);
@@ -813,19 +630,22 @@
trunk->keepalive_interval, VTY_NEWLINE);
else
vty_out(vty, " no rtp keep-alive%s", VTY_NEWLINE);
- vty_out(vty, " loop %d%s",
- trunk->audio_loop, VTY_NEWLINE);
+ vty_out(vty, " loop %d%s", trunk->audio_loop, VTY_NEWLINE);
vty_out(vty, " force-realloc %d%s",
trunk->force_realloc, VTY_NEWLINE);
+ vty_out(vty, " rtp-accept-all %d%s",
+ trunk->rtp_accept_all, VTY_NEWLINE);
if (trunk->omit_rtcp)
vty_out(vty, " rtcp-omit%s", VTY_NEWLINE);
else
vty_out(vty, " no rtcp-omit%s", VTY_NEWLINE);
if (trunk->force_constant_ssrc || trunk->force_aligned_timing) {
vty_out(vty, " %srtp-patch ssrc%s",
- trunk->force_constant_ssrc ? "" : "no ", VTY_NEWLINE);
+ trunk->force_constant_ssrc ? "" : "no ",
+ VTY_NEWLINE);
vty_out(vty, " %srtp-patch timestamp%s",
- trunk->force_aligned_timing ? "" : "no ", VTY_NEWLINE);
+ trunk->force_aligned_timing ? "" : "no ",
+ VTY_NEWLINE);
} else
vty_out(vty, " no rtp-patch%s", VTY_NEWLINE);
if (trunk->audio_fmtp_extra)
@@ -867,13 +687,13 @@
}
ALIAS_DEPRECATED(cfg_trunk_payload_number, cfg_trunk_payload_number_cmd_old,
- "sdp audio payload number <0-255>",
- SDP_STR AUDIO_STR AUDIO_STR "Number\n" "Payload Number\n")
+ "sdp audio payload number <0-255>",
+ SDP_STR AUDIO_STR AUDIO_STR "Number\n" "Payload Number\n")
-DEFUN(cfg_trunk_payload_name,
+ DEFUN(cfg_trunk_payload_name,
cfg_trunk_payload_name_cmd,
"sdp audio-payload name NAME",
- SDP_STR AUDIO_STR "Payload\n" "Payload Name\n")
+ SDP_STR AUDIO_STR "Payload\n" "Payload Name\n")
{
struct mgcp_trunk_config *trunk = vty->index;
@@ -882,15 +702,13 @@
}
ALIAS_DEPRECATED(cfg_trunk_payload_name, cfg_trunk_payload_name_cmd_old,
- "sdp audio payload name NAME",
- SDP_STR AUDIO_STR AUDIO_STR "Payload\n" "Payload Name\n")
+ "sdp audio payload name NAME",
+ SDP_STR AUDIO_STR AUDIO_STR "Payload\n" "Payload Name\n")
-
-DEFUN(cfg_trunk_loop,
+ DEFUN(cfg_trunk_loop,
cfg_trunk_loop_cmd,
"loop (0|1)",
- "Loop audio for all endpoints on this trunk\n"
- "Don't Loop\n" "Loop\n")
+ "Loop audio for all endpoints on this trunk\n" "Don't Loop\n" "Loop\n")
{
struct mgcp_trunk_config *trunk = vty->index;
@@ -905,8 +723,7 @@
DEFUN(cfg_trunk_sdp_payload_send_ptime,
cfg_trunk_sdp_payload_send_ptime_cmd,
"sdp audio-payload send-ptime",
- SDP_STR AUDIO_STR
- "Send SDP ptime (packet duration) attribute\n")
+ SDP_STR AUDIO_STR "Send SDP ptime (packet duration) attribute\n")
{
struct mgcp_trunk_config *trunk = vty->index;
trunk->audio_send_ptime = 1;
@@ -916,8 +733,7 @@
DEFUN(cfg_trunk_no_sdp_payload_send_ptime,
cfg_trunk_no_sdp_payload_send_ptime_cmd,
"no sdp audio-payload send-ptime",
- NO_STR SDP_STR AUDIO_STR
- "Send SDP ptime (packet duration) attribute\n")
+ NO_STR SDP_STR AUDIO_STR "Send SDP ptime (packet duration) attribute\n")
{
struct mgcp_trunk_config *trunk = vty->index;
trunk->audio_send_ptime = 0;
@@ -927,8 +743,7 @@
DEFUN(cfg_trunk_sdp_payload_send_name,
cfg_trunk_sdp_payload_send_name_cmd,
"sdp audio-payload send-name",
- SDP_STR AUDIO_STR
- "Send SDP rtpmap with the audio name\n")
+ SDP_STR AUDIO_STR "Send SDP rtpmap with the audio name\n")
{
struct mgcp_trunk_config *trunk = vty->index;
trunk->audio_send_name = 1;
@@ -938,18 +753,14 @@
DEFUN(cfg_trunk_no_sdp_payload_send_name,
cfg_trunk_no_sdp_payload_send_name_cmd,
"no sdp audio-payload send-name",
- NO_STR SDP_STR AUDIO_STR
- "Send SDP rtpmap with the audio name\n")
+ NO_STR SDP_STR AUDIO_STR "Send SDP rtpmap with the audio name\n")
{
struct mgcp_trunk_config *trunk = vty->index;
trunk->audio_send_name = 0;
return CMD_SUCCESS;
}
-DEFUN(cfg_trunk_omit_rtcp,
- cfg_trunk_omit_rtcp_cmd,
- "rtcp-omit",
- RTCP_OMIT_STR)
+DEFUN(cfg_trunk_omit_rtcp, cfg_trunk_omit_rtcp_cmd, "rtcp-omit", RTCP_OMIT_STR)
{
struct mgcp_trunk_config *trunk = vty->index;
trunk->omit_rtcp = 1;
@@ -957,9 +768,7 @@
}
DEFUN(cfg_trunk_no_omit_rtcp,
- cfg_trunk_no_omit_rtcp_cmd,
- "no rtcp-omit",
- NO_STR RTCP_OMIT_STR)
+ cfg_trunk_no_omit_rtcp_cmd, "no rtcp-omit", NO_STR RTCP_OMIT_STR)
{
struct mgcp_trunk_config *trunk = vty->index;
trunk->omit_rtcp = 0;
@@ -968,10 +777,7 @@
DEFUN(cfg_trunk_patch_rtp_ssrc,
cfg_trunk_patch_rtp_ssrc_cmd,
- "rtp-patch ssrc",
- RTP_PATCH_STR
- "Force a fixed SSRC\n"
- )
+ "rtp-patch ssrc", RTP_PATCH_STR "Force a fixed SSRC\n")
{
struct mgcp_trunk_config *trunk = vty->index;
trunk->force_constant_ssrc = 1;
@@ -980,10 +786,7 @@
DEFUN(cfg_trunk_no_patch_rtp_ssrc,
cfg_trunk_no_patch_rtp_ssrc_cmd,
- "no rtp-patch ssrc",
- NO_STR RTP_PATCH_STR
- "Force a fixed SSRC\n"
- )
+ "no rtp-patch ssrc", NO_STR RTP_PATCH_STR "Force a fixed SSRC\n")
{
struct mgcp_trunk_config *trunk = vty->index;
trunk->force_constant_ssrc = 0;
@@ -992,10 +795,7 @@
DEFUN(cfg_trunk_patch_rtp_ts,
cfg_trunk_patch_rtp_ts_cmd,
- "rtp-patch timestamp",
- RTP_PATCH_STR
- "Adjust RTP timestamp\n"
- )
+ "rtp-patch timestamp", RTP_PATCH_STR "Adjust RTP timestamp\n")
{
struct mgcp_trunk_config *trunk = vty->index;
trunk->force_aligned_timing = 1;
@@ -1004,10 +804,7 @@
DEFUN(cfg_trunk_no_patch_rtp_ts,
cfg_trunk_no_patch_rtp_ts_cmd,
- "no rtp-patch timestamp",
- NO_STR RTP_PATCH_STR
- "Adjust RTP timestamp\n"
- )
+ "no rtp-patch timestamp", NO_STR RTP_PATCH_STR "Adjust RTP timestamp\n")
{
struct mgcp_trunk_config *trunk = vty->index;
trunk->force_aligned_timing = 0;
@@ -1015,9 +812,7 @@
}
DEFUN(cfg_trunk_no_patch_rtp,
- cfg_trunk_no_patch_rtp_cmd,
- "no rtp-patch",
- NO_STR RTP_PATCH_STR)
+ cfg_trunk_no_patch_rtp_cmd, "no rtp-patch", NO_STR RTP_PATCH_STR)
{
struct mgcp_trunk_config *trunk = vty->index;
trunk->force_constant_ssrc = 0;
@@ -1028,9 +823,7 @@
DEFUN(cfg_trunk_rtp_keepalive,
cfg_trunk_rtp_keepalive_cmd,
"rtp keep-alive <1-120>",
- RTP_STR RTP_KEEPALIVE_STR
- "Keep-alive interval in secs\n"
- )
+ RTP_STR RTP_KEEPALIVE_STR "Keep-alive interval in secs\n")
{
struct mgcp_trunk_config *trunk = vty->index;
mgcp_trunk_set_keepalive(trunk, atoi(argv[0]));
@@ -1040,9 +833,7 @@
DEFUN(cfg_trunk_rtp_keepalive_once,
cfg_trunk_rtp_keepalive_once_cmd,
"rtp keep-alive once",
- RTP_STR RTP_KEEPALIVE_STR
- "Send dummy packet only once after CRCX/MDCX\n"
- )
+ RTP_STR RTP_KEEPALIVE_STR "Send dummy packet only once after CRCX/MDCX\n")
{
struct mgcp_trunk_config *trunk = vty->index;
mgcp_trunk_set_keepalive(trunk, MGCP_KEEPALIVE_ONCE);
@@ -1051,9 +842,7 @@
DEFUN(cfg_trunk_no_rtp_keepalive,
cfg_trunk_no_rtp_keepalive_cmd,
- "no rtp keep-alive",
- NO_STR RTP_STR RTP_KEEPALIVE_STR
- )
+ "no rtp keep-alive", NO_STR RTP_STR RTP_KEEPALIVE_STR)
{
struct mgcp_trunk_config *trunk = vty->index;
mgcp_trunk_set_keepalive(trunk, 0);
@@ -1062,8 +851,7 @@
DEFUN(cfg_trunk_allow_transcoding,
cfg_trunk_allow_transcoding_cmd,
- "allow-transcoding",
- "Allow transcoding\n")
+ "allow-transcoding", "Allow transcoding\n")
{
struct mgcp_trunk_config *trunk = vty->index;
trunk->no_audio_transcoding = 0;
@@ -1072,22 +860,23 @@
DEFUN(cfg_trunk_no_allow_transcoding,
cfg_trunk_no_allow_transcoding_cmd,
- "no allow-transcoding",
- NO_STR "Allow transcoding\n")
+ "no allow-transcoding", NO_STR "Allow transcoding\n")
{
struct mgcp_trunk_config *trunk = vty->index;
trunk->no_audio_transcoding = 1;
return CMD_SUCCESS;
}
-DEFUN(loop_endp,
- loop_endp_cmd,
+DEFUN(loop_conn,
+ loop_conn_cmd,
"loop-endpoint <0-64> NAME (0|1)",
"Loop a given endpoint\n" "Trunk number\n"
- "The name in hex of the endpoint\n" "Disable the loop\n" "Enable the loop\n")
+ "The name in hex of the endpoint\n" "Disable the loop\n"
+ "Enable the loop\n")
{
struct mgcp_trunk_config *trunk;
struct mgcp_endpoint *endp;
+ struct mgcp_conn *conn;
trunk = find_trunk(g_cfg, atoi(argv[0]));
if (!trunk) {
@@ -1105,41 +894,48 @@
int endp_no = strtoul(argv[1], NULL, 16);
if (endp_no < 1 || endp_no >= trunk->number_endpoints) {
vty_out(vty, "Loopback number %s/%d is invalid.%s",
- argv[1], endp_no, VTY_NEWLINE);
+ argv[1], endp_no, VTY_NEWLINE);
return CMD_WARNING;
}
-
endp = &trunk->endpoints[endp_no];
int loop = atoi(argv[2]);
+ llist_for_each_entry(conn, &endp->conns, entry) {
+ if (conn->type == MGCP_CONN_TYPE_RTP)
+ /* Handle it like a MDCX, switch on SSRC patching if enabled */
+ mgcp_rtp_end_config(endp, 1, &conn->u.rtp.end);
+ else {
+ /* FIXME: Introduce support for other connection (E1)
+ * types when implementation is available */
+ vty_out(vty, "%%Can't enable SSRC patching,"
+ "connection %s is not an RTP connection.%s",
+ mgcp_conn_dump(conn), VTY_NEWLINE);
+ }
- if (loop)
- endp->conn_mode = MGCP_CONN_LOOPBACK;
- else
- endp->conn_mode = endp->orig_mode;
-
- /* Handle it like a MDCX, switch on SSRC patching if enabled */
- mgcp_rtp_end_config(endp, 1, &endp->bts_end);
- mgcp_rtp_end_config(endp, 1, &endp->net_end);
+ if (loop)
+ conn->mode = MGCP_CONN_LOOPBACK;
+ else
+ conn->mode = conn->mode_orig;
+ }
return CMD_SUCCESS;
}
-DEFUN(tap_call,
- tap_call_cmd,
- "tap-call <0-64> ENDPOINT (bts-in|bts-out|net-in|net-out) A.B.C.D <0-65534>",
+DEFUN(tap_rtp,
+ tap_rtp_cmd,
+ "tap-rtp <0-64> ENDPOINT CONN (in|out) A.B.C.D <0-65534>",
"Forward data on endpoint to a different system\n" "Trunk number\n"
"The endpoint in hex\n"
- "Forward the data coming from the bts\n"
- "Forward the data coming from the bts leaving to the network\n"
- "Forward the data coming from the net\n"
- "Forward the data coming from the net leaving to the bts\n"
+ "The connection id in hex\n"
+ "Forward incoming data\n"
+ "Forward leaving data\n"
"destination IP of the data\n" "destination port\n")
{
struct mgcp_rtp_tap *tap;
struct mgcp_trunk_config *trunk;
struct mgcp_endpoint *endp;
- int port = 0;
+ struct mgcp_conn_rtp *conn;
+ uint32_t conn_id;
trunk = find_trunk(g_cfg, atoi(argv[0]));
if (!trunk) {
@@ -1157,37 +953,39 @@
int endp_no = strtoul(argv[1], NULL, 16);
if (endp_no < 1 || endp_no >= trunk->number_endpoints) {
vty_out(vty, "Endpoint number %s/%d is invalid.%s",
- argv[1], endp_no, VTY_NEWLINE);
+ argv[1], endp_no, VTY_NEWLINE);
return CMD_WARNING;
}
endp = &trunk->endpoints[endp_no];
- if (strcmp(argv[2], "bts-in") == 0) {
- port = MGCP_TAP_BTS_IN;
- } else if (strcmp(argv[2], "bts-out") == 0) {
- port = MGCP_TAP_BTS_OUT;
- } else if (strcmp(argv[2], "net-in") == 0) {
- port = MGCP_TAP_NET_IN;
- } else if (strcmp(argv[2], "net-out") == 0) {
- port = MGCP_TAP_NET_OUT;
- } else {
+ conn_id = strtoul(argv[2], NULL, 10);
+ conn = mgcp_conn_get_rtp(endp, conn_id);
+ if (!conn) {
+ vty_out(vty, "Conn ID %s/%d is invalid.%s",
+ argv[2], conn_id, VTY_NEWLINE);
+ return CMD_WARNING;
+ }
+
+ if (strcmp(argv[3], "in") == 0)
+ tap = &conn->tap_in;
+ else if (strcmp(argv[3], "out") == 0)
+ tap = &conn->tap_out;
+ else {
vty_out(vty, "Unknown mode... tricked vty?%s", VTY_NEWLINE);
return CMD_WARNING;
}
- tap = &endp->taps[port];
memset(&tap->forward, 0, sizeof(tap->forward));
- inet_aton(argv[3], &tap->forward.sin_addr);
- tap->forward.sin_port = htons(atoi(argv[4]));
+ inet_aton(argv[4], &tap->forward.sin_addr);
+ tap->forward.sin_port = htons(atoi(argv[5]));
tap->enabled = 1;
return CMD_SUCCESS;
}
DEFUN(free_endp, free_endp_cmd,
"free-endpoint <0-64> NUMBER",
- "Free the given endpoint\n" "Trunk number\n"
- "Endpoint number in hex.\n")
+ "Free the given endpoint\n" "Trunk number\n" "Endpoint number in hex.\n")
{
struct mgcp_trunk_config *trunk;
struct mgcp_endpoint *endp;
@@ -1208,7 +1006,7 @@
int endp_no = strtoul(argv[1], NULL, 16);
if (endp_no < 1 || endp_no >= trunk->number_endpoints) {
vty_out(vty, "Endpoint number %s/%d is invalid.%s",
- argv[1], endp_no, VTY_NEWLINE);
+ argv[1], endp_no, VTY_NEWLINE);
return CMD_WARNING;
}
@@ -1219,8 +1017,7 @@
DEFUN(reset_endp, reset_endp_cmd,
"reset-endpoint <0-64> NUMBER",
- "Reset the given endpoint\n" "Trunk number\n"
- "Endpoint number in hex.\n")
+ "Reset the given endpoint\n" "Trunk number\n" "Endpoint number in hex.\n")
{
struct mgcp_trunk_config *trunk;
struct mgcp_endpoint *endp;
@@ -1242,7 +1039,7 @@
endp_no = strtoul(argv[1], NULL, 16);
if (endp_no < 1 || endp_no >= trunk->number_endpoints) {
vty_out(vty, "Endpoint number %s/%d is invalid.%s",
- argv[1], endp_no, VTY_NEWLINE);
+ argv[1], endp_no, VTY_NEWLINE);
return CMD_WARNING;
}
@@ -1256,8 +1053,7 @@
}
DEFUN(reset_all_endp, reset_all_endp_cmd,
- "reset-all-endpoints",
- "Reset all endpoints\n")
+ "reset-all-endpoints", "Reset all endpoints\n")
{
int rc;
@@ -1274,21 +1070,25 @@
DEFUN(cfg_mgcp_osmux,
cfg_mgcp_osmux_cmd,
"osmux (on|off|only)",
- OSMUX_STR "Enable OSMUX\n" "Disable OSMUX\n" "Only use OSMUX\n")
+ OSMUX_STR "Enable OSMUX\n" "Disable OSMUX\n" "Only use OSMUX\n")
{
if (strcmp(argv[0], "off") == 0) {
g_cfg->osmux = OSMUX_USAGE_OFF;
return CMD_SUCCESS;
}
+ /* Since OSMUX support is not finished, we do not
+ * allow to turn it on yet. */
+ vty_out(vty, "OSMUX currently unavailable in this software version.%s", VTY_NEWLINE);
+ return CMD_WARNING;
+
if (strcmp(argv[0], "on") == 0)
g_cfg->osmux = OSMUX_USAGE_ON;
else if (strcmp(argv[0], "only") == 0)
g_cfg->osmux = OSMUX_USAGE_ONLY;
if (g_cfg->trunk.audio_loop) {
- vty_out(vty, "Cannot use `loop' with `osmux'.%s",
- VTY_NEWLINE);
+ vty_out(vty, "Cannot use `loop' with `osmux'.%s", VTY_NEWLINE);
return CMD_WARNING;
}
@@ -1297,8 +1097,7 @@
DEFUN(cfg_mgcp_osmux_ip,
cfg_mgcp_osmux_ip_cmd,
- "osmux bind-ip A.B.C.D",
- OSMUX_STR IP_STR "IPv4 Address to bind to\n")
+ "osmux bind-ip A.B.C.D", OSMUX_STR IP_STR "IPv4 Address to bind to\n")
{
osmo_talloc_replace_string(g_cfg, &g_cfg->osmux_addr, argv[0]);
return CMD_SUCCESS;
@@ -1324,8 +1123,7 @@
DEFUN(cfg_mgcp_osmux_port,
cfg_mgcp_osmux_port_cmd,
- "osmux port <1-65535>",
- OSMUX_STR "port\n" "UDP port\n")
+ "osmux port <1-65535>", OSMUX_STR "port\n" "UDP port\n")
{
g_cfg->osmux_port = atoi(argv[0]);
return CMD_SUCCESS;
@@ -1334,7 +1132,8 @@
DEFUN(cfg_mgcp_osmux_dummy,
cfg_mgcp_osmux_dummy_cmd,
"osmux dummy (on|off)",
- OSMUX_STR "Dummy padding\n" "Enable dummy padding\n" "Disable dummy padding\n")
+ OSMUX_STR "Dummy padding\n" "Enable dummy padding\n"
+ "Disable dummy padding\n")
{
if (strcmp(argv[0], "on") == 0)
g_cfg->osmux_dummy = 1;
@@ -1347,8 +1146,8 @@
int mgcp_vty_init(void)
{
install_element_ve(&show_mgcp_cmd);
- install_element(ENABLE_NODE, &loop_endp_cmd);
- install_element(ENABLE_NODE, &tap_call_cmd);
+ install_element(ENABLE_NODE, &loop_conn_cmd);
+ install_element(ENABLE_NODE, &tap_rtp_cmd);
install_element(ENABLE_NODE, &free_endp_cmd);
install_element(ENABLE_NODE, &reset_endp_cmd);
install_element(ENABLE_NODE, &reset_all_endp_cmd);
@@ -1358,21 +1157,12 @@
vty_install_default(MGCP_NODE);
install_element(MGCP_NODE, &cfg_mgcp_local_ip_cmd);
- install_element(MGCP_NODE, &cfg_mgcp_bts_ip_cmd);
install_element(MGCP_NODE, &cfg_mgcp_bind_ip_cmd);
install_element(MGCP_NODE, &cfg_mgcp_bind_port_cmd);
install_element(MGCP_NODE, &cfg_mgcp_bind_early_cmd);
- install_element(MGCP_NODE, &cfg_mgcp_rtp_base_port_cmd);
- install_element(MGCP_NODE, &cfg_mgcp_rtp_bts_base_port_cmd);
- install_element(MGCP_NODE, &cfg_mgcp_rtp_net_base_port_cmd);
- install_element(MGCP_NODE, &cfg_mgcp_rtp_bts_range_cmd);
- install_element(MGCP_NODE, &cfg_mgcp_rtp_bts_bind_ip_cmd);
- install_element(MGCP_NODE, &cfg_mgcp_rtp_no_bts_bind_ip_cmd);
install_element(MGCP_NODE, &cfg_mgcp_rtp_net_range_cmd);
install_element(MGCP_NODE, &cfg_mgcp_rtp_net_bind_ip_cmd);
install_element(MGCP_NODE, &cfg_mgcp_rtp_no_net_bind_ip_cmd);
- install_element(MGCP_NODE, &cfg_mgcp_rtp_transcoder_range_cmd);
- install_element(MGCP_NODE, &cfg_mgcp_rtp_transcoder_base_cmd);
install_element(MGCP_NODE, &cfg_mgcp_rtp_ip_dscp_cmd);
install_element(MGCP_NODE, &cfg_mgcp_rtp_ip_tos_cmd);
install_element(MGCP_NODE, &cfg_mgcp_rtp_force_ptime_cmd);
@@ -1382,15 +1172,13 @@
install_element(MGCP_NODE, &cfg_mgcp_no_rtp_keepalive_cmd);
install_element(MGCP_NODE, &cfg_mgcp_agent_addr_cmd);
install_element(MGCP_NODE, &cfg_mgcp_agent_addr_cmd_old);
- install_element(MGCP_NODE, &cfg_mgcp_transcoder_cmd);
- install_element(MGCP_NODE, &cfg_mgcp_no_transcoder_cmd);
- install_element(MGCP_NODE, &cfg_mgcp_transcoder_remote_base_cmd);
install_element(MGCP_NODE, &cfg_mgcp_sdp_payload_number_cmd);
install_element(MGCP_NODE, &cfg_mgcp_sdp_payload_name_cmd);
install_element(MGCP_NODE, &cfg_mgcp_sdp_payload_number_cmd_old);
install_element(MGCP_NODE, &cfg_mgcp_sdp_payload_name_cmd_old);
install_element(MGCP_NODE, &cfg_mgcp_loop_cmd);
install_element(MGCP_NODE, &cfg_mgcp_force_realloc_cmd);
+ install_element(MGCP_NODE, &cfg_mgcp_rtp_accept_all_cmd);
install_element(MGCP_NODE, &cfg_mgcp_number_endp_cmd);
install_element(MGCP_NODE, &cfg_mgcp_omit_rtcp_cmd);
install_element(MGCP_NODE, &cfg_mgcp_no_omit_rtcp_cmd);
@@ -1413,7 +1201,6 @@
install_element(MGCP_NODE, &cfg_mgcp_allow_transcoding_cmd);
install_element(MGCP_NODE, &cfg_mgcp_no_allow_transcoding_cmd);
-
install_element(MGCP_NODE, &cfg_mgcp_trunk_cmd);
install_node(&trunk_node, config_write_trunk);
vty_install_default(TRUNK_NODE);
@@ -1445,9 +1232,6 @@
static int allocate_trunk(struct mgcp_trunk_config *trunk)
{
- int i;
- struct mgcp_config *cfg = trunk->cfg;
-
if (mgcp_endpoints_allocate(trunk) != 0) {
LOGP(DLMGCP, LOGL_ERROR,
"Failed to allocate %d endpoints on trunk %d.\n",
@@ -1455,54 +1239,6 @@
return -1;
}
- /* early bind */
- for (i = 1; i < trunk->number_endpoints; ++i) {
- struct mgcp_endpoint *endp = &trunk->endpoints[i];
-
- if (cfg->bts_ports.mode == PORT_ALLOC_STATIC) {
- cfg->last_bts_port += 2;
- if (mgcp_bind_bts_rtp_port(endp, cfg->last_bts_port) != 0) {
- LOGP(DLMGCP, LOGL_FATAL,
- "Failed to bind: %d\n", cfg->last_bts_port);
- return -1;
- }
- endp->bts_end.local_alloc = PORT_ALLOC_STATIC;
- }
-
- if (cfg->net_ports.mode == PORT_ALLOC_STATIC) {
- cfg->last_net_port += 2;
- if (mgcp_bind_net_rtp_port(endp, cfg->last_net_port) != 0) {
- LOGP(DLMGCP, LOGL_FATAL,
- "Failed to bind: %d\n", cfg->last_net_port);
- return -1;
- }
- endp->net_end.local_alloc = PORT_ALLOC_STATIC;
- }
-
- if (trunk->trunk_type == MGCP_TRUNK_VIRTUAL &&
- cfg->transcoder_ip && cfg->transcoder_ports.mode == PORT_ALLOC_STATIC) {
- int rtp_port;
-
- /* network side */
- rtp_port = rtp_calculate_port(ENDPOINT_NUMBER(endp),
- cfg->transcoder_ports.base_port);
- if (mgcp_bind_trans_net_rtp_port(endp, rtp_port) != 0) {
- LOGP(DLMGCP, LOGL_FATAL, "Failed to bind: %d\n", rtp_port);
- return -1;
- }
- endp->trans_net.local_alloc = PORT_ALLOC_STATIC;
-
- /* bts side */
- rtp_port = rtp_calculate_port(endp_back_channel(ENDPOINT_NUMBER(endp)),
- cfg->transcoder_ports.base_port);
- if (mgcp_bind_trans_bts_rtp_port(endp, rtp_port) != 0) {
- LOGP(DLMGCP, LOGL_FATAL, "Failed to bind: %d\n", rtp_port);
- return -1;
- }
- endp->trans_bts.local_alloc = PORT_ALLOC_STATIC;
- }
- }
-
return 0;
}
@@ -1519,32 +1255,27 @@
g_cfg = cfg;
rc = vty_read_config_file(config_file, NULL);
if (rc < 0) {
- fprintf(stderr, "Failed to parse the config file: '%s'\n", config_file);
+ fprintf(stderr, "Failed to parse the config file: '%s'\n",
+ config_file);
return rc;
}
-
- if (!g_cfg->bts_ip)
- fprintf(stderr, "No BTS ip address specified. This will allow everyone to connect.\n");
-
if (!g_cfg->source_addr) {
fprintf(stderr, "You need to specify a bind address.\n");
return -1;
}
- /* initialize the last ports */
- g_cfg->last_bts_port = rtp_calculate_port(0, g_cfg->bts_ports.base_port);
- g_cfg->last_net_port = rtp_calculate_port(0, g_cfg->net_ports.base_port);
-
if (allocate_trunk(&g_cfg->trunk) != 0) {
- LOGP(DLMGCP, LOGL_ERROR, "Failed to initialize the virtual trunk.\n");
+ LOGP(DLMGCP, LOGL_ERROR,
+ "Failed to initialize the virtual trunk.\n");
return -1;
}
llist_for_each_entry(trunk, &g_cfg->trunks, entry) {
if (allocate_trunk(trunk) != 0) {
LOGP(DLMGCP, LOGL_ERROR,
- "Failed to initialize E1 trunk %d.\n", trunk->trunk_nr);
+ "Failed to initialize E1 trunk %d.\n",
+ trunk->trunk_nr);
return -1;
}
}
@@ -1552,4 +1283,3 @@
return 0;
}
-
diff --git a/src/osmo-mgw/Makefile.am b/src/osmo-mgw/Makefile.am
index 3e3511d..b6099fc 100644
--- a/src/osmo-mgw/Makefile.am
+++ b/src/osmo-mgw/Makefile.am
@@ -9,23 +9,20 @@
$(LIBOSMOCORE_CFLAGS) \
$(LIBOSMOVTY_CFLAGS) \
$(LIBOSMONETIF_CFLAGS) \
- $(LIBBCG729_CFLAGS) \
$(COVERAGE_CFLAGS) \
$(NULL)
bin_PROGRAMS = \
- osmo-bsc_mgcp \
+ osmo-mgw \
$(NULL)
-osmo_bsc_mgcp_SOURCES = \
- mgcp_main.c \
+osmo_mgw_SOURCES = \
+ mgw_main.c \
$(NULL)
-osmo_bsc_mgcp_LDADD = \
- $(top_builddir)/src/libosmo-legacy-mgcp/libosmo-legacy-mgcp.la \
+osmo_mgw_LDADD = \
+ $(top_builddir)/src/libosmo-mgcp/libosmo-mgcp.la \
$(LIBOSMOCORE_LIBS) \
$(LIBOSMONETIF_LIBS) \
$(LIBOSMOVTY_LIBS) \
- $(LIBBCG729_LIBS) \
- $(LIBRARY_GSM) \
$(NULL)
diff --git a/src/osmo-mgw/mgcp_main.c b/src/osmo-mgw/mgw_main.c
similarity index 91%
rename from src/osmo-mgw/mgcp_main.c
rename to src/osmo-mgw/mgw_main.c
index 48241a6..aef99ae 100644
--- a/src/osmo-mgw/mgcp_main.c
+++ b/src/osmo-mgw/mgw_main.c
@@ -32,9 +32,9 @@
#include <sys/socket.h>
-#include <osmocom/legacy_mgcp/mgcp.h>
-#include <osmocom/legacy_mgcp/mgcp_internal.h>
-#include <osmocom/legacy_mgcp/vty.h>
+#include <osmocom/mgcp/mgcp.h>
+#include <osmocom/mgcp/mgcp_internal.h>
+#include <osmocom/mgcp/vty.h>
#include <osmocom/core/application.h>
#include <osmocom/core/msgb.h>
@@ -52,14 +52,10 @@
#include "../../bscconfig.h"
-#ifdef BUILD_MGCP_TRANSCODING
-#include <osmocom/legacy_mgcp/mgcp_transcode.h>
-#endif
-
#define _GNU_SOURCE
#include <getopt.h>
-#warning "Make use of the rtp proxy code"
+/* FIXME: Make use of the rtp proxy code */
static struct mgcp_config *cfg;
static struct mgcp_trunk_config *reset_trunk;
@@ -68,6 +64,7 @@
const char *openbsc_copyright =
"Copyright (C) 2009-2010 Holger Freyther and On-Waves\r\n"
+ "Copyright (C) 2017 by sysmocom s.f.m.c. GmbH <info@sysmocom.de>\r\n"
"Contributions by Daniel Willmann, Jan Lübbe, Stefan Schmidt\r\n"
"Dieter Spaar, Andreas Eversberg, Harald Welte\r\n\r\n"
"License AGPLv3+: GNU AGPL version 3 or later <http://gnu.org/licenses/agpl-3.0.html>\r\n"
@@ -132,10 +129,14 @@
}
}
-/* simply remember this */
+/* Callback function to be called when the RSIP ("Reset in Progress") mgcp
+ * command is received */
static int mgcp_rsip_cb(struct mgcp_trunk_config *tcfg)
{
+ /* Set flag so that, when read_call_agent() is called next time
+ * the reset can progress */
reset_endpoints = 1;
+
reset_trunk = tcfg;
return 0;
@@ -173,13 +174,17 @@
msgb_free(resp);
}
+ /* reset endpoints */
if (reset_endpoints) {
LOGP(DLMGCP, LOGL_NOTICE,
"Asked to reset endpoints: %d/%d\n",
reset_trunk->trunk_nr, reset_trunk->trunk_type);
+
+ /* reset flag */
reset_endpoints = 0;
- /* is checking in_addr.s_addr == INADDR_LOOPBACK making it more secure? */
+ /* Walk over all endpoints and trigger a release, this will release all
+ * endpoints, possible open connections are forcefully dropped */
for (i = 1; i < reset_trunk->number_endpoints; ++i)
mgcp_release_endp(&reset_trunk->endpoints[i]);
}
@@ -220,7 +225,7 @@
static struct vty_app_info vty_info = {
- .name = "OpenBSC MGCP",
+ .name = "OsmoMGW",
.version = PACKAGE_VERSION,
.go_parent_cb = mgcp_vty_go_parent,
.is_config_node = mgcp_vty_is_config_node,
@@ -250,14 +255,6 @@
if (!cfg)
return -1;
-#ifdef BUILD_MGCP_TRANSCODING
- cfg->setup_rtp_processing_cb = &mgcp_transcoding_setup;
- cfg->rtp_processing_cb = &mgcp_transcoding_process_rtp;
- cfg->get_net_downlink_format_cb = &mgcp_transcoding_net_downlink_format;
-#endif
-
- cfg->trunk.force_realloc = 1;
-
vty_info.copyright = openbsc_copyright;
vty_init(&vty_info);
logging_vty_add_cmds(NULL);
@@ -279,7 +276,8 @@
if (rc < 0)
return rc;
- /* set some callbacks */
+ /* Set the reset callback function. This functions is called when the
+ * mgcp-command "RSIP" (Reset in Progress) is received */
cfg->reset_cb = mgcp_rsip_cb;
/* we need to bind a socket */