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