blob: 2ca3b66511974e8d34682445794507e1bfc004ad [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" },
Philipp Maier704c4f02018-06-07 18:51:31 +020063 { 0, NULL },
64};
65
66/* Get encoding name from a full codec string e,g.
67 * ("CODEC/8000/2" => returns "CODEC") */
68static char *extract_codec_name(const char *str)
69{
70 static char buf[64];
71 unsigned int i;
72
73 if (!str)
74 return NULL;
75
Philipp Maier704c4f02018-06-07 18:51:31 +020076 osmo_strlcpy(buf, str, sizeof(buf));
77
78 for (i = 0; i < strlen(buf); i++) {
79 if (buf[i] == '/')
80 buf[i] = '\0';
81 }
82
83 return buf;
84}
85
86/*! Map a string to a codec.
87 * \ptmap[in] str input string (e.g "GSM/8000/1", "GSM/8000" or "GSM")
88 * \returns codec that corresponds to the given string representation. */
89enum mgcp_codecs map_str_to_codec(const char *str)
90{
91 unsigned int i;
92 char *codec_name;
93 char str_buf[64];
94
95 osmo_strlcpy(str_buf, extract_codec_name(str), sizeof(str_buf));
96
Neels Hofmeyr47642f22019-02-27 05:56:53 +010097 for (i = 0; i < ARRAY_SIZE(osmo_mgcpc_codec_names); i++) {
98 codec_name = extract_codec_name(osmo_mgcpc_codec_names[i].str);
Philipp Maier704c4f02018-06-07 18:51:31 +020099 if (!codec_name)
100 continue;
101 if (strcmp(codec_name, str_buf) == 0)
Neels Hofmeyr47642f22019-02-27 05:56:53 +0100102 return osmo_mgcpc_codec_names[i].value;
Philipp Maier704c4f02018-06-07 18:51:31 +0200103 }
104
105 return -1;
106}
107
108/* Check the ptmap for illegal mappings */
Neels Hofmeyr84274e42019-04-27 19:08:36 +0200109static int check_ptmap(const struct ptmap *ptmap)
Philipp Maier704c4f02018-06-07 18:51:31 +0200110{
111 /* Check if there are mappings that leave the IANA assigned dynamic
112 * payload type range. Under normal conditions such mappings should
113 * not occur */
114
115 /* Its ok to have a 1:1 mapping in the statically defined
116 * range, this won't hurt */
117 if (ptmap->codec == ptmap->pt)
118 return 0;
119
120 if (ptmap->codec < 96 || ptmap->codec > 127)
121 goto error;
122 if (ptmap->pt < 96 || ptmap->pt > 127)
123 goto error;
124
125 return 0;
126error:
127 LOGP(DLMGCP, LOGL_ERROR,
128 "ptmap contains illegal mapping: codec=%u maps to pt=%u\n",
129 ptmap->codec, ptmap->pt);
130 return -1;
131}
132
133/*! Map a codec to a payload type.
134 * \ptmap[in] payload pointer to payload type map with specified payload types.
135 * \ptmap[in] ptmap_len length of the payload type map.
136 * \ptmap[in] codec the codec for which the payload type should be looked up.
137 * \returns assigned payload type */
Neels Hofmeyr84274e42019-04-27 19:08:36 +0200138unsigned int map_codec_to_pt(const struct ptmap *ptmap, unsigned int ptmap_len,
Philipp Maier704c4f02018-06-07 18:51:31 +0200139 enum mgcp_codecs codec)
140{
141 unsigned int i;
142
143 /*! Note: If the payload type map is empty or the codec is not found
144 * in the map, then a 1:1 mapping is performed. If the codec falls
145 * into the statically defined range or if the mapping table isself
146 * tries to map to the statically defined range, then the mapping
147 * is also ignored and a 1:1 mapping is performed instead. */
148
149 /* we may return the codec directly since enum mgcp_codecs directly
150 * corresponds to the statićally assigned payload types */
151 if (codec < 96 || codec > 127)
152 return codec;
153
154 for (i = 0; i < ptmap_len; i++) {
155 /* Skip illegal map entries */
156 if (check_ptmap(ptmap) == 0 && ptmap->codec == codec)
157 return ptmap->pt;
158 ptmap++;
159 }
160
161 /* If nothing is found, do not perform any mapping */
162 return codec;
163}
164
165/*! Map a payload type to a codec.
166 * \ptmap[in] payload pointer to payload type map with specified payload types.
167 * \ptmap[in] ptmap_len length of the payload type map.
168 * \ptmap[in] payload type for which the codec should be looked up.
169 * \returns codec that corresponds to the specified payload type */
170enum mgcp_codecs map_pt_to_codec(struct ptmap *ptmap, unsigned int ptmap_len,
171 unsigned int pt)
172{
173 unsigned int i;
174
175 /*! Note: If the payload type map is empty or the payload type is not
176 * found in the map, then a 1:1 mapping is performed. If the payload
177 * type falls into the statically defined range or if the mapping
178 * table isself tries to map to the statically defined range, then
179 * the mapping is also ignored and a 1:1 mapping is performed
180 * instead. */
181
182 /* See also note in map_codec_to_pt() */
183 if (pt < 96 || pt > 127)
184 return pt;
185
186 for (i = 0; i < ptmap_len; i++) {
187 if (check_ptmap(ptmap) == 0 && ptmap->pt == pt)
188 return ptmap->codec;
189 ptmap++;
190 }
191
192 /* If nothing is found, do not perform any mapping */
193 return pt;
194}
195
Philipp Maierc824fe42021-08-03 15:00:02 +0200196/*! Initialize MGCP client configuration struct with default values.
Philipp Maier275ac972018-01-20 00:58:16 +0100197 * \param[out] conf Client configuration.*/
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200198void mgcp_client_conf_init(struct mgcp_client_conf *conf)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200199{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200200 /* NULL and -1 default to MGCP_CLIENT_*_DEFAULT values */
201 *conf = (struct mgcp_client_conf){
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200202 .local_addr = NULL,
203 .local_port = -1,
204 .remote_addr = NULL,
205 .remote_port = -1,
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200206 };
Philipp Maier3f2c15f2021-07-22 11:53:07 +0200207
208 INIT_LLIST_HEAD(&conf->reset_epnames);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200209}
210
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200211static void mgcp_client_handle_response(struct mgcp_client *mgcp,
212 struct mgcp_response_pending *pending,
213 struct mgcp_response *response)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200214{
215 if (!pending) {
Philipp Maierdf9192e2021-09-03 17:50:51 +0200216 LOGPMGW(mgcp, LOGL_ERROR, "Cannot handle NULL response\n");
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200217 return;
218 }
219 if (pending->response_cb)
220 pending->response_cb(response, pending->priv);
221 else
Philipp Maierdf9192e2021-09-03 17:50:51 +0200222 LOGPMGW(mgcp, LOGL_DEBUG, "MGCP response ignored (NULL cb)\n");
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200223 talloc_free(pending);
224}
225
226static int mgcp_response_parse_head(struct mgcp_response *r, struct msgb *msg)
227{
228 int comment_pos;
229 char *end;
230
231 if (mgcp_msg_terminate_nul(msg))
232 goto response_parse_failure;
233
234 r->body = (char *)msg->data;
235
Harald Welte9bf7c532017-11-17 14:14:31 +0100236 if (sscanf(r->body, "%3d %u %n",
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200237 &r->head.response_code, &r->head.trans_id,
238 &comment_pos) != 2)
239 goto response_parse_failure;
240
Philipp Maierabe8c892018-01-20 00:15:12 +0100241 osmo_strlcpy(r->head.comment, r->body + comment_pos, sizeof(r->head.comment));
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200242 end = strchr(r->head.comment, '\r');
243 if (!end)
244 goto response_parse_failure;
245 /* Mark the end of the comment */
246 *end = '\0';
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200247 return 0;
248
249response_parse_failure:
250 LOGP(DLMGCP, LOGL_ERROR,
251 "Failed to parse MGCP response header\n");
252 return -EINVAL;
253}
254
255/* TODO undup against mgcp_protocol.c:mgcp_check_param() */
256static bool mgcp_line_is_valid(const char *line)
257{
258 const size_t line_len = strlen(line);
259 if (line[0] == '\0')
260 return true;
261
262 if (line_len < 2
263 || line[1] != '=') {
264 LOGP(DLMGCP, LOGL_ERROR,
265 "Wrong MGCP option format: '%s'\n",
266 line);
267 return false;
268 }
269
270 return true;
271}
272
Philipp Maier704c4f02018-06-07 18:51:31 +0200273/* Parse a line like "m=audio 16002 RTP/AVP 98", extract port and payload types */
274static int mgcp_parse_audio_port_pt(struct mgcp_response *r, char *line)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200275{
Philipp Maier704c4f02018-06-07 18:51:31 +0200276 char *pt_str;
Pau Espin Pedrolc5c14302019-07-23 16:19:41 +0200277 char *pt_end;
Pau Espin Pedrola2b1c5e2019-07-26 14:13:14 +0200278 unsigned long int pt;
Philipp Maier704c4f02018-06-07 18:51:31 +0200279 unsigned int count = 0;
280 unsigned int i;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200281
Philipp Maier704c4f02018-06-07 18:51:31 +0200282 /* Extract port information */
283 if (sscanf(line, "m=audio %hu", &r->audio_port) != 1)
284 goto response_parse_failure_port;
Philipp Maier10f32db2017-12-13 12:34:34 +0100285 if (r->audio_port == 0)
Philipp Maier704c4f02018-06-07 18:51:31 +0200286 goto response_parse_failure_port;
Philipp Maier10f32db2017-12-13 12:34:34 +0100287
Philipp Maier704c4f02018-06-07 18:51:31 +0200288 /* Extract payload types */
289 line = strstr(line, "RTP/AVP ");
290 if (!line)
291 goto exit;
292
293 pt_str = strtok(line, " ");
294 while (1) {
295 /* Do not allow excessive payload types */
296 if (count > ARRAY_SIZE(r->codecs))
297 goto response_parse_failure_pt;
298
299 pt_str = strtok(NULL, " ");
300 if (!pt_str)
301 break;
Pau Espin Pedrolc5c14302019-07-23 16:19:41 +0200302 errno = 0;
303 pt = strtoul(pt_str, &pt_end, 0);
304 if ((errno == ERANGE && pt == ULONG_MAX) || (errno && !pt) ||
305 pt_str == pt_end)
306 goto response_parse_failure_pt;
Philipp Maier704c4f02018-06-07 18:51:31 +0200307
Pau Espin Pedrola2b1c5e2019-07-26 14:13:14 +0200308 if (pt >> 7) /* PT is 7 bit field, higher values not allowed */
309 goto response_parse_failure_pt;
310
Philipp Maier704c4f02018-06-07 18:51:31 +0200311 /* Do not allow duplicate payload types */
312 for (i = 0; i < count; i++)
313 if (r->codecs[i] == pt)
314 goto response_parse_failure_pt;
315
316 /* Note: The payload type we store may not necessarly match
317 * the codec types we have defined in enum mgcp_codecs. To
318 * ensure that the end result only contains codec types which
319 * match enum mgcp_codecs, we will go through afterwards and
320 * remap the affected entries with the inrofmation we learn
321 * from rtpmap */
322 r->codecs[count] = pt;
323 count++;
324 }
325
326 r->codecs_len = count;
327
328exit:
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200329 return 0;
330
Philipp Maier704c4f02018-06-07 18:51:31 +0200331response_parse_failure_port:
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200332 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier704c4f02018-06-07 18:51:31 +0200333 "Failed to parse SDP parameter port (%s)\n", line);
Philipp Maier06da85e2017-10-05 18:49:24 +0200334 return -EINVAL;
Philipp Maier704c4f02018-06-07 18:51:31 +0200335
336response_parse_failure_pt:
337 LOGP(DLMGCP, LOGL_ERROR,
338 "Failed to parse SDP parameter payload types (%s)\n", line);
339 return -EINVAL;
340}
341
342/* Parse a line like "m=audio 16002 RTP/AVP 98", extract port and payload types */
343static int mgcp_parse_audio_ptime_rtpmap(struct mgcp_response *r, const char *line)
344{
345 unsigned int pt;
346 char codec_resp[64];
Neels Hofmeyr3ab8ca42019-08-08 04:03:42 +0200347 enum mgcp_codecs codec;
Pau Espin Pedrolc12bfb72019-04-16 17:23:09 +0200348
Neels Hofmeyr23f40482019-08-08 04:03:42 +0200349#define A_PTIME "a=ptime:"
350#define A_RTPMAP "a=rtpmap:"
Pau Espin Pedrolc12bfb72019-04-16 17:23:09 +0200351
Neels Hofmeyr23f40482019-08-08 04:03:42 +0200352 if (osmo_str_startswith(line, A_PTIME)) {
353 if (sscanf(line, A_PTIME "%u", &r->ptime) != 1) {
354 LOGP(DLMGCP, LOGL_ERROR,
355 "Failed to parse SDP parameter, invalid ptime (%s)\n", line);
356 return -EINVAL;
357 }
358 } else if (osmo_str_startswith(line, A_RTPMAP)) {
359 if (sscanf(line, A_RTPMAP "%d %63s", &pt, codec_resp) != 2) {
360 LOGP(DLMGCP, LOGL_ERROR,
361 "Failed to parse SDP parameter, invalid rtpmap: %s\n", osmo_quote_str(line, -1));
362 return -EINVAL;
363 }
Neels Hofmeyr3ab8ca42019-08-08 04:03:42 +0200364 if (r->ptmap_len >= ARRAY_SIZE(r->ptmap)) {
365 LOGP(DLMGCP, LOGL_ERROR, "No more space in ptmap array (len=%u)\n", r->ptmap_len);
366 return -ENOSPC;
Neels Hofmeyr23f40482019-08-08 04:03:42 +0200367 }
Neels Hofmeyr3ab8ca42019-08-08 04:03:42 +0200368 codec = map_str_to_codec(codec_resp);
369 r->ptmap[r->ptmap_len].pt = pt;
370 r->ptmap[r->ptmap_len].codec = codec;
371 r->ptmap_len++;
Philipp Maier704c4f02018-06-07 18:51:31 +0200372 }
Pau Espin Pedrolc12bfb72019-04-16 17:23:09 +0200373
Philipp Maier704c4f02018-06-07 18:51:31 +0200374 return 0;
Philipp Maier06da85e2017-10-05 18:49:24 +0200375}
376
377/* Parse a line like "c=IN IP4 10.11.12.13" */
378static int mgcp_parse_audio_ip(struct mgcp_response *r, const char *line)
379{
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +0200380 struct in6_addr ip_test;
381 bool is_ipv6;
Philipp Maier06da85e2017-10-05 18:49:24 +0200382
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +0200383 if (strncmp("c=IN IP", line, 7) != 0)
Philipp Maier06da85e2017-10-05 18:49:24 +0200384 goto response_parse_failure;
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +0200385 line += 7;
386 if (*line == '6')
387 is_ipv6 = true;
388 else if (*line == '4')
389 is_ipv6 = false;
390 else
Philipp Maier06da85e2017-10-05 18:49:24 +0200391 goto response_parse_failure;
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +0200392 line++;
393 if (*line != ' ')
Philipp Maier06da85e2017-10-05 18:49:24 +0200394 goto response_parse_failure;
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +0200395 line++;
Philipp Maier06da85e2017-10-05 18:49:24 +0200396
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +0200397 /* Extract and check IP-Address */
398 if (is_ipv6) {
399 /* 45 = INET6_ADDRSTRLEN -1 */
400 if (sscanf(line, "%45s", r->audio_ip) != 1)
401 goto response_parse_failure;
402 if (inet_pton(AF_INET6, r->audio_ip, &ip_test) != 1)
403 goto response_parse_failure;
404 } else {
405 /* 15 = INET_ADDRSTRLEN -1 */
406 if (sscanf(line, "%15s", r->audio_ip) != 1)
407 goto response_parse_failure;
408 if (inet_pton(AF_INET, r->audio_ip, &ip_test) != 1)
409 goto response_parse_failure;
410 }
Philipp Maier06da85e2017-10-05 18:49:24 +0200411 return 0;
412
413response_parse_failure:
414 LOGP(DLMGCP, LOGL_ERROR,
415 "Failed to parse MGCP response header (audio ip)\n");
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200416 return -EINVAL;
417}
418
Pau Espin Pedrol91088c32019-04-24 21:02:40 +0200419/*! Extract OSMUX CID from an MGCP parameter line (string).
420 * \param[in] line single parameter line from the MGCP message
421 * \returns OSMUX CID, -1 wildcard, -2 on error
422 * FIXME: This is a copy of function in mgcp_msg.c. Have some common.c file between both libs?
423 */
424static int mgcp_parse_osmux_cid(const char *line)
425{
426 int osmux_cid;
427
428
Pau Espin Pedrolc1bf4692019-05-14 16:23:24 +0200429 if (strcasecmp(line + 2, "Osmux: *") == 0) {
Pau Espin Pedrol91088c32019-04-24 21:02:40 +0200430 LOGP(DLMGCP, LOGL_DEBUG, "Parsed wilcard Osmux CID\n");
431 return -1;
432 }
433
Pau Espin Pedrolc1bf4692019-05-14 16:23:24 +0200434 if (sscanf(line + 2 + 7, "%u", &osmux_cid) != 1) {
Pau Espin Pedrol91088c32019-04-24 21:02:40 +0200435 LOGP(DLMGCP, LOGL_ERROR, "Failed parsing Osmux in MGCP msg line: %s\n",
436 line);
437 return -2;
438 }
439
Pau Espin Pedrol91088c32019-04-24 21:02:40 +0200440 if (osmux_cid > OSMUX_CID_MAX) { /* OSMUX_CID_MAX from libosmo-netif */
441 LOGP(DLMGCP, LOGL_ERROR, "Osmux ID too large: %u > %u\n",
442 osmux_cid, OSMUX_CID_MAX);
443 return -2;
444 }
Pau Espin Pedrolc7c8e642022-10-06 18:24:21 +0200445 LOGP(DLMGCP, LOGL_DEBUG, "MGW offered Osmux CID %u\n", osmux_cid);
Pau Espin Pedrol91088c32019-04-24 21:02:40 +0200446
447 return osmux_cid;
448}
449
Philipp Maier3b12e1b2018-01-18 15:16:13 +0100450/* A new section is marked by a double line break, check a few more
451 * patterns as there may be variants */
452static char *mgcp_find_section_end(char *string)
453{
454 char *rc;
455
456 rc = strstr(string, "\n\n");
457 if (rc)
458 return rc;
459
460 rc = strstr(string, "\n\r\n\r");
461 if (rc)
462 return rc;
463
464 rc = strstr(string, "\r\n\r\n");
465 if (rc)
466 return rc;
467
468 return NULL;
469}
470
Philipp Maier275ac972018-01-20 00:58:16 +0100471/*! Parse body (SDP) parameters of the MGCP response
472 * \param[in,out] r Response data
473 * \returns 0 on success, -EINVAL on error. */
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200474int mgcp_response_parse_params(struct mgcp_response *r)
475{
476 char *line;
477 int rc;
Neels Hofmeyr0793d2f2018-02-21 14:55:34 +0100478 char *data;
Philipp Maiere9d645b2018-01-19 23:54:08 +0100479 char *data_ptr;
Philipp Maier704c4f02018-06-07 18:51:31 +0200480 int i;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200481
Philipp Maiere9d645b2018-01-19 23:54:08 +0100482 /* Since this functions performs a destructive parsing, we create a
483 * local copy of the body data */
Neels Hofmeyr0793d2f2018-02-21 14:55:34 +0100484 OSMO_ASSERT(r->body);
485 data = talloc_strdup(r, r->body);
Philipp Maiere9d645b2018-01-19 23:54:08 +0100486 OSMO_ASSERT(data);
Philipp Maier55295f72018-01-15 14:00:28 +0100487
Philipp Maiere9d645b2018-01-19 23:54:08 +0100488 /* Find beginning of the parameter (SDP) section */
489 data_ptr = mgcp_find_section_end(data);
Neels Hofmeyr10835332018-02-21 14:55:34 +0100490 if (!data_ptr) {
Neels Hofmeyre8278312019-09-19 03:06:46 +0200491 LOGP(DLMGCP, LOGL_DEBUG, "MGCP response contains no SDP parameters\n");
492 rc = 0;
Philipp Maiere9d645b2018-01-19 23:54:08 +0100493 goto exit;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200494 }
495
Neels Hofmeyr0793d2f2018-02-21 14:55:34 +0100496 /* data_ptr now points to the beginning of the section-end-marker; for_each_non_empty_line()
497 * skips any \r and \n characters for free, so we don't need to skip the marker. */
498
Philipp Maiere9d645b2018-01-19 23:54:08 +0100499 for_each_non_empty_line(line, data_ptr) {
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200500 if (!mgcp_line_is_valid(line))
501 return -EINVAL;
502
503 switch (line[0]) {
Philipp Maier704c4f02018-06-07 18:51:31 +0200504 case 'a':
505 rc = mgcp_parse_audio_ptime_rtpmap(r, line);
506 if (rc)
507 goto exit;
508 break;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200509 case 'm':
Philipp Maier704c4f02018-06-07 18:51:31 +0200510 rc = mgcp_parse_audio_port_pt(r, line);
Philipp Maier06da85e2017-10-05 18:49:24 +0200511 if (rc)
Philipp Maiere9d645b2018-01-19 23:54:08 +0100512 goto exit;
Philipp Maier06da85e2017-10-05 18:49:24 +0200513 break;
514 case 'c':
515 rc = mgcp_parse_audio_ip(r, line);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200516 if (rc)
Philipp Maiere9d645b2018-01-19 23:54:08 +0100517 goto exit;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200518 break;
519 default:
520 /* skip unhandled parameters */
521 break;
522 }
523 }
Philipp Maiere9d645b2018-01-19 23:54:08 +0100524
Philipp Maier704c4f02018-06-07 18:51:31 +0200525 /* See also note in mgcp_parse_audio_port_pt() */
526 for (i = 0; i < r->codecs_len; i++)
527 r->codecs[i] = map_pt_to_codec(r->ptmap, r->ptmap_len, r->codecs[i]);
528
Philipp Maiere9d645b2018-01-19 23:54:08 +0100529 rc = 0;
530exit:
531 talloc_free(data);
532 return rc;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200533}
534
Philipp Maier55295f72018-01-15 14:00:28 +0100535/* Parse a line like "X: something" */
536static int mgcp_parse_head_param(char *result, unsigned int result_len,
537 char label, const char *line)
Philipp Maierffd75e42017-11-22 11:44:50 +0100538{
Philipp Maier55295f72018-01-15 14:00:28 +0100539 char label_string[4];
Neels Hofmeyr23e7bf12018-09-03 21:26:22 +0200540 size_t rc;
Philipp Maier55295f72018-01-15 14:00:28 +0100541
542 /* Detect empty parameters */
Philipp Maierffd75e42017-11-22 11:44:50 +0100543 if (strlen(line) < 4)
544 goto response_parse_failure;
545
Philipp Maier55295f72018-01-15 14:00:28 +0100546 /* Check if the label matches */
547 snprintf(label_string, sizeof(label_string), "%c: ", label);
548 if (memcmp(label_string, line, 3) != 0)
Philipp Maierffd75e42017-11-22 11:44:50 +0100549 goto response_parse_failure;
550
Philipp Maier55295f72018-01-15 14:00:28 +0100551 /* Copy payload part of the string to destinations (the label string
552 * is always 3 chars long) */
Neels Hofmeyr23e7bf12018-09-03 21:26:22 +0200553 rc = osmo_strlcpy(result, line + 3, result_len);
554 if (rc >= result_len) {
555 LOGP(DLMGCP, LOGL_ERROR,
556 "Failed to parse MGCP response (parameter label: %c):"
557 " the received conn ID is too long: %zu, maximum is %u characters\n",
558 label, rc, result_len - 1);
559 return -ENOSPC;
560 }
Philipp Maierffd75e42017-11-22 11:44:50 +0100561 return 0;
562
563response_parse_failure:
564 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier55295f72018-01-15 14:00:28 +0100565 "Failed to parse MGCP response (parameter label: %c)\n", label);
Philipp Maierffd75e42017-11-22 11:44:50 +0100566 return -EINVAL;
567}
568
569/* Parse MGCP parameters of the response */
570static int parse_head_params(struct mgcp_response *r)
571{
572 char *line;
573 int rc = 0;
574 OSMO_ASSERT(r->body);
Philipp Maier55295f72018-01-15 14:00:28 +0100575 char *data;
576 char *data_ptr;
577 char *data_end;
Philipp Maierffd75e42017-11-22 11:44:50 +0100578
Philipp Maier55295f72018-01-15 14:00:28 +0100579 /* Since this functions performs a destructive parsing, we create a
580 * local copy of the body data */
Philipp Maier1fc69992018-01-31 15:32:55 +0100581 data = talloc_zero_size(r, strlen(r->body)+1);
Philipp Maier55295f72018-01-15 14:00:28 +0100582 OSMO_ASSERT(data);
583 data_ptr = data;
584 osmo_strlcpy(data, r->body, strlen(r->body));
585
586 /* If there is an SDP body attached, prevent for_each_non_empty_line()
587 * into running in there, we are not yet interested in the parameters
588 * stored there. */
Pau Espin Pedrolc12bfb72019-04-16 17:23:09 +0200589 data_end = mgcp_find_section_end(data);
Philipp Maierffd75e42017-11-22 11:44:50 +0100590 if (data_end)
591 *data_end = '\0';
592
Philipp Maier55295f72018-01-15 14:00:28 +0100593 for_each_non_empty_line(line, data_ptr) {
Pau Espin Pedrol166077e2019-06-26 12:21:38 +0200594 switch (toupper(line[0])) {
Philipp Maier55295f72018-01-15 14:00:28 +0100595 case 'Z':
596 rc = mgcp_parse_head_param(r->head.endpoint,
597 sizeof(r->head.endpoint),
598 'Z', line);
599 if (rc)
600 goto exit;
Philipp Maier771b26a2018-01-31 13:53:11 +0100601
602 /* A specific endpoint identifier returned by the MGW
603 * must not contain any wildcard characters */
604 if (strstr(r->head.endpoint, "*") != NULL) {
605 rc = -EINVAL;
606 goto exit;
607 }
Philipp Maier3261dc72018-01-31 14:03:13 +0100608
609 /* A specific endpoint identifier returned by the MGW
610 * must contain an @ character */
611 if (strstr(r->head.endpoint, "@") == NULL) {
612 rc = -EINVAL;
613 goto exit;
614 }
Philipp Maier55295f72018-01-15 14:00:28 +0100615 break;
Philipp Maierffd75e42017-11-22 11:44:50 +0100616 case 'I':
Philipp Maier55295f72018-01-15 14:00:28 +0100617 rc = mgcp_parse_head_param(r->head.conn_id,
618 sizeof(r->head.conn_id),
619 'I', line);
Philipp Maierffd75e42017-11-22 11:44:50 +0100620 if (rc)
621 goto exit;
622 break;
Pau Espin Pedrol91088c32019-04-24 21:02:40 +0200623 case 'X':
Pau Espin Pedrolc1bf4692019-05-14 16:23:24 +0200624 if (strncasecmp("Osmux: ", line + 2, strlen("Osmux: ")) == 0) {
Pau Espin Pedrol91088c32019-04-24 21:02:40 +0200625 rc = mgcp_parse_osmux_cid(line);
626 if (rc < 0) {
627 /* -1: we don't want wildcards in response. -2: error */
628 rc = -EINVAL;
629 goto exit;
630 }
631 r->head.x_osmo_osmux_use = true;
632 r->head.x_osmo_osmux_cid = (uint8_t) rc;
633 rc = 0;
634 break;
635 }
636 /* Ignore unknown X-headers */
637 break;
Philipp Maierffd75e42017-11-22 11:44:50 +0100638 default:
639 /* skip unhandled parameters */
640 break;
641 }
642 }
643exit:
Philipp Maier55295f72018-01-15 14:00:28 +0100644 talloc_free(data);
Philipp Maierffd75e42017-11-22 11:44:50 +0100645 return rc;
646}
647
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200648static struct mgcp_response_pending *mgcp_client_response_pending_get(
649 struct mgcp_client *mgcp,
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100650 mgcp_trans_id_t trans_id)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200651{
652 struct mgcp_response_pending *pending;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200653 llist_for_each_entry(pending, &mgcp->responses_pending, entry) {
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100654 if (pending->trans_id == trans_id) {
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200655 llist_del(&pending->entry);
656 return pending;
657 }
658 }
659 return NULL;
660}
661
662/* Feed an MGCP message into the receive processing.
663 * Parse the head and call any callback registered for the transaction id found
664 * in the MGCP message. This is normally called directly from the internal
665 * mgcp_do_read that reads from the socket connected to the MGCP gateway. This
666 * function is published mainly to be able to feed data from the test suite.
667 */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200668int mgcp_client_rx(struct mgcp_client *mgcp, struct msgb *msg)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200669{
Philipp Maier1fc69992018-01-31 15:32:55 +0100670 struct mgcp_response *r;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200671 struct mgcp_response_pending *pending;
672 int rc;
673
Philipp Maier1fc69992018-01-31 15:32:55 +0100674 r = talloc_zero(mgcp, struct mgcp_response);
675 OSMO_ASSERT(r);
676
677 rc = mgcp_response_parse_head(r, msg);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200678 if (rc) {
Philipp Maierdf9192e2021-09-03 17:50:51 +0200679 LOGPMGW(mgcp, LOGL_ERROR, "Cannot parse MGCP response (head)\n");
Philipp Maier1fc69992018-01-31 15:32:55 +0100680 rc = 1;
681 goto error;
Philipp Maierffd75e42017-11-22 11:44:50 +0100682 }
683
Philipp Maierdf9192e2021-09-03 17:50:51 +0200684 LOGPMGW(mgcp, LOGL_DEBUG, "MGCP client: Rx %d %u %s\n",
Neels Hofmeyrd5731072021-07-15 01:36:47 +0200685 r->head.response_code, r->head.trans_id, r->head.comment);
686
Philipp Maier1fc69992018-01-31 15:32:55 +0100687 rc = parse_head_params(r);
Philipp Maierffd75e42017-11-22 11:44:50 +0100688 if (rc) {
Philipp Maierdf9192e2021-09-03 17:50:51 +0200689 LOGPMGW(mgcp, LOGL_ERROR, "Cannot parse MGCP response (head parameters)\n");
Philipp Maier1fc69992018-01-31 15:32:55 +0100690 rc = 1;
691 goto error;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200692 }
693
Philipp Maier1fc69992018-01-31 15:32:55 +0100694 pending = mgcp_client_response_pending_get(mgcp, r->head.trans_id);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200695 if (!pending) {
Philipp Maierdf9192e2021-09-03 17:50:51 +0200696 LOGPMGW(mgcp, LOGL_ERROR, "Cannot find matching MGCP transaction for trans_id %d\n",
697 r->head.trans_id);
Philipp Maier1fc69992018-01-31 15:32:55 +0100698 rc = -ENOENT;
699 goto error;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200700 }
701
Philipp Maier1fc69992018-01-31 15:32:55 +0100702 mgcp_client_handle_response(mgcp, pending, r);
703 rc = 0;
704
705error:
706 talloc_free(r);
707 return rc;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200708}
709
710static int mgcp_do_read(struct osmo_fd *fd)
711{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200712 struct mgcp_client *mgcp = fd->data;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200713 struct msgb *msg;
714 int ret;
715
716 msg = msgb_alloc_headroom(4096, 128, "mgcp_from_gw");
717 if (!msg) {
Philipp Maierdf9192e2021-09-03 17:50:51 +0200718 LOGPMGW(mgcp, LOGL_ERROR, "Failed to allocate MGCP message.\n");
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200719 return -1;
720 }
721
722 ret = read(fd->fd, msg->data, 4096 - 128);
723 if (ret <= 0) {
Philipp Maierdf9192e2021-09-03 17:50:51 +0200724 LOGPMGW(mgcp, LOGL_ERROR, "Failed to read: %s: %d='%s'\n",
725 osmo_sock_get_name2(fd->fd), errno, strerror(errno));
Neels Hofmeyr0a403792018-12-12 03:10:18 +0100726
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200727 msgb_free(msg);
728 return -1;
Harald Welte9bf7c532017-11-17 14:14:31 +0100729 }
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200730
731 msg->l2h = msgb_put(msg, ret);
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200732 ret = mgcp_client_rx(mgcp, msg);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200733 talloc_free(msg);
734 return ret;
735}
736
737static int mgcp_do_write(struct osmo_fd *fd, struct msgb *msg)
738{
739 int ret;
Philipp Maierdf9192e2021-09-03 17:50:51 +0200740 struct mgcp_client *mgcp = fd->data;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200741
Philipp Maierdf9192e2021-09-03 17:50:51 +0200742 LOGPMGW(mgcp, LOGL_DEBUG, "Tx MGCP: %s: len=%u '%s'...\n",
743 osmo_sock_get_name2(fd->fd), msg->len,
744 osmo_escape_str((const char *)msg->data, OSMO_MIN(42, msg->len)));
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200745
746 ret = write(fd->fd, msg->data, msg->len);
747 if (ret != msg->len)
Philipp Maierdf9192e2021-09-03 17:50:51 +0200748 LOGPMGW(mgcp, LOGL_ERROR, "Failed to Tx MGCP: %s: %d='%s'; msg: len=%u '%s'...\n",
749 osmo_sock_get_name2(fd->fd), errno, strerror(errno),
750 msg->len, osmo_escape_str((const char *)msg->data, OSMO_MIN(42, msg->len)));
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200751 return ret;
752}
753
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200754struct mgcp_client *mgcp_client_init(void *ctx,
755 struct mgcp_client_conf *conf)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200756{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200757 struct mgcp_client *mgcp;
Philipp Maier3f2c15f2021-07-22 11:53:07 +0200758 struct reset_ep *reset_ep;
759 struct reset_ep *actual_reset_ep;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200760
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200761 mgcp = talloc_zero(ctx, struct mgcp_client);
Harald Welteefe15712020-07-15 10:56:45 +0200762 if (!mgcp)
763 return NULL;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200764
765 INIT_LLIST_HEAD(&mgcp->responses_pending);
766 INIT_LLIST_HEAD(&mgcp->inuse_endpoints);
767
768 mgcp->next_trans_id = 1;
769
770 mgcp->actual.local_addr = conf->local_addr ? conf->local_addr :
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200771 MGCP_CLIENT_LOCAL_ADDR_DEFAULT;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200772 mgcp->actual.local_port = conf->local_port >= 0 ? (uint16_t)conf->local_port :
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200773 MGCP_CLIENT_LOCAL_PORT_DEFAULT;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200774
775 mgcp->actual.remote_addr = conf->remote_addr ? conf->remote_addr :
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200776 MGCP_CLIENT_REMOTE_ADDR_DEFAULT;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200777 mgcp->actual.remote_port = conf->remote_port >= 0 ? (uint16_t)conf->remote_port :
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200778 MGCP_CLIENT_REMOTE_PORT_DEFAULT;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200779
Neels Hofmeyrac69ea92018-12-19 00:27:50 +0100780 if (osmo_strlcpy(mgcp->actual.endpoint_domain_name, conf->endpoint_domain_name,
781 sizeof(mgcp->actual.endpoint_domain_name))
782 >= sizeof(mgcp->actual.endpoint_domain_name)) {
Philipp Maierdf9192e2021-09-03 17:50:51 +0200783 LOGPMGW(mgcp, LOGL_ERROR, "MGCP client: endpoint domain name is too long, max length is %zu: '%s'\n",
784 sizeof(mgcp->actual.endpoint_domain_name) - 1, conf->endpoint_domain_name);
Neels Hofmeyrac69ea92018-12-19 00:27:50 +0100785 talloc_free(mgcp);
786 return NULL;
787 }
Philipp Maierdf9192e2021-09-03 17:50:51 +0200788 LOGPMGW(mgcp, LOGL_NOTICE, "MGCP client: using endpoint domain '@%s'\n", mgcp_client_endpoint_domain(mgcp));
Neels Hofmeyrac69ea92018-12-19 00:27:50 +0100789
Philipp Maier3f2c15f2021-07-22 11:53:07 +0200790 INIT_LLIST_HEAD(&mgcp->actual.reset_epnames);
791 llist_for_each_entry(reset_ep, &conf->reset_epnames, list) {
792 actual_reset_ep = talloc_memdup(mgcp, reset_ep, sizeof(*reset_ep));
793 llist_add_tail(&actual_reset_ep->list, &mgcp->actual.reset_epnames);
794 }
795
Philipp Maierdf9192e2021-09-03 17:50:51 +0200796 if (conf->description)
797 mgcp->actual.description = talloc_strdup(mgcp, conf->description);
798
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200799 return mgcp;
800}
801
Philipp Maier3d2b76f2021-08-04 14:08:37 +0200802static int init_socket(struct mgcp_client *mgcp, unsigned int retry_n_ports)
Philipp Maier6a26c162018-08-01 16:37:06 +0200803{
804 int rc;
805 struct osmo_wqueue *wq;
Philipp Maier3d2b76f2021-08-04 14:08:37 +0200806 unsigned int i;
Philipp Maier6a26c162018-08-01 16:37:06 +0200807
808 wq = &mgcp->wq;
809
Philipp Maier3d2b76f2021-08-04 14:08:37 +0200810 for (i = 0; i < retry_n_ports + 1; i++) {
Philipp Maier6a26c162018-08-01 16:37:06 +0200811
Philipp Maier3d2b76f2021-08-04 14:08:37 +0200812 /* Initialize socket with the currently configured port number */
Pau Espin Pedrol531470a2020-08-31 17:06:55 +0200813 rc = osmo_sock_init2_ofd(&wq->bfd, AF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, mgcp->actual.local_addr,
Philipp Maier6a26c162018-08-01 16:37:06 +0200814 mgcp->actual.local_port, mgcp->actual.remote_addr, mgcp->actual.remote_port,
815 OSMO_SOCK_F_BIND | OSMO_SOCK_F_CONNECT);
816 if (rc > 0)
817 return rc;
818
Philipp Maier3d2b76f2021-08-04 14:08:37 +0200819 /* If there is a different port than the default port configured then we assume that the user has
820 * chosen that port conciously and we will not try to resolve this by silently choosing a different
821 * port. */
Philipp Maier9a7ccc32018-08-09 11:41:19 +0200822 if (mgcp->actual.local_port != MGCP_CLIENT_LOCAL_PORT_DEFAULT && i == 0)
Philipp Maier6a26c162018-08-01 16:37:06 +0200823 return -EINVAL;
824
Philipp Maier3d2b76f2021-08-04 14:08:37 +0200825 if (i == retry_n_ports) {
826 /* Last try failed */
Philipp Maierdf9192e2021-09-03 17:50:51 +0200827 LOGPMGW(mgcp, LOGL_NOTICE, "Failed to bind to %s:%d -- check configuration!\n",
Philipp Maier3d2b76f2021-08-04 14:08:37 +0200828 mgcp->actual.local_addr ? mgcp->actual.local_addr : "(any)", mgcp->actual.local_port);
829 if (retry_n_ports == 0)
830 return -EINVAL;
831 } else {
832 /* Choose a new port number to try next */
Philipp Maierdf9192e2021-09-03 17:50:51 +0200833 LOGPMGW(mgcp, LOGL_NOTICE,
834 "Failed to bind to %s:%d, retrying with port %d -- check configuration!\n",
Philipp Maier3d2b76f2021-08-04 14:08:37 +0200835 mgcp->actual.local_addr ? mgcp->actual.local_addr : "(any)", mgcp->actual.local_port,
836 mgcp->actual.local_port + 1);
837 mgcp->actual.local_port++;
838 }
Philipp Maier6a26c162018-08-01 16:37:06 +0200839 }
840
Philipp Maierdf9192e2021-09-03 17:50:51 +0200841 LOGPMGW(mgcp, LOGL_FATAL, "Failed to find a port to bind on %u times -- check configuration!\n", i);
Philipp Maier6a26c162018-08-01 16:37:06 +0200842 return -EINVAL;
843}
844
Philipp Maier3f2c15f2021-07-22 11:53:07 +0200845/* Safely ignore the MGCP response to the DLCX sent via _mgcp_client_send_dlcx() */
846static void _ignore_mgcp_response(struct mgcp_response *response, void *priv) { }
847
848/* Format DLCX message (fire and forget) and send it off to the MGW */
849static void _mgcp_client_send_dlcx(struct mgcp_client *mgcp, const char *epname)
850{
851 struct msgb *msgb_dlcx;
852 struct mgcp_msg mgcp_msg_dlcx = {
853 .verb = MGCP_VERB_DLCX,
854 .presence = MGCP_MSG_PRESENCE_ENDPOINT,
855 };
856 osmo_strlcpy(mgcp_msg_dlcx.endpoint, epname, sizeof(mgcp_msg_dlcx.endpoint));
857 msgb_dlcx = mgcp_msg_gen(mgcp, &mgcp_msg_dlcx);
858 mgcp_client_tx(mgcp, msgb_dlcx, &_ignore_mgcp_response, NULL);
859}
860
861static const char *_mgcp_client_name_append_domain(const struct mgcp_client *mgcp, const char *name)
862{
863 static char endpoint[MGCP_ENDPOINT_MAXLEN];
864 int rc;
865
866 rc = snprintf(endpoint, sizeof(endpoint), "%s@%s", name, mgcp_client_endpoint_domain(mgcp));
867 if (rc > sizeof(endpoint) - 1) {
Philipp Maierdf9192e2021-09-03 17:50:51 +0200868 LOGPMGW(mgcp, LOGL_ERROR, "MGCP endpoint exceeds maximum length of %zu: '%s@%s'\n",
869 sizeof(endpoint) - 1, name, mgcp_client_endpoint_domain(mgcp));
Philipp Maier3f2c15f2021-07-22 11:53:07 +0200870 return NULL;
871 }
872 if (rc < 1) {
Philipp Maierdf9192e2021-09-03 17:50:51 +0200873 LOGPMGW(mgcp, LOGL_ERROR, "Cannot compose MGCP endpoint name\n");
Philipp Maier3f2c15f2021-07-22 11:53:07 +0200874 return NULL;
875 }
876 return endpoint;
877}
878
879/*! Initialize client connection (opens socket)
Philipp Maier275ac972018-01-20 00:58:16 +0100880 * \param[in,out] mgcp MGCP client descriptor.
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200881 * \param[in] retry_n_ports number of consecutive local ports that should be used to retry on failure.
Philipp Maier275ac972018-01-20 00:58:16 +0100882 * \returns 0 on success, -EINVAL on error. */
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200883int mgcp_client_connect2(struct mgcp_client *mgcp, unsigned int retry_n_ports)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200884{
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200885 struct osmo_wqueue *wq;
886 int rc;
Philipp Maier3f2c15f2021-07-22 11:53:07 +0200887 struct reset_ep *reset_ep;
888 const char *epname;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200889
890 if (!mgcp) {
Philipp Maierdf9192e2021-09-03 17:50:51 +0200891 LOGPMGW(mgcp, LOGL_FATAL, "Client not initialized properly\n");
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200892 return -EINVAL;
893 }
894
895 wq = &mgcp->wq;
Harald Weltec2a8f592020-10-19 13:25:41 +0200896 osmo_wqueue_init(wq, 1024);
897 wq->read_cb = mgcp_do_read;
898 wq->write_cb = mgcp_do_write;
899
900 osmo_fd_setup(&wq->bfd, -1, OSMO_FD_READ, osmo_wqueue_bfd_cb, mgcp, 0);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200901
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200902 rc = init_socket(mgcp, retry_n_ports);
Harald Welte8890dfa2017-11-17 15:09:30 +0100903 if (rc < 0) {
Philipp Maierdf9192e2021-09-03 17:50:51 +0200904 LOGPMGW(mgcp, LOGL_FATAL,
905 "Failed to initialize socket %s:%u -> %s:%u for MGW: %s\n",
Philipp Maier276a4142021-07-29 11:55:14 +0200906 mgcp->actual.local_addr ? mgcp->actual.local_addr : "(any)", mgcp->actual.local_port,
907 mgcp->actual.remote_addr ? mgcp->actual.local_addr : "(any)", mgcp->actual.remote_port,
908 strerror(errno));
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200909 goto error_close_fd;
910 }
911
Philipp Maierdf9192e2021-09-03 17:50:51 +0200912 LOGPMGW(mgcp, LOGL_INFO, "MGW connection: %s\n", osmo_sock_get_name2(wq->bfd.fd));
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200913
Philipp Maier3f2c15f2021-07-22 11:53:07 +0200914 /* If configured, send a DLCX message to the endpoints that are configured to
915 * be reset on startup. Usually this is a wildcarded endpoint. */
916 llist_for_each_entry(reset_ep, &mgcp->actual.reset_epnames, list) {
917 epname = _mgcp_client_name_append_domain(mgcp, reset_ep->name);
Philipp Maierdf9192e2021-09-03 17:50:51 +0200918 LOGPMGW(mgcp, LOGL_INFO, "Sending DLCX to: %s\n", epname);
Philipp Maier3f2c15f2021-07-22 11:53:07 +0200919 _mgcp_client_send_dlcx(mgcp, epname);
920 }
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200921 return 0;
922error_close_fd:
923 close(wq->bfd.fd);
924 wq->bfd.fd = -1;
925 return rc;
926}
927
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200928/*! Initialize client connection (opens socket)
929 * \param[in,out] mgcp MGCP client descriptor.
930 * \returns 0 on success, -EINVAL on error. */
931int mgcp_client_connect(struct mgcp_client *mgcp)
932{
933 return mgcp_client_connect2(mgcp, 99);
934}
935
936/*! Terminate client connection
937 * \param[in,out] mgcp MGCP client descriptor.
938 * \returns 0 on success, -EINVAL on error. */
939void mgcp_client_disconnect(struct mgcp_client *mgcp)
940{
941 struct osmo_wqueue *wq;
942
943 if (!mgcp) {
Philipp Maierdf9192e2021-09-03 17:50:51 +0200944 LOGP(DLMGCP, LOGL_FATAL, "MGCP client not initialized properly\n");
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200945 return;
946 }
947
948 wq = &mgcp->wq;
949 osmo_wqueue_clear(wq);
Philipp Maierdf9192e2021-09-03 17:50:51 +0200950 LOGPMGW(mgcp, LOGL_INFO, "MGCP association: %s -- closed!\n", osmo_sock_get_name2(wq->bfd.fd));
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200951 close(wq->bfd.fd);
952 wq->bfd.fd = -1;
953 if (osmo_fd_is_registered(&wq->bfd))
954 osmo_fd_unregister(&wq->bfd);
955}
956
Philipp Maier275ac972018-01-20 00:58:16 +0100957/*! Get the IP-Aaddress of the associated MGW as string.
958 * \param[in] mgcp MGCP client descriptor.
959 * \returns a pointer to the address string. */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200960const char *mgcp_client_remote_addr_str(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200961{
962 return mgcp->actual.remote_addr;
963}
964
Philipp Maier275ac972018-01-20 00:58:16 +0100965/*! Get the IP-Port of the associated MGW.
966 * \param[in] mgcp MGCP client descriptor.
967 * \returns port number. */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200968uint16_t mgcp_client_remote_port(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200969{
970 return mgcp->actual.remote_port;
971}
972
Pau Espin Pedrol74d0e5c2020-09-02 17:19:20 +0200973/*! Get the IP-Address of the associated MGW as its numeric representation.
974 * DEPRECATED, DON'T USE.
Philipp Maier275ac972018-01-20 00:58:16 +0100975 * \param[in] mgcp MGCP client descriptor.
976 * \returns IP-Address as 32 bit integer (network byte order) */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200977uint32_t mgcp_client_remote_addr_n(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200978{
Pau Espin Pedrol74d0e5c2020-09-02 17:19:20 +0200979 return 0;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200980}
981
Neels Hofmeyrac69ea92018-12-19 00:27:50 +0100982/* To compose endpoint names, usually for CRCX, use this as domain name.
983 * For example, snprintf("rtpbridge\*@%s", mgcp_client_endpoint_domain(mgcp)). */
984const char *mgcp_client_endpoint_domain(const struct mgcp_client *mgcp)
985{
986 return mgcp->actual.endpoint_domain_name[0] ? mgcp->actual.endpoint_domain_name : "mgw";
987}
988
Philipp Maiera466f572020-06-25 16:11:04 +0200989/*! Compose endpoint name for a wildcarded request to the virtual trunk
990 * \param[in] mgcp MGCP client descriptor.
991 * \returns string containing the endpoint name (e.g. rtpbridge\*@mgw) */
Pau Espin Pedrolca0818c2019-04-16 17:23:38 +0200992const char *mgcp_client_rtpbridge_wildcard(const struct mgcp_client *mgcp)
993{
994 return _mgcp_client_name_append_domain(mgcp, "rtpbridge/*");
995}
996
Philipp Maier48635912020-06-25 20:16:22 +0200997/*! Compose endpoint name for an E1 endpoint.
998 * \param[in] ctx talloc context.
999 * \param[in] mgcp MGCP client descriptor.
1000 * \param[in] trunk_id id number of the E1 trunk (1-64).
1001 * \param[in] ts timeslot on the E1 trunk (1-31).
1002 * \param[in] rate bitrate used on the E1 trunk (e.g 16 for 16kbit).
1003 * \param[in] offset bit offset of the E1 subslot (e.g. 4 for the third 16k subslot).
1004 * \returns string containing the endpoint name (e.g. ds/e1-1/s-1/su16-4). */
1005const char *mgcp_client_e1_epname(void *ctx, const struct mgcp_client *mgcp, uint8_t trunk_id, uint8_t ts,
1006 uint8_t rate, uint8_t offset)
1007{
1008 /* See also comment in libosmo-mgcp, mgcp_client.c, gen_e1_epname() */
1009 const uint8_t valid_rates[] = { 64, 32, 32, 16, 16, 16, 16, 8, 8, 8, 8, 8, 8, 8, 8 };
1010 const uint8_t valid_offsets[] = { 0, 0, 4, 0, 2, 4, 6, 0, 1, 2, 3, 4, 5, 6, 7 };
1011
1012 uint8_t i;
1013 bool rate_offs_valid = false;
1014 char *epname;
1015
1016 epname =
1017 talloc_asprintf(ctx, "ds/e1-%u/s-%u/su%u-%u@%s", trunk_id, ts, rate, offset,
1018 mgcp_client_endpoint_domain(mgcp));
1019 if (!epname) {
Philipp Maierdf9192e2021-09-03 17:50:51 +02001020 LOGPMGW(mgcp, LOGL_ERROR, "Cannot compose MGCP e1-endpoint name!\n");
Philipp Maier48635912020-06-25 20:16:22 +02001021 return NULL;
1022 }
1023
1024 /* Check if the supplied rate/offset pair resembles a valid combination */
1025 for (i = 0; i < sizeof(valid_rates); i++) {
1026 if (valid_rates[i] == rate && valid_offsets[i] == offset)
1027 rate_offs_valid = true;
1028 }
1029 if (!rate_offs_valid) {
Philipp Maierdf9192e2021-09-03 17:50:51 +02001030 LOGPMGW(mgcp, LOGL_ERROR,
1031 "Cannot compose MGCP e1-endpoint name (%s), rate(%u)/offset(%u) combination is invalid!\n",
1032 epname, rate, offset);
Philipp Maier48635912020-06-25 20:16:22 +02001033 talloc_free(epname);
1034 return NULL;
1035 }
1036
1037 /* An E1 line has a maximum of 32 timeslots, while the first (ts=0) is
1038 * reserverd for framing and alignment, so we can not use it here. */
Philipp Maier92a73cd2020-11-26 22:21:11 +01001039 if (ts == 0 || ts > NUM_E1_TS-1) {
Philipp Maierdf9192e2021-09-03 17:50:51 +02001040 LOGPMGW(mgcp, LOGL_ERROR,
1041 "Cannot compose MGCP e1-endpoint name (%s), E1-timeslot number (%u) is invalid!\n",
1042 epname, ts);
Philipp Maier48635912020-06-25 20:16:22 +02001043 talloc_free(epname);
1044 return NULL;
1045 }
1046
1047 return epname;
1048}
1049
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +02001050struct mgcp_response_pending * mgcp_client_pending_add(
1051 struct mgcp_client *mgcp,
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001052 mgcp_trans_id_t trans_id,
1053 mgcp_response_cb_t response_cb,
1054 void *priv)
1055{
1056 struct mgcp_response_pending *pending;
1057
1058 pending = talloc_zero(mgcp, struct mgcp_response_pending);
Harald Welte827da2c2020-07-15 10:57:08 +02001059 if (!pending)
1060 return NULL;
1061
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001062 pending->trans_id = trans_id;
1063 pending->response_cb = response_cb;
1064 pending->priv = priv;
1065 llist_add_tail(&pending->entry, &mgcp->responses_pending);
1066
1067 return pending;
1068}
1069
Philipp Maierdf9192e2021-09-03 17:50:51 +02001070/* Send the MGCP message in msg to the MGW and handle a response with
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001071 * response_cb. NOTE: the response_cb still needs to call
1072 * mgcp_response_parse_params(response) to get the parsed parameters -- to
1073 * potentially save some CPU cycles, only the head line has been parsed when
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +01001074 * the response_cb is invoked.
1075 * Before the priv pointer becomes invalid, e.g. due to transaction timeout,
1076 * mgcp_client_cancel() needs to be called for this transaction.
1077 */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +02001078int mgcp_client_tx(struct mgcp_client *mgcp, struct msgb *msg,
1079 mgcp_response_cb_t response_cb, void *priv)
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001080{
Harald Welte563ffc52020-06-17 21:54:13 +07001081 struct mgcp_response_pending *pending = NULL;
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001082 mgcp_trans_id_t trans_id;
1083 int rc;
1084
1085 trans_id = msg->cb[MSGB_CB_MGCP_TRANS_ID];
1086 if (!trans_id) {
Philipp Maierdf9192e2021-09-03 17:50:51 +02001087 LOGPMGW(mgcp, LOGL_ERROR,
1088 "Unset transaction id in mgcp send request\n");
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001089 talloc_free(msg);
1090 return -EINVAL;
1091 }
1092
Harald Welte563ffc52020-06-17 21:54:13 +07001093 /* Do not allocate a dummy 'mgcp_response_pending' entry */
1094 if (response_cb != NULL) {
1095 pending = mgcp_client_pending_add(mgcp, trans_id, response_cb, priv);
1096 if (!pending) {
1097 talloc_free(msg);
1098 return -ENOMEM;
1099 }
Harald Welte827da2c2020-07-15 10:57:08 +02001100 }
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001101
1102 if (msgb_l2len(msg) > 4096) {
Philipp Maierdf9192e2021-09-03 17:50:51 +02001103 LOGPMGW(mgcp, LOGL_ERROR,
1104 "Cannot send, MGCP message too large: %u\n",
1105 msgb_l2len(msg));
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001106 msgb_free(msg);
1107 rc = -EINVAL;
1108 goto mgcp_tx_error;
1109 }
1110
1111 rc = osmo_wqueue_enqueue(&mgcp->wq, msg);
1112 if (rc) {
Philipp Maierdf9192e2021-09-03 17:50:51 +02001113 LOGPMGW(mgcp, LOGL_FATAL, "Could not queue message to MGW\n");
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001114 msgb_free(msg);
1115 goto mgcp_tx_error;
1116 } else
Philipp Maierdf9192e2021-09-03 17:50:51 +02001117 LOGPMGW(mgcp, LOGL_DEBUG, "Queued %u bytes for MGW\n",
1118 msgb_l2len(msg));
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001119 return 0;
1120
1121mgcp_tx_error:
Harald Welte563ffc52020-06-17 21:54:13 +07001122 if (!pending)
Vadim Yanitskiyffbc6182020-07-16 15:48:13 +07001123 return rc;
Vadim Yanitskiy3f8139c2020-06-17 20:50:17 +07001124 /* Dequeue pending response, it's going to be free()d */
1125 llist_del(&pending->entry);
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001126 /* Pass NULL to response cb to indicate an error */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +02001127 mgcp_client_handle_response(mgcp, pending, NULL);
Vadim Yanitskiyffbc6182020-07-16 15:48:13 +07001128 return rc;
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001129}
1130
Philipp Maier275ac972018-01-20 00:58:16 +01001131/*! Cancel a pending transaction.
1132 * \param[in] mgcp MGCP client descriptor.
1133 * \param[in,out] trans_id Transaction id.
1134 * \returns 0 on success, -ENOENT on error.
1135 *
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +01001136 * Should a priv pointer passed to mgcp_client_tx() become invalid, this function must be called. In
1137 * practical terms, if the caller of mgcp_client_tx() wishes to tear down a transaction without having
1138 * received a response this function must be called. The trans_id can be obtained by calling
Philipp Maier275ac972018-01-20 00:58:16 +01001139 * mgcp_msg_trans_id() on the msgb produced by mgcp_msg_gen(). */
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +01001140int mgcp_client_cancel(struct mgcp_client *mgcp, mgcp_trans_id_t trans_id)
1141{
1142 struct mgcp_response_pending *pending = mgcp_client_response_pending_get(mgcp, trans_id);
1143 if (!pending) {
Philipp Maier275ac972018-01-20 00:58:16 +01001144 /*! Note: it is not harmful to cancel a transaction twice. */
Philipp Maierdf9192e2021-09-03 17:50:51 +02001145 LOGPMGW(mgcp, LOGL_ERROR, "Cannot cancel, no such transaction: %u\n", trans_id);
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +01001146 return -ENOENT;
1147 }
Philipp Maierdf9192e2021-09-03 17:50:51 +02001148 LOGPMGW(mgcp, LOGL_DEBUG, "Canceled transaction %u\n", trans_id);
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +01001149 talloc_free(pending);
1150 return 0;
Philipp Maier275ac972018-01-20 00:58:16 +01001151 /*! We don't really need to clean up the wqueue: In all sane cases, the msgb has already been sent
1152 * out and is no longer in the wqueue. If it still is in the wqueue, then sending MGCP messages
1153 * per se is broken and the program should notice so by a full wqueue. Even if this was called
1154 * before we had a chance to send out the message and it is still going to be sent, we will just
1155 * ignore the reply to it later. Removing a msgb from the wqueue here would just introduce more
1156 * bug surface in terms of failing to update wqueue API's counters or some such.
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +01001157 */
1158}
1159
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +02001160static mgcp_trans_id_t mgcp_client_next_trans_id(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001161{
1162 /* avoid zero trans_id to distinguish from unset trans_id */
1163 if (!mgcp->next_trans_id)
1164 mgcp->next_trans_id ++;
1165 return mgcp->next_trans_id ++;
1166}
1167
Philipp Maier1dc6be62017-10-05 18:25:37 +02001168#define MGCP_CRCX_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT | \
1169 MGCP_MSG_PRESENCE_CALL_ID | \
Philipp Maier1dc6be62017-10-05 18:25:37 +02001170 MGCP_MSG_PRESENCE_CONN_MODE)
1171#define MGCP_MDCX_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT | \
Philipp Maier490cbaa2018-01-22 17:32:38 +01001172 MGCP_MSG_PRESENCE_CALL_ID | \
Philipp Maier1dc6be62017-10-05 18:25:37 +02001173 MGCP_MSG_PRESENCE_CONN_ID)
1174#define MGCP_DLCX_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT)
1175#define MGCP_AUEP_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT)
1176#define MGCP_RSIP_MANDATORY 0 /* none */
1177
Philipp Maier704c4f02018-06-07 18:51:31 +02001178/* Helper function for mgcp_msg_gen(): Add LCO information to MGCP message */
1179static int add_lco(struct msgb *msg, struct mgcp_msg *mgcp_msg)
1180{
1181 unsigned int i;
1182 int rc = 0;
1183 const char *codec;
1184 unsigned int pt;
1185
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001186 rc |= msgb_printf(msg, "L:");
Philipp Maier704c4f02018-06-07 18:51:31 +02001187
1188 if (mgcp_msg->ptime)
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001189 rc |= msgb_printf(msg, " p:%u,", mgcp_msg->ptime);
Philipp Maier704c4f02018-06-07 18:51:31 +02001190
1191 if (mgcp_msg->codecs_len) {
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001192 rc |= msgb_printf(msg, " a:");
Philipp Maier704c4f02018-06-07 18:51:31 +02001193 for (i = 0; i < mgcp_msg->codecs_len; i++) {
1194 pt = mgcp_msg->codecs[i];
Neels Hofmeyr47642f22019-02-27 05:56:53 +01001195 codec = get_value_string_or_null(osmo_mgcpc_codec_names, pt);
Pau Espin Pedrolc12bfb72019-04-16 17:23:09 +02001196
Philipp Maier704c4f02018-06-07 18:51:31 +02001197 /* Note: Use codec descriptors from enum mgcp_codecs
1198 * in mgcp_client only! */
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001199 if (!codec) {
1200 msgb_free(msg);
1201 return -EINVAL;
1202 }
1203
1204 rc |= msgb_printf(msg, "%s", extract_codec_name(codec));
Philipp Maier704c4f02018-06-07 18:51:31 +02001205 if (i < mgcp_msg->codecs_len - 1)
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001206 rc |= msgb_printf(msg, ";");
Philipp Maier704c4f02018-06-07 18:51:31 +02001207 }
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001208 rc |= msgb_printf(msg, ",");
Philipp Maier704c4f02018-06-07 18:51:31 +02001209 }
1210
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001211 rc |= msgb_printf(msg, " nt:IN\r\n");
Philipp Maier704c4f02018-06-07 18:51:31 +02001212
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001213 if (rc != 0) {
1214 LOGP(DLMGCP, LOGL_ERROR,
1215 "message buffer to small, can not generate MGCP message (LCO)\n");
1216 msgb_free(msg);
1217 return -ENOBUFS;
1218 }
1219 return 0;
Philipp Maier704c4f02018-06-07 18:51:31 +02001220}
1221
1222/* Helper function for mgcp_msg_gen(): Add SDP information to MGCP message */
1223static int add_sdp(struct msgb *msg, struct mgcp_msg *mgcp_msg, struct mgcp_client *mgcp)
1224{
1225 unsigned int i;
1226 int rc = 0;
Pau Espin Pedrol8667d512020-08-28 19:37:08 +02001227 char local_ip[INET6_ADDRSTRLEN];
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +02001228 int local_ip_family, audio_ip_family;
Philipp Maier704c4f02018-06-07 18:51:31 +02001229 const char *codec;
1230 unsigned int pt;
1231
1232 /* Add separator to mark the beginning of the SDP block */
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001233 rc |= msgb_printf(msg, "\r\n");
Philipp Maier704c4f02018-06-07 18:51:31 +02001234
1235 /* Add SDP protocol version */
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001236 rc |= msgb_printf(msg, "v=0\r\n");
Philipp Maier704c4f02018-06-07 18:51:31 +02001237
1238 /* Determine local IP-Address */
1239 if (osmo_sock_local_ip(local_ip, mgcp->actual.remote_addr) < 0) {
Philipp Maierdf9192e2021-09-03 17:50:51 +02001240 LOGPMGW(mgcp, LOGL_ERROR,
1241 "Could not determine local IP-Address!\n");
Philipp Maier704c4f02018-06-07 18:51:31 +02001242 msgb_free(msg);
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001243 return -EINVAL;
Philipp Maier704c4f02018-06-07 18:51:31 +02001244 }
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +02001245 local_ip_family = osmo_ip_str_type(local_ip);
1246 if (local_ip_family == AF_UNSPEC) {
1247 msgb_free(msg);
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001248 return -EINVAL;
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +02001249 }
1250 audio_ip_family = osmo_ip_str_type(mgcp_msg->audio_ip);
1251 if (audio_ip_family == AF_UNSPEC) {
1252 msgb_free(msg);
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001253 return -EINVAL;
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +02001254 }
Philipp Maier704c4f02018-06-07 18:51:31 +02001255
1256 /* Add owner/creator (SDP) */
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001257 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 +02001258 local_ip_family == AF_INET6 ? '6' : '4',
1259 local_ip);
Philipp Maier704c4f02018-06-07 18:51:31 +02001260
1261 /* Add session name (none) */
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001262 rc |= msgb_printf(msg, "s=-\r\n");
Philipp Maier704c4f02018-06-07 18:51:31 +02001263
1264 /* Add RTP address and port */
1265 if (mgcp_msg->audio_port == 0) {
Philipp Maierdf9192e2021-09-03 17:50:51 +02001266 LOGPMGW(mgcp, LOGL_ERROR,
1267 "Invalid port number, can not generate MGCP message\n");
Philipp Maier704c4f02018-06-07 18:51:31 +02001268 msgb_free(msg);
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001269 return -EINVAL;
Philipp Maier704c4f02018-06-07 18:51:31 +02001270 }
1271 if (strlen(mgcp_msg->audio_ip) <= 0) {
Philipp Maierdf9192e2021-09-03 17:50:51 +02001272 LOGPMGW(mgcp, LOGL_ERROR,
1273 "Empty ip address, can not generate MGCP message\n");
Philipp Maier704c4f02018-06-07 18:51:31 +02001274 msgb_free(msg);
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001275 return -EINVAL;
Philipp Maier704c4f02018-06-07 18:51:31 +02001276 }
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001277 rc |= msgb_printf(msg, "c=IN IP%c %s\r\n",
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +02001278 audio_ip_family == AF_INET6 ? '6' : '4',
1279 mgcp_msg->audio_ip);
Philipp Maier704c4f02018-06-07 18:51:31 +02001280
1281 /* Add time description, active time (SDP) */
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001282 rc |= msgb_printf(msg, "t=0 0\r\n");
Philipp Maier704c4f02018-06-07 18:51:31 +02001283
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001284 rc |= msgb_printf(msg, "m=audio %u RTP/AVP", mgcp_msg->audio_port);
Philipp Maier704c4f02018-06-07 18:51:31 +02001285 for (i = 0; i < mgcp_msg->codecs_len; i++) {
1286 pt = map_codec_to_pt(mgcp_msg->ptmap, mgcp_msg->ptmap_len, mgcp_msg->codecs[i]);
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001287 rc |= msgb_printf(msg, " %u", pt);
Philipp Maier704c4f02018-06-07 18:51:31 +02001288
1289 }
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001290 rc |= msgb_printf(msg, "\r\n");
Philipp Maier704c4f02018-06-07 18:51:31 +02001291
Philipp Maier228e5912019-03-05 13:56:59 +01001292 /* Add optional codec parameters (fmtp) */
1293 if (mgcp_msg->param_present) {
1294 for (i = 0; i < mgcp_msg->codecs_len; i++) {
1295 /* The following is only applicable for AMR */
1296 if (mgcp_msg->codecs[i] != CODEC_AMR_8000_1 && mgcp_msg->codecs[i] != CODEC_AMRWB_16000_1)
1297 continue;
1298 pt = map_codec_to_pt(mgcp_msg->ptmap, mgcp_msg->ptmap_len, mgcp_msg->codecs[i]);
1299 if (mgcp_msg->param.amr_octet_aligned_present && mgcp_msg->param.amr_octet_aligned)
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001300 rc |= msgb_printf(msg, "a=fmtp:%u octet-align=1\r\n", pt);
Philipp Maier228e5912019-03-05 13:56:59 +01001301 else if (mgcp_msg->param.amr_octet_aligned_present && !mgcp_msg->param.amr_octet_aligned)
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001302 rc |= msgb_printf(msg, "a=fmtp:%u octet-align=0\r\n", pt);
Philipp Maier228e5912019-03-05 13:56:59 +01001303 }
1304 }
1305
Philipp Maier704c4f02018-06-07 18:51:31 +02001306 for (i = 0; i < mgcp_msg->codecs_len; i++) {
1307 pt = map_codec_to_pt(mgcp_msg->ptmap, mgcp_msg->ptmap_len, mgcp_msg->codecs[i]);
Pau Espin Pedrolc12bfb72019-04-16 17:23:09 +02001308
Philipp Maier704c4f02018-06-07 18:51:31 +02001309 /* Note: Only dynamic payload type from the range 96-127
1310 * require to be explained further via rtpmap. All others
1311 * are implcitly definedby the number in m=audio */
1312 if (pt >= 96 && pt <= 127) {
Neels Hofmeyr47642f22019-02-27 05:56:53 +01001313 codec = get_value_string_or_null(osmo_mgcpc_codec_names, mgcp_msg->codecs[i]);
Philipp Maier704c4f02018-06-07 18:51:31 +02001314
1315 /* Note: Use codec descriptors from enum mgcp_codecs
1316 * in mgcp_client only! */
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001317 if (!codec) {
1318 msgb_free(msg);
1319 return -EINVAL;
1320 }
Pau Espin Pedrolc12bfb72019-04-16 17:23:09 +02001321
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001322 rc |= msgb_printf(msg, "a=rtpmap:%u %s\r\n", pt, codec);
Philipp Maier704c4f02018-06-07 18:51:31 +02001323 }
1324 }
Pau Espin Pedrolc12bfb72019-04-16 17:23:09 +02001325
Philipp Maier704c4f02018-06-07 18:51:31 +02001326 if (mgcp_msg->ptime)
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001327 rc |= msgb_printf(msg, "a=ptime:%u\r\n", mgcp_msg->ptime);
Philipp Maier704c4f02018-06-07 18:51:31 +02001328
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001329 if (rc != 0) {
Philipp Maierdf9192e2021-09-03 17:50:51 +02001330 LOGPMGW(mgcp, LOGL_ERROR, "Message buffer to small, can not generate MGCP message (SDP)\n");
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001331 msgb_free(msg);
1332 return -ENOBUFS;
1333 }
1334
1335 return 0;
Philipp Maier704c4f02018-06-07 18:51:31 +02001336}
1337
Philipp Maier275ac972018-01-20 00:58:16 +01001338/*! Generate an MGCP message
1339 * \param[in] mgcp MGCP client descriptor.
1340 * \param[in] mgcp_msg Message description
1341 * \returns message buffer on success, NULL on error. */
Philipp Maier1dc6be62017-10-05 18:25:37 +02001342struct msgb *mgcp_msg_gen(struct mgcp_client *mgcp, struct mgcp_msg *mgcp_msg)
1343{
1344 mgcp_trans_id_t trans_id = mgcp_client_next_trans_id(mgcp);
1345 uint32_t mandatory_mask;
1346 struct msgb *msg = msgb_alloc_headroom(4096, 128, "MGCP tx");
1347 int rc = 0;
Philipp Maier704c4f02018-06-07 18:51:31 +02001348 bool use_sdp = false;
Pau Espin Pedrol900cd652019-04-24 22:06:22 +02001349 char buf[32];
Philipp Maier1dc6be62017-10-05 18:25:37 +02001350
1351 msg->l2h = msg->data;
1352 msg->cb[MSGB_CB_MGCP_TRANS_ID] = trans_id;
1353
1354 /* Add command verb */
1355 switch (mgcp_msg->verb) {
1356 case MGCP_VERB_CRCX:
1357 mandatory_mask = MGCP_CRCX_MANDATORY;
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001358 rc |= msgb_printf(msg, "CRCX %u", trans_id);
Philipp Maier1dc6be62017-10-05 18:25:37 +02001359 break;
1360 case MGCP_VERB_MDCX:
1361 mandatory_mask = MGCP_MDCX_MANDATORY;
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001362 rc |= msgb_printf(msg, "MDCX %u", trans_id);
Philipp Maier1dc6be62017-10-05 18:25:37 +02001363 break;
1364 case MGCP_VERB_DLCX:
1365 mandatory_mask = MGCP_DLCX_MANDATORY;
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001366 rc |= msgb_printf(msg, "DLCX %u", trans_id);
Philipp Maier1dc6be62017-10-05 18:25:37 +02001367 break;
1368 case MGCP_VERB_AUEP:
1369 mandatory_mask = MGCP_AUEP_MANDATORY;
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001370 rc |= msgb_printf(msg, "AUEP %u", trans_id);
Philipp Maier1dc6be62017-10-05 18:25:37 +02001371 break;
1372 case MGCP_VERB_RSIP:
1373 mandatory_mask = MGCP_RSIP_MANDATORY;
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001374 rc |= msgb_printf(msg, "RSIP %u", trans_id);
Philipp Maier1dc6be62017-10-05 18:25:37 +02001375 break;
1376 default:
Philipp Maierdf9192e2021-09-03 17:50:51 +02001377 LOGPMGW(mgcp, LOGL_ERROR, "Invalid command verb, can not generate MGCP message\n");
Philipp Maier1dc6be62017-10-05 18:25:37 +02001378 msgb_free(msg);
1379 return NULL;
1380 }
1381
1382 /* Check if mandatory fields are missing */
1383 if (!((mgcp_msg->presence & mandatory_mask) == mandatory_mask)) {
Philipp Maierdf9192e2021-09-03 17:50:51 +02001384 LOGPMGW(mgcp, LOGL_ERROR,
1385 "One or more missing mandatory fields, can not generate MGCP message\n");
Philipp Maier1dc6be62017-10-05 18:25:37 +02001386 msgb_free(msg);
1387 return NULL;
1388 }
1389
1390 /* Add endpoint name */
Philipp Maier7bc55522017-12-10 22:52:22 +01001391 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_ENDPOINT) {
1392 if (strlen(mgcp_msg->endpoint) <= 0) {
Philipp Maierdf9192e2021-09-03 17:50:51 +02001393 LOGPMGW(mgcp, LOGL_ERROR, "Empty endpoint name, can not generate MGCP message\n");
Philipp Maier7bc55522017-12-10 22:52:22 +01001394 msgb_free(msg);
1395 return NULL;
1396 }
Philipp Maier3aa81572018-01-31 14:13:45 +01001397
1398 if (strstr(mgcp_msg->endpoint, "@") == NULL) {
Philipp Maierdf9192e2021-09-03 17:50:51 +02001399 LOGPMGW(mgcp, LOGL_ERROR,
1400 "Endpoint name (%s) lacks separator (@), can not generate MGCP message\n",
1401 mgcp_msg->endpoint);
Philipp Maier3aa81572018-01-31 14:13:45 +01001402 msgb_free(msg);
Vadim Yanitskiye6743452020-06-18 03:04:13 +07001403 return NULL;
Philipp Maier3aa81572018-01-31 14:13:45 +01001404 }
1405
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001406 rc |= msgb_printf(msg, " %s", mgcp_msg->endpoint);
Philipp Maier7bc55522017-12-10 22:52:22 +01001407 }
Philipp Maier1dc6be62017-10-05 18:25:37 +02001408
1409 /* Add protocol version */
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001410 rc |= msgb_printf(msg, " MGCP 1.0\r\n");
Philipp Maier1dc6be62017-10-05 18:25:37 +02001411
1412 /* Add call id */
1413 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_CALL_ID)
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001414 rc |= msgb_printf(msg, "C: %x\r\n", mgcp_msg->call_id);
Philipp Maier1dc6be62017-10-05 18:25:37 +02001415
1416 /* Add connection id */
Philipp Maier7bc55522017-12-10 22:52:22 +01001417 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_CONN_ID) {
1418 if (strlen(mgcp_msg->conn_id) <= 0) {
Philipp Maierdf9192e2021-09-03 17:50:51 +02001419 LOGPMGW(mgcp, LOGL_ERROR, "Empty connection id, can not generate MGCP message\n");
Philipp Maier7bc55522017-12-10 22:52:22 +01001420 msgb_free(msg);
1421 return NULL;
1422 }
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001423 rc |= msgb_printf(msg, "I: %s\r\n", mgcp_msg->conn_id);
Philipp Maier7bc55522017-12-10 22:52:22 +01001424 }
Philipp Maier1dc6be62017-10-05 18:25:37 +02001425
Philipp Maier704c4f02018-06-07 18:51:31 +02001426 /* Using SDP makes sense when a valid IP/Port combination is specifiec,
1427 * if we do not know this information yet, we fall back to LCO */
1428 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_AUDIO_IP
1429 && mgcp_msg->presence & MGCP_MSG_PRESENCE_AUDIO_PORT)
1430 use_sdp = true;
1431
1432 /* Add local connection options (LCO) */
1433 if (!use_sdp
1434 && (mgcp_msg->verb == MGCP_VERB_CRCX
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001435 || mgcp_msg->verb == MGCP_VERB_MDCX)) {
1436 if (add_lco(msg, mgcp_msg) < 0)
1437 return NULL;
1438 }
Philipp Maier1dc6be62017-10-05 18:25:37 +02001439
1440 /* Add mode */
1441 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_CONN_MODE)
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001442 rc |=
Philipp Maier1dc6be62017-10-05 18:25:37 +02001443 msgb_printf(msg, "M: %s\r\n",
1444 mgcp_client_cmode_name(mgcp_msg->conn_mode));
1445
Neels Hofmeyre6d8e912018-08-23 16:36:48 +02001446 /* Add X-Osmo-IGN */
1447 if ((mgcp_msg->presence & MGCP_MSG_PRESENCE_X_OSMO_IGN)
1448 && (mgcp_msg->x_osmo_ign != 0))
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001449 rc |=
Neels Hofmeyre6d8e912018-08-23 16:36:48 +02001450 msgb_printf(msg, MGCP_X_OSMO_IGN_HEADER "%s\r\n",
1451 mgcp_msg->x_osmo_ign & MGCP_X_OSMO_IGN_CALLID ? " C": "");
1452
Pau Espin Pedrol900cd652019-04-24 22:06:22 +02001453 /* Add X-Osmo-Osmux */
1454 if ((mgcp_msg->presence & MGCP_MSG_PRESENCE_X_OSMO_OSMUX_CID)) {
Pau Espin Pedrol1b1d7ed2019-05-23 16:48:24 +02001455 if (mgcp_msg->x_osmo_osmux_cid < -1 || mgcp_msg->x_osmo_osmux_cid > OSMUX_CID_MAX) {
Philipp Maierdf9192e2021-09-03 17:50:51 +02001456 LOGPMGW(mgcp, LOGL_ERROR, "Wrong Osmux CID %d, can not generate MGCP message\n",
1457 mgcp_msg->x_osmo_osmux_cid);
Pau Espin Pedrol1b1d7ed2019-05-23 16:48:24 +02001458 msgb_free(msg);
1459 return NULL;
1460 }
Pau Espin Pedrol900cd652019-04-24 22:06:22 +02001461 snprintf(buf, sizeof(buf), " %d", mgcp_msg->x_osmo_osmux_cid);
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001462 rc |=
Pau Espin Pedrol900cd652019-04-24 22:06:22 +02001463 msgb_printf(msg, MGCP_X_OSMO_OSMUX_HEADER "%s\r\n",
1464 mgcp_msg->x_osmo_osmux_cid == -1 ? " *": buf);
1465 }
1466
1467
Philipp Maier704c4f02018-06-07 18:51:31 +02001468 /* Add session description protocol (SDP) */
1469 if (use_sdp
1470 && (mgcp_msg->verb == MGCP_VERB_CRCX
1471 || mgcp_msg->verb == MGCP_VERB_MDCX)) {
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001472 if (add_sdp(msg, mgcp_msg, mgcp) < 0)
Philipp Maier9d25d7a2018-01-22 17:31:10 +01001473 return NULL;
Philipp Maier1dc6be62017-10-05 18:25:37 +02001474 }
1475
1476 if (rc != 0) {
Philipp Maierdf9192e2021-09-03 17:50:51 +02001477 LOGPMGW(mgcp, LOGL_ERROR, "Message buffer to small, can not generate MGCP message\n");
Philipp Maier1dc6be62017-10-05 18:25:37 +02001478 msgb_free(msg);
1479 msg = NULL;
1480 }
1481
1482 return msg;
1483}
1484
Philipp Maier275ac972018-01-20 00:58:16 +01001485/*! Retrieve the MGCP transaction ID from a msgb generated by mgcp_msg_gen()
1486 * \param[in] msg message buffer
1487 * \returns Transaction id. */
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +01001488mgcp_trans_id_t mgcp_msg_trans_id(struct msgb *msg)
1489{
1490 return (mgcp_trans_id_t)msg->cb[MSGB_CB_MGCP_TRANS_ID];
1491}
1492
Philipp Maierfa495d62021-09-02 10:08:56 +02001493/*! Get the configuration parameters for a given MGCP client instance
Philipp Maier275ac972018-01-20 00:58:16 +01001494 * \param[in] mgcp MGCP client descriptor.
1495 * \returns configuration */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +02001496struct mgcp_client_conf *mgcp_client_conf_actual(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001497{
1498 return &mgcp->actual;
1499}
Neels Hofmeyrd95ab1e2017-09-22 00:52:54 +02001500
1501const struct value_string mgcp_client_connection_mode_strs[] = {
1502 { MGCP_CONN_NONE, "none" },
1503 { MGCP_CONN_RECV_SEND, "sendrecv" },
1504 { MGCP_CONN_SEND_ONLY, "sendonly" },
1505 { MGCP_CONN_RECV_ONLY, "recvonly" },
1506 { MGCP_CONN_LOOPBACK, "loopback" },
1507 { 0, NULL }
1508};
Philipp Maierdf9192e2021-09-03 17:50:51 +02001509
1510/*! Get MGCP client instance name (VTY).
1511 * \param[in] mgcp MGCP client descriptor.
1512 * \returns MGCP client name.
1513 *
1514 * The user can only modify the name of an MGCP client instance when it is
1515 * part of a pool. For single MGCP client instances and MGCP client instance
1516 * where no description is set via the VTY, the MGW domain name will be used
1517 * as name. */
1518const char *mgcp_client_name(const struct mgcp_client *mgcp)
1519{
1520 if (!mgcp)
1521 return "(null)";
1522
1523 if (mgcp->actual.description)
1524 return mgcp->actual.description;
1525 else
1526 return mgcp_client_endpoint_domain(mgcp);
1527}