Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 1 | /* mgcp_utils - common functions to setup an MGCP connection |
| 2 | */ |
| 3 | /* (C) 2016 by sysmocom s.f.m.c. GmbH <info@sysmocom.de> |
| 4 | * All Rights Reserved |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
Harald Welte | 220f487 | 2018-02-04 09:04:16 +0100 | [diff] [blame] | 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
Harald Welte | 220f487 | 2018-02-04 09:04:16 +0100 | [diff] [blame] | 14 | * GNU General Public License for more details. |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 15 | * |
Harald Welte | 220f487 | 2018-02-04 09:04:16 +0100 | [diff] [blame] | 16 | * You should have received a copy of the GNU General Public License |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | */ |
| 20 | |
| 21 | #include <osmocom/core/linuxlist.h> |
| 22 | #include <osmocom/core/select.h> |
| 23 | #include <osmocom/core/write_queue.h> |
| 24 | #include <osmocom/core/msgb.h> |
| 25 | #include <osmocom/core/logging.h> |
Philipp Maier | 1dc6be6 | 2017-10-05 18:25:37 +0200 | [diff] [blame] | 26 | #include <osmocom/core/byteswap.h> |
Harald Welte | 8890dfa | 2017-11-17 15:09:30 +0100 | [diff] [blame] | 27 | #include <osmocom/core/socket.h> |
Pau Espin Pedrol | 9dc7359 | 2020-08-28 20:21:55 +0200 | [diff] [blame] | 28 | #include <osmocom/core/sockaddr_str.h> |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 29 | |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 30 | #include <osmocom/mgcp_client/mgcp_client.h> |
| 31 | #include <osmocom/mgcp_client/mgcp_client_internal.h> |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 32 | |
Philipp Maier | 92a73cd | 2020-11-26 22:21:11 +0100 | [diff] [blame] | 33 | #include <osmocom/abis/e1_input.h> |
| 34 | |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 35 | #include <netinet/in.h> |
| 36 | #include <arpa/inet.h> |
| 37 | |
| 38 | #include <errno.h> |
| 39 | #include <unistd.h> |
| 40 | #include <string.h> |
Pau Espin Pedrol | 166077e | 2019-06-26 12:21:38 +0200 | [diff] [blame] | 41 | #include <ctype.h> |
Pau Espin Pedrol | c5c1430 | 2019-07-23 16:19:41 +0200 | [diff] [blame] | 42 | #include <stdlib.h> |
| 43 | #include <limits.h> |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 44 | |
Pau Espin Pedrol | 1b1d7ed | 2019-05-23 16:48:24 +0200 | [diff] [blame] | 45 | #ifndef OSMUX_CID_MAX |
| 46 | #define OSMUX_CID_MAX 255 /* FIXME: use OSMUX_CID_MAX from libosmo-netif? */ |
| 47 | #endif |
| 48 | |
Philipp Maier | df9192e | 2021-09-03 17:50:51 +0200 | [diff] [blame] | 49 | #define LOGPMGW(mgcp, level, fmt, args...) \ |
| 50 | LOGP(DLMGCP, level, "MGW(%s) " fmt, mgcp_client_name(mgcp), ## args) |
| 51 | |
Philipp Maier | 704c4f0 | 2018-06-07 18:51:31 +0200 | [diff] [blame] | 52 | /* Codec descripton for dynamic payload types (SDP) */ |
Neels Hofmeyr | 47642f2 | 2019-02-27 05:56:53 +0100 | [diff] [blame] | 53 | const struct value_string osmo_mgcpc_codec_names[] = { |
Philipp Maier | 704c4f0 | 2018-06-07 18:51:31 +0200 | [diff] [blame] | 54 | { CODEC_PCMU_8000_1, "PCMU/8000/1" }, |
| 55 | { CODEC_GSM_8000_1, "GSM/8000/1" }, |
| 56 | { CODEC_PCMA_8000_1, "PCMA/8000/1" }, |
| 57 | { CODEC_G729_8000_1, "G729/8000/1" }, |
| 58 | { CODEC_GSMEFR_8000_1, "GSM-EFR/8000/1" }, |
| 59 | { CODEC_GSMHR_8000_1, "GSM-HR-08/8000/1" }, |
| 60 | { CODEC_AMR_8000_1, "AMR/8000/1" }, |
| 61 | { CODEC_AMRWB_16000_1, "AMR-WB/16000/1" }, |
Philipp Maier | 1de5ed6 | 2021-12-21 14:38:14 +0100 | [diff] [blame] | 62 | { CODEC_IUFP, "VND.3GPP.IUFP/16000" }, |
Oliver Smith | 26d6b2b | 2023-01-24 13:04:47 +0100 | [diff] [blame] | 63 | { CODEC_CLEARMODE, "CLEARMODE/8000" }, |
Philipp Maier | 704c4f0 | 2018-06-07 18:51:31 +0200 | [diff] [blame] | 64 | { 0, NULL }, |
| 65 | }; |
| 66 | |
| 67 | /* Get encoding name from a full codec string e,g. |
| 68 | * ("CODEC/8000/2" => returns "CODEC") */ |
| 69 | static char *extract_codec_name(const char *str) |
| 70 | { |
| 71 | static char buf[64]; |
| 72 | unsigned int i; |
| 73 | |
| 74 | if (!str) |
| 75 | return NULL; |
| 76 | |
Philipp Maier | 704c4f0 | 2018-06-07 18:51:31 +0200 | [diff] [blame] | 77 | osmo_strlcpy(buf, str, sizeof(buf)); |
| 78 | |
| 79 | for (i = 0; i < strlen(buf); i++) { |
| 80 | if (buf[i] == '/') |
| 81 | buf[i] = '\0'; |
| 82 | } |
| 83 | |
| 84 | return buf; |
| 85 | } |
| 86 | |
| 87 | /*! Map a string to a codec. |
| 88 | * \ptmap[in] str input string (e.g "GSM/8000/1", "GSM/8000" or "GSM") |
| 89 | * \returns codec that corresponds to the given string representation. */ |
| 90 | enum mgcp_codecs map_str_to_codec(const char *str) |
| 91 | { |
| 92 | unsigned int i; |
| 93 | char *codec_name; |
| 94 | char str_buf[64]; |
| 95 | |
| 96 | osmo_strlcpy(str_buf, extract_codec_name(str), sizeof(str_buf)); |
| 97 | |
Neels Hofmeyr | 47642f2 | 2019-02-27 05:56:53 +0100 | [diff] [blame] | 98 | for (i = 0; i < ARRAY_SIZE(osmo_mgcpc_codec_names); i++) { |
| 99 | codec_name = extract_codec_name(osmo_mgcpc_codec_names[i].str); |
Philipp Maier | 704c4f0 | 2018-06-07 18:51:31 +0200 | [diff] [blame] | 100 | if (!codec_name) |
| 101 | continue; |
| 102 | if (strcmp(codec_name, str_buf) == 0) |
Neels Hofmeyr | 47642f2 | 2019-02-27 05:56:53 +0100 | [diff] [blame] | 103 | return osmo_mgcpc_codec_names[i].value; |
Philipp Maier | 704c4f0 | 2018-06-07 18:51:31 +0200 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | return -1; |
| 107 | } |
| 108 | |
| 109 | /* Check the ptmap for illegal mappings */ |
Neels Hofmeyr | 84274e4 | 2019-04-27 19:08:36 +0200 | [diff] [blame] | 110 | static int check_ptmap(const struct ptmap *ptmap) |
Philipp Maier | 704c4f0 | 2018-06-07 18:51:31 +0200 | [diff] [blame] | 111 | { |
| 112 | /* Check if there are mappings that leave the IANA assigned dynamic |
| 113 | * payload type range. Under normal conditions such mappings should |
| 114 | * not occur */ |
| 115 | |
| 116 | /* Its ok to have a 1:1 mapping in the statically defined |
| 117 | * range, this won't hurt */ |
| 118 | if (ptmap->codec == ptmap->pt) |
| 119 | return 0; |
| 120 | |
| 121 | if (ptmap->codec < 96 || ptmap->codec > 127) |
| 122 | goto error; |
| 123 | if (ptmap->pt < 96 || ptmap->pt > 127) |
| 124 | goto error; |
| 125 | |
| 126 | return 0; |
| 127 | error: |
| 128 | LOGP(DLMGCP, LOGL_ERROR, |
| 129 | "ptmap contains illegal mapping: codec=%u maps to pt=%u\n", |
| 130 | ptmap->codec, ptmap->pt); |
| 131 | return -1; |
| 132 | } |
| 133 | |
| 134 | /*! Map a codec to a payload type. |
| 135 | * \ptmap[in] payload pointer to payload type map with specified payload types. |
| 136 | * \ptmap[in] ptmap_len length of the payload type map. |
| 137 | * \ptmap[in] codec the codec for which the payload type should be looked up. |
| 138 | * \returns assigned payload type */ |
Neels Hofmeyr | 84274e4 | 2019-04-27 19:08:36 +0200 | [diff] [blame] | 139 | unsigned int map_codec_to_pt(const struct ptmap *ptmap, unsigned int ptmap_len, |
Philipp Maier | 704c4f0 | 2018-06-07 18:51:31 +0200 | [diff] [blame] | 140 | enum mgcp_codecs codec) |
| 141 | { |
| 142 | unsigned int i; |
| 143 | |
| 144 | /*! Note: If the payload type map is empty or the codec is not found |
| 145 | * in the map, then a 1:1 mapping is performed. If the codec falls |
| 146 | * into the statically defined range or if the mapping table isself |
| 147 | * tries to map to the statically defined range, then the mapping |
| 148 | * is also ignored and a 1:1 mapping is performed instead. */ |
| 149 | |
| 150 | /* we may return the codec directly since enum mgcp_codecs directly |
Oliver Smith | 292ba1b | 2023-06-28 10:30:25 +0200 | [diff] [blame] | 151 | * corresponds to the statically assigned payload types */ |
Philipp Maier | 704c4f0 | 2018-06-07 18:51:31 +0200 | [diff] [blame] | 152 | if (codec < 96 || codec > 127) |
| 153 | return codec; |
| 154 | |
| 155 | for (i = 0; i < ptmap_len; i++) { |
| 156 | /* Skip illegal map entries */ |
| 157 | if (check_ptmap(ptmap) == 0 && ptmap->codec == codec) |
| 158 | return ptmap->pt; |
| 159 | ptmap++; |
| 160 | } |
| 161 | |
| 162 | /* If nothing is found, do not perform any mapping */ |
| 163 | return codec; |
| 164 | } |
| 165 | |
| 166 | /*! Map a payload type to a codec. |
| 167 | * \ptmap[in] payload pointer to payload type map with specified payload types. |
| 168 | * \ptmap[in] ptmap_len length of the payload type map. |
| 169 | * \ptmap[in] payload type for which the codec should be looked up. |
| 170 | * \returns codec that corresponds to the specified payload type */ |
| 171 | enum mgcp_codecs map_pt_to_codec(struct ptmap *ptmap, unsigned int ptmap_len, |
| 172 | unsigned int pt) |
| 173 | { |
| 174 | unsigned int i; |
| 175 | |
| 176 | /*! Note: If the payload type map is empty or the payload type is not |
| 177 | * found in the map, then a 1:1 mapping is performed. If the payload |
| 178 | * type falls into the statically defined range or if the mapping |
| 179 | * table isself tries to map to the statically defined range, then |
| 180 | * the mapping is also ignored and a 1:1 mapping is performed |
| 181 | * instead. */ |
| 182 | |
| 183 | /* See also note in map_codec_to_pt() */ |
| 184 | if (pt < 96 || pt > 127) |
| 185 | return pt; |
| 186 | |
| 187 | for (i = 0; i < ptmap_len; i++) { |
| 188 | if (check_ptmap(ptmap) == 0 && ptmap->pt == pt) |
| 189 | return ptmap->codec; |
| 190 | ptmap++; |
| 191 | } |
| 192 | |
| 193 | /* If nothing is found, do not perform any mapping */ |
| 194 | return pt; |
| 195 | } |
| 196 | |
Pau Espin Pedrol | 8e8d59f | 2023-06-13 19:41:44 +0200 | [diff] [blame] | 197 | static void _mgcp_client_conf_init(struct mgcp_client_conf *conf) |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 198 | { |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 199 | /* NULL and -1 default to MGCP_CLIENT_*_DEFAULT values */ |
| 200 | *conf = (struct mgcp_client_conf){ |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 201 | .local_addr = NULL, |
| 202 | .local_port = -1, |
| 203 | .remote_addr = NULL, |
| 204 | .remote_port = -1, |
Pau Espin Pedrol | 563386e | 2023-06-14 12:20:49 +0200 | [diff] [blame] | 205 | .keepalive = { |
| 206 | .timeout_sec = 0, /* disabled */ |
| 207 | .req_interval_sec = 0, /* disabled */ |
| 208 | .req_endpoint_name = MGCP_CLIENT_KEEPALIVE_DEFAULT_ENDP, |
| 209 | }, |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 210 | }; |
Philipp Maier | 3f2c15f | 2021-07-22 11:53:07 +0200 | [diff] [blame] | 211 | |
| 212 | INIT_LLIST_HEAD(&conf->reset_epnames); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 213 | } |
| 214 | |
Pau Espin Pedrol | 8e8d59f | 2023-06-13 19:41:44 +0200 | [diff] [blame] | 215 | /*! Allocate and initialize MGCP client configuration struct with default values. |
| 216 | * \param[in] ctx talloc context to use as a parent during allocation. |
| 217 | * |
| 218 | * The returned struct can be freed using talloc_free(). |
| 219 | */ |
| 220 | struct mgcp_client_conf *mgcp_client_conf_alloc(void *ctx) |
| 221 | { |
| 222 | struct mgcp_client_conf *conf = talloc(ctx, struct mgcp_client_conf); |
| 223 | _mgcp_client_conf_init(conf); |
| 224 | return conf; |
| 225 | } |
| 226 | |
| 227 | /*! Initialize MGCP client configuration struct with default values. |
| 228 | * \param[out] conf Client configuration. |
| 229 | * |
| 230 | * This function is deprecated and should not be used, as it may break if size |
| 231 | * of struct mgcp_client_conf changes in the future! |
| 232 | */ |
| 233 | void mgcp_client_conf_init(struct mgcp_client_conf *conf) |
| 234 | { |
| 235 | _mgcp_client_conf_init(conf); |
| 236 | } |
| 237 | |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 238 | static void mgcp_client_handle_response(struct mgcp_client *mgcp, |
| 239 | struct mgcp_response_pending *pending, |
| 240 | struct mgcp_response *response) |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 241 | { |
| 242 | if (!pending) { |
Philipp Maier | df9192e | 2021-09-03 17:50:51 +0200 | [diff] [blame] | 243 | LOGPMGW(mgcp, LOGL_ERROR, "Cannot handle NULL response\n"); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 244 | return; |
| 245 | } |
| 246 | if (pending->response_cb) |
| 247 | pending->response_cb(response, pending->priv); |
| 248 | else |
Philipp Maier | df9192e | 2021-09-03 17:50:51 +0200 | [diff] [blame] | 249 | LOGPMGW(mgcp, LOGL_DEBUG, "MGCP response ignored (NULL cb)\n"); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 250 | talloc_free(pending); |
| 251 | } |
| 252 | |
| 253 | static int mgcp_response_parse_head(struct mgcp_response *r, struct msgb *msg) |
| 254 | { |
| 255 | int comment_pos; |
| 256 | char *end; |
| 257 | |
| 258 | if (mgcp_msg_terminate_nul(msg)) |
| 259 | goto response_parse_failure; |
| 260 | |
| 261 | r->body = (char *)msg->data; |
| 262 | |
Harald Welte | 9bf7c53 | 2017-11-17 14:14:31 +0100 | [diff] [blame] | 263 | if (sscanf(r->body, "%3d %u %n", |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 264 | &r->head.response_code, &r->head.trans_id, |
| 265 | &comment_pos) != 2) |
| 266 | goto response_parse_failure; |
| 267 | |
Philipp Maier | abe8c89 | 2018-01-20 00:15:12 +0100 | [diff] [blame] | 268 | osmo_strlcpy(r->head.comment, r->body + comment_pos, sizeof(r->head.comment)); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 269 | end = strchr(r->head.comment, '\r'); |
| 270 | if (!end) |
| 271 | goto response_parse_failure; |
| 272 | /* Mark the end of the comment */ |
| 273 | *end = '\0'; |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 274 | return 0; |
| 275 | |
| 276 | response_parse_failure: |
| 277 | LOGP(DLMGCP, LOGL_ERROR, |
| 278 | "Failed to parse MGCP response header\n"); |
| 279 | return -EINVAL; |
| 280 | } |
| 281 | |
| 282 | /* TODO undup against mgcp_protocol.c:mgcp_check_param() */ |
| 283 | static bool mgcp_line_is_valid(const char *line) |
| 284 | { |
| 285 | const size_t line_len = strlen(line); |
| 286 | if (line[0] == '\0') |
| 287 | return true; |
| 288 | |
| 289 | if (line_len < 2 |
| 290 | || line[1] != '=') { |
| 291 | LOGP(DLMGCP, LOGL_ERROR, |
| 292 | "Wrong MGCP option format: '%s'\n", |
| 293 | line); |
| 294 | return false; |
| 295 | } |
| 296 | |
| 297 | return true; |
| 298 | } |
| 299 | |
Philipp Maier | 704c4f0 | 2018-06-07 18:51:31 +0200 | [diff] [blame] | 300 | /* Parse a line like "m=audio 16002 RTP/AVP 98", extract port and payload types */ |
| 301 | static int mgcp_parse_audio_port_pt(struct mgcp_response *r, char *line) |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 302 | { |
Philipp Maier | 704c4f0 | 2018-06-07 18:51:31 +0200 | [diff] [blame] | 303 | char *pt_str; |
Pau Espin Pedrol | c5c1430 | 2019-07-23 16:19:41 +0200 | [diff] [blame] | 304 | char *pt_end; |
Pau Espin Pedrol | a2b1c5e | 2019-07-26 14:13:14 +0200 | [diff] [blame] | 305 | unsigned long int pt; |
Philipp Maier | 704c4f0 | 2018-06-07 18:51:31 +0200 | [diff] [blame] | 306 | unsigned int count = 0; |
| 307 | unsigned int i; |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 308 | |
Philipp Maier | 704c4f0 | 2018-06-07 18:51:31 +0200 | [diff] [blame] | 309 | /* Extract port information */ |
| 310 | if (sscanf(line, "m=audio %hu", &r->audio_port) != 1) |
| 311 | goto response_parse_failure_port; |
Philipp Maier | 10f32db | 2017-12-13 12:34:34 +0100 | [diff] [blame] | 312 | if (r->audio_port == 0) |
Philipp Maier | 704c4f0 | 2018-06-07 18:51:31 +0200 | [diff] [blame] | 313 | goto response_parse_failure_port; |
Philipp Maier | 10f32db | 2017-12-13 12:34:34 +0100 | [diff] [blame] | 314 | |
Philipp Maier | 704c4f0 | 2018-06-07 18:51:31 +0200 | [diff] [blame] | 315 | /* Extract payload types */ |
| 316 | line = strstr(line, "RTP/AVP "); |
| 317 | if (!line) |
| 318 | goto exit; |
| 319 | |
| 320 | pt_str = strtok(line, " "); |
| 321 | while (1) { |
| 322 | /* Do not allow excessive payload types */ |
| 323 | if (count > ARRAY_SIZE(r->codecs)) |
| 324 | goto response_parse_failure_pt; |
| 325 | |
| 326 | pt_str = strtok(NULL, " "); |
| 327 | if (!pt_str) |
| 328 | break; |
Pau Espin Pedrol | c5c1430 | 2019-07-23 16:19:41 +0200 | [diff] [blame] | 329 | errno = 0; |
| 330 | pt = strtoul(pt_str, &pt_end, 0); |
| 331 | if ((errno == ERANGE && pt == ULONG_MAX) || (errno && !pt) || |
| 332 | pt_str == pt_end) |
| 333 | goto response_parse_failure_pt; |
Philipp Maier | 704c4f0 | 2018-06-07 18:51:31 +0200 | [diff] [blame] | 334 | |
Pau Espin Pedrol | a2b1c5e | 2019-07-26 14:13:14 +0200 | [diff] [blame] | 335 | if (pt >> 7) /* PT is 7 bit field, higher values not allowed */ |
| 336 | goto response_parse_failure_pt; |
| 337 | |
Philipp Maier | 704c4f0 | 2018-06-07 18:51:31 +0200 | [diff] [blame] | 338 | /* Do not allow duplicate payload types */ |
| 339 | for (i = 0; i < count; i++) |
| 340 | if (r->codecs[i] == pt) |
| 341 | goto response_parse_failure_pt; |
| 342 | |
| 343 | /* Note: The payload type we store may not necessarly match |
| 344 | * the codec types we have defined in enum mgcp_codecs. To |
| 345 | * ensure that the end result only contains codec types which |
| 346 | * match enum mgcp_codecs, we will go through afterwards and |
| 347 | * remap the affected entries with the inrofmation we learn |
| 348 | * from rtpmap */ |
| 349 | r->codecs[count] = pt; |
| 350 | count++; |
| 351 | } |
| 352 | |
| 353 | r->codecs_len = count; |
| 354 | |
| 355 | exit: |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 356 | return 0; |
| 357 | |
Philipp Maier | 704c4f0 | 2018-06-07 18:51:31 +0200 | [diff] [blame] | 358 | response_parse_failure_port: |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 359 | LOGP(DLMGCP, LOGL_ERROR, |
Philipp Maier | 704c4f0 | 2018-06-07 18:51:31 +0200 | [diff] [blame] | 360 | "Failed to parse SDP parameter port (%s)\n", line); |
Philipp Maier | 06da85e | 2017-10-05 18:49:24 +0200 | [diff] [blame] | 361 | return -EINVAL; |
Philipp Maier | 704c4f0 | 2018-06-07 18:51:31 +0200 | [diff] [blame] | 362 | |
| 363 | response_parse_failure_pt: |
| 364 | LOGP(DLMGCP, LOGL_ERROR, |
| 365 | "Failed to parse SDP parameter payload types (%s)\n", line); |
| 366 | return -EINVAL; |
| 367 | } |
| 368 | |
| 369 | /* Parse a line like "m=audio 16002 RTP/AVP 98", extract port and payload types */ |
| 370 | static int mgcp_parse_audio_ptime_rtpmap(struct mgcp_response *r, const char *line) |
| 371 | { |
| 372 | unsigned int pt; |
| 373 | char codec_resp[64]; |
Oliver Smith | 9423817 | 2023-06-28 13:28:57 +0200 | [diff] [blame] | 374 | int rc; |
Pau Espin Pedrol | c12bfb7 | 2019-04-16 17:23:09 +0200 | [diff] [blame] | 375 | |
Neels Hofmeyr | 23f4048 | 2019-08-08 04:03:42 +0200 | [diff] [blame] | 376 | #define A_PTIME "a=ptime:" |
| 377 | #define A_RTPMAP "a=rtpmap:" |
Pau Espin Pedrol | c12bfb7 | 2019-04-16 17:23:09 +0200 | [diff] [blame] | 378 | |
Neels Hofmeyr | 23f4048 | 2019-08-08 04:03:42 +0200 | [diff] [blame] | 379 | if (osmo_str_startswith(line, A_PTIME)) { |
| 380 | if (sscanf(line, A_PTIME "%u", &r->ptime) != 1) { |
| 381 | LOGP(DLMGCP, LOGL_ERROR, |
| 382 | "Failed to parse SDP parameter, invalid ptime (%s)\n", line); |
| 383 | return -EINVAL; |
| 384 | } |
| 385 | } else if (osmo_str_startswith(line, A_RTPMAP)) { |
| 386 | if (sscanf(line, A_RTPMAP "%d %63s", &pt, codec_resp) != 2) { |
| 387 | LOGP(DLMGCP, LOGL_ERROR, |
| 388 | "Failed to parse SDP parameter, invalid rtpmap: %s\n", osmo_quote_str(line, -1)); |
| 389 | return -EINVAL; |
| 390 | } |
Neels Hofmeyr | 3ab8ca4 | 2019-08-08 04:03:42 +0200 | [diff] [blame] | 391 | if (r->ptmap_len >= ARRAY_SIZE(r->ptmap)) { |
| 392 | LOGP(DLMGCP, LOGL_ERROR, "No more space in ptmap array (len=%u)\n", r->ptmap_len); |
| 393 | return -ENOSPC; |
Neels Hofmeyr | 23f4048 | 2019-08-08 04:03:42 +0200 | [diff] [blame] | 394 | } |
Oliver Smith | 9423817 | 2023-06-28 13:28:57 +0200 | [diff] [blame] | 395 | rc = map_str_to_codec(codec_resp); |
| 396 | if (rc < 0) { |
| 397 | LOGP(DLMGCP, LOGL_ERROR, |
| 398 | "Failed to parse SDP parameter, can't parse codec in rtpmap: %s\n", osmo_quote_str(line, -1)); |
| 399 | return -EINVAL; |
| 400 | } |
Neels Hofmeyr | 3ab8ca4 | 2019-08-08 04:03:42 +0200 | [diff] [blame] | 401 | r->ptmap[r->ptmap_len].pt = pt; |
Oliver Smith | 9423817 | 2023-06-28 13:28:57 +0200 | [diff] [blame] | 402 | r->ptmap[r->ptmap_len].codec = rc; |
Neels Hofmeyr | 3ab8ca4 | 2019-08-08 04:03:42 +0200 | [diff] [blame] | 403 | r->ptmap_len++; |
Philipp Maier | 704c4f0 | 2018-06-07 18:51:31 +0200 | [diff] [blame] | 404 | } |
Pau Espin Pedrol | c12bfb7 | 2019-04-16 17:23:09 +0200 | [diff] [blame] | 405 | |
Philipp Maier | 704c4f0 | 2018-06-07 18:51:31 +0200 | [diff] [blame] | 406 | return 0; |
Philipp Maier | 06da85e | 2017-10-05 18:49:24 +0200 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | /* Parse a line like "c=IN IP4 10.11.12.13" */ |
| 410 | static int mgcp_parse_audio_ip(struct mgcp_response *r, const char *line) |
| 411 | { |
Pau Espin Pedrol | 9dc7359 | 2020-08-28 20:21:55 +0200 | [diff] [blame] | 412 | struct in6_addr ip_test; |
| 413 | bool is_ipv6; |
Philipp Maier | 06da85e | 2017-10-05 18:49:24 +0200 | [diff] [blame] | 414 | |
Pau Espin Pedrol | 9dc7359 | 2020-08-28 20:21:55 +0200 | [diff] [blame] | 415 | if (strncmp("c=IN IP", line, 7) != 0) |
Philipp Maier | 06da85e | 2017-10-05 18:49:24 +0200 | [diff] [blame] | 416 | goto response_parse_failure; |
Pau Espin Pedrol | 9dc7359 | 2020-08-28 20:21:55 +0200 | [diff] [blame] | 417 | line += 7; |
| 418 | if (*line == '6') |
| 419 | is_ipv6 = true; |
| 420 | else if (*line == '4') |
| 421 | is_ipv6 = false; |
| 422 | else |
Philipp Maier | 06da85e | 2017-10-05 18:49:24 +0200 | [diff] [blame] | 423 | goto response_parse_failure; |
Pau Espin Pedrol | 9dc7359 | 2020-08-28 20:21:55 +0200 | [diff] [blame] | 424 | line++; |
| 425 | if (*line != ' ') |
Philipp Maier | 06da85e | 2017-10-05 18:49:24 +0200 | [diff] [blame] | 426 | goto response_parse_failure; |
Pau Espin Pedrol | 9dc7359 | 2020-08-28 20:21:55 +0200 | [diff] [blame] | 427 | line++; |
Philipp Maier | 06da85e | 2017-10-05 18:49:24 +0200 | [diff] [blame] | 428 | |
Pau Espin Pedrol | 9dc7359 | 2020-08-28 20:21:55 +0200 | [diff] [blame] | 429 | /* Extract and check IP-Address */ |
| 430 | if (is_ipv6) { |
| 431 | /* 45 = INET6_ADDRSTRLEN -1 */ |
| 432 | if (sscanf(line, "%45s", r->audio_ip) != 1) |
| 433 | goto response_parse_failure; |
| 434 | if (inet_pton(AF_INET6, r->audio_ip, &ip_test) != 1) |
| 435 | goto response_parse_failure; |
| 436 | } else { |
| 437 | /* 15 = INET_ADDRSTRLEN -1 */ |
| 438 | if (sscanf(line, "%15s", r->audio_ip) != 1) |
| 439 | goto response_parse_failure; |
| 440 | if (inet_pton(AF_INET, r->audio_ip, &ip_test) != 1) |
| 441 | goto response_parse_failure; |
| 442 | } |
Philipp Maier | 06da85e | 2017-10-05 18:49:24 +0200 | [diff] [blame] | 443 | return 0; |
| 444 | |
| 445 | response_parse_failure: |
| 446 | LOGP(DLMGCP, LOGL_ERROR, |
| 447 | "Failed to parse MGCP response header (audio ip)\n"); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 448 | return -EINVAL; |
| 449 | } |
| 450 | |
Pau Espin Pedrol | 91088c3 | 2019-04-24 21:02:40 +0200 | [diff] [blame] | 451 | /*! Extract OSMUX CID from an MGCP parameter line (string). |
| 452 | * \param[in] line single parameter line from the MGCP message |
| 453 | * \returns OSMUX CID, -1 wildcard, -2 on error |
| 454 | * FIXME: This is a copy of function in mgcp_msg.c. Have some common.c file between both libs? |
| 455 | */ |
| 456 | static int mgcp_parse_osmux_cid(const char *line) |
| 457 | { |
| 458 | int osmux_cid; |
| 459 | |
| 460 | |
Pau Espin Pedrol | c1bf469 | 2019-05-14 16:23:24 +0200 | [diff] [blame] | 461 | if (strcasecmp(line + 2, "Osmux: *") == 0) { |
Pau Espin Pedrol | 91088c3 | 2019-04-24 21:02:40 +0200 | [diff] [blame] | 462 | LOGP(DLMGCP, LOGL_DEBUG, "Parsed wilcard Osmux CID\n"); |
| 463 | return -1; |
| 464 | } |
| 465 | |
Pau Espin Pedrol | c1bf469 | 2019-05-14 16:23:24 +0200 | [diff] [blame] | 466 | if (sscanf(line + 2 + 7, "%u", &osmux_cid) != 1) { |
Pau Espin Pedrol | 91088c3 | 2019-04-24 21:02:40 +0200 | [diff] [blame] | 467 | LOGP(DLMGCP, LOGL_ERROR, "Failed parsing Osmux in MGCP msg line: %s\n", |
| 468 | line); |
| 469 | return -2; |
| 470 | } |
| 471 | |
Pau Espin Pedrol | 91088c3 | 2019-04-24 21:02:40 +0200 | [diff] [blame] | 472 | if (osmux_cid > OSMUX_CID_MAX) { /* OSMUX_CID_MAX from libosmo-netif */ |
| 473 | LOGP(DLMGCP, LOGL_ERROR, "Osmux ID too large: %u > %u\n", |
| 474 | osmux_cid, OSMUX_CID_MAX); |
| 475 | return -2; |
| 476 | } |
Pau Espin Pedrol | c7c8e64 | 2022-10-06 18:24:21 +0200 | [diff] [blame] | 477 | LOGP(DLMGCP, LOGL_DEBUG, "MGW offered Osmux CID %u\n", osmux_cid); |
Pau Espin Pedrol | 91088c3 | 2019-04-24 21:02:40 +0200 | [diff] [blame] | 478 | |
| 479 | return osmux_cid; |
| 480 | } |
| 481 | |
Philipp Maier | 3b12e1b | 2018-01-18 15:16:13 +0100 | [diff] [blame] | 482 | /* A new section is marked by a double line break, check a few more |
| 483 | * patterns as there may be variants */ |
| 484 | static char *mgcp_find_section_end(char *string) |
| 485 | { |
| 486 | char *rc; |
| 487 | |
| 488 | rc = strstr(string, "\n\n"); |
| 489 | if (rc) |
Neels Hofmeyr | ce356ee | 2023-03-30 16:51:40 +0200 | [diff] [blame] | 490 | return rc + 2; |
Philipp Maier | 3b12e1b | 2018-01-18 15:16:13 +0100 | [diff] [blame] | 491 | |
| 492 | rc = strstr(string, "\n\r\n\r"); |
| 493 | if (rc) |
Neels Hofmeyr | ce356ee | 2023-03-30 16:51:40 +0200 | [diff] [blame] | 494 | return rc + 4; |
Philipp Maier | 3b12e1b | 2018-01-18 15:16:13 +0100 | [diff] [blame] | 495 | |
| 496 | rc = strstr(string, "\r\n\r\n"); |
| 497 | if (rc) |
Neels Hofmeyr | ce356ee | 2023-03-30 16:51:40 +0200 | [diff] [blame] | 498 | return rc + 4; |
Philipp Maier | 3b12e1b | 2018-01-18 15:16:13 +0100 | [diff] [blame] | 499 | |
| 500 | return NULL; |
| 501 | } |
| 502 | |
Philipp Maier | 275ac97 | 2018-01-20 00:58:16 +0100 | [diff] [blame] | 503 | /*! Parse body (SDP) parameters of the MGCP response |
| 504 | * \param[in,out] r Response data |
| 505 | * \returns 0 on success, -EINVAL on error. */ |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 506 | int mgcp_response_parse_params(struct mgcp_response *r) |
| 507 | { |
| 508 | char *line; |
| 509 | int rc; |
Neels Hofmeyr | 0793d2f | 2018-02-21 14:55:34 +0100 | [diff] [blame] | 510 | char *data; |
Philipp Maier | e9d645b | 2018-01-19 23:54:08 +0100 | [diff] [blame] | 511 | char *data_ptr; |
Philipp Maier | 704c4f0 | 2018-06-07 18:51:31 +0200 | [diff] [blame] | 512 | int i; |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 513 | |
Philipp Maier | e9d645b | 2018-01-19 23:54:08 +0100 | [diff] [blame] | 514 | /* Since this functions performs a destructive parsing, we create a |
| 515 | * local copy of the body data */ |
Neels Hofmeyr | 0793d2f | 2018-02-21 14:55:34 +0100 | [diff] [blame] | 516 | OSMO_ASSERT(r->body); |
| 517 | data = talloc_strdup(r, r->body); |
Philipp Maier | e9d645b | 2018-01-19 23:54:08 +0100 | [diff] [blame] | 518 | OSMO_ASSERT(data); |
Philipp Maier | 55295f7 | 2018-01-15 14:00:28 +0100 | [diff] [blame] | 519 | |
Philipp Maier | e9d645b | 2018-01-19 23:54:08 +0100 | [diff] [blame] | 520 | /* Find beginning of the parameter (SDP) section */ |
| 521 | data_ptr = mgcp_find_section_end(data); |
Neels Hofmeyr | 1083533 | 2018-02-21 14:55:34 +0100 | [diff] [blame] | 522 | if (!data_ptr) { |
Neels Hofmeyr | e827831 | 2019-09-19 03:06:46 +0200 | [diff] [blame] | 523 | LOGP(DLMGCP, LOGL_DEBUG, "MGCP response contains no SDP parameters\n"); |
| 524 | rc = 0; |
Philipp Maier | e9d645b | 2018-01-19 23:54:08 +0100 | [diff] [blame] | 525 | goto exit; |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 526 | } |
| 527 | |
Neels Hofmeyr | 0793d2f | 2018-02-21 14:55:34 +0100 | [diff] [blame] | 528 | /* data_ptr now points to the beginning of the section-end-marker; for_each_non_empty_line() |
| 529 | * skips any \r and \n characters for free, so we don't need to skip the marker. */ |
| 530 | |
Philipp Maier | e9d645b | 2018-01-19 23:54:08 +0100 | [diff] [blame] | 531 | for_each_non_empty_line(line, data_ptr) { |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 532 | if (!mgcp_line_is_valid(line)) |
| 533 | return -EINVAL; |
| 534 | |
| 535 | switch (line[0]) { |
Philipp Maier | 704c4f0 | 2018-06-07 18:51:31 +0200 | [diff] [blame] | 536 | case 'a': |
| 537 | rc = mgcp_parse_audio_ptime_rtpmap(r, line); |
| 538 | if (rc) |
| 539 | goto exit; |
| 540 | break; |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 541 | case 'm': |
Philipp Maier | 704c4f0 | 2018-06-07 18:51:31 +0200 | [diff] [blame] | 542 | rc = mgcp_parse_audio_port_pt(r, line); |
Philipp Maier | 06da85e | 2017-10-05 18:49:24 +0200 | [diff] [blame] | 543 | if (rc) |
Philipp Maier | e9d645b | 2018-01-19 23:54:08 +0100 | [diff] [blame] | 544 | goto exit; |
Philipp Maier | 06da85e | 2017-10-05 18:49:24 +0200 | [diff] [blame] | 545 | break; |
| 546 | case 'c': |
| 547 | rc = mgcp_parse_audio_ip(r, line); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 548 | if (rc) |
Philipp Maier | e9d645b | 2018-01-19 23:54:08 +0100 | [diff] [blame] | 549 | goto exit; |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 550 | break; |
| 551 | default: |
| 552 | /* skip unhandled parameters */ |
| 553 | break; |
| 554 | } |
| 555 | } |
Philipp Maier | e9d645b | 2018-01-19 23:54:08 +0100 | [diff] [blame] | 556 | |
Philipp Maier | 704c4f0 | 2018-06-07 18:51:31 +0200 | [diff] [blame] | 557 | /* See also note in mgcp_parse_audio_port_pt() */ |
| 558 | for (i = 0; i < r->codecs_len; i++) |
| 559 | r->codecs[i] = map_pt_to_codec(r->ptmap, r->ptmap_len, r->codecs[i]); |
| 560 | |
Philipp Maier | e9d645b | 2018-01-19 23:54:08 +0100 | [diff] [blame] | 561 | rc = 0; |
| 562 | exit: |
| 563 | talloc_free(data); |
| 564 | return rc; |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 565 | } |
| 566 | |
Philipp Maier | 55295f7 | 2018-01-15 14:00:28 +0100 | [diff] [blame] | 567 | /* Parse a line like "X: something" */ |
| 568 | static int mgcp_parse_head_param(char *result, unsigned int result_len, |
| 569 | char label, const char *line) |
Philipp Maier | ffd75e4 | 2017-11-22 11:44:50 +0100 | [diff] [blame] | 570 | { |
Philipp Maier | 55295f7 | 2018-01-15 14:00:28 +0100 | [diff] [blame] | 571 | char label_string[4]; |
Neels Hofmeyr | 23e7bf1 | 2018-09-03 21:26:22 +0200 | [diff] [blame] | 572 | size_t rc; |
Philipp Maier | 55295f7 | 2018-01-15 14:00:28 +0100 | [diff] [blame] | 573 | |
| 574 | /* Detect empty parameters */ |
Philipp Maier | ffd75e4 | 2017-11-22 11:44:50 +0100 | [diff] [blame] | 575 | if (strlen(line) < 4) |
| 576 | goto response_parse_failure; |
| 577 | |
Philipp Maier | 55295f7 | 2018-01-15 14:00:28 +0100 | [diff] [blame] | 578 | /* Check if the label matches */ |
| 579 | snprintf(label_string, sizeof(label_string), "%c: ", label); |
| 580 | if (memcmp(label_string, line, 3) != 0) |
Philipp Maier | ffd75e4 | 2017-11-22 11:44:50 +0100 | [diff] [blame] | 581 | goto response_parse_failure; |
| 582 | |
Philipp Maier | 55295f7 | 2018-01-15 14:00:28 +0100 | [diff] [blame] | 583 | /* Copy payload part of the string to destinations (the label string |
| 584 | * is always 3 chars long) */ |
Neels Hofmeyr | 23e7bf1 | 2018-09-03 21:26:22 +0200 | [diff] [blame] | 585 | rc = osmo_strlcpy(result, line + 3, result_len); |
| 586 | if (rc >= result_len) { |
| 587 | LOGP(DLMGCP, LOGL_ERROR, |
| 588 | "Failed to parse MGCP response (parameter label: %c):" |
| 589 | " the received conn ID is too long: %zu, maximum is %u characters\n", |
| 590 | label, rc, result_len - 1); |
| 591 | return -ENOSPC; |
| 592 | } |
Philipp Maier | ffd75e4 | 2017-11-22 11:44:50 +0100 | [diff] [blame] | 593 | return 0; |
| 594 | |
| 595 | response_parse_failure: |
| 596 | LOGP(DLMGCP, LOGL_ERROR, |
Philipp Maier | 55295f7 | 2018-01-15 14:00:28 +0100 | [diff] [blame] | 597 | "Failed to parse MGCP response (parameter label: %c)\n", label); |
Philipp Maier | ffd75e4 | 2017-11-22 11:44:50 +0100 | [diff] [blame] | 598 | return -EINVAL; |
| 599 | } |
| 600 | |
| 601 | /* Parse MGCP parameters of the response */ |
| 602 | static int parse_head_params(struct mgcp_response *r) |
| 603 | { |
| 604 | char *line; |
| 605 | int rc = 0; |
| 606 | OSMO_ASSERT(r->body); |
Philipp Maier | 55295f7 | 2018-01-15 14:00:28 +0100 | [diff] [blame] | 607 | char *data; |
| 608 | char *data_ptr; |
| 609 | char *data_end; |
Philipp Maier | ffd75e4 | 2017-11-22 11:44:50 +0100 | [diff] [blame] | 610 | |
Philipp Maier | 55295f7 | 2018-01-15 14:00:28 +0100 | [diff] [blame] | 611 | /* Since this functions performs a destructive parsing, we create a |
| 612 | * local copy of the body data */ |
Philipp Maier | 1fc6999 | 2018-01-31 15:32:55 +0100 | [diff] [blame] | 613 | data = talloc_zero_size(r, strlen(r->body)+1); |
Philipp Maier | 55295f7 | 2018-01-15 14:00:28 +0100 | [diff] [blame] | 614 | OSMO_ASSERT(data); |
| 615 | data_ptr = data; |
| 616 | osmo_strlcpy(data, r->body, strlen(r->body)); |
| 617 | |
| 618 | /* If there is an SDP body attached, prevent for_each_non_empty_line() |
| 619 | * into running in there, we are not yet interested in the parameters |
| 620 | * stored there. */ |
Pau Espin Pedrol | c12bfb7 | 2019-04-16 17:23:09 +0200 | [diff] [blame] | 621 | data_end = mgcp_find_section_end(data); |
Philipp Maier | ffd75e4 | 2017-11-22 11:44:50 +0100 | [diff] [blame] | 622 | if (data_end) |
| 623 | *data_end = '\0'; |
| 624 | |
Philipp Maier | 55295f7 | 2018-01-15 14:00:28 +0100 | [diff] [blame] | 625 | for_each_non_empty_line(line, data_ptr) { |
Pau Espin Pedrol | 166077e | 2019-06-26 12:21:38 +0200 | [diff] [blame] | 626 | switch (toupper(line[0])) { |
Philipp Maier | 55295f7 | 2018-01-15 14:00:28 +0100 | [diff] [blame] | 627 | case 'Z': |
| 628 | rc = mgcp_parse_head_param(r->head.endpoint, |
| 629 | sizeof(r->head.endpoint), |
| 630 | 'Z', line); |
| 631 | if (rc) |
| 632 | goto exit; |
Philipp Maier | 771b26a | 2018-01-31 13:53:11 +0100 | [diff] [blame] | 633 | |
| 634 | /* A specific endpoint identifier returned by the MGW |
| 635 | * must not contain any wildcard characters */ |
| 636 | if (strstr(r->head.endpoint, "*") != NULL) { |
| 637 | rc = -EINVAL; |
| 638 | goto exit; |
| 639 | } |
Philipp Maier | 3261dc7 | 2018-01-31 14:03:13 +0100 | [diff] [blame] | 640 | |
| 641 | /* A specific endpoint identifier returned by the MGW |
| 642 | * must contain an @ character */ |
| 643 | if (strstr(r->head.endpoint, "@") == NULL) { |
| 644 | rc = -EINVAL; |
| 645 | goto exit; |
| 646 | } |
Philipp Maier | 55295f7 | 2018-01-15 14:00:28 +0100 | [diff] [blame] | 647 | break; |
Philipp Maier | ffd75e4 | 2017-11-22 11:44:50 +0100 | [diff] [blame] | 648 | case 'I': |
Philipp Maier | 55295f7 | 2018-01-15 14:00:28 +0100 | [diff] [blame] | 649 | rc = mgcp_parse_head_param(r->head.conn_id, |
| 650 | sizeof(r->head.conn_id), |
| 651 | 'I', line); |
Philipp Maier | ffd75e4 | 2017-11-22 11:44:50 +0100 | [diff] [blame] | 652 | if (rc) |
| 653 | goto exit; |
| 654 | break; |
Pau Espin Pedrol | 91088c3 | 2019-04-24 21:02:40 +0200 | [diff] [blame] | 655 | case 'X': |
Pau Espin Pedrol | c1bf469 | 2019-05-14 16:23:24 +0200 | [diff] [blame] | 656 | if (strncasecmp("Osmux: ", line + 2, strlen("Osmux: ")) == 0) { |
Pau Espin Pedrol | 91088c3 | 2019-04-24 21:02:40 +0200 | [diff] [blame] | 657 | rc = mgcp_parse_osmux_cid(line); |
| 658 | if (rc < 0) { |
| 659 | /* -1: we don't want wildcards in response. -2: error */ |
| 660 | rc = -EINVAL; |
| 661 | goto exit; |
| 662 | } |
| 663 | r->head.x_osmo_osmux_use = true; |
| 664 | r->head.x_osmo_osmux_cid = (uint8_t) rc; |
| 665 | rc = 0; |
| 666 | break; |
| 667 | } |
| 668 | /* Ignore unknown X-headers */ |
| 669 | break; |
Philipp Maier | ffd75e4 | 2017-11-22 11:44:50 +0100 | [diff] [blame] | 670 | default: |
| 671 | /* skip unhandled parameters */ |
| 672 | break; |
| 673 | } |
| 674 | } |
| 675 | exit: |
Philipp Maier | 55295f7 | 2018-01-15 14:00:28 +0100 | [diff] [blame] | 676 | talloc_free(data); |
Philipp Maier | ffd75e4 | 2017-11-22 11:44:50 +0100 | [diff] [blame] | 677 | return rc; |
| 678 | } |
| 679 | |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 680 | static struct mgcp_response_pending *mgcp_client_response_pending_get( |
| 681 | struct mgcp_client *mgcp, |
Neels Hofmeyr | c8f37cb | 2017-11-30 13:43:11 +0100 | [diff] [blame] | 682 | mgcp_trans_id_t trans_id) |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 683 | { |
| 684 | struct mgcp_response_pending *pending; |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 685 | llist_for_each_entry(pending, &mgcp->responses_pending, entry) { |
Neels Hofmeyr | c8f37cb | 2017-11-30 13:43:11 +0100 | [diff] [blame] | 686 | if (pending->trans_id == trans_id) { |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 687 | llist_del(&pending->entry); |
| 688 | return pending; |
| 689 | } |
| 690 | } |
| 691 | return NULL; |
| 692 | } |
| 693 | |
| 694 | /* Feed an MGCP message into the receive processing. |
| 695 | * Parse the head and call any callback registered for the transaction id found |
| 696 | * in the MGCP message. This is normally called directly from the internal |
| 697 | * mgcp_do_read that reads from the socket connected to the MGCP gateway. This |
| 698 | * function is published mainly to be able to feed data from the test suite. |
| 699 | */ |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 700 | int mgcp_client_rx(struct mgcp_client *mgcp, struct msgb *msg) |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 701 | { |
Philipp Maier | 1fc6999 | 2018-01-31 15:32:55 +0100 | [diff] [blame] | 702 | struct mgcp_response *r; |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 703 | struct mgcp_response_pending *pending; |
| 704 | int rc; |
| 705 | |
Philipp Maier | 1fc6999 | 2018-01-31 15:32:55 +0100 | [diff] [blame] | 706 | r = talloc_zero(mgcp, struct mgcp_response); |
| 707 | OSMO_ASSERT(r); |
| 708 | |
Pau Espin Pedrol | 563386e | 2023-06-14 12:20:49 +0200 | [diff] [blame] | 709 | /* Re-arm keepalive timer if enabled */ |
| 710 | if (OSMO_UNLIKELY(mgcp->conn_up == false)) { |
| 711 | LOGPMGW(mgcp, LOGL_NOTICE, "MGCP link to MGW now considered UP\n"); |
| 712 | mgcp->conn_up = true; |
| 713 | } |
| 714 | if (mgcp->actual.keepalive.timeout_sec > 0) |
| 715 | osmo_timer_schedule(&mgcp->keepalive_rx_timer, mgcp->actual.keepalive.timeout_sec, 0); |
| 716 | |
Philipp Maier | 1fc6999 | 2018-01-31 15:32:55 +0100 | [diff] [blame] | 717 | rc = mgcp_response_parse_head(r, msg); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 718 | if (rc) { |
Philipp Maier | df9192e | 2021-09-03 17:50:51 +0200 | [diff] [blame] | 719 | LOGPMGW(mgcp, LOGL_ERROR, "Cannot parse MGCP response (head)\n"); |
Philipp Maier | 1fc6999 | 2018-01-31 15:32:55 +0100 | [diff] [blame] | 720 | rc = 1; |
| 721 | goto error; |
Philipp Maier | ffd75e4 | 2017-11-22 11:44:50 +0100 | [diff] [blame] | 722 | } |
| 723 | |
Philipp Maier | df9192e | 2021-09-03 17:50:51 +0200 | [diff] [blame] | 724 | LOGPMGW(mgcp, LOGL_DEBUG, "MGCP client: Rx %d %u %s\n", |
Neels Hofmeyr | d573107 | 2021-07-15 01:36:47 +0200 | [diff] [blame] | 725 | r->head.response_code, r->head.trans_id, r->head.comment); |
| 726 | |
Philipp Maier | 1fc6999 | 2018-01-31 15:32:55 +0100 | [diff] [blame] | 727 | rc = parse_head_params(r); |
Philipp Maier | ffd75e4 | 2017-11-22 11:44:50 +0100 | [diff] [blame] | 728 | if (rc) { |
Philipp Maier | df9192e | 2021-09-03 17:50:51 +0200 | [diff] [blame] | 729 | LOGPMGW(mgcp, LOGL_ERROR, "Cannot parse MGCP response (head parameters)\n"); |
Philipp Maier | 1fc6999 | 2018-01-31 15:32:55 +0100 | [diff] [blame] | 730 | rc = 1; |
| 731 | goto error; |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 732 | } |
| 733 | |
Philipp Maier | 1fc6999 | 2018-01-31 15:32:55 +0100 | [diff] [blame] | 734 | pending = mgcp_client_response_pending_get(mgcp, r->head.trans_id); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 735 | if (!pending) { |
Philipp Maier | df9192e | 2021-09-03 17:50:51 +0200 | [diff] [blame] | 736 | LOGPMGW(mgcp, LOGL_ERROR, "Cannot find matching MGCP transaction for trans_id %d\n", |
| 737 | r->head.trans_id); |
Philipp Maier | 1fc6999 | 2018-01-31 15:32:55 +0100 | [diff] [blame] | 738 | rc = -ENOENT; |
| 739 | goto error; |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 740 | } |
| 741 | |
Philipp Maier | 1fc6999 | 2018-01-31 15:32:55 +0100 | [diff] [blame] | 742 | mgcp_client_handle_response(mgcp, pending, r); |
| 743 | rc = 0; |
| 744 | |
| 745 | error: |
| 746 | talloc_free(r); |
| 747 | return rc; |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 748 | } |
| 749 | |
| 750 | static int mgcp_do_read(struct osmo_fd *fd) |
| 751 | { |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 752 | struct mgcp_client *mgcp = fd->data; |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 753 | struct msgb *msg; |
| 754 | int ret; |
| 755 | |
| 756 | msg = msgb_alloc_headroom(4096, 128, "mgcp_from_gw"); |
| 757 | if (!msg) { |
Philipp Maier | df9192e | 2021-09-03 17:50:51 +0200 | [diff] [blame] | 758 | LOGPMGW(mgcp, LOGL_ERROR, "Failed to allocate MGCP message.\n"); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 759 | return -1; |
| 760 | } |
| 761 | |
| 762 | ret = read(fd->fd, msg->data, 4096 - 128); |
| 763 | if (ret <= 0) { |
Philipp Maier | df9192e | 2021-09-03 17:50:51 +0200 | [diff] [blame] | 764 | LOGPMGW(mgcp, LOGL_ERROR, "Failed to read: %s: %d='%s'\n", |
| 765 | osmo_sock_get_name2(fd->fd), errno, strerror(errno)); |
Neels Hofmeyr | 0a40379 | 2018-12-12 03:10:18 +0100 | [diff] [blame] | 766 | |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 767 | msgb_free(msg); |
| 768 | return -1; |
Harald Welte | 9bf7c53 | 2017-11-17 14:14:31 +0100 | [diff] [blame] | 769 | } |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 770 | |
| 771 | msg->l2h = msgb_put(msg, ret); |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 772 | ret = mgcp_client_rx(mgcp, msg); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 773 | talloc_free(msg); |
| 774 | return ret; |
| 775 | } |
| 776 | |
| 777 | static int mgcp_do_write(struct osmo_fd *fd, struct msgb *msg) |
| 778 | { |
| 779 | int ret; |
Philipp Maier | df9192e | 2021-09-03 17:50:51 +0200 | [diff] [blame] | 780 | struct mgcp_client *mgcp = fd->data; |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 781 | |
Philipp Maier | df9192e | 2021-09-03 17:50:51 +0200 | [diff] [blame] | 782 | LOGPMGW(mgcp, LOGL_DEBUG, "Tx MGCP: %s: len=%u '%s'...\n", |
| 783 | osmo_sock_get_name2(fd->fd), msg->len, |
| 784 | osmo_escape_str((const char *)msg->data, OSMO_MIN(42, msg->len))); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 785 | |
| 786 | ret = write(fd->fd, msg->data, msg->len); |
Pau Espin Pedrol | 563386e | 2023-06-14 12:20:49 +0200 | [diff] [blame] | 787 | if (OSMO_UNLIKELY(ret != msg->len)) |
Philipp Maier | df9192e | 2021-09-03 17:50:51 +0200 | [diff] [blame] | 788 | LOGPMGW(mgcp, LOGL_ERROR, "Failed to Tx MGCP: %s: %d='%s'; msg: len=%u '%s'...\n", |
| 789 | osmo_sock_get_name2(fd->fd), errno, strerror(errno), |
| 790 | msg->len, osmo_escape_str((const char *)msg->data, OSMO_MIN(42, msg->len))); |
Pau Espin Pedrol | 563386e | 2023-06-14 12:20:49 +0200 | [diff] [blame] | 791 | |
| 792 | /* Re-arm the keepalive Tx timer: */ |
| 793 | if (mgcp->actual.keepalive.req_interval_sec > 0) |
| 794 | osmo_timer_schedule(&mgcp->keepalive_tx_timer, mgcp->actual.keepalive.req_interval_sec, 0); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 795 | return ret; |
| 796 | } |
| 797 | |
Pau Espin Pedrol | 2e411f3 | 2023-06-14 11:52:04 +0200 | [diff] [blame] | 798 | static const char *_mgcp_client_name_append_domain(const struct mgcp_client *mgcp, const char *name) |
| 799 | { |
| 800 | static char endpoint[MGCP_ENDPOINT_MAXLEN]; |
| 801 | int rc; |
| 802 | |
| 803 | rc = snprintf(endpoint, sizeof(endpoint), "%s@%s", name, mgcp_client_endpoint_domain(mgcp)); |
| 804 | if (rc > sizeof(endpoint) - 1) { |
| 805 | LOGPMGW(mgcp, LOGL_ERROR, "MGCP endpoint exceeds maximum length of %zu: '%s@%s'\n", |
| 806 | sizeof(endpoint) - 1, name, mgcp_client_endpoint_domain(mgcp)); |
| 807 | return NULL; |
| 808 | } |
| 809 | if (rc < 1) { |
| 810 | LOGPMGW(mgcp, LOGL_ERROR, "Cannot compose MGCP endpoint name\n"); |
| 811 | return NULL; |
| 812 | } |
| 813 | return endpoint; |
| 814 | } |
| 815 | |
| 816 | /* Safely ignore the MGCP response to the DLCX sent via _mgcp_client_send_dlcx() */ |
| 817 | static void _ignore_mgcp_response(struct mgcp_response *response, void *priv) { } |
| 818 | |
| 819 | /* Format DLCX message (fire and forget) and send it off to the MGW */ |
| 820 | static void _mgcp_client_send_dlcx(struct mgcp_client *mgcp, const char *epname) |
| 821 | { |
| 822 | struct msgb *msgb_dlcx; |
| 823 | struct mgcp_msg mgcp_msg_dlcx = { |
| 824 | .verb = MGCP_VERB_DLCX, |
| 825 | .presence = MGCP_MSG_PRESENCE_ENDPOINT, |
| 826 | }; |
| 827 | osmo_strlcpy(mgcp_msg_dlcx.endpoint, epname, sizeof(mgcp_msg_dlcx.endpoint)); |
| 828 | msgb_dlcx = mgcp_msg_gen(mgcp, &mgcp_msg_dlcx); |
| 829 | mgcp_client_tx(mgcp, msgb_dlcx, &_ignore_mgcp_response, NULL); |
| 830 | } |
| 831 | |
Pau Espin Pedrol | 563386e | 2023-06-14 12:20:49 +0200 | [diff] [blame] | 832 | /* Format AuditEndpoint message (fire and forget) and send it off to the MGW */ |
| 833 | static void _mgcp_client_send_auep(struct mgcp_client *mgcp, const char *epname) |
| 834 | { |
| 835 | struct msgb *msgb_auep; |
| 836 | struct mgcp_msg mgcp_msg_auep = { |
| 837 | .verb = MGCP_VERB_AUEP, |
| 838 | .presence = MGCP_MSG_PRESENCE_ENDPOINT, |
| 839 | }; |
| 840 | OSMO_STRLCPY_ARRAY(mgcp_msg_auep.endpoint, epname); |
| 841 | msgb_auep = mgcp_msg_gen(mgcp, &mgcp_msg_auep); |
| 842 | mgcp_client_tx(mgcp, msgb_auep, &_ignore_mgcp_response, NULL); |
| 843 | } |
| 844 | |
| 845 | static void mgcp_client_keepalive_tx_timer_cb(void *data) |
| 846 | { |
| 847 | struct mgcp_client *mgcp = (struct mgcp_client *)data; |
| 848 | LOGPMGW(mgcp, LOGL_INFO, "Triggering keepalive MGCP request\n"); |
| 849 | const char *epname = _mgcp_client_name_append_domain(mgcp, mgcp->actual.keepalive.req_endpoint_name); |
| 850 | _mgcp_client_send_auep(mgcp, epname); |
| 851 | |
| 852 | /* Re-arm the timer: */ |
| 853 | osmo_timer_schedule(&mgcp->keepalive_tx_timer, mgcp->actual.keepalive.req_interval_sec, 0); |
| 854 | } |
| 855 | |
| 856 | static void mgcp_client_keepalive_rx_timer_cb(void *data) |
| 857 | { |
| 858 | struct mgcp_client *mgcp = (struct mgcp_client *)data; |
| 859 | LOGPMGW(mgcp, LOGL_ERROR, "MGCP link to MGW now considered DOWN (keepalive timeout, more than %u seconds with no answer from MGW)\n", |
| 860 | mgcp->actual.keepalive.timeout_sec); |
| 861 | mgcp->conn_up = false; |
| 862 | /* TODO: Potentially time out all ongoing transactions for that MGW. Maybe based on VTY cfg? */ |
| 863 | } |
| 864 | |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 865 | struct mgcp_client *mgcp_client_init(void *ctx, |
| 866 | struct mgcp_client_conf *conf) |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 867 | { |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 868 | struct mgcp_client *mgcp; |
Philipp Maier | 3f2c15f | 2021-07-22 11:53:07 +0200 | [diff] [blame] | 869 | struct reset_ep *reset_ep; |
| 870 | struct reset_ep *actual_reset_ep; |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 871 | |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 872 | mgcp = talloc_zero(ctx, struct mgcp_client); |
Harald Welte | efe1571 | 2020-07-15 10:56:45 +0200 | [diff] [blame] | 873 | if (!mgcp) |
| 874 | return NULL; |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 875 | |
| 876 | INIT_LLIST_HEAD(&mgcp->responses_pending); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 877 | |
| 878 | mgcp->next_trans_id = 1; |
| 879 | |
| 880 | mgcp->actual.local_addr = conf->local_addr ? conf->local_addr : |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 881 | MGCP_CLIENT_LOCAL_ADDR_DEFAULT; |
Pau Espin Pedrol | 8e74c63 | 2022-10-17 19:20:45 +0200 | [diff] [blame] | 882 | mgcp->actual.local_port = conf->local_port > 0 ? (uint16_t)conf->local_port : |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 883 | MGCP_CLIENT_LOCAL_PORT_DEFAULT; |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 884 | |
| 885 | mgcp->actual.remote_addr = conf->remote_addr ? conf->remote_addr : |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 886 | MGCP_CLIENT_REMOTE_ADDR_DEFAULT; |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 887 | mgcp->actual.remote_port = conf->remote_port >= 0 ? (uint16_t)conf->remote_port : |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 888 | MGCP_CLIENT_REMOTE_PORT_DEFAULT; |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 889 | |
Neels Hofmeyr | ac69ea9 | 2018-12-19 00:27:50 +0100 | [diff] [blame] | 890 | if (osmo_strlcpy(mgcp->actual.endpoint_domain_name, conf->endpoint_domain_name, |
| 891 | sizeof(mgcp->actual.endpoint_domain_name)) |
| 892 | >= sizeof(mgcp->actual.endpoint_domain_name)) { |
Philipp Maier | df9192e | 2021-09-03 17:50:51 +0200 | [diff] [blame] | 893 | LOGPMGW(mgcp, LOGL_ERROR, "MGCP client: endpoint domain name is too long, max length is %zu: '%s'\n", |
| 894 | sizeof(mgcp->actual.endpoint_domain_name) - 1, conf->endpoint_domain_name); |
Neels Hofmeyr | ac69ea9 | 2018-12-19 00:27:50 +0100 | [diff] [blame] | 895 | talloc_free(mgcp); |
| 896 | return NULL; |
| 897 | } |
Philipp Maier | df9192e | 2021-09-03 17:50:51 +0200 | [diff] [blame] | 898 | LOGPMGW(mgcp, LOGL_NOTICE, "MGCP client: using endpoint domain '@%s'\n", mgcp_client_endpoint_domain(mgcp)); |
Neels Hofmeyr | ac69ea9 | 2018-12-19 00:27:50 +0100 | [diff] [blame] | 899 | |
Philipp Maier | 3f2c15f | 2021-07-22 11:53:07 +0200 | [diff] [blame] | 900 | INIT_LLIST_HEAD(&mgcp->actual.reset_epnames); |
| 901 | llist_for_each_entry(reset_ep, &conf->reset_epnames, list) { |
| 902 | actual_reset_ep = talloc_memdup(mgcp, reset_ep, sizeof(*reset_ep)); |
| 903 | llist_add_tail(&actual_reset_ep->list, &mgcp->actual.reset_epnames); |
| 904 | } |
| 905 | |
Philipp Maier | df9192e | 2021-09-03 17:50:51 +0200 | [diff] [blame] | 906 | if (conf->description) |
| 907 | mgcp->actual.description = talloc_strdup(mgcp, conf->description); |
| 908 | |
Pau Espin Pedrol | 563386e | 2023-06-14 12:20:49 +0200 | [diff] [blame] | 909 | osmo_wqueue_init(&mgcp->wq, 1024); |
| 910 | mgcp->wq.read_cb = mgcp_do_read; |
| 911 | mgcp->wq.write_cb = mgcp_do_write; |
| 912 | osmo_fd_setup(&mgcp->wq.bfd, -1, OSMO_FD_READ, osmo_wqueue_bfd_cb, mgcp, 0); |
| 913 | |
| 914 | memcpy(&mgcp->actual.keepalive, &conf->keepalive, sizeof(conf->keepalive)); |
| 915 | osmo_timer_setup(&mgcp->keepalive_tx_timer, mgcp_client_keepalive_tx_timer_cb, mgcp); |
| 916 | osmo_timer_setup(&mgcp->keepalive_rx_timer, mgcp_client_keepalive_rx_timer_cb, mgcp); |
| 917 | |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 918 | return mgcp; |
| 919 | } |
| 920 | |
Philipp Maier | 3f2c15f | 2021-07-22 11:53:07 +0200 | [diff] [blame] | 921 | /*! Initialize client connection (opens socket) |
Philipp Maier | 275ac97 | 2018-01-20 00:58:16 +0100 | [diff] [blame] | 922 | * \param[in,out] mgcp MGCP client descriptor. |
| 923 | * \returns 0 on success, -EINVAL on error. */ |
Pau Espin Pedrol | 8e74c63 | 2022-10-17 19:20:45 +0200 | [diff] [blame] | 924 | int mgcp_client_connect(struct mgcp_client *mgcp) |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 925 | { |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 926 | int rc; |
Philipp Maier | 3f2c15f | 2021-07-22 11:53:07 +0200 | [diff] [blame] | 927 | struct reset_ep *reset_ep; |
| 928 | const char *epname; |
Pau Espin Pedrol | 563386e | 2023-06-14 12:20:49 +0200 | [diff] [blame] | 929 | bool some_dlcx_sent = false; |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 930 | |
| 931 | if (!mgcp) { |
Philipp Maier | df9192e | 2021-09-03 17:50:51 +0200 | [diff] [blame] | 932 | LOGPMGW(mgcp, LOGL_FATAL, "Client not initialized properly\n"); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 933 | return -EINVAL; |
| 934 | } |
| 935 | |
Pau Espin Pedrol | 563386e | 2023-06-14 12:20:49 +0200 | [diff] [blame] | 936 | rc = osmo_sock_init2_ofd(&mgcp->wq.bfd, AF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, mgcp->actual.local_addr, |
Pau Espin Pedrol | 8e74c63 | 2022-10-17 19:20:45 +0200 | [diff] [blame] | 937 | mgcp->actual.local_port, mgcp->actual.remote_addr, mgcp->actual.remote_port, |
| 938 | OSMO_SOCK_F_BIND | OSMO_SOCK_F_CONNECT); |
Harald Welte | 8890dfa | 2017-11-17 15:09:30 +0100 | [diff] [blame] | 939 | if (rc < 0) { |
Philipp Maier | df9192e | 2021-09-03 17:50:51 +0200 | [diff] [blame] | 940 | LOGPMGW(mgcp, LOGL_FATAL, |
| 941 | "Failed to initialize socket %s:%u -> %s:%u for MGW: %s\n", |
Philipp Maier | 276a414 | 2021-07-29 11:55:14 +0200 | [diff] [blame] | 942 | mgcp->actual.local_addr ? mgcp->actual.local_addr : "(any)", mgcp->actual.local_port, |
| 943 | mgcp->actual.remote_addr ? mgcp->actual.local_addr : "(any)", mgcp->actual.remote_port, |
| 944 | strerror(errno)); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 945 | goto error_close_fd; |
| 946 | } |
| 947 | |
Pau Espin Pedrol | 563386e | 2023-06-14 12:20:49 +0200 | [diff] [blame] | 948 | LOGPMGW(mgcp, LOGL_INFO, "MGW connection: %s\n", osmo_sock_get_name2(mgcp->wq.bfd.fd)); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 949 | |
Philipp Maier | 3f2c15f | 2021-07-22 11:53:07 +0200 | [diff] [blame] | 950 | /* If configured, send a DLCX message to the endpoints that are configured to |
| 951 | * be reset on startup. Usually this is a wildcarded endpoint. */ |
| 952 | llist_for_each_entry(reset_ep, &mgcp->actual.reset_epnames, list) { |
| 953 | epname = _mgcp_client_name_append_domain(mgcp, reset_ep->name); |
Philipp Maier | df9192e | 2021-09-03 17:50:51 +0200 | [diff] [blame] | 954 | LOGPMGW(mgcp, LOGL_INFO, "Sending DLCX to: %s\n", epname); |
Philipp Maier | 3f2c15f | 2021-07-22 11:53:07 +0200 | [diff] [blame] | 955 | _mgcp_client_send_dlcx(mgcp, epname); |
Pau Espin Pedrol | 563386e | 2023-06-14 12:20:49 +0200 | [diff] [blame] | 956 | some_dlcx_sent = true; |
Philipp Maier | 3f2c15f | 2021-07-22 11:53:07 +0200 | [diff] [blame] | 957 | } |
Pau Espin Pedrol | 563386e | 2023-06-14 12:20:49 +0200 | [diff] [blame] | 958 | |
Pau Espin Pedrol | 4c06589 | 2023-06-26 18:44:44 +0200 | [diff] [blame] | 959 | if (mgcp->actual.keepalive.req_interval_sec > 0) { |
| 960 | if (!some_dlcx_sent) { |
Pau Espin Pedrol | 563386e | 2023-06-14 12:20:49 +0200 | [diff] [blame] | 961 | /* Attempt an immediate probe to find out if link is UP or DOWN: */ |
| 962 | osmo_timer_schedule(&mgcp->keepalive_tx_timer, 0, 0); |
Pau Espin Pedrol | 563386e | 2023-06-14 12:20:49 +0200 | [diff] [blame] | 963 | } |
Pau Espin Pedrol | 4c06589 | 2023-06-26 18:44:44 +0200 | [diff] [blame] | 964 | /* else: keepalive_tx_timer was already scheduled (if needed) down in the stack during Tx DLCX above */ |
| 965 | } else { |
| 966 | /* Assume link is UP by default, so that this MGW can be selected: */ |
| 967 | mgcp->conn_up = true; |
Pau Espin Pedrol | 563386e | 2023-06-14 12:20:49 +0200 | [diff] [blame] | 968 | } |
Pau Espin Pedrol | 563386e | 2023-06-14 12:20:49 +0200 | [diff] [blame] | 969 | |
| 970 | if (mgcp->actual.keepalive.timeout_sec > 0) |
| 971 | osmo_timer_schedule(&mgcp->keepalive_rx_timer, mgcp->actual.keepalive.timeout_sec, 0); |
| 972 | |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 973 | return 0; |
| 974 | error_close_fd: |
Pau Espin Pedrol | 563386e | 2023-06-14 12:20:49 +0200 | [diff] [blame] | 975 | close(mgcp->wq.bfd.fd); |
| 976 | mgcp->wq.bfd.fd = -1; |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 977 | return rc; |
| 978 | } |
| 979 | |
Pau Espin Pedrol | 8e74c63 | 2022-10-17 19:20:45 +0200 | [diff] [blame] | 980 | /*! DEPRECATED: Initialize client connection (opens socket) |
Philipp Maier | 3f4a4cb | 2021-07-26 13:20:05 +0200 | [diff] [blame] | 981 | * \param[in,out] mgcp MGCP client descriptor. |
| 982 | * \returns 0 on success, -EINVAL on error. */ |
Pau Espin Pedrol | 8e74c63 | 2022-10-17 19:20:45 +0200 | [diff] [blame] | 983 | int mgcp_client_connect2(struct mgcp_client *mgcp, unsigned int retry_n_ports) |
Philipp Maier | 3f4a4cb | 2021-07-26 13:20:05 +0200 | [diff] [blame] | 984 | { |
Pau Espin Pedrol | 8e74c63 | 2022-10-17 19:20:45 +0200 | [diff] [blame] | 985 | return mgcp_client_connect(mgcp); |
Philipp Maier | 3f4a4cb | 2021-07-26 13:20:05 +0200 | [diff] [blame] | 986 | } |
| 987 | |
| 988 | /*! Terminate client connection |
| 989 | * \param[in,out] mgcp MGCP client descriptor. |
| 990 | * \returns 0 on success, -EINVAL on error. */ |
| 991 | void mgcp_client_disconnect(struct mgcp_client *mgcp) |
| 992 | { |
| 993 | struct osmo_wqueue *wq; |
| 994 | |
| 995 | if (!mgcp) { |
Philipp Maier | df9192e | 2021-09-03 17:50:51 +0200 | [diff] [blame] | 996 | LOGP(DLMGCP, LOGL_FATAL, "MGCP client not initialized properly\n"); |
Philipp Maier | 3f4a4cb | 2021-07-26 13:20:05 +0200 | [diff] [blame] | 997 | return; |
| 998 | } |
| 999 | |
Pau Espin Pedrol | 563386e | 2023-06-14 12:20:49 +0200 | [diff] [blame] | 1000 | /* Disarm keepalive Tx/Rx timer until next connect() */ |
| 1001 | osmo_timer_del(&mgcp->keepalive_rx_timer); |
| 1002 | osmo_timer_del(&mgcp->keepalive_tx_timer); |
| 1003 | mgcp->conn_up = false; |
| 1004 | |
Philipp Maier | 3f4a4cb | 2021-07-26 13:20:05 +0200 | [diff] [blame] | 1005 | wq = &mgcp->wq; |
| 1006 | osmo_wqueue_clear(wq); |
Philipp Maier | df9192e | 2021-09-03 17:50:51 +0200 | [diff] [blame] | 1007 | LOGPMGW(mgcp, LOGL_INFO, "MGCP association: %s -- closed!\n", osmo_sock_get_name2(wq->bfd.fd)); |
Philipp Maier | 3f4a4cb | 2021-07-26 13:20:05 +0200 | [diff] [blame] | 1008 | if (osmo_fd_is_registered(&wq->bfd)) |
| 1009 | osmo_fd_unregister(&wq->bfd); |
Pau Espin Pedrol | 747fbce | 2023-03-14 11:48:27 +0100 | [diff] [blame] | 1010 | close(wq->bfd.fd); |
| 1011 | wq->bfd.fd = -1; |
Philipp Maier | 3f4a4cb | 2021-07-26 13:20:05 +0200 | [diff] [blame] | 1012 | } |
| 1013 | |
Philipp Maier | 275ac97 | 2018-01-20 00:58:16 +0100 | [diff] [blame] | 1014 | /*! Get the IP-Aaddress of the associated MGW as string. |
| 1015 | * \param[in] mgcp MGCP client descriptor. |
| 1016 | * \returns a pointer to the address string. */ |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 1017 | const char *mgcp_client_remote_addr_str(struct mgcp_client *mgcp) |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 1018 | { |
| 1019 | return mgcp->actual.remote_addr; |
| 1020 | } |
| 1021 | |
Philipp Maier | 275ac97 | 2018-01-20 00:58:16 +0100 | [diff] [blame] | 1022 | /*! Get the IP-Port of the associated MGW. |
| 1023 | * \param[in] mgcp MGCP client descriptor. |
| 1024 | * \returns port number. */ |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 1025 | uint16_t mgcp_client_remote_port(struct mgcp_client *mgcp) |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 1026 | { |
| 1027 | return mgcp->actual.remote_port; |
| 1028 | } |
| 1029 | |
Pau Espin Pedrol | 74d0e5c | 2020-09-02 17:19:20 +0200 | [diff] [blame] | 1030 | /*! Get the IP-Address of the associated MGW as its numeric representation. |
| 1031 | * DEPRECATED, DON'T USE. |
Philipp Maier | 275ac97 | 2018-01-20 00:58:16 +0100 | [diff] [blame] | 1032 | * \param[in] mgcp MGCP client descriptor. |
| 1033 | * \returns IP-Address as 32 bit integer (network byte order) */ |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 1034 | uint32_t mgcp_client_remote_addr_n(struct mgcp_client *mgcp) |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 1035 | { |
Pau Espin Pedrol | 74d0e5c | 2020-09-02 17:19:20 +0200 | [diff] [blame] | 1036 | return 0; |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 1037 | } |
| 1038 | |
Neels Hofmeyr | ac69ea9 | 2018-12-19 00:27:50 +0100 | [diff] [blame] | 1039 | /* To compose endpoint names, usually for CRCX, use this as domain name. |
| 1040 | * For example, snprintf("rtpbridge\*@%s", mgcp_client_endpoint_domain(mgcp)). */ |
| 1041 | const char *mgcp_client_endpoint_domain(const struct mgcp_client *mgcp) |
| 1042 | { |
| 1043 | return mgcp->actual.endpoint_domain_name[0] ? mgcp->actual.endpoint_domain_name : "mgw"; |
| 1044 | } |
| 1045 | |
Philipp Maier | a466f57 | 2020-06-25 16:11:04 +0200 | [diff] [blame] | 1046 | /*! Compose endpoint name for a wildcarded request to the virtual trunk |
| 1047 | * \param[in] mgcp MGCP client descriptor. |
| 1048 | * \returns string containing the endpoint name (e.g. rtpbridge\*@mgw) */ |
Pau Espin Pedrol | ca0818c | 2019-04-16 17:23:38 +0200 | [diff] [blame] | 1049 | const char *mgcp_client_rtpbridge_wildcard(const struct mgcp_client *mgcp) |
| 1050 | { |
| 1051 | return _mgcp_client_name_append_domain(mgcp, "rtpbridge/*"); |
| 1052 | } |
| 1053 | |
Philipp Maier | 4863591 | 2020-06-25 20:16:22 +0200 | [diff] [blame] | 1054 | /*! Compose endpoint name for an E1 endpoint. |
| 1055 | * \param[in] ctx talloc context. |
| 1056 | * \param[in] mgcp MGCP client descriptor. |
| 1057 | * \param[in] trunk_id id number of the E1 trunk (1-64). |
| 1058 | * \param[in] ts timeslot on the E1 trunk (1-31). |
| 1059 | * \param[in] rate bitrate used on the E1 trunk (e.g 16 for 16kbit). |
| 1060 | * \param[in] offset bit offset of the E1 subslot (e.g. 4 for the third 16k subslot). |
| 1061 | * \returns string containing the endpoint name (e.g. ds/e1-1/s-1/su16-4). */ |
| 1062 | const char *mgcp_client_e1_epname(void *ctx, const struct mgcp_client *mgcp, uint8_t trunk_id, uint8_t ts, |
| 1063 | uint8_t rate, uint8_t offset) |
| 1064 | { |
| 1065 | /* See also comment in libosmo-mgcp, mgcp_client.c, gen_e1_epname() */ |
| 1066 | const uint8_t valid_rates[] = { 64, 32, 32, 16, 16, 16, 16, 8, 8, 8, 8, 8, 8, 8, 8 }; |
| 1067 | const uint8_t valid_offsets[] = { 0, 0, 4, 0, 2, 4, 6, 0, 1, 2, 3, 4, 5, 6, 7 }; |
| 1068 | |
| 1069 | uint8_t i; |
| 1070 | bool rate_offs_valid = false; |
| 1071 | char *epname; |
| 1072 | |
| 1073 | epname = |
| 1074 | talloc_asprintf(ctx, "ds/e1-%u/s-%u/su%u-%u@%s", trunk_id, ts, rate, offset, |
| 1075 | mgcp_client_endpoint_domain(mgcp)); |
| 1076 | if (!epname) { |
Philipp Maier | df9192e | 2021-09-03 17:50:51 +0200 | [diff] [blame] | 1077 | LOGPMGW(mgcp, LOGL_ERROR, "Cannot compose MGCP e1-endpoint name!\n"); |
Philipp Maier | 4863591 | 2020-06-25 20:16:22 +0200 | [diff] [blame] | 1078 | return NULL; |
| 1079 | } |
| 1080 | |
| 1081 | /* Check if the supplied rate/offset pair resembles a valid combination */ |
| 1082 | for (i = 0; i < sizeof(valid_rates); i++) { |
| 1083 | if (valid_rates[i] == rate && valid_offsets[i] == offset) |
| 1084 | rate_offs_valid = true; |
| 1085 | } |
| 1086 | if (!rate_offs_valid) { |
Philipp Maier | df9192e | 2021-09-03 17:50:51 +0200 | [diff] [blame] | 1087 | LOGPMGW(mgcp, LOGL_ERROR, |
| 1088 | "Cannot compose MGCP e1-endpoint name (%s), rate(%u)/offset(%u) combination is invalid!\n", |
| 1089 | epname, rate, offset); |
Philipp Maier | 4863591 | 2020-06-25 20:16:22 +0200 | [diff] [blame] | 1090 | talloc_free(epname); |
| 1091 | return NULL; |
| 1092 | } |
| 1093 | |
| 1094 | /* An E1 line has a maximum of 32 timeslots, while the first (ts=0) is |
| 1095 | * reserverd for framing and alignment, so we can not use it here. */ |
Philipp Maier | 92a73cd | 2020-11-26 22:21:11 +0100 | [diff] [blame] | 1096 | if (ts == 0 || ts > NUM_E1_TS-1) { |
Philipp Maier | df9192e | 2021-09-03 17:50:51 +0200 | [diff] [blame] | 1097 | LOGPMGW(mgcp, LOGL_ERROR, |
| 1098 | "Cannot compose MGCP e1-endpoint name (%s), E1-timeslot number (%u) is invalid!\n", |
| 1099 | epname, ts); |
Philipp Maier | 4863591 | 2020-06-25 20:16:22 +0200 | [diff] [blame] | 1100 | talloc_free(epname); |
| 1101 | return NULL; |
| 1102 | } |
| 1103 | |
| 1104 | return epname; |
| 1105 | } |
| 1106 | |
Philipp Maier | 30bdf94 | 2023-02-20 12:40:46 +0100 | [diff] [blame] | 1107 | struct mgcp_response_pending *mgcp_client_pending_add(struct mgcp_client *mgcp, mgcp_trans_id_t trans_id, |
| 1108 | mgcp_response_cb_t response_cb, void *priv) |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 1109 | { |
| 1110 | struct mgcp_response_pending *pending; |
| 1111 | |
| 1112 | pending = talloc_zero(mgcp, struct mgcp_response_pending); |
Harald Welte | 827da2c | 2020-07-15 10:57:08 +0200 | [diff] [blame] | 1113 | if (!pending) |
| 1114 | return NULL; |
| 1115 | |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 1116 | pending->trans_id = trans_id; |
| 1117 | pending->response_cb = response_cb; |
| 1118 | pending->priv = priv; |
| 1119 | llist_add_tail(&pending->entry, &mgcp->responses_pending); |
| 1120 | |
| 1121 | return pending; |
| 1122 | } |
| 1123 | |
Philipp Maier | df9192e | 2021-09-03 17:50:51 +0200 | [diff] [blame] | 1124 | /* Send the MGCP message in msg to the MGW and handle a response with |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 1125 | * response_cb. NOTE: the response_cb still needs to call |
| 1126 | * mgcp_response_parse_params(response) to get the parsed parameters -- to |
| 1127 | * potentially save some CPU cycles, only the head line has been parsed when |
Neels Hofmeyr | c8f37cb | 2017-11-30 13:43:11 +0100 | [diff] [blame] | 1128 | * the response_cb is invoked. |
| 1129 | * Before the priv pointer becomes invalid, e.g. due to transaction timeout, |
| 1130 | * mgcp_client_cancel() needs to be called for this transaction. |
| 1131 | */ |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 1132 | int mgcp_client_tx(struct mgcp_client *mgcp, struct msgb *msg, |
| 1133 | mgcp_response_cb_t response_cb, void *priv) |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 1134 | { |
Harald Welte | 563ffc5 | 2020-06-17 21:54:13 +0700 | [diff] [blame] | 1135 | struct mgcp_response_pending *pending = NULL; |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 1136 | mgcp_trans_id_t trans_id; |
| 1137 | int rc; |
| 1138 | |
| 1139 | trans_id = msg->cb[MSGB_CB_MGCP_TRANS_ID]; |
| 1140 | if (!trans_id) { |
Philipp Maier | df9192e | 2021-09-03 17:50:51 +0200 | [diff] [blame] | 1141 | LOGPMGW(mgcp, LOGL_ERROR, |
| 1142 | "Unset transaction id in mgcp send request\n"); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 1143 | talloc_free(msg); |
| 1144 | return -EINVAL; |
| 1145 | } |
| 1146 | |
Harald Welte | 563ffc5 | 2020-06-17 21:54:13 +0700 | [diff] [blame] | 1147 | /* Do not allocate a dummy 'mgcp_response_pending' entry */ |
| 1148 | if (response_cb != NULL) { |
| 1149 | pending = mgcp_client_pending_add(mgcp, trans_id, response_cb, priv); |
| 1150 | if (!pending) { |
| 1151 | talloc_free(msg); |
| 1152 | return -ENOMEM; |
| 1153 | } |
Harald Welte | 827da2c | 2020-07-15 10:57:08 +0200 | [diff] [blame] | 1154 | } |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 1155 | |
| 1156 | if (msgb_l2len(msg) > 4096) { |
Philipp Maier | df9192e | 2021-09-03 17:50:51 +0200 | [diff] [blame] | 1157 | LOGPMGW(mgcp, LOGL_ERROR, |
| 1158 | "Cannot send, MGCP message too large: %u\n", |
| 1159 | msgb_l2len(msg)); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 1160 | msgb_free(msg); |
| 1161 | rc = -EINVAL; |
| 1162 | goto mgcp_tx_error; |
| 1163 | } |
| 1164 | |
| 1165 | rc = osmo_wqueue_enqueue(&mgcp->wq, msg); |
| 1166 | if (rc) { |
Philipp Maier | df9192e | 2021-09-03 17:50:51 +0200 | [diff] [blame] | 1167 | LOGPMGW(mgcp, LOGL_FATAL, "Could not queue message to MGW\n"); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 1168 | msgb_free(msg); |
| 1169 | goto mgcp_tx_error; |
| 1170 | } else |
Philipp Maier | df9192e | 2021-09-03 17:50:51 +0200 | [diff] [blame] | 1171 | LOGPMGW(mgcp, LOGL_DEBUG, "Queued %u bytes for MGW\n", |
| 1172 | msgb_l2len(msg)); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 1173 | return 0; |
| 1174 | |
| 1175 | mgcp_tx_error: |
Harald Welte | 563ffc5 | 2020-06-17 21:54:13 +0700 | [diff] [blame] | 1176 | if (!pending) |
Vadim Yanitskiy | ffbc618 | 2020-07-16 15:48:13 +0700 | [diff] [blame] | 1177 | return rc; |
Vadim Yanitskiy | 3f8139c | 2020-06-17 20:50:17 +0700 | [diff] [blame] | 1178 | /* Dequeue pending response, it's going to be free()d */ |
| 1179 | llist_del(&pending->entry); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 1180 | /* Pass NULL to response cb to indicate an error */ |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 1181 | mgcp_client_handle_response(mgcp, pending, NULL); |
Vadim Yanitskiy | ffbc618 | 2020-07-16 15:48:13 +0700 | [diff] [blame] | 1182 | return rc; |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 1183 | } |
| 1184 | |
Philipp Maier | 275ac97 | 2018-01-20 00:58:16 +0100 | [diff] [blame] | 1185 | /*! Cancel a pending transaction. |
| 1186 | * \param[in] mgcp MGCP client descriptor. |
| 1187 | * \param[in,out] trans_id Transaction id. |
| 1188 | * \returns 0 on success, -ENOENT on error. |
| 1189 | * |
Neels Hofmeyr | c8f37cb | 2017-11-30 13:43:11 +0100 | [diff] [blame] | 1190 | * Should a priv pointer passed to mgcp_client_tx() become invalid, this function must be called. In |
| 1191 | * practical terms, if the caller of mgcp_client_tx() wishes to tear down a transaction without having |
| 1192 | * received a response this function must be called. The trans_id can be obtained by calling |
Philipp Maier | 275ac97 | 2018-01-20 00:58:16 +0100 | [diff] [blame] | 1193 | * mgcp_msg_trans_id() on the msgb produced by mgcp_msg_gen(). */ |
Neels Hofmeyr | c8f37cb | 2017-11-30 13:43:11 +0100 | [diff] [blame] | 1194 | int mgcp_client_cancel(struct mgcp_client *mgcp, mgcp_trans_id_t trans_id) |
| 1195 | { |
| 1196 | struct mgcp_response_pending *pending = mgcp_client_response_pending_get(mgcp, trans_id); |
| 1197 | if (!pending) { |
Philipp Maier | 275ac97 | 2018-01-20 00:58:16 +0100 | [diff] [blame] | 1198 | /*! Note: it is not harmful to cancel a transaction twice. */ |
Philipp Maier | df9192e | 2021-09-03 17:50:51 +0200 | [diff] [blame] | 1199 | LOGPMGW(mgcp, LOGL_ERROR, "Cannot cancel, no such transaction: %u\n", trans_id); |
Neels Hofmeyr | c8f37cb | 2017-11-30 13:43:11 +0100 | [diff] [blame] | 1200 | return -ENOENT; |
| 1201 | } |
Philipp Maier | df9192e | 2021-09-03 17:50:51 +0200 | [diff] [blame] | 1202 | LOGPMGW(mgcp, LOGL_DEBUG, "Canceled transaction %u\n", trans_id); |
Neels Hofmeyr | c8f37cb | 2017-11-30 13:43:11 +0100 | [diff] [blame] | 1203 | talloc_free(pending); |
| 1204 | return 0; |
Philipp Maier | 275ac97 | 2018-01-20 00:58:16 +0100 | [diff] [blame] | 1205 | /*! We don't really need to clean up the wqueue: In all sane cases, the msgb has already been sent |
| 1206 | * out and is no longer in the wqueue. If it still is in the wqueue, then sending MGCP messages |
| 1207 | * per se is broken and the program should notice so by a full wqueue. Even if this was called |
| 1208 | * before we had a chance to send out the message and it is still going to be sent, we will just |
| 1209 | * ignore the reply to it later. Removing a msgb from the wqueue here would just introduce more |
| 1210 | * bug surface in terms of failing to update wqueue API's counters or some such. |
Neels Hofmeyr | c8f37cb | 2017-11-30 13:43:11 +0100 | [diff] [blame] | 1211 | */ |
| 1212 | } |
| 1213 | |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 1214 | static mgcp_trans_id_t mgcp_client_next_trans_id(struct mgcp_client *mgcp) |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 1215 | { |
| 1216 | /* avoid zero trans_id to distinguish from unset trans_id */ |
| 1217 | if (!mgcp->next_trans_id) |
| 1218 | mgcp->next_trans_id ++; |
| 1219 | return mgcp->next_trans_id ++; |
| 1220 | } |
| 1221 | |
Philipp Maier | 1dc6be6 | 2017-10-05 18:25:37 +0200 | [diff] [blame] | 1222 | #define MGCP_CRCX_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT | \ |
| 1223 | MGCP_MSG_PRESENCE_CALL_ID | \ |
Philipp Maier | 1dc6be6 | 2017-10-05 18:25:37 +0200 | [diff] [blame] | 1224 | MGCP_MSG_PRESENCE_CONN_MODE) |
| 1225 | #define MGCP_MDCX_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT | \ |
Philipp Maier | 490cbaa | 2018-01-22 17:32:38 +0100 | [diff] [blame] | 1226 | MGCP_MSG_PRESENCE_CALL_ID | \ |
Philipp Maier | 1dc6be6 | 2017-10-05 18:25:37 +0200 | [diff] [blame] | 1227 | MGCP_MSG_PRESENCE_CONN_ID) |
| 1228 | #define MGCP_DLCX_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT) |
| 1229 | #define MGCP_AUEP_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT) |
| 1230 | #define MGCP_RSIP_MANDATORY 0 /* none */ |
| 1231 | |
Philipp Maier | 704c4f0 | 2018-06-07 18:51:31 +0200 | [diff] [blame] | 1232 | /* Helper function for mgcp_msg_gen(): Add LCO information to MGCP message */ |
| 1233 | static int add_lco(struct msgb *msg, struct mgcp_msg *mgcp_msg) |
| 1234 | { |
| 1235 | unsigned int i; |
Philipp Maier | 704c4f0 | 2018-06-07 18:51:31 +0200 | [diff] [blame] | 1236 | const char *codec; |
| 1237 | unsigned int pt; |
| 1238 | |
Neels Hofmeyr | 45fe47c | 2023-03-30 16:53:13 +0200 | [diff] [blame] | 1239 | #define MSGB_PRINTF_OR_RET(FMT, ARGS...) do { \ |
| 1240 | if (msgb_printf(msg, FMT, ##ARGS) != 0) { \ |
| 1241 | LOGP(DLMGCP, LOGL_ERROR, "Message buffer too small, can not generate MGCP/SDP message\n"); \ |
| 1242 | return -ENOBUFS; \ |
| 1243 | } \ |
| 1244 | } while (0) |
| 1245 | |
| 1246 | MSGB_PRINTF_OR_RET("L:"); |
Philipp Maier | 704c4f0 | 2018-06-07 18:51:31 +0200 | [diff] [blame] | 1247 | |
| 1248 | if (mgcp_msg->ptime) |
Neels Hofmeyr | 45fe47c | 2023-03-30 16:53:13 +0200 | [diff] [blame] | 1249 | MSGB_PRINTF_OR_RET(" p:%u,", mgcp_msg->ptime); |
Philipp Maier | 704c4f0 | 2018-06-07 18:51:31 +0200 | [diff] [blame] | 1250 | |
| 1251 | if (mgcp_msg->codecs_len) { |
Neels Hofmeyr | 45fe47c | 2023-03-30 16:53:13 +0200 | [diff] [blame] | 1252 | MSGB_PRINTF_OR_RET(" a:"); |
Philipp Maier | 704c4f0 | 2018-06-07 18:51:31 +0200 | [diff] [blame] | 1253 | for (i = 0; i < mgcp_msg->codecs_len; i++) { |
| 1254 | pt = mgcp_msg->codecs[i]; |
Neels Hofmeyr | 47642f2 | 2019-02-27 05:56:53 +0100 | [diff] [blame] | 1255 | codec = get_value_string_or_null(osmo_mgcpc_codec_names, pt); |
Pau Espin Pedrol | c12bfb7 | 2019-04-16 17:23:09 +0200 | [diff] [blame] | 1256 | |
Philipp Maier | 704c4f0 | 2018-06-07 18:51:31 +0200 | [diff] [blame] | 1257 | /* Note: Use codec descriptors from enum mgcp_codecs |
| 1258 | * in mgcp_client only! */ |
Neels Hofmeyr | 45fe47c | 2023-03-30 16:53:13 +0200 | [diff] [blame] | 1259 | if (!codec) |
Philipp Maier | 7d86d4c | 2021-06-11 15:16:13 +0200 | [diff] [blame] | 1260 | return -EINVAL; |
Philipp Maier | 7d86d4c | 2021-06-11 15:16:13 +0200 | [diff] [blame] | 1261 | |
Neels Hofmeyr | 45fe47c | 2023-03-30 16:53:13 +0200 | [diff] [blame] | 1262 | MSGB_PRINTF_OR_RET("%s", extract_codec_name(codec)); |
Philipp Maier | 704c4f0 | 2018-06-07 18:51:31 +0200 | [diff] [blame] | 1263 | if (i < mgcp_msg->codecs_len - 1) |
Neels Hofmeyr | 45fe47c | 2023-03-30 16:53:13 +0200 | [diff] [blame] | 1264 | MSGB_PRINTF_OR_RET(";"); |
Philipp Maier | 704c4f0 | 2018-06-07 18:51:31 +0200 | [diff] [blame] | 1265 | } |
Neels Hofmeyr | 45fe47c | 2023-03-30 16:53:13 +0200 | [diff] [blame] | 1266 | MSGB_PRINTF_OR_RET(","); |
Philipp Maier | 704c4f0 | 2018-06-07 18:51:31 +0200 | [diff] [blame] | 1267 | } |
| 1268 | |
Neels Hofmeyr | 45fe47c | 2023-03-30 16:53:13 +0200 | [diff] [blame] | 1269 | MSGB_PRINTF_OR_RET(" nt:IN\r\n"); |
Philipp Maier | 7d86d4c | 2021-06-11 15:16:13 +0200 | [diff] [blame] | 1270 | return 0; |
Neels Hofmeyr | 45fe47c | 2023-03-30 16:53:13 +0200 | [diff] [blame] | 1271 | |
| 1272 | #undef MSGB_PRINTF_OR_RET |
Philipp Maier | 704c4f0 | 2018-06-07 18:51:31 +0200 | [diff] [blame] | 1273 | } |
| 1274 | |
| 1275 | /* Helper function for mgcp_msg_gen(): Add SDP information to MGCP message */ |
| 1276 | static int add_sdp(struct msgb *msg, struct mgcp_msg *mgcp_msg, struct mgcp_client *mgcp) |
| 1277 | { |
| 1278 | unsigned int i; |
Pau Espin Pedrol | 8667d51 | 2020-08-28 19:37:08 +0200 | [diff] [blame] | 1279 | char local_ip[INET6_ADDRSTRLEN]; |
Pau Espin Pedrol | 9dc7359 | 2020-08-28 20:21:55 +0200 | [diff] [blame] | 1280 | int local_ip_family, audio_ip_family; |
Philipp Maier | 704c4f0 | 2018-06-07 18:51:31 +0200 | [diff] [blame] | 1281 | const char *codec; |
| 1282 | unsigned int pt; |
| 1283 | |
Neels Hofmeyr | 45fe47c | 2023-03-30 16:53:13 +0200 | [diff] [blame] | 1284 | #define MSGB_PRINTF_OR_RET(FMT, ARGS...) do { \ |
| 1285 | if (msgb_printf(msg, FMT, ##ARGS) != 0) { \ |
| 1286 | LOGPMGW(mgcp, LOGL_ERROR, "Message buffer too small, can not generate MGCP message (SDP)\n"); \ |
| 1287 | return -ENOBUFS; \ |
| 1288 | } \ |
| 1289 | } while (0) |
| 1290 | |
Philipp Maier | 704c4f0 | 2018-06-07 18:51:31 +0200 | [diff] [blame] | 1291 | /* Add separator to mark the beginning of the SDP block */ |
Neels Hofmeyr | 45fe47c | 2023-03-30 16:53:13 +0200 | [diff] [blame] | 1292 | MSGB_PRINTF_OR_RET("\r\n"); |
Philipp Maier | 704c4f0 | 2018-06-07 18:51:31 +0200 | [diff] [blame] | 1293 | |
| 1294 | /* Add SDP protocol version */ |
Neels Hofmeyr | 45fe47c | 2023-03-30 16:53:13 +0200 | [diff] [blame] | 1295 | MSGB_PRINTF_OR_RET("v=0\r\n"); |
Philipp Maier | 704c4f0 | 2018-06-07 18:51:31 +0200 | [diff] [blame] | 1296 | |
| 1297 | /* Determine local IP-Address */ |
| 1298 | if (osmo_sock_local_ip(local_ip, mgcp->actual.remote_addr) < 0) { |
Philipp Maier | df9192e | 2021-09-03 17:50:51 +0200 | [diff] [blame] | 1299 | LOGPMGW(mgcp, LOGL_ERROR, |
| 1300 | "Could not determine local IP-Address!\n"); |
Philipp Maier | 7d86d4c | 2021-06-11 15:16:13 +0200 | [diff] [blame] | 1301 | return -EINVAL; |
Philipp Maier | 704c4f0 | 2018-06-07 18:51:31 +0200 | [diff] [blame] | 1302 | } |
Pau Espin Pedrol | 9dc7359 | 2020-08-28 20:21:55 +0200 | [diff] [blame] | 1303 | local_ip_family = osmo_ip_str_type(local_ip); |
Neels Hofmeyr | 45fe47c | 2023-03-30 16:53:13 +0200 | [diff] [blame] | 1304 | if (local_ip_family == AF_UNSPEC) |
Philipp Maier | 7d86d4c | 2021-06-11 15:16:13 +0200 | [diff] [blame] | 1305 | return -EINVAL; |
Pau Espin Pedrol | 9dc7359 | 2020-08-28 20:21:55 +0200 | [diff] [blame] | 1306 | audio_ip_family = osmo_ip_str_type(mgcp_msg->audio_ip); |
Neels Hofmeyr | 45fe47c | 2023-03-30 16:53:13 +0200 | [diff] [blame] | 1307 | if (audio_ip_family == AF_UNSPEC) |
Philipp Maier | 7d86d4c | 2021-06-11 15:16:13 +0200 | [diff] [blame] | 1308 | return -EINVAL; |
Philipp Maier | 704c4f0 | 2018-06-07 18:51:31 +0200 | [diff] [blame] | 1309 | |
| 1310 | /* Add owner/creator (SDP) */ |
Neels Hofmeyr | 45fe47c | 2023-03-30 16:53:13 +0200 | [diff] [blame] | 1311 | MSGB_PRINTF_OR_RET("o=- %x 23 IN IP%c %s\r\n", mgcp_msg->call_id, |
Pau Espin Pedrol | 9dc7359 | 2020-08-28 20:21:55 +0200 | [diff] [blame] | 1312 | local_ip_family == AF_INET6 ? '6' : '4', |
| 1313 | local_ip); |
Philipp Maier | 704c4f0 | 2018-06-07 18:51:31 +0200 | [diff] [blame] | 1314 | |
| 1315 | /* Add session name (none) */ |
Neels Hofmeyr | 45fe47c | 2023-03-30 16:53:13 +0200 | [diff] [blame] | 1316 | MSGB_PRINTF_OR_RET("s=-\r\n"); |
Philipp Maier | 704c4f0 | 2018-06-07 18:51:31 +0200 | [diff] [blame] | 1317 | |
| 1318 | /* Add RTP address and port */ |
| 1319 | if (mgcp_msg->audio_port == 0) { |
Philipp Maier | df9192e | 2021-09-03 17:50:51 +0200 | [diff] [blame] | 1320 | LOGPMGW(mgcp, LOGL_ERROR, |
| 1321 | "Invalid port number, can not generate MGCP message\n"); |
Philipp Maier | 704c4f0 | 2018-06-07 18:51:31 +0200 | [diff] [blame] | 1322 | msgb_free(msg); |
Philipp Maier | 7d86d4c | 2021-06-11 15:16:13 +0200 | [diff] [blame] | 1323 | return -EINVAL; |
Philipp Maier | 704c4f0 | 2018-06-07 18:51:31 +0200 | [diff] [blame] | 1324 | } |
| 1325 | if (strlen(mgcp_msg->audio_ip) <= 0) { |
Philipp Maier | df9192e | 2021-09-03 17:50:51 +0200 | [diff] [blame] | 1326 | LOGPMGW(mgcp, LOGL_ERROR, |
| 1327 | "Empty ip address, can not generate MGCP message\n"); |
Philipp Maier | 704c4f0 | 2018-06-07 18:51:31 +0200 | [diff] [blame] | 1328 | msgb_free(msg); |
Philipp Maier | 7d86d4c | 2021-06-11 15:16:13 +0200 | [diff] [blame] | 1329 | return -EINVAL; |
Philipp Maier | 704c4f0 | 2018-06-07 18:51:31 +0200 | [diff] [blame] | 1330 | } |
Neels Hofmeyr | 45fe47c | 2023-03-30 16:53:13 +0200 | [diff] [blame] | 1331 | MSGB_PRINTF_OR_RET("c=IN IP%c %s\r\n", |
Pau Espin Pedrol | 9dc7359 | 2020-08-28 20:21:55 +0200 | [diff] [blame] | 1332 | audio_ip_family == AF_INET6 ? '6' : '4', |
| 1333 | mgcp_msg->audio_ip); |
Philipp Maier | 704c4f0 | 2018-06-07 18:51:31 +0200 | [diff] [blame] | 1334 | |
| 1335 | /* Add time description, active time (SDP) */ |
Neels Hofmeyr | 45fe47c | 2023-03-30 16:53:13 +0200 | [diff] [blame] | 1336 | MSGB_PRINTF_OR_RET("t=0 0\r\n"); |
Philipp Maier | 704c4f0 | 2018-06-07 18:51:31 +0200 | [diff] [blame] | 1337 | |
Neels Hofmeyr | 45fe47c | 2023-03-30 16:53:13 +0200 | [diff] [blame] | 1338 | MSGB_PRINTF_OR_RET("m=audio %u RTP/AVP", mgcp_msg->audio_port); |
Philipp Maier | 704c4f0 | 2018-06-07 18:51:31 +0200 | [diff] [blame] | 1339 | for (i = 0; i < mgcp_msg->codecs_len; i++) { |
| 1340 | pt = map_codec_to_pt(mgcp_msg->ptmap, mgcp_msg->ptmap_len, mgcp_msg->codecs[i]); |
Neels Hofmeyr | 45fe47c | 2023-03-30 16:53:13 +0200 | [diff] [blame] | 1341 | MSGB_PRINTF_OR_RET(" %u", pt); |
Philipp Maier | 704c4f0 | 2018-06-07 18:51:31 +0200 | [diff] [blame] | 1342 | |
| 1343 | } |
Neels Hofmeyr | 45fe47c | 2023-03-30 16:53:13 +0200 | [diff] [blame] | 1344 | MSGB_PRINTF_OR_RET("\r\n"); |
Philipp Maier | 704c4f0 | 2018-06-07 18:51:31 +0200 | [diff] [blame] | 1345 | |
Philipp Maier | 228e591 | 2019-03-05 13:56:59 +0100 | [diff] [blame] | 1346 | /* Add optional codec parameters (fmtp) */ |
| 1347 | if (mgcp_msg->param_present) { |
| 1348 | for (i = 0; i < mgcp_msg->codecs_len; i++) { |
| 1349 | /* The following is only applicable for AMR */ |
| 1350 | if (mgcp_msg->codecs[i] != CODEC_AMR_8000_1 && mgcp_msg->codecs[i] != CODEC_AMRWB_16000_1) |
| 1351 | continue; |
| 1352 | pt = map_codec_to_pt(mgcp_msg->ptmap, mgcp_msg->ptmap_len, mgcp_msg->codecs[i]); |
| 1353 | if (mgcp_msg->param.amr_octet_aligned_present && mgcp_msg->param.amr_octet_aligned) |
Neels Hofmeyr | 45fe47c | 2023-03-30 16:53:13 +0200 | [diff] [blame] | 1354 | MSGB_PRINTF_OR_RET("a=fmtp:%u octet-align=1\r\n", pt); |
Philipp Maier | 228e591 | 2019-03-05 13:56:59 +0100 | [diff] [blame] | 1355 | else if (mgcp_msg->param.amr_octet_aligned_present && !mgcp_msg->param.amr_octet_aligned) |
Neels Hofmeyr | 45fe47c | 2023-03-30 16:53:13 +0200 | [diff] [blame] | 1356 | MSGB_PRINTF_OR_RET("a=fmtp:%u octet-align=0\r\n", pt); |
Philipp Maier | 228e591 | 2019-03-05 13:56:59 +0100 | [diff] [blame] | 1357 | } |
| 1358 | } |
| 1359 | |
Philipp Maier | 704c4f0 | 2018-06-07 18:51:31 +0200 | [diff] [blame] | 1360 | for (i = 0; i < mgcp_msg->codecs_len; i++) { |
| 1361 | pt = map_codec_to_pt(mgcp_msg->ptmap, mgcp_msg->ptmap_len, mgcp_msg->codecs[i]); |
Pau Espin Pedrol | c12bfb7 | 2019-04-16 17:23:09 +0200 | [diff] [blame] | 1362 | |
Philipp Maier | 704c4f0 | 2018-06-07 18:51:31 +0200 | [diff] [blame] | 1363 | /* Note: Only dynamic payload type from the range 96-127 |
| 1364 | * require to be explained further via rtpmap. All others |
| 1365 | * are implcitly definedby the number in m=audio */ |
| 1366 | if (pt >= 96 && pt <= 127) { |
Neels Hofmeyr | 47642f2 | 2019-02-27 05:56:53 +0100 | [diff] [blame] | 1367 | codec = get_value_string_or_null(osmo_mgcpc_codec_names, mgcp_msg->codecs[i]); |
Philipp Maier | 704c4f0 | 2018-06-07 18:51:31 +0200 | [diff] [blame] | 1368 | |
| 1369 | /* Note: Use codec descriptors from enum mgcp_codecs |
| 1370 | * in mgcp_client only! */ |
Neels Hofmeyr | 45fe47c | 2023-03-30 16:53:13 +0200 | [diff] [blame] | 1371 | if (!codec) |
Philipp Maier | 7d86d4c | 2021-06-11 15:16:13 +0200 | [diff] [blame] | 1372 | return -EINVAL; |
Pau Espin Pedrol | c12bfb7 | 2019-04-16 17:23:09 +0200 | [diff] [blame] | 1373 | |
Neels Hofmeyr | 45fe47c | 2023-03-30 16:53:13 +0200 | [diff] [blame] | 1374 | MSGB_PRINTF_OR_RET("a=rtpmap:%u %s\r\n", pt, codec); |
Philipp Maier | 704c4f0 | 2018-06-07 18:51:31 +0200 | [diff] [blame] | 1375 | } |
| 1376 | } |
Pau Espin Pedrol | c12bfb7 | 2019-04-16 17:23:09 +0200 | [diff] [blame] | 1377 | |
Philipp Maier | 704c4f0 | 2018-06-07 18:51:31 +0200 | [diff] [blame] | 1378 | if (mgcp_msg->ptime) |
Neels Hofmeyr | 45fe47c | 2023-03-30 16:53:13 +0200 | [diff] [blame] | 1379 | MSGB_PRINTF_OR_RET("a=ptime:%u\r\n", mgcp_msg->ptime); |
Philipp Maier | 7d86d4c | 2021-06-11 15:16:13 +0200 | [diff] [blame] | 1380 | |
| 1381 | return 0; |
Neels Hofmeyr | 45fe47c | 2023-03-30 16:53:13 +0200 | [diff] [blame] | 1382 | #undef MSGB_PRINTF_OR_RET |
Philipp Maier | 704c4f0 | 2018-06-07 18:51:31 +0200 | [diff] [blame] | 1383 | } |
| 1384 | |
Philipp Maier | 275ac97 | 2018-01-20 00:58:16 +0100 | [diff] [blame] | 1385 | /*! Generate an MGCP message |
| 1386 | * \param[in] mgcp MGCP client descriptor. |
| 1387 | * \param[in] mgcp_msg Message description |
| 1388 | * \returns message buffer on success, NULL on error. */ |
Philipp Maier | 1dc6be6 | 2017-10-05 18:25:37 +0200 | [diff] [blame] | 1389 | struct msgb *mgcp_msg_gen(struct mgcp_client *mgcp, struct mgcp_msg *mgcp_msg) |
| 1390 | { |
| 1391 | mgcp_trans_id_t trans_id = mgcp_client_next_trans_id(mgcp); |
| 1392 | uint32_t mandatory_mask; |
| 1393 | struct msgb *msg = msgb_alloc_headroom(4096, 128, "MGCP tx"); |
Philipp Maier | 704c4f0 | 2018-06-07 18:51:31 +0200 | [diff] [blame] | 1394 | bool use_sdp = false; |
Pau Espin Pedrol | 900cd65 | 2019-04-24 22:06:22 +0200 | [diff] [blame] | 1395 | char buf[32]; |
Philipp Maier | 1dc6be6 | 2017-10-05 18:25:37 +0200 | [diff] [blame] | 1396 | |
Neels Hofmeyr | 45fe47c | 2023-03-30 16:53:13 +0200 | [diff] [blame] | 1397 | #define MSGB_PRINTF_OR_RET(FMT, ARGS...) do { \ |
| 1398 | if (msgb_printf(msg, FMT, ##ARGS) != 0) { \ |
| 1399 | LOGPMGW(mgcp, LOGL_ERROR, "Message buffer too small, can not generate MGCP/SDP message\n"); \ |
| 1400 | goto exit_error; \ |
| 1401 | } \ |
| 1402 | } while (0) |
| 1403 | |
Philipp Maier | 1dc6be6 | 2017-10-05 18:25:37 +0200 | [diff] [blame] | 1404 | msg->l2h = msg->data; |
| 1405 | msg->cb[MSGB_CB_MGCP_TRANS_ID] = trans_id; |
| 1406 | |
| 1407 | /* Add command verb */ |
| 1408 | switch (mgcp_msg->verb) { |
| 1409 | case MGCP_VERB_CRCX: |
| 1410 | mandatory_mask = MGCP_CRCX_MANDATORY; |
Neels Hofmeyr | 45fe47c | 2023-03-30 16:53:13 +0200 | [diff] [blame] | 1411 | MSGB_PRINTF_OR_RET("CRCX %u", trans_id); |
Philipp Maier | 1dc6be6 | 2017-10-05 18:25:37 +0200 | [diff] [blame] | 1412 | break; |
| 1413 | case MGCP_VERB_MDCX: |
| 1414 | mandatory_mask = MGCP_MDCX_MANDATORY; |
Neels Hofmeyr | 45fe47c | 2023-03-30 16:53:13 +0200 | [diff] [blame] | 1415 | MSGB_PRINTF_OR_RET("MDCX %u", trans_id); |
Philipp Maier | 1dc6be6 | 2017-10-05 18:25:37 +0200 | [diff] [blame] | 1416 | break; |
| 1417 | case MGCP_VERB_DLCX: |
| 1418 | mandatory_mask = MGCP_DLCX_MANDATORY; |
Neels Hofmeyr | 45fe47c | 2023-03-30 16:53:13 +0200 | [diff] [blame] | 1419 | MSGB_PRINTF_OR_RET("DLCX %u", trans_id); |
Philipp Maier | 1dc6be6 | 2017-10-05 18:25:37 +0200 | [diff] [blame] | 1420 | break; |
| 1421 | case MGCP_VERB_AUEP: |
| 1422 | mandatory_mask = MGCP_AUEP_MANDATORY; |
Neels Hofmeyr | 45fe47c | 2023-03-30 16:53:13 +0200 | [diff] [blame] | 1423 | MSGB_PRINTF_OR_RET("AUEP %u", trans_id); |
Philipp Maier | 1dc6be6 | 2017-10-05 18:25:37 +0200 | [diff] [blame] | 1424 | break; |
| 1425 | case MGCP_VERB_RSIP: |
| 1426 | mandatory_mask = MGCP_RSIP_MANDATORY; |
Neels Hofmeyr | 45fe47c | 2023-03-30 16:53:13 +0200 | [diff] [blame] | 1427 | MSGB_PRINTF_OR_RET("RSIP %u", trans_id); |
Philipp Maier | 1dc6be6 | 2017-10-05 18:25:37 +0200 | [diff] [blame] | 1428 | break; |
| 1429 | default: |
Philipp Maier | df9192e | 2021-09-03 17:50:51 +0200 | [diff] [blame] | 1430 | LOGPMGW(mgcp, LOGL_ERROR, "Invalid command verb, can not generate MGCP message\n"); |
Neels Hofmeyr | 45fe47c | 2023-03-30 16:53:13 +0200 | [diff] [blame] | 1431 | goto exit_error; |
Philipp Maier | 1dc6be6 | 2017-10-05 18:25:37 +0200 | [diff] [blame] | 1432 | } |
| 1433 | |
| 1434 | /* Check if mandatory fields are missing */ |
| 1435 | if (!((mgcp_msg->presence & mandatory_mask) == mandatory_mask)) { |
Philipp Maier | df9192e | 2021-09-03 17:50:51 +0200 | [diff] [blame] | 1436 | LOGPMGW(mgcp, LOGL_ERROR, |
| 1437 | "One or more missing mandatory fields, can not generate MGCP message\n"); |
Neels Hofmeyr | 45fe47c | 2023-03-30 16:53:13 +0200 | [diff] [blame] | 1438 | goto exit_error; |
Philipp Maier | 1dc6be6 | 2017-10-05 18:25:37 +0200 | [diff] [blame] | 1439 | } |
| 1440 | |
| 1441 | /* Add endpoint name */ |
Philipp Maier | 7bc5552 | 2017-12-10 22:52:22 +0100 | [diff] [blame] | 1442 | if (mgcp_msg->presence & MGCP_MSG_PRESENCE_ENDPOINT) { |
| 1443 | if (strlen(mgcp_msg->endpoint) <= 0) { |
Philipp Maier | df9192e | 2021-09-03 17:50:51 +0200 | [diff] [blame] | 1444 | LOGPMGW(mgcp, LOGL_ERROR, "Empty endpoint name, can not generate MGCP message\n"); |
Neels Hofmeyr | 45fe47c | 2023-03-30 16:53:13 +0200 | [diff] [blame] | 1445 | goto exit_error; |
Philipp Maier | 7bc5552 | 2017-12-10 22:52:22 +0100 | [diff] [blame] | 1446 | } |
Philipp Maier | 3aa8157 | 2018-01-31 14:13:45 +0100 | [diff] [blame] | 1447 | |
| 1448 | if (strstr(mgcp_msg->endpoint, "@") == NULL) { |
Philipp Maier | df9192e | 2021-09-03 17:50:51 +0200 | [diff] [blame] | 1449 | LOGPMGW(mgcp, LOGL_ERROR, |
| 1450 | "Endpoint name (%s) lacks separator (@), can not generate MGCP message\n", |
| 1451 | mgcp_msg->endpoint); |
Neels Hofmeyr | 45fe47c | 2023-03-30 16:53:13 +0200 | [diff] [blame] | 1452 | goto exit_error; |
Philipp Maier | 3aa8157 | 2018-01-31 14:13:45 +0100 | [diff] [blame] | 1453 | } |
| 1454 | |
Neels Hofmeyr | 45fe47c | 2023-03-30 16:53:13 +0200 | [diff] [blame] | 1455 | MSGB_PRINTF_OR_RET(" %s", mgcp_msg->endpoint); |
Philipp Maier | 7bc5552 | 2017-12-10 22:52:22 +0100 | [diff] [blame] | 1456 | } |
Philipp Maier | 1dc6be6 | 2017-10-05 18:25:37 +0200 | [diff] [blame] | 1457 | |
| 1458 | /* Add protocol version */ |
Neels Hofmeyr | 45fe47c | 2023-03-30 16:53:13 +0200 | [diff] [blame] | 1459 | MSGB_PRINTF_OR_RET(" MGCP 1.0\r\n"); |
Philipp Maier | 1dc6be6 | 2017-10-05 18:25:37 +0200 | [diff] [blame] | 1460 | |
| 1461 | /* Add call id */ |
| 1462 | if (mgcp_msg->presence & MGCP_MSG_PRESENCE_CALL_ID) |
Neels Hofmeyr | 45fe47c | 2023-03-30 16:53:13 +0200 | [diff] [blame] | 1463 | MSGB_PRINTF_OR_RET("C: %x\r\n", mgcp_msg->call_id); |
Philipp Maier | 1dc6be6 | 2017-10-05 18:25:37 +0200 | [diff] [blame] | 1464 | |
| 1465 | /* Add connection id */ |
Philipp Maier | 7bc5552 | 2017-12-10 22:52:22 +0100 | [diff] [blame] | 1466 | if (mgcp_msg->presence & MGCP_MSG_PRESENCE_CONN_ID) { |
| 1467 | if (strlen(mgcp_msg->conn_id) <= 0) { |
Philipp Maier | df9192e | 2021-09-03 17:50:51 +0200 | [diff] [blame] | 1468 | LOGPMGW(mgcp, LOGL_ERROR, "Empty connection id, can not generate MGCP message\n"); |
Neels Hofmeyr | 45fe47c | 2023-03-30 16:53:13 +0200 | [diff] [blame] | 1469 | goto exit_error; |
Philipp Maier | 7bc5552 | 2017-12-10 22:52:22 +0100 | [diff] [blame] | 1470 | } |
Neels Hofmeyr | 45fe47c | 2023-03-30 16:53:13 +0200 | [diff] [blame] | 1471 | MSGB_PRINTF_OR_RET("I: %s\r\n", mgcp_msg->conn_id); |
Philipp Maier | 7bc5552 | 2017-12-10 22:52:22 +0100 | [diff] [blame] | 1472 | } |
Philipp Maier | 1dc6be6 | 2017-10-05 18:25:37 +0200 | [diff] [blame] | 1473 | |
Oliver Smith | a312523 | 2023-04-18 16:11:03 +0200 | [diff] [blame] | 1474 | /* Using SDP makes sense when a valid IP/Port combination is specified, |
Philipp Maier | 704c4f0 | 2018-06-07 18:51:31 +0200 | [diff] [blame] | 1475 | * if we do not know this information yet, we fall back to LCO */ |
| 1476 | if (mgcp_msg->presence & MGCP_MSG_PRESENCE_AUDIO_IP |
| 1477 | && mgcp_msg->presence & MGCP_MSG_PRESENCE_AUDIO_PORT) |
| 1478 | use_sdp = true; |
| 1479 | |
| 1480 | /* Add local connection options (LCO) */ |
| 1481 | if (!use_sdp |
| 1482 | && (mgcp_msg->verb == MGCP_VERB_CRCX |
Philipp Maier | 7d86d4c | 2021-06-11 15:16:13 +0200 | [diff] [blame] | 1483 | || mgcp_msg->verb == MGCP_VERB_MDCX)) { |
Oliver Smith | 0dbaaad | 2023-02-22 16:30:02 +0100 | [diff] [blame] | 1484 | if (add_lco(msg, mgcp_msg) < 0) { |
| 1485 | LOGPMGW(mgcp, LOGL_ERROR, "Failed to add LCO, can not generate MGCP message\n"); |
Neels Hofmeyr | 45fe47c | 2023-03-30 16:53:13 +0200 | [diff] [blame] | 1486 | goto exit_error; |
Oliver Smith | 0dbaaad | 2023-02-22 16:30:02 +0100 | [diff] [blame] | 1487 | } |
Philipp Maier | 7d86d4c | 2021-06-11 15:16:13 +0200 | [diff] [blame] | 1488 | } |
Philipp Maier | 1dc6be6 | 2017-10-05 18:25:37 +0200 | [diff] [blame] | 1489 | |
| 1490 | /* Add mode */ |
| 1491 | if (mgcp_msg->presence & MGCP_MSG_PRESENCE_CONN_MODE) |
Neels Hofmeyr | 45fe47c | 2023-03-30 16:53:13 +0200 | [diff] [blame] | 1492 | MSGB_PRINTF_OR_RET("M: %s\r\n", mgcp_client_cmode_name(mgcp_msg->conn_mode)); |
Philipp Maier | 1dc6be6 | 2017-10-05 18:25:37 +0200 | [diff] [blame] | 1493 | |
Neels Hofmeyr | e6d8e91 | 2018-08-23 16:36:48 +0200 | [diff] [blame] | 1494 | /* Add X-Osmo-IGN */ |
| 1495 | if ((mgcp_msg->presence & MGCP_MSG_PRESENCE_X_OSMO_IGN) |
| 1496 | && (mgcp_msg->x_osmo_ign != 0)) |
Neels Hofmeyr | 45fe47c | 2023-03-30 16:53:13 +0200 | [diff] [blame] | 1497 | MSGB_PRINTF_OR_RET(MGCP_X_OSMO_IGN_HEADER "%s\r\n", |
| 1498 | mgcp_msg->x_osmo_ign & MGCP_X_OSMO_IGN_CALLID ? " C" : ""); |
Neels Hofmeyr | e6d8e91 | 2018-08-23 16:36:48 +0200 | [diff] [blame] | 1499 | |
Pau Espin Pedrol | 900cd65 | 2019-04-24 22:06:22 +0200 | [diff] [blame] | 1500 | /* Add X-Osmo-Osmux */ |
| 1501 | if ((mgcp_msg->presence & MGCP_MSG_PRESENCE_X_OSMO_OSMUX_CID)) { |
Pau Espin Pedrol | 1b1d7ed | 2019-05-23 16:48:24 +0200 | [diff] [blame] | 1502 | if (mgcp_msg->x_osmo_osmux_cid < -1 || mgcp_msg->x_osmo_osmux_cid > OSMUX_CID_MAX) { |
Philipp Maier | df9192e | 2021-09-03 17:50:51 +0200 | [diff] [blame] | 1503 | LOGPMGW(mgcp, LOGL_ERROR, "Wrong Osmux CID %d, can not generate MGCP message\n", |
| 1504 | mgcp_msg->x_osmo_osmux_cid); |
Neels Hofmeyr | 45fe47c | 2023-03-30 16:53:13 +0200 | [diff] [blame] | 1505 | goto exit_error; |
Pau Espin Pedrol | 1b1d7ed | 2019-05-23 16:48:24 +0200 | [diff] [blame] | 1506 | } |
Pau Espin Pedrol | 900cd65 | 2019-04-24 22:06:22 +0200 | [diff] [blame] | 1507 | snprintf(buf, sizeof(buf), " %d", mgcp_msg->x_osmo_osmux_cid); |
Neels Hofmeyr | 45fe47c | 2023-03-30 16:53:13 +0200 | [diff] [blame] | 1508 | MSGB_PRINTF_OR_RET(MGCP_X_OSMO_OSMUX_HEADER "%s\r\n", |
| 1509 | mgcp_msg->x_osmo_osmux_cid == -1 ? " *" : buf); |
Pau Espin Pedrol | 900cd65 | 2019-04-24 22:06:22 +0200 | [diff] [blame] | 1510 | } |
| 1511 | |
| 1512 | |
Philipp Maier | 704c4f0 | 2018-06-07 18:51:31 +0200 | [diff] [blame] | 1513 | /* Add session description protocol (SDP) */ |
| 1514 | if (use_sdp |
| 1515 | && (mgcp_msg->verb == MGCP_VERB_CRCX |
| 1516 | || mgcp_msg->verb == MGCP_VERB_MDCX)) { |
Oliver Smith | 0dbaaad | 2023-02-22 16:30:02 +0100 | [diff] [blame] | 1517 | if (add_sdp(msg, mgcp_msg, mgcp) < 0) { |
| 1518 | LOGPMGW(mgcp, LOGL_ERROR, "Failed to add SDP, can not generate MGCP message\n"); |
Neels Hofmeyr | 45fe47c | 2023-03-30 16:53:13 +0200 | [diff] [blame] | 1519 | goto exit_error; |
Oliver Smith | 0dbaaad | 2023-02-22 16:30:02 +0100 | [diff] [blame] | 1520 | } |
Philipp Maier | 1dc6be6 | 2017-10-05 18:25:37 +0200 | [diff] [blame] | 1521 | } |
| 1522 | |
Philipp Maier | 1dc6be6 | 2017-10-05 18:25:37 +0200 | [diff] [blame] | 1523 | return msg; |
Neels Hofmeyr | 45fe47c | 2023-03-30 16:53:13 +0200 | [diff] [blame] | 1524 | |
| 1525 | exit_error: |
| 1526 | msgb_free(msg); |
| 1527 | return NULL; |
| 1528 | #undef MSGB_PRINTF_OR_RET |
Philipp Maier | 1dc6be6 | 2017-10-05 18:25:37 +0200 | [diff] [blame] | 1529 | } |
| 1530 | |
Philipp Maier | 275ac97 | 2018-01-20 00:58:16 +0100 | [diff] [blame] | 1531 | /*! Retrieve the MGCP transaction ID from a msgb generated by mgcp_msg_gen() |
| 1532 | * \param[in] msg message buffer |
| 1533 | * \returns Transaction id. */ |
Neels Hofmeyr | c8f37cb | 2017-11-30 13:43:11 +0100 | [diff] [blame] | 1534 | mgcp_trans_id_t mgcp_msg_trans_id(struct msgb *msg) |
| 1535 | { |
| 1536 | return (mgcp_trans_id_t)msg->cb[MSGB_CB_MGCP_TRANS_ID]; |
| 1537 | } |
| 1538 | |
Philipp Maier | fa495d6 | 2021-09-02 10:08:56 +0200 | [diff] [blame] | 1539 | /*! Get the configuration parameters for a given MGCP client instance |
Philipp Maier | 275ac97 | 2018-01-20 00:58:16 +0100 | [diff] [blame] | 1540 | * \param[in] mgcp MGCP client descriptor. |
| 1541 | * \returns configuration */ |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 1542 | struct mgcp_client_conf *mgcp_client_conf_actual(struct mgcp_client *mgcp) |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 1543 | { |
| 1544 | return &mgcp->actual; |
| 1545 | } |
Neels Hofmeyr | d95ab1e | 2017-09-22 00:52:54 +0200 | [diff] [blame] | 1546 | |
| 1547 | const struct value_string mgcp_client_connection_mode_strs[] = { |
| 1548 | { MGCP_CONN_NONE, "none" }, |
| 1549 | { MGCP_CONN_RECV_SEND, "sendrecv" }, |
| 1550 | { MGCP_CONN_SEND_ONLY, "sendonly" }, |
| 1551 | { MGCP_CONN_RECV_ONLY, "recvonly" }, |
| 1552 | { MGCP_CONN_LOOPBACK, "loopback" }, |
| 1553 | { 0, NULL } |
| 1554 | }; |
Philipp Maier | df9192e | 2021-09-03 17:50:51 +0200 | [diff] [blame] | 1555 | |
| 1556 | /*! Get MGCP client instance name (VTY). |
| 1557 | * \param[in] mgcp MGCP client descriptor. |
| 1558 | * \returns MGCP client name. |
| 1559 | * |
| 1560 | * The user can only modify the name of an MGCP client instance when it is |
| 1561 | * part of a pool. For single MGCP client instances and MGCP client instance |
| 1562 | * where no description is set via the VTY, the MGW domain name will be used |
| 1563 | * as name. */ |
| 1564 | const char *mgcp_client_name(const struct mgcp_client *mgcp) |
| 1565 | { |
| 1566 | if (!mgcp) |
| 1567 | return "(null)"; |
| 1568 | |
| 1569 | if (mgcp->actual.description) |
| 1570 | return mgcp->actual.description; |
| 1571 | else |
| 1572 | return mgcp_client_endpoint_domain(mgcp); |
| 1573 | } |