blob: 2dcab62a8e21ff5898b0db82f24be50f9af02c3b [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>
Neels Hofmeyre9920f22017-07-10 15:07:22 +020028
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +020029#include <osmocom/mgcp_client/mgcp_client.h>
30#include <osmocom/mgcp_client/mgcp_client_internal.h>
Neels Hofmeyre9920f22017-07-10 15:07:22 +020031
32#include <netinet/in.h>
33#include <arpa/inet.h>
34
35#include <errno.h>
36#include <unistd.h>
37#include <string.h>
38
Philipp Maier704c4f02018-06-07 18:51:31 +020039/* Codec descripton for dynamic payload types (SDP) */
Neels Hofmeyr47642f22019-02-27 05:56:53 +010040const struct value_string osmo_mgcpc_codec_names[] = {
Philipp Maier704c4f02018-06-07 18:51:31 +020041 { CODEC_PCMU_8000_1, "PCMU/8000/1" },
42 { CODEC_GSM_8000_1, "GSM/8000/1" },
43 { CODEC_PCMA_8000_1, "PCMA/8000/1" },
44 { CODEC_G729_8000_1, "G729/8000/1" },
45 { CODEC_GSMEFR_8000_1, "GSM-EFR/8000/1" },
46 { CODEC_GSMHR_8000_1, "GSM-HR-08/8000/1" },
47 { CODEC_AMR_8000_1, "AMR/8000/1" },
48 { CODEC_AMRWB_16000_1, "AMR-WB/16000/1" },
49 { 0, NULL },
50};
51
52/* Get encoding name from a full codec string e,g.
53 * ("CODEC/8000/2" => returns "CODEC") */
54static char *extract_codec_name(const char *str)
55{
56 static char buf[64];
57 unsigned int i;
58
59 if (!str)
60 return NULL;
61
62 /* FIXME osmo_strlcpy */
63 osmo_strlcpy(buf, str, sizeof(buf));
64
65 for (i = 0; i < strlen(buf); i++) {
66 if (buf[i] == '/')
67 buf[i] = '\0';
68 }
69
70 return buf;
71}
72
73/*! Map a string to a codec.
74 * \ptmap[in] str input string (e.g "GSM/8000/1", "GSM/8000" or "GSM")
75 * \returns codec that corresponds to the given string representation. */
76enum mgcp_codecs map_str_to_codec(const char *str)
77{
78 unsigned int i;
79 char *codec_name;
80 char str_buf[64];
81
82 osmo_strlcpy(str_buf, extract_codec_name(str), sizeof(str_buf));
83
Neels Hofmeyr47642f22019-02-27 05:56:53 +010084 for (i = 0; i < ARRAY_SIZE(osmo_mgcpc_codec_names); i++) {
85 codec_name = extract_codec_name(osmo_mgcpc_codec_names[i].str);
Philipp Maier704c4f02018-06-07 18:51:31 +020086 if (!codec_name)
87 continue;
88 if (strcmp(codec_name, str_buf) == 0)
Neels Hofmeyr47642f22019-02-27 05:56:53 +010089 return osmo_mgcpc_codec_names[i].value;
Philipp Maier704c4f02018-06-07 18:51:31 +020090 }
91
92 return -1;
93}
94
95/* Check the ptmap for illegal mappings */
96static int check_ptmap(struct ptmap *ptmap)
97{
98 /* Check if there are mappings that leave the IANA assigned dynamic
99 * payload type range. Under normal conditions such mappings should
100 * not occur */
101
102 /* Its ok to have a 1:1 mapping in the statically defined
103 * range, this won't hurt */
104 if (ptmap->codec == ptmap->pt)
105 return 0;
106
107 if (ptmap->codec < 96 || ptmap->codec > 127)
108 goto error;
109 if (ptmap->pt < 96 || ptmap->pt > 127)
110 goto error;
111
112 return 0;
113error:
114 LOGP(DLMGCP, LOGL_ERROR,
115 "ptmap contains illegal mapping: codec=%u maps to pt=%u\n",
116 ptmap->codec, ptmap->pt);
117 return -1;
118}
119
120/*! Map a codec to a payload type.
121 * \ptmap[in] payload pointer to payload type map with specified payload types.
122 * \ptmap[in] ptmap_len length of the payload type map.
123 * \ptmap[in] codec the codec for which the payload type should be looked up.
124 * \returns assigned payload type */
125unsigned int map_codec_to_pt(struct ptmap *ptmap, unsigned int ptmap_len,
126 enum mgcp_codecs codec)
127{
128 unsigned int i;
129
130 /*! Note: If the payload type map is empty or the codec is not found
131 * in the map, then a 1:1 mapping is performed. If the codec falls
132 * into the statically defined range or if the mapping table isself
133 * tries to map to the statically defined range, then the mapping
134 * is also ignored and a 1:1 mapping is performed instead. */
135
136 /* we may return the codec directly since enum mgcp_codecs directly
137 * corresponds to the statićally assigned payload types */
138 if (codec < 96 || codec > 127)
139 return codec;
140
141 for (i = 0; i < ptmap_len; i++) {
142 /* Skip illegal map entries */
143 if (check_ptmap(ptmap) == 0 && ptmap->codec == codec)
144 return ptmap->pt;
145 ptmap++;
146 }
147
148 /* If nothing is found, do not perform any mapping */
149 return codec;
150}
151
152/*! Map a payload type to a codec.
153 * \ptmap[in] payload pointer to payload type map with specified payload types.
154 * \ptmap[in] ptmap_len length of the payload type map.
155 * \ptmap[in] payload type for which the codec should be looked up.
156 * \returns codec that corresponds to the specified payload type */
157enum mgcp_codecs map_pt_to_codec(struct ptmap *ptmap, unsigned int ptmap_len,
158 unsigned int pt)
159{
160 unsigned int i;
161
162 /*! Note: If the payload type map is empty or the payload type is not
163 * found in the map, then a 1:1 mapping is performed. If the payload
164 * type falls into the statically defined range or if the mapping
165 * table isself tries to map to the statically defined range, then
166 * the mapping is also ignored and a 1:1 mapping is performed
167 * instead. */
168
169 /* See also note in map_codec_to_pt() */
170 if (pt < 96 || pt > 127)
171 return pt;
172
173 for (i = 0; i < ptmap_len; i++) {
174 if (check_ptmap(ptmap) == 0 && ptmap->pt == pt)
175 return ptmap->codec;
176 ptmap++;
177 }
178
179 /* If nothing is found, do not perform any mapping */
180 return pt;
181}
182
Philipp Maier275ac972018-01-20 00:58:16 +0100183/*! Initalize MGCP client configuration struct with default values.
184 * \param[out] conf Client configuration.*/
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200185void mgcp_client_conf_init(struct mgcp_client_conf *conf)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200186{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200187 /* NULL and -1 default to MGCP_CLIENT_*_DEFAULT values */
188 *conf = (struct mgcp_client_conf){
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200189 .local_addr = NULL,
190 .local_port = -1,
191 .remote_addr = NULL,
192 .remote_port = -1,
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200193 };
194}
195
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200196static void mgcp_client_handle_response(struct mgcp_client *mgcp,
197 struct mgcp_response_pending *pending,
198 struct mgcp_response *response)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200199{
200 if (!pending) {
201 LOGP(DLMGCP, LOGL_ERROR,
202 "Cannot handle NULL response\n");
203 return;
204 }
205 if (pending->response_cb)
206 pending->response_cb(response, pending->priv);
207 else
Neels Hofmeyred0c1aa2018-12-21 03:07:53 +0100208 LOGP(DLMGCP, LOGL_DEBUG, "MGCP response ignored (NULL cb)\n");
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200209 talloc_free(pending);
210}
211
212static int mgcp_response_parse_head(struct mgcp_response *r, struct msgb *msg)
213{
214 int comment_pos;
215 char *end;
216
217 if (mgcp_msg_terminate_nul(msg))
218 goto response_parse_failure;
219
220 r->body = (char *)msg->data;
221
Harald Welte9bf7c532017-11-17 14:14:31 +0100222 if (sscanf(r->body, "%3d %u %n",
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200223 &r->head.response_code, &r->head.trans_id,
224 &comment_pos) != 2)
225 goto response_parse_failure;
226
Philipp Maierabe8c892018-01-20 00:15:12 +0100227 osmo_strlcpy(r->head.comment, r->body + comment_pos, sizeof(r->head.comment));
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200228 end = strchr(r->head.comment, '\r');
229 if (!end)
230 goto response_parse_failure;
231 /* Mark the end of the comment */
232 *end = '\0';
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200233 return 0;
234
235response_parse_failure:
236 LOGP(DLMGCP, LOGL_ERROR,
237 "Failed to parse MGCP response header\n");
238 return -EINVAL;
239}
240
241/* TODO undup against mgcp_protocol.c:mgcp_check_param() */
242static bool mgcp_line_is_valid(const char *line)
243{
244 const size_t line_len = strlen(line);
245 if (line[0] == '\0')
246 return true;
247
248 if (line_len < 2
249 || line[1] != '=') {
250 LOGP(DLMGCP, LOGL_ERROR,
251 "Wrong MGCP option format: '%s'\n",
252 line);
253 return false;
254 }
255
256 return true;
257}
258
Philipp Maier704c4f02018-06-07 18:51:31 +0200259/* Parse a line like "m=audio 16002 RTP/AVP 98", extract port and payload types */
260static int mgcp_parse_audio_port_pt(struct mgcp_response *r, char *line)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200261{
Philipp Maier704c4f02018-06-07 18:51:31 +0200262 char *pt_str;
263 unsigned int pt;
264 unsigned int count = 0;
265 unsigned int i;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200266
Philipp Maier704c4f02018-06-07 18:51:31 +0200267 /* Extract port information */
268 if (sscanf(line, "m=audio %hu", &r->audio_port) != 1)
269 goto response_parse_failure_port;
Philipp Maier10f32db2017-12-13 12:34:34 +0100270 if (r->audio_port == 0)
Philipp Maier704c4f02018-06-07 18:51:31 +0200271 goto response_parse_failure_port;
Philipp Maier10f32db2017-12-13 12:34:34 +0100272
Philipp Maier704c4f02018-06-07 18:51:31 +0200273 /* Extract payload types */
274 line = strstr(line, "RTP/AVP ");
275 if (!line)
276 goto exit;
277
278 pt_str = strtok(line, " ");
279 while (1) {
280 /* Do not allow excessive payload types */
281 if (count > ARRAY_SIZE(r->codecs))
282 goto response_parse_failure_pt;
283
284 pt_str = strtok(NULL, " ");
285 if (!pt_str)
286 break;
287 pt = atoi(pt_str);
288
289 /* Do not allow duplicate payload types */
290 for (i = 0; i < count; i++)
291 if (r->codecs[i] == pt)
292 goto response_parse_failure_pt;
293
294 /* Note: The payload type we store may not necessarly match
295 * the codec types we have defined in enum mgcp_codecs. To
296 * ensure that the end result only contains codec types which
297 * match enum mgcp_codecs, we will go through afterwards and
298 * remap the affected entries with the inrofmation we learn
299 * from rtpmap */
300 r->codecs[count] = pt;
301 count++;
302 }
303
304 r->codecs_len = count;
305
306exit:
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200307 return 0;
308
Philipp Maier704c4f02018-06-07 18:51:31 +0200309response_parse_failure_port:
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200310 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier704c4f02018-06-07 18:51:31 +0200311 "Failed to parse SDP parameter port (%s)\n", line);
Philipp Maier06da85e2017-10-05 18:49:24 +0200312 return -EINVAL;
Philipp Maier704c4f02018-06-07 18:51:31 +0200313
314response_parse_failure_pt:
315 LOGP(DLMGCP, LOGL_ERROR,
316 "Failed to parse SDP parameter payload types (%s)\n", line);
317 return -EINVAL;
318}
319
320/* Parse a line like "m=audio 16002 RTP/AVP 98", extract port and payload types */
321static int mgcp_parse_audio_ptime_rtpmap(struct mgcp_response *r, const char *line)
322{
323 unsigned int pt;
324 char codec_resp[64];
325 unsigned int codec;
Pau Espin Pedrolc12bfb72019-04-16 17:23:09 +0200326
327
Philipp Maier704c4f02018-06-07 18:51:31 +0200328 if (strstr(line, "ptime")) {
329 if (sscanf(line, "a=ptime:%u", &r->ptime) != 1)
330 goto response_parse_failure_ptime;
331 } else if (strstr(line, "rtpmap")) {
332 if (sscanf(line, "a=rtpmap:%d %63s", &pt, codec_resp) == 2) {
333 /* The MGW may assign an own payload type in the
334 * response if the choosen codec falls into the IANA
335 * assigned dynamic payload type range (96-127).
336 * Normally the MGW should obey the 3gpp payload type
337 * assignments, which are fixed, so we likely wont see
338 * anything unexpected here. In order to be sure that
339 * we will now check the codec string and if the result
340 * does not match to what is IANA / 3gpp assigned, we
341 * will create an entry in the ptmap table so we can
342 * lookup later what has been assigned. */
343 codec = map_str_to_codec(codec_resp);
344 if (codec != pt) {
345 if (r->ptmap_len < ARRAY_SIZE(r->ptmap)) {
346 r->ptmap[r->ptmap_len].pt = pt;
347 r->ptmap[r->ptmap_len].codec = codec;
348 r->ptmap_len++;
349 } else
350 goto response_parse_failure_rtpmap;
351 }
352
353 } else
354 goto response_parse_failure_rtpmap;
355 }
Pau Espin Pedrolc12bfb72019-04-16 17:23:09 +0200356
Philipp Maier704c4f02018-06-07 18:51:31 +0200357 return 0;
358
359response_parse_failure_ptime:
360 LOGP(DLMGCP, LOGL_ERROR,
361 "Failed to parse SDP parameter, invalid ptime (%s)\n", line);
Pau Espin Pedrolc12bfb72019-04-16 17:23:09 +0200362 return -EINVAL;
Philipp Maier704c4f02018-06-07 18:51:31 +0200363response_parse_failure_rtpmap:
364 LOGP(DLMGCP, LOGL_ERROR,
365 "Failed to parse SDP parameter, invalid rtpmap (%s)\n", line);
Pau Espin Pedrolc12bfb72019-04-16 17:23:09 +0200366 return -EINVAL;
Philipp Maier06da85e2017-10-05 18:49:24 +0200367}
368
369/* Parse a line like "c=IN IP4 10.11.12.13" */
370static int mgcp_parse_audio_ip(struct mgcp_response *r, const char *line)
371{
372 struct in_addr ip_test;
373
374 if (strlen(line) < 16)
375 goto response_parse_failure;
376
377 /* The current implementation strictly supports IPV4 only ! */
378 if (memcmp("c=IN IP4 ", line, 9) != 0)
379 goto response_parse_failure;
380
381 /* Extract IP-Address */
Philipp Maierf8bfbe82017-11-23 19:32:31 +0100382 osmo_strlcpy(r->audio_ip, line + 9, sizeof(r->audio_ip));
Philipp Maier06da85e2017-10-05 18:49:24 +0200383
384 /* Check IP-Address */
385 if (inet_aton(r->audio_ip, &ip_test) == 0)
386 goto response_parse_failure;
387
388 return 0;
389
390response_parse_failure:
391 LOGP(DLMGCP, LOGL_ERROR,
392 "Failed to parse MGCP response header (audio ip)\n");
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200393 return -EINVAL;
394}
395
Philipp Maier3b12e1b2018-01-18 15:16:13 +0100396/* A new section is marked by a double line break, check a few more
397 * patterns as there may be variants */
398static char *mgcp_find_section_end(char *string)
399{
400 char *rc;
401
402 rc = strstr(string, "\n\n");
403 if (rc)
404 return rc;
405
406 rc = strstr(string, "\n\r\n\r");
407 if (rc)
408 return rc;
409
410 rc = strstr(string, "\r\n\r\n");
411 if (rc)
412 return rc;
413
414 return NULL;
415}
416
Philipp Maier275ac972018-01-20 00:58:16 +0100417/*! Parse body (SDP) parameters of the MGCP response
418 * \param[in,out] r Response data
419 * \returns 0 on success, -EINVAL on error. */
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200420int mgcp_response_parse_params(struct mgcp_response *r)
421{
422 char *line;
423 int rc;
Neels Hofmeyr0793d2f2018-02-21 14:55:34 +0100424 char *data;
Philipp Maiere9d645b2018-01-19 23:54:08 +0100425 char *data_ptr;
Philipp Maier704c4f02018-06-07 18:51:31 +0200426 int i;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200427
Philipp Maiere9d645b2018-01-19 23:54:08 +0100428 /* Since this functions performs a destructive parsing, we create a
429 * local copy of the body data */
Neels Hofmeyr0793d2f2018-02-21 14:55:34 +0100430 OSMO_ASSERT(r->body);
431 data = talloc_strdup(r, r->body);
Philipp Maiere9d645b2018-01-19 23:54:08 +0100432 OSMO_ASSERT(data);
Philipp Maier55295f72018-01-15 14:00:28 +0100433
Philipp Maiere9d645b2018-01-19 23:54:08 +0100434 /* Find beginning of the parameter (SDP) section */
435 data_ptr = mgcp_find_section_end(data);
Neels Hofmeyr10835332018-02-21 14:55:34 +0100436 if (!data_ptr) {
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200437 LOGP(DLMGCP, LOGL_ERROR,
Neels Hofmeyr0793d2f2018-02-21 14:55:34 +0100438 "MGCP response: cannot find start of SDP parameters\n");
Philipp Maiere9d645b2018-01-19 23:54:08 +0100439 rc = -EINVAL;
440 goto exit;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200441 }
442
Neels Hofmeyr0793d2f2018-02-21 14:55:34 +0100443 /* data_ptr now points to the beginning of the section-end-marker; for_each_non_empty_line()
444 * skips any \r and \n characters for free, so we don't need to skip the marker. */
445
Philipp Maiere9d645b2018-01-19 23:54:08 +0100446 for_each_non_empty_line(line, data_ptr) {
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200447 if (!mgcp_line_is_valid(line))
448 return -EINVAL;
449
450 switch (line[0]) {
Philipp Maier704c4f02018-06-07 18:51:31 +0200451 case 'a':
452 rc = mgcp_parse_audio_ptime_rtpmap(r, line);
453 if (rc)
454 goto exit;
455 break;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200456 case 'm':
Philipp Maier704c4f02018-06-07 18:51:31 +0200457 rc = mgcp_parse_audio_port_pt(r, line);
Philipp Maier06da85e2017-10-05 18:49:24 +0200458 if (rc)
Philipp Maiere9d645b2018-01-19 23:54:08 +0100459 goto exit;
Philipp Maier06da85e2017-10-05 18:49:24 +0200460 break;
461 case 'c':
462 rc = mgcp_parse_audio_ip(r, line);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200463 if (rc)
Philipp Maiere9d645b2018-01-19 23:54:08 +0100464 goto exit;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200465 break;
466 default:
467 /* skip unhandled parameters */
468 break;
469 }
470 }
Philipp Maiere9d645b2018-01-19 23:54:08 +0100471
Philipp Maier704c4f02018-06-07 18:51:31 +0200472 /* See also note in mgcp_parse_audio_port_pt() */
473 for (i = 0; i < r->codecs_len; i++)
474 r->codecs[i] = map_pt_to_codec(r->ptmap, r->ptmap_len, r->codecs[i]);
475
Philipp Maiere9d645b2018-01-19 23:54:08 +0100476 rc = 0;
477exit:
478 talloc_free(data);
479 return rc;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200480}
481
Philipp Maier55295f72018-01-15 14:00:28 +0100482/* Parse a line like "X: something" */
483static int mgcp_parse_head_param(char *result, unsigned int result_len,
484 char label, const char *line)
Philipp Maierffd75e42017-11-22 11:44:50 +0100485{
Philipp Maier55295f72018-01-15 14:00:28 +0100486 char label_string[4];
Neels Hofmeyr23e7bf12018-09-03 21:26:22 +0200487 size_t rc;
Philipp Maier55295f72018-01-15 14:00:28 +0100488
489 /* Detect empty parameters */
Philipp Maierffd75e42017-11-22 11:44:50 +0100490 if (strlen(line) < 4)
491 goto response_parse_failure;
492
Philipp Maier55295f72018-01-15 14:00:28 +0100493 /* Check if the label matches */
494 snprintf(label_string, sizeof(label_string), "%c: ", label);
495 if (memcmp(label_string, line, 3) != 0)
Philipp Maierffd75e42017-11-22 11:44:50 +0100496 goto response_parse_failure;
497
Philipp Maier55295f72018-01-15 14:00:28 +0100498 /* Copy payload part of the string to destinations (the label string
499 * is always 3 chars long) */
Neels Hofmeyr23e7bf12018-09-03 21:26:22 +0200500 rc = osmo_strlcpy(result, line + 3, result_len);
501 if (rc >= result_len) {
502 LOGP(DLMGCP, LOGL_ERROR,
503 "Failed to parse MGCP response (parameter label: %c):"
504 " the received conn ID is too long: %zu, maximum is %u characters\n",
505 label, rc, result_len - 1);
506 return -ENOSPC;
507 }
Philipp Maierffd75e42017-11-22 11:44:50 +0100508 return 0;
509
510response_parse_failure:
511 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier55295f72018-01-15 14:00:28 +0100512 "Failed to parse MGCP response (parameter label: %c)\n", label);
Philipp Maierffd75e42017-11-22 11:44:50 +0100513 return -EINVAL;
514}
515
516/* Parse MGCP parameters of the response */
517static int parse_head_params(struct mgcp_response *r)
518{
519 char *line;
520 int rc = 0;
521 OSMO_ASSERT(r->body);
Philipp Maier55295f72018-01-15 14:00:28 +0100522 char *data;
523 char *data_ptr;
524 char *data_end;
Philipp Maierffd75e42017-11-22 11:44:50 +0100525
Philipp Maier55295f72018-01-15 14:00:28 +0100526 /* Since this functions performs a destructive parsing, we create a
527 * local copy of the body data */
Philipp Maier1fc69992018-01-31 15:32:55 +0100528 data = talloc_zero_size(r, strlen(r->body)+1);
Philipp Maier55295f72018-01-15 14:00:28 +0100529 OSMO_ASSERT(data);
530 data_ptr = data;
531 osmo_strlcpy(data, r->body, strlen(r->body));
532
533 /* If there is an SDP body attached, prevent for_each_non_empty_line()
534 * into running in there, we are not yet interested in the parameters
535 * stored there. */
Pau Espin Pedrolc12bfb72019-04-16 17:23:09 +0200536 data_end = mgcp_find_section_end(data);
Philipp Maierffd75e42017-11-22 11:44:50 +0100537 if (data_end)
538 *data_end = '\0';
539
Philipp Maier55295f72018-01-15 14:00:28 +0100540 for_each_non_empty_line(line, data_ptr) {
Philipp Maierffd75e42017-11-22 11:44:50 +0100541 switch (line[0]) {
Philipp Maier55295f72018-01-15 14:00:28 +0100542 case 'Z':
543 rc = mgcp_parse_head_param(r->head.endpoint,
544 sizeof(r->head.endpoint),
545 'Z', line);
546 if (rc)
547 goto exit;
Philipp Maier771b26a2018-01-31 13:53:11 +0100548
549 /* A specific endpoint identifier returned by the MGW
550 * must not contain any wildcard characters */
551 if (strstr(r->head.endpoint, "*") != NULL) {
552 rc = -EINVAL;
553 goto exit;
554 }
Philipp Maier3261dc72018-01-31 14:03:13 +0100555
556 /* A specific endpoint identifier returned by the MGW
557 * must contain an @ character */
558 if (strstr(r->head.endpoint, "@") == NULL) {
559 rc = -EINVAL;
560 goto exit;
561 }
Philipp Maier55295f72018-01-15 14:00:28 +0100562 break;
Philipp Maierffd75e42017-11-22 11:44:50 +0100563 case 'I':
Philipp Maier55295f72018-01-15 14:00:28 +0100564 rc = mgcp_parse_head_param(r->head.conn_id,
565 sizeof(r->head.conn_id),
566 'I', line);
Philipp Maierffd75e42017-11-22 11:44:50 +0100567 if (rc)
568 goto exit;
569 break;
570 default:
571 /* skip unhandled parameters */
572 break;
573 }
574 }
575exit:
Philipp Maier55295f72018-01-15 14:00:28 +0100576 talloc_free(data);
Philipp Maierffd75e42017-11-22 11:44:50 +0100577 return rc;
578}
579
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200580static struct mgcp_response_pending *mgcp_client_response_pending_get(
581 struct mgcp_client *mgcp,
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100582 mgcp_trans_id_t trans_id)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200583{
584 struct mgcp_response_pending *pending;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200585 llist_for_each_entry(pending, &mgcp->responses_pending, entry) {
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100586 if (pending->trans_id == trans_id) {
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200587 llist_del(&pending->entry);
588 return pending;
589 }
590 }
591 return NULL;
592}
593
594/* Feed an MGCP message into the receive processing.
595 * Parse the head and call any callback registered for the transaction id found
596 * in the MGCP message. This is normally called directly from the internal
597 * mgcp_do_read that reads from the socket connected to the MGCP gateway. This
598 * function is published mainly to be able to feed data from the test suite.
599 */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200600int mgcp_client_rx(struct mgcp_client *mgcp, struct msgb *msg)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200601{
Philipp Maier1fc69992018-01-31 15:32:55 +0100602 struct mgcp_response *r;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200603 struct mgcp_response_pending *pending;
604 int rc;
605
Philipp Maier1fc69992018-01-31 15:32:55 +0100606 r = talloc_zero(mgcp, struct mgcp_response);
607 OSMO_ASSERT(r);
608
609 rc = mgcp_response_parse_head(r, msg);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200610 if (rc) {
Philipp Maierffd75e42017-11-22 11:44:50 +0100611 LOGP(DLMGCP, LOGL_ERROR, "Cannot parse MGCP response (head)\n");
Philipp Maier1fc69992018-01-31 15:32:55 +0100612 rc = 1;
613 goto error;
Philipp Maierffd75e42017-11-22 11:44:50 +0100614 }
615
Philipp Maier1fc69992018-01-31 15:32:55 +0100616 rc = parse_head_params(r);
Philipp Maierffd75e42017-11-22 11:44:50 +0100617 if (rc) {
618 LOGP(DLMGCP, LOGL_ERROR, "Cannot parse MGCP response (head parameters)\n");
Philipp Maier1fc69992018-01-31 15:32:55 +0100619 rc = 1;
620 goto error;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200621 }
622
Philipp Maier1fc69992018-01-31 15:32:55 +0100623 pending = mgcp_client_response_pending_get(mgcp, r->head.trans_id);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200624 if (!pending) {
625 LOGP(DLMGCP, LOGL_ERROR,
626 "Cannot find matching MGCP transaction for trans_id %d\n",
Philipp Maier1fc69992018-01-31 15:32:55 +0100627 r->head.trans_id);
628 rc = -ENOENT;
629 goto error;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200630 }
631
Philipp Maier1fc69992018-01-31 15:32:55 +0100632 mgcp_client_handle_response(mgcp, pending, r);
633 rc = 0;
634
635error:
636 talloc_free(r);
637 return rc;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200638}
639
640static int mgcp_do_read(struct osmo_fd *fd)
641{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200642 struct mgcp_client *mgcp = fd->data;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200643 struct msgb *msg;
644 int ret;
645
646 msg = msgb_alloc_headroom(4096, 128, "mgcp_from_gw");
647 if (!msg) {
648 LOGP(DLMGCP, LOGL_ERROR, "Failed to allocate MGCP message.\n");
649 return -1;
650 }
651
652 ret = read(fd->fd, msg->data, 4096 - 128);
653 if (ret <= 0) {
Neels Hofmeyr0a403792018-12-12 03:10:18 +0100654 LOGP(DLMGCP, LOGL_ERROR, "Failed to read: %s: %d='%s'\n", osmo_sock_get_name2(fd->fd),
655 errno, strerror(errno));
656
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200657 msgb_free(msg);
658 return -1;
659 } else if (ret > 4096 - 128) {
Neels Hofmeyr0a403792018-12-12 03:10:18 +0100660 LOGP(DLMGCP, LOGL_ERROR, "Too much data: %s: %d\n", osmo_sock_get_name2(fd->fd), ret);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200661 msgb_free(msg);
662 return -1;
Harald Welte9bf7c532017-11-17 14:14:31 +0100663 }
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200664
665 msg->l2h = msgb_put(msg, ret);
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200666 ret = mgcp_client_rx(mgcp, msg);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200667 talloc_free(msg);
668 return ret;
669}
670
671static int mgcp_do_write(struct osmo_fd *fd, struct msgb *msg)
672{
673 int ret;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200674
Neels Hofmeyr0a403792018-12-12 03:10:18 +0100675 LOGP(DLMGCP, LOGL_DEBUG, "Tx MGCP: %s: len=%u '%s'...\n",
676 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 +0200677
678 ret = write(fd->fd, msg->data, msg->len);
679 if (ret != msg->len)
Neels Hofmeyr0a403792018-12-12 03:10:18 +0100680 LOGP(DLMGCP, LOGL_ERROR, "Failed to Tx MGCP: %s: %d='%s'; msg: len=%u '%s'...\n",
681 osmo_sock_get_name2(fd->fd), errno, strerror(errno),
Neels Hofmeyr32d15cc2018-12-12 03:05:33 +0100682 msg->len, osmo_escape_str((const char*)msg->data, OSMO_MIN(42, msg->len)));
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200683 return ret;
684}
685
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200686struct mgcp_client *mgcp_client_init(void *ctx,
687 struct mgcp_client_conf *conf)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200688{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200689 struct mgcp_client *mgcp;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200690
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200691 mgcp = talloc_zero(ctx, struct mgcp_client);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200692
693 INIT_LLIST_HEAD(&mgcp->responses_pending);
694 INIT_LLIST_HEAD(&mgcp->inuse_endpoints);
695
696 mgcp->next_trans_id = 1;
697
698 mgcp->actual.local_addr = conf->local_addr ? conf->local_addr :
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200699 MGCP_CLIENT_LOCAL_ADDR_DEFAULT;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200700 mgcp->actual.local_port = conf->local_port >= 0 ? (uint16_t)conf->local_port :
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200701 MGCP_CLIENT_LOCAL_PORT_DEFAULT;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200702
703 mgcp->actual.remote_addr = conf->remote_addr ? conf->remote_addr :
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200704 MGCP_CLIENT_REMOTE_ADDR_DEFAULT;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200705 mgcp->actual.remote_port = conf->remote_port >= 0 ? (uint16_t)conf->remote_port :
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200706 MGCP_CLIENT_REMOTE_PORT_DEFAULT;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200707
Neels Hofmeyrac69ea92018-12-19 00:27:50 +0100708 if (osmo_strlcpy(mgcp->actual.endpoint_domain_name, conf->endpoint_domain_name,
709 sizeof(mgcp->actual.endpoint_domain_name))
710 >= sizeof(mgcp->actual.endpoint_domain_name)) {
711 LOGP(DLMGCP, LOGL_ERROR, "MGCP client: endpoint domain name is too long, max length is %zu: '%s'\n",
712 sizeof(mgcp->actual.endpoint_domain_name) - 1, conf->endpoint_domain_name);
713 talloc_free(mgcp);
714 return NULL;
715 }
716 LOGP(DLMGCP, LOGL_NOTICE, "MGCP client: using endpoint domain '@%s'\n", mgcp_client_endpoint_domain(mgcp));
717
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200718 return mgcp;
719}
720
Philipp Maier6a26c162018-08-01 16:37:06 +0200721static int init_socket(struct mgcp_client *mgcp)
722{
723 int rc;
724 struct osmo_wqueue *wq;
725 int i;
726
727 wq = &mgcp->wq;
728
729 for (i = 0; i < 100; i++) {
730
731 /* Initalize socket with the currently configured port
732 * number */
733 rc = osmo_sock_init2_ofd(&wq->bfd, AF_INET, SOCK_DGRAM, IPPROTO_UDP, mgcp->actual.local_addr,
734 mgcp->actual.local_port, mgcp->actual.remote_addr, mgcp->actual.remote_port,
735 OSMO_SOCK_F_BIND | OSMO_SOCK_F_CONNECT);
736 if (rc > 0)
737 return rc;
738
739 /* If there is a different port than the default port
740 * configured then we assume that the user has choosen
741 * that port conciously and we will not try to resolve
742 * this by silently choosing a different port. */
Philipp Maier9a7ccc32018-08-09 11:41:19 +0200743 if (mgcp->actual.local_port != MGCP_CLIENT_LOCAL_PORT_DEFAULT && i == 0)
Philipp Maier6a26c162018-08-01 16:37:06 +0200744 return -EINVAL;
745
746 /* Choose a new port number to try next */
747 LOGP(DLMGCP, LOGL_NOTICE,
Neels Hofmeyr0a403792018-12-12 03:10:18 +0100748 "MGCPGW failed to bind to %s:%u, retrying with port %u\n",
749 mgcp->actual.local_addr, mgcp->actual.local_port, mgcp->actual.local_port + 1);
Philipp Maier6a26c162018-08-01 16:37:06 +0200750 mgcp->actual.local_port++;
751 }
752
Neels Hofmeyr0a403792018-12-12 03:10:18 +0100753 LOGP(DLMGCP, LOGL_FATAL, "MGCPGW failed to find a port to bind on %i times.\n", i);
Philipp Maier6a26c162018-08-01 16:37:06 +0200754 return -EINVAL;
755}
756
Philipp Maier275ac972018-01-20 00:58:16 +0100757/*! Initalize client connection (opens socket only, no request is sent yet)
758 * \param[in,out] mgcp MGCP client descriptor.
759 * \returns 0 on success, -EINVAL on error. */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200760int mgcp_client_connect(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200761{
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200762 struct sockaddr_in addr;
763 struct osmo_wqueue *wq;
764 int rc;
765
766 if (!mgcp) {
767 LOGP(DLMGCP, LOGL_FATAL, "MGCPGW client not initialized properly\n");
768 return -EINVAL;
769 }
770
771 wq = &mgcp->wq;
772
Philipp Maier6a26c162018-08-01 16:37:06 +0200773 rc = init_socket(mgcp);
Harald Welte8890dfa2017-11-17 15:09:30 +0100774 if (rc < 0) {
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200775 LOGP(DLMGCP, LOGL_FATAL,
Harald Welte8890dfa2017-11-17 15:09:30 +0100776 "Failed to initialize socket %s:%u -> %s:%u for MGCP GW: %s\n",
777 mgcp->actual.local_addr, mgcp->actual.local_port,
778 mgcp->actual.remote_addr, mgcp->actual.remote_port, strerror(errno));
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200779 goto error_close_fd;
780 }
781
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200782 inet_aton(mgcp->actual.remote_addr, &addr.sin_addr);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200783 mgcp->remote_addr = htonl(addr.sin_addr.s_addr);
784
785 osmo_wqueue_init(wq, 10);
786 wq->bfd.when = BSC_FD_READ;
787 wq->bfd.data = mgcp;
788 wq->read_cb = mgcp_do_read;
789 wq->write_cb = mgcp_do_write;
790
Neels Hofmeyr0a403792018-12-12 03:10:18 +0100791 LOGP(DLMGCP, LOGL_INFO, "MGCP GW connection: %s\n", osmo_sock_get_name2(wq->bfd.fd));
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200792
793 return 0;
794error_close_fd:
795 close(wq->bfd.fd);
796 wq->bfd.fd = -1;
797 return rc;
798}
799
Philipp Maier275ac972018-01-20 00:58:16 +0100800/*! Get the IP-Aaddress of the associated MGW as string.
801 * \param[in] mgcp MGCP client descriptor.
802 * \returns a pointer to the address string. */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200803const char *mgcp_client_remote_addr_str(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200804{
805 return mgcp->actual.remote_addr;
806}
807
Philipp Maier275ac972018-01-20 00:58:16 +0100808/*! Get the IP-Port of the associated MGW.
809 * \param[in] mgcp MGCP client descriptor.
810 * \returns port number. */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200811uint16_t mgcp_client_remote_port(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200812{
813 return mgcp->actual.remote_port;
814}
815
Philipp Maier275ac972018-01-20 00:58:16 +0100816/*! Get the IP-Aaddress of the associated MGW as its numeric representation.
817 * \param[in] mgcp MGCP client descriptor.
818 * \returns IP-Address as 32 bit integer (network byte order) */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200819uint32_t mgcp_client_remote_addr_n(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200820{
821 return mgcp->remote_addr;
822}
823
Neels Hofmeyrac69ea92018-12-19 00:27:50 +0100824/* To compose endpoint names, usually for CRCX, use this as domain name.
825 * For example, snprintf("rtpbridge\*@%s", mgcp_client_endpoint_domain(mgcp)). */
826const char *mgcp_client_endpoint_domain(const struct mgcp_client *mgcp)
827{
828 return mgcp->actual.endpoint_domain_name[0] ? mgcp->actual.endpoint_domain_name : "mgw";
829}
830
Pau Espin Pedrolca0818c2019-04-16 17:23:38 +0200831static const char *_mgcp_client_name_append_domain(const struct mgcp_client *mgcp, char *name)
Neels Hofmeyrac69ea92018-12-19 00:27:50 +0100832{
833 static char endpoint[MGCP_ENDPOINT_MAXLEN];
834 int rc;
835
Pau Espin Pedrolca0818c2019-04-16 17:23:38 +0200836 rc = snprintf(endpoint, sizeof(endpoint), "%s@%s", name, mgcp_client_endpoint_domain(mgcp));
Neels Hofmeyrac69ea92018-12-19 00:27:50 +0100837 if (rc > sizeof(endpoint) - 1) {
Pau Espin Pedrolca0818c2019-04-16 17:23:38 +0200838 LOGP(DLMGCP, LOGL_ERROR, "MGCP endpoint exceeds maximum length of %zu: '%s@%s'\n",
839 sizeof(endpoint) - 1, name, mgcp_client_endpoint_domain(mgcp));
Neels Hofmeyrac69ea92018-12-19 00:27:50 +0100840 return NULL;
841 }
842 if (rc < 1) {
843 LOGP(DLMGCP, LOGL_ERROR, "Cannot compose MGCP endpoint name\n");
844 return NULL;
845 }
846 return endpoint;
847}
848
Pau Espin Pedrolca0818c2019-04-16 17:23:38 +0200849const char *mgcp_client_rtpbridge_wildcard(const struct mgcp_client *mgcp)
850{
851 return _mgcp_client_name_append_domain(mgcp, "rtpbridge/*");
852}
853
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200854struct mgcp_response_pending * mgcp_client_pending_add(
855 struct mgcp_client *mgcp,
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200856 mgcp_trans_id_t trans_id,
857 mgcp_response_cb_t response_cb,
858 void *priv)
859{
860 struct mgcp_response_pending *pending;
861
862 pending = talloc_zero(mgcp, struct mgcp_response_pending);
863 pending->trans_id = trans_id;
864 pending->response_cb = response_cb;
865 pending->priv = priv;
866 llist_add_tail(&pending->entry, &mgcp->responses_pending);
867
868 return pending;
869}
870
871/* Send the MGCP message in msg to the MGCP GW and handle a response with
872 * response_cb. NOTE: the response_cb still needs to call
873 * mgcp_response_parse_params(response) to get the parsed parameters -- to
874 * potentially save some CPU cycles, only the head line has been parsed when
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100875 * the response_cb is invoked.
876 * Before the priv pointer becomes invalid, e.g. due to transaction timeout,
877 * mgcp_client_cancel() needs to be called for this transaction.
878 */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200879int mgcp_client_tx(struct mgcp_client *mgcp, struct msgb *msg,
880 mgcp_response_cb_t response_cb, void *priv)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200881{
882 struct mgcp_response_pending *pending;
883 mgcp_trans_id_t trans_id;
884 int rc;
885
886 trans_id = msg->cb[MSGB_CB_MGCP_TRANS_ID];
887 if (!trans_id) {
888 LOGP(DLMGCP, LOGL_ERROR,
889 "Unset transaction id in mgcp send request\n");
890 talloc_free(msg);
891 return -EINVAL;
892 }
893
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200894 pending = mgcp_client_pending_add(mgcp, trans_id, response_cb, priv);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200895
896 if (msgb_l2len(msg) > 4096) {
897 LOGP(DLMGCP, LOGL_ERROR,
898 "Cannot send, MGCP message too large: %u\n",
899 msgb_l2len(msg));
900 msgb_free(msg);
901 rc = -EINVAL;
902 goto mgcp_tx_error;
903 }
904
905 rc = osmo_wqueue_enqueue(&mgcp->wq, msg);
906 if (rc) {
907 LOGP(DLMGCP, LOGL_FATAL, "Could not queue message to MGCP GW\n");
908 msgb_free(msg);
909 goto mgcp_tx_error;
910 } else
Neels Hofmeyred0c1aa2018-12-21 03:07:53 +0100911 LOGP(DLMGCP, LOGL_DEBUG, "Queued %u bytes for MGCP GW\n",
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200912 msgb_l2len(msg));
913 return 0;
914
915mgcp_tx_error:
916 /* Pass NULL to response cb to indicate an error */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200917 mgcp_client_handle_response(mgcp, pending, NULL);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200918 return -1;
919}
920
Philipp Maier275ac972018-01-20 00:58:16 +0100921/*! Cancel a pending transaction.
922 * \param[in] mgcp MGCP client descriptor.
923 * \param[in,out] trans_id Transaction id.
924 * \returns 0 on success, -ENOENT on error.
925 *
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100926 * Should a priv pointer passed to mgcp_client_tx() become invalid, this function must be called. In
927 * practical terms, if the caller of mgcp_client_tx() wishes to tear down a transaction without having
928 * received a response this function must be called. The trans_id can be obtained by calling
Philipp Maier275ac972018-01-20 00:58:16 +0100929 * mgcp_msg_trans_id() on the msgb produced by mgcp_msg_gen(). */
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100930int mgcp_client_cancel(struct mgcp_client *mgcp, mgcp_trans_id_t trans_id)
931{
932 struct mgcp_response_pending *pending = mgcp_client_response_pending_get(mgcp, trans_id);
933 if (!pending) {
Philipp Maier275ac972018-01-20 00:58:16 +0100934 /*! Note: it is not harmful to cancel a transaction twice. */
Neels Hofmeyred0c1aa2018-12-21 03:07:53 +0100935 LOGP(DLMGCP, LOGL_ERROR, "Cannot cancel, no such transaction: %u\n", trans_id);
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100936 return -ENOENT;
937 }
Neels Hofmeyred0c1aa2018-12-21 03:07:53 +0100938 LOGP(DLMGCP, LOGL_DEBUG, "Canceled transaction %u\n", trans_id);
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100939 talloc_free(pending);
940 return 0;
Philipp Maier275ac972018-01-20 00:58:16 +0100941 /*! We don't really need to clean up the wqueue: In all sane cases, the msgb has already been sent
942 * out and is no longer in the wqueue. If it still is in the wqueue, then sending MGCP messages
943 * per se is broken and the program should notice so by a full wqueue. Even if this was called
944 * before we had a chance to send out the message and it is still going to be sent, we will just
945 * ignore the reply to it later. Removing a msgb from the wqueue here would just introduce more
946 * bug surface in terms of failing to update wqueue API's counters or some such.
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100947 */
948}
949
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200950static mgcp_trans_id_t mgcp_client_next_trans_id(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200951{
952 /* avoid zero trans_id to distinguish from unset trans_id */
953 if (!mgcp->next_trans_id)
954 mgcp->next_trans_id ++;
955 return mgcp->next_trans_id ++;
956}
957
Philipp Maier1dc6be62017-10-05 18:25:37 +0200958#define MGCP_CRCX_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT | \
959 MGCP_MSG_PRESENCE_CALL_ID | \
Philipp Maier1dc6be62017-10-05 18:25:37 +0200960 MGCP_MSG_PRESENCE_CONN_MODE)
961#define MGCP_MDCX_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT | \
Philipp Maier490cbaa2018-01-22 17:32:38 +0100962 MGCP_MSG_PRESENCE_CALL_ID | \
Philipp Maier1dc6be62017-10-05 18:25:37 +0200963 MGCP_MSG_PRESENCE_CONN_ID)
964#define MGCP_DLCX_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT)
965#define MGCP_AUEP_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT)
966#define MGCP_RSIP_MANDATORY 0 /* none */
967
Philipp Maier704c4f02018-06-07 18:51:31 +0200968/* Helper function for mgcp_msg_gen(): Add LCO information to MGCP message */
969static int add_lco(struct msgb *msg, struct mgcp_msg *mgcp_msg)
970{
971 unsigned int i;
972 int rc = 0;
973 const char *codec;
974 unsigned int pt;
975
976 rc += msgb_printf(msg, "L:");
977
978 if (mgcp_msg->ptime)
979 rc += msgb_printf(msg, " p:%u,", mgcp_msg->ptime);
980
981 if (mgcp_msg->codecs_len) {
982 rc += msgb_printf(msg, " a:");
983 for (i = 0; i < mgcp_msg->codecs_len; i++) {
984 pt = mgcp_msg->codecs[i];
Neels Hofmeyr47642f22019-02-27 05:56:53 +0100985 codec = get_value_string_or_null(osmo_mgcpc_codec_names, pt);
Pau Espin Pedrolc12bfb72019-04-16 17:23:09 +0200986
Philipp Maier704c4f02018-06-07 18:51:31 +0200987 /* Note: Use codec descriptors from enum mgcp_codecs
988 * in mgcp_client only! */
989 OSMO_ASSERT(codec);
990 rc += msgb_printf(msg, "%s", extract_codec_name(codec));
991 if (i < mgcp_msg->codecs_len - 1)
992 rc += msgb_printf(msg, ";");
993 }
994 rc += msgb_printf(msg, ",");
995 }
996
997 rc += msgb_printf(msg, " nt:IN\r\n");
998
999 return rc;
1000}
1001
1002/* Helper function for mgcp_msg_gen(): Add SDP information to MGCP message */
1003static int add_sdp(struct msgb *msg, struct mgcp_msg *mgcp_msg, struct mgcp_client *mgcp)
1004{
1005 unsigned int i;
1006 int rc = 0;
1007 char local_ip[INET_ADDRSTRLEN];
1008 const char *codec;
1009 unsigned int pt;
1010
1011 /* Add separator to mark the beginning of the SDP block */
1012 rc += msgb_printf(msg, "\r\n");
1013
1014 /* Add SDP protocol version */
1015 rc += msgb_printf(msg, "v=0\r\n");
1016
1017 /* Determine local IP-Address */
1018 if (osmo_sock_local_ip(local_ip, mgcp->actual.remote_addr) < 0) {
1019 LOGP(DLMGCP, LOGL_ERROR,
1020 "Could not determine local IP-Address!\n");
1021 msgb_free(msg);
1022 return -2;
1023 }
1024
1025 /* Add owner/creator (SDP) */
1026 rc += msgb_printf(msg, "o=- %x 23 IN IP4 %s\r\n",
1027 mgcp_msg->call_id, local_ip);
1028
1029 /* Add session name (none) */
1030 rc += msgb_printf(msg, "s=-\r\n");
1031
1032 /* Add RTP address and port */
1033 if (mgcp_msg->audio_port == 0) {
1034 LOGP(DLMGCP, LOGL_ERROR,
1035 "Invalid port number, can not generate MGCP message\n");
1036 msgb_free(msg);
1037 return -2;
1038 }
1039 if (strlen(mgcp_msg->audio_ip) <= 0) {
1040 LOGP(DLMGCP, LOGL_ERROR,
1041 "Empty ip address, can not generate MGCP message\n");
1042 msgb_free(msg);
1043 return -2;
1044 }
1045 rc += msgb_printf(msg, "c=IN IP4 %s\r\n", mgcp_msg->audio_ip);
1046
1047 /* Add time description, active time (SDP) */
1048 rc += msgb_printf(msg, "t=0 0\r\n");
1049
1050 rc += msgb_printf(msg, "m=audio %u RTP/AVP", mgcp_msg->audio_port);
1051 for (i = 0; i < mgcp_msg->codecs_len; i++) {
1052 pt = map_codec_to_pt(mgcp_msg->ptmap, mgcp_msg->ptmap_len, mgcp_msg->codecs[i]);
1053 rc += msgb_printf(msg, " %u", pt);
1054
1055 }
1056 rc += msgb_printf(msg, "\r\n");
1057
Philipp Maier228e5912019-03-05 13:56:59 +01001058 /* Add optional codec parameters (fmtp) */
1059 if (mgcp_msg->param_present) {
1060 for (i = 0; i < mgcp_msg->codecs_len; i++) {
1061 /* The following is only applicable for AMR */
1062 if (mgcp_msg->codecs[i] != CODEC_AMR_8000_1 && mgcp_msg->codecs[i] != CODEC_AMRWB_16000_1)
1063 continue;
1064 pt = map_codec_to_pt(mgcp_msg->ptmap, mgcp_msg->ptmap_len, mgcp_msg->codecs[i]);
1065 if (mgcp_msg->param.amr_octet_aligned_present && mgcp_msg->param.amr_octet_aligned)
1066 rc += msgb_printf(msg, "a=fmtp:%u octet-align=1\r\n", pt);
1067 else if (mgcp_msg->param.amr_octet_aligned_present && !mgcp_msg->param.amr_octet_aligned)
1068 rc += msgb_printf(msg, "a=fmtp:%u octet-align=0\r\n", pt);
1069 }
1070 }
1071
Philipp Maier704c4f02018-06-07 18:51:31 +02001072 for (i = 0; i < mgcp_msg->codecs_len; i++) {
1073 pt = map_codec_to_pt(mgcp_msg->ptmap, mgcp_msg->ptmap_len, mgcp_msg->codecs[i]);
Pau Espin Pedrolc12bfb72019-04-16 17:23:09 +02001074
Philipp Maier704c4f02018-06-07 18:51:31 +02001075 /* Note: Only dynamic payload type from the range 96-127
1076 * require to be explained further via rtpmap. All others
1077 * are implcitly definedby the number in m=audio */
1078 if (pt >= 96 && pt <= 127) {
Neels Hofmeyr47642f22019-02-27 05:56:53 +01001079 codec = get_value_string_or_null(osmo_mgcpc_codec_names, mgcp_msg->codecs[i]);
Philipp Maier704c4f02018-06-07 18:51:31 +02001080
1081 /* Note: Use codec descriptors from enum mgcp_codecs
1082 * in mgcp_client only! */
1083 OSMO_ASSERT(codec);
Pau Espin Pedrolc12bfb72019-04-16 17:23:09 +02001084
Philipp Maier704c4f02018-06-07 18:51:31 +02001085 rc += msgb_printf(msg, "a=rtpmap:%u %s\r\n", pt, codec);
1086 }
1087 }
Pau Espin Pedrolc12bfb72019-04-16 17:23:09 +02001088
Philipp Maier704c4f02018-06-07 18:51:31 +02001089 if (mgcp_msg->ptime)
1090 rc += msgb_printf(msg, "a=ptime:%u\r\n", mgcp_msg->ptime);
1091
1092 return rc;
1093}
1094
Philipp Maier275ac972018-01-20 00:58:16 +01001095/*! Generate an MGCP message
1096 * \param[in] mgcp MGCP client descriptor.
1097 * \param[in] mgcp_msg Message description
1098 * \returns message buffer on success, NULL on error. */
Philipp Maier1dc6be62017-10-05 18:25:37 +02001099struct msgb *mgcp_msg_gen(struct mgcp_client *mgcp, struct mgcp_msg *mgcp_msg)
1100{
1101 mgcp_trans_id_t trans_id = mgcp_client_next_trans_id(mgcp);
1102 uint32_t mandatory_mask;
1103 struct msgb *msg = msgb_alloc_headroom(4096, 128, "MGCP tx");
1104 int rc = 0;
Philipp Maier704c4f02018-06-07 18:51:31 +02001105 int rc_sdp;
1106 bool use_sdp = false;
Philipp Maier1dc6be62017-10-05 18:25:37 +02001107
1108 msg->l2h = msg->data;
1109 msg->cb[MSGB_CB_MGCP_TRANS_ID] = trans_id;
1110
1111 /* Add command verb */
1112 switch (mgcp_msg->verb) {
1113 case MGCP_VERB_CRCX:
1114 mandatory_mask = MGCP_CRCX_MANDATORY;
1115 rc += msgb_printf(msg, "CRCX %u", trans_id);
1116 break;
1117 case MGCP_VERB_MDCX:
1118 mandatory_mask = MGCP_MDCX_MANDATORY;
1119 rc += msgb_printf(msg, "MDCX %u", trans_id);
1120 break;
1121 case MGCP_VERB_DLCX:
1122 mandatory_mask = MGCP_DLCX_MANDATORY;
1123 rc += msgb_printf(msg, "DLCX %u", trans_id);
1124 break;
1125 case MGCP_VERB_AUEP:
1126 mandatory_mask = MGCP_AUEP_MANDATORY;
1127 rc += msgb_printf(msg, "AUEP %u", trans_id);
1128 break;
1129 case MGCP_VERB_RSIP:
1130 mandatory_mask = MGCP_RSIP_MANDATORY;
1131 rc += msgb_printf(msg, "RSIP %u", trans_id);
1132 break;
1133 default:
1134 LOGP(DLMGCP, LOGL_ERROR,
1135 "Invalid command verb, can not generate MGCP message\n");
1136 msgb_free(msg);
1137 return NULL;
1138 }
1139
1140 /* Check if mandatory fields are missing */
1141 if (!((mgcp_msg->presence & mandatory_mask) == mandatory_mask)) {
1142 LOGP(DLMGCP, LOGL_ERROR,
1143 "One or more missing mandatory fields, can not generate MGCP message\n");
1144 msgb_free(msg);
1145 return NULL;
1146 }
1147
1148 /* Add endpoint name */
Philipp Maier7bc55522017-12-10 22:52:22 +01001149 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_ENDPOINT) {
1150 if (strlen(mgcp_msg->endpoint) <= 0) {
1151 LOGP(DLMGCP, LOGL_ERROR,
1152 "Empty endpoint name, can not generate MGCP message\n");
1153 msgb_free(msg);
1154 return NULL;
1155 }
Philipp Maier3aa81572018-01-31 14:13:45 +01001156
1157 if (strstr(mgcp_msg->endpoint, "@") == NULL) {
1158 LOGP(DLMGCP, LOGL_ERROR,
1159 "Endpoint name (%s) lacks separator (@), can not generate MGCP message\n",
1160 mgcp_msg->endpoint);
1161 msgb_free(msg);
1162 }
1163
Philipp Maier1dc6be62017-10-05 18:25:37 +02001164 rc += msgb_printf(msg, " %s", mgcp_msg->endpoint);
Philipp Maier7bc55522017-12-10 22:52:22 +01001165 }
Philipp Maier1dc6be62017-10-05 18:25:37 +02001166
1167 /* Add protocol version */
1168 rc += msgb_printf(msg, " MGCP 1.0\r\n");
1169
1170 /* Add call id */
1171 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_CALL_ID)
1172 rc += msgb_printf(msg, "C: %x\r\n", mgcp_msg->call_id);
1173
1174 /* Add connection id */
Philipp Maier7bc55522017-12-10 22:52:22 +01001175 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_CONN_ID) {
1176 if (strlen(mgcp_msg->conn_id) <= 0) {
1177 LOGP(DLMGCP, LOGL_ERROR,
1178 "Empty connection id, can not generate MGCP message\n");
1179 msgb_free(msg);
1180 return NULL;
1181 }
Philipp Maier01d24a32017-11-21 17:26:09 +01001182 rc += msgb_printf(msg, "I: %s\r\n", mgcp_msg->conn_id);
Philipp Maier7bc55522017-12-10 22:52:22 +01001183 }
Philipp Maier1dc6be62017-10-05 18:25:37 +02001184
Philipp Maier704c4f02018-06-07 18:51:31 +02001185 /* Using SDP makes sense when a valid IP/Port combination is specifiec,
1186 * if we do not know this information yet, we fall back to LCO */
1187 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_AUDIO_IP
1188 && mgcp_msg->presence & MGCP_MSG_PRESENCE_AUDIO_PORT)
1189 use_sdp = true;
1190
1191 /* Add local connection options (LCO) */
1192 if (!use_sdp
1193 && (mgcp_msg->verb == MGCP_VERB_CRCX
1194 || mgcp_msg->verb == MGCP_VERB_MDCX))
1195 rc += add_lco(msg, mgcp_msg);
Philipp Maier1dc6be62017-10-05 18:25:37 +02001196
1197 /* Add mode */
1198 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_CONN_MODE)
1199 rc +=
1200 msgb_printf(msg, "M: %s\r\n",
1201 mgcp_client_cmode_name(mgcp_msg->conn_mode));
1202
Neels Hofmeyre6d8e912018-08-23 16:36:48 +02001203 /* Add X-Osmo-IGN */
1204 if ((mgcp_msg->presence & MGCP_MSG_PRESENCE_X_OSMO_IGN)
1205 && (mgcp_msg->x_osmo_ign != 0))
1206 rc +=
1207 msgb_printf(msg, MGCP_X_OSMO_IGN_HEADER "%s\r\n",
1208 mgcp_msg->x_osmo_ign & MGCP_X_OSMO_IGN_CALLID ? " C": "");
1209
Philipp Maier704c4f02018-06-07 18:51:31 +02001210 /* Add session description protocol (SDP) */
1211 if (use_sdp
1212 && (mgcp_msg->verb == MGCP_VERB_CRCX
1213 || mgcp_msg->verb == MGCP_VERB_MDCX)) {
1214 rc_sdp = add_sdp(msg, mgcp_msg, mgcp);
1215 if (rc_sdp == -2)
Philipp Maier9d25d7a2018-01-22 17:31:10 +01001216 return NULL;
Philipp Maier704c4f02018-06-07 18:51:31 +02001217 else
1218 rc += rc_sdp;
Philipp Maier1dc6be62017-10-05 18:25:37 +02001219 }
1220
1221 if (rc != 0) {
1222 LOGP(DLMGCP, LOGL_ERROR,
1223 "message buffer to small, can not generate MGCP message\n");
1224 msgb_free(msg);
1225 msg = NULL;
1226 }
1227
1228 return msg;
1229}
1230
Philipp Maier275ac972018-01-20 00:58:16 +01001231/*! Retrieve the MGCP transaction ID from a msgb generated by mgcp_msg_gen()
1232 * \param[in] msg message buffer
1233 * \returns Transaction id. */
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +01001234mgcp_trans_id_t mgcp_msg_trans_id(struct msgb *msg)
1235{
1236 return (mgcp_trans_id_t)msg->cb[MSGB_CB_MGCP_TRANS_ID];
1237}
1238
Philipp Maier275ac972018-01-20 00:58:16 +01001239/*! Get the configuration parameters a given MGCP client instance
1240 * \param[in] mgcp MGCP client descriptor.
1241 * \returns configuration */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +02001242struct mgcp_client_conf *mgcp_client_conf_actual(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001243{
1244 return &mgcp->actual;
1245}
Neels Hofmeyrd95ab1e2017-09-22 00:52:54 +02001246
1247const struct value_string mgcp_client_connection_mode_strs[] = {
1248 { MGCP_CONN_NONE, "none" },
1249 { MGCP_CONN_RECV_SEND, "sendrecv" },
1250 { MGCP_CONN_SEND_ONLY, "sendonly" },
1251 { MGCP_CONN_RECV_ONLY, "recvonly" },
1252 { MGCP_CONN_LOOPBACK, "loopback" },
1253 { 0, NULL }
1254};