blob: cc6637b1836108eebe8fafeded7a504a7cb7b214 [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.
108 * \param[in] index trunk number.
109 * \returns pointer to trunk configuration, NULL on error. */
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200110struct mgcp_trunk *mgcp_trunk_by_num(const struct mgcp_config *cfg, int index)
111{
112 struct mgcp_trunk *trunk;
113
114 llist_for_each_entry(trunk, &cfg->trunks, entry)
115 if (trunk->trunk_nr == index)
116 return trunk;
117
118 return NULL;
119}
120
Philipp Maier7e9ddc92020-06-10 15:22:32 +0200121/* Made public for unit-testing, do not use from outside this file */
122int e1_trunk_nr_from_epname(const char *epname)
123{
124 unsigned long int trunk_nr;
125 size_t prefix_len;
126 char *str_trunk_nr_end;
127
128 prefix_len = sizeof(MGCP_ENDPOINT_PREFIX_E1_TRUNK) - 1;
129 if (strncmp(epname, MGCP_ENDPOINT_PREFIX_E1_TRUNK, prefix_len) != 0)
130 return -EINVAL;
131
132 errno = 0;
133 trunk_nr = strtoul(epname + prefix_len, &str_trunk_nr_end, 10);
134 if (errno == ERANGE || trunk_nr > 64 || trunk_nr == 0
135 || epname + prefix_len == str_trunk_nr_end
136 || str_trunk_nr_end[0] != '/')
137 return -EINVAL;
138 else
139 return trunk_nr;
140}
141
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200142/*! Find a trunk by the trunk prefix in the endpoint name.
143 * \param[in] epname endpoint name with trunk prefix to look up.
144 * \param[in] cfg that contains the trunks where the endpoint is located.
145 * \returns trunk or NULL if trunk was not found. */
146struct mgcp_trunk *mgcp_trunk_by_name(const struct mgcp_config *cfg, const char *epname)
147{
148 size_t prefix_len;
149 char epname_lc[MGCP_ENDPOINT_MAXLEN];
Philipp Maier7e9ddc92020-06-10 15:22:32 +0200150 unsigned long int trunk_nr;
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200151
152 osmo_str_tolower_buf(epname_lc, sizeof(epname_lc), epname);
153 epname = epname_lc;
154
155 prefix_len = sizeof(MGCP_ENDPOINT_PREFIX_VIRTUAL_TRUNK) - 1;
156 if (strncmp(epname, MGCP_ENDPOINT_PREFIX_VIRTUAL_TRUNK, prefix_len) == 0) {
Philipp Maier7a641822020-06-15 16:05:42 +0200157 return mgcp_trunk_by_num(cfg, MGCP_VIRT_TRUNK_ID);
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200158 }
159
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200160 prefix_len = sizeof(MGCP_ENDPOINT_PREFIX_E1_TRUNK) - 1;
161 if (strncmp(epname, MGCP_ENDPOINT_PREFIX_E1_TRUNK, prefix_len) == 0) {
Philipp Maier7e9ddc92020-06-10 15:22:32 +0200162 trunk_nr = e1_trunk_nr_from_epname(epname);
163 if (trunk_nr < 0)
164 return NULL;
165 return mgcp_trunk_by_num(cfg, trunk_nr);
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200166 }
167
168 /* Earlier versions of osmo-mgw were accepting endpoint names
169 * without trunk prefix. This is normally not allowed, each MGCP
170 * request should supply an endpoint name with trunk prefix.
171 * However in order to stay compatible with old versions of
172 * osmo-bsc and osmo-msc we still accept endpoint names without
173 * trunk prefix and just assume that the virtual trunk should
174 * be selected. There is even a TTCN3 test for this, see also:
175 * MGCP_Test.TC_crcx_noprefix */
176 if ((epname[0] >= '0' && epname[0] <= '9') || (epname[0] >= 'a' && epname[0] <= 'f')) {
177 LOGP(DLMGCP, LOGL_ERROR, "missing trunk prefix in endpoint name \"%s\", assuming trunk \"%s\"!\n", epname,
178 MGCP_ENDPOINT_PREFIX_VIRTUAL_TRUNK);
Philipp Maierd19de2e2020-06-03 13:55:33 +0200179 return mgcp_trunk_by_num(cfg, MGCP_VIRT_TRUNK_ID);
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200180 }
181
182 LOGP(DLMGCP, LOGL_ERROR, "unable to find trunk for endpoint name \"%s\"!\n", epname);
183 return NULL;
184}