blob: 74b9cf721298ea1a8f324b2ee96431764a03b0e1 [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 Maier704c4f02018-06-07 18:51:31 +020049/* Codec descripton for dynamic payload types (SDP) */
Neels Hofmeyr47642f22019-02-27 05:56:53 +010050const struct value_string osmo_mgcpc_codec_names[] = {
Philipp Maier704c4f02018-06-07 18:51:31 +020051 { CODEC_PCMU_8000_1, "PCMU/8000/1" },
52 { CODEC_GSM_8000_1, "GSM/8000/1" },
53 { CODEC_PCMA_8000_1, "PCMA/8000/1" },
54 { CODEC_G729_8000_1, "G729/8000/1" },
55 { CODEC_GSMEFR_8000_1, "GSM-EFR/8000/1" },
56 { CODEC_GSMHR_8000_1, "GSM-HR-08/8000/1" },
57 { CODEC_AMR_8000_1, "AMR/8000/1" },
58 { CODEC_AMRWB_16000_1, "AMR-WB/16000/1" },
59 { 0, NULL },
60};
61
62/* Get encoding name from a full codec string e,g.
63 * ("CODEC/8000/2" => returns "CODEC") */
64static char *extract_codec_name(const char *str)
65{
66 static char buf[64];
67 unsigned int i;
68
69 if (!str)
70 return NULL;
71
Philipp Maier704c4f02018-06-07 18:51:31 +020072 osmo_strlcpy(buf, str, sizeof(buf));
73
74 for (i = 0; i < strlen(buf); i++) {
75 if (buf[i] == '/')
76 buf[i] = '\0';
77 }
78
79 return buf;
80}
81
82/*! Map a string to a codec.
83 * \ptmap[in] str input string (e.g "GSM/8000/1", "GSM/8000" or "GSM")
84 * \returns codec that corresponds to the given string representation. */
85enum mgcp_codecs map_str_to_codec(const char *str)
86{
87 unsigned int i;
88 char *codec_name;
89 char str_buf[64];
90
91 osmo_strlcpy(str_buf, extract_codec_name(str), sizeof(str_buf));
92
Neels Hofmeyr47642f22019-02-27 05:56:53 +010093 for (i = 0; i < ARRAY_SIZE(osmo_mgcpc_codec_names); i++) {
94 codec_name = extract_codec_name(osmo_mgcpc_codec_names[i].str);
Philipp Maier704c4f02018-06-07 18:51:31 +020095 if (!codec_name)
96 continue;
97 if (strcmp(codec_name, str_buf) == 0)
Neels Hofmeyr47642f22019-02-27 05:56:53 +010098 return osmo_mgcpc_codec_names[i].value;
Philipp Maier704c4f02018-06-07 18:51:31 +020099 }
100
101 return -1;
102}
103
104/* Check the ptmap for illegal mappings */
Neels Hofmeyr84274e42019-04-27 19:08:36 +0200105static int check_ptmap(const struct ptmap *ptmap)
Philipp Maier704c4f02018-06-07 18:51:31 +0200106{
107 /* Check if there are mappings that leave the IANA assigned dynamic
108 * payload type range. Under normal conditions such mappings should
109 * not occur */
110
111 /* Its ok to have a 1:1 mapping in the statically defined
112 * range, this won't hurt */
113 if (ptmap->codec == ptmap->pt)
114 return 0;
115
116 if (ptmap->codec < 96 || ptmap->codec > 127)
117 goto error;
118 if (ptmap->pt < 96 || ptmap->pt > 127)
119 goto error;
120
121 return 0;
122error:
123 LOGP(DLMGCP, LOGL_ERROR,
124 "ptmap contains illegal mapping: codec=%u maps to pt=%u\n",
125 ptmap->codec, ptmap->pt);
126 return -1;
127}
128
129/*! Map a codec to a payload type.
130 * \ptmap[in] payload pointer to payload type map with specified payload types.
131 * \ptmap[in] ptmap_len length of the payload type map.
132 * \ptmap[in] codec the codec for which the payload type should be looked up.
133 * \returns assigned payload type */
Neels Hofmeyr84274e42019-04-27 19:08:36 +0200134unsigned int map_codec_to_pt(const struct ptmap *ptmap, unsigned int ptmap_len,
Philipp Maier704c4f02018-06-07 18:51:31 +0200135 enum mgcp_codecs codec)
136{
137 unsigned int i;
138
139 /*! Note: If the payload type map is empty or the codec is not found
140 * in the map, then a 1:1 mapping is performed. If the codec falls
141 * into the statically defined range or if the mapping table isself
142 * tries to map to the statically defined range, then the mapping
143 * is also ignored and a 1:1 mapping is performed instead. */
144
145 /* we may return the codec directly since enum mgcp_codecs directly
146 * corresponds to the statićally assigned payload types */
147 if (codec < 96 || codec > 127)
148 return codec;
149
150 for (i = 0; i < ptmap_len; i++) {
151 /* Skip illegal map entries */
152 if (check_ptmap(ptmap) == 0 && ptmap->codec == codec)
153 return ptmap->pt;
154 ptmap++;
155 }
156
157 /* If nothing is found, do not perform any mapping */
158 return codec;
159}
160
161/*! Map a payload type to a codec.
162 * \ptmap[in] payload pointer to payload type map with specified payload types.
163 * \ptmap[in] ptmap_len length of the payload type map.
164 * \ptmap[in] payload type for which the codec should be looked up.
165 * \returns codec that corresponds to the specified payload type */
166enum mgcp_codecs map_pt_to_codec(struct ptmap *ptmap, unsigned int ptmap_len,
167 unsigned int pt)
168{
169 unsigned int i;
170
171 /*! Note: If the payload type map is empty or the payload type is not
172 * found in the map, then a 1:1 mapping is performed. If the payload
173 * type falls into the statically defined range or if the mapping
174 * table isself tries to map to the statically defined range, then
175 * the mapping is also ignored and a 1:1 mapping is performed
176 * instead. */
177
178 /* See also note in map_codec_to_pt() */
179 if (pt < 96 || pt > 127)
180 return pt;
181
182 for (i = 0; i < ptmap_len; i++) {
183 if (check_ptmap(ptmap) == 0 && ptmap->pt == pt)
184 return ptmap->codec;
185 ptmap++;
186 }
187
188 /* If nothing is found, do not perform any mapping */
189 return pt;
190}
191
Philipp Maierc824fe42021-08-03 15:00:02 +0200192/*! Initialize MGCP client configuration struct with default values.
Philipp Maier275ac972018-01-20 00:58:16 +0100193 * \param[out] conf Client configuration.*/
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200194void mgcp_client_conf_init(struct mgcp_client_conf *conf)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200195{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200196 /* NULL and -1 default to MGCP_CLIENT_*_DEFAULT values */
197 *conf = (struct mgcp_client_conf){
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200198 .local_addr = NULL,
199 .local_port = -1,
200 .remote_addr = NULL,
201 .remote_port = -1,
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200202 };
Philipp Maier3f2c15f2021-07-22 11:53:07 +0200203
204 INIT_LLIST_HEAD(&conf->reset_epnames);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200205}
206
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200207static void mgcp_client_handle_response(struct mgcp_client *mgcp,
208 struct mgcp_response_pending *pending,
209 struct mgcp_response *response)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200210{
211 if (!pending) {
212 LOGP(DLMGCP, LOGL_ERROR,
213 "Cannot handle NULL response\n");
214 return;
215 }
216 if (pending->response_cb)
217 pending->response_cb(response, pending->priv);
218 else
Neels Hofmeyred0c1aa2018-12-21 03:07:53 +0100219 LOGP(DLMGCP, LOGL_DEBUG, "MGCP response ignored (NULL cb)\n");
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200220 talloc_free(pending);
221}
222
223static int mgcp_response_parse_head(struct mgcp_response *r, struct msgb *msg)
224{
225 int comment_pos;
226 char *end;
227
228 if (mgcp_msg_terminate_nul(msg))
229 goto response_parse_failure;
230
231 r->body = (char *)msg->data;
232
Harald Welte9bf7c532017-11-17 14:14:31 +0100233 if (sscanf(r->body, "%3d %u %n",
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200234 &r->head.response_code, &r->head.trans_id,
235 &comment_pos) != 2)
236 goto response_parse_failure;
237
Philipp Maierabe8c892018-01-20 00:15:12 +0100238 osmo_strlcpy(r->head.comment, r->body + comment_pos, sizeof(r->head.comment));
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200239 end = strchr(r->head.comment, '\r');
240 if (!end)
241 goto response_parse_failure;
242 /* Mark the end of the comment */
243 *end = '\0';
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200244 return 0;
245
246response_parse_failure:
247 LOGP(DLMGCP, LOGL_ERROR,
248 "Failed to parse MGCP response header\n");
249 return -EINVAL;
250}
251
252/* TODO undup against mgcp_protocol.c:mgcp_check_param() */
253static bool mgcp_line_is_valid(const char *line)
254{
255 const size_t line_len = strlen(line);
256 if (line[0] == '\0')
257 return true;
258
259 if (line_len < 2
260 || line[1] != '=') {
261 LOGP(DLMGCP, LOGL_ERROR,
262 "Wrong MGCP option format: '%s'\n",
263 line);
264 return false;
265 }
266
267 return true;
268}
269
Philipp Maier704c4f02018-06-07 18:51:31 +0200270/* Parse a line like "m=audio 16002 RTP/AVP 98", extract port and payload types */
271static int mgcp_parse_audio_port_pt(struct mgcp_response *r, char *line)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200272{
Philipp Maier704c4f02018-06-07 18:51:31 +0200273 char *pt_str;
Pau Espin Pedrolc5c14302019-07-23 16:19:41 +0200274 char *pt_end;
Pau Espin Pedrola2b1c5e2019-07-26 14:13:14 +0200275 unsigned long int pt;
Philipp Maier704c4f02018-06-07 18:51:31 +0200276 unsigned int count = 0;
277 unsigned int i;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200278
Philipp Maier704c4f02018-06-07 18:51:31 +0200279 /* Extract port information */
280 if (sscanf(line, "m=audio %hu", &r->audio_port) != 1)
281 goto response_parse_failure_port;
Philipp Maier10f32db2017-12-13 12:34:34 +0100282 if (r->audio_port == 0)
Philipp Maier704c4f02018-06-07 18:51:31 +0200283 goto response_parse_failure_port;
Philipp Maier10f32db2017-12-13 12:34:34 +0100284
Philipp Maier704c4f02018-06-07 18:51:31 +0200285 /* Extract payload types */
286 line = strstr(line, "RTP/AVP ");
287 if (!line)
288 goto exit;
289
290 pt_str = strtok(line, " ");
291 while (1) {
292 /* Do not allow excessive payload types */
293 if (count > ARRAY_SIZE(r->codecs))
294 goto response_parse_failure_pt;
295
296 pt_str = strtok(NULL, " ");
297 if (!pt_str)
298 break;
Pau Espin Pedrolc5c14302019-07-23 16:19:41 +0200299 errno = 0;
300 pt = strtoul(pt_str, &pt_end, 0);
301 if ((errno == ERANGE && pt == ULONG_MAX) || (errno && !pt) ||
302 pt_str == pt_end)
303 goto response_parse_failure_pt;
Philipp Maier704c4f02018-06-07 18:51:31 +0200304
Pau Espin Pedrola2b1c5e2019-07-26 14:13:14 +0200305 if (pt >> 7) /* PT is 7 bit field, higher values not allowed */
306 goto response_parse_failure_pt;
307
Philipp Maier704c4f02018-06-07 18:51:31 +0200308 /* Do not allow duplicate payload types */
309 for (i = 0; i < count; i++)
310 if (r->codecs[i] == pt)
311 goto response_parse_failure_pt;
312
313 /* Note: The payload type we store may not necessarly match
314 * the codec types we have defined in enum mgcp_codecs. To
315 * ensure that the end result only contains codec types which
316 * match enum mgcp_codecs, we will go through afterwards and
317 * remap the affected entries with the inrofmation we learn
318 * from rtpmap */
319 r->codecs[count] = pt;
320 count++;
321 }
322
323 r->codecs_len = count;
324
325exit:
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200326 return 0;
327
Philipp Maier704c4f02018-06-07 18:51:31 +0200328response_parse_failure_port:
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200329 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier704c4f02018-06-07 18:51:31 +0200330 "Failed to parse SDP parameter port (%s)\n", line);
Philipp Maier06da85e2017-10-05 18:49:24 +0200331 return -EINVAL;
Philipp Maier704c4f02018-06-07 18:51:31 +0200332
333response_parse_failure_pt:
334 LOGP(DLMGCP, LOGL_ERROR,
335 "Failed to parse SDP parameter payload types (%s)\n", line);
336 return -EINVAL;
337}
338
339/* Parse a line like "m=audio 16002 RTP/AVP 98", extract port and payload types */
340static int mgcp_parse_audio_ptime_rtpmap(struct mgcp_response *r, const char *line)
341{
342 unsigned int pt;
343 char codec_resp[64];
Neels Hofmeyr3ab8ca42019-08-08 04:03:42 +0200344 enum mgcp_codecs codec;
Pau Espin Pedrolc12bfb72019-04-16 17:23:09 +0200345
Neels Hofmeyr23f40482019-08-08 04:03:42 +0200346#define A_PTIME "a=ptime:"
347#define A_RTPMAP "a=rtpmap:"
Pau Espin Pedrolc12bfb72019-04-16 17:23:09 +0200348
Neels Hofmeyr23f40482019-08-08 04:03:42 +0200349 if (osmo_str_startswith(line, A_PTIME)) {
350 if (sscanf(line, A_PTIME "%u", &r->ptime) != 1) {
351 LOGP(DLMGCP, LOGL_ERROR,
352 "Failed to parse SDP parameter, invalid ptime (%s)\n", line);
353 return -EINVAL;
354 }
355 } else if (osmo_str_startswith(line, A_RTPMAP)) {
356 if (sscanf(line, A_RTPMAP "%d %63s", &pt, codec_resp) != 2) {
357 LOGP(DLMGCP, LOGL_ERROR,
358 "Failed to parse SDP parameter, invalid rtpmap: %s\n", osmo_quote_str(line, -1));
359 return -EINVAL;
360 }
Neels Hofmeyr3ab8ca42019-08-08 04:03:42 +0200361 if (r->ptmap_len >= ARRAY_SIZE(r->ptmap)) {
362 LOGP(DLMGCP, LOGL_ERROR, "No more space in ptmap array (len=%u)\n", r->ptmap_len);
363 return -ENOSPC;
Neels Hofmeyr23f40482019-08-08 04:03:42 +0200364 }
Neels Hofmeyr3ab8ca42019-08-08 04:03:42 +0200365 codec = map_str_to_codec(codec_resp);
366 r->ptmap[r->ptmap_len].pt = pt;
367 r->ptmap[r->ptmap_len].codec = codec;
368 r->ptmap_len++;
Philipp Maier704c4f02018-06-07 18:51:31 +0200369 }
Pau Espin Pedrolc12bfb72019-04-16 17:23:09 +0200370
Philipp Maier704c4f02018-06-07 18:51:31 +0200371 return 0;
Philipp Maier06da85e2017-10-05 18:49:24 +0200372}
373
374/* Parse a line like "c=IN IP4 10.11.12.13" */
375static int mgcp_parse_audio_ip(struct mgcp_response *r, const char *line)
376{
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +0200377 struct in6_addr ip_test;
378 bool is_ipv6;
Philipp Maier06da85e2017-10-05 18:49:24 +0200379
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +0200380 if (strncmp("c=IN IP", line, 7) != 0)
Philipp Maier06da85e2017-10-05 18:49:24 +0200381 goto response_parse_failure;
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +0200382 line += 7;
383 if (*line == '6')
384 is_ipv6 = true;
385 else if (*line == '4')
386 is_ipv6 = false;
387 else
Philipp Maier06da85e2017-10-05 18:49:24 +0200388 goto response_parse_failure;
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +0200389 line++;
390 if (*line != ' ')
Philipp Maier06da85e2017-10-05 18:49:24 +0200391 goto response_parse_failure;
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +0200392 line++;
Philipp Maier06da85e2017-10-05 18:49:24 +0200393
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +0200394 /* Extract and check IP-Address */
395 if (is_ipv6) {
396 /* 45 = INET6_ADDRSTRLEN -1 */
397 if (sscanf(line, "%45s", r->audio_ip) != 1)
398 goto response_parse_failure;
399 if (inet_pton(AF_INET6, r->audio_ip, &ip_test) != 1)
400 goto response_parse_failure;
401 } else {
402 /* 15 = INET_ADDRSTRLEN -1 */
403 if (sscanf(line, "%15s", r->audio_ip) != 1)
404 goto response_parse_failure;
405 if (inet_pton(AF_INET, r->audio_ip, &ip_test) != 1)
406 goto response_parse_failure;
407 }
Philipp Maier06da85e2017-10-05 18:49:24 +0200408 return 0;
409
410response_parse_failure:
411 LOGP(DLMGCP, LOGL_ERROR,
412 "Failed to parse MGCP response header (audio ip)\n");
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200413 return -EINVAL;
414}
415
Pau Espin Pedrol91088c32019-04-24 21:02:40 +0200416/*! Extract OSMUX CID from an MGCP parameter line (string).
417 * \param[in] line single parameter line from the MGCP message
418 * \returns OSMUX CID, -1 wildcard, -2 on error
419 * FIXME: This is a copy of function in mgcp_msg.c. Have some common.c file between both libs?
420 */
421static int mgcp_parse_osmux_cid(const char *line)
422{
423 int osmux_cid;
424
425
Pau Espin Pedrolc1bf4692019-05-14 16:23:24 +0200426 if (strcasecmp(line + 2, "Osmux: *") == 0) {
Pau Espin Pedrol91088c32019-04-24 21:02:40 +0200427 LOGP(DLMGCP, LOGL_DEBUG, "Parsed wilcard Osmux CID\n");
428 return -1;
429 }
430
Pau Espin Pedrolc1bf4692019-05-14 16:23:24 +0200431 if (sscanf(line + 2 + 7, "%u", &osmux_cid) != 1) {
Pau Espin Pedrol91088c32019-04-24 21:02:40 +0200432 LOGP(DLMGCP, LOGL_ERROR, "Failed parsing Osmux in MGCP msg line: %s\n",
433 line);
434 return -2;
435 }
436
Pau Espin Pedrol91088c32019-04-24 21:02:40 +0200437 if (osmux_cid > OSMUX_CID_MAX) { /* OSMUX_CID_MAX from libosmo-netif */
438 LOGP(DLMGCP, LOGL_ERROR, "Osmux ID too large: %u > %u\n",
439 osmux_cid, OSMUX_CID_MAX);
440 return -2;
441 }
442 LOGP(DLMGCP, LOGL_DEBUG, "bsc-nat offered Osmux CID %u\n", osmux_cid);
443
444 return osmux_cid;
445}
446
Philipp Maier3b12e1b2018-01-18 15:16:13 +0100447/* A new section is marked by a double line break, check a few more
448 * patterns as there may be variants */
449static char *mgcp_find_section_end(char *string)
450{
451 char *rc;
452
453 rc = strstr(string, "\n\n");
454 if (rc)
455 return rc;
456
457 rc = strstr(string, "\n\r\n\r");
458 if (rc)
459 return rc;
460
461 rc = strstr(string, "\r\n\r\n");
462 if (rc)
463 return rc;
464
465 return NULL;
466}
467
Philipp Maier275ac972018-01-20 00:58:16 +0100468/*! Parse body (SDP) parameters of the MGCP response
469 * \param[in,out] r Response data
470 * \returns 0 on success, -EINVAL on error. */
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200471int mgcp_response_parse_params(struct mgcp_response *r)
472{
473 char *line;
474 int rc;
Neels Hofmeyr0793d2f2018-02-21 14:55:34 +0100475 char *data;
Philipp Maiere9d645b2018-01-19 23:54:08 +0100476 char *data_ptr;
Philipp Maier704c4f02018-06-07 18:51:31 +0200477 int i;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200478
Philipp Maiere9d645b2018-01-19 23:54:08 +0100479 /* Since this functions performs a destructive parsing, we create a
480 * local copy of the body data */
Neels Hofmeyr0793d2f2018-02-21 14:55:34 +0100481 OSMO_ASSERT(r->body);
482 data = talloc_strdup(r, r->body);
Philipp Maiere9d645b2018-01-19 23:54:08 +0100483 OSMO_ASSERT(data);
Philipp Maier55295f72018-01-15 14:00:28 +0100484
Philipp Maiere9d645b2018-01-19 23:54:08 +0100485 /* Find beginning of the parameter (SDP) section */
486 data_ptr = mgcp_find_section_end(data);
Neels Hofmeyr10835332018-02-21 14:55:34 +0100487 if (!data_ptr) {
Neels Hofmeyre8278312019-09-19 03:06:46 +0200488 LOGP(DLMGCP, LOGL_DEBUG, "MGCP response contains no SDP parameters\n");
489 rc = 0;
Philipp Maiere9d645b2018-01-19 23:54:08 +0100490 goto exit;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200491 }
492
Neels Hofmeyr0793d2f2018-02-21 14:55:34 +0100493 /* data_ptr now points to the beginning of the section-end-marker; for_each_non_empty_line()
494 * skips any \r and \n characters for free, so we don't need to skip the marker. */
495
Philipp Maiere9d645b2018-01-19 23:54:08 +0100496 for_each_non_empty_line(line, data_ptr) {
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200497 if (!mgcp_line_is_valid(line))
498 return -EINVAL;
499
500 switch (line[0]) {
Philipp Maier704c4f02018-06-07 18:51:31 +0200501 case 'a':
502 rc = mgcp_parse_audio_ptime_rtpmap(r, line);
503 if (rc)
504 goto exit;
505 break;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200506 case 'm':
Philipp Maier704c4f02018-06-07 18:51:31 +0200507 rc = mgcp_parse_audio_port_pt(r, line);
Philipp Maier06da85e2017-10-05 18:49:24 +0200508 if (rc)
Philipp Maiere9d645b2018-01-19 23:54:08 +0100509 goto exit;
Philipp Maier06da85e2017-10-05 18:49:24 +0200510 break;
511 case 'c':
512 rc = mgcp_parse_audio_ip(r, line);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200513 if (rc)
Philipp Maiere9d645b2018-01-19 23:54:08 +0100514 goto exit;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200515 break;
516 default:
517 /* skip unhandled parameters */
518 break;
519 }
520 }
Philipp Maiere9d645b2018-01-19 23:54:08 +0100521
Philipp Maier704c4f02018-06-07 18:51:31 +0200522 /* See also note in mgcp_parse_audio_port_pt() */
523 for (i = 0; i < r->codecs_len; i++)
524 r->codecs[i] = map_pt_to_codec(r->ptmap, r->ptmap_len, r->codecs[i]);
525
Philipp Maiere9d645b2018-01-19 23:54:08 +0100526 rc = 0;
527exit:
528 talloc_free(data);
529 return rc;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200530}
531
Philipp Maier55295f72018-01-15 14:00:28 +0100532/* Parse a line like "X: something" */
533static int mgcp_parse_head_param(char *result, unsigned int result_len,
534 char label, const char *line)
Philipp Maierffd75e42017-11-22 11:44:50 +0100535{
Philipp Maier55295f72018-01-15 14:00:28 +0100536 char label_string[4];
Neels Hofmeyr23e7bf12018-09-03 21:26:22 +0200537 size_t rc;
Philipp Maier55295f72018-01-15 14:00:28 +0100538
539 /* Detect empty parameters */
Philipp Maierffd75e42017-11-22 11:44:50 +0100540 if (strlen(line) < 4)
541 goto response_parse_failure;
542
Philipp Maier55295f72018-01-15 14:00:28 +0100543 /* Check if the label matches */
544 snprintf(label_string, sizeof(label_string), "%c: ", label);
545 if (memcmp(label_string, line, 3) != 0)
Philipp Maierffd75e42017-11-22 11:44:50 +0100546 goto response_parse_failure;
547
Philipp Maier55295f72018-01-15 14:00:28 +0100548 /* Copy payload part of the string to destinations (the label string
549 * is always 3 chars long) */
Neels Hofmeyr23e7bf12018-09-03 21:26:22 +0200550 rc = osmo_strlcpy(result, line + 3, result_len);
551 if (rc >= result_len) {
552 LOGP(DLMGCP, LOGL_ERROR,
553 "Failed to parse MGCP response (parameter label: %c):"
554 " the received conn ID is too long: %zu, maximum is %u characters\n",
555 label, rc, result_len - 1);
556 return -ENOSPC;
557 }
Philipp Maierffd75e42017-11-22 11:44:50 +0100558 return 0;
559
560response_parse_failure:
561 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier55295f72018-01-15 14:00:28 +0100562 "Failed to parse MGCP response (parameter label: %c)\n", label);
Philipp Maierffd75e42017-11-22 11:44:50 +0100563 return -EINVAL;
564}
565
566/* Parse MGCP parameters of the response */
567static int parse_head_params(struct mgcp_response *r)
568{
569 char *line;
570 int rc = 0;
571 OSMO_ASSERT(r->body);
Philipp Maier55295f72018-01-15 14:00:28 +0100572 char *data;
573 char *data_ptr;
574 char *data_end;
Philipp Maierffd75e42017-11-22 11:44:50 +0100575
Philipp Maier55295f72018-01-15 14:00:28 +0100576 /* Since this functions performs a destructive parsing, we create a
577 * local copy of the body data */
Philipp Maier1fc69992018-01-31 15:32:55 +0100578 data = talloc_zero_size(r, strlen(r->body)+1);
Philipp Maier55295f72018-01-15 14:00:28 +0100579 OSMO_ASSERT(data);
580 data_ptr = data;
581 osmo_strlcpy(data, r->body, strlen(r->body));
582
583 /* If there is an SDP body attached, prevent for_each_non_empty_line()
584 * into running in there, we are not yet interested in the parameters
585 * stored there. */
Pau Espin Pedrolc12bfb72019-04-16 17:23:09 +0200586 data_end = mgcp_find_section_end(data);
Philipp Maierffd75e42017-11-22 11:44:50 +0100587 if (data_end)
588 *data_end = '\0';
589
Philipp Maier55295f72018-01-15 14:00:28 +0100590 for_each_non_empty_line(line, data_ptr) {
Pau Espin Pedrol166077e2019-06-26 12:21:38 +0200591 switch (toupper(line[0])) {
Philipp Maier55295f72018-01-15 14:00:28 +0100592 case 'Z':
593 rc = mgcp_parse_head_param(r->head.endpoint,
594 sizeof(r->head.endpoint),
595 'Z', line);
596 if (rc)
597 goto exit;
Philipp Maier771b26a2018-01-31 13:53:11 +0100598
599 /* A specific endpoint identifier returned by the MGW
600 * must not contain any wildcard characters */
601 if (strstr(r->head.endpoint, "*") != NULL) {
602 rc = -EINVAL;
603 goto exit;
604 }
Philipp Maier3261dc72018-01-31 14:03:13 +0100605
606 /* A specific endpoint identifier returned by the MGW
607 * must contain an @ character */
608 if (strstr(r->head.endpoint, "@") == NULL) {
609 rc = -EINVAL;
610 goto exit;
611 }
Philipp Maier55295f72018-01-15 14:00:28 +0100612 break;
Philipp Maierffd75e42017-11-22 11:44:50 +0100613 case 'I':
Philipp Maier55295f72018-01-15 14:00:28 +0100614 rc = mgcp_parse_head_param(r->head.conn_id,
615 sizeof(r->head.conn_id),
616 'I', line);
Philipp Maierffd75e42017-11-22 11:44:50 +0100617 if (rc)
618 goto exit;
619 break;
Pau Espin Pedrol91088c32019-04-24 21:02:40 +0200620 case 'X':
Pau Espin Pedrolc1bf4692019-05-14 16:23:24 +0200621 if (strncasecmp("Osmux: ", line + 2, strlen("Osmux: ")) == 0) {
Pau Espin Pedrol91088c32019-04-24 21:02:40 +0200622 rc = mgcp_parse_osmux_cid(line);
623 if (rc < 0) {
624 /* -1: we don't want wildcards in response. -2: error */
625 rc = -EINVAL;
626 goto exit;
627 }
628 r->head.x_osmo_osmux_use = true;
629 r->head.x_osmo_osmux_cid = (uint8_t) rc;
630 rc = 0;
631 break;
632 }
633 /* Ignore unknown X-headers */
634 break;
Philipp Maierffd75e42017-11-22 11:44:50 +0100635 default:
636 /* skip unhandled parameters */
637 break;
638 }
639 }
640exit:
Philipp Maier55295f72018-01-15 14:00:28 +0100641 talloc_free(data);
Philipp Maierffd75e42017-11-22 11:44:50 +0100642 return rc;
643}
644
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200645static struct mgcp_response_pending *mgcp_client_response_pending_get(
646 struct mgcp_client *mgcp,
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100647 mgcp_trans_id_t trans_id)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200648{
649 struct mgcp_response_pending *pending;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200650 llist_for_each_entry(pending, &mgcp->responses_pending, entry) {
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100651 if (pending->trans_id == trans_id) {
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200652 llist_del(&pending->entry);
653 return pending;
654 }
655 }
656 return NULL;
657}
658
659/* Feed an MGCP message into the receive processing.
660 * Parse the head and call any callback registered for the transaction id found
661 * in the MGCP message. This is normally called directly from the internal
662 * mgcp_do_read that reads from the socket connected to the MGCP gateway. This
663 * function is published mainly to be able to feed data from the test suite.
664 */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200665int mgcp_client_rx(struct mgcp_client *mgcp, struct msgb *msg)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200666{
Philipp Maier1fc69992018-01-31 15:32:55 +0100667 struct mgcp_response *r;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200668 struct mgcp_response_pending *pending;
669 int rc;
670
Philipp Maier1fc69992018-01-31 15:32:55 +0100671 r = talloc_zero(mgcp, struct mgcp_response);
672 OSMO_ASSERT(r);
673
674 rc = mgcp_response_parse_head(r, msg);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200675 if (rc) {
Philipp Maierffd75e42017-11-22 11:44:50 +0100676 LOGP(DLMGCP, LOGL_ERROR, "Cannot parse MGCP response (head)\n");
Philipp Maier1fc69992018-01-31 15:32:55 +0100677 rc = 1;
678 goto error;
Philipp Maierffd75e42017-11-22 11:44:50 +0100679 }
680
Neels Hofmeyrd5731072021-07-15 01:36:47 +0200681 LOGP(DLMGCP, LOGL_DEBUG, "MGCP client: Rx %d %u %s\n",
682 r->head.response_code, r->head.trans_id, r->head.comment);
683
Philipp Maier1fc69992018-01-31 15:32:55 +0100684 rc = parse_head_params(r);
Philipp Maierffd75e42017-11-22 11:44:50 +0100685 if (rc) {
686 LOGP(DLMGCP, LOGL_ERROR, "Cannot parse MGCP response (head parameters)\n");
Philipp Maier1fc69992018-01-31 15:32:55 +0100687 rc = 1;
688 goto error;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200689 }
690
Philipp Maier1fc69992018-01-31 15:32:55 +0100691 pending = mgcp_client_response_pending_get(mgcp, r->head.trans_id);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200692 if (!pending) {
693 LOGP(DLMGCP, LOGL_ERROR,
694 "Cannot find matching MGCP transaction for trans_id %d\n",
Philipp Maier1fc69992018-01-31 15:32:55 +0100695 r->head.trans_id);
696 rc = -ENOENT;
697 goto error;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200698 }
699
Philipp Maier1fc69992018-01-31 15:32:55 +0100700 mgcp_client_handle_response(mgcp, pending, r);
701 rc = 0;
702
703error:
704 talloc_free(r);
705 return rc;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200706}
707
708static int mgcp_do_read(struct osmo_fd *fd)
709{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200710 struct mgcp_client *mgcp = fd->data;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200711 struct msgb *msg;
712 int ret;
713
714 msg = msgb_alloc_headroom(4096, 128, "mgcp_from_gw");
715 if (!msg) {
716 LOGP(DLMGCP, LOGL_ERROR, "Failed to allocate MGCP message.\n");
717 return -1;
718 }
719
720 ret = read(fd->fd, msg->data, 4096 - 128);
721 if (ret <= 0) {
Neels Hofmeyr0a403792018-12-12 03:10:18 +0100722 LOGP(DLMGCP, LOGL_ERROR, "Failed to read: %s: %d='%s'\n", osmo_sock_get_name2(fd->fd),
723 errno, strerror(errno));
724
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200725 msgb_free(msg);
726 return -1;
727 } else if (ret > 4096 - 128) {
Neels Hofmeyr0a403792018-12-12 03:10:18 +0100728 LOGP(DLMGCP, LOGL_ERROR, "Too much data: %s: %d\n", osmo_sock_get_name2(fd->fd), ret);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200729 msgb_free(msg);
730 return -1;
Harald Welte9bf7c532017-11-17 14:14:31 +0100731 }
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200732
733 msg->l2h = msgb_put(msg, ret);
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200734 ret = mgcp_client_rx(mgcp, msg);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200735 talloc_free(msg);
736 return ret;
737}
738
739static int mgcp_do_write(struct osmo_fd *fd, struct msgb *msg)
740{
741 int ret;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200742
Neels Hofmeyr0a403792018-12-12 03:10:18 +0100743 LOGP(DLMGCP, LOGL_DEBUG, "Tx MGCP: %s: len=%u '%s'...\n",
744 osmo_sock_get_name2(fd->fd), msg->len, 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)
Neels Hofmeyr0a403792018-12-12 03:10:18 +0100748 LOGP(DLMGCP, LOGL_ERROR, "Failed to Tx MGCP: %s: %d='%s'; msg: len=%u '%s'...\n",
749 osmo_sock_get_name2(fd->fd), errno, strerror(errno),
Neels Hofmeyr32d15cc2018-12-12 03:05:33 +0100750 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)) {
783 LOGP(DLMGCP, 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);
785 talloc_free(mgcp);
786 return NULL;
787 }
788 LOGP(DLMGCP, LOGL_NOTICE, "MGCP client: using endpoint domain '@%s'\n", mgcp_client_endpoint_domain(mgcp));
789
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
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200796 return mgcp;
797}
798
Philipp Maier3d2b76f2021-08-04 14:08:37 +0200799static int init_socket(struct mgcp_client *mgcp, unsigned int retry_n_ports)
Philipp Maier6a26c162018-08-01 16:37:06 +0200800{
801 int rc;
802 struct osmo_wqueue *wq;
Philipp Maier3d2b76f2021-08-04 14:08:37 +0200803 unsigned int i;
Philipp Maier6a26c162018-08-01 16:37:06 +0200804
805 wq = &mgcp->wq;
806
Philipp Maier3d2b76f2021-08-04 14:08:37 +0200807 for (i = 0; i < retry_n_ports + 1; i++) {
Philipp Maier6a26c162018-08-01 16:37:06 +0200808
Philipp Maier3d2b76f2021-08-04 14:08:37 +0200809 /* Initialize socket with the currently configured port number */
Pau Espin Pedrol531470a2020-08-31 17:06:55 +0200810 rc = osmo_sock_init2_ofd(&wq->bfd, AF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, mgcp->actual.local_addr,
Philipp Maier6a26c162018-08-01 16:37:06 +0200811 mgcp->actual.local_port, mgcp->actual.remote_addr, mgcp->actual.remote_port,
812 OSMO_SOCK_F_BIND | OSMO_SOCK_F_CONNECT);
813 if (rc > 0)
814 return rc;
815
Philipp Maier3d2b76f2021-08-04 14:08:37 +0200816 /* If there is a different port than the default port configured then we assume that the user has
817 * chosen that port conciously and we will not try to resolve this by silently choosing a different
818 * port. */
Philipp Maier9a7ccc32018-08-09 11:41:19 +0200819 if (mgcp->actual.local_port != MGCP_CLIENT_LOCAL_PORT_DEFAULT && i == 0)
Philipp Maier6a26c162018-08-01 16:37:06 +0200820 return -EINVAL;
821
Philipp Maier3d2b76f2021-08-04 14:08:37 +0200822 if (i == retry_n_ports) {
823 /* Last try failed */
824 LOGP(DLMGCP, LOGL_NOTICE, "MGCPGW failed to bind to %s:%d -- check configuration!\n",
825 mgcp->actual.local_addr ? mgcp->actual.local_addr : "(any)", mgcp->actual.local_port);
826 if (retry_n_ports == 0)
827 return -EINVAL;
828 } else {
829 /* Choose a new port number to try next */
830 LOGP(DLMGCP, LOGL_NOTICE,
831 "MGCPGW failed to bind to %s:%d, retrying with port %d -- check configuration!\n",
832 mgcp->actual.local_addr ? mgcp->actual.local_addr : "(any)", mgcp->actual.local_port,
833 mgcp->actual.local_port + 1);
834 mgcp->actual.local_port++;
835 }
Philipp Maier6a26c162018-08-01 16:37:06 +0200836 }
837
Philipp Maier3d2b76f2021-08-04 14:08:37 +0200838 LOGP(DLMGCP, LOGL_FATAL, "MGCPGW failed to find a port to bind on %u times -- check configuration!\n", i);
Philipp Maier6a26c162018-08-01 16:37:06 +0200839 return -EINVAL;
840}
841
Philipp Maier3f2c15f2021-07-22 11:53:07 +0200842/* Safely ignore the MGCP response to the DLCX sent via _mgcp_client_send_dlcx() */
843static void _ignore_mgcp_response(struct mgcp_response *response, void *priv) { }
844
845/* Format DLCX message (fire and forget) and send it off to the MGW */
846static void _mgcp_client_send_dlcx(struct mgcp_client *mgcp, const char *epname)
847{
848 struct msgb *msgb_dlcx;
849 struct mgcp_msg mgcp_msg_dlcx = {
850 .verb = MGCP_VERB_DLCX,
851 .presence = MGCP_MSG_PRESENCE_ENDPOINT,
852 };
853 osmo_strlcpy(mgcp_msg_dlcx.endpoint, epname, sizeof(mgcp_msg_dlcx.endpoint));
854 msgb_dlcx = mgcp_msg_gen(mgcp, &mgcp_msg_dlcx);
855 mgcp_client_tx(mgcp, msgb_dlcx, &_ignore_mgcp_response, NULL);
856}
857
858static const char *_mgcp_client_name_append_domain(const struct mgcp_client *mgcp, const char *name)
859{
860 static char endpoint[MGCP_ENDPOINT_MAXLEN];
861 int rc;
862
863 rc = snprintf(endpoint, sizeof(endpoint), "%s@%s", name, mgcp_client_endpoint_domain(mgcp));
864 if (rc > sizeof(endpoint) - 1) {
865 LOGP(DLMGCP, LOGL_ERROR, "MGCP endpoint exceeds maximum length of %zu: '%s@%s'\n",
866 sizeof(endpoint) - 1, name, mgcp_client_endpoint_domain(mgcp));
867 return NULL;
868 }
869 if (rc < 1) {
870 LOGP(DLMGCP, LOGL_ERROR, "Cannot compose MGCP endpoint name\n");
871 return NULL;
872 }
873 return endpoint;
874}
875
876/*! Initialize client connection (opens socket)
Philipp Maier275ac972018-01-20 00:58:16 +0100877 * \param[in,out] mgcp MGCP client descriptor.
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200878 * \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 +0100879 * \returns 0 on success, -EINVAL on error. */
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200880int mgcp_client_connect2(struct mgcp_client *mgcp, unsigned int retry_n_ports)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200881{
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200882 struct osmo_wqueue *wq;
883 int rc;
Philipp Maier3f2c15f2021-07-22 11:53:07 +0200884 struct reset_ep *reset_ep;
885 const char *epname;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200886
887 if (!mgcp) {
888 LOGP(DLMGCP, LOGL_FATAL, "MGCPGW client not initialized properly\n");
889 return -EINVAL;
890 }
891
892 wq = &mgcp->wq;
Harald Weltec2a8f592020-10-19 13:25:41 +0200893 osmo_wqueue_init(wq, 1024);
894 wq->read_cb = mgcp_do_read;
895 wq->write_cb = mgcp_do_write;
896
897 osmo_fd_setup(&wq->bfd, -1, OSMO_FD_READ, osmo_wqueue_bfd_cb, mgcp, 0);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200898
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200899 rc = init_socket(mgcp, retry_n_ports);
Harald Welte8890dfa2017-11-17 15:09:30 +0100900 if (rc < 0) {
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200901 LOGP(DLMGCP, LOGL_FATAL,
Harald Welte8890dfa2017-11-17 15:09:30 +0100902 "Failed to initialize socket %s:%u -> %s:%u for MGCP GW: %s\n",
Philipp Maier276a4142021-07-29 11:55:14 +0200903 mgcp->actual.local_addr ? mgcp->actual.local_addr : "(any)", mgcp->actual.local_port,
904 mgcp->actual.remote_addr ? mgcp->actual.local_addr : "(any)", mgcp->actual.remote_port,
905 strerror(errno));
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200906 goto error_close_fd;
907 }
908
Neels Hofmeyr0a403792018-12-12 03:10:18 +0100909 LOGP(DLMGCP, LOGL_INFO, "MGCP GW connection: %s\n", osmo_sock_get_name2(wq->bfd.fd));
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200910
Philipp Maier3f2c15f2021-07-22 11:53:07 +0200911 /* If configured, send a DLCX message to the endpoints that are configured to
912 * be reset on startup. Usually this is a wildcarded endpoint. */
913 llist_for_each_entry(reset_ep, &mgcp->actual.reset_epnames, list) {
914 epname = _mgcp_client_name_append_domain(mgcp, reset_ep->name);
915 LOGP(DLMGCP, LOGL_INFO, "MGCP GW sending DLCX to: %s\n", epname);
916 _mgcp_client_send_dlcx(mgcp, epname);
917 }
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200918 return 0;
919error_close_fd:
920 close(wq->bfd.fd);
921 wq->bfd.fd = -1;
922 return rc;
923}
924
Philipp Maier3f4a4cb2021-07-26 13:20:05 +0200925/*! Initialize client connection (opens socket)
926 * \param[in,out] mgcp MGCP client descriptor.
927 * \returns 0 on success, -EINVAL on error. */
928int mgcp_client_connect(struct mgcp_client *mgcp)
929{
930 return mgcp_client_connect2(mgcp, 99);
931}
932
933/*! Terminate client connection
934 * \param[in,out] mgcp MGCP client descriptor.
935 * \returns 0 on success, -EINVAL on error. */
936void mgcp_client_disconnect(struct mgcp_client *mgcp)
937{
938 struct osmo_wqueue *wq;
939
940 if (!mgcp) {
941 LOGP(DLMGCP, LOGL_FATAL, "MGCPGW client not initialized properly\n");
942 return;
943 }
944
945 wq = &mgcp->wq;
946 osmo_wqueue_clear(wq);
947 LOGP(DLMGCP, LOGL_INFO, "MGCP GW connection: %s -- closed!\n", osmo_sock_get_name2(wq->bfd.fd));
948 close(wq->bfd.fd);
949 wq->bfd.fd = -1;
950 if (osmo_fd_is_registered(&wq->bfd))
951 osmo_fd_unregister(&wq->bfd);
952}
953
Philipp Maier275ac972018-01-20 00:58:16 +0100954/*! Get the IP-Aaddress of the associated MGW as string.
955 * \param[in] mgcp MGCP client descriptor.
956 * \returns a pointer to the address string. */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200957const char *mgcp_client_remote_addr_str(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200958{
959 return mgcp->actual.remote_addr;
960}
961
Philipp Maier275ac972018-01-20 00:58:16 +0100962/*! Get the IP-Port of the associated MGW.
963 * \param[in] mgcp MGCP client descriptor.
964 * \returns port number. */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200965uint16_t mgcp_client_remote_port(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200966{
967 return mgcp->actual.remote_port;
968}
969
Pau Espin Pedrol74d0e5c2020-09-02 17:19:20 +0200970/*! Get the IP-Address of the associated MGW as its numeric representation.
971 * DEPRECATED, DON'T USE.
Philipp Maier275ac972018-01-20 00:58:16 +0100972 * \param[in] mgcp MGCP client descriptor.
973 * \returns IP-Address as 32 bit integer (network byte order) */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200974uint32_t mgcp_client_remote_addr_n(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200975{
Pau Espin Pedrol74d0e5c2020-09-02 17:19:20 +0200976 return 0;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200977}
978
Neels Hofmeyrac69ea92018-12-19 00:27:50 +0100979/* To compose endpoint names, usually for CRCX, use this as domain name.
980 * For example, snprintf("rtpbridge\*@%s", mgcp_client_endpoint_domain(mgcp)). */
981const char *mgcp_client_endpoint_domain(const struct mgcp_client *mgcp)
982{
983 return mgcp->actual.endpoint_domain_name[0] ? mgcp->actual.endpoint_domain_name : "mgw";
984}
985
Philipp Maiera466f572020-06-25 16:11:04 +0200986/*! Compose endpoint name for a wildcarded request to the virtual trunk
987 * \param[in] mgcp MGCP client descriptor.
988 * \returns string containing the endpoint name (e.g. rtpbridge\*@mgw) */
Pau Espin Pedrolca0818c2019-04-16 17:23:38 +0200989const char *mgcp_client_rtpbridge_wildcard(const struct mgcp_client *mgcp)
990{
991 return _mgcp_client_name_append_domain(mgcp, "rtpbridge/*");
992}
993
Philipp Maier48635912020-06-25 20:16:22 +0200994/*! Compose endpoint name for an E1 endpoint.
995 * \param[in] ctx talloc context.
996 * \param[in] mgcp MGCP client descriptor.
997 * \param[in] trunk_id id number of the E1 trunk (1-64).
998 * \param[in] ts timeslot on the E1 trunk (1-31).
999 * \param[in] rate bitrate used on the E1 trunk (e.g 16 for 16kbit).
1000 * \param[in] offset bit offset of the E1 subslot (e.g. 4 for the third 16k subslot).
1001 * \returns string containing the endpoint name (e.g. ds/e1-1/s-1/su16-4). */
1002const char *mgcp_client_e1_epname(void *ctx, const struct mgcp_client *mgcp, uint8_t trunk_id, uint8_t ts,
1003 uint8_t rate, uint8_t offset)
1004{
1005 /* See also comment in libosmo-mgcp, mgcp_client.c, gen_e1_epname() */
1006 const uint8_t valid_rates[] = { 64, 32, 32, 16, 16, 16, 16, 8, 8, 8, 8, 8, 8, 8, 8 };
1007 const uint8_t valid_offsets[] = { 0, 0, 4, 0, 2, 4, 6, 0, 1, 2, 3, 4, 5, 6, 7 };
1008
1009 uint8_t i;
1010 bool rate_offs_valid = false;
1011 char *epname;
1012
1013 epname =
1014 talloc_asprintf(ctx, "ds/e1-%u/s-%u/su%u-%u@%s", trunk_id, ts, rate, offset,
1015 mgcp_client_endpoint_domain(mgcp));
1016 if (!epname) {
1017 LOGP(DLMGCP, LOGL_ERROR, "Cannot compose MGCP e1-endpoint name!\n");
1018 return NULL;
1019 }
1020
1021 /* Check if the supplied rate/offset pair resembles a valid combination */
1022 for (i = 0; i < sizeof(valid_rates); i++) {
1023 if (valid_rates[i] == rate && valid_offsets[i] == offset)
1024 rate_offs_valid = true;
1025 }
1026 if (!rate_offs_valid) {
1027 LOGP(DLMGCP, LOGL_ERROR,
1028 "Cannot compose MGCP e1-endpoint name (%s), rate(%u)/offset(%u) combination is invalid!\n", epname,
1029 rate, offset);
1030 talloc_free(epname);
1031 return NULL;
1032 }
1033
1034 /* An E1 line has a maximum of 32 timeslots, while the first (ts=0) is
1035 * reserverd for framing and alignment, so we can not use it here. */
Philipp Maier92a73cd2020-11-26 22:21:11 +01001036 if (ts == 0 || ts > NUM_E1_TS-1) {
Philipp Maier48635912020-06-25 20:16:22 +02001037 LOGP(DLMGCP, LOGL_ERROR,
1038 "Cannot compose MGCP e1-endpoint name (%s), E1-timeslot number (%u) is invalid!\n", epname, ts);
1039 talloc_free(epname);
1040 return NULL;
1041 }
1042
1043 return epname;
1044}
1045
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +02001046struct mgcp_response_pending * mgcp_client_pending_add(
1047 struct mgcp_client *mgcp,
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001048 mgcp_trans_id_t trans_id,
1049 mgcp_response_cb_t response_cb,
1050 void *priv)
1051{
1052 struct mgcp_response_pending *pending;
1053
1054 pending = talloc_zero(mgcp, struct mgcp_response_pending);
Harald Welte827da2c2020-07-15 10:57:08 +02001055 if (!pending)
1056 return NULL;
1057
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001058 pending->trans_id = trans_id;
1059 pending->response_cb = response_cb;
1060 pending->priv = priv;
1061 llist_add_tail(&pending->entry, &mgcp->responses_pending);
1062
1063 return pending;
1064}
1065
1066/* Send the MGCP message in msg to the MGCP GW and handle a response with
1067 * response_cb. NOTE: the response_cb still needs to call
1068 * mgcp_response_parse_params(response) to get the parsed parameters -- to
1069 * potentially save some CPU cycles, only the head line has been parsed when
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +01001070 * the response_cb is invoked.
1071 * Before the priv pointer becomes invalid, e.g. due to transaction timeout,
1072 * mgcp_client_cancel() needs to be called for this transaction.
1073 */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +02001074int mgcp_client_tx(struct mgcp_client *mgcp, struct msgb *msg,
1075 mgcp_response_cb_t response_cb, void *priv)
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001076{
Harald Welte563ffc52020-06-17 21:54:13 +07001077 struct mgcp_response_pending *pending = NULL;
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001078 mgcp_trans_id_t trans_id;
1079 int rc;
1080
1081 trans_id = msg->cb[MSGB_CB_MGCP_TRANS_ID];
1082 if (!trans_id) {
1083 LOGP(DLMGCP, LOGL_ERROR,
1084 "Unset transaction id in mgcp send request\n");
1085 talloc_free(msg);
1086 return -EINVAL;
1087 }
1088
Harald Welte563ffc52020-06-17 21:54:13 +07001089 /* Do not allocate a dummy 'mgcp_response_pending' entry */
1090 if (response_cb != NULL) {
1091 pending = mgcp_client_pending_add(mgcp, trans_id, response_cb, priv);
1092 if (!pending) {
1093 talloc_free(msg);
1094 return -ENOMEM;
1095 }
Harald Welte827da2c2020-07-15 10:57:08 +02001096 }
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001097
1098 if (msgb_l2len(msg) > 4096) {
1099 LOGP(DLMGCP, LOGL_ERROR,
1100 "Cannot send, MGCP message too large: %u\n",
1101 msgb_l2len(msg));
1102 msgb_free(msg);
1103 rc = -EINVAL;
1104 goto mgcp_tx_error;
1105 }
1106
1107 rc = osmo_wqueue_enqueue(&mgcp->wq, msg);
1108 if (rc) {
1109 LOGP(DLMGCP, LOGL_FATAL, "Could not queue message to MGCP GW\n");
1110 msgb_free(msg);
1111 goto mgcp_tx_error;
1112 } else
Neels Hofmeyred0c1aa2018-12-21 03:07:53 +01001113 LOGP(DLMGCP, LOGL_DEBUG, "Queued %u bytes for MGCP GW\n",
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001114 msgb_l2len(msg));
1115 return 0;
1116
1117mgcp_tx_error:
Harald Welte563ffc52020-06-17 21:54:13 +07001118 if (!pending)
Vadim Yanitskiyffbc6182020-07-16 15:48:13 +07001119 return rc;
Vadim Yanitskiy3f8139c2020-06-17 20:50:17 +07001120 /* Dequeue pending response, it's going to be free()d */
1121 llist_del(&pending->entry);
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001122 /* Pass NULL to response cb to indicate an error */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +02001123 mgcp_client_handle_response(mgcp, pending, NULL);
Vadim Yanitskiyffbc6182020-07-16 15:48:13 +07001124 return rc;
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001125}
1126
Philipp Maier275ac972018-01-20 00:58:16 +01001127/*! Cancel a pending transaction.
1128 * \param[in] mgcp MGCP client descriptor.
1129 * \param[in,out] trans_id Transaction id.
1130 * \returns 0 on success, -ENOENT on error.
1131 *
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +01001132 * Should a priv pointer passed to mgcp_client_tx() become invalid, this function must be called. In
1133 * practical terms, if the caller of mgcp_client_tx() wishes to tear down a transaction without having
1134 * received a response this function must be called. The trans_id can be obtained by calling
Philipp Maier275ac972018-01-20 00:58:16 +01001135 * mgcp_msg_trans_id() on the msgb produced by mgcp_msg_gen(). */
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +01001136int mgcp_client_cancel(struct mgcp_client *mgcp, mgcp_trans_id_t trans_id)
1137{
1138 struct mgcp_response_pending *pending = mgcp_client_response_pending_get(mgcp, trans_id);
1139 if (!pending) {
Philipp Maier275ac972018-01-20 00:58:16 +01001140 /*! Note: it is not harmful to cancel a transaction twice. */
Neels Hofmeyred0c1aa2018-12-21 03:07:53 +01001141 LOGP(DLMGCP, LOGL_ERROR, "Cannot cancel, no such transaction: %u\n", trans_id);
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +01001142 return -ENOENT;
1143 }
Neels Hofmeyred0c1aa2018-12-21 03:07:53 +01001144 LOGP(DLMGCP, LOGL_DEBUG, "Canceled transaction %u\n", trans_id);
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +01001145 talloc_free(pending);
1146 return 0;
Philipp Maier275ac972018-01-20 00:58:16 +01001147 /*! We don't really need to clean up the wqueue: In all sane cases, the msgb has already been sent
1148 * out and is no longer in the wqueue. If it still is in the wqueue, then sending MGCP messages
1149 * per se is broken and the program should notice so by a full wqueue. Even if this was called
1150 * before we had a chance to send out the message and it is still going to be sent, we will just
1151 * ignore the reply to it later. Removing a msgb from the wqueue here would just introduce more
1152 * bug surface in terms of failing to update wqueue API's counters or some such.
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +01001153 */
1154}
1155
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +02001156static mgcp_trans_id_t mgcp_client_next_trans_id(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001157{
1158 /* avoid zero trans_id to distinguish from unset trans_id */
1159 if (!mgcp->next_trans_id)
1160 mgcp->next_trans_id ++;
1161 return mgcp->next_trans_id ++;
1162}
1163
Philipp Maier1dc6be62017-10-05 18:25:37 +02001164#define MGCP_CRCX_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT | \
1165 MGCP_MSG_PRESENCE_CALL_ID | \
Philipp Maier1dc6be62017-10-05 18:25:37 +02001166 MGCP_MSG_PRESENCE_CONN_MODE)
1167#define MGCP_MDCX_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT | \
Philipp Maier490cbaa2018-01-22 17:32:38 +01001168 MGCP_MSG_PRESENCE_CALL_ID | \
Philipp Maier1dc6be62017-10-05 18:25:37 +02001169 MGCP_MSG_PRESENCE_CONN_ID)
1170#define MGCP_DLCX_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT)
1171#define MGCP_AUEP_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT)
1172#define MGCP_RSIP_MANDATORY 0 /* none */
1173
Philipp Maier704c4f02018-06-07 18:51:31 +02001174/* Helper function for mgcp_msg_gen(): Add LCO information to MGCP message */
1175static int add_lco(struct msgb *msg, struct mgcp_msg *mgcp_msg)
1176{
1177 unsigned int i;
1178 int rc = 0;
1179 const char *codec;
1180 unsigned int pt;
1181
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001182 rc |= msgb_printf(msg, "L:");
Philipp Maier704c4f02018-06-07 18:51:31 +02001183
1184 if (mgcp_msg->ptime)
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001185 rc |= msgb_printf(msg, " p:%u,", mgcp_msg->ptime);
Philipp Maier704c4f02018-06-07 18:51:31 +02001186
1187 if (mgcp_msg->codecs_len) {
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001188 rc |= msgb_printf(msg, " a:");
Philipp Maier704c4f02018-06-07 18:51:31 +02001189 for (i = 0; i < mgcp_msg->codecs_len; i++) {
1190 pt = mgcp_msg->codecs[i];
Neels Hofmeyr47642f22019-02-27 05:56:53 +01001191 codec = get_value_string_or_null(osmo_mgcpc_codec_names, pt);
Pau Espin Pedrolc12bfb72019-04-16 17:23:09 +02001192
Philipp Maier704c4f02018-06-07 18:51:31 +02001193 /* Note: Use codec descriptors from enum mgcp_codecs
1194 * in mgcp_client only! */
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001195 if (!codec) {
1196 msgb_free(msg);
1197 return -EINVAL;
1198 }
1199
1200 rc |= msgb_printf(msg, "%s", extract_codec_name(codec));
Philipp Maier704c4f02018-06-07 18:51:31 +02001201 if (i < mgcp_msg->codecs_len - 1)
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001202 rc |= msgb_printf(msg, ";");
Philipp Maier704c4f02018-06-07 18:51:31 +02001203 }
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001204 rc |= msgb_printf(msg, ",");
Philipp Maier704c4f02018-06-07 18:51:31 +02001205 }
1206
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001207 rc |= msgb_printf(msg, " nt:IN\r\n");
Philipp Maier704c4f02018-06-07 18:51:31 +02001208
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001209 if (rc != 0) {
1210 LOGP(DLMGCP, LOGL_ERROR,
1211 "message buffer to small, can not generate MGCP message (LCO)\n");
1212 msgb_free(msg);
1213 return -ENOBUFS;
1214 }
1215 return 0;
Philipp Maier704c4f02018-06-07 18:51:31 +02001216}
1217
1218/* Helper function for mgcp_msg_gen(): Add SDP information to MGCP message */
1219static int add_sdp(struct msgb *msg, struct mgcp_msg *mgcp_msg, struct mgcp_client *mgcp)
1220{
1221 unsigned int i;
1222 int rc = 0;
Pau Espin Pedrol8667d512020-08-28 19:37:08 +02001223 char local_ip[INET6_ADDRSTRLEN];
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +02001224 int local_ip_family, audio_ip_family;
Philipp Maier704c4f02018-06-07 18:51:31 +02001225 const char *codec;
1226 unsigned int pt;
1227
1228 /* Add separator to mark the beginning of the SDP block */
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001229 rc |= msgb_printf(msg, "\r\n");
Philipp Maier704c4f02018-06-07 18:51:31 +02001230
1231 /* Add SDP protocol version */
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001232 rc |= msgb_printf(msg, "v=0\r\n");
Philipp Maier704c4f02018-06-07 18:51:31 +02001233
1234 /* Determine local IP-Address */
1235 if (osmo_sock_local_ip(local_ip, mgcp->actual.remote_addr) < 0) {
1236 LOGP(DLMGCP, LOGL_ERROR,
1237 "Could not determine local IP-Address!\n");
1238 msgb_free(msg);
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001239 return -EINVAL;
Philipp Maier704c4f02018-06-07 18:51:31 +02001240 }
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +02001241 local_ip_family = osmo_ip_str_type(local_ip);
1242 if (local_ip_family == AF_UNSPEC) {
1243 msgb_free(msg);
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001244 return -EINVAL;
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +02001245 }
1246 audio_ip_family = osmo_ip_str_type(mgcp_msg->audio_ip);
1247 if (audio_ip_family == AF_UNSPEC) {
1248 msgb_free(msg);
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001249 return -EINVAL;
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +02001250 }
Philipp Maier704c4f02018-06-07 18:51:31 +02001251
1252 /* Add owner/creator (SDP) */
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001253 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 +02001254 local_ip_family == AF_INET6 ? '6' : '4',
1255 local_ip);
Philipp Maier704c4f02018-06-07 18:51:31 +02001256
1257 /* Add session name (none) */
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001258 rc |= msgb_printf(msg, "s=-\r\n");
Philipp Maier704c4f02018-06-07 18:51:31 +02001259
1260 /* Add RTP address and port */
1261 if (mgcp_msg->audio_port == 0) {
1262 LOGP(DLMGCP, LOGL_ERROR,
1263 "Invalid port number, can not generate MGCP message\n");
1264 msgb_free(msg);
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001265 return -EINVAL;
Philipp Maier704c4f02018-06-07 18:51:31 +02001266 }
1267 if (strlen(mgcp_msg->audio_ip) <= 0) {
1268 LOGP(DLMGCP, LOGL_ERROR,
1269 "Empty ip address, can not generate MGCP message\n");
1270 msgb_free(msg);
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001271 return -EINVAL;
Philipp Maier704c4f02018-06-07 18:51:31 +02001272 }
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001273 rc |= msgb_printf(msg, "c=IN IP%c %s\r\n",
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +02001274 audio_ip_family == AF_INET6 ? '6' : '4',
1275 mgcp_msg->audio_ip);
Philipp Maier704c4f02018-06-07 18:51:31 +02001276
1277 /* Add time description, active time (SDP) */
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001278 rc |= msgb_printf(msg, "t=0 0\r\n");
Philipp Maier704c4f02018-06-07 18:51:31 +02001279
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001280 rc |= msgb_printf(msg, "m=audio %u RTP/AVP", mgcp_msg->audio_port);
Philipp Maier704c4f02018-06-07 18:51:31 +02001281 for (i = 0; i < mgcp_msg->codecs_len; i++) {
1282 pt = map_codec_to_pt(mgcp_msg->ptmap, mgcp_msg->ptmap_len, mgcp_msg->codecs[i]);
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001283 rc |= msgb_printf(msg, " %u", pt);
Philipp Maier704c4f02018-06-07 18:51:31 +02001284
1285 }
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001286 rc |= msgb_printf(msg, "\r\n");
Philipp Maier704c4f02018-06-07 18:51:31 +02001287
Philipp Maier228e5912019-03-05 13:56:59 +01001288 /* Add optional codec parameters (fmtp) */
1289 if (mgcp_msg->param_present) {
1290 for (i = 0; i < mgcp_msg->codecs_len; i++) {
1291 /* The following is only applicable for AMR */
1292 if (mgcp_msg->codecs[i] != CODEC_AMR_8000_1 && mgcp_msg->codecs[i] != CODEC_AMRWB_16000_1)
1293 continue;
1294 pt = map_codec_to_pt(mgcp_msg->ptmap, mgcp_msg->ptmap_len, mgcp_msg->codecs[i]);
1295 if (mgcp_msg->param.amr_octet_aligned_present && mgcp_msg->param.amr_octet_aligned)
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001296 rc |= msgb_printf(msg, "a=fmtp:%u octet-align=1\r\n", pt);
Philipp Maier228e5912019-03-05 13:56:59 +01001297 else if (mgcp_msg->param.amr_octet_aligned_present && !mgcp_msg->param.amr_octet_aligned)
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001298 rc |= msgb_printf(msg, "a=fmtp:%u octet-align=0\r\n", pt);
Philipp Maier228e5912019-03-05 13:56:59 +01001299 }
1300 }
1301
Philipp Maier704c4f02018-06-07 18:51:31 +02001302 for (i = 0; i < mgcp_msg->codecs_len; i++) {
1303 pt = map_codec_to_pt(mgcp_msg->ptmap, mgcp_msg->ptmap_len, mgcp_msg->codecs[i]);
Pau Espin Pedrolc12bfb72019-04-16 17:23:09 +02001304
Philipp Maier704c4f02018-06-07 18:51:31 +02001305 /* Note: Only dynamic payload type from the range 96-127
1306 * require to be explained further via rtpmap. All others
1307 * are implcitly definedby the number in m=audio */
1308 if (pt >= 96 && pt <= 127) {
Neels Hofmeyr47642f22019-02-27 05:56:53 +01001309 codec = get_value_string_or_null(osmo_mgcpc_codec_names, mgcp_msg->codecs[i]);
Philipp Maier704c4f02018-06-07 18:51:31 +02001310
1311 /* Note: Use codec descriptors from enum mgcp_codecs
1312 * in mgcp_client only! */
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001313 if (!codec) {
1314 msgb_free(msg);
1315 return -EINVAL;
1316 }
Pau Espin Pedrolc12bfb72019-04-16 17:23:09 +02001317
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001318 rc |= msgb_printf(msg, "a=rtpmap:%u %s\r\n", pt, codec);
Philipp Maier704c4f02018-06-07 18:51:31 +02001319 }
1320 }
Pau Espin Pedrolc12bfb72019-04-16 17:23:09 +02001321
Philipp Maier704c4f02018-06-07 18:51:31 +02001322 if (mgcp_msg->ptime)
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001323 rc |= msgb_printf(msg, "a=ptime:%u\r\n", mgcp_msg->ptime);
Philipp Maier704c4f02018-06-07 18:51:31 +02001324
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001325 if (rc != 0) {
1326 LOGP(DLMGCP, LOGL_ERROR,
1327 "message buffer to small, can not generate MGCP message (SDP)\n");
1328 msgb_free(msg);
1329 return -ENOBUFS;
1330 }
1331
1332 return 0;
Philipp Maier704c4f02018-06-07 18:51:31 +02001333}
1334
Philipp Maier275ac972018-01-20 00:58:16 +01001335/*! Generate an MGCP message
1336 * \param[in] mgcp MGCP client descriptor.
1337 * \param[in] mgcp_msg Message description
1338 * \returns message buffer on success, NULL on error. */
Philipp Maier1dc6be62017-10-05 18:25:37 +02001339struct msgb *mgcp_msg_gen(struct mgcp_client *mgcp, struct mgcp_msg *mgcp_msg)
1340{
1341 mgcp_trans_id_t trans_id = mgcp_client_next_trans_id(mgcp);
1342 uint32_t mandatory_mask;
1343 struct msgb *msg = msgb_alloc_headroom(4096, 128, "MGCP tx");
1344 int rc = 0;
Philipp Maier704c4f02018-06-07 18:51:31 +02001345 bool use_sdp = false;
Pau Espin Pedrol900cd652019-04-24 22:06:22 +02001346 char buf[32];
Philipp Maier1dc6be62017-10-05 18:25:37 +02001347
1348 msg->l2h = msg->data;
1349 msg->cb[MSGB_CB_MGCP_TRANS_ID] = trans_id;
1350
1351 /* Add command verb */
1352 switch (mgcp_msg->verb) {
1353 case MGCP_VERB_CRCX:
1354 mandatory_mask = MGCP_CRCX_MANDATORY;
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001355 rc |= msgb_printf(msg, "CRCX %u", trans_id);
Philipp Maier1dc6be62017-10-05 18:25:37 +02001356 break;
1357 case MGCP_VERB_MDCX:
1358 mandatory_mask = MGCP_MDCX_MANDATORY;
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001359 rc |= msgb_printf(msg, "MDCX %u", trans_id);
Philipp Maier1dc6be62017-10-05 18:25:37 +02001360 break;
1361 case MGCP_VERB_DLCX:
1362 mandatory_mask = MGCP_DLCX_MANDATORY;
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001363 rc |= msgb_printf(msg, "DLCX %u", trans_id);
Philipp Maier1dc6be62017-10-05 18:25:37 +02001364 break;
1365 case MGCP_VERB_AUEP:
1366 mandatory_mask = MGCP_AUEP_MANDATORY;
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001367 rc |= msgb_printf(msg, "AUEP %u", trans_id);
Philipp Maier1dc6be62017-10-05 18:25:37 +02001368 break;
1369 case MGCP_VERB_RSIP:
1370 mandatory_mask = MGCP_RSIP_MANDATORY;
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001371 rc |= msgb_printf(msg, "RSIP %u", trans_id);
Philipp Maier1dc6be62017-10-05 18:25:37 +02001372 break;
1373 default:
1374 LOGP(DLMGCP, LOGL_ERROR,
1375 "Invalid command verb, can not generate MGCP message\n");
1376 msgb_free(msg);
1377 return NULL;
1378 }
1379
1380 /* Check if mandatory fields are missing */
1381 if (!((mgcp_msg->presence & mandatory_mask) == mandatory_mask)) {
1382 LOGP(DLMGCP, LOGL_ERROR,
1383 "One or more missing mandatory fields, can not generate MGCP message\n");
1384 msgb_free(msg);
1385 return NULL;
1386 }
1387
1388 /* Add endpoint name */
Philipp Maier7bc55522017-12-10 22:52:22 +01001389 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_ENDPOINT) {
1390 if (strlen(mgcp_msg->endpoint) <= 0) {
1391 LOGP(DLMGCP, LOGL_ERROR,
1392 "Empty endpoint name, can not generate MGCP message\n");
1393 msgb_free(msg);
1394 return NULL;
1395 }
Philipp Maier3aa81572018-01-31 14:13:45 +01001396
1397 if (strstr(mgcp_msg->endpoint, "@") == NULL) {
1398 LOGP(DLMGCP, LOGL_ERROR,
1399 "Endpoint name (%s) lacks separator (@), can not generate MGCP message\n",
1400 mgcp_msg->endpoint);
1401 msgb_free(msg);
Vadim Yanitskiye6743452020-06-18 03:04:13 +07001402 return NULL;
Philipp Maier3aa81572018-01-31 14:13:45 +01001403 }
1404
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001405 rc |= msgb_printf(msg, " %s", mgcp_msg->endpoint);
Philipp Maier7bc55522017-12-10 22:52:22 +01001406 }
Philipp Maier1dc6be62017-10-05 18:25:37 +02001407
1408 /* Add protocol version */
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001409 rc |= msgb_printf(msg, " MGCP 1.0\r\n");
Philipp Maier1dc6be62017-10-05 18:25:37 +02001410
1411 /* Add call id */
1412 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_CALL_ID)
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001413 rc |= msgb_printf(msg, "C: %x\r\n", mgcp_msg->call_id);
Philipp Maier1dc6be62017-10-05 18:25:37 +02001414
1415 /* Add connection id */
Philipp Maier7bc55522017-12-10 22:52:22 +01001416 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_CONN_ID) {
1417 if (strlen(mgcp_msg->conn_id) <= 0) {
1418 LOGP(DLMGCP, LOGL_ERROR,
1419 "Empty connection id, can not generate MGCP message\n");
1420 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) {
1456 LOGP(DLMGCP, LOGL_ERROR,
1457 "Wrong Osmux CID %d, can not generate MGCP message\n",
1458 mgcp_msg->x_osmo_osmux_cid);
1459 msgb_free(msg);
1460 return NULL;
1461 }
Pau Espin Pedrol900cd652019-04-24 22:06:22 +02001462 snprintf(buf, sizeof(buf), " %d", mgcp_msg->x_osmo_osmux_cid);
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001463 rc |=
Pau Espin Pedrol900cd652019-04-24 22:06:22 +02001464 msgb_printf(msg, MGCP_X_OSMO_OSMUX_HEADER "%s\r\n",
1465 mgcp_msg->x_osmo_osmux_cid == -1 ? " *": buf);
1466 }
1467
1468
Philipp Maier704c4f02018-06-07 18:51:31 +02001469 /* Add session description protocol (SDP) */
1470 if (use_sdp
1471 && (mgcp_msg->verb == MGCP_VERB_CRCX
1472 || mgcp_msg->verb == MGCP_VERB_MDCX)) {
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001473 if (add_sdp(msg, mgcp_msg, mgcp) < 0)
Philipp Maier9d25d7a2018-01-22 17:31:10 +01001474 return NULL;
Philipp Maier1dc6be62017-10-05 18:25:37 +02001475 }
1476
1477 if (rc != 0) {
1478 LOGP(DLMGCP, LOGL_ERROR,
1479 "message buffer to small, can not generate MGCP message\n");
1480 msgb_free(msg);
1481 msg = NULL;
1482 }
1483
1484 return msg;
1485}
1486
Philipp Maier275ac972018-01-20 00:58:16 +01001487/*! Retrieve the MGCP transaction ID from a msgb generated by mgcp_msg_gen()
1488 * \param[in] msg message buffer
1489 * \returns Transaction id. */
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +01001490mgcp_trans_id_t mgcp_msg_trans_id(struct msgb *msg)
1491{
1492 return (mgcp_trans_id_t)msg->cb[MSGB_CB_MGCP_TRANS_ID];
1493}
1494
Philipp Maierfa495d62021-09-02 10:08:56 +02001495/*! Get the configuration parameters for a given MGCP client instance
Philipp Maier275ac972018-01-20 00:58:16 +01001496 * \param[in] mgcp MGCP client descriptor.
1497 * \returns configuration */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +02001498struct mgcp_client_conf *mgcp_client_conf_actual(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001499{
1500 return &mgcp->actual;
1501}
Neels Hofmeyrd95ab1e2017-09-22 00:52:54 +02001502
1503const struct value_string mgcp_client_connection_mode_strs[] = {
1504 { MGCP_CONN_NONE, "none" },
1505 { MGCP_CONN_RECV_SEND, "sendrecv" },
1506 { MGCP_CONN_SEND_ONLY, "sendonly" },
1507 { MGCP_CONN_RECV_ONLY, "recvonly" },
1508 { MGCP_CONN_LOOPBACK, "loopback" },
1509 { 0, NULL }
1510};