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