blob: 9d60ee889fbc96389cb322c7b06cccf57f58ff98 [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
72 /* FIXME osmo_strlcpy */
73 osmo_strlcpy(buf, str, sizeof(buf));
74
75 for (i = 0; i < strlen(buf); i++) {
76 if (buf[i] == '/')
77 buf[i] = '\0';
78 }
79
80 return buf;
81}
82
83/*! Map a string to a codec.
84 * \ptmap[in] str input string (e.g "GSM/8000/1", "GSM/8000" or "GSM")
85 * \returns codec that corresponds to the given string representation. */
86enum mgcp_codecs map_str_to_codec(const char *str)
87{
88 unsigned int i;
89 char *codec_name;
90 char str_buf[64];
91
92 osmo_strlcpy(str_buf, extract_codec_name(str), sizeof(str_buf));
93
Neels Hofmeyr47642f22019-02-27 05:56:53 +010094 for (i = 0; i < ARRAY_SIZE(osmo_mgcpc_codec_names); i++) {
95 codec_name = extract_codec_name(osmo_mgcpc_codec_names[i].str);
Philipp Maier704c4f02018-06-07 18:51:31 +020096 if (!codec_name)
97 continue;
98 if (strcmp(codec_name, str_buf) == 0)
Neels Hofmeyr47642f22019-02-27 05:56:53 +010099 return osmo_mgcpc_codec_names[i].value;
Philipp Maier704c4f02018-06-07 18:51:31 +0200100 }
101
102 return -1;
103}
104
105/* Check the ptmap for illegal mappings */
Neels Hofmeyr84274e42019-04-27 19:08:36 +0200106static int check_ptmap(const struct ptmap *ptmap)
Philipp Maier704c4f02018-06-07 18:51:31 +0200107{
108 /* Check if there are mappings that leave the IANA assigned dynamic
109 * payload type range. Under normal conditions such mappings should
110 * not occur */
111
112 /* Its ok to have a 1:1 mapping in the statically defined
113 * range, this won't hurt */
114 if (ptmap->codec == ptmap->pt)
115 return 0;
116
117 if (ptmap->codec < 96 || ptmap->codec > 127)
118 goto error;
119 if (ptmap->pt < 96 || ptmap->pt > 127)
120 goto error;
121
122 return 0;
123error:
124 LOGP(DLMGCP, LOGL_ERROR,
125 "ptmap contains illegal mapping: codec=%u maps to pt=%u\n",
126 ptmap->codec, ptmap->pt);
127 return -1;
128}
129
130/*! Map a codec to a payload type.
131 * \ptmap[in] payload pointer to payload type map with specified payload types.
132 * \ptmap[in] ptmap_len length of the payload type map.
133 * \ptmap[in] codec the codec for which the payload type should be looked up.
134 * \returns assigned payload type */
Neels Hofmeyr84274e42019-04-27 19:08:36 +0200135unsigned int map_codec_to_pt(const struct ptmap *ptmap, unsigned int ptmap_len,
Philipp Maier704c4f02018-06-07 18:51:31 +0200136 enum mgcp_codecs codec)
137{
138 unsigned int i;
139
140 /*! Note: If the payload type map is empty or the codec is not found
141 * in the map, then a 1:1 mapping is performed. If the codec falls
142 * into the statically defined range or if the mapping table isself
143 * tries to map to the statically defined range, then the mapping
144 * is also ignored and a 1:1 mapping is performed instead. */
145
146 /* we may return the codec directly since enum mgcp_codecs directly
147 * corresponds to the statićally assigned payload types */
148 if (codec < 96 || codec > 127)
149 return codec;
150
151 for (i = 0; i < ptmap_len; i++) {
152 /* Skip illegal map entries */
153 if (check_ptmap(ptmap) == 0 && ptmap->codec == codec)
154 return ptmap->pt;
155 ptmap++;
156 }
157
158 /* If nothing is found, do not perform any mapping */
159 return codec;
160}
161
162/*! Map a payload type to a codec.
163 * \ptmap[in] payload pointer to payload type map with specified payload types.
164 * \ptmap[in] ptmap_len length of the payload type map.
165 * \ptmap[in] payload type for which the codec should be looked up.
166 * \returns codec that corresponds to the specified payload type */
167enum mgcp_codecs map_pt_to_codec(struct ptmap *ptmap, unsigned int ptmap_len,
168 unsigned int pt)
169{
170 unsigned int i;
171
172 /*! Note: If the payload type map is empty or the payload type is not
173 * found in the map, then a 1:1 mapping is performed. If the payload
174 * type falls into the statically defined range or if the mapping
175 * table isself tries to map to the statically defined range, then
176 * the mapping is also ignored and a 1:1 mapping is performed
177 * instead. */
178
179 /* See also note in map_codec_to_pt() */
180 if (pt < 96 || pt > 127)
181 return pt;
182
183 for (i = 0; i < ptmap_len; i++) {
184 if (check_ptmap(ptmap) == 0 && ptmap->pt == pt)
185 return ptmap->codec;
186 ptmap++;
187 }
188
189 /* If nothing is found, do not perform any mapping */
190 return pt;
191}
192
Philipp Maier275ac972018-01-20 00:58:16 +0100193/*! Initalize MGCP client configuration struct with default values.
194 * \param[out] conf Client configuration.*/
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200195void mgcp_client_conf_init(struct mgcp_client_conf *conf)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200196{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200197 /* NULL and -1 default to MGCP_CLIENT_*_DEFAULT values */
198 *conf = (struct mgcp_client_conf){
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200199 .local_addr = NULL,
200 .local_port = -1,
201 .remote_addr = NULL,
202 .remote_port = -1,
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200203 };
204}
205
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200206static void mgcp_client_handle_response(struct mgcp_client *mgcp,
207 struct mgcp_response_pending *pending,
208 struct mgcp_response *response)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200209{
210 if (!pending) {
211 LOGP(DLMGCP, LOGL_ERROR,
212 "Cannot handle NULL response\n");
213 return;
214 }
215 if (pending->response_cb)
216 pending->response_cb(response, pending->priv);
217 else
Neels Hofmeyred0c1aa2018-12-21 03:07:53 +0100218 LOGP(DLMGCP, LOGL_DEBUG, "MGCP response ignored (NULL cb)\n");
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200219 talloc_free(pending);
220}
221
222static int mgcp_response_parse_head(struct mgcp_response *r, struct msgb *msg)
223{
224 int comment_pos;
225 char *end;
226
227 if (mgcp_msg_terminate_nul(msg))
228 goto response_parse_failure;
229
230 r->body = (char *)msg->data;
231
Harald Welte9bf7c532017-11-17 14:14:31 +0100232 if (sscanf(r->body, "%3d %u %n",
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200233 &r->head.response_code, &r->head.trans_id,
234 &comment_pos) != 2)
235 goto response_parse_failure;
236
Philipp Maierabe8c892018-01-20 00:15:12 +0100237 osmo_strlcpy(r->head.comment, r->body + comment_pos, sizeof(r->head.comment));
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200238 end = strchr(r->head.comment, '\r');
239 if (!end)
240 goto response_parse_failure;
241 /* Mark the end of the comment */
242 *end = '\0';
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200243 return 0;
244
245response_parse_failure:
246 LOGP(DLMGCP, LOGL_ERROR,
247 "Failed to parse MGCP response header\n");
248 return -EINVAL;
249}
250
251/* TODO undup against mgcp_protocol.c:mgcp_check_param() */
252static bool mgcp_line_is_valid(const char *line)
253{
254 const size_t line_len = strlen(line);
255 if (line[0] == '\0')
256 return true;
257
258 if (line_len < 2
259 || line[1] != '=') {
260 LOGP(DLMGCP, LOGL_ERROR,
261 "Wrong MGCP option format: '%s'\n",
262 line);
263 return false;
264 }
265
266 return true;
267}
268
Philipp Maier704c4f02018-06-07 18:51:31 +0200269/* Parse a line like "m=audio 16002 RTP/AVP 98", extract port and payload types */
270static int mgcp_parse_audio_port_pt(struct mgcp_response *r, char *line)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200271{
Philipp Maier704c4f02018-06-07 18:51:31 +0200272 char *pt_str;
Pau Espin Pedrolc5c14302019-07-23 16:19:41 +0200273 char *pt_end;
Pau Espin Pedrola2b1c5e2019-07-26 14:13:14 +0200274 unsigned long int pt;
Philipp Maier704c4f02018-06-07 18:51:31 +0200275 unsigned int count = 0;
276 unsigned int i;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200277
Philipp Maier704c4f02018-06-07 18:51:31 +0200278 /* Extract port information */
279 if (sscanf(line, "m=audio %hu", &r->audio_port) != 1)
280 goto response_parse_failure_port;
Philipp Maier10f32db2017-12-13 12:34:34 +0100281 if (r->audio_port == 0)
Philipp Maier704c4f02018-06-07 18:51:31 +0200282 goto response_parse_failure_port;
Philipp Maier10f32db2017-12-13 12:34:34 +0100283
Philipp Maier704c4f02018-06-07 18:51:31 +0200284 /* Extract payload types */
285 line = strstr(line, "RTP/AVP ");
286 if (!line)
287 goto exit;
288
289 pt_str = strtok(line, " ");
290 while (1) {
291 /* Do not allow excessive payload types */
292 if (count > ARRAY_SIZE(r->codecs))
293 goto response_parse_failure_pt;
294
295 pt_str = strtok(NULL, " ");
296 if (!pt_str)
297 break;
Pau Espin Pedrolc5c14302019-07-23 16:19:41 +0200298 errno = 0;
299 pt = strtoul(pt_str, &pt_end, 0);
300 if ((errno == ERANGE && pt == ULONG_MAX) || (errno && !pt) ||
301 pt_str == pt_end)
302 goto response_parse_failure_pt;
Philipp Maier704c4f02018-06-07 18:51:31 +0200303
Pau Espin Pedrola2b1c5e2019-07-26 14:13:14 +0200304 if (pt >> 7) /* PT is 7 bit field, higher values not allowed */
305 goto response_parse_failure_pt;
306
Philipp Maier704c4f02018-06-07 18:51:31 +0200307 /* Do not allow duplicate payload types */
308 for (i = 0; i < count; i++)
309 if (r->codecs[i] == pt)
310 goto response_parse_failure_pt;
311
312 /* Note: The payload type we store may not necessarly match
313 * the codec types we have defined in enum mgcp_codecs. To
314 * ensure that the end result only contains codec types which
315 * match enum mgcp_codecs, we will go through afterwards and
316 * remap the affected entries with the inrofmation we learn
317 * from rtpmap */
318 r->codecs[count] = pt;
319 count++;
320 }
321
322 r->codecs_len = count;
323
324exit:
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200325 return 0;
326
Philipp Maier704c4f02018-06-07 18:51:31 +0200327response_parse_failure_port:
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200328 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier704c4f02018-06-07 18:51:31 +0200329 "Failed to parse SDP parameter port (%s)\n", line);
Philipp Maier06da85e2017-10-05 18:49:24 +0200330 return -EINVAL;
Philipp Maier704c4f02018-06-07 18:51:31 +0200331
332response_parse_failure_pt:
333 LOGP(DLMGCP, LOGL_ERROR,
334 "Failed to parse SDP parameter payload types (%s)\n", line);
335 return -EINVAL;
336}
337
338/* Parse a line like "m=audio 16002 RTP/AVP 98", extract port and payload types */
339static int mgcp_parse_audio_ptime_rtpmap(struct mgcp_response *r, const char *line)
340{
341 unsigned int pt;
342 char codec_resp[64];
Neels Hofmeyr3ab8ca42019-08-08 04:03:42 +0200343 enum mgcp_codecs codec;
Pau Espin Pedrolc12bfb72019-04-16 17:23:09 +0200344
Neels Hofmeyr23f40482019-08-08 04:03:42 +0200345#define A_PTIME "a=ptime:"
346#define A_RTPMAP "a=rtpmap:"
Pau Espin Pedrolc12bfb72019-04-16 17:23:09 +0200347
Neels Hofmeyr23f40482019-08-08 04:03:42 +0200348 if (osmo_str_startswith(line, A_PTIME)) {
349 if (sscanf(line, A_PTIME "%u", &r->ptime) != 1) {
350 LOGP(DLMGCP, LOGL_ERROR,
351 "Failed to parse SDP parameter, invalid ptime (%s)\n", line);
352 return -EINVAL;
353 }
354 } else if (osmo_str_startswith(line, A_RTPMAP)) {
355 if (sscanf(line, A_RTPMAP "%d %63s", &pt, codec_resp) != 2) {
356 LOGP(DLMGCP, LOGL_ERROR,
357 "Failed to parse SDP parameter, invalid rtpmap: %s\n", osmo_quote_str(line, -1));
358 return -EINVAL;
359 }
Neels Hofmeyr3ab8ca42019-08-08 04:03:42 +0200360 if (r->ptmap_len >= ARRAY_SIZE(r->ptmap)) {
361 LOGP(DLMGCP, LOGL_ERROR, "No more space in ptmap array (len=%u)\n", r->ptmap_len);
362 return -ENOSPC;
Neels Hofmeyr23f40482019-08-08 04:03:42 +0200363 }
Neels Hofmeyr3ab8ca42019-08-08 04:03:42 +0200364 codec = map_str_to_codec(codec_resp);
365 r->ptmap[r->ptmap_len].pt = pt;
366 r->ptmap[r->ptmap_len].codec = codec;
367 r->ptmap_len++;
Philipp Maier704c4f02018-06-07 18:51:31 +0200368 }
Pau Espin Pedrolc12bfb72019-04-16 17:23:09 +0200369
Philipp Maier704c4f02018-06-07 18:51:31 +0200370 return 0;
Philipp Maier06da85e2017-10-05 18:49:24 +0200371}
372
373/* Parse a line like "c=IN IP4 10.11.12.13" */
374static int mgcp_parse_audio_ip(struct mgcp_response *r, const char *line)
375{
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +0200376 struct in6_addr ip_test;
377 bool is_ipv6;
Philipp Maier06da85e2017-10-05 18:49:24 +0200378
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +0200379 if (strncmp("c=IN IP", line, 7) != 0)
Philipp Maier06da85e2017-10-05 18:49:24 +0200380 goto response_parse_failure;
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +0200381 line += 7;
382 if (*line == '6')
383 is_ipv6 = true;
384 else if (*line == '4')
385 is_ipv6 = false;
386 else
Philipp Maier06da85e2017-10-05 18:49:24 +0200387 goto response_parse_failure;
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +0200388 line++;
389 if (*line != ' ')
Philipp Maier06da85e2017-10-05 18:49:24 +0200390 goto response_parse_failure;
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +0200391 line++;
Philipp Maier06da85e2017-10-05 18:49:24 +0200392
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +0200393 /* Extract and check IP-Address */
394 if (is_ipv6) {
395 /* 45 = INET6_ADDRSTRLEN -1 */
396 if (sscanf(line, "%45s", r->audio_ip) != 1)
397 goto response_parse_failure;
398 if (inet_pton(AF_INET6, r->audio_ip, &ip_test) != 1)
399 goto response_parse_failure;
400 } else {
401 /* 15 = INET_ADDRSTRLEN -1 */
402 if (sscanf(line, "%15s", r->audio_ip) != 1)
403 goto response_parse_failure;
404 if (inet_pton(AF_INET, r->audio_ip, &ip_test) != 1)
405 goto response_parse_failure;
406 }
Philipp Maier06da85e2017-10-05 18:49:24 +0200407 return 0;
408
409response_parse_failure:
410 LOGP(DLMGCP, LOGL_ERROR,
411 "Failed to parse MGCP response header (audio ip)\n");
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200412 return -EINVAL;
413}
414
Pau Espin Pedrol91088c32019-04-24 21:02:40 +0200415/*! Extract OSMUX CID from an MGCP parameter line (string).
416 * \param[in] line single parameter line from the MGCP message
417 * \returns OSMUX CID, -1 wildcard, -2 on error
418 * FIXME: This is a copy of function in mgcp_msg.c. Have some common.c file between both libs?
419 */
420static int mgcp_parse_osmux_cid(const char *line)
421{
422 int osmux_cid;
423
424
Pau Espin Pedrolc1bf4692019-05-14 16:23:24 +0200425 if (strcasecmp(line + 2, "Osmux: *") == 0) {
Pau Espin Pedrol91088c32019-04-24 21:02:40 +0200426 LOGP(DLMGCP, LOGL_DEBUG, "Parsed wilcard Osmux CID\n");
427 return -1;
428 }
429
Pau Espin Pedrolc1bf4692019-05-14 16:23:24 +0200430 if (sscanf(line + 2 + 7, "%u", &osmux_cid) != 1) {
Pau Espin Pedrol91088c32019-04-24 21:02:40 +0200431 LOGP(DLMGCP, LOGL_ERROR, "Failed parsing Osmux in MGCP msg line: %s\n",
432 line);
433 return -2;
434 }
435
Pau Espin Pedrol91088c32019-04-24 21:02:40 +0200436 if (osmux_cid > OSMUX_CID_MAX) { /* OSMUX_CID_MAX from libosmo-netif */
437 LOGP(DLMGCP, LOGL_ERROR, "Osmux ID too large: %u > %u\n",
438 osmux_cid, OSMUX_CID_MAX);
439 return -2;
440 }
441 LOGP(DLMGCP, LOGL_DEBUG, "bsc-nat offered Osmux CID %u\n", osmux_cid);
442
443 return osmux_cid;
444}
445
Philipp Maier3b12e1b2018-01-18 15:16:13 +0100446/* A new section is marked by a double line break, check a few more
447 * patterns as there may be variants */
448static char *mgcp_find_section_end(char *string)
449{
450 char *rc;
451
452 rc = strstr(string, "\n\n");
453 if (rc)
454 return rc;
455
456 rc = strstr(string, "\n\r\n\r");
457 if (rc)
458 return rc;
459
460 rc = strstr(string, "\r\n\r\n");
461 if (rc)
462 return rc;
463
464 return NULL;
465}
466
Philipp Maier275ac972018-01-20 00:58:16 +0100467/*! Parse body (SDP) parameters of the MGCP response
468 * \param[in,out] r Response data
469 * \returns 0 on success, -EINVAL on error. */
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200470int mgcp_response_parse_params(struct mgcp_response *r)
471{
472 char *line;
473 int rc;
Neels Hofmeyr0793d2f2018-02-21 14:55:34 +0100474 char *data;
Philipp Maiere9d645b2018-01-19 23:54:08 +0100475 char *data_ptr;
Philipp Maier704c4f02018-06-07 18:51:31 +0200476 int i;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200477
Philipp Maiere9d645b2018-01-19 23:54:08 +0100478 /* Since this functions performs a destructive parsing, we create a
479 * local copy of the body data */
Neels Hofmeyr0793d2f2018-02-21 14:55:34 +0100480 OSMO_ASSERT(r->body);
481 data = talloc_strdup(r, r->body);
Philipp Maiere9d645b2018-01-19 23:54:08 +0100482 OSMO_ASSERT(data);
Philipp Maier55295f72018-01-15 14:00:28 +0100483
Philipp Maiere9d645b2018-01-19 23:54:08 +0100484 /* Find beginning of the parameter (SDP) section */
485 data_ptr = mgcp_find_section_end(data);
Neels Hofmeyr10835332018-02-21 14:55:34 +0100486 if (!data_ptr) {
Neels Hofmeyre8278312019-09-19 03:06:46 +0200487 LOGP(DLMGCP, LOGL_DEBUG, "MGCP response contains no SDP parameters\n");
488 rc = 0;
Philipp Maiere9d645b2018-01-19 23:54:08 +0100489 goto exit;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200490 }
491
Neels Hofmeyr0793d2f2018-02-21 14:55:34 +0100492 /* data_ptr now points to the beginning of the section-end-marker; for_each_non_empty_line()
493 * skips any \r and \n characters for free, so we don't need to skip the marker. */
494
Philipp Maiere9d645b2018-01-19 23:54:08 +0100495 for_each_non_empty_line(line, data_ptr) {
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200496 if (!mgcp_line_is_valid(line))
497 return -EINVAL;
498
499 switch (line[0]) {
Philipp Maier704c4f02018-06-07 18:51:31 +0200500 case 'a':
501 rc = mgcp_parse_audio_ptime_rtpmap(r, line);
502 if (rc)
503 goto exit;
504 break;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200505 case 'm':
Philipp Maier704c4f02018-06-07 18:51:31 +0200506 rc = mgcp_parse_audio_port_pt(r, line);
Philipp Maier06da85e2017-10-05 18:49:24 +0200507 if (rc)
Philipp Maiere9d645b2018-01-19 23:54:08 +0100508 goto exit;
Philipp Maier06da85e2017-10-05 18:49:24 +0200509 break;
510 case 'c':
511 rc = mgcp_parse_audio_ip(r, line);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200512 if (rc)
Philipp Maiere9d645b2018-01-19 23:54:08 +0100513 goto exit;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200514 break;
515 default:
516 /* skip unhandled parameters */
517 break;
518 }
519 }
Philipp Maiere9d645b2018-01-19 23:54:08 +0100520
Philipp Maier704c4f02018-06-07 18:51:31 +0200521 /* See also note in mgcp_parse_audio_port_pt() */
522 for (i = 0; i < r->codecs_len; i++)
523 r->codecs[i] = map_pt_to_codec(r->ptmap, r->ptmap_len, r->codecs[i]);
524
Philipp Maiere9d645b2018-01-19 23:54:08 +0100525 rc = 0;
526exit:
527 talloc_free(data);
528 return rc;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200529}
530
Philipp Maier55295f72018-01-15 14:00:28 +0100531/* Parse a line like "X: something" */
532static int mgcp_parse_head_param(char *result, unsigned int result_len,
533 char label, const char *line)
Philipp Maierffd75e42017-11-22 11:44:50 +0100534{
Philipp Maier55295f72018-01-15 14:00:28 +0100535 char label_string[4];
Neels Hofmeyr23e7bf12018-09-03 21:26:22 +0200536 size_t rc;
Philipp Maier55295f72018-01-15 14:00:28 +0100537
538 /* Detect empty parameters */
Philipp Maierffd75e42017-11-22 11:44:50 +0100539 if (strlen(line) < 4)
540 goto response_parse_failure;
541
Philipp Maier55295f72018-01-15 14:00:28 +0100542 /* Check if the label matches */
543 snprintf(label_string, sizeof(label_string), "%c: ", label);
544 if (memcmp(label_string, line, 3) != 0)
Philipp Maierffd75e42017-11-22 11:44:50 +0100545 goto response_parse_failure;
546
Philipp Maier55295f72018-01-15 14:00:28 +0100547 /* Copy payload part of the string to destinations (the label string
548 * is always 3 chars long) */
Neels Hofmeyr23e7bf12018-09-03 21:26:22 +0200549 rc = osmo_strlcpy(result, line + 3, result_len);
550 if (rc >= result_len) {
551 LOGP(DLMGCP, LOGL_ERROR,
552 "Failed to parse MGCP response (parameter label: %c):"
553 " the received conn ID is too long: %zu, maximum is %u characters\n",
554 label, rc, result_len - 1);
555 return -ENOSPC;
556 }
Philipp Maierffd75e42017-11-22 11:44:50 +0100557 return 0;
558
559response_parse_failure:
560 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier55295f72018-01-15 14:00:28 +0100561 "Failed to parse MGCP response (parameter label: %c)\n", label);
Philipp Maierffd75e42017-11-22 11:44:50 +0100562 return -EINVAL;
563}
564
565/* Parse MGCP parameters of the response */
566static int parse_head_params(struct mgcp_response *r)
567{
568 char *line;
569 int rc = 0;
570 OSMO_ASSERT(r->body);
Philipp Maier55295f72018-01-15 14:00:28 +0100571 char *data;
572 char *data_ptr;
573 char *data_end;
Philipp Maierffd75e42017-11-22 11:44:50 +0100574
Philipp Maier55295f72018-01-15 14:00:28 +0100575 /* Since this functions performs a destructive parsing, we create a
576 * local copy of the body data */
Philipp Maier1fc69992018-01-31 15:32:55 +0100577 data = talloc_zero_size(r, strlen(r->body)+1);
Philipp Maier55295f72018-01-15 14:00:28 +0100578 OSMO_ASSERT(data);
579 data_ptr = data;
580 osmo_strlcpy(data, r->body, strlen(r->body));
581
582 /* If there is an SDP body attached, prevent for_each_non_empty_line()
583 * into running in there, we are not yet interested in the parameters
584 * stored there. */
Pau Espin Pedrolc12bfb72019-04-16 17:23:09 +0200585 data_end = mgcp_find_section_end(data);
Philipp Maierffd75e42017-11-22 11:44:50 +0100586 if (data_end)
587 *data_end = '\0';
588
Philipp Maier55295f72018-01-15 14:00:28 +0100589 for_each_non_empty_line(line, data_ptr) {
Pau Espin Pedrol166077e2019-06-26 12:21:38 +0200590 switch (toupper(line[0])) {
Philipp Maier55295f72018-01-15 14:00:28 +0100591 case 'Z':
592 rc = mgcp_parse_head_param(r->head.endpoint,
593 sizeof(r->head.endpoint),
594 'Z', line);
595 if (rc)
596 goto exit;
Philipp Maier771b26a2018-01-31 13:53:11 +0100597
598 /* A specific endpoint identifier returned by the MGW
599 * must not contain any wildcard characters */
600 if (strstr(r->head.endpoint, "*") != NULL) {
601 rc = -EINVAL;
602 goto exit;
603 }
Philipp Maier3261dc72018-01-31 14:03:13 +0100604
605 /* A specific endpoint identifier returned by the MGW
606 * must contain an @ character */
607 if (strstr(r->head.endpoint, "@") == NULL) {
608 rc = -EINVAL;
609 goto exit;
610 }
Philipp Maier55295f72018-01-15 14:00:28 +0100611 break;
Philipp Maierffd75e42017-11-22 11:44:50 +0100612 case 'I':
Philipp Maier55295f72018-01-15 14:00:28 +0100613 rc = mgcp_parse_head_param(r->head.conn_id,
614 sizeof(r->head.conn_id),
615 'I', line);
Philipp Maierffd75e42017-11-22 11:44:50 +0100616 if (rc)
617 goto exit;
618 break;
Pau Espin Pedrol91088c32019-04-24 21:02:40 +0200619 case 'X':
Pau Espin Pedrolc1bf4692019-05-14 16:23:24 +0200620 if (strncasecmp("Osmux: ", line + 2, strlen("Osmux: ")) == 0) {
Pau Espin Pedrol91088c32019-04-24 21:02:40 +0200621 rc = mgcp_parse_osmux_cid(line);
622 if (rc < 0) {
623 /* -1: we don't want wildcards in response. -2: error */
624 rc = -EINVAL;
625 goto exit;
626 }
627 r->head.x_osmo_osmux_use = true;
628 r->head.x_osmo_osmux_cid = (uint8_t) rc;
629 rc = 0;
630 break;
631 }
632 /* Ignore unknown X-headers */
633 break;
Philipp Maierffd75e42017-11-22 11:44:50 +0100634 default:
635 /* skip unhandled parameters */
636 break;
637 }
638 }
639exit:
Philipp Maier55295f72018-01-15 14:00:28 +0100640 talloc_free(data);
Philipp Maierffd75e42017-11-22 11:44:50 +0100641 return rc;
642}
643
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200644static struct mgcp_response_pending *mgcp_client_response_pending_get(
645 struct mgcp_client *mgcp,
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100646 mgcp_trans_id_t trans_id)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200647{
648 struct mgcp_response_pending *pending;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200649 llist_for_each_entry(pending, &mgcp->responses_pending, entry) {
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100650 if (pending->trans_id == trans_id) {
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200651 llist_del(&pending->entry);
652 return pending;
653 }
654 }
655 return NULL;
656}
657
658/* Feed an MGCP message into the receive processing.
659 * Parse the head and call any callback registered for the transaction id found
660 * in the MGCP message. This is normally called directly from the internal
661 * mgcp_do_read that reads from the socket connected to the MGCP gateway. This
662 * function is published mainly to be able to feed data from the test suite.
663 */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200664int mgcp_client_rx(struct mgcp_client *mgcp, struct msgb *msg)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200665{
Philipp Maier1fc69992018-01-31 15:32:55 +0100666 struct mgcp_response *r;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200667 struct mgcp_response_pending *pending;
668 int rc;
669
Philipp Maier1fc69992018-01-31 15:32:55 +0100670 r = talloc_zero(mgcp, struct mgcp_response);
671 OSMO_ASSERT(r);
672
673 rc = mgcp_response_parse_head(r, msg);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200674 if (rc) {
Philipp Maierffd75e42017-11-22 11:44:50 +0100675 LOGP(DLMGCP, LOGL_ERROR, "Cannot parse MGCP response (head)\n");
Philipp Maier1fc69992018-01-31 15:32:55 +0100676 rc = 1;
677 goto error;
Philipp Maierffd75e42017-11-22 11:44:50 +0100678 }
679
Neels Hofmeyrd5731072021-07-15 01:36:47 +0200680 LOGP(DLMGCP, LOGL_DEBUG, "MGCP client: Rx %d %u %s\n",
681 r->head.response_code, r->head.trans_id, r->head.comment);
682
Philipp Maier1fc69992018-01-31 15:32:55 +0100683 rc = parse_head_params(r);
Philipp Maierffd75e42017-11-22 11:44:50 +0100684 if (rc) {
685 LOGP(DLMGCP, LOGL_ERROR, "Cannot parse MGCP response (head parameters)\n");
Philipp Maier1fc69992018-01-31 15:32:55 +0100686 rc = 1;
687 goto error;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200688 }
689
Philipp Maier1fc69992018-01-31 15:32:55 +0100690 pending = mgcp_client_response_pending_get(mgcp, r->head.trans_id);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200691 if (!pending) {
692 LOGP(DLMGCP, LOGL_ERROR,
693 "Cannot find matching MGCP transaction for trans_id %d\n",
Philipp Maier1fc69992018-01-31 15:32:55 +0100694 r->head.trans_id);
695 rc = -ENOENT;
696 goto error;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200697 }
698
Philipp Maier1fc69992018-01-31 15:32:55 +0100699 mgcp_client_handle_response(mgcp, pending, r);
700 rc = 0;
701
702error:
703 talloc_free(r);
704 return rc;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200705}
706
707static int mgcp_do_read(struct osmo_fd *fd)
708{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200709 struct mgcp_client *mgcp = fd->data;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200710 struct msgb *msg;
711 int ret;
712
713 msg = msgb_alloc_headroom(4096, 128, "mgcp_from_gw");
714 if (!msg) {
715 LOGP(DLMGCP, LOGL_ERROR, "Failed to allocate MGCP message.\n");
716 return -1;
717 }
718
719 ret = read(fd->fd, msg->data, 4096 - 128);
720 if (ret <= 0) {
Neels Hofmeyr0a403792018-12-12 03:10:18 +0100721 LOGP(DLMGCP, LOGL_ERROR, "Failed to read: %s: %d='%s'\n", osmo_sock_get_name2(fd->fd),
722 errno, strerror(errno));
723
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200724 msgb_free(msg);
725 return -1;
726 } else if (ret > 4096 - 128) {
Neels Hofmeyr0a403792018-12-12 03:10:18 +0100727 LOGP(DLMGCP, LOGL_ERROR, "Too much data: %s: %d\n", osmo_sock_get_name2(fd->fd), ret);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200728 msgb_free(msg);
729 return -1;
Harald Welte9bf7c532017-11-17 14:14:31 +0100730 }
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200731
732 msg->l2h = msgb_put(msg, ret);
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200733 ret = mgcp_client_rx(mgcp, msg);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200734 talloc_free(msg);
735 return ret;
736}
737
738static int mgcp_do_write(struct osmo_fd *fd, struct msgb *msg)
739{
740 int ret;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200741
Neels Hofmeyr0a403792018-12-12 03:10:18 +0100742 LOGP(DLMGCP, LOGL_DEBUG, "Tx MGCP: %s: len=%u '%s'...\n",
743 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 +0200744
745 ret = write(fd->fd, msg->data, msg->len);
746 if (ret != msg->len)
Neels Hofmeyr0a403792018-12-12 03:10:18 +0100747 LOGP(DLMGCP, LOGL_ERROR, "Failed to Tx MGCP: %s: %d='%s'; msg: len=%u '%s'...\n",
748 osmo_sock_get_name2(fd->fd), errno, strerror(errno),
Neels Hofmeyr32d15cc2018-12-12 03:05:33 +0100749 msg->len, osmo_escape_str((const char*)msg->data, OSMO_MIN(42, msg->len)));
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200750 return ret;
751}
752
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200753struct mgcp_client *mgcp_client_init(void *ctx,
754 struct mgcp_client_conf *conf)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200755{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200756 struct mgcp_client *mgcp;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200757
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200758 mgcp = talloc_zero(ctx, struct mgcp_client);
Harald Welteefe15712020-07-15 10:56:45 +0200759 if (!mgcp)
760 return NULL;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200761
762 INIT_LLIST_HEAD(&mgcp->responses_pending);
763 INIT_LLIST_HEAD(&mgcp->inuse_endpoints);
764
765 mgcp->next_trans_id = 1;
766
767 mgcp->actual.local_addr = conf->local_addr ? conf->local_addr :
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200768 MGCP_CLIENT_LOCAL_ADDR_DEFAULT;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200769 mgcp->actual.local_port = conf->local_port >= 0 ? (uint16_t)conf->local_port :
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200770 MGCP_CLIENT_LOCAL_PORT_DEFAULT;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200771
772 mgcp->actual.remote_addr = conf->remote_addr ? conf->remote_addr :
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200773 MGCP_CLIENT_REMOTE_ADDR_DEFAULT;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200774 mgcp->actual.remote_port = conf->remote_port >= 0 ? (uint16_t)conf->remote_port :
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200775 MGCP_CLIENT_REMOTE_PORT_DEFAULT;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200776
Neels Hofmeyrac69ea92018-12-19 00:27:50 +0100777 if (osmo_strlcpy(mgcp->actual.endpoint_domain_name, conf->endpoint_domain_name,
778 sizeof(mgcp->actual.endpoint_domain_name))
779 >= sizeof(mgcp->actual.endpoint_domain_name)) {
780 LOGP(DLMGCP, LOGL_ERROR, "MGCP client: endpoint domain name is too long, max length is %zu: '%s'\n",
781 sizeof(mgcp->actual.endpoint_domain_name) - 1, conf->endpoint_domain_name);
782 talloc_free(mgcp);
783 return NULL;
784 }
785 LOGP(DLMGCP, LOGL_NOTICE, "MGCP client: using endpoint domain '@%s'\n", mgcp_client_endpoint_domain(mgcp));
786
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200787 return mgcp;
788}
789
Philipp Maier6a26c162018-08-01 16:37:06 +0200790static int init_socket(struct mgcp_client *mgcp)
791{
792 int rc;
793 struct osmo_wqueue *wq;
794 int i;
795
796 wq = &mgcp->wq;
797
798 for (i = 0; i < 100; i++) {
799
800 /* Initalize socket with the currently configured port
801 * number */
Pau Espin Pedrol531470a2020-08-31 17:06:55 +0200802 rc = osmo_sock_init2_ofd(&wq->bfd, AF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, mgcp->actual.local_addr,
Philipp Maier6a26c162018-08-01 16:37:06 +0200803 mgcp->actual.local_port, mgcp->actual.remote_addr, mgcp->actual.remote_port,
804 OSMO_SOCK_F_BIND | OSMO_SOCK_F_CONNECT);
805 if (rc > 0)
806 return rc;
807
808 /* If there is a different port than the default port
809 * configured then we assume that the user has choosen
810 * that port conciously and we will not try to resolve
811 * this by silently choosing a different port. */
Philipp Maier9a7ccc32018-08-09 11:41:19 +0200812 if (mgcp->actual.local_port != MGCP_CLIENT_LOCAL_PORT_DEFAULT && i == 0)
Philipp Maier6a26c162018-08-01 16:37:06 +0200813 return -EINVAL;
814
815 /* Choose a new port number to try next */
816 LOGP(DLMGCP, LOGL_NOTICE,
Neels Hofmeyr0a403792018-12-12 03:10:18 +0100817 "MGCPGW failed to bind to %s:%u, retrying with port %u\n",
818 mgcp->actual.local_addr, mgcp->actual.local_port, mgcp->actual.local_port + 1);
Philipp Maier6a26c162018-08-01 16:37:06 +0200819 mgcp->actual.local_port++;
820 }
821
Neels Hofmeyr0a403792018-12-12 03:10:18 +0100822 LOGP(DLMGCP, LOGL_FATAL, "MGCPGW failed to find a port to bind on %i times.\n", i);
Philipp Maier6a26c162018-08-01 16:37:06 +0200823 return -EINVAL;
824}
825
Philipp Maier275ac972018-01-20 00:58:16 +0100826/*! Initalize client connection (opens socket only, no request is sent yet)
827 * \param[in,out] mgcp MGCP client descriptor.
828 * \returns 0 on success, -EINVAL on error. */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200829int mgcp_client_connect(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200830{
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200831 struct osmo_wqueue *wq;
832 int rc;
833
834 if (!mgcp) {
835 LOGP(DLMGCP, LOGL_FATAL, "MGCPGW client not initialized properly\n");
836 return -EINVAL;
837 }
838
839 wq = &mgcp->wq;
Harald Weltec2a8f592020-10-19 13:25:41 +0200840 osmo_wqueue_init(wq, 1024);
841 wq->read_cb = mgcp_do_read;
842 wq->write_cb = mgcp_do_write;
843
844 osmo_fd_setup(&wq->bfd, -1, OSMO_FD_READ, osmo_wqueue_bfd_cb, mgcp, 0);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200845
Philipp Maier6a26c162018-08-01 16:37:06 +0200846 rc = init_socket(mgcp);
Harald Welte8890dfa2017-11-17 15:09:30 +0100847 if (rc < 0) {
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200848 LOGP(DLMGCP, LOGL_FATAL,
Harald Welte8890dfa2017-11-17 15:09:30 +0100849 "Failed to initialize socket %s:%u -> %s:%u for MGCP GW: %s\n",
850 mgcp->actual.local_addr, mgcp->actual.local_port,
851 mgcp->actual.remote_addr, mgcp->actual.remote_port, strerror(errno));
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200852 goto error_close_fd;
853 }
854
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200855
Neels Hofmeyr0a403792018-12-12 03:10:18 +0100856 LOGP(DLMGCP, LOGL_INFO, "MGCP GW connection: %s\n", osmo_sock_get_name2(wq->bfd.fd));
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200857
858 return 0;
859error_close_fd:
860 close(wq->bfd.fd);
861 wq->bfd.fd = -1;
862 return rc;
863}
864
Philipp Maier275ac972018-01-20 00:58:16 +0100865/*! Get the IP-Aaddress of the associated MGW as string.
866 * \param[in] mgcp MGCP client descriptor.
867 * \returns a pointer to the address string. */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200868const char *mgcp_client_remote_addr_str(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200869{
870 return mgcp->actual.remote_addr;
871}
872
Philipp Maier275ac972018-01-20 00:58:16 +0100873/*! Get the IP-Port of the associated MGW.
874 * \param[in] mgcp MGCP client descriptor.
875 * \returns port number. */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200876uint16_t mgcp_client_remote_port(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200877{
878 return mgcp->actual.remote_port;
879}
880
Pau Espin Pedrol74d0e5c2020-09-02 17:19:20 +0200881/*! Get the IP-Address of the associated MGW as its numeric representation.
882 * DEPRECATED, DON'T USE.
Philipp Maier275ac972018-01-20 00:58:16 +0100883 * \param[in] mgcp MGCP client descriptor.
884 * \returns IP-Address as 32 bit integer (network byte order) */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200885uint32_t mgcp_client_remote_addr_n(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200886{
Pau Espin Pedrol74d0e5c2020-09-02 17:19:20 +0200887 return 0;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200888}
889
Neels Hofmeyrac69ea92018-12-19 00:27:50 +0100890/* To compose endpoint names, usually for CRCX, use this as domain name.
891 * For example, snprintf("rtpbridge\*@%s", mgcp_client_endpoint_domain(mgcp)). */
892const char *mgcp_client_endpoint_domain(const struct mgcp_client *mgcp)
893{
894 return mgcp->actual.endpoint_domain_name[0] ? mgcp->actual.endpoint_domain_name : "mgw";
895}
896
Pau Espin Pedrolca0818c2019-04-16 17:23:38 +0200897static const char *_mgcp_client_name_append_domain(const struct mgcp_client *mgcp, char *name)
Neels Hofmeyrac69ea92018-12-19 00:27:50 +0100898{
899 static char endpoint[MGCP_ENDPOINT_MAXLEN];
900 int rc;
901
Pau Espin Pedrolca0818c2019-04-16 17:23:38 +0200902 rc = snprintf(endpoint, sizeof(endpoint), "%s@%s", name, mgcp_client_endpoint_domain(mgcp));
Neels Hofmeyrac69ea92018-12-19 00:27:50 +0100903 if (rc > sizeof(endpoint) - 1) {
Pau Espin Pedrolca0818c2019-04-16 17:23:38 +0200904 LOGP(DLMGCP, LOGL_ERROR, "MGCP endpoint exceeds maximum length of %zu: '%s@%s'\n",
905 sizeof(endpoint) - 1, name, mgcp_client_endpoint_domain(mgcp));
Neels Hofmeyrac69ea92018-12-19 00:27:50 +0100906 return NULL;
907 }
908 if (rc < 1) {
909 LOGP(DLMGCP, LOGL_ERROR, "Cannot compose MGCP endpoint name\n");
910 return NULL;
911 }
912 return endpoint;
913}
914
Philipp Maiera466f572020-06-25 16:11:04 +0200915/*! Compose endpoint name for a wildcarded request to the virtual trunk
916 * \param[in] mgcp MGCP client descriptor.
917 * \returns string containing the endpoint name (e.g. rtpbridge\*@mgw) */
Pau Espin Pedrolca0818c2019-04-16 17:23:38 +0200918const char *mgcp_client_rtpbridge_wildcard(const struct mgcp_client *mgcp)
919{
920 return _mgcp_client_name_append_domain(mgcp, "rtpbridge/*");
921}
922
Philipp Maier48635912020-06-25 20:16:22 +0200923/*! Compose endpoint name for an E1 endpoint.
924 * \param[in] ctx talloc context.
925 * \param[in] mgcp MGCP client descriptor.
926 * \param[in] trunk_id id number of the E1 trunk (1-64).
927 * \param[in] ts timeslot on the E1 trunk (1-31).
928 * \param[in] rate bitrate used on the E1 trunk (e.g 16 for 16kbit).
929 * \param[in] offset bit offset of the E1 subslot (e.g. 4 for the third 16k subslot).
930 * \returns string containing the endpoint name (e.g. ds/e1-1/s-1/su16-4). */
931const char *mgcp_client_e1_epname(void *ctx, const struct mgcp_client *mgcp, uint8_t trunk_id, uint8_t ts,
932 uint8_t rate, uint8_t offset)
933{
934 /* See also comment in libosmo-mgcp, mgcp_client.c, gen_e1_epname() */
935 const uint8_t valid_rates[] = { 64, 32, 32, 16, 16, 16, 16, 8, 8, 8, 8, 8, 8, 8, 8 };
936 const uint8_t valid_offsets[] = { 0, 0, 4, 0, 2, 4, 6, 0, 1, 2, 3, 4, 5, 6, 7 };
937
938 uint8_t i;
939 bool rate_offs_valid = false;
940 char *epname;
941
942 epname =
943 talloc_asprintf(ctx, "ds/e1-%u/s-%u/su%u-%u@%s", trunk_id, ts, rate, offset,
944 mgcp_client_endpoint_domain(mgcp));
945 if (!epname) {
946 LOGP(DLMGCP, LOGL_ERROR, "Cannot compose MGCP e1-endpoint name!\n");
947 return NULL;
948 }
949
950 /* Check if the supplied rate/offset pair resembles a valid combination */
951 for (i = 0; i < sizeof(valid_rates); i++) {
952 if (valid_rates[i] == rate && valid_offsets[i] == offset)
953 rate_offs_valid = true;
954 }
955 if (!rate_offs_valid) {
956 LOGP(DLMGCP, LOGL_ERROR,
957 "Cannot compose MGCP e1-endpoint name (%s), rate(%u)/offset(%u) combination is invalid!\n", epname,
958 rate, offset);
959 talloc_free(epname);
960 return NULL;
961 }
962
963 /* An E1 line has a maximum of 32 timeslots, while the first (ts=0) is
964 * reserverd for framing and alignment, so we can not use it here. */
Philipp Maier92a73cd2020-11-26 22:21:11 +0100965 if (ts == 0 || ts > NUM_E1_TS-1) {
Philipp Maier48635912020-06-25 20:16:22 +0200966 LOGP(DLMGCP, LOGL_ERROR,
967 "Cannot compose MGCP e1-endpoint name (%s), E1-timeslot number (%u) is invalid!\n", epname, ts);
968 talloc_free(epname);
969 return NULL;
970 }
971
972 return epname;
973}
974
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200975struct mgcp_response_pending * mgcp_client_pending_add(
976 struct mgcp_client *mgcp,
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200977 mgcp_trans_id_t trans_id,
978 mgcp_response_cb_t response_cb,
979 void *priv)
980{
981 struct mgcp_response_pending *pending;
982
983 pending = talloc_zero(mgcp, struct mgcp_response_pending);
Harald Welte827da2c2020-07-15 10:57:08 +0200984 if (!pending)
985 return NULL;
986
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200987 pending->trans_id = trans_id;
988 pending->response_cb = response_cb;
989 pending->priv = priv;
990 llist_add_tail(&pending->entry, &mgcp->responses_pending);
991
992 return pending;
993}
994
995/* Send the MGCP message in msg to the MGCP GW and handle a response with
996 * response_cb. NOTE: the response_cb still needs to call
997 * mgcp_response_parse_params(response) to get the parsed parameters -- to
998 * potentially save some CPU cycles, only the head line has been parsed when
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100999 * the response_cb is invoked.
1000 * Before the priv pointer becomes invalid, e.g. due to transaction timeout,
1001 * mgcp_client_cancel() needs to be called for this transaction.
1002 */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +02001003int mgcp_client_tx(struct mgcp_client *mgcp, struct msgb *msg,
1004 mgcp_response_cb_t response_cb, void *priv)
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001005{
Harald Welte563ffc52020-06-17 21:54:13 +07001006 struct mgcp_response_pending *pending = NULL;
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001007 mgcp_trans_id_t trans_id;
1008 int rc;
1009
1010 trans_id = msg->cb[MSGB_CB_MGCP_TRANS_ID];
1011 if (!trans_id) {
1012 LOGP(DLMGCP, LOGL_ERROR,
1013 "Unset transaction id in mgcp send request\n");
1014 talloc_free(msg);
1015 return -EINVAL;
1016 }
1017
Harald Welte563ffc52020-06-17 21:54:13 +07001018 /* Do not allocate a dummy 'mgcp_response_pending' entry */
1019 if (response_cb != NULL) {
1020 pending = mgcp_client_pending_add(mgcp, trans_id, response_cb, priv);
1021 if (!pending) {
1022 talloc_free(msg);
1023 return -ENOMEM;
1024 }
Harald Welte827da2c2020-07-15 10:57:08 +02001025 }
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001026
1027 if (msgb_l2len(msg) > 4096) {
1028 LOGP(DLMGCP, LOGL_ERROR,
1029 "Cannot send, MGCP message too large: %u\n",
1030 msgb_l2len(msg));
1031 msgb_free(msg);
1032 rc = -EINVAL;
1033 goto mgcp_tx_error;
1034 }
1035
1036 rc = osmo_wqueue_enqueue(&mgcp->wq, msg);
1037 if (rc) {
1038 LOGP(DLMGCP, LOGL_FATAL, "Could not queue message to MGCP GW\n");
1039 msgb_free(msg);
1040 goto mgcp_tx_error;
1041 } else
Neels Hofmeyred0c1aa2018-12-21 03:07:53 +01001042 LOGP(DLMGCP, LOGL_DEBUG, "Queued %u bytes for MGCP GW\n",
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001043 msgb_l2len(msg));
1044 return 0;
1045
1046mgcp_tx_error:
Harald Welte563ffc52020-06-17 21:54:13 +07001047 if (!pending)
Vadim Yanitskiyffbc6182020-07-16 15:48:13 +07001048 return rc;
Vadim Yanitskiy3f8139c2020-06-17 20:50:17 +07001049 /* Dequeue pending response, it's going to be free()d */
1050 llist_del(&pending->entry);
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001051 /* Pass NULL to response cb to indicate an error */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +02001052 mgcp_client_handle_response(mgcp, pending, NULL);
Vadim Yanitskiyffbc6182020-07-16 15:48:13 +07001053 return rc;
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001054}
1055
Philipp Maier275ac972018-01-20 00:58:16 +01001056/*! Cancel a pending transaction.
1057 * \param[in] mgcp MGCP client descriptor.
1058 * \param[in,out] trans_id Transaction id.
1059 * \returns 0 on success, -ENOENT on error.
1060 *
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +01001061 * Should a priv pointer passed to mgcp_client_tx() become invalid, this function must be called. In
1062 * practical terms, if the caller of mgcp_client_tx() wishes to tear down a transaction without having
1063 * received a response this function must be called. The trans_id can be obtained by calling
Philipp Maier275ac972018-01-20 00:58:16 +01001064 * mgcp_msg_trans_id() on the msgb produced by mgcp_msg_gen(). */
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +01001065int mgcp_client_cancel(struct mgcp_client *mgcp, mgcp_trans_id_t trans_id)
1066{
1067 struct mgcp_response_pending *pending = mgcp_client_response_pending_get(mgcp, trans_id);
1068 if (!pending) {
Philipp Maier275ac972018-01-20 00:58:16 +01001069 /*! Note: it is not harmful to cancel a transaction twice. */
Neels Hofmeyred0c1aa2018-12-21 03:07:53 +01001070 LOGP(DLMGCP, LOGL_ERROR, "Cannot cancel, no such transaction: %u\n", trans_id);
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +01001071 return -ENOENT;
1072 }
Neels Hofmeyred0c1aa2018-12-21 03:07:53 +01001073 LOGP(DLMGCP, LOGL_DEBUG, "Canceled transaction %u\n", trans_id);
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +01001074 talloc_free(pending);
1075 return 0;
Philipp Maier275ac972018-01-20 00:58:16 +01001076 /*! We don't really need to clean up the wqueue: In all sane cases, the msgb has already been sent
1077 * out and is no longer in the wqueue. If it still is in the wqueue, then sending MGCP messages
1078 * per se is broken and the program should notice so by a full wqueue. Even if this was called
1079 * before we had a chance to send out the message and it is still going to be sent, we will just
1080 * ignore the reply to it later. Removing a msgb from the wqueue here would just introduce more
1081 * bug surface in terms of failing to update wqueue API's counters or some such.
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +01001082 */
1083}
1084
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +02001085static mgcp_trans_id_t mgcp_client_next_trans_id(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001086{
1087 /* avoid zero trans_id to distinguish from unset trans_id */
1088 if (!mgcp->next_trans_id)
1089 mgcp->next_trans_id ++;
1090 return mgcp->next_trans_id ++;
1091}
1092
Philipp Maier1dc6be62017-10-05 18:25:37 +02001093#define MGCP_CRCX_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT | \
1094 MGCP_MSG_PRESENCE_CALL_ID | \
Philipp Maier1dc6be62017-10-05 18:25:37 +02001095 MGCP_MSG_PRESENCE_CONN_MODE)
1096#define MGCP_MDCX_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT | \
Philipp Maier490cbaa2018-01-22 17:32:38 +01001097 MGCP_MSG_PRESENCE_CALL_ID | \
Philipp Maier1dc6be62017-10-05 18:25:37 +02001098 MGCP_MSG_PRESENCE_CONN_ID)
1099#define MGCP_DLCX_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT)
1100#define MGCP_AUEP_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT)
1101#define MGCP_RSIP_MANDATORY 0 /* none */
1102
Philipp Maier704c4f02018-06-07 18:51:31 +02001103/* Helper function for mgcp_msg_gen(): Add LCO information to MGCP message */
1104static int add_lco(struct msgb *msg, struct mgcp_msg *mgcp_msg)
1105{
1106 unsigned int i;
1107 int rc = 0;
1108 const char *codec;
1109 unsigned int pt;
1110
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001111 rc |= msgb_printf(msg, "L:");
Philipp Maier704c4f02018-06-07 18:51:31 +02001112
1113 if (mgcp_msg->ptime)
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001114 rc |= msgb_printf(msg, " p:%u,", mgcp_msg->ptime);
Philipp Maier704c4f02018-06-07 18:51:31 +02001115
1116 if (mgcp_msg->codecs_len) {
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001117 rc |= msgb_printf(msg, " a:");
Philipp Maier704c4f02018-06-07 18:51:31 +02001118 for (i = 0; i < mgcp_msg->codecs_len; i++) {
1119 pt = mgcp_msg->codecs[i];
Neels Hofmeyr47642f22019-02-27 05:56:53 +01001120 codec = get_value_string_or_null(osmo_mgcpc_codec_names, pt);
Pau Espin Pedrolc12bfb72019-04-16 17:23:09 +02001121
Philipp Maier704c4f02018-06-07 18:51:31 +02001122 /* Note: Use codec descriptors from enum mgcp_codecs
1123 * in mgcp_client only! */
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001124 if (!codec) {
1125 msgb_free(msg);
1126 return -EINVAL;
1127 }
1128
1129 rc |= msgb_printf(msg, "%s", extract_codec_name(codec));
Philipp Maier704c4f02018-06-07 18:51:31 +02001130 if (i < mgcp_msg->codecs_len - 1)
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001131 rc |= msgb_printf(msg, ";");
Philipp Maier704c4f02018-06-07 18:51:31 +02001132 }
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001133 rc |= msgb_printf(msg, ",");
Philipp Maier704c4f02018-06-07 18:51:31 +02001134 }
1135
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001136 rc |= msgb_printf(msg, " nt:IN\r\n");
Philipp Maier704c4f02018-06-07 18:51:31 +02001137
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001138 if (rc != 0) {
1139 LOGP(DLMGCP, LOGL_ERROR,
1140 "message buffer to small, can not generate MGCP message (LCO)\n");
1141 msgb_free(msg);
1142 return -ENOBUFS;
1143 }
1144 return 0;
Philipp Maier704c4f02018-06-07 18:51:31 +02001145}
1146
1147/* Helper function for mgcp_msg_gen(): Add SDP information to MGCP message */
1148static int add_sdp(struct msgb *msg, struct mgcp_msg *mgcp_msg, struct mgcp_client *mgcp)
1149{
1150 unsigned int i;
1151 int rc = 0;
Pau Espin Pedrol8667d512020-08-28 19:37:08 +02001152 char local_ip[INET6_ADDRSTRLEN];
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +02001153 int local_ip_family, audio_ip_family;
Philipp Maier704c4f02018-06-07 18:51:31 +02001154 const char *codec;
1155 unsigned int pt;
1156
1157 /* Add separator to mark the beginning of the SDP block */
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001158 rc |= msgb_printf(msg, "\r\n");
Philipp Maier704c4f02018-06-07 18:51:31 +02001159
1160 /* Add SDP protocol version */
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001161 rc |= msgb_printf(msg, "v=0\r\n");
Philipp Maier704c4f02018-06-07 18:51:31 +02001162
1163 /* Determine local IP-Address */
1164 if (osmo_sock_local_ip(local_ip, mgcp->actual.remote_addr) < 0) {
1165 LOGP(DLMGCP, LOGL_ERROR,
1166 "Could not determine local IP-Address!\n");
1167 msgb_free(msg);
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001168 return -EINVAL;
Philipp Maier704c4f02018-06-07 18:51:31 +02001169 }
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +02001170 local_ip_family = osmo_ip_str_type(local_ip);
1171 if (local_ip_family == AF_UNSPEC) {
1172 msgb_free(msg);
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001173 return -EINVAL;
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +02001174 }
1175 audio_ip_family = osmo_ip_str_type(mgcp_msg->audio_ip);
1176 if (audio_ip_family == AF_UNSPEC) {
1177 msgb_free(msg);
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001178 return -EINVAL;
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +02001179 }
Philipp Maier704c4f02018-06-07 18:51:31 +02001180
1181 /* Add owner/creator (SDP) */
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001182 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 +02001183 local_ip_family == AF_INET6 ? '6' : '4',
1184 local_ip);
Philipp Maier704c4f02018-06-07 18:51:31 +02001185
1186 /* Add session name (none) */
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001187 rc |= msgb_printf(msg, "s=-\r\n");
Philipp Maier704c4f02018-06-07 18:51:31 +02001188
1189 /* Add RTP address and port */
1190 if (mgcp_msg->audio_port == 0) {
1191 LOGP(DLMGCP, LOGL_ERROR,
1192 "Invalid port number, can not generate MGCP message\n");
1193 msgb_free(msg);
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001194 return -EINVAL;
Philipp Maier704c4f02018-06-07 18:51:31 +02001195 }
1196 if (strlen(mgcp_msg->audio_ip) <= 0) {
1197 LOGP(DLMGCP, LOGL_ERROR,
1198 "Empty ip address, can not generate MGCP message\n");
1199 msgb_free(msg);
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001200 return -EINVAL;
Philipp Maier704c4f02018-06-07 18:51:31 +02001201 }
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001202 rc |= msgb_printf(msg, "c=IN IP%c %s\r\n",
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +02001203 audio_ip_family == AF_INET6 ? '6' : '4',
1204 mgcp_msg->audio_ip);
Philipp Maier704c4f02018-06-07 18:51:31 +02001205
1206 /* Add time description, active time (SDP) */
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001207 rc |= msgb_printf(msg, "t=0 0\r\n");
Philipp Maier704c4f02018-06-07 18:51:31 +02001208
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001209 rc |= msgb_printf(msg, "m=audio %u RTP/AVP", mgcp_msg->audio_port);
Philipp Maier704c4f02018-06-07 18:51:31 +02001210 for (i = 0; i < mgcp_msg->codecs_len; i++) {
1211 pt = map_codec_to_pt(mgcp_msg->ptmap, mgcp_msg->ptmap_len, mgcp_msg->codecs[i]);
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001212 rc |= msgb_printf(msg, " %u", pt);
Philipp Maier704c4f02018-06-07 18:51:31 +02001213
1214 }
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001215 rc |= msgb_printf(msg, "\r\n");
Philipp Maier704c4f02018-06-07 18:51:31 +02001216
Philipp Maier228e5912019-03-05 13:56:59 +01001217 /* Add optional codec parameters (fmtp) */
1218 if (mgcp_msg->param_present) {
1219 for (i = 0; i < mgcp_msg->codecs_len; i++) {
1220 /* The following is only applicable for AMR */
1221 if (mgcp_msg->codecs[i] != CODEC_AMR_8000_1 && mgcp_msg->codecs[i] != CODEC_AMRWB_16000_1)
1222 continue;
1223 pt = map_codec_to_pt(mgcp_msg->ptmap, mgcp_msg->ptmap_len, mgcp_msg->codecs[i]);
1224 if (mgcp_msg->param.amr_octet_aligned_present && mgcp_msg->param.amr_octet_aligned)
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001225 rc |= msgb_printf(msg, "a=fmtp:%u octet-align=1\r\n", pt);
Philipp Maier228e5912019-03-05 13:56:59 +01001226 else if (mgcp_msg->param.amr_octet_aligned_present && !mgcp_msg->param.amr_octet_aligned)
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001227 rc |= msgb_printf(msg, "a=fmtp:%u octet-align=0\r\n", pt);
Philipp Maier228e5912019-03-05 13:56:59 +01001228 }
1229 }
1230
Philipp Maier704c4f02018-06-07 18:51:31 +02001231 for (i = 0; i < mgcp_msg->codecs_len; i++) {
1232 pt = map_codec_to_pt(mgcp_msg->ptmap, mgcp_msg->ptmap_len, mgcp_msg->codecs[i]);
Pau Espin Pedrolc12bfb72019-04-16 17:23:09 +02001233
Philipp Maier704c4f02018-06-07 18:51:31 +02001234 /* Note: Only dynamic payload type from the range 96-127
1235 * require to be explained further via rtpmap. All others
1236 * are implcitly definedby the number in m=audio */
1237 if (pt >= 96 && pt <= 127) {
Neels Hofmeyr47642f22019-02-27 05:56:53 +01001238 codec = get_value_string_or_null(osmo_mgcpc_codec_names, mgcp_msg->codecs[i]);
Philipp Maier704c4f02018-06-07 18:51:31 +02001239
1240 /* Note: Use codec descriptors from enum mgcp_codecs
1241 * in mgcp_client only! */
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001242 if (!codec) {
1243 msgb_free(msg);
1244 return -EINVAL;
1245 }
Pau Espin Pedrolc12bfb72019-04-16 17:23:09 +02001246
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001247 rc |= msgb_printf(msg, "a=rtpmap:%u %s\r\n", pt, codec);
Philipp Maier704c4f02018-06-07 18:51:31 +02001248 }
1249 }
Pau Espin Pedrolc12bfb72019-04-16 17:23:09 +02001250
Philipp Maier704c4f02018-06-07 18:51:31 +02001251 if (mgcp_msg->ptime)
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001252 rc |= msgb_printf(msg, "a=ptime:%u\r\n", mgcp_msg->ptime);
Philipp Maier704c4f02018-06-07 18:51:31 +02001253
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001254 if (rc != 0) {
1255 LOGP(DLMGCP, LOGL_ERROR,
1256 "message buffer to small, can not generate MGCP message (SDP)\n");
1257 msgb_free(msg);
1258 return -ENOBUFS;
1259 }
1260
1261 return 0;
Philipp Maier704c4f02018-06-07 18:51:31 +02001262}
1263
Philipp Maier275ac972018-01-20 00:58:16 +01001264/*! Generate an MGCP message
1265 * \param[in] mgcp MGCP client descriptor.
1266 * \param[in] mgcp_msg Message description
1267 * \returns message buffer on success, NULL on error. */
Philipp Maier1dc6be62017-10-05 18:25:37 +02001268struct msgb *mgcp_msg_gen(struct mgcp_client *mgcp, struct mgcp_msg *mgcp_msg)
1269{
1270 mgcp_trans_id_t trans_id = mgcp_client_next_trans_id(mgcp);
1271 uint32_t mandatory_mask;
1272 struct msgb *msg = msgb_alloc_headroom(4096, 128, "MGCP tx");
1273 int rc = 0;
Philipp Maier704c4f02018-06-07 18:51:31 +02001274 bool use_sdp = false;
Pau Espin Pedrol900cd652019-04-24 22:06:22 +02001275 char buf[32];
Philipp Maier1dc6be62017-10-05 18:25:37 +02001276
1277 msg->l2h = msg->data;
1278 msg->cb[MSGB_CB_MGCP_TRANS_ID] = trans_id;
1279
1280 /* Add command verb */
1281 switch (mgcp_msg->verb) {
1282 case MGCP_VERB_CRCX:
1283 mandatory_mask = MGCP_CRCX_MANDATORY;
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001284 rc |= msgb_printf(msg, "CRCX %u", trans_id);
Philipp Maier1dc6be62017-10-05 18:25:37 +02001285 break;
1286 case MGCP_VERB_MDCX:
1287 mandatory_mask = MGCP_MDCX_MANDATORY;
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001288 rc |= msgb_printf(msg, "MDCX %u", trans_id);
Philipp Maier1dc6be62017-10-05 18:25:37 +02001289 break;
1290 case MGCP_VERB_DLCX:
1291 mandatory_mask = MGCP_DLCX_MANDATORY;
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001292 rc |= msgb_printf(msg, "DLCX %u", trans_id);
Philipp Maier1dc6be62017-10-05 18:25:37 +02001293 break;
1294 case MGCP_VERB_AUEP:
1295 mandatory_mask = MGCP_AUEP_MANDATORY;
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001296 rc |= msgb_printf(msg, "AUEP %u", trans_id);
Philipp Maier1dc6be62017-10-05 18:25:37 +02001297 break;
1298 case MGCP_VERB_RSIP:
1299 mandatory_mask = MGCP_RSIP_MANDATORY;
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001300 rc |= msgb_printf(msg, "RSIP %u", trans_id);
Philipp Maier1dc6be62017-10-05 18:25:37 +02001301 break;
1302 default:
1303 LOGP(DLMGCP, LOGL_ERROR,
1304 "Invalid command verb, can not generate MGCP message\n");
1305 msgb_free(msg);
1306 return NULL;
1307 }
1308
1309 /* Check if mandatory fields are missing */
1310 if (!((mgcp_msg->presence & mandatory_mask) == mandatory_mask)) {
1311 LOGP(DLMGCP, LOGL_ERROR,
1312 "One or more missing mandatory fields, can not generate MGCP message\n");
1313 msgb_free(msg);
1314 return NULL;
1315 }
1316
1317 /* Add endpoint name */
Philipp Maier7bc55522017-12-10 22:52:22 +01001318 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_ENDPOINT) {
1319 if (strlen(mgcp_msg->endpoint) <= 0) {
1320 LOGP(DLMGCP, LOGL_ERROR,
1321 "Empty endpoint name, can not generate MGCP message\n");
1322 msgb_free(msg);
1323 return NULL;
1324 }
Philipp Maier3aa81572018-01-31 14:13:45 +01001325
1326 if (strstr(mgcp_msg->endpoint, "@") == NULL) {
1327 LOGP(DLMGCP, LOGL_ERROR,
1328 "Endpoint name (%s) lacks separator (@), can not generate MGCP message\n",
1329 mgcp_msg->endpoint);
1330 msgb_free(msg);
Vadim Yanitskiye6743452020-06-18 03:04:13 +07001331 return NULL;
Philipp Maier3aa81572018-01-31 14:13:45 +01001332 }
1333
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001334 rc |= msgb_printf(msg, " %s", mgcp_msg->endpoint);
Philipp Maier7bc55522017-12-10 22:52:22 +01001335 }
Philipp Maier1dc6be62017-10-05 18:25:37 +02001336
1337 /* Add protocol version */
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001338 rc |= msgb_printf(msg, " MGCP 1.0\r\n");
Philipp Maier1dc6be62017-10-05 18:25:37 +02001339
1340 /* Add call id */
1341 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_CALL_ID)
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001342 rc |= msgb_printf(msg, "C: %x\r\n", mgcp_msg->call_id);
Philipp Maier1dc6be62017-10-05 18:25:37 +02001343
1344 /* Add connection id */
Philipp Maier7bc55522017-12-10 22:52:22 +01001345 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_CONN_ID) {
1346 if (strlen(mgcp_msg->conn_id) <= 0) {
1347 LOGP(DLMGCP, LOGL_ERROR,
1348 "Empty connection id, can not generate MGCP message\n");
1349 msgb_free(msg);
1350 return NULL;
1351 }
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001352 rc |= msgb_printf(msg, "I: %s\r\n", mgcp_msg->conn_id);
Philipp Maier7bc55522017-12-10 22:52:22 +01001353 }
Philipp Maier1dc6be62017-10-05 18:25:37 +02001354
Philipp Maier704c4f02018-06-07 18:51:31 +02001355 /* Using SDP makes sense when a valid IP/Port combination is specifiec,
1356 * if we do not know this information yet, we fall back to LCO */
1357 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_AUDIO_IP
1358 && mgcp_msg->presence & MGCP_MSG_PRESENCE_AUDIO_PORT)
1359 use_sdp = true;
1360
1361 /* Add local connection options (LCO) */
1362 if (!use_sdp
1363 && (mgcp_msg->verb == MGCP_VERB_CRCX
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001364 || mgcp_msg->verb == MGCP_VERB_MDCX)) {
1365 if (add_lco(msg, mgcp_msg) < 0)
1366 return NULL;
1367 }
Philipp Maier1dc6be62017-10-05 18:25:37 +02001368
1369 /* Add mode */
1370 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_CONN_MODE)
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001371 rc |=
Philipp Maier1dc6be62017-10-05 18:25:37 +02001372 msgb_printf(msg, "M: %s\r\n",
1373 mgcp_client_cmode_name(mgcp_msg->conn_mode));
1374
Neels Hofmeyre6d8e912018-08-23 16:36:48 +02001375 /* Add X-Osmo-IGN */
1376 if ((mgcp_msg->presence & MGCP_MSG_PRESENCE_X_OSMO_IGN)
1377 && (mgcp_msg->x_osmo_ign != 0))
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001378 rc |=
Neels Hofmeyre6d8e912018-08-23 16:36:48 +02001379 msgb_printf(msg, MGCP_X_OSMO_IGN_HEADER "%s\r\n",
1380 mgcp_msg->x_osmo_ign & MGCP_X_OSMO_IGN_CALLID ? " C": "");
1381
Pau Espin Pedrol900cd652019-04-24 22:06:22 +02001382 /* Add X-Osmo-Osmux */
1383 if ((mgcp_msg->presence & MGCP_MSG_PRESENCE_X_OSMO_OSMUX_CID)) {
Pau Espin Pedrol1b1d7ed2019-05-23 16:48:24 +02001384 if (mgcp_msg->x_osmo_osmux_cid < -1 || mgcp_msg->x_osmo_osmux_cid > OSMUX_CID_MAX) {
1385 LOGP(DLMGCP, LOGL_ERROR,
1386 "Wrong Osmux CID %d, can not generate MGCP message\n",
1387 mgcp_msg->x_osmo_osmux_cid);
1388 msgb_free(msg);
1389 return NULL;
1390 }
Pau Espin Pedrol900cd652019-04-24 22:06:22 +02001391 snprintf(buf, sizeof(buf), " %d", mgcp_msg->x_osmo_osmux_cid);
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001392 rc |=
Pau Espin Pedrol900cd652019-04-24 22:06:22 +02001393 msgb_printf(msg, MGCP_X_OSMO_OSMUX_HEADER "%s\r\n",
1394 mgcp_msg->x_osmo_osmux_cid == -1 ? " *": buf);
1395 }
1396
1397
Philipp Maier704c4f02018-06-07 18:51:31 +02001398 /* Add session description protocol (SDP) */
1399 if (use_sdp
1400 && (mgcp_msg->verb == MGCP_VERB_CRCX
1401 || mgcp_msg->verb == MGCP_VERB_MDCX)) {
Philipp Maier7d86d4c2021-06-11 15:16:13 +02001402 if (add_sdp(msg, mgcp_msg, mgcp) < 0)
Philipp Maier9d25d7a2018-01-22 17:31:10 +01001403 return NULL;
Philipp Maier1dc6be62017-10-05 18:25:37 +02001404 }
1405
1406 if (rc != 0) {
1407 LOGP(DLMGCP, LOGL_ERROR,
1408 "message buffer to small, can not generate MGCP message\n");
1409 msgb_free(msg);
1410 msg = NULL;
1411 }
1412
1413 return msg;
1414}
1415
Philipp Maier275ac972018-01-20 00:58:16 +01001416/*! Retrieve the MGCP transaction ID from a msgb generated by mgcp_msg_gen()
1417 * \param[in] msg message buffer
1418 * \returns Transaction id. */
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +01001419mgcp_trans_id_t mgcp_msg_trans_id(struct msgb *msg)
1420{
1421 return (mgcp_trans_id_t)msg->cb[MSGB_CB_MGCP_TRANS_ID];
1422}
1423
Philipp Maier275ac972018-01-20 00:58:16 +01001424/*! Get the configuration parameters a given MGCP client instance
1425 * \param[in] mgcp MGCP client descriptor.
1426 * \returns configuration */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +02001427struct mgcp_client_conf *mgcp_client_conf_actual(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001428{
1429 return &mgcp->actual;
1430}
Neels Hofmeyrd95ab1e2017-09-22 00:52:54 +02001431
1432const struct value_string mgcp_client_connection_mode_strs[] = {
1433 { MGCP_CONN_NONE, "none" },
1434 { MGCP_CONN_RECV_SEND, "sendrecv" },
1435 { MGCP_CONN_SEND_ONLY, "sendonly" },
1436 { MGCP_CONN_RECV_ONLY, "recvonly" },
1437 { MGCP_CONN_LOOPBACK, "loopback" },
1438 { 0, NULL }
1439};