Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1 | /* Endpoint types */ |
| 2 | |
| 3 | /* |
Philipp Maier | c66ab2c | 2020-06-02 20:55:34 +0200 | [diff] [blame] | 4 | * (C) 2017-2020 by sysmocom s.f.m.c. GmbH <info@sysmocom.de> |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 5 | * 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 Maier | 993ea6b | 2020-08-04 18:26:50 +0200 | [diff] [blame] | 24 | #include <osmocom/mgcp/mgcp.h> |
| 25 | #include <osmocom/mgcp/mgcp_protocol.h> |
| 26 | #include <osmocom/mgcp/mgcp_conn.h> |
Philipp Maier | 37d11c8 | 2018-02-01 14:38:12 +0100 | [diff] [blame] | 27 | #include <osmocom/mgcp/mgcp_endp.h> |
Philipp Maier | c66ab2c | 2020-06-02 20:55:34 +0200 | [diff] [blame] | 28 | #include <osmocom/mgcp/mgcp_trunk.h> |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 29 | |
Philipp Maier | 99d4d36 | 2020-09-07 11:58:09 +0200 | [diff] [blame] | 30 | #include <osmocom/abis/e1_input.h> |
Philipp Maier | 889fe7f | 2020-07-06 17:44:12 +0200 | [diff] [blame] | 31 | #include <osmocom/mgcp/mgcp_e1.h> |
Philipp Maier | 124a3e0 | 2021-07-26 11:17:15 +0200 | [diff] [blame] | 32 | #include <osmocom/core/stat_item.h> |
Philipp Maier | 889fe7f | 2020-07-06 17:44:12 +0200 | [diff] [blame] | 33 | |
Philipp Maier | 8d6a193 | 2020-06-18 12:19:31 +0200 | [diff] [blame] | 34 | #define E1_RATE_MAX 64 |
| 35 | #define E1_OFFS_MAX 8 |
| 36 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 37 | /* Endpoint typeset definition */ |
| 38 | const struct mgcp_endpoint_typeset ep_typeset = { |
| 39 | /* Specify endpoint properties for RTP endpoint */ |
Philipp Maier | 0996a1e | 2020-06-10 15:27:14 +0200 | [diff] [blame] | 40 | .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 Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 51 | }; |
Philipp Maier | edc00f4 | 2018-01-24 11:58:56 +0100 | [diff] [blame] | 52 | |
Philipp Maier | 7462b95 | 2020-06-10 14:50:34 +0200 | [diff] [blame] | 53 | /* Generate virtual endpoint name from given parameters */ |
| 54 | static 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 Maier | 98c09b3 | 2020-06-18 12:17:55 +0200 | [diff] [blame] | 61 | /* Generate E1 endpoint name from given numeric parameters */ |
Philipp Maier | d70eef6 | 2021-07-19 13:53:28 +0200 | [diff] [blame] | 62 | static char *gen_e1_epname(void *ctx, const char *domain, unsigned int trunk_nr, |
Philipp Maier | 0ffa3bd | 2020-06-30 16:17:03 +0200 | [diff] [blame] | 63 | uint8_t ts_nr, uint8_t ss_nr) |
Philipp Maier | 98c09b3 | 2020-06-18 12:17:55 +0200 | [diff] [blame] | 64 | { |
Philipp Maier | 98c09b3 | 2020-06-18 12:17:55 +0200 | [diff] [blame] | 65 | unsigned int rate; |
| 66 | unsigned int offset; |
| 67 | |
Philipp Maier | 8d6a193 | 2020-06-18 12:19:31 +0200 | [diff] [blame] | 68 | OSMO_ASSERT(ss_nr < sizeof(e1_rates)); |
Philipp Maier | 98c09b3 | 2020-06-18 12:17:55 +0200 | [diff] [blame] | 69 | |
Philipp Maier | 8d6a193 | 2020-06-18 12:19:31 +0200 | [diff] [blame] | 70 | rate = e1_rates[ss_nr]; |
| 71 | offset = e1_offsets[ss_nr]; |
Philipp Maier | 98c09b3 | 2020-06-18 12:17:55 +0200 | [diff] [blame] | 72 | |
Philipp Maier | 0ffa3bd | 2020-06-30 16:17:03 +0200 | [diff] [blame] | 73 | 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 Maier | 98c09b3 | 2020-06-18 12:17:55 +0200 | [diff] [blame] | 76 | } |
| 77 | |
Philipp Maier | c66ab2c | 2020-06-02 20:55:34 +0200 | [diff] [blame] | 78 | /*! allocate an endpoint and set default values. |
| 79 | * \param[in] trunk configuration. |
Philipp Maier | 7462b95 | 2020-06-10 14:50:34 +0200 | [diff] [blame] | 80 | * \param[in] name endpoint index. |
Philipp Maier | c66ab2c | 2020-06-02 20:55:34 +0200 | [diff] [blame] | 81 | * \returns endpoint on success, NULL on failure. */ |
Philipp Maier | 7462b95 | 2020-06-10 14:50:34 +0200 | [diff] [blame] | 82 | struct mgcp_endpoint *mgcp_endp_alloc(struct mgcp_trunk *trunk, |
| 83 | unsigned int index) |
Philipp Maier | c66ab2c | 2020-06-02 20:55:34 +0200 | [diff] [blame] | 84 | { |
| 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 Maier | c66ab2c | 2020-06-02 20:55:34 +0200 | [diff] [blame] | 92 | endp->trunk = trunk; |
Philipp Maier | c66ab2c | 2020-06-02 20:55:34 +0200 | [diff] [blame] | 93 | |
| 94 | switch (trunk->trunk_type) { |
| 95 | case MGCP_TRUNK_VIRTUAL: |
| 96 | endp->type = &ep_typeset.rtp; |
Philipp Maier | 7462b95 | 2020-06-10 14:50:34 +0200 | [diff] [blame] | 97 | endp->name = gen_virtual_epname(endp, trunk->cfg->domain, index); |
Philipp Maier | c66ab2c | 2020-06-02 20:55:34 +0200 | [diff] [blame] | 98 | break; |
| 99 | case MGCP_TRUNK_E1: |
Philipp Maier | 889fe7f | 2020-07-06 17:44:12 +0200 | [diff] [blame] | 100 | endp->type = &ep_typeset.e1; |
Philipp Maier | 0ffa3bd | 2020-06-30 16:17:03 +0200 | [diff] [blame] | 101 | endp->name = gen_e1_epname(endp, trunk->cfg->domain, |
| 102 | trunk->trunk_nr, |
Philipp Maier | fbcf399 | 2020-07-02 23:08:37 +0200 | [diff] [blame] | 103 | index / MGCP_ENDP_E1_SUBSLOTS, index % MGCP_ENDP_E1_SUBSLOTS); |
Philipp Maier | c66ab2c | 2020-06-02 20:55:34 +0200 | [diff] [blame] | 104 | 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 Maier | c66ab2c | 2020-06-02 20:55:34 +0200 | [diff] [blame] | 113 | /* 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) */ |
| 117 | static 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. */ |
| 144 | static 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 | |
Eric | 25ecc91 | 2021-09-06 03:43:50 +0200 | [diff] [blame] | 160 | /*! Convert all characters in epname to lowercase and strip trunk prefix and |
Philipp Maier | c66ab2c | 2020-06-02 20:55:34 +0200 | [diff] [blame] | 161 | * 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 |
Eric | 25ecc91 | 2021-09-06 03:43:50 +0200 | [diff] [blame] | 164 | * (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. */ |
| 168 | void mgcp_endp_strip_name(char *epname_stripped, const char *epname, |
Philipp Maier | c66ab2c | 2020-06-02 20:55:34 +0200 | [diff] [blame] | 169 | 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. */ |
| 179 | static 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 Maier | 8d6a193 | 2020-06-18 12:19:31 +0200 | [diff] [blame] | 186 | /* 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 Maier | c66ab2c | 2020-06-02 20:55:34 +0200 | [diff] [blame] | 189 | return endp; |
| 190 | } |
| 191 | |
| 192 | return NULL; |
| 193 | } |
| 194 | |
Eric | 25ecc91 | 2021-09-06 03:43:50 +0200 | [diff] [blame] | 195 | /*! Find an endpoint of a trunk specified by its name. |
Eric | aac84ed | 2021-11-09 16:22:01 +0100 | [diff] [blame] | 196 | * \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. */ |
Eric | 25ecc91 | 2021-09-06 03:43:50 +0200 | [diff] [blame] | 199 | struct mgcp_endpoint *mgcp_endp_find_specific(const char *epname, |
Philipp Maier | c66ab2c | 2020-06-02 20:55:34 +0200 | [diff] [blame] | 200 | 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 */ |
Eric | 25ecc91 | 2021-09-06 03:43:50 +0200 | [diff] [blame] | 208 | mgcp_endp_strip_name(epname_stripped, epname, trunk); |
Philipp Maier | c66ab2c | 2020-06-02 20:55:34 +0200 | [diff] [blame] | 209 | |
| 210 | for (i = 0; i < trunk->number_endpoints; i++) { |
| 211 | endp = trunk->endpoints[i]; |
Eric | 25ecc91 | 2021-09-06 03:43:50 +0200 | [diff] [blame] | 212 | mgcp_endp_strip_name(epname_stripped_endp, endp->name, trunk); |
Philipp Maier | c66ab2c | 2020-06-02 20:55:34 +0200 | [diff] [blame] | 213 | if (strcmp(epname_stripped_endp, epname_stripped) == 0) |
| 214 | return endp; |
| 215 | } |
| 216 | |
| 217 | return NULL; |
| 218 | } |
| 219 | |
Philipp Maier | d64c041 | 2021-07-14 11:53:49 +0200 | [diff] [blame] | 220 | /*! 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. */ |
| 224 | bool mgcp_endp_is_wildcarded(const char *epname) |
| 225 | { |
| 226 | if (strstr(epname, "*")) |
| 227 | return true; |
| 228 | |
| 229 | return false; |
| 230 | } |
| 231 | |
Philipp Maier | c66ab2c | 2020-06-02 20:55:34 +0200 | [diff] [blame] | 232 | /*! 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. */ |
| 237 | struct 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 Maier | d64c041 | 2021-07-14 11:53:49 +0200 | [diff] [blame] | 248 | if (mgcp_endp_is_wildcarded(epname)) { |
Philipp Maier | c66ab2c | 2020-06-02 20:55:34 +0200 | [diff] [blame] | 249 | 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 Maier | c66ab2c | 2020-06-02 20:55:34 +0200 | [diff] [blame] | 254 | 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) */ |
Eric | 25ecc91 | 2021-09-06 03:43:50 +0200 | [diff] [blame] | 267 | endp = mgcp_endp_find_specific(epname, trunk); |
Philipp Maier | c66ab2c | 2020-06-02 20:55:34 +0200 | [diff] [blame] | 268 | if (endp) { |
| 269 | LOGPENDP(endp, DLMGCP, LOGL_DEBUG, |
| 270 | "(trunk:%d) found endpoint: %s\n", |
| 271 | trunk->trunk_nr, endp->name); |
Philipp Maier | c66ab2c | 2020-06-02 20:55:34 +0200 | [diff] [blame] | 272 | 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 Maier | c66ab2c | 2020-06-02 20:55:34 +0200 | [diff] [blame] | 284 | /*! 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. */ |
| 289 | struct 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 Maier | c66ab2c | 2020-06-02 20:55:34 +0200 | [diff] [blame] | 307 | /* 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 Maier | 8d6a193 | 2020-06-18 12:19:31 +0200 | [diff] [blame] | 317 | |
| 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. */ |
| 320 | static 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 Maier | 99d4d36 | 2020-09-07 11:58:09 +0200 | [diff] [blame] | 338 | if (errno == ERANGE || res > NUM_E1_TS) |
Philipp Maier | 8d6a193 | 2020-06-18 12:19:31 +0200 | [diff] [blame] | 339 | 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. */ |
| 349 | static 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. */ |
| 384 | static 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. */ |
| 417 | static 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!) */ |
| 439 | static 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 Maier | fe67e09 | 2020-07-07 21:21:45 +0200 | [diff] [blame] | 471 | 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 Maier | 8d6a193 | 2020-06-18 12:19:31 +0200 | [diff] [blame] | 473 | { 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 Maier | 0ffa3bd | 2020-06-30 16:17:03 +0200 | [diff] [blame] | 515 | epname_check = gen_e1_epname(endp, endp->trunk->cfg->domain, |
| 516 | endp->trunk->trunk_nr, ts_nr, |
| 517 | interlock[i]); |
Eric | 25ecc91 | 2021-09-06 03:43:50 +0200 | [diff] [blame] | 518 | endp_check = mgcp_endp_find_specific(epname_check, endp->trunk); |
Philipp Maier | 8d6a193 | 2020-06-18 12:19:31 +0200 | [diff] [blame] | 519 | 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. */ |
| 545 | bool 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 Maier | 889fe7f | 2020-07-06 17:44:12 +0200 | [diff] [blame] | 561 | |
| 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. */ |
| 567 | int 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 Maier | 124a3e0 | 2021-07-26 11:17:15 +0200 | [diff] [blame] | 582 | osmo_stat_item_inc(osmo_stat_item_group_get_item(endp->trunk->stats.common, |
| 583 | TRUNK_STAT_ENDPOINTS_USED), 1); |
Philipp Maier | 889fe7f | 2020-07-06 17:44:12 +0200 | [diff] [blame] | 584 | |
| 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 Pedrol | 30e0135 | 2020-09-21 12:29:41 +0200 | [diff] [blame] | 606 | /* Make sure the endpoint is released when claiming the endpoint fails. */ |
Philipp Maier | 889fe7f | 2020-07-06 17:44:12 +0200 | [diff] [blame] | 607 | 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. */ |
| 616 | void 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 Pedrol | 6d0a59a | 2020-09-08 16:50:22 +0200 | [diff] [blame] | 630 | |
| 631 | void mgcp_endp_add_conn(struct mgcp_endpoint *endp, struct mgcp_conn *conn) |
| 632 | { |
| 633 | llist_add(&conn->entry, &endp->conns); |
| 634 | } |
| 635 | |
| 636 | void 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 Maier | a8f1328 | 2023-02-09 13:34:28 +0100 | [diff] [blame] | 647 | |
| 648 | /*! release endpoint, all open connections are closed. |
| 649 | * \param[in] endp endpoint to release */ |
| 650 | void 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 | |
Philipp Maier | 2a9ba66 | 2023-02-09 13:39:31 +0100 | [diff] [blame] | 674 | if (endp->trunk->trunk_type == MGCP_TRUNK_E1) { |
| 675 | uint8_t ts = e1_ts_nr_from_epname(endp->name); |
| 676 | mgcp_e1_endp_release(endp, ts); |
| 677 | } |
Philipp Maier | a8f1328 | 2023-02-09 13:34:28 +0100 | [diff] [blame] | 678 | } |
| 679 | |