blob: 366316397d212a7a54ff839a4c94bbef5f5111bd [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];
553
554 /* Detect empty parameters */
Philipp Maierffd75e42017-11-22 11:44:50 +0100555 if (strlen(line) < 4)
556 goto response_parse_failure;
557
Philipp Maier55295f72018-01-15 14:00:28 +0100558 /* Check if the label matches */
559 snprintf(label_string, sizeof(label_string), "%c: ", label);
560 if (memcmp(label_string, line, 3) != 0)
Philipp Maierffd75e42017-11-22 11:44:50 +0100561 goto response_parse_failure;
562
Philipp Maier55295f72018-01-15 14:00:28 +0100563 /* Copy payload part of the string to destinations (the label string
564 * is always 3 chars long) */
565 osmo_strlcpy(result, line + 3, result_len);
Philipp Maierffd75e42017-11-22 11:44:50 +0100566 return 0;
567
568response_parse_failure:
569 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier55295f72018-01-15 14:00:28 +0100570 "Failed to parse MGCP response (parameter label: %c)\n", label);
Philipp Maierffd75e42017-11-22 11:44:50 +0100571 return -EINVAL;
572}
573
574/* Parse MGCP parameters of the response */
575static int parse_head_params(struct mgcp_response *r)
576{
577 char *line;
578 int rc = 0;
579 OSMO_ASSERT(r->body);
Philipp Maier55295f72018-01-15 14:00:28 +0100580 char *data;
581 char *data_ptr;
582 char *data_end;
Philipp Maierffd75e42017-11-22 11:44:50 +0100583
Philipp Maier55295f72018-01-15 14:00:28 +0100584 /* Since this functions performs a destructive parsing, we create a
585 * local copy of the body data */
Philipp Maier1fc69992018-01-31 15:32:55 +0100586 data = talloc_zero_size(r, strlen(r->body)+1);
Philipp Maier55295f72018-01-15 14:00:28 +0100587 OSMO_ASSERT(data);
588 data_ptr = data;
589 osmo_strlcpy(data, r->body, strlen(r->body));
590
591 /* If there is an SDP body attached, prevent for_each_non_empty_line()
592 * into running in there, we are not yet interested in the parameters
593 * stored there. */
594 data_end = mgcp_find_section_end(data);
Philipp Maierffd75e42017-11-22 11:44:50 +0100595 if (data_end)
596 *data_end = '\0';
597
Philipp Maier55295f72018-01-15 14:00:28 +0100598 for_each_non_empty_line(line, data_ptr) {
Philipp Maierffd75e42017-11-22 11:44:50 +0100599 switch (line[0]) {
Philipp Maier55295f72018-01-15 14:00:28 +0100600 case 'Z':
601 rc = mgcp_parse_head_param(r->head.endpoint,
602 sizeof(r->head.endpoint),
603 'Z', line);
604 if (rc)
605 goto exit;
Philipp Maier771b26a2018-01-31 13:53:11 +0100606
607 /* A specific endpoint identifier returned by the MGW
608 * must not contain any wildcard characters */
609 if (strstr(r->head.endpoint, "*") != NULL) {
610 rc = -EINVAL;
611 goto exit;
612 }
Philipp Maier3261dc72018-01-31 14:03:13 +0100613
614 /* A specific endpoint identifier returned by the MGW
615 * must contain an @ character */
616 if (strstr(r->head.endpoint, "@") == NULL) {
617 rc = -EINVAL;
618 goto exit;
619 }
Philipp Maier55295f72018-01-15 14:00:28 +0100620 break;
Philipp Maierffd75e42017-11-22 11:44:50 +0100621 case 'I':
Philipp Maier55295f72018-01-15 14:00:28 +0100622 rc = mgcp_parse_head_param(r->head.conn_id,
623 sizeof(r->head.conn_id),
624 'I', line);
Philipp Maierffd75e42017-11-22 11:44:50 +0100625 if (rc)
626 goto exit;
627 break;
628 default:
629 /* skip unhandled parameters */
630 break;
631 }
632 }
633exit:
Philipp Maier55295f72018-01-15 14:00:28 +0100634 talloc_free(data);
Philipp Maierffd75e42017-11-22 11:44:50 +0100635 return rc;
636}
637
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200638static struct mgcp_response_pending *mgcp_client_response_pending_get(
639 struct mgcp_client *mgcp,
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100640 mgcp_trans_id_t trans_id)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200641{
642 struct mgcp_response_pending *pending;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200643 llist_for_each_entry(pending, &mgcp->responses_pending, entry) {
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100644 if (pending->trans_id == trans_id) {
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200645 llist_del(&pending->entry);
646 return pending;
647 }
648 }
649 return NULL;
650}
651
652/* Feed an MGCP message into the receive processing.
653 * Parse the head and call any callback registered for the transaction id found
654 * in the MGCP message. This is normally called directly from the internal
655 * mgcp_do_read that reads from the socket connected to the MGCP gateway. This
656 * function is published mainly to be able to feed data from the test suite.
657 */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200658int mgcp_client_rx(struct mgcp_client *mgcp, struct msgb *msg)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200659{
Philipp Maier1fc69992018-01-31 15:32:55 +0100660 struct mgcp_response *r;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200661 struct mgcp_response_pending *pending;
662 int rc;
663
Philipp Maier1fc69992018-01-31 15:32:55 +0100664 r = talloc_zero(mgcp, struct mgcp_response);
665 OSMO_ASSERT(r);
666
667 rc = mgcp_response_parse_head(r, msg);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200668 if (rc) {
Philipp Maierffd75e42017-11-22 11:44:50 +0100669 LOGP(DLMGCP, LOGL_ERROR, "Cannot parse MGCP response (head)\n");
Philipp Maier1fc69992018-01-31 15:32:55 +0100670 rc = 1;
671 goto error;
Philipp Maierffd75e42017-11-22 11:44:50 +0100672 }
673
Philipp Maier1fc69992018-01-31 15:32:55 +0100674 rc = parse_head_params(r);
Philipp Maierffd75e42017-11-22 11:44:50 +0100675 if (rc) {
676 LOGP(DLMGCP, LOGL_ERROR, "Cannot parse MGCP response (head parameters)\n");
Philipp Maier1fc69992018-01-31 15:32:55 +0100677 rc = 1;
678 goto error;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200679 }
680
Philipp Maier1fc69992018-01-31 15:32:55 +0100681 pending = mgcp_client_response_pending_get(mgcp, r->head.trans_id);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200682 if (!pending) {
683 LOGP(DLMGCP, LOGL_ERROR,
684 "Cannot find matching MGCP transaction for trans_id %d\n",
Philipp Maier1fc69992018-01-31 15:32:55 +0100685 r->head.trans_id);
686 rc = -ENOENT;
687 goto error;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200688 }
689
Philipp Maier1fc69992018-01-31 15:32:55 +0100690 mgcp_client_handle_response(mgcp, pending, r);
691 rc = 0;
692
693error:
694 talloc_free(r);
695 return rc;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200696}
697
698static int mgcp_do_read(struct osmo_fd *fd)
699{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200700 struct mgcp_client *mgcp = fd->data;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200701 struct msgb *msg;
702 int ret;
703
704 msg = msgb_alloc_headroom(4096, 128, "mgcp_from_gw");
705 if (!msg) {
706 LOGP(DLMGCP, LOGL_ERROR, "Failed to allocate MGCP message.\n");
707 return -1;
708 }
709
710 ret = read(fd->fd, msg->data, 4096 - 128);
711 if (ret <= 0) {
712 LOGP(DLMGCP, LOGL_ERROR, "Failed to read: %d/%s\n", errno, strerror(errno));
713 msgb_free(msg);
714 return -1;
715 } else if (ret > 4096 - 128) {
716 LOGP(DLMGCP, LOGL_ERROR, "Too much data: %d\n", ret);
717 msgb_free(msg);
718 return -1;
Harald Welte9bf7c532017-11-17 14:14:31 +0100719 }
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200720
721 msg->l2h = msgb_put(msg, ret);
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200722 ret = mgcp_client_rx(mgcp, msg);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200723 talloc_free(msg);
724 return ret;
725}
726
727static int mgcp_do_write(struct osmo_fd *fd, struct msgb *msg)
728{
729 int ret;
730 static char strbuf[4096];
731 unsigned int l = msg->len < sizeof(strbuf) ? msg->len : sizeof(strbuf);
732 unsigned int i;
733
Philipp Maierf8bfbe82017-11-23 19:32:31 +0100734 osmo_strlcpy(strbuf, (const char*)msg->data, l);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200735 for (i = 0; i < sizeof(strbuf); i++) {
736 if (strbuf[i] == '\n' || strbuf[i] == '\r') {
737 strbuf[i] = '\0';
738 break;
739 }
740 }
741 DEBUGP(DLMGCP, "Tx MGCP msg to MGCP GW: '%s'\n", strbuf);
742
743 LOGP(DLMGCP, LOGL_DEBUG, "Sending msg to MGCP GW size: %u\n", msg->len);
744
745 ret = write(fd->fd, msg->data, msg->len);
746 if (ret != msg->len)
747 LOGP(DLMGCP, LOGL_ERROR, "Failed to forward message to MGCP"
748 " GW: %s\n", strerror(errno));
749
750 return ret;
751}
752
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200753struct mgcp_client *mgcp_client_init(void *ctx,
754 struct mgcp_client_conf *conf)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200755{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200756 struct mgcp_client *mgcp;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200757
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200758 mgcp = talloc_zero(ctx, struct mgcp_client);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200759
760 INIT_LLIST_HEAD(&mgcp->responses_pending);
761 INIT_LLIST_HEAD(&mgcp->inuse_endpoints);
762
763 mgcp->next_trans_id = 1;
764
765 mgcp->actual.local_addr = conf->local_addr ? conf->local_addr :
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200766 MGCP_CLIENT_LOCAL_ADDR_DEFAULT;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200767 mgcp->actual.local_port = conf->local_port >= 0 ? (uint16_t)conf->local_port :
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200768 MGCP_CLIENT_LOCAL_PORT_DEFAULT;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200769
770 mgcp->actual.remote_addr = conf->remote_addr ? conf->remote_addr :
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200771 MGCP_CLIENT_REMOTE_ADDR_DEFAULT;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200772 mgcp->actual.remote_port = conf->remote_port >= 0 ? (uint16_t)conf->remote_port :
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200773 MGCP_CLIENT_REMOTE_PORT_DEFAULT;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200774
775 mgcp->actual.first_endpoint = conf->first_endpoint > 0 ? (uint16_t)conf->first_endpoint : 0;
776 mgcp->actual.last_endpoint = conf->last_endpoint > 0 ? (uint16_t)conf->last_endpoint : 0;
Neels Hofmeyrc0dcc3c2017-12-02 18:31:34 +0000777 mgcp->actual.bts_base = conf->bts_base > 0 ? (uint16_t)conf->bts_base : 4000;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200778
779 return mgcp;
780}
781
Philipp Maier6a26c162018-08-01 16:37:06 +0200782static int init_socket(struct mgcp_client *mgcp)
783{
784 int rc;
785 struct osmo_wqueue *wq;
786 int i;
787
788 wq = &mgcp->wq;
789
790 for (i = 0; i < 100; i++) {
791
792 /* Initalize socket with the currently configured port
793 * number */
794 rc = osmo_sock_init2_ofd(&wq->bfd, AF_INET, SOCK_DGRAM, IPPROTO_UDP, mgcp->actual.local_addr,
795 mgcp->actual.local_port, mgcp->actual.remote_addr, mgcp->actual.remote_port,
796 OSMO_SOCK_F_BIND | OSMO_SOCK_F_CONNECT);
797 if (rc > 0)
798 return rc;
799
800 /* If there is a different port than the default port
801 * configured then we assume that the user has choosen
802 * that port conciously and we will not try to resolve
803 * this by silently choosing a different port. */
804 if (mgcp->actual.local_port != MGCP_CLIENT_LOCAL_PORT_DEFAULT)
805 return -EINVAL;
806
807 /* Choose a new port number to try next */
808 LOGP(DLMGCP, LOGL_NOTICE,
809 "MGCPGW faild to bind to port %u, retrying with port %u -- check configuration!\n",
810 mgcp->actual.local_port, mgcp->actual.local_port + 1);
811 mgcp->actual.local_port++;
812 }
813
814 LOGP(DLMGCP, LOGL_FATAL, "MGCPGW faild to find a port to bind on %i times.\n", i);
815 return -EINVAL;
816}
817
Philipp Maier275ac972018-01-20 00:58:16 +0100818/*! Initalize client connection (opens socket only, no request is sent yet)
819 * \param[in,out] mgcp MGCP client descriptor.
820 * \returns 0 on success, -EINVAL on error. */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200821int mgcp_client_connect(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200822{
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200823 struct sockaddr_in addr;
824 struct osmo_wqueue *wq;
825 int rc;
826
827 if (!mgcp) {
828 LOGP(DLMGCP, LOGL_FATAL, "MGCPGW client not initialized properly\n");
829 return -EINVAL;
830 }
831
832 wq = &mgcp->wq;
833
Philipp Maier6a26c162018-08-01 16:37:06 +0200834 rc = init_socket(mgcp);
Harald Welte8890dfa2017-11-17 15:09:30 +0100835 if (rc < 0) {
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200836 LOGP(DLMGCP, LOGL_FATAL,
Harald Welte8890dfa2017-11-17 15:09:30 +0100837 "Failed to initialize socket %s:%u -> %s:%u for MGCP GW: %s\n",
838 mgcp->actual.local_addr, mgcp->actual.local_port,
839 mgcp->actual.remote_addr, mgcp->actual.remote_port, strerror(errno));
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200840 goto error_close_fd;
841 }
842
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200843 inet_aton(mgcp->actual.remote_addr, &addr.sin_addr);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200844 mgcp->remote_addr = htonl(addr.sin_addr.s_addr);
845
846 osmo_wqueue_init(wq, 10);
847 wq->bfd.when = BSC_FD_READ;
848 wq->bfd.data = mgcp;
849 wq->read_cb = mgcp_do_read;
850 wq->write_cb = mgcp_do_write;
851
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200852 LOGP(DLMGCP, LOGL_INFO, "MGCP GW connection: %s:%u -> %s:%u\n",
853 mgcp->actual.local_addr, mgcp->actual.local_port,
854 mgcp->actual.remote_addr, mgcp->actual.remote_port);
855
856 return 0;
857error_close_fd:
858 close(wq->bfd.fd);
859 wq->bfd.fd = -1;
860 return rc;
861}
862
Philipp Maier275ac972018-01-20 00:58:16 +0100863/*! Get the IP-Aaddress of the associated MGW as string.
864 * \param[in] mgcp MGCP client descriptor.
865 * \returns a pointer to the address string. */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200866const char *mgcp_client_remote_addr_str(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200867{
868 return mgcp->actual.remote_addr;
869}
870
Philipp Maier275ac972018-01-20 00:58:16 +0100871/*! Get the IP-Port of the associated MGW.
872 * \param[in] mgcp MGCP client descriptor.
873 * \returns port number. */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200874uint16_t mgcp_client_remote_port(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200875{
876 return mgcp->actual.remote_port;
877}
878
Philipp Maier275ac972018-01-20 00:58:16 +0100879/*! Get the IP-Aaddress of the associated MGW as its numeric representation.
880 * \param[in] mgcp MGCP client descriptor.
881 * \returns IP-Address as 32 bit integer (network byte order) */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200882uint32_t mgcp_client_remote_addr_n(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200883{
884 return mgcp->remote_addr;
885}
886
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200887struct mgcp_response_pending * mgcp_client_pending_add(
888 struct mgcp_client *mgcp,
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200889 mgcp_trans_id_t trans_id,
890 mgcp_response_cb_t response_cb,
891 void *priv)
892{
893 struct mgcp_response_pending *pending;
894
895 pending = talloc_zero(mgcp, struct mgcp_response_pending);
896 pending->trans_id = trans_id;
897 pending->response_cb = response_cb;
898 pending->priv = priv;
899 llist_add_tail(&pending->entry, &mgcp->responses_pending);
900
901 return pending;
902}
903
904/* Send the MGCP message in msg to the MGCP GW and handle a response with
905 * response_cb. NOTE: the response_cb still needs to call
906 * mgcp_response_parse_params(response) to get the parsed parameters -- to
907 * potentially save some CPU cycles, only the head line has been parsed when
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100908 * the response_cb is invoked.
909 * Before the priv pointer becomes invalid, e.g. due to transaction timeout,
910 * mgcp_client_cancel() needs to be called for this transaction.
911 */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200912int mgcp_client_tx(struct mgcp_client *mgcp, struct msgb *msg,
913 mgcp_response_cb_t response_cb, void *priv)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200914{
915 struct mgcp_response_pending *pending;
916 mgcp_trans_id_t trans_id;
917 int rc;
918
919 trans_id = msg->cb[MSGB_CB_MGCP_TRANS_ID];
920 if (!trans_id) {
921 LOGP(DLMGCP, LOGL_ERROR,
922 "Unset transaction id in mgcp send request\n");
923 talloc_free(msg);
924 return -EINVAL;
925 }
926
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200927 pending = mgcp_client_pending_add(mgcp, trans_id, response_cb, priv);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200928
929 if (msgb_l2len(msg) > 4096) {
930 LOGP(DLMGCP, LOGL_ERROR,
931 "Cannot send, MGCP message too large: %u\n",
932 msgb_l2len(msg));
933 msgb_free(msg);
934 rc = -EINVAL;
935 goto mgcp_tx_error;
936 }
937
938 rc = osmo_wqueue_enqueue(&mgcp->wq, msg);
939 if (rc) {
940 LOGP(DLMGCP, LOGL_FATAL, "Could not queue message to MGCP GW\n");
941 msgb_free(msg);
942 goto mgcp_tx_error;
943 } else
944 LOGP(DLMGCP, LOGL_INFO, "Queued %u bytes for MGCP GW\n",
945 msgb_l2len(msg));
946 return 0;
947
948mgcp_tx_error:
949 /* Pass NULL to response cb to indicate an error */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200950 mgcp_client_handle_response(mgcp, pending, NULL);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200951 return -1;
952}
953
Philipp Maier275ac972018-01-20 00:58:16 +0100954/*! Cancel a pending transaction.
955 * \param[in] mgcp MGCP client descriptor.
956 * \param[in,out] trans_id Transaction id.
957 * \returns 0 on success, -ENOENT on error.
958 *
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100959 * Should a priv pointer passed to mgcp_client_tx() become invalid, this function must be called. In
960 * practical terms, if the caller of mgcp_client_tx() wishes to tear down a transaction without having
961 * received a response this function must be called. The trans_id can be obtained by calling
Philipp Maier275ac972018-01-20 00:58:16 +0100962 * mgcp_msg_trans_id() on the msgb produced by mgcp_msg_gen(). */
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100963int mgcp_client_cancel(struct mgcp_client *mgcp, mgcp_trans_id_t trans_id)
964{
965 struct mgcp_response_pending *pending = mgcp_client_response_pending_get(mgcp, trans_id);
966 if (!pending) {
Philipp Maier275ac972018-01-20 00:58:16 +0100967 /*! Note: it is not harmful to cancel a transaction twice. */
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100968 LOGP(DLMGCP, LOGL_INFO, "Cannot cancel, no such transaction: %u\n", trans_id);
969 return -ENOENT;
970 }
971 LOGP(DLMGCP, LOGL_INFO, "Canceled transaction %u\n", trans_id);
972 talloc_free(pending);
973 return 0;
Philipp Maier275ac972018-01-20 00:58:16 +0100974 /*! We don't really need to clean up the wqueue: In all sane cases, the msgb has already been sent
975 * out and is no longer in the wqueue. If it still is in the wqueue, then sending MGCP messages
976 * per se is broken and the program should notice so by a full wqueue. Even if this was called
977 * before we had a chance to send out the message and it is still going to be sent, we will just
978 * ignore the reply to it later. Removing a msgb from the wqueue here would just introduce more
979 * bug surface in terms of failing to update wqueue API's counters or some such.
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100980 */
981}
982
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200983static struct msgb *mgcp_msg_from_buf(mgcp_trans_id_t trans_id,
984 const char *buf, int len)
985{
986 struct msgb *msg;
987
988 if (len > (4096 - 128)) {
989 LOGP(DLMGCP, LOGL_ERROR, "Cannot send to MGCP GW:"
990 " message too large: %d\n", len);
991 return NULL;
992 }
993
994 msg = msgb_alloc_headroom(4096, 128, "MGCP tx");
995 OSMO_ASSERT(msg);
996
997 char *dst = (char*)msgb_put(msg, len);
998 memcpy(dst, buf, len);
999 msg->l2h = msg->data;
1000 msg->cb[MSGB_CB_MGCP_TRANS_ID] = trans_id;
1001
1002 return msg;
1003}
1004
1005static struct msgb *mgcp_msg_from_str(mgcp_trans_id_t trans_id,
1006 const char *fmt, ...)
1007{
1008 static char compose[4096 - 128];
1009 va_list ap;
1010 int len;
1011 OSMO_ASSERT(fmt);
1012
1013 va_start(ap, fmt);
1014 len = vsnprintf(compose, sizeof(compose), fmt, ap);
1015 va_end(ap);
1016 if (len >= sizeof(compose)) {
1017 LOGP(DLMGCP, LOGL_ERROR,
1018 "Message too large: trans_id=%u len=%d\n",
1019 trans_id, len);
1020 return NULL;
1021 }
1022 if (len < 1) {
1023 LOGP(DLMGCP, LOGL_ERROR,
1024 "Failed to compose message: trans_id=%u len=%d\n",
1025 trans_id, len);
1026 return NULL;
1027 }
1028 return mgcp_msg_from_buf(trans_id, compose, len);
1029}
1030
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +02001031static mgcp_trans_id_t mgcp_client_next_trans_id(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001032{
1033 /* avoid zero trans_id to distinguish from unset trans_id */
1034 if (!mgcp->next_trans_id)
1035 mgcp->next_trans_id ++;
1036 return mgcp->next_trans_id ++;
1037}
1038
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +02001039struct msgb *mgcp_msg_crcx(struct mgcp_client *mgcp,
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001040 uint16_t rtp_endpoint, unsigned int call_id,
1041 enum mgcp_connection_mode mode)
1042{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +02001043 mgcp_trans_id_t trans_id = mgcp_client_next_trans_id(mgcp);
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001044 return mgcp_msg_from_str(trans_id,
1045 "CRCX %u %x@mgw MGCP 1.0\r\n"
1046 "C: %x\r\n"
1047 "L: p:20, a:AMR, nt:IN\r\n"
1048 "M: %s\r\n"
1049 ,
1050 trans_id,
1051 rtp_endpoint,
1052 call_id,
Neels Hofmeyrd95ab1e2017-09-22 00:52:54 +02001053 mgcp_client_cmode_name(mode));
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001054}
1055
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +02001056struct msgb *mgcp_msg_mdcx(struct mgcp_client *mgcp,
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001057 uint16_t rtp_endpoint, const char *rtp_conn_addr,
1058 uint16_t rtp_port, enum mgcp_connection_mode mode)
1059
1060{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +02001061 mgcp_trans_id_t trans_id = mgcp_client_next_trans_id(mgcp);
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001062 return mgcp_msg_from_str(trans_id,
1063 "MDCX %u %x@mgw MGCP 1.0\r\n"
1064 "M: %s\r\n"
1065 "\r\n"
1066 "c=IN IP4 %s\r\n"
1067 "m=audio %u RTP/AVP 255\r\n"
1068 ,
1069 trans_id,
1070 rtp_endpoint,
Neels Hofmeyrd95ab1e2017-09-22 00:52:54 +02001071 mgcp_client_cmode_name(mode),
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001072 rtp_conn_addr,
1073 rtp_port);
1074}
1075
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +02001076struct msgb *mgcp_msg_dlcx(struct mgcp_client *mgcp, uint16_t rtp_endpoint,
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001077 unsigned int call_id)
1078{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +02001079 mgcp_trans_id_t trans_id = mgcp_client_next_trans_id(mgcp);
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001080 return mgcp_msg_from_str(trans_id,
1081 "DLCX %u %x@mgw MGCP 1.0\r\n"
1082 "C: %x\r\n", trans_id, rtp_endpoint, call_id);
1083}
1084
Philipp Maier1dc6be62017-10-05 18:25:37 +02001085#define MGCP_CRCX_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT | \
1086 MGCP_MSG_PRESENCE_CALL_ID | \
Philipp Maier1dc6be62017-10-05 18:25:37 +02001087 MGCP_MSG_PRESENCE_CONN_MODE)
1088#define MGCP_MDCX_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT | \
Philipp Maier490cbaa2018-01-22 17:32:38 +01001089 MGCP_MSG_PRESENCE_CALL_ID | \
Philipp Maier1dc6be62017-10-05 18:25:37 +02001090 MGCP_MSG_PRESENCE_CONN_ID)
1091#define MGCP_DLCX_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT)
1092#define MGCP_AUEP_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT)
1093#define MGCP_RSIP_MANDATORY 0 /* none */
1094
Philipp Maier704c4f02018-06-07 18:51:31 +02001095/* Helper function for mgcp_msg_gen(): Add LCO information to MGCP message */
1096static int add_lco(struct msgb *msg, struct mgcp_msg *mgcp_msg)
1097{
1098 unsigned int i;
1099 int rc = 0;
1100 const char *codec;
1101 unsigned int pt;
1102
1103 rc += msgb_printf(msg, "L:");
1104
1105 if (mgcp_msg->ptime)
1106 rc += msgb_printf(msg, " p:%u,", mgcp_msg->ptime);
1107
1108 if (mgcp_msg->codecs_len) {
1109 rc += msgb_printf(msg, " a:");
1110 for (i = 0; i < mgcp_msg->codecs_len; i++) {
1111 pt = mgcp_msg->codecs[i];
1112 codec = get_value_string_or_null(codec_table, pt);
1113
1114 /* Note: Use codec descriptors from enum mgcp_codecs
1115 * in mgcp_client only! */
1116 OSMO_ASSERT(codec);
1117 rc += msgb_printf(msg, "%s", extract_codec_name(codec));
1118 if (i < mgcp_msg->codecs_len - 1)
1119 rc += msgb_printf(msg, ";");
1120 }
1121 rc += msgb_printf(msg, ",");
1122 }
1123
1124 rc += msgb_printf(msg, " nt:IN\r\n");
1125
1126 return rc;
1127}
1128
1129/* Helper function for mgcp_msg_gen(): Add SDP information to MGCP message */
1130static int add_sdp(struct msgb *msg, struct mgcp_msg *mgcp_msg, struct mgcp_client *mgcp)
1131{
1132 unsigned int i;
1133 int rc = 0;
1134 char local_ip[INET_ADDRSTRLEN];
1135 const char *codec;
1136 unsigned int pt;
1137
1138 /* Add separator to mark the beginning of the SDP block */
1139 rc += msgb_printf(msg, "\r\n");
1140
1141 /* Add SDP protocol version */
1142 rc += msgb_printf(msg, "v=0\r\n");
1143
1144 /* Determine local IP-Address */
1145 if (osmo_sock_local_ip(local_ip, mgcp->actual.remote_addr) < 0) {
1146 LOGP(DLMGCP, LOGL_ERROR,
1147 "Could not determine local IP-Address!\n");
1148 msgb_free(msg);
1149 return -2;
1150 }
1151
1152 /* Add owner/creator (SDP) */
1153 rc += msgb_printf(msg, "o=- %x 23 IN IP4 %s\r\n",
1154 mgcp_msg->call_id, local_ip);
1155
1156 /* Add session name (none) */
1157 rc += msgb_printf(msg, "s=-\r\n");
1158
1159 /* Add RTP address and port */
1160 if (mgcp_msg->audio_port == 0) {
1161 LOGP(DLMGCP, LOGL_ERROR,
1162 "Invalid port number, can not generate MGCP message\n");
1163 msgb_free(msg);
1164 return -2;
1165 }
1166 if (strlen(mgcp_msg->audio_ip) <= 0) {
1167 LOGP(DLMGCP, LOGL_ERROR,
1168 "Empty ip address, can not generate MGCP message\n");
1169 msgb_free(msg);
1170 return -2;
1171 }
1172 rc += msgb_printf(msg, "c=IN IP4 %s\r\n", mgcp_msg->audio_ip);
1173
1174 /* Add time description, active time (SDP) */
1175 rc += msgb_printf(msg, "t=0 0\r\n");
1176
1177 rc += msgb_printf(msg, "m=audio %u RTP/AVP", mgcp_msg->audio_port);
1178 for (i = 0; i < mgcp_msg->codecs_len; i++) {
1179 pt = map_codec_to_pt(mgcp_msg->ptmap, mgcp_msg->ptmap_len, mgcp_msg->codecs[i]);
1180 rc += msgb_printf(msg, " %u", pt);
1181
1182 }
1183 rc += msgb_printf(msg, "\r\n");
1184
1185 for (i = 0; i < mgcp_msg->codecs_len; i++) {
1186 pt = map_codec_to_pt(mgcp_msg->ptmap, mgcp_msg->ptmap_len, mgcp_msg->codecs[i]);
1187
1188 /* Note: Only dynamic payload type from the range 96-127
1189 * require to be explained further via rtpmap. All others
1190 * are implcitly definedby the number in m=audio */
1191 if (pt >= 96 && pt <= 127) {
1192 codec = get_value_string_or_null(codec_table, mgcp_msg->codecs[i]);
1193
1194 /* Note: Use codec descriptors from enum mgcp_codecs
1195 * in mgcp_client only! */
1196 OSMO_ASSERT(codec);
1197
1198 rc += msgb_printf(msg, "a=rtpmap:%u %s\r\n", pt, codec);
1199 }
1200 }
1201
1202 if (mgcp_msg->ptime)
1203 rc += msgb_printf(msg, "a=ptime:%u\r\n", mgcp_msg->ptime);
1204
1205 return rc;
1206}
1207
Philipp Maier275ac972018-01-20 00:58:16 +01001208/*! Generate an MGCP message
1209 * \param[in] mgcp MGCP client descriptor.
1210 * \param[in] mgcp_msg Message description
1211 * \returns message buffer on success, NULL on error. */
Philipp Maier1dc6be62017-10-05 18:25:37 +02001212struct msgb *mgcp_msg_gen(struct mgcp_client *mgcp, struct mgcp_msg *mgcp_msg)
1213{
1214 mgcp_trans_id_t trans_id = mgcp_client_next_trans_id(mgcp);
1215 uint32_t mandatory_mask;
1216 struct msgb *msg = msgb_alloc_headroom(4096, 128, "MGCP tx");
1217 int rc = 0;
Philipp Maier704c4f02018-06-07 18:51:31 +02001218 int rc_sdp;
1219 bool use_sdp = false;
Philipp Maier1dc6be62017-10-05 18:25:37 +02001220
1221 msg->l2h = msg->data;
1222 msg->cb[MSGB_CB_MGCP_TRANS_ID] = trans_id;
1223
1224 /* Add command verb */
1225 switch (mgcp_msg->verb) {
1226 case MGCP_VERB_CRCX:
1227 mandatory_mask = MGCP_CRCX_MANDATORY;
1228 rc += msgb_printf(msg, "CRCX %u", trans_id);
1229 break;
1230 case MGCP_VERB_MDCX:
1231 mandatory_mask = MGCP_MDCX_MANDATORY;
1232 rc += msgb_printf(msg, "MDCX %u", trans_id);
1233 break;
1234 case MGCP_VERB_DLCX:
1235 mandatory_mask = MGCP_DLCX_MANDATORY;
1236 rc += msgb_printf(msg, "DLCX %u", trans_id);
1237 break;
1238 case MGCP_VERB_AUEP:
1239 mandatory_mask = MGCP_AUEP_MANDATORY;
1240 rc += msgb_printf(msg, "AUEP %u", trans_id);
1241 break;
1242 case MGCP_VERB_RSIP:
1243 mandatory_mask = MGCP_RSIP_MANDATORY;
1244 rc += msgb_printf(msg, "RSIP %u", trans_id);
1245 break;
1246 default:
1247 LOGP(DLMGCP, LOGL_ERROR,
1248 "Invalid command verb, can not generate MGCP message\n");
1249 msgb_free(msg);
1250 return NULL;
1251 }
1252
1253 /* Check if mandatory fields are missing */
1254 if (!((mgcp_msg->presence & mandatory_mask) == mandatory_mask)) {
1255 LOGP(DLMGCP, LOGL_ERROR,
1256 "One or more missing mandatory fields, can not generate MGCP message\n");
1257 msgb_free(msg);
1258 return NULL;
1259 }
1260
1261 /* Add endpoint name */
Philipp Maier7bc55522017-12-10 22:52:22 +01001262 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_ENDPOINT) {
1263 if (strlen(mgcp_msg->endpoint) <= 0) {
1264 LOGP(DLMGCP, LOGL_ERROR,
1265 "Empty endpoint name, can not generate MGCP message\n");
1266 msgb_free(msg);
1267 return NULL;
1268 }
Philipp Maier3aa81572018-01-31 14:13:45 +01001269
1270 if (strstr(mgcp_msg->endpoint, "@") == NULL) {
1271 LOGP(DLMGCP, LOGL_ERROR,
1272 "Endpoint name (%s) lacks separator (@), can not generate MGCP message\n",
1273 mgcp_msg->endpoint);
1274 msgb_free(msg);
1275 }
1276
Philipp Maier1dc6be62017-10-05 18:25:37 +02001277 rc += msgb_printf(msg, " %s", mgcp_msg->endpoint);
Philipp Maier7bc55522017-12-10 22:52:22 +01001278 }
Philipp Maier1dc6be62017-10-05 18:25:37 +02001279
1280 /* Add protocol version */
1281 rc += msgb_printf(msg, " MGCP 1.0\r\n");
1282
1283 /* Add call id */
1284 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_CALL_ID)
1285 rc += msgb_printf(msg, "C: %x\r\n", mgcp_msg->call_id);
1286
1287 /* Add connection id */
Philipp Maier7bc55522017-12-10 22:52:22 +01001288 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_CONN_ID) {
1289 if (strlen(mgcp_msg->conn_id) <= 0) {
1290 LOGP(DLMGCP, LOGL_ERROR,
1291 "Empty connection id, can not generate MGCP message\n");
1292 msgb_free(msg);
1293 return NULL;
1294 }
Philipp Maier01d24a32017-11-21 17:26:09 +01001295 rc += msgb_printf(msg, "I: %s\r\n", mgcp_msg->conn_id);
Philipp Maier7bc55522017-12-10 22:52:22 +01001296 }
Philipp Maier1dc6be62017-10-05 18:25:37 +02001297
Philipp Maier704c4f02018-06-07 18:51:31 +02001298 /* Using SDP makes sense when a valid IP/Port combination is specifiec,
1299 * if we do not know this information yet, we fall back to LCO */
1300 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_AUDIO_IP
1301 && mgcp_msg->presence & MGCP_MSG_PRESENCE_AUDIO_PORT)
1302 use_sdp = true;
1303
1304 /* Add local connection options (LCO) */
1305 if (!use_sdp
1306 && (mgcp_msg->verb == MGCP_VERB_CRCX
1307 || mgcp_msg->verb == MGCP_VERB_MDCX))
1308 rc += add_lco(msg, mgcp_msg);
Philipp Maier1dc6be62017-10-05 18:25:37 +02001309
1310 /* Add mode */
1311 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_CONN_MODE)
1312 rc +=
1313 msgb_printf(msg, "M: %s\r\n",
1314 mgcp_client_cmode_name(mgcp_msg->conn_mode));
1315
Philipp Maier704c4f02018-06-07 18:51:31 +02001316 /* Add session description protocol (SDP) */
1317 if (use_sdp
1318 && (mgcp_msg->verb == MGCP_VERB_CRCX
1319 || mgcp_msg->verb == MGCP_VERB_MDCX)) {
1320 rc_sdp = add_sdp(msg, mgcp_msg, mgcp);
1321 if (rc_sdp == -2)
Philipp Maier9d25d7a2018-01-22 17:31:10 +01001322 return NULL;
Philipp Maier704c4f02018-06-07 18:51:31 +02001323 else
1324 rc += rc_sdp;
Philipp Maier1dc6be62017-10-05 18:25:37 +02001325 }
1326
1327 if (rc != 0) {
1328 LOGP(DLMGCP, LOGL_ERROR,
1329 "message buffer to small, can not generate MGCP message\n");
1330 msgb_free(msg);
1331 msg = NULL;
1332 }
1333
1334 return msg;
1335}
1336
Philipp Maier275ac972018-01-20 00:58:16 +01001337/*! Retrieve the MGCP transaction ID from a msgb generated by mgcp_msg_gen()
1338 * \param[in] msg message buffer
1339 * \returns Transaction id. */
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +01001340mgcp_trans_id_t mgcp_msg_trans_id(struct msgb *msg)
1341{
1342 return (mgcp_trans_id_t)msg->cb[MSGB_CB_MGCP_TRANS_ID];
1343}
1344
Philipp Maier275ac972018-01-20 00:58:16 +01001345/*! Get the configuration parameters a given MGCP client instance
1346 * \param[in] mgcp MGCP client descriptor.
1347 * \returns configuration */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +02001348struct mgcp_client_conf *mgcp_client_conf_actual(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001349{
1350 return &mgcp->actual;
1351}
Neels Hofmeyrd95ab1e2017-09-22 00:52:54 +02001352
1353const struct value_string mgcp_client_connection_mode_strs[] = {
1354 { MGCP_CONN_NONE, "none" },
1355 { MGCP_CONN_RECV_SEND, "sendrecv" },
1356 { MGCP_CONN_SEND_ONLY, "sendonly" },
1357 { MGCP_CONN_RECV_ONLY, "recvonly" },
1358 { MGCP_CONN_LOOPBACK, "loopback" },
1359 { 0, NULL }
1360};