blob: 5e7bebb53a0b07502077b0e90b3113f6b58e3129 [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 Maier87bd9be2017-08-22 16:35:41 +020024#include <osmocom/mgcp/mgcp_internal.h>
Philipp Maier37d11c82018-02-01 14:38:12 +010025#include <osmocom/mgcp/mgcp_endp.h>
Philipp Maierc66ab2c2020-06-02 20:55:34 +020026#include <osmocom/mgcp/mgcp_trunk.h>
Philipp Maier87bd9be2017-08-22 16:35:41 +020027
Philipp Maier8d6a1932020-06-18 12:19:31 +020028#define E1_TIMESLOTS 32
29#define E1_RATE_MAX 64
30#define E1_OFFS_MAX 8
31
32/* A 64k timeslot on an E1 line can be subdevied into the following
33 * subslot combinations:
34 *
35 * subslot: offset:
36 * [ ][ ][ 16k ][8k_subslot] 0
37 * [ ][ 32k ][_subslot__][8k_subslot] 1
38 * [ ][ subslot ][ 16k ][8k_subslot] 2
39 * [ 64k ][__________][_subslot__][8k_subslot] 3
40 * [ timeslot ][ ][ 16k ][8k_subslot] 4
41 * [ ][ 32K ][_subslot__][8k_subslot] 5
42 * [ ][ subslot ][ 16k ][8k_subslot] 6
43 * [ ][ ][ subslot ][8k_subslot] 7
44 *
45 * Since overlapping assignment of subslots is not possible there is a limited
46 * set of subslot assignments possible. The e1_rates array lists the possible
47 * assignments as depicted above. Also each subslot assignment comes along with
48 * a bit offset in the E1 bitstream. The e1_offsets arrays lists the bit
49 * offsets. */
50static const uint8_t e1_rates[] =
51 { 64, 32, 32, 16, 16, 16, 16, 8, 8, 8, 8, 8, 8, 8, 8 };
52static const uint8_t e1_offsets[] =
53 { 0, 0, 4, 0, 2, 4, 6, 0, 1, 2, 3, 4, 5, 6, 7 };
54
Philipp Maier87bd9be2017-08-22 16:35:41 +020055/* Endpoint typeset definition */
56const struct mgcp_endpoint_typeset ep_typeset = {
57 /* Specify endpoint properties for RTP endpoint */
Philipp Maier0996a1e2020-06-10 15:27:14 +020058 .rtp = {
59 .max_conns = 2,
60 .dispatch_rtp_cb = mgcp_dispatch_rtp_bridge_cb,
61 .cleanup_cb = mgcp_cleanup_rtp_bridge_cb,
62 },
63 /* Specify endpoint properties for E1 endpoint */
64 .e1 = {
65 .max_conns = 1,
66 .dispatch_rtp_cb = mgcp_dispatch_e1_bridge_cb,
67 .cleanup_cb = mgcp_cleanup_e1_bridge_cb,
68 },
Philipp Maier87bd9be2017-08-22 16:35:41 +020069};
Philipp Maieredc00f42018-01-24 11:58:56 +010070
Philipp Maier7462b952020-06-10 14:50:34 +020071/* Generate virtual endpoint name from given parameters */
72static char *gen_virtual_epname(void *ctx, const char *domain,
73 unsigned int index)
74{
75 return talloc_asprintf(ctx, "%s%x@%s",
76 MGCP_ENDPOINT_PREFIX_VIRTUAL_TRUNK, index, domain);
77}
78
Philipp Maier98c09b32020-06-18 12:17:55 +020079/* Generate E1 endpoint name from given numeric parameters */
80static char *gen_e1_epname(void *ctx, uint8_t trunk_nr, uint8_t ts_nr,
81 uint8_t ss_nr)
82{
Philipp Maier98c09b32020-06-18 12:17:55 +020083 unsigned int rate;
84 unsigned int offset;
85
Philipp Maier8d6a1932020-06-18 12:19:31 +020086 OSMO_ASSERT(ss_nr < sizeof(e1_rates));
Philipp Maier98c09b32020-06-18 12:17:55 +020087
Philipp Maier8d6a1932020-06-18 12:19:31 +020088 rate = e1_rates[ss_nr];
89 offset = e1_offsets[ss_nr];
Philipp Maier98c09b32020-06-18 12:17:55 +020090
91 return talloc_asprintf(ctx, "%s%u/s-%u/su%u-%u",
92 MGCP_ENDPOINT_PREFIX_E1_TRUNK, trunk_nr, ts_nr, rate, offset);
93}
94
Philipp Maierc66ab2c2020-06-02 20:55:34 +020095/*! allocate an endpoint and set default values.
96 * \param[in] trunk configuration.
Philipp Maier7462b952020-06-10 14:50:34 +020097 * \param[in] name endpoint index.
Philipp Maierc66ab2c2020-06-02 20:55:34 +020098 * \returns endpoint on success, NULL on failure. */
Philipp Maier7462b952020-06-10 14:50:34 +020099struct mgcp_endpoint *mgcp_endp_alloc(struct mgcp_trunk *trunk,
100 unsigned int index)
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200101{
102 struct mgcp_endpoint *endp;
103
104 endp = talloc_zero(trunk->endpoints, struct mgcp_endpoint);
105 if (!endp)
106 return NULL;
107
108 INIT_LLIST_HEAD(&endp->conns);
109 endp->cfg = trunk->cfg;
110 endp->trunk = trunk;
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200111
112 switch (trunk->trunk_type) {
113 case MGCP_TRUNK_VIRTUAL:
114 endp->type = &ep_typeset.rtp;
Philipp Maier7462b952020-06-10 14:50:34 +0200115 endp->name = gen_virtual_epname(endp, trunk->cfg->domain, index);
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200116 break;
117 case MGCP_TRUNK_E1:
Philipp Maier7462b952020-06-10 14:50:34 +0200118 endp->type = &ep_typeset.rtp;
Philipp Maier98c09b32020-06-18 12:17:55 +0200119 endp->name = gen_e1_epname(endp, trunk->trunk_nr, index / 15, index % 15);
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200120 break;
121 default:
122 osmo_panic("Cannot allocate unimplemented trunk type %d! %s:%d\n",
123 trunk->trunk_type, __FILE__, __LINE__);
124 }
125
126 return endp;
127}
128
Philipp Maieredc00f42018-01-24 11:58:56 +0100129/*! release endpoint, all open connections are closed.
130 * \param[in] endp endpoint to release */
Philipp Maier1355d7e2018-02-01 14:30:06 +0100131void mgcp_endp_release(struct mgcp_endpoint *endp)
Philipp Maieredc00f42018-01-24 11:58:56 +0100132{
Pau Espin Pedrol3239f622019-04-24 18:47:46 +0200133 LOGPENDP(endp, DLMGCP, LOGL_DEBUG, "Releasing endpoint\n");
Philipp Maieredc00f42018-01-24 11:58:56 +0100134
135 /* Normally this function should only be called when
136 * all connections have been removed already. In case
137 * that there are still connections open (e.g. when
138 * RSIP is executed), free them all at once. */
139 mgcp_conn_free_all(endp);
140
141 /* Reset endpoint parameters and states */
142 talloc_free(endp->callid);
143 endp->callid = NULL;
144 talloc_free(endp->local_options.string);
145 endp->local_options.string = NULL;
146 talloc_free(endp->local_options.codec);
147 endp->local_options.codec = NULL;
Philipp Maier207ab512018-02-02 14:19:26 +0100148 endp->wildcarded_req = false;
Philipp Maieredc00f42018-01-24 11:58:56 +0100149}
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200150
151/* Check if the endpoint name contains the prefix (e.g. "rtpbridge/" or
152 * "ds/e1-") and write the epname without the prefix back to the memory
153 * pointed at by epname. (per trunk the prefix is the same for all endpoints,
154 * so no ambiguity is introduced) */
155static void chop_epname_prefix(char *epname, const struct mgcp_trunk *trunk)
156{
157 size_t prefix_len;
158 switch (trunk->trunk_type) {
159 case MGCP_TRUNK_VIRTUAL:
160 prefix_len = sizeof(MGCP_ENDPOINT_PREFIX_VIRTUAL_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 case MGCP_TRUNK_E1:
168 prefix_len = sizeof(MGCP_ENDPOINT_PREFIX_E1_TRUNK) - 1;
169 if (strncmp
170 (epname, MGCP_ENDPOINT_PREFIX_VIRTUAL_TRUNK,
171 prefix_len) == 0)
172 memmove(epname, epname + prefix_len,
173 strlen(epname) - prefix_len + 1);
174 return;
175 default:
176 OSMO_ASSERT(false);
177 }
178}
179
180/* Check if the endpoint name contains a suffix (e.g. "@mgw") and truncate
181 * epname by writing a '\0' char where the suffix starts. */
182static void chop_epname_suffix(char *epname, const struct mgcp_trunk *trunk)
183{
184 char *suffix_begin;
185
186 /* Endpoints on the virtual trunk may have a domain name that is
187 * followed after an @ character, this can be chopped off. All
188 * other supported trunk types do not have any suffixes that may
189 * be chopped off */
190 if (trunk->trunk_type == MGCP_TRUNK_VIRTUAL) {
191 suffix_begin = strchr(epname, '@');
192 if (!suffix_begin)
193 return;
194 *suffix_begin = '\0';
195 }
196}
197
198/* Convert all characters in epname to lowercase and strip trunk prefix and
199 * endpoint name suffix (domain name) from epname. The result is written to
200 * to the memory pointed at by epname_stripped. The expected size of the
201 * result is either equal or lower then the length of the input string
202 * (epname) */
203static void strip_epname(char *epname_stripped, const char *epname,
204 const struct mgcp_trunk *trunk)
205{
206 osmo_str_tolower_buf(epname_stripped, MGCP_ENDPOINT_MAXLEN, epname);
207 chop_epname_prefix(epname_stripped, trunk);
208 chop_epname_suffix(epname_stripped, trunk);
209}
210
211/* Go through the trunk and find a random free (no active calls) endpoint,
212 * this function is called when a wildcarded request is carried out, which
213 * means that it is up to the MGW to choose a random free endpoint. */
214static struct mgcp_endpoint *find_free_endpoint(const struct mgcp_trunk *trunk)
215{
216 struct mgcp_endpoint *endp;
217 unsigned int i;
218
219 for (i = 0; i < trunk->number_endpoints; i++) {
220 endp = trunk->endpoints[i];
Philipp Maier8d6a1932020-06-18 12:19:31 +0200221 /* A free endpoint must not serve a call already and it must
222 * be available. */
223 if (endp->callid == NULL && mgcp_endp_avail(endp))
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200224 return endp;
225 }
226
227 return NULL;
228}
229
230/* Find an endpoint specified by its name. If the endpoint can not be found,
231 * return NULL */
232static struct mgcp_endpoint *find_specific_endpoint(const char *epname,
233 const struct mgcp_trunk *trunk)
234{
235 char epname_stripped[MGCP_ENDPOINT_MAXLEN];
236 char epname_stripped_endp[MGCP_ENDPOINT_MAXLEN];
237 struct mgcp_endpoint *endp;
238 unsigned int i;
239
240 /* Strip irrelevant information from the endpoint name */
241 strip_epname(epname_stripped, epname, trunk);
242
243 for (i = 0; i < trunk->number_endpoints; i++) {
244 endp = trunk->endpoints[i];
245 strip_epname(epname_stripped_endp, endp->name, trunk);
246 if (strcmp(epname_stripped_endp, epname_stripped) == 0)
247 return endp;
248 }
249
250 return NULL;
251}
252
253/*! Find an endpoint by its name on a specified trunk.
254 * \param[out] cause pointer to store cause code, can be NULL.
255 * \param[in] epname endpoint name to lookup.
256 * \param[in] trunk where the endpoint is located.
257 * \returns endpoint or NULL if endpoint was not found. */
258struct mgcp_endpoint *mgcp_endp_by_name_trunk(int *cause, const char *epname,
259 const struct mgcp_trunk *trunk)
260{
261 struct mgcp_endpoint *endp;
262
263 if (cause)
264 *cause = 0;
265
266 /* At the moment we only support a primitive ('*'-only) method of
267 * wildcarded endpoint searches that picks the next free endpoint on
268 * a trunk. */
269 if (strstr(epname, "*")) {
270 endp = find_free_endpoint(trunk);
271 if (endp) {
272 LOGPENDP(endp, DLMGCP, LOGL_DEBUG,
273 "(trunk:%d) found free endpoint: %s\n",
274 trunk->trunk_nr, endp->name);
275 endp->wildcarded_req = true;
276 return endp;
277 }
278
279 LOGP(DLMGCP, LOGL_ERROR,
280 "(trunk:%d) Not able to find a free endpoint\n",
281 trunk->trunk_nr);
282 if (cause)
283 *cause = -403;
284 return NULL;
285 }
286
287 /* Find an endpoint by its name (if wildcarded request is not
288 * applicable) */
289 endp = find_specific_endpoint(epname, trunk);
290 if (endp) {
291 LOGPENDP(endp, DLMGCP, LOGL_DEBUG,
292 "(trunk:%d) found endpoint: %s\n",
293 trunk->trunk_nr, endp->name);
294 endp->wildcarded_req = false;
295 return endp;
296 }
297
298 LOGP(DLMGCP, LOGL_ERROR,
299 "(trunk:%d) Not able to find specified endpoint: %s\n",
300 trunk->trunk_nr, epname);
301 if (cause)
302 *cause = -500;
303
304 return NULL;
305}
306
307/* Check if the domain name, which is supplied with the endpoint name
308 * matches the configuration. */
309static int check_domain_name(const char *epname, struct mgcp_config *cfg)
310{
311 char *domain_to_check;
312
313 domain_to_check = strstr(epname, "@");
314 if (!domain_to_check) {
315 LOGP(DLMGCP, LOGL_ERROR, "missing domain name in endpoint name \"%s\", expecting \"%s\"\n",
316 epname, cfg->domain);
317 return -EINVAL;
318 }
319
320 /* Accept any domain if configured as "*" */
321 if (!strcmp(cfg->domain, "*"))
322 return 0;
323
324 if (strcmp(domain_to_check+1, cfg->domain) != 0) {
325 LOGP(DLMGCP, LOGL_ERROR, "wrong domain name in endpoint name \"%s\", expecting \"%s\"\n",
326 epname, cfg->domain);
327 return -EINVAL;
328 }
329
330 return 0;
331}
332
333/*! Find an endpoint by its name, search at all trunks.
334 * \param[out] cause, pointer to store cause code, can be NULL.
335 * \param[in] epname, must contain trunk prefix.
336 * \param[in] cfg, mgcp configuration (trunks).
337 * \returns endpoint or NULL if endpoint was not found. */
338struct mgcp_endpoint *mgcp_endp_by_name(int *cause, const char *epname,
339 struct mgcp_config *cfg)
340{
341 struct mgcp_trunk *trunk;
342 struct mgcp_endpoint *endp;
343 char epname_lc[MGCP_ENDPOINT_MAXLEN];
344
345 osmo_str_tolower_buf(epname_lc, sizeof(epname_lc), epname);
346 epname = epname_lc;
347
348 if (cause)
349 *cause = -500;
350
351 /* Identify the trunk where the endpoint is located */
352 trunk = mgcp_trunk_by_name(cfg, epname);
353 if (!trunk)
354 return NULL;
355
356 /* Virtual endpoints require a domain name (see RFC3435, section E.3) */
357 if (trunk->trunk_type == MGCP_TRUNK_VIRTUAL) {
358 if (check_domain_name(epname, cfg))
359 return NULL;
360 }
361
362 /* Identify the endpoint on the trunk */
363 endp = mgcp_endp_by_name_trunk(cause, epname, trunk);
364 if (!endp) {
365 return NULL;
366 }
367
368 if (cause)
369 *cause = 0;
370 return endp;
371}
Philipp Maier8d6a1932020-06-18 12:19:31 +0200372
373/* Get the E1 timeslot number from a given E1 endpoint name
374 * (e.g. ds/e1-0/s-30/su16-4), returns 0xff on error. */
375static uint8_t e1_ts_nr_from_epname(const char *epname)
376{
377 char buf[MGCP_ENDPOINT_MAXLEN + 1];
378 char *save_ptr = NULL;
379 char *buf_ptr = buf;
380 char *token;
381 unsigned long int res = 0;
382
383 strncpy(buf, epname, MGCP_ENDPOINT_MAXLEN);
384
385 while (1) {
386 token = strtok_r(buf_ptr, "/", &save_ptr);
387 buf_ptr = NULL;
388 if (!token)
389 break;
390 if (strncmp(token, "s-", 2) == 0) {
391 errno = 0;
392 res = strtoul(token + 2, NULL, 10);
393 if (errno == ERANGE || res > E1_TIMESLOTS)
394 return 0xff;
395 return (uint8_t) res;
396 }
397 }
398
399 return 0xff;
400}
401
402/* Get the E1 timeslot number from a given E1 endpoint name
403 * (e.g. ds/e1-0/s-30/su16-4), returns 0xff on error. */
404static uint8_t e1_rate_from_epname(const char *epname)
405{
406 char buf[MGCP_ENDPOINT_MAXLEN + 1];
407 char *save_ptr = NULL;
408 char *buf_ptr = buf;
409 char *token;
410 unsigned long int res = 0;
411 unsigned int i;
412
413 strncpy(buf, epname, MGCP_ENDPOINT_MAXLEN);
414
415 while (1) {
416 token = strtok_r(buf_ptr, "/", &save_ptr);
417 buf_ptr = NULL;
418 if (!token)
419 break;
420 if (strncmp(token, "su", 2) == 0) {
421 errno = 0;
422 res = strtoul(token + 2, NULL, 10);
423 if (errno == ERANGE || res > E1_RATE_MAX)
424 return 0xff;
425 /* Make sure the rate is a valid rate */
426 for (i = 0; i < sizeof(e1_rates); i++) {
427 if (res == e1_rates[i])
428 return (uint8_t) res;
429 }
430 return 0xff;
431 }
432 }
433
434 return 0xff;
435}
436
437/* Get the E1 bitstream offset from a given E1 endpoint name
438 * (e.g. ds/e1-0/s-30/su16-4), returns 0xff on error. */
439static uint8_t e1_offs_from_epname(const char *epname)
440{
441 char buf[MGCP_ENDPOINT_MAXLEN + 1];
442 char *save_ptr = NULL;
443 char *buf_ptr = buf;
444 char *token;
445 unsigned long int res = 0;
446
447 strncpy(buf, epname, MGCP_ENDPOINT_MAXLEN);
448
449 while (1) {
450 token = strtok_r(buf_ptr, "/", &save_ptr);
451 buf_ptr = NULL;
452 if (!token)
453 break;
454 if (strncmp(token, "su", 2) == 0) {
455 token = strstr(token, "-");
456 if (!token)
457 return 0xff;
458 token += 1;
459 errno = 0;
460 res = strtoul(token, NULL, 10);
461 if (errno == ERANGE || res > E1_OFFS_MAX)
462 return 0xff;
463 return (uint8_t) res;
464 }
465 }
466
467 return 0xff;
468}
469
470/* Get the E1 subslot number (internal) from a given E1 endpoint name
471 * (e.g. ds/e1-0/s-30/su16-4), returns 0xff on error. */
472static uint8_t e1_ss_nr_from_epname(const char *epname)
473{
474 uint8_t rate;
475 uint8_t offs;
476 unsigned int i;
477
478 rate = e1_rate_from_epname(epname);
479 offs = e1_offs_from_epname(epname);
480
481 osmo_static_assert(sizeof(e1_rates) == sizeof(e1_offsets), e1_rates_e1_offsets_size);
482
483 for (i = 0; i < sizeof(e1_rates); i++) {
484 if ((e1_rates[i] == rate) && (e1_offsets[i] == offs))
485 return i;
486 }
487
488 return 0xff;
489}
490
491/* Check if the selected E1 endpoint is avalable, which means that none of
492 * the overlapping endpoints are currently serving a call. (if the system
493 * is properly configured such a situation should never ocurr!) */
494static bool endp_avail_e1(struct mgcp_endpoint *endp)
495{
496 /* The following map shows the overlapping of the subslots and their
497 * respective rates. The numbers on the right running from top to bottom
498 * are the bit offsets in the whole 64k timeslot. The numbers inside the
499 * boxes symbolize the internal subslot number (array index) and the
500 * rate in the form: i:r where i is the subslot number and r the
501 * respective rate.
502 *
503 * +--------+--------+--------+--------+ 0
504 * | | | | 7:8k |
505 * | | + 3:16k +--------+ 1
506 * | | | | 8:8k |
507 * | | 1:32k +--------+--------+ 2
508 * | | | | 9:8k |
509 * | | + 4:16k +--------+ 3
510 * | | | | 10:8k |
511 * | 0:64k +--------+--------+--------+ 4
512 * | | | | 11:8k |
513 * | | + 5:16k +--------+ 5
514 * | | | | 12:8k |
515 * | | 2:32k +--------+--------+ 6
516 * | | | | 13:8k |
517 * | | + 6:16k +--------+ 7
518 * | | | | 14:8k |
519 * +--------+--------+--------+--------+ 8
520 *
521 * The following array contains tables with the subslot numbers that must be
522 * unused for each subslot. During this test we do not have to check the
523 * endpoint we need to verify, only the overlaps need to be checked. This is
524 * also the reason why the related subslot number is missing from each each
525 * line. */
526 const int8_t interlock_tab[15][16] =
527 { { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, -1 },
528 { 0, 3, 4, 7, 8, 9, 10, -1, -1, -1, -1, -1, -1, -1, -1 },
529 { 0, 5, 6, 11, 12, 13, 14, -1, -1, -1, -1, -1, -1, -1, -1 },
530 { 0, 1, 7, 8, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
531 { 0, 1, 9, 10, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
532 { 0, 2, 11, 12, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
533 { 0, 2, 13, 14, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
534 { 0, 1, 3, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
535 { 0, 1, 3, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
536 { 0, 1, 4, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
537 { 0, 1, 4, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
538 { 0, 2, 5, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
539 { 0, 2, 5, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
540 { 0, 2, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
541 { 0, 2, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 } };
542
543 const int8_t *interlock;
544 unsigned int i;
545 uint8_t ts_nr = 0;
546 uint8_t ss_nr = 0;
547 char *epname_check;
548 struct mgcp_endpoint *endp_check;
549 bool available = true;
550
551 /* This function must only be used with E1 type endpoints! */
552 OSMO_ASSERT(endp->trunk->trunk_type == MGCP_TRUNK_E1);
553
554 ts_nr = e1_ts_nr_from_epname(endp->name);
555 ss_nr = e1_ss_nr_from_epname(endp->name);
556 if (ts_nr == 0xff || ss_nr == 0xff) {
557 LOGPENDP(endp, DLMGCP, LOGL_ERROR,
558 "cannot check endpoint availability, endpoint name not parseable!\n");
559 return false;
560 }
561
562 interlock = interlock_tab[ss_nr];
563
564 for (i = 0; i < sizeof(interlock_tab[0]); i++) {
565 /* Detect row end */
566 if (interlock[i] == -1)
567 break;
568
569 /* Pick overlapping endpoint to check */
570 epname_check =
571 gen_e1_epname(endp, endp->trunk->trunk_nr, ts_nr,
572 interlock[i]);
573 endp_check = find_specific_endpoint(epname_check, endp->trunk);
574 if (!endp_check) {
575 LOGPENDP(endp, DLMGCP, LOGL_ERROR,
576 "cannot check endpoint availability, overlapping endpoint:%s not found!\n",
577 epname_check);
578 talloc_free(epname_check);
579 continue;
580 }
581 talloc_free(epname_check);
582
583 /* Check if overlapping endpoint currently serves another call
584 * (This is an exceptional situation, that should not occur
585 * in a properly configured environment!) */
586 if (endp_check->callid) {
587 LOGPENDP(endp, DLMGCP, LOGL_ERROR,
588 "endpoint unavailable - overlapping endpoint:%s already serves a call!\n",
589 endp_check->name);
590 available = false;
591 }
592 }
593
594 return available;
595}
596
597/*! check if an endpoint is available for any kind of operation.
598 * \param[in] endp endpoint to check.
599 * \returns true if endpoint is avalable, false it is blocked for any reason. */
600bool mgcp_endp_avail(struct mgcp_endpoint *endp)
601{
602 switch (endp->trunk->trunk_type) {
603 case MGCP_TRUNK_VIRTUAL:
604 /* There are no obstacles that may render a virtual trunk
605 * endpoint unusable, so virtual trunk endpoints are always
606 * available */
607 return true;
608 case MGCP_TRUNK_E1:
609 return endp_avail_e1(endp);
610 default:
611 OSMO_ASSERT(false);
612 }
613
614 return false;
615}