blob: 03e533021978590be9a97678555b4fbb85488f29 [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 /*! Convert all characters in epname to lowercase and strip trunk prefix and
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200161 * endpoint name suffix (domain name) from epname. The result is written to
162 * to the memory pointed at by epname_stripped. The expected size of the
163 * result is either equal or lower then the length of the input string
Eric25ecc912021-09-06 03:43:50 +0200164 * (epname)
165 * \param[out] epname_stripped pointer to store the stripped ep name.
166 * \param[in] epname endpoint name to lookup.
167 * \param[in] trunk where the endpoint is located. */
168void mgcp_endp_strip_name(char *epname_stripped, const char *epname,
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200169 const struct mgcp_trunk *trunk)
170{
171 osmo_str_tolower_buf(epname_stripped, MGCP_ENDPOINT_MAXLEN, epname);
172 chop_epname_prefix(epname_stripped, trunk);
173 chop_epname_suffix(epname_stripped, trunk);
174}
175
176/* Go through the trunk and find a random free (no active calls) endpoint,
177 * this function is called when a wildcarded request is carried out, which
178 * means that it is up to the MGW to choose a random free endpoint. */
179static struct mgcp_endpoint *find_free_endpoint(const struct mgcp_trunk *trunk)
180{
181 struct mgcp_endpoint *endp;
182 unsigned int i;
183
184 for (i = 0; i < trunk->number_endpoints; i++) {
185 endp = trunk->endpoints[i];
Philipp Maier8d6a1932020-06-18 12:19:31 +0200186 /* A free endpoint must not serve a call already and it must
187 * be available. */
188 if (endp->callid == NULL && mgcp_endp_avail(endp))
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200189 return endp;
190 }
191
192 return NULL;
193}
194
Eric25ecc912021-09-06 03:43:50 +0200195/*! Find an endpoint of a trunk specified by its name.
Ericaac84ed2021-11-09 16:22:01 +0100196 * \param[in] epname endpoint name to check.
197 * \param[in] trunk mgcp_trunk that might have this endpoint.
198 * \returns NULL if no ep found, else endpoint. */
Eric25ecc912021-09-06 03:43:50 +0200199struct mgcp_endpoint *mgcp_endp_find_specific(const char *epname,
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200200 const struct mgcp_trunk *trunk)
201{
202 char epname_stripped[MGCP_ENDPOINT_MAXLEN];
203 char epname_stripped_endp[MGCP_ENDPOINT_MAXLEN];
204 struct mgcp_endpoint *endp;
205 unsigned int i;
206
207 /* Strip irrelevant information from the endpoint name */
Eric25ecc912021-09-06 03:43:50 +0200208 mgcp_endp_strip_name(epname_stripped, epname, trunk);
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200209
210 for (i = 0; i < trunk->number_endpoints; i++) {
211 endp = trunk->endpoints[i];
Eric25ecc912021-09-06 03:43:50 +0200212 mgcp_endp_strip_name(epname_stripped_endp, endp->name, trunk);
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200213 if (strcmp(epname_stripped_endp, epname_stripped) == 0)
214 return endp;
215 }
216
217 return NULL;
218}
219
Philipp Maierd64c0412021-07-14 11:53:49 +0200220/*! Check if the given epname refers to a wildcarded request or to a specific
221 * endpoint.
222 * \param[in] epname endpoint name to check
223 * \returns true if epname refers to wildcarded request, else false. */
224bool mgcp_endp_is_wildcarded(const char *epname)
225{
226 if (strstr(epname, "*"))
227 return true;
228
229 return false;
230}
231
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200232/*! Find an endpoint by its name on a specified trunk.
233 * \param[out] cause pointer to store cause code, can be NULL.
234 * \param[in] epname endpoint name to lookup.
235 * \param[in] trunk where the endpoint is located.
236 * \returns endpoint or NULL if endpoint was not found. */
237struct mgcp_endpoint *mgcp_endp_by_name_trunk(int *cause, const char *epname,
238 const struct mgcp_trunk *trunk)
239{
240 struct mgcp_endpoint *endp;
241
242 if (cause)
243 *cause = 0;
244
245 /* At the moment we only support a primitive ('*'-only) method of
246 * wildcarded endpoint searches that picks the next free endpoint on
247 * a trunk. */
Philipp Maierd64c0412021-07-14 11:53:49 +0200248 if (mgcp_endp_is_wildcarded(epname)) {
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200249 endp = find_free_endpoint(trunk);
250 if (endp) {
251 LOGPENDP(endp, DLMGCP, LOGL_DEBUG,
252 "(trunk:%d) found free endpoint: %s\n",
253 trunk->trunk_nr, endp->name);
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200254 return endp;
255 }
256
257 LOGP(DLMGCP, LOGL_ERROR,
258 "(trunk:%d) Not able to find a free endpoint\n",
259 trunk->trunk_nr);
260 if (cause)
261 *cause = -403;
262 return NULL;
263 }
264
265 /* Find an endpoint by its name (if wildcarded request is not
266 * applicable) */
Eric25ecc912021-09-06 03:43:50 +0200267 endp = mgcp_endp_find_specific(epname, trunk);
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200268 if (endp) {
269 LOGPENDP(endp, DLMGCP, LOGL_DEBUG,
270 "(trunk:%d) found endpoint: %s\n",
271 trunk->trunk_nr, endp->name);
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200272 return endp;
273 }
274
275 LOGP(DLMGCP, LOGL_ERROR,
276 "(trunk:%d) Not able to find specified endpoint: %s\n",
277 trunk->trunk_nr, epname);
278 if (cause)
279 *cause = -500;
280
281 return NULL;
282}
283
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200284/*! Find an endpoint by its name, search at all trunks.
285 * \param[out] cause, pointer to store cause code, can be NULL.
286 * \param[in] epname, must contain trunk prefix.
287 * \param[in] cfg, mgcp configuration (trunks).
288 * \returns endpoint or NULL if endpoint was not found. */
289struct mgcp_endpoint *mgcp_endp_by_name(int *cause, const char *epname,
290 struct mgcp_config *cfg)
291{
292 struct mgcp_trunk *trunk;
293 struct mgcp_endpoint *endp;
294 char epname_lc[MGCP_ENDPOINT_MAXLEN];
295
296 osmo_str_tolower_buf(epname_lc, sizeof(epname_lc), epname);
297 epname = epname_lc;
298
299 if (cause)
300 *cause = -500;
301
302 /* Identify the trunk where the endpoint is located */
303 trunk = mgcp_trunk_by_name(cfg, epname);
304 if (!trunk)
305 return NULL;
306
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200307 /* Identify the endpoint on the trunk */
308 endp = mgcp_endp_by_name_trunk(cause, epname, trunk);
309 if (!endp) {
310 return NULL;
311 }
312
313 if (cause)
314 *cause = 0;
315 return endp;
316}
Philipp Maier8d6a1932020-06-18 12:19:31 +0200317
318/* Get the E1 timeslot number from a given E1 endpoint name
319 * (e.g. ds/e1-0/s-30/su16-4), returns 0xff on error. */
320static uint8_t e1_ts_nr_from_epname(const char *epname)
321{
322 char buf[MGCP_ENDPOINT_MAXLEN + 1];
323 char *save_ptr = NULL;
324 char *buf_ptr = buf;
325 char *token;
326 unsigned long int res = 0;
327
328 strncpy(buf, epname, MGCP_ENDPOINT_MAXLEN);
329
330 while (1) {
331 token = strtok_r(buf_ptr, "/", &save_ptr);
332 buf_ptr = NULL;
333 if (!token)
334 break;
335 if (strncmp(token, "s-", 2) == 0) {
336 errno = 0;
337 res = strtoul(token + 2, NULL, 10);
Philipp Maier99d4d362020-09-07 11:58:09 +0200338 if (errno == ERANGE || res > NUM_E1_TS)
Philipp Maier8d6a1932020-06-18 12:19:31 +0200339 return 0xff;
340 return (uint8_t) res;
341 }
342 }
343
344 return 0xff;
345}
346
347/* Get the E1 timeslot number from a given E1 endpoint name
348 * (e.g. ds/e1-0/s-30/su16-4), returns 0xff on error. */
349static uint8_t e1_rate_from_epname(const char *epname)
350{
351 char buf[MGCP_ENDPOINT_MAXLEN + 1];
352 char *save_ptr = NULL;
353 char *buf_ptr = buf;
354 char *token;
355 unsigned long int res = 0;
356 unsigned int i;
357
358 strncpy(buf, epname, MGCP_ENDPOINT_MAXLEN);
359
360 while (1) {
361 token = strtok_r(buf_ptr, "/", &save_ptr);
362 buf_ptr = NULL;
363 if (!token)
364 break;
365 if (strncmp(token, "su", 2) == 0) {
366 errno = 0;
367 res = strtoul(token + 2, NULL, 10);
368 if (errno == ERANGE || res > E1_RATE_MAX)
369 return 0xff;
370 /* Make sure the rate is a valid rate */
371 for (i = 0; i < sizeof(e1_rates); i++) {
372 if (res == e1_rates[i])
373 return (uint8_t) res;
374 }
375 return 0xff;
376 }
377 }
378
379 return 0xff;
380}
381
382/* Get the E1 bitstream offset from a given E1 endpoint name
383 * (e.g. ds/e1-0/s-30/su16-4), returns 0xff on error. */
384static uint8_t e1_offs_from_epname(const char *epname)
385{
386 char buf[MGCP_ENDPOINT_MAXLEN + 1];
387 char *save_ptr = NULL;
388 char *buf_ptr = buf;
389 char *token;
390 unsigned long int res = 0;
391
392 strncpy(buf, epname, MGCP_ENDPOINT_MAXLEN);
393
394 while (1) {
395 token = strtok_r(buf_ptr, "/", &save_ptr);
396 buf_ptr = NULL;
397 if (!token)
398 break;
399 if (strncmp(token, "su", 2) == 0) {
400 token = strstr(token, "-");
401 if (!token)
402 return 0xff;
403 token += 1;
404 errno = 0;
405 res = strtoul(token, NULL, 10);
406 if (errno == ERANGE || res > E1_OFFS_MAX)
407 return 0xff;
408 return (uint8_t) res;
409 }
410 }
411
412 return 0xff;
413}
414
415/* Get the E1 subslot number (internal) from a given E1 endpoint name
416 * (e.g. ds/e1-0/s-30/su16-4), returns 0xff on error. */
417static uint8_t e1_ss_nr_from_epname(const char *epname)
418{
419 uint8_t rate;
420 uint8_t offs;
421 unsigned int i;
422
423 rate = e1_rate_from_epname(epname);
424 offs = e1_offs_from_epname(epname);
425
426 osmo_static_assert(sizeof(e1_rates) == sizeof(e1_offsets), e1_rates_e1_offsets_size);
427
428 for (i = 0; i < sizeof(e1_rates); i++) {
429 if ((e1_rates[i] == rate) && (e1_offsets[i] == offs))
430 return i;
431 }
432
433 return 0xff;
434}
435
436/* Check if the selected E1 endpoint is avalable, which means that none of
437 * the overlapping endpoints are currently serving a call. (if the system
438 * is properly configured such a situation should never ocurr!) */
439static bool endp_avail_e1(struct mgcp_endpoint *endp)
440{
441 /* The following map shows the overlapping of the subslots and their
442 * respective rates. The numbers on the right running from top to bottom
443 * are the bit offsets in the whole 64k timeslot. The numbers inside the
444 * boxes symbolize the internal subslot number (array index) and the
445 * rate in the form: i:r where i is the subslot number and r the
446 * respective rate.
447 *
448 * +--------+--------+--------+--------+ 0
449 * | | | | 7:8k |
450 * | | + 3:16k +--------+ 1
451 * | | | | 8:8k |
452 * | | 1:32k +--------+--------+ 2
453 * | | | | 9:8k |
454 * | | + 4:16k +--------+ 3
455 * | | | | 10:8k |
456 * | 0:64k +--------+--------+--------+ 4
457 * | | | | 11:8k |
458 * | | + 5:16k +--------+ 5
459 * | | | | 12:8k |
460 * | | 2:32k +--------+--------+ 6
461 * | | | | 13:8k |
462 * | | + 6:16k +--------+ 7
463 * | | | | 14:8k |
464 * +--------+--------+--------+--------+ 8
465 *
466 * The following array contains tables with the subslot numbers that must be
467 * unused for each subslot. During this test we do not have to check the
468 * endpoint we need to verify, only the overlaps need to be checked. This is
469 * also the reason why the related subslot number is missing from each each
470 * line. */
Philipp Maierfe67e092020-07-07 21:21:45 +0200471 const int8_t interlock_tab[MGCP_ENDP_E1_SUBSLOTS][15] = {
472 { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, -1 },
Philipp Maier8d6a1932020-06-18 12:19:31 +0200473 { 0, 3, 4, 7, 8, 9, 10, -1, -1, -1, -1, -1, -1, -1, -1 },
474 { 0, 5, 6, 11, 12, 13, 14, -1, -1, -1, -1, -1, -1, -1, -1 },
475 { 0, 1, 7, 8, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
476 { 0, 1, 9, 10, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
477 { 0, 2, 11, 12, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
478 { 0, 2, 13, 14, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
479 { 0, 1, 3, -1, -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, 4, -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, 2, 5, -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, 6, -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
488 const int8_t *interlock;
489 unsigned int i;
490 uint8_t ts_nr = 0;
491 uint8_t ss_nr = 0;
492 char *epname_check;
493 struct mgcp_endpoint *endp_check;
494 bool available = true;
495
496 /* This function must only be used with E1 type endpoints! */
497 OSMO_ASSERT(endp->trunk->trunk_type == MGCP_TRUNK_E1);
498
499 ts_nr = e1_ts_nr_from_epname(endp->name);
500 ss_nr = e1_ss_nr_from_epname(endp->name);
501 if (ts_nr == 0xff || ss_nr == 0xff) {
502 LOGPENDP(endp, DLMGCP, LOGL_ERROR,
503 "cannot check endpoint availability, endpoint name not parseable!\n");
504 return false;
505 }
506
507 interlock = interlock_tab[ss_nr];
508
509 for (i = 0; i < sizeof(interlock_tab[0]); i++) {
510 /* Detect row end */
511 if (interlock[i] == -1)
512 break;
513
514 /* Pick overlapping endpoint to check */
Philipp Maier0ffa3bd2020-06-30 16:17:03 +0200515 epname_check = gen_e1_epname(endp, endp->trunk->cfg->domain,
516 endp->trunk->trunk_nr, ts_nr,
517 interlock[i]);
Eric25ecc912021-09-06 03:43:50 +0200518 endp_check = mgcp_endp_find_specific(epname_check, endp->trunk);
Philipp Maier8d6a1932020-06-18 12:19:31 +0200519 if (!endp_check) {
520 LOGPENDP(endp, DLMGCP, LOGL_ERROR,
521 "cannot check endpoint availability, overlapping endpoint:%s not found!\n",
522 epname_check);
523 talloc_free(epname_check);
524 continue;
525 }
526 talloc_free(epname_check);
527
528 /* Check if overlapping endpoint currently serves another call
529 * (This is an exceptional situation, that should not occur
530 * in a properly configured environment!) */
531 if (endp_check->callid) {
532 LOGPENDP(endp, DLMGCP, LOGL_ERROR,
533 "endpoint unavailable - overlapping endpoint:%s already serves a call!\n",
534 endp_check->name);
535 available = false;
536 }
537 }
538
539 return available;
540}
541
542/*! check if an endpoint is available for any kind of operation.
543 * \param[in] endp endpoint to check.
544 * \returns true if endpoint is avalable, false it is blocked for any reason. */
545bool mgcp_endp_avail(struct mgcp_endpoint *endp)
546{
547 switch (endp->trunk->trunk_type) {
548 case MGCP_TRUNK_VIRTUAL:
549 /* There are no obstacles that may render a virtual trunk
550 * endpoint unusable, so virtual trunk endpoints are always
551 * available */
552 return true;
553 case MGCP_TRUNK_E1:
554 return endp_avail_e1(endp);
555 default:
556 OSMO_ASSERT(false);
557 }
558
559 return false;
560}
Philipp Maier889fe7f2020-07-06 17:44:12 +0200561
562/*! claim endpoint, sets callid and activates endpoint, should be called at the
563 * beginning of the CRCX procedure when it is clear that a new call should be
564 * created.
565 * \param[in] endp endpoint to claim.
566 * \param[in] callid that is assingned to this endpoint. */
567int mgcp_endp_claim(struct mgcp_endpoint *endp, const char *callid)
568{
569 int rc = 0;
570 uint8_t ts;
571 uint8_t ss;
572 uint8_t offs;
573
574 /* TODO: Make this function more intelligent, it should run the
575 * call id checks we currently have in protocol.c directly here. */
576
577 /* Set the callid, creation of another connection will only be possible
578 * when the callid matches up. (Connections are distinguished by their
579 * connection ids) */
580 endp->callid = talloc_strdup(endp, callid);
581 OSMO_ASSERT(endp->callid);
Philipp Maier124a3e02021-07-26 11:17:15 +0200582 osmo_stat_item_inc(osmo_stat_item_group_get_item(endp->trunk->stats.common,
583 TRUNK_STAT_ENDPOINTS_USED), 1);
Philipp Maier889fe7f2020-07-06 17:44:12 +0200584
585 /* Allocate resources */
586 switch (endp->trunk->trunk_type) {
587 case MGCP_TRUNK_VIRTUAL:
588 /* No additional initaliziation required here, virtual
589 * endpoints will open/close network sockets themselves
590 * on demand. */
591 break;
592 case MGCP_TRUNK_E1:
593 ts = e1_ts_nr_from_epname(endp->name);
594 ss = e1_ss_nr_from_epname(endp->name);
595 offs = e1_offs_from_epname(endp->name);
596 OSMO_ASSERT(ts != 0xFF);
597 OSMO_ASSERT(ts != 0);
598 OSMO_ASSERT(ss != 0xFF);
599 OSMO_ASSERT(offs != 0xFF);
600 rc = mgcp_e1_endp_equip(endp, ts, ss, offs);
601 break;
602 default:
603 OSMO_ASSERT(false);
604 }
605
Pau Espin Pedrol30e01352020-09-21 12:29:41 +0200606 /* Make sure the endpoint is released when claiming the endpoint fails. */
Philipp Maier889fe7f2020-07-06 17:44:12 +0200607 if (rc < 0)
608 mgcp_endp_release(endp);
609
610 return rc;
611}
612
613/*! update endpoint, updates internal endpoint specific data, should be
614 * after when MDCX or CRCX has been executed successuflly.
615 * \param[in] endp endpoint to update. */
616void mgcp_endp_update(struct mgcp_endpoint *endp)
617{
618 /* Allocate resources */
619 switch (endp->trunk->trunk_type) {
620 case MGCP_TRUNK_VIRTUAL:
621 /* No updating initaliziation required for virtual endpoints. */
622 break;
623 case MGCP_TRUNK_E1:
624 mgcp_e1_endp_update(endp);
625 break;
626 default:
627 OSMO_ASSERT(false);
628 }
629}
Pau Espin Pedrol6d0a59a2020-09-08 16:50:22 +0200630
631void mgcp_endp_add_conn(struct mgcp_endpoint *endp, struct mgcp_conn *conn)
632{
633 llist_add(&conn->entry, &endp->conns);
634}
635
636void mgcp_endp_remove_conn(struct mgcp_endpoint *endp, struct mgcp_conn *conn)
637{
638 /* Run endpoint cleanup action. By this we inform the endpoint about
639 * the removal of the connection and allow it to clean up its inner
640 * state accordingly */
641 if (endp->type->cleanup_cb)
642 endp->type->cleanup_cb(endp, conn);
643 llist_del(&conn->entry);
644 if (llist_empty(&endp->conns))
645 mgcp_endp_release(endp);
646}
Philipp Maiera8f13282023-02-09 13:34:28 +0100647
648/*! release endpoint, all open connections are closed.
649 * \param[in] endp endpoint to release */
650void mgcp_endp_release(struct mgcp_endpoint *endp)
651{
652 LOGPENDP(endp, DLMGCP, LOGL_DEBUG, "Releasing endpoint\n");
653
654 /* Normally this function should only be called when
655 * all connections have been removed already. In case
656 * that there are still connections open (e.g. when
657 * RSIP is executed), free them all at once. */
658 mgcp_conn_free_all(endp);
659
660 /* We must only decrement the stat item when the endpoint as actually
661 * claimed. An endpoint is claimed when a call-id is set */
662 if (endp->callid)
663 osmo_stat_item_dec(osmo_stat_item_group_get_item(endp->trunk->stats.common,
664 TRUNK_STAT_ENDPOINTS_USED), 1);
665
666 /* Reset endpoint parameters and states */
667 talloc_free(endp->callid);
668 endp->callid = NULL;
669 talloc_free(endp->local_options.string);
670 endp->local_options.string = NULL;
671 talloc_free(endp->local_options.codec);
672 endp->local_options.codec = NULL;
673
674 if (endp->trunk->trunk_type == MGCP_TRUNK_E1)
675 mgcp_e1_endp_release(endp);
676}
677