blob: f3785cd30384405308760bd82b2ffb4789658432 [file] [log] [blame]
Philipp Maierc66ab2c2020-06-02 20:55:34 +02001/* Trunk handling */
2
3/*
4 * (C) 2009-2012 by Holger Hans Peter Freyther <zecke@selfish.org>
5 * (C) 2009-2012 by On-Waves
6 * (C) 2017-2020 by sysmocom s.f.m.c. GmbH <info@sysmocom.de>
7 * All Rights Reserved
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU Affero General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Affero General Public License for more details.
18 *
19 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 *
22 */
23
24#include <osmocom/mgcp/mgcp_internal.h>
25#include <osmocom/mgcp/mgcp_endp.h>
26#include <osmocom/mgcp/mgcp_trunk.h>
27
Philipp Maierbce5f292020-06-18 11:57:17 +020028/*! allocate trunk and add it (if required) to the trunk list.
29 * (called once at startup by VTY).
30 * \param[in] cfg mgcp configuration.
Philipp Maierbce5f292020-06-18 11:57:17 +020031 * \param[in] ttype trunk type.
Philipp Maier080935a2020-07-01 23:08:50 +020032 * \param[in] nr trunk number.
Philipp Maierbce5f292020-06-18 11:57:17 +020033 * \returns pointer to allocated trunk, NULL on failure. */
Philipp Maierc66ab2c2020-06-02 20:55:34 +020034struct mgcp_trunk *mgcp_trunk_alloc(struct mgcp_config *cfg, enum mgcp_trunk_type ttype, int nr)
35{
36 struct mgcp_trunk *trunk;
37
38 trunk = talloc_zero(cfg, struct mgcp_trunk);
39 if (!trunk) {
40 LOGP(DLMGCP, LOGL_ERROR, "Failed to allocate.\n");
41 return NULL;
42 }
43
44 trunk->cfg = cfg;
45 trunk->trunk_type = ttype;
46 trunk->trunk_nr = nr;
47
48 trunk->audio_send_ptime = 1;
49 trunk->audio_send_name = 1;
50 trunk->vty_number_endpoints = 33;
51 trunk->omit_rtcp = 0;
52
53 mgcp_trunk_set_keepalive(trunk, MGCP_KEEPALIVE_ONCE);
54
Philipp Maierd19de2e2020-06-03 13:55:33 +020055 llist_add_tail(&trunk->entry, &cfg->trunks);
Philipp Maierc66ab2c2020-06-02 20:55:34 +020056
57 mgcp_ratectr_trunk_alloc(cfg, &trunk->ratectr);
58
59 return trunk;
60}
61
Philipp Maierbce5f292020-06-18 11:57:17 +020062/*! allocate endpoints and set default values
63 * (called once at startup by VTY).
64 * \param[in] trunk trunk configuration.
65 * \returns 0 on success, -1 on failure. */
Philipp Maierc66ab2c2020-06-02 20:55:34 +020066int mgcp_trunk_alloc_endpts(struct mgcp_trunk *trunk)
67{
68 int i;
Philipp Maierc66ab2c2020-06-02 20:55:34 +020069 struct mgcp_endpoint *endp;
70
71 /* Make sure the amount of requested endpoints does not execeed
72 * sane limits. The VTY already limits the possible amount,
73 * however miss-initalation of the struct or memory corruption
74 * could still lead to an excessive allocation of endpoints, so
75 * better stop early if that is the case. */
76 OSMO_ASSERT(trunk->vty_number_endpoints < 65534);
77
78 /* This function is called once on startup by the VTY to allocate the
79 * endpoints. The number of endpoints must not change througout the
80 * runtime of the MGW */
81 OSMO_ASSERT(trunk->number_endpoints == 0);
82 OSMO_ASSERT(trunk->endpoints == NULL);
83
84 /* allocate pointer array for the endpoints */
85 trunk->endpoints = _talloc_zero_array(trunk->cfg,
86 sizeof(struct mgcp_endpoint *), trunk->vty_number_endpoints, "endpoints");
87 if (!trunk->endpoints)
88 return -1;
89
90 /* create endpoints */
91 for (i = 0; i < trunk->vty_number_endpoints; ++i) {
Philipp Maier7462b952020-06-10 14:50:34 +020092 endp = mgcp_endp_alloc(trunk, i);
Philipp Maierc66ab2c2020-06-02 20:55:34 +020093 if (!endp) {
94 talloc_free(trunk->endpoints);
95 return -1;
96 }
97 trunk->endpoints[i] = endp;
98 }
99
100 /* make the endpoints we just created available to the MGW code */
101 trunk->number_endpoints = trunk->vty_number_endpoints;
102
103 return 0;
104}
105
106/*! get trunk configuration by trunk number (index).
Philipp Maierbce5f292020-06-18 11:57:17 +0200107 * \param[in] cfg mgcp configuration.
Philipp Maier6fbbeec2020-07-01 23:00:54 +0200108 * \param[in] ttype trunk type.
109 * \param[in] nr trunk number.
Philipp Maierbce5f292020-06-18 11:57:17 +0200110 * \returns pointer to trunk configuration, NULL on error. */
Philipp Maier6fbbeec2020-07-01 23:00:54 +0200111struct mgcp_trunk *mgcp_trunk_by_num(const struct mgcp_config *cfg, enum mgcp_trunk_type ttype, int nr)
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200112{
113 struct mgcp_trunk *trunk;
114
Philipp Maier6fbbeec2020-07-01 23:00:54 +0200115 llist_for_each_entry(trunk, &cfg->trunks, entry) {
116 if (trunk->trunk_nr == nr && trunk->trunk_type == ttype)
117 return trunk;
118 }
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200119
120 return NULL;
121}
122
Philipp Maier7e9ddc92020-06-10 15:22:32 +0200123/* Made public for unit-testing, do not use from outside this file */
124int e1_trunk_nr_from_epname(const char *epname)
125{
126 unsigned long int trunk_nr;
127 size_t prefix_len;
128 char *str_trunk_nr_end;
129
130 prefix_len = sizeof(MGCP_ENDPOINT_PREFIX_E1_TRUNK) - 1;
131 if (strncmp(epname, MGCP_ENDPOINT_PREFIX_E1_TRUNK, prefix_len) != 0)
132 return -EINVAL;
133
134 errno = 0;
135 trunk_nr = strtoul(epname + prefix_len, &str_trunk_nr_end, 10);
136 if (errno == ERANGE || trunk_nr > 64 || trunk_nr == 0
137 || epname + prefix_len == str_trunk_nr_end
138 || str_trunk_nr_end[0] != '/')
139 return -EINVAL;
140 else
141 return trunk_nr;
142}
143
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200144/*! Find a trunk by the trunk prefix in the endpoint name.
145 * \param[in] epname endpoint name with trunk prefix to look up.
146 * \param[in] cfg that contains the trunks where the endpoint is located.
147 * \returns trunk or NULL if trunk was not found. */
148struct mgcp_trunk *mgcp_trunk_by_name(const struct mgcp_config *cfg, const char *epname)
149{
150 size_t prefix_len;
151 char epname_lc[MGCP_ENDPOINT_MAXLEN];
Vadim Yanitskiyca8639d2020-07-07 14:02:17 +0700152 int trunk_nr;
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200153
154 osmo_str_tolower_buf(epname_lc, sizeof(epname_lc), epname);
155 epname = epname_lc;
156
157 prefix_len = sizeof(MGCP_ENDPOINT_PREFIX_VIRTUAL_TRUNK) - 1;
158 if (strncmp(epname, MGCP_ENDPOINT_PREFIX_VIRTUAL_TRUNK, prefix_len) == 0) {
Philipp Maier6fbbeec2020-07-01 23:00:54 +0200159 return mgcp_trunk_by_num(cfg, MGCP_TRUNK_VIRTUAL, MGCP_VIRT_TRUNK_ID);
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200160 }
161
Philipp Maier24b8c0c2020-07-02 14:59:16 +0200162 trunk_nr = e1_trunk_nr_from_epname(epname);
163 if (trunk_nr > 0)
Philipp Maier6fbbeec2020-07-01 23:00:54 +0200164 return mgcp_trunk_by_num(cfg, MGCP_TRUNK_E1, trunk_nr);
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200165
166 /* Earlier versions of osmo-mgw were accepting endpoint names
167 * without trunk prefix. This is normally not allowed, each MGCP
168 * request should supply an endpoint name with trunk prefix.
169 * However in order to stay compatible with old versions of
170 * osmo-bsc and osmo-msc we still accept endpoint names without
171 * trunk prefix and just assume that the virtual trunk should
172 * be selected. There is even a TTCN3 test for this, see also:
173 * MGCP_Test.TC_crcx_noprefix */
174 if ((epname[0] >= '0' && epname[0] <= '9') || (epname[0] >= 'a' && epname[0] <= 'f')) {
175 LOGP(DLMGCP, LOGL_ERROR, "missing trunk prefix in endpoint name \"%s\", assuming trunk \"%s\"!\n", epname,
176 MGCP_ENDPOINT_PREFIX_VIRTUAL_TRUNK);
Philipp Maier6fbbeec2020-07-01 23:00:54 +0200177 return mgcp_trunk_by_num(cfg, MGCP_TRUNK_VIRTUAL, MGCP_VIRT_TRUNK_ID);
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200178 }
179
180 LOGP(DLMGCP, LOGL_ERROR, "unable to find trunk for endpoint name \"%s\"!\n", epname);
181 return NULL;
182}