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