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