blob: fc9c5d3dc854f34daa5cd1487315369ad06589aa [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) */
40static const struct value_string codec_table[] = {
41 { 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
84 for (i = 0; i < ARRAY_SIZE(codec_table); i++) {
85 codec_name = extract_codec_name(codec_table[i].str);
86 if (!codec_name)
87 continue;
88 if (strcmp(codec_name, str_buf) == 0)
89 return codec_table[i].value;
90 }
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,
193 .first_endpoint = 0,
194 .last_endpoint = 0,
Neels Hofmeyrc0dcc3c2017-12-02 18:31:34 +0000195 .bts_base = 0,
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200196 };
197}
198
199/* Test if a given endpoint id is currently in use */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200200static bool endpoint_in_use(uint16_t id, struct mgcp_client *client)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200201{
202 struct mgcp_inuse_endpoint *endpoint;
203 llist_for_each_entry(endpoint, &client->inuse_endpoints, entry) {
204 if (endpoint->id == id)
205 return true;
206 }
207
208 return false;
209}
210
Philipp Maier275ac972018-01-20 00:58:16 +0100211/*! Pick next free endpoint ID.
212 * \param[in,out] client MGCP client descriptor.
213 * \returns 0 on success, -EINVAL on error. */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200214int mgcp_client_next_endpoint(struct mgcp_client *client)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200215{
216 int i;
217 uint16_t first_endpoint = client->actual.first_endpoint;
218 uint16_t last_endpoint = client->actual.last_endpoint;
219 struct mgcp_inuse_endpoint *endpoint;
220
221 /* Use the maximum permitted range if the VTY
222 * configuration does not specify a range */
223 if (client->actual.last_endpoint == 0) {
224 first_endpoint = 1;
225 last_endpoint = 65534;
226 }
227
228 /* Test the permitted endpoint range for an endpoint
229 * number that is not in use. When a suitable endpoint
230 * number can be found, seize it by adding it to the
231 * inuse list. */
232 for (i=first_endpoint;i<last_endpoint;i++)
233 {
234 if (endpoint_in_use(i,client) == false) {
235 endpoint = talloc_zero(client, struct mgcp_inuse_endpoint);
236 endpoint->id = i;
237 llist_add_tail(&endpoint->entry, &client->inuse_endpoints);
238 return endpoint->id;
239 }
240 }
241
242 /* All endpoints are busy! */
243 return -EINVAL;
244}
245
Philipp Maier275ac972018-01-20 00:58:16 +0100246/*! Release a seized endpoint ID to make it available again for other calls.
247 * \param[in] id Endpoint ID
248 * \param[in,out] client MGCP client descriptor. */
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200249/* Release a seized endpoint id to make it available again for other calls */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200250void mgcp_client_release_endpoint(uint16_t id, struct mgcp_client *client)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200251{
252 struct mgcp_inuse_endpoint *endpoint;
253 struct mgcp_inuse_endpoint *endpoint_tmp;
254 llist_for_each_entry_safe(endpoint, endpoint_tmp, &client->inuse_endpoints, entry) {
255 if (endpoint->id == id) {
256 llist_del(&endpoint->entry);
257 talloc_free(endpoint);
258 }
259 }
260}
261
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200262static void mgcp_client_handle_response(struct mgcp_client *mgcp,
263 struct mgcp_response_pending *pending,
264 struct mgcp_response *response)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200265{
266 if (!pending) {
267 LOGP(DLMGCP, LOGL_ERROR,
268 "Cannot handle NULL response\n");
269 return;
270 }
271 if (pending->response_cb)
272 pending->response_cb(response, pending->priv);
273 else
274 LOGP(DLMGCP, LOGL_INFO, "MGCP response ignored (NULL cb)\n");
275 talloc_free(pending);
276}
277
278static int mgcp_response_parse_head(struct mgcp_response *r, struct msgb *msg)
279{
280 int comment_pos;
281 char *end;
282
283 if (mgcp_msg_terminate_nul(msg))
284 goto response_parse_failure;
285
286 r->body = (char *)msg->data;
287
Harald Welte9bf7c532017-11-17 14:14:31 +0100288 if (sscanf(r->body, "%3d %u %n",
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200289 &r->head.response_code, &r->head.trans_id,
290 &comment_pos) != 2)
291 goto response_parse_failure;
292
Philipp Maierabe8c892018-01-20 00:15:12 +0100293 osmo_strlcpy(r->head.comment, r->body + comment_pos, sizeof(r->head.comment));
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200294 end = strchr(r->head.comment, '\r');
295 if (!end)
296 goto response_parse_failure;
297 /* Mark the end of the comment */
298 *end = '\0';
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200299 return 0;
300
301response_parse_failure:
302 LOGP(DLMGCP, LOGL_ERROR,
303 "Failed to parse MGCP response header\n");
304 return -EINVAL;
305}
306
307/* TODO undup against mgcp_protocol.c:mgcp_check_param() */
308static bool mgcp_line_is_valid(const char *line)
309{
310 const size_t line_len = strlen(line);
311 if (line[0] == '\0')
312 return true;
313
314 if (line_len < 2
315 || line[1] != '=') {
316 LOGP(DLMGCP, LOGL_ERROR,
317 "Wrong MGCP option format: '%s'\n",
318 line);
319 return false;
320 }
321
322 return true;
323}
324
Philipp Maier704c4f02018-06-07 18:51:31 +0200325/* Parse a line like "m=audio 16002 RTP/AVP 98", extract port and payload types */
326static int mgcp_parse_audio_port_pt(struct mgcp_response *r, char *line)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200327{
Philipp Maier704c4f02018-06-07 18:51:31 +0200328 char *pt_str;
329 unsigned int pt;
330 unsigned int count = 0;
331 unsigned int i;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200332
Philipp Maier704c4f02018-06-07 18:51:31 +0200333 /* Extract port information */
334 if (sscanf(line, "m=audio %hu", &r->audio_port) != 1)
335 goto response_parse_failure_port;
Philipp Maier10f32db2017-12-13 12:34:34 +0100336 if (r->audio_port == 0)
Philipp Maier704c4f02018-06-07 18:51:31 +0200337 goto response_parse_failure_port;
Philipp Maier10f32db2017-12-13 12:34:34 +0100338
Philipp Maier704c4f02018-06-07 18:51:31 +0200339 /* Extract payload types */
340 line = strstr(line, "RTP/AVP ");
341 if (!line)
342 goto exit;
343
344 pt_str = strtok(line, " ");
345 while (1) {
346 /* Do not allow excessive payload types */
347 if (count > ARRAY_SIZE(r->codecs))
348 goto response_parse_failure_pt;
349
350 pt_str = strtok(NULL, " ");
351 if (!pt_str)
352 break;
353 pt = atoi(pt_str);
354
355 /* Do not allow duplicate payload types */
356 for (i = 0; i < count; i++)
357 if (r->codecs[i] == pt)
358 goto response_parse_failure_pt;
359
360 /* Note: The payload type we store may not necessarly match
361 * the codec types we have defined in enum mgcp_codecs. To
362 * ensure that the end result only contains codec types which
363 * match enum mgcp_codecs, we will go through afterwards and
364 * remap the affected entries with the inrofmation we learn
365 * from rtpmap */
366 r->codecs[count] = pt;
367 count++;
368 }
369
370 r->codecs_len = count;
371
372exit:
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200373 return 0;
374
Philipp Maier704c4f02018-06-07 18:51:31 +0200375response_parse_failure_port:
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200376 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier704c4f02018-06-07 18:51:31 +0200377 "Failed to parse SDP parameter port (%s)\n", line);
Philipp Maier06da85e2017-10-05 18:49:24 +0200378 return -EINVAL;
Philipp Maier704c4f02018-06-07 18:51:31 +0200379
380response_parse_failure_pt:
381 LOGP(DLMGCP, LOGL_ERROR,
382 "Failed to parse SDP parameter payload types (%s)\n", line);
383 return -EINVAL;
384}
385
386/* Parse a line like "m=audio 16002 RTP/AVP 98", extract port and payload types */
387static int mgcp_parse_audio_ptime_rtpmap(struct mgcp_response *r, const char *line)
388{
389 unsigned int pt;
390 char codec_resp[64];
391 unsigned int codec;
392
393
394 if (strstr(line, "ptime")) {
395 if (sscanf(line, "a=ptime:%u", &r->ptime) != 1)
396 goto response_parse_failure_ptime;
397 } else if (strstr(line, "rtpmap")) {
398 if (sscanf(line, "a=rtpmap:%d %63s", &pt, codec_resp) == 2) {
399 /* The MGW may assign an own payload type in the
400 * response if the choosen codec falls into the IANA
401 * assigned dynamic payload type range (96-127).
402 * Normally the MGW should obey the 3gpp payload type
403 * assignments, which are fixed, so we likely wont see
404 * anything unexpected here. In order to be sure that
405 * we will now check the codec string and if the result
406 * does not match to what is IANA / 3gpp assigned, we
407 * will create an entry in the ptmap table so we can
408 * lookup later what has been assigned. */
409 codec = map_str_to_codec(codec_resp);
410 if (codec != pt) {
411 if (r->ptmap_len < ARRAY_SIZE(r->ptmap)) {
412 r->ptmap[r->ptmap_len].pt = pt;
413 r->ptmap[r->ptmap_len].codec = codec;
414 r->ptmap_len++;
415 } else
416 goto response_parse_failure_rtpmap;
417 }
418
419 } else
420 goto response_parse_failure_rtpmap;
421 }
422
423 return 0;
424
425response_parse_failure_ptime:
426 LOGP(DLMGCP, LOGL_ERROR,
427 "Failed to parse SDP parameter, invalid ptime (%s)\n", line);
428 return -EINVAL;
429response_parse_failure_rtpmap:
430 LOGP(DLMGCP, LOGL_ERROR,
431 "Failed to parse SDP parameter, invalid rtpmap (%s)\n", line);
432 return -EINVAL;
Philipp Maier06da85e2017-10-05 18:49:24 +0200433}
434
435/* Parse a line like "c=IN IP4 10.11.12.13" */
436static int mgcp_parse_audio_ip(struct mgcp_response *r, const char *line)
437{
438 struct in_addr ip_test;
439
440 if (strlen(line) < 16)
441 goto response_parse_failure;
442
443 /* The current implementation strictly supports IPV4 only ! */
444 if (memcmp("c=IN IP4 ", line, 9) != 0)
445 goto response_parse_failure;
446
447 /* Extract IP-Address */
Philipp Maierf8bfbe82017-11-23 19:32:31 +0100448 osmo_strlcpy(r->audio_ip, line + 9, sizeof(r->audio_ip));
Philipp Maier06da85e2017-10-05 18:49:24 +0200449
450 /* Check IP-Address */
451 if (inet_aton(r->audio_ip, &ip_test) == 0)
452 goto response_parse_failure;
453
454 return 0;
455
456response_parse_failure:
457 LOGP(DLMGCP, LOGL_ERROR,
458 "Failed to parse MGCP response header (audio ip)\n");
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200459 return -EINVAL;
460}
461
Philipp Maier3b12e1b2018-01-18 15:16:13 +0100462/* A new section is marked by a double line break, check a few more
463 * patterns as there may be variants */
464static char *mgcp_find_section_end(char *string)
465{
466 char *rc;
467
468 rc = strstr(string, "\n\n");
469 if (rc)
470 return rc;
471
472 rc = strstr(string, "\n\r\n\r");
473 if (rc)
474 return rc;
475
476 rc = strstr(string, "\r\n\r\n");
477 if (rc)
478 return rc;
479
480 return NULL;
481}
482
Philipp Maier275ac972018-01-20 00:58:16 +0100483/*! Parse body (SDP) parameters of the MGCP response
484 * \param[in,out] r Response data
485 * \returns 0 on success, -EINVAL on error. */
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200486int mgcp_response_parse_params(struct mgcp_response *r)
487{
488 char *line;
489 int rc;
Neels Hofmeyr0793d2f2018-02-21 14:55:34 +0100490 char *data;
Philipp Maiere9d645b2018-01-19 23:54:08 +0100491 char *data_ptr;
Philipp Maier704c4f02018-06-07 18:51:31 +0200492 int i;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200493
Philipp Maiere9d645b2018-01-19 23:54:08 +0100494 /* Since this functions performs a destructive parsing, we create a
495 * local copy of the body data */
Neels Hofmeyr0793d2f2018-02-21 14:55:34 +0100496 OSMO_ASSERT(r->body);
497 data = talloc_strdup(r, r->body);
Philipp Maiere9d645b2018-01-19 23:54:08 +0100498 OSMO_ASSERT(data);
Philipp Maier55295f72018-01-15 14:00:28 +0100499
Philipp Maiere9d645b2018-01-19 23:54:08 +0100500 /* Find beginning of the parameter (SDP) section */
501 data_ptr = mgcp_find_section_end(data);
Neels Hofmeyr10835332018-02-21 14:55:34 +0100502 if (!data_ptr) {
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200503 LOGP(DLMGCP, LOGL_ERROR,
Neels Hofmeyr0793d2f2018-02-21 14:55:34 +0100504 "MGCP response: cannot find start of SDP parameters\n");
Philipp Maiere9d645b2018-01-19 23:54:08 +0100505 rc = -EINVAL;
506 goto exit;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200507 }
508
Neels Hofmeyr0793d2f2018-02-21 14:55:34 +0100509 /* data_ptr now points to the beginning of the section-end-marker; for_each_non_empty_line()
510 * skips any \r and \n characters for free, so we don't need to skip the marker. */
511
Philipp Maiere9d645b2018-01-19 23:54:08 +0100512 for_each_non_empty_line(line, data_ptr) {
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200513 if (!mgcp_line_is_valid(line))
514 return -EINVAL;
515
516 switch (line[0]) {
Philipp Maier704c4f02018-06-07 18:51:31 +0200517 case 'a':
518 rc = mgcp_parse_audio_ptime_rtpmap(r, line);
519 if (rc)
520 goto exit;
521 break;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200522 case 'm':
Philipp Maier704c4f02018-06-07 18:51:31 +0200523 rc = mgcp_parse_audio_port_pt(r, line);
Philipp Maier06da85e2017-10-05 18:49:24 +0200524 if (rc)
Philipp Maiere9d645b2018-01-19 23:54:08 +0100525 goto exit;
Philipp Maier06da85e2017-10-05 18:49:24 +0200526 break;
527 case 'c':
528 rc = mgcp_parse_audio_ip(r, line);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200529 if (rc)
Philipp Maiere9d645b2018-01-19 23:54:08 +0100530 goto exit;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200531 break;
532 default:
533 /* skip unhandled parameters */
534 break;
535 }
536 }
Philipp Maiere9d645b2018-01-19 23:54:08 +0100537
Philipp Maier704c4f02018-06-07 18:51:31 +0200538 /* See also note in mgcp_parse_audio_port_pt() */
539 for (i = 0; i < r->codecs_len; i++)
540 r->codecs[i] = map_pt_to_codec(r->ptmap, r->ptmap_len, r->codecs[i]);
541
Philipp Maiere9d645b2018-01-19 23:54:08 +0100542 rc = 0;
543exit:
544 talloc_free(data);
545 return rc;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200546}
547
Philipp Maier55295f72018-01-15 14:00:28 +0100548/* Parse a line like "X: something" */
549static int mgcp_parse_head_param(char *result, unsigned int result_len,
550 char label, const char *line)
Philipp Maierffd75e42017-11-22 11:44:50 +0100551{
Philipp Maier55295f72018-01-15 14:00:28 +0100552 char label_string[4];
Neels Hofmeyr23e7bf12018-09-03 21:26:22 +0200553 size_t rc;
Philipp Maier55295f72018-01-15 14:00:28 +0100554
555 /* Detect empty parameters */
Philipp Maierffd75e42017-11-22 11:44:50 +0100556 if (strlen(line) < 4)
557 goto response_parse_failure;
558
Philipp Maier55295f72018-01-15 14:00:28 +0100559 /* Check if the label matches */
560 snprintf(label_string, sizeof(label_string), "%c: ", label);
561 if (memcmp(label_string, line, 3) != 0)
Philipp Maierffd75e42017-11-22 11:44:50 +0100562 goto response_parse_failure;
563
Philipp Maier55295f72018-01-15 14:00:28 +0100564 /* Copy payload part of the string to destinations (the label string
565 * is always 3 chars long) */
Neels Hofmeyr23e7bf12018-09-03 21:26:22 +0200566 rc = osmo_strlcpy(result, line + 3, result_len);
567 if (rc >= result_len) {
568 LOGP(DLMGCP, LOGL_ERROR,
569 "Failed to parse MGCP response (parameter label: %c):"
570 " the received conn ID is too long: %zu, maximum is %u characters\n",
571 label, rc, result_len - 1);
572 return -ENOSPC;
573 }
Philipp Maierffd75e42017-11-22 11:44:50 +0100574 return 0;
575
576response_parse_failure:
577 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier55295f72018-01-15 14:00:28 +0100578 "Failed to parse MGCP response (parameter label: %c)\n", label);
Philipp Maierffd75e42017-11-22 11:44:50 +0100579 return -EINVAL;
580}
581
582/* Parse MGCP parameters of the response */
583static int parse_head_params(struct mgcp_response *r)
584{
585 char *line;
586 int rc = 0;
587 OSMO_ASSERT(r->body);
Philipp Maier55295f72018-01-15 14:00:28 +0100588 char *data;
589 char *data_ptr;
590 char *data_end;
Philipp Maierffd75e42017-11-22 11:44:50 +0100591
Philipp Maier55295f72018-01-15 14:00:28 +0100592 /* Since this functions performs a destructive parsing, we create a
593 * local copy of the body data */
Philipp Maier1fc69992018-01-31 15:32:55 +0100594 data = talloc_zero_size(r, strlen(r->body)+1);
Philipp Maier55295f72018-01-15 14:00:28 +0100595 OSMO_ASSERT(data);
596 data_ptr = data;
597 osmo_strlcpy(data, r->body, strlen(r->body));
598
599 /* If there is an SDP body attached, prevent for_each_non_empty_line()
600 * into running in there, we are not yet interested in the parameters
601 * stored there. */
602 data_end = mgcp_find_section_end(data);
Philipp Maierffd75e42017-11-22 11:44:50 +0100603 if (data_end)
604 *data_end = '\0';
605
Philipp Maier55295f72018-01-15 14:00:28 +0100606 for_each_non_empty_line(line, data_ptr) {
Philipp Maierffd75e42017-11-22 11:44:50 +0100607 switch (line[0]) {
Philipp Maier55295f72018-01-15 14:00:28 +0100608 case 'Z':
609 rc = mgcp_parse_head_param(r->head.endpoint,
610 sizeof(r->head.endpoint),
611 'Z', line);
612 if (rc)
613 goto exit;
Philipp Maier771b26a2018-01-31 13:53:11 +0100614
615 /* A specific endpoint identifier returned by the MGW
616 * must not contain any wildcard characters */
617 if (strstr(r->head.endpoint, "*") != NULL) {
618 rc = -EINVAL;
619 goto exit;
620 }
Philipp Maier3261dc72018-01-31 14:03:13 +0100621
622 /* A specific endpoint identifier returned by the MGW
623 * must contain an @ character */
624 if (strstr(r->head.endpoint, "@") == NULL) {
625 rc = -EINVAL;
626 goto exit;
627 }
Philipp Maier55295f72018-01-15 14:00:28 +0100628 break;
Philipp Maierffd75e42017-11-22 11:44:50 +0100629 case 'I':
Philipp Maier55295f72018-01-15 14:00:28 +0100630 rc = mgcp_parse_head_param(r->head.conn_id,
631 sizeof(r->head.conn_id),
632 'I', line);
Philipp Maierffd75e42017-11-22 11:44:50 +0100633 if (rc)
634 goto exit;
635 break;
636 default:
637 /* skip unhandled parameters */
638 break;
639 }
640 }
641exit:
Philipp Maier55295f72018-01-15 14:00:28 +0100642 talloc_free(data);
Philipp Maierffd75e42017-11-22 11:44:50 +0100643 return rc;
644}
645
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200646static struct mgcp_response_pending *mgcp_client_response_pending_get(
647 struct mgcp_client *mgcp,
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100648 mgcp_trans_id_t trans_id)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200649{
650 struct mgcp_response_pending *pending;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200651 llist_for_each_entry(pending, &mgcp->responses_pending, entry) {
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100652 if (pending->trans_id == trans_id) {
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200653 llist_del(&pending->entry);
654 return pending;
655 }
656 }
657 return NULL;
658}
659
660/* Feed an MGCP message into the receive processing.
661 * Parse the head and call any callback registered for the transaction id found
662 * in the MGCP message. This is normally called directly from the internal
663 * mgcp_do_read that reads from the socket connected to the MGCP gateway. This
664 * function is published mainly to be able to feed data from the test suite.
665 */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200666int mgcp_client_rx(struct mgcp_client *mgcp, struct msgb *msg)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200667{
Philipp Maier1fc69992018-01-31 15:32:55 +0100668 struct mgcp_response *r;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200669 struct mgcp_response_pending *pending;
670 int rc;
671
Philipp Maier1fc69992018-01-31 15:32:55 +0100672 r = talloc_zero(mgcp, struct mgcp_response);
673 OSMO_ASSERT(r);
674
675 rc = mgcp_response_parse_head(r, msg);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200676 if (rc) {
Philipp Maierffd75e42017-11-22 11:44:50 +0100677 LOGP(DLMGCP, LOGL_ERROR, "Cannot parse MGCP response (head)\n");
Philipp Maier1fc69992018-01-31 15:32:55 +0100678 rc = 1;
679 goto error;
Philipp Maierffd75e42017-11-22 11:44:50 +0100680 }
681
Philipp Maier1fc69992018-01-31 15:32:55 +0100682 rc = parse_head_params(r);
Philipp Maierffd75e42017-11-22 11:44:50 +0100683 if (rc) {
684 LOGP(DLMGCP, LOGL_ERROR, "Cannot parse MGCP response (head parameters)\n");
Philipp Maier1fc69992018-01-31 15:32:55 +0100685 rc = 1;
686 goto error;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200687 }
688
Philipp Maier1fc69992018-01-31 15:32:55 +0100689 pending = mgcp_client_response_pending_get(mgcp, r->head.trans_id);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200690 if (!pending) {
691 LOGP(DLMGCP, LOGL_ERROR,
692 "Cannot find matching MGCP transaction for trans_id %d\n",
Philipp Maier1fc69992018-01-31 15:32:55 +0100693 r->head.trans_id);
694 rc = -ENOENT;
695 goto error;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200696 }
697
Philipp Maier1fc69992018-01-31 15:32:55 +0100698 mgcp_client_handle_response(mgcp, pending, r);
699 rc = 0;
700
701error:
702 talloc_free(r);
703 return rc;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200704}
705
706static int mgcp_do_read(struct osmo_fd *fd)
707{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200708 struct mgcp_client *mgcp = fd->data;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200709 struct msgb *msg;
710 int ret;
711
712 msg = msgb_alloc_headroom(4096, 128, "mgcp_from_gw");
713 if (!msg) {
714 LOGP(DLMGCP, LOGL_ERROR, "Failed to allocate MGCP message.\n");
715 return -1;
716 }
717
718 ret = read(fd->fd, msg->data, 4096 - 128);
719 if (ret <= 0) {
720 LOGP(DLMGCP, LOGL_ERROR, "Failed to read: %d/%s\n", errno, strerror(errno));
721 msgb_free(msg);
722 return -1;
723 } else if (ret > 4096 - 128) {
724 LOGP(DLMGCP, LOGL_ERROR, "Too much data: %d\n", ret);
725 msgb_free(msg);
726 return -1;
Harald Welte9bf7c532017-11-17 14:14:31 +0100727 }
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200728
729 msg->l2h = msgb_put(msg, ret);
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200730 ret = mgcp_client_rx(mgcp, msg);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200731 talloc_free(msg);
732 return ret;
733}
734
735static int mgcp_do_write(struct osmo_fd *fd, struct msgb *msg)
736{
737 int ret;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200738
Neels Hofmeyr32d15cc2018-12-12 03:05:33 +0100739 LOGP(DLMGCP, LOGL_DEBUG, "Sending msg to MGCP GW size: len=%u '%s'...\n",
740 msg->len, osmo_escape_str((const char*)msg->data, OSMO_MIN(42, msg->len)));
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200741
742 ret = write(fd->fd, msg->data, msg->len);
743 if (ret != msg->len)
Neels Hofmeyr32d15cc2018-12-12 03:05:33 +0100744 LOGP(DLMGCP, LOGL_ERROR, "Failed to Tx MGCP: %d='%s'; msg: len=%u '%s'...\n",
745 errno, strerror(errno),
746 msg->len, osmo_escape_str((const char*)msg->data, OSMO_MIN(42, msg->len)));
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200747 return ret;
748}
749
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200750struct mgcp_client *mgcp_client_init(void *ctx,
751 struct mgcp_client_conf *conf)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200752{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200753 struct mgcp_client *mgcp;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200754
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200755 mgcp = talloc_zero(ctx, struct mgcp_client);
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
772 mgcp->actual.first_endpoint = conf->first_endpoint > 0 ? (uint16_t)conf->first_endpoint : 0;
773 mgcp->actual.last_endpoint = conf->last_endpoint > 0 ? (uint16_t)conf->last_endpoint : 0;
Neels Hofmeyrc0dcc3c2017-12-02 18:31:34 +0000774 mgcp->actual.bts_base = conf->bts_base > 0 ? (uint16_t)conf->bts_base : 4000;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200775
776 return mgcp;
777}
778
Philipp Maier6a26c162018-08-01 16:37:06 +0200779static int init_socket(struct mgcp_client *mgcp)
780{
781 int rc;
782 struct osmo_wqueue *wq;
783 int i;
784
785 wq = &mgcp->wq;
786
787 for (i = 0; i < 100; i++) {
788
789 /* Initalize socket with the currently configured port
790 * number */
791 rc = osmo_sock_init2_ofd(&wq->bfd, AF_INET, SOCK_DGRAM, IPPROTO_UDP, mgcp->actual.local_addr,
792 mgcp->actual.local_port, mgcp->actual.remote_addr, mgcp->actual.remote_port,
793 OSMO_SOCK_F_BIND | OSMO_SOCK_F_CONNECT);
794 if (rc > 0)
795 return rc;
796
797 /* If there is a different port than the default port
798 * configured then we assume that the user has choosen
799 * that port conciously and we will not try to resolve
800 * this by silently choosing a different port. */
Philipp Maier9a7ccc32018-08-09 11:41:19 +0200801 if (mgcp->actual.local_port != MGCP_CLIENT_LOCAL_PORT_DEFAULT && i == 0)
Philipp Maier6a26c162018-08-01 16:37:06 +0200802 return -EINVAL;
803
804 /* Choose a new port number to try next */
805 LOGP(DLMGCP, LOGL_NOTICE,
806 "MGCPGW faild to bind to port %u, retrying with port %u -- check configuration!\n",
807 mgcp->actual.local_port, mgcp->actual.local_port + 1);
808 mgcp->actual.local_port++;
809 }
810
811 LOGP(DLMGCP, LOGL_FATAL, "MGCPGW faild to find a port to bind on %i times.\n", i);
812 return -EINVAL;
813}
814
Philipp Maier275ac972018-01-20 00:58:16 +0100815/*! Initalize client connection (opens socket only, no request is sent yet)
816 * \param[in,out] mgcp MGCP client descriptor.
817 * \returns 0 on success, -EINVAL on error. */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200818int mgcp_client_connect(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200819{
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200820 struct sockaddr_in addr;
821 struct osmo_wqueue *wq;
822 int rc;
823
824 if (!mgcp) {
825 LOGP(DLMGCP, LOGL_FATAL, "MGCPGW client not initialized properly\n");
826 return -EINVAL;
827 }
828
829 wq = &mgcp->wq;
830
Philipp Maier6a26c162018-08-01 16:37:06 +0200831 rc = init_socket(mgcp);
Harald Welte8890dfa2017-11-17 15:09:30 +0100832 if (rc < 0) {
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200833 LOGP(DLMGCP, LOGL_FATAL,
Harald Welte8890dfa2017-11-17 15:09:30 +0100834 "Failed to initialize socket %s:%u -> %s:%u for MGCP GW: %s\n",
835 mgcp->actual.local_addr, mgcp->actual.local_port,
836 mgcp->actual.remote_addr, mgcp->actual.remote_port, strerror(errno));
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200837 goto error_close_fd;
838 }
839
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200840 inet_aton(mgcp->actual.remote_addr, &addr.sin_addr);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200841 mgcp->remote_addr = htonl(addr.sin_addr.s_addr);
842
843 osmo_wqueue_init(wq, 10);
844 wq->bfd.when = BSC_FD_READ;
845 wq->bfd.data = mgcp;
846 wq->read_cb = mgcp_do_read;
847 wq->write_cb = mgcp_do_write;
848
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200849 LOGP(DLMGCP, LOGL_INFO, "MGCP GW connection: %s:%u -> %s:%u\n",
850 mgcp->actual.local_addr, mgcp->actual.local_port,
851 mgcp->actual.remote_addr, mgcp->actual.remote_port);
852
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
Philipp Maier275ac972018-01-20 00:58:16 +0100876/*! Get the IP-Aaddress of the associated MGW as its numeric representation.
877 * \param[in] mgcp MGCP client descriptor.
878 * \returns IP-Address as 32 bit integer (network byte order) */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200879uint32_t mgcp_client_remote_addr_n(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200880{
881 return mgcp->remote_addr;
882}
883
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200884struct mgcp_response_pending * mgcp_client_pending_add(
885 struct mgcp_client *mgcp,
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200886 mgcp_trans_id_t trans_id,
887 mgcp_response_cb_t response_cb,
888 void *priv)
889{
890 struct mgcp_response_pending *pending;
891
892 pending = talloc_zero(mgcp, struct mgcp_response_pending);
893 pending->trans_id = trans_id;
894 pending->response_cb = response_cb;
895 pending->priv = priv;
896 llist_add_tail(&pending->entry, &mgcp->responses_pending);
897
898 return pending;
899}
900
901/* Send the MGCP message in msg to the MGCP GW and handle a response with
902 * response_cb. NOTE: the response_cb still needs to call
903 * mgcp_response_parse_params(response) to get the parsed parameters -- to
904 * potentially save some CPU cycles, only the head line has been parsed when
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100905 * the response_cb is invoked.
906 * Before the priv pointer becomes invalid, e.g. due to transaction timeout,
907 * mgcp_client_cancel() needs to be called for this transaction.
908 */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200909int mgcp_client_tx(struct mgcp_client *mgcp, struct msgb *msg,
910 mgcp_response_cb_t response_cb, void *priv)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200911{
912 struct mgcp_response_pending *pending;
913 mgcp_trans_id_t trans_id;
914 int rc;
915
916 trans_id = msg->cb[MSGB_CB_MGCP_TRANS_ID];
917 if (!trans_id) {
918 LOGP(DLMGCP, LOGL_ERROR,
919 "Unset transaction id in mgcp send request\n");
920 talloc_free(msg);
921 return -EINVAL;
922 }
923
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200924 pending = mgcp_client_pending_add(mgcp, trans_id, response_cb, priv);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200925
926 if (msgb_l2len(msg) > 4096) {
927 LOGP(DLMGCP, LOGL_ERROR,
928 "Cannot send, MGCP message too large: %u\n",
929 msgb_l2len(msg));
930 msgb_free(msg);
931 rc = -EINVAL;
932 goto mgcp_tx_error;
933 }
934
935 rc = osmo_wqueue_enqueue(&mgcp->wq, msg);
936 if (rc) {
937 LOGP(DLMGCP, LOGL_FATAL, "Could not queue message to MGCP GW\n");
938 msgb_free(msg);
939 goto mgcp_tx_error;
940 } else
941 LOGP(DLMGCP, LOGL_INFO, "Queued %u bytes for MGCP GW\n",
942 msgb_l2len(msg));
943 return 0;
944
945mgcp_tx_error:
946 /* Pass NULL to response cb to indicate an error */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200947 mgcp_client_handle_response(mgcp, pending, NULL);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200948 return -1;
949}
950
Philipp Maier275ac972018-01-20 00:58:16 +0100951/*! Cancel a pending transaction.
952 * \param[in] mgcp MGCP client descriptor.
953 * \param[in,out] trans_id Transaction id.
954 * \returns 0 on success, -ENOENT on error.
955 *
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100956 * Should a priv pointer passed to mgcp_client_tx() become invalid, this function must be called. In
957 * practical terms, if the caller of mgcp_client_tx() wishes to tear down a transaction without having
958 * received a response this function must be called. The trans_id can be obtained by calling
Philipp Maier275ac972018-01-20 00:58:16 +0100959 * mgcp_msg_trans_id() on the msgb produced by mgcp_msg_gen(). */
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100960int mgcp_client_cancel(struct mgcp_client *mgcp, mgcp_trans_id_t trans_id)
961{
962 struct mgcp_response_pending *pending = mgcp_client_response_pending_get(mgcp, trans_id);
963 if (!pending) {
Philipp Maier275ac972018-01-20 00:58:16 +0100964 /*! Note: it is not harmful to cancel a transaction twice. */
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100965 LOGP(DLMGCP, LOGL_INFO, "Cannot cancel, no such transaction: %u\n", trans_id);
966 return -ENOENT;
967 }
968 LOGP(DLMGCP, LOGL_INFO, "Canceled transaction %u\n", trans_id);
969 talloc_free(pending);
970 return 0;
Philipp Maier275ac972018-01-20 00:58:16 +0100971 /*! We don't really need to clean up the wqueue: In all sane cases, the msgb has already been sent
972 * out and is no longer in the wqueue. If it still is in the wqueue, then sending MGCP messages
973 * per se is broken and the program should notice so by a full wqueue. Even if this was called
974 * before we had a chance to send out the message and it is still going to be sent, we will just
975 * ignore the reply to it later. Removing a msgb from the wqueue here would just introduce more
976 * bug surface in terms of failing to update wqueue API's counters or some such.
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100977 */
978}
979
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200980static struct msgb *mgcp_msg_from_buf(mgcp_trans_id_t trans_id,
981 const char *buf, int len)
982{
983 struct msgb *msg;
984
985 if (len > (4096 - 128)) {
986 LOGP(DLMGCP, LOGL_ERROR, "Cannot send to MGCP GW:"
987 " message too large: %d\n", len);
988 return NULL;
989 }
990
991 msg = msgb_alloc_headroom(4096, 128, "MGCP tx");
992 OSMO_ASSERT(msg);
993
994 char *dst = (char*)msgb_put(msg, len);
995 memcpy(dst, buf, len);
996 msg->l2h = msg->data;
997 msg->cb[MSGB_CB_MGCP_TRANS_ID] = trans_id;
998
999 return msg;
1000}
1001
1002static struct msgb *mgcp_msg_from_str(mgcp_trans_id_t trans_id,
1003 const char *fmt, ...)
1004{
1005 static char compose[4096 - 128];
1006 va_list ap;
1007 int len;
1008 OSMO_ASSERT(fmt);
1009
1010 va_start(ap, fmt);
1011 len = vsnprintf(compose, sizeof(compose), fmt, ap);
1012 va_end(ap);
1013 if (len >= sizeof(compose)) {
1014 LOGP(DLMGCP, LOGL_ERROR,
1015 "Message too large: trans_id=%u len=%d\n",
1016 trans_id, len);
1017 return NULL;
1018 }
1019 if (len < 1) {
1020 LOGP(DLMGCP, LOGL_ERROR,
1021 "Failed to compose message: trans_id=%u len=%d\n",
1022 trans_id, len);
1023 return NULL;
1024 }
1025 return mgcp_msg_from_buf(trans_id, compose, len);
1026}
1027
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +02001028static mgcp_trans_id_t mgcp_client_next_trans_id(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001029{
1030 /* avoid zero trans_id to distinguish from unset trans_id */
1031 if (!mgcp->next_trans_id)
1032 mgcp->next_trans_id ++;
1033 return mgcp->next_trans_id ++;
1034}
1035
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +02001036struct msgb *mgcp_msg_crcx(struct mgcp_client *mgcp,
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001037 uint16_t rtp_endpoint, unsigned int call_id,
1038 enum mgcp_connection_mode mode)
1039{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +02001040 mgcp_trans_id_t trans_id = mgcp_client_next_trans_id(mgcp);
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001041 return mgcp_msg_from_str(trans_id,
1042 "CRCX %u %x@mgw MGCP 1.0\r\n"
1043 "C: %x\r\n"
1044 "L: p:20, a:AMR, nt:IN\r\n"
1045 "M: %s\r\n"
1046 ,
1047 trans_id,
1048 rtp_endpoint,
1049 call_id,
Neels Hofmeyrd95ab1e2017-09-22 00:52:54 +02001050 mgcp_client_cmode_name(mode));
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001051}
1052
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +02001053struct msgb *mgcp_msg_mdcx(struct mgcp_client *mgcp,
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001054 uint16_t rtp_endpoint, const char *rtp_conn_addr,
1055 uint16_t rtp_port, enum mgcp_connection_mode mode)
1056
1057{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +02001058 mgcp_trans_id_t trans_id = mgcp_client_next_trans_id(mgcp);
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001059 return mgcp_msg_from_str(trans_id,
1060 "MDCX %u %x@mgw MGCP 1.0\r\n"
1061 "M: %s\r\n"
1062 "\r\n"
1063 "c=IN IP4 %s\r\n"
1064 "m=audio %u RTP/AVP 255\r\n"
1065 ,
1066 trans_id,
1067 rtp_endpoint,
Neels Hofmeyrd95ab1e2017-09-22 00:52:54 +02001068 mgcp_client_cmode_name(mode),
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001069 rtp_conn_addr,
1070 rtp_port);
1071}
1072
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +02001073struct msgb *mgcp_msg_dlcx(struct mgcp_client *mgcp, uint16_t rtp_endpoint,
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001074 unsigned int call_id)
1075{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +02001076 mgcp_trans_id_t trans_id = mgcp_client_next_trans_id(mgcp);
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001077 return mgcp_msg_from_str(trans_id,
1078 "DLCX %u %x@mgw MGCP 1.0\r\n"
1079 "C: %x\r\n", trans_id, rtp_endpoint, call_id);
1080}
1081
Philipp Maier1dc6be62017-10-05 18:25:37 +02001082#define MGCP_CRCX_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT | \
1083 MGCP_MSG_PRESENCE_CALL_ID | \
Philipp Maier1dc6be62017-10-05 18:25:37 +02001084 MGCP_MSG_PRESENCE_CONN_MODE)
1085#define MGCP_MDCX_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT | \
Philipp Maier490cbaa2018-01-22 17:32:38 +01001086 MGCP_MSG_PRESENCE_CALL_ID | \
Philipp Maier1dc6be62017-10-05 18:25:37 +02001087 MGCP_MSG_PRESENCE_CONN_ID)
1088#define MGCP_DLCX_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT)
1089#define MGCP_AUEP_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT)
1090#define MGCP_RSIP_MANDATORY 0 /* none */
1091
Philipp Maier704c4f02018-06-07 18:51:31 +02001092/* Helper function for mgcp_msg_gen(): Add LCO information to MGCP message */
1093static int add_lco(struct msgb *msg, struct mgcp_msg *mgcp_msg)
1094{
1095 unsigned int i;
1096 int rc = 0;
1097 const char *codec;
1098 unsigned int pt;
1099
1100 rc += msgb_printf(msg, "L:");
1101
1102 if (mgcp_msg->ptime)
1103 rc += msgb_printf(msg, " p:%u,", mgcp_msg->ptime);
1104
1105 if (mgcp_msg->codecs_len) {
1106 rc += msgb_printf(msg, " a:");
1107 for (i = 0; i < mgcp_msg->codecs_len; i++) {
1108 pt = mgcp_msg->codecs[i];
1109 codec = get_value_string_or_null(codec_table, pt);
1110
1111 /* Note: Use codec descriptors from enum mgcp_codecs
1112 * in mgcp_client only! */
1113 OSMO_ASSERT(codec);
1114 rc += msgb_printf(msg, "%s", extract_codec_name(codec));
1115 if (i < mgcp_msg->codecs_len - 1)
1116 rc += msgb_printf(msg, ";");
1117 }
1118 rc += msgb_printf(msg, ",");
1119 }
1120
1121 rc += msgb_printf(msg, " nt:IN\r\n");
1122
1123 return rc;
1124}
1125
1126/* Helper function for mgcp_msg_gen(): Add SDP information to MGCP message */
1127static int add_sdp(struct msgb *msg, struct mgcp_msg *mgcp_msg, struct mgcp_client *mgcp)
1128{
1129 unsigned int i;
1130 int rc = 0;
1131 char local_ip[INET_ADDRSTRLEN];
1132 const char *codec;
1133 unsigned int pt;
1134
1135 /* Add separator to mark the beginning of the SDP block */
1136 rc += msgb_printf(msg, "\r\n");
1137
1138 /* Add SDP protocol version */
1139 rc += msgb_printf(msg, "v=0\r\n");
1140
1141 /* Determine local IP-Address */
1142 if (osmo_sock_local_ip(local_ip, mgcp->actual.remote_addr) < 0) {
1143 LOGP(DLMGCP, LOGL_ERROR,
1144 "Could not determine local IP-Address!\n");
1145 msgb_free(msg);
1146 return -2;
1147 }
1148
1149 /* Add owner/creator (SDP) */
1150 rc += msgb_printf(msg, "o=- %x 23 IN IP4 %s\r\n",
1151 mgcp_msg->call_id, local_ip);
1152
1153 /* Add session name (none) */
1154 rc += msgb_printf(msg, "s=-\r\n");
1155
1156 /* Add RTP address and port */
1157 if (mgcp_msg->audio_port == 0) {
1158 LOGP(DLMGCP, LOGL_ERROR,
1159 "Invalid port number, can not generate MGCP message\n");
1160 msgb_free(msg);
1161 return -2;
1162 }
1163 if (strlen(mgcp_msg->audio_ip) <= 0) {
1164 LOGP(DLMGCP, LOGL_ERROR,
1165 "Empty ip address, can not generate MGCP message\n");
1166 msgb_free(msg);
1167 return -2;
1168 }
1169 rc += msgb_printf(msg, "c=IN IP4 %s\r\n", mgcp_msg->audio_ip);
1170
1171 /* Add time description, active time (SDP) */
1172 rc += msgb_printf(msg, "t=0 0\r\n");
1173
1174 rc += msgb_printf(msg, "m=audio %u RTP/AVP", mgcp_msg->audio_port);
1175 for (i = 0; i < mgcp_msg->codecs_len; i++) {
1176 pt = map_codec_to_pt(mgcp_msg->ptmap, mgcp_msg->ptmap_len, mgcp_msg->codecs[i]);
1177 rc += msgb_printf(msg, " %u", pt);
1178
1179 }
1180 rc += msgb_printf(msg, "\r\n");
1181
1182 for (i = 0; i < mgcp_msg->codecs_len; i++) {
1183 pt = map_codec_to_pt(mgcp_msg->ptmap, mgcp_msg->ptmap_len, mgcp_msg->codecs[i]);
1184
1185 /* Note: Only dynamic payload type from the range 96-127
1186 * require to be explained further via rtpmap. All others
1187 * are implcitly definedby the number in m=audio */
1188 if (pt >= 96 && pt <= 127) {
1189 codec = get_value_string_or_null(codec_table, mgcp_msg->codecs[i]);
1190
1191 /* Note: Use codec descriptors from enum mgcp_codecs
1192 * in mgcp_client only! */
1193 OSMO_ASSERT(codec);
1194
1195 rc += msgb_printf(msg, "a=rtpmap:%u %s\r\n", pt, codec);
1196 }
1197 }
1198
1199 if (mgcp_msg->ptime)
1200 rc += msgb_printf(msg, "a=ptime:%u\r\n", mgcp_msg->ptime);
1201
1202 return rc;
1203}
1204
Philipp Maier275ac972018-01-20 00:58:16 +01001205/*! Generate an MGCP message
1206 * \param[in] mgcp MGCP client descriptor.
1207 * \param[in] mgcp_msg Message description
1208 * \returns message buffer on success, NULL on error. */
Philipp Maier1dc6be62017-10-05 18:25:37 +02001209struct msgb *mgcp_msg_gen(struct mgcp_client *mgcp, struct mgcp_msg *mgcp_msg)
1210{
1211 mgcp_trans_id_t trans_id = mgcp_client_next_trans_id(mgcp);
1212 uint32_t mandatory_mask;
1213 struct msgb *msg = msgb_alloc_headroom(4096, 128, "MGCP tx");
1214 int rc = 0;
Philipp Maier704c4f02018-06-07 18:51:31 +02001215 int rc_sdp;
1216 bool use_sdp = false;
Philipp Maier1dc6be62017-10-05 18:25:37 +02001217
1218 msg->l2h = msg->data;
1219 msg->cb[MSGB_CB_MGCP_TRANS_ID] = trans_id;
1220
1221 /* Add command verb */
1222 switch (mgcp_msg->verb) {
1223 case MGCP_VERB_CRCX:
1224 mandatory_mask = MGCP_CRCX_MANDATORY;
1225 rc += msgb_printf(msg, "CRCX %u", trans_id);
1226 break;
1227 case MGCP_VERB_MDCX:
1228 mandatory_mask = MGCP_MDCX_MANDATORY;
1229 rc += msgb_printf(msg, "MDCX %u", trans_id);
1230 break;
1231 case MGCP_VERB_DLCX:
1232 mandatory_mask = MGCP_DLCX_MANDATORY;
1233 rc += msgb_printf(msg, "DLCX %u", trans_id);
1234 break;
1235 case MGCP_VERB_AUEP:
1236 mandatory_mask = MGCP_AUEP_MANDATORY;
1237 rc += msgb_printf(msg, "AUEP %u", trans_id);
1238 break;
1239 case MGCP_VERB_RSIP:
1240 mandatory_mask = MGCP_RSIP_MANDATORY;
1241 rc += msgb_printf(msg, "RSIP %u", trans_id);
1242 break;
1243 default:
1244 LOGP(DLMGCP, LOGL_ERROR,
1245 "Invalid command verb, can not generate MGCP message\n");
1246 msgb_free(msg);
1247 return NULL;
1248 }
1249
1250 /* Check if mandatory fields are missing */
1251 if (!((mgcp_msg->presence & mandatory_mask) == mandatory_mask)) {
1252 LOGP(DLMGCP, LOGL_ERROR,
1253 "One or more missing mandatory fields, can not generate MGCP message\n");
1254 msgb_free(msg);
1255 return NULL;
1256 }
1257
1258 /* Add endpoint name */
Philipp Maier7bc55522017-12-10 22:52:22 +01001259 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_ENDPOINT) {
1260 if (strlen(mgcp_msg->endpoint) <= 0) {
1261 LOGP(DLMGCP, LOGL_ERROR,
1262 "Empty endpoint name, can not generate MGCP message\n");
1263 msgb_free(msg);
1264 return NULL;
1265 }
Philipp Maier3aa81572018-01-31 14:13:45 +01001266
1267 if (strstr(mgcp_msg->endpoint, "@") == NULL) {
1268 LOGP(DLMGCP, LOGL_ERROR,
1269 "Endpoint name (%s) lacks separator (@), can not generate MGCP message\n",
1270 mgcp_msg->endpoint);
1271 msgb_free(msg);
1272 }
1273
Philipp Maier1dc6be62017-10-05 18:25:37 +02001274 rc += msgb_printf(msg, " %s", mgcp_msg->endpoint);
Philipp Maier7bc55522017-12-10 22:52:22 +01001275 }
Philipp Maier1dc6be62017-10-05 18:25:37 +02001276
1277 /* Add protocol version */
1278 rc += msgb_printf(msg, " MGCP 1.0\r\n");
1279
1280 /* Add call id */
1281 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_CALL_ID)
1282 rc += msgb_printf(msg, "C: %x\r\n", mgcp_msg->call_id);
1283
1284 /* Add connection id */
Philipp Maier7bc55522017-12-10 22:52:22 +01001285 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_CONN_ID) {
1286 if (strlen(mgcp_msg->conn_id) <= 0) {
1287 LOGP(DLMGCP, LOGL_ERROR,
1288 "Empty connection id, can not generate MGCP message\n");
1289 msgb_free(msg);
1290 return NULL;
1291 }
Philipp Maier01d24a32017-11-21 17:26:09 +01001292 rc += msgb_printf(msg, "I: %s\r\n", mgcp_msg->conn_id);
Philipp Maier7bc55522017-12-10 22:52:22 +01001293 }
Philipp Maier1dc6be62017-10-05 18:25:37 +02001294
Philipp Maier704c4f02018-06-07 18:51:31 +02001295 /* Using SDP makes sense when a valid IP/Port combination is specifiec,
1296 * if we do not know this information yet, we fall back to LCO */
1297 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_AUDIO_IP
1298 && mgcp_msg->presence & MGCP_MSG_PRESENCE_AUDIO_PORT)
1299 use_sdp = true;
1300
1301 /* Add local connection options (LCO) */
1302 if (!use_sdp
1303 && (mgcp_msg->verb == MGCP_VERB_CRCX
1304 || mgcp_msg->verb == MGCP_VERB_MDCX))
1305 rc += add_lco(msg, mgcp_msg);
Philipp Maier1dc6be62017-10-05 18:25:37 +02001306
1307 /* Add mode */
1308 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_CONN_MODE)
1309 rc +=
1310 msgb_printf(msg, "M: %s\r\n",
1311 mgcp_client_cmode_name(mgcp_msg->conn_mode));
1312
Neels Hofmeyre6d8e912018-08-23 16:36:48 +02001313 /* Add X-Osmo-IGN */
1314 if ((mgcp_msg->presence & MGCP_MSG_PRESENCE_X_OSMO_IGN)
1315 && (mgcp_msg->x_osmo_ign != 0))
1316 rc +=
1317 msgb_printf(msg, MGCP_X_OSMO_IGN_HEADER "%s\r\n",
1318 mgcp_msg->x_osmo_ign & MGCP_X_OSMO_IGN_CALLID ? " C": "");
1319
Philipp Maier704c4f02018-06-07 18:51:31 +02001320 /* Add session description protocol (SDP) */
1321 if (use_sdp
1322 && (mgcp_msg->verb == MGCP_VERB_CRCX
1323 || mgcp_msg->verb == MGCP_VERB_MDCX)) {
1324 rc_sdp = add_sdp(msg, mgcp_msg, mgcp);
1325 if (rc_sdp == -2)
Philipp Maier9d25d7a2018-01-22 17:31:10 +01001326 return NULL;
Philipp Maier704c4f02018-06-07 18:51:31 +02001327 else
1328 rc += rc_sdp;
Philipp Maier1dc6be62017-10-05 18:25:37 +02001329 }
1330
1331 if (rc != 0) {
1332 LOGP(DLMGCP, LOGL_ERROR,
1333 "message buffer to small, can not generate MGCP message\n");
1334 msgb_free(msg);
1335 msg = NULL;
1336 }
1337
1338 return msg;
1339}
1340
Philipp Maier275ac972018-01-20 00:58:16 +01001341/*! Retrieve the MGCP transaction ID from a msgb generated by mgcp_msg_gen()
1342 * \param[in] msg message buffer
1343 * \returns Transaction id. */
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +01001344mgcp_trans_id_t mgcp_msg_trans_id(struct msgb *msg)
1345{
1346 return (mgcp_trans_id_t)msg->cb[MSGB_CB_MGCP_TRANS_ID];
1347}
1348
Philipp Maier275ac972018-01-20 00:58:16 +01001349/*! Get the configuration parameters a given MGCP client instance
1350 * \param[in] mgcp MGCP client descriptor.
1351 * \returns configuration */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +02001352struct mgcp_client_conf *mgcp_client_conf_actual(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001353{
1354 return &mgcp->actual;
1355}
Neels Hofmeyrd95ab1e2017-09-22 00:52:54 +02001356
1357const struct value_string mgcp_client_connection_mode_strs[] = {
1358 { MGCP_CONN_NONE, "none" },
1359 { MGCP_CONN_RECV_SEND, "sendrecv" },
1360 { MGCP_CONN_SEND_ONLY, "sendonly" },
1361 { MGCP_CONN_RECV_ONLY, "recvonly" },
1362 { MGCP_CONN_LOOPBACK, "loopback" },
1363 { 0, NULL }
1364};