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