blob: c3ecc9fb714b5eb163a81adb35413b768e742ca2 [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
Philipp Maierc824fe42021-08-03 15:00:02 +0200197/*! Initialize MGCP client configuration struct with default values.
Philipp Maier275ac972018-01-20 00:58:16 +0100198 * \param[out] conf Client configuration.*/
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200199void mgcp_client_conf_init(struct mgcp_client_conf *conf)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200200{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200201 /* NULL and -1 default to MGCP_CLIENT_*_DEFAULT values */
202 *conf = (struct mgcp_client_conf){
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200203 .local_addr = NULL,
204 .local_port = -1,
205 .remote_addr = NULL,
206 .remote_port = -1,
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200207 };
Philipp Maier3f2c15f2021-07-22 11:53:07 +0200208
209 INIT_LLIST_HEAD(&conf->reset_epnames);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200210}
211
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200212static void mgcp_client_handle_response(struct mgcp_client *mgcp,
213 struct mgcp_response_pending *pending,
214 struct mgcp_response *response)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200215{
216 if (!pending) {
Philipp Maierdf9192e2021-09-03 17:50:51 +0200217 LOGPMGW(mgcp, LOGL_ERROR, "Cannot handle NULL response\n");
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200218 return;
219 }
220 if (pending->response_cb)
221 pending->response_cb(response, pending->priv);
222 else
Philipp Maierdf9192e2021-09-03 17:50:51 +0200223 LOGPMGW(mgcp, LOGL_DEBUG, "MGCP response ignored (NULL cb)\n");
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200224 talloc_free(pending);
225}
226
227static int mgcp_response_parse_head(struct mgcp_response *r, struct msgb *msg)
228{
229 int comment_pos;
230 char *end;
231
232 if (mgcp_msg_terminate_nul(msg))
233 goto response_parse_failure;
234
235 r->body = (char *)msg->data;
236
Harald Welte9bf7c532017-11-17 14:14:31 +0100237 if (sscanf(r->body, "%3d %u %n",
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200238 &r->head.response_code, &r->head.trans_id,
239 &comment_pos) != 2)
240 goto response_parse_failure;
241
Philipp Maierabe8c892018-01-20 00:15:12 +0100242 osmo_strlcpy(r->head.comment, r->body + comment_pos, sizeof(r->head.comment));
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200243 end = strchr(r->head.comment, '\r');
244 if (!end)
245 goto response_parse_failure;
246 /* Mark the end of the comment */
247 *end = '\0';
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200248 return 0;
249
250response_parse_failure:
251 LOGP(DLMGCP, LOGL_ERROR,
252 "Failed to parse MGCP response header\n");
253 return -EINVAL;
254}
255
256/* TODO undup against mgcp_protocol.c:mgcp_check_param() */
257static bool mgcp_line_is_valid(const char *line)
258{
259 const size_t line_len = strlen(line);
260 if (line[0] == '\0')
261 return true;
262
263 if (line_len < 2
264 || line[1] != '=') {
265 LOGP(DLMGCP, LOGL_ERROR,
266 "Wrong MGCP option format: '%s'\n",
267 line);
268 return false;
269 }
270
271 return true;
272}
273
Philipp Maier704c4f02018-06-07 18:51:31 +0200274/* Parse a line like "m=audio 16002 RTP/AVP 98", extract port and payload types */
275static int mgcp_parse_audio_port_pt(struct mgcp_response *r, char *line)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200276{
Philipp Maier704c4f02018-06-07 18:51:31 +0200277 char *pt_str;
Pau Espin Pedrolc5c14302019-07-23 16:19:41 +0200278 char *pt_end;
Pau Espin Pedrola2b1c5e2019-07-26 14:13:14 +0200279 unsigned long int pt;
Philipp Maier704c4f02018-06-07 18:51:31 +0200280 unsigned int count = 0;
281 unsigned int i;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200282
Philipp Maier704c4f02018-06-07 18:51:31 +0200283 /* Extract port information */
284 if (sscanf(line, "m=audio %hu", &r->audio_port) != 1)
285 goto response_parse_failure_port;
Philipp Maier10f32db2017-12-13 12:34:34 +0100286 if (r->audio_port == 0)
Philipp Maier704c4f02018-06-07 18:51:31 +0200287 goto response_parse_failure_port;
Philipp Maier10f32db2017-12-13 12:34:34 +0100288
Philipp Maier704c4f02018-06-07 18:51:31 +0200289 /* Extract payload types */
290 line = strstr(line, "RTP/AVP ");
291 if (!line)
292 goto exit;
293
294 pt_str = strtok(line, " ");
295 while (1) {
296 /* Do not allow excessive payload types */
297 if (count > ARRAY_SIZE(r->codecs))
298 goto response_parse_failure_pt;
299
300 pt_str = strtok(NULL, " ");
301 if (!pt_str)
302 break;
Pau Espin Pedrolc5c14302019-07-23 16:19:41 +0200303 errno = 0;
304 pt = strtoul(pt_str, &pt_end, 0);
305 if ((errno == ERANGE && pt == ULONG_MAX) || (errno && !pt) ||
306 pt_str == pt_end)
307 goto response_parse_failure_pt;
Philipp Maier704c4f02018-06-07 18:51:31 +0200308
Pau Espin Pedrola2b1c5e2019-07-26 14:13:14 +0200309 if (pt >> 7) /* PT is 7 bit field, higher values not allowed */
310 goto response_parse_failure_pt;
311
Philipp Maier704c4f02018-06-07 18:51:31 +0200312 /* Do not allow duplicate payload types */
313 for (i = 0; i < count; i++)
314 if (r->codecs[i] == pt)
315 goto response_parse_failure_pt;
316
317 /* Note: The payload type we store may not necessarly match
318 * the codec types we have defined in enum mgcp_codecs. To
319 * ensure that the end result only contains codec types which
320 * match enum mgcp_codecs, we will go through afterwards and
321 * remap the affected entries with the inrofmation we learn
322 * from rtpmap */
323 r->codecs[count] = pt;
324 count++;
325 }
326
327 r->codecs_len = count;
328
329exit:
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200330 return 0;
331
Philipp Maier704c4f02018-06-07 18:51:31 +0200332response_parse_failure_port:
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200333 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier704c4f02018-06-07 18:51:31 +0200334 "Failed to parse SDP parameter port (%s)\n", line);
Philipp Maier06da85e2017-10-05 18:49:24 +0200335 return -EINVAL;
Philipp Maier704c4f02018-06-07 18:51:31 +0200336
337response_parse_failure_pt:
338 LOGP(DLMGCP, LOGL_ERROR,
339 "Failed to parse SDP parameter payload types (%s)\n", line);
340 return -EINVAL;
341}
342
343/* Parse a line like "m=audio 16002 RTP/AVP 98", extract port and payload types */
344static int mgcp_parse_audio_ptime_rtpmap(struct mgcp_response *r, const char *line)
345{
346 unsigned int pt;
347 char codec_resp[64];
Neels Hofmeyr3ab8ca42019-08-08 04:03:42 +0200348 enum mgcp_codecs codec;
Pau Espin Pedrolc12bfb72019-04-16 17:23:09 +0200349
Neels Hofmeyr23f40482019-08-08 04:03:42 +0200350#define A_PTIME "a=ptime:"
351#define A_RTPMAP "a=rtpmap:"
Pau Espin Pedrolc12bfb72019-04-16 17:23:09 +0200352
Neels Hofmeyr23f40482019-08-08 04:03:42 +0200353 if (osmo_str_startswith(line, A_PTIME)) {
354 if (sscanf(line, A_PTIME "%u", &r->ptime) != 1) {
355 LOGP(DLMGCP, LOGL_ERROR,
356 "Failed to parse SDP parameter, invalid ptime (%s)\n", line);
357 return -EINVAL;
358 }
359 } else if (osmo_str_startswith(line, A_RTPMAP)) {
360 if (sscanf(line, A_RTPMAP "%d %63s", &pt, codec_resp) != 2) {
361 LOGP(DLMGCP, LOGL_ERROR,
362 "Failed to parse SDP parameter, invalid rtpmap: %s\n", osmo_quote_str(line, -1));
363 return -EINVAL;
364 }
Neels Hofmeyr3ab8ca42019-08-08 04:03:42 +0200365 if (r->ptmap_len >= ARRAY_SIZE(r->ptmap)) {
366 LOGP(DLMGCP, LOGL_ERROR, "No more space in ptmap array (len=%u)\n", r->ptmap_len);
367 return -ENOSPC;
Neels Hofmeyr23f40482019-08-08 04:03:42 +0200368 }
Neels Hofmeyr3ab8ca42019-08-08 04:03:42 +0200369 codec = map_str_to_codec(codec_resp);
370 r->ptmap[r->ptmap_len].pt = pt;
371 r->ptmap[r->ptmap_len].codec = codec;
372 r->ptmap_len++;
Philipp Maier704c4f02018-06-07 18:51:31 +0200373 }
Pau Espin Pedrolc12bfb72019-04-16 17:23:09 +0200374
Philipp Maier704c4f02018-06-07 18:51:31 +0200375 return 0;
Philipp Maier06da85e2017-10-05 18:49:24 +0200376}
377
378/* Parse a line like "c=IN IP4 10.11.12.13" */
379static int mgcp_parse_audio_ip(struct mgcp_response *r, const char *line)
380{
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +0200381 struct in6_addr ip_test;
382 bool is_ipv6;
Philipp Maier06da85e2017-10-05 18:49:24 +0200383
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +0200384 if (strncmp("c=IN IP", line, 7) != 0)
Philipp Maier06da85e2017-10-05 18:49:24 +0200385 goto response_parse_failure;
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +0200386 line += 7;
387 if (*line == '6')
388 is_ipv6 = true;
389 else if (*line == '4')
390 is_ipv6 = false;
391 else
Philipp Maier06da85e2017-10-05 18:49:24 +0200392 goto response_parse_failure;
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +0200393 line++;
394 if (*line != ' ')
Philipp Maier06da85e2017-10-05 18:49:24 +0200395 goto response_parse_failure;
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +0200396 line++;
Philipp Maier06da85e2017-10-05 18:49:24 +0200397
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +0200398 /* Extract and check IP-Address */
399 if (is_ipv6) {
400 /* 45 = INET6_ADDRSTRLEN -1 */
401 if (sscanf(line, "%45s", r->audio_ip) != 1)
402 goto response_parse_failure;
403 if (inet_pton(AF_INET6, r->audio_ip, &ip_test) != 1)
404 goto response_parse_failure;
405 } else {
406 /* 15 = INET_ADDRSTRLEN -1 */
407 if (sscanf(line, "%15s", r->audio_ip) != 1)
408 goto response_parse_failure;
409 if (inet_pton(AF_INET, r->audio_ip, &ip_test) != 1)
410 goto response_parse_failure;
411 }
Philipp Maier06da85e2017-10-05 18:49:24 +0200412 return 0;
413
414response_parse_failure:
415 LOGP(DLMGCP, LOGL_ERROR,
416 "Failed to parse MGCP response header (audio ip)\n");
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200417 return -EINVAL;
418}
419
Pau Espin Pedrol91088c32019-04-24 21:02:40 +0200420/*! Extract OSMUX CID from an MGCP parameter line (string).
421 * \param[in] line single parameter line from the MGCP message
422 * \returns OSMUX CID, -1 wildcard, -2 on error
423 * FIXME: This is a copy of function in mgcp_msg.c. Have some common.c file between both libs?
424 */
425static int mgcp_parse_osmux_cid(const char *line)
426{
427 int osmux_cid;
428
429
Pau Espin Pedrolc1bf4692019-05-14 16:23:24 +0200430 if (strcasecmp(line + 2, "Osmux: *") == 0) {
Pau Espin Pedrol91088c32019-04-24 21:02:40 +0200431 LOGP(DLMGCP, LOGL_DEBUG, "Parsed wilcard Osmux CID\n");
432 return -1;
433 }
434
Pau Espin Pedrolc1bf4692019-05-14 16:23:24 +0200435 if (sscanf(line + 2 + 7, "%u", &osmux_cid) != 1) {
Pau Espin Pedrol91088c32019-04-24 21:02:40 +0200436 LOGP(DLMGCP, LOGL_ERROR, "Failed parsing Osmux in MGCP msg line: %s\n",
437 line);
438 return -2;
439 }
440
Pau Espin Pedrol91088c32019-04-24 21:02:40 +0200441 if (osmux_cid > OSMUX_CID_MAX) { /* OSMUX_CID_MAX from libosmo-netif */
442 LOGP(DLMGCP, LOGL_ERROR, "Osmux ID too large: %u > %u\n",
443 osmux_cid, OSMUX_CID_MAX);
444 return -2;
445 }
Pau Espin Pedrolc7c8e642022-10-06 18:24:21 +0200446 LOGP(DLMGCP, LOGL_DEBUG, "MGW offered Osmux CID %u\n", osmux_cid);
Pau Espin Pedrol91088c32019-04-24 21:02:40 +0200447
448 return osmux_cid;
449}
450
Philipp Maier3b12e1b2018-01-18 15:16:13 +0100451/* A new section is marked by a double line break, check a few more
452 * patterns as there may be variants */
453static char *mgcp_find_section_end(char *string)
454{
455 char *rc;
456
457 rc = strstr(string, "\n\n");
458 if (rc)
459 return rc;
460
461 rc = strstr(string, "\n\r\n\r");
462 if (rc)
463 return rc;
464
465 rc = strstr(string, "\r\n\r\n");
466 if (rc)
467 return rc;
468
469 return NULL;
470}
471
Philipp Maier275ac972018-01-20 00:58:16 +0100472/*! Parse body (SDP) parameters of the MGCP response
473 * \param[in,out] r Response data
474 * \returns 0 on success, -EINVAL on error. */
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200475int mgcp_response_parse_params(struct mgcp_response *r)
476{
477 char *line;
478 int rc;
Neels Hofmeyr0793d2f2018-02-21 14:55:34 +0100479 char *data;
Philipp Maiere9d645b2018-01-19 23:54:08 +0100480 char *data_ptr;
Philipp Maier704c4f02018-06-07 18:51:31 +0200481 int i;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200482
Philipp Maiere9d645b2018-01-19 23:54:08 +0100483 /* Since this functions performs a destructive parsing, we create a
484 * local copy of the body data */
Neels Hofmeyr0793d2f2018-02-21 14:55:34 +0100485 OSMO_ASSERT(r->body);
486 data = talloc_strdup(r, r->body);
Philipp Maiere9d645b2018-01-19 23:54:08 +0100487 OSMO_ASSERT(data);
Philipp Maier55295f72018-01-15 14:00:28 +0100488
Philipp Maiere9d645b2018-01-19 23:54:08 +0100489 /* Find beginning of the parameter (SDP) section */
490 data_ptr = mgcp_find_section_end(data);
Neels Hofmeyr10835332018-02-21 14:55:34 +0100491 if (!data_ptr) {
Neels Hofmeyre8278312019-09-19 03:06:46 +0200492 LOGP(DLMGCP, LOGL_DEBUG, "MGCP response contains no SDP parameters\n");
493 rc = 0;
Philipp Maiere9d645b2018-01-19 23:54:08 +0100494 goto exit;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200495 }
496
Neels Hofmeyr0793d2f2018-02-21 14:55:34 +0100497 /* data_ptr now points to the beginning of the section-end-marker; for_each_non_empty_line()
498 * skips any \r and \n characters for free, so we don't need to skip the marker. */
499
Philipp Maiere9d645b2018-01-19 23:54:08 +0100500 for_each_non_empty_line(line, data_ptr) {
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200501 if (!mgcp_line_is_valid(line))
502 return -EINVAL;
503
504 switch (line[0]) {
Philipp Maier704c4f02018-06-07 18:51:31 +0200505 case 'a':
506 rc = mgcp_parse_audio_ptime_rtpmap(r, line);
507 if (rc)
508 goto exit;
509 break;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200510 case 'm':
Philipp Maier704c4f02018-06-07 18:51:31 +0200511 rc = mgcp_parse_audio_port_pt(r, line);
Philipp Maier06da85e2017-10-05 18:49:24 +0200512 if (rc)
Philipp Maiere9d645b2018-01-19 23:54:08 +0100513 goto exit;
Philipp Maier06da85e2017-10-05 18:49:24 +0200514 break;
515 case 'c':
516 rc = mgcp_parse_audio_ip(r, line);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200517 if (rc)
Philipp Maiere9d645b2018-01-19 23:54:08 +0100518 goto exit;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200519 break;
520 default:
521 /* skip unhandled parameters */
522 break;
523 }
524 }
Philipp Maiere9d645b2018-01-19 23:54:08 +0100525
Philipp Maier704c4f02018-06-07 18:51:31 +0200526 /* See also note in mgcp_parse_audio_port_pt() */
527 for (i = 0; i < r->codecs_len; i++)
528 r->codecs[i] = map_pt_to_codec(r->ptmap, r->ptmap_len, r->codecs[i]);
529
Philipp Maiere9d645b2018-01-19 23:54:08 +0100530 rc = 0;
531exit:
532 talloc_free(data);
533 return rc;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200534}
535
Philipp Maier55295f72018-01-15 14:00:28 +0100536/* Parse a line like "X: something" */
537static int mgcp_parse_head_param(char *result, unsigned int result_len,
538 char label, const char *line)
Philipp Maierffd75e42017-11-22 11:44:50 +0100539{
Philipp Maier55295f72018-01-15 14:00:28 +0100540 char label_string[4];
Neels Hofmeyr23e7bf12018-09-03 21:26:22 +0200541 size_t rc;
Philipp Maier55295f72018-01-15 14:00:28 +0100542
543 /* Detect empty parameters */
Philipp Maierffd75e42017-11-22 11:44:50 +0100544 if (strlen(line) < 4)
545 goto response_parse_failure;
546
Philipp Maier55295f72018-01-15 14:00:28 +0100547 /* Check if the label matches */
548 snprintf(label_string, sizeof(label_string), "%c: ", label);
549 if (memcmp(label_string, line, 3) != 0)
Philipp Maierffd75e42017-11-22 11:44:50 +0100550 goto response_parse_failure;
551
Philipp Maier55295f72018-01-15 14:00:28 +0100552 /* Copy payload part of the string to destinations (the label string
553 * is always 3 chars long) */
Neels Hofmeyr23e7bf12018-09-03 21:26:22 +0200554 rc = osmo_strlcpy(result, line + 3, result_len);
555 if (rc >= result_len) {
556 LOGP(DLMGCP, LOGL_ERROR,
557 "Failed to parse MGCP response (parameter label: %c):"
558 " the received conn ID is too long: %zu, maximum is %u characters\n",
559 label, rc, result_len - 1);
560 return -ENOSPC;
561 }
Philipp Maierffd75e42017-11-22 11:44:50 +0100562 return 0;
563
564response_parse_failure:
565 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier55295f72018-01-15 14:00:28 +0100566 "Failed to parse MGCP response (parameter label: %c)\n", label);
Philipp Maierffd75e42017-11-22 11:44:50 +0100567 return -EINVAL;
568}
569
570/* Parse MGCP parameters of the response */
571static int parse_head_params(struct mgcp_response *r)
572{
573 char *line;
574 int rc = 0;
575 OSMO_ASSERT(r->body);
Philipp Maier55295f72018-01-15 14:00:28 +0100576 char *data;
577 char *data_ptr;
578 char *data_end;
Philipp Maierffd75e42017-11-22 11:44:50 +0100579
Philipp Maier55295f72018-01-15 14:00:28 +0100580 /* Since this functions performs a destructive parsing, we create a
581 * local copy of the body data */
Philipp Maier1fc69992018-01-31 15:32:55 +0100582 data = talloc_zero_size(r, strlen(r->body)+1);
Philipp Maier55295f72018-01-15 14:00:28 +0100583 OSMO_ASSERT(data);
584 data_ptr = data;
585 osmo_strlcpy(data, r->body, strlen(r->body));
586
587 /* If there is an SDP body attached, prevent for_each_non_empty_line()
588 * into running in there, we are not yet interested in the parameters
589 * stored there. */
Pau Espin Pedrolc12bfb72019-04-16 17:23:09 +0200590 data_end = mgcp_find_section_end(data);
Philipp Maierffd75e42017-11-22 11:44:50 +0100591 if (data_end)
592 *data_end = '\0';
593
Philipp Maier55295f72018-01-15 14:00:28 +0100594 for_each_non_empty_line(line, data_ptr) {
Pau Espin Pedrol166077e2019-06-26 12:21:38 +0200595 switch (toupper(line[0])) {
Philipp Maier55295f72018-01-15 14:00:28 +0100596 case 'Z':
597 rc = mgcp_parse_head_param(r->head.endpoint,
598 sizeof(r->head.endpoint),
599 'Z', line);
600 if (rc)
601 goto exit;
Philipp Maier771b26a2018-01-31 13:53:11 +0100602
603 /* A specific endpoint identifier returned by the MGW
604 * must not contain any wildcard characters */
605 if (strstr(r->head.endpoint, "*") != NULL) {
606 rc = -EINVAL;
607 goto exit;
608 }
Philipp Maier3261dc72018-01-31 14:03:13 +0100609
610 /* A specific endpoint identifier returned by the MGW
611 * must contain an @ character */
612 if (strstr(r->head.endpoint, "@") == NULL) {
613 rc = -EINVAL;
614 goto exit;
615 }
Philipp Maier55295f72018-01-15 14:00:28 +0100616 break;
Philipp Maierffd75e42017-11-22 11:44:50 +0100617 case 'I':
Philipp Maier55295f72018-01-15 14:00:28 +0100618 rc = mgcp_parse_head_param(r->head.conn_id,
619 sizeof(r->head.conn_id),
620 'I', line);
Philipp Maierffd75e42017-11-22 11:44:50 +0100621 if (rc)
622 goto exit;
623 break;
Pau Espin Pedrol91088c32019-04-24 21:02:40 +0200624 case 'X':
Pau Espin Pedrolc1bf4692019-05-14 16:23:24 +0200625 if (strncasecmp("Osmux: ", line + 2, strlen("Osmux: ")) == 0) {
Pau Espin Pedrol91088c32019-04-24 21:02:40 +0200626 rc = mgcp_parse_osmux_cid(line);
627 if (rc < 0) {
628 /* -1: we don't want wildcards in response. -2: error */
629 rc = -EINVAL;
630 goto exit;
631 }
632 r->head.x_osmo_osmux_use = true;
633 r->head.x_osmo_osmux_cid = (uint8_t) rc;
634 rc = 0;
635 break;
636 }
637 /* Ignore unknown X-headers */
638 break;
Philipp Maierffd75e42017-11-22 11:44:50 +0100639 default:
640 /* skip unhandled parameters */
641 break;
642 }
643 }
644exit:
Philipp Maier55295f72018-01-15 14:00:28 +0100645 talloc_free(data);
Philipp Maierffd75e42017-11-22 11:44:50 +0100646 return rc;
647}
648
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200649static struct mgcp_response_pending *mgcp_client_response_pending_get(
650 struct mgcp_client *mgcp,
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100651 mgcp_trans_id_t trans_id)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200652{
653 struct mgcp_response_pending *pending;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200654 llist_for_each_entry(pending, &mgcp->responses_pending, entry) {
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100655 if (pending->trans_id == trans_id) {
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200656 llist_del(&pending->entry);
657 return pending;
658 }
659 }
660 return NULL;
661}
662
663/* Feed an MGCP message into the receive processing.
664 * Parse the head and call any callback registered for the transaction id found
665 * in the MGCP message. This is normally called directly from the internal
666 * mgcp_do_read that reads from the socket connected to the MGCP gateway. This
667 * function is published mainly to be able to feed data from the test suite.
668 */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200669int mgcp_client_rx(struct mgcp_client *mgcp, struct msgb *msg)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200670{
Philipp Maier1fc69992018-01-31 15:32:55 +0100671 struct mgcp_response *r;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200672 struct mgcp_response_pending *pending;
673 int rc;
674
Philipp Maier1fc69992018-01-31 15:32:55 +0100675 r = talloc_zero(mgcp, struct mgcp_response);
676 OSMO_ASSERT(r);
677
678 rc = mgcp_response_parse_head(r, msg);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200679 if (rc) {
Philipp Maierdf9192e2021-09-03 17:50:51 +0200680 LOGPMGW(mgcp, LOGL_ERROR, "Cannot parse MGCP response (head)\n");
Philipp Maier1fc69992018-01-31 15:32:55 +0100681 rc = 1;
682 goto error;
Philipp Maierffd75e42017-11-22 11:44:50 +0100683 }
684
Philipp Maierdf9192e2021-09-03 17:50:51 +0200685 LOGPMGW(mgcp, LOGL_DEBUG, "MGCP client: Rx %d %u %s\n",
Neels Hofmeyrd5731072021-07-15 01:36:47 +0200686 r->head.response_code, r->head.trans_id, r->head.comment);
687
Philipp Maier1fc69992018-01-31 15:32:55 +0100688 rc = parse_head_params(r);
Philipp Maierffd75e42017-11-22 11:44:50 +0100689 if (rc) {
Philipp Maierdf9192e2021-09-03 17:50:51 +0200690 LOGPMGW(mgcp, LOGL_ERROR, "Cannot parse MGCP response (head parameters)\n");
Philipp Maier1fc69992018-01-31 15:32:55 +0100691 rc = 1;
692 goto error;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200693 }
694
Philipp Maier1fc69992018-01-31 15:32:55 +0100695 pending = mgcp_client_response_pending_get(mgcp, r->head.trans_id);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200696 if (!pending) {
Philipp Maierdf9192e2021-09-03 17:50:51 +0200697 LOGPMGW(mgcp, LOGL_ERROR, "Cannot find matching MGCP transaction for trans_id %d\n",
698 r->head.trans_id);
Philipp Maier1fc69992018-01-31 15:32:55 +0100699 rc = -ENOENT;
700 goto error;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200701 }
702
Philipp Maier1fc69992018-01-31 15:32:55 +0100703 mgcp_client_handle_response(mgcp, pending, r);
704 rc = 0;
705
706error:
707 talloc_free(r);
708 return rc;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200709}
710
711static int mgcp_do_read(struct osmo_fd *fd)
712{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200713 struct mgcp_client *mgcp = fd->data;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200714 struct msgb *msg;
715 int ret;
716
717 msg = msgb_alloc_headroom(4096, 128, "mgcp_from_gw");
718 if (!msg) {
Philipp Maierdf9192e2021-09-03 17:50:51 +0200719 LOGPMGW(mgcp, LOGL_ERROR, "Failed to allocate MGCP message.\n");
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200720 return -1;
721 }
722
723 ret = read(fd->fd, msg->data, 4096 - 128);
724 if (ret <= 0) {
Philipp Maierdf9192e2021-09-03 17:50:51 +0200725 LOGPMGW(mgcp, LOGL_ERROR, "Failed to read: %s: %d='%s'\n",
726 osmo_sock_get_name2(fd->fd), errno, strerror(errno));
Neels Hofmeyr0a403792018-12-12 03:10:18 +0100727
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200728 msgb_free(msg);
729 return -1;
Harald Welte9bf7c532017-11-17 14:14:31 +0100730 }
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200731
732 msg->l2h = msgb_put(msg, ret);
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200733 ret = mgcp_client_rx(mgcp, msg);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200734 talloc_free(msg);
735 return ret;
736}
737
738static int mgcp_do_write(struct osmo_fd *fd, struct msgb *msg)
739{
740 int ret;
Philipp Maierdf9192e2021-09-03 17:50:51 +0200741 struct mgcp_client *mgcp = fd->data;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200742
Philipp Maierdf9192e2021-09-03 17:50:51 +0200743 LOGPMGW(mgcp, LOGL_DEBUG, "Tx MGCP: %s: len=%u '%s'...\n",
744 osmo_sock_get_name2(fd->fd), msg->len,
745 osmo_escape_str((const char *)msg->data, OSMO_MIN(42, msg->len)));
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200746
747 ret = write(fd->fd, msg->data, msg->len);
748 if (ret != msg->len)
Philipp Maierdf9192e2021-09-03 17:50:51 +0200749 LOGPMGW(mgcp, LOGL_ERROR, "Failed to Tx MGCP: %s: %d='%s'; msg: len=%u '%s'...\n",
750 osmo_sock_get_name2(fd->fd), errno, strerror(errno),
751 msg->len, osmo_escape_str((const char *)msg->data, OSMO_MIN(42, msg->len)));
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200752 return ret;
753}
754
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200755struct mgcp_client *mgcp_client_init(void *ctx,
756 struct mgcp_client_conf *conf)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200757{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200758 struct mgcp_client *mgcp;
Philipp Maier3f2c15f2021-07-22 11:53:07 +0200759 struct reset_ep *reset_ep;
760 struct reset_ep *actual_reset_ep;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200761
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200762 mgcp = talloc_zero(ctx, struct mgcp_client);
Harald Welteefe15712020-07-15 10:56:45 +0200763 if (!mgcp)
764 return NULL;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200765
766 INIT_LLIST_HEAD(&mgcp->responses_pending);
767 INIT_LLIST_HEAD(&mgcp->inuse_endpoints);
768
769 mgcp->next_trans_id = 1;
770
771 mgcp->actual.local_addr = conf->local_addr ? conf->local_addr :
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200772 MGCP_CLIENT_LOCAL_ADDR_DEFAULT;
Pau Espin Pedrol8e74c632022-10-17 19:20:45 +0200773 mgcp->actual.local_port = conf->local_port > 0 ? (uint16_t)conf->local_port :
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200774 MGCP_CLIENT_LOCAL_PORT_DEFAULT;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200775
776 mgcp->actual.remote_addr = conf->remote_addr ? conf->remote_addr :
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200777 MGCP_CLIENT_REMOTE_ADDR_DEFAULT;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200778 mgcp->actual.remote_port = conf->remote_port >= 0 ? (uint16_t)conf->remote_port :
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200779 MGCP_CLIENT_REMOTE_PORT_DEFAULT;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200780
Neels Hofmeyrac69ea92018-12-19 00:27:50 +0100781 if (osmo_strlcpy(mgcp->actual.endpoint_domain_name, conf->endpoint_domain_name,
782 sizeof(mgcp->actual.endpoint_domain_name))
783 >= sizeof(mgcp->actual.endpoint_domain_name)) {
Philipp Maierdf9192e2021-09-03 17:50:51 +0200784 LOGPMGW(mgcp, LOGL_ERROR, "MGCP client: endpoint domain name is too long, max length is %zu: '%s'\n",
785 sizeof(mgcp->actual.endpoint_domain_name) - 1, conf->endpoint_domain_name);
Neels Hofmeyrac69ea92018-12-19 00:27:50 +0100786 talloc_free(mgcp);
787 return NULL;
788 }
Philipp Maierdf9192e2021-09-03 17:50:51 +0200789 LOGPMGW(mgcp, LOGL_NOTICE, "MGCP client: using endpoint domain '@%s'\n", mgcp_client_endpoint_domain(mgcp));
Neels Hofmeyrac69ea92018-12-19 00:27:50 +0100790
Philipp Maier3f2c15f2021-07-22 11:53:07 +0200791 INIT_LLIST_HEAD(&mgcp->actual.reset_epnames);
792 llist_for_each_entry(reset_ep, &conf->reset_epnames, list) {
793 actual_reset_ep = talloc_memdup(mgcp, reset_ep, sizeof(*reset_ep));
794 llist_add_tail(&actual_reset_ep->list, &mgcp->actual.reset_epnames);
795 }
796
Philipp Maierdf9192e2021-09-03 17:50:51 +0200797 if (conf->description)
798 mgcp->actual.description = talloc_strdup(mgcp, conf->description);
799
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200800 return mgcp;
801}
802
Philipp Maier3f2c15f2021-07-22 11:53:07 +0200803/* Safely ignore the MGCP response to the DLCX sent via _mgcp_client_send_dlcx() */
804static void _ignore_mgcp_response(struct mgcp_response *response, void *priv) { }
805
806/* Format DLCX message (fire and forget) and send it off to the MGW */
807static void _mgcp_client_send_dlcx(struct mgcp_client *mgcp, const char *epname)
808{
809 struct msgb *msgb_dlcx;
810 struct mgcp_msg mgcp_msg_dlcx = {
811 .verb = MGCP_VERB_DLCX,
812 .presence = MGCP_MSG_PRESENCE_ENDPOINT,
813 };
814 osmo_strlcpy(mgcp_msg_dlcx.endpoint, epname, sizeof(mgcp_msg_dlcx.endpoint));
815 msgb_dlcx = mgcp_msg_gen(mgcp, &mgcp_msg_dlcx);
816 mgcp_client_tx(mgcp, msgb_dlcx, &_ignore_mgcp_response, NULL);
817}
818
819static const char *_mgcp_client_name_append_domain(const struct mgcp_client *mgcp, const char *name)
820{
821 static char endpoint[MGCP_ENDPOINT_MAXLEN];
822 int rc;
823
824 rc = snprintf(endpoint, sizeof(endpoint), "%s@%s", name, mgcp_client_endpoint_domain(mgcp));
825 if (rc > sizeof(endpoint) - 1) {
Philipp Maierdf9192e2021-09-03 17:50:51 +0200826 LOGPMGW(mgcp, LOGL_ERROR, "MGCP endpoint exceeds maximum length of %zu: '%s@%s'\n",
827 sizeof(endpoint) - 1, name, mgcp_client_endpoint_domain(mgcp));
Philipp Maier3f2c15f2021-07-22 11:53:07 +0200828 return NULL;
829 }
830 if (rc < 1) {
Philipp Maierdf9192e2021-09-03 17:50:51 +0200831 LOGPMGW(mgcp, LOGL_ERROR, "Cannot compose MGCP endpoint name\n");
Philipp Maier3f2c15f2021-07-22 11:53:07 +0200832 return NULL;
833 }
834 return endpoint;
835}
836
837/*! Initialize client connection (opens socket)
Philipp Maier275ac972018-01-20 00:58:16 +0100838 * \param[in,out] mgcp MGCP client descriptor.
839 * \returns 0 on success, -EINVAL on error. */
Pau Espin Pedrol8e74c632022-10-17 19:20:45 +0200840int mgcp_client_connect(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200841{
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200842 struct osmo_wqueue *wq;
843 int rc;
Philipp Maier3f2c15f2021-07-22 11:53:07 +0200844 struct reset_ep *reset_ep;
845 const char *epname;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200846
847 if (!mgcp) {
Philipp Maierdf9192e2021-09-03 17:50:51 +0200848 LOGPMGW(mgcp, LOGL_FATAL, "Client not initialized properly\n");
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200849 return -EINVAL;
850 }
851
852 wq = &mgcp->wq;
Harald Weltec2a8f592020-10-19 13:25:41 +0200853 osmo_wqueue_init(wq, 1024);
854 wq->read_cb = mgcp_do_read;
855 wq->write_cb = mgcp_do_write;
856
857 osmo_fd_setup(&wq->bfd, -1, OSMO_FD_READ, osmo_wqueue_bfd_cb, mgcp, 0);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200858
Pau Espin Pedrol8e74c632022-10-17 19:20:45 +0200859 rc = osmo_sock_init2_ofd(&wq->bfd, AF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, mgcp->actual.local_addr,
860 mgcp->actual.local_port, mgcp->actual.remote_addr, mgcp->actual.remote_port,
861 OSMO_SOCK_F_BIND | OSMO_SOCK_F_CONNECT);
Harald Welte8890dfa2017-11-17 15:09:30 +0100862 if (rc < 0) {
Philipp Maierdf9192e2021-09-03 17:50:51 +0200863 LOGPMGW(mgcp, LOGL_FATAL,
864 "Failed to initialize socket %s:%u -> %s:%u for MGW: %s\n",
Philipp Maier276a4142021-07-29 11:55:14 +0200865 mgcp->actual.local_addr ? mgcp->actual.local_addr : "(any)", mgcp->actual.local_port,
866 mgcp->actual.remote_addr ? mgcp->actual.local_addr : "(any)", mgcp->actual.remote_port,
867 strerror(errno));
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200868 goto error_close_fd;
869 }
870
Philipp Maierdf9192e2021-09-03 17:50:51 +0200871 LOGPMGW(mgcp, LOGL_INFO, "MGW connection: %s\n", osmo_sock_get_name2(wq->bfd.fd));
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200872
Philipp Maier3f2c15f2021-07-22 11:53:07 +0200873 /* If configured, send a DLCX message to the endpoints that are configured to
874 * be reset on startup. Usually this is a wildcarded endpoint. */
875 llist_for_each_entry(reset_ep, &mgcp->actual.reset_epnames, list) {
876 epname = _mgcp_client_name_append_domain(mgcp, reset_ep->name);
Philipp Maierdf9192e2021-09-03 17:50:51 +0200877 LOGPMGW(mgcp, LOGL_INFO, "Sending DLCX to: %s\n", epname);
Philipp Maier3f2c15f2021-07-22 11:53:07 +0200878 _mgcp_client_send_dlcx(mgcp, epname);
879 }
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200880 return 0;
881error_close_fd:
882 close(wq->bfd.fd);
883 wq->bfd.fd = -1;
884 return rc;
885}
886
Pau Espin Pedrol8e74c632022-10-17 19:20:45 +0200887/*! DEPRECATED: Initialize client connection (opens socket)
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200888 * \param[in,out] mgcp MGCP client descriptor.
889 * \returns 0 on success, -EINVAL on error. */
Pau Espin Pedrol8e74c632022-10-17 19:20:45 +0200890int mgcp_client_connect2(struct mgcp_client *mgcp, unsigned int retry_n_ports)
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200891{
Pau Espin Pedrol8e74c632022-10-17 19:20:45 +0200892 return mgcp_client_connect(mgcp);
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200893}
894
895/*! Terminate client connection
896 * \param[in,out] mgcp MGCP client descriptor.
897 * \returns 0 on success, -EINVAL on error. */
898void mgcp_client_disconnect(struct mgcp_client *mgcp)
899{
900 struct osmo_wqueue *wq;
901
902 if (!mgcp) {
Philipp Maierdf9192e2021-09-03 17:50:51 +0200903 LOGP(DLMGCP, LOGL_FATAL, "MGCP client not initialized properly\n");
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200904 return;
905 }
906
907 wq = &mgcp->wq;
908 osmo_wqueue_clear(wq);
Philipp Maierdf9192e2021-09-03 17:50:51 +0200909 LOGPMGW(mgcp, LOGL_INFO, "MGCP association: %s -- closed!\n", osmo_sock_get_name2(wq->bfd.fd));
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200910 close(wq->bfd.fd);
911 wq->bfd.fd = -1;
912 if (osmo_fd_is_registered(&wq->bfd))
913 osmo_fd_unregister(&wq->bfd);
914}
915
Philipp Maier275ac972018-01-20 00:58:16 +0100916/*! Get the IP-Aaddress of the associated MGW as string.
917 * \param[in] mgcp MGCP client descriptor.
918 * \returns a pointer to the address string. */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200919const char *mgcp_client_remote_addr_str(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200920{
921 return mgcp->actual.remote_addr;
922}
923
Philipp Maier275ac972018-01-20 00:58:16 +0100924/*! Get the IP-Port of the associated MGW.
925 * \param[in] mgcp MGCP client descriptor.
926 * \returns port number. */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200927uint16_t mgcp_client_remote_port(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200928{
929 return mgcp->actual.remote_port;
930}
931
Pau Espin Pedrol74d0e5c2020-09-02 17:19:20 +0200932/*! Get the IP-Address of the associated MGW as its numeric representation.
933 * DEPRECATED, DON'T USE.
Philipp Maier275ac972018-01-20 00:58:16 +0100934 * \param[in] mgcp MGCP client descriptor.
935 * \returns IP-Address as 32 bit integer (network byte order) */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200936uint32_t mgcp_client_remote_addr_n(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200937{
Pau Espin Pedrol74d0e5c2020-09-02 17:19:20 +0200938 return 0;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200939}
940
Neels Hofmeyrac69ea92018-12-19 00:27:50 +0100941/* To compose endpoint names, usually for CRCX, use this as domain name.
942 * For example, snprintf("rtpbridge\*@%s", mgcp_client_endpoint_domain(mgcp)). */
943const char *mgcp_client_endpoint_domain(const struct mgcp_client *mgcp)
944{
945 return mgcp->actual.endpoint_domain_name[0] ? mgcp->actual.endpoint_domain_name : "mgw";
946}
947
Philipp Maiera466f572020-06-25 16:11:04 +0200948/*! Compose endpoint name for a wildcarded request to the virtual trunk
949 * \param[in] mgcp MGCP client descriptor.
950 * \returns string containing the endpoint name (e.g. rtpbridge\*@mgw) */
Pau Espin Pedrolca0818c2019-04-16 17:23:38 +0200951const char *mgcp_client_rtpbridge_wildcard(const struct mgcp_client *mgcp)
952{
953 return _mgcp_client_name_append_domain(mgcp, "rtpbridge/*");
954}
955
Philipp Maier48635912020-06-25 20:16:22 +0200956/*! Compose endpoint name for an E1 endpoint.
957 * \param[in] ctx talloc context.
958 * \param[in] mgcp MGCP client descriptor.
959 * \param[in] trunk_id id number of the E1 trunk (1-64).
960 * \param[in] ts timeslot on the E1 trunk (1-31).
961 * \param[in] rate bitrate used on the E1 trunk (e.g 16 for 16kbit).
962 * \param[in] offset bit offset of the E1 subslot (e.g. 4 for the third 16k subslot).
963 * \returns string containing the endpoint name (e.g. ds/e1-1/s-1/su16-4). */
964const char *mgcp_client_e1_epname(void *ctx, const struct mgcp_client *mgcp, uint8_t trunk_id, uint8_t ts,
965 uint8_t rate, uint8_t offset)
966{
967 /* See also comment in libosmo-mgcp, mgcp_client.c, gen_e1_epname() */
968 const uint8_t valid_rates[] = { 64, 32, 32, 16, 16, 16, 16, 8, 8, 8, 8, 8, 8, 8, 8 };
969 const uint8_t valid_offsets[] = { 0, 0, 4, 0, 2, 4, 6, 0, 1, 2, 3, 4, 5, 6, 7 };
970
971 uint8_t i;
972 bool rate_offs_valid = false;
973 char *epname;
974
975 epname =
976 talloc_asprintf(ctx, "ds/e1-%u/s-%u/su%u-%u@%s", trunk_id, ts, rate, offset,
977 mgcp_client_endpoint_domain(mgcp));
978 if (!epname) {
Philipp Maierdf9192e2021-09-03 17:50:51 +0200979 LOGPMGW(mgcp, LOGL_ERROR, "Cannot compose MGCP e1-endpoint name!\n");
Philipp Maier48635912020-06-25 20:16:22 +0200980 return NULL;
981 }
982
983 /* Check if the supplied rate/offset pair resembles a valid combination */
984 for (i = 0; i < sizeof(valid_rates); i++) {
985 if (valid_rates[i] == rate && valid_offsets[i] == offset)
986 rate_offs_valid = true;
987 }
988 if (!rate_offs_valid) {
Philipp Maierdf9192e2021-09-03 17:50:51 +0200989 LOGPMGW(mgcp, LOGL_ERROR,
990 "Cannot compose MGCP e1-endpoint name (%s), rate(%u)/offset(%u) combination is invalid!\n",
991 epname, rate, offset);
Philipp Maier48635912020-06-25 20:16:22 +0200992 talloc_free(epname);
993 return NULL;
994 }
995
996 /* An E1 line has a maximum of 32 timeslots, while the first (ts=0) is
997 * reserverd for framing and alignment, so we can not use it here. */
Philipp Maier92a73cd2020-11-26 22:21:11 +0100998 if (ts == 0 || ts > NUM_E1_TS-1) {
Philipp Maierdf9192e2021-09-03 17:50:51 +0200999 LOGPMGW(mgcp, LOGL_ERROR,
1000 "Cannot compose MGCP e1-endpoint name (%s), E1-timeslot number (%u) is invalid!\n",
1001 epname, ts);
Philipp Maier48635912020-06-25 20:16:22 +02001002 talloc_free(epname);
1003 return NULL;
1004 }
1005
1006 return epname;
1007}
1008
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +02001009struct mgcp_response_pending * mgcp_client_pending_add(
1010 struct mgcp_client *mgcp,
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001011 mgcp_trans_id_t trans_id,
1012 mgcp_response_cb_t response_cb,
1013 void *priv)
1014{
1015 struct mgcp_response_pending *pending;
1016
1017 pending = talloc_zero(mgcp, struct mgcp_response_pending);
Harald Welte827da2c2020-07-15 10:57:08 +02001018 if (!pending)
1019 return NULL;
1020
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001021 pending->trans_id = trans_id;
1022 pending->response_cb = response_cb;
1023 pending->priv = priv;
1024 llist_add_tail(&pending->entry, &mgcp->responses_pending);
1025
1026 return pending;
1027}
1028
Philipp Maierdf9192e2021-09-03 17:50:51 +02001029/* Send the MGCP message in msg to the MGW and handle a response with
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001030 * response_cb. NOTE: the response_cb still needs to call
1031 * mgcp_response_parse_params(response) to get the parsed parameters -- to
1032 * potentially save some CPU cycles, only the head line has been parsed when
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +01001033 * the response_cb is invoked.
1034 * Before the priv pointer becomes invalid, e.g. due to transaction timeout,
1035 * mgcp_client_cancel() needs to be called for this transaction.
1036 */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +02001037int mgcp_client_tx(struct mgcp_client *mgcp, struct msgb *msg,
1038 mgcp_response_cb_t response_cb, void *priv)
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001039{
Harald Welte563ffc52020-06-17 21:54:13 +07001040 struct mgcp_response_pending *pending = NULL;
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001041 mgcp_trans_id_t trans_id;
1042 int rc;
1043
1044 trans_id = msg->cb[MSGB_CB_MGCP_TRANS_ID];
1045 if (!trans_id) {
Philipp Maierdf9192e2021-09-03 17:50:51 +02001046 LOGPMGW(mgcp, LOGL_ERROR,
1047 "Unset transaction id in mgcp send request\n");
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001048 talloc_free(msg);
1049 return -EINVAL;
1050 }
1051
Harald Welte563ffc52020-06-17 21:54:13 +07001052 /* Do not allocate a dummy 'mgcp_response_pending' entry */
1053 if (response_cb != NULL) {
1054 pending = mgcp_client_pending_add(mgcp, trans_id, response_cb, priv);
1055 if (!pending) {
1056 talloc_free(msg);
1057 return -ENOMEM;
1058 }
Harald Welte827da2c2020-07-15 10:57:08 +02001059 }
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001060
1061 if (msgb_l2len(msg) > 4096) {
Philipp Maierdf9192e2021-09-03 17:50:51 +02001062 LOGPMGW(mgcp, LOGL_ERROR,
1063 "Cannot send, MGCP message too large: %u\n",
1064 msgb_l2len(msg));
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001065 msgb_free(msg);
1066 rc = -EINVAL;
1067 goto mgcp_tx_error;
1068 }
1069
1070 rc = osmo_wqueue_enqueue(&mgcp->wq, msg);
1071 if (rc) {
Philipp Maierdf9192e2021-09-03 17:50:51 +02001072 LOGPMGW(mgcp, LOGL_FATAL, "Could not queue message to MGW\n");
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001073 msgb_free(msg);
1074 goto mgcp_tx_error;
1075 } else
Philipp Maierdf9192e2021-09-03 17:50:51 +02001076 LOGPMGW(mgcp, LOGL_DEBUG, "Queued %u bytes for MGW\n",
1077 msgb_l2len(msg));
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001078 return 0;
1079
1080mgcp_tx_error:
Harald Welte563ffc52020-06-17 21:54:13 +07001081 if (!pending)
Vadim Yanitskiyffbc6182020-07-16 15:48:13 +07001082 return rc;
Vadim Yanitskiy3f8139c2020-06-17 20:50:17 +07001083 /* Dequeue pending response, it's going to be free()d */
1084 llist_del(&pending->entry);
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001085 /* Pass NULL to response cb to indicate an error */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +02001086 mgcp_client_handle_response(mgcp, pending, NULL);
Vadim Yanitskiyffbc6182020-07-16 15:48:13 +07001087 return rc;
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001088}
1089
Philipp Maier275ac972018-01-20 00:58:16 +01001090/*! Cancel a pending transaction.
1091 * \param[in] mgcp MGCP client descriptor.
1092 * \param[in,out] trans_id Transaction id.
1093 * \returns 0 on success, -ENOENT on error.
1094 *
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +01001095 * Should a priv pointer passed to mgcp_client_tx() become invalid, this function must be called. In
1096 * practical terms, if the caller of mgcp_client_tx() wishes to tear down a transaction without having
1097 * received a response this function must be called. The trans_id can be obtained by calling
Philipp Maier275ac972018-01-20 00:58:16 +01001098 * mgcp_msg_trans_id() on the msgb produced by mgcp_msg_gen(). */
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +01001099int mgcp_client_cancel(struct mgcp_client *mgcp, mgcp_trans_id_t trans_id)
1100{
1101 struct mgcp_response_pending *pending = mgcp_client_response_pending_get(mgcp, trans_id);
1102 if (!pending) {
Philipp Maier275ac972018-01-20 00:58:16 +01001103 /*! Note: it is not harmful to cancel a transaction twice. */
Philipp Maierdf9192e2021-09-03 17:50:51 +02001104 LOGPMGW(mgcp, LOGL_ERROR, "Cannot cancel, no such transaction: %u\n", trans_id);
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +01001105 return -ENOENT;
1106 }
Philipp Maierdf9192e2021-09-03 17:50:51 +02001107 LOGPMGW(mgcp, LOGL_DEBUG, "Canceled transaction %u\n", trans_id);
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +01001108 talloc_free(pending);
1109 return 0;
Philipp Maier275ac972018-01-20 00:58:16 +01001110 /*! We don't really need to clean up the wqueue: In all sane cases, the msgb has already been sent
1111 * out and is no longer in the wqueue. If it still is in the wqueue, then sending MGCP messages
1112 * per se is broken and the program should notice so by a full wqueue. Even if this was called
1113 * before we had a chance to send out the message and it is still going to be sent, we will just
1114 * ignore the reply to it later. Removing a msgb from the wqueue here would just introduce more
1115 * bug surface in terms of failing to update wqueue API's counters or some such.
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +01001116 */
1117}
1118
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +02001119static mgcp_trans_id_t mgcp_client_next_trans_id(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001120{
1121 /* avoid zero trans_id to distinguish from unset trans_id */
1122 if (!mgcp->next_trans_id)
1123 mgcp->next_trans_id ++;
1124 return mgcp->next_trans_id ++;
1125}
1126
Philipp Maier1dc6be62017-10-05 18:25:37 +02001127#define MGCP_CRCX_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT | \
1128 MGCP_MSG_PRESENCE_CALL_ID | \
Philipp Maier1dc6be62017-10-05 18:25:37 +02001129 MGCP_MSG_PRESENCE_CONN_MODE)
1130#define MGCP_MDCX_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT | \
Philipp Maier490cbaa2018-01-22 17:32:38 +01001131 MGCP_MSG_PRESENCE_CALL_ID | \
Philipp Maier1dc6be62017-10-05 18:25:37 +02001132 MGCP_MSG_PRESENCE_CONN_ID)
1133#define MGCP_DLCX_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT)
1134#define MGCP_AUEP_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT)
1135#define MGCP_RSIP_MANDATORY 0 /* none */
1136
Philipp Maier704c4f02018-06-07 18:51:31 +02001137/* Helper function for mgcp_msg_gen(): Add LCO information to MGCP message */
1138static int add_lco(struct msgb *msg, struct mgcp_msg *mgcp_msg)
1139{
1140 unsigned int i;
1141 int rc = 0;
1142 const char *codec;
1143 unsigned int pt;
1144
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001145 rc |= msgb_printf(msg, "L:");
Philipp Maier704c4f02018-06-07 18:51:31 +02001146
1147 if (mgcp_msg->ptime)
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001148 rc |= msgb_printf(msg, " p:%u,", mgcp_msg->ptime);
Philipp Maier704c4f02018-06-07 18:51:31 +02001149
1150 if (mgcp_msg->codecs_len) {
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001151 rc |= msgb_printf(msg, " a:");
Philipp Maier704c4f02018-06-07 18:51:31 +02001152 for (i = 0; i < mgcp_msg->codecs_len; i++) {
1153 pt = mgcp_msg->codecs[i];
Neels Hofmeyr47642f22019-02-27 05:56:53 +01001154 codec = get_value_string_or_null(osmo_mgcpc_codec_names, pt);
Pau Espin Pedrolc12bfb72019-04-16 17:23:09 +02001155
Philipp Maier704c4f02018-06-07 18:51:31 +02001156 /* Note: Use codec descriptors from enum mgcp_codecs
1157 * in mgcp_client only! */
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001158 if (!codec) {
1159 msgb_free(msg);
1160 return -EINVAL;
1161 }
1162
1163 rc |= msgb_printf(msg, "%s", extract_codec_name(codec));
Philipp Maier704c4f02018-06-07 18:51:31 +02001164 if (i < mgcp_msg->codecs_len - 1)
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001165 rc |= msgb_printf(msg, ";");
Philipp Maier704c4f02018-06-07 18:51:31 +02001166 }
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001167 rc |= msgb_printf(msg, ",");
Philipp Maier704c4f02018-06-07 18:51:31 +02001168 }
1169
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001170 rc |= msgb_printf(msg, " nt:IN\r\n");
Philipp Maier704c4f02018-06-07 18:51:31 +02001171
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001172 if (rc != 0) {
1173 LOGP(DLMGCP, LOGL_ERROR,
1174 "message buffer to small, can not generate MGCP message (LCO)\n");
1175 msgb_free(msg);
1176 return -ENOBUFS;
1177 }
1178 return 0;
Philipp Maier704c4f02018-06-07 18:51:31 +02001179}
1180
1181/* Helper function for mgcp_msg_gen(): Add SDP information to MGCP message */
1182static int add_sdp(struct msgb *msg, struct mgcp_msg *mgcp_msg, struct mgcp_client *mgcp)
1183{
1184 unsigned int i;
1185 int rc = 0;
Pau Espin Pedrol8667d512020-08-28 19:37:08 +02001186 char local_ip[INET6_ADDRSTRLEN];
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +02001187 int local_ip_family, audio_ip_family;
Philipp Maier704c4f02018-06-07 18:51:31 +02001188 const char *codec;
1189 unsigned int pt;
1190
1191 /* Add separator to mark the beginning of the SDP block */
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001192 rc |= msgb_printf(msg, "\r\n");
Philipp Maier704c4f02018-06-07 18:51:31 +02001193
1194 /* Add SDP protocol version */
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001195 rc |= msgb_printf(msg, "v=0\r\n");
Philipp Maier704c4f02018-06-07 18:51:31 +02001196
1197 /* Determine local IP-Address */
1198 if (osmo_sock_local_ip(local_ip, mgcp->actual.remote_addr) < 0) {
Philipp Maierdf9192e2021-09-03 17:50:51 +02001199 LOGPMGW(mgcp, LOGL_ERROR,
1200 "Could not determine local IP-Address!\n");
Philipp Maier704c4f02018-06-07 18:51:31 +02001201 msgb_free(msg);
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001202 return -EINVAL;
Philipp Maier704c4f02018-06-07 18:51:31 +02001203 }
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +02001204 local_ip_family = osmo_ip_str_type(local_ip);
1205 if (local_ip_family == AF_UNSPEC) {
1206 msgb_free(msg);
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001207 return -EINVAL;
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +02001208 }
1209 audio_ip_family = osmo_ip_str_type(mgcp_msg->audio_ip);
1210 if (audio_ip_family == AF_UNSPEC) {
1211 msgb_free(msg);
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001212 return -EINVAL;
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +02001213 }
Philipp Maier704c4f02018-06-07 18:51:31 +02001214
1215 /* Add owner/creator (SDP) */
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001216 rc |= msgb_printf(msg, "o=- %x 23 IN IP%c %s\r\n", mgcp_msg->call_id,
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +02001217 local_ip_family == AF_INET6 ? '6' : '4',
1218 local_ip);
Philipp Maier704c4f02018-06-07 18:51:31 +02001219
1220 /* Add session name (none) */
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001221 rc |= msgb_printf(msg, "s=-\r\n");
Philipp Maier704c4f02018-06-07 18:51:31 +02001222
1223 /* Add RTP address and port */
1224 if (mgcp_msg->audio_port == 0) {
Philipp Maierdf9192e2021-09-03 17:50:51 +02001225 LOGPMGW(mgcp, LOGL_ERROR,
1226 "Invalid port number, can not generate MGCP message\n");
Philipp Maier704c4f02018-06-07 18:51:31 +02001227 msgb_free(msg);
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001228 return -EINVAL;
Philipp Maier704c4f02018-06-07 18:51:31 +02001229 }
1230 if (strlen(mgcp_msg->audio_ip) <= 0) {
Philipp Maierdf9192e2021-09-03 17:50:51 +02001231 LOGPMGW(mgcp, LOGL_ERROR,
1232 "Empty ip address, can not generate MGCP message\n");
Philipp Maier704c4f02018-06-07 18:51:31 +02001233 msgb_free(msg);
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001234 return -EINVAL;
Philipp Maier704c4f02018-06-07 18:51:31 +02001235 }
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001236 rc |= msgb_printf(msg, "c=IN IP%c %s\r\n",
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +02001237 audio_ip_family == AF_INET6 ? '6' : '4',
1238 mgcp_msg->audio_ip);
Philipp Maier704c4f02018-06-07 18:51:31 +02001239
1240 /* Add time description, active time (SDP) */
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001241 rc |= msgb_printf(msg, "t=0 0\r\n");
Philipp Maier704c4f02018-06-07 18:51:31 +02001242
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001243 rc |= msgb_printf(msg, "m=audio %u RTP/AVP", mgcp_msg->audio_port);
Philipp Maier704c4f02018-06-07 18:51:31 +02001244 for (i = 0; i < mgcp_msg->codecs_len; i++) {
1245 pt = map_codec_to_pt(mgcp_msg->ptmap, mgcp_msg->ptmap_len, mgcp_msg->codecs[i]);
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001246 rc |= msgb_printf(msg, " %u", pt);
Philipp Maier704c4f02018-06-07 18:51:31 +02001247
1248 }
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001249 rc |= msgb_printf(msg, "\r\n");
Philipp Maier704c4f02018-06-07 18:51:31 +02001250
Philipp Maier228e5912019-03-05 13:56:59 +01001251 /* Add optional codec parameters (fmtp) */
1252 if (mgcp_msg->param_present) {
1253 for (i = 0; i < mgcp_msg->codecs_len; i++) {
1254 /* The following is only applicable for AMR */
1255 if (mgcp_msg->codecs[i] != CODEC_AMR_8000_1 && mgcp_msg->codecs[i] != CODEC_AMRWB_16000_1)
1256 continue;
1257 pt = map_codec_to_pt(mgcp_msg->ptmap, mgcp_msg->ptmap_len, mgcp_msg->codecs[i]);
1258 if (mgcp_msg->param.amr_octet_aligned_present && mgcp_msg->param.amr_octet_aligned)
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001259 rc |= msgb_printf(msg, "a=fmtp:%u octet-align=1\r\n", pt);
Philipp Maier228e5912019-03-05 13:56:59 +01001260 else if (mgcp_msg->param.amr_octet_aligned_present && !mgcp_msg->param.amr_octet_aligned)
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001261 rc |= msgb_printf(msg, "a=fmtp:%u octet-align=0\r\n", pt);
Philipp Maier228e5912019-03-05 13:56:59 +01001262 }
1263 }
1264
Philipp Maier704c4f02018-06-07 18:51:31 +02001265 for (i = 0; i < mgcp_msg->codecs_len; i++) {
1266 pt = map_codec_to_pt(mgcp_msg->ptmap, mgcp_msg->ptmap_len, mgcp_msg->codecs[i]);
Pau Espin Pedrolc12bfb72019-04-16 17:23:09 +02001267
Philipp Maier704c4f02018-06-07 18:51:31 +02001268 /* Note: Only dynamic payload type from the range 96-127
1269 * require to be explained further via rtpmap. All others
1270 * are implcitly definedby the number in m=audio */
1271 if (pt >= 96 && pt <= 127) {
Neels Hofmeyr47642f22019-02-27 05:56:53 +01001272 codec = get_value_string_or_null(osmo_mgcpc_codec_names, mgcp_msg->codecs[i]);
Philipp Maier704c4f02018-06-07 18:51:31 +02001273
1274 /* Note: Use codec descriptors from enum mgcp_codecs
1275 * in mgcp_client only! */
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001276 if (!codec) {
1277 msgb_free(msg);
1278 return -EINVAL;
1279 }
Pau Espin Pedrolc12bfb72019-04-16 17:23:09 +02001280
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001281 rc |= msgb_printf(msg, "a=rtpmap:%u %s\r\n", pt, codec);
Philipp Maier704c4f02018-06-07 18:51:31 +02001282 }
1283 }
Pau Espin Pedrolc12bfb72019-04-16 17:23:09 +02001284
Philipp Maier704c4f02018-06-07 18:51:31 +02001285 if (mgcp_msg->ptime)
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001286 rc |= msgb_printf(msg, "a=ptime:%u\r\n", mgcp_msg->ptime);
Philipp Maier704c4f02018-06-07 18:51:31 +02001287
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001288 if (rc != 0) {
Philipp Maierdf9192e2021-09-03 17:50:51 +02001289 LOGPMGW(mgcp, LOGL_ERROR, "Message buffer to small, can not generate MGCP message (SDP)\n");
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001290 msgb_free(msg);
1291 return -ENOBUFS;
1292 }
1293
1294 return 0;
Philipp Maier704c4f02018-06-07 18:51:31 +02001295}
1296
Philipp Maier275ac972018-01-20 00:58:16 +01001297/*! Generate an MGCP message
1298 * \param[in] mgcp MGCP client descriptor.
1299 * \param[in] mgcp_msg Message description
1300 * \returns message buffer on success, NULL on error. */
Philipp Maier1dc6be62017-10-05 18:25:37 +02001301struct msgb *mgcp_msg_gen(struct mgcp_client *mgcp, struct mgcp_msg *mgcp_msg)
1302{
1303 mgcp_trans_id_t trans_id = mgcp_client_next_trans_id(mgcp);
1304 uint32_t mandatory_mask;
1305 struct msgb *msg = msgb_alloc_headroom(4096, 128, "MGCP tx");
1306 int rc = 0;
Philipp Maier704c4f02018-06-07 18:51:31 +02001307 bool use_sdp = false;
Pau Espin Pedrol900cd652019-04-24 22:06:22 +02001308 char buf[32];
Philipp Maier1dc6be62017-10-05 18:25:37 +02001309
1310 msg->l2h = msg->data;
1311 msg->cb[MSGB_CB_MGCP_TRANS_ID] = trans_id;
1312
1313 /* Add command verb */
1314 switch (mgcp_msg->verb) {
1315 case MGCP_VERB_CRCX:
1316 mandatory_mask = MGCP_CRCX_MANDATORY;
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001317 rc |= msgb_printf(msg, "CRCX %u", trans_id);
Philipp Maier1dc6be62017-10-05 18:25:37 +02001318 break;
1319 case MGCP_VERB_MDCX:
1320 mandatory_mask = MGCP_MDCX_MANDATORY;
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001321 rc |= msgb_printf(msg, "MDCX %u", trans_id);
Philipp Maier1dc6be62017-10-05 18:25:37 +02001322 break;
1323 case MGCP_VERB_DLCX:
1324 mandatory_mask = MGCP_DLCX_MANDATORY;
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001325 rc |= msgb_printf(msg, "DLCX %u", trans_id);
Philipp Maier1dc6be62017-10-05 18:25:37 +02001326 break;
1327 case MGCP_VERB_AUEP:
1328 mandatory_mask = MGCP_AUEP_MANDATORY;
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001329 rc |= msgb_printf(msg, "AUEP %u", trans_id);
Philipp Maier1dc6be62017-10-05 18:25:37 +02001330 break;
1331 case MGCP_VERB_RSIP:
1332 mandatory_mask = MGCP_RSIP_MANDATORY;
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001333 rc |= msgb_printf(msg, "RSIP %u", trans_id);
Philipp Maier1dc6be62017-10-05 18:25:37 +02001334 break;
1335 default:
Philipp Maierdf9192e2021-09-03 17:50:51 +02001336 LOGPMGW(mgcp, LOGL_ERROR, "Invalid command verb, can not generate MGCP message\n");
Philipp Maier1dc6be62017-10-05 18:25:37 +02001337 msgb_free(msg);
1338 return NULL;
1339 }
1340
1341 /* Check if mandatory fields are missing */
1342 if (!((mgcp_msg->presence & mandatory_mask) == mandatory_mask)) {
Philipp Maierdf9192e2021-09-03 17:50:51 +02001343 LOGPMGW(mgcp, LOGL_ERROR,
1344 "One or more missing mandatory fields, can not generate MGCP message\n");
Philipp Maier1dc6be62017-10-05 18:25:37 +02001345 msgb_free(msg);
1346 return NULL;
1347 }
1348
1349 /* Add endpoint name */
Philipp Maier7bc55522017-12-10 22:52:22 +01001350 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_ENDPOINT) {
1351 if (strlen(mgcp_msg->endpoint) <= 0) {
Philipp Maierdf9192e2021-09-03 17:50:51 +02001352 LOGPMGW(mgcp, LOGL_ERROR, "Empty endpoint name, can not generate MGCP message\n");
Philipp Maier7bc55522017-12-10 22:52:22 +01001353 msgb_free(msg);
1354 return NULL;
1355 }
Philipp Maier3aa81572018-01-31 14:13:45 +01001356
1357 if (strstr(mgcp_msg->endpoint, "@") == NULL) {
Philipp Maierdf9192e2021-09-03 17:50:51 +02001358 LOGPMGW(mgcp, LOGL_ERROR,
1359 "Endpoint name (%s) lacks separator (@), can not generate MGCP message\n",
1360 mgcp_msg->endpoint);
Philipp Maier3aa81572018-01-31 14:13:45 +01001361 msgb_free(msg);
Vadim Yanitskiye6743452020-06-18 03:04:13 +07001362 return NULL;
Philipp Maier3aa81572018-01-31 14:13:45 +01001363 }
1364
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001365 rc |= msgb_printf(msg, " %s", mgcp_msg->endpoint);
Philipp Maier7bc55522017-12-10 22:52:22 +01001366 }
Philipp Maier1dc6be62017-10-05 18:25:37 +02001367
1368 /* Add protocol version */
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001369 rc |= msgb_printf(msg, " MGCP 1.0\r\n");
Philipp Maier1dc6be62017-10-05 18:25:37 +02001370
1371 /* Add call id */
1372 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_CALL_ID)
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001373 rc |= msgb_printf(msg, "C: %x\r\n", mgcp_msg->call_id);
Philipp Maier1dc6be62017-10-05 18:25:37 +02001374
1375 /* Add connection id */
Philipp Maier7bc55522017-12-10 22:52:22 +01001376 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_CONN_ID) {
1377 if (strlen(mgcp_msg->conn_id) <= 0) {
Philipp Maierdf9192e2021-09-03 17:50:51 +02001378 LOGPMGW(mgcp, LOGL_ERROR, "Empty connection id, can not generate MGCP message\n");
Philipp Maier7bc55522017-12-10 22:52:22 +01001379 msgb_free(msg);
1380 return NULL;
1381 }
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001382 rc |= msgb_printf(msg, "I: %s\r\n", mgcp_msg->conn_id);
Philipp Maier7bc55522017-12-10 22:52:22 +01001383 }
Philipp Maier1dc6be62017-10-05 18:25:37 +02001384
Philipp Maier704c4f02018-06-07 18:51:31 +02001385 /* Using SDP makes sense when a valid IP/Port combination is specifiec,
1386 * if we do not know this information yet, we fall back to LCO */
1387 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_AUDIO_IP
1388 && mgcp_msg->presence & MGCP_MSG_PRESENCE_AUDIO_PORT)
1389 use_sdp = true;
1390
1391 /* Add local connection options (LCO) */
1392 if (!use_sdp
1393 && (mgcp_msg->verb == MGCP_VERB_CRCX
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001394 || mgcp_msg->verb == MGCP_VERB_MDCX)) {
1395 if (add_lco(msg, mgcp_msg) < 0)
1396 return NULL;
1397 }
Philipp Maier1dc6be62017-10-05 18:25:37 +02001398
1399 /* Add mode */
1400 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_CONN_MODE)
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001401 rc |=
Philipp Maier1dc6be62017-10-05 18:25:37 +02001402 msgb_printf(msg, "M: %s\r\n",
1403 mgcp_client_cmode_name(mgcp_msg->conn_mode));
1404
Neels Hofmeyre6d8e912018-08-23 16:36:48 +02001405 /* Add X-Osmo-IGN */
1406 if ((mgcp_msg->presence & MGCP_MSG_PRESENCE_X_OSMO_IGN)
1407 && (mgcp_msg->x_osmo_ign != 0))
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001408 rc |=
Neels Hofmeyre6d8e912018-08-23 16:36:48 +02001409 msgb_printf(msg, MGCP_X_OSMO_IGN_HEADER "%s\r\n",
1410 mgcp_msg->x_osmo_ign & MGCP_X_OSMO_IGN_CALLID ? " C": "");
1411
Pau Espin Pedrol900cd652019-04-24 22:06:22 +02001412 /* Add X-Osmo-Osmux */
1413 if ((mgcp_msg->presence & MGCP_MSG_PRESENCE_X_OSMO_OSMUX_CID)) {
Pau Espin Pedrol1b1d7ed2019-05-23 16:48:24 +02001414 if (mgcp_msg->x_osmo_osmux_cid < -1 || mgcp_msg->x_osmo_osmux_cid > OSMUX_CID_MAX) {
Philipp Maierdf9192e2021-09-03 17:50:51 +02001415 LOGPMGW(mgcp, LOGL_ERROR, "Wrong Osmux CID %d, can not generate MGCP message\n",
1416 mgcp_msg->x_osmo_osmux_cid);
Pau Espin Pedrol1b1d7ed2019-05-23 16:48:24 +02001417 msgb_free(msg);
1418 return NULL;
1419 }
Pau Espin Pedrol900cd652019-04-24 22:06:22 +02001420 snprintf(buf, sizeof(buf), " %d", mgcp_msg->x_osmo_osmux_cid);
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001421 rc |=
Pau Espin Pedrol900cd652019-04-24 22:06:22 +02001422 msgb_printf(msg, MGCP_X_OSMO_OSMUX_HEADER "%s\r\n",
1423 mgcp_msg->x_osmo_osmux_cid == -1 ? " *": buf);
1424 }
1425
1426
Philipp Maier704c4f02018-06-07 18:51:31 +02001427 /* Add session description protocol (SDP) */
1428 if (use_sdp
1429 && (mgcp_msg->verb == MGCP_VERB_CRCX
1430 || mgcp_msg->verb == MGCP_VERB_MDCX)) {
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001431 if (add_sdp(msg, mgcp_msg, mgcp) < 0)
Philipp Maier9d25d7a2018-01-22 17:31:10 +01001432 return NULL;
Philipp Maier1dc6be62017-10-05 18:25:37 +02001433 }
1434
1435 if (rc != 0) {
Philipp Maierdf9192e2021-09-03 17:50:51 +02001436 LOGPMGW(mgcp, LOGL_ERROR, "Message buffer to small, can not generate MGCP message\n");
Philipp Maier1dc6be62017-10-05 18:25:37 +02001437 msgb_free(msg);
1438 msg = NULL;
1439 }
1440
1441 return msg;
1442}
1443
Philipp Maier275ac972018-01-20 00:58:16 +01001444/*! Retrieve the MGCP transaction ID from a msgb generated by mgcp_msg_gen()
1445 * \param[in] msg message buffer
1446 * \returns Transaction id. */
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +01001447mgcp_trans_id_t mgcp_msg_trans_id(struct msgb *msg)
1448{
1449 return (mgcp_trans_id_t)msg->cb[MSGB_CB_MGCP_TRANS_ID];
1450}
1451
Philipp Maierfa495d62021-09-02 10:08:56 +02001452/*! Get the configuration parameters for a given MGCP client instance
Philipp Maier275ac972018-01-20 00:58:16 +01001453 * \param[in] mgcp MGCP client descriptor.
1454 * \returns configuration */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +02001455struct mgcp_client_conf *mgcp_client_conf_actual(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001456{
1457 return &mgcp->actual;
1458}
Neels Hofmeyrd95ab1e2017-09-22 00:52:54 +02001459
1460const struct value_string mgcp_client_connection_mode_strs[] = {
1461 { MGCP_CONN_NONE, "none" },
1462 { MGCP_CONN_RECV_SEND, "sendrecv" },
1463 { MGCP_CONN_SEND_ONLY, "sendonly" },
1464 { MGCP_CONN_RECV_ONLY, "recvonly" },
1465 { MGCP_CONN_LOOPBACK, "loopback" },
1466 { 0, NULL }
1467};
Philipp Maierdf9192e2021-09-03 17:50:51 +02001468
1469/*! Get MGCP client instance name (VTY).
1470 * \param[in] mgcp MGCP client descriptor.
1471 * \returns MGCP client name.
1472 *
1473 * The user can only modify the name of an MGCP client instance when it is
1474 * part of a pool. For single MGCP client instances and MGCP client instance
1475 * where no description is set via the VTY, the MGW domain name will be used
1476 * as name. */
1477const char *mgcp_client_name(const struct mgcp_client *mgcp)
1478{
1479 if (!mgcp)
1480 return "(null)";
1481
1482 if (mgcp->actual.description)
1483 return mgcp->actual.description;
1484 else
1485 return mgcp_client_endpoint_domain(mgcp);
1486}