blob: f74255c859fd2730cd3ae0c329c8391ba90d9b8d [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,
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200205 };
Philipp Maier3f2c15f2021-07-22 11:53:07 +0200206
207 INIT_LLIST_HEAD(&conf->reset_epnames);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200208}
209
Pau Espin Pedrol8e8d59f2023-06-13 19:41:44 +0200210/*! Allocate and initialize MGCP client configuration struct with default values.
211 * \param[in] ctx talloc context to use as a parent during allocation.
212 *
213 * The returned struct can be freed using talloc_free().
214 */
215struct mgcp_client_conf *mgcp_client_conf_alloc(void *ctx)
216{
217 struct mgcp_client_conf *conf = talloc(ctx, struct mgcp_client_conf);
218 _mgcp_client_conf_init(conf);
219 return conf;
220}
221
222/*! Initialize MGCP client configuration struct with default values.
223 * \param[out] conf Client configuration.
224 *
225 * This function is deprecated and should not be used, as it may break if size
226 * of struct mgcp_client_conf changes in the future!
227 */
228void mgcp_client_conf_init(struct mgcp_client_conf *conf)
229{
230 _mgcp_client_conf_init(conf);
231}
232
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200233static void mgcp_client_handle_response(struct mgcp_client *mgcp,
234 struct mgcp_response_pending *pending,
235 struct mgcp_response *response)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200236{
237 if (!pending) {
Philipp Maierdf9192e2021-09-03 17:50:51 +0200238 LOGPMGW(mgcp, LOGL_ERROR, "Cannot handle NULL response\n");
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200239 return;
240 }
241 if (pending->response_cb)
242 pending->response_cb(response, pending->priv);
243 else
Philipp Maierdf9192e2021-09-03 17:50:51 +0200244 LOGPMGW(mgcp, LOGL_DEBUG, "MGCP response ignored (NULL cb)\n");
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200245 talloc_free(pending);
246}
247
248static int mgcp_response_parse_head(struct mgcp_response *r, struct msgb *msg)
249{
250 int comment_pos;
251 char *end;
252
253 if (mgcp_msg_terminate_nul(msg))
254 goto response_parse_failure;
255
256 r->body = (char *)msg->data;
257
Harald Welte9bf7c532017-11-17 14:14:31 +0100258 if (sscanf(r->body, "%3d %u %n",
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200259 &r->head.response_code, &r->head.trans_id,
260 &comment_pos) != 2)
261 goto response_parse_failure;
262
Philipp Maierabe8c892018-01-20 00:15:12 +0100263 osmo_strlcpy(r->head.comment, r->body + comment_pos, sizeof(r->head.comment));
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200264 end = strchr(r->head.comment, '\r');
265 if (!end)
266 goto response_parse_failure;
267 /* Mark the end of the comment */
268 *end = '\0';
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200269 return 0;
270
271response_parse_failure:
272 LOGP(DLMGCP, LOGL_ERROR,
273 "Failed to parse MGCP response header\n");
274 return -EINVAL;
275}
276
277/* TODO undup against mgcp_protocol.c:mgcp_check_param() */
278static bool mgcp_line_is_valid(const char *line)
279{
280 const size_t line_len = strlen(line);
281 if (line[0] == '\0')
282 return true;
283
284 if (line_len < 2
285 || line[1] != '=') {
286 LOGP(DLMGCP, LOGL_ERROR,
287 "Wrong MGCP option format: '%s'\n",
288 line);
289 return false;
290 }
291
292 return true;
293}
294
Philipp Maier704c4f02018-06-07 18:51:31 +0200295/* Parse a line like "m=audio 16002 RTP/AVP 98", extract port and payload types */
296static int mgcp_parse_audio_port_pt(struct mgcp_response *r, char *line)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200297{
Philipp Maier704c4f02018-06-07 18:51:31 +0200298 char *pt_str;
Pau Espin Pedrolc5c14302019-07-23 16:19:41 +0200299 char *pt_end;
Pau Espin Pedrola2b1c5e2019-07-26 14:13:14 +0200300 unsigned long int pt;
Philipp Maier704c4f02018-06-07 18:51:31 +0200301 unsigned int count = 0;
302 unsigned int i;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200303
Philipp Maier704c4f02018-06-07 18:51:31 +0200304 /* Extract port information */
305 if (sscanf(line, "m=audio %hu", &r->audio_port) != 1)
306 goto response_parse_failure_port;
Philipp Maier10f32db2017-12-13 12:34:34 +0100307 if (r->audio_port == 0)
Philipp Maier704c4f02018-06-07 18:51:31 +0200308 goto response_parse_failure_port;
Philipp Maier10f32db2017-12-13 12:34:34 +0100309
Philipp Maier704c4f02018-06-07 18:51:31 +0200310 /* Extract payload types */
311 line = strstr(line, "RTP/AVP ");
312 if (!line)
313 goto exit;
314
315 pt_str = strtok(line, " ");
316 while (1) {
317 /* Do not allow excessive payload types */
318 if (count > ARRAY_SIZE(r->codecs))
319 goto response_parse_failure_pt;
320
321 pt_str = strtok(NULL, " ");
322 if (!pt_str)
323 break;
Pau Espin Pedrolc5c14302019-07-23 16:19:41 +0200324 errno = 0;
325 pt = strtoul(pt_str, &pt_end, 0);
326 if ((errno == ERANGE && pt == ULONG_MAX) || (errno && !pt) ||
327 pt_str == pt_end)
328 goto response_parse_failure_pt;
Philipp Maier704c4f02018-06-07 18:51:31 +0200329
Pau Espin Pedrola2b1c5e2019-07-26 14:13:14 +0200330 if (pt >> 7) /* PT is 7 bit field, higher values not allowed */
331 goto response_parse_failure_pt;
332
Philipp Maier704c4f02018-06-07 18:51:31 +0200333 /* Do not allow duplicate payload types */
334 for (i = 0; i < count; i++)
335 if (r->codecs[i] == pt)
336 goto response_parse_failure_pt;
337
338 /* Note: The payload type we store may not necessarly match
339 * the codec types we have defined in enum mgcp_codecs. To
340 * ensure that the end result only contains codec types which
341 * match enum mgcp_codecs, we will go through afterwards and
342 * remap the affected entries with the inrofmation we learn
343 * from rtpmap */
344 r->codecs[count] = pt;
345 count++;
346 }
347
348 r->codecs_len = count;
349
350exit:
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200351 return 0;
352
Philipp Maier704c4f02018-06-07 18:51:31 +0200353response_parse_failure_port:
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200354 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier704c4f02018-06-07 18:51:31 +0200355 "Failed to parse SDP parameter port (%s)\n", line);
Philipp Maier06da85e2017-10-05 18:49:24 +0200356 return -EINVAL;
Philipp Maier704c4f02018-06-07 18:51:31 +0200357
358response_parse_failure_pt:
359 LOGP(DLMGCP, LOGL_ERROR,
360 "Failed to parse SDP parameter payload types (%s)\n", line);
361 return -EINVAL;
362}
363
364/* Parse a line like "m=audio 16002 RTP/AVP 98", extract port and payload types */
365static int mgcp_parse_audio_ptime_rtpmap(struct mgcp_response *r, const char *line)
366{
367 unsigned int pt;
368 char codec_resp[64];
Neels Hofmeyr3ab8ca42019-08-08 04:03:42 +0200369 enum mgcp_codecs codec;
Pau Espin Pedrolc12bfb72019-04-16 17:23:09 +0200370
Neels Hofmeyr23f40482019-08-08 04:03:42 +0200371#define A_PTIME "a=ptime:"
372#define A_RTPMAP "a=rtpmap:"
Pau Espin Pedrolc12bfb72019-04-16 17:23:09 +0200373
Neels Hofmeyr23f40482019-08-08 04:03:42 +0200374 if (osmo_str_startswith(line, A_PTIME)) {
375 if (sscanf(line, A_PTIME "%u", &r->ptime) != 1) {
376 LOGP(DLMGCP, LOGL_ERROR,
377 "Failed to parse SDP parameter, invalid ptime (%s)\n", line);
378 return -EINVAL;
379 }
380 } else if (osmo_str_startswith(line, A_RTPMAP)) {
381 if (sscanf(line, A_RTPMAP "%d %63s", &pt, codec_resp) != 2) {
382 LOGP(DLMGCP, LOGL_ERROR,
383 "Failed to parse SDP parameter, invalid rtpmap: %s\n", osmo_quote_str(line, -1));
384 return -EINVAL;
385 }
Neels Hofmeyr3ab8ca42019-08-08 04:03:42 +0200386 if (r->ptmap_len >= ARRAY_SIZE(r->ptmap)) {
387 LOGP(DLMGCP, LOGL_ERROR, "No more space in ptmap array (len=%u)\n", r->ptmap_len);
388 return -ENOSPC;
Neels Hofmeyr23f40482019-08-08 04:03:42 +0200389 }
Neels Hofmeyr3ab8ca42019-08-08 04:03:42 +0200390 codec = map_str_to_codec(codec_resp);
391 r->ptmap[r->ptmap_len].pt = pt;
392 r->ptmap[r->ptmap_len].codec = codec;
393 r->ptmap_len++;
Philipp Maier704c4f02018-06-07 18:51:31 +0200394 }
Pau Espin Pedrolc12bfb72019-04-16 17:23:09 +0200395
Philipp Maier704c4f02018-06-07 18:51:31 +0200396 return 0;
Philipp Maier06da85e2017-10-05 18:49:24 +0200397}
398
399/* Parse a line like "c=IN IP4 10.11.12.13" */
400static int mgcp_parse_audio_ip(struct mgcp_response *r, const char *line)
401{
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +0200402 struct in6_addr ip_test;
403 bool is_ipv6;
Philipp Maier06da85e2017-10-05 18:49:24 +0200404
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +0200405 if (strncmp("c=IN IP", line, 7) != 0)
Philipp Maier06da85e2017-10-05 18:49:24 +0200406 goto response_parse_failure;
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +0200407 line += 7;
408 if (*line == '6')
409 is_ipv6 = true;
410 else if (*line == '4')
411 is_ipv6 = false;
412 else
Philipp Maier06da85e2017-10-05 18:49:24 +0200413 goto response_parse_failure;
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +0200414 line++;
415 if (*line != ' ')
Philipp Maier06da85e2017-10-05 18:49:24 +0200416 goto response_parse_failure;
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +0200417 line++;
Philipp Maier06da85e2017-10-05 18:49:24 +0200418
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +0200419 /* Extract and check IP-Address */
420 if (is_ipv6) {
421 /* 45 = INET6_ADDRSTRLEN -1 */
422 if (sscanf(line, "%45s", r->audio_ip) != 1)
423 goto response_parse_failure;
424 if (inet_pton(AF_INET6, r->audio_ip, &ip_test) != 1)
425 goto response_parse_failure;
426 } else {
427 /* 15 = INET_ADDRSTRLEN -1 */
428 if (sscanf(line, "%15s", r->audio_ip) != 1)
429 goto response_parse_failure;
430 if (inet_pton(AF_INET, r->audio_ip, &ip_test) != 1)
431 goto response_parse_failure;
432 }
Philipp Maier06da85e2017-10-05 18:49:24 +0200433 return 0;
434
435response_parse_failure:
436 LOGP(DLMGCP, LOGL_ERROR,
437 "Failed to parse MGCP response header (audio ip)\n");
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200438 return -EINVAL;
439}
440
Pau Espin Pedrol91088c32019-04-24 21:02:40 +0200441/*! Extract OSMUX CID from an MGCP parameter line (string).
442 * \param[in] line single parameter line from the MGCP message
443 * \returns OSMUX CID, -1 wildcard, -2 on error
444 * FIXME: This is a copy of function in mgcp_msg.c. Have some common.c file between both libs?
445 */
446static int mgcp_parse_osmux_cid(const char *line)
447{
448 int osmux_cid;
449
450
Pau Espin Pedrolc1bf4692019-05-14 16:23:24 +0200451 if (strcasecmp(line + 2, "Osmux: *") == 0) {
Pau Espin Pedrol91088c32019-04-24 21:02:40 +0200452 LOGP(DLMGCP, LOGL_DEBUG, "Parsed wilcard Osmux CID\n");
453 return -1;
454 }
455
Pau Espin Pedrolc1bf4692019-05-14 16:23:24 +0200456 if (sscanf(line + 2 + 7, "%u", &osmux_cid) != 1) {
Pau Espin Pedrol91088c32019-04-24 21:02:40 +0200457 LOGP(DLMGCP, LOGL_ERROR, "Failed parsing Osmux in MGCP msg line: %s\n",
458 line);
459 return -2;
460 }
461
Pau Espin Pedrol91088c32019-04-24 21:02:40 +0200462 if (osmux_cid > OSMUX_CID_MAX) { /* OSMUX_CID_MAX from libosmo-netif */
463 LOGP(DLMGCP, LOGL_ERROR, "Osmux ID too large: %u > %u\n",
464 osmux_cid, OSMUX_CID_MAX);
465 return -2;
466 }
Pau Espin Pedrolc7c8e642022-10-06 18:24:21 +0200467 LOGP(DLMGCP, LOGL_DEBUG, "MGW offered Osmux CID %u\n", osmux_cid);
Pau Espin Pedrol91088c32019-04-24 21:02:40 +0200468
469 return osmux_cid;
470}
471
Philipp Maier3b12e1b2018-01-18 15:16:13 +0100472/* A new section is marked by a double line break, check a few more
473 * patterns as there may be variants */
474static char *mgcp_find_section_end(char *string)
475{
476 char *rc;
477
478 rc = strstr(string, "\n\n");
479 if (rc)
Neels Hofmeyrce356ee2023-03-30 16:51:40 +0200480 return rc + 2;
Philipp Maier3b12e1b2018-01-18 15:16:13 +0100481
482 rc = strstr(string, "\n\r\n\r");
483 if (rc)
Neels Hofmeyrce356ee2023-03-30 16:51:40 +0200484 return rc + 4;
Philipp Maier3b12e1b2018-01-18 15:16:13 +0100485
486 rc = strstr(string, "\r\n\r\n");
487 if (rc)
Neels Hofmeyrce356ee2023-03-30 16:51:40 +0200488 return rc + 4;
Philipp Maier3b12e1b2018-01-18 15:16:13 +0100489
490 return NULL;
491}
492
Philipp Maier275ac972018-01-20 00:58:16 +0100493/*! Parse body (SDP) parameters of the MGCP response
494 * \param[in,out] r Response data
495 * \returns 0 on success, -EINVAL on error. */
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200496int mgcp_response_parse_params(struct mgcp_response *r)
497{
498 char *line;
499 int rc;
Neels Hofmeyr0793d2f2018-02-21 14:55:34 +0100500 char *data;
Philipp Maiere9d645b2018-01-19 23:54:08 +0100501 char *data_ptr;
Philipp Maier704c4f02018-06-07 18:51:31 +0200502 int i;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200503
Philipp Maiere9d645b2018-01-19 23:54:08 +0100504 /* Since this functions performs a destructive parsing, we create a
505 * local copy of the body data */
Neels Hofmeyr0793d2f2018-02-21 14:55:34 +0100506 OSMO_ASSERT(r->body);
507 data = talloc_strdup(r, r->body);
Philipp Maiere9d645b2018-01-19 23:54:08 +0100508 OSMO_ASSERT(data);
Philipp Maier55295f72018-01-15 14:00:28 +0100509
Philipp Maiere9d645b2018-01-19 23:54:08 +0100510 /* Find beginning of the parameter (SDP) section */
511 data_ptr = mgcp_find_section_end(data);
Neels Hofmeyr10835332018-02-21 14:55:34 +0100512 if (!data_ptr) {
Neels Hofmeyre8278312019-09-19 03:06:46 +0200513 LOGP(DLMGCP, LOGL_DEBUG, "MGCP response contains no SDP parameters\n");
514 rc = 0;
Philipp Maiere9d645b2018-01-19 23:54:08 +0100515 goto exit;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200516 }
517
Neels Hofmeyr0793d2f2018-02-21 14:55:34 +0100518 /* data_ptr now points to the beginning of the section-end-marker; for_each_non_empty_line()
519 * skips any \r and \n characters for free, so we don't need to skip the marker. */
520
Philipp Maiere9d645b2018-01-19 23:54:08 +0100521 for_each_non_empty_line(line, data_ptr) {
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200522 if (!mgcp_line_is_valid(line))
523 return -EINVAL;
524
525 switch (line[0]) {
Philipp Maier704c4f02018-06-07 18:51:31 +0200526 case 'a':
527 rc = mgcp_parse_audio_ptime_rtpmap(r, line);
528 if (rc)
529 goto exit;
530 break;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200531 case 'm':
Philipp Maier704c4f02018-06-07 18:51:31 +0200532 rc = mgcp_parse_audio_port_pt(r, line);
Philipp Maier06da85e2017-10-05 18:49:24 +0200533 if (rc)
Philipp Maiere9d645b2018-01-19 23:54:08 +0100534 goto exit;
Philipp Maier06da85e2017-10-05 18:49:24 +0200535 break;
536 case 'c':
537 rc = mgcp_parse_audio_ip(r, line);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200538 if (rc)
Philipp Maiere9d645b2018-01-19 23:54:08 +0100539 goto exit;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200540 break;
541 default:
542 /* skip unhandled parameters */
543 break;
544 }
545 }
Philipp Maiere9d645b2018-01-19 23:54:08 +0100546
Philipp Maier704c4f02018-06-07 18:51:31 +0200547 /* See also note in mgcp_parse_audio_port_pt() */
548 for (i = 0; i < r->codecs_len; i++)
549 r->codecs[i] = map_pt_to_codec(r->ptmap, r->ptmap_len, r->codecs[i]);
550
Philipp Maiere9d645b2018-01-19 23:54:08 +0100551 rc = 0;
552exit:
553 talloc_free(data);
554 return rc;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200555}
556
Philipp Maier55295f72018-01-15 14:00:28 +0100557/* Parse a line like "X: something" */
558static int mgcp_parse_head_param(char *result, unsigned int result_len,
559 char label, const char *line)
Philipp Maierffd75e42017-11-22 11:44:50 +0100560{
Philipp Maier55295f72018-01-15 14:00:28 +0100561 char label_string[4];
Neels Hofmeyr23e7bf12018-09-03 21:26:22 +0200562 size_t rc;
Philipp Maier55295f72018-01-15 14:00:28 +0100563
564 /* Detect empty parameters */
Philipp Maierffd75e42017-11-22 11:44:50 +0100565 if (strlen(line) < 4)
566 goto response_parse_failure;
567
Philipp Maier55295f72018-01-15 14:00:28 +0100568 /* Check if the label matches */
569 snprintf(label_string, sizeof(label_string), "%c: ", label);
570 if (memcmp(label_string, line, 3) != 0)
Philipp Maierffd75e42017-11-22 11:44:50 +0100571 goto response_parse_failure;
572
Philipp Maier55295f72018-01-15 14:00:28 +0100573 /* Copy payload part of the string to destinations (the label string
574 * is always 3 chars long) */
Neels Hofmeyr23e7bf12018-09-03 21:26:22 +0200575 rc = osmo_strlcpy(result, line + 3, result_len);
576 if (rc >= result_len) {
577 LOGP(DLMGCP, LOGL_ERROR,
578 "Failed to parse MGCP response (parameter label: %c):"
579 " the received conn ID is too long: %zu, maximum is %u characters\n",
580 label, rc, result_len - 1);
581 return -ENOSPC;
582 }
Philipp Maierffd75e42017-11-22 11:44:50 +0100583 return 0;
584
585response_parse_failure:
586 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier55295f72018-01-15 14:00:28 +0100587 "Failed to parse MGCP response (parameter label: %c)\n", label);
Philipp Maierffd75e42017-11-22 11:44:50 +0100588 return -EINVAL;
589}
590
591/* Parse MGCP parameters of the response */
592static int parse_head_params(struct mgcp_response *r)
593{
594 char *line;
595 int rc = 0;
596 OSMO_ASSERT(r->body);
Philipp Maier55295f72018-01-15 14:00:28 +0100597 char *data;
598 char *data_ptr;
599 char *data_end;
Philipp Maierffd75e42017-11-22 11:44:50 +0100600
Philipp Maier55295f72018-01-15 14:00:28 +0100601 /* Since this functions performs a destructive parsing, we create a
602 * local copy of the body data */
Philipp Maier1fc69992018-01-31 15:32:55 +0100603 data = talloc_zero_size(r, strlen(r->body)+1);
Philipp Maier55295f72018-01-15 14:00:28 +0100604 OSMO_ASSERT(data);
605 data_ptr = data;
606 osmo_strlcpy(data, r->body, strlen(r->body));
607
608 /* If there is an SDP body attached, prevent for_each_non_empty_line()
609 * into running in there, we are not yet interested in the parameters
610 * stored there. */
Pau Espin Pedrolc12bfb72019-04-16 17:23:09 +0200611 data_end = mgcp_find_section_end(data);
Philipp Maierffd75e42017-11-22 11:44:50 +0100612 if (data_end)
613 *data_end = '\0';
614
Philipp Maier55295f72018-01-15 14:00:28 +0100615 for_each_non_empty_line(line, data_ptr) {
Pau Espin Pedrol166077e2019-06-26 12:21:38 +0200616 switch (toupper(line[0])) {
Philipp Maier55295f72018-01-15 14:00:28 +0100617 case 'Z':
618 rc = mgcp_parse_head_param(r->head.endpoint,
619 sizeof(r->head.endpoint),
620 'Z', line);
621 if (rc)
622 goto exit;
Philipp Maier771b26a2018-01-31 13:53:11 +0100623
624 /* A specific endpoint identifier returned by the MGW
625 * must not contain any wildcard characters */
626 if (strstr(r->head.endpoint, "*") != NULL) {
627 rc = -EINVAL;
628 goto exit;
629 }
Philipp Maier3261dc72018-01-31 14:03:13 +0100630
631 /* A specific endpoint identifier returned by the MGW
632 * must contain an @ character */
633 if (strstr(r->head.endpoint, "@") == NULL) {
634 rc = -EINVAL;
635 goto exit;
636 }
Philipp Maier55295f72018-01-15 14:00:28 +0100637 break;
Philipp Maierffd75e42017-11-22 11:44:50 +0100638 case 'I':
Philipp Maier55295f72018-01-15 14:00:28 +0100639 rc = mgcp_parse_head_param(r->head.conn_id,
640 sizeof(r->head.conn_id),
641 'I', line);
Philipp Maierffd75e42017-11-22 11:44:50 +0100642 if (rc)
643 goto exit;
644 break;
Pau Espin Pedrol91088c32019-04-24 21:02:40 +0200645 case 'X':
Pau Espin Pedrolc1bf4692019-05-14 16:23:24 +0200646 if (strncasecmp("Osmux: ", line + 2, strlen("Osmux: ")) == 0) {
Pau Espin Pedrol91088c32019-04-24 21:02:40 +0200647 rc = mgcp_parse_osmux_cid(line);
648 if (rc < 0) {
649 /* -1: we don't want wildcards in response. -2: error */
650 rc = -EINVAL;
651 goto exit;
652 }
653 r->head.x_osmo_osmux_use = true;
654 r->head.x_osmo_osmux_cid = (uint8_t) rc;
655 rc = 0;
656 break;
657 }
658 /* Ignore unknown X-headers */
659 break;
Philipp Maierffd75e42017-11-22 11:44:50 +0100660 default:
661 /* skip unhandled parameters */
662 break;
663 }
664 }
665exit:
Philipp Maier55295f72018-01-15 14:00:28 +0100666 talloc_free(data);
Philipp Maierffd75e42017-11-22 11:44:50 +0100667 return rc;
668}
669
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200670static struct mgcp_response_pending *mgcp_client_response_pending_get(
671 struct mgcp_client *mgcp,
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100672 mgcp_trans_id_t trans_id)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200673{
674 struct mgcp_response_pending *pending;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200675 llist_for_each_entry(pending, &mgcp->responses_pending, entry) {
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100676 if (pending->trans_id == trans_id) {
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200677 llist_del(&pending->entry);
678 return pending;
679 }
680 }
681 return NULL;
682}
683
684/* Feed an MGCP message into the receive processing.
685 * Parse the head and call any callback registered for the transaction id found
686 * in the MGCP message. This is normally called directly from the internal
687 * mgcp_do_read that reads from the socket connected to the MGCP gateway. This
688 * function is published mainly to be able to feed data from the test suite.
689 */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200690int mgcp_client_rx(struct mgcp_client *mgcp, struct msgb *msg)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200691{
Philipp Maier1fc69992018-01-31 15:32:55 +0100692 struct mgcp_response *r;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200693 struct mgcp_response_pending *pending;
694 int rc;
695
Philipp Maier1fc69992018-01-31 15:32:55 +0100696 r = talloc_zero(mgcp, struct mgcp_response);
697 OSMO_ASSERT(r);
698
699 rc = mgcp_response_parse_head(r, msg);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200700 if (rc) {
Philipp Maierdf9192e2021-09-03 17:50:51 +0200701 LOGPMGW(mgcp, LOGL_ERROR, "Cannot parse MGCP response (head)\n");
Philipp Maier1fc69992018-01-31 15:32:55 +0100702 rc = 1;
703 goto error;
Philipp Maierffd75e42017-11-22 11:44:50 +0100704 }
705
Philipp Maierdf9192e2021-09-03 17:50:51 +0200706 LOGPMGW(mgcp, LOGL_DEBUG, "MGCP client: Rx %d %u %s\n",
Neels Hofmeyrd5731072021-07-15 01:36:47 +0200707 r->head.response_code, r->head.trans_id, r->head.comment);
708
Philipp Maier1fc69992018-01-31 15:32:55 +0100709 rc = parse_head_params(r);
Philipp Maierffd75e42017-11-22 11:44:50 +0100710 if (rc) {
Philipp Maierdf9192e2021-09-03 17:50:51 +0200711 LOGPMGW(mgcp, LOGL_ERROR, "Cannot parse MGCP response (head parameters)\n");
Philipp Maier1fc69992018-01-31 15:32:55 +0100712 rc = 1;
713 goto error;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200714 }
715
Philipp Maier1fc69992018-01-31 15:32:55 +0100716 pending = mgcp_client_response_pending_get(mgcp, r->head.trans_id);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200717 if (!pending) {
Philipp Maierdf9192e2021-09-03 17:50:51 +0200718 LOGPMGW(mgcp, LOGL_ERROR, "Cannot find matching MGCP transaction for trans_id %d\n",
719 r->head.trans_id);
Philipp Maier1fc69992018-01-31 15:32:55 +0100720 rc = -ENOENT;
721 goto error;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200722 }
723
Philipp Maier1fc69992018-01-31 15:32:55 +0100724 mgcp_client_handle_response(mgcp, pending, r);
725 rc = 0;
726
727error:
728 talloc_free(r);
729 return rc;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200730}
731
732static int mgcp_do_read(struct osmo_fd *fd)
733{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200734 struct mgcp_client *mgcp = fd->data;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200735 struct msgb *msg;
736 int ret;
737
738 msg = msgb_alloc_headroom(4096, 128, "mgcp_from_gw");
739 if (!msg) {
Philipp Maierdf9192e2021-09-03 17:50:51 +0200740 LOGPMGW(mgcp, LOGL_ERROR, "Failed to allocate MGCP message.\n");
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200741 return -1;
742 }
743
744 ret = read(fd->fd, msg->data, 4096 - 128);
745 if (ret <= 0) {
Philipp Maierdf9192e2021-09-03 17:50:51 +0200746 LOGPMGW(mgcp, LOGL_ERROR, "Failed to read: %s: %d='%s'\n",
747 osmo_sock_get_name2(fd->fd), errno, strerror(errno));
Neels Hofmeyr0a403792018-12-12 03:10:18 +0100748
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200749 msgb_free(msg);
750 return -1;
Harald Welte9bf7c532017-11-17 14:14:31 +0100751 }
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200752
753 msg->l2h = msgb_put(msg, ret);
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200754 ret = mgcp_client_rx(mgcp, msg);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200755 talloc_free(msg);
756 return ret;
757}
758
759static int mgcp_do_write(struct osmo_fd *fd, struct msgb *msg)
760{
761 int ret;
Philipp Maierdf9192e2021-09-03 17:50:51 +0200762 struct mgcp_client *mgcp = fd->data;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200763
Philipp Maierdf9192e2021-09-03 17:50:51 +0200764 LOGPMGW(mgcp, LOGL_DEBUG, "Tx MGCP: %s: len=%u '%s'...\n",
765 osmo_sock_get_name2(fd->fd), msg->len,
766 osmo_escape_str((const char *)msg->data, OSMO_MIN(42, msg->len)));
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200767
768 ret = write(fd->fd, msg->data, msg->len);
769 if (ret != msg->len)
Philipp Maierdf9192e2021-09-03 17:50:51 +0200770 LOGPMGW(mgcp, LOGL_ERROR, "Failed to Tx MGCP: %s: %d='%s'; msg: len=%u '%s'...\n",
771 osmo_sock_get_name2(fd->fd), errno, strerror(errno),
772 msg->len, osmo_escape_str((const char *)msg->data, OSMO_MIN(42, msg->len)));
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200773 return ret;
774}
775
Pau Espin Pedrol2e411f32023-06-14 11:52:04 +0200776static const char *_mgcp_client_name_append_domain(const struct mgcp_client *mgcp, const char *name)
777{
778 static char endpoint[MGCP_ENDPOINT_MAXLEN];
779 int rc;
780
781 rc = snprintf(endpoint, sizeof(endpoint), "%s@%s", name, mgcp_client_endpoint_domain(mgcp));
782 if (rc > sizeof(endpoint) - 1) {
783 LOGPMGW(mgcp, LOGL_ERROR, "MGCP endpoint exceeds maximum length of %zu: '%s@%s'\n",
784 sizeof(endpoint) - 1, name, mgcp_client_endpoint_domain(mgcp));
785 return NULL;
786 }
787 if (rc < 1) {
788 LOGPMGW(mgcp, LOGL_ERROR, "Cannot compose MGCP endpoint name\n");
789 return NULL;
790 }
791 return endpoint;
792}
793
794/* Safely ignore the MGCP response to the DLCX sent via _mgcp_client_send_dlcx() */
795static void _ignore_mgcp_response(struct mgcp_response *response, void *priv) { }
796
797/* Format DLCX message (fire and forget) and send it off to the MGW */
798static void _mgcp_client_send_dlcx(struct mgcp_client *mgcp, const char *epname)
799{
800 struct msgb *msgb_dlcx;
801 struct mgcp_msg mgcp_msg_dlcx = {
802 .verb = MGCP_VERB_DLCX,
803 .presence = MGCP_MSG_PRESENCE_ENDPOINT,
804 };
805 osmo_strlcpy(mgcp_msg_dlcx.endpoint, epname, sizeof(mgcp_msg_dlcx.endpoint));
806 msgb_dlcx = mgcp_msg_gen(mgcp, &mgcp_msg_dlcx);
807 mgcp_client_tx(mgcp, msgb_dlcx, &_ignore_mgcp_response, NULL);
808}
809
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200810struct mgcp_client *mgcp_client_init(void *ctx,
811 struct mgcp_client_conf *conf)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200812{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200813 struct mgcp_client *mgcp;
Philipp Maier3f2c15f2021-07-22 11:53:07 +0200814 struct reset_ep *reset_ep;
815 struct reset_ep *actual_reset_ep;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200816
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200817 mgcp = talloc_zero(ctx, struct mgcp_client);
Harald Welteefe15712020-07-15 10:56:45 +0200818 if (!mgcp)
819 return NULL;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200820
821 INIT_LLIST_HEAD(&mgcp->responses_pending);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200822
823 mgcp->next_trans_id = 1;
824
825 mgcp->actual.local_addr = conf->local_addr ? conf->local_addr :
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200826 MGCP_CLIENT_LOCAL_ADDR_DEFAULT;
Pau Espin Pedrol8e74c632022-10-17 19:20:45 +0200827 mgcp->actual.local_port = conf->local_port > 0 ? (uint16_t)conf->local_port :
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200828 MGCP_CLIENT_LOCAL_PORT_DEFAULT;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200829
830 mgcp->actual.remote_addr = conf->remote_addr ? conf->remote_addr :
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200831 MGCP_CLIENT_REMOTE_ADDR_DEFAULT;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200832 mgcp->actual.remote_port = conf->remote_port >= 0 ? (uint16_t)conf->remote_port :
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200833 MGCP_CLIENT_REMOTE_PORT_DEFAULT;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200834
Neels Hofmeyrac69ea92018-12-19 00:27:50 +0100835 if (osmo_strlcpy(mgcp->actual.endpoint_domain_name, conf->endpoint_domain_name,
836 sizeof(mgcp->actual.endpoint_domain_name))
837 >= sizeof(mgcp->actual.endpoint_domain_name)) {
Philipp Maierdf9192e2021-09-03 17:50:51 +0200838 LOGPMGW(mgcp, LOGL_ERROR, "MGCP client: endpoint domain name is too long, max length is %zu: '%s'\n",
839 sizeof(mgcp->actual.endpoint_domain_name) - 1, conf->endpoint_domain_name);
Neels Hofmeyrac69ea92018-12-19 00:27:50 +0100840 talloc_free(mgcp);
841 return NULL;
842 }
Philipp Maierdf9192e2021-09-03 17:50:51 +0200843 LOGPMGW(mgcp, LOGL_NOTICE, "MGCP client: using endpoint domain '@%s'\n", mgcp_client_endpoint_domain(mgcp));
Neels Hofmeyrac69ea92018-12-19 00:27:50 +0100844
Philipp Maier3f2c15f2021-07-22 11:53:07 +0200845 INIT_LLIST_HEAD(&mgcp->actual.reset_epnames);
846 llist_for_each_entry(reset_ep, &conf->reset_epnames, list) {
847 actual_reset_ep = talloc_memdup(mgcp, reset_ep, sizeof(*reset_ep));
848 llist_add_tail(&actual_reset_ep->list, &mgcp->actual.reset_epnames);
849 }
850
Philipp Maierdf9192e2021-09-03 17:50:51 +0200851 if (conf->description)
852 mgcp->actual.description = talloc_strdup(mgcp, conf->description);
853
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200854 return mgcp;
855}
856
Philipp Maier3f2c15f2021-07-22 11:53:07 +0200857/*! Initialize client connection (opens socket)
Philipp Maier275ac972018-01-20 00:58:16 +0100858 * \param[in,out] mgcp MGCP client descriptor.
859 * \returns 0 on success, -EINVAL on error. */
Pau Espin Pedrol8e74c632022-10-17 19:20:45 +0200860int mgcp_client_connect(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200861{
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200862 struct osmo_wqueue *wq;
863 int rc;
Philipp Maier3f2c15f2021-07-22 11:53:07 +0200864 struct reset_ep *reset_ep;
865 const char *epname;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200866
867 if (!mgcp) {
Philipp Maierdf9192e2021-09-03 17:50:51 +0200868 LOGPMGW(mgcp, LOGL_FATAL, "Client not initialized properly\n");
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200869 return -EINVAL;
870 }
871
872 wq = &mgcp->wq;
Harald Weltec2a8f592020-10-19 13:25:41 +0200873 osmo_wqueue_init(wq, 1024);
874 wq->read_cb = mgcp_do_read;
875 wq->write_cb = mgcp_do_write;
876
877 osmo_fd_setup(&wq->bfd, -1, OSMO_FD_READ, osmo_wqueue_bfd_cb, mgcp, 0);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200878
Pau Espin Pedrol8e74c632022-10-17 19:20:45 +0200879 rc = osmo_sock_init2_ofd(&wq->bfd, AF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, mgcp->actual.local_addr,
880 mgcp->actual.local_port, mgcp->actual.remote_addr, mgcp->actual.remote_port,
881 OSMO_SOCK_F_BIND | OSMO_SOCK_F_CONNECT);
Harald Welte8890dfa2017-11-17 15:09:30 +0100882 if (rc < 0) {
Philipp Maierdf9192e2021-09-03 17:50:51 +0200883 LOGPMGW(mgcp, LOGL_FATAL,
884 "Failed to initialize socket %s:%u -> %s:%u for MGW: %s\n",
Philipp Maier276a4142021-07-29 11:55:14 +0200885 mgcp->actual.local_addr ? mgcp->actual.local_addr : "(any)", mgcp->actual.local_port,
886 mgcp->actual.remote_addr ? mgcp->actual.local_addr : "(any)", mgcp->actual.remote_port,
887 strerror(errno));
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200888 goto error_close_fd;
889 }
890
Philipp Maierdf9192e2021-09-03 17:50:51 +0200891 LOGPMGW(mgcp, LOGL_INFO, "MGW connection: %s\n", osmo_sock_get_name2(wq->bfd.fd));
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200892
Philipp Maier3f2c15f2021-07-22 11:53:07 +0200893 /* If configured, send a DLCX message to the endpoints that are configured to
894 * be reset on startup. Usually this is a wildcarded endpoint. */
895 llist_for_each_entry(reset_ep, &mgcp->actual.reset_epnames, list) {
896 epname = _mgcp_client_name_append_domain(mgcp, reset_ep->name);
Philipp Maierdf9192e2021-09-03 17:50:51 +0200897 LOGPMGW(mgcp, LOGL_INFO, "Sending DLCX to: %s\n", epname);
Philipp Maier3f2c15f2021-07-22 11:53:07 +0200898 _mgcp_client_send_dlcx(mgcp, epname);
899 }
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200900 return 0;
901error_close_fd:
902 close(wq->bfd.fd);
903 wq->bfd.fd = -1;
904 return rc;
905}
906
Pau Espin Pedrol8e74c632022-10-17 19:20:45 +0200907/*! DEPRECATED: Initialize client connection (opens socket)
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200908 * \param[in,out] mgcp MGCP client descriptor.
909 * \returns 0 on success, -EINVAL on error. */
Pau Espin Pedrol8e74c632022-10-17 19:20:45 +0200910int mgcp_client_connect2(struct mgcp_client *mgcp, unsigned int retry_n_ports)
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200911{
Pau Espin Pedrol8e74c632022-10-17 19:20:45 +0200912 return mgcp_client_connect(mgcp);
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200913}
914
915/*! Terminate client connection
916 * \param[in,out] mgcp MGCP client descriptor.
917 * \returns 0 on success, -EINVAL on error. */
918void mgcp_client_disconnect(struct mgcp_client *mgcp)
919{
920 struct osmo_wqueue *wq;
921
922 if (!mgcp) {
Philipp Maierdf9192e2021-09-03 17:50:51 +0200923 LOGP(DLMGCP, LOGL_FATAL, "MGCP client not initialized properly\n");
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200924 return;
925 }
926
927 wq = &mgcp->wq;
928 osmo_wqueue_clear(wq);
Philipp Maierdf9192e2021-09-03 17:50:51 +0200929 LOGPMGW(mgcp, LOGL_INFO, "MGCP association: %s -- closed!\n", osmo_sock_get_name2(wq->bfd.fd));
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200930 if (osmo_fd_is_registered(&wq->bfd))
931 osmo_fd_unregister(&wq->bfd);
Pau Espin Pedrol747fbce2023-03-14 11:48:27 +0100932 close(wq->bfd.fd);
933 wq->bfd.fd = -1;
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200934}
935
Philipp Maier275ac972018-01-20 00:58:16 +0100936/*! Get the IP-Aaddress of the associated MGW as string.
937 * \param[in] mgcp MGCP client descriptor.
938 * \returns a pointer to the address string. */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200939const char *mgcp_client_remote_addr_str(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200940{
941 return mgcp->actual.remote_addr;
942}
943
Philipp Maier275ac972018-01-20 00:58:16 +0100944/*! Get the IP-Port of the associated MGW.
945 * \param[in] mgcp MGCP client descriptor.
946 * \returns port number. */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200947uint16_t mgcp_client_remote_port(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200948{
949 return mgcp->actual.remote_port;
950}
951
Pau Espin Pedrol74d0e5c2020-09-02 17:19:20 +0200952/*! Get the IP-Address of the associated MGW as its numeric representation.
953 * DEPRECATED, DON'T USE.
Philipp Maier275ac972018-01-20 00:58:16 +0100954 * \param[in] mgcp MGCP client descriptor.
955 * \returns IP-Address as 32 bit integer (network byte order) */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200956uint32_t mgcp_client_remote_addr_n(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200957{
Pau Espin Pedrol74d0e5c2020-09-02 17:19:20 +0200958 return 0;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200959}
960
Neels Hofmeyrac69ea92018-12-19 00:27:50 +0100961/* To compose endpoint names, usually for CRCX, use this as domain name.
962 * For example, snprintf("rtpbridge\*@%s", mgcp_client_endpoint_domain(mgcp)). */
963const char *mgcp_client_endpoint_domain(const struct mgcp_client *mgcp)
964{
965 return mgcp->actual.endpoint_domain_name[0] ? mgcp->actual.endpoint_domain_name : "mgw";
966}
967
Philipp Maiera466f572020-06-25 16:11:04 +0200968/*! Compose endpoint name for a wildcarded request to the virtual trunk
969 * \param[in] mgcp MGCP client descriptor.
970 * \returns string containing the endpoint name (e.g. rtpbridge\*@mgw) */
Pau Espin Pedrolca0818c2019-04-16 17:23:38 +0200971const char *mgcp_client_rtpbridge_wildcard(const struct mgcp_client *mgcp)
972{
973 return _mgcp_client_name_append_domain(mgcp, "rtpbridge/*");
974}
975
Philipp Maier48635912020-06-25 20:16:22 +0200976/*! Compose endpoint name for an E1 endpoint.
977 * \param[in] ctx talloc context.
978 * \param[in] mgcp MGCP client descriptor.
979 * \param[in] trunk_id id number of the E1 trunk (1-64).
980 * \param[in] ts timeslot on the E1 trunk (1-31).
981 * \param[in] rate bitrate used on the E1 trunk (e.g 16 for 16kbit).
982 * \param[in] offset bit offset of the E1 subslot (e.g. 4 for the third 16k subslot).
983 * \returns string containing the endpoint name (e.g. ds/e1-1/s-1/su16-4). */
984const char *mgcp_client_e1_epname(void *ctx, const struct mgcp_client *mgcp, uint8_t trunk_id, uint8_t ts,
985 uint8_t rate, uint8_t offset)
986{
987 /* See also comment in libosmo-mgcp, mgcp_client.c, gen_e1_epname() */
988 const uint8_t valid_rates[] = { 64, 32, 32, 16, 16, 16, 16, 8, 8, 8, 8, 8, 8, 8, 8 };
989 const uint8_t valid_offsets[] = { 0, 0, 4, 0, 2, 4, 6, 0, 1, 2, 3, 4, 5, 6, 7 };
990
991 uint8_t i;
992 bool rate_offs_valid = false;
993 char *epname;
994
995 epname =
996 talloc_asprintf(ctx, "ds/e1-%u/s-%u/su%u-%u@%s", trunk_id, ts, rate, offset,
997 mgcp_client_endpoint_domain(mgcp));
998 if (!epname) {
Philipp Maierdf9192e2021-09-03 17:50:51 +0200999 LOGPMGW(mgcp, LOGL_ERROR, "Cannot compose MGCP e1-endpoint name!\n");
Philipp Maier48635912020-06-25 20:16:22 +02001000 return NULL;
1001 }
1002
1003 /* Check if the supplied rate/offset pair resembles a valid combination */
1004 for (i = 0; i < sizeof(valid_rates); i++) {
1005 if (valid_rates[i] == rate && valid_offsets[i] == offset)
1006 rate_offs_valid = true;
1007 }
1008 if (!rate_offs_valid) {
Philipp Maierdf9192e2021-09-03 17:50:51 +02001009 LOGPMGW(mgcp, LOGL_ERROR,
1010 "Cannot compose MGCP e1-endpoint name (%s), rate(%u)/offset(%u) combination is invalid!\n",
1011 epname, rate, offset);
Philipp Maier48635912020-06-25 20:16:22 +02001012 talloc_free(epname);
1013 return NULL;
1014 }
1015
1016 /* An E1 line has a maximum of 32 timeslots, while the first (ts=0) is
1017 * reserverd for framing and alignment, so we can not use it here. */
Philipp Maier92a73cd2020-11-26 22:21:11 +01001018 if (ts == 0 || ts > NUM_E1_TS-1) {
Philipp Maierdf9192e2021-09-03 17:50:51 +02001019 LOGPMGW(mgcp, LOGL_ERROR,
1020 "Cannot compose MGCP e1-endpoint name (%s), E1-timeslot number (%u) is invalid!\n",
1021 epname, ts);
Philipp Maier48635912020-06-25 20:16:22 +02001022 talloc_free(epname);
1023 return NULL;
1024 }
1025
1026 return epname;
1027}
1028
Philipp Maier30bdf942023-02-20 12:40:46 +01001029struct mgcp_response_pending *mgcp_client_pending_add(struct mgcp_client *mgcp, mgcp_trans_id_t trans_id,
1030 mgcp_response_cb_t response_cb, void *priv)
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001031{
1032 struct mgcp_response_pending *pending;
1033
1034 pending = talloc_zero(mgcp, struct mgcp_response_pending);
Harald Welte827da2c2020-07-15 10:57:08 +02001035 if (!pending)
1036 return NULL;
1037
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001038 pending->trans_id = trans_id;
1039 pending->response_cb = response_cb;
1040 pending->priv = priv;
1041 llist_add_tail(&pending->entry, &mgcp->responses_pending);
1042
1043 return pending;
1044}
1045
Philipp Maierdf9192e2021-09-03 17:50:51 +02001046/* Send the MGCP message in msg to the MGW and handle a response with
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001047 * response_cb. NOTE: the response_cb still needs to call
1048 * mgcp_response_parse_params(response) to get the parsed parameters -- to
1049 * potentially save some CPU cycles, only the head line has been parsed when
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +01001050 * the response_cb is invoked.
1051 * Before the priv pointer becomes invalid, e.g. due to transaction timeout,
1052 * mgcp_client_cancel() needs to be called for this transaction.
1053 */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +02001054int mgcp_client_tx(struct mgcp_client *mgcp, struct msgb *msg,
1055 mgcp_response_cb_t response_cb, void *priv)
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001056{
Harald Welte563ffc52020-06-17 21:54:13 +07001057 struct mgcp_response_pending *pending = NULL;
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001058 mgcp_trans_id_t trans_id;
1059 int rc;
1060
1061 trans_id = msg->cb[MSGB_CB_MGCP_TRANS_ID];
1062 if (!trans_id) {
Philipp Maierdf9192e2021-09-03 17:50:51 +02001063 LOGPMGW(mgcp, LOGL_ERROR,
1064 "Unset transaction id in mgcp send request\n");
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001065 talloc_free(msg);
1066 return -EINVAL;
1067 }
1068
Harald Welte563ffc52020-06-17 21:54:13 +07001069 /* Do not allocate a dummy 'mgcp_response_pending' entry */
1070 if (response_cb != NULL) {
1071 pending = mgcp_client_pending_add(mgcp, trans_id, response_cb, priv);
1072 if (!pending) {
1073 talloc_free(msg);
1074 return -ENOMEM;
1075 }
Harald Welte827da2c2020-07-15 10:57:08 +02001076 }
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001077
1078 if (msgb_l2len(msg) > 4096) {
Philipp Maierdf9192e2021-09-03 17:50:51 +02001079 LOGPMGW(mgcp, LOGL_ERROR,
1080 "Cannot send, MGCP message too large: %u\n",
1081 msgb_l2len(msg));
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001082 msgb_free(msg);
1083 rc = -EINVAL;
1084 goto mgcp_tx_error;
1085 }
1086
1087 rc = osmo_wqueue_enqueue(&mgcp->wq, msg);
1088 if (rc) {
Philipp Maierdf9192e2021-09-03 17:50:51 +02001089 LOGPMGW(mgcp, LOGL_FATAL, "Could not queue message to MGW\n");
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001090 msgb_free(msg);
1091 goto mgcp_tx_error;
1092 } else
Philipp Maierdf9192e2021-09-03 17:50:51 +02001093 LOGPMGW(mgcp, LOGL_DEBUG, "Queued %u bytes for MGW\n",
1094 msgb_l2len(msg));
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001095 return 0;
1096
1097mgcp_tx_error:
Harald Welte563ffc52020-06-17 21:54:13 +07001098 if (!pending)
Vadim Yanitskiyffbc6182020-07-16 15:48:13 +07001099 return rc;
Vadim Yanitskiy3f8139c2020-06-17 20:50:17 +07001100 /* Dequeue pending response, it's going to be free()d */
1101 llist_del(&pending->entry);
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001102 /* Pass NULL to response cb to indicate an error */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +02001103 mgcp_client_handle_response(mgcp, pending, NULL);
Vadim Yanitskiyffbc6182020-07-16 15:48:13 +07001104 return rc;
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001105}
1106
Philipp Maier275ac972018-01-20 00:58:16 +01001107/*! Cancel a pending transaction.
1108 * \param[in] mgcp MGCP client descriptor.
1109 * \param[in,out] trans_id Transaction id.
1110 * \returns 0 on success, -ENOENT on error.
1111 *
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +01001112 * Should a priv pointer passed to mgcp_client_tx() become invalid, this function must be called. In
1113 * practical terms, if the caller of mgcp_client_tx() wishes to tear down a transaction without having
1114 * received a response this function must be called. The trans_id can be obtained by calling
Philipp Maier275ac972018-01-20 00:58:16 +01001115 * mgcp_msg_trans_id() on the msgb produced by mgcp_msg_gen(). */
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +01001116int mgcp_client_cancel(struct mgcp_client *mgcp, mgcp_trans_id_t trans_id)
1117{
1118 struct mgcp_response_pending *pending = mgcp_client_response_pending_get(mgcp, trans_id);
1119 if (!pending) {
Philipp Maier275ac972018-01-20 00:58:16 +01001120 /*! Note: it is not harmful to cancel a transaction twice. */
Philipp Maierdf9192e2021-09-03 17:50:51 +02001121 LOGPMGW(mgcp, LOGL_ERROR, "Cannot cancel, no such transaction: %u\n", trans_id);
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +01001122 return -ENOENT;
1123 }
Philipp Maierdf9192e2021-09-03 17:50:51 +02001124 LOGPMGW(mgcp, LOGL_DEBUG, "Canceled transaction %u\n", trans_id);
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +01001125 talloc_free(pending);
1126 return 0;
Philipp Maier275ac972018-01-20 00:58:16 +01001127 /*! We don't really need to clean up the wqueue: In all sane cases, the msgb has already been sent
1128 * out and is no longer in the wqueue. If it still is in the wqueue, then sending MGCP messages
1129 * per se is broken and the program should notice so by a full wqueue. Even if this was called
1130 * before we had a chance to send out the message and it is still going to be sent, we will just
1131 * ignore the reply to it later. Removing a msgb from the wqueue here would just introduce more
1132 * bug surface in terms of failing to update wqueue API's counters or some such.
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +01001133 */
1134}
1135
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +02001136static mgcp_trans_id_t mgcp_client_next_trans_id(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001137{
1138 /* avoid zero trans_id to distinguish from unset trans_id */
1139 if (!mgcp->next_trans_id)
1140 mgcp->next_trans_id ++;
1141 return mgcp->next_trans_id ++;
1142}
1143
Philipp Maier1dc6be62017-10-05 18:25:37 +02001144#define MGCP_CRCX_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT | \
1145 MGCP_MSG_PRESENCE_CALL_ID | \
Philipp Maier1dc6be62017-10-05 18:25:37 +02001146 MGCP_MSG_PRESENCE_CONN_MODE)
1147#define MGCP_MDCX_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT | \
Philipp Maier490cbaa2018-01-22 17:32:38 +01001148 MGCP_MSG_PRESENCE_CALL_ID | \
Philipp Maier1dc6be62017-10-05 18:25:37 +02001149 MGCP_MSG_PRESENCE_CONN_ID)
1150#define MGCP_DLCX_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT)
1151#define MGCP_AUEP_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT)
1152#define MGCP_RSIP_MANDATORY 0 /* none */
1153
Philipp Maier704c4f02018-06-07 18:51:31 +02001154/* Helper function for mgcp_msg_gen(): Add LCO information to MGCP message */
1155static int add_lco(struct msgb *msg, struct mgcp_msg *mgcp_msg)
1156{
1157 unsigned int i;
Philipp Maier704c4f02018-06-07 18:51:31 +02001158 const char *codec;
1159 unsigned int pt;
1160
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001161#define MSGB_PRINTF_OR_RET(FMT, ARGS...) do { \
1162 if (msgb_printf(msg, FMT, ##ARGS) != 0) { \
1163 LOGP(DLMGCP, LOGL_ERROR, "Message buffer too small, can not generate MGCP/SDP message\n"); \
1164 return -ENOBUFS; \
1165 } \
1166 } while (0)
1167
1168 MSGB_PRINTF_OR_RET("L:");
Philipp Maier704c4f02018-06-07 18:51:31 +02001169
1170 if (mgcp_msg->ptime)
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001171 MSGB_PRINTF_OR_RET(" p:%u,", mgcp_msg->ptime);
Philipp Maier704c4f02018-06-07 18:51:31 +02001172
1173 if (mgcp_msg->codecs_len) {
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001174 MSGB_PRINTF_OR_RET(" a:");
Philipp Maier704c4f02018-06-07 18:51:31 +02001175 for (i = 0; i < mgcp_msg->codecs_len; i++) {
1176 pt = mgcp_msg->codecs[i];
Neels Hofmeyr47642f22019-02-27 05:56:53 +01001177 codec = get_value_string_or_null(osmo_mgcpc_codec_names, pt);
Pau Espin Pedrolc12bfb72019-04-16 17:23:09 +02001178
Philipp Maier704c4f02018-06-07 18:51:31 +02001179 /* Note: Use codec descriptors from enum mgcp_codecs
1180 * in mgcp_client only! */
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001181 if (!codec)
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001182 return -EINVAL;
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001183
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001184 MSGB_PRINTF_OR_RET("%s", extract_codec_name(codec));
Philipp Maier704c4f02018-06-07 18:51:31 +02001185 if (i < mgcp_msg->codecs_len - 1)
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001186 MSGB_PRINTF_OR_RET(";");
Philipp Maier704c4f02018-06-07 18:51:31 +02001187 }
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001188 MSGB_PRINTF_OR_RET(",");
Philipp Maier704c4f02018-06-07 18:51:31 +02001189 }
1190
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001191 MSGB_PRINTF_OR_RET(" nt:IN\r\n");
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001192 return 0;
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001193
1194#undef MSGB_PRINTF_OR_RET
Philipp Maier704c4f02018-06-07 18:51:31 +02001195}
1196
1197/* Helper function for mgcp_msg_gen(): Add SDP information to MGCP message */
1198static int add_sdp(struct msgb *msg, struct mgcp_msg *mgcp_msg, struct mgcp_client *mgcp)
1199{
1200 unsigned int i;
Pau Espin Pedrol8667d512020-08-28 19:37:08 +02001201 char local_ip[INET6_ADDRSTRLEN];
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +02001202 int local_ip_family, audio_ip_family;
Philipp Maier704c4f02018-06-07 18:51:31 +02001203 const char *codec;
1204 unsigned int pt;
1205
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001206#define MSGB_PRINTF_OR_RET(FMT, ARGS...) do { \
1207 if (msgb_printf(msg, FMT, ##ARGS) != 0) { \
1208 LOGPMGW(mgcp, LOGL_ERROR, "Message buffer too small, can not generate MGCP message (SDP)\n"); \
1209 return -ENOBUFS; \
1210 } \
1211 } while (0)
1212
Philipp Maier704c4f02018-06-07 18:51:31 +02001213 /* Add separator to mark the beginning of the SDP block */
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001214 MSGB_PRINTF_OR_RET("\r\n");
Philipp Maier704c4f02018-06-07 18:51:31 +02001215
1216 /* Add SDP protocol version */
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001217 MSGB_PRINTF_OR_RET("v=0\r\n");
Philipp Maier704c4f02018-06-07 18:51:31 +02001218
1219 /* Determine local IP-Address */
1220 if (osmo_sock_local_ip(local_ip, mgcp->actual.remote_addr) < 0) {
Philipp Maierdf9192e2021-09-03 17:50:51 +02001221 LOGPMGW(mgcp, LOGL_ERROR,
1222 "Could not determine local IP-Address!\n");
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001223 return -EINVAL;
Philipp Maier704c4f02018-06-07 18:51:31 +02001224 }
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +02001225 local_ip_family = osmo_ip_str_type(local_ip);
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001226 if (local_ip_family == AF_UNSPEC)
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001227 return -EINVAL;
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +02001228 audio_ip_family = osmo_ip_str_type(mgcp_msg->audio_ip);
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001229 if (audio_ip_family == AF_UNSPEC)
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001230 return -EINVAL;
Philipp Maier704c4f02018-06-07 18:51:31 +02001231
1232 /* Add owner/creator (SDP) */
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001233 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 +02001234 local_ip_family == AF_INET6 ? '6' : '4',
1235 local_ip);
Philipp Maier704c4f02018-06-07 18:51:31 +02001236
1237 /* Add session name (none) */
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001238 MSGB_PRINTF_OR_RET("s=-\r\n");
Philipp Maier704c4f02018-06-07 18:51:31 +02001239
1240 /* Add RTP address and port */
1241 if (mgcp_msg->audio_port == 0) {
Philipp Maierdf9192e2021-09-03 17:50:51 +02001242 LOGPMGW(mgcp, LOGL_ERROR,
1243 "Invalid port number, can not generate MGCP message\n");
Philipp Maier704c4f02018-06-07 18:51:31 +02001244 msgb_free(msg);
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001245 return -EINVAL;
Philipp Maier704c4f02018-06-07 18:51:31 +02001246 }
1247 if (strlen(mgcp_msg->audio_ip) <= 0) {
Philipp Maierdf9192e2021-09-03 17:50:51 +02001248 LOGPMGW(mgcp, LOGL_ERROR,
1249 "Empty ip address, can not generate MGCP message\n");
Philipp Maier704c4f02018-06-07 18:51:31 +02001250 msgb_free(msg);
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001251 return -EINVAL;
Philipp Maier704c4f02018-06-07 18:51:31 +02001252 }
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001253 MSGB_PRINTF_OR_RET("c=IN IP%c %s\r\n",
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +02001254 audio_ip_family == AF_INET6 ? '6' : '4',
1255 mgcp_msg->audio_ip);
Philipp Maier704c4f02018-06-07 18:51:31 +02001256
1257 /* Add time description, active time (SDP) */
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001258 MSGB_PRINTF_OR_RET("t=0 0\r\n");
Philipp Maier704c4f02018-06-07 18:51:31 +02001259
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001260 MSGB_PRINTF_OR_RET("m=audio %u RTP/AVP", mgcp_msg->audio_port);
Philipp Maier704c4f02018-06-07 18:51:31 +02001261 for (i = 0; i < mgcp_msg->codecs_len; i++) {
1262 pt = map_codec_to_pt(mgcp_msg->ptmap, mgcp_msg->ptmap_len, mgcp_msg->codecs[i]);
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001263 MSGB_PRINTF_OR_RET(" %u", pt);
Philipp Maier704c4f02018-06-07 18:51:31 +02001264
1265 }
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001266 MSGB_PRINTF_OR_RET("\r\n");
Philipp Maier704c4f02018-06-07 18:51:31 +02001267
Philipp Maier228e5912019-03-05 13:56:59 +01001268 /* Add optional codec parameters (fmtp) */
1269 if (mgcp_msg->param_present) {
1270 for (i = 0; i < mgcp_msg->codecs_len; i++) {
1271 /* The following is only applicable for AMR */
1272 if (mgcp_msg->codecs[i] != CODEC_AMR_8000_1 && mgcp_msg->codecs[i] != CODEC_AMRWB_16000_1)
1273 continue;
1274 pt = map_codec_to_pt(mgcp_msg->ptmap, mgcp_msg->ptmap_len, mgcp_msg->codecs[i]);
1275 if (mgcp_msg->param.amr_octet_aligned_present && mgcp_msg->param.amr_octet_aligned)
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001276 MSGB_PRINTF_OR_RET("a=fmtp:%u octet-align=1\r\n", pt);
Philipp Maier228e5912019-03-05 13:56:59 +01001277 else if (mgcp_msg->param.amr_octet_aligned_present && !mgcp_msg->param.amr_octet_aligned)
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001278 MSGB_PRINTF_OR_RET("a=fmtp:%u octet-align=0\r\n", pt);
Philipp Maier228e5912019-03-05 13:56:59 +01001279 }
1280 }
1281
Philipp Maier704c4f02018-06-07 18:51:31 +02001282 for (i = 0; i < mgcp_msg->codecs_len; i++) {
1283 pt = map_codec_to_pt(mgcp_msg->ptmap, mgcp_msg->ptmap_len, mgcp_msg->codecs[i]);
Pau Espin Pedrolc12bfb72019-04-16 17:23:09 +02001284
Philipp Maier704c4f02018-06-07 18:51:31 +02001285 /* Note: Only dynamic payload type from the range 96-127
1286 * require to be explained further via rtpmap. All others
1287 * are implcitly definedby the number in m=audio */
1288 if (pt >= 96 && pt <= 127) {
Neels Hofmeyr47642f22019-02-27 05:56:53 +01001289 codec = get_value_string_or_null(osmo_mgcpc_codec_names, mgcp_msg->codecs[i]);
Philipp Maier704c4f02018-06-07 18:51:31 +02001290
1291 /* Note: Use codec descriptors from enum mgcp_codecs
1292 * in mgcp_client only! */
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001293 if (!codec)
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001294 return -EINVAL;
Pau Espin Pedrolc12bfb72019-04-16 17:23:09 +02001295
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001296 MSGB_PRINTF_OR_RET("a=rtpmap:%u %s\r\n", pt, codec);
Philipp Maier704c4f02018-06-07 18:51:31 +02001297 }
1298 }
Pau Espin Pedrolc12bfb72019-04-16 17:23:09 +02001299
Philipp Maier704c4f02018-06-07 18:51:31 +02001300 if (mgcp_msg->ptime)
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001301 MSGB_PRINTF_OR_RET("a=ptime:%u\r\n", mgcp_msg->ptime);
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001302
1303 return 0;
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001304#undef MSGB_PRINTF_OR_RET
Philipp Maier704c4f02018-06-07 18:51:31 +02001305}
1306
Philipp Maier275ac972018-01-20 00:58:16 +01001307/*! Generate an MGCP message
1308 * \param[in] mgcp MGCP client descriptor.
1309 * \param[in] mgcp_msg Message description
1310 * \returns message buffer on success, NULL on error. */
Philipp Maier1dc6be62017-10-05 18:25:37 +02001311struct msgb *mgcp_msg_gen(struct mgcp_client *mgcp, struct mgcp_msg *mgcp_msg)
1312{
1313 mgcp_trans_id_t trans_id = mgcp_client_next_trans_id(mgcp);
1314 uint32_t mandatory_mask;
1315 struct msgb *msg = msgb_alloc_headroom(4096, 128, "MGCP tx");
Philipp Maier704c4f02018-06-07 18:51:31 +02001316 bool use_sdp = false;
Pau Espin Pedrol900cd652019-04-24 22:06:22 +02001317 char buf[32];
Philipp Maier1dc6be62017-10-05 18:25:37 +02001318
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001319#define MSGB_PRINTF_OR_RET(FMT, ARGS...) do { \
1320 if (msgb_printf(msg, FMT, ##ARGS) != 0) { \
1321 LOGPMGW(mgcp, LOGL_ERROR, "Message buffer too small, can not generate MGCP/SDP message\n"); \
1322 goto exit_error; \
1323 } \
1324 } while (0)
1325
Philipp Maier1dc6be62017-10-05 18:25:37 +02001326 msg->l2h = msg->data;
1327 msg->cb[MSGB_CB_MGCP_TRANS_ID] = trans_id;
1328
1329 /* Add command verb */
1330 switch (mgcp_msg->verb) {
1331 case MGCP_VERB_CRCX:
1332 mandatory_mask = MGCP_CRCX_MANDATORY;
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001333 MSGB_PRINTF_OR_RET("CRCX %u", trans_id);
Philipp Maier1dc6be62017-10-05 18:25:37 +02001334 break;
1335 case MGCP_VERB_MDCX:
1336 mandatory_mask = MGCP_MDCX_MANDATORY;
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001337 MSGB_PRINTF_OR_RET("MDCX %u", trans_id);
Philipp Maier1dc6be62017-10-05 18:25:37 +02001338 break;
1339 case MGCP_VERB_DLCX:
1340 mandatory_mask = MGCP_DLCX_MANDATORY;
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001341 MSGB_PRINTF_OR_RET("DLCX %u", trans_id);
Philipp Maier1dc6be62017-10-05 18:25:37 +02001342 break;
1343 case MGCP_VERB_AUEP:
1344 mandatory_mask = MGCP_AUEP_MANDATORY;
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001345 MSGB_PRINTF_OR_RET("AUEP %u", trans_id);
Philipp Maier1dc6be62017-10-05 18:25:37 +02001346 break;
1347 case MGCP_VERB_RSIP:
1348 mandatory_mask = MGCP_RSIP_MANDATORY;
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001349 MSGB_PRINTF_OR_RET("RSIP %u", trans_id);
Philipp Maier1dc6be62017-10-05 18:25:37 +02001350 break;
1351 default:
Philipp Maierdf9192e2021-09-03 17:50:51 +02001352 LOGPMGW(mgcp, LOGL_ERROR, "Invalid command verb, can not generate MGCP message\n");
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001353 goto exit_error;
Philipp Maier1dc6be62017-10-05 18:25:37 +02001354 }
1355
1356 /* Check if mandatory fields are missing */
1357 if (!((mgcp_msg->presence & mandatory_mask) == mandatory_mask)) {
Philipp Maierdf9192e2021-09-03 17:50:51 +02001358 LOGPMGW(mgcp, LOGL_ERROR,
1359 "One or more missing mandatory fields, can not generate MGCP message\n");
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001360 goto exit_error;
Philipp Maier1dc6be62017-10-05 18:25:37 +02001361 }
1362
1363 /* Add endpoint name */
Philipp Maier7bc55522017-12-10 22:52:22 +01001364 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_ENDPOINT) {
1365 if (strlen(mgcp_msg->endpoint) <= 0) {
Philipp Maierdf9192e2021-09-03 17:50:51 +02001366 LOGPMGW(mgcp, LOGL_ERROR, "Empty endpoint name, can not generate MGCP message\n");
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001367 goto exit_error;
Philipp Maier7bc55522017-12-10 22:52:22 +01001368 }
Philipp Maier3aa81572018-01-31 14:13:45 +01001369
1370 if (strstr(mgcp_msg->endpoint, "@") == NULL) {
Philipp Maierdf9192e2021-09-03 17:50:51 +02001371 LOGPMGW(mgcp, LOGL_ERROR,
1372 "Endpoint name (%s) lacks separator (@), can not generate MGCP message\n",
1373 mgcp_msg->endpoint);
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001374 goto exit_error;
Philipp Maier3aa81572018-01-31 14:13:45 +01001375 }
1376
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001377 MSGB_PRINTF_OR_RET(" %s", mgcp_msg->endpoint);
Philipp Maier7bc55522017-12-10 22:52:22 +01001378 }
Philipp Maier1dc6be62017-10-05 18:25:37 +02001379
1380 /* Add protocol version */
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001381 MSGB_PRINTF_OR_RET(" MGCP 1.0\r\n");
Philipp Maier1dc6be62017-10-05 18:25:37 +02001382
1383 /* Add call id */
1384 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_CALL_ID)
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001385 MSGB_PRINTF_OR_RET("C: %x\r\n", mgcp_msg->call_id);
Philipp Maier1dc6be62017-10-05 18:25:37 +02001386
1387 /* Add connection id */
Philipp Maier7bc55522017-12-10 22:52:22 +01001388 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_CONN_ID) {
1389 if (strlen(mgcp_msg->conn_id) <= 0) {
Philipp Maierdf9192e2021-09-03 17:50:51 +02001390 LOGPMGW(mgcp, LOGL_ERROR, "Empty connection id, can not generate MGCP message\n");
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001391 goto exit_error;
Philipp Maier7bc55522017-12-10 22:52:22 +01001392 }
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001393 MSGB_PRINTF_OR_RET("I: %s\r\n", mgcp_msg->conn_id);
Philipp Maier7bc55522017-12-10 22:52:22 +01001394 }
Philipp Maier1dc6be62017-10-05 18:25:37 +02001395
Oliver Smitha3125232023-04-18 16:11:03 +02001396 /* Using SDP makes sense when a valid IP/Port combination is specified,
Philipp Maier704c4f02018-06-07 18:51:31 +02001397 * if we do not know this information yet, we fall back to LCO */
1398 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_AUDIO_IP
1399 && mgcp_msg->presence & MGCP_MSG_PRESENCE_AUDIO_PORT)
1400 use_sdp = true;
1401
1402 /* Add local connection options (LCO) */
1403 if (!use_sdp
1404 && (mgcp_msg->verb == MGCP_VERB_CRCX
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001405 || mgcp_msg->verb == MGCP_VERB_MDCX)) {
Oliver Smith0dbaaad2023-02-22 16:30:02 +01001406 if (add_lco(msg, mgcp_msg) < 0) {
1407 LOGPMGW(mgcp, LOGL_ERROR, "Failed to add LCO, can not generate MGCP message\n");
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001408 goto exit_error;
Oliver Smith0dbaaad2023-02-22 16:30:02 +01001409 }
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001410 }
Philipp Maier1dc6be62017-10-05 18:25:37 +02001411
1412 /* Add mode */
1413 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_CONN_MODE)
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001414 MSGB_PRINTF_OR_RET("M: %s\r\n", mgcp_client_cmode_name(mgcp_msg->conn_mode));
Philipp Maier1dc6be62017-10-05 18:25:37 +02001415
Neels Hofmeyre6d8e912018-08-23 16:36:48 +02001416 /* Add X-Osmo-IGN */
1417 if ((mgcp_msg->presence & MGCP_MSG_PRESENCE_X_OSMO_IGN)
1418 && (mgcp_msg->x_osmo_ign != 0))
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001419 MSGB_PRINTF_OR_RET(MGCP_X_OSMO_IGN_HEADER "%s\r\n",
1420 mgcp_msg->x_osmo_ign & MGCP_X_OSMO_IGN_CALLID ? " C" : "");
Neels Hofmeyre6d8e912018-08-23 16:36:48 +02001421
Pau Espin Pedrol900cd652019-04-24 22:06:22 +02001422 /* Add X-Osmo-Osmux */
1423 if ((mgcp_msg->presence & MGCP_MSG_PRESENCE_X_OSMO_OSMUX_CID)) {
Pau Espin Pedrol1b1d7ed2019-05-23 16:48:24 +02001424 if (mgcp_msg->x_osmo_osmux_cid < -1 || mgcp_msg->x_osmo_osmux_cid > OSMUX_CID_MAX) {
Philipp Maierdf9192e2021-09-03 17:50:51 +02001425 LOGPMGW(mgcp, LOGL_ERROR, "Wrong Osmux CID %d, can not generate MGCP message\n",
1426 mgcp_msg->x_osmo_osmux_cid);
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001427 goto exit_error;
Pau Espin Pedrol1b1d7ed2019-05-23 16:48:24 +02001428 }
Pau Espin Pedrol900cd652019-04-24 22:06:22 +02001429 snprintf(buf, sizeof(buf), " %d", mgcp_msg->x_osmo_osmux_cid);
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001430 MSGB_PRINTF_OR_RET(MGCP_X_OSMO_OSMUX_HEADER "%s\r\n",
1431 mgcp_msg->x_osmo_osmux_cid == -1 ? " *" : buf);
Pau Espin Pedrol900cd652019-04-24 22:06:22 +02001432 }
1433
1434
Philipp Maier704c4f02018-06-07 18:51:31 +02001435 /* Add session description protocol (SDP) */
1436 if (use_sdp
1437 && (mgcp_msg->verb == MGCP_VERB_CRCX
1438 || mgcp_msg->verb == MGCP_VERB_MDCX)) {
Oliver Smith0dbaaad2023-02-22 16:30:02 +01001439 if (add_sdp(msg, mgcp_msg, mgcp) < 0) {
1440 LOGPMGW(mgcp, LOGL_ERROR, "Failed to add SDP, can not generate MGCP message\n");
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001441 goto exit_error;
Oliver Smith0dbaaad2023-02-22 16:30:02 +01001442 }
Philipp Maier1dc6be62017-10-05 18:25:37 +02001443 }
1444
Philipp Maier1dc6be62017-10-05 18:25:37 +02001445 return msg;
Neels Hofmeyr45fe47c2023-03-30 16:53:13 +02001446
1447exit_error:
1448 msgb_free(msg);
1449 return NULL;
1450#undef MSGB_PRINTF_OR_RET
Philipp Maier1dc6be62017-10-05 18:25:37 +02001451}
1452
Philipp Maier275ac972018-01-20 00:58:16 +01001453/*! Retrieve the MGCP transaction ID from a msgb generated by mgcp_msg_gen()
1454 * \param[in] msg message buffer
1455 * \returns Transaction id. */
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +01001456mgcp_trans_id_t mgcp_msg_trans_id(struct msgb *msg)
1457{
1458 return (mgcp_trans_id_t)msg->cb[MSGB_CB_MGCP_TRANS_ID];
1459}
1460
Philipp Maierfa495d62021-09-02 10:08:56 +02001461/*! Get the configuration parameters for a given MGCP client instance
Philipp Maier275ac972018-01-20 00:58:16 +01001462 * \param[in] mgcp MGCP client descriptor.
1463 * \returns configuration */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +02001464struct mgcp_client_conf *mgcp_client_conf_actual(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001465{
1466 return &mgcp->actual;
1467}
Neels Hofmeyrd95ab1e2017-09-22 00:52:54 +02001468
1469const struct value_string mgcp_client_connection_mode_strs[] = {
1470 { MGCP_CONN_NONE, "none" },
1471 { MGCP_CONN_RECV_SEND, "sendrecv" },
1472 { MGCP_CONN_SEND_ONLY, "sendonly" },
1473 { MGCP_CONN_RECV_ONLY, "recvonly" },
1474 { MGCP_CONN_LOOPBACK, "loopback" },
1475 { 0, NULL }
1476};
Philipp Maierdf9192e2021-09-03 17:50:51 +02001477
1478/*! Get MGCP client instance name (VTY).
1479 * \param[in] mgcp MGCP client descriptor.
1480 * \returns MGCP client name.
1481 *
1482 * The user can only modify the name of an MGCP client instance when it is
1483 * part of a pool. For single MGCP client instances and MGCP client instance
1484 * where no description is set via the VTY, the MGW domain name will be used
1485 * as name. */
1486const char *mgcp_client_name(const struct mgcp_client *mgcp)
1487{
1488 if (!mgcp)
1489 return "(null)";
1490
1491 if (mgcp->actual.description)
1492 return mgcp->actual.description;
1493 else
1494 return mgcp_client_endpoint_domain(mgcp);
1495}