blob: a134273dd4365d7982976521c26294fc17a1446d [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
Philipp Maier1fc69992018-01-31 15:32:55 +0100680 rc = parse_head_params(r);
Philipp Maierffd75e42017-11-22 11:44:50 +0100681 if (rc) {
682 LOGP(DLMGCP, LOGL_ERROR, "Cannot parse MGCP response (head parameters)\n");
Philipp Maier1fc69992018-01-31 15:32:55 +0100683 rc = 1;
684 goto error;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200685 }
686
Philipp Maier1fc69992018-01-31 15:32:55 +0100687 pending = mgcp_client_response_pending_get(mgcp, r->head.trans_id);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200688 if (!pending) {
689 LOGP(DLMGCP, LOGL_ERROR,
690 "Cannot find matching MGCP transaction for trans_id %d\n",
Philipp Maier1fc69992018-01-31 15:32:55 +0100691 r->head.trans_id);
692 rc = -ENOENT;
693 goto error;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200694 }
695
Philipp Maier1fc69992018-01-31 15:32:55 +0100696 mgcp_client_handle_response(mgcp, pending, r);
697 rc = 0;
698
699error:
700 talloc_free(r);
701 return rc;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200702}
703
704static int mgcp_do_read(struct osmo_fd *fd)
705{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200706 struct mgcp_client *mgcp = fd->data;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200707 struct msgb *msg;
708 int ret;
709
710 msg = msgb_alloc_headroom(4096, 128, "mgcp_from_gw");
711 if (!msg) {
712 LOGP(DLMGCP, LOGL_ERROR, "Failed to allocate MGCP message.\n");
713 return -1;
714 }
715
716 ret = read(fd->fd, msg->data, 4096 - 128);
717 if (ret <= 0) {
Neels Hofmeyr0a403792018-12-12 03:10:18 +0100718 LOGP(DLMGCP, LOGL_ERROR, "Failed to read: %s: %d='%s'\n", osmo_sock_get_name2(fd->fd),
719 errno, strerror(errno));
720
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200721 msgb_free(msg);
722 return -1;
723 } else if (ret > 4096 - 128) {
Neels Hofmeyr0a403792018-12-12 03:10:18 +0100724 LOGP(DLMGCP, LOGL_ERROR, "Too much data: %s: %d\n", osmo_sock_get_name2(fd->fd), ret);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200725 msgb_free(msg);
726 return -1;
Harald Welte9bf7c532017-11-17 14:14:31 +0100727 }
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200728
729 msg->l2h = msgb_put(msg, ret);
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200730 ret = mgcp_client_rx(mgcp, msg);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200731 talloc_free(msg);
732 return ret;
733}
734
735static int mgcp_do_write(struct osmo_fd *fd, struct msgb *msg)
736{
737 int ret;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200738
Neels Hofmeyr0a403792018-12-12 03:10:18 +0100739 LOGP(DLMGCP, LOGL_DEBUG, "Tx MGCP: %s: len=%u '%s'...\n",
740 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 +0200741
742 ret = write(fd->fd, msg->data, msg->len);
743 if (ret != msg->len)
Neels Hofmeyr0a403792018-12-12 03:10:18 +0100744 LOGP(DLMGCP, LOGL_ERROR, "Failed to Tx MGCP: %s: %d='%s'; msg: len=%u '%s'...\n",
745 osmo_sock_get_name2(fd->fd), errno, strerror(errno),
Neels Hofmeyr32d15cc2018-12-12 03:05:33 +0100746 msg->len, osmo_escape_str((const char*)msg->data, OSMO_MIN(42, msg->len)));
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200747 return ret;
748}
749
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200750struct mgcp_client *mgcp_client_init(void *ctx,
751 struct mgcp_client_conf *conf)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200752{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200753 struct mgcp_client *mgcp;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200754
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200755 mgcp = talloc_zero(ctx, struct mgcp_client);
Harald Welteefe15712020-07-15 10:56:45 +0200756 if (!mgcp)
757 return NULL;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200758
759 INIT_LLIST_HEAD(&mgcp->responses_pending);
760 INIT_LLIST_HEAD(&mgcp->inuse_endpoints);
761
762 mgcp->next_trans_id = 1;
763
764 mgcp->actual.local_addr = conf->local_addr ? conf->local_addr :
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200765 MGCP_CLIENT_LOCAL_ADDR_DEFAULT;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200766 mgcp->actual.local_port = conf->local_port >= 0 ? (uint16_t)conf->local_port :
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200767 MGCP_CLIENT_LOCAL_PORT_DEFAULT;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200768
769 mgcp->actual.remote_addr = conf->remote_addr ? conf->remote_addr :
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200770 MGCP_CLIENT_REMOTE_ADDR_DEFAULT;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200771 mgcp->actual.remote_port = conf->remote_port >= 0 ? (uint16_t)conf->remote_port :
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200772 MGCP_CLIENT_REMOTE_PORT_DEFAULT;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200773
Neels Hofmeyrac69ea92018-12-19 00:27:50 +0100774 if (osmo_strlcpy(mgcp->actual.endpoint_domain_name, conf->endpoint_domain_name,
775 sizeof(mgcp->actual.endpoint_domain_name))
776 >= sizeof(mgcp->actual.endpoint_domain_name)) {
777 LOGP(DLMGCP, LOGL_ERROR, "MGCP client: endpoint domain name is too long, max length is %zu: '%s'\n",
778 sizeof(mgcp->actual.endpoint_domain_name) - 1, conf->endpoint_domain_name);
779 talloc_free(mgcp);
780 return NULL;
781 }
782 LOGP(DLMGCP, LOGL_NOTICE, "MGCP client: using endpoint domain '@%s'\n", mgcp_client_endpoint_domain(mgcp));
783
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200784 return mgcp;
785}
786
Philipp Maier6a26c162018-08-01 16:37:06 +0200787static int init_socket(struct mgcp_client *mgcp)
788{
789 int rc;
790 struct osmo_wqueue *wq;
791 int i;
792
793 wq = &mgcp->wq;
794
795 for (i = 0; i < 100; i++) {
796
797 /* Initalize socket with the currently configured port
798 * number */
Pau Espin Pedrol531470a2020-08-31 17:06:55 +0200799 rc = osmo_sock_init2_ofd(&wq->bfd, AF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, mgcp->actual.local_addr,
Philipp Maier6a26c162018-08-01 16:37:06 +0200800 mgcp->actual.local_port, mgcp->actual.remote_addr, mgcp->actual.remote_port,
801 OSMO_SOCK_F_BIND | OSMO_SOCK_F_CONNECT);
802 if (rc > 0)
803 return rc;
804
805 /* If there is a different port than the default port
806 * configured then we assume that the user has choosen
807 * that port conciously and we will not try to resolve
808 * this by silently choosing a different port. */
Philipp Maier9a7ccc32018-08-09 11:41:19 +0200809 if (mgcp->actual.local_port != MGCP_CLIENT_LOCAL_PORT_DEFAULT && i == 0)
Philipp Maier6a26c162018-08-01 16:37:06 +0200810 return -EINVAL;
811
812 /* Choose a new port number to try next */
813 LOGP(DLMGCP, LOGL_NOTICE,
Neels Hofmeyr0a403792018-12-12 03:10:18 +0100814 "MGCPGW failed to bind to %s:%u, retrying with port %u\n",
815 mgcp->actual.local_addr, mgcp->actual.local_port, mgcp->actual.local_port + 1);
Philipp Maier6a26c162018-08-01 16:37:06 +0200816 mgcp->actual.local_port++;
817 }
818
Neels Hofmeyr0a403792018-12-12 03:10:18 +0100819 LOGP(DLMGCP, LOGL_FATAL, "MGCPGW failed to find a port to bind on %i times.\n", i);
Philipp Maier6a26c162018-08-01 16:37:06 +0200820 return -EINVAL;
821}
822
Philipp Maier275ac972018-01-20 00:58:16 +0100823/*! Initalize client connection (opens socket only, no request is sent yet)
824 * \param[in,out] mgcp MGCP client descriptor.
825 * \returns 0 on success, -EINVAL on error. */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200826int mgcp_client_connect(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200827{
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200828 struct osmo_wqueue *wq;
829 int rc;
830
831 if (!mgcp) {
832 LOGP(DLMGCP, LOGL_FATAL, "MGCPGW client not initialized properly\n");
833 return -EINVAL;
834 }
835
836 wq = &mgcp->wq;
Harald Weltec2a8f592020-10-19 13:25:41 +0200837 osmo_wqueue_init(wq, 1024);
838 wq->read_cb = mgcp_do_read;
839 wq->write_cb = mgcp_do_write;
840
841 osmo_fd_setup(&wq->bfd, -1, OSMO_FD_READ, osmo_wqueue_bfd_cb, mgcp, 0);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200842
Philipp Maier6a26c162018-08-01 16:37:06 +0200843 rc = init_socket(mgcp);
Harald Welte8890dfa2017-11-17 15:09:30 +0100844 if (rc < 0) {
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200845 LOGP(DLMGCP, LOGL_FATAL,
Harald Welte8890dfa2017-11-17 15:09:30 +0100846 "Failed to initialize socket %s:%u -> %s:%u for MGCP GW: %s\n",
847 mgcp->actual.local_addr, mgcp->actual.local_port,
848 mgcp->actual.remote_addr, mgcp->actual.remote_port, strerror(errno));
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200849 goto error_close_fd;
850 }
851
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200852
Neels Hofmeyr0a403792018-12-12 03:10:18 +0100853 LOGP(DLMGCP, LOGL_INFO, "MGCP GW connection: %s\n", osmo_sock_get_name2(wq->bfd.fd));
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200854
855 return 0;
856error_close_fd:
857 close(wq->bfd.fd);
858 wq->bfd.fd = -1;
859 return rc;
860}
861
Philipp Maier275ac972018-01-20 00:58:16 +0100862/*! Get the IP-Aaddress of the associated MGW as string.
863 * \param[in] mgcp MGCP client descriptor.
864 * \returns a pointer to the address string. */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200865const char *mgcp_client_remote_addr_str(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200866{
867 return mgcp->actual.remote_addr;
868}
869
Philipp Maier275ac972018-01-20 00:58:16 +0100870/*! Get the IP-Port of the associated MGW.
871 * \param[in] mgcp MGCP client descriptor.
872 * \returns port number. */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200873uint16_t mgcp_client_remote_port(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200874{
875 return mgcp->actual.remote_port;
876}
877
Pau Espin Pedrol74d0e5c2020-09-02 17:19:20 +0200878/*! Get the IP-Address of the associated MGW as its numeric representation.
879 * DEPRECATED, DON'T USE.
Philipp Maier275ac972018-01-20 00:58:16 +0100880 * \param[in] mgcp MGCP client descriptor.
881 * \returns IP-Address as 32 bit integer (network byte order) */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200882uint32_t mgcp_client_remote_addr_n(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200883{
Pau Espin Pedrol74d0e5c2020-09-02 17:19:20 +0200884 return 0;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200885}
886
Neels Hofmeyrac69ea92018-12-19 00:27:50 +0100887/* To compose endpoint names, usually for CRCX, use this as domain name.
888 * For example, snprintf("rtpbridge\*@%s", mgcp_client_endpoint_domain(mgcp)). */
889const char *mgcp_client_endpoint_domain(const struct mgcp_client *mgcp)
890{
891 return mgcp->actual.endpoint_domain_name[0] ? mgcp->actual.endpoint_domain_name : "mgw";
892}
893
Pau Espin Pedrolca0818c2019-04-16 17:23:38 +0200894static const char *_mgcp_client_name_append_domain(const struct mgcp_client *mgcp, char *name)
Neels Hofmeyrac69ea92018-12-19 00:27:50 +0100895{
896 static char endpoint[MGCP_ENDPOINT_MAXLEN];
897 int rc;
898
Pau Espin Pedrolca0818c2019-04-16 17:23:38 +0200899 rc = snprintf(endpoint, sizeof(endpoint), "%s@%s", name, mgcp_client_endpoint_domain(mgcp));
Neels Hofmeyrac69ea92018-12-19 00:27:50 +0100900 if (rc > sizeof(endpoint) - 1) {
Pau Espin Pedrolca0818c2019-04-16 17:23:38 +0200901 LOGP(DLMGCP, LOGL_ERROR, "MGCP endpoint exceeds maximum length of %zu: '%s@%s'\n",
902 sizeof(endpoint) - 1, name, mgcp_client_endpoint_domain(mgcp));
Neels Hofmeyrac69ea92018-12-19 00:27:50 +0100903 return NULL;
904 }
905 if (rc < 1) {
906 LOGP(DLMGCP, LOGL_ERROR, "Cannot compose MGCP endpoint name\n");
907 return NULL;
908 }
909 return endpoint;
910}
911
Philipp Maiera466f572020-06-25 16:11:04 +0200912/*! Compose endpoint name for a wildcarded request to the virtual trunk
913 * \param[in] mgcp MGCP client descriptor.
914 * \returns string containing the endpoint name (e.g. rtpbridge\*@mgw) */
Pau Espin Pedrolca0818c2019-04-16 17:23:38 +0200915const char *mgcp_client_rtpbridge_wildcard(const struct mgcp_client *mgcp)
916{
917 return _mgcp_client_name_append_domain(mgcp, "rtpbridge/*");
918}
919
Philipp Maier48635912020-06-25 20:16:22 +0200920/*! Compose endpoint name for an E1 endpoint.
921 * \param[in] ctx talloc context.
922 * \param[in] mgcp MGCP client descriptor.
923 * \param[in] trunk_id id number of the E1 trunk (1-64).
924 * \param[in] ts timeslot on the E1 trunk (1-31).
925 * \param[in] rate bitrate used on the E1 trunk (e.g 16 for 16kbit).
926 * \param[in] offset bit offset of the E1 subslot (e.g. 4 for the third 16k subslot).
927 * \returns string containing the endpoint name (e.g. ds/e1-1/s-1/su16-4). */
928const char *mgcp_client_e1_epname(void *ctx, const struct mgcp_client *mgcp, uint8_t trunk_id, uint8_t ts,
929 uint8_t rate, uint8_t offset)
930{
931 /* See also comment in libosmo-mgcp, mgcp_client.c, gen_e1_epname() */
932 const uint8_t valid_rates[] = { 64, 32, 32, 16, 16, 16, 16, 8, 8, 8, 8, 8, 8, 8, 8 };
933 const uint8_t valid_offsets[] = { 0, 0, 4, 0, 2, 4, 6, 0, 1, 2, 3, 4, 5, 6, 7 };
934
935 uint8_t i;
936 bool rate_offs_valid = false;
937 char *epname;
938
939 epname =
940 talloc_asprintf(ctx, "ds/e1-%u/s-%u/su%u-%u@%s", trunk_id, ts, rate, offset,
941 mgcp_client_endpoint_domain(mgcp));
942 if (!epname) {
943 LOGP(DLMGCP, LOGL_ERROR, "Cannot compose MGCP e1-endpoint name!\n");
944 return NULL;
945 }
946
947 /* Check if the supplied rate/offset pair resembles a valid combination */
948 for (i = 0; i < sizeof(valid_rates); i++) {
949 if (valid_rates[i] == rate && valid_offsets[i] == offset)
950 rate_offs_valid = true;
951 }
952 if (!rate_offs_valid) {
953 LOGP(DLMGCP, LOGL_ERROR,
954 "Cannot compose MGCP e1-endpoint name (%s), rate(%u)/offset(%u) combination is invalid!\n", epname,
955 rate, offset);
956 talloc_free(epname);
957 return NULL;
958 }
959
960 /* An E1 line has a maximum of 32 timeslots, while the first (ts=0) is
961 * reserverd for framing and alignment, so we can not use it here. */
Philipp Maier92a73cd2020-11-26 22:21:11 +0100962 if (ts == 0 || ts > NUM_E1_TS-1) {
Philipp Maier48635912020-06-25 20:16:22 +0200963 LOGP(DLMGCP, LOGL_ERROR,
964 "Cannot compose MGCP e1-endpoint name (%s), E1-timeslot number (%u) is invalid!\n", epname, ts);
965 talloc_free(epname);
966 return NULL;
967 }
968
969 return epname;
970}
971
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200972struct mgcp_response_pending * mgcp_client_pending_add(
973 struct mgcp_client *mgcp,
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200974 mgcp_trans_id_t trans_id,
975 mgcp_response_cb_t response_cb,
976 void *priv)
977{
978 struct mgcp_response_pending *pending;
979
980 pending = talloc_zero(mgcp, struct mgcp_response_pending);
Harald Welte827da2c2020-07-15 10:57:08 +0200981 if (!pending)
982 return NULL;
983
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200984 pending->trans_id = trans_id;
985 pending->response_cb = response_cb;
986 pending->priv = priv;
987 llist_add_tail(&pending->entry, &mgcp->responses_pending);
988
989 return pending;
990}
991
992/* Send the MGCP message in msg to the MGCP GW and handle a response with
993 * response_cb. NOTE: the response_cb still needs to call
994 * mgcp_response_parse_params(response) to get the parsed parameters -- to
995 * potentially save some CPU cycles, only the head line has been parsed when
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100996 * the response_cb is invoked.
997 * Before the priv pointer becomes invalid, e.g. due to transaction timeout,
998 * mgcp_client_cancel() needs to be called for this transaction.
999 */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +02001000int mgcp_client_tx(struct mgcp_client *mgcp, struct msgb *msg,
1001 mgcp_response_cb_t response_cb, void *priv)
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001002{
Harald Welte563ffc52020-06-17 21:54:13 +07001003 struct mgcp_response_pending *pending = NULL;
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001004 mgcp_trans_id_t trans_id;
1005 int rc;
1006
1007 trans_id = msg->cb[MSGB_CB_MGCP_TRANS_ID];
1008 if (!trans_id) {
1009 LOGP(DLMGCP, LOGL_ERROR,
1010 "Unset transaction id in mgcp send request\n");
1011 talloc_free(msg);
1012 return -EINVAL;
1013 }
1014
Harald Welte563ffc52020-06-17 21:54:13 +07001015 /* Do not allocate a dummy 'mgcp_response_pending' entry */
1016 if (response_cb != NULL) {
1017 pending = mgcp_client_pending_add(mgcp, trans_id, response_cb, priv);
1018 if (!pending) {
1019 talloc_free(msg);
1020 return -ENOMEM;
1021 }
Harald Welte827da2c2020-07-15 10:57:08 +02001022 }
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001023
1024 if (msgb_l2len(msg) > 4096) {
1025 LOGP(DLMGCP, LOGL_ERROR,
1026 "Cannot send, MGCP message too large: %u\n",
1027 msgb_l2len(msg));
1028 msgb_free(msg);
1029 rc = -EINVAL;
1030 goto mgcp_tx_error;
1031 }
1032
1033 rc = osmo_wqueue_enqueue(&mgcp->wq, msg);
1034 if (rc) {
1035 LOGP(DLMGCP, LOGL_FATAL, "Could not queue message to MGCP GW\n");
1036 msgb_free(msg);
1037 goto mgcp_tx_error;
1038 } else
Neels Hofmeyred0c1aa2018-12-21 03:07:53 +01001039 LOGP(DLMGCP, LOGL_DEBUG, "Queued %u bytes for MGCP GW\n",
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001040 msgb_l2len(msg));
1041 return 0;
1042
1043mgcp_tx_error:
Harald Welte563ffc52020-06-17 21:54:13 +07001044 if (!pending)
Vadim Yanitskiyffbc6182020-07-16 15:48:13 +07001045 return rc;
Vadim Yanitskiy3f8139c2020-06-17 20:50:17 +07001046 /* Dequeue pending response, it's going to be free()d */
1047 llist_del(&pending->entry);
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001048 /* Pass NULL to response cb to indicate an error */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +02001049 mgcp_client_handle_response(mgcp, pending, NULL);
Vadim Yanitskiyffbc6182020-07-16 15:48:13 +07001050 return rc;
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001051}
1052
Philipp Maier275ac972018-01-20 00:58:16 +01001053/*! Cancel a pending transaction.
1054 * \param[in] mgcp MGCP client descriptor.
1055 * \param[in,out] trans_id Transaction id.
1056 * \returns 0 on success, -ENOENT on error.
1057 *
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +01001058 * Should a priv pointer passed to mgcp_client_tx() become invalid, this function must be called. In
1059 * practical terms, if the caller of mgcp_client_tx() wishes to tear down a transaction without having
1060 * received a response this function must be called. The trans_id can be obtained by calling
Philipp Maier275ac972018-01-20 00:58:16 +01001061 * mgcp_msg_trans_id() on the msgb produced by mgcp_msg_gen(). */
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +01001062int mgcp_client_cancel(struct mgcp_client *mgcp, mgcp_trans_id_t trans_id)
1063{
1064 struct mgcp_response_pending *pending = mgcp_client_response_pending_get(mgcp, trans_id);
1065 if (!pending) {
Philipp Maier275ac972018-01-20 00:58:16 +01001066 /*! Note: it is not harmful to cancel a transaction twice. */
Neels Hofmeyred0c1aa2018-12-21 03:07:53 +01001067 LOGP(DLMGCP, LOGL_ERROR, "Cannot cancel, no such transaction: %u\n", trans_id);
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +01001068 return -ENOENT;
1069 }
Neels Hofmeyred0c1aa2018-12-21 03:07:53 +01001070 LOGP(DLMGCP, LOGL_DEBUG, "Canceled transaction %u\n", trans_id);
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +01001071 talloc_free(pending);
1072 return 0;
Philipp Maier275ac972018-01-20 00:58:16 +01001073 /*! We don't really need to clean up the wqueue: In all sane cases, the msgb has already been sent
1074 * out and is no longer in the wqueue. If it still is in the wqueue, then sending MGCP messages
1075 * per se is broken and the program should notice so by a full wqueue. Even if this was called
1076 * before we had a chance to send out the message and it is still going to be sent, we will just
1077 * ignore the reply to it later. Removing a msgb from the wqueue here would just introduce more
1078 * bug surface in terms of failing to update wqueue API's counters or some such.
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +01001079 */
1080}
1081
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +02001082static mgcp_trans_id_t mgcp_client_next_trans_id(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001083{
1084 /* avoid zero trans_id to distinguish from unset trans_id */
1085 if (!mgcp->next_trans_id)
1086 mgcp->next_trans_id ++;
1087 return mgcp->next_trans_id ++;
1088}
1089
Philipp Maier1dc6be62017-10-05 18:25:37 +02001090#define MGCP_CRCX_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT | \
1091 MGCP_MSG_PRESENCE_CALL_ID | \
Philipp Maier1dc6be62017-10-05 18:25:37 +02001092 MGCP_MSG_PRESENCE_CONN_MODE)
1093#define MGCP_MDCX_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT | \
Philipp Maier490cbaa2018-01-22 17:32:38 +01001094 MGCP_MSG_PRESENCE_CALL_ID | \
Philipp Maier1dc6be62017-10-05 18:25:37 +02001095 MGCP_MSG_PRESENCE_CONN_ID)
1096#define MGCP_DLCX_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT)
1097#define MGCP_AUEP_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT)
1098#define MGCP_RSIP_MANDATORY 0 /* none */
1099
Philipp Maier704c4f02018-06-07 18:51:31 +02001100/* Helper function for mgcp_msg_gen(): Add LCO information to MGCP message */
1101static int add_lco(struct msgb *msg, struct mgcp_msg *mgcp_msg)
1102{
1103 unsigned int i;
1104 int rc = 0;
1105 const char *codec;
1106 unsigned int pt;
1107
1108 rc += msgb_printf(msg, "L:");
1109
1110 if (mgcp_msg->ptime)
1111 rc += msgb_printf(msg, " p:%u,", mgcp_msg->ptime);
1112
1113 if (mgcp_msg->codecs_len) {
1114 rc += msgb_printf(msg, " a:");
1115 for (i = 0; i < mgcp_msg->codecs_len; i++) {
1116 pt = mgcp_msg->codecs[i];
Neels Hofmeyr47642f22019-02-27 05:56:53 +01001117 codec = get_value_string_or_null(osmo_mgcpc_codec_names, pt);
Pau Espin Pedrolc12bfb72019-04-16 17:23:09 +02001118
Philipp Maier704c4f02018-06-07 18:51:31 +02001119 /* Note: Use codec descriptors from enum mgcp_codecs
1120 * in mgcp_client only! */
1121 OSMO_ASSERT(codec);
1122 rc += msgb_printf(msg, "%s", extract_codec_name(codec));
1123 if (i < mgcp_msg->codecs_len - 1)
1124 rc += msgb_printf(msg, ";");
1125 }
1126 rc += msgb_printf(msg, ",");
1127 }
1128
1129 rc += msgb_printf(msg, " nt:IN\r\n");
1130
1131 return rc;
1132}
1133
1134/* Helper function for mgcp_msg_gen(): Add SDP information to MGCP message */
1135static int add_sdp(struct msgb *msg, struct mgcp_msg *mgcp_msg, struct mgcp_client *mgcp)
1136{
1137 unsigned int i;
1138 int rc = 0;
Pau Espin Pedrol8667d512020-08-28 19:37:08 +02001139 char local_ip[INET6_ADDRSTRLEN];
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +02001140 int local_ip_family, audio_ip_family;
Philipp Maier704c4f02018-06-07 18:51:31 +02001141 const char *codec;
1142 unsigned int pt;
1143
1144 /* Add separator to mark the beginning of the SDP block */
1145 rc += msgb_printf(msg, "\r\n");
1146
1147 /* Add SDP protocol version */
1148 rc += msgb_printf(msg, "v=0\r\n");
1149
1150 /* Determine local IP-Address */
1151 if (osmo_sock_local_ip(local_ip, mgcp->actual.remote_addr) < 0) {
1152 LOGP(DLMGCP, LOGL_ERROR,
1153 "Could not determine local IP-Address!\n");
1154 msgb_free(msg);
1155 return -2;
1156 }
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +02001157 local_ip_family = osmo_ip_str_type(local_ip);
1158 if (local_ip_family == AF_UNSPEC) {
1159 msgb_free(msg);
1160 return -2;
1161 }
1162 audio_ip_family = osmo_ip_str_type(mgcp_msg->audio_ip);
1163 if (audio_ip_family == AF_UNSPEC) {
1164 msgb_free(msg);
1165 return -2;
1166 }
Philipp Maier704c4f02018-06-07 18:51:31 +02001167
1168 /* Add owner/creator (SDP) */
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +02001169 rc += msgb_printf(msg, "o=- %x 23 IN IP%c %s\r\n", mgcp_msg->call_id,
1170 local_ip_family == AF_INET6 ? '6' : '4',
1171 local_ip);
Philipp Maier704c4f02018-06-07 18:51:31 +02001172
1173 /* Add session name (none) */
1174 rc += msgb_printf(msg, "s=-\r\n");
1175
1176 /* Add RTP address and port */
1177 if (mgcp_msg->audio_port == 0) {
1178 LOGP(DLMGCP, LOGL_ERROR,
1179 "Invalid port number, can not generate MGCP message\n");
1180 msgb_free(msg);
1181 return -2;
1182 }
1183 if (strlen(mgcp_msg->audio_ip) <= 0) {
1184 LOGP(DLMGCP, LOGL_ERROR,
1185 "Empty ip address, can not generate MGCP message\n");
1186 msgb_free(msg);
1187 return -2;
1188 }
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +02001189 rc += msgb_printf(msg, "c=IN IP%c %s\r\n",
1190 audio_ip_family == AF_INET6 ? '6' : '4',
1191 mgcp_msg->audio_ip);
Philipp Maier704c4f02018-06-07 18:51:31 +02001192
1193 /* Add time description, active time (SDP) */
1194 rc += msgb_printf(msg, "t=0 0\r\n");
1195
1196 rc += msgb_printf(msg, "m=audio %u RTP/AVP", mgcp_msg->audio_port);
1197 for (i = 0; i < mgcp_msg->codecs_len; i++) {
1198 pt = map_codec_to_pt(mgcp_msg->ptmap, mgcp_msg->ptmap_len, mgcp_msg->codecs[i]);
1199 rc += msgb_printf(msg, " %u", pt);
1200
1201 }
1202 rc += msgb_printf(msg, "\r\n");
1203
Philipp Maier228e5912019-03-05 13:56:59 +01001204 /* Add optional codec parameters (fmtp) */
1205 if (mgcp_msg->param_present) {
1206 for (i = 0; i < mgcp_msg->codecs_len; i++) {
1207 /* The following is only applicable for AMR */
1208 if (mgcp_msg->codecs[i] != CODEC_AMR_8000_1 && mgcp_msg->codecs[i] != CODEC_AMRWB_16000_1)
1209 continue;
1210 pt = map_codec_to_pt(mgcp_msg->ptmap, mgcp_msg->ptmap_len, mgcp_msg->codecs[i]);
1211 if (mgcp_msg->param.amr_octet_aligned_present && mgcp_msg->param.amr_octet_aligned)
1212 rc += msgb_printf(msg, "a=fmtp:%u octet-align=1\r\n", pt);
1213 else if (mgcp_msg->param.amr_octet_aligned_present && !mgcp_msg->param.amr_octet_aligned)
1214 rc += msgb_printf(msg, "a=fmtp:%u octet-align=0\r\n", pt);
1215 }
1216 }
1217
Philipp Maier704c4f02018-06-07 18:51:31 +02001218 for (i = 0; i < mgcp_msg->codecs_len; i++) {
1219 pt = map_codec_to_pt(mgcp_msg->ptmap, mgcp_msg->ptmap_len, mgcp_msg->codecs[i]);
Pau Espin Pedrolc12bfb72019-04-16 17:23:09 +02001220
Philipp Maier704c4f02018-06-07 18:51:31 +02001221 /* Note: Only dynamic payload type from the range 96-127
1222 * require to be explained further via rtpmap. All others
1223 * are implcitly definedby the number in m=audio */
1224 if (pt >= 96 && pt <= 127) {
Neels Hofmeyr47642f22019-02-27 05:56:53 +01001225 codec = get_value_string_or_null(osmo_mgcpc_codec_names, mgcp_msg->codecs[i]);
Philipp Maier704c4f02018-06-07 18:51:31 +02001226
1227 /* Note: Use codec descriptors from enum mgcp_codecs
1228 * in mgcp_client only! */
1229 OSMO_ASSERT(codec);
Pau Espin Pedrolc12bfb72019-04-16 17:23:09 +02001230
Philipp Maier704c4f02018-06-07 18:51:31 +02001231 rc += msgb_printf(msg, "a=rtpmap:%u %s\r\n", pt, codec);
1232 }
1233 }
Pau Espin Pedrolc12bfb72019-04-16 17:23:09 +02001234
Philipp Maier704c4f02018-06-07 18:51:31 +02001235 if (mgcp_msg->ptime)
1236 rc += msgb_printf(msg, "a=ptime:%u\r\n", mgcp_msg->ptime);
1237
1238 return rc;
1239}
1240
Philipp Maier275ac972018-01-20 00:58:16 +01001241/*! Generate an MGCP message
1242 * \param[in] mgcp MGCP client descriptor.
1243 * \param[in] mgcp_msg Message description
1244 * \returns message buffer on success, NULL on error. */
Philipp Maier1dc6be62017-10-05 18:25:37 +02001245struct msgb *mgcp_msg_gen(struct mgcp_client *mgcp, struct mgcp_msg *mgcp_msg)
1246{
1247 mgcp_trans_id_t trans_id = mgcp_client_next_trans_id(mgcp);
1248 uint32_t mandatory_mask;
1249 struct msgb *msg = msgb_alloc_headroom(4096, 128, "MGCP tx");
1250 int rc = 0;
Philipp Maier704c4f02018-06-07 18:51:31 +02001251 int rc_sdp;
1252 bool use_sdp = false;
Pau Espin Pedrol900cd652019-04-24 22:06:22 +02001253 char buf[32];
Philipp Maier1dc6be62017-10-05 18:25:37 +02001254
1255 msg->l2h = msg->data;
1256 msg->cb[MSGB_CB_MGCP_TRANS_ID] = trans_id;
1257
1258 /* Add command verb */
1259 switch (mgcp_msg->verb) {
1260 case MGCP_VERB_CRCX:
1261 mandatory_mask = MGCP_CRCX_MANDATORY;
1262 rc += msgb_printf(msg, "CRCX %u", trans_id);
1263 break;
1264 case MGCP_VERB_MDCX:
1265 mandatory_mask = MGCP_MDCX_MANDATORY;
1266 rc += msgb_printf(msg, "MDCX %u", trans_id);
1267 break;
1268 case MGCP_VERB_DLCX:
1269 mandatory_mask = MGCP_DLCX_MANDATORY;
1270 rc += msgb_printf(msg, "DLCX %u", trans_id);
1271 break;
1272 case MGCP_VERB_AUEP:
1273 mandatory_mask = MGCP_AUEP_MANDATORY;
1274 rc += msgb_printf(msg, "AUEP %u", trans_id);
1275 break;
1276 case MGCP_VERB_RSIP:
1277 mandatory_mask = MGCP_RSIP_MANDATORY;
1278 rc += msgb_printf(msg, "RSIP %u", trans_id);
1279 break;
1280 default:
1281 LOGP(DLMGCP, LOGL_ERROR,
1282 "Invalid command verb, can not generate MGCP message\n");
1283 msgb_free(msg);
1284 return NULL;
1285 }
1286
1287 /* Check if mandatory fields are missing */
1288 if (!((mgcp_msg->presence & mandatory_mask) == mandatory_mask)) {
1289 LOGP(DLMGCP, LOGL_ERROR,
1290 "One or more missing mandatory fields, can not generate MGCP message\n");
1291 msgb_free(msg);
1292 return NULL;
1293 }
1294
1295 /* Add endpoint name */
Philipp Maier7bc55522017-12-10 22:52:22 +01001296 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_ENDPOINT) {
1297 if (strlen(mgcp_msg->endpoint) <= 0) {
1298 LOGP(DLMGCP, LOGL_ERROR,
1299 "Empty endpoint name, can not generate MGCP message\n");
1300 msgb_free(msg);
1301 return NULL;
1302 }
Philipp Maier3aa81572018-01-31 14:13:45 +01001303
1304 if (strstr(mgcp_msg->endpoint, "@") == NULL) {
1305 LOGP(DLMGCP, LOGL_ERROR,
1306 "Endpoint name (%s) lacks separator (@), can not generate MGCP message\n",
1307 mgcp_msg->endpoint);
1308 msgb_free(msg);
Vadim Yanitskiye6743452020-06-18 03:04:13 +07001309 return NULL;
Philipp Maier3aa81572018-01-31 14:13:45 +01001310 }
1311
Philipp Maier1dc6be62017-10-05 18:25:37 +02001312 rc += msgb_printf(msg, " %s", mgcp_msg->endpoint);
Philipp Maier7bc55522017-12-10 22:52:22 +01001313 }
Philipp Maier1dc6be62017-10-05 18:25:37 +02001314
1315 /* Add protocol version */
1316 rc += msgb_printf(msg, " MGCP 1.0\r\n");
1317
1318 /* Add call id */
1319 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_CALL_ID)
1320 rc += msgb_printf(msg, "C: %x\r\n", mgcp_msg->call_id);
1321
1322 /* Add connection id */
Philipp Maier7bc55522017-12-10 22:52:22 +01001323 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_CONN_ID) {
1324 if (strlen(mgcp_msg->conn_id) <= 0) {
1325 LOGP(DLMGCP, LOGL_ERROR,
1326 "Empty connection id, can not generate MGCP message\n");
1327 msgb_free(msg);
1328 return NULL;
1329 }
Philipp Maier01d24a32017-11-21 17:26:09 +01001330 rc += msgb_printf(msg, "I: %s\r\n", mgcp_msg->conn_id);
Philipp Maier7bc55522017-12-10 22:52:22 +01001331 }
Philipp Maier1dc6be62017-10-05 18:25:37 +02001332
Philipp Maier704c4f02018-06-07 18:51:31 +02001333 /* Using SDP makes sense when a valid IP/Port combination is specifiec,
1334 * if we do not know this information yet, we fall back to LCO */
1335 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_AUDIO_IP
1336 && mgcp_msg->presence & MGCP_MSG_PRESENCE_AUDIO_PORT)
1337 use_sdp = true;
1338
1339 /* Add local connection options (LCO) */
1340 if (!use_sdp
1341 && (mgcp_msg->verb == MGCP_VERB_CRCX
1342 || mgcp_msg->verb == MGCP_VERB_MDCX))
1343 rc += add_lco(msg, mgcp_msg);
Philipp Maier1dc6be62017-10-05 18:25:37 +02001344
1345 /* Add mode */
1346 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_CONN_MODE)
1347 rc +=
1348 msgb_printf(msg, "M: %s\r\n",
1349 mgcp_client_cmode_name(mgcp_msg->conn_mode));
1350
Neels Hofmeyre6d8e912018-08-23 16:36:48 +02001351 /* Add X-Osmo-IGN */
1352 if ((mgcp_msg->presence & MGCP_MSG_PRESENCE_X_OSMO_IGN)
1353 && (mgcp_msg->x_osmo_ign != 0))
1354 rc +=
1355 msgb_printf(msg, MGCP_X_OSMO_IGN_HEADER "%s\r\n",
1356 mgcp_msg->x_osmo_ign & MGCP_X_OSMO_IGN_CALLID ? " C": "");
1357
Pau Espin Pedrol900cd652019-04-24 22:06:22 +02001358 /* Add X-Osmo-Osmux */
1359 if ((mgcp_msg->presence & MGCP_MSG_PRESENCE_X_OSMO_OSMUX_CID)) {
Pau Espin Pedrol1b1d7ed2019-05-23 16:48:24 +02001360 if (mgcp_msg->x_osmo_osmux_cid < -1 || mgcp_msg->x_osmo_osmux_cid > OSMUX_CID_MAX) {
1361 LOGP(DLMGCP, LOGL_ERROR,
1362 "Wrong Osmux CID %d, can not generate MGCP message\n",
1363 mgcp_msg->x_osmo_osmux_cid);
1364 msgb_free(msg);
1365 return NULL;
1366 }
Pau Espin Pedrol900cd652019-04-24 22:06:22 +02001367 snprintf(buf, sizeof(buf), " %d", mgcp_msg->x_osmo_osmux_cid);
1368 rc +=
1369 msgb_printf(msg, MGCP_X_OSMO_OSMUX_HEADER "%s\r\n",
1370 mgcp_msg->x_osmo_osmux_cid == -1 ? " *": buf);
1371 }
1372
1373
Philipp Maier704c4f02018-06-07 18:51:31 +02001374 /* Add session description protocol (SDP) */
1375 if (use_sdp
1376 && (mgcp_msg->verb == MGCP_VERB_CRCX
1377 || mgcp_msg->verb == MGCP_VERB_MDCX)) {
1378 rc_sdp = add_sdp(msg, mgcp_msg, mgcp);
1379 if (rc_sdp == -2)
Philipp Maier9d25d7a2018-01-22 17:31:10 +01001380 return NULL;
Philipp Maier704c4f02018-06-07 18:51:31 +02001381 else
1382 rc += rc_sdp;
Philipp Maier1dc6be62017-10-05 18:25:37 +02001383 }
1384
1385 if (rc != 0) {
1386 LOGP(DLMGCP, LOGL_ERROR,
1387 "message buffer to small, can not generate MGCP message\n");
1388 msgb_free(msg);
1389 msg = NULL;
1390 }
1391
1392 return msg;
1393}
1394
Philipp Maier275ac972018-01-20 00:58:16 +01001395/*! Retrieve the MGCP transaction ID from a msgb generated by mgcp_msg_gen()
1396 * \param[in] msg message buffer
1397 * \returns Transaction id. */
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +01001398mgcp_trans_id_t mgcp_msg_trans_id(struct msgb *msg)
1399{
1400 return (mgcp_trans_id_t)msg->cb[MSGB_CB_MGCP_TRANS_ID];
1401}
1402
Philipp Maier275ac972018-01-20 00:58:16 +01001403/*! Get the configuration parameters a given MGCP client instance
1404 * \param[in] mgcp MGCP client descriptor.
1405 * \returns configuration */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +02001406struct mgcp_client_conf *mgcp_client_conf_actual(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001407{
1408 return &mgcp->actual;
1409}
Neels Hofmeyrd95ab1e2017-09-22 00:52:54 +02001410
1411const struct value_string mgcp_client_connection_mode_strs[] = {
1412 { MGCP_CONN_NONE, "none" },
1413 { MGCP_CONN_RECV_SEND, "sendrecv" },
1414 { MGCP_CONN_SEND_ONLY, "sendonly" },
1415 { MGCP_CONN_RECV_ONLY, "recvonly" },
1416 { MGCP_CONN_LOOPBACK, "loopback" },
1417 { 0, NULL }
1418};