blob: 677a4f3950d236a163547afe2b2ced3d21603d61 [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];
72 unsigned int i;
73
74 if (!str)
75 return NULL;
76
Philipp Maier704c4f02018-06-07 18:51:31 +020077 osmo_strlcpy(buf, str, sizeof(buf));
78
79 for (i = 0; i < strlen(buf); i++) {
80 if (buf[i] == '/')
81 buf[i] = '\0';
82 }
83
84 return buf;
85}
86
87/*! Map a string to a codec.
88 * \ptmap[in] str input string (e.g "GSM/8000/1", "GSM/8000" or "GSM")
89 * \returns codec that corresponds to the given string representation. */
90enum mgcp_codecs map_str_to_codec(const char *str)
91{
92 unsigned int i;
93 char *codec_name;
94 char str_buf[64];
95
96 osmo_strlcpy(str_buf, extract_codec_name(str), sizeof(str_buf));
97
Neels Hofmeyr47642f22019-02-27 05:56:53 +010098 for (i = 0; i < ARRAY_SIZE(osmo_mgcpc_codec_names); i++) {
99 codec_name = extract_codec_name(osmo_mgcpc_codec_names[i].str);
Philipp Maier704c4f02018-06-07 18:51:31 +0200100 if (!codec_name)
101 continue;
102 if (strcmp(codec_name, str_buf) == 0)
Neels Hofmeyr47642f22019-02-27 05:56:53 +0100103 return osmo_mgcpc_codec_names[i].value;
Philipp Maier704c4f02018-06-07 18:51:31 +0200104 }
105
106 return -1;
107}
108
109/* Check the ptmap for illegal mappings */
Neels Hofmeyr84274e42019-04-27 19:08:36 +0200110static int check_ptmap(const struct ptmap *ptmap)
Philipp Maier704c4f02018-06-07 18:51:31 +0200111{
112 /* Check if there are mappings that leave the IANA assigned dynamic
113 * payload type range. Under normal conditions such mappings should
114 * not occur */
115
116 /* Its ok to have a 1:1 mapping in the statically defined
117 * range, this won't hurt */
118 if (ptmap->codec == ptmap->pt)
119 return 0;
120
121 if (ptmap->codec < 96 || ptmap->codec > 127)
122 goto error;
123 if (ptmap->pt < 96 || ptmap->pt > 127)
124 goto error;
125
126 return 0;
127error:
128 LOGP(DLMGCP, LOGL_ERROR,
129 "ptmap contains illegal mapping: codec=%u maps to pt=%u\n",
130 ptmap->codec, ptmap->pt);
131 return -1;
132}
133
134/*! Map a codec to a payload type.
135 * \ptmap[in] payload pointer to payload type map with specified payload types.
136 * \ptmap[in] ptmap_len length of the payload type map.
137 * \ptmap[in] codec the codec for which the payload type should be looked up.
138 * \returns assigned payload type */
Neels Hofmeyr84274e42019-04-27 19:08:36 +0200139unsigned int map_codec_to_pt(const struct ptmap *ptmap, unsigned int ptmap_len,
Philipp Maier704c4f02018-06-07 18:51:31 +0200140 enum mgcp_codecs codec)
141{
142 unsigned int i;
143
144 /*! Note: If the payload type map is empty or the codec is not found
145 * in the map, then a 1:1 mapping is performed. If the codec falls
146 * into the statically defined range or if the mapping table isself
147 * tries to map to the statically defined range, then the mapping
148 * is also ignored and a 1:1 mapping is performed instead. */
149
150 /* we may return the codec directly since enum mgcp_codecs directly
151 * corresponds to the statićally assigned payload types */
152 if (codec < 96 || codec > 127)
153 return codec;
154
155 for (i = 0; i < ptmap_len; i++) {
156 /* Skip illegal map entries */
157 if (check_ptmap(ptmap) == 0 && ptmap->codec == codec)
158 return ptmap->pt;
159 ptmap++;
160 }
161
162 /* If nothing is found, do not perform any mapping */
163 return codec;
164}
165
166/*! Map a payload type to a codec.
167 * \ptmap[in] payload pointer to payload type map with specified payload types.
168 * \ptmap[in] ptmap_len length of the payload type map.
169 * \ptmap[in] payload type for which the codec should be looked up.
170 * \returns codec that corresponds to the specified payload type */
171enum mgcp_codecs map_pt_to_codec(struct ptmap *ptmap, unsigned int ptmap_len,
172 unsigned int pt)
173{
174 unsigned int i;
175
176 /*! Note: If the payload type map is empty or the payload type is not
177 * found in the map, then a 1:1 mapping is performed. If the payload
178 * type falls into the statically defined range or if the mapping
179 * table isself tries to map to the statically defined range, then
180 * the mapping is also ignored and a 1:1 mapping is performed
181 * instead. */
182
183 /* See also note in map_codec_to_pt() */
184 if (pt < 96 || pt > 127)
185 return pt;
186
187 for (i = 0; i < ptmap_len; i++) {
188 if (check_ptmap(ptmap) == 0 && ptmap->pt == pt)
189 return ptmap->codec;
190 ptmap++;
191 }
192
193 /* If nothing is found, do not perform any mapping */
194 return pt;
195}
196
Pau Espin Pedrol8e8d59f2023-06-13 19:41:44 +0200197static void _mgcp_client_conf_init(struct mgcp_client_conf *conf)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200198{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200199 /* NULL and -1 default to MGCP_CLIENT_*_DEFAULT values */
200 *conf = (struct mgcp_client_conf){
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200201 .local_addr = NULL,
202 .local_port = -1,
203 .remote_addr = NULL,
204 .remote_port = -1,
Pau Espin Pedrol563386e2023-06-14 12:20:49 +0200205 .keepalive = {
206 .timeout_sec = 0, /* disabled */
207 .req_interval_sec = 0, /* disabled */
208 .req_endpoint_name = MGCP_CLIENT_KEEPALIVE_DEFAULT_ENDP,
209 },
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200210 };
Philipp Maier3f2c15f2021-07-22 11:53:07 +0200211
212 INIT_LLIST_HEAD(&conf->reset_epnames);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200213}
214
Pau Espin Pedrol8e8d59f2023-06-13 19:41:44 +0200215/*! Allocate and initialize MGCP client configuration struct with default values.
216 * \param[in] ctx talloc context to use as a parent during allocation.
217 *
218 * The returned struct can be freed using talloc_free().
219 */
220struct mgcp_client_conf *mgcp_client_conf_alloc(void *ctx)
221{
222 struct mgcp_client_conf *conf = talloc(ctx, struct mgcp_client_conf);
223 _mgcp_client_conf_init(conf);
224 return conf;
225}
226
227/*! Initialize MGCP client configuration struct with default values.
228 * \param[out] conf Client configuration.
229 *
230 * This function is deprecated and should not be used, as it may break if size
231 * of struct mgcp_client_conf changes in the future!
232 */
233void mgcp_client_conf_init(struct mgcp_client_conf *conf)
234{
235 _mgcp_client_conf_init(conf);
236}
237
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200238static void mgcp_client_handle_response(struct mgcp_client *mgcp,
239 struct mgcp_response_pending *pending,
240 struct mgcp_response *response)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200241{
242 if (!pending) {
Philipp Maierdf9192e2021-09-03 17:50:51 +0200243 LOGPMGW(mgcp, LOGL_ERROR, "Cannot handle NULL response\n");
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200244 return;
245 }
246 if (pending->response_cb)
247 pending->response_cb(response, pending->priv);
248 else
Philipp Maierdf9192e2021-09-03 17:50:51 +0200249 LOGPMGW(mgcp, LOGL_DEBUG, "MGCP response ignored (NULL cb)\n");
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200250 talloc_free(pending);
251}
252
253static int mgcp_response_parse_head(struct mgcp_response *r, struct msgb *msg)
254{
255 int comment_pos;
256 char *end;
257
258 if (mgcp_msg_terminate_nul(msg))
259 goto response_parse_failure;
260
261 r->body = (char *)msg->data;
262
Harald Welte9bf7c532017-11-17 14:14:31 +0100263 if (sscanf(r->body, "%3d %u %n",
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200264 &r->head.response_code, &r->head.trans_id,
265 &comment_pos) != 2)
266 goto response_parse_failure;
267
Philipp Maierabe8c892018-01-20 00:15:12 +0100268 osmo_strlcpy(r->head.comment, r->body + comment_pos, sizeof(r->head.comment));
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200269 end = strchr(r->head.comment, '\r');
270 if (!end)
271 goto response_parse_failure;
272 /* Mark the end of the comment */
273 *end = '\0';
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200274 return 0;
275
276response_parse_failure:
277 LOGP(DLMGCP, LOGL_ERROR,
278 "Failed to parse MGCP response header\n");
279 return -EINVAL;
280}
281
282/* TODO undup against mgcp_protocol.c:mgcp_check_param() */
283static bool mgcp_line_is_valid(const char *line)
284{
285 const size_t line_len = strlen(line);
286 if (line[0] == '\0')
287 return true;
288
289 if (line_len < 2
290 || line[1] != '=') {
291 LOGP(DLMGCP, LOGL_ERROR,
292 "Wrong MGCP option format: '%s'\n",
293 line);
294 return false;
295 }
296
297 return true;
298}
299
Philipp Maier704c4f02018-06-07 18:51:31 +0200300/* Parse a line like "m=audio 16002 RTP/AVP 98", extract port and payload types */
301static int mgcp_parse_audio_port_pt(struct mgcp_response *r, char *line)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200302{
Philipp Maier704c4f02018-06-07 18:51:31 +0200303 char *pt_str;
Pau Espin Pedrolc5c14302019-07-23 16:19:41 +0200304 char *pt_end;
Pau Espin Pedrola2b1c5e2019-07-26 14:13:14 +0200305 unsigned long int pt;
Philipp Maier704c4f02018-06-07 18:51:31 +0200306 unsigned int count = 0;
307 unsigned int i;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200308
Philipp Maier704c4f02018-06-07 18:51:31 +0200309 /* Extract port information */
310 if (sscanf(line, "m=audio %hu", &r->audio_port) != 1)
311 goto response_parse_failure_port;
Philipp Maier10f32db2017-12-13 12:34:34 +0100312 if (r->audio_port == 0)
Philipp Maier704c4f02018-06-07 18:51:31 +0200313 goto response_parse_failure_port;
Philipp Maier10f32db2017-12-13 12:34:34 +0100314
Philipp Maier704c4f02018-06-07 18:51:31 +0200315 /* Extract payload types */
316 line = strstr(line, "RTP/AVP ");
317 if (!line)
318 goto exit;
319
320 pt_str = strtok(line, " ");
321 while (1) {
322 /* Do not allow excessive payload types */
323 if (count > ARRAY_SIZE(r->codecs))
324 goto response_parse_failure_pt;
325
326 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) ||
332 pt_str == pt_end)
333 goto response_parse_failure_pt;
Philipp Maier704c4f02018-06-07 18:51:31 +0200334
Pau Espin Pedrola2b1c5e2019-07-26 14:13:14 +0200335 if (pt >> 7) /* PT is 7 bit field, higher values not allowed */
336 goto response_parse_failure_pt;
337
Philipp Maier704c4f02018-06-07 18:51:31 +0200338 /* Do not allow duplicate payload types */
339 for (i = 0; i < count; i++)
340 if (r->codecs[i] == pt)
341 goto response_parse_failure_pt;
342
343 /* Note: The payload type we store may not necessarly match
344 * the codec types we have defined in enum mgcp_codecs. To
345 * ensure that the end result only contains codec types which
346 * match enum mgcp_codecs, we will go through afterwards and
347 * remap the affected entries with the inrofmation we learn
348 * from rtpmap */
349 r->codecs[count] = pt;
350 count++;
351 }
352
353 r->codecs_len = count;
354
355exit:
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200356 return 0;
357
Philipp Maier704c4f02018-06-07 18:51:31 +0200358response_parse_failure_port:
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200359 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier704c4f02018-06-07 18:51:31 +0200360 "Failed to parse SDP parameter port (%s)\n", line);
Philipp Maier06da85e2017-10-05 18:49:24 +0200361 return -EINVAL;
Philipp Maier704c4f02018-06-07 18:51:31 +0200362
363response_parse_failure_pt:
364 LOGP(DLMGCP, LOGL_ERROR,
365 "Failed to parse SDP parameter payload types (%s)\n", line);
366 return -EINVAL;
367}
368
369/* Parse a line like "m=audio 16002 RTP/AVP 98", extract port and payload types */
370static int mgcp_parse_audio_ptime_rtpmap(struct mgcp_response *r, const char *line)
371{
372 unsigned int pt;
373 char codec_resp[64];
Neels Hofmeyr3ab8ca42019-08-08 04:03:42 +0200374 enum mgcp_codecs codec;
Pau Espin Pedrolc12bfb72019-04-16 17:23:09 +0200375
Neels Hofmeyr23f40482019-08-08 04:03:42 +0200376#define A_PTIME "a=ptime:"
377#define A_RTPMAP "a=rtpmap:"
Pau Espin Pedrolc12bfb72019-04-16 17:23:09 +0200378
Neels Hofmeyr23f40482019-08-08 04:03:42 +0200379 if (osmo_str_startswith(line, A_PTIME)) {
380 if (sscanf(line, A_PTIME "%u", &r->ptime) != 1) {
381 LOGP(DLMGCP, LOGL_ERROR,
382 "Failed to parse SDP parameter, invalid ptime (%s)\n", line);
383 return -EINVAL;
384 }
385 } else if (osmo_str_startswith(line, A_RTPMAP)) {
386 if (sscanf(line, A_RTPMAP "%d %63s", &pt, codec_resp) != 2) {
387 LOGP(DLMGCP, LOGL_ERROR,
388 "Failed to parse SDP parameter, invalid rtpmap: %s\n", osmo_quote_str(line, -1));
389 return -EINVAL;
390 }
Neels Hofmeyr3ab8ca42019-08-08 04:03:42 +0200391 if (r->ptmap_len >= ARRAY_SIZE(r->ptmap)) {
392 LOGP(DLMGCP, LOGL_ERROR, "No more space in ptmap array (len=%u)\n", r->ptmap_len);
393 return -ENOSPC;
Neels Hofmeyr23f40482019-08-08 04:03:42 +0200394 }
Neels Hofmeyr3ab8ca42019-08-08 04:03:42 +0200395 codec = map_str_to_codec(codec_resp);
396 r->ptmap[r->ptmap_len].pt = pt;
397 r->ptmap[r->ptmap_len].codec = codec;
398 r->ptmap_len++;
Philipp Maier704c4f02018-06-07 18:51:31 +0200399 }
Pau Espin Pedrolc12bfb72019-04-16 17:23:09 +0200400
Philipp Maier704c4f02018-06-07 18:51:31 +0200401 return 0;
Philipp Maier06da85e2017-10-05 18:49:24 +0200402}
403
404/* Parse a line like "c=IN IP4 10.11.12.13" */
405static int mgcp_parse_audio_ip(struct mgcp_response *r, const char *line)
406{
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +0200407 struct in6_addr ip_test;
408 bool is_ipv6;
Philipp Maier06da85e2017-10-05 18:49:24 +0200409
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +0200410 if (strncmp("c=IN IP", line, 7) != 0)
Philipp Maier06da85e2017-10-05 18:49:24 +0200411 goto response_parse_failure;
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +0200412 line += 7;
413 if (*line == '6')
414 is_ipv6 = true;
415 else if (*line == '4')
416 is_ipv6 = false;
417 else
Philipp Maier06da85e2017-10-05 18:49:24 +0200418 goto response_parse_failure;
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +0200419 line++;
420 if (*line != ' ')
Philipp Maier06da85e2017-10-05 18:49:24 +0200421 goto response_parse_failure;
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +0200422 line++;
Philipp Maier06da85e2017-10-05 18:49:24 +0200423
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +0200424 /* Extract and check IP-Address */
425 if (is_ipv6) {
426 /* 45 = INET6_ADDRSTRLEN -1 */
427 if (sscanf(line, "%45s", r->audio_ip) != 1)
428 goto response_parse_failure;
429 if (inet_pton(AF_INET6, r->audio_ip, &ip_test) != 1)
430 goto response_parse_failure;
431 } else {
432 /* 15 = INET_ADDRSTRLEN -1 */
433 if (sscanf(line, "%15s", r->audio_ip) != 1)
434 goto response_parse_failure;
435 if (inet_pton(AF_INET, r->audio_ip, &ip_test) != 1)
436 goto response_parse_failure;
437 }
Philipp Maier06da85e2017-10-05 18:49:24 +0200438 return 0;
439
440response_parse_failure:
441 LOGP(DLMGCP, LOGL_ERROR,
442 "Failed to parse MGCP response header (audio ip)\n");
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200443 return -EINVAL;
444}
445
Pau Espin Pedrol91088c32019-04-24 21:02:40 +0200446/*! Extract OSMUX CID from an MGCP parameter line (string).
447 * \param[in] line single parameter line from the MGCP message
448 * \returns OSMUX CID, -1 wildcard, -2 on error
449 * FIXME: This is a copy of function in mgcp_msg.c. Have some common.c file between both libs?
450 */
451static int mgcp_parse_osmux_cid(const char *line)
452{
453 int osmux_cid;
454
455
Pau Espin Pedrolc1bf4692019-05-14 16:23:24 +0200456 if (strcasecmp(line + 2, "Osmux: *") == 0) {
Pau Espin Pedrol91088c32019-04-24 21:02:40 +0200457 LOGP(DLMGCP, LOGL_DEBUG, "Parsed wilcard Osmux CID\n");
458 return -1;
459 }
460
Pau Espin Pedrolc1bf4692019-05-14 16:23:24 +0200461 if (sscanf(line + 2 + 7, "%u", &osmux_cid) != 1) {
Pau Espin Pedrol91088c32019-04-24 21:02:40 +0200462 LOGP(DLMGCP, LOGL_ERROR, "Failed parsing Osmux in MGCP msg line: %s\n",
463 line);
464 return -2;
465 }
466
Pau Espin Pedrol91088c32019-04-24 21:02:40 +0200467 if (osmux_cid > OSMUX_CID_MAX) { /* OSMUX_CID_MAX from libosmo-netif */
468 LOGP(DLMGCP, LOGL_ERROR, "Osmux ID too large: %u > %u\n",
469 osmux_cid, OSMUX_CID_MAX);
470 return -2;
471 }
Pau Espin Pedrolc7c8e642022-10-06 18:24:21 +0200472 LOGP(DLMGCP, LOGL_DEBUG, "MGW offered Osmux CID %u\n", osmux_cid);
Pau Espin Pedrol91088c32019-04-24 21:02:40 +0200473
474 return osmux_cid;
475}
476
Philipp Maier3b12e1b2018-01-18 15:16:13 +0100477/* A new section is marked by a double line break, check a few more
478 * patterns as there may be variants */
479static char *mgcp_find_section_end(char *string)
480{
481 char *rc;
482
483 rc = strstr(string, "\n\n");
484 if (rc)
Neels Hofmeyrce356ee2023-03-30 16:51:40 +0200485 return rc + 2;
Philipp Maier3b12e1b2018-01-18 15:16:13 +0100486
487 rc = strstr(string, "\n\r\n\r");
488 if (rc)
Neels Hofmeyrce356ee2023-03-30 16:51:40 +0200489 return rc + 4;
Philipp Maier3b12e1b2018-01-18 15:16:13 +0100490
491 rc = strstr(string, "\r\n\r\n");
492 if (rc)
Neels Hofmeyrce356ee2023-03-30 16:51:40 +0200493 return rc + 4;
Philipp Maier3b12e1b2018-01-18 15:16:13 +0100494
495 return NULL;
496}
497
Philipp Maier275ac972018-01-20 00:58:16 +0100498/*! Parse body (SDP) parameters of the MGCP response
499 * \param[in,out] r Response data
500 * \returns 0 on success, -EINVAL on error. */
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200501int mgcp_response_parse_params(struct mgcp_response *r)
502{
503 char *line;
504 int rc;
Neels Hofmeyr0793d2f2018-02-21 14:55:34 +0100505 char *data;
Philipp Maiere9d645b2018-01-19 23:54:08 +0100506 char *data_ptr;
Philipp Maier704c4f02018-06-07 18:51:31 +0200507 int i;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200508
Philipp Maiere9d645b2018-01-19 23:54:08 +0100509 /* Since this functions performs a destructive parsing, we create a
510 * local copy of the body data */
Neels Hofmeyr0793d2f2018-02-21 14:55:34 +0100511 OSMO_ASSERT(r->body);
512 data = talloc_strdup(r, r->body);
Philipp Maiere9d645b2018-01-19 23:54:08 +0100513 OSMO_ASSERT(data);
Philipp Maier55295f72018-01-15 14:00:28 +0100514
Philipp Maiere9d645b2018-01-19 23:54:08 +0100515 /* Find beginning of the parameter (SDP) section */
516 data_ptr = mgcp_find_section_end(data);
Neels Hofmeyr10835332018-02-21 14:55:34 +0100517 if (!data_ptr) {
Neels Hofmeyre8278312019-09-19 03:06:46 +0200518 LOGP(DLMGCP, LOGL_DEBUG, "MGCP response contains no SDP parameters\n");
519 rc = 0;
Philipp Maiere9d645b2018-01-19 23:54:08 +0100520 goto exit;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200521 }
522
Neels Hofmeyr0793d2f2018-02-21 14:55:34 +0100523 /* data_ptr now points to the beginning of the section-end-marker; for_each_non_empty_line()
524 * skips any \r and \n characters for free, so we don't need to skip the marker. */
525
Philipp Maiere9d645b2018-01-19 23:54:08 +0100526 for_each_non_empty_line(line, data_ptr) {
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200527 if (!mgcp_line_is_valid(line))
528 return -EINVAL;
529
530 switch (line[0]) {
Philipp Maier704c4f02018-06-07 18:51:31 +0200531 case 'a':
532 rc = mgcp_parse_audio_ptime_rtpmap(r, line);
533 if (rc)
534 goto exit;
535 break;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200536 case 'm':
Philipp Maier704c4f02018-06-07 18:51:31 +0200537 rc = mgcp_parse_audio_port_pt(r, line);
Philipp Maier06da85e2017-10-05 18:49:24 +0200538 if (rc)
Philipp Maiere9d645b2018-01-19 23:54:08 +0100539 goto exit;
Philipp Maier06da85e2017-10-05 18:49:24 +0200540 break;
541 case 'c':
542 rc = mgcp_parse_audio_ip(r, line);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200543 if (rc)
Philipp Maiere9d645b2018-01-19 23:54:08 +0100544 goto exit;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200545 break;
546 default:
547 /* skip unhandled parameters */
548 break;
549 }
550 }
Philipp Maiere9d645b2018-01-19 23:54:08 +0100551
Philipp Maier704c4f02018-06-07 18:51:31 +0200552 /* See also note in mgcp_parse_audio_port_pt() */
553 for (i = 0; i < r->codecs_len; i++)
554 r->codecs[i] = map_pt_to_codec(r->ptmap, r->ptmap_len, r->codecs[i]);
555
Philipp Maiere9d645b2018-01-19 23:54:08 +0100556 rc = 0;
557exit:
558 talloc_free(data);
559 return rc;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200560}
561
Philipp Maier55295f72018-01-15 14:00:28 +0100562/* Parse a line like "X: something" */
563static int mgcp_parse_head_param(char *result, unsigned int result_len,
564 char label, const char *line)
Philipp Maierffd75e42017-11-22 11:44:50 +0100565{
Philipp Maier55295f72018-01-15 14:00:28 +0100566 char label_string[4];
Neels Hofmeyr23e7bf12018-09-03 21:26:22 +0200567 size_t rc;
Philipp Maier55295f72018-01-15 14:00:28 +0100568
569 /* Detect empty parameters */
Philipp Maierffd75e42017-11-22 11:44:50 +0100570 if (strlen(line) < 4)
571 goto response_parse_failure;
572
Philipp Maier55295f72018-01-15 14:00:28 +0100573 /* Check if the label matches */
574 snprintf(label_string, sizeof(label_string), "%c: ", label);
575 if (memcmp(label_string, line, 3) != 0)
Philipp Maierffd75e42017-11-22 11:44:50 +0100576 goto response_parse_failure;
577
Philipp Maier55295f72018-01-15 14:00:28 +0100578 /* Copy payload part of the string to destinations (the label string
579 * is always 3 chars long) */
Neels Hofmeyr23e7bf12018-09-03 21:26:22 +0200580 rc = osmo_strlcpy(result, line + 3, result_len);
581 if (rc >= result_len) {
582 LOGP(DLMGCP, LOGL_ERROR,
583 "Failed to parse MGCP response (parameter label: %c):"
584 " the received conn ID is too long: %zu, maximum is %u characters\n",
585 label, rc, result_len - 1);
586 return -ENOSPC;
587 }
Philipp Maierffd75e42017-11-22 11:44:50 +0100588 return 0;
589
590response_parse_failure:
591 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier55295f72018-01-15 14:00:28 +0100592 "Failed to parse MGCP response (parameter label: %c)\n", label);
Philipp Maierffd75e42017-11-22 11:44:50 +0100593 return -EINVAL;
594}
595
596/* Parse MGCP parameters of the response */
597static int parse_head_params(struct mgcp_response *r)
598{
599 char *line;
600 int rc = 0;
601 OSMO_ASSERT(r->body);
Philipp Maier55295f72018-01-15 14:00:28 +0100602 char *data;
603 char *data_ptr;
604 char *data_end;
Philipp Maierffd75e42017-11-22 11:44:50 +0100605
Philipp Maier55295f72018-01-15 14:00:28 +0100606 /* Since this functions performs a destructive parsing, we create a
607 * local copy of the body data */
Philipp Maier1fc69992018-01-31 15:32:55 +0100608 data = talloc_zero_size(r, strlen(r->body)+1);
Philipp Maier55295f72018-01-15 14:00:28 +0100609 OSMO_ASSERT(data);
610 data_ptr = data;
611 osmo_strlcpy(data, r->body, strlen(r->body));
612
613 /* If there is an SDP body attached, prevent for_each_non_empty_line()
614 * into running in there, we are not yet interested in the parameters
615 * stored there. */
Pau Espin Pedrolc12bfb72019-04-16 17:23:09 +0200616 data_end = mgcp_find_section_end(data);
Philipp Maierffd75e42017-11-22 11:44:50 +0100617 if (data_end)
618 *data_end = '\0';
619
Philipp Maier55295f72018-01-15 14:00:28 +0100620 for_each_non_empty_line(line, data_ptr) {
Pau Espin Pedrol166077e2019-06-26 12:21:38 +0200621 switch (toupper(line[0])) {
Philipp Maier55295f72018-01-15 14:00:28 +0100622 case 'Z':
623 rc = mgcp_parse_head_param(r->head.endpoint,
624 sizeof(r->head.endpoint),
625 'Z', line);
626 if (rc)
627 goto exit;
Philipp Maier771b26a2018-01-31 13:53:11 +0100628
629 /* A specific endpoint identifier returned by the MGW
630 * must not contain any wildcard characters */
631 if (strstr(r->head.endpoint, "*") != NULL) {
632 rc = -EINVAL;
633 goto exit;
634 }
Philipp Maier3261dc72018-01-31 14:03:13 +0100635
636 /* A specific endpoint identifier returned by the MGW
637 * must contain an @ character */
638 if (strstr(r->head.endpoint, "@") == NULL) {
639 rc = -EINVAL;
640 goto exit;
641 }
Philipp Maier55295f72018-01-15 14:00:28 +0100642 break;
Philipp Maierffd75e42017-11-22 11:44:50 +0100643 case 'I':
Philipp Maier55295f72018-01-15 14:00:28 +0100644 rc = mgcp_parse_head_param(r->head.conn_id,
645 sizeof(r->head.conn_id),
646 'I', line);
Philipp Maierffd75e42017-11-22 11:44:50 +0100647 if (rc)
648 goto exit;
649 break;
Pau Espin Pedrol91088c32019-04-24 21:02:40 +0200650 case 'X':
Pau Espin Pedrolc1bf4692019-05-14 16:23:24 +0200651 if (strncasecmp("Osmux: ", line + 2, strlen("Osmux: ")) == 0) {
Pau Espin Pedrol91088c32019-04-24 21:02:40 +0200652 rc = mgcp_parse_osmux_cid(line);
653 if (rc < 0) {
654 /* -1: we don't want wildcards in response. -2: error */
655 rc = -EINVAL;
656 goto exit;
657 }
658 r->head.x_osmo_osmux_use = true;
659 r->head.x_osmo_osmux_cid = (uint8_t) rc;
660 rc = 0;
661 break;
662 }
663 /* Ignore unknown X-headers */
664 break;
Philipp Maierffd75e42017-11-22 11:44:50 +0100665 default:
666 /* skip unhandled parameters */
667 break;
668 }
669 }
670exit:
Philipp Maier55295f72018-01-15 14:00:28 +0100671 talloc_free(data);
Philipp Maierffd75e42017-11-22 11:44:50 +0100672 return rc;
673}
674
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200675static struct mgcp_response_pending *mgcp_client_response_pending_get(
676 struct mgcp_client *mgcp,
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100677 mgcp_trans_id_t trans_id)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200678{
679 struct mgcp_response_pending *pending;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200680 llist_for_each_entry(pending, &mgcp->responses_pending, entry) {
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100681 if (pending->trans_id == trans_id) {
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200682 llist_del(&pending->entry);
683 return pending;
684 }
685 }
686 return NULL;
687}
688
689/* Feed an MGCP message into the receive processing.
690 * Parse the head and call any callback registered for the transaction id found
691 * in the MGCP message. This is normally called directly from the internal
692 * mgcp_do_read that reads from the socket connected to the MGCP gateway. This
693 * function is published mainly to be able to feed data from the test suite.
694 */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200695int mgcp_client_rx(struct mgcp_client *mgcp, struct msgb *msg)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200696{
Philipp Maier1fc69992018-01-31 15:32:55 +0100697 struct mgcp_response *r;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200698 struct mgcp_response_pending *pending;
699 int rc;
700
Philipp Maier1fc69992018-01-31 15:32:55 +0100701 r = talloc_zero(mgcp, struct mgcp_response);
702 OSMO_ASSERT(r);
703
Pau Espin Pedrol563386e2023-06-14 12:20:49 +0200704 /* Re-arm keepalive timer if enabled */
705 if (OSMO_UNLIKELY(mgcp->conn_up == false)) {
706 LOGPMGW(mgcp, LOGL_NOTICE, "MGCP link to MGW now considered UP\n");
707 mgcp->conn_up = true;
708 }
709 if (mgcp->actual.keepalive.timeout_sec > 0)
710 osmo_timer_schedule(&mgcp->keepalive_rx_timer, mgcp->actual.keepalive.timeout_sec, 0);
711
Philipp Maier1fc69992018-01-31 15:32:55 +0100712 rc = mgcp_response_parse_head(r, msg);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200713 if (rc) {
Philipp Maierdf9192e2021-09-03 17:50:51 +0200714 LOGPMGW(mgcp, LOGL_ERROR, "Cannot parse MGCP response (head)\n");
Philipp Maier1fc69992018-01-31 15:32:55 +0100715 rc = 1;
716 goto error;
Philipp Maierffd75e42017-11-22 11:44:50 +0100717 }
718
Philipp Maierdf9192e2021-09-03 17:50:51 +0200719 LOGPMGW(mgcp, LOGL_DEBUG, "MGCP client: Rx %d %u %s\n",
Neels Hofmeyrd5731072021-07-15 01:36:47 +0200720 r->head.response_code, r->head.trans_id, r->head.comment);
721
Philipp Maier1fc69992018-01-31 15:32:55 +0100722 rc = parse_head_params(r);
Philipp Maierffd75e42017-11-22 11:44:50 +0100723 if (rc) {
Philipp Maierdf9192e2021-09-03 17:50:51 +0200724 LOGPMGW(mgcp, LOGL_ERROR, "Cannot parse MGCP response (head parameters)\n");
Philipp Maier1fc69992018-01-31 15:32:55 +0100725 rc = 1;
726 goto error;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200727 }
728
Philipp Maier1fc69992018-01-31 15:32:55 +0100729 pending = mgcp_client_response_pending_get(mgcp, r->head.trans_id);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200730 if (!pending) {
Philipp Maierdf9192e2021-09-03 17:50:51 +0200731 LOGPMGW(mgcp, LOGL_ERROR, "Cannot find matching MGCP transaction for trans_id %d\n",
732 r->head.trans_id);
Philipp Maier1fc69992018-01-31 15:32:55 +0100733 rc = -ENOENT;
734 goto error;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200735 }
736
Philipp Maier1fc69992018-01-31 15:32:55 +0100737 mgcp_client_handle_response(mgcp, pending, r);
738 rc = 0;
739
740error:
741 talloc_free(r);
742 return rc;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200743}
744
745static int mgcp_do_read(struct osmo_fd *fd)
746{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200747 struct mgcp_client *mgcp = fd->data;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200748 struct msgb *msg;
749 int ret;
750
751 msg = msgb_alloc_headroom(4096, 128, "mgcp_from_gw");
752 if (!msg) {
Philipp Maierdf9192e2021-09-03 17:50:51 +0200753 LOGPMGW(mgcp, LOGL_ERROR, "Failed to allocate MGCP message.\n");
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200754 return -1;
755 }
756
757 ret = read(fd->fd, msg->data, 4096 - 128);
758 if (ret <= 0) {
Philipp Maierdf9192e2021-09-03 17:50:51 +0200759 LOGPMGW(mgcp, LOGL_ERROR, "Failed to read: %s: %d='%s'\n",
760 osmo_sock_get_name2(fd->fd), errno, strerror(errno));
Neels Hofmeyr0a403792018-12-12 03:10:18 +0100761
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200762 msgb_free(msg);
763 return -1;
Harald Welte9bf7c532017-11-17 14:14:31 +0100764 }
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200765
766 msg->l2h = msgb_put(msg, ret);
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200767 ret = mgcp_client_rx(mgcp, msg);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200768 talloc_free(msg);
769 return ret;
770}
771
772static int mgcp_do_write(struct osmo_fd *fd, struct msgb *msg)
773{
774 int ret;
Philipp Maierdf9192e2021-09-03 17:50:51 +0200775 struct mgcp_client *mgcp = fd->data;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200776
Philipp Maierdf9192e2021-09-03 17:50:51 +0200777 LOGPMGW(mgcp, LOGL_DEBUG, "Tx MGCP: %s: len=%u '%s'...\n",
778 osmo_sock_get_name2(fd->fd), msg->len,
779 osmo_escape_str((const char *)msg->data, OSMO_MIN(42, msg->len)));
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200780
781 ret = write(fd->fd, msg->data, msg->len);
Pau Espin Pedrol563386e2023-06-14 12:20:49 +0200782 if (OSMO_UNLIKELY(ret != msg->len))
Philipp Maierdf9192e2021-09-03 17:50:51 +0200783 LOGPMGW(mgcp, LOGL_ERROR, "Failed to Tx MGCP: %s: %d='%s'; msg: len=%u '%s'...\n",
784 osmo_sock_get_name2(fd->fd), errno, strerror(errno),
785 msg->len, osmo_escape_str((const char *)msg->data, OSMO_MIN(42, msg->len)));
Pau Espin Pedrol563386e2023-06-14 12:20:49 +0200786
787 /* Re-arm the keepalive Tx timer: */
788 if (mgcp->actual.keepalive.req_interval_sec > 0)
789 osmo_timer_schedule(&mgcp->keepalive_tx_timer, mgcp->actual.keepalive.req_interval_sec, 0);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200790 return ret;
791}
792
Pau Espin Pedrol2e411f32023-06-14 11:52:04 +0200793static const char *_mgcp_client_name_append_domain(const struct mgcp_client *mgcp, const char *name)
794{
795 static char endpoint[MGCP_ENDPOINT_MAXLEN];
796 int rc;
797
798 rc = snprintf(endpoint, sizeof(endpoint), "%s@%s", name, mgcp_client_endpoint_domain(mgcp));
799 if (rc > sizeof(endpoint) - 1) {
800 LOGPMGW(mgcp, LOGL_ERROR, "MGCP endpoint exceeds maximum length of %zu: '%s@%s'\n",
801 sizeof(endpoint) - 1, name, mgcp_client_endpoint_domain(mgcp));
802 return NULL;
803 }
804 if (rc < 1) {
805 LOGPMGW(mgcp, LOGL_ERROR, "Cannot compose MGCP endpoint name\n");
806 return NULL;
807 }
808 return endpoint;
809}
810
811/* Safely ignore the MGCP response to the DLCX sent via _mgcp_client_send_dlcx() */
812static void _ignore_mgcp_response(struct mgcp_response *response, void *priv) { }
813
814/* Format DLCX message (fire and forget) and send it off to the MGW */
815static void _mgcp_client_send_dlcx(struct mgcp_client *mgcp, const char *epname)
816{
817 struct msgb *msgb_dlcx;
818 struct mgcp_msg mgcp_msg_dlcx = {
819 .verb = MGCP_VERB_DLCX,
820 .presence = MGCP_MSG_PRESENCE_ENDPOINT,
821 };
822 osmo_strlcpy(mgcp_msg_dlcx.endpoint, epname, sizeof(mgcp_msg_dlcx.endpoint));
823 msgb_dlcx = mgcp_msg_gen(mgcp, &mgcp_msg_dlcx);
824 mgcp_client_tx(mgcp, msgb_dlcx, &_ignore_mgcp_response, NULL);
825}
826
Pau Espin Pedrol563386e2023-06-14 12:20:49 +0200827/* Format AuditEndpoint message (fire and forget) and send it off to the MGW */
828static void _mgcp_client_send_auep(struct mgcp_client *mgcp, const char *epname)
829{
830 struct msgb *msgb_auep;
831 struct mgcp_msg mgcp_msg_auep = {
832 .verb = MGCP_VERB_AUEP,
833 .presence = MGCP_MSG_PRESENCE_ENDPOINT,
834 };
835 OSMO_STRLCPY_ARRAY(mgcp_msg_auep.endpoint, epname);
836 msgb_auep = mgcp_msg_gen(mgcp, &mgcp_msg_auep);
837 mgcp_client_tx(mgcp, msgb_auep, &_ignore_mgcp_response, NULL);
838}
839
840static void mgcp_client_keepalive_tx_timer_cb(void *data)
841{
842 struct mgcp_client *mgcp = (struct mgcp_client *)data;
843 LOGPMGW(mgcp, LOGL_INFO, "Triggering keepalive MGCP request\n");
844 const char *epname = _mgcp_client_name_append_domain(mgcp, mgcp->actual.keepalive.req_endpoint_name);
845 _mgcp_client_send_auep(mgcp, epname);
846
847 /* Re-arm the timer: */
848 osmo_timer_schedule(&mgcp->keepalive_tx_timer, mgcp->actual.keepalive.req_interval_sec, 0);
849}
850
851static void mgcp_client_keepalive_rx_timer_cb(void *data)
852{
853 struct mgcp_client *mgcp = (struct mgcp_client *)data;
854 LOGPMGW(mgcp, LOGL_ERROR, "MGCP link to MGW now considered DOWN (keepalive timeout, more than %u seconds with no answer from MGW)\n",
855 mgcp->actual.keepalive.timeout_sec);
856 mgcp->conn_up = false;
857 /* TODO: Potentially time out all ongoing transactions for that MGW. Maybe based on VTY cfg? */
858}
859
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200860struct mgcp_client *mgcp_client_init(void *ctx,
861 struct mgcp_client_conf *conf)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200862{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200863 struct mgcp_client *mgcp;
Philipp Maier3f2c15f2021-07-22 11:53:07 +0200864 struct reset_ep *reset_ep;
865 struct reset_ep *actual_reset_ep;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200866
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200867 mgcp = talloc_zero(ctx, struct mgcp_client);
Harald Welteefe15712020-07-15 10:56:45 +0200868 if (!mgcp)
869 return NULL;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200870
871 INIT_LLIST_HEAD(&mgcp->responses_pending);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200872
873 mgcp->next_trans_id = 1;
874
875 mgcp->actual.local_addr = conf->local_addr ? conf->local_addr :
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200876 MGCP_CLIENT_LOCAL_ADDR_DEFAULT;
Pau Espin Pedrol8e74c632022-10-17 19:20:45 +0200877 mgcp->actual.local_port = conf->local_port > 0 ? (uint16_t)conf->local_port :
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200878 MGCP_CLIENT_LOCAL_PORT_DEFAULT;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200879
880 mgcp->actual.remote_addr = conf->remote_addr ? conf->remote_addr :
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200881 MGCP_CLIENT_REMOTE_ADDR_DEFAULT;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200882 mgcp->actual.remote_port = conf->remote_port >= 0 ? (uint16_t)conf->remote_port :
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200883 MGCP_CLIENT_REMOTE_PORT_DEFAULT;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200884
Neels Hofmeyrac69ea92018-12-19 00:27:50 +0100885 if (osmo_strlcpy(mgcp->actual.endpoint_domain_name, conf->endpoint_domain_name,
886 sizeof(mgcp->actual.endpoint_domain_name))
887 >= sizeof(mgcp->actual.endpoint_domain_name)) {
Philipp Maierdf9192e2021-09-03 17:50:51 +0200888 LOGPMGW(mgcp, LOGL_ERROR, "MGCP client: endpoint domain name is too long, max length is %zu: '%s'\n",
889 sizeof(mgcp->actual.endpoint_domain_name) - 1, conf->endpoint_domain_name);
Neels Hofmeyrac69ea92018-12-19 00:27:50 +0100890 talloc_free(mgcp);
891 return NULL;
892 }
Philipp Maierdf9192e2021-09-03 17:50:51 +0200893 LOGPMGW(mgcp, LOGL_NOTICE, "MGCP client: using endpoint domain '@%s'\n", mgcp_client_endpoint_domain(mgcp));
Neels Hofmeyrac69ea92018-12-19 00:27:50 +0100894
Philipp Maier3f2c15f2021-07-22 11:53:07 +0200895 INIT_LLIST_HEAD(&mgcp->actual.reset_epnames);
896 llist_for_each_entry(reset_ep, &conf->reset_epnames, list) {
897 actual_reset_ep = talloc_memdup(mgcp, reset_ep, sizeof(*reset_ep));
898 llist_add_tail(&actual_reset_ep->list, &mgcp->actual.reset_epnames);
899 }
900
Philipp Maierdf9192e2021-09-03 17:50:51 +0200901 if (conf->description)
902 mgcp->actual.description = talloc_strdup(mgcp, conf->description);
903
Pau Espin Pedrol563386e2023-06-14 12:20:49 +0200904 osmo_wqueue_init(&mgcp->wq, 1024);
905 mgcp->wq.read_cb = mgcp_do_read;
906 mgcp->wq.write_cb = mgcp_do_write;
907 osmo_fd_setup(&mgcp->wq.bfd, -1, OSMO_FD_READ, osmo_wqueue_bfd_cb, mgcp, 0);
908
909 memcpy(&mgcp->actual.keepalive, &conf->keepalive, sizeof(conf->keepalive));
910 osmo_timer_setup(&mgcp->keepalive_tx_timer, mgcp_client_keepalive_tx_timer_cb, mgcp);
911 osmo_timer_setup(&mgcp->keepalive_rx_timer, mgcp_client_keepalive_rx_timer_cb, mgcp);
912
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200913 return mgcp;
914}
915
Philipp Maier3f2c15f2021-07-22 11:53:07 +0200916/*! Initialize client connection (opens socket)
Philipp Maier275ac972018-01-20 00:58:16 +0100917 * \param[in,out] mgcp MGCP client descriptor.
918 * \returns 0 on success, -EINVAL on error. */
Pau Espin Pedrol8e74c632022-10-17 19:20:45 +0200919int mgcp_client_connect(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200920{
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200921 int rc;
Philipp Maier3f2c15f2021-07-22 11:53:07 +0200922 struct reset_ep *reset_ep;
923 const char *epname;
Pau Espin Pedrol563386e2023-06-14 12:20:49 +0200924 bool some_dlcx_sent = false;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200925
926 if (!mgcp) {
Philipp Maierdf9192e2021-09-03 17:50:51 +0200927 LOGPMGW(mgcp, LOGL_FATAL, "Client not initialized properly\n");
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200928 return -EINVAL;
929 }
930
Pau Espin Pedrol563386e2023-06-14 12:20:49 +0200931 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 +0200932 mgcp->actual.local_port, mgcp->actual.remote_addr, mgcp->actual.remote_port,
933 OSMO_SOCK_F_BIND | OSMO_SOCK_F_CONNECT);
Harald Welte8890dfa2017-11-17 15:09:30 +0100934 if (rc < 0) {
Philipp Maierdf9192e2021-09-03 17:50:51 +0200935 LOGPMGW(mgcp, LOGL_FATAL,
936 "Failed to initialize socket %s:%u -> %s:%u for MGW: %s\n",
Philipp Maier276a4142021-07-29 11:55:14 +0200937 mgcp->actual.local_addr ? mgcp->actual.local_addr : "(any)", mgcp->actual.local_port,
938 mgcp->actual.remote_addr ? mgcp->actual.local_addr : "(any)", mgcp->actual.remote_port,
939 strerror(errno));
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200940 goto error_close_fd;
941 }
942
Pau Espin Pedrol563386e2023-06-14 12:20:49 +0200943 LOGPMGW(mgcp, LOGL_INFO, "MGW connection: %s\n", osmo_sock_get_name2(mgcp->wq.bfd.fd));
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200944
Philipp Maier3f2c15f2021-07-22 11:53:07 +0200945 /* If configured, send a DLCX message to the endpoints that are configured to
946 * be reset on startup. Usually this is a wildcarded endpoint. */
947 llist_for_each_entry(reset_ep, &mgcp->actual.reset_epnames, list) {
948 epname = _mgcp_client_name_append_domain(mgcp, reset_ep->name);
Philipp Maierdf9192e2021-09-03 17:50:51 +0200949 LOGPMGW(mgcp, LOGL_INFO, "Sending DLCX to: %s\n", epname);
Philipp Maier3f2c15f2021-07-22 11:53:07 +0200950 _mgcp_client_send_dlcx(mgcp, epname);
Pau Espin Pedrol563386e2023-06-14 12:20:49 +0200951 some_dlcx_sent = true;
Philipp Maier3f2c15f2021-07-22 11:53:07 +0200952 }
Pau Espin Pedrol563386e2023-06-14 12:20:49 +0200953
954 if (!some_dlcx_sent) {
955 if (mgcp->actual.keepalive.req_interval_sec > 0) {
956 /* Attempt an immediate probe to find out if link is UP or DOWN: */
957 osmo_timer_schedule(&mgcp->keepalive_tx_timer, 0, 0);
958 } else {
959 /* Assume link is UP by default, so that this MGW can be selected: */
960 mgcp->conn_up = true;
961 }
962 }
963 /* else: keepalive_tx_timer was already scheduled (if needed) down in the stack during Tx DLCX above */
964
965 if (mgcp->actual.keepalive.timeout_sec > 0)
966 osmo_timer_schedule(&mgcp->keepalive_rx_timer, mgcp->actual.keepalive.timeout_sec, 0);
967
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200968 return 0;
969error_close_fd:
Pau Espin Pedrol563386e2023-06-14 12:20:49 +0200970 close(mgcp->wq.bfd.fd);
971 mgcp->wq.bfd.fd = -1;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200972 return rc;
973}
974
Pau Espin Pedrol8e74c632022-10-17 19:20:45 +0200975/*! DEPRECATED: Initialize client connection (opens socket)
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200976 * \param[in,out] mgcp MGCP client descriptor.
977 * \returns 0 on success, -EINVAL on error. */
Pau Espin Pedrol8e74c632022-10-17 19:20:45 +0200978int mgcp_client_connect2(struct mgcp_client *mgcp, unsigned int retry_n_ports)
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200979{
Pau Espin Pedrol8e74c632022-10-17 19:20:45 +0200980 return mgcp_client_connect(mgcp);
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200981}
982
983/*! Terminate client connection
984 * \param[in,out] mgcp MGCP client descriptor.
985 * \returns 0 on success, -EINVAL on error. */
986void mgcp_client_disconnect(struct mgcp_client *mgcp)
987{
988 struct osmo_wqueue *wq;
989
990 if (!mgcp) {
Philipp Maierdf9192e2021-09-03 17:50:51 +0200991 LOGP(DLMGCP, LOGL_FATAL, "MGCP client not initialized properly\n");
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200992 return;
993 }
994
Pau Espin Pedrol563386e2023-06-14 12:20:49 +0200995 /* Disarm keepalive Tx/Rx timer until next connect() */
996 osmo_timer_del(&mgcp->keepalive_rx_timer);
997 osmo_timer_del(&mgcp->keepalive_tx_timer);
998 mgcp->conn_up = false;
999
Philipp Maier3f4a4cb2021-07-26 13:20:05 +02001000 wq = &mgcp->wq;
1001 osmo_wqueue_clear(wq);
Philipp Maierdf9192e2021-09-03 17:50:51 +02001002 LOGPMGW(mgcp, LOGL_INFO, "MGCP association: %s -- closed!\n", osmo_sock_get_name2(wq->bfd.fd));
Philipp Maier3f4a4cb2021-07-26 13:20:05 +02001003 if (osmo_fd_is_registered(&wq->bfd))
1004 osmo_fd_unregister(&wq->bfd);
Pau Espin Pedrol747fbce2023-03-14 11:48:27 +01001005 close(wq->bfd.fd);
1006 wq->bfd.fd = -1;
Philipp Maier3f4a4cb2021-07-26 13:20:05 +02001007}
1008
Philipp Maier275ac972018-01-20 00:58:16 +01001009/*! Get the IP-Aaddress of the associated MGW as string.
1010 * \param[in] mgcp MGCP client descriptor.
1011 * \returns a pointer to the address string. */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +02001012const char *mgcp_client_remote_addr_str(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001013{
1014 return mgcp->actual.remote_addr;
1015}
1016
Philipp Maier275ac972018-01-20 00:58:16 +01001017/*! Get the IP-Port of the associated MGW.
1018 * \param[in] mgcp MGCP client descriptor.
1019 * \returns port number. */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +02001020uint16_t mgcp_client_remote_port(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001021{
1022 return mgcp->actual.remote_port;
1023}
1024
Pau Espin Pedrol74d0e5c2020-09-02 17:19:20 +02001025/*! Get the IP-Address of the associated MGW as its numeric representation.
1026 * DEPRECATED, DON'T USE.
Philipp Maier275ac972018-01-20 00:58:16 +01001027 * \param[in] mgcp MGCP client descriptor.
1028 * \returns IP-Address as 32 bit integer (network byte order) */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +02001029uint32_t mgcp_client_remote_addr_n(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001030{
Pau Espin Pedrol74d0e5c2020-09-02 17:19:20 +02001031 return 0;
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001032}
1033
Neels Hofmeyrac69ea92018-12-19 00:27:50 +01001034/* To compose endpoint names, usually for CRCX, use this as domain name.
1035 * For example, snprintf("rtpbridge\*@%s", mgcp_client_endpoint_domain(mgcp)). */
1036const char *mgcp_client_endpoint_domain(const struct mgcp_client *mgcp)
1037{
1038 return mgcp->actual.endpoint_domain_name[0] ? mgcp->actual.endpoint_domain_name : "mgw";
1039}
1040
Philipp Maiera466f572020-06-25 16:11:04 +02001041/*! Compose endpoint name for a wildcarded request to the virtual trunk
1042 * \param[in] mgcp MGCP client descriptor.
1043 * \returns string containing the endpoint name (e.g. rtpbridge\*@mgw) */
Pau Espin Pedrolca0818c2019-04-16 17:23:38 +02001044const char *mgcp_client_rtpbridge_wildcard(const struct mgcp_client *mgcp)
1045{
1046 return _mgcp_client_name_append_domain(mgcp, "rtpbridge/*");
1047}
1048
Philipp Maier48635912020-06-25 20:16:22 +02001049/*! Compose endpoint name for an E1 endpoint.
1050 * \param[in] ctx talloc context.
1051 * \param[in] mgcp MGCP client descriptor.
1052 * \param[in] trunk_id id number of the E1 trunk (1-64).
1053 * \param[in] ts timeslot on the E1 trunk (1-31).
1054 * \param[in] rate bitrate used on the E1 trunk (e.g 16 for 16kbit).
1055 * \param[in] offset bit offset of the E1 subslot (e.g. 4 for the third 16k subslot).
1056 * \returns string containing the endpoint name (e.g. ds/e1-1/s-1/su16-4). */
1057const char *mgcp_client_e1_epname(void *ctx, const struct mgcp_client *mgcp, uint8_t trunk_id, uint8_t ts,
1058 uint8_t rate, uint8_t offset)
1059{
1060 /* See also comment in libosmo-mgcp, mgcp_client.c, gen_e1_epname() */
1061 const uint8_t valid_rates[] = { 64, 32, 32, 16, 16, 16, 16, 8, 8, 8, 8, 8, 8, 8, 8 };
1062 const uint8_t valid_offsets[] = { 0, 0, 4, 0, 2, 4, 6, 0, 1, 2, 3, 4, 5, 6, 7 };
1063
1064 uint8_t i;
1065 bool rate_offs_valid = false;
1066 char *epname;
1067
1068 epname =
1069 talloc_asprintf(ctx, "ds/e1-%u/s-%u/su%u-%u@%s", trunk_id, ts, rate, offset,
1070 mgcp_client_endpoint_domain(mgcp));
1071 if (!epname) {
Philipp Maierdf9192e2021-09-03 17:50:51 +02001072 LOGPMGW(mgcp, LOGL_ERROR, "Cannot compose MGCP e1-endpoint name!\n");
Philipp Maier48635912020-06-25 20:16:22 +02001073 return NULL;
1074 }
1075
1076 /* Check if the supplied rate/offset pair resembles a valid combination */
1077 for (i = 0; i < sizeof(valid_rates); i++) {
1078 if (valid_rates[i] == rate && valid_offsets[i] == offset)
1079 rate_offs_valid = true;
1080 }
1081 if (!rate_offs_valid) {
Philipp Maierdf9192e2021-09-03 17:50:51 +02001082 LOGPMGW(mgcp, LOGL_ERROR,
1083 "Cannot compose MGCP e1-endpoint name (%s), rate(%u)/offset(%u) combination is invalid!\n",
1084 epname, rate, offset);
Philipp Maier48635912020-06-25 20:16:22 +02001085 talloc_free(epname);
1086 return NULL;
1087 }
1088
1089 /* An E1 line has a maximum of 32 timeslots, while the first (ts=0) is
1090 * reserverd for framing and alignment, so we can not use it here. */
Philipp Maier92a73cd2020-11-26 22:21:11 +01001091 if (ts == 0 || ts > NUM_E1_TS-1) {
Philipp Maierdf9192e2021-09-03 17:50:51 +02001092 LOGPMGW(mgcp, LOGL_ERROR,
1093 "Cannot compose MGCP e1-endpoint name (%s), E1-timeslot number (%u) is invalid!\n",
1094 epname, ts);
Philipp Maier48635912020-06-25 20:16:22 +02001095 talloc_free(epname);
1096 return NULL;
1097 }
1098
1099 return epname;
1100}
1101
Philipp Maier30bdf942023-02-20 12:40:46 +01001102struct mgcp_response_pending *mgcp_client_pending_add(struct mgcp_client *mgcp, mgcp_trans_id_t trans_id,
1103 mgcp_response_cb_t response_cb, void *priv)
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001104{
1105 struct mgcp_response_pending *pending;
1106
1107 pending = talloc_zero(mgcp, struct mgcp_response_pending);
Harald Welte827da2c2020-07-15 10:57:08 +02001108 if (!pending)
1109 return NULL;
1110
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001111 pending->trans_id = trans_id;
1112 pending->response_cb = response_cb;
1113 pending->priv = priv;
1114 llist_add_tail(&pending->entry, &mgcp->responses_pending);
1115
1116 return pending;
1117}
1118
Philipp Maierdf9192e2021-09-03 17:50:51 +02001119/* Send the MGCP message in msg to the MGW and handle a response with
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001120 * response_cb. NOTE: the response_cb still needs to call
1121 * mgcp_response_parse_params(response) to get the parsed parameters -- to
1122 * potentially save some CPU cycles, only the head line has been parsed when
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +01001123 * the response_cb is invoked.
1124 * Before the priv pointer becomes invalid, e.g. due to transaction timeout,
1125 * mgcp_client_cancel() needs to be called for this transaction.
1126 */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +02001127int mgcp_client_tx(struct mgcp_client *mgcp, struct msgb *msg,
1128 mgcp_response_cb_t response_cb, void *priv)
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001129{
Harald Welte563ffc52020-06-17 21:54:13 +07001130 struct mgcp_response_pending *pending = NULL;
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001131 mgcp_trans_id_t trans_id;
1132 int rc;
1133
1134 trans_id = msg->cb[MSGB_CB_MGCP_TRANS_ID];
1135 if (!trans_id) {
Philipp Maierdf9192e2021-09-03 17:50:51 +02001136 LOGPMGW(mgcp, LOGL_ERROR,
1137 "Unset transaction id in mgcp send request\n");
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001138 talloc_free(msg);
1139 return -EINVAL;
1140 }
1141
Harald Welte563ffc52020-06-17 21:54:13 +07001142 /* Do not allocate a dummy 'mgcp_response_pending' entry */
1143 if (response_cb != NULL) {
1144 pending = mgcp_client_pending_add(mgcp, trans_id, response_cb, priv);
1145 if (!pending) {
1146 talloc_free(msg);
1147 return -ENOMEM;
1148 }
Harald Welte827da2c2020-07-15 10:57:08 +02001149 }
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001150
1151 if (msgb_l2len(msg) > 4096) {
Philipp Maierdf9192e2021-09-03 17:50:51 +02001152 LOGPMGW(mgcp, LOGL_ERROR,
1153 "Cannot send, MGCP message too large: %u\n",
1154 msgb_l2len(msg));
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001155 msgb_free(msg);
1156 rc = -EINVAL;
1157 goto mgcp_tx_error;
1158 }
1159
1160 rc = osmo_wqueue_enqueue(&mgcp->wq, msg);
1161 if (rc) {
Philipp Maierdf9192e2021-09-03 17:50:51 +02001162 LOGPMGW(mgcp, LOGL_FATAL, "Could not queue message to MGW\n");
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001163 msgb_free(msg);
1164 goto mgcp_tx_error;
1165 } else
Philipp Maierdf9192e2021-09-03 17:50:51 +02001166 LOGPMGW(mgcp, LOGL_DEBUG, "Queued %u bytes for MGW\n",
1167 msgb_l2len(msg));
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001168 return 0;
1169
1170mgcp_tx_error:
Harald Welte563ffc52020-06-17 21:54:13 +07001171 if (!pending)
Vadim Yanitskiyffbc6182020-07-16 15:48:13 +07001172 return rc;
Vadim Yanitskiy3f8139c2020-06-17 20:50:17 +07001173 /* Dequeue pending response, it's going to be free()d */
1174 llist_del(&pending->entry);
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001175 /* Pass NULL to response cb to indicate an error */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +02001176 mgcp_client_handle_response(mgcp, pending, NULL);
Vadim Yanitskiyffbc6182020-07-16 15:48:13 +07001177 return rc;
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001178}
1179
Philipp Maier275ac972018-01-20 00:58:16 +01001180/*! Cancel a pending transaction.
1181 * \param[in] mgcp MGCP client descriptor.
1182 * \param[in,out] trans_id Transaction id.
1183 * \returns 0 on success, -ENOENT on error.
1184 *
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +01001185 * Should a priv pointer passed to mgcp_client_tx() become invalid, this function must be called. In
1186 * practical terms, if the caller of mgcp_client_tx() wishes to tear down a transaction without having
1187 * received a response this function must be called. The trans_id can be obtained by calling
Philipp Maier275ac972018-01-20 00:58:16 +01001188 * mgcp_msg_trans_id() on the msgb produced by mgcp_msg_gen(). */
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +01001189int mgcp_client_cancel(struct mgcp_client *mgcp, mgcp_trans_id_t trans_id)
1190{
1191 struct mgcp_response_pending *pending = mgcp_client_response_pending_get(mgcp, trans_id);
1192 if (!pending) {
Philipp Maier275ac972018-01-20 00:58:16 +01001193 /*! Note: it is not harmful to cancel a transaction twice. */
Philipp Maierdf9192e2021-09-03 17:50:51 +02001194 LOGPMGW(mgcp, LOGL_ERROR, "Cannot cancel, no such transaction: %u\n", trans_id);
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +01001195 return -ENOENT;
1196 }
Philipp Maierdf9192e2021-09-03 17:50:51 +02001197 LOGPMGW(mgcp, LOGL_DEBUG, "Canceled transaction %u\n", trans_id);
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +01001198 talloc_free(pending);
1199 return 0;
Philipp Maier275ac972018-01-20 00:58:16 +01001200 /*! We don't really need to clean up the wqueue: In all sane cases, the msgb has already been sent
1201 * out and is no longer in the wqueue. If it still is in the wqueue, then sending MGCP messages
1202 * per se is broken and the program should notice so by a full wqueue. Even if this was called
1203 * before we had a chance to send out the message and it is still going to be sent, we will just
1204 * ignore the reply to it later. Removing a msgb from the wqueue here would just introduce more
1205 * bug surface in terms of failing to update wqueue API's counters or some such.
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +01001206 */
1207}
1208
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +02001209static mgcp_trans_id_t mgcp_client_next_trans_id(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001210{
1211 /* avoid zero trans_id to distinguish from unset trans_id */
1212 if (!mgcp->next_trans_id)
1213 mgcp->next_trans_id ++;
1214 return mgcp->next_trans_id ++;
1215}
1216
Philipp Maier1dc6be62017-10-05 18:25:37 +02001217#define MGCP_CRCX_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT | \
1218 MGCP_MSG_PRESENCE_CALL_ID | \
Philipp Maier1dc6be62017-10-05 18:25:37 +02001219 MGCP_MSG_PRESENCE_CONN_MODE)
1220#define MGCP_MDCX_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT | \
Philipp Maier490cbaa2018-01-22 17:32:38 +01001221 MGCP_MSG_PRESENCE_CALL_ID | \
Philipp Maier1dc6be62017-10-05 18:25:37 +02001222 MGCP_MSG_PRESENCE_CONN_ID)
1223#define MGCP_DLCX_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT)
1224#define MGCP_AUEP_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT)
1225#define MGCP_RSIP_MANDATORY 0 /* none */
1226
Philipp Maier704c4f02018-06-07 18:51:31 +02001227/* Helper function for mgcp_msg_gen(): Add LCO information to MGCP message */
1228static int add_lco(struct msgb *msg, struct mgcp_msg *mgcp_msg)
1229{
1230 unsigned int i;
Philipp Maier704c4f02018-06-07 18:51:31 +02001231 const char *codec;
1232 unsigned int pt;
1233
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001234#define MSGB_PRINTF_OR_RET(FMT, ARGS...) do { \
1235 if (msgb_printf(msg, FMT, ##ARGS) != 0) { \
1236 LOGP(DLMGCP, LOGL_ERROR, "Message buffer too small, can not generate MGCP/SDP message\n"); \
1237 return -ENOBUFS; \
1238 } \
1239 } while (0)
1240
1241 MSGB_PRINTF_OR_RET("L:");
Philipp Maier704c4f02018-06-07 18:51:31 +02001242
1243 if (mgcp_msg->ptime)
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001244 MSGB_PRINTF_OR_RET(" p:%u,", mgcp_msg->ptime);
Philipp Maier704c4f02018-06-07 18:51:31 +02001245
1246 if (mgcp_msg->codecs_len) {
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001247 MSGB_PRINTF_OR_RET(" a:");
Philipp Maier704c4f02018-06-07 18:51:31 +02001248 for (i = 0; i < mgcp_msg->codecs_len; i++) {
1249 pt = mgcp_msg->codecs[i];
Neels Hofmeyr47642f22019-02-27 05:56:53 +01001250 codec = get_value_string_or_null(osmo_mgcpc_codec_names, pt);
Pau Espin Pedrolc12bfb72019-04-16 17:23:09 +02001251
Philipp Maier704c4f02018-06-07 18:51:31 +02001252 /* Note: Use codec descriptors from enum mgcp_codecs
1253 * in mgcp_client only! */
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001254 if (!codec)
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001255 return -EINVAL;
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001256
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001257 MSGB_PRINTF_OR_RET("%s", extract_codec_name(codec));
Philipp Maier704c4f02018-06-07 18:51:31 +02001258 if (i < mgcp_msg->codecs_len - 1)
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001259 MSGB_PRINTF_OR_RET(";");
Philipp Maier704c4f02018-06-07 18:51:31 +02001260 }
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001261 MSGB_PRINTF_OR_RET(",");
Philipp Maier704c4f02018-06-07 18:51:31 +02001262 }
1263
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001264 MSGB_PRINTF_OR_RET(" nt:IN\r\n");
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001265 return 0;
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001266
1267#undef MSGB_PRINTF_OR_RET
Philipp Maier704c4f02018-06-07 18:51:31 +02001268}
1269
1270/* Helper function for mgcp_msg_gen(): Add SDP information to MGCP message */
1271static int add_sdp(struct msgb *msg, struct mgcp_msg *mgcp_msg, struct mgcp_client *mgcp)
1272{
1273 unsigned int i;
Pau Espin Pedrol8667d512020-08-28 19:37:08 +02001274 char local_ip[INET6_ADDRSTRLEN];
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +02001275 int local_ip_family, audio_ip_family;
Philipp Maier704c4f02018-06-07 18:51:31 +02001276 const char *codec;
1277 unsigned int pt;
1278
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001279#define MSGB_PRINTF_OR_RET(FMT, ARGS...) do { \
1280 if (msgb_printf(msg, FMT, ##ARGS) != 0) { \
1281 LOGPMGW(mgcp, LOGL_ERROR, "Message buffer too small, can not generate MGCP message (SDP)\n"); \
1282 return -ENOBUFS; \
1283 } \
1284 } while (0)
1285
Philipp Maier704c4f02018-06-07 18:51:31 +02001286 /* Add separator to mark the beginning of the SDP block */
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001287 MSGB_PRINTF_OR_RET("\r\n");
Philipp Maier704c4f02018-06-07 18:51:31 +02001288
1289 /* Add SDP protocol version */
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001290 MSGB_PRINTF_OR_RET("v=0\r\n");
Philipp Maier704c4f02018-06-07 18:51:31 +02001291
1292 /* Determine local IP-Address */
1293 if (osmo_sock_local_ip(local_ip, mgcp->actual.remote_addr) < 0) {
Philipp Maierdf9192e2021-09-03 17:50:51 +02001294 LOGPMGW(mgcp, LOGL_ERROR,
1295 "Could not determine local IP-Address!\n");
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001296 return -EINVAL;
Philipp Maier704c4f02018-06-07 18:51:31 +02001297 }
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +02001298 local_ip_family = osmo_ip_str_type(local_ip);
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001299 if (local_ip_family == AF_UNSPEC)
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001300 return -EINVAL;
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +02001301 audio_ip_family = osmo_ip_str_type(mgcp_msg->audio_ip);
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001302 if (audio_ip_family == AF_UNSPEC)
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001303 return -EINVAL;
Philipp Maier704c4f02018-06-07 18:51:31 +02001304
1305 /* Add owner/creator (SDP) */
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001306 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 +02001307 local_ip_family == AF_INET6 ? '6' : '4',
1308 local_ip);
Philipp Maier704c4f02018-06-07 18:51:31 +02001309
1310 /* Add session name (none) */
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001311 MSGB_PRINTF_OR_RET("s=-\r\n");
Philipp Maier704c4f02018-06-07 18:51:31 +02001312
1313 /* Add RTP address and port */
1314 if (mgcp_msg->audio_port == 0) {
Philipp Maierdf9192e2021-09-03 17:50:51 +02001315 LOGPMGW(mgcp, LOGL_ERROR,
1316 "Invalid port number, can not generate MGCP message\n");
Philipp Maier704c4f02018-06-07 18:51:31 +02001317 msgb_free(msg);
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001318 return -EINVAL;
Philipp Maier704c4f02018-06-07 18:51:31 +02001319 }
1320 if (strlen(mgcp_msg->audio_ip) <= 0) {
Philipp Maierdf9192e2021-09-03 17:50:51 +02001321 LOGPMGW(mgcp, LOGL_ERROR,
1322 "Empty ip address, can not generate MGCP message\n");
Philipp Maier704c4f02018-06-07 18:51:31 +02001323 msgb_free(msg);
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001324 return -EINVAL;
Philipp Maier704c4f02018-06-07 18:51:31 +02001325 }
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001326 MSGB_PRINTF_OR_RET("c=IN IP%c %s\r\n",
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +02001327 audio_ip_family == AF_INET6 ? '6' : '4',
1328 mgcp_msg->audio_ip);
Philipp Maier704c4f02018-06-07 18:51:31 +02001329
1330 /* Add time description, active time (SDP) */
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001331 MSGB_PRINTF_OR_RET("t=0 0\r\n");
Philipp Maier704c4f02018-06-07 18:51:31 +02001332
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001333 MSGB_PRINTF_OR_RET("m=audio %u RTP/AVP", mgcp_msg->audio_port);
Philipp Maier704c4f02018-06-07 18:51:31 +02001334 for (i = 0; i < mgcp_msg->codecs_len; i++) {
1335 pt = map_codec_to_pt(mgcp_msg->ptmap, mgcp_msg->ptmap_len, mgcp_msg->codecs[i]);
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001336 MSGB_PRINTF_OR_RET(" %u", pt);
Philipp Maier704c4f02018-06-07 18:51:31 +02001337
1338 }
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001339 MSGB_PRINTF_OR_RET("\r\n");
Philipp Maier704c4f02018-06-07 18:51:31 +02001340
Philipp Maier228e5912019-03-05 13:56:59 +01001341 /* Add optional codec parameters (fmtp) */
1342 if (mgcp_msg->param_present) {
1343 for (i = 0; i < mgcp_msg->codecs_len; i++) {
1344 /* The following is only applicable for AMR */
1345 if (mgcp_msg->codecs[i] != CODEC_AMR_8000_1 && mgcp_msg->codecs[i] != CODEC_AMRWB_16000_1)
1346 continue;
1347 pt = map_codec_to_pt(mgcp_msg->ptmap, mgcp_msg->ptmap_len, mgcp_msg->codecs[i]);
1348 if (mgcp_msg->param.amr_octet_aligned_present && mgcp_msg->param.amr_octet_aligned)
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001349 MSGB_PRINTF_OR_RET("a=fmtp:%u octet-align=1\r\n", pt);
Philipp Maier228e5912019-03-05 13:56:59 +01001350 else if (mgcp_msg->param.amr_octet_aligned_present && !mgcp_msg->param.amr_octet_aligned)
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001351 MSGB_PRINTF_OR_RET("a=fmtp:%u octet-align=0\r\n", pt);
Philipp Maier228e5912019-03-05 13:56:59 +01001352 }
1353 }
1354
Philipp Maier704c4f02018-06-07 18:51:31 +02001355 for (i = 0; i < mgcp_msg->codecs_len; i++) {
1356 pt = map_codec_to_pt(mgcp_msg->ptmap, mgcp_msg->ptmap_len, mgcp_msg->codecs[i]);
Pau Espin Pedrolc12bfb72019-04-16 17:23:09 +02001357
Philipp Maier704c4f02018-06-07 18:51:31 +02001358 /* Note: Only dynamic payload type from the range 96-127
1359 * require to be explained further via rtpmap. All others
1360 * are implcitly definedby the number in m=audio */
1361 if (pt >= 96 && pt <= 127) {
Neels Hofmeyr47642f22019-02-27 05:56:53 +01001362 codec = get_value_string_or_null(osmo_mgcpc_codec_names, mgcp_msg->codecs[i]);
Philipp Maier704c4f02018-06-07 18:51:31 +02001363
1364 /* Note: Use codec descriptors from enum mgcp_codecs
1365 * in mgcp_client only! */
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001366 if (!codec)
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001367 return -EINVAL;
Pau Espin Pedrolc12bfb72019-04-16 17:23:09 +02001368
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001369 MSGB_PRINTF_OR_RET("a=rtpmap:%u %s\r\n", pt, codec);
Philipp Maier704c4f02018-06-07 18:51:31 +02001370 }
1371 }
Pau Espin Pedrolc12bfb72019-04-16 17:23:09 +02001372
Philipp Maier704c4f02018-06-07 18:51:31 +02001373 if (mgcp_msg->ptime)
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001374 MSGB_PRINTF_OR_RET("a=ptime:%u\r\n", mgcp_msg->ptime);
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001375
1376 return 0;
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001377#undef MSGB_PRINTF_OR_RET
Philipp Maier704c4f02018-06-07 18:51:31 +02001378}
1379
Philipp Maier275ac972018-01-20 00:58:16 +01001380/*! Generate an MGCP message
1381 * \param[in] mgcp MGCP client descriptor.
1382 * \param[in] mgcp_msg Message description
1383 * \returns message buffer on success, NULL on error. */
Philipp Maier1dc6be62017-10-05 18:25:37 +02001384struct msgb *mgcp_msg_gen(struct mgcp_client *mgcp, struct mgcp_msg *mgcp_msg)
1385{
1386 mgcp_trans_id_t trans_id = mgcp_client_next_trans_id(mgcp);
1387 uint32_t mandatory_mask;
1388 struct msgb *msg = msgb_alloc_headroom(4096, 128, "MGCP tx");
Philipp Maier704c4f02018-06-07 18:51:31 +02001389 bool use_sdp = false;
Pau Espin Pedrol900cd652019-04-24 22:06:22 +02001390 char buf[32];
Philipp Maier1dc6be62017-10-05 18:25:37 +02001391
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001392#define MSGB_PRINTF_OR_RET(FMT, ARGS...) do { \
1393 if (msgb_printf(msg, FMT, ##ARGS) != 0) { \
1394 LOGPMGW(mgcp, LOGL_ERROR, "Message buffer too small, can not generate MGCP/SDP message\n"); \
1395 goto exit_error; \
1396 } \
1397 } while (0)
1398
Philipp Maier1dc6be62017-10-05 18:25:37 +02001399 msg->l2h = msg->data;
1400 msg->cb[MSGB_CB_MGCP_TRANS_ID] = trans_id;
1401
1402 /* Add command verb */
1403 switch (mgcp_msg->verb) {
1404 case MGCP_VERB_CRCX:
1405 mandatory_mask = MGCP_CRCX_MANDATORY;
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001406 MSGB_PRINTF_OR_RET("CRCX %u", trans_id);
Philipp Maier1dc6be62017-10-05 18:25:37 +02001407 break;
1408 case MGCP_VERB_MDCX:
1409 mandatory_mask = MGCP_MDCX_MANDATORY;
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001410 MSGB_PRINTF_OR_RET("MDCX %u", trans_id);
Philipp Maier1dc6be62017-10-05 18:25:37 +02001411 break;
1412 case MGCP_VERB_DLCX:
1413 mandatory_mask = MGCP_DLCX_MANDATORY;
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001414 MSGB_PRINTF_OR_RET("DLCX %u", trans_id);
Philipp Maier1dc6be62017-10-05 18:25:37 +02001415 break;
1416 case MGCP_VERB_AUEP:
1417 mandatory_mask = MGCP_AUEP_MANDATORY;
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001418 MSGB_PRINTF_OR_RET("AUEP %u", trans_id);
Philipp Maier1dc6be62017-10-05 18:25:37 +02001419 break;
1420 case MGCP_VERB_RSIP:
1421 mandatory_mask = MGCP_RSIP_MANDATORY;
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001422 MSGB_PRINTF_OR_RET("RSIP %u", trans_id);
Philipp Maier1dc6be62017-10-05 18:25:37 +02001423 break;
1424 default:
Philipp Maierdf9192e2021-09-03 17:50:51 +02001425 LOGPMGW(mgcp, LOGL_ERROR, "Invalid command verb, can not generate MGCP message\n");
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001426 goto exit_error;
Philipp Maier1dc6be62017-10-05 18:25:37 +02001427 }
1428
1429 /* Check if mandatory fields are missing */
1430 if (!((mgcp_msg->presence & mandatory_mask) == mandatory_mask)) {
Philipp Maierdf9192e2021-09-03 17:50:51 +02001431 LOGPMGW(mgcp, LOGL_ERROR,
1432 "One or more missing mandatory fields, can not generate MGCP message\n");
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001433 goto exit_error;
Philipp Maier1dc6be62017-10-05 18:25:37 +02001434 }
1435
1436 /* Add endpoint name */
Philipp Maier7bc55522017-12-10 22:52:22 +01001437 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_ENDPOINT) {
1438 if (strlen(mgcp_msg->endpoint) <= 0) {
Philipp Maierdf9192e2021-09-03 17:50:51 +02001439 LOGPMGW(mgcp, LOGL_ERROR, "Empty endpoint name, can not generate MGCP message\n");
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001440 goto exit_error;
Philipp Maier7bc55522017-12-10 22:52:22 +01001441 }
Philipp Maier3aa81572018-01-31 14:13:45 +01001442
1443 if (strstr(mgcp_msg->endpoint, "@") == NULL) {
Philipp Maierdf9192e2021-09-03 17:50:51 +02001444 LOGPMGW(mgcp, LOGL_ERROR,
1445 "Endpoint name (%s) lacks separator (@), can not generate MGCP message\n",
1446 mgcp_msg->endpoint);
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001447 goto exit_error;
Philipp Maier3aa81572018-01-31 14:13:45 +01001448 }
1449
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001450 MSGB_PRINTF_OR_RET(" %s", mgcp_msg->endpoint);
Philipp Maier7bc55522017-12-10 22:52:22 +01001451 }
Philipp Maier1dc6be62017-10-05 18:25:37 +02001452
1453 /* Add protocol version */
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001454 MSGB_PRINTF_OR_RET(" MGCP 1.0\r\n");
Philipp Maier1dc6be62017-10-05 18:25:37 +02001455
1456 /* Add call id */
1457 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_CALL_ID)
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001458 MSGB_PRINTF_OR_RET("C: %x\r\n", mgcp_msg->call_id);
Philipp Maier1dc6be62017-10-05 18:25:37 +02001459
1460 /* Add connection id */
Philipp Maier7bc55522017-12-10 22:52:22 +01001461 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_CONN_ID) {
1462 if (strlen(mgcp_msg->conn_id) <= 0) {
Philipp Maierdf9192e2021-09-03 17:50:51 +02001463 LOGPMGW(mgcp, LOGL_ERROR, "Empty connection id, can not generate MGCP message\n");
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001464 goto exit_error;
Philipp Maier7bc55522017-12-10 22:52:22 +01001465 }
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001466 MSGB_PRINTF_OR_RET("I: %s\r\n", mgcp_msg->conn_id);
Philipp Maier7bc55522017-12-10 22:52:22 +01001467 }
Philipp Maier1dc6be62017-10-05 18:25:37 +02001468
Oliver Smitha3125232023-04-18 16:11:03 +02001469 /* Using SDP makes sense when a valid IP/Port combination is specified,
Philipp Maier704c4f02018-06-07 18:51:31 +02001470 * if we do not know this information yet, we fall back to LCO */
1471 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_AUDIO_IP
1472 && mgcp_msg->presence & MGCP_MSG_PRESENCE_AUDIO_PORT)
1473 use_sdp = true;
1474
1475 /* Add local connection options (LCO) */
1476 if (!use_sdp
1477 && (mgcp_msg->verb == MGCP_VERB_CRCX
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001478 || mgcp_msg->verb == MGCP_VERB_MDCX)) {
Oliver Smith0dbaaad2023-02-22 16:30:02 +01001479 if (add_lco(msg, mgcp_msg) < 0) {
1480 LOGPMGW(mgcp, LOGL_ERROR, "Failed to add LCO, can not generate MGCP message\n");
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001481 goto exit_error;
Oliver Smith0dbaaad2023-02-22 16:30:02 +01001482 }
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001483 }
Philipp Maier1dc6be62017-10-05 18:25:37 +02001484
1485 /* Add mode */
1486 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_CONN_MODE)
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001487 MSGB_PRINTF_OR_RET("M: %s\r\n", mgcp_client_cmode_name(mgcp_msg->conn_mode));
Philipp Maier1dc6be62017-10-05 18:25:37 +02001488
Neels Hofmeyre6d8e912018-08-23 16:36:48 +02001489 /* Add X-Osmo-IGN */
1490 if ((mgcp_msg->presence & MGCP_MSG_PRESENCE_X_OSMO_IGN)
1491 && (mgcp_msg->x_osmo_ign != 0))
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001492 MSGB_PRINTF_OR_RET(MGCP_X_OSMO_IGN_HEADER "%s\r\n",
1493 mgcp_msg->x_osmo_ign & MGCP_X_OSMO_IGN_CALLID ? " C" : "");
Neels Hofmeyre6d8e912018-08-23 16:36:48 +02001494
Pau Espin Pedrol900cd652019-04-24 22:06:22 +02001495 /* Add X-Osmo-Osmux */
1496 if ((mgcp_msg->presence & MGCP_MSG_PRESENCE_X_OSMO_OSMUX_CID)) {
Pau Espin Pedrol1b1d7ed2019-05-23 16:48:24 +02001497 if (mgcp_msg->x_osmo_osmux_cid < -1 || mgcp_msg->x_osmo_osmux_cid > OSMUX_CID_MAX) {
Philipp Maierdf9192e2021-09-03 17:50:51 +02001498 LOGPMGW(mgcp, LOGL_ERROR, "Wrong Osmux CID %d, can not generate MGCP message\n",
1499 mgcp_msg->x_osmo_osmux_cid);
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001500 goto exit_error;
Pau Espin Pedrol1b1d7ed2019-05-23 16:48:24 +02001501 }
Pau Espin Pedrol900cd652019-04-24 22:06:22 +02001502 snprintf(buf, sizeof(buf), " %d", mgcp_msg->x_osmo_osmux_cid);
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001503 MSGB_PRINTF_OR_RET(MGCP_X_OSMO_OSMUX_HEADER "%s\r\n",
1504 mgcp_msg->x_osmo_osmux_cid == -1 ? " *" : buf);
Pau Espin Pedrol900cd652019-04-24 22:06:22 +02001505 }
1506
1507
Philipp Maier704c4f02018-06-07 18:51:31 +02001508 /* Add session description protocol (SDP) */
1509 if (use_sdp
1510 && (mgcp_msg->verb == MGCP_VERB_CRCX
1511 || mgcp_msg->verb == MGCP_VERB_MDCX)) {
Oliver Smith0dbaaad2023-02-22 16:30:02 +01001512 if (add_sdp(msg, mgcp_msg, mgcp) < 0) {
1513 LOGPMGW(mgcp, LOGL_ERROR, "Failed to add SDP, can not generate MGCP message\n");
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001514 goto exit_error;
Oliver Smith0dbaaad2023-02-22 16:30:02 +01001515 }
Philipp Maier1dc6be62017-10-05 18:25:37 +02001516 }
1517
Philipp Maier1dc6be62017-10-05 18:25:37 +02001518 return msg;
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001519
1520exit_error:
1521 msgb_free(msg);
1522 return NULL;
1523#undef MSGB_PRINTF_OR_RET
Philipp Maier1dc6be62017-10-05 18:25:37 +02001524}
1525
Philipp Maier275ac972018-01-20 00:58:16 +01001526/*! Retrieve the MGCP transaction ID from a msgb generated by mgcp_msg_gen()
1527 * \param[in] msg message buffer
1528 * \returns Transaction id. */
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +01001529mgcp_trans_id_t mgcp_msg_trans_id(struct msgb *msg)
1530{
1531 return (mgcp_trans_id_t)msg->cb[MSGB_CB_MGCP_TRANS_ID];
1532}
1533
Philipp Maierfa495d62021-09-02 10:08:56 +02001534/*! Get the configuration parameters for a given MGCP client instance
Philipp Maier275ac972018-01-20 00:58:16 +01001535 * \param[in] mgcp MGCP client descriptor.
1536 * \returns configuration */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +02001537struct mgcp_client_conf *mgcp_client_conf_actual(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001538{
1539 return &mgcp->actual;
1540}
Neels Hofmeyrd95ab1e2017-09-22 00:52:54 +02001541
1542const struct value_string mgcp_client_connection_mode_strs[] = {
1543 { MGCP_CONN_NONE, "none" },
1544 { MGCP_CONN_RECV_SEND, "sendrecv" },
1545 { MGCP_CONN_SEND_ONLY, "sendonly" },
1546 { MGCP_CONN_RECV_ONLY, "recvonly" },
1547 { MGCP_CONN_LOOPBACK, "loopback" },
1548 { 0, NULL }
1549};
Philipp Maierdf9192e2021-09-03 17:50:51 +02001550
1551/*! Get MGCP client instance name (VTY).
1552 * \param[in] mgcp MGCP client descriptor.
1553 * \returns MGCP client name.
1554 *
1555 * The user can only modify the name of an MGCP client instance when it is
1556 * part of a pool. For single MGCP client instances and MGCP client instance
1557 * where no description is set via the VTY, the MGW domain name will be used
1558 * as name. */
1559const char *mgcp_client_name(const struct mgcp_client *mgcp)
1560{
1561 if (!mgcp)
1562 return "(null)";
1563
1564 if (mgcp->actual.description)
1565 return mgcp->actual.description;
1566 else
1567 return mgcp_client_endpoint_domain(mgcp);
1568}