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