blob: 225b1eee32148e3707a4d2b7ccb2bb0c7231d818 [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 Maier6a26c162018-08-01 16:37:06 +0200799static int init_socket(struct mgcp_client *mgcp)
800{
801 int rc;
802 struct osmo_wqueue *wq;
803 int i;
804
805 wq = &mgcp->wq;
806
807 for (i = 0; i < 100; i++) {
808
809 /* Initalize socket with the currently configured port
810 * number */
Pau Espin Pedrol531470a2020-08-31 17:06:55 +0200811 rc = osmo_sock_init2_ofd(&wq->bfd, AF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, mgcp->actual.local_addr,
Philipp Maier6a26c162018-08-01 16:37:06 +0200812 mgcp->actual.local_port, mgcp->actual.remote_addr, mgcp->actual.remote_port,
813 OSMO_SOCK_F_BIND | OSMO_SOCK_F_CONNECT);
814 if (rc > 0)
815 return rc;
816
817 /* If there is a different port than the default port
818 * configured then we assume that the user has choosen
819 * that port conciously and we will not try to resolve
820 * this by silently choosing a different port. */
Philipp Maier9a7ccc32018-08-09 11:41:19 +0200821 if (mgcp->actual.local_port != MGCP_CLIENT_LOCAL_PORT_DEFAULT && i == 0)
Philipp Maier6a26c162018-08-01 16:37:06 +0200822 return -EINVAL;
823
824 /* Choose a new port number to try next */
825 LOGP(DLMGCP, LOGL_NOTICE,
Neels Hofmeyr0a403792018-12-12 03:10:18 +0100826 "MGCPGW failed to bind to %s:%u, retrying with port %u\n",
Philipp Maier276a4142021-07-29 11:55:14 +0200827 mgcp->actual.local_addr ? mgcp->actual.local_addr : "(any)", mgcp->actual.local_port,
828 mgcp->actual.local_port + 1);
Philipp Maier6a26c162018-08-01 16:37:06 +0200829 mgcp->actual.local_port++;
830 }
831
Neels Hofmeyr0a403792018-12-12 03:10:18 +0100832 LOGP(DLMGCP, LOGL_FATAL, "MGCPGW failed to find a port to bind on %i times.\n", i);
Philipp Maier6a26c162018-08-01 16:37:06 +0200833 return -EINVAL;
834}
835
Philipp Maier3f2c15f2021-07-22 11:53:07 +0200836/* Safely ignore the MGCP response to the DLCX sent via _mgcp_client_send_dlcx() */
837static void _ignore_mgcp_response(struct mgcp_response *response, void *priv) { }
838
839/* Format DLCX message (fire and forget) and send it off to the MGW */
840static void _mgcp_client_send_dlcx(struct mgcp_client *mgcp, const char *epname)
841{
842 struct msgb *msgb_dlcx;
843 struct mgcp_msg mgcp_msg_dlcx = {
844 .verb = MGCP_VERB_DLCX,
845 .presence = MGCP_MSG_PRESENCE_ENDPOINT,
846 };
847 osmo_strlcpy(mgcp_msg_dlcx.endpoint, epname, sizeof(mgcp_msg_dlcx.endpoint));
848 msgb_dlcx = mgcp_msg_gen(mgcp, &mgcp_msg_dlcx);
849 mgcp_client_tx(mgcp, msgb_dlcx, &_ignore_mgcp_response, NULL);
850}
851
852static const char *_mgcp_client_name_append_domain(const struct mgcp_client *mgcp, const char *name)
853{
854 static char endpoint[MGCP_ENDPOINT_MAXLEN];
855 int rc;
856
857 rc = snprintf(endpoint, sizeof(endpoint), "%s@%s", name, mgcp_client_endpoint_domain(mgcp));
858 if (rc > sizeof(endpoint) - 1) {
859 LOGP(DLMGCP, LOGL_ERROR, "MGCP endpoint exceeds maximum length of %zu: '%s@%s'\n",
860 sizeof(endpoint) - 1, name, mgcp_client_endpoint_domain(mgcp));
861 return NULL;
862 }
863 if (rc < 1) {
864 LOGP(DLMGCP, LOGL_ERROR, "Cannot compose MGCP endpoint name\n");
865 return NULL;
866 }
867 return endpoint;
868}
869
870/*! Initialize client connection (opens socket)
Philipp Maier275ac972018-01-20 00:58:16 +0100871 * \param[in,out] mgcp MGCP client descriptor.
872 * \returns 0 on success, -EINVAL on error. */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200873int mgcp_client_connect(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200874{
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200875 struct osmo_wqueue *wq;
876 int rc;
Philipp Maier3f2c15f2021-07-22 11:53:07 +0200877 struct reset_ep *reset_ep;
878 const char *epname;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200879
880 if (!mgcp) {
881 LOGP(DLMGCP, LOGL_FATAL, "MGCPGW client not initialized properly\n");
882 return -EINVAL;
883 }
884
885 wq = &mgcp->wq;
Harald Weltec2a8f592020-10-19 13:25:41 +0200886 osmo_wqueue_init(wq, 1024);
887 wq->read_cb = mgcp_do_read;
888 wq->write_cb = mgcp_do_write;
889
890 osmo_fd_setup(&wq->bfd, -1, OSMO_FD_READ, osmo_wqueue_bfd_cb, mgcp, 0);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200891
Philipp Maier6a26c162018-08-01 16:37:06 +0200892 rc = init_socket(mgcp);
Harald Welte8890dfa2017-11-17 15:09:30 +0100893 if (rc < 0) {
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200894 LOGP(DLMGCP, LOGL_FATAL,
Harald Welte8890dfa2017-11-17 15:09:30 +0100895 "Failed to initialize socket %s:%u -> %s:%u for MGCP GW: %s\n",
Philipp Maier276a4142021-07-29 11:55:14 +0200896 mgcp->actual.local_addr ? mgcp->actual.local_addr : "(any)", mgcp->actual.local_port,
897 mgcp->actual.remote_addr ? mgcp->actual.local_addr : "(any)", mgcp->actual.remote_port,
898 strerror(errno));
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200899 goto error_close_fd;
900 }
901
Neels Hofmeyr0a403792018-12-12 03:10:18 +0100902 LOGP(DLMGCP, LOGL_INFO, "MGCP GW connection: %s\n", osmo_sock_get_name2(wq->bfd.fd));
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200903
Philipp Maier3f2c15f2021-07-22 11:53:07 +0200904 /* If configured, send a DLCX message to the endpoints that are configured to
905 * be reset on startup. Usually this is a wildcarded endpoint. */
906 llist_for_each_entry(reset_ep, &mgcp->actual.reset_epnames, list) {
907 epname = _mgcp_client_name_append_domain(mgcp, reset_ep->name);
908 LOGP(DLMGCP, LOGL_INFO, "MGCP GW sending DLCX to: %s\n", epname);
909 _mgcp_client_send_dlcx(mgcp, epname);
910 }
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200911 return 0;
912error_close_fd:
913 close(wq->bfd.fd);
914 wq->bfd.fd = -1;
915 return rc;
916}
917
Philipp Maier275ac972018-01-20 00:58:16 +0100918/*! Get the IP-Aaddress of the associated MGW as string.
919 * \param[in] mgcp MGCP client descriptor.
920 * \returns a pointer to the address string. */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200921const char *mgcp_client_remote_addr_str(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200922{
923 return mgcp->actual.remote_addr;
924}
925
Philipp Maier275ac972018-01-20 00:58:16 +0100926/*! Get the IP-Port of the associated MGW.
927 * \param[in] mgcp MGCP client descriptor.
928 * \returns port number. */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200929uint16_t mgcp_client_remote_port(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200930{
931 return mgcp->actual.remote_port;
932}
933
Pau Espin Pedrol74d0e5c2020-09-02 17:19:20 +0200934/*! Get the IP-Address of the associated MGW as its numeric representation.
935 * DEPRECATED, DON'T USE.
Philipp Maier275ac972018-01-20 00:58:16 +0100936 * \param[in] mgcp MGCP client descriptor.
937 * \returns IP-Address as 32 bit integer (network byte order) */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200938uint32_t mgcp_client_remote_addr_n(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200939{
Pau Espin Pedrol74d0e5c2020-09-02 17:19:20 +0200940 return 0;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200941}
942
Neels Hofmeyrac69ea92018-12-19 00:27:50 +0100943/* To compose endpoint names, usually for CRCX, use this as domain name.
944 * For example, snprintf("rtpbridge\*@%s", mgcp_client_endpoint_domain(mgcp)). */
945const char *mgcp_client_endpoint_domain(const struct mgcp_client *mgcp)
946{
947 return mgcp->actual.endpoint_domain_name[0] ? mgcp->actual.endpoint_domain_name : "mgw";
948}
949
Philipp Maiera466f572020-06-25 16:11:04 +0200950/*! Compose endpoint name for a wildcarded request to the virtual trunk
951 * \param[in] mgcp MGCP client descriptor.
952 * \returns string containing the endpoint name (e.g. rtpbridge\*@mgw) */
Pau Espin Pedrolca0818c2019-04-16 17:23:38 +0200953const char *mgcp_client_rtpbridge_wildcard(const struct mgcp_client *mgcp)
954{
955 return _mgcp_client_name_append_domain(mgcp, "rtpbridge/*");
956}
957
Philipp Maier48635912020-06-25 20:16:22 +0200958/*! Compose endpoint name for an E1 endpoint.
959 * \param[in] ctx talloc context.
960 * \param[in] mgcp MGCP client descriptor.
961 * \param[in] trunk_id id number of the E1 trunk (1-64).
962 * \param[in] ts timeslot on the E1 trunk (1-31).
963 * \param[in] rate bitrate used on the E1 trunk (e.g 16 for 16kbit).
964 * \param[in] offset bit offset of the E1 subslot (e.g. 4 for the third 16k subslot).
965 * \returns string containing the endpoint name (e.g. ds/e1-1/s-1/su16-4). */
966const char *mgcp_client_e1_epname(void *ctx, const struct mgcp_client *mgcp, uint8_t trunk_id, uint8_t ts,
967 uint8_t rate, uint8_t offset)
968{
969 /* See also comment in libosmo-mgcp, mgcp_client.c, gen_e1_epname() */
970 const uint8_t valid_rates[] = { 64, 32, 32, 16, 16, 16, 16, 8, 8, 8, 8, 8, 8, 8, 8 };
971 const uint8_t valid_offsets[] = { 0, 0, 4, 0, 2, 4, 6, 0, 1, 2, 3, 4, 5, 6, 7 };
972
973 uint8_t i;
974 bool rate_offs_valid = false;
975 char *epname;
976
977 epname =
978 talloc_asprintf(ctx, "ds/e1-%u/s-%u/su%u-%u@%s", trunk_id, ts, rate, offset,
979 mgcp_client_endpoint_domain(mgcp));
980 if (!epname) {
981 LOGP(DLMGCP, LOGL_ERROR, "Cannot compose MGCP e1-endpoint name!\n");
982 return NULL;
983 }
984
985 /* Check if the supplied rate/offset pair resembles a valid combination */
986 for (i = 0; i < sizeof(valid_rates); i++) {
987 if (valid_rates[i] == rate && valid_offsets[i] == offset)
988 rate_offs_valid = true;
989 }
990 if (!rate_offs_valid) {
991 LOGP(DLMGCP, LOGL_ERROR,
992 "Cannot compose MGCP e1-endpoint name (%s), rate(%u)/offset(%u) combination is invalid!\n", epname,
993 rate, offset);
994 talloc_free(epname);
995 return NULL;
996 }
997
998 /* An E1 line has a maximum of 32 timeslots, while the first (ts=0) is
999 * reserverd for framing and alignment, so we can not use it here. */
Philipp Maier92a73cd2020-11-26 22:21:11 +01001000 if (ts == 0 || ts > NUM_E1_TS-1) {
Philipp Maier48635912020-06-25 20:16:22 +02001001 LOGP(DLMGCP, LOGL_ERROR,
1002 "Cannot compose MGCP e1-endpoint name (%s), E1-timeslot number (%u) is invalid!\n", epname, ts);
1003 talloc_free(epname);
1004 return NULL;
1005 }
1006
1007 return epname;
1008}
1009
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +02001010struct mgcp_response_pending * mgcp_client_pending_add(
1011 struct mgcp_client *mgcp,
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001012 mgcp_trans_id_t trans_id,
1013 mgcp_response_cb_t response_cb,
1014 void *priv)
1015{
1016 struct mgcp_response_pending *pending;
1017
1018 pending = talloc_zero(mgcp, struct mgcp_response_pending);
Harald Welte827da2c2020-07-15 10:57:08 +02001019 if (!pending)
1020 return NULL;
1021
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001022 pending->trans_id = trans_id;
1023 pending->response_cb = response_cb;
1024 pending->priv = priv;
1025 llist_add_tail(&pending->entry, &mgcp->responses_pending);
1026
1027 return pending;
1028}
1029
1030/* Send the MGCP message in msg to the MGCP GW and handle a response with
1031 * response_cb. NOTE: the response_cb still needs to call
1032 * mgcp_response_parse_params(response) to get the parsed parameters -- to
1033 * potentially save some CPU cycles, only the head line has been parsed when
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +01001034 * the response_cb is invoked.
1035 * Before the priv pointer becomes invalid, e.g. due to transaction timeout,
1036 * mgcp_client_cancel() needs to be called for this transaction.
1037 */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +02001038int mgcp_client_tx(struct mgcp_client *mgcp, struct msgb *msg,
1039 mgcp_response_cb_t response_cb, void *priv)
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001040{
Harald Welte563ffc52020-06-17 21:54:13 +07001041 struct mgcp_response_pending *pending = NULL;
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001042 mgcp_trans_id_t trans_id;
1043 int rc;
1044
1045 trans_id = msg->cb[MSGB_CB_MGCP_TRANS_ID];
1046 if (!trans_id) {
1047 LOGP(DLMGCP, LOGL_ERROR,
1048 "Unset transaction id in mgcp send request\n");
1049 talloc_free(msg);
1050 return -EINVAL;
1051 }
1052
Harald Welte563ffc52020-06-17 21:54:13 +07001053 /* Do not allocate a dummy 'mgcp_response_pending' entry */
1054 if (response_cb != NULL) {
1055 pending = mgcp_client_pending_add(mgcp, trans_id, response_cb, priv);
1056 if (!pending) {
1057 talloc_free(msg);
1058 return -ENOMEM;
1059 }
Harald Welte827da2c2020-07-15 10:57:08 +02001060 }
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001061
1062 if (msgb_l2len(msg) > 4096) {
1063 LOGP(DLMGCP, LOGL_ERROR,
1064 "Cannot send, MGCP message too large: %u\n",
1065 msgb_l2len(msg));
1066 msgb_free(msg);
1067 rc = -EINVAL;
1068 goto mgcp_tx_error;
1069 }
1070
1071 rc = osmo_wqueue_enqueue(&mgcp->wq, msg);
1072 if (rc) {
1073 LOGP(DLMGCP, LOGL_FATAL, "Could not queue message to MGCP GW\n");
1074 msgb_free(msg);
1075 goto mgcp_tx_error;
1076 } else
Neels Hofmeyred0c1aa2018-12-21 03:07:53 +01001077 LOGP(DLMGCP, LOGL_DEBUG, "Queued %u bytes for MGCP GW\n",
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001078 msgb_l2len(msg));
1079 return 0;
1080
1081mgcp_tx_error:
Harald Welte563ffc52020-06-17 21:54:13 +07001082 if (!pending)
Vadim Yanitskiyffbc6182020-07-16 15:48:13 +07001083 return rc;
Vadim Yanitskiy3f8139c2020-06-17 20:50:17 +07001084 /* Dequeue pending response, it's going to be free()d */
1085 llist_del(&pending->entry);
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001086 /* Pass NULL to response cb to indicate an error */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +02001087 mgcp_client_handle_response(mgcp, pending, NULL);
Vadim Yanitskiyffbc6182020-07-16 15:48:13 +07001088 return rc;
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001089}
1090
Philipp Maier275ac972018-01-20 00:58:16 +01001091/*! Cancel a pending transaction.
1092 * \param[in] mgcp MGCP client descriptor.
1093 * \param[in,out] trans_id Transaction id.
1094 * \returns 0 on success, -ENOENT on error.
1095 *
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +01001096 * Should a priv pointer passed to mgcp_client_tx() become invalid, this function must be called. In
1097 * practical terms, if the caller of mgcp_client_tx() wishes to tear down a transaction without having
1098 * received a response this function must be called. The trans_id can be obtained by calling
Philipp Maier275ac972018-01-20 00:58:16 +01001099 * mgcp_msg_trans_id() on the msgb produced by mgcp_msg_gen(). */
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +01001100int mgcp_client_cancel(struct mgcp_client *mgcp, mgcp_trans_id_t trans_id)
1101{
1102 struct mgcp_response_pending *pending = mgcp_client_response_pending_get(mgcp, trans_id);
1103 if (!pending) {
Philipp Maier275ac972018-01-20 00:58:16 +01001104 /*! Note: it is not harmful to cancel a transaction twice. */
Neels Hofmeyred0c1aa2018-12-21 03:07:53 +01001105 LOGP(DLMGCP, LOGL_ERROR, "Cannot cancel, no such transaction: %u\n", trans_id);
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +01001106 return -ENOENT;
1107 }
Neels Hofmeyred0c1aa2018-12-21 03:07:53 +01001108 LOGP(DLMGCP, LOGL_DEBUG, "Canceled transaction %u\n", trans_id);
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +01001109 talloc_free(pending);
1110 return 0;
Philipp Maier275ac972018-01-20 00:58:16 +01001111 /*! We don't really need to clean up the wqueue: In all sane cases, the msgb has already been sent
1112 * out and is no longer in the wqueue. If it still is in the wqueue, then sending MGCP messages
1113 * per se is broken and the program should notice so by a full wqueue. Even if this was called
1114 * before we had a chance to send out the message and it is still going to be sent, we will just
1115 * ignore the reply to it later. Removing a msgb from the wqueue here would just introduce more
1116 * bug surface in terms of failing to update wqueue API's counters or some such.
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +01001117 */
1118}
1119
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +02001120static mgcp_trans_id_t mgcp_client_next_trans_id(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001121{
1122 /* avoid zero trans_id to distinguish from unset trans_id */
1123 if (!mgcp->next_trans_id)
1124 mgcp->next_trans_id ++;
1125 return mgcp->next_trans_id ++;
1126}
1127
Philipp Maier1dc6be62017-10-05 18:25:37 +02001128#define MGCP_CRCX_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT | \
1129 MGCP_MSG_PRESENCE_CALL_ID | \
Philipp Maier1dc6be62017-10-05 18:25:37 +02001130 MGCP_MSG_PRESENCE_CONN_MODE)
1131#define MGCP_MDCX_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT | \
Philipp Maier490cbaa2018-01-22 17:32:38 +01001132 MGCP_MSG_PRESENCE_CALL_ID | \
Philipp Maier1dc6be62017-10-05 18:25:37 +02001133 MGCP_MSG_PRESENCE_CONN_ID)
1134#define MGCP_DLCX_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT)
1135#define MGCP_AUEP_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT)
1136#define MGCP_RSIP_MANDATORY 0 /* none */
1137
Philipp Maier704c4f02018-06-07 18:51:31 +02001138/* Helper function for mgcp_msg_gen(): Add LCO information to MGCP message */
1139static int add_lco(struct msgb *msg, struct mgcp_msg *mgcp_msg)
1140{
1141 unsigned int i;
1142 int rc = 0;
1143 const char *codec;
1144 unsigned int pt;
1145
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001146 rc |= msgb_printf(msg, "L:");
Philipp Maier704c4f02018-06-07 18:51:31 +02001147
1148 if (mgcp_msg->ptime)
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001149 rc |= msgb_printf(msg, " p:%u,", mgcp_msg->ptime);
Philipp Maier704c4f02018-06-07 18:51:31 +02001150
1151 if (mgcp_msg->codecs_len) {
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001152 rc |= msgb_printf(msg, " a:");
Philipp Maier704c4f02018-06-07 18:51:31 +02001153 for (i = 0; i < mgcp_msg->codecs_len; i++) {
1154 pt = mgcp_msg->codecs[i];
Neels Hofmeyr47642f22019-02-27 05:56:53 +01001155 codec = get_value_string_or_null(osmo_mgcpc_codec_names, pt);
Pau Espin Pedrolc12bfb72019-04-16 17:23:09 +02001156
Philipp Maier704c4f02018-06-07 18:51:31 +02001157 /* Note: Use codec descriptors from enum mgcp_codecs
1158 * in mgcp_client only! */
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001159 if (!codec) {
1160 msgb_free(msg);
1161 return -EINVAL;
1162 }
1163
1164 rc |= msgb_printf(msg, "%s", extract_codec_name(codec));
Philipp Maier704c4f02018-06-07 18:51:31 +02001165 if (i < mgcp_msg->codecs_len - 1)
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001166 rc |= msgb_printf(msg, ";");
Philipp Maier704c4f02018-06-07 18:51:31 +02001167 }
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001168 rc |= msgb_printf(msg, ",");
Philipp Maier704c4f02018-06-07 18:51:31 +02001169 }
1170
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001171 rc |= msgb_printf(msg, " nt:IN\r\n");
Philipp Maier704c4f02018-06-07 18:51:31 +02001172
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001173 if (rc != 0) {
1174 LOGP(DLMGCP, LOGL_ERROR,
1175 "message buffer to small, can not generate MGCP message (LCO)\n");
1176 msgb_free(msg);
1177 return -ENOBUFS;
1178 }
1179 return 0;
Philipp Maier704c4f02018-06-07 18:51:31 +02001180}
1181
1182/* Helper function for mgcp_msg_gen(): Add SDP information to MGCP message */
1183static int add_sdp(struct msgb *msg, struct mgcp_msg *mgcp_msg, struct mgcp_client *mgcp)
1184{
1185 unsigned int i;
1186 int rc = 0;
Pau Espin Pedrol8667d512020-08-28 19:37:08 +02001187 char local_ip[INET6_ADDRSTRLEN];
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +02001188 int local_ip_family, audio_ip_family;
Philipp Maier704c4f02018-06-07 18:51:31 +02001189 const char *codec;
1190 unsigned int pt;
1191
1192 /* Add separator to mark the beginning of the SDP block */
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001193 rc |= msgb_printf(msg, "\r\n");
Philipp Maier704c4f02018-06-07 18:51:31 +02001194
1195 /* Add SDP protocol version */
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001196 rc |= msgb_printf(msg, "v=0\r\n");
Philipp Maier704c4f02018-06-07 18:51:31 +02001197
1198 /* Determine local IP-Address */
1199 if (osmo_sock_local_ip(local_ip, mgcp->actual.remote_addr) < 0) {
1200 LOGP(DLMGCP, LOGL_ERROR,
1201 "Could not determine local IP-Address!\n");
1202 msgb_free(msg);
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001203 return -EINVAL;
Philipp Maier704c4f02018-06-07 18:51:31 +02001204 }
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +02001205 local_ip_family = osmo_ip_str_type(local_ip);
1206 if (local_ip_family == AF_UNSPEC) {
1207 msgb_free(msg);
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001208 return -EINVAL;
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +02001209 }
1210 audio_ip_family = osmo_ip_str_type(mgcp_msg->audio_ip);
1211 if (audio_ip_family == AF_UNSPEC) {
1212 msgb_free(msg);
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001213 return -EINVAL;
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +02001214 }
Philipp Maier704c4f02018-06-07 18:51:31 +02001215
1216 /* Add owner/creator (SDP) */
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001217 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 +02001218 local_ip_family == AF_INET6 ? '6' : '4',
1219 local_ip);
Philipp Maier704c4f02018-06-07 18:51:31 +02001220
1221 /* Add session name (none) */
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001222 rc |= msgb_printf(msg, "s=-\r\n");
Philipp Maier704c4f02018-06-07 18:51:31 +02001223
1224 /* Add RTP address and port */
1225 if (mgcp_msg->audio_port == 0) {
1226 LOGP(DLMGCP, LOGL_ERROR,
1227 "Invalid port number, can not generate MGCP message\n");
1228 msgb_free(msg);
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001229 return -EINVAL;
Philipp Maier704c4f02018-06-07 18:51:31 +02001230 }
1231 if (strlen(mgcp_msg->audio_ip) <= 0) {
1232 LOGP(DLMGCP, LOGL_ERROR,
1233 "Empty ip address, can not generate MGCP message\n");
1234 msgb_free(msg);
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001235 return -EINVAL;
Philipp Maier704c4f02018-06-07 18:51:31 +02001236 }
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001237 rc |= msgb_printf(msg, "c=IN IP%c %s\r\n",
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +02001238 audio_ip_family == AF_INET6 ? '6' : '4',
1239 mgcp_msg->audio_ip);
Philipp Maier704c4f02018-06-07 18:51:31 +02001240
1241 /* Add time description, active time (SDP) */
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001242 rc |= msgb_printf(msg, "t=0 0\r\n");
Philipp Maier704c4f02018-06-07 18:51:31 +02001243
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001244 rc |= msgb_printf(msg, "m=audio %u RTP/AVP", mgcp_msg->audio_port);
Philipp Maier704c4f02018-06-07 18:51:31 +02001245 for (i = 0; i < mgcp_msg->codecs_len; i++) {
1246 pt = map_codec_to_pt(mgcp_msg->ptmap, mgcp_msg->ptmap_len, mgcp_msg->codecs[i]);
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001247 rc |= msgb_printf(msg, " %u", pt);
Philipp Maier704c4f02018-06-07 18:51:31 +02001248
1249 }
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001250 rc |= msgb_printf(msg, "\r\n");
Philipp Maier704c4f02018-06-07 18:51:31 +02001251
Philipp Maier228e5912019-03-05 13:56:59 +01001252 /* Add optional codec parameters (fmtp) */
1253 if (mgcp_msg->param_present) {
1254 for (i = 0; i < mgcp_msg->codecs_len; i++) {
1255 /* The following is only applicable for AMR */
1256 if (mgcp_msg->codecs[i] != CODEC_AMR_8000_1 && mgcp_msg->codecs[i] != CODEC_AMRWB_16000_1)
1257 continue;
1258 pt = map_codec_to_pt(mgcp_msg->ptmap, mgcp_msg->ptmap_len, mgcp_msg->codecs[i]);
1259 if (mgcp_msg->param.amr_octet_aligned_present && mgcp_msg->param.amr_octet_aligned)
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001260 rc |= msgb_printf(msg, "a=fmtp:%u octet-align=1\r\n", pt);
Philipp Maier228e5912019-03-05 13:56:59 +01001261 else if (mgcp_msg->param.amr_octet_aligned_present && !mgcp_msg->param.amr_octet_aligned)
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001262 rc |= msgb_printf(msg, "a=fmtp:%u octet-align=0\r\n", pt);
Philipp Maier228e5912019-03-05 13:56:59 +01001263 }
1264 }
1265
Philipp Maier704c4f02018-06-07 18:51:31 +02001266 for (i = 0; i < mgcp_msg->codecs_len; i++) {
1267 pt = map_codec_to_pt(mgcp_msg->ptmap, mgcp_msg->ptmap_len, mgcp_msg->codecs[i]);
Pau Espin Pedrolc12bfb72019-04-16 17:23:09 +02001268
Philipp Maier704c4f02018-06-07 18:51:31 +02001269 /* Note: Only dynamic payload type from the range 96-127
1270 * require to be explained further via rtpmap. All others
1271 * are implcitly definedby the number in m=audio */
1272 if (pt >= 96 && pt <= 127) {
Neels Hofmeyr47642f22019-02-27 05:56:53 +01001273 codec = get_value_string_or_null(osmo_mgcpc_codec_names, mgcp_msg->codecs[i]);
Philipp Maier704c4f02018-06-07 18:51:31 +02001274
1275 /* Note: Use codec descriptors from enum mgcp_codecs
1276 * in mgcp_client only! */
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001277 if (!codec) {
1278 msgb_free(msg);
1279 return -EINVAL;
1280 }
Pau Espin Pedrolc12bfb72019-04-16 17:23:09 +02001281
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001282 rc |= msgb_printf(msg, "a=rtpmap:%u %s\r\n", pt, codec);
Philipp Maier704c4f02018-06-07 18:51:31 +02001283 }
1284 }
Pau Espin Pedrolc12bfb72019-04-16 17:23:09 +02001285
Philipp Maier704c4f02018-06-07 18:51:31 +02001286 if (mgcp_msg->ptime)
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001287 rc |= msgb_printf(msg, "a=ptime:%u\r\n", mgcp_msg->ptime);
Philipp Maier704c4f02018-06-07 18:51:31 +02001288
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001289 if (rc != 0) {
1290 LOGP(DLMGCP, LOGL_ERROR,
1291 "message buffer to small, can not generate MGCP message (SDP)\n");
1292 msgb_free(msg);
1293 return -ENOBUFS;
1294 }
1295
1296 return 0;
Philipp Maier704c4f02018-06-07 18:51:31 +02001297}
1298
Philipp Maier275ac972018-01-20 00:58:16 +01001299/*! Generate an MGCP message
1300 * \param[in] mgcp MGCP client descriptor.
1301 * \param[in] mgcp_msg Message description
1302 * \returns message buffer on success, NULL on error. */
Philipp Maier1dc6be62017-10-05 18:25:37 +02001303struct msgb *mgcp_msg_gen(struct mgcp_client *mgcp, struct mgcp_msg *mgcp_msg)
1304{
1305 mgcp_trans_id_t trans_id = mgcp_client_next_trans_id(mgcp);
1306 uint32_t mandatory_mask;
1307 struct msgb *msg = msgb_alloc_headroom(4096, 128, "MGCP tx");
1308 int rc = 0;
Philipp Maier704c4f02018-06-07 18:51:31 +02001309 bool use_sdp = false;
Pau Espin Pedrol900cd652019-04-24 22:06:22 +02001310 char buf[32];
Philipp Maier1dc6be62017-10-05 18:25:37 +02001311
1312 msg->l2h = msg->data;
1313 msg->cb[MSGB_CB_MGCP_TRANS_ID] = trans_id;
1314
1315 /* Add command verb */
1316 switch (mgcp_msg->verb) {
1317 case MGCP_VERB_CRCX:
1318 mandatory_mask = MGCP_CRCX_MANDATORY;
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001319 rc |= msgb_printf(msg, "CRCX %u", trans_id);
Philipp Maier1dc6be62017-10-05 18:25:37 +02001320 break;
1321 case MGCP_VERB_MDCX:
1322 mandatory_mask = MGCP_MDCX_MANDATORY;
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001323 rc |= msgb_printf(msg, "MDCX %u", trans_id);
Philipp Maier1dc6be62017-10-05 18:25:37 +02001324 break;
1325 case MGCP_VERB_DLCX:
1326 mandatory_mask = MGCP_DLCX_MANDATORY;
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001327 rc |= msgb_printf(msg, "DLCX %u", trans_id);
Philipp Maier1dc6be62017-10-05 18:25:37 +02001328 break;
1329 case MGCP_VERB_AUEP:
1330 mandatory_mask = MGCP_AUEP_MANDATORY;
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001331 rc |= msgb_printf(msg, "AUEP %u", trans_id);
Philipp Maier1dc6be62017-10-05 18:25:37 +02001332 break;
1333 case MGCP_VERB_RSIP:
1334 mandatory_mask = MGCP_RSIP_MANDATORY;
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001335 rc |= msgb_printf(msg, "RSIP %u", trans_id);
Philipp Maier1dc6be62017-10-05 18:25:37 +02001336 break;
1337 default:
1338 LOGP(DLMGCP, LOGL_ERROR,
1339 "Invalid command verb, can not generate MGCP message\n");
1340 msgb_free(msg);
1341 return NULL;
1342 }
1343
1344 /* Check if mandatory fields are missing */
1345 if (!((mgcp_msg->presence & mandatory_mask) == mandatory_mask)) {
1346 LOGP(DLMGCP, LOGL_ERROR,
1347 "One or more missing mandatory fields, can not generate MGCP message\n");
1348 msgb_free(msg);
1349 return NULL;
1350 }
1351
1352 /* Add endpoint name */
Philipp Maier7bc55522017-12-10 22:52:22 +01001353 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_ENDPOINT) {
1354 if (strlen(mgcp_msg->endpoint) <= 0) {
1355 LOGP(DLMGCP, LOGL_ERROR,
1356 "Empty endpoint name, can not generate MGCP message\n");
1357 msgb_free(msg);
1358 return NULL;
1359 }
Philipp Maier3aa81572018-01-31 14:13:45 +01001360
1361 if (strstr(mgcp_msg->endpoint, "@") == NULL) {
1362 LOGP(DLMGCP, LOGL_ERROR,
1363 "Endpoint name (%s) lacks separator (@), can not generate MGCP message\n",
1364 mgcp_msg->endpoint);
1365 msgb_free(msg);
Vadim Yanitskiye6743452020-06-18 03:04:13 +07001366 return NULL;
Philipp Maier3aa81572018-01-31 14:13:45 +01001367 }
1368
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001369 rc |= msgb_printf(msg, " %s", mgcp_msg->endpoint);
Philipp Maier7bc55522017-12-10 22:52:22 +01001370 }
Philipp Maier1dc6be62017-10-05 18:25:37 +02001371
1372 /* Add protocol version */
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001373 rc |= msgb_printf(msg, " MGCP 1.0\r\n");
Philipp Maier1dc6be62017-10-05 18:25:37 +02001374
1375 /* Add call id */
1376 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_CALL_ID)
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001377 rc |= msgb_printf(msg, "C: %x\r\n", mgcp_msg->call_id);
Philipp Maier1dc6be62017-10-05 18:25:37 +02001378
1379 /* Add connection id */
Philipp Maier7bc55522017-12-10 22:52:22 +01001380 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_CONN_ID) {
1381 if (strlen(mgcp_msg->conn_id) <= 0) {
1382 LOGP(DLMGCP, LOGL_ERROR,
1383 "Empty connection id, can not generate MGCP message\n");
1384 msgb_free(msg);
1385 return NULL;
1386 }
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001387 rc |= msgb_printf(msg, "I: %s\r\n", mgcp_msg->conn_id);
Philipp Maier7bc55522017-12-10 22:52:22 +01001388 }
Philipp Maier1dc6be62017-10-05 18:25:37 +02001389
Philipp Maier704c4f02018-06-07 18:51:31 +02001390 /* Using SDP makes sense when a valid IP/Port combination is specifiec,
1391 * if we do not know this information yet, we fall back to LCO */
1392 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_AUDIO_IP
1393 && mgcp_msg->presence & MGCP_MSG_PRESENCE_AUDIO_PORT)
1394 use_sdp = true;
1395
1396 /* Add local connection options (LCO) */
1397 if (!use_sdp
1398 && (mgcp_msg->verb == MGCP_VERB_CRCX
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001399 || mgcp_msg->verb == MGCP_VERB_MDCX)) {
1400 if (add_lco(msg, mgcp_msg) < 0)
1401 return NULL;
1402 }
Philipp Maier1dc6be62017-10-05 18:25:37 +02001403
1404 /* Add mode */
1405 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_CONN_MODE)
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001406 rc |=
Philipp Maier1dc6be62017-10-05 18:25:37 +02001407 msgb_printf(msg, "M: %s\r\n",
1408 mgcp_client_cmode_name(mgcp_msg->conn_mode));
1409
Neels Hofmeyre6d8e912018-08-23 16:36:48 +02001410 /* Add X-Osmo-IGN */
1411 if ((mgcp_msg->presence & MGCP_MSG_PRESENCE_X_OSMO_IGN)
1412 && (mgcp_msg->x_osmo_ign != 0))
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001413 rc |=
Neels Hofmeyre6d8e912018-08-23 16:36:48 +02001414 msgb_printf(msg, MGCP_X_OSMO_IGN_HEADER "%s\r\n",
1415 mgcp_msg->x_osmo_ign & MGCP_X_OSMO_IGN_CALLID ? " C": "");
1416
Pau Espin Pedrol900cd652019-04-24 22:06:22 +02001417 /* Add X-Osmo-Osmux */
1418 if ((mgcp_msg->presence & MGCP_MSG_PRESENCE_X_OSMO_OSMUX_CID)) {
Pau Espin Pedrol1b1d7ed2019-05-23 16:48:24 +02001419 if (mgcp_msg->x_osmo_osmux_cid < -1 || mgcp_msg->x_osmo_osmux_cid > OSMUX_CID_MAX) {
1420 LOGP(DLMGCP, LOGL_ERROR,
1421 "Wrong Osmux CID %d, can not generate MGCP message\n",
1422 mgcp_msg->x_osmo_osmux_cid);
1423 msgb_free(msg);
1424 return NULL;
1425 }
Pau Espin Pedrol900cd652019-04-24 22:06:22 +02001426 snprintf(buf, sizeof(buf), " %d", mgcp_msg->x_osmo_osmux_cid);
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001427 rc |=
Pau Espin Pedrol900cd652019-04-24 22:06:22 +02001428 msgb_printf(msg, MGCP_X_OSMO_OSMUX_HEADER "%s\r\n",
1429 mgcp_msg->x_osmo_osmux_cid == -1 ? " *": buf);
1430 }
1431
1432
Philipp Maier704c4f02018-06-07 18:51:31 +02001433 /* Add session description protocol (SDP) */
1434 if (use_sdp
1435 && (mgcp_msg->verb == MGCP_VERB_CRCX
1436 || mgcp_msg->verb == MGCP_VERB_MDCX)) {
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001437 if (add_sdp(msg, mgcp_msg, mgcp) < 0)
Philipp Maier9d25d7a2018-01-22 17:31:10 +01001438 return NULL;
Philipp Maier1dc6be62017-10-05 18:25:37 +02001439 }
1440
1441 if (rc != 0) {
1442 LOGP(DLMGCP, LOGL_ERROR,
1443 "message buffer to small, can not generate MGCP message\n");
1444 msgb_free(msg);
1445 msg = NULL;
1446 }
1447
1448 return msg;
1449}
1450
Philipp Maier275ac972018-01-20 00:58:16 +01001451/*! Retrieve the MGCP transaction ID from a msgb generated by mgcp_msg_gen()
1452 * \param[in] msg message buffer
1453 * \returns Transaction id. */
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +01001454mgcp_trans_id_t mgcp_msg_trans_id(struct msgb *msg)
1455{
1456 return (mgcp_trans_id_t)msg->cb[MSGB_CB_MGCP_TRANS_ID];
1457}
1458
Philipp Maier275ac972018-01-20 00:58:16 +01001459/*! Get the configuration parameters a given MGCP client instance
1460 * \param[in] mgcp MGCP client descriptor.
1461 * \returns configuration */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +02001462struct mgcp_client_conf *mgcp_client_conf_actual(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001463{
1464 return &mgcp->actual;
1465}
Neels Hofmeyrd95ab1e2017-09-22 00:52:54 +02001466
1467const struct value_string mgcp_client_connection_mode_strs[] = {
1468 { MGCP_CONN_NONE, "none" },
1469 { MGCP_CONN_RECV_SEND, "sendrecv" },
1470 { MGCP_CONN_SEND_ONLY, "sendonly" },
1471 { MGCP_CONN_RECV_ONLY, "recvonly" },
1472 { MGCP_CONN_LOOPBACK, "loopback" },
1473 { 0, NULL }
1474};