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