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