blob: 22dbeb7a0d16517428ec3a0cc17c900f2c1d8ff6 [file] [log] [blame]
Philipp Maier87bd9be2017-08-22 16:35:41 +02001/* Endpoint types */
2
3/*
Philipp Maierc66ab2c2020-06-02 20:55:34 +02004 * (C) 2017-2020 by sysmocom s.f.m.c. GmbH <info@sysmocom.de>
Philipp Maier87bd9be2017-08-22 16:35:41 +02005 * All Rights Reserved
6 *
7 * Author: Philipp Maier
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
Philipp Maier993ea6b2020-08-04 18:26:50 +020024#include <osmocom/mgcp/mgcp.h>
25#include <osmocom/mgcp/mgcp_protocol.h>
26#include <osmocom/mgcp/mgcp_conn.h>
Philipp Maier37d11c82018-02-01 14:38:12 +010027#include <osmocom/mgcp/mgcp_endp.h>
Philipp Maierc66ab2c2020-06-02 20:55:34 +020028#include <osmocom/mgcp/mgcp_trunk.h>
Philipp Maier87bd9be2017-08-22 16:35:41 +020029
Philipp Maier99d4d362020-09-07 11:58:09 +020030#include <osmocom/abis/e1_input.h>
Philipp Maier889fe7f2020-07-06 17:44:12 +020031#include <osmocom/mgcp/mgcp_e1.h>
Philipp Maier124a3e02021-07-26 11:17:15 +020032#include <osmocom/core/stat_item.h>
Philipp Maier889fe7f2020-07-06 17:44:12 +020033
Philipp Maier8d6a1932020-06-18 12:19:31 +020034#define E1_RATE_MAX 64
35#define E1_OFFS_MAX 8
36
Philipp Maier87bd9be2017-08-22 16:35:41 +020037/* Endpoint typeset definition */
38const struct mgcp_endpoint_typeset ep_typeset = {
39 /* Specify endpoint properties for RTP endpoint */
Philipp Maier0996a1e2020-06-10 15:27:14 +020040 .rtp = {
41 .max_conns = 2,
42 .dispatch_rtp_cb = mgcp_dispatch_rtp_bridge_cb,
43 .cleanup_cb = mgcp_cleanup_rtp_bridge_cb,
44 },
45 /* Specify endpoint properties for E1 endpoint */
46 .e1 = {
47 .max_conns = 1,
48 .dispatch_rtp_cb = mgcp_dispatch_e1_bridge_cb,
49 .cleanup_cb = mgcp_cleanup_e1_bridge_cb,
50 },
Philipp Maier87bd9be2017-08-22 16:35:41 +020051};
Philipp Maieredc00f42018-01-24 11:58:56 +010052
Philipp Maier7462b952020-06-10 14:50:34 +020053/* Generate virtual endpoint name from given parameters */
54static char *gen_virtual_epname(void *ctx, const char *domain,
55 unsigned int index)
56{
57 return talloc_asprintf(ctx, "%s%x@%s",
58 MGCP_ENDPOINT_PREFIX_VIRTUAL_TRUNK, index, domain);
59}
60
Philipp Maier98c09b32020-06-18 12:17:55 +020061/* Generate E1 endpoint name from given numeric parameters */
Philipp Maierd70eef62021-07-19 13:53:28 +020062static char *gen_e1_epname(void *ctx, const char *domain, unsigned int trunk_nr,
Philipp Maier0ffa3bd2020-06-30 16:17:03 +020063 uint8_t ts_nr, uint8_t ss_nr)
Philipp Maier98c09b32020-06-18 12:17:55 +020064{
Philipp Maier98c09b32020-06-18 12:17:55 +020065 unsigned int rate;
66 unsigned int offset;
67
Philipp Maier8d6a1932020-06-18 12:19:31 +020068 OSMO_ASSERT(ss_nr < sizeof(e1_rates));
Philipp Maier98c09b32020-06-18 12:17:55 +020069
Philipp Maier8d6a1932020-06-18 12:19:31 +020070 rate = e1_rates[ss_nr];
71 offset = e1_offsets[ss_nr];
Philipp Maier98c09b32020-06-18 12:17:55 +020072
Philipp Maier0ffa3bd2020-06-30 16:17:03 +020073 return talloc_asprintf(ctx, "%s%u/s-%u/su%u-%u@%s",
74 MGCP_ENDPOINT_PREFIX_E1_TRUNK, trunk_nr, ts_nr,
75 rate, offset, domain);
Philipp Maier98c09b32020-06-18 12:17:55 +020076}
77
Philipp Maierc66ab2c2020-06-02 20:55:34 +020078/*! allocate an endpoint and set default values.
79 * \param[in] trunk configuration.
Philipp Maier7462b952020-06-10 14:50:34 +020080 * \param[in] name endpoint index.
Philipp Maierc66ab2c2020-06-02 20:55:34 +020081 * \returns endpoint on success, NULL on failure. */
Philipp Maier7462b952020-06-10 14:50:34 +020082struct mgcp_endpoint *mgcp_endp_alloc(struct mgcp_trunk *trunk,
83 unsigned int index)
Philipp Maierc66ab2c2020-06-02 20:55:34 +020084{
85 struct mgcp_endpoint *endp;
86
87 endp = talloc_zero(trunk->endpoints, struct mgcp_endpoint);
88 if (!endp)
89 return NULL;
90
91 INIT_LLIST_HEAD(&endp->conns);
Philipp Maierc66ab2c2020-06-02 20:55:34 +020092 endp->trunk = trunk;
Philipp Maierc66ab2c2020-06-02 20:55:34 +020093
94 switch (trunk->trunk_type) {
95 case MGCP_TRUNK_VIRTUAL:
96 endp->type = &ep_typeset.rtp;
Philipp Maier7462b952020-06-10 14:50:34 +020097 endp->name = gen_virtual_epname(endp, trunk->cfg->domain, index);
Philipp Maierc66ab2c2020-06-02 20:55:34 +020098 break;
99 case MGCP_TRUNK_E1:
Philipp Maier889fe7f2020-07-06 17:44:12 +0200100 endp->type = &ep_typeset.e1;
Philipp Maier0ffa3bd2020-06-30 16:17:03 +0200101 endp->name = gen_e1_epname(endp, trunk->cfg->domain,
102 trunk->trunk_nr,
Philipp Maierfbcf3992020-07-02 23:08:37 +0200103 index / MGCP_ENDP_E1_SUBSLOTS, index % MGCP_ENDP_E1_SUBSLOTS);
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200104 break;
105 default:
106 osmo_panic("Cannot allocate unimplemented trunk type %d! %s:%d\n",
107 trunk->trunk_type, __FILE__, __LINE__);
108 }
109
110 return endp;
111}
112
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200113/* Check if the endpoint name contains the prefix (e.g. "rtpbridge/" or
114 * "ds/e1-") and write the epname without the prefix back to the memory
115 * pointed at by epname. (per trunk the prefix is the same for all endpoints,
116 * so no ambiguity is introduced) */
117static void chop_epname_prefix(char *epname, const struct mgcp_trunk *trunk)
118{
119 size_t prefix_len;
120 switch (trunk->trunk_type) {
121 case MGCP_TRUNK_VIRTUAL:
122 prefix_len = sizeof(MGCP_ENDPOINT_PREFIX_VIRTUAL_TRUNK) - 1;
123 if (strncmp
124 (epname, MGCP_ENDPOINT_PREFIX_VIRTUAL_TRUNK,
125 prefix_len) == 0)
126 memmove(epname, epname + prefix_len,
127 strlen(epname) - prefix_len + 1);
128 return;
129 case MGCP_TRUNK_E1:
130 prefix_len = sizeof(MGCP_ENDPOINT_PREFIX_E1_TRUNK) - 1;
131 if (strncmp
132 (epname, MGCP_ENDPOINT_PREFIX_VIRTUAL_TRUNK,
133 prefix_len) == 0)
134 memmove(epname, epname + prefix_len,
135 strlen(epname) - prefix_len + 1);
136 return;
137 default:
138 OSMO_ASSERT(false);
139 }
140}
141
142/* Check if the endpoint name contains a suffix (e.g. "@mgw") and truncate
143 * epname by writing a '\0' char where the suffix starts. */
144static void chop_epname_suffix(char *epname, const struct mgcp_trunk *trunk)
145{
146 char *suffix_begin;
147
148 /* Endpoints on the virtual trunk may have a domain name that is
149 * followed after an @ character, this can be chopped off. All
150 * other supported trunk types do not have any suffixes that may
151 * be chopped off */
152 if (trunk->trunk_type == MGCP_TRUNK_VIRTUAL) {
153 suffix_begin = strchr(epname, '@');
154 if (!suffix_begin)
155 return;
156 *suffix_begin = '\0';
157 }
158}
159
Eric25ecc912021-09-06 03:43:50 +0200160
161 /*! Convert all characters in epname to lowercase and strip trunk prefix and
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200162 * endpoint name suffix (domain name) from epname. The result is written to
163 * to the memory pointed at by epname_stripped. The expected size of the
164 * result is either equal or lower then the length of the input string
Eric25ecc912021-09-06 03:43:50 +0200165 * (epname)
166 * \param[out] epname_stripped pointer to store the stripped ep name.
167 * \param[in] epname endpoint name to lookup.
168 * \param[in] trunk where the endpoint is located. */
169void mgcp_endp_strip_name(char *epname_stripped, const char *epname,
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200170 const struct mgcp_trunk *trunk)
171{
172 osmo_str_tolower_buf(epname_stripped, MGCP_ENDPOINT_MAXLEN, epname);
173 chop_epname_prefix(epname_stripped, trunk);
174 chop_epname_suffix(epname_stripped, trunk);
175}
176
177/* Go through the trunk and find a random free (no active calls) endpoint,
178 * this function is called when a wildcarded request is carried out, which
179 * means that it is up to the MGW to choose a random free endpoint. */
180static struct mgcp_endpoint *find_free_endpoint(const struct mgcp_trunk *trunk)
181{
182 struct mgcp_endpoint *endp;
183 unsigned int i;
184
185 for (i = 0; i < trunk->number_endpoints; i++) {
186 endp = trunk->endpoints[i];
Philipp Maier8d6a1932020-06-18 12:19:31 +0200187 /* A free endpoint must not serve a call already and it must
188 * be available. */
189 if (endp->callid == NULL && mgcp_endp_avail(endp))
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200190 return endp;
191 }
192
193 return NULL;
194}
195
Eric25ecc912021-09-06 03:43:50 +0200196/*! Find an endpoint of a trunk specified by its name.
Ericaac84ed2021-11-09 16:22:01 +0100197 * \param[in] epname endpoint name to check.
198 * \param[in] trunk mgcp_trunk that might have this endpoint.
199 * \returns NULL if no ep found, else endpoint. */
Eric25ecc912021-09-06 03:43:50 +0200200struct mgcp_endpoint *mgcp_endp_find_specific(const char *epname,
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200201 const struct mgcp_trunk *trunk)
202{
203 char epname_stripped[MGCP_ENDPOINT_MAXLEN];
204 char epname_stripped_endp[MGCP_ENDPOINT_MAXLEN];
205 struct mgcp_endpoint *endp;
206 unsigned int i;
207
208 /* Strip irrelevant information from the endpoint name */
Eric25ecc912021-09-06 03:43:50 +0200209 mgcp_endp_strip_name(epname_stripped, epname, trunk);
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200210
211 for (i = 0; i < trunk->number_endpoints; i++) {
212 endp = trunk->endpoints[i];
Eric25ecc912021-09-06 03:43:50 +0200213 mgcp_endp_strip_name(epname_stripped_endp, endp->name, trunk);
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200214 if (strcmp(epname_stripped_endp, epname_stripped) == 0)
215 return endp;
216 }
217
218 return NULL;
219}
220
Philipp Maierd64c0412021-07-14 11:53:49 +0200221/*! Check if the given epname refers to a wildcarded request or to a specific
222 * endpoint.
223 * \param[in] epname endpoint name to check
224 * \returns true if epname refers to wildcarded request, else false. */
225bool mgcp_endp_is_wildcarded(const char *epname)
226{
227 if (strstr(epname, "*"))
228 return true;
229
230 return false;
231}
232
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200233/*! Find an endpoint by its name on a specified trunk.
234 * \param[out] cause pointer to store cause code, can be NULL.
235 * \param[in] epname endpoint name to lookup.
236 * \param[in] trunk where the endpoint is located.
237 * \returns endpoint or NULL if endpoint was not found. */
238struct mgcp_endpoint *mgcp_endp_by_name_trunk(int *cause, const char *epname,
239 const struct mgcp_trunk *trunk)
240{
241 struct mgcp_endpoint *endp;
242
243 if (cause)
244 *cause = 0;
245
246 /* At the moment we only support a primitive ('*'-only) method of
247 * wildcarded endpoint searches that picks the next free endpoint on
248 * a trunk. */
Philipp Maierd64c0412021-07-14 11:53:49 +0200249 if (mgcp_endp_is_wildcarded(epname)) {
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200250 endp = find_free_endpoint(trunk);
251 if (endp) {
252 LOGPENDP(endp, DLMGCP, LOGL_DEBUG,
253 "(trunk:%d) found free endpoint: %s\n",
254 trunk->trunk_nr, endp->name);
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200255 return endp;
256 }
257
258 LOGP(DLMGCP, LOGL_ERROR,
259 "(trunk:%d) Not able to find a free endpoint\n",
260 trunk->trunk_nr);
261 if (cause)
262 *cause = -403;
263 return NULL;
264 }
265
266 /* Find an endpoint by its name (if wildcarded request is not
267 * applicable) */
Eric25ecc912021-09-06 03:43:50 +0200268 endp = mgcp_endp_find_specific(epname, trunk);
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200269 if (endp) {
270 LOGPENDP(endp, DLMGCP, LOGL_DEBUG,
271 "(trunk:%d) found endpoint: %s\n",
272 trunk->trunk_nr, endp->name);
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200273 return endp;
274 }
275
276 LOGP(DLMGCP, LOGL_ERROR,
277 "(trunk:%d) Not able to find specified endpoint: %s\n",
278 trunk->trunk_nr, epname);
279 if (cause)
280 *cause = -500;
281
282 return NULL;
283}
284
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200285/*! Find an endpoint by its name, search at all trunks.
286 * \param[out] cause, pointer to store cause code, can be NULL.
287 * \param[in] epname, must contain trunk prefix.
288 * \param[in] cfg, mgcp configuration (trunks).
289 * \returns endpoint or NULL if endpoint was not found. */
290struct mgcp_endpoint *mgcp_endp_by_name(int *cause, const char *epname,
291 struct mgcp_config *cfg)
292{
293 struct mgcp_trunk *trunk;
294 struct mgcp_endpoint *endp;
295 char epname_lc[MGCP_ENDPOINT_MAXLEN];
296
297 osmo_str_tolower_buf(epname_lc, sizeof(epname_lc), epname);
298 epname = epname_lc;
299
300 if (cause)
301 *cause = -500;
302
303 /* Identify the trunk where the endpoint is located */
304 trunk = mgcp_trunk_by_name(cfg, epname);
305 if (!trunk)
306 return NULL;
307
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200308 /* Identify the endpoint on the trunk */
309 endp = mgcp_endp_by_name_trunk(cause, epname, trunk);
310 if (!endp) {
311 return NULL;
312 }
313
314 if (cause)
315 *cause = 0;
316 return endp;
317}
Philipp Maier8d6a1932020-06-18 12:19:31 +0200318
319/* Get the E1 timeslot number from a given E1 endpoint name
320 * (e.g. ds/e1-0/s-30/su16-4), returns 0xff on error. */
321static uint8_t e1_ts_nr_from_epname(const char *epname)
322{
323 char buf[MGCP_ENDPOINT_MAXLEN + 1];
324 char *save_ptr = NULL;
325 char *buf_ptr = buf;
326 char *token;
327 unsigned long int res = 0;
328
329 strncpy(buf, epname, MGCP_ENDPOINT_MAXLEN);
330
331 while (1) {
332 token = strtok_r(buf_ptr, "/", &save_ptr);
333 buf_ptr = NULL;
334 if (!token)
335 break;
336 if (strncmp(token, "s-", 2) == 0) {
337 errno = 0;
338 res = strtoul(token + 2, NULL, 10);
Philipp Maier99d4d362020-09-07 11:58:09 +0200339 if (errno == ERANGE || res > NUM_E1_TS)
Philipp Maier8d6a1932020-06-18 12:19:31 +0200340 return 0xff;
341 return (uint8_t) res;
342 }
343 }
344
345 return 0xff;
346}
347
348/* Get the E1 timeslot number from a given E1 endpoint name
349 * (e.g. ds/e1-0/s-30/su16-4), returns 0xff on error. */
350static uint8_t e1_rate_from_epname(const char *epname)
351{
352 char buf[MGCP_ENDPOINT_MAXLEN + 1];
353 char *save_ptr = NULL;
354 char *buf_ptr = buf;
355 char *token;
356 unsigned long int res = 0;
357 unsigned int i;
358
359 strncpy(buf, epname, MGCP_ENDPOINT_MAXLEN);
360
361 while (1) {
362 token = strtok_r(buf_ptr, "/", &save_ptr);
363 buf_ptr = NULL;
364 if (!token)
365 break;
366 if (strncmp(token, "su", 2) == 0) {
367 errno = 0;
368 res = strtoul(token + 2, NULL, 10);
369 if (errno == ERANGE || res > E1_RATE_MAX)
370 return 0xff;
371 /* Make sure the rate is a valid rate */
372 for (i = 0; i < sizeof(e1_rates); i++) {
373 if (res == e1_rates[i])
374 return (uint8_t) res;
375 }
376 return 0xff;
377 }
378 }
379
380 return 0xff;
381}
382
383/* Get the E1 bitstream offset from a given E1 endpoint name
384 * (e.g. ds/e1-0/s-30/su16-4), returns 0xff on error. */
385static uint8_t e1_offs_from_epname(const char *epname)
386{
387 char buf[MGCP_ENDPOINT_MAXLEN + 1];
388 char *save_ptr = NULL;
389 char *buf_ptr = buf;
390 char *token;
391 unsigned long int res = 0;
392
393 strncpy(buf, epname, MGCP_ENDPOINT_MAXLEN);
394
395 while (1) {
396 token = strtok_r(buf_ptr, "/", &save_ptr);
397 buf_ptr = NULL;
398 if (!token)
399 break;
400 if (strncmp(token, "su", 2) == 0) {
401 token = strstr(token, "-");
402 if (!token)
403 return 0xff;
404 token += 1;
405 errno = 0;
406 res = strtoul(token, NULL, 10);
407 if (errno == ERANGE || res > E1_OFFS_MAX)
408 return 0xff;
409 return (uint8_t) res;
410 }
411 }
412
413 return 0xff;
414}
415
416/* Get the E1 subslot number (internal) from a given E1 endpoint name
417 * (e.g. ds/e1-0/s-30/su16-4), returns 0xff on error. */
418static uint8_t e1_ss_nr_from_epname(const char *epname)
419{
420 uint8_t rate;
421 uint8_t offs;
422 unsigned int i;
423
424 rate = e1_rate_from_epname(epname);
425 offs = e1_offs_from_epname(epname);
426
427 osmo_static_assert(sizeof(e1_rates) == sizeof(e1_offsets), e1_rates_e1_offsets_size);
428
429 for (i = 0; i < sizeof(e1_rates); i++) {
430 if ((e1_rates[i] == rate) && (e1_offsets[i] == offs))
431 return i;
432 }
433
434 return 0xff;
435}
436
437/* Check if the selected E1 endpoint is avalable, which means that none of
438 * the overlapping endpoints are currently serving a call. (if the system
439 * is properly configured such a situation should never ocurr!) */
440static bool endp_avail_e1(struct mgcp_endpoint *endp)
441{
442 /* The following map shows the overlapping of the subslots and their
443 * respective rates. The numbers on the right running from top to bottom
444 * are the bit offsets in the whole 64k timeslot. The numbers inside the
445 * boxes symbolize the internal subslot number (array index) and the
446 * rate in the form: i:r where i is the subslot number and r the
447 * respective rate.
448 *
449 * +--------+--------+--------+--------+ 0
450 * | | | | 7:8k |
451 * | | + 3:16k +--------+ 1
452 * | | | | 8:8k |
453 * | | 1:32k +--------+--------+ 2
454 * | | | | 9:8k |
455 * | | + 4:16k +--------+ 3
456 * | | | | 10:8k |
457 * | 0:64k +--------+--------+--------+ 4
458 * | | | | 11:8k |
459 * | | + 5:16k +--------+ 5
460 * | | | | 12:8k |
461 * | | 2:32k +--------+--------+ 6
462 * | | | | 13:8k |
463 * | | + 6:16k +--------+ 7
464 * | | | | 14:8k |
465 * +--------+--------+--------+--------+ 8
466 *
467 * The following array contains tables with the subslot numbers that must be
468 * unused for each subslot. During this test we do not have to check the
469 * endpoint we need to verify, only the overlaps need to be checked. This is
470 * also the reason why the related subslot number is missing from each each
471 * line. */
Philipp Maierfe67e092020-07-07 21:21:45 +0200472 const int8_t interlock_tab[MGCP_ENDP_E1_SUBSLOTS][15] = {
473 { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, -1 },
Philipp Maier8d6a1932020-06-18 12:19:31 +0200474 { 0, 3, 4, 7, 8, 9, 10, -1, -1, -1, -1, -1, -1, -1, -1 },
475 { 0, 5, 6, 11, 12, 13, 14, -1, -1, -1, -1, -1, -1, -1, -1 },
476 { 0, 1, 7, 8, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
477 { 0, 1, 9, 10, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
478 { 0, 2, 11, 12, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
479 { 0, 2, 13, 14, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
480 { 0, 1, 3, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
481 { 0, 1, 3, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
482 { 0, 1, 4, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
483 { 0, 1, 4, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
484 { 0, 2, 5, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
485 { 0, 2, 5, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
486 { 0, 2, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
487 { 0, 2, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 } };
488
489 const int8_t *interlock;
490 unsigned int i;
491 uint8_t ts_nr = 0;
492 uint8_t ss_nr = 0;
493 char *epname_check;
494 struct mgcp_endpoint *endp_check;
495 bool available = true;
496
497 /* This function must only be used with E1 type endpoints! */
498 OSMO_ASSERT(endp->trunk->trunk_type == MGCP_TRUNK_E1);
499
500 ts_nr = e1_ts_nr_from_epname(endp->name);
501 ss_nr = e1_ss_nr_from_epname(endp->name);
502 if (ts_nr == 0xff || ss_nr == 0xff) {
503 LOGPENDP(endp, DLMGCP, LOGL_ERROR,
504 "cannot check endpoint availability, endpoint name not parseable!\n");
505 return false;
506 }
507
508 interlock = interlock_tab[ss_nr];
509
510 for (i = 0; i < sizeof(interlock_tab[0]); i++) {
511 /* Detect row end */
512 if (interlock[i] == -1)
513 break;
514
515 /* Pick overlapping endpoint to check */
Philipp Maier0ffa3bd2020-06-30 16:17:03 +0200516 epname_check = gen_e1_epname(endp, endp->trunk->cfg->domain,
517 endp->trunk->trunk_nr, ts_nr,
518 interlock[i]);
Eric25ecc912021-09-06 03:43:50 +0200519 endp_check = mgcp_endp_find_specific(epname_check, endp->trunk);
Philipp Maier8d6a1932020-06-18 12:19:31 +0200520 if (!endp_check) {
521 LOGPENDP(endp, DLMGCP, LOGL_ERROR,
522 "cannot check endpoint availability, overlapping endpoint:%s not found!\n",
523 epname_check);
524 talloc_free(epname_check);
525 continue;
526 }
527 talloc_free(epname_check);
528
529 /* Check if overlapping endpoint currently serves another call
530 * (This is an exceptional situation, that should not occur
531 * in a properly configured environment!) */
532 if (endp_check->callid) {
533 LOGPENDP(endp, DLMGCP, LOGL_ERROR,
534 "endpoint unavailable - overlapping endpoint:%s already serves a call!\n",
535 endp_check->name);
536 available = false;
537 }
538 }
539
540 return available;
541}
542
543/*! check if an endpoint is available for any kind of operation.
544 * \param[in] endp endpoint to check.
545 * \returns true if endpoint is avalable, false it is blocked for any reason. */
546bool mgcp_endp_avail(struct mgcp_endpoint *endp)
547{
548 switch (endp->trunk->trunk_type) {
549 case MGCP_TRUNK_VIRTUAL:
550 /* There are no obstacles that may render a virtual trunk
551 * endpoint unusable, so virtual trunk endpoints are always
552 * available */
553 return true;
554 case MGCP_TRUNK_E1:
555 return endp_avail_e1(endp);
556 default:
557 OSMO_ASSERT(false);
558 }
559
560 return false;
561}
Philipp Maier889fe7f2020-07-06 17:44:12 +0200562
563/*! claim endpoint, sets callid and activates endpoint, should be called at the
564 * beginning of the CRCX procedure when it is clear that a new call should be
565 * created.
566 * \param[in] endp endpoint to claim.
567 * \param[in] callid that is assingned to this endpoint. */
568int mgcp_endp_claim(struct mgcp_endpoint *endp, const char *callid)
569{
570 int rc = 0;
571 uint8_t ts;
572 uint8_t ss;
573 uint8_t offs;
574
575 /* TODO: Make this function more intelligent, it should run the
576 * call id checks we currently have in protocol.c directly here. */
577
578 /* Set the callid, creation of another connection will only be possible
579 * when the callid matches up. (Connections are distinguished by their
580 * connection ids) */
581 endp->callid = talloc_strdup(endp, callid);
582 OSMO_ASSERT(endp->callid);
Philipp Maier124a3e02021-07-26 11:17:15 +0200583 osmo_stat_item_inc(osmo_stat_item_group_get_item(endp->trunk->stats.common,
584 TRUNK_STAT_ENDPOINTS_USED), 1);
Philipp Maier889fe7f2020-07-06 17:44:12 +0200585
586 /* Allocate resources */
587 switch (endp->trunk->trunk_type) {
588 case MGCP_TRUNK_VIRTUAL:
589 /* No additional initaliziation required here, virtual
590 * endpoints will open/close network sockets themselves
591 * on demand. */
592 break;
593 case MGCP_TRUNK_E1:
594 ts = e1_ts_nr_from_epname(endp->name);
595 ss = e1_ss_nr_from_epname(endp->name);
596 offs = e1_offs_from_epname(endp->name);
597 OSMO_ASSERT(ts != 0xFF);
598 OSMO_ASSERT(ts != 0);
599 OSMO_ASSERT(ss != 0xFF);
600 OSMO_ASSERT(offs != 0xFF);
601 rc = mgcp_e1_endp_equip(endp, ts, ss, offs);
602 break;
603 default:
604 OSMO_ASSERT(false);
605 }
606
Pau Espin Pedrol30e01352020-09-21 12:29:41 +0200607 /* Make sure the endpoint is released when claiming the endpoint fails. */
Philipp Maier889fe7f2020-07-06 17:44:12 +0200608 if (rc < 0)
609 mgcp_endp_release(endp);
610
611 return rc;
612}
613
614/*! update endpoint, updates internal endpoint specific data, should be
615 * after when MDCX or CRCX has been executed successuflly.
616 * \param[in] endp endpoint to update. */
617void mgcp_endp_update(struct mgcp_endpoint *endp)
618{
619 /* Allocate resources */
620 switch (endp->trunk->trunk_type) {
621 case MGCP_TRUNK_VIRTUAL:
622 /* No updating initaliziation required for virtual endpoints. */
623 break;
624 case MGCP_TRUNK_E1:
625 mgcp_e1_endp_update(endp);
626 break;
627 default:
628 OSMO_ASSERT(false);
629 }
630}
Pau Espin Pedrol6d0a59a2020-09-08 16:50:22 +0200631
632void mgcp_endp_add_conn(struct mgcp_endpoint *endp, struct mgcp_conn *conn)
633{
634 llist_add(&conn->entry, &endp->conns);
635}
636
637void mgcp_endp_remove_conn(struct mgcp_endpoint *endp, struct mgcp_conn *conn)
638{
639 /* Run endpoint cleanup action. By this we inform the endpoint about
640 * the removal of the connection and allow it to clean up its inner
641 * state accordingly */
642 if (endp->type->cleanup_cb)
643 endp->type->cleanup_cb(endp, conn);
644 llist_del(&conn->entry);
645 if (llist_empty(&endp->conns))
646 mgcp_endp_release(endp);
647}
Philipp Maiera8f13282023-02-09 13:34:28 +0100648
649/*! release endpoint, all open connections are closed.
650 * \param[in] endp endpoint to release */
651void mgcp_endp_release(struct mgcp_endpoint *endp)
652{
653 LOGPENDP(endp, DLMGCP, LOGL_DEBUG, "Releasing endpoint\n");
654
655 /* Normally this function should only be called when
656 * all connections have been removed already. In case
657 * that there are still connections open (e.g. when
658 * RSIP is executed), free them all at once. */
659 mgcp_conn_free_all(endp);
660
661 /* We must only decrement the stat item when the endpoint as actually
662 * claimed. An endpoint is claimed when a call-id is set */
663 if (endp->callid)
664 osmo_stat_item_dec(osmo_stat_item_group_get_item(endp->trunk->stats.common,
665 TRUNK_STAT_ENDPOINTS_USED), 1);
666
667 /* Reset endpoint parameters and states */
668 talloc_free(endp->callid);
669 endp->callid = NULL;
670 talloc_free(endp->local_options.string);
671 endp->local_options.string = NULL;
672 talloc_free(endp->local_options.codec);
673 endp->local_options.codec = NULL;
674
675 if (endp->trunk->trunk_type == MGCP_TRUNK_E1)
676 mgcp_e1_endp_release(endp);
677}
678