blob: 668d88bf99426812d2e774d5be0701677031141f [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 Maier889fe7f2020-07-06 17:44:12 +020030#include <osmocom/mgcp/mgcp_e1.h>
31
Philipp Maier8d6a1932020-06-18 12:19:31 +020032#define E1_TIMESLOTS 32
33#define E1_RATE_MAX 64
34#define E1_OFFS_MAX 8
35
Philipp Maier87bd9be2017-08-22 16:35:41 +020036/* Endpoint typeset definition */
37const struct mgcp_endpoint_typeset ep_typeset = {
38 /* Specify endpoint properties for RTP endpoint */
Philipp Maier0996a1e2020-06-10 15:27:14 +020039 .rtp = {
40 .max_conns = 2,
41 .dispatch_rtp_cb = mgcp_dispatch_rtp_bridge_cb,
42 .cleanup_cb = mgcp_cleanup_rtp_bridge_cb,
43 },
44 /* Specify endpoint properties for E1 endpoint */
45 .e1 = {
46 .max_conns = 1,
47 .dispatch_rtp_cb = mgcp_dispatch_e1_bridge_cb,
48 .cleanup_cb = mgcp_cleanup_e1_bridge_cb,
49 },
Philipp Maier87bd9be2017-08-22 16:35:41 +020050};
Philipp Maieredc00f42018-01-24 11:58:56 +010051
Philipp Maier7462b952020-06-10 14:50:34 +020052/* Generate virtual endpoint name from given parameters */
53static char *gen_virtual_epname(void *ctx, const char *domain,
54 unsigned int index)
55{
56 return talloc_asprintf(ctx, "%s%x@%s",
57 MGCP_ENDPOINT_PREFIX_VIRTUAL_TRUNK, index, domain);
58}
59
Philipp Maier98c09b32020-06-18 12:17:55 +020060/* Generate E1 endpoint name from given numeric parameters */
Philipp Maier0ffa3bd2020-06-30 16:17:03 +020061static char *gen_e1_epname(void *ctx, const char *domain, uint8_t trunk_nr,
62 uint8_t ts_nr, uint8_t ss_nr)
Philipp Maier98c09b32020-06-18 12:17:55 +020063{
Philipp Maier98c09b32020-06-18 12:17:55 +020064 unsigned int rate;
65 unsigned int offset;
66
Philipp Maier8d6a1932020-06-18 12:19:31 +020067 OSMO_ASSERT(ss_nr < sizeof(e1_rates));
Philipp Maier98c09b32020-06-18 12:17:55 +020068
Philipp Maier8d6a1932020-06-18 12:19:31 +020069 rate = e1_rates[ss_nr];
70 offset = e1_offsets[ss_nr];
Philipp Maier98c09b32020-06-18 12:17:55 +020071
Philipp Maier0ffa3bd2020-06-30 16:17:03 +020072 return talloc_asprintf(ctx, "%s%u/s-%u/su%u-%u@%s",
73 MGCP_ENDPOINT_PREFIX_E1_TRUNK, trunk_nr, ts_nr,
74 rate, offset, domain);
Philipp Maier98c09b32020-06-18 12:17:55 +020075}
76
Philipp Maierc66ab2c2020-06-02 20:55:34 +020077/*! allocate an endpoint and set default values.
78 * \param[in] trunk configuration.
Philipp Maier7462b952020-06-10 14:50:34 +020079 * \param[in] name endpoint index.
Philipp Maierc66ab2c2020-06-02 20:55:34 +020080 * \returns endpoint on success, NULL on failure. */
Philipp Maier7462b952020-06-10 14:50:34 +020081struct mgcp_endpoint *mgcp_endp_alloc(struct mgcp_trunk *trunk,
82 unsigned int index)
Philipp Maierc66ab2c2020-06-02 20:55:34 +020083{
84 struct mgcp_endpoint *endp;
85
86 endp = talloc_zero(trunk->endpoints, struct mgcp_endpoint);
87 if (!endp)
88 return NULL;
89
90 INIT_LLIST_HEAD(&endp->conns);
91 endp->cfg = trunk->cfg;
92 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
125 /* Reset endpoint parameters and states */
126 talloc_free(endp->callid);
127 endp->callid = NULL;
128 talloc_free(endp->local_options.string);
129 endp->local_options.string = NULL;
130 talloc_free(endp->local_options.codec);
131 endp->local_options.codec = NULL;
Philipp Maier207ab512018-02-02 14:19:26 +0100132 endp->wildcarded_req = false;
Philipp Maier889fe7f2020-07-06 17:44:12 +0200133
134 if (endp->trunk->trunk_type == MGCP_TRUNK_E1)
135 mgcp_e1_endp_release(endp);
Philipp Maieredc00f42018-01-24 11:58:56 +0100136}
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200137
138/* Check if the endpoint name contains the prefix (e.g. "rtpbridge/" or
139 * "ds/e1-") and write the epname without the prefix back to the memory
140 * pointed at by epname. (per trunk the prefix is the same for all endpoints,
141 * so no ambiguity is introduced) */
142static void chop_epname_prefix(char *epname, const struct mgcp_trunk *trunk)
143{
144 size_t prefix_len;
145 switch (trunk->trunk_type) {
146 case MGCP_TRUNK_VIRTUAL:
147 prefix_len = sizeof(MGCP_ENDPOINT_PREFIX_VIRTUAL_TRUNK) - 1;
148 if (strncmp
149 (epname, MGCP_ENDPOINT_PREFIX_VIRTUAL_TRUNK,
150 prefix_len) == 0)
151 memmove(epname, epname + prefix_len,
152 strlen(epname) - prefix_len + 1);
153 return;
154 case MGCP_TRUNK_E1:
155 prefix_len = sizeof(MGCP_ENDPOINT_PREFIX_E1_TRUNK) - 1;
156 if (strncmp
157 (epname, MGCP_ENDPOINT_PREFIX_VIRTUAL_TRUNK,
158 prefix_len) == 0)
159 memmove(epname, epname + prefix_len,
160 strlen(epname) - prefix_len + 1);
161 return;
162 default:
163 OSMO_ASSERT(false);
164 }
165}
166
167/* Check if the endpoint name contains a suffix (e.g. "@mgw") and truncate
168 * epname by writing a '\0' char where the suffix starts. */
169static void chop_epname_suffix(char *epname, const struct mgcp_trunk *trunk)
170{
171 char *suffix_begin;
172
173 /* Endpoints on the virtual trunk may have a domain name that is
174 * followed after an @ character, this can be chopped off. All
175 * other supported trunk types do not have any suffixes that may
176 * be chopped off */
177 if (trunk->trunk_type == MGCP_TRUNK_VIRTUAL) {
178 suffix_begin = strchr(epname, '@');
179 if (!suffix_begin)
180 return;
181 *suffix_begin = '\0';
182 }
183}
184
185/* Convert all characters in epname to lowercase and strip trunk prefix and
186 * endpoint name suffix (domain name) from epname. The result is written to
187 * to the memory pointed at by epname_stripped. The expected size of the
188 * result is either equal or lower then the length of the input string
189 * (epname) */
190static void strip_epname(char *epname_stripped, const char *epname,
191 const struct mgcp_trunk *trunk)
192{
193 osmo_str_tolower_buf(epname_stripped, MGCP_ENDPOINT_MAXLEN, epname);
194 chop_epname_prefix(epname_stripped, trunk);
195 chop_epname_suffix(epname_stripped, trunk);
196}
197
198/* Go through the trunk and find a random free (no active calls) endpoint,
199 * this function is called when a wildcarded request is carried out, which
200 * means that it is up to the MGW to choose a random free endpoint. */
201static struct mgcp_endpoint *find_free_endpoint(const struct mgcp_trunk *trunk)
202{
203 struct mgcp_endpoint *endp;
204 unsigned int i;
205
206 for (i = 0; i < trunk->number_endpoints; i++) {
207 endp = trunk->endpoints[i];
Philipp Maier8d6a1932020-06-18 12:19:31 +0200208 /* A free endpoint must not serve a call already and it must
209 * be available. */
210 if (endp->callid == NULL && mgcp_endp_avail(endp))
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200211 return endp;
212 }
213
214 return NULL;
215}
216
217/* Find an endpoint specified by its name. If the endpoint can not be found,
218 * return NULL */
219static struct mgcp_endpoint *find_specific_endpoint(const char *epname,
220 const struct mgcp_trunk *trunk)
221{
222 char epname_stripped[MGCP_ENDPOINT_MAXLEN];
223 char epname_stripped_endp[MGCP_ENDPOINT_MAXLEN];
224 struct mgcp_endpoint *endp;
225 unsigned int i;
226
227 /* Strip irrelevant information from the endpoint name */
228 strip_epname(epname_stripped, epname, trunk);
229
230 for (i = 0; i < trunk->number_endpoints; i++) {
231 endp = trunk->endpoints[i];
232 strip_epname(epname_stripped_endp, endp->name, trunk);
233 if (strcmp(epname_stripped_endp, epname_stripped) == 0)
234 return endp;
235 }
236
237 return NULL;
238}
239
240/*! Find an endpoint by its name on a specified trunk.
241 * \param[out] cause pointer to store cause code, can be NULL.
242 * \param[in] epname endpoint name to lookup.
243 * \param[in] trunk where the endpoint is located.
244 * \returns endpoint or NULL if endpoint was not found. */
245struct mgcp_endpoint *mgcp_endp_by_name_trunk(int *cause, const char *epname,
246 const struct mgcp_trunk *trunk)
247{
248 struct mgcp_endpoint *endp;
249
250 if (cause)
251 *cause = 0;
252
253 /* At the moment we only support a primitive ('*'-only) method of
254 * wildcarded endpoint searches that picks the next free endpoint on
255 * a trunk. */
256 if (strstr(epname, "*")) {
257 endp = find_free_endpoint(trunk);
258 if (endp) {
259 LOGPENDP(endp, DLMGCP, LOGL_DEBUG,
260 "(trunk:%d) found free endpoint: %s\n",
261 trunk->trunk_nr, endp->name);
262 endp->wildcarded_req = true;
263 return endp;
264 }
265
266 LOGP(DLMGCP, LOGL_ERROR,
267 "(trunk:%d) Not able to find a free endpoint\n",
268 trunk->trunk_nr);
269 if (cause)
270 *cause = -403;
271 return NULL;
272 }
273
274 /* Find an endpoint by its name (if wildcarded request is not
275 * applicable) */
276 endp = find_specific_endpoint(epname, trunk);
277 if (endp) {
278 LOGPENDP(endp, DLMGCP, LOGL_DEBUG,
279 "(trunk:%d) found endpoint: %s\n",
280 trunk->trunk_nr, endp->name);
281 endp->wildcarded_req = false;
282 return endp;
283 }
284
285 LOGP(DLMGCP, LOGL_ERROR,
286 "(trunk:%d) Not able to find specified endpoint: %s\n",
287 trunk->trunk_nr, epname);
288 if (cause)
289 *cause = -500;
290
291 return NULL;
292}
293
294/* Check if the domain name, which is supplied with the endpoint name
295 * matches the configuration. */
296static int check_domain_name(const char *epname, struct mgcp_config *cfg)
297{
298 char *domain_to_check;
299
300 domain_to_check = strstr(epname, "@");
301 if (!domain_to_check) {
302 LOGP(DLMGCP, LOGL_ERROR, "missing domain name in endpoint name \"%s\", expecting \"%s\"\n",
303 epname, cfg->domain);
304 return -EINVAL;
305 }
306
307 /* Accept any domain if configured as "*" */
308 if (!strcmp(cfg->domain, "*"))
309 return 0;
310
311 if (strcmp(domain_to_check+1, cfg->domain) != 0) {
312 LOGP(DLMGCP, LOGL_ERROR, "wrong domain name in endpoint name \"%s\", expecting \"%s\"\n",
313 epname, cfg->domain);
314 return -EINVAL;
315 }
316
317 return 0;
318}
319
320/*! Find an endpoint by its name, search at all trunks.
321 * \param[out] cause, pointer to store cause code, can be NULL.
322 * \param[in] epname, must contain trunk prefix.
323 * \param[in] cfg, mgcp configuration (trunks).
324 * \returns endpoint or NULL if endpoint was not found. */
325struct mgcp_endpoint *mgcp_endp_by_name(int *cause, const char *epname,
326 struct mgcp_config *cfg)
327{
328 struct mgcp_trunk *trunk;
329 struct mgcp_endpoint *endp;
330 char epname_lc[MGCP_ENDPOINT_MAXLEN];
331
332 osmo_str_tolower_buf(epname_lc, sizeof(epname_lc), epname);
333 epname = epname_lc;
334
335 if (cause)
336 *cause = -500;
337
338 /* Identify the trunk where the endpoint is located */
339 trunk = mgcp_trunk_by_name(cfg, epname);
340 if (!trunk)
341 return NULL;
342
Philipp Maier0ffa3bd2020-06-30 16:17:03 +0200343 /* All endpoint names require a domain as suffix */
344 if (check_domain_name(epname, cfg))
345 return NULL;
Philipp Maierc66ab2c2020-06-02 20:55:34 +0200346
347 /* Identify the endpoint on the trunk */
348 endp = mgcp_endp_by_name_trunk(cause, epname, trunk);
349 if (!endp) {
350 return NULL;
351 }
352
353 if (cause)
354 *cause = 0;
355 return endp;
356}
Philipp Maier8d6a1932020-06-18 12:19:31 +0200357
358/* Get the E1 timeslot number from a given E1 endpoint name
359 * (e.g. ds/e1-0/s-30/su16-4), returns 0xff on error. */
360static uint8_t e1_ts_nr_from_epname(const char *epname)
361{
362 char buf[MGCP_ENDPOINT_MAXLEN + 1];
363 char *save_ptr = NULL;
364 char *buf_ptr = buf;
365 char *token;
366 unsigned long int res = 0;
367
368 strncpy(buf, epname, MGCP_ENDPOINT_MAXLEN);
369
370 while (1) {
371 token = strtok_r(buf_ptr, "/", &save_ptr);
372 buf_ptr = NULL;
373 if (!token)
374 break;
375 if (strncmp(token, "s-", 2) == 0) {
376 errno = 0;
377 res = strtoul(token + 2, NULL, 10);
378 if (errno == ERANGE || res > E1_TIMESLOTS)
379 return 0xff;
380 return (uint8_t) res;
381 }
382 }
383
384 return 0xff;
385}
386
387/* Get the E1 timeslot number from a given E1 endpoint name
388 * (e.g. ds/e1-0/s-30/su16-4), returns 0xff on error. */
389static uint8_t e1_rate_from_epname(const char *epname)
390{
391 char buf[MGCP_ENDPOINT_MAXLEN + 1];
392 char *save_ptr = NULL;
393 char *buf_ptr = buf;
394 char *token;
395 unsigned long int res = 0;
396 unsigned int i;
397
398 strncpy(buf, epname, MGCP_ENDPOINT_MAXLEN);
399
400 while (1) {
401 token = strtok_r(buf_ptr, "/", &save_ptr);
402 buf_ptr = NULL;
403 if (!token)
404 break;
405 if (strncmp(token, "su", 2) == 0) {
406 errno = 0;
407 res = strtoul(token + 2, NULL, 10);
408 if (errno == ERANGE || res > E1_RATE_MAX)
409 return 0xff;
410 /* Make sure the rate is a valid rate */
411 for (i = 0; i < sizeof(e1_rates); i++) {
412 if (res == e1_rates[i])
413 return (uint8_t) res;
414 }
415 return 0xff;
416 }
417 }
418
419 return 0xff;
420}
421
422/* Get the E1 bitstream offset from a given E1 endpoint name
423 * (e.g. ds/e1-0/s-30/su16-4), returns 0xff on error. */
424static uint8_t e1_offs_from_epname(const char *epname)
425{
426 char buf[MGCP_ENDPOINT_MAXLEN + 1];
427 char *save_ptr = NULL;
428 char *buf_ptr = buf;
429 char *token;
430 unsigned long int res = 0;
431
432 strncpy(buf, epname, MGCP_ENDPOINT_MAXLEN);
433
434 while (1) {
435 token = strtok_r(buf_ptr, "/", &save_ptr);
436 buf_ptr = NULL;
437 if (!token)
438 break;
439 if (strncmp(token, "su", 2) == 0) {
440 token = strstr(token, "-");
441 if (!token)
442 return 0xff;
443 token += 1;
444 errno = 0;
445 res = strtoul(token, NULL, 10);
446 if (errno == ERANGE || res > E1_OFFS_MAX)
447 return 0xff;
448 return (uint8_t) res;
449 }
450 }
451
452 return 0xff;
453}
454
455/* Get the E1 subslot number (internal) from a given E1 endpoint name
456 * (e.g. ds/e1-0/s-30/su16-4), returns 0xff on error. */
457static uint8_t e1_ss_nr_from_epname(const char *epname)
458{
459 uint8_t rate;
460 uint8_t offs;
461 unsigned int i;
462
463 rate = e1_rate_from_epname(epname);
464 offs = e1_offs_from_epname(epname);
465
466 osmo_static_assert(sizeof(e1_rates) == sizeof(e1_offsets), e1_rates_e1_offsets_size);
467
468 for (i = 0; i < sizeof(e1_rates); i++) {
469 if ((e1_rates[i] == rate) && (e1_offsets[i] == offs))
470 return i;
471 }
472
473 return 0xff;
474}
475
476/* Check if the selected E1 endpoint is avalable, which means that none of
477 * the overlapping endpoints are currently serving a call. (if the system
478 * is properly configured such a situation should never ocurr!) */
479static bool endp_avail_e1(struct mgcp_endpoint *endp)
480{
481 /* The following map shows the overlapping of the subslots and their
482 * respective rates. The numbers on the right running from top to bottom
483 * are the bit offsets in the whole 64k timeslot. The numbers inside the
484 * boxes symbolize the internal subslot number (array index) and the
485 * rate in the form: i:r where i is the subslot number and r the
486 * respective rate.
487 *
488 * +--------+--------+--------+--------+ 0
489 * | | | | 7:8k |
490 * | | + 3:16k +--------+ 1
491 * | | | | 8:8k |
492 * | | 1:32k +--------+--------+ 2
493 * | | | | 9:8k |
494 * | | + 4:16k +--------+ 3
495 * | | | | 10:8k |
496 * | 0:64k +--------+--------+--------+ 4
497 * | | | | 11:8k |
498 * | | + 5:16k +--------+ 5
499 * | | | | 12:8k |
500 * | | 2:32k +--------+--------+ 6
501 * | | | | 13:8k |
502 * | | + 6:16k +--------+ 7
503 * | | | | 14:8k |
504 * +--------+--------+--------+--------+ 8
505 *
506 * The following array contains tables with the subslot numbers that must be
507 * unused for each subslot. During this test we do not have to check the
508 * endpoint we need to verify, only the overlaps need to be checked. This is
509 * also the reason why the related subslot number is missing from each each
510 * line. */
Philipp Maierfe67e092020-07-07 21:21:45 +0200511 const int8_t interlock_tab[MGCP_ENDP_E1_SUBSLOTS][15] = {
512 { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, -1 },
Philipp Maier8d6a1932020-06-18 12:19:31 +0200513 { 0, 3, 4, 7, 8, 9, 10, -1, -1, -1, -1, -1, -1, -1, -1 },
514 { 0, 5, 6, 11, 12, 13, 14, -1, -1, -1, -1, -1, -1, -1, -1 },
515 { 0, 1, 7, 8, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
516 { 0, 1, 9, 10, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
517 { 0, 2, 11, 12, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
518 { 0, 2, 13, 14, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
519 { 0, 1, 3, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
520 { 0, 1, 3, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
521 { 0, 1, 4, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
522 { 0, 1, 4, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
523 { 0, 2, 5, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
524 { 0, 2, 5, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
525 { 0, 2, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
526 { 0, 2, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 } };
527
528 const int8_t *interlock;
529 unsigned int i;
530 uint8_t ts_nr = 0;
531 uint8_t ss_nr = 0;
532 char *epname_check;
533 struct mgcp_endpoint *endp_check;
534 bool available = true;
535
536 /* This function must only be used with E1 type endpoints! */
537 OSMO_ASSERT(endp->trunk->trunk_type == MGCP_TRUNK_E1);
538
539 ts_nr = e1_ts_nr_from_epname(endp->name);
540 ss_nr = e1_ss_nr_from_epname(endp->name);
541 if (ts_nr == 0xff || ss_nr == 0xff) {
542 LOGPENDP(endp, DLMGCP, LOGL_ERROR,
543 "cannot check endpoint availability, endpoint name not parseable!\n");
544 return false;
545 }
546
547 interlock = interlock_tab[ss_nr];
548
549 for (i = 0; i < sizeof(interlock_tab[0]); i++) {
550 /* Detect row end */
551 if (interlock[i] == -1)
552 break;
553
554 /* Pick overlapping endpoint to check */
Philipp Maier0ffa3bd2020-06-30 16:17:03 +0200555 epname_check = gen_e1_epname(endp, endp->trunk->cfg->domain,
556 endp->trunk->trunk_nr, ts_nr,
557 interlock[i]);
Philipp Maier8d6a1932020-06-18 12:19:31 +0200558 endp_check = find_specific_endpoint(epname_check, endp->trunk);
559 if (!endp_check) {
560 LOGPENDP(endp, DLMGCP, LOGL_ERROR,
561 "cannot check endpoint availability, overlapping endpoint:%s not found!\n",
562 epname_check);
563 talloc_free(epname_check);
564 continue;
565 }
566 talloc_free(epname_check);
567
568 /* Check if overlapping endpoint currently serves another call
569 * (This is an exceptional situation, that should not occur
570 * in a properly configured environment!) */
571 if (endp_check->callid) {
572 LOGPENDP(endp, DLMGCP, LOGL_ERROR,
573 "endpoint unavailable - overlapping endpoint:%s already serves a call!\n",
574 endp_check->name);
575 available = false;
576 }
577 }
578
579 return available;
580}
581
582/*! check if an endpoint is available for any kind of operation.
583 * \param[in] endp endpoint to check.
584 * \returns true if endpoint is avalable, false it is blocked for any reason. */
585bool mgcp_endp_avail(struct mgcp_endpoint *endp)
586{
587 switch (endp->trunk->trunk_type) {
588 case MGCP_TRUNK_VIRTUAL:
589 /* There are no obstacles that may render a virtual trunk
590 * endpoint unusable, so virtual trunk endpoints are always
591 * available */
592 return true;
593 case MGCP_TRUNK_E1:
594 return endp_avail_e1(endp);
595 default:
596 OSMO_ASSERT(false);
597 }
598
599 return false;
600}
Philipp Maier889fe7f2020-07-06 17:44:12 +0200601
602/*! claim endpoint, sets callid and activates endpoint, should be called at the
603 * beginning of the CRCX procedure when it is clear that a new call should be
604 * created.
605 * \param[in] endp endpoint to claim.
606 * \param[in] callid that is assingned to this endpoint. */
607int mgcp_endp_claim(struct mgcp_endpoint *endp, const char *callid)
608{
609 int rc = 0;
610 uint8_t ts;
611 uint8_t ss;
612 uint8_t offs;
613
614 /* TODO: Make this function more intelligent, it should run the
615 * call id checks we currently have in protocol.c directly here. */
616
617 /* Set the callid, creation of another connection will only be possible
618 * when the callid matches up. (Connections are distinguished by their
619 * connection ids) */
620 endp->callid = talloc_strdup(endp, callid);
621 OSMO_ASSERT(endp->callid);
622
623 /* Allocate resources */
624 switch (endp->trunk->trunk_type) {
625 case MGCP_TRUNK_VIRTUAL:
626 /* No additional initaliziation required here, virtual
627 * endpoints will open/close network sockets themselves
628 * on demand. */
629 break;
630 case MGCP_TRUNK_E1:
631 ts = e1_ts_nr_from_epname(endp->name);
632 ss = e1_ss_nr_from_epname(endp->name);
633 offs = e1_offs_from_epname(endp->name);
634 OSMO_ASSERT(ts != 0xFF);
635 OSMO_ASSERT(ts != 0);
636 OSMO_ASSERT(ss != 0xFF);
637 OSMO_ASSERT(offs != 0xFF);
638 rc = mgcp_e1_endp_equip(endp, ts, ss, offs);
639 break;
640 default:
641 OSMO_ASSERT(false);
642 }
643
644 /* Make sure the endpoint is released when claiming the endpoint
645 * failes. */
646 if (rc < 0)
647 mgcp_endp_release(endp);
648
649 return rc;
650}
651
652/*! update endpoint, updates internal endpoint specific data, should be
653 * after when MDCX or CRCX has been executed successuflly.
654 * \param[in] endp endpoint to update. */
655void mgcp_endp_update(struct mgcp_endpoint *endp)
656{
657 /* Allocate resources */
658 switch (endp->trunk->trunk_type) {
659 case MGCP_TRUNK_VIRTUAL:
660 /* No updating initaliziation required for virtual endpoints. */
661 break;
662 case MGCP_TRUNK_E1:
663 mgcp_e1_endp_update(endp);
664 break;
665 default:
666 OSMO_ASSERT(false);
667 }
668}