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 | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 24 | #include <osmocom/mgcp/mgcp_internal.h> |
Philipp Maier | 37d11c8 | 2018-02-01 14:38:12 +0100 | [diff] [blame] | 25 | #include <osmocom/mgcp/mgcp_endp.h> |
Philipp Maier | c66ab2c | 2020-06-02 20:55:34 +0200 | [diff] [blame] | 26 | #include <osmocom/mgcp/mgcp_trunk.h> |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 27 | |
| 28 | /* Endpoint typeset definition */ |
| 29 | const struct mgcp_endpoint_typeset ep_typeset = { |
| 30 | /* Specify endpoint properties for RTP endpoint */ |
| 31 | .rtp.max_conns = 2, |
Philipp Maier | df5d219 | 2018-01-24 11:39:32 +0100 | [diff] [blame] | 32 | .rtp.dispatch_rtp_cb = mgcp_dispatch_rtp_bridge_cb, |
| 33 | .rtp.cleanup_cb = mgcp_cleanup_rtp_bridge_cb |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 34 | }; |
Philipp Maier | edc00f4 | 2018-01-24 11:58:56 +0100 | [diff] [blame] | 35 | |
Philipp Maier | 7462b95 | 2020-06-10 14:50:34 +0200 | [diff] [blame^] | 36 | /* Generate virtual endpoint name from given parameters */ |
| 37 | static char *gen_virtual_epname(void *ctx, const char *domain, |
| 38 | unsigned int index) |
| 39 | { |
| 40 | return talloc_asprintf(ctx, "%s%x@%s", |
| 41 | MGCP_ENDPOINT_PREFIX_VIRTUAL_TRUNK, index, domain); |
| 42 | } |
| 43 | |
Philipp Maier | c66ab2c | 2020-06-02 20:55:34 +0200 | [diff] [blame] | 44 | /*! allocate an endpoint and set default values. |
| 45 | * \param[in] trunk configuration. |
Philipp Maier | 7462b95 | 2020-06-10 14:50:34 +0200 | [diff] [blame^] | 46 | * \param[in] name endpoint index. |
Philipp Maier | c66ab2c | 2020-06-02 20:55:34 +0200 | [diff] [blame] | 47 | * \returns endpoint on success, NULL on failure. */ |
Philipp Maier | 7462b95 | 2020-06-10 14:50:34 +0200 | [diff] [blame^] | 48 | struct mgcp_endpoint *mgcp_endp_alloc(struct mgcp_trunk *trunk, |
| 49 | unsigned int index) |
Philipp Maier | c66ab2c | 2020-06-02 20:55:34 +0200 | [diff] [blame] | 50 | { |
| 51 | struct mgcp_endpoint *endp; |
| 52 | |
| 53 | endp = talloc_zero(trunk->endpoints, struct mgcp_endpoint); |
| 54 | if (!endp) |
| 55 | return NULL; |
| 56 | |
| 57 | INIT_LLIST_HEAD(&endp->conns); |
| 58 | endp->cfg = trunk->cfg; |
| 59 | endp->trunk = trunk; |
Philipp Maier | c66ab2c | 2020-06-02 20:55:34 +0200 | [diff] [blame] | 60 | |
| 61 | switch (trunk->trunk_type) { |
| 62 | case MGCP_TRUNK_VIRTUAL: |
| 63 | endp->type = &ep_typeset.rtp; |
Philipp Maier | 7462b95 | 2020-06-10 14:50:34 +0200 | [diff] [blame^] | 64 | endp->name = gen_virtual_epname(endp, trunk->cfg->domain, index); |
Philipp Maier | c66ab2c | 2020-06-02 20:55:34 +0200 | [diff] [blame] | 65 | break; |
| 66 | case MGCP_TRUNK_E1: |
Philipp Maier | 7462b95 | 2020-06-10 14:50:34 +0200 | [diff] [blame^] | 67 | /* FIXME: E1 trunk implementation is work in progress, this endpoint |
| 68 | * name is incomplete (subslots) */ |
| 69 | endp->name = talloc_asprintf(endp, "%s-1/%x", MGCP_ENDPOINT_PREFIX_E1_TRUNK, index); |
Philipp Maier | c66ab2c | 2020-06-02 20:55:34 +0200 | [diff] [blame] | 70 | LOGP(DLMGCP, LOGL_FATAL, "E1 trunks not implemented!\n"); |
Philipp Maier | 7462b95 | 2020-06-10 14:50:34 +0200 | [diff] [blame^] | 71 | endp->type = &ep_typeset.rtp; |
Philipp Maier | c66ab2c | 2020-06-02 20:55:34 +0200 | [diff] [blame] | 72 | break; |
| 73 | default: |
| 74 | osmo_panic("Cannot allocate unimplemented trunk type %d! %s:%d\n", |
| 75 | trunk->trunk_type, __FILE__, __LINE__); |
| 76 | } |
| 77 | |
| 78 | return endp; |
| 79 | } |
| 80 | |
Philipp Maier | edc00f4 | 2018-01-24 11:58:56 +0100 | [diff] [blame] | 81 | /*! release endpoint, all open connections are closed. |
| 82 | * \param[in] endp endpoint to release */ |
Philipp Maier | 1355d7e | 2018-02-01 14:30:06 +0100 | [diff] [blame] | 83 | void mgcp_endp_release(struct mgcp_endpoint *endp) |
Philipp Maier | edc00f4 | 2018-01-24 11:58:56 +0100 | [diff] [blame] | 84 | { |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 85 | LOGPENDP(endp, DLMGCP, LOGL_DEBUG, "Releasing endpoint\n"); |
Philipp Maier | edc00f4 | 2018-01-24 11:58:56 +0100 | [diff] [blame] | 86 | |
| 87 | /* Normally this function should only be called when |
| 88 | * all connections have been removed already. In case |
| 89 | * that there are still connections open (e.g. when |
| 90 | * RSIP is executed), free them all at once. */ |
| 91 | mgcp_conn_free_all(endp); |
| 92 | |
| 93 | /* Reset endpoint parameters and states */ |
| 94 | talloc_free(endp->callid); |
| 95 | endp->callid = NULL; |
| 96 | talloc_free(endp->local_options.string); |
| 97 | endp->local_options.string = NULL; |
| 98 | talloc_free(endp->local_options.codec); |
| 99 | endp->local_options.codec = NULL; |
Philipp Maier | 207ab51 | 2018-02-02 14:19:26 +0100 | [diff] [blame] | 100 | endp->wildcarded_req = false; |
Philipp Maier | edc00f4 | 2018-01-24 11:58:56 +0100 | [diff] [blame] | 101 | } |
Philipp Maier | c66ab2c | 2020-06-02 20:55:34 +0200 | [diff] [blame] | 102 | |
| 103 | /* Check if the endpoint name contains the prefix (e.g. "rtpbridge/" or |
| 104 | * "ds/e1-") and write the epname without the prefix back to the memory |
| 105 | * pointed at by epname. (per trunk the prefix is the same for all endpoints, |
| 106 | * so no ambiguity is introduced) */ |
| 107 | static void chop_epname_prefix(char *epname, const struct mgcp_trunk *trunk) |
| 108 | { |
| 109 | size_t prefix_len; |
| 110 | switch (trunk->trunk_type) { |
| 111 | case MGCP_TRUNK_VIRTUAL: |
| 112 | prefix_len = sizeof(MGCP_ENDPOINT_PREFIX_VIRTUAL_TRUNK) - 1; |
| 113 | if (strncmp |
| 114 | (epname, MGCP_ENDPOINT_PREFIX_VIRTUAL_TRUNK, |
| 115 | prefix_len) == 0) |
| 116 | memmove(epname, epname + prefix_len, |
| 117 | strlen(epname) - prefix_len + 1); |
| 118 | return; |
| 119 | case MGCP_TRUNK_E1: |
| 120 | prefix_len = sizeof(MGCP_ENDPOINT_PREFIX_E1_TRUNK) - 1; |
| 121 | if (strncmp |
| 122 | (epname, MGCP_ENDPOINT_PREFIX_VIRTUAL_TRUNK, |
| 123 | prefix_len) == 0) |
| 124 | memmove(epname, epname + prefix_len, |
| 125 | strlen(epname) - prefix_len + 1); |
| 126 | return; |
| 127 | default: |
| 128 | OSMO_ASSERT(false); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | /* Check if the endpoint name contains a suffix (e.g. "@mgw") and truncate |
| 133 | * epname by writing a '\0' char where the suffix starts. */ |
| 134 | static void chop_epname_suffix(char *epname, const struct mgcp_trunk *trunk) |
| 135 | { |
| 136 | char *suffix_begin; |
| 137 | |
| 138 | /* Endpoints on the virtual trunk may have a domain name that is |
| 139 | * followed after an @ character, this can be chopped off. All |
| 140 | * other supported trunk types do not have any suffixes that may |
| 141 | * be chopped off */ |
| 142 | if (trunk->trunk_type == MGCP_TRUNK_VIRTUAL) { |
| 143 | suffix_begin = strchr(epname, '@'); |
| 144 | if (!suffix_begin) |
| 145 | return; |
| 146 | *suffix_begin = '\0'; |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | /* Convert all characters in epname to lowercase and strip trunk prefix and |
| 151 | * endpoint name suffix (domain name) from epname. The result is written to |
| 152 | * to the memory pointed at by epname_stripped. The expected size of the |
| 153 | * result is either equal or lower then the length of the input string |
| 154 | * (epname) */ |
| 155 | static void strip_epname(char *epname_stripped, const char *epname, |
| 156 | const struct mgcp_trunk *trunk) |
| 157 | { |
| 158 | osmo_str_tolower_buf(epname_stripped, MGCP_ENDPOINT_MAXLEN, epname); |
| 159 | chop_epname_prefix(epname_stripped, trunk); |
| 160 | chop_epname_suffix(epname_stripped, trunk); |
| 161 | } |
| 162 | |
| 163 | /* Go through the trunk and find a random free (no active calls) endpoint, |
| 164 | * this function is called when a wildcarded request is carried out, which |
| 165 | * means that it is up to the MGW to choose a random free endpoint. */ |
| 166 | static struct mgcp_endpoint *find_free_endpoint(const struct mgcp_trunk *trunk) |
| 167 | { |
| 168 | struct mgcp_endpoint *endp; |
| 169 | unsigned int i; |
| 170 | |
| 171 | for (i = 0; i < trunk->number_endpoints; i++) { |
| 172 | endp = trunk->endpoints[i]; |
| 173 | if (endp->callid == NULL) |
| 174 | return endp; |
| 175 | } |
| 176 | |
| 177 | return NULL; |
| 178 | } |
| 179 | |
| 180 | /* Find an endpoint specified by its name. If the endpoint can not be found, |
| 181 | * return NULL */ |
| 182 | static struct mgcp_endpoint *find_specific_endpoint(const char *epname, |
| 183 | const struct mgcp_trunk *trunk) |
| 184 | { |
| 185 | char epname_stripped[MGCP_ENDPOINT_MAXLEN]; |
| 186 | char epname_stripped_endp[MGCP_ENDPOINT_MAXLEN]; |
| 187 | struct mgcp_endpoint *endp; |
| 188 | unsigned int i; |
| 189 | |
| 190 | /* Strip irrelevant information from the endpoint name */ |
| 191 | strip_epname(epname_stripped, epname, trunk); |
| 192 | |
| 193 | for (i = 0; i < trunk->number_endpoints; i++) { |
| 194 | endp = trunk->endpoints[i]; |
| 195 | strip_epname(epname_stripped_endp, endp->name, trunk); |
| 196 | if (strcmp(epname_stripped_endp, epname_stripped) == 0) |
| 197 | return endp; |
| 198 | } |
| 199 | |
| 200 | return NULL; |
| 201 | } |
| 202 | |
| 203 | /*! Find an endpoint by its name on a specified trunk. |
| 204 | * \param[out] cause pointer to store cause code, can be NULL. |
| 205 | * \param[in] epname endpoint name to lookup. |
| 206 | * \param[in] trunk where the endpoint is located. |
| 207 | * \returns endpoint or NULL if endpoint was not found. */ |
| 208 | struct mgcp_endpoint *mgcp_endp_by_name_trunk(int *cause, const char *epname, |
| 209 | const struct mgcp_trunk *trunk) |
| 210 | { |
| 211 | struct mgcp_endpoint *endp; |
| 212 | |
| 213 | if (cause) |
| 214 | *cause = 0; |
| 215 | |
| 216 | /* At the moment we only support a primitive ('*'-only) method of |
| 217 | * wildcarded endpoint searches that picks the next free endpoint on |
| 218 | * a trunk. */ |
| 219 | if (strstr(epname, "*")) { |
| 220 | endp = find_free_endpoint(trunk); |
| 221 | if (endp) { |
| 222 | LOGPENDP(endp, DLMGCP, LOGL_DEBUG, |
| 223 | "(trunk:%d) found free endpoint: %s\n", |
| 224 | trunk->trunk_nr, endp->name); |
| 225 | endp->wildcarded_req = true; |
| 226 | return endp; |
| 227 | } |
| 228 | |
| 229 | LOGP(DLMGCP, LOGL_ERROR, |
| 230 | "(trunk:%d) Not able to find a free endpoint\n", |
| 231 | trunk->trunk_nr); |
| 232 | if (cause) |
| 233 | *cause = -403; |
| 234 | return NULL; |
| 235 | } |
| 236 | |
| 237 | /* Find an endpoint by its name (if wildcarded request is not |
| 238 | * applicable) */ |
| 239 | endp = find_specific_endpoint(epname, trunk); |
| 240 | if (endp) { |
| 241 | LOGPENDP(endp, DLMGCP, LOGL_DEBUG, |
| 242 | "(trunk:%d) found endpoint: %s\n", |
| 243 | trunk->trunk_nr, endp->name); |
| 244 | endp->wildcarded_req = false; |
| 245 | return endp; |
| 246 | } |
| 247 | |
| 248 | LOGP(DLMGCP, LOGL_ERROR, |
| 249 | "(trunk:%d) Not able to find specified endpoint: %s\n", |
| 250 | trunk->trunk_nr, epname); |
| 251 | if (cause) |
| 252 | *cause = -500; |
| 253 | |
| 254 | return NULL; |
| 255 | } |
| 256 | |
| 257 | /* Check if the domain name, which is supplied with the endpoint name |
| 258 | * matches the configuration. */ |
| 259 | static int check_domain_name(const char *epname, struct mgcp_config *cfg) |
| 260 | { |
| 261 | char *domain_to_check; |
| 262 | |
| 263 | domain_to_check = strstr(epname, "@"); |
| 264 | if (!domain_to_check) { |
| 265 | LOGP(DLMGCP, LOGL_ERROR, "missing domain name in endpoint name \"%s\", expecting \"%s\"\n", |
| 266 | epname, cfg->domain); |
| 267 | return -EINVAL; |
| 268 | } |
| 269 | |
| 270 | /* Accept any domain if configured as "*" */ |
| 271 | if (!strcmp(cfg->domain, "*")) |
| 272 | return 0; |
| 273 | |
| 274 | if (strcmp(domain_to_check+1, cfg->domain) != 0) { |
| 275 | LOGP(DLMGCP, LOGL_ERROR, "wrong domain name in endpoint name \"%s\", expecting \"%s\"\n", |
| 276 | epname, cfg->domain); |
| 277 | return -EINVAL; |
| 278 | } |
| 279 | |
| 280 | return 0; |
| 281 | } |
| 282 | |
| 283 | /*! Find an endpoint by its name, search at all trunks. |
| 284 | * \param[out] cause, pointer to store cause code, can be NULL. |
| 285 | * \param[in] epname, must contain trunk prefix. |
| 286 | * \param[in] cfg, mgcp configuration (trunks). |
| 287 | * \returns endpoint or NULL if endpoint was not found. */ |
| 288 | struct mgcp_endpoint *mgcp_endp_by_name(int *cause, const char *epname, |
| 289 | struct mgcp_config *cfg) |
| 290 | { |
| 291 | struct mgcp_trunk *trunk; |
| 292 | struct mgcp_endpoint *endp; |
| 293 | char epname_lc[MGCP_ENDPOINT_MAXLEN]; |
| 294 | |
| 295 | osmo_str_tolower_buf(epname_lc, sizeof(epname_lc), epname); |
| 296 | epname = epname_lc; |
| 297 | |
| 298 | if (cause) |
| 299 | *cause = -500; |
| 300 | |
| 301 | /* Identify the trunk where the endpoint is located */ |
| 302 | trunk = mgcp_trunk_by_name(cfg, epname); |
| 303 | if (!trunk) |
| 304 | return NULL; |
| 305 | |
| 306 | /* Virtual endpoints require a domain name (see RFC3435, section E.3) */ |
| 307 | if (trunk->trunk_type == MGCP_TRUNK_VIRTUAL) { |
| 308 | if (check_domain_name(epname, cfg)) |
| 309 | return NULL; |
| 310 | } |
| 311 | |
| 312 | /* Identify the endpoint on the trunk */ |
| 313 | endp = mgcp_endp_by_name_trunk(cause, epname, trunk); |
| 314 | if (!endp) { |
| 315 | return NULL; |
| 316 | } |
| 317 | |
| 318 | if (cause) |
| 319 | *cause = 0; |
| 320 | return endp; |
| 321 | } |