blob: a41e0d0f6a5fde251de85934d05bcd44649ca01f [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 Maieredc00f42018-01-24 11:58:56 +0100113/*! release endpoint, all open connections are closed.
114 * \param[in] endp endpoint to release */
Philipp Maier1355d7e2018-02-01 14:30:06 +0100115void mgcp_endp_release(struct mgcp_endpoint *endp)
Philipp Maieredc00f42018-01-24 11:58:56 +0100116{
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200117 LOGPENDP(endp, DLMGCP, LOGL_DEBUG, "Releasing endpoint\n");
Philipp Maieredc00f42018-01-24 11:58:56 +0100118
119 /* Normally this function should only be called when
120 * all connections have been removed already. In case
121 * that there are still connections open (e.g. when
122 * RSIP is executed), free them all at once. */
123 mgcp_conn_free_all(endp);
124
Philipp Maier124a3e02021-07-26 11:17:15 +0200125 /* We must only decrement the stat item when the endpoint as actually
126 * claimed. An endpoint is claimed when a call-id is set */
127 if (endp->callid)
128 osmo_stat_item_dec(osmo_stat_item_group_get_item(endp->trunk->stats.common,
129 TRUNK_STAT_ENDPOINTS_USED), 1);
130
Philipp Maieredc00f42018-01-24 11:58:56 +0100131 /* Reset endpoint parameters and states */
132 talloc_free(endp->callid);
133 endp->callid = NULL;
134 talloc_free(endp->local_options.string);
135 endp->local_options.string = NULL;
136 talloc_free(endp->local_options.codec);
137 endp->local_options.codec = NULL;
Philipp Maier889fe7f2020-07-06 17:44:12 +0200138
139 if (endp->trunk->trunk_type == MGCP_TRUNK_E1)
140 mgcp_e1_endp_release(endp);
Philipp Maieredc00f42018-01-24 11:58:56 +0100141}
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200142
143/* Check if the endpoint name contains the prefix (e.g. "rtpbridge/" or
144 * "ds/e1-") and write the epname without the prefix back to the memory
145 * pointed at by epname. (per trunk the prefix is the same for all endpoints,
146 * so no ambiguity is introduced) */
147static void chop_epname_prefix(char *epname, const struct mgcp_trunk *trunk)
148{
149 size_t prefix_len;
150 switch (trunk->trunk_type) {
151 case MGCP_TRUNK_VIRTUAL:
152 prefix_len = sizeof(MGCP_ENDPOINT_PREFIX_VIRTUAL_TRUNK) - 1;
153 if (strncmp
154 (epname, MGCP_ENDPOINT_PREFIX_VIRTUAL_TRUNK,
155 prefix_len) == 0)
156 memmove(epname, epname + prefix_len,
157 strlen(epname) - prefix_len + 1);
158 return;
159 case MGCP_TRUNK_E1:
160 prefix_len = sizeof(MGCP_ENDPOINT_PREFIX_E1_TRUNK) - 1;
161 if (strncmp
162 (epname, MGCP_ENDPOINT_PREFIX_VIRTUAL_TRUNK,
163 prefix_len) == 0)
164 memmove(epname, epname + prefix_len,
165 strlen(epname) - prefix_len + 1);
166 return;
167 default:
168 OSMO_ASSERT(false);
169 }
170}
171
172/* Check if the endpoint name contains a suffix (e.g. "@mgw") and truncate
173 * epname by writing a '\0' char where the suffix starts. */
174static void chop_epname_suffix(char *epname, const struct mgcp_trunk *trunk)
175{
176 char *suffix_begin;
177
178 /* Endpoints on the virtual trunk may have a domain name that is
179 * followed after an @ character, this can be chopped off. All
180 * other supported trunk types do not have any suffixes that may
181 * be chopped off */
182 if (trunk->trunk_type == MGCP_TRUNK_VIRTUAL) {
183 suffix_begin = strchr(epname, '@');
184 if (!suffix_begin)
185 return;
186 *suffix_begin = '\0';
187 }
188}
189
Eric25ecc912021-09-06 03:43:50 +0200190
191 /*! Convert all characters in epname to lowercase and strip trunk prefix and
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200192 * endpoint name suffix (domain name) from epname. The result is written to
193 * to the memory pointed at by epname_stripped. The expected size of the
194 * result is either equal or lower then the length of the input string
Eric25ecc912021-09-06 03:43:50 +0200195 * (epname)
196 * \param[out] epname_stripped pointer to store the stripped ep name.
197 * \param[in] epname endpoint name to lookup.
198 * \param[in] trunk where the endpoint is located. */
199void mgcp_endp_strip_name(char *epname_stripped, const char *epname,
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200200 const struct mgcp_trunk *trunk)
201{
202 osmo_str_tolower_buf(epname_stripped, MGCP_ENDPOINT_MAXLEN, epname);
203 chop_epname_prefix(epname_stripped, trunk);
204 chop_epname_suffix(epname_stripped, trunk);
205}
206
207/* Go through the trunk and find a random free (no active calls) endpoint,
208 * this function is called when a wildcarded request is carried out, which
209 * means that it is up to the MGW to choose a random free endpoint. */
210static struct mgcp_endpoint *find_free_endpoint(const struct mgcp_trunk *trunk)
211{
212 struct mgcp_endpoint *endp;
213 unsigned int i;
214
215 for (i = 0; i < trunk->number_endpoints; i++) {
216 endp = trunk->endpoints[i];
Philipp Maier8d6a1932020-06-18 12:19:31 +0200217 /* A free endpoint must not serve a call already and it must
218 * be available. */
219 if (endp->callid == NULL && mgcp_endp_avail(endp))
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200220 return endp;
221 }
222
223 return NULL;
224}
225
Eric25ecc912021-09-06 03:43:50 +0200226/*! Find an endpoint of a trunk specified by its name.
Ericaac84ed2021-11-09 16:22:01 +0100227 * \param[in] epname endpoint name to check.
228 * \param[in] trunk mgcp_trunk that might have this endpoint.
229 * \returns NULL if no ep found, else endpoint. */
Eric25ecc912021-09-06 03:43:50 +0200230struct mgcp_endpoint *mgcp_endp_find_specific(const char *epname,
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200231 const struct mgcp_trunk *trunk)
232{
233 char epname_stripped[MGCP_ENDPOINT_MAXLEN];
234 char epname_stripped_endp[MGCP_ENDPOINT_MAXLEN];
235 struct mgcp_endpoint *endp;
236 unsigned int i;
237
238 /* Strip irrelevant information from the endpoint name */
Eric25ecc912021-09-06 03:43:50 +0200239 mgcp_endp_strip_name(epname_stripped, epname, trunk);
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200240
241 for (i = 0; i < trunk->number_endpoints; i++) {
242 endp = trunk->endpoints[i];
Eric25ecc912021-09-06 03:43:50 +0200243 mgcp_endp_strip_name(epname_stripped_endp, endp->name, trunk);
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200244 if (strcmp(epname_stripped_endp, epname_stripped) == 0)
245 return endp;
246 }
247
248 return NULL;
249}
250
Philipp Maierd64c0412021-07-14 11:53:49 +0200251/*! Check if the given epname refers to a wildcarded request or to a specific
252 * endpoint.
253 * \param[in] epname endpoint name to check
254 * \returns true if epname refers to wildcarded request, else false. */
255bool mgcp_endp_is_wildcarded(const char *epname)
256{
257 if (strstr(epname, "*"))
258 return true;
259
260 return false;
261}
262
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200263/*! Find an endpoint by its name on a specified trunk.
264 * \param[out] cause pointer to store cause code, can be NULL.
265 * \param[in] epname endpoint name to lookup.
266 * \param[in] trunk where the endpoint is located.
267 * \returns endpoint or NULL if endpoint was not found. */
268struct mgcp_endpoint *mgcp_endp_by_name_trunk(int *cause, const char *epname,
269 const struct mgcp_trunk *trunk)
270{
271 struct mgcp_endpoint *endp;
272
273 if (cause)
274 *cause = 0;
275
276 /* At the moment we only support a primitive ('*'-only) method of
277 * wildcarded endpoint searches that picks the next free endpoint on
278 * a trunk. */
Philipp Maierd64c0412021-07-14 11:53:49 +0200279 if (mgcp_endp_is_wildcarded(epname)) {
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200280 endp = find_free_endpoint(trunk);
281 if (endp) {
282 LOGPENDP(endp, DLMGCP, LOGL_DEBUG,
283 "(trunk:%d) found free endpoint: %s\n",
284 trunk->trunk_nr, endp->name);
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200285 return endp;
286 }
287
288 LOGP(DLMGCP, LOGL_ERROR,
289 "(trunk:%d) Not able to find a free endpoint\n",
290 trunk->trunk_nr);
291 if (cause)
292 *cause = -403;
293 return NULL;
294 }
295
296 /* Find an endpoint by its name (if wildcarded request is not
297 * applicable) */
Eric25ecc912021-09-06 03:43:50 +0200298 endp = mgcp_endp_find_specific(epname, trunk);
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200299 if (endp) {
300 LOGPENDP(endp, DLMGCP, LOGL_DEBUG,
301 "(trunk:%d) found endpoint: %s\n",
302 trunk->trunk_nr, endp->name);
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200303 return endp;
304 }
305
306 LOGP(DLMGCP, LOGL_ERROR,
307 "(trunk:%d) Not able to find specified endpoint: %s\n",
308 trunk->trunk_nr, epname);
309 if (cause)
310 *cause = -500;
311
312 return NULL;
313}
314
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200315/*! Find an endpoint by its name, search at all trunks.
316 * \param[out] cause, pointer to store cause code, can be NULL.
317 * \param[in] epname, must contain trunk prefix.
318 * \param[in] cfg, mgcp configuration (trunks).
319 * \returns endpoint or NULL if endpoint was not found. */
320struct mgcp_endpoint *mgcp_endp_by_name(int *cause, const char *epname,
321 struct mgcp_config *cfg)
322{
323 struct mgcp_trunk *trunk;
324 struct mgcp_endpoint *endp;
325 char epname_lc[MGCP_ENDPOINT_MAXLEN];
326
327 osmo_str_tolower_buf(epname_lc, sizeof(epname_lc), epname);
328 epname = epname_lc;
329
330 if (cause)
331 *cause = -500;
332
333 /* Identify the trunk where the endpoint is located */
334 trunk = mgcp_trunk_by_name(cfg, epname);
335 if (!trunk)
336 return NULL;
337
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200338 /* Identify the endpoint on the trunk */
339 endp = mgcp_endp_by_name_trunk(cause, epname, trunk);
340 if (!endp) {
341 return NULL;
342 }
343
344 if (cause)
345 *cause = 0;
346 return endp;
347}
Philipp Maier8d6a1932020-06-18 12:19:31 +0200348
349/* Get the E1 timeslot number from a given E1 endpoint name
350 * (e.g. ds/e1-0/s-30/su16-4), returns 0xff on error. */
351static uint8_t e1_ts_nr_from_epname(const char *epname)
352{
353 char buf[MGCP_ENDPOINT_MAXLEN + 1];
354 char *save_ptr = NULL;
355 char *buf_ptr = buf;
356 char *token;
357 unsigned long int res = 0;
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, "s-", 2) == 0) {
367 errno = 0;
368 res = strtoul(token + 2, NULL, 10);
Philipp Maier99d4d362020-09-07 11:58:09 +0200369 if (errno == ERANGE || res > NUM_E1_TS)
Philipp Maier8d6a1932020-06-18 12:19:31 +0200370 return 0xff;
371 return (uint8_t) res;
372 }
373 }
374
375 return 0xff;
376}
377
378/* Get the E1 timeslot number from a given E1 endpoint name
379 * (e.g. ds/e1-0/s-30/su16-4), returns 0xff on error. */
380static uint8_t e1_rate_from_epname(const char *epname)
381{
382 char buf[MGCP_ENDPOINT_MAXLEN + 1];
383 char *save_ptr = NULL;
384 char *buf_ptr = buf;
385 char *token;
386 unsigned long int res = 0;
387 unsigned int i;
388
389 strncpy(buf, epname, MGCP_ENDPOINT_MAXLEN);
390
391 while (1) {
392 token = strtok_r(buf_ptr, "/", &save_ptr);
393 buf_ptr = NULL;
394 if (!token)
395 break;
396 if (strncmp(token, "su", 2) == 0) {
397 errno = 0;
398 res = strtoul(token + 2, NULL, 10);
399 if (errno == ERANGE || res > E1_RATE_MAX)
400 return 0xff;
401 /* Make sure the rate is a valid rate */
402 for (i = 0; i < sizeof(e1_rates); i++) {
403 if (res == e1_rates[i])
404 return (uint8_t) res;
405 }
406 return 0xff;
407 }
408 }
409
410 return 0xff;
411}
412
413/* Get the E1 bitstream offset from a given E1 endpoint name
414 * (e.g. ds/e1-0/s-30/su16-4), returns 0xff on error. */
415static uint8_t e1_offs_from_epname(const char *epname)
416{
417 char buf[MGCP_ENDPOINT_MAXLEN + 1];
418 char *save_ptr = NULL;
419 char *buf_ptr = buf;
420 char *token;
421 unsigned long int res = 0;
422
423 strncpy(buf, epname, MGCP_ENDPOINT_MAXLEN);
424
425 while (1) {
426 token = strtok_r(buf_ptr, "/", &save_ptr);
427 buf_ptr = NULL;
428 if (!token)
429 break;
430 if (strncmp(token, "su", 2) == 0) {
431 token = strstr(token, "-");
432 if (!token)
433 return 0xff;
434 token += 1;
435 errno = 0;
436 res = strtoul(token, NULL, 10);
437 if (errno == ERANGE || res > E1_OFFS_MAX)
438 return 0xff;
439 return (uint8_t) res;
440 }
441 }
442
443 return 0xff;
444}
445
446/* Get the E1 subslot number (internal) from a given E1 endpoint name
447 * (e.g. ds/e1-0/s-30/su16-4), returns 0xff on error. */
448static uint8_t e1_ss_nr_from_epname(const char *epname)
449{
450 uint8_t rate;
451 uint8_t offs;
452 unsigned int i;
453
454 rate = e1_rate_from_epname(epname);
455 offs = e1_offs_from_epname(epname);
456
457 osmo_static_assert(sizeof(e1_rates) == sizeof(e1_offsets), e1_rates_e1_offsets_size);
458
459 for (i = 0; i < sizeof(e1_rates); i++) {
460 if ((e1_rates[i] == rate) && (e1_offsets[i] == offs))
461 return i;
462 }
463
464 return 0xff;
465}
466
467/* Check if the selected E1 endpoint is avalable, which means that none of
468 * the overlapping endpoints are currently serving a call. (if the system
469 * is properly configured such a situation should never ocurr!) */
470static bool endp_avail_e1(struct mgcp_endpoint *endp)
471{
472 /* The following map shows the overlapping of the subslots and their
473 * respective rates. The numbers on the right running from top to bottom
474 * are the bit offsets in the whole 64k timeslot. The numbers inside the
475 * boxes symbolize the internal subslot number (array index) and the
476 * rate in the form: i:r where i is the subslot number and r the
477 * respective rate.
478 *
479 * +--------+--------+--------+--------+ 0
480 * | | | | 7:8k |
481 * | | + 3:16k +--------+ 1
482 * | | | | 8:8k |
483 * | | 1:32k +--------+--------+ 2
484 * | | | | 9:8k |
485 * | | + 4:16k +--------+ 3
486 * | | | | 10:8k |
487 * | 0:64k +--------+--------+--------+ 4
488 * | | | | 11:8k |
489 * | | + 5:16k +--------+ 5
490 * | | | | 12:8k |
491 * | | 2:32k +--------+--------+ 6
492 * | | | | 13:8k |
493 * | | + 6:16k +--------+ 7
494 * | | | | 14:8k |
495 * +--------+--------+--------+--------+ 8
496 *
497 * The following array contains tables with the subslot numbers that must be
498 * unused for each subslot. During this test we do not have to check the
499 * endpoint we need to verify, only the overlaps need to be checked. This is
500 * also the reason why the related subslot number is missing from each each
501 * line. */
Philipp Maierfe67e092020-07-07 21:21:45 +0200502 const int8_t interlock_tab[MGCP_ENDP_E1_SUBSLOTS][15] = {
503 { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, -1 },
Philipp Maier8d6a1932020-06-18 12:19:31 +0200504 { 0, 3, 4, 7, 8, 9, 10, -1, -1, -1, -1, -1, -1, -1, -1 },
505 { 0, 5, 6, 11, 12, 13, 14, -1, -1, -1, -1, -1, -1, -1, -1 },
506 { 0, 1, 7, 8, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
507 { 0, 1, 9, 10, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
508 { 0, 2, 11, 12, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
509 { 0, 2, 13, 14, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
510 { 0, 1, 3, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
511 { 0, 1, 3, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
512 { 0, 1, 4, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
513 { 0, 1, 4, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
514 { 0, 2, 5, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
515 { 0, 2, 5, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
516 { 0, 2, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
517 { 0, 2, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 } };
518
519 const int8_t *interlock;
520 unsigned int i;
521 uint8_t ts_nr = 0;
522 uint8_t ss_nr = 0;
523 char *epname_check;
524 struct mgcp_endpoint *endp_check;
525 bool available = true;
526
527 /* This function must only be used with E1 type endpoints! */
528 OSMO_ASSERT(endp->trunk->trunk_type == MGCP_TRUNK_E1);
529
530 ts_nr = e1_ts_nr_from_epname(endp->name);
531 ss_nr = e1_ss_nr_from_epname(endp->name);
532 if (ts_nr == 0xff || ss_nr == 0xff) {
533 LOGPENDP(endp, DLMGCP, LOGL_ERROR,
534 "cannot check endpoint availability, endpoint name not parseable!\n");
535 return false;
536 }
537
538 interlock = interlock_tab[ss_nr];
539
540 for (i = 0; i < sizeof(interlock_tab[0]); i++) {
541 /* Detect row end */
542 if (interlock[i] == -1)
543 break;
544
545 /* Pick overlapping endpoint to check */
Philipp Maier0ffa3bd2020-06-30 16:17:03 +0200546 epname_check = gen_e1_epname(endp, endp->trunk->cfg->domain,
547 endp->trunk->trunk_nr, ts_nr,
548 interlock[i]);
Eric25ecc912021-09-06 03:43:50 +0200549 endp_check = mgcp_endp_find_specific(epname_check, endp->trunk);
Philipp Maier8d6a1932020-06-18 12:19:31 +0200550 if (!endp_check) {
551 LOGPENDP(endp, DLMGCP, LOGL_ERROR,
552 "cannot check endpoint availability, overlapping endpoint:%s not found!\n",
553 epname_check);
554 talloc_free(epname_check);
555 continue;
556 }
557 talloc_free(epname_check);
558
559 /* Check if overlapping endpoint currently serves another call
560 * (This is an exceptional situation, that should not occur
561 * in a properly configured environment!) */
562 if (endp_check->callid) {
563 LOGPENDP(endp, DLMGCP, LOGL_ERROR,
564 "endpoint unavailable - overlapping endpoint:%s already serves a call!\n",
565 endp_check->name);
566 available = false;
567 }
568 }
569
570 return available;
571}
572
573/*! check if an endpoint is available for any kind of operation.
574 * \param[in] endp endpoint to check.
575 * \returns true if endpoint is avalable, false it is blocked for any reason. */
576bool mgcp_endp_avail(struct mgcp_endpoint *endp)
577{
578 switch (endp->trunk->trunk_type) {
579 case MGCP_TRUNK_VIRTUAL:
580 /* There are no obstacles that may render a virtual trunk
581 * endpoint unusable, so virtual trunk endpoints are always
582 * available */
583 return true;
584 case MGCP_TRUNK_E1:
585 return endp_avail_e1(endp);
586 default:
587 OSMO_ASSERT(false);
588 }
589
590 return false;
591}
Philipp Maier889fe7f2020-07-06 17:44:12 +0200592
593/*! claim endpoint, sets callid and activates endpoint, should be called at the
594 * beginning of the CRCX procedure when it is clear that a new call should be
595 * created.
596 * \param[in] endp endpoint to claim.
597 * \param[in] callid that is assingned to this endpoint. */
598int mgcp_endp_claim(struct mgcp_endpoint *endp, const char *callid)
599{
600 int rc = 0;
601 uint8_t ts;
602 uint8_t ss;
603 uint8_t offs;
604
605 /* TODO: Make this function more intelligent, it should run the
606 * call id checks we currently have in protocol.c directly here. */
607
608 /* Set the callid, creation of another connection will only be possible
609 * when the callid matches up. (Connections are distinguished by their
610 * connection ids) */
611 endp->callid = talloc_strdup(endp, callid);
612 OSMO_ASSERT(endp->callid);
Philipp Maier124a3e02021-07-26 11:17:15 +0200613 osmo_stat_item_inc(osmo_stat_item_group_get_item(endp->trunk->stats.common,
614 TRUNK_STAT_ENDPOINTS_USED), 1);
Philipp Maier889fe7f2020-07-06 17:44:12 +0200615
616 /* Allocate resources */
617 switch (endp->trunk->trunk_type) {
618 case MGCP_TRUNK_VIRTUAL:
619 /* No additional initaliziation required here, virtual
620 * endpoints will open/close network sockets themselves
621 * on demand. */
622 break;
623 case MGCP_TRUNK_E1:
624 ts = e1_ts_nr_from_epname(endp->name);
625 ss = e1_ss_nr_from_epname(endp->name);
626 offs = e1_offs_from_epname(endp->name);
627 OSMO_ASSERT(ts != 0xFF);
628 OSMO_ASSERT(ts != 0);
629 OSMO_ASSERT(ss != 0xFF);
630 OSMO_ASSERT(offs != 0xFF);
631 rc = mgcp_e1_endp_equip(endp, ts, ss, offs);
632 break;
633 default:
634 OSMO_ASSERT(false);
635 }
636
Pau Espin Pedrol30e01352020-09-21 12:29:41 +0200637 /* Make sure the endpoint is released when claiming the endpoint fails. */
Philipp Maier889fe7f2020-07-06 17:44:12 +0200638 if (rc < 0)
639 mgcp_endp_release(endp);
640
641 return rc;
642}
643
644/*! update endpoint, updates internal endpoint specific data, should be
645 * after when MDCX or CRCX has been executed successuflly.
646 * \param[in] endp endpoint to update. */
647void mgcp_endp_update(struct mgcp_endpoint *endp)
648{
649 /* Allocate resources */
650 switch (endp->trunk->trunk_type) {
651 case MGCP_TRUNK_VIRTUAL:
652 /* No updating initaliziation required for virtual endpoints. */
653 break;
654 case MGCP_TRUNK_E1:
655 mgcp_e1_endp_update(endp);
656 break;
657 default:
658 OSMO_ASSERT(false);
659 }
660}
Pau Espin Pedrol6d0a59a2020-09-08 16:50:22 +0200661
662void mgcp_endp_add_conn(struct mgcp_endpoint *endp, struct mgcp_conn *conn)
663{
664 llist_add(&conn->entry, &endp->conns);
665}
666
667void mgcp_endp_remove_conn(struct mgcp_endpoint *endp, struct mgcp_conn *conn)
668{
669 /* Run endpoint cleanup action. By this we inform the endpoint about
670 * the removal of the connection and allow it to clean up its inner
671 * state accordingly */
672 if (endp->type->cleanup_cb)
673 endp->type->cleanup_cb(endp, conn);
674 llist_del(&conn->entry);
675 if (llist_empty(&endp->conns))
676 mgcp_endp_release(endp);
677}