blob: 69d687bb76f112a67f78a0a4aae08ee160efd599 [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 };
203}
204
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200205static void mgcp_client_handle_response(struct mgcp_client *mgcp,
206 struct mgcp_response_pending *pending,
207 struct mgcp_response *response)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200208{
209 if (!pending) {
210 LOGP(DLMGCP, LOGL_ERROR,
211 "Cannot handle NULL response\n");
212 return;
213 }
214 if (pending->response_cb)
215 pending->response_cb(response, pending->priv);
216 else
Neels Hofmeyred0c1aa2018-12-21 03:07:53 +0100217 LOGP(DLMGCP, LOGL_DEBUG, "MGCP response ignored (NULL cb)\n");
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200218 talloc_free(pending);
219}
220
221static int mgcp_response_parse_head(struct mgcp_response *r, struct msgb *msg)
222{
223 int comment_pos;
224 char *end;
225
226 if (mgcp_msg_terminate_nul(msg))
227 goto response_parse_failure;
228
229 r->body = (char *)msg->data;
230
Harald Welte9bf7c532017-11-17 14:14:31 +0100231 if (sscanf(r->body, "%3d %u %n",
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200232 &r->head.response_code, &r->head.trans_id,
233 &comment_pos) != 2)
234 goto response_parse_failure;
235
Philipp Maierabe8c892018-01-20 00:15:12 +0100236 osmo_strlcpy(r->head.comment, r->body + comment_pos, sizeof(r->head.comment));
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200237 end = strchr(r->head.comment, '\r');
238 if (!end)
239 goto response_parse_failure;
240 /* Mark the end of the comment */
241 *end = '\0';
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200242 return 0;
243
244response_parse_failure:
245 LOGP(DLMGCP, LOGL_ERROR,
246 "Failed to parse MGCP response header\n");
247 return -EINVAL;
248}
249
250/* TODO undup against mgcp_protocol.c:mgcp_check_param() */
251static bool mgcp_line_is_valid(const char *line)
252{
253 const size_t line_len = strlen(line);
254 if (line[0] == '\0')
255 return true;
256
257 if (line_len < 2
258 || line[1] != '=') {
259 LOGP(DLMGCP, LOGL_ERROR,
260 "Wrong MGCP option format: '%s'\n",
261 line);
262 return false;
263 }
264
265 return true;
266}
267
Philipp Maier704c4f02018-06-07 18:51:31 +0200268/* Parse a line like "m=audio 16002 RTP/AVP 98", extract port and payload types */
269static int mgcp_parse_audio_port_pt(struct mgcp_response *r, char *line)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200270{
Philipp Maier704c4f02018-06-07 18:51:31 +0200271 char *pt_str;
Pau Espin Pedrolc5c14302019-07-23 16:19:41 +0200272 char *pt_end;
Pau Espin Pedrola2b1c5e2019-07-26 14:13:14 +0200273 unsigned long int pt;
Philipp Maier704c4f02018-06-07 18:51:31 +0200274 unsigned int count = 0;
275 unsigned int i;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200276
Philipp Maier704c4f02018-06-07 18:51:31 +0200277 /* Extract port information */
278 if (sscanf(line, "m=audio %hu", &r->audio_port) != 1)
279 goto response_parse_failure_port;
Philipp Maier10f32db2017-12-13 12:34:34 +0100280 if (r->audio_port == 0)
Philipp Maier704c4f02018-06-07 18:51:31 +0200281 goto response_parse_failure_port;
Philipp Maier10f32db2017-12-13 12:34:34 +0100282
Philipp Maier704c4f02018-06-07 18:51:31 +0200283 /* Extract payload types */
284 line = strstr(line, "RTP/AVP ");
285 if (!line)
286 goto exit;
287
288 pt_str = strtok(line, " ");
289 while (1) {
290 /* Do not allow excessive payload types */
291 if (count > ARRAY_SIZE(r->codecs))
292 goto response_parse_failure_pt;
293
294 pt_str = strtok(NULL, " ");
295 if (!pt_str)
296 break;
Pau Espin Pedrolc5c14302019-07-23 16:19:41 +0200297 errno = 0;
298 pt = strtoul(pt_str, &pt_end, 0);
299 if ((errno == ERANGE && pt == ULONG_MAX) || (errno && !pt) ||
300 pt_str == pt_end)
301 goto response_parse_failure_pt;
Philipp Maier704c4f02018-06-07 18:51:31 +0200302
Pau Espin Pedrola2b1c5e2019-07-26 14:13:14 +0200303 if (pt >> 7) /* PT is 7 bit field, higher values not allowed */
304 goto response_parse_failure_pt;
305
Philipp Maier704c4f02018-06-07 18:51:31 +0200306 /* Do not allow duplicate payload types */
307 for (i = 0; i < count; i++)
308 if (r->codecs[i] == pt)
309 goto response_parse_failure_pt;
310
311 /* Note: The payload type we store may not necessarly match
312 * the codec types we have defined in enum mgcp_codecs. To
313 * ensure that the end result only contains codec types which
314 * match enum mgcp_codecs, we will go through afterwards and
315 * remap the affected entries with the inrofmation we learn
316 * from rtpmap */
317 r->codecs[count] = pt;
318 count++;
319 }
320
321 r->codecs_len = count;
322
323exit:
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200324 return 0;
325
Philipp Maier704c4f02018-06-07 18:51:31 +0200326response_parse_failure_port:
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200327 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier704c4f02018-06-07 18:51:31 +0200328 "Failed to parse SDP parameter port (%s)\n", line);
Philipp Maier06da85e2017-10-05 18:49:24 +0200329 return -EINVAL;
Philipp Maier704c4f02018-06-07 18:51:31 +0200330
331response_parse_failure_pt:
332 LOGP(DLMGCP, LOGL_ERROR,
333 "Failed to parse SDP parameter payload types (%s)\n", line);
334 return -EINVAL;
335}
336
337/* Parse a line like "m=audio 16002 RTP/AVP 98", extract port and payload types */
338static int mgcp_parse_audio_ptime_rtpmap(struct mgcp_response *r, const char *line)
339{
340 unsigned int pt;
341 char codec_resp[64];
Neels Hofmeyr3ab8ca42019-08-08 04:03:42 +0200342 enum mgcp_codecs codec;
Pau Espin Pedrolc12bfb72019-04-16 17:23:09 +0200343
Neels Hofmeyr23f40482019-08-08 04:03:42 +0200344#define A_PTIME "a=ptime:"
345#define A_RTPMAP "a=rtpmap:"
Pau Espin Pedrolc12bfb72019-04-16 17:23:09 +0200346
Neels Hofmeyr23f40482019-08-08 04:03:42 +0200347 if (osmo_str_startswith(line, A_PTIME)) {
348 if (sscanf(line, A_PTIME "%u", &r->ptime) != 1) {
349 LOGP(DLMGCP, LOGL_ERROR,
350 "Failed to parse SDP parameter, invalid ptime (%s)\n", line);
351 return -EINVAL;
352 }
353 } else if (osmo_str_startswith(line, A_RTPMAP)) {
354 if (sscanf(line, A_RTPMAP "%d %63s", &pt, codec_resp) != 2) {
355 LOGP(DLMGCP, LOGL_ERROR,
356 "Failed to parse SDP parameter, invalid rtpmap: %s\n", osmo_quote_str(line, -1));
357 return -EINVAL;
358 }
Neels Hofmeyr3ab8ca42019-08-08 04:03:42 +0200359 if (r->ptmap_len >= ARRAY_SIZE(r->ptmap)) {
360 LOGP(DLMGCP, LOGL_ERROR, "No more space in ptmap array (len=%u)\n", r->ptmap_len);
361 return -ENOSPC;
Neels Hofmeyr23f40482019-08-08 04:03:42 +0200362 }
Neels Hofmeyr3ab8ca42019-08-08 04:03:42 +0200363 codec = map_str_to_codec(codec_resp);
364 r->ptmap[r->ptmap_len].pt = pt;
365 r->ptmap[r->ptmap_len].codec = codec;
366 r->ptmap_len++;
Philipp Maier704c4f02018-06-07 18:51:31 +0200367 }
Pau Espin Pedrolc12bfb72019-04-16 17:23:09 +0200368
Philipp Maier704c4f02018-06-07 18:51:31 +0200369 return 0;
Philipp Maier06da85e2017-10-05 18:49:24 +0200370}
371
372/* Parse a line like "c=IN IP4 10.11.12.13" */
373static int mgcp_parse_audio_ip(struct mgcp_response *r, const char *line)
374{
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +0200375 struct in6_addr ip_test;
376 bool is_ipv6;
Philipp Maier06da85e2017-10-05 18:49:24 +0200377
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +0200378 if (strncmp("c=IN IP", line, 7) != 0)
Philipp Maier06da85e2017-10-05 18:49:24 +0200379 goto response_parse_failure;
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +0200380 line += 7;
381 if (*line == '6')
382 is_ipv6 = true;
383 else if (*line == '4')
384 is_ipv6 = false;
385 else
Philipp Maier06da85e2017-10-05 18:49:24 +0200386 goto response_parse_failure;
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +0200387 line++;
388 if (*line != ' ')
Philipp Maier06da85e2017-10-05 18:49:24 +0200389 goto response_parse_failure;
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +0200390 line++;
Philipp Maier06da85e2017-10-05 18:49:24 +0200391
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +0200392 /* Extract and check IP-Address */
393 if (is_ipv6) {
394 /* 45 = INET6_ADDRSTRLEN -1 */
395 if (sscanf(line, "%45s", r->audio_ip) != 1)
396 goto response_parse_failure;
397 if (inet_pton(AF_INET6, r->audio_ip, &ip_test) != 1)
398 goto response_parse_failure;
399 } else {
400 /* 15 = INET_ADDRSTRLEN -1 */
401 if (sscanf(line, "%15s", r->audio_ip) != 1)
402 goto response_parse_failure;
403 if (inet_pton(AF_INET, r->audio_ip, &ip_test) != 1)
404 goto response_parse_failure;
405 }
Philipp Maier06da85e2017-10-05 18:49:24 +0200406 return 0;
407
408response_parse_failure:
409 LOGP(DLMGCP, LOGL_ERROR,
410 "Failed to parse MGCP response header (audio ip)\n");
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200411 return -EINVAL;
412}
413
Pau Espin Pedrol91088c32019-04-24 21:02:40 +0200414/*! Extract OSMUX CID from an MGCP parameter line (string).
415 * \param[in] line single parameter line from the MGCP message
416 * \returns OSMUX CID, -1 wildcard, -2 on error
417 * FIXME: This is a copy of function in mgcp_msg.c. Have some common.c file between both libs?
418 */
419static int mgcp_parse_osmux_cid(const char *line)
420{
421 int osmux_cid;
422
423
Pau Espin Pedrolc1bf4692019-05-14 16:23:24 +0200424 if (strcasecmp(line + 2, "Osmux: *") == 0) {
Pau Espin Pedrol91088c32019-04-24 21:02:40 +0200425 LOGP(DLMGCP, LOGL_DEBUG, "Parsed wilcard Osmux CID\n");
426 return -1;
427 }
428
Pau Espin Pedrolc1bf4692019-05-14 16:23:24 +0200429 if (sscanf(line + 2 + 7, "%u", &osmux_cid) != 1) {
Pau Espin Pedrol91088c32019-04-24 21:02:40 +0200430 LOGP(DLMGCP, LOGL_ERROR, "Failed parsing Osmux in MGCP msg line: %s\n",
431 line);
432 return -2;
433 }
434
Pau Espin Pedrol91088c32019-04-24 21:02:40 +0200435 if (osmux_cid > OSMUX_CID_MAX) { /* OSMUX_CID_MAX from libosmo-netif */
436 LOGP(DLMGCP, LOGL_ERROR, "Osmux ID too large: %u > %u\n",
437 osmux_cid, OSMUX_CID_MAX);
438 return -2;
439 }
440 LOGP(DLMGCP, LOGL_DEBUG, "bsc-nat offered Osmux CID %u\n", osmux_cid);
441
442 return osmux_cid;
443}
444
Philipp Maier3b12e1b2018-01-18 15:16:13 +0100445/* A new section is marked by a double line break, check a few more
446 * patterns as there may be variants */
447static char *mgcp_find_section_end(char *string)
448{
449 char *rc;
450
451 rc = strstr(string, "\n\n");
452 if (rc)
453 return rc;
454
455 rc = strstr(string, "\n\r\n\r");
456 if (rc)
457 return rc;
458
459 rc = strstr(string, "\r\n\r\n");
460 if (rc)
461 return rc;
462
463 return NULL;
464}
465
Philipp Maier275ac972018-01-20 00:58:16 +0100466/*! Parse body (SDP) parameters of the MGCP response
467 * \param[in,out] r Response data
468 * \returns 0 on success, -EINVAL on error. */
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200469int mgcp_response_parse_params(struct mgcp_response *r)
470{
471 char *line;
472 int rc;
Neels Hofmeyr0793d2f2018-02-21 14:55:34 +0100473 char *data;
Philipp Maiere9d645b2018-01-19 23:54:08 +0100474 char *data_ptr;
Philipp Maier704c4f02018-06-07 18:51:31 +0200475 int i;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200476
Philipp Maiere9d645b2018-01-19 23:54:08 +0100477 /* Since this functions performs a destructive parsing, we create a
478 * local copy of the body data */
Neels Hofmeyr0793d2f2018-02-21 14:55:34 +0100479 OSMO_ASSERT(r->body);
480 data = talloc_strdup(r, r->body);
Philipp Maiere9d645b2018-01-19 23:54:08 +0100481 OSMO_ASSERT(data);
Philipp Maier55295f72018-01-15 14:00:28 +0100482
Philipp Maiere9d645b2018-01-19 23:54:08 +0100483 /* Find beginning of the parameter (SDP) section */
484 data_ptr = mgcp_find_section_end(data);
Neels Hofmeyr10835332018-02-21 14:55:34 +0100485 if (!data_ptr) {
Neels Hofmeyre8278312019-09-19 03:06:46 +0200486 LOGP(DLMGCP, LOGL_DEBUG, "MGCP response contains no SDP parameters\n");
487 rc = 0;
Philipp Maiere9d645b2018-01-19 23:54:08 +0100488 goto exit;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200489 }
490
Neels Hofmeyr0793d2f2018-02-21 14:55:34 +0100491 /* data_ptr now points to the beginning of the section-end-marker; for_each_non_empty_line()
492 * skips any \r and \n characters for free, so we don't need to skip the marker. */
493
Philipp Maiere9d645b2018-01-19 23:54:08 +0100494 for_each_non_empty_line(line, data_ptr) {
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200495 if (!mgcp_line_is_valid(line))
496 return -EINVAL;
497
498 switch (line[0]) {
Philipp Maier704c4f02018-06-07 18:51:31 +0200499 case 'a':
500 rc = mgcp_parse_audio_ptime_rtpmap(r, line);
501 if (rc)
502 goto exit;
503 break;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200504 case 'm':
Philipp Maier704c4f02018-06-07 18:51:31 +0200505 rc = mgcp_parse_audio_port_pt(r, line);
Philipp Maier06da85e2017-10-05 18:49:24 +0200506 if (rc)
Philipp Maiere9d645b2018-01-19 23:54:08 +0100507 goto exit;
Philipp Maier06da85e2017-10-05 18:49:24 +0200508 break;
509 case 'c':
510 rc = mgcp_parse_audio_ip(r, line);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200511 if (rc)
Philipp Maiere9d645b2018-01-19 23:54:08 +0100512 goto exit;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200513 break;
514 default:
515 /* skip unhandled parameters */
516 break;
517 }
518 }
Philipp Maiere9d645b2018-01-19 23:54:08 +0100519
Philipp Maier704c4f02018-06-07 18:51:31 +0200520 /* See also note in mgcp_parse_audio_port_pt() */
521 for (i = 0; i < r->codecs_len; i++)
522 r->codecs[i] = map_pt_to_codec(r->ptmap, r->ptmap_len, r->codecs[i]);
523
Philipp Maiere9d645b2018-01-19 23:54:08 +0100524 rc = 0;
525exit:
526 talloc_free(data);
527 return rc;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200528}
529
Philipp Maier55295f72018-01-15 14:00:28 +0100530/* Parse a line like "X: something" */
531static int mgcp_parse_head_param(char *result, unsigned int result_len,
532 char label, const char *line)
Philipp Maierffd75e42017-11-22 11:44:50 +0100533{
Philipp Maier55295f72018-01-15 14:00:28 +0100534 char label_string[4];
Neels Hofmeyr23e7bf12018-09-03 21:26:22 +0200535 size_t rc;
Philipp Maier55295f72018-01-15 14:00:28 +0100536
537 /* Detect empty parameters */
Philipp Maierffd75e42017-11-22 11:44:50 +0100538 if (strlen(line) < 4)
539 goto response_parse_failure;
540
Philipp Maier55295f72018-01-15 14:00:28 +0100541 /* Check if the label matches */
542 snprintf(label_string, sizeof(label_string), "%c: ", label);
543 if (memcmp(label_string, line, 3) != 0)
Philipp Maierffd75e42017-11-22 11:44:50 +0100544 goto response_parse_failure;
545
Philipp Maier55295f72018-01-15 14:00:28 +0100546 /* Copy payload part of the string to destinations (the label string
547 * is always 3 chars long) */
Neels Hofmeyr23e7bf12018-09-03 21:26:22 +0200548 rc = osmo_strlcpy(result, line + 3, result_len);
549 if (rc >= result_len) {
550 LOGP(DLMGCP, LOGL_ERROR,
551 "Failed to parse MGCP response (parameter label: %c):"
552 " the received conn ID is too long: %zu, maximum is %u characters\n",
553 label, rc, result_len - 1);
554 return -ENOSPC;
555 }
Philipp Maierffd75e42017-11-22 11:44:50 +0100556 return 0;
557
558response_parse_failure:
559 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier55295f72018-01-15 14:00:28 +0100560 "Failed to parse MGCP response (parameter label: %c)\n", label);
Philipp Maierffd75e42017-11-22 11:44:50 +0100561 return -EINVAL;
562}
563
564/* Parse MGCP parameters of the response */
565static int parse_head_params(struct mgcp_response *r)
566{
567 char *line;
568 int rc = 0;
569 OSMO_ASSERT(r->body);
Philipp Maier55295f72018-01-15 14:00:28 +0100570 char *data;
571 char *data_ptr;
572 char *data_end;
Philipp Maierffd75e42017-11-22 11:44:50 +0100573
Philipp Maier55295f72018-01-15 14:00:28 +0100574 /* Since this functions performs a destructive parsing, we create a
575 * local copy of the body data */
Philipp Maier1fc69992018-01-31 15:32:55 +0100576 data = talloc_zero_size(r, strlen(r->body)+1);
Philipp Maier55295f72018-01-15 14:00:28 +0100577 OSMO_ASSERT(data);
578 data_ptr = data;
579 osmo_strlcpy(data, r->body, strlen(r->body));
580
581 /* If there is an SDP body attached, prevent for_each_non_empty_line()
582 * into running in there, we are not yet interested in the parameters
583 * stored there. */
Pau Espin Pedrolc12bfb72019-04-16 17:23:09 +0200584 data_end = mgcp_find_section_end(data);
Philipp Maierffd75e42017-11-22 11:44:50 +0100585 if (data_end)
586 *data_end = '\0';
587
Philipp Maier55295f72018-01-15 14:00:28 +0100588 for_each_non_empty_line(line, data_ptr) {
Pau Espin Pedrol166077e2019-06-26 12:21:38 +0200589 switch (toupper(line[0])) {
Philipp Maier55295f72018-01-15 14:00:28 +0100590 case 'Z':
591 rc = mgcp_parse_head_param(r->head.endpoint,
592 sizeof(r->head.endpoint),
593 'Z', line);
594 if (rc)
595 goto exit;
Philipp Maier771b26a2018-01-31 13:53:11 +0100596
597 /* A specific endpoint identifier returned by the MGW
598 * must not contain any wildcard characters */
599 if (strstr(r->head.endpoint, "*") != NULL) {
600 rc = -EINVAL;
601 goto exit;
602 }
Philipp Maier3261dc72018-01-31 14:03:13 +0100603
604 /* A specific endpoint identifier returned by the MGW
605 * must contain an @ character */
606 if (strstr(r->head.endpoint, "@") == NULL) {
607 rc = -EINVAL;
608 goto exit;
609 }
Philipp Maier55295f72018-01-15 14:00:28 +0100610 break;
Philipp Maierffd75e42017-11-22 11:44:50 +0100611 case 'I':
Philipp Maier55295f72018-01-15 14:00:28 +0100612 rc = mgcp_parse_head_param(r->head.conn_id,
613 sizeof(r->head.conn_id),
614 'I', line);
Philipp Maierffd75e42017-11-22 11:44:50 +0100615 if (rc)
616 goto exit;
617 break;
Pau Espin Pedrol91088c32019-04-24 21:02:40 +0200618 case 'X':
Pau Espin Pedrolc1bf4692019-05-14 16:23:24 +0200619 if (strncasecmp("Osmux: ", line + 2, strlen("Osmux: ")) == 0) {
Pau Espin Pedrol91088c32019-04-24 21:02:40 +0200620 rc = mgcp_parse_osmux_cid(line);
621 if (rc < 0) {
622 /* -1: we don't want wildcards in response. -2: error */
623 rc = -EINVAL;
624 goto exit;
625 }
626 r->head.x_osmo_osmux_use = true;
627 r->head.x_osmo_osmux_cid = (uint8_t) rc;
628 rc = 0;
629 break;
630 }
631 /* Ignore unknown X-headers */
632 break;
Philipp Maierffd75e42017-11-22 11:44:50 +0100633 default:
634 /* skip unhandled parameters */
635 break;
636 }
637 }
638exit:
Philipp Maier55295f72018-01-15 14:00:28 +0100639 talloc_free(data);
Philipp Maierffd75e42017-11-22 11:44:50 +0100640 return rc;
641}
642
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200643static struct mgcp_response_pending *mgcp_client_response_pending_get(
644 struct mgcp_client *mgcp,
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100645 mgcp_trans_id_t trans_id)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200646{
647 struct mgcp_response_pending *pending;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200648 llist_for_each_entry(pending, &mgcp->responses_pending, entry) {
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100649 if (pending->trans_id == trans_id) {
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200650 llist_del(&pending->entry);
651 return pending;
652 }
653 }
654 return NULL;
655}
656
657/* Feed an MGCP message into the receive processing.
658 * Parse the head and call any callback registered for the transaction id found
659 * in the MGCP message. This is normally called directly from the internal
660 * mgcp_do_read that reads from the socket connected to the MGCP gateway. This
661 * function is published mainly to be able to feed data from the test suite.
662 */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200663int mgcp_client_rx(struct mgcp_client *mgcp, struct msgb *msg)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200664{
Philipp Maier1fc69992018-01-31 15:32:55 +0100665 struct mgcp_response *r;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200666 struct mgcp_response_pending *pending;
667 int rc;
668
Philipp Maier1fc69992018-01-31 15:32:55 +0100669 r = talloc_zero(mgcp, struct mgcp_response);
670 OSMO_ASSERT(r);
671
672 rc = mgcp_response_parse_head(r, msg);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200673 if (rc) {
Philipp Maierffd75e42017-11-22 11:44:50 +0100674 LOGP(DLMGCP, LOGL_ERROR, "Cannot parse MGCP response (head)\n");
Philipp Maier1fc69992018-01-31 15:32:55 +0100675 rc = 1;
676 goto error;
Philipp Maierffd75e42017-11-22 11:44:50 +0100677 }
678
Neels Hofmeyrd5731072021-07-15 01:36:47 +0200679 LOGP(DLMGCP, LOGL_DEBUG, "MGCP client: Rx %d %u %s\n",
680 r->head.response_code, r->head.trans_id, r->head.comment);
681
Philipp Maier1fc69992018-01-31 15:32:55 +0100682 rc = parse_head_params(r);
Philipp Maierffd75e42017-11-22 11:44:50 +0100683 if (rc) {
684 LOGP(DLMGCP, LOGL_ERROR, "Cannot parse MGCP response (head parameters)\n");
Philipp Maier1fc69992018-01-31 15:32:55 +0100685 rc = 1;
686 goto error;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200687 }
688
Philipp Maier1fc69992018-01-31 15:32:55 +0100689 pending = mgcp_client_response_pending_get(mgcp, r->head.trans_id);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200690 if (!pending) {
691 LOGP(DLMGCP, LOGL_ERROR,
692 "Cannot find matching MGCP transaction for trans_id %d\n",
Philipp Maier1fc69992018-01-31 15:32:55 +0100693 r->head.trans_id);
694 rc = -ENOENT;
695 goto error;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200696 }
697
Philipp Maier1fc69992018-01-31 15:32:55 +0100698 mgcp_client_handle_response(mgcp, pending, r);
699 rc = 0;
700
701error:
702 talloc_free(r);
703 return rc;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200704}
705
706static int mgcp_do_read(struct osmo_fd *fd)
707{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200708 struct mgcp_client *mgcp = fd->data;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200709 struct msgb *msg;
710 int ret;
711
712 msg = msgb_alloc_headroom(4096, 128, "mgcp_from_gw");
713 if (!msg) {
714 LOGP(DLMGCP, LOGL_ERROR, "Failed to allocate MGCP message.\n");
715 return -1;
716 }
717
718 ret = read(fd->fd, msg->data, 4096 - 128);
719 if (ret <= 0) {
Neels Hofmeyr0a403792018-12-12 03:10:18 +0100720 LOGP(DLMGCP, LOGL_ERROR, "Failed to read: %s: %d='%s'\n", osmo_sock_get_name2(fd->fd),
721 errno, strerror(errno));
722
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200723 msgb_free(msg);
724 return -1;
725 } else if (ret > 4096 - 128) {
Neels Hofmeyr0a403792018-12-12 03:10:18 +0100726 LOGP(DLMGCP, LOGL_ERROR, "Too much data: %s: %d\n", osmo_sock_get_name2(fd->fd), ret);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200727 msgb_free(msg);
728 return -1;
Harald Welte9bf7c532017-11-17 14:14:31 +0100729 }
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200730
731 msg->l2h = msgb_put(msg, ret);
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200732 ret = mgcp_client_rx(mgcp, msg);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200733 talloc_free(msg);
734 return ret;
735}
736
737static int mgcp_do_write(struct osmo_fd *fd, struct msgb *msg)
738{
739 int ret;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200740
Neels Hofmeyr0a403792018-12-12 03:10:18 +0100741 LOGP(DLMGCP, LOGL_DEBUG, "Tx MGCP: %s: len=%u '%s'...\n",
742 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 +0200743
744 ret = write(fd->fd, msg->data, msg->len);
745 if (ret != msg->len)
Neels Hofmeyr0a403792018-12-12 03:10:18 +0100746 LOGP(DLMGCP, LOGL_ERROR, "Failed to Tx MGCP: %s: %d='%s'; msg: len=%u '%s'...\n",
747 osmo_sock_get_name2(fd->fd), errno, strerror(errno),
Neels Hofmeyr32d15cc2018-12-12 03:05:33 +0100748 msg->len, osmo_escape_str((const char*)msg->data, OSMO_MIN(42, msg->len)));
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200749 return ret;
750}
751
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200752struct mgcp_client *mgcp_client_init(void *ctx,
753 struct mgcp_client_conf *conf)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200754{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200755 struct mgcp_client *mgcp;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200756
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200757 mgcp = talloc_zero(ctx, struct mgcp_client);
Harald Welteefe15712020-07-15 10:56:45 +0200758 if (!mgcp)
759 return NULL;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200760
761 INIT_LLIST_HEAD(&mgcp->responses_pending);
762 INIT_LLIST_HEAD(&mgcp->inuse_endpoints);
763
764 mgcp->next_trans_id = 1;
765
766 mgcp->actual.local_addr = conf->local_addr ? conf->local_addr :
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200767 MGCP_CLIENT_LOCAL_ADDR_DEFAULT;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200768 mgcp->actual.local_port = conf->local_port >= 0 ? (uint16_t)conf->local_port :
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200769 MGCP_CLIENT_LOCAL_PORT_DEFAULT;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200770
771 mgcp->actual.remote_addr = conf->remote_addr ? conf->remote_addr :
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200772 MGCP_CLIENT_REMOTE_ADDR_DEFAULT;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200773 mgcp->actual.remote_port = conf->remote_port >= 0 ? (uint16_t)conf->remote_port :
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200774 MGCP_CLIENT_REMOTE_PORT_DEFAULT;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200775
Neels Hofmeyrac69ea92018-12-19 00:27:50 +0100776 if (osmo_strlcpy(mgcp->actual.endpoint_domain_name, conf->endpoint_domain_name,
777 sizeof(mgcp->actual.endpoint_domain_name))
778 >= sizeof(mgcp->actual.endpoint_domain_name)) {
779 LOGP(DLMGCP, LOGL_ERROR, "MGCP client: endpoint domain name is too long, max length is %zu: '%s'\n",
780 sizeof(mgcp->actual.endpoint_domain_name) - 1, conf->endpoint_domain_name);
781 talloc_free(mgcp);
782 return NULL;
783 }
784 LOGP(DLMGCP, LOGL_NOTICE, "MGCP client: using endpoint domain '@%s'\n", mgcp_client_endpoint_domain(mgcp));
785
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200786 return mgcp;
787}
788
Philipp Maier6a26c162018-08-01 16:37:06 +0200789static int init_socket(struct mgcp_client *mgcp)
790{
791 int rc;
792 struct osmo_wqueue *wq;
793 int i;
794
795 wq = &mgcp->wq;
796
797 for (i = 0; i < 100; i++) {
798
799 /* Initalize socket with the currently configured port
800 * number */
Pau Espin Pedrol531470a2020-08-31 17:06:55 +0200801 rc = osmo_sock_init2_ofd(&wq->bfd, AF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, mgcp->actual.local_addr,
Philipp Maier6a26c162018-08-01 16:37:06 +0200802 mgcp->actual.local_port, mgcp->actual.remote_addr, mgcp->actual.remote_port,
803 OSMO_SOCK_F_BIND | OSMO_SOCK_F_CONNECT);
804 if (rc > 0)
805 return rc;
806
807 /* If there is a different port than the default port
808 * configured then we assume that the user has choosen
809 * that port conciously and we will not try to resolve
810 * this by silently choosing a different port. */
Philipp Maier9a7ccc32018-08-09 11:41:19 +0200811 if (mgcp->actual.local_port != MGCP_CLIENT_LOCAL_PORT_DEFAULT && i == 0)
Philipp Maier6a26c162018-08-01 16:37:06 +0200812 return -EINVAL;
813
814 /* Choose a new port number to try next */
815 LOGP(DLMGCP, LOGL_NOTICE,
Neels Hofmeyr0a403792018-12-12 03:10:18 +0100816 "MGCPGW failed to bind to %s:%u, retrying with port %u\n",
817 mgcp->actual.local_addr, mgcp->actual.local_port, mgcp->actual.local_port + 1);
Philipp Maier6a26c162018-08-01 16:37:06 +0200818 mgcp->actual.local_port++;
819 }
820
Neels Hofmeyr0a403792018-12-12 03:10:18 +0100821 LOGP(DLMGCP, LOGL_FATAL, "MGCPGW failed to find a port to bind on %i times.\n", i);
Philipp Maier6a26c162018-08-01 16:37:06 +0200822 return -EINVAL;
823}
824
Philipp Maier275ac972018-01-20 00:58:16 +0100825/*! Initalize client connection (opens socket only, no request is sent yet)
826 * \param[in,out] mgcp MGCP client descriptor.
827 * \returns 0 on success, -EINVAL on error. */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200828int mgcp_client_connect(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200829{
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200830 struct osmo_wqueue *wq;
831 int rc;
832
833 if (!mgcp) {
834 LOGP(DLMGCP, LOGL_FATAL, "MGCPGW client not initialized properly\n");
835 return -EINVAL;
836 }
837
838 wq = &mgcp->wq;
Harald Weltec2a8f592020-10-19 13:25:41 +0200839 osmo_wqueue_init(wq, 1024);
840 wq->read_cb = mgcp_do_read;
841 wq->write_cb = mgcp_do_write;
842
843 osmo_fd_setup(&wq->bfd, -1, OSMO_FD_READ, osmo_wqueue_bfd_cb, mgcp, 0);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200844
Philipp Maier6a26c162018-08-01 16:37:06 +0200845 rc = init_socket(mgcp);
Harald Welte8890dfa2017-11-17 15:09:30 +0100846 if (rc < 0) {
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200847 LOGP(DLMGCP, LOGL_FATAL,
Harald Welte8890dfa2017-11-17 15:09:30 +0100848 "Failed to initialize socket %s:%u -> %s:%u for MGCP GW: %s\n",
849 mgcp->actual.local_addr, mgcp->actual.local_port,
850 mgcp->actual.remote_addr, mgcp->actual.remote_port, strerror(errno));
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200851 goto error_close_fd;
852 }
853
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200854
Neels Hofmeyr0a403792018-12-12 03:10:18 +0100855 LOGP(DLMGCP, LOGL_INFO, "MGCP GW connection: %s\n", osmo_sock_get_name2(wq->bfd.fd));
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200856
857 return 0;
858error_close_fd:
859 close(wq->bfd.fd);
860 wq->bfd.fd = -1;
861 return rc;
862}
863
Philipp Maier275ac972018-01-20 00:58:16 +0100864/*! Get the IP-Aaddress of the associated MGW as string.
865 * \param[in] mgcp MGCP client descriptor.
866 * \returns a pointer to the address string. */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200867const char *mgcp_client_remote_addr_str(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200868{
869 return mgcp->actual.remote_addr;
870}
871
Philipp Maier275ac972018-01-20 00:58:16 +0100872/*! Get the IP-Port of the associated MGW.
873 * \param[in] mgcp MGCP client descriptor.
874 * \returns port number. */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200875uint16_t mgcp_client_remote_port(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200876{
877 return mgcp->actual.remote_port;
878}
879
Pau Espin Pedrol74d0e5c2020-09-02 17:19:20 +0200880/*! Get the IP-Address of the associated MGW as its numeric representation.
881 * DEPRECATED, DON'T USE.
Philipp Maier275ac972018-01-20 00:58:16 +0100882 * \param[in] mgcp MGCP client descriptor.
883 * \returns IP-Address as 32 bit integer (network byte order) */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200884uint32_t mgcp_client_remote_addr_n(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200885{
Pau Espin Pedrol74d0e5c2020-09-02 17:19:20 +0200886 return 0;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200887}
888
Neels Hofmeyrac69ea92018-12-19 00:27:50 +0100889/* To compose endpoint names, usually for CRCX, use this as domain name.
890 * For example, snprintf("rtpbridge\*@%s", mgcp_client_endpoint_domain(mgcp)). */
891const char *mgcp_client_endpoint_domain(const struct mgcp_client *mgcp)
892{
893 return mgcp->actual.endpoint_domain_name[0] ? mgcp->actual.endpoint_domain_name : "mgw";
894}
895
Pau Espin Pedrolca0818c2019-04-16 17:23:38 +0200896static const char *_mgcp_client_name_append_domain(const struct mgcp_client *mgcp, char *name)
Neels Hofmeyrac69ea92018-12-19 00:27:50 +0100897{
898 static char endpoint[MGCP_ENDPOINT_MAXLEN];
899 int rc;
900
Pau Espin Pedrolca0818c2019-04-16 17:23:38 +0200901 rc = snprintf(endpoint, sizeof(endpoint), "%s@%s", name, mgcp_client_endpoint_domain(mgcp));
Neels Hofmeyrac69ea92018-12-19 00:27:50 +0100902 if (rc > sizeof(endpoint) - 1) {
Pau Espin Pedrolca0818c2019-04-16 17:23:38 +0200903 LOGP(DLMGCP, LOGL_ERROR, "MGCP endpoint exceeds maximum length of %zu: '%s@%s'\n",
904 sizeof(endpoint) - 1, name, mgcp_client_endpoint_domain(mgcp));
Neels Hofmeyrac69ea92018-12-19 00:27:50 +0100905 return NULL;
906 }
907 if (rc < 1) {
908 LOGP(DLMGCP, LOGL_ERROR, "Cannot compose MGCP endpoint name\n");
909 return NULL;
910 }
911 return endpoint;
912}
913
Philipp Maiera466f572020-06-25 16:11:04 +0200914/*! Compose endpoint name for a wildcarded request to the virtual trunk
915 * \param[in] mgcp MGCP client descriptor.
916 * \returns string containing the endpoint name (e.g. rtpbridge\*@mgw) */
Pau Espin Pedrolca0818c2019-04-16 17:23:38 +0200917const char *mgcp_client_rtpbridge_wildcard(const struct mgcp_client *mgcp)
918{
919 return _mgcp_client_name_append_domain(mgcp, "rtpbridge/*");
920}
921
Philipp Maier48635912020-06-25 20:16:22 +0200922/*! Compose endpoint name for an E1 endpoint.
923 * \param[in] ctx talloc context.
924 * \param[in] mgcp MGCP client descriptor.
925 * \param[in] trunk_id id number of the E1 trunk (1-64).
926 * \param[in] ts timeslot on the E1 trunk (1-31).
927 * \param[in] rate bitrate used on the E1 trunk (e.g 16 for 16kbit).
928 * \param[in] offset bit offset of the E1 subslot (e.g. 4 for the third 16k subslot).
929 * \returns string containing the endpoint name (e.g. ds/e1-1/s-1/su16-4). */
930const char *mgcp_client_e1_epname(void *ctx, const struct mgcp_client *mgcp, uint8_t trunk_id, uint8_t ts,
931 uint8_t rate, uint8_t offset)
932{
933 /* See also comment in libosmo-mgcp, mgcp_client.c, gen_e1_epname() */
934 const uint8_t valid_rates[] = { 64, 32, 32, 16, 16, 16, 16, 8, 8, 8, 8, 8, 8, 8, 8 };
935 const uint8_t valid_offsets[] = { 0, 0, 4, 0, 2, 4, 6, 0, 1, 2, 3, 4, 5, 6, 7 };
936
937 uint8_t i;
938 bool rate_offs_valid = false;
939 char *epname;
940
941 epname =
942 talloc_asprintf(ctx, "ds/e1-%u/s-%u/su%u-%u@%s", trunk_id, ts, rate, offset,
943 mgcp_client_endpoint_domain(mgcp));
944 if (!epname) {
945 LOGP(DLMGCP, LOGL_ERROR, "Cannot compose MGCP e1-endpoint name!\n");
946 return NULL;
947 }
948
949 /* Check if the supplied rate/offset pair resembles a valid combination */
950 for (i = 0; i < sizeof(valid_rates); i++) {
951 if (valid_rates[i] == rate && valid_offsets[i] == offset)
952 rate_offs_valid = true;
953 }
954 if (!rate_offs_valid) {
955 LOGP(DLMGCP, LOGL_ERROR,
956 "Cannot compose MGCP e1-endpoint name (%s), rate(%u)/offset(%u) combination is invalid!\n", epname,
957 rate, offset);
958 talloc_free(epname);
959 return NULL;
960 }
961
962 /* An E1 line has a maximum of 32 timeslots, while the first (ts=0) is
963 * reserverd for framing and alignment, so we can not use it here. */
Philipp Maier92a73cd2020-11-26 22:21:11 +0100964 if (ts == 0 || ts > NUM_E1_TS-1) {
Philipp Maier48635912020-06-25 20:16:22 +0200965 LOGP(DLMGCP, LOGL_ERROR,
966 "Cannot compose MGCP e1-endpoint name (%s), E1-timeslot number (%u) is invalid!\n", epname, ts);
967 talloc_free(epname);
968 return NULL;
969 }
970
971 return epname;
972}
973
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200974struct mgcp_response_pending * mgcp_client_pending_add(
975 struct mgcp_client *mgcp,
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200976 mgcp_trans_id_t trans_id,
977 mgcp_response_cb_t response_cb,
978 void *priv)
979{
980 struct mgcp_response_pending *pending;
981
982 pending = talloc_zero(mgcp, struct mgcp_response_pending);
Harald Welte827da2c2020-07-15 10:57:08 +0200983 if (!pending)
984 return NULL;
985
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200986 pending->trans_id = trans_id;
987 pending->response_cb = response_cb;
988 pending->priv = priv;
989 llist_add_tail(&pending->entry, &mgcp->responses_pending);
990
991 return pending;
992}
993
994/* Send the MGCP message in msg to the MGCP GW and handle a response with
995 * response_cb. NOTE: the response_cb still needs to call
996 * mgcp_response_parse_params(response) to get the parsed parameters -- to
997 * potentially save some CPU cycles, only the head line has been parsed when
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100998 * the response_cb is invoked.
999 * Before the priv pointer becomes invalid, e.g. due to transaction timeout,
1000 * mgcp_client_cancel() needs to be called for this transaction.
1001 */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +02001002int mgcp_client_tx(struct mgcp_client *mgcp, struct msgb *msg,
1003 mgcp_response_cb_t response_cb, void *priv)
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001004{
Harald Welte563ffc52020-06-17 21:54:13 +07001005 struct mgcp_response_pending *pending = NULL;
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001006 mgcp_trans_id_t trans_id;
1007 int rc;
1008
1009 trans_id = msg->cb[MSGB_CB_MGCP_TRANS_ID];
1010 if (!trans_id) {
1011 LOGP(DLMGCP, LOGL_ERROR,
1012 "Unset transaction id in mgcp send request\n");
1013 talloc_free(msg);
1014 return -EINVAL;
1015 }
1016
Harald Welte563ffc52020-06-17 21:54:13 +07001017 /* Do not allocate a dummy 'mgcp_response_pending' entry */
1018 if (response_cb != NULL) {
1019 pending = mgcp_client_pending_add(mgcp, trans_id, response_cb, priv);
1020 if (!pending) {
1021 talloc_free(msg);
1022 return -ENOMEM;
1023 }
Harald Welte827da2c2020-07-15 10:57:08 +02001024 }
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001025
1026 if (msgb_l2len(msg) > 4096) {
1027 LOGP(DLMGCP, LOGL_ERROR,
1028 "Cannot send, MGCP message too large: %u\n",
1029 msgb_l2len(msg));
1030 msgb_free(msg);
1031 rc = -EINVAL;
1032 goto mgcp_tx_error;
1033 }
1034
1035 rc = osmo_wqueue_enqueue(&mgcp->wq, msg);
1036 if (rc) {
1037 LOGP(DLMGCP, LOGL_FATAL, "Could not queue message to MGCP GW\n");
1038 msgb_free(msg);
1039 goto mgcp_tx_error;
1040 } else
Neels Hofmeyred0c1aa2018-12-21 03:07:53 +01001041 LOGP(DLMGCP, LOGL_DEBUG, "Queued %u bytes for MGCP GW\n",
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001042 msgb_l2len(msg));
1043 return 0;
1044
1045mgcp_tx_error:
Harald Welte563ffc52020-06-17 21:54:13 +07001046 if (!pending)
Vadim Yanitskiyffbc6182020-07-16 15:48:13 +07001047 return rc;
Vadim Yanitskiy3f8139c2020-06-17 20:50:17 +07001048 /* Dequeue pending response, it's going to be free()d */
1049 llist_del(&pending->entry);
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001050 /* Pass NULL to response cb to indicate an error */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +02001051 mgcp_client_handle_response(mgcp, pending, NULL);
Vadim Yanitskiyffbc6182020-07-16 15:48:13 +07001052 return rc;
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001053}
1054
Philipp Maier275ac972018-01-20 00:58:16 +01001055/*! Cancel a pending transaction.
1056 * \param[in] mgcp MGCP client descriptor.
1057 * \param[in,out] trans_id Transaction id.
1058 * \returns 0 on success, -ENOENT on error.
1059 *
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +01001060 * Should a priv pointer passed to mgcp_client_tx() become invalid, this function must be called. In
1061 * practical terms, if the caller of mgcp_client_tx() wishes to tear down a transaction without having
1062 * received a response this function must be called. The trans_id can be obtained by calling
Philipp Maier275ac972018-01-20 00:58:16 +01001063 * mgcp_msg_trans_id() on the msgb produced by mgcp_msg_gen(). */
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +01001064int mgcp_client_cancel(struct mgcp_client *mgcp, mgcp_trans_id_t trans_id)
1065{
1066 struct mgcp_response_pending *pending = mgcp_client_response_pending_get(mgcp, trans_id);
1067 if (!pending) {
Philipp Maier275ac972018-01-20 00:58:16 +01001068 /*! Note: it is not harmful to cancel a transaction twice. */
Neels Hofmeyred0c1aa2018-12-21 03:07:53 +01001069 LOGP(DLMGCP, LOGL_ERROR, "Cannot cancel, no such transaction: %u\n", trans_id);
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +01001070 return -ENOENT;
1071 }
Neels Hofmeyred0c1aa2018-12-21 03:07:53 +01001072 LOGP(DLMGCP, LOGL_DEBUG, "Canceled transaction %u\n", trans_id);
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +01001073 talloc_free(pending);
1074 return 0;
Philipp Maier275ac972018-01-20 00:58:16 +01001075 /*! We don't really need to clean up the wqueue: In all sane cases, the msgb has already been sent
1076 * out and is no longer in the wqueue. If it still is in the wqueue, then sending MGCP messages
1077 * per se is broken and the program should notice so by a full wqueue. Even if this was called
1078 * before we had a chance to send out the message and it is still going to be sent, we will just
1079 * ignore the reply to it later. Removing a msgb from the wqueue here would just introduce more
1080 * bug surface in terms of failing to update wqueue API's counters or some such.
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +01001081 */
1082}
1083
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +02001084static mgcp_trans_id_t mgcp_client_next_trans_id(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001085{
1086 /* avoid zero trans_id to distinguish from unset trans_id */
1087 if (!mgcp->next_trans_id)
1088 mgcp->next_trans_id ++;
1089 return mgcp->next_trans_id ++;
1090}
1091
Philipp Maier1dc6be62017-10-05 18:25:37 +02001092#define MGCP_CRCX_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT | \
1093 MGCP_MSG_PRESENCE_CALL_ID | \
Philipp Maier1dc6be62017-10-05 18:25:37 +02001094 MGCP_MSG_PRESENCE_CONN_MODE)
1095#define MGCP_MDCX_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT | \
Philipp Maier490cbaa2018-01-22 17:32:38 +01001096 MGCP_MSG_PRESENCE_CALL_ID | \
Philipp Maier1dc6be62017-10-05 18:25:37 +02001097 MGCP_MSG_PRESENCE_CONN_ID)
1098#define MGCP_DLCX_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT)
1099#define MGCP_AUEP_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT)
1100#define MGCP_RSIP_MANDATORY 0 /* none */
1101
Philipp Maier704c4f02018-06-07 18:51:31 +02001102/* Helper function for mgcp_msg_gen(): Add LCO information to MGCP message */
1103static int add_lco(struct msgb *msg, struct mgcp_msg *mgcp_msg)
1104{
1105 unsigned int i;
1106 int rc = 0;
1107 const char *codec;
1108 unsigned int pt;
1109
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001110 rc |= msgb_printf(msg, "L:");
Philipp Maier704c4f02018-06-07 18:51:31 +02001111
1112 if (mgcp_msg->ptime)
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001113 rc |= msgb_printf(msg, " p:%u,", mgcp_msg->ptime);
Philipp Maier704c4f02018-06-07 18:51:31 +02001114
1115 if (mgcp_msg->codecs_len) {
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001116 rc |= msgb_printf(msg, " a:");
Philipp Maier704c4f02018-06-07 18:51:31 +02001117 for (i = 0; i < mgcp_msg->codecs_len; i++) {
1118 pt = mgcp_msg->codecs[i];
Neels Hofmeyr47642f22019-02-27 05:56:53 +01001119 codec = get_value_string_or_null(osmo_mgcpc_codec_names, pt);
Pau Espin Pedrolc12bfb72019-04-16 17:23:09 +02001120
Philipp Maier704c4f02018-06-07 18:51:31 +02001121 /* Note: Use codec descriptors from enum mgcp_codecs
1122 * in mgcp_client only! */
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001123 if (!codec) {
1124 msgb_free(msg);
1125 return -EINVAL;
1126 }
1127
1128 rc |= msgb_printf(msg, "%s", extract_codec_name(codec));
Philipp Maier704c4f02018-06-07 18:51:31 +02001129 if (i < mgcp_msg->codecs_len - 1)
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001130 rc |= msgb_printf(msg, ";");
Philipp Maier704c4f02018-06-07 18:51:31 +02001131 }
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001132 rc |= msgb_printf(msg, ",");
Philipp Maier704c4f02018-06-07 18:51:31 +02001133 }
1134
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001135 rc |= msgb_printf(msg, " nt:IN\r\n");
Philipp Maier704c4f02018-06-07 18:51:31 +02001136
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001137 if (rc != 0) {
1138 LOGP(DLMGCP, LOGL_ERROR,
1139 "message buffer to small, can not generate MGCP message (LCO)\n");
1140 msgb_free(msg);
1141 return -ENOBUFS;
1142 }
1143 return 0;
Philipp Maier704c4f02018-06-07 18:51:31 +02001144}
1145
1146/* Helper function for mgcp_msg_gen(): Add SDP information to MGCP message */
1147static int add_sdp(struct msgb *msg, struct mgcp_msg *mgcp_msg, struct mgcp_client *mgcp)
1148{
1149 unsigned int i;
1150 int rc = 0;
Pau Espin Pedrol8667d512020-08-28 19:37:08 +02001151 char local_ip[INET6_ADDRSTRLEN];
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +02001152 int local_ip_family, audio_ip_family;
Philipp Maier704c4f02018-06-07 18:51:31 +02001153 const char *codec;
1154 unsigned int pt;
1155
1156 /* Add separator to mark the beginning of the SDP block */
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001157 rc |= msgb_printf(msg, "\r\n");
Philipp Maier704c4f02018-06-07 18:51:31 +02001158
1159 /* Add SDP protocol version */
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001160 rc |= msgb_printf(msg, "v=0\r\n");
Philipp Maier704c4f02018-06-07 18:51:31 +02001161
1162 /* Determine local IP-Address */
1163 if (osmo_sock_local_ip(local_ip, mgcp->actual.remote_addr) < 0) {
1164 LOGP(DLMGCP, LOGL_ERROR,
1165 "Could not determine local IP-Address!\n");
1166 msgb_free(msg);
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001167 return -EINVAL;
Philipp Maier704c4f02018-06-07 18:51:31 +02001168 }
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +02001169 local_ip_family = osmo_ip_str_type(local_ip);
1170 if (local_ip_family == AF_UNSPEC) {
1171 msgb_free(msg);
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001172 return -EINVAL;
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +02001173 }
1174 audio_ip_family = osmo_ip_str_type(mgcp_msg->audio_ip);
1175 if (audio_ip_family == AF_UNSPEC) {
1176 msgb_free(msg);
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001177 return -EINVAL;
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +02001178 }
Philipp Maier704c4f02018-06-07 18:51:31 +02001179
1180 /* Add owner/creator (SDP) */
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001181 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 +02001182 local_ip_family == AF_INET6 ? '6' : '4',
1183 local_ip);
Philipp Maier704c4f02018-06-07 18:51:31 +02001184
1185 /* Add session name (none) */
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001186 rc |= msgb_printf(msg, "s=-\r\n");
Philipp Maier704c4f02018-06-07 18:51:31 +02001187
1188 /* Add RTP address and port */
1189 if (mgcp_msg->audio_port == 0) {
1190 LOGP(DLMGCP, LOGL_ERROR,
1191 "Invalid port number, can not generate MGCP message\n");
1192 msgb_free(msg);
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001193 return -EINVAL;
Philipp Maier704c4f02018-06-07 18:51:31 +02001194 }
1195 if (strlen(mgcp_msg->audio_ip) <= 0) {
1196 LOGP(DLMGCP, LOGL_ERROR,
1197 "Empty ip address, can not generate MGCP message\n");
1198 msgb_free(msg);
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001199 return -EINVAL;
Philipp Maier704c4f02018-06-07 18:51:31 +02001200 }
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001201 rc |= msgb_printf(msg, "c=IN IP%c %s\r\n",
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +02001202 audio_ip_family == AF_INET6 ? '6' : '4',
1203 mgcp_msg->audio_ip);
Philipp Maier704c4f02018-06-07 18:51:31 +02001204
1205 /* Add time description, active time (SDP) */
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001206 rc |= msgb_printf(msg, "t=0 0\r\n");
Philipp Maier704c4f02018-06-07 18:51:31 +02001207
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001208 rc |= msgb_printf(msg, "m=audio %u RTP/AVP", mgcp_msg->audio_port);
Philipp Maier704c4f02018-06-07 18:51:31 +02001209 for (i = 0; i < mgcp_msg->codecs_len; i++) {
1210 pt = map_codec_to_pt(mgcp_msg->ptmap, mgcp_msg->ptmap_len, mgcp_msg->codecs[i]);
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001211 rc |= msgb_printf(msg, " %u", pt);
Philipp Maier704c4f02018-06-07 18:51:31 +02001212
1213 }
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001214 rc |= msgb_printf(msg, "\r\n");
Philipp Maier704c4f02018-06-07 18:51:31 +02001215
Philipp Maier228e5912019-03-05 13:56:59 +01001216 /* Add optional codec parameters (fmtp) */
1217 if (mgcp_msg->param_present) {
1218 for (i = 0; i < mgcp_msg->codecs_len; i++) {
1219 /* The following is only applicable for AMR */
1220 if (mgcp_msg->codecs[i] != CODEC_AMR_8000_1 && mgcp_msg->codecs[i] != CODEC_AMRWB_16000_1)
1221 continue;
1222 pt = map_codec_to_pt(mgcp_msg->ptmap, mgcp_msg->ptmap_len, mgcp_msg->codecs[i]);
1223 if (mgcp_msg->param.amr_octet_aligned_present && mgcp_msg->param.amr_octet_aligned)
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001224 rc |= msgb_printf(msg, "a=fmtp:%u octet-align=1\r\n", pt);
Philipp Maier228e5912019-03-05 13:56:59 +01001225 else if (mgcp_msg->param.amr_octet_aligned_present && !mgcp_msg->param.amr_octet_aligned)
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001226 rc |= msgb_printf(msg, "a=fmtp:%u octet-align=0\r\n", pt);
Philipp Maier228e5912019-03-05 13:56:59 +01001227 }
1228 }
1229
Philipp Maier704c4f02018-06-07 18:51:31 +02001230 for (i = 0; i < mgcp_msg->codecs_len; i++) {
1231 pt = map_codec_to_pt(mgcp_msg->ptmap, mgcp_msg->ptmap_len, mgcp_msg->codecs[i]);
Pau Espin Pedrolc12bfb72019-04-16 17:23:09 +02001232
Philipp Maier704c4f02018-06-07 18:51:31 +02001233 /* Note: Only dynamic payload type from the range 96-127
1234 * require to be explained further via rtpmap. All others
1235 * are implcitly definedby the number in m=audio */
1236 if (pt >= 96 && pt <= 127) {
Neels Hofmeyr47642f22019-02-27 05:56:53 +01001237 codec = get_value_string_or_null(osmo_mgcpc_codec_names, mgcp_msg->codecs[i]);
Philipp Maier704c4f02018-06-07 18:51:31 +02001238
1239 /* Note: Use codec descriptors from enum mgcp_codecs
1240 * in mgcp_client only! */
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001241 if (!codec) {
1242 msgb_free(msg);
1243 return -EINVAL;
1244 }
Pau Espin Pedrolc12bfb72019-04-16 17:23:09 +02001245
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001246 rc |= msgb_printf(msg, "a=rtpmap:%u %s\r\n", pt, codec);
Philipp Maier704c4f02018-06-07 18:51:31 +02001247 }
1248 }
Pau Espin Pedrolc12bfb72019-04-16 17:23:09 +02001249
Philipp Maier704c4f02018-06-07 18:51:31 +02001250 if (mgcp_msg->ptime)
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001251 rc |= msgb_printf(msg, "a=ptime:%u\r\n", mgcp_msg->ptime);
Philipp Maier704c4f02018-06-07 18:51:31 +02001252
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001253 if (rc != 0) {
1254 LOGP(DLMGCP, LOGL_ERROR,
1255 "message buffer to small, can not generate MGCP message (SDP)\n");
1256 msgb_free(msg);
1257 return -ENOBUFS;
1258 }
1259
1260 return 0;
Philipp Maier704c4f02018-06-07 18:51:31 +02001261}
1262
Philipp Maier275ac972018-01-20 00:58:16 +01001263/*! Generate an MGCP message
1264 * \param[in] mgcp MGCP client descriptor.
1265 * \param[in] mgcp_msg Message description
1266 * \returns message buffer on success, NULL on error. */
Philipp Maier1dc6be62017-10-05 18:25:37 +02001267struct msgb *mgcp_msg_gen(struct mgcp_client *mgcp, struct mgcp_msg *mgcp_msg)
1268{
1269 mgcp_trans_id_t trans_id = mgcp_client_next_trans_id(mgcp);
1270 uint32_t mandatory_mask;
1271 struct msgb *msg = msgb_alloc_headroom(4096, 128, "MGCP tx");
1272 int rc = 0;
Philipp Maier704c4f02018-06-07 18:51:31 +02001273 bool use_sdp = false;
Pau Espin Pedrol900cd652019-04-24 22:06:22 +02001274 char buf[32];
Philipp Maier1dc6be62017-10-05 18:25:37 +02001275
1276 msg->l2h = msg->data;
1277 msg->cb[MSGB_CB_MGCP_TRANS_ID] = trans_id;
1278
1279 /* Add command verb */
1280 switch (mgcp_msg->verb) {
1281 case MGCP_VERB_CRCX:
1282 mandatory_mask = MGCP_CRCX_MANDATORY;
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001283 rc |= msgb_printf(msg, "CRCX %u", trans_id);
Philipp Maier1dc6be62017-10-05 18:25:37 +02001284 break;
1285 case MGCP_VERB_MDCX:
1286 mandatory_mask = MGCP_MDCX_MANDATORY;
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001287 rc |= msgb_printf(msg, "MDCX %u", trans_id);
Philipp Maier1dc6be62017-10-05 18:25:37 +02001288 break;
1289 case MGCP_VERB_DLCX:
1290 mandatory_mask = MGCP_DLCX_MANDATORY;
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001291 rc |= msgb_printf(msg, "DLCX %u", trans_id);
Philipp Maier1dc6be62017-10-05 18:25:37 +02001292 break;
1293 case MGCP_VERB_AUEP:
1294 mandatory_mask = MGCP_AUEP_MANDATORY;
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001295 rc |= msgb_printf(msg, "AUEP %u", trans_id);
Philipp Maier1dc6be62017-10-05 18:25:37 +02001296 break;
1297 case MGCP_VERB_RSIP:
1298 mandatory_mask = MGCP_RSIP_MANDATORY;
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001299 rc |= msgb_printf(msg, "RSIP %u", trans_id);
Philipp Maier1dc6be62017-10-05 18:25:37 +02001300 break;
1301 default:
1302 LOGP(DLMGCP, LOGL_ERROR,
1303 "Invalid command verb, can not generate MGCP message\n");
1304 msgb_free(msg);
1305 return NULL;
1306 }
1307
1308 /* Check if mandatory fields are missing */
1309 if (!((mgcp_msg->presence & mandatory_mask) == mandatory_mask)) {
1310 LOGP(DLMGCP, LOGL_ERROR,
1311 "One or more missing mandatory fields, can not generate MGCP message\n");
1312 msgb_free(msg);
1313 return NULL;
1314 }
1315
1316 /* Add endpoint name */
Philipp Maier7bc55522017-12-10 22:52:22 +01001317 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_ENDPOINT) {
1318 if (strlen(mgcp_msg->endpoint) <= 0) {
1319 LOGP(DLMGCP, LOGL_ERROR,
1320 "Empty endpoint name, can not generate MGCP message\n");
1321 msgb_free(msg);
1322 return NULL;
1323 }
Philipp Maier3aa81572018-01-31 14:13:45 +01001324
1325 if (strstr(mgcp_msg->endpoint, "@") == NULL) {
1326 LOGP(DLMGCP, LOGL_ERROR,
1327 "Endpoint name (%s) lacks separator (@), can not generate MGCP message\n",
1328 mgcp_msg->endpoint);
1329 msgb_free(msg);
Vadim Yanitskiye6743452020-06-18 03:04:13 +07001330 return NULL;
Philipp Maier3aa81572018-01-31 14:13:45 +01001331 }
1332
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001333 rc |= msgb_printf(msg, " %s", mgcp_msg->endpoint);
Philipp Maier7bc55522017-12-10 22:52:22 +01001334 }
Philipp Maier1dc6be62017-10-05 18:25:37 +02001335
1336 /* Add protocol version */
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001337 rc |= msgb_printf(msg, " MGCP 1.0\r\n");
Philipp Maier1dc6be62017-10-05 18:25:37 +02001338
1339 /* Add call id */
1340 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_CALL_ID)
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001341 rc |= msgb_printf(msg, "C: %x\r\n", mgcp_msg->call_id);
Philipp Maier1dc6be62017-10-05 18:25:37 +02001342
1343 /* Add connection id */
Philipp Maier7bc55522017-12-10 22:52:22 +01001344 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_CONN_ID) {
1345 if (strlen(mgcp_msg->conn_id) <= 0) {
1346 LOGP(DLMGCP, LOGL_ERROR,
1347 "Empty connection id, can not generate MGCP message\n");
1348 msgb_free(msg);
1349 return NULL;
1350 }
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001351 rc |= msgb_printf(msg, "I: %s\r\n", mgcp_msg->conn_id);
Philipp Maier7bc55522017-12-10 22:52:22 +01001352 }
Philipp Maier1dc6be62017-10-05 18:25:37 +02001353
Philipp Maier704c4f02018-06-07 18:51:31 +02001354 /* Using SDP makes sense when a valid IP/Port combination is specifiec,
1355 * if we do not know this information yet, we fall back to LCO */
1356 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_AUDIO_IP
1357 && mgcp_msg->presence & MGCP_MSG_PRESENCE_AUDIO_PORT)
1358 use_sdp = true;
1359
1360 /* Add local connection options (LCO) */
1361 if (!use_sdp
1362 && (mgcp_msg->verb == MGCP_VERB_CRCX
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001363 || mgcp_msg->verb == MGCP_VERB_MDCX)) {
1364 if (add_lco(msg, mgcp_msg) < 0)
1365 return NULL;
1366 }
Philipp Maier1dc6be62017-10-05 18:25:37 +02001367
1368 /* Add mode */
1369 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_CONN_MODE)
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001370 rc |=
Philipp Maier1dc6be62017-10-05 18:25:37 +02001371 msgb_printf(msg, "M: %s\r\n",
1372 mgcp_client_cmode_name(mgcp_msg->conn_mode));
1373
Neels Hofmeyre6d8e912018-08-23 16:36:48 +02001374 /* Add X-Osmo-IGN */
1375 if ((mgcp_msg->presence & MGCP_MSG_PRESENCE_X_OSMO_IGN)
1376 && (mgcp_msg->x_osmo_ign != 0))
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001377 rc |=
Neels Hofmeyre6d8e912018-08-23 16:36:48 +02001378 msgb_printf(msg, MGCP_X_OSMO_IGN_HEADER "%s\r\n",
1379 mgcp_msg->x_osmo_ign & MGCP_X_OSMO_IGN_CALLID ? " C": "");
1380
Pau Espin Pedrol900cd652019-04-24 22:06:22 +02001381 /* Add X-Osmo-Osmux */
1382 if ((mgcp_msg->presence & MGCP_MSG_PRESENCE_X_OSMO_OSMUX_CID)) {
Pau Espin Pedrol1b1d7ed2019-05-23 16:48:24 +02001383 if (mgcp_msg->x_osmo_osmux_cid < -1 || mgcp_msg->x_osmo_osmux_cid > OSMUX_CID_MAX) {
1384 LOGP(DLMGCP, LOGL_ERROR,
1385 "Wrong Osmux CID %d, can not generate MGCP message\n",
1386 mgcp_msg->x_osmo_osmux_cid);
1387 msgb_free(msg);
1388 return NULL;
1389 }
Pau Espin Pedrol900cd652019-04-24 22:06:22 +02001390 snprintf(buf, sizeof(buf), " %d", mgcp_msg->x_osmo_osmux_cid);
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001391 rc |=
Pau Espin Pedrol900cd652019-04-24 22:06:22 +02001392 msgb_printf(msg, MGCP_X_OSMO_OSMUX_HEADER "%s\r\n",
1393 mgcp_msg->x_osmo_osmux_cid == -1 ? " *": buf);
1394 }
1395
1396
Philipp Maier704c4f02018-06-07 18:51:31 +02001397 /* Add session description protocol (SDP) */
1398 if (use_sdp
1399 && (mgcp_msg->verb == MGCP_VERB_CRCX
1400 || mgcp_msg->verb == MGCP_VERB_MDCX)) {
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001401 if (add_sdp(msg, mgcp_msg, mgcp) < 0)
Philipp Maier9d25d7a2018-01-22 17:31:10 +01001402 return NULL;
Philipp Maier1dc6be62017-10-05 18:25:37 +02001403 }
1404
1405 if (rc != 0) {
1406 LOGP(DLMGCP, LOGL_ERROR,
1407 "message buffer to small, can not generate MGCP message\n");
1408 msgb_free(msg);
1409 msg = NULL;
1410 }
1411
1412 return msg;
1413}
1414
Philipp Maier275ac972018-01-20 00:58:16 +01001415/*! Retrieve the MGCP transaction ID from a msgb generated by mgcp_msg_gen()
1416 * \param[in] msg message buffer
1417 * \returns Transaction id. */
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +01001418mgcp_trans_id_t mgcp_msg_trans_id(struct msgb *msg)
1419{
1420 return (mgcp_trans_id_t)msg->cb[MSGB_CB_MGCP_TRANS_ID];
1421}
1422
Philipp Maier275ac972018-01-20 00:58:16 +01001423/*! Get the configuration parameters a given MGCP client instance
1424 * \param[in] mgcp MGCP client descriptor.
1425 * \returns configuration */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +02001426struct mgcp_client_conf *mgcp_client_conf_actual(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001427{
1428 return &mgcp->actual;
1429}
Neels Hofmeyrd95ab1e2017-09-22 00:52:54 +02001430
1431const struct value_string mgcp_client_connection_mode_strs[] = {
1432 { MGCP_CONN_NONE, "none" },
1433 { MGCP_CONN_RECV_SEND, "sendrecv" },
1434 { MGCP_CONN_SEND_ONLY, "sendonly" },
1435 { MGCP_CONN_RECV_ONLY, "recvonly" },
1436 { MGCP_CONN_LOOPBACK, "loopback" },
1437 { 0, NULL }
1438};