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