blob: 208604ad8b767299a99d3579eb4fb190d7c15290 [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.
31 * \param[in] nr trunk number.
32 * \param[in] ttype trunk type.
33 * \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;
69 char ep_name_buf[MGCP_ENDPOINT_MAXLEN];
70 struct mgcp_endpoint *endp;
71
72 /* Make sure the amount of requested endpoints does not execeed
73 * sane limits. The VTY already limits the possible amount,
74 * however miss-initalation of the struct or memory corruption
75 * could still lead to an excessive allocation of endpoints, so
76 * better stop early if that is the case. */
77 OSMO_ASSERT(trunk->vty_number_endpoints < 65534);
78
79 /* This function is called once on startup by the VTY to allocate the
80 * endpoints. The number of endpoints must not change througout the
81 * runtime of the MGW */
82 OSMO_ASSERT(trunk->number_endpoints == 0);
83 OSMO_ASSERT(trunk->endpoints == NULL);
84
85 /* allocate pointer array for the endpoints */
86 trunk->endpoints = _talloc_zero_array(trunk->cfg,
87 sizeof(struct mgcp_endpoint *), trunk->vty_number_endpoints, "endpoints");
88 if (!trunk->endpoints)
89 return -1;
90
91 /* create endpoints */
92 for (i = 0; i < trunk->vty_number_endpoints; ++i) {
93 switch (trunk->trunk_type) {
94 case MGCP_TRUNK_VIRTUAL:
95 snprintf(ep_name_buf, sizeof(ep_name_buf), "%s%x@%s", MGCP_ENDPOINT_PREFIX_VIRTUAL_TRUNK, i,
96 trunk->cfg->domain);
97 break;
98 case MGCP_TRUNK_E1:
99 /* FIXME: E1 trunk implementation is work in progress, this endpoint
100 * name is incomplete (subslots) */
101 snprintf(ep_name_buf, sizeof(ep_name_buf), "%s-1/%x", MGCP_ENDPOINT_PREFIX_E1_TRUNK, i);
102 break;
103 default:
104 osmo_panic("Cannot allocate unimplemented trunk type %d! %s:%d\n",
105 trunk->trunk_type, __FILE__, __LINE__);
106 }
107
108 endp = mgcp_endp_alloc(trunk, ep_name_buf);
109 if (!endp) {
110 talloc_free(trunk->endpoints);
111 return -1;
112 }
113 trunk->endpoints[i] = endp;
114 }
115
116 /* make the endpoints we just created available to the MGW code */
117 trunk->number_endpoints = trunk->vty_number_endpoints;
118
119 return 0;
120}
121
122/*! get trunk configuration by trunk number (index).
Philipp Maierbce5f292020-06-18 11:57:17 +0200123 * \param[in] cfg mgcp configuration.
124 * \param[in] index trunk number.
125 * \returns pointer to trunk configuration, NULL on error. */
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200126struct mgcp_trunk *mgcp_trunk_by_num(const struct mgcp_config *cfg, int index)
127{
128 struct mgcp_trunk *trunk;
129
130 llist_for_each_entry(trunk, &cfg->trunks, entry)
131 if (trunk->trunk_nr == index)
132 return trunk;
133
134 return NULL;
135}
136
137/*! Find a trunk by the trunk prefix in the endpoint name.
138 * \param[in] epname endpoint name with trunk prefix to look up.
139 * \param[in] cfg that contains the trunks where the endpoint is located.
140 * \returns trunk or NULL if trunk was not found. */
141struct mgcp_trunk *mgcp_trunk_by_name(const struct mgcp_config *cfg, const char *epname)
142{
143 size_t prefix_len;
144 char epname_lc[MGCP_ENDPOINT_MAXLEN];
145
146 osmo_str_tolower_buf(epname_lc, sizeof(epname_lc), epname);
147 epname = epname_lc;
148
149 prefix_len = sizeof(MGCP_ENDPOINT_PREFIX_VIRTUAL_TRUNK) - 1;
150 if (strncmp(epname, MGCP_ENDPOINT_PREFIX_VIRTUAL_TRUNK, prefix_len) == 0) {
Philipp Maierd19de2e2020-06-03 13:55:33 +0200151 return mgcp_trunk_by_num(cfg, MGCP_VIRT_TRUNK_ID);
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200152 }
153
154 /* E1 trunks are not implemented yet, so we deny any request for an
155 * e1 trunk for now. */
156 prefix_len = sizeof(MGCP_ENDPOINT_PREFIX_E1_TRUNK) - 1;
157 if (strncmp(epname, MGCP_ENDPOINT_PREFIX_E1_TRUNK, prefix_len) == 0) {
158 LOGP(DLMGCP, LOGL_ERROR,
159 "endpoint name \"%s\" suggests an E1 trunk, but E1 trunks are not implemented in this version of osmo-mgw!\n", epname);
160 return NULL;
161 }
162
163 /* Earlier versions of osmo-mgw were accepting endpoint names
164 * without trunk prefix. This is normally not allowed, each MGCP
165 * request should supply an endpoint name with trunk prefix.
166 * However in order to stay compatible with old versions of
167 * osmo-bsc and osmo-msc we still accept endpoint names without
168 * trunk prefix and just assume that the virtual trunk should
169 * be selected. There is even a TTCN3 test for this, see also:
170 * MGCP_Test.TC_crcx_noprefix */
171 if ((epname[0] >= '0' && epname[0] <= '9') || (epname[0] >= 'a' && epname[0] <= 'f')) {
172 LOGP(DLMGCP, LOGL_ERROR, "missing trunk prefix in endpoint name \"%s\", assuming trunk \"%s\"!\n", epname,
173 MGCP_ENDPOINT_PREFIX_VIRTUAL_TRUNK);
Philipp Maierd19de2e2020-06-03 13:55:33 +0200174 return mgcp_trunk_by_num(cfg, MGCP_VIRT_TRUNK_ID);
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200175 }
176
177 LOGP(DLMGCP, LOGL_ERROR, "unable to find trunk for endpoint name \"%s\"!\n", epname);
178 return NULL;
179}