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