blob: 6f4c1f36efe88f0ba5531d67cab2e01fe1a75189 [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
33#include <netinet/in.h>
34#include <arpa/inet.h>
35
36#include <errno.h>
37#include <unistd.h>
38#include <string.h>
Pau Espin Pedrol166077e2019-06-26 12:21:38 +020039#include <ctype.h>
Pau Espin Pedrolc5c14302019-07-23 16:19:41 +020040#include <stdlib.h>
41#include <limits.h>
Neels Hofmeyre9920f22017-07-10 15:07:22 +020042
Pau Espin Pedrol1b1d7ed2019-05-23 16:48:24 +020043#ifndef OSMUX_CID_MAX
44#define OSMUX_CID_MAX 255 /* FIXME: use OSMUX_CID_MAX from libosmo-netif? */
45#endif
46
Philipp Maier704c4f02018-06-07 18:51:31 +020047/* Codec descripton for dynamic payload types (SDP) */
Neels Hofmeyr47642f22019-02-27 05:56:53 +010048const struct value_string osmo_mgcpc_codec_names[] = {
Philipp Maier704c4f02018-06-07 18:51:31 +020049 { CODEC_PCMU_8000_1, "PCMU/8000/1" },
50 { CODEC_GSM_8000_1, "GSM/8000/1" },
51 { CODEC_PCMA_8000_1, "PCMA/8000/1" },
52 { CODEC_G729_8000_1, "G729/8000/1" },
53 { CODEC_GSMEFR_8000_1, "GSM-EFR/8000/1" },
54 { CODEC_GSMHR_8000_1, "GSM-HR-08/8000/1" },
55 { CODEC_AMR_8000_1, "AMR/8000/1" },
56 { CODEC_AMRWB_16000_1, "AMR-WB/16000/1" },
57 { 0, NULL },
58};
59
60/* Get encoding name from a full codec string e,g.
61 * ("CODEC/8000/2" => returns "CODEC") */
62static char *extract_codec_name(const char *str)
63{
64 static char buf[64];
65 unsigned int i;
66
67 if (!str)
68 return NULL;
69
70 /* FIXME osmo_strlcpy */
71 osmo_strlcpy(buf, str, sizeof(buf));
72
73 for (i = 0; i < strlen(buf); i++) {
74 if (buf[i] == '/')
75 buf[i] = '\0';
76 }
77
78 return buf;
79}
80
81/*! Map a string to a codec.
82 * \ptmap[in] str input string (e.g "GSM/8000/1", "GSM/8000" or "GSM")
83 * \returns codec that corresponds to the given string representation. */
84enum mgcp_codecs map_str_to_codec(const char *str)
85{
86 unsigned int i;
87 char *codec_name;
88 char str_buf[64];
89
90 osmo_strlcpy(str_buf, extract_codec_name(str), sizeof(str_buf));
91
Neels Hofmeyr47642f22019-02-27 05:56:53 +010092 for (i = 0; i < ARRAY_SIZE(osmo_mgcpc_codec_names); i++) {
93 codec_name = extract_codec_name(osmo_mgcpc_codec_names[i].str);
Philipp Maier704c4f02018-06-07 18:51:31 +020094 if (!codec_name)
95 continue;
96 if (strcmp(codec_name, str_buf) == 0)
Neels Hofmeyr47642f22019-02-27 05:56:53 +010097 return osmo_mgcpc_codec_names[i].value;
Philipp Maier704c4f02018-06-07 18:51:31 +020098 }
99
100 return -1;
101}
102
103/* Check the ptmap for illegal mappings */
Neels Hofmeyr84274e42019-04-27 19:08:36 +0200104static int check_ptmap(const struct ptmap *ptmap)
Philipp Maier704c4f02018-06-07 18:51:31 +0200105{
106 /* Check if there are mappings that leave the IANA assigned dynamic
107 * payload type range. Under normal conditions such mappings should
108 * not occur */
109
110 /* Its ok to have a 1:1 mapping in the statically defined
111 * range, this won't hurt */
112 if (ptmap->codec == ptmap->pt)
113 return 0;
114
115 if (ptmap->codec < 96 || ptmap->codec > 127)
116 goto error;
117 if (ptmap->pt < 96 || ptmap->pt > 127)
118 goto error;
119
120 return 0;
121error:
122 LOGP(DLMGCP, LOGL_ERROR,
123 "ptmap contains illegal mapping: codec=%u maps to pt=%u\n",
124 ptmap->codec, ptmap->pt);
125 return -1;
126}
127
128/*! Map a codec to a payload type.
129 * \ptmap[in] payload pointer to payload type map with specified payload types.
130 * \ptmap[in] ptmap_len length of the payload type map.
131 * \ptmap[in] codec the codec for which the payload type should be looked up.
132 * \returns assigned payload type */
Neels Hofmeyr84274e42019-04-27 19:08:36 +0200133unsigned int map_codec_to_pt(const struct ptmap *ptmap, unsigned int ptmap_len,
Philipp Maier704c4f02018-06-07 18:51:31 +0200134 enum mgcp_codecs codec)
135{
136 unsigned int i;
137
138 /*! Note: If the payload type map is empty or the codec is not found
139 * in the map, then a 1:1 mapping is performed. If the codec falls
140 * into the statically defined range or if the mapping table isself
141 * tries to map to the statically defined range, then the mapping
142 * is also ignored and a 1:1 mapping is performed instead. */
143
144 /* we may return the codec directly since enum mgcp_codecs directly
145 * corresponds to the statićally assigned payload types */
146 if (codec < 96 || codec > 127)
147 return codec;
148
149 for (i = 0; i < ptmap_len; i++) {
150 /* Skip illegal map entries */
151 if (check_ptmap(ptmap) == 0 && ptmap->codec == codec)
152 return ptmap->pt;
153 ptmap++;
154 }
155
156 /* If nothing is found, do not perform any mapping */
157 return codec;
158}
159
160/*! Map a payload type to a codec.
161 * \ptmap[in] payload pointer to payload type map with specified payload types.
162 * \ptmap[in] ptmap_len length of the payload type map.
163 * \ptmap[in] payload type for which the codec should be looked up.
164 * \returns codec that corresponds to the specified payload type */
165enum mgcp_codecs map_pt_to_codec(struct ptmap *ptmap, unsigned int ptmap_len,
166 unsigned int pt)
167{
168 unsigned int i;
169
170 /*! Note: If the payload type map is empty or the payload type is not
171 * found in the map, then a 1:1 mapping is performed. If the payload
172 * type falls into the statically defined range or if the mapping
173 * table isself tries to map to the statically defined range, then
174 * the mapping is also ignored and a 1:1 mapping is performed
175 * instead. */
176
177 /* See also note in map_codec_to_pt() */
178 if (pt < 96 || pt > 127)
179 return pt;
180
181 for (i = 0; i < ptmap_len; i++) {
182 if (check_ptmap(ptmap) == 0 && ptmap->pt == pt)
183 return ptmap->codec;
184 ptmap++;
185 }
186
187 /* If nothing is found, do not perform any mapping */
188 return pt;
189}
190
Philipp Maier275ac972018-01-20 00:58:16 +0100191/*! Initalize MGCP client configuration struct with default values.
192 * \param[out] conf Client configuration.*/
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200193void mgcp_client_conf_init(struct mgcp_client_conf *conf)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200194{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200195 /* NULL and -1 default to MGCP_CLIENT_*_DEFAULT values */
196 *conf = (struct mgcp_client_conf){
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200197 .local_addr = NULL,
198 .local_port = -1,
199 .remote_addr = NULL,
200 .remote_port = -1,
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200201 };
202}
203
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200204static void mgcp_client_handle_response(struct mgcp_client *mgcp,
205 struct mgcp_response_pending *pending,
206 struct mgcp_response *response)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200207{
208 if (!pending) {
209 LOGP(DLMGCP, LOGL_ERROR,
210 "Cannot handle NULL response\n");
211 return;
212 }
213 if (pending->response_cb)
214 pending->response_cb(response, pending->priv);
215 else
Neels Hofmeyred0c1aa2018-12-21 03:07:53 +0100216 LOGP(DLMGCP, LOGL_DEBUG, "MGCP response ignored (NULL cb)\n");
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200217 talloc_free(pending);
218}
219
220static int mgcp_response_parse_head(struct mgcp_response *r, struct msgb *msg)
221{
222 int comment_pos;
223 char *end;
224
225 if (mgcp_msg_terminate_nul(msg))
226 goto response_parse_failure;
227
228 r->body = (char *)msg->data;
229
Harald Welte9bf7c532017-11-17 14:14:31 +0100230 if (sscanf(r->body, "%3d %u %n",
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200231 &r->head.response_code, &r->head.trans_id,
232 &comment_pos) != 2)
233 goto response_parse_failure;
234
Philipp Maierabe8c892018-01-20 00:15:12 +0100235 osmo_strlcpy(r->head.comment, r->body + comment_pos, sizeof(r->head.comment));
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200236 end = strchr(r->head.comment, '\r');
237 if (!end)
238 goto response_parse_failure;
239 /* Mark the end of the comment */
240 *end = '\0';
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200241 return 0;
242
243response_parse_failure:
244 LOGP(DLMGCP, LOGL_ERROR,
245 "Failed to parse MGCP response header\n");
246 return -EINVAL;
247}
248
249/* TODO undup against mgcp_protocol.c:mgcp_check_param() */
250static bool mgcp_line_is_valid(const char *line)
251{
252 const size_t line_len = strlen(line);
253 if (line[0] == '\0')
254 return true;
255
256 if (line_len < 2
257 || line[1] != '=') {
258 LOGP(DLMGCP, LOGL_ERROR,
259 "Wrong MGCP option format: '%s'\n",
260 line);
261 return false;
262 }
263
264 return true;
265}
266
Philipp Maier704c4f02018-06-07 18:51:31 +0200267/* Parse a line like "m=audio 16002 RTP/AVP 98", extract port and payload types */
268static int mgcp_parse_audio_port_pt(struct mgcp_response *r, char *line)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200269{
Philipp Maier704c4f02018-06-07 18:51:31 +0200270 char *pt_str;
Pau Espin Pedrolc5c14302019-07-23 16:19:41 +0200271 char *pt_end;
Pau Espin Pedrola2b1c5e2019-07-26 14:13:14 +0200272 unsigned long int pt;
Philipp Maier704c4f02018-06-07 18:51:31 +0200273 unsigned int count = 0;
274 unsigned int i;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200275
Philipp Maier704c4f02018-06-07 18:51:31 +0200276 /* Extract port information */
277 if (sscanf(line, "m=audio %hu", &r->audio_port) != 1)
278 goto response_parse_failure_port;
Philipp Maier10f32db2017-12-13 12:34:34 +0100279 if (r->audio_port == 0)
Philipp Maier704c4f02018-06-07 18:51:31 +0200280 goto response_parse_failure_port;
Philipp Maier10f32db2017-12-13 12:34:34 +0100281
Philipp Maier704c4f02018-06-07 18:51:31 +0200282 /* Extract payload types */
283 line = strstr(line, "RTP/AVP ");
284 if (!line)
285 goto exit;
286
287 pt_str = strtok(line, " ");
288 while (1) {
289 /* Do not allow excessive payload types */
290 if (count > ARRAY_SIZE(r->codecs))
291 goto response_parse_failure_pt;
292
293 pt_str = strtok(NULL, " ");
294 if (!pt_str)
295 break;
Pau Espin Pedrolc5c14302019-07-23 16:19:41 +0200296 errno = 0;
297 pt = strtoul(pt_str, &pt_end, 0);
298 if ((errno == ERANGE && pt == ULONG_MAX) || (errno && !pt) ||
299 pt_str == pt_end)
300 goto response_parse_failure_pt;
Philipp Maier704c4f02018-06-07 18:51:31 +0200301
Pau Espin Pedrola2b1c5e2019-07-26 14:13:14 +0200302 if (pt >> 7) /* PT is 7 bit field, higher values not allowed */
303 goto response_parse_failure_pt;
304
Philipp Maier704c4f02018-06-07 18:51:31 +0200305 /* Do not allow duplicate payload types */
306 for (i = 0; i < count; i++)
307 if (r->codecs[i] == pt)
308 goto response_parse_failure_pt;
309
310 /* Note: The payload type we store may not necessarly match
311 * the codec types we have defined in enum mgcp_codecs. To
312 * ensure that the end result only contains codec types which
313 * match enum mgcp_codecs, we will go through afterwards and
314 * remap the affected entries with the inrofmation we learn
315 * from rtpmap */
316 r->codecs[count] = pt;
317 count++;
318 }
319
320 r->codecs_len = count;
321
322exit:
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200323 return 0;
324
Philipp Maier704c4f02018-06-07 18:51:31 +0200325response_parse_failure_port:
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200326 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier704c4f02018-06-07 18:51:31 +0200327 "Failed to parse SDP parameter port (%s)\n", line);
Philipp Maier06da85e2017-10-05 18:49:24 +0200328 return -EINVAL;
Philipp Maier704c4f02018-06-07 18:51:31 +0200329
330response_parse_failure_pt:
331 LOGP(DLMGCP, LOGL_ERROR,
332 "Failed to parse SDP parameter payload types (%s)\n", line);
333 return -EINVAL;
334}
335
336/* Parse a line like "m=audio 16002 RTP/AVP 98", extract port and payload types */
337static int mgcp_parse_audio_ptime_rtpmap(struct mgcp_response *r, const char *line)
338{
339 unsigned int pt;
340 char codec_resp[64];
Neels Hofmeyr3ab8ca42019-08-08 04:03:42 +0200341 enum mgcp_codecs codec;
Pau Espin Pedrolc12bfb72019-04-16 17:23:09 +0200342
Neels Hofmeyr23f40482019-08-08 04:03:42 +0200343#define A_PTIME "a=ptime:"
344#define A_RTPMAP "a=rtpmap:"
Pau Espin Pedrolc12bfb72019-04-16 17:23:09 +0200345
Neels Hofmeyr23f40482019-08-08 04:03:42 +0200346 if (osmo_str_startswith(line, A_PTIME)) {
347 if (sscanf(line, A_PTIME "%u", &r->ptime) != 1) {
348 LOGP(DLMGCP, LOGL_ERROR,
349 "Failed to parse SDP parameter, invalid ptime (%s)\n", line);
350 return -EINVAL;
351 }
352 } else if (osmo_str_startswith(line, A_RTPMAP)) {
353 if (sscanf(line, A_RTPMAP "%d %63s", &pt, codec_resp) != 2) {
354 LOGP(DLMGCP, LOGL_ERROR,
355 "Failed to parse SDP parameter, invalid rtpmap: %s\n", osmo_quote_str(line, -1));
356 return -EINVAL;
357 }
Neels Hofmeyr3ab8ca42019-08-08 04:03:42 +0200358 if (r->ptmap_len >= ARRAY_SIZE(r->ptmap)) {
359 LOGP(DLMGCP, LOGL_ERROR, "No more space in ptmap array (len=%u)\n", r->ptmap_len);
360 return -ENOSPC;
Neels Hofmeyr23f40482019-08-08 04:03:42 +0200361 }
Neels Hofmeyr3ab8ca42019-08-08 04:03:42 +0200362 codec = map_str_to_codec(codec_resp);
363 r->ptmap[r->ptmap_len].pt = pt;
364 r->ptmap[r->ptmap_len].codec = codec;
365 r->ptmap_len++;
Philipp Maier704c4f02018-06-07 18:51:31 +0200366 }
Pau Espin Pedrolc12bfb72019-04-16 17:23:09 +0200367
Philipp Maier704c4f02018-06-07 18:51:31 +0200368 return 0;
Philipp Maier06da85e2017-10-05 18:49:24 +0200369}
370
371/* Parse a line like "c=IN IP4 10.11.12.13" */
372static int mgcp_parse_audio_ip(struct mgcp_response *r, const char *line)
373{
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +0200374 struct in6_addr ip_test;
375 bool is_ipv6;
Philipp Maier06da85e2017-10-05 18:49:24 +0200376
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +0200377 if (strncmp("c=IN IP", line, 7) != 0)
Philipp Maier06da85e2017-10-05 18:49:24 +0200378 goto response_parse_failure;
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +0200379 line += 7;
380 if (*line == '6')
381 is_ipv6 = true;
382 else if (*line == '4')
383 is_ipv6 = false;
384 else
Philipp Maier06da85e2017-10-05 18:49:24 +0200385 goto response_parse_failure;
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +0200386 line++;
387 if (*line != ' ')
Philipp Maier06da85e2017-10-05 18:49:24 +0200388 goto response_parse_failure;
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +0200389 line++;
Philipp Maier06da85e2017-10-05 18:49:24 +0200390
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +0200391 /* Extract and check IP-Address */
392 if (is_ipv6) {
393 /* 45 = INET6_ADDRSTRLEN -1 */
394 if (sscanf(line, "%45s", r->audio_ip) != 1)
395 goto response_parse_failure;
396 if (inet_pton(AF_INET6, r->audio_ip, &ip_test) != 1)
397 goto response_parse_failure;
398 } else {
399 /* 15 = INET_ADDRSTRLEN -1 */
400 if (sscanf(line, "%15s", r->audio_ip) != 1)
401 goto response_parse_failure;
402 if (inet_pton(AF_INET, r->audio_ip, &ip_test) != 1)
403 goto response_parse_failure;
404 }
Philipp Maier06da85e2017-10-05 18:49:24 +0200405 return 0;
406
407response_parse_failure:
408 LOGP(DLMGCP, LOGL_ERROR,
409 "Failed to parse MGCP response header (audio ip)\n");
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200410 return -EINVAL;
411}
412
Pau Espin Pedrol91088c32019-04-24 21:02:40 +0200413/*! Extract OSMUX CID from an MGCP parameter line (string).
414 * \param[in] line single parameter line from the MGCP message
415 * \returns OSMUX CID, -1 wildcard, -2 on error
416 * FIXME: This is a copy of function in mgcp_msg.c. Have some common.c file between both libs?
417 */
418static int mgcp_parse_osmux_cid(const char *line)
419{
420 int osmux_cid;
421
422
Pau Espin Pedrolc1bf4692019-05-14 16:23:24 +0200423 if (strcasecmp(line + 2, "Osmux: *") == 0) {
Pau Espin Pedrol91088c32019-04-24 21:02:40 +0200424 LOGP(DLMGCP, LOGL_DEBUG, "Parsed wilcard Osmux CID\n");
425 return -1;
426 }
427
Pau Espin Pedrolc1bf4692019-05-14 16:23:24 +0200428 if (sscanf(line + 2 + 7, "%u", &osmux_cid) != 1) {
Pau Espin Pedrol91088c32019-04-24 21:02:40 +0200429 LOGP(DLMGCP, LOGL_ERROR, "Failed parsing Osmux in MGCP msg line: %s\n",
430 line);
431 return -2;
432 }
433
Pau Espin Pedrol91088c32019-04-24 21:02:40 +0200434 if (osmux_cid > OSMUX_CID_MAX) { /* OSMUX_CID_MAX from libosmo-netif */
435 LOGP(DLMGCP, LOGL_ERROR, "Osmux ID too large: %u > %u\n",
436 osmux_cid, OSMUX_CID_MAX);
437 return -2;
438 }
439 LOGP(DLMGCP, LOGL_DEBUG, "bsc-nat offered Osmux CID %u\n", osmux_cid);
440
441 return osmux_cid;
442}
443
Philipp Maier3b12e1b2018-01-18 15:16:13 +0100444/* A new section is marked by a double line break, check a few more
445 * patterns as there may be variants */
446static char *mgcp_find_section_end(char *string)
447{
448 char *rc;
449
450 rc = strstr(string, "\n\n");
451 if (rc)
452 return rc;
453
454 rc = strstr(string, "\n\r\n\r");
455 if (rc)
456 return rc;
457
458 rc = strstr(string, "\r\n\r\n");
459 if (rc)
460 return rc;
461
462 return NULL;
463}
464
Philipp Maier275ac972018-01-20 00:58:16 +0100465/*! Parse body (SDP) parameters of the MGCP response
466 * \param[in,out] r Response data
467 * \returns 0 on success, -EINVAL on error. */
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200468int mgcp_response_parse_params(struct mgcp_response *r)
469{
470 char *line;
471 int rc;
Neels Hofmeyr0793d2f2018-02-21 14:55:34 +0100472 char *data;
Philipp Maiere9d645b2018-01-19 23:54:08 +0100473 char *data_ptr;
Philipp Maier704c4f02018-06-07 18:51:31 +0200474 int i;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200475
Philipp Maiere9d645b2018-01-19 23:54:08 +0100476 /* Since this functions performs a destructive parsing, we create a
477 * local copy of the body data */
Neels Hofmeyr0793d2f2018-02-21 14:55:34 +0100478 OSMO_ASSERT(r->body);
479 data = talloc_strdup(r, r->body);
Philipp Maiere9d645b2018-01-19 23:54:08 +0100480 OSMO_ASSERT(data);
Philipp Maier55295f72018-01-15 14:00:28 +0100481
Philipp Maiere9d645b2018-01-19 23:54:08 +0100482 /* Find beginning of the parameter (SDP) section */
483 data_ptr = mgcp_find_section_end(data);
Neels Hofmeyr10835332018-02-21 14:55:34 +0100484 if (!data_ptr) {
Neels Hofmeyre8278312019-09-19 03:06:46 +0200485 LOGP(DLMGCP, LOGL_DEBUG, "MGCP response contains no SDP parameters\n");
486 rc = 0;
Philipp Maiere9d645b2018-01-19 23:54:08 +0100487 goto exit;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200488 }
489
Neels Hofmeyr0793d2f2018-02-21 14:55:34 +0100490 /* data_ptr now points to the beginning of the section-end-marker; for_each_non_empty_line()
491 * skips any \r and \n characters for free, so we don't need to skip the marker. */
492
Philipp Maiere9d645b2018-01-19 23:54:08 +0100493 for_each_non_empty_line(line, data_ptr) {
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200494 if (!mgcp_line_is_valid(line))
495 return -EINVAL;
496
497 switch (line[0]) {
Philipp Maier704c4f02018-06-07 18:51:31 +0200498 case 'a':
499 rc = mgcp_parse_audio_ptime_rtpmap(r, line);
500 if (rc)
501 goto exit;
502 break;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200503 case 'm':
Philipp Maier704c4f02018-06-07 18:51:31 +0200504 rc = mgcp_parse_audio_port_pt(r, line);
Philipp Maier06da85e2017-10-05 18:49:24 +0200505 if (rc)
Philipp Maiere9d645b2018-01-19 23:54:08 +0100506 goto exit;
Philipp Maier06da85e2017-10-05 18:49:24 +0200507 break;
508 case 'c':
509 rc = mgcp_parse_audio_ip(r, line);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200510 if (rc)
Philipp Maiere9d645b2018-01-19 23:54:08 +0100511 goto exit;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200512 break;
513 default:
514 /* skip unhandled parameters */
515 break;
516 }
517 }
Philipp Maiere9d645b2018-01-19 23:54:08 +0100518
Philipp Maier704c4f02018-06-07 18:51:31 +0200519 /* See also note in mgcp_parse_audio_port_pt() */
520 for (i = 0; i < r->codecs_len; i++)
521 r->codecs[i] = map_pt_to_codec(r->ptmap, r->ptmap_len, r->codecs[i]);
522
Philipp Maiere9d645b2018-01-19 23:54:08 +0100523 rc = 0;
524exit:
525 talloc_free(data);
526 return rc;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200527}
528
Philipp Maier55295f72018-01-15 14:00:28 +0100529/* Parse a line like "X: something" */
530static int mgcp_parse_head_param(char *result, unsigned int result_len,
531 char label, const char *line)
Philipp Maierffd75e42017-11-22 11:44:50 +0100532{
Philipp Maier55295f72018-01-15 14:00:28 +0100533 char label_string[4];
Neels Hofmeyr23e7bf12018-09-03 21:26:22 +0200534 size_t rc;
Philipp Maier55295f72018-01-15 14:00:28 +0100535
536 /* Detect empty parameters */
Philipp Maierffd75e42017-11-22 11:44:50 +0100537 if (strlen(line) < 4)
538 goto response_parse_failure;
539
Philipp Maier55295f72018-01-15 14:00:28 +0100540 /* Check if the label matches */
541 snprintf(label_string, sizeof(label_string), "%c: ", label);
542 if (memcmp(label_string, line, 3) != 0)
Philipp Maierffd75e42017-11-22 11:44:50 +0100543 goto response_parse_failure;
544
Philipp Maier55295f72018-01-15 14:00:28 +0100545 /* Copy payload part of the string to destinations (the label string
546 * is always 3 chars long) */
Neels Hofmeyr23e7bf12018-09-03 21:26:22 +0200547 rc = osmo_strlcpy(result, line + 3, result_len);
548 if (rc >= result_len) {
549 LOGP(DLMGCP, LOGL_ERROR,
550 "Failed to parse MGCP response (parameter label: %c):"
551 " the received conn ID is too long: %zu, maximum is %u characters\n",
552 label, rc, result_len - 1);
553 return -ENOSPC;
554 }
Philipp Maierffd75e42017-11-22 11:44:50 +0100555 return 0;
556
557response_parse_failure:
558 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier55295f72018-01-15 14:00:28 +0100559 "Failed to parse MGCP response (parameter label: %c)\n", label);
Philipp Maierffd75e42017-11-22 11:44:50 +0100560 return -EINVAL;
561}
562
563/* Parse MGCP parameters of the response */
564static int parse_head_params(struct mgcp_response *r)
565{
566 char *line;
567 int rc = 0;
568 OSMO_ASSERT(r->body);
Philipp Maier55295f72018-01-15 14:00:28 +0100569 char *data;
570 char *data_ptr;
571 char *data_end;
Philipp Maierffd75e42017-11-22 11:44:50 +0100572
Philipp Maier55295f72018-01-15 14:00:28 +0100573 /* Since this functions performs a destructive parsing, we create a
574 * local copy of the body data */
Philipp Maier1fc69992018-01-31 15:32:55 +0100575 data = talloc_zero_size(r, strlen(r->body)+1);
Philipp Maier55295f72018-01-15 14:00:28 +0100576 OSMO_ASSERT(data);
577 data_ptr = data;
578 osmo_strlcpy(data, r->body, strlen(r->body));
579
580 /* If there is an SDP body attached, prevent for_each_non_empty_line()
581 * into running in there, we are not yet interested in the parameters
582 * stored there. */
Pau Espin Pedrolc12bfb72019-04-16 17:23:09 +0200583 data_end = mgcp_find_section_end(data);
Philipp Maierffd75e42017-11-22 11:44:50 +0100584 if (data_end)
585 *data_end = '\0';
586
Philipp Maier55295f72018-01-15 14:00:28 +0100587 for_each_non_empty_line(line, data_ptr) {
Pau Espin Pedrol166077e2019-06-26 12:21:38 +0200588 switch (toupper(line[0])) {
Philipp Maier55295f72018-01-15 14:00:28 +0100589 case 'Z':
590 rc = mgcp_parse_head_param(r->head.endpoint,
591 sizeof(r->head.endpoint),
592 'Z', line);
593 if (rc)
594 goto exit;
Philipp Maier771b26a2018-01-31 13:53:11 +0100595
596 /* A specific endpoint identifier returned by the MGW
597 * must not contain any wildcard characters */
598 if (strstr(r->head.endpoint, "*") != NULL) {
599 rc = -EINVAL;
600 goto exit;
601 }
Philipp Maier3261dc72018-01-31 14:03:13 +0100602
603 /* A specific endpoint identifier returned by the MGW
604 * must contain an @ character */
605 if (strstr(r->head.endpoint, "@") == NULL) {
606 rc = -EINVAL;
607 goto exit;
608 }
Philipp Maier55295f72018-01-15 14:00:28 +0100609 break;
Philipp Maierffd75e42017-11-22 11:44:50 +0100610 case 'I':
Philipp Maier55295f72018-01-15 14:00:28 +0100611 rc = mgcp_parse_head_param(r->head.conn_id,
612 sizeof(r->head.conn_id),
613 'I', line);
Philipp Maierffd75e42017-11-22 11:44:50 +0100614 if (rc)
615 goto exit;
616 break;
Pau Espin Pedrol91088c32019-04-24 21:02:40 +0200617 case 'X':
Pau Espin Pedrolc1bf4692019-05-14 16:23:24 +0200618 if (strncasecmp("Osmux: ", line + 2, strlen("Osmux: ")) == 0) {
Pau Espin Pedrol91088c32019-04-24 21:02:40 +0200619 rc = mgcp_parse_osmux_cid(line);
620 if (rc < 0) {
621 /* -1: we don't want wildcards in response. -2: error */
622 rc = -EINVAL;
623 goto exit;
624 }
625 r->head.x_osmo_osmux_use = true;
626 r->head.x_osmo_osmux_cid = (uint8_t) rc;
627 rc = 0;
628 break;
629 }
630 /* Ignore unknown X-headers */
631 break;
Philipp Maierffd75e42017-11-22 11:44:50 +0100632 default:
633 /* skip unhandled parameters */
634 break;
635 }
636 }
637exit:
Philipp Maier55295f72018-01-15 14:00:28 +0100638 talloc_free(data);
Philipp Maierffd75e42017-11-22 11:44:50 +0100639 return rc;
640}
641
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200642static struct mgcp_response_pending *mgcp_client_response_pending_get(
643 struct mgcp_client *mgcp,
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100644 mgcp_trans_id_t trans_id)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200645{
646 struct mgcp_response_pending *pending;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200647 llist_for_each_entry(pending, &mgcp->responses_pending, entry) {
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100648 if (pending->trans_id == trans_id) {
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200649 llist_del(&pending->entry);
650 return pending;
651 }
652 }
653 return NULL;
654}
655
656/* Feed an MGCP message into the receive processing.
657 * Parse the head and call any callback registered for the transaction id found
658 * in the MGCP message. This is normally called directly from the internal
659 * mgcp_do_read that reads from the socket connected to the MGCP gateway. This
660 * function is published mainly to be able to feed data from the test suite.
661 */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200662int mgcp_client_rx(struct mgcp_client *mgcp, struct msgb *msg)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200663{
Philipp Maier1fc69992018-01-31 15:32:55 +0100664 struct mgcp_response *r;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200665 struct mgcp_response_pending *pending;
666 int rc;
667
Philipp Maier1fc69992018-01-31 15:32:55 +0100668 r = talloc_zero(mgcp, struct mgcp_response);
669 OSMO_ASSERT(r);
670
671 rc = mgcp_response_parse_head(r, msg);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200672 if (rc) {
Philipp Maierffd75e42017-11-22 11:44:50 +0100673 LOGP(DLMGCP, LOGL_ERROR, "Cannot parse MGCP response (head)\n");
Philipp Maier1fc69992018-01-31 15:32:55 +0100674 rc = 1;
675 goto error;
Philipp Maierffd75e42017-11-22 11:44:50 +0100676 }
677
Philipp Maier1fc69992018-01-31 15:32:55 +0100678 rc = parse_head_params(r);
Philipp Maierffd75e42017-11-22 11:44:50 +0100679 if (rc) {
680 LOGP(DLMGCP, LOGL_ERROR, "Cannot parse MGCP response (head parameters)\n");
Philipp Maier1fc69992018-01-31 15:32:55 +0100681 rc = 1;
682 goto error;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200683 }
684
Philipp Maier1fc69992018-01-31 15:32:55 +0100685 pending = mgcp_client_response_pending_get(mgcp, r->head.trans_id);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200686 if (!pending) {
687 LOGP(DLMGCP, LOGL_ERROR,
688 "Cannot find matching MGCP transaction for trans_id %d\n",
Philipp Maier1fc69992018-01-31 15:32:55 +0100689 r->head.trans_id);
690 rc = -ENOENT;
691 goto error;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200692 }
693
Philipp Maier1fc69992018-01-31 15:32:55 +0100694 mgcp_client_handle_response(mgcp, pending, r);
695 rc = 0;
696
697error:
698 talloc_free(r);
699 return rc;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200700}
701
702static int mgcp_do_read(struct osmo_fd *fd)
703{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200704 struct mgcp_client *mgcp = fd->data;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200705 struct msgb *msg;
706 int ret;
707
708 msg = msgb_alloc_headroom(4096, 128, "mgcp_from_gw");
709 if (!msg) {
710 LOGP(DLMGCP, LOGL_ERROR, "Failed to allocate MGCP message.\n");
711 return -1;
712 }
713
714 ret = read(fd->fd, msg->data, 4096 - 128);
715 if (ret <= 0) {
Neels Hofmeyr0a403792018-12-12 03:10:18 +0100716 LOGP(DLMGCP, LOGL_ERROR, "Failed to read: %s: %d='%s'\n", osmo_sock_get_name2(fd->fd),
717 errno, strerror(errno));
718
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200719 msgb_free(msg);
720 return -1;
721 } else if (ret > 4096 - 128) {
Neels Hofmeyr0a403792018-12-12 03:10:18 +0100722 LOGP(DLMGCP, LOGL_ERROR, "Too much data: %s: %d\n", osmo_sock_get_name2(fd->fd), ret);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200723 msgb_free(msg);
724 return -1;
Harald Welte9bf7c532017-11-17 14:14:31 +0100725 }
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200726
727 msg->l2h = msgb_put(msg, ret);
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200728 ret = mgcp_client_rx(mgcp, msg);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200729 talloc_free(msg);
730 return ret;
731}
732
733static int mgcp_do_write(struct osmo_fd *fd, struct msgb *msg)
734{
735 int ret;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200736
Neels Hofmeyr0a403792018-12-12 03:10:18 +0100737 LOGP(DLMGCP, LOGL_DEBUG, "Tx MGCP: %s: len=%u '%s'...\n",
738 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 +0200739
740 ret = write(fd->fd, msg->data, msg->len);
741 if (ret != msg->len)
Neels Hofmeyr0a403792018-12-12 03:10:18 +0100742 LOGP(DLMGCP, LOGL_ERROR, "Failed to Tx MGCP: %s: %d='%s'; msg: len=%u '%s'...\n",
743 osmo_sock_get_name2(fd->fd), errno, strerror(errno),
Neels Hofmeyr32d15cc2018-12-12 03:05:33 +0100744 msg->len, osmo_escape_str((const char*)msg->data, OSMO_MIN(42, msg->len)));
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200745 return ret;
746}
747
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200748struct mgcp_client *mgcp_client_init(void *ctx,
749 struct mgcp_client_conf *conf)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200750{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200751 struct mgcp_client *mgcp;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200752
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200753 mgcp = talloc_zero(ctx, struct mgcp_client);
Harald Welteefe15712020-07-15 10:56:45 +0200754 if (!mgcp)
755 return NULL;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200756
757 INIT_LLIST_HEAD(&mgcp->responses_pending);
758 INIT_LLIST_HEAD(&mgcp->inuse_endpoints);
759
760 mgcp->next_trans_id = 1;
761
762 mgcp->actual.local_addr = conf->local_addr ? conf->local_addr :
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200763 MGCP_CLIENT_LOCAL_ADDR_DEFAULT;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200764 mgcp->actual.local_port = conf->local_port >= 0 ? (uint16_t)conf->local_port :
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200765 MGCP_CLIENT_LOCAL_PORT_DEFAULT;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200766
767 mgcp->actual.remote_addr = conf->remote_addr ? conf->remote_addr :
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200768 MGCP_CLIENT_REMOTE_ADDR_DEFAULT;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200769 mgcp->actual.remote_port = conf->remote_port >= 0 ? (uint16_t)conf->remote_port :
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200770 MGCP_CLIENT_REMOTE_PORT_DEFAULT;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200771
Neels Hofmeyrac69ea92018-12-19 00:27:50 +0100772 if (osmo_strlcpy(mgcp->actual.endpoint_domain_name, conf->endpoint_domain_name,
773 sizeof(mgcp->actual.endpoint_domain_name))
774 >= sizeof(mgcp->actual.endpoint_domain_name)) {
775 LOGP(DLMGCP, LOGL_ERROR, "MGCP client: endpoint domain name is too long, max length is %zu: '%s'\n",
776 sizeof(mgcp->actual.endpoint_domain_name) - 1, conf->endpoint_domain_name);
777 talloc_free(mgcp);
778 return NULL;
779 }
780 LOGP(DLMGCP, LOGL_NOTICE, "MGCP client: using endpoint domain '@%s'\n", mgcp_client_endpoint_domain(mgcp));
781
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200782 return mgcp;
783}
784
Philipp Maier6a26c162018-08-01 16:37:06 +0200785static int init_socket(struct mgcp_client *mgcp)
786{
787 int rc;
788 struct osmo_wqueue *wq;
789 int i;
790
791 wq = &mgcp->wq;
792
793 for (i = 0; i < 100; i++) {
794
795 /* Initalize socket with the currently configured port
796 * number */
Pau Espin Pedrol531470a2020-08-31 17:06:55 +0200797 rc = osmo_sock_init2_ofd(&wq->bfd, AF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, mgcp->actual.local_addr,
Philipp Maier6a26c162018-08-01 16:37:06 +0200798 mgcp->actual.local_port, mgcp->actual.remote_addr, mgcp->actual.remote_port,
799 OSMO_SOCK_F_BIND | OSMO_SOCK_F_CONNECT);
800 if (rc > 0)
801 return rc;
802
803 /* If there is a different port than the default port
804 * configured then we assume that the user has choosen
805 * that port conciously and we will not try to resolve
806 * this by silently choosing a different port. */
Philipp Maier9a7ccc32018-08-09 11:41:19 +0200807 if (mgcp->actual.local_port != MGCP_CLIENT_LOCAL_PORT_DEFAULT && i == 0)
Philipp Maier6a26c162018-08-01 16:37:06 +0200808 return -EINVAL;
809
810 /* Choose a new port number to try next */
811 LOGP(DLMGCP, LOGL_NOTICE,
Neels Hofmeyr0a403792018-12-12 03:10:18 +0100812 "MGCPGW failed to bind to %s:%u, retrying with port %u\n",
813 mgcp->actual.local_addr, mgcp->actual.local_port, mgcp->actual.local_port + 1);
Philipp Maier6a26c162018-08-01 16:37:06 +0200814 mgcp->actual.local_port++;
815 }
816
Neels Hofmeyr0a403792018-12-12 03:10:18 +0100817 LOGP(DLMGCP, LOGL_FATAL, "MGCPGW failed to find a port to bind on %i times.\n", i);
Philipp Maier6a26c162018-08-01 16:37:06 +0200818 return -EINVAL;
819}
820
Philipp Maier275ac972018-01-20 00:58:16 +0100821/*! Initalize client connection (opens socket only, no request is sent yet)
822 * \param[in,out] mgcp MGCP client descriptor.
823 * \returns 0 on success, -EINVAL on error. */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200824int mgcp_client_connect(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200825{
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200826 struct osmo_wqueue *wq;
827 int rc;
828
829 if (!mgcp) {
830 LOGP(DLMGCP, LOGL_FATAL, "MGCPGW client not initialized properly\n");
831 return -EINVAL;
832 }
833
834 wq = &mgcp->wq;
Harald Weltec2a8f592020-10-19 13:25:41 +0200835 osmo_wqueue_init(wq, 1024);
836 wq->read_cb = mgcp_do_read;
837 wq->write_cb = mgcp_do_write;
838
839 osmo_fd_setup(&wq->bfd, -1, OSMO_FD_READ, osmo_wqueue_bfd_cb, mgcp, 0);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200840
Philipp Maier6a26c162018-08-01 16:37:06 +0200841 rc = init_socket(mgcp);
Harald Welte8890dfa2017-11-17 15:09:30 +0100842 if (rc < 0) {
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200843 LOGP(DLMGCP, LOGL_FATAL,
Harald Welte8890dfa2017-11-17 15:09:30 +0100844 "Failed to initialize socket %s:%u -> %s:%u for MGCP GW: %s\n",
845 mgcp->actual.local_addr, mgcp->actual.local_port,
846 mgcp->actual.remote_addr, mgcp->actual.remote_port, strerror(errno));
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200847 goto error_close_fd;
848 }
849
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200850
Neels Hofmeyr0a403792018-12-12 03:10:18 +0100851 LOGP(DLMGCP, LOGL_INFO, "MGCP GW connection: %s\n", osmo_sock_get_name2(wq->bfd.fd));
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200852
853 return 0;
854error_close_fd:
855 close(wq->bfd.fd);
856 wq->bfd.fd = -1;
857 return rc;
858}
859
Philipp Maier275ac972018-01-20 00:58:16 +0100860/*! Get the IP-Aaddress of the associated MGW as string.
861 * \param[in] mgcp MGCP client descriptor.
862 * \returns a pointer to the address string. */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200863const char *mgcp_client_remote_addr_str(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200864{
865 return mgcp->actual.remote_addr;
866}
867
Philipp Maier275ac972018-01-20 00:58:16 +0100868/*! Get the IP-Port of the associated MGW.
869 * \param[in] mgcp MGCP client descriptor.
870 * \returns port number. */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200871uint16_t mgcp_client_remote_port(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200872{
873 return mgcp->actual.remote_port;
874}
875
Pau Espin Pedrol74d0e5c2020-09-02 17:19:20 +0200876/*! Get the IP-Address of the associated MGW as its numeric representation.
877 * DEPRECATED, DON'T USE.
Philipp Maier275ac972018-01-20 00:58:16 +0100878 * \param[in] mgcp MGCP client descriptor.
879 * \returns IP-Address as 32 bit integer (network byte order) */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200880uint32_t mgcp_client_remote_addr_n(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200881{
Pau Espin Pedrol74d0e5c2020-09-02 17:19:20 +0200882 return 0;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200883}
884
Neels Hofmeyrac69ea92018-12-19 00:27:50 +0100885/* To compose endpoint names, usually for CRCX, use this as domain name.
886 * For example, snprintf("rtpbridge\*@%s", mgcp_client_endpoint_domain(mgcp)). */
887const char *mgcp_client_endpoint_domain(const struct mgcp_client *mgcp)
888{
889 return mgcp->actual.endpoint_domain_name[0] ? mgcp->actual.endpoint_domain_name : "mgw";
890}
891
Pau Espin Pedrolca0818c2019-04-16 17:23:38 +0200892static const char *_mgcp_client_name_append_domain(const struct mgcp_client *mgcp, char *name)
Neels Hofmeyrac69ea92018-12-19 00:27:50 +0100893{
894 static char endpoint[MGCP_ENDPOINT_MAXLEN];
895 int rc;
896
Pau Espin Pedrolca0818c2019-04-16 17:23:38 +0200897 rc = snprintf(endpoint, sizeof(endpoint), "%s@%s", name, mgcp_client_endpoint_domain(mgcp));
Neels Hofmeyrac69ea92018-12-19 00:27:50 +0100898 if (rc > sizeof(endpoint) - 1) {
Pau Espin Pedrolca0818c2019-04-16 17:23:38 +0200899 LOGP(DLMGCP, LOGL_ERROR, "MGCP endpoint exceeds maximum length of %zu: '%s@%s'\n",
900 sizeof(endpoint) - 1, name, mgcp_client_endpoint_domain(mgcp));
Neels Hofmeyrac69ea92018-12-19 00:27:50 +0100901 return NULL;
902 }
903 if (rc < 1) {
904 LOGP(DLMGCP, LOGL_ERROR, "Cannot compose MGCP endpoint name\n");
905 return NULL;
906 }
907 return endpoint;
908}
909
Philipp Maiera466f572020-06-25 16:11:04 +0200910/*! Compose endpoint name for a wildcarded request to the virtual trunk
911 * \param[in] mgcp MGCP client descriptor.
912 * \returns string containing the endpoint name (e.g. rtpbridge\*@mgw) */
Pau Espin Pedrolca0818c2019-04-16 17:23:38 +0200913const char *mgcp_client_rtpbridge_wildcard(const struct mgcp_client *mgcp)
914{
915 return _mgcp_client_name_append_domain(mgcp, "rtpbridge/*");
916}
917
Philipp Maier48635912020-06-25 20:16:22 +0200918/*! Compose endpoint name for an E1 endpoint.
919 * \param[in] ctx talloc context.
920 * \param[in] mgcp MGCP client descriptor.
921 * \param[in] trunk_id id number of the E1 trunk (1-64).
922 * \param[in] ts timeslot on the E1 trunk (1-31).
923 * \param[in] rate bitrate used on the E1 trunk (e.g 16 for 16kbit).
924 * \param[in] offset bit offset of the E1 subslot (e.g. 4 for the third 16k subslot).
925 * \returns string containing the endpoint name (e.g. ds/e1-1/s-1/su16-4). */
926const char *mgcp_client_e1_epname(void *ctx, const struct mgcp_client *mgcp, uint8_t trunk_id, uint8_t ts,
927 uint8_t rate, uint8_t offset)
928{
929 /* See also comment in libosmo-mgcp, mgcp_client.c, gen_e1_epname() */
930 const uint8_t valid_rates[] = { 64, 32, 32, 16, 16, 16, 16, 8, 8, 8, 8, 8, 8, 8, 8 };
931 const uint8_t valid_offsets[] = { 0, 0, 4, 0, 2, 4, 6, 0, 1, 2, 3, 4, 5, 6, 7 };
932
933 uint8_t i;
934 bool rate_offs_valid = false;
935 char *epname;
936
937 epname =
938 talloc_asprintf(ctx, "ds/e1-%u/s-%u/su%u-%u@%s", trunk_id, ts, rate, offset,
939 mgcp_client_endpoint_domain(mgcp));
940 if (!epname) {
941 LOGP(DLMGCP, LOGL_ERROR, "Cannot compose MGCP e1-endpoint name!\n");
942 return NULL;
943 }
944
945 /* Check if the supplied rate/offset pair resembles a valid combination */
946 for (i = 0; i < sizeof(valid_rates); i++) {
947 if (valid_rates[i] == rate && valid_offsets[i] == offset)
948 rate_offs_valid = true;
949 }
950 if (!rate_offs_valid) {
951 LOGP(DLMGCP, LOGL_ERROR,
952 "Cannot compose MGCP e1-endpoint name (%s), rate(%u)/offset(%u) combination is invalid!\n", epname,
953 rate, offset);
954 talloc_free(epname);
955 return NULL;
956 }
957
958 /* An E1 line has a maximum of 32 timeslots, while the first (ts=0) is
959 * reserverd for framing and alignment, so we can not use it here. */
960 if (ts == 0 || ts > 31) {
961 LOGP(DLMGCP, LOGL_ERROR,
962 "Cannot compose MGCP e1-endpoint name (%s), E1-timeslot number (%u) is invalid!\n", epname, ts);
963 talloc_free(epname);
964 return NULL;
965 }
966
967 return epname;
968}
969
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200970struct mgcp_response_pending * mgcp_client_pending_add(
971 struct mgcp_client *mgcp,
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200972 mgcp_trans_id_t trans_id,
973 mgcp_response_cb_t response_cb,
974 void *priv)
975{
976 struct mgcp_response_pending *pending;
977
978 pending = talloc_zero(mgcp, struct mgcp_response_pending);
Harald Welte827da2c2020-07-15 10:57:08 +0200979 if (!pending)
980 return NULL;
981
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200982 pending->trans_id = trans_id;
983 pending->response_cb = response_cb;
984 pending->priv = priv;
985 llist_add_tail(&pending->entry, &mgcp->responses_pending);
986
987 return pending;
988}
989
990/* Send the MGCP message in msg to the MGCP GW and handle a response with
991 * response_cb. NOTE: the response_cb still needs to call
992 * mgcp_response_parse_params(response) to get the parsed parameters -- to
993 * potentially save some CPU cycles, only the head line has been parsed when
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100994 * the response_cb is invoked.
995 * Before the priv pointer becomes invalid, e.g. due to transaction timeout,
996 * mgcp_client_cancel() needs to be called for this transaction.
997 */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200998int mgcp_client_tx(struct mgcp_client *mgcp, struct msgb *msg,
999 mgcp_response_cb_t response_cb, void *priv)
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001000{
Harald Welte563ffc52020-06-17 21:54:13 +07001001 struct mgcp_response_pending *pending = NULL;
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001002 mgcp_trans_id_t trans_id;
1003 int rc;
1004
1005 trans_id = msg->cb[MSGB_CB_MGCP_TRANS_ID];
1006 if (!trans_id) {
1007 LOGP(DLMGCP, LOGL_ERROR,
1008 "Unset transaction id in mgcp send request\n");
1009 talloc_free(msg);
1010 return -EINVAL;
1011 }
1012
Harald Welte563ffc52020-06-17 21:54:13 +07001013 /* Do not allocate a dummy 'mgcp_response_pending' entry */
1014 if (response_cb != NULL) {
1015 pending = mgcp_client_pending_add(mgcp, trans_id, response_cb, priv);
1016 if (!pending) {
1017 talloc_free(msg);
1018 return -ENOMEM;
1019 }
Harald Welte827da2c2020-07-15 10:57:08 +02001020 }
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001021
1022 if (msgb_l2len(msg) > 4096) {
1023 LOGP(DLMGCP, LOGL_ERROR,
1024 "Cannot send, MGCP message too large: %u\n",
1025 msgb_l2len(msg));
1026 msgb_free(msg);
1027 rc = -EINVAL;
1028 goto mgcp_tx_error;
1029 }
1030
1031 rc = osmo_wqueue_enqueue(&mgcp->wq, msg);
1032 if (rc) {
1033 LOGP(DLMGCP, LOGL_FATAL, "Could not queue message to MGCP GW\n");
1034 msgb_free(msg);
1035 goto mgcp_tx_error;
1036 } else
Neels Hofmeyred0c1aa2018-12-21 03:07:53 +01001037 LOGP(DLMGCP, LOGL_DEBUG, "Queued %u bytes for MGCP GW\n",
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001038 msgb_l2len(msg));
1039 return 0;
1040
1041mgcp_tx_error:
Harald Welte563ffc52020-06-17 21:54:13 +07001042 if (!pending)
Vadim Yanitskiyffbc6182020-07-16 15:48:13 +07001043 return rc;
Vadim Yanitskiy3f8139c2020-06-17 20:50:17 +07001044 /* Dequeue pending response, it's going to be free()d */
1045 llist_del(&pending->entry);
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001046 /* Pass NULL to response cb to indicate an error */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +02001047 mgcp_client_handle_response(mgcp, pending, NULL);
Vadim Yanitskiyffbc6182020-07-16 15:48:13 +07001048 return rc;
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001049}
1050
Philipp Maier275ac972018-01-20 00:58:16 +01001051/*! Cancel a pending transaction.
1052 * \param[in] mgcp MGCP client descriptor.
1053 * \param[in,out] trans_id Transaction id.
1054 * \returns 0 on success, -ENOENT on error.
1055 *
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +01001056 * Should a priv pointer passed to mgcp_client_tx() become invalid, this function must be called. In
1057 * practical terms, if the caller of mgcp_client_tx() wishes to tear down a transaction without having
1058 * received a response this function must be called. The trans_id can be obtained by calling
Philipp Maier275ac972018-01-20 00:58:16 +01001059 * mgcp_msg_trans_id() on the msgb produced by mgcp_msg_gen(). */
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +01001060int mgcp_client_cancel(struct mgcp_client *mgcp, mgcp_trans_id_t trans_id)
1061{
1062 struct mgcp_response_pending *pending = mgcp_client_response_pending_get(mgcp, trans_id);
1063 if (!pending) {
Philipp Maier275ac972018-01-20 00:58:16 +01001064 /*! Note: it is not harmful to cancel a transaction twice. */
Neels Hofmeyred0c1aa2018-12-21 03:07:53 +01001065 LOGP(DLMGCP, LOGL_ERROR, "Cannot cancel, no such transaction: %u\n", trans_id);
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +01001066 return -ENOENT;
1067 }
Neels Hofmeyred0c1aa2018-12-21 03:07:53 +01001068 LOGP(DLMGCP, LOGL_DEBUG, "Canceled transaction %u\n", trans_id);
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +01001069 talloc_free(pending);
1070 return 0;
Philipp Maier275ac972018-01-20 00:58:16 +01001071 /*! We don't really need to clean up the wqueue: In all sane cases, the msgb has already been sent
1072 * out and is no longer in the wqueue. If it still is in the wqueue, then sending MGCP messages
1073 * per se is broken and the program should notice so by a full wqueue. Even if this was called
1074 * before we had a chance to send out the message and it is still going to be sent, we will just
1075 * ignore the reply to it later. Removing a msgb from the wqueue here would just introduce more
1076 * bug surface in terms of failing to update wqueue API's counters or some such.
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +01001077 */
1078}
1079
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +02001080static mgcp_trans_id_t mgcp_client_next_trans_id(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001081{
1082 /* avoid zero trans_id to distinguish from unset trans_id */
1083 if (!mgcp->next_trans_id)
1084 mgcp->next_trans_id ++;
1085 return mgcp->next_trans_id ++;
1086}
1087
Philipp Maier1dc6be62017-10-05 18:25:37 +02001088#define MGCP_CRCX_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT | \
1089 MGCP_MSG_PRESENCE_CALL_ID | \
Philipp Maier1dc6be62017-10-05 18:25:37 +02001090 MGCP_MSG_PRESENCE_CONN_MODE)
1091#define MGCP_MDCX_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT | \
Philipp Maier490cbaa2018-01-22 17:32:38 +01001092 MGCP_MSG_PRESENCE_CALL_ID | \
Philipp Maier1dc6be62017-10-05 18:25:37 +02001093 MGCP_MSG_PRESENCE_CONN_ID)
1094#define MGCP_DLCX_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT)
1095#define MGCP_AUEP_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT)
1096#define MGCP_RSIP_MANDATORY 0 /* none */
1097
Philipp Maier704c4f02018-06-07 18:51:31 +02001098/* Helper function for mgcp_msg_gen(): Add LCO information to MGCP message */
1099static int add_lco(struct msgb *msg, struct mgcp_msg *mgcp_msg)
1100{
1101 unsigned int i;
1102 int rc = 0;
1103 const char *codec;
1104 unsigned int pt;
1105
1106 rc += msgb_printf(msg, "L:");
1107
1108 if (mgcp_msg->ptime)
1109 rc += msgb_printf(msg, " p:%u,", mgcp_msg->ptime);
1110
1111 if (mgcp_msg->codecs_len) {
1112 rc += msgb_printf(msg, " a:");
1113 for (i = 0; i < mgcp_msg->codecs_len; i++) {
1114 pt = mgcp_msg->codecs[i];
Neels Hofmeyr47642f22019-02-27 05:56:53 +01001115 codec = get_value_string_or_null(osmo_mgcpc_codec_names, pt);
Pau Espin Pedrolc12bfb72019-04-16 17:23:09 +02001116
Philipp Maier704c4f02018-06-07 18:51:31 +02001117 /* Note: Use codec descriptors from enum mgcp_codecs
1118 * in mgcp_client only! */
1119 OSMO_ASSERT(codec);
1120 rc += msgb_printf(msg, "%s", extract_codec_name(codec));
1121 if (i < mgcp_msg->codecs_len - 1)
1122 rc += msgb_printf(msg, ";");
1123 }
1124 rc += msgb_printf(msg, ",");
1125 }
1126
1127 rc += msgb_printf(msg, " nt:IN\r\n");
1128
1129 return rc;
1130}
1131
1132/* Helper function for mgcp_msg_gen(): Add SDP information to MGCP message */
1133static int add_sdp(struct msgb *msg, struct mgcp_msg *mgcp_msg, struct mgcp_client *mgcp)
1134{
1135 unsigned int i;
1136 int rc = 0;
Pau Espin Pedrol8667d512020-08-28 19:37:08 +02001137 char local_ip[INET6_ADDRSTRLEN];
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +02001138 int local_ip_family, audio_ip_family;
Philipp Maier704c4f02018-06-07 18:51:31 +02001139 const char *codec;
1140 unsigned int pt;
1141
1142 /* Add separator to mark the beginning of the SDP block */
1143 rc += msgb_printf(msg, "\r\n");
1144
1145 /* Add SDP protocol version */
1146 rc += msgb_printf(msg, "v=0\r\n");
1147
1148 /* Determine local IP-Address */
1149 if (osmo_sock_local_ip(local_ip, mgcp->actual.remote_addr) < 0) {
1150 LOGP(DLMGCP, LOGL_ERROR,
1151 "Could not determine local IP-Address!\n");
1152 msgb_free(msg);
1153 return -2;
1154 }
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +02001155 local_ip_family = osmo_ip_str_type(local_ip);
1156 if (local_ip_family == AF_UNSPEC) {
1157 msgb_free(msg);
1158 return -2;
1159 }
1160 audio_ip_family = osmo_ip_str_type(mgcp_msg->audio_ip);
1161 if (audio_ip_family == AF_UNSPEC) {
1162 msgb_free(msg);
1163 return -2;
1164 }
Philipp Maier704c4f02018-06-07 18:51:31 +02001165
1166 /* Add owner/creator (SDP) */
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +02001167 rc += msgb_printf(msg, "o=- %x 23 IN IP%c %s\r\n", mgcp_msg->call_id,
1168 local_ip_family == AF_INET6 ? '6' : '4',
1169 local_ip);
Philipp Maier704c4f02018-06-07 18:51:31 +02001170
1171 /* Add session name (none) */
1172 rc += msgb_printf(msg, "s=-\r\n");
1173
1174 /* Add RTP address and port */
1175 if (mgcp_msg->audio_port == 0) {
1176 LOGP(DLMGCP, LOGL_ERROR,
1177 "Invalid port number, can not generate MGCP message\n");
1178 msgb_free(msg);
1179 return -2;
1180 }
1181 if (strlen(mgcp_msg->audio_ip) <= 0) {
1182 LOGP(DLMGCP, LOGL_ERROR,
1183 "Empty ip address, can not generate MGCP message\n");
1184 msgb_free(msg);
1185 return -2;
1186 }
Pau Espin Pedrol9dc73592020-08-28 20:21:55 +02001187 rc += msgb_printf(msg, "c=IN IP%c %s\r\n",
1188 audio_ip_family == AF_INET6 ? '6' : '4',
1189 mgcp_msg->audio_ip);
Philipp Maier704c4f02018-06-07 18:51:31 +02001190
1191 /* Add time description, active time (SDP) */
1192 rc += msgb_printf(msg, "t=0 0\r\n");
1193
1194 rc += msgb_printf(msg, "m=audio %u RTP/AVP", mgcp_msg->audio_port);
1195 for (i = 0; i < mgcp_msg->codecs_len; i++) {
1196 pt = map_codec_to_pt(mgcp_msg->ptmap, mgcp_msg->ptmap_len, mgcp_msg->codecs[i]);
1197 rc += msgb_printf(msg, " %u", pt);
1198
1199 }
1200 rc += msgb_printf(msg, "\r\n");
1201
Philipp Maier228e5912019-03-05 13:56:59 +01001202 /* Add optional codec parameters (fmtp) */
1203 if (mgcp_msg->param_present) {
1204 for (i = 0; i < mgcp_msg->codecs_len; i++) {
1205 /* The following is only applicable for AMR */
1206 if (mgcp_msg->codecs[i] != CODEC_AMR_8000_1 && mgcp_msg->codecs[i] != CODEC_AMRWB_16000_1)
1207 continue;
1208 pt = map_codec_to_pt(mgcp_msg->ptmap, mgcp_msg->ptmap_len, mgcp_msg->codecs[i]);
1209 if (mgcp_msg->param.amr_octet_aligned_present && mgcp_msg->param.amr_octet_aligned)
1210 rc += msgb_printf(msg, "a=fmtp:%u octet-align=1\r\n", pt);
1211 else if (mgcp_msg->param.amr_octet_aligned_present && !mgcp_msg->param.amr_octet_aligned)
1212 rc += msgb_printf(msg, "a=fmtp:%u octet-align=0\r\n", pt);
1213 }
1214 }
1215
Philipp Maier704c4f02018-06-07 18:51:31 +02001216 for (i = 0; i < mgcp_msg->codecs_len; i++) {
1217 pt = map_codec_to_pt(mgcp_msg->ptmap, mgcp_msg->ptmap_len, mgcp_msg->codecs[i]);
Pau Espin Pedrolc12bfb72019-04-16 17:23:09 +02001218
Philipp Maier704c4f02018-06-07 18:51:31 +02001219 /* Note: Only dynamic payload type from the range 96-127
1220 * require to be explained further via rtpmap. All others
1221 * are implcitly definedby the number in m=audio */
1222 if (pt >= 96 && pt <= 127) {
Neels Hofmeyr47642f22019-02-27 05:56:53 +01001223 codec = get_value_string_or_null(osmo_mgcpc_codec_names, mgcp_msg->codecs[i]);
Philipp Maier704c4f02018-06-07 18:51:31 +02001224
1225 /* Note: Use codec descriptors from enum mgcp_codecs
1226 * in mgcp_client only! */
1227 OSMO_ASSERT(codec);
Pau Espin Pedrolc12bfb72019-04-16 17:23:09 +02001228
Philipp Maier704c4f02018-06-07 18:51:31 +02001229 rc += msgb_printf(msg, "a=rtpmap:%u %s\r\n", pt, codec);
1230 }
1231 }
Pau Espin Pedrolc12bfb72019-04-16 17:23:09 +02001232
Philipp Maier704c4f02018-06-07 18:51:31 +02001233 if (mgcp_msg->ptime)
1234 rc += msgb_printf(msg, "a=ptime:%u\r\n", mgcp_msg->ptime);
1235
1236 return rc;
1237}
1238
Philipp Maier275ac972018-01-20 00:58:16 +01001239/*! Generate an MGCP message
1240 * \param[in] mgcp MGCP client descriptor.
1241 * \param[in] mgcp_msg Message description
1242 * \returns message buffer on success, NULL on error. */
Philipp Maier1dc6be62017-10-05 18:25:37 +02001243struct msgb *mgcp_msg_gen(struct mgcp_client *mgcp, struct mgcp_msg *mgcp_msg)
1244{
1245 mgcp_trans_id_t trans_id = mgcp_client_next_trans_id(mgcp);
1246 uint32_t mandatory_mask;
1247 struct msgb *msg = msgb_alloc_headroom(4096, 128, "MGCP tx");
1248 int rc = 0;
Philipp Maier704c4f02018-06-07 18:51:31 +02001249 int rc_sdp;
1250 bool use_sdp = false;
Pau Espin Pedrol900cd652019-04-24 22:06:22 +02001251 char buf[32];
Philipp Maier1dc6be62017-10-05 18:25:37 +02001252
1253 msg->l2h = msg->data;
1254 msg->cb[MSGB_CB_MGCP_TRANS_ID] = trans_id;
1255
1256 /* Add command verb */
1257 switch (mgcp_msg->verb) {
1258 case MGCP_VERB_CRCX:
1259 mandatory_mask = MGCP_CRCX_MANDATORY;
1260 rc += msgb_printf(msg, "CRCX %u", trans_id);
1261 break;
1262 case MGCP_VERB_MDCX:
1263 mandatory_mask = MGCP_MDCX_MANDATORY;
1264 rc += msgb_printf(msg, "MDCX %u", trans_id);
1265 break;
1266 case MGCP_VERB_DLCX:
1267 mandatory_mask = MGCP_DLCX_MANDATORY;
1268 rc += msgb_printf(msg, "DLCX %u", trans_id);
1269 break;
1270 case MGCP_VERB_AUEP:
1271 mandatory_mask = MGCP_AUEP_MANDATORY;
1272 rc += msgb_printf(msg, "AUEP %u", trans_id);
1273 break;
1274 case MGCP_VERB_RSIP:
1275 mandatory_mask = MGCP_RSIP_MANDATORY;
1276 rc += msgb_printf(msg, "RSIP %u", trans_id);
1277 break;
1278 default:
1279 LOGP(DLMGCP, LOGL_ERROR,
1280 "Invalid command verb, can not generate MGCP message\n");
1281 msgb_free(msg);
1282 return NULL;
1283 }
1284
1285 /* Check if mandatory fields are missing */
1286 if (!((mgcp_msg->presence & mandatory_mask) == mandatory_mask)) {
1287 LOGP(DLMGCP, LOGL_ERROR,
1288 "One or more missing mandatory fields, can not generate MGCP message\n");
1289 msgb_free(msg);
1290 return NULL;
1291 }
1292
1293 /* Add endpoint name */
Philipp Maier7bc55522017-12-10 22:52:22 +01001294 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_ENDPOINT) {
1295 if (strlen(mgcp_msg->endpoint) <= 0) {
1296 LOGP(DLMGCP, LOGL_ERROR,
1297 "Empty endpoint name, can not generate MGCP message\n");
1298 msgb_free(msg);
1299 return NULL;
1300 }
Philipp Maier3aa81572018-01-31 14:13:45 +01001301
1302 if (strstr(mgcp_msg->endpoint, "@") == NULL) {
1303 LOGP(DLMGCP, LOGL_ERROR,
1304 "Endpoint name (%s) lacks separator (@), can not generate MGCP message\n",
1305 mgcp_msg->endpoint);
1306 msgb_free(msg);
Vadim Yanitskiye6743452020-06-18 03:04:13 +07001307 return NULL;
Philipp Maier3aa81572018-01-31 14:13:45 +01001308 }
1309
Philipp Maier1dc6be62017-10-05 18:25:37 +02001310 rc += msgb_printf(msg, " %s", mgcp_msg->endpoint);
Philipp Maier7bc55522017-12-10 22:52:22 +01001311 }
Philipp Maier1dc6be62017-10-05 18:25:37 +02001312
1313 /* Add protocol version */
1314 rc += msgb_printf(msg, " MGCP 1.0\r\n");
1315
1316 /* Add call id */
1317 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_CALL_ID)
1318 rc += msgb_printf(msg, "C: %x\r\n", mgcp_msg->call_id);
1319
1320 /* Add connection id */
Philipp Maier7bc55522017-12-10 22:52:22 +01001321 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_CONN_ID) {
1322 if (strlen(mgcp_msg->conn_id) <= 0) {
1323 LOGP(DLMGCP, LOGL_ERROR,
1324 "Empty connection id, can not generate MGCP message\n");
1325 msgb_free(msg);
1326 return NULL;
1327 }
Philipp Maier01d24a32017-11-21 17:26:09 +01001328 rc += msgb_printf(msg, "I: %s\r\n", mgcp_msg->conn_id);
Philipp Maier7bc55522017-12-10 22:52:22 +01001329 }
Philipp Maier1dc6be62017-10-05 18:25:37 +02001330
Philipp Maier704c4f02018-06-07 18:51:31 +02001331 /* Using SDP makes sense when a valid IP/Port combination is specifiec,
1332 * if we do not know this information yet, we fall back to LCO */
1333 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_AUDIO_IP
1334 && mgcp_msg->presence & MGCP_MSG_PRESENCE_AUDIO_PORT)
1335 use_sdp = true;
1336
1337 /* Add local connection options (LCO) */
1338 if (!use_sdp
1339 && (mgcp_msg->verb == MGCP_VERB_CRCX
1340 || mgcp_msg->verb == MGCP_VERB_MDCX))
1341 rc += add_lco(msg, mgcp_msg);
Philipp Maier1dc6be62017-10-05 18:25:37 +02001342
1343 /* Add mode */
1344 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_CONN_MODE)
1345 rc +=
1346 msgb_printf(msg, "M: %s\r\n",
1347 mgcp_client_cmode_name(mgcp_msg->conn_mode));
1348
Neels Hofmeyre6d8e912018-08-23 16:36:48 +02001349 /* Add X-Osmo-IGN */
1350 if ((mgcp_msg->presence & MGCP_MSG_PRESENCE_X_OSMO_IGN)
1351 && (mgcp_msg->x_osmo_ign != 0))
1352 rc +=
1353 msgb_printf(msg, MGCP_X_OSMO_IGN_HEADER "%s\r\n",
1354 mgcp_msg->x_osmo_ign & MGCP_X_OSMO_IGN_CALLID ? " C": "");
1355
Pau Espin Pedrol900cd652019-04-24 22:06:22 +02001356 /* Add X-Osmo-Osmux */
1357 if ((mgcp_msg->presence & MGCP_MSG_PRESENCE_X_OSMO_OSMUX_CID)) {
Pau Espin Pedrol1b1d7ed2019-05-23 16:48:24 +02001358 if (mgcp_msg->x_osmo_osmux_cid < -1 || mgcp_msg->x_osmo_osmux_cid > OSMUX_CID_MAX) {
1359 LOGP(DLMGCP, LOGL_ERROR,
1360 "Wrong Osmux CID %d, can not generate MGCP message\n",
1361 mgcp_msg->x_osmo_osmux_cid);
1362 msgb_free(msg);
1363 return NULL;
1364 }
Pau Espin Pedrol900cd652019-04-24 22:06:22 +02001365 snprintf(buf, sizeof(buf), " %d", mgcp_msg->x_osmo_osmux_cid);
1366 rc +=
1367 msgb_printf(msg, MGCP_X_OSMO_OSMUX_HEADER "%s\r\n",
1368 mgcp_msg->x_osmo_osmux_cid == -1 ? " *": buf);
1369 }
1370
1371
Philipp Maier704c4f02018-06-07 18:51:31 +02001372 /* Add session description protocol (SDP) */
1373 if (use_sdp
1374 && (mgcp_msg->verb == MGCP_VERB_CRCX
1375 || mgcp_msg->verb == MGCP_VERB_MDCX)) {
1376 rc_sdp = add_sdp(msg, mgcp_msg, mgcp);
1377 if (rc_sdp == -2)
Philipp Maier9d25d7a2018-01-22 17:31:10 +01001378 return NULL;
Philipp Maier704c4f02018-06-07 18:51:31 +02001379 else
1380 rc += rc_sdp;
Philipp Maier1dc6be62017-10-05 18:25:37 +02001381 }
1382
1383 if (rc != 0) {
1384 LOGP(DLMGCP, LOGL_ERROR,
1385 "message buffer to small, can not generate MGCP message\n");
1386 msgb_free(msg);
1387 msg = NULL;
1388 }
1389
1390 return msg;
1391}
1392
Philipp Maier275ac972018-01-20 00:58:16 +01001393/*! Retrieve the MGCP transaction ID from a msgb generated by mgcp_msg_gen()
1394 * \param[in] msg message buffer
1395 * \returns Transaction id. */
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +01001396mgcp_trans_id_t mgcp_msg_trans_id(struct msgb *msg)
1397{
1398 return (mgcp_trans_id_t)msg->cb[MSGB_CB_MGCP_TRANS_ID];
1399}
1400
Philipp Maier275ac972018-01-20 00:58:16 +01001401/*! Get the configuration parameters a given MGCP client instance
1402 * \param[in] mgcp MGCP client descriptor.
1403 * \returns configuration */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +02001404struct mgcp_client_conf *mgcp_client_conf_actual(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001405{
1406 return &mgcp->actual;
1407}
Neels Hofmeyrd95ab1e2017-09-22 00:52:54 +02001408
1409const struct value_string mgcp_client_connection_mode_strs[] = {
1410 { MGCP_CONN_NONE, "none" },
1411 { MGCP_CONN_RECV_SEND, "sendrecv" },
1412 { MGCP_CONN_SEND_ONLY, "sendonly" },
1413 { MGCP_CONN_RECV_ONLY, "recvonly" },
1414 { MGCP_CONN_LOOPBACK, "loopback" },
1415 { 0, NULL }
1416};