blob: 489ce69426f04373e29aa97a046834db63779a4b [file] [log] [blame]
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001/* 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 Welte220f4872018-02-04 09:04:16 +01007 * 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 Hofmeyre9920f22017-07-10 15:07:22 +02009 * (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 Welte220f4872018-02-04 09:04:16 +010014 * GNU General Public License for more details.
Neels Hofmeyre9920f22017-07-10 15:07:22 +020015 *
Harald Welte220f4872018-02-04 09:04:16 +010016 * You should have received a copy of the GNU General Public License
Neels Hofmeyre9920f22017-07-10 15:07:22 +020017 * 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 Maier1dc6be62017-10-05 18:25:37 +020026#include <osmocom/core/byteswap.h>
Harald Welte8890dfa2017-11-17 15:09:30 +010027#include <osmocom/core/socket.h>
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +020028#include <osmocom/core/sockaddr_str.h>
Neels Hofmeyre9920f22017-07-10 15:07:22 +020029
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +020030#include <osmocom/mgcp_client/mgcp_client.h>
31#include <osmocom/mgcp_client/mgcp_client_internal.h>
Neels Hofmeyre9920f22017-07-10 15:07:22 +020032
Philipp Maier92a73cd2020-11-26 22:21:11 +010033#include <osmocom/abis/e1_input.h>
34
Neels Hofmeyre9920f22017-07-10 15:07:22 +020035#include <netinet/in.h>
36#include <arpa/inet.h>
37
38#include <errno.h>
39#include <unistd.h>
40#include <string.h>
Pau Espin Pedrol166077e2019-06-26 12:21:38 +020041#include <ctype.h>
Pau Espin Pedrolc5c14302019-07-23 16:19:41 +020042#include <stdlib.h>
43#include <limits.h>
Neels Hofmeyre9920f22017-07-10 15:07:22 +020044
Pau Espin Pedrol1b1d7ed2019-05-23 16:48:24 +020045#ifndef OSMUX_CID_MAX
46#define OSMUX_CID_MAX 255 /* FIXME: use OSMUX_CID_MAX from libosmo-netif? */
47#endif
48
Philipp Maierdf9192e2021-09-03 17:50:51 +020049#define LOGPMGW(mgcp, level, fmt, args...) \
50LOGP(DLMGCP, level, "MGW(%s) " fmt, mgcp_client_name(mgcp), ## args)
51
Philipp Maier704c4f02018-06-07 18:51:31 +020052/* Codec descripton for dynamic payload types (SDP) */
Neels Hofmeyr47642f22019-02-27 05:56:53 +010053const struct value_string osmo_mgcpc_codec_names[] = {
Philipp Maier704c4f02018-06-07 18:51:31 +020054 { 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 Maier1de5ed62021-12-21 14:38:14 +010062 { CODEC_IUFP, "VND.3GPP.IUFP/16000" },
Oliver Smith26d6b2b2023-01-24 13:04:47 +010063 { CODEC_CLEARMODE, "CLEARMODE/8000" },
Philipp Maier704c4f02018-06-07 18:51:31 +020064 { 0, NULL },
65};
66
67/* Get encoding name from a full codec string e,g.
68 * ("CODEC/8000/2" => returns "CODEC") */
69static char *extract_codec_name(const char *str)
70{
71 static char buf[64];
Neels Hofmeyrc364f4a2023-06-30 05:09:53 +020072 char *pos;
Philipp Maier704c4f02018-06-07 18:51:31 +020073
74 if (!str)
75 return NULL;
76
Philipp Maier704c4f02018-06-07 18:51:31 +020077 osmo_strlcpy(buf, str, sizeof(buf));
78
Neels Hofmeyrc364f4a2023-06-30 05:09:53 +020079 pos = strchr(buf, '/');
80 if (pos)
81 *pos = '\0';
Philipp Maier704c4f02018-06-07 18:51:31 +020082
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. */
89enum 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 Hofmeyr47642f22019-02-27 05:56:53 +010097 for (i = 0; i < ARRAY_SIZE(osmo_mgcpc_codec_names); i++) {
98 codec_name = extract_codec_name(osmo_mgcpc_codec_names[i].str);
Philipp Maier704c4f02018-06-07 18:51:31 +020099 if (!codec_name)
100 continue;
101 if (strcmp(codec_name, str_buf) == 0)
Neels Hofmeyr47642f22019-02-27 05:56:53 +0100102 return osmo_mgcpc_codec_names[i].value;
Philipp Maier704c4f02018-06-07 18:51:31 +0200103 }
104
105 return -1;
106}
107
108/* Check the ptmap for illegal mappings */
Neels Hofmeyr84274e42019-04-27 19:08:36 +0200109static int check_ptmap(const struct ptmap *ptmap)
Philipp Maier704c4f02018-06-07 18:51:31 +0200110{
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;
126error:
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 Hofmeyr84274e42019-04-27 19:08:36 +0200138unsigned int map_codec_to_pt(const struct ptmap *ptmap, unsigned int ptmap_len,
Philipp Maier704c4f02018-06-07 18:51:31 +0200139 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
Oliver Smith292ba1b2023-06-28 10:30:25 +0200150 * corresponds to the statically assigned payload types */
Philipp Maier704c4f02018-06-07 18:51:31 +0200151 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 */
170enum 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
Pau Espin Pedrol8e8d59f2023-06-13 19:41:44 +0200196static void _mgcp_client_conf_init(struct mgcp_client_conf *conf)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200197{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200198 /* NULL and -1 default to MGCP_CLIENT_*_DEFAULT values */
199 *conf = (struct mgcp_client_conf){
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200200 .local_addr = NULL,
201 .local_port = -1,
202 .remote_addr = NULL,
203 .remote_port = -1,
Pau Espin Pedrol563386e2023-06-14 12:20:49 +0200204 .keepalive = {
205 .timeout_sec = 0, /* disabled */
206 .req_interval_sec = 0, /* disabled */
207 .req_endpoint_name = MGCP_CLIENT_KEEPALIVE_DEFAULT_ENDP,
208 },
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200209 };
Philipp Maier3f2c15f2021-07-22 11:53:07 +0200210
211 INIT_LLIST_HEAD(&conf->reset_epnames);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200212}
213
Pau Espin Pedrol8e8d59f2023-06-13 19:41:44 +0200214/*! Allocate and initialize MGCP client configuration struct with default values.
215 * \param[in] ctx talloc context to use as a parent during allocation.
216 *
217 * The returned struct can be freed using talloc_free().
218 */
219struct mgcp_client_conf *mgcp_client_conf_alloc(void *ctx)
220{
221 struct mgcp_client_conf *conf = talloc(ctx, struct mgcp_client_conf);
222 _mgcp_client_conf_init(conf);
223 return conf;
224}
225
226/*! Initialize MGCP client configuration struct with default values.
227 * \param[out] conf Client configuration.
228 *
229 * This function is deprecated and should not be used, as it may break if size
230 * of struct mgcp_client_conf changes in the future!
231 */
232void mgcp_client_conf_init(struct mgcp_client_conf *conf)
233{
234 _mgcp_client_conf_init(conf);
235}
236
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200237static void mgcp_client_handle_response(struct mgcp_client *mgcp,
238 struct mgcp_response_pending *pending,
239 struct mgcp_response *response)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200240{
241 if (!pending) {
Philipp Maierdf9192e2021-09-03 17:50:51 +0200242 LOGPMGW(mgcp, LOGL_ERROR, "Cannot handle NULL response\n");
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200243 return;
244 }
245 if (pending->response_cb)
246 pending->response_cb(response, pending->priv);
247 else
Philipp Maierdf9192e2021-09-03 17:50:51 +0200248 LOGPMGW(mgcp, LOGL_DEBUG, "MGCP response ignored (NULL cb)\n");
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200249 talloc_free(pending);
250}
251
252static int mgcp_response_parse_head(struct mgcp_response *r, struct msgb *msg)
253{
254 int comment_pos;
255 char *end;
256
257 if (mgcp_msg_terminate_nul(msg))
258 goto response_parse_failure;
259
260 r->body = (char *)msg->data;
261
Harald Welte9bf7c532017-11-17 14:14:31 +0100262 if (sscanf(r->body, "%3d %u %n",
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200263 &r->head.response_code, &r->head.trans_id,
264 &comment_pos) != 2)
265 goto response_parse_failure;
266
Philipp Maierabe8c892018-01-20 00:15:12 +0100267 osmo_strlcpy(r->head.comment, r->body + comment_pos, sizeof(r->head.comment));
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200268 end = strchr(r->head.comment, '\r');
269 if (!end)
270 goto response_parse_failure;
271 /* Mark the end of the comment */
272 *end = '\0';
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200273 return 0;
274
275response_parse_failure:
276 LOGP(DLMGCP, LOGL_ERROR,
277 "Failed to parse MGCP response header\n");
278 return -EINVAL;
279}
280
281/* TODO undup against mgcp_protocol.c:mgcp_check_param() */
282static bool mgcp_line_is_valid(const char *line)
283{
284 const size_t line_len = strlen(line);
285 if (line[0] == '\0')
286 return true;
287
288 if (line_len < 2
289 || line[1] != '=') {
290 LOGP(DLMGCP, LOGL_ERROR,
291 "Wrong MGCP option format: '%s'\n",
292 line);
293 return false;
294 }
295
296 return true;
297}
298
Philipp Maier704c4f02018-06-07 18:51:31 +0200299/* Parse a line like "m=audio 16002 RTP/AVP 98", extract port and payload types */
300static int mgcp_parse_audio_port_pt(struct mgcp_response *r, char *line)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200301{
Philipp Maier704c4f02018-06-07 18:51:31 +0200302 char *pt_str;
Pau Espin Pedrolc5c14302019-07-23 16:19:41 +0200303 char *pt_end;
Pau Espin Pedrola2b1c5e2019-07-26 14:13:14 +0200304 unsigned long int pt;
Neels Hofmeyrcc2f7932023-12-07 03:46:46 +0100305 unsigned int ptmap_len;
Philipp Maier704c4f02018-06-07 18:51:31 +0200306 unsigned int i;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200307
Philipp Maier704c4f02018-06-07 18:51:31 +0200308 /* Extract port information */
309 if (sscanf(line, "m=audio %hu", &r->audio_port) != 1)
310 goto response_parse_failure_port;
Philipp Maier10f32db2017-12-13 12:34:34 +0100311 if (r->audio_port == 0)
Philipp Maier704c4f02018-06-07 18:51:31 +0200312 goto response_parse_failure_port;
Philipp Maier10f32db2017-12-13 12:34:34 +0100313
Philipp Maier704c4f02018-06-07 18:51:31 +0200314 /* Extract payload types */
315 line = strstr(line, "RTP/AVP ");
316 if (!line)
317 goto exit;
318
Neels Hofmeyrcc2f7932023-12-07 03:46:46 +0100319 /* Clear any previous entries before writing over r->ptmap */
320 r->ptmap_len = 0;
321 /* Keep a local ptmap_len to show only the full list after parsing succeeded in whole. */
322 ptmap_len = 0;
323
Philipp Maier704c4f02018-06-07 18:51:31 +0200324 pt_str = strtok(line, " ");
325 while (1) {
Philipp Maier704c4f02018-06-07 18:51:31 +0200326 pt_str = strtok(NULL, " ");
327 if (!pt_str)
328 break;
Pau Espin Pedrolc5c14302019-07-23 16:19:41 +0200329 errno = 0;
330 pt = strtoul(pt_str, &pt_end, 0);
331 if ((errno == ERANGE && pt == ULONG_MAX) || (errno && !pt) ||
Neels Hofmeyr3a3e1b52023-12-22 02:46:28 +0100332 pt_str == pt_end) {
333 LOGP(DLMGCP, LOGL_ERROR, "SDP: cannot parse payload type number from '%s'\n", pt_str);
Pau Espin Pedrolc5c14302019-07-23 16:19:41 +0200334 goto response_parse_failure_pt;
Neels Hofmeyr3a3e1b52023-12-22 02:46:28 +0100335 }
Philipp Maier704c4f02018-06-07 18:51:31 +0200336
Neels Hofmeyr3a3e1b52023-12-22 02:46:28 +0100337 /* PT is 7 bit field, higher values not allowed */
338 if (pt >> 7) {
339 LOGP(DLMGCP, LOGL_ERROR, "SDP: payload type number out of range: %lu > 127\n", pt);
Pau Espin Pedrola2b1c5e2019-07-26 14:13:14 +0200340 goto response_parse_failure_pt;
Neels Hofmeyr3a3e1b52023-12-22 02:46:28 +0100341 }
Pau Espin Pedrola2b1c5e2019-07-26 14:13:14 +0200342
Philipp Maier704c4f02018-06-07 18:51:31 +0200343 /* Do not allow duplicate payload types */
Neels Hofmeyr3a3e1b52023-12-22 02:46:28 +0100344 for (i = 0; i < ptmap_len; i++) {
345 if (r->ptmap[i].pt == pt) {
346 LOGP(DLMGCP, LOGL_ERROR, "SDP: payload type number %lu listed twice\n", pt);
Philipp Maier704c4f02018-06-07 18:51:31 +0200347 goto response_parse_failure_pt;
Neels Hofmeyr3a3e1b52023-12-22 02:46:28 +0100348 }
349 }
Philipp Maier704c4f02018-06-07 18:51:31 +0200350
Neels Hofmeyr186d4f12023-12-22 02:44:22 +0100351 /* Do not allow excessive payload types */
Neels Hofmeyr3a3e1b52023-12-22 02:46:28 +0100352 if (ptmap_len >= ARRAY_SIZE(r->ptmap)) {
353 LOGP(DLMGCP, LOGL_ERROR,
354 "SDP: can parse only up to %zu payload type numbers\n", ARRAY_SIZE(r->ptmap));
Neels Hofmeyr186d4f12023-12-22 02:44:22 +0100355 goto response_parse_failure_pt;
Neels Hofmeyr3a3e1b52023-12-22 02:46:28 +0100356 }
Neels Hofmeyr186d4f12023-12-22 02:44:22 +0100357
Neels Hofmeyrcc2f7932023-12-07 03:46:46 +0100358 /* Some payload type numbers imply a specific codec. For those, using the PT number as enum mgcp_codecs
359 * yields the correct result. If no more specific information on the codec follows in "a=rtpmap:N"
360 * lines, then this default number takes over. This only applies for PT below the dynamic range (<96). */
361 if (pt < 96)
362 r->ptmap[ptmap_len].codec = pt;
363 else
364 r->ptmap[ptmap_len].codec = -1;
365 r->ptmap[ptmap_len].pt = pt;
366 ptmap_len++;
Philipp Maier704c4f02018-06-07 18:51:31 +0200367 }
368
Neels Hofmeyrcc2f7932023-12-07 03:46:46 +0100369 /* Parsing succeeded, publish all entries. */
370 r->ptmap_len = ptmap_len;
Philipp Maier704c4f02018-06-07 18:51:31 +0200371
372exit:
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200373 return 0;
374
Philipp Maier704c4f02018-06-07 18:51:31 +0200375response_parse_failure_port:
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200376 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier704c4f02018-06-07 18:51:31 +0200377 "Failed to parse SDP parameter port (%s)\n", line);
Philipp Maier06da85e2017-10-05 18:49:24 +0200378 return -EINVAL;
Philipp Maier704c4f02018-06-07 18:51:31 +0200379
380response_parse_failure_pt:
381 LOGP(DLMGCP, LOGL_ERROR,
382 "Failed to parse SDP parameter payload types (%s)\n", line);
383 return -EINVAL;
384}
385
Neels Hofmeyrcc2f7932023-12-07 03:46:46 +0100386/* Parse an 'a=...' parameter */
Philipp Maier704c4f02018-06-07 18:51:31 +0200387static int mgcp_parse_audio_ptime_rtpmap(struct mgcp_response *r, const char *line)
388{
389 unsigned int pt;
Neels Hofmeyrcc2f7932023-12-07 03:46:46 +0100390 unsigned int i;
Philipp Maier704c4f02018-06-07 18:51:31 +0200391 char codec_resp[64];
Oliver Smith94238172023-06-28 13:28:57 +0200392 int rc;
Pau Espin Pedrolc12bfb72019-04-16 17:23:09 +0200393
Neels Hofmeyr23f40482019-08-08 04:03:42 +0200394#define A_PTIME "a=ptime:"
395#define A_RTPMAP "a=rtpmap:"
Pau Espin Pedrolc12bfb72019-04-16 17:23:09 +0200396
Neels Hofmeyr23f40482019-08-08 04:03:42 +0200397 if (osmo_str_startswith(line, A_PTIME)) {
398 if (sscanf(line, A_PTIME "%u", &r->ptime) != 1) {
399 LOGP(DLMGCP, LOGL_ERROR,
400 "Failed to parse SDP parameter, invalid ptime (%s)\n", line);
401 return -EINVAL;
402 }
403 } else if (osmo_str_startswith(line, A_RTPMAP)) {
404 if (sscanf(line, A_RTPMAP "%d %63s", &pt, codec_resp) != 2) {
405 LOGP(DLMGCP, LOGL_ERROR,
406 "Failed to parse SDP parameter, invalid rtpmap: %s\n", osmo_quote_str(line, -1));
407 return -EINVAL;
408 }
Oliver Smith94238172023-06-28 13:28:57 +0200409 rc = map_str_to_codec(codec_resp);
410 if (rc < 0) {
411 LOGP(DLMGCP, LOGL_ERROR,
412 "Failed to parse SDP parameter, can't parse codec in rtpmap: %s\n", osmo_quote_str(line, -1));
413 return -EINVAL;
414 }
Neels Hofmeyrcc2f7932023-12-07 03:46:46 +0100415
416 /* Earlier, a line like "m=audio 16002 RTP/AVP 98 112 3" established the desired order of payloads, now
417 * enrich it with actual codec information provided by "a=rtpmap:..." entries.
418 * For each, find the entry with the right pt number and add the info there. */
419
420 for (i = 0; i < r->ptmap_len; i++) {
421 if (r->ptmap[i].pt != pt)
422 continue;
423 r->ptmap[i].codec = rc;
424 return 0;
425 }
426
427 /* No entry was found. This is an error in the MGCP protocol, but let's just add another entry
428 * anyway, to not make it look like it was never there. */
429 LOGP(DLMGCP, LOGL_ERROR,
430 "error in MGCP message: 'a=rtpmap:%u' has no matching entry in 'm=audio ... %u'\n",
431 pt, pt);
432 if (r->ptmap_len >= ARRAY_SIZE(r->ptmap)) {
433 LOGP(DLMGCP, LOGL_ERROR,
434 "cannot parse all codecs: can only store up to %zu rtpmap entries.\n",
435 ARRAY_SIZE(r->ptmap));
436 return -ENOSPC;
437 }
438 r->ptmap[r->ptmap_len] = (struct ptmap){
439 .pt = pt,
440 .codec = rc,
441 };
Neels Hofmeyr3ab8ca42019-08-08 04:03:42 +0200442 r->ptmap_len++;
Philipp Maier704c4f02018-06-07 18:51:31 +0200443 }
Pau Espin Pedrolc12bfb72019-04-16 17:23:09 +0200444
Philipp Maier704c4f02018-06-07 18:51:31 +0200445 return 0;
Philipp Maier06da85e2017-10-05 18:49:24 +0200446}
447
448/* Parse a line like "c=IN IP4 10.11.12.13" */
449static int mgcp_parse_audio_ip(struct mgcp_response *r, const char *line)
450{
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +0200451 struct in6_addr ip_test;
452 bool is_ipv6;
Philipp Maier06da85e2017-10-05 18:49:24 +0200453
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +0200454 if (strncmp("c=IN IP", line, 7) != 0)
Philipp Maier06da85e2017-10-05 18:49:24 +0200455 goto response_parse_failure;
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +0200456 line += 7;
457 if (*line == '6')
458 is_ipv6 = true;
459 else if (*line == '4')
460 is_ipv6 = false;
461 else
Philipp Maier06da85e2017-10-05 18:49:24 +0200462 goto response_parse_failure;
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +0200463 line++;
464 if (*line != ' ')
Philipp Maier06da85e2017-10-05 18:49:24 +0200465 goto response_parse_failure;
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +0200466 line++;
Philipp Maier06da85e2017-10-05 18:49:24 +0200467
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +0200468 /* Extract and check IP-Address */
469 if (is_ipv6) {
470 /* 45 = INET6_ADDRSTRLEN -1 */
471 if (sscanf(line, "%45s", r->audio_ip) != 1)
472 goto response_parse_failure;
473 if (inet_pton(AF_INET6, r->audio_ip, &ip_test) != 1)
474 goto response_parse_failure;
475 } else {
476 /* 15 = INET_ADDRSTRLEN -1 */
477 if (sscanf(line, "%15s", r->audio_ip) != 1)
478 goto response_parse_failure;
479 if (inet_pton(AF_INET, r->audio_ip, &ip_test) != 1)
480 goto response_parse_failure;
481 }
Philipp Maier06da85e2017-10-05 18:49:24 +0200482 return 0;
483
484response_parse_failure:
485 LOGP(DLMGCP, LOGL_ERROR,
486 "Failed to parse MGCP response header (audio ip)\n");
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200487 return -EINVAL;
488}
489
Pau Espin Pedrol91088c32019-04-24 21:02:40 +0200490/*! Extract OSMUX CID from an MGCP parameter line (string).
491 * \param[in] line single parameter line from the MGCP message
492 * \returns OSMUX CID, -1 wildcard, -2 on error
493 * FIXME: This is a copy of function in mgcp_msg.c. Have some common.c file between both libs?
494 */
495static int mgcp_parse_osmux_cid(const char *line)
496{
497 int osmux_cid;
498
499
Pau Espin Pedrolc1bf4692019-05-14 16:23:24 +0200500 if (strcasecmp(line + 2, "Osmux: *") == 0) {
Pau Espin Pedrol91088c32019-04-24 21:02:40 +0200501 LOGP(DLMGCP, LOGL_DEBUG, "Parsed wilcard Osmux CID\n");
502 return -1;
503 }
504
Pau Espin Pedrolc1bf4692019-05-14 16:23:24 +0200505 if (sscanf(line + 2 + 7, "%u", &osmux_cid) != 1) {
Pau Espin Pedrol91088c32019-04-24 21:02:40 +0200506 LOGP(DLMGCP, LOGL_ERROR, "Failed parsing Osmux in MGCP msg line: %s\n",
507 line);
508 return -2;
509 }
510
Pau Espin Pedrol91088c32019-04-24 21:02:40 +0200511 if (osmux_cid > OSMUX_CID_MAX) { /* OSMUX_CID_MAX from libosmo-netif */
512 LOGP(DLMGCP, LOGL_ERROR, "Osmux ID too large: %u > %u\n",
513 osmux_cid, OSMUX_CID_MAX);
514 return -2;
515 }
Pau Espin Pedrolc7c8e642022-10-06 18:24:21 +0200516 LOGP(DLMGCP, LOGL_DEBUG, "MGW offered Osmux CID %u\n", osmux_cid);
Pau Espin Pedrol91088c32019-04-24 21:02:40 +0200517
518 return osmux_cid;
519}
520
Philipp Maier3b12e1b2018-01-18 15:16:13 +0100521/* A new section is marked by a double line break, check a few more
522 * patterns as there may be variants */
523static char *mgcp_find_section_end(char *string)
524{
525 char *rc;
526
527 rc = strstr(string, "\n\n");
528 if (rc)
Neels Hofmeyrce356ee2023-03-30 16:51:40 +0200529 return rc + 2;
Philipp Maier3b12e1b2018-01-18 15:16:13 +0100530
531 rc = strstr(string, "\n\r\n\r");
532 if (rc)
Neels Hofmeyrce356ee2023-03-30 16:51:40 +0200533 return rc + 4;
Philipp Maier3b12e1b2018-01-18 15:16:13 +0100534
535 rc = strstr(string, "\r\n\r\n");
536 if (rc)
Neels Hofmeyrce356ee2023-03-30 16:51:40 +0200537 return rc + 4;
Philipp Maier3b12e1b2018-01-18 15:16:13 +0100538
539 return NULL;
540}
541
Philipp Maier275ac972018-01-20 00:58:16 +0100542/*! Parse body (SDP) parameters of the MGCP response
543 * \param[in,out] r Response data
544 * \returns 0 on success, -EINVAL on error. */
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200545int mgcp_response_parse_params(struct mgcp_response *r)
546{
547 char *line;
548 int rc;
Neels Hofmeyr0793d2f2018-02-21 14:55:34 +0100549 char *data;
Philipp Maiere9d645b2018-01-19 23:54:08 +0100550 char *data_ptr;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200551
Philipp Maiere9d645b2018-01-19 23:54:08 +0100552 /* Since this functions performs a destructive parsing, we create a
553 * local copy of the body data */
Neels Hofmeyr0793d2f2018-02-21 14:55:34 +0100554 OSMO_ASSERT(r->body);
555 data = talloc_strdup(r, r->body);
Philipp Maiere9d645b2018-01-19 23:54:08 +0100556 OSMO_ASSERT(data);
Philipp Maier55295f72018-01-15 14:00:28 +0100557
Philipp Maiere9d645b2018-01-19 23:54:08 +0100558 /* Find beginning of the parameter (SDP) section */
559 data_ptr = mgcp_find_section_end(data);
Neels Hofmeyr10835332018-02-21 14:55:34 +0100560 if (!data_ptr) {
Neels Hofmeyre8278312019-09-19 03:06:46 +0200561 LOGP(DLMGCP, LOGL_DEBUG, "MGCP response contains no SDP parameters\n");
562 rc = 0;
Philipp Maiere9d645b2018-01-19 23:54:08 +0100563 goto exit;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200564 }
565
Neels Hofmeyr0793d2f2018-02-21 14:55:34 +0100566 /* data_ptr now points to the beginning of the section-end-marker; for_each_non_empty_line()
567 * skips any \r and \n characters for free, so we don't need to skip the marker. */
568
Philipp Maiere9d645b2018-01-19 23:54:08 +0100569 for_each_non_empty_line(line, data_ptr) {
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200570 if (!mgcp_line_is_valid(line))
571 return -EINVAL;
572
573 switch (line[0]) {
Philipp Maier704c4f02018-06-07 18:51:31 +0200574 case 'a':
575 rc = mgcp_parse_audio_ptime_rtpmap(r, line);
576 if (rc)
577 goto exit;
578 break;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200579 case 'm':
Philipp Maier704c4f02018-06-07 18:51:31 +0200580 rc = mgcp_parse_audio_port_pt(r, line);
Philipp Maier06da85e2017-10-05 18:49:24 +0200581 if (rc)
Philipp Maiere9d645b2018-01-19 23:54:08 +0100582 goto exit;
Philipp Maier06da85e2017-10-05 18:49:24 +0200583 break;
584 case 'c':
585 rc = mgcp_parse_audio_ip(r, line);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200586 if (rc)
Philipp Maiere9d645b2018-01-19 23:54:08 +0100587 goto exit;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200588 break;
589 default:
590 /* skip unhandled parameters */
591 break;
592 }
593 }
Philipp Maiere9d645b2018-01-19 23:54:08 +0100594
595 rc = 0;
596exit:
597 talloc_free(data);
598 return rc;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200599}
600
Philipp Maier55295f72018-01-15 14:00:28 +0100601/* Parse a line like "X: something" */
602static int mgcp_parse_head_param(char *result, unsigned int result_len,
603 char label, const char *line)
Philipp Maierffd75e42017-11-22 11:44:50 +0100604{
Philipp Maier55295f72018-01-15 14:00:28 +0100605 char label_string[4];
Neels Hofmeyr23e7bf12018-09-03 21:26:22 +0200606 size_t rc;
Philipp Maier55295f72018-01-15 14:00:28 +0100607
608 /* Detect empty parameters */
Philipp Maierffd75e42017-11-22 11:44:50 +0100609 if (strlen(line) < 4)
610 goto response_parse_failure;
611
Philipp Maier55295f72018-01-15 14:00:28 +0100612 /* Check if the label matches */
613 snprintf(label_string, sizeof(label_string), "%c: ", label);
614 if (memcmp(label_string, line, 3) != 0)
Philipp Maierffd75e42017-11-22 11:44:50 +0100615 goto response_parse_failure;
616
Philipp Maier55295f72018-01-15 14:00:28 +0100617 /* Copy payload part of the string to destinations (the label string
618 * is always 3 chars long) */
Neels Hofmeyr23e7bf12018-09-03 21:26:22 +0200619 rc = osmo_strlcpy(result, line + 3, result_len);
620 if (rc >= result_len) {
621 LOGP(DLMGCP, LOGL_ERROR,
622 "Failed to parse MGCP response (parameter label: %c):"
623 " the received conn ID is too long: %zu, maximum is %u characters\n",
624 label, rc, result_len - 1);
625 return -ENOSPC;
626 }
Philipp Maierffd75e42017-11-22 11:44:50 +0100627 return 0;
628
629response_parse_failure:
630 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier55295f72018-01-15 14:00:28 +0100631 "Failed to parse MGCP response (parameter label: %c)\n", label);
Philipp Maierffd75e42017-11-22 11:44:50 +0100632 return -EINVAL;
633}
634
635/* Parse MGCP parameters of the response */
636static int parse_head_params(struct mgcp_response *r)
637{
638 char *line;
639 int rc = 0;
640 OSMO_ASSERT(r->body);
Philipp Maier55295f72018-01-15 14:00:28 +0100641 char *data;
642 char *data_ptr;
643 char *data_end;
Philipp Maierffd75e42017-11-22 11:44:50 +0100644
Philipp Maier55295f72018-01-15 14:00:28 +0100645 /* Since this functions performs a destructive parsing, we create a
646 * local copy of the body data */
Philipp Maier1fc69992018-01-31 15:32:55 +0100647 data = talloc_zero_size(r, strlen(r->body)+1);
Philipp Maier55295f72018-01-15 14:00:28 +0100648 OSMO_ASSERT(data);
649 data_ptr = data;
650 osmo_strlcpy(data, r->body, strlen(r->body));
651
652 /* If there is an SDP body attached, prevent for_each_non_empty_line()
653 * into running in there, we are not yet interested in the parameters
654 * stored there. */
Pau Espin Pedrolc12bfb72019-04-16 17:23:09 +0200655 data_end = mgcp_find_section_end(data);
Philipp Maierffd75e42017-11-22 11:44:50 +0100656 if (data_end)
657 *data_end = '\0';
658
Philipp Maier55295f72018-01-15 14:00:28 +0100659 for_each_non_empty_line(line, data_ptr) {
Pau Espin Pedrol166077e2019-06-26 12:21:38 +0200660 switch (toupper(line[0])) {
Philipp Maier55295f72018-01-15 14:00:28 +0100661 case 'Z':
662 rc = mgcp_parse_head_param(r->head.endpoint,
663 sizeof(r->head.endpoint),
664 'Z', line);
665 if (rc)
666 goto exit;
Philipp Maier771b26a2018-01-31 13:53:11 +0100667
668 /* A specific endpoint identifier returned by the MGW
669 * must not contain any wildcard characters */
670 if (strstr(r->head.endpoint, "*") != NULL) {
671 rc = -EINVAL;
672 goto exit;
673 }
Philipp Maier3261dc72018-01-31 14:03:13 +0100674
675 /* A specific endpoint identifier returned by the MGW
676 * must contain an @ character */
677 if (strstr(r->head.endpoint, "@") == NULL) {
678 rc = -EINVAL;
679 goto exit;
680 }
Philipp Maier55295f72018-01-15 14:00:28 +0100681 break;
Philipp Maierffd75e42017-11-22 11:44:50 +0100682 case 'I':
Philipp Maier55295f72018-01-15 14:00:28 +0100683 rc = mgcp_parse_head_param(r->head.conn_id,
684 sizeof(r->head.conn_id),
685 'I', line);
Philipp Maierffd75e42017-11-22 11:44:50 +0100686 if (rc)
687 goto exit;
688 break;
Pau Espin Pedrol91088c32019-04-24 21:02:40 +0200689 case 'X':
Pau Espin Pedrolc1bf4692019-05-14 16:23:24 +0200690 if (strncasecmp("Osmux: ", line + 2, strlen("Osmux: ")) == 0) {
Pau Espin Pedrol91088c32019-04-24 21:02:40 +0200691 rc = mgcp_parse_osmux_cid(line);
692 if (rc < 0) {
693 /* -1: we don't want wildcards in response. -2: error */
694 rc = -EINVAL;
695 goto exit;
696 }
697 r->head.x_osmo_osmux_use = true;
698 r->head.x_osmo_osmux_cid = (uint8_t) rc;
699 rc = 0;
700 break;
701 }
702 /* Ignore unknown X-headers */
703 break;
Philipp Maierffd75e42017-11-22 11:44:50 +0100704 default:
705 /* skip unhandled parameters */
706 break;
707 }
708 }
709exit:
Philipp Maier55295f72018-01-15 14:00:28 +0100710 talloc_free(data);
Philipp Maierffd75e42017-11-22 11:44:50 +0100711 return rc;
712}
713
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200714static struct mgcp_response_pending *mgcp_client_response_pending_get(
715 struct mgcp_client *mgcp,
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100716 mgcp_trans_id_t trans_id)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200717{
718 struct mgcp_response_pending *pending;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200719 llist_for_each_entry(pending, &mgcp->responses_pending, entry) {
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100720 if (pending->trans_id == trans_id) {
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200721 llist_del(&pending->entry);
722 return pending;
723 }
724 }
725 return NULL;
726}
727
728/* Feed an MGCP message into the receive processing.
729 * Parse the head and call any callback registered for the transaction id found
730 * in the MGCP message. This is normally called directly from the internal
731 * mgcp_do_read that reads from the socket connected to the MGCP gateway. This
732 * function is published mainly to be able to feed data from the test suite.
733 */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200734int mgcp_client_rx(struct mgcp_client *mgcp, struct msgb *msg)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200735{
Philipp Maier1fc69992018-01-31 15:32:55 +0100736 struct mgcp_response *r;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200737 struct mgcp_response_pending *pending;
738 int rc;
739
Philipp Maier1fc69992018-01-31 15:32:55 +0100740 r = talloc_zero(mgcp, struct mgcp_response);
741 OSMO_ASSERT(r);
742
Pau Espin Pedrol563386e2023-06-14 12:20:49 +0200743 /* Re-arm keepalive timer if enabled */
744 if (OSMO_UNLIKELY(mgcp->conn_up == false)) {
745 LOGPMGW(mgcp, LOGL_NOTICE, "MGCP link to MGW now considered UP\n");
746 mgcp->conn_up = true;
747 }
748 if (mgcp->actual.keepalive.timeout_sec > 0)
749 osmo_timer_schedule(&mgcp->keepalive_rx_timer, mgcp->actual.keepalive.timeout_sec, 0);
750
Philipp Maier1fc69992018-01-31 15:32:55 +0100751 rc = mgcp_response_parse_head(r, msg);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200752 if (rc) {
Philipp Maierdf9192e2021-09-03 17:50:51 +0200753 LOGPMGW(mgcp, LOGL_ERROR, "Cannot parse MGCP response (head)\n");
Philipp Maier1fc69992018-01-31 15:32:55 +0100754 rc = 1;
755 goto error;
Philipp Maierffd75e42017-11-22 11:44:50 +0100756 }
757
Philipp Maierdf9192e2021-09-03 17:50:51 +0200758 LOGPMGW(mgcp, LOGL_DEBUG, "MGCP client: Rx %d %u %s\n",
Neels Hofmeyrd5731072021-07-15 01:36:47 +0200759 r->head.response_code, r->head.trans_id, r->head.comment);
760
Philipp Maier1fc69992018-01-31 15:32:55 +0100761 rc = parse_head_params(r);
Philipp Maierffd75e42017-11-22 11:44:50 +0100762 if (rc) {
Philipp Maierdf9192e2021-09-03 17:50:51 +0200763 LOGPMGW(mgcp, LOGL_ERROR, "Cannot parse MGCP response (head parameters)\n");
Philipp Maier1fc69992018-01-31 15:32:55 +0100764 rc = 1;
765 goto error;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200766 }
767
Philipp Maier1fc69992018-01-31 15:32:55 +0100768 pending = mgcp_client_response_pending_get(mgcp, r->head.trans_id);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200769 if (!pending) {
Philipp Maierdf9192e2021-09-03 17:50:51 +0200770 LOGPMGW(mgcp, LOGL_ERROR, "Cannot find matching MGCP transaction for trans_id %d\n",
771 r->head.trans_id);
Philipp Maier1fc69992018-01-31 15:32:55 +0100772 rc = -ENOENT;
773 goto error;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200774 }
775
Philipp Maier1fc69992018-01-31 15:32:55 +0100776 mgcp_client_handle_response(mgcp, pending, r);
777 rc = 0;
778
779error:
780 talloc_free(r);
781 return rc;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200782}
783
784static int mgcp_do_read(struct osmo_fd *fd)
785{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200786 struct mgcp_client *mgcp = fd->data;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200787 struct msgb *msg;
788 int ret;
789
790 msg = msgb_alloc_headroom(4096, 128, "mgcp_from_gw");
791 if (!msg) {
Philipp Maierdf9192e2021-09-03 17:50:51 +0200792 LOGPMGW(mgcp, LOGL_ERROR, "Failed to allocate MGCP message.\n");
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200793 return -1;
794 }
795
Vadim Yanitskiyf3715dc2023-12-11 21:03:26 +0700796 /* msgb_tailroom() is basically (4096 - 128); -1 is for '\0' */
797 ret = read(fd->fd, msg->data, msgb_tailroom(msg) - 1);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200798 if (ret <= 0) {
Philipp Maierdf9192e2021-09-03 17:50:51 +0200799 LOGPMGW(mgcp, LOGL_ERROR, "Failed to read: %s: %d='%s'\n",
800 osmo_sock_get_name2(fd->fd), errno, strerror(errno));
Neels Hofmeyr0a403792018-12-12 03:10:18 +0100801
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200802 msgb_free(msg);
803 return -1;
Harald Welte9bf7c532017-11-17 14:14:31 +0100804 }
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200805
806 msg->l2h = msgb_put(msg, ret);
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200807 ret = mgcp_client_rx(mgcp, msg);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200808 talloc_free(msg);
809 return ret;
810}
811
812static int mgcp_do_write(struct osmo_fd *fd, struct msgb *msg)
813{
814 int ret;
Philipp Maierdf9192e2021-09-03 17:50:51 +0200815 struct mgcp_client *mgcp = fd->data;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200816
Philipp Maierdf9192e2021-09-03 17:50:51 +0200817 LOGPMGW(mgcp, LOGL_DEBUG, "Tx MGCP: %s: len=%u '%s'...\n",
818 osmo_sock_get_name2(fd->fd), msg->len,
819 osmo_escape_str((const char *)msg->data, OSMO_MIN(42, msg->len)));
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200820
821 ret = write(fd->fd, msg->data, msg->len);
Pau Espin Pedrol563386e2023-06-14 12:20:49 +0200822 if (OSMO_UNLIKELY(ret != msg->len))
Philipp Maierdf9192e2021-09-03 17:50:51 +0200823 LOGPMGW(mgcp, LOGL_ERROR, "Failed to Tx MGCP: %s: %d='%s'; msg: len=%u '%s'...\n",
824 osmo_sock_get_name2(fd->fd), errno, strerror(errno),
825 msg->len, osmo_escape_str((const char *)msg->data, OSMO_MIN(42, msg->len)));
Pau Espin Pedrol563386e2023-06-14 12:20:49 +0200826
827 /* Re-arm the keepalive Tx timer: */
828 if (mgcp->actual.keepalive.req_interval_sec > 0)
829 osmo_timer_schedule(&mgcp->keepalive_tx_timer, mgcp->actual.keepalive.req_interval_sec, 0);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200830 return ret;
831}
832
Pau Espin Pedrol2e411f32023-06-14 11:52:04 +0200833static const char *_mgcp_client_name_append_domain(const struct mgcp_client *mgcp, const char *name)
834{
835 static char endpoint[MGCP_ENDPOINT_MAXLEN];
836 int rc;
837
838 rc = snprintf(endpoint, sizeof(endpoint), "%s@%s", name, mgcp_client_endpoint_domain(mgcp));
839 if (rc > sizeof(endpoint) - 1) {
840 LOGPMGW(mgcp, LOGL_ERROR, "MGCP endpoint exceeds maximum length of %zu: '%s@%s'\n",
841 sizeof(endpoint) - 1, name, mgcp_client_endpoint_domain(mgcp));
842 return NULL;
843 }
844 if (rc < 1) {
845 LOGPMGW(mgcp, LOGL_ERROR, "Cannot compose MGCP endpoint name\n");
846 return NULL;
847 }
848 return endpoint;
849}
850
851/* Safely ignore the MGCP response to the DLCX sent via _mgcp_client_send_dlcx() */
852static void _ignore_mgcp_response(struct mgcp_response *response, void *priv) { }
853
854/* Format DLCX message (fire and forget) and send it off to the MGW */
855static void _mgcp_client_send_dlcx(struct mgcp_client *mgcp, const char *epname)
856{
857 struct msgb *msgb_dlcx;
858 struct mgcp_msg mgcp_msg_dlcx = {
859 .verb = MGCP_VERB_DLCX,
860 .presence = MGCP_MSG_PRESENCE_ENDPOINT,
861 };
862 osmo_strlcpy(mgcp_msg_dlcx.endpoint, epname, sizeof(mgcp_msg_dlcx.endpoint));
863 msgb_dlcx = mgcp_msg_gen(mgcp, &mgcp_msg_dlcx);
864 mgcp_client_tx(mgcp, msgb_dlcx, &_ignore_mgcp_response, NULL);
865}
866
Pau Espin Pedrol563386e2023-06-14 12:20:49 +0200867/* Format AuditEndpoint message (fire and forget) and send it off to the MGW */
868static void _mgcp_client_send_auep(struct mgcp_client *mgcp, const char *epname)
869{
870 struct msgb *msgb_auep;
871 struct mgcp_msg mgcp_msg_auep = {
872 .verb = MGCP_VERB_AUEP,
873 .presence = MGCP_MSG_PRESENCE_ENDPOINT,
874 };
875 OSMO_STRLCPY_ARRAY(mgcp_msg_auep.endpoint, epname);
876 msgb_auep = mgcp_msg_gen(mgcp, &mgcp_msg_auep);
877 mgcp_client_tx(mgcp, msgb_auep, &_ignore_mgcp_response, NULL);
878}
879
880static void mgcp_client_keepalive_tx_timer_cb(void *data)
881{
882 struct mgcp_client *mgcp = (struct mgcp_client *)data;
883 LOGPMGW(mgcp, LOGL_INFO, "Triggering keepalive MGCP request\n");
884 const char *epname = _mgcp_client_name_append_domain(mgcp, mgcp->actual.keepalive.req_endpoint_name);
885 _mgcp_client_send_auep(mgcp, epname);
886
887 /* Re-arm the timer: */
888 osmo_timer_schedule(&mgcp->keepalive_tx_timer, mgcp->actual.keepalive.req_interval_sec, 0);
889}
890
891static void mgcp_client_keepalive_rx_timer_cb(void *data)
892{
893 struct mgcp_client *mgcp = (struct mgcp_client *)data;
894 LOGPMGW(mgcp, LOGL_ERROR, "MGCP link to MGW now considered DOWN (keepalive timeout, more than %u seconds with no answer from MGW)\n",
895 mgcp->actual.keepalive.timeout_sec);
896 mgcp->conn_up = false;
897 /* TODO: Potentially time out all ongoing transactions for that MGW. Maybe based on VTY cfg? */
898}
899
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200900struct mgcp_client *mgcp_client_init(void *ctx,
901 struct mgcp_client_conf *conf)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200902{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200903 struct mgcp_client *mgcp;
Philipp Maier3f2c15f2021-07-22 11:53:07 +0200904 struct reset_ep *reset_ep;
905 struct reset_ep *actual_reset_ep;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200906
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200907 mgcp = talloc_zero(ctx, struct mgcp_client);
Harald Welteefe15712020-07-15 10:56:45 +0200908 if (!mgcp)
909 return NULL;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200910
911 INIT_LLIST_HEAD(&mgcp->responses_pending);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200912
913 mgcp->next_trans_id = 1;
914
915 mgcp->actual.local_addr = conf->local_addr ? conf->local_addr :
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200916 MGCP_CLIENT_LOCAL_ADDR_DEFAULT;
Pau Espin Pedrol8e74c632022-10-17 19:20:45 +0200917 mgcp->actual.local_port = conf->local_port > 0 ? (uint16_t)conf->local_port :
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200918 MGCP_CLIENT_LOCAL_PORT_DEFAULT;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200919
920 mgcp->actual.remote_addr = conf->remote_addr ? conf->remote_addr :
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200921 MGCP_CLIENT_REMOTE_ADDR_DEFAULT;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200922 mgcp->actual.remote_port = conf->remote_port >= 0 ? (uint16_t)conf->remote_port :
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200923 MGCP_CLIENT_REMOTE_PORT_DEFAULT;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200924
Neels Hofmeyrac69ea92018-12-19 00:27:50 +0100925 if (osmo_strlcpy(mgcp->actual.endpoint_domain_name, conf->endpoint_domain_name,
926 sizeof(mgcp->actual.endpoint_domain_name))
927 >= sizeof(mgcp->actual.endpoint_domain_name)) {
Philipp Maierdf9192e2021-09-03 17:50:51 +0200928 LOGPMGW(mgcp, LOGL_ERROR, "MGCP client: endpoint domain name is too long, max length is %zu: '%s'\n",
929 sizeof(mgcp->actual.endpoint_domain_name) - 1, conf->endpoint_domain_name);
Neels Hofmeyrac69ea92018-12-19 00:27:50 +0100930 talloc_free(mgcp);
931 return NULL;
932 }
Philipp Maierdf9192e2021-09-03 17:50:51 +0200933 LOGPMGW(mgcp, LOGL_NOTICE, "MGCP client: using endpoint domain '@%s'\n", mgcp_client_endpoint_domain(mgcp));
Neels Hofmeyrac69ea92018-12-19 00:27:50 +0100934
Philipp Maier3f2c15f2021-07-22 11:53:07 +0200935 INIT_LLIST_HEAD(&mgcp->actual.reset_epnames);
936 llist_for_each_entry(reset_ep, &conf->reset_epnames, list) {
937 actual_reset_ep = talloc_memdup(mgcp, reset_ep, sizeof(*reset_ep));
938 llist_add_tail(&actual_reset_ep->list, &mgcp->actual.reset_epnames);
939 }
940
Philipp Maierdf9192e2021-09-03 17:50:51 +0200941 if (conf->description)
942 mgcp->actual.description = talloc_strdup(mgcp, conf->description);
943
Pau Espin Pedrol563386e2023-06-14 12:20:49 +0200944 osmo_wqueue_init(&mgcp->wq, 1024);
945 mgcp->wq.read_cb = mgcp_do_read;
946 mgcp->wq.write_cb = mgcp_do_write;
947 osmo_fd_setup(&mgcp->wq.bfd, -1, OSMO_FD_READ, osmo_wqueue_bfd_cb, mgcp, 0);
948
949 memcpy(&mgcp->actual.keepalive, &conf->keepalive, sizeof(conf->keepalive));
950 osmo_timer_setup(&mgcp->keepalive_tx_timer, mgcp_client_keepalive_tx_timer_cb, mgcp);
951 osmo_timer_setup(&mgcp->keepalive_rx_timer, mgcp_client_keepalive_rx_timer_cb, mgcp);
952
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200953 return mgcp;
954}
955
Philipp Maier3f2c15f2021-07-22 11:53:07 +0200956/*! Initialize client connection (opens socket)
Philipp Maier275ac972018-01-20 00:58:16 +0100957 * \param[in,out] mgcp MGCP client descriptor.
958 * \returns 0 on success, -EINVAL on error. */
Pau Espin Pedrol8e74c632022-10-17 19:20:45 +0200959int mgcp_client_connect(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200960{
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200961 int rc;
Philipp Maier3f2c15f2021-07-22 11:53:07 +0200962 struct reset_ep *reset_ep;
963 const char *epname;
Pau Espin Pedrol563386e2023-06-14 12:20:49 +0200964 bool some_dlcx_sent = false;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200965
966 if (!mgcp) {
Philipp Maierdf9192e2021-09-03 17:50:51 +0200967 LOGPMGW(mgcp, LOGL_FATAL, "Client not initialized properly\n");
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200968 return -EINVAL;
969 }
970
Pau Espin Pedrol563386e2023-06-14 12:20:49 +0200971 rc = osmo_sock_init2_ofd(&mgcp->wq.bfd, AF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, mgcp->actual.local_addr,
Pau Espin Pedrol8e74c632022-10-17 19:20:45 +0200972 mgcp->actual.local_port, mgcp->actual.remote_addr, mgcp->actual.remote_port,
973 OSMO_SOCK_F_BIND | OSMO_SOCK_F_CONNECT);
Harald Welte8890dfa2017-11-17 15:09:30 +0100974 if (rc < 0) {
Philipp Maierdf9192e2021-09-03 17:50:51 +0200975 LOGPMGW(mgcp, LOGL_FATAL,
976 "Failed to initialize socket %s:%u -> %s:%u for MGW: %s\n",
Philipp Maier276a4142021-07-29 11:55:14 +0200977 mgcp->actual.local_addr ? mgcp->actual.local_addr : "(any)", mgcp->actual.local_port,
978 mgcp->actual.remote_addr ? mgcp->actual.local_addr : "(any)", mgcp->actual.remote_port,
979 strerror(errno));
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200980 goto error_close_fd;
981 }
982
Pau Espin Pedrol563386e2023-06-14 12:20:49 +0200983 LOGPMGW(mgcp, LOGL_INFO, "MGW connection: %s\n", osmo_sock_get_name2(mgcp->wq.bfd.fd));
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200984
Philipp Maier3f2c15f2021-07-22 11:53:07 +0200985 /* If configured, send a DLCX message to the endpoints that are configured to
986 * be reset on startup. Usually this is a wildcarded endpoint. */
987 llist_for_each_entry(reset_ep, &mgcp->actual.reset_epnames, list) {
988 epname = _mgcp_client_name_append_domain(mgcp, reset_ep->name);
Philipp Maierdf9192e2021-09-03 17:50:51 +0200989 LOGPMGW(mgcp, LOGL_INFO, "Sending DLCX to: %s\n", epname);
Philipp Maier3f2c15f2021-07-22 11:53:07 +0200990 _mgcp_client_send_dlcx(mgcp, epname);
Pau Espin Pedrol563386e2023-06-14 12:20:49 +0200991 some_dlcx_sent = true;
Philipp Maier3f2c15f2021-07-22 11:53:07 +0200992 }
Pau Espin Pedrol563386e2023-06-14 12:20:49 +0200993
Pau Espin Pedrol4c065892023-06-26 18:44:44 +0200994 if (mgcp->actual.keepalive.req_interval_sec > 0) {
995 if (!some_dlcx_sent) {
Pau Espin Pedrol563386e2023-06-14 12:20:49 +0200996 /* Attempt an immediate probe to find out if link is UP or DOWN: */
997 osmo_timer_schedule(&mgcp->keepalive_tx_timer, 0, 0);
Pau Espin Pedrol563386e2023-06-14 12:20:49 +0200998 }
Pau Espin Pedrol4c065892023-06-26 18:44:44 +0200999 /* else: keepalive_tx_timer was already scheduled (if needed) down in the stack during Tx DLCX above */
1000 } else {
1001 /* Assume link is UP by default, so that this MGW can be selected: */
1002 mgcp->conn_up = true;
Pau Espin Pedrol563386e2023-06-14 12:20:49 +02001003 }
Pau Espin Pedrol563386e2023-06-14 12:20:49 +02001004
1005 if (mgcp->actual.keepalive.timeout_sec > 0)
1006 osmo_timer_schedule(&mgcp->keepalive_rx_timer, mgcp->actual.keepalive.timeout_sec, 0);
1007
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001008 return 0;
1009error_close_fd:
Pau Espin Pedrol563386e2023-06-14 12:20:49 +02001010 close(mgcp->wq.bfd.fd);
1011 mgcp->wq.bfd.fd = -1;
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001012 return rc;
1013}
1014
Pau Espin Pedrol8e74c632022-10-17 19:20:45 +02001015/*! DEPRECATED: Initialize client connection (opens socket)
Philipp Maier3f4a4cb2021-07-26 13:20:05 +02001016 * \param[in,out] mgcp MGCP client descriptor.
1017 * \returns 0 on success, -EINVAL on error. */
Pau Espin Pedrol8e74c632022-10-17 19:20:45 +02001018int mgcp_client_connect2(struct mgcp_client *mgcp, unsigned int retry_n_ports)
Philipp Maier3f4a4cb2021-07-26 13:20:05 +02001019{
Pau Espin Pedrol8e74c632022-10-17 19:20:45 +02001020 return mgcp_client_connect(mgcp);
Philipp Maier3f4a4cb2021-07-26 13:20:05 +02001021}
1022
1023/*! Terminate client connection
1024 * \param[in,out] mgcp MGCP client descriptor.
1025 * \returns 0 on success, -EINVAL on error. */
1026void mgcp_client_disconnect(struct mgcp_client *mgcp)
1027{
1028 struct osmo_wqueue *wq;
1029
1030 if (!mgcp) {
Philipp Maierdf9192e2021-09-03 17:50:51 +02001031 LOGP(DLMGCP, LOGL_FATAL, "MGCP client not initialized properly\n");
Philipp Maier3f4a4cb2021-07-26 13:20:05 +02001032 return;
1033 }
1034
Pau Espin Pedrol563386e2023-06-14 12:20:49 +02001035 /* Disarm keepalive Tx/Rx timer until next connect() */
1036 osmo_timer_del(&mgcp->keepalive_rx_timer);
1037 osmo_timer_del(&mgcp->keepalive_tx_timer);
1038 mgcp->conn_up = false;
1039
Philipp Maier3f4a4cb2021-07-26 13:20:05 +02001040 wq = &mgcp->wq;
1041 osmo_wqueue_clear(wq);
Philipp Maierdf9192e2021-09-03 17:50:51 +02001042 LOGPMGW(mgcp, LOGL_INFO, "MGCP association: %s -- closed!\n", osmo_sock_get_name2(wq->bfd.fd));
Philipp Maier3f4a4cb2021-07-26 13:20:05 +02001043 if (osmo_fd_is_registered(&wq->bfd))
1044 osmo_fd_unregister(&wq->bfd);
Pau Espin Pedrol747fbce2023-03-14 11:48:27 +01001045 close(wq->bfd.fd);
1046 wq->bfd.fd = -1;
Philipp Maier3f4a4cb2021-07-26 13:20:05 +02001047}
1048
Philipp Maier275ac972018-01-20 00:58:16 +01001049/*! Get the IP-Aaddress of the associated MGW as string.
1050 * \param[in] mgcp MGCP client descriptor.
1051 * \returns a pointer to the address string. */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +02001052const char *mgcp_client_remote_addr_str(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001053{
1054 return mgcp->actual.remote_addr;
1055}
1056
Philipp Maier275ac972018-01-20 00:58:16 +01001057/*! Get the IP-Port of the associated MGW.
1058 * \param[in] mgcp MGCP client descriptor.
1059 * \returns port number. */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +02001060uint16_t mgcp_client_remote_port(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001061{
1062 return mgcp->actual.remote_port;
1063}
1064
Pau Espin Pedrol74d0e5c2020-09-02 17:19:20 +02001065/*! Get the IP-Address of the associated MGW as its numeric representation.
1066 * DEPRECATED, DON'T USE.
Philipp Maier275ac972018-01-20 00:58:16 +01001067 * \param[in] mgcp MGCP client descriptor.
1068 * \returns IP-Address as 32 bit integer (network byte order) */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +02001069uint32_t mgcp_client_remote_addr_n(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001070{
Pau Espin Pedrol74d0e5c2020-09-02 17:19:20 +02001071 return 0;
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001072}
1073
Neels Hofmeyrac69ea92018-12-19 00:27:50 +01001074/* To compose endpoint names, usually for CRCX, use this as domain name.
1075 * For example, snprintf("rtpbridge\*@%s", mgcp_client_endpoint_domain(mgcp)). */
1076const char *mgcp_client_endpoint_domain(const struct mgcp_client *mgcp)
1077{
1078 return mgcp->actual.endpoint_domain_name[0] ? mgcp->actual.endpoint_domain_name : "mgw";
1079}
1080
Philipp Maiera466f572020-06-25 16:11:04 +02001081/*! Compose endpoint name for a wildcarded request to the virtual trunk
1082 * \param[in] mgcp MGCP client descriptor.
1083 * \returns string containing the endpoint name (e.g. rtpbridge\*@mgw) */
Pau Espin Pedrolca0818c2019-04-16 17:23:38 +02001084const char *mgcp_client_rtpbridge_wildcard(const struct mgcp_client *mgcp)
1085{
1086 return _mgcp_client_name_append_domain(mgcp, "rtpbridge/*");
1087}
1088
Philipp Maier48635912020-06-25 20:16:22 +02001089/*! Compose endpoint name for an E1 endpoint.
1090 * \param[in] ctx talloc context.
1091 * \param[in] mgcp MGCP client descriptor.
1092 * \param[in] trunk_id id number of the E1 trunk (1-64).
1093 * \param[in] ts timeslot on the E1 trunk (1-31).
1094 * \param[in] rate bitrate used on the E1 trunk (e.g 16 for 16kbit).
1095 * \param[in] offset bit offset of the E1 subslot (e.g. 4 for the third 16k subslot).
1096 * \returns string containing the endpoint name (e.g. ds/e1-1/s-1/su16-4). */
1097const char *mgcp_client_e1_epname(void *ctx, const struct mgcp_client *mgcp, uint8_t trunk_id, uint8_t ts,
1098 uint8_t rate, uint8_t offset)
1099{
1100 /* See also comment in libosmo-mgcp, mgcp_client.c, gen_e1_epname() */
1101 const uint8_t valid_rates[] = { 64, 32, 32, 16, 16, 16, 16, 8, 8, 8, 8, 8, 8, 8, 8 };
1102 const uint8_t valid_offsets[] = { 0, 0, 4, 0, 2, 4, 6, 0, 1, 2, 3, 4, 5, 6, 7 };
1103
1104 uint8_t i;
1105 bool rate_offs_valid = false;
1106 char *epname;
1107
1108 epname =
1109 talloc_asprintf(ctx, "ds/e1-%u/s-%u/su%u-%u@%s", trunk_id, ts, rate, offset,
1110 mgcp_client_endpoint_domain(mgcp));
1111 if (!epname) {
Philipp Maierdf9192e2021-09-03 17:50:51 +02001112 LOGPMGW(mgcp, LOGL_ERROR, "Cannot compose MGCP e1-endpoint name!\n");
Philipp Maier48635912020-06-25 20:16:22 +02001113 return NULL;
1114 }
1115
1116 /* Check if the supplied rate/offset pair resembles a valid combination */
1117 for (i = 0; i < sizeof(valid_rates); i++) {
1118 if (valid_rates[i] == rate && valid_offsets[i] == offset)
1119 rate_offs_valid = true;
1120 }
1121 if (!rate_offs_valid) {
Philipp Maierdf9192e2021-09-03 17:50:51 +02001122 LOGPMGW(mgcp, LOGL_ERROR,
1123 "Cannot compose MGCP e1-endpoint name (%s), rate(%u)/offset(%u) combination is invalid!\n",
1124 epname, rate, offset);
Philipp Maier48635912020-06-25 20:16:22 +02001125 talloc_free(epname);
1126 return NULL;
1127 }
1128
1129 /* An E1 line has a maximum of 32 timeslots, while the first (ts=0) is
1130 * reserverd for framing and alignment, so we can not use it here. */
Philipp Maier92a73cd2020-11-26 22:21:11 +01001131 if (ts == 0 || ts > NUM_E1_TS-1) {
Philipp Maierdf9192e2021-09-03 17:50:51 +02001132 LOGPMGW(mgcp, LOGL_ERROR,
1133 "Cannot compose MGCP e1-endpoint name (%s), E1-timeslot number (%u) is invalid!\n",
1134 epname, ts);
Philipp Maier48635912020-06-25 20:16:22 +02001135 talloc_free(epname);
1136 return NULL;
1137 }
1138
1139 return epname;
1140}
1141
Philipp Maier30bdf942023-02-20 12:40:46 +01001142struct mgcp_response_pending *mgcp_client_pending_add(struct mgcp_client *mgcp, mgcp_trans_id_t trans_id,
1143 mgcp_response_cb_t response_cb, void *priv)
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001144{
1145 struct mgcp_response_pending *pending;
1146
1147 pending = talloc_zero(mgcp, struct mgcp_response_pending);
Harald Welte827da2c2020-07-15 10:57:08 +02001148 if (!pending)
1149 return NULL;
1150
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001151 pending->trans_id = trans_id;
1152 pending->response_cb = response_cb;
1153 pending->priv = priv;
1154 llist_add_tail(&pending->entry, &mgcp->responses_pending);
1155
1156 return pending;
1157}
1158
Philipp Maierdf9192e2021-09-03 17:50:51 +02001159/* Send the MGCP message in msg to the MGW and handle a response with
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001160 * response_cb. NOTE: the response_cb still needs to call
1161 * mgcp_response_parse_params(response) to get the parsed parameters -- to
1162 * potentially save some CPU cycles, only the head line has been parsed when
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +01001163 * the response_cb is invoked.
1164 * Before the priv pointer becomes invalid, e.g. due to transaction timeout,
1165 * mgcp_client_cancel() needs to be called for this transaction.
1166 */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +02001167int mgcp_client_tx(struct mgcp_client *mgcp, struct msgb *msg,
1168 mgcp_response_cb_t response_cb, void *priv)
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001169{
Harald Welte563ffc52020-06-17 21:54:13 +07001170 struct mgcp_response_pending *pending = NULL;
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001171 mgcp_trans_id_t trans_id;
1172 int rc;
1173
1174 trans_id = msg->cb[MSGB_CB_MGCP_TRANS_ID];
1175 if (!trans_id) {
Philipp Maierdf9192e2021-09-03 17:50:51 +02001176 LOGPMGW(mgcp, LOGL_ERROR,
1177 "Unset transaction id in mgcp send request\n");
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001178 talloc_free(msg);
1179 return -EINVAL;
1180 }
1181
Harald Welte563ffc52020-06-17 21:54:13 +07001182 /* Do not allocate a dummy 'mgcp_response_pending' entry */
1183 if (response_cb != NULL) {
1184 pending = mgcp_client_pending_add(mgcp, trans_id, response_cb, priv);
1185 if (!pending) {
1186 talloc_free(msg);
1187 return -ENOMEM;
1188 }
Harald Welte827da2c2020-07-15 10:57:08 +02001189 }
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001190
1191 if (msgb_l2len(msg) > 4096) {
Philipp Maierdf9192e2021-09-03 17:50:51 +02001192 LOGPMGW(mgcp, LOGL_ERROR,
1193 "Cannot send, MGCP message too large: %u\n",
1194 msgb_l2len(msg));
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001195 msgb_free(msg);
1196 rc = -EINVAL;
1197 goto mgcp_tx_error;
1198 }
1199
1200 rc = osmo_wqueue_enqueue(&mgcp->wq, msg);
1201 if (rc) {
Philipp Maierdf9192e2021-09-03 17:50:51 +02001202 LOGPMGW(mgcp, LOGL_FATAL, "Could not queue message to MGW\n");
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001203 msgb_free(msg);
1204 goto mgcp_tx_error;
1205 } else
Philipp Maierdf9192e2021-09-03 17:50:51 +02001206 LOGPMGW(mgcp, LOGL_DEBUG, "Queued %u bytes for MGW\n",
1207 msgb_l2len(msg));
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001208 return 0;
1209
1210mgcp_tx_error:
Harald Welte563ffc52020-06-17 21:54:13 +07001211 if (!pending)
Vadim Yanitskiyffbc6182020-07-16 15:48:13 +07001212 return rc;
Vadim Yanitskiy3f8139c2020-06-17 20:50:17 +07001213 /* Dequeue pending response, it's going to be free()d */
1214 llist_del(&pending->entry);
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001215 /* Pass NULL to response cb to indicate an error */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +02001216 mgcp_client_handle_response(mgcp, pending, NULL);
Vadim Yanitskiyffbc6182020-07-16 15:48:13 +07001217 return rc;
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001218}
1219
Philipp Maier275ac972018-01-20 00:58:16 +01001220/*! Cancel a pending transaction.
1221 * \param[in] mgcp MGCP client descriptor.
1222 * \param[in,out] trans_id Transaction id.
1223 * \returns 0 on success, -ENOENT on error.
1224 *
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +01001225 * Should a priv pointer passed to mgcp_client_tx() become invalid, this function must be called. In
1226 * practical terms, if the caller of mgcp_client_tx() wishes to tear down a transaction without having
1227 * received a response this function must be called. The trans_id can be obtained by calling
Philipp Maier275ac972018-01-20 00:58:16 +01001228 * mgcp_msg_trans_id() on the msgb produced by mgcp_msg_gen(). */
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +01001229int mgcp_client_cancel(struct mgcp_client *mgcp, mgcp_trans_id_t trans_id)
1230{
1231 struct mgcp_response_pending *pending = mgcp_client_response_pending_get(mgcp, trans_id);
1232 if (!pending) {
Philipp Maier275ac972018-01-20 00:58:16 +01001233 /*! Note: it is not harmful to cancel a transaction twice. */
Philipp Maierdf9192e2021-09-03 17:50:51 +02001234 LOGPMGW(mgcp, LOGL_ERROR, "Cannot cancel, no such transaction: %u\n", trans_id);
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +01001235 return -ENOENT;
1236 }
Philipp Maierdf9192e2021-09-03 17:50:51 +02001237 LOGPMGW(mgcp, LOGL_DEBUG, "Canceled transaction %u\n", trans_id);
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +01001238 talloc_free(pending);
1239 return 0;
Philipp Maier275ac972018-01-20 00:58:16 +01001240 /*! We don't really need to clean up the wqueue: In all sane cases, the msgb has already been sent
1241 * out and is no longer in the wqueue. If it still is in the wqueue, then sending MGCP messages
1242 * per se is broken and the program should notice so by a full wqueue. Even if this was called
1243 * before we had a chance to send out the message and it is still going to be sent, we will just
1244 * ignore the reply to it later. Removing a msgb from the wqueue here would just introduce more
1245 * bug surface in terms of failing to update wqueue API's counters or some such.
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +01001246 */
1247}
1248
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +02001249static mgcp_trans_id_t mgcp_client_next_trans_id(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001250{
1251 /* avoid zero trans_id to distinguish from unset trans_id */
1252 if (!mgcp->next_trans_id)
1253 mgcp->next_trans_id ++;
1254 return mgcp->next_trans_id ++;
1255}
1256
Philipp Maier1dc6be62017-10-05 18:25:37 +02001257#define MGCP_CRCX_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT | \
1258 MGCP_MSG_PRESENCE_CALL_ID | \
Philipp Maier1dc6be62017-10-05 18:25:37 +02001259 MGCP_MSG_PRESENCE_CONN_MODE)
1260#define MGCP_MDCX_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT | \
Philipp Maier490cbaa2018-01-22 17:32:38 +01001261 MGCP_MSG_PRESENCE_CALL_ID | \
Philipp Maier1dc6be62017-10-05 18:25:37 +02001262 MGCP_MSG_PRESENCE_CONN_ID)
1263#define MGCP_DLCX_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT)
1264#define MGCP_AUEP_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT)
1265#define MGCP_RSIP_MANDATORY 0 /* none */
1266
Philipp Maier704c4f02018-06-07 18:51:31 +02001267/* Helper function for mgcp_msg_gen(): Add LCO information to MGCP message */
1268static int add_lco(struct msgb *msg, struct mgcp_msg *mgcp_msg)
1269{
1270 unsigned int i;
Philipp Maier704c4f02018-06-07 18:51:31 +02001271 const char *codec;
Philipp Maier704c4f02018-06-07 18:51:31 +02001272
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001273#define MSGB_PRINTF_OR_RET(FMT, ARGS...) do { \
1274 if (msgb_printf(msg, FMT, ##ARGS) != 0) { \
1275 LOGP(DLMGCP, LOGL_ERROR, "Message buffer too small, can not generate MGCP/SDP message\n"); \
1276 return -ENOBUFS; \
1277 } \
1278 } while (0)
1279
1280 MSGB_PRINTF_OR_RET("L:");
Philipp Maier704c4f02018-06-07 18:51:31 +02001281
1282 if (mgcp_msg->ptime)
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001283 MSGB_PRINTF_OR_RET(" p:%u,", mgcp_msg->ptime);
Philipp Maier704c4f02018-06-07 18:51:31 +02001284
Neels Hofmeyrcc2f7932023-12-07 03:46:46 +01001285 if (mgcp_msg->ptmap_len) {
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001286 MSGB_PRINTF_OR_RET(" a:");
Neels Hofmeyrcc2f7932023-12-07 03:46:46 +01001287 for (i = 0; i < mgcp_msg->ptmap_len; i++) {
1288 codec = get_value_string_or_null(osmo_mgcpc_codec_names, mgcp_msg->ptmap[i].codec);
Pau Espin Pedrolc12bfb72019-04-16 17:23:09 +02001289
Philipp Maier704c4f02018-06-07 18:51:31 +02001290 /* Note: Use codec descriptors from enum mgcp_codecs
1291 * in mgcp_client only! */
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001292 if (!codec)
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001293 return -EINVAL;
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001294
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001295 MSGB_PRINTF_OR_RET("%s", extract_codec_name(codec));
Neels Hofmeyrcc2f7932023-12-07 03:46:46 +01001296 if (i < mgcp_msg->ptmap_len - 1)
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001297 MSGB_PRINTF_OR_RET(";");
Philipp Maier704c4f02018-06-07 18:51:31 +02001298 }
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001299 MSGB_PRINTF_OR_RET(",");
Philipp Maier704c4f02018-06-07 18:51:31 +02001300 }
1301
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001302 MSGB_PRINTF_OR_RET(" nt:IN\r\n");
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001303 return 0;
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001304
1305#undef MSGB_PRINTF_OR_RET
Philipp Maier704c4f02018-06-07 18:51:31 +02001306}
1307
1308/* Helper function for mgcp_msg_gen(): Add SDP information to MGCP message */
1309static int add_sdp(struct msgb *msg, struct mgcp_msg *mgcp_msg, struct mgcp_client *mgcp)
1310{
1311 unsigned int i;
Pau Espin Pedrol8667d512020-08-28 19:37:08 +02001312 char local_ip[INET6_ADDRSTRLEN];
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +02001313 int local_ip_family, audio_ip_family;
Philipp Maier704c4f02018-06-07 18:51:31 +02001314 const char *codec;
1315 unsigned int pt;
1316
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001317#define MSGB_PRINTF_OR_RET(FMT, ARGS...) do { \
1318 if (msgb_printf(msg, FMT, ##ARGS) != 0) { \
1319 LOGPMGW(mgcp, LOGL_ERROR, "Message buffer too small, can not generate MGCP message (SDP)\n"); \
1320 return -ENOBUFS; \
1321 } \
1322 } while (0)
1323
Philipp Maier704c4f02018-06-07 18:51:31 +02001324 /* Add separator to mark the beginning of the SDP block */
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001325 MSGB_PRINTF_OR_RET("\r\n");
Philipp Maier704c4f02018-06-07 18:51:31 +02001326
1327 /* Add SDP protocol version */
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001328 MSGB_PRINTF_OR_RET("v=0\r\n");
Philipp Maier704c4f02018-06-07 18:51:31 +02001329
1330 /* Determine local IP-Address */
1331 if (osmo_sock_local_ip(local_ip, mgcp->actual.remote_addr) < 0) {
Philipp Maierdf9192e2021-09-03 17:50:51 +02001332 LOGPMGW(mgcp, LOGL_ERROR,
1333 "Could not determine local IP-Address!\n");
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001334 return -EINVAL;
Philipp Maier704c4f02018-06-07 18:51:31 +02001335 }
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +02001336 local_ip_family = osmo_ip_str_type(local_ip);
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001337 if (local_ip_family == AF_UNSPEC)
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001338 return -EINVAL;
Philipp Maier704c4f02018-06-07 18:51:31 +02001339
1340 /* Add owner/creator (SDP) */
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001341 MSGB_PRINTF_OR_RET("o=- %x 23 IN IP%c %s\r\n", mgcp_msg->call_id,
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +02001342 local_ip_family == AF_INET6 ? '6' : '4',
1343 local_ip);
Philipp Maier704c4f02018-06-07 18:51:31 +02001344
1345 /* Add session name (none) */
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001346 MSGB_PRINTF_OR_RET("s=-\r\n");
Philipp Maier704c4f02018-06-07 18:51:31 +02001347
Pau Espin Pedrola5acaa62023-11-28 18:28:44 +01001348 /* Add RTP address */
1349 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_AUDIO_IP) {
1350 audio_ip_family = osmo_ip_str_type(mgcp_msg->audio_ip);
1351 if (audio_ip_family == AF_UNSPEC)
1352 return -EINVAL;
1353 if (strlen(mgcp_msg->audio_ip) <= 0) {
1354 LOGPMGW(mgcp, LOGL_ERROR,
1355 "Empty ip address, can not generate MGCP message\n");
1356 return -EINVAL;
1357 }
1358 MSGB_PRINTF_OR_RET("c=IN IP%c %s\r\n",
1359 audio_ip_family == AF_INET6 ? '6' : '4',
1360 mgcp_msg->audio_ip);
Philipp Maier704c4f02018-06-07 18:51:31 +02001361 }
Philipp Maier704c4f02018-06-07 18:51:31 +02001362
1363 /* Add time description, active time (SDP) */
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001364 MSGB_PRINTF_OR_RET("t=0 0\r\n");
Philipp Maier704c4f02018-06-07 18:51:31 +02001365
Pau Espin Pedrola5acaa62023-11-28 18:28:44 +01001366 /* Add RTP address port and codecs */
1367 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_AUDIO_PORT) {
1368 if (mgcp_msg->audio_port == 0) {
1369 LOGPMGW(mgcp, LOGL_ERROR,
1370 "Invalid port number, can not generate MGCP message\n");
1371 return -EINVAL;
1372 }
1373 MSGB_PRINTF_OR_RET("m=audio %u RTP/AVP", mgcp_msg->audio_port);
Neels Hofmeyrcc2f7932023-12-07 03:46:46 +01001374 for (i = 0; i < mgcp_msg->ptmap_len; i++)
1375 MSGB_PRINTF_OR_RET(" %u", mgcp_msg->ptmap[i].pt);
Pau Espin Pedrola5acaa62023-11-28 18:28:44 +01001376 MSGB_PRINTF_OR_RET("\r\n");
Philipp Maier704c4f02018-06-07 18:51:31 +02001377 }
Philipp Maier704c4f02018-06-07 18:51:31 +02001378
Philipp Maier228e5912019-03-05 13:56:59 +01001379 /* Add optional codec parameters (fmtp) */
1380 if (mgcp_msg->param_present) {
Neels Hofmeyrcc2f7932023-12-07 03:46:46 +01001381 for (i = 0; i < mgcp_msg->ptmap_len; i++) {
Philipp Maier228e5912019-03-05 13:56:59 +01001382 /* The following is only applicable for AMR */
Neels Hofmeyrcc2f7932023-12-07 03:46:46 +01001383 if (mgcp_msg->ptmap[i].codec != CODEC_AMR_8000_1
1384 && mgcp_msg->ptmap[i].codec != CODEC_AMRWB_16000_1)
Pau Espin Pedrolf6db4652023-12-05 13:35:41 +01001385 continue;
Neels Hofmeyrcc2f7932023-12-07 03:46:46 +01001386 pt = mgcp_msg->ptmap[i].pt;
Philipp Maier228e5912019-03-05 13:56:59 +01001387 if (mgcp_msg->param.amr_octet_aligned_present && mgcp_msg->param.amr_octet_aligned)
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001388 MSGB_PRINTF_OR_RET("a=fmtp:%u octet-align=1\r\n", pt);
Philipp Maier228e5912019-03-05 13:56:59 +01001389 else if (mgcp_msg->param.amr_octet_aligned_present && !mgcp_msg->param.amr_octet_aligned)
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001390 MSGB_PRINTF_OR_RET("a=fmtp:%u octet-align=0\r\n", pt);
Philipp Maier228e5912019-03-05 13:56:59 +01001391 }
1392 }
1393
Neels Hofmeyrcc2f7932023-12-07 03:46:46 +01001394 for (i = 0; i < mgcp_msg->ptmap_len; i++) {
1395 pt = mgcp_msg->ptmap[i].pt;
Pau Espin Pedrolc12bfb72019-04-16 17:23:09 +02001396
Philipp Maier704c4f02018-06-07 18:51:31 +02001397 /* Note: Only dynamic payload type from the range 96-127
1398 * require to be explained further via rtpmap. All others
1399 * are implcitly definedby the number in m=audio */
1400 if (pt >= 96 && pt <= 127) {
Neels Hofmeyrcc2f7932023-12-07 03:46:46 +01001401 codec = get_value_string_or_null(osmo_mgcpc_codec_names, mgcp_msg->ptmap[i].codec);
Philipp Maier704c4f02018-06-07 18:51:31 +02001402
1403 /* Note: Use codec descriptors from enum mgcp_codecs
1404 * in mgcp_client only! */
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001405 if (!codec)
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001406 return -EINVAL;
Pau Espin Pedrolc12bfb72019-04-16 17:23:09 +02001407
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001408 MSGB_PRINTF_OR_RET("a=rtpmap:%u %s\r\n", pt, codec);
Philipp Maier704c4f02018-06-07 18:51:31 +02001409 }
1410 }
Pau Espin Pedrolc12bfb72019-04-16 17:23:09 +02001411
Philipp Maier704c4f02018-06-07 18:51:31 +02001412 if (mgcp_msg->ptime)
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001413 MSGB_PRINTF_OR_RET("a=ptime:%u\r\n", mgcp_msg->ptime);
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001414
1415 return 0;
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001416#undef MSGB_PRINTF_OR_RET
Philipp Maier704c4f02018-06-07 18:51:31 +02001417}
1418
Philipp Maier275ac972018-01-20 00:58:16 +01001419/*! Generate an MGCP message
1420 * \param[in] mgcp MGCP client descriptor.
1421 * \param[in] mgcp_msg Message description
1422 * \returns message buffer on success, NULL on error. */
Philipp Maier1dc6be62017-10-05 18:25:37 +02001423struct msgb *mgcp_msg_gen(struct mgcp_client *mgcp, struct mgcp_msg *mgcp_msg)
1424{
1425 mgcp_trans_id_t trans_id = mgcp_client_next_trans_id(mgcp);
1426 uint32_t mandatory_mask;
1427 struct msgb *msg = msgb_alloc_headroom(4096, 128, "MGCP tx");
Philipp Maier704c4f02018-06-07 18:51:31 +02001428 bool use_sdp = false;
Pau Espin Pedrol900cd652019-04-24 22:06:22 +02001429 char buf[32];
Philipp Maier1dc6be62017-10-05 18:25:37 +02001430
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001431#define MSGB_PRINTF_OR_RET(FMT, ARGS...) do { \
1432 if (msgb_printf(msg, FMT, ##ARGS) != 0) { \
1433 LOGPMGW(mgcp, LOGL_ERROR, "Message buffer too small, can not generate MGCP/SDP message\n"); \
1434 goto exit_error; \
1435 } \
1436 } while (0)
1437
Philipp Maier1dc6be62017-10-05 18:25:37 +02001438 msg->l2h = msg->data;
1439 msg->cb[MSGB_CB_MGCP_TRANS_ID] = trans_id;
1440
1441 /* Add command verb */
1442 switch (mgcp_msg->verb) {
1443 case MGCP_VERB_CRCX:
1444 mandatory_mask = MGCP_CRCX_MANDATORY;
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001445 MSGB_PRINTF_OR_RET("CRCX %u", trans_id);
Philipp Maier1dc6be62017-10-05 18:25:37 +02001446 break;
1447 case MGCP_VERB_MDCX:
1448 mandatory_mask = MGCP_MDCX_MANDATORY;
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001449 MSGB_PRINTF_OR_RET("MDCX %u", trans_id);
Philipp Maier1dc6be62017-10-05 18:25:37 +02001450 break;
1451 case MGCP_VERB_DLCX:
1452 mandatory_mask = MGCP_DLCX_MANDATORY;
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001453 MSGB_PRINTF_OR_RET("DLCX %u", trans_id);
Philipp Maier1dc6be62017-10-05 18:25:37 +02001454 break;
1455 case MGCP_VERB_AUEP:
1456 mandatory_mask = MGCP_AUEP_MANDATORY;
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001457 MSGB_PRINTF_OR_RET("AUEP %u", trans_id);
Philipp Maier1dc6be62017-10-05 18:25:37 +02001458 break;
1459 case MGCP_VERB_RSIP:
1460 mandatory_mask = MGCP_RSIP_MANDATORY;
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001461 MSGB_PRINTF_OR_RET("RSIP %u", trans_id);
Philipp Maier1dc6be62017-10-05 18:25:37 +02001462 break;
1463 default:
Philipp Maierdf9192e2021-09-03 17:50:51 +02001464 LOGPMGW(mgcp, LOGL_ERROR, "Invalid command verb, can not generate MGCP message\n");
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001465 goto exit_error;
Philipp Maier1dc6be62017-10-05 18:25:37 +02001466 }
1467
1468 /* Check if mandatory fields are missing */
1469 if (!((mgcp_msg->presence & mandatory_mask) == mandatory_mask)) {
Philipp Maierdf9192e2021-09-03 17:50:51 +02001470 LOGPMGW(mgcp, LOGL_ERROR,
1471 "One or more missing mandatory fields, can not generate MGCP message\n");
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001472 goto exit_error;
Philipp Maier1dc6be62017-10-05 18:25:37 +02001473 }
1474
1475 /* Add endpoint name */
Philipp Maier7bc55522017-12-10 22:52:22 +01001476 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_ENDPOINT) {
1477 if (strlen(mgcp_msg->endpoint) <= 0) {
Philipp Maierdf9192e2021-09-03 17:50:51 +02001478 LOGPMGW(mgcp, LOGL_ERROR, "Empty endpoint name, can not generate MGCP message\n");
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001479 goto exit_error;
Philipp Maier7bc55522017-12-10 22:52:22 +01001480 }
Philipp Maier3aa81572018-01-31 14:13:45 +01001481
1482 if (strstr(mgcp_msg->endpoint, "@") == NULL) {
Philipp Maierdf9192e2021-09-03 17:50:51 +02001483 LOGPMGW(mgcp, LOGL_ERROR,
1484 "Endpoint name (%s) lacks separator (@), can not generate MGCP message\n",
1485 mgcp_msg->endpoint);
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001486 goto exit_error;
Philipp Maier3aa81572018-01-31 14:13:45 +01001487 }
1488
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001489 MSGB_PRINTF_OR_RET(" %s", mgcp_msg->endpoint);
Philipp Maier7bc55522017-12-10 22:52:22 +01001490 }
Philipp Maier1dc6be62017-10-05 18:25:37 +02001491
1492 /* Add protocol version */
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001493 MSGB_PRINTF_OR_RET(" MGCP 1.0\r\n");
Philipp Maier1dc6be62017-10-05 18:25:37 +02001494
1495 /* Add call id */
1496 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_CALL_ID)
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001497 MSGB_PRINTF_OR_RET("C: %x\r\n", mgcp_msg->call_id);
Philipp Maier1dc6be62017-10-05 18:25:37 +02001498
1499 /* Add connection id */
Philipp Maier7bc55522017-12-10 22:52:22 +01001500 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_CONN_ID) {
1501 if (strlen(mgcp_msg->conn_id) <= 0) {
Philipp Maierdf9192e2021-09-03 17:50:51 +02001502 LOGPMGW(mgcp, LOGL_ERROR, "Empty connection id, can not generate MGCP message\n");
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001503 goto exit_error;
Philipp Maier7bc55522017-12-10 22:52:22 +01001504 }
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001505 MSGB_PRINTF_OR_RET("I: %s\r\n", mgcp_msg->conn_id);
Philipp Maier7bc55522017-12-10 22:52:22 +01001506 }
Philipp Maier1dc6be62017-10-05 18:25:37 +02001507
Pau Espin Pedrola5acaa62023-11-28 18:28:44 +01001508 /* Using SDP makes sense when a valid IP or Port is specified,
Philipp Maier704c4f02018-06-07 18:51:31 +02001509 * if we do not know this information yet, we fall back to LCO */
1510 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_AUDIO_IP
Pau Espin Pedrola5acaa62023-11-28 18:28:44 +01001511 || mgcp_msg->presence & MGCP_MSG_PRESENCE_AUDIO_PORT)
Philipp Maier704c4f02018-06-07 18:51:31 +02001512 use_sdp = true;
1513
1514 /* Add local connection options (LCO) */
1515 if (!use_sdp
1516 && (mgcp_msg->verb == MGCP_VERB_CRCX
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001517 || mgcp_msg->verb == MGCP_VERB_MDCX)) {
Oliver Smith0dbaaad2023-02-22 16:30:02 +01001518 if (add_lco(msg, mgcp_msg) < 0) {
1519 LOGPMGW(mgcp, LOGL_ERROR, "Failed to add LCO, can not generate MGCP message\n");
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001520 goto exit_error;
Oliver Smith0dbaaad2023-02-22 16:30:02 +01001521 }
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001522 }
Philipp Maier1dc6be62017-10-05 18:25:37 +02001523
1524 /* Add mode */
1525 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_CONN_MODE)
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001526 MSGB_PRINTF_OR_RET("M: %s\r\n", mgcp_client_cmode_name(mgcp_msg->conn_mode));
Philipp Maier1dc6be62017-10-05 18:25:37 +02001527
Neels Hofmeyre6d8e912018-08-23 16:36:48 +02001528 /* Add X-Osmo-IGN */
1529 if ((mgcp_msg->presence & MGCP_MSG_PRESENCE_X_OSMO_IGN)
1530 && (mgcp_msg->x_osmo_ign != 0))
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001531 MSGB_PRINTF_OR_RET(MGCP_X_OSMO_IGN_HEADER "%s\r\n",
1532 mgcp_msg->x_osmo_ign & MGCP_X_OSMO_IGN_CALLID ? " C" : "");
Neels Hofmeyre6d8e912018-08-23 16:36:48 +02001533
Pau Espin Pedrol900cd652019-04-24 22:06:22 +02001534 /* Add X-Osmo-Osmux */
1535 if ((mgcp_msg->presence & MGCP_MSG_PRESENCE_X_OSMO_OSMUX_CID)) {
Pau Espin Pedrol1b1d7ed2019-05-23 16:48:24 +02001536 if (mgcp_msg->x_osmo_osmux_cid < -1 || mgcp_msg->x_osmo_osmux_cid > OSMUX_CID_MAX) {
Philipp Maierdf9192e2021-09-03 17:50:51 +02001537 LOGPMGW(mgcp, LOGL_ERROR, "Wrong Osmux CID %d, can not generate MGCP message\n",
1538 mgcp_msg->x_osmo_osmux_cid);
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001539 goto exit_error;
Pau Espin Pedrol1b1d7ed2019-05-23 16:48:24 +02001540 }
Pau Espin Pedrol900cd652019-04-24 22:06:22 +02001541 snprintf(buf, sizeof(buf), " %d", mgcp_msg->x_osmo_osmux_cid);
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001542 MSGB_PRINTF_OR_RET(MGCP_X_OSMO_OSMUX_HEADER "%s\r\n",
1543 mgcp_msg->x_osmo_osmux_cid == -1 ? " *" : buf);
Pau Espin Pedrol900cd652019-04-24 22:06:22 +02001544 }
1545
1546
Philipp Maier704c4f02018-06-07 18:51:31 +02001547 /* Add session description protocol (SDP) */
1548 if (use_sdp
1549 && (mgcp_msg->verb == MGCP_VERB_CRCX
1550 || mgcp_msg->verb == MGCP_VERB_MDCX)) {
Oliver Smith0dbaaad2023-02-22 16:30:02 +01001551 if (add_sdp(msg, mgcp_msg, mgcp) < 0) {
1552 LOGPMGW(mgcp, LOGL_ERROR, "Failed to add SDP, can not generate MGCP message\n");
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001553 goto exit_error;
Oliver Smith0dbaaad2023-02-22 16:30:02 +01001554 }
Philipp Maier1dc6be62017-10-05 18:25:37 +02001555 }
1556
Philipp Maier1dc6be62017-10-05 18:25:37 +02001557 return msg;
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001558
1559exit_error:
1560 msgb_free(msg);
1561 return NULL;
1562#undef MSGB_PRINTF_OR_RET
Philipp Maier1dc6be62017-10-05 18:25:37 +02001563}
1564
Philipp Maier275ac972018-01-20 00:58:16 +01001565/*! Retrieve the MGCP transaction ID from a msgb generated by mgcp_msg_gen()
1566 * \param[in] msg message buffer
1567 * \returns Transaction id. */
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +01001568mgcp_trans_id_t mgcp_msg_trans_id(struct msgb *msg)
1569{
1570 return (mgcp_trans_id_t)msg->cb[MSGB_CB_MGCP_TRANS_ID];
1571}
1572
Philipp Maierfa495d62021-09-02 10:08:56 +02001573/*! Get the configuration parameters for a given MGCP client instance
Philipp Maier275ac972018-01-20 00:58:16 +01001574 * \param[in] mgcp MGCP client descriptor.
1575 * \returns configuration */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +02001576struct mgcp_client_conf *mgcp_client_conf_actual(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001577{
1578 return &mgcp->actual;
1579}
Neels Hofmeyrd95ab1e2017-09-22 00:52:54 +02001580
1581const struct value_string mgcp_client_connection_mode_strs[] = {
1582 { MGCP_CONN_NONE, "none" },
1583 { MGCP_CONN_RECV_SEND, "sendrecv" },
1584 { MGCP_CONN_SEND_ONLY, "sendonly" },
1585 { MGCP_CONN_RECV_ONLY, "recvonly" },
Andreas Eversbergdc7dfd02023-07-05 12:36:49 +02001586 { MGCP_CONN_CONFECHO, "confecho" },
Neels Hofmeyrd95ab1e2017-09-22 00:52:54 +02001587 { MGCP_CONN_LOOPBACK, "loopback" },
1588 { 0, NULL }
1589};
Philipp Maierdf9192e2021-09-03 17:50:51 +02001590
1591/*! Get MGCP client instance name (VTY).
1592 * \param[in] mgcp MGCP client descriptor.
1593 * \returns MGCP client name.
1594 *
1595 * The user can only modify the name of an MGCP client instance when it is
1596 * part of a pool. For single MGCP client instances and MGCP client instance
1597 * where no description is set via the VTY, the MGW domain name will be used
1598 * as name. */
1599const char *mgcp_client_name(const struct mgcp_client *mgcp)
1600{
1601 if (!mgcp)
1602 return "(null)";
1603
1604 if (mgcp->actual.description)
1605 return mgcp->actual.description;
1606 else
1607 return mgcp_client_endpoint_domain(mgcp);
1608}
Neels Hofmeyrcc2f7932023-12-07 03:46:46 +01001609
1610/*! Return typical cmp result, comparing a to b.
1611 * Return 0 if a == b, -1 if a < b, 1 if a > b; comparing all members of ptmap in turn. */
1612int ptmap_cmp(const struct ptmap *a, const struct ptmap *b)
1613{
1614 int rc;
1615 if (a == b)
1616 return 0;
1617 if (!a)
1618 return -1;
1619 if (!b)
1620 return 1;
1621 rc = OSMO_CMP(a->codec, b->codec);
1622 if (rc)
1623 return rc;
1624 return OSMO_CMP(a->pt, b->pt);
1625}