blob: 00b52f8d0fa9fc2d62398f1922e3b35f2c33e158 [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 Maier275ac972018-01-20 00:58:16 +0100782/*! Initalize client connection (opens socket only, no request is sent yet)
783 * \param[in,out] mgcp MGCP client descriptor.
784 * \returns 0 on success, -EINVAL on error. */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200785int mgcp_client_connect(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200786{
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200787 struct sockaddr_in addr;
788 struct osmo_wqueue *wq;
789 int rc;
790
791 if (!mgcp) {
792 LOGP(DLMGCP, LOGL_FATAL, "MGCPGW client not initialized properly\n");
793 return -EINVAL;
794 }
795
796 wq = &mgcp->wq;
797
Harald Welte8890dfa2017-11-17 15:09:30 +0100798 rc = osmo_sock_init2_ofd(&wq->bfd, AF_INET, SOCK_DGRAM, IPPROTO_UDP,
799 mgcp->actual.local_addr, mgcp->actual.local_port,
800 mgcp->actual.remote_addr, mgcp->actual.remote_port,
801 OSMO_SOCK_F_BIND | OSMO_SOCK_F_CONNECT);
802 if (rc < 0) {
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200803 LOGP(DLMGCP, LOGL_FATAL,
Harald Welte8890dfa2017-11-17 15:09:30 +0100804 "Failed to initialize socket %s:%u -> %s:%u for MGCP GW: %s\n",
805 mgcp->actual.local_addr, mgcp->actual.local_port,
806 mgcp->actual.remote_addr, mgcp->actual.remote_port, strerror(errno));
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200807 goto error_close_fd;
808 }
809
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200810 inet_aton(mgcp->actual.remote_addr, &addr.sin_addr);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200811 mgcp->remote_addr = htonl(addr.sin_addr.s_addr);
812
813 osmo_wqueue_init(wq, 10);
814 wq->bfd.when = BSC_FD_READ;
815 wq->bfd.data = mgcp;
816 wq->read_cb = mgcp_do_read;
817 wq->write_cb = mgcp_do_write;
818
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200819 LOGP(DLMGCP, LOGL_INFO, "MGCP GW connection: %s:%u -> %s:%u\n",
820 mgcp->actual.local_addr, mgcp->actual.local_port,
821 mgcp->actual.remote_addr, mgcp->actual.remote_port);
822
823 return 0;
824error_close_fd:
825 close(wq->bfd.fd);
826 wq->bfd.fd = -1;
827 return rc;
828}
829
Philipp Maier275ac972018-01-20 00:58:16 +0100830/*! Get the IP-Aaddress of the associated MGW as string.
831 * \param[in] mgcp MGCP client descriptor.
832 * \returns a pointer to the address string. */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200833const char *mgcp_client_remote_addr_str(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200834{
835 return mgcp->actual.remote_addr;
836}
837
Philipp Maier275ac972018-01-20 00:58:16 +0100838/*! Get the IP-Port of the associated MGW.
839 * \param[in] mgcp MGCP client descriptor.
840 * \returns port number. */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200841uint16_t mgcp_client_remote_port(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200842{
843 return mgcp->actual.remote_port;
844}
845
Philipp Maier275ac972018-01-20 00:58:16 +0100846/*! Get the IP-Aaddress of the associated MGW as its numeric representation.
847 * \param[in] mgcp MGCP client descriptor.
848 * \returns IP-Address as 32 bit integer (network byte order) */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200849uint32_t mgcp_client_remote_addr_n(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200850{
851 return mgcp->remote_addr;
852}
853
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200854struct mgcp_response_pending * mgcp_client_pending_add(
855 struct mgcp_client *mgcp,
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200856 mgcp_trans_id_t trans_id,
857 mgcp_response_cb_t response_cb,
858 void *priv)
859{
860 struct mgcp_response_pending *pending;
861
862 pending = talloc_zero(mgcp, struct mgcp_response_pending);
863 pending->trans_id = trans_id;
864 pending->response_cb = response_cb;
865 pending->priv = priv;
866 llist_add_tail(&pending->entry, &mgcp->responses_pending);
867
868 return pending;
869}
870
871/* Send the MGCP message in msg to the MGCP GW and handle a response with
872 * response_cb. NOTE: the response_cb still needs to call
873 * mgcp_response_parse_params(response) to get the parsed parameters -- to
874 * potentially save some CPU cycles, only the head line has been parsed when
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100875 * the response_cb is invoked.
876 * Before the priv pointer becomes invalid, e.g. due to transaction timeout,
877 * mgcp_client_cancel() needs to be called for this transaction.
878 */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200879int mgcp_client_tx(struct mgcp_client *mgcp, struct msgb *msg,
880 mgcp_response_cb_t response_cb, void *priv)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200881{
882 struct mgcp_response_pending *pending;
883 mgcp_trans_id_t trans_id;
884 int rc;
885
886 trans_id = msg->cb[MSGB_CB_MGCP_TRANS_ID];
887 if (!trans_id) {
888 LOGP(DLMGCP, LOGL_ERROR,
889 "Unset transaction id in mgcp send request\n");
890 talloc_free(msg);
891 return -EINVAL;
892 }
893
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200894 pending = mgcp_client_pending_add(mgcp, trans_id, response_cb, priv);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200895
896 if (msgb_l2len(msg) > 4096) {
897 LOGP(DLMGCP, LOGL_ERROR,
898 "Cannot send, MGCP message too large: %u\n",
899 msgb_l2len(msg));
900 msgb_free(msg);
901 rc = -EINVAL;
902 goto mgcp_tx_error;
903 }
904
905 rc = osmo_wqueue_enqueue(&mgcp->wq, msg);
906 if (rc) {
907 LOGP(DLMGCP, LOGL_FATAL, "Could not queue message to MGCP GW\n");
908 msgb_free(msg);
909 goto mgcp_tx_error;
910 } else
911 LOGP(DLMGCP, LOGL_INFO, "Queued %u bytes for MGCP GW\n",
912 msgb_l2len(msg));
913 return 0;
914
915mgcp_tx_error:
916 /* Pass NULL to response cb to indicate an error */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200917 mgcp_client_handle_response(mgcp, pending, NULL);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200918 return -1;
919}
920
Philipp Maier275ac972018-01-20 00:58:16 +0100921/*! Cancel a pending transaction.
922 * \param[in] mgcp MGCP client descriptor.
923 * \param[in,out] trans_id Transaction id.
924 * \returns 0 on success, -ENOENT on error.
925 *
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100926 * Should a priv pointer passed to mgcp_client_tx() become invalid, this function must be called. In
927 * practical terms, if the caller of mgcp_client_tx() wishes to tear down a transaction without having
928 * received a response this function must be called. The trans_id can be obtained by calling
Philipp Maier275ac972018-01-20 00:58:16 +0100929 * mgcp_msg_trans_id() on the msgb produced by mgcp_msg_gen(). */
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100930int mgcp_client_cancel(struct mgcp_client *mgcp, mgcp_trans_id_t trans_id)
931{
932 struct mgcp_response_pending *pending = mgcp_client_response_pending_get(mgcp, trans_id);
933 if (!pending) {
Philipp Maier275ac972018-01-20 00:58:16 +0100934 /*! Note: it is not harmful to cancel a transaction twice. */
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100935 LOGP(DLMGCP, LOGL_INFO, "Cannot cancel, no such transaction: %u\n", trans_id);
936 return -ENOENT;
937 }
938 LOGP(DLMGCP, LOGL_INFO, "Canceled transaction %u\n", trans_id);
939 talloc_free(pending);
940 return 0;
Philipp Maier275ac972018-01-20 00:58:16 +0100941 /*! We don't really need to clean up the wqueue: In all sane cases, the msgb has already been sent
942 * out and is no longer in the wqueue. If it still is in the wqueue, then sending MGCP messages
943 * per se is broken and the program should notice so by a full wqueue. Even if this was called
944 * before we had a chance to send out the message and it is still going to be sent, we will just
945 * ignore the reply to it later. Removing a msgb from the wqueue here would just introduce more
946 * bug surface in terms of failing to update wqueue API's counters or some such.
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100947 */
948}
949
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200950static struct msgb *mgcp_msg_from_buf(mgcp_trans_id_t trans_id,
951 const char *buf, int len)
952{
953 struct msgb *msg;
954
955 if (len > (4096 - 128)) {
956 LOGP(DLMGCP, LOGL_ERROR, "Cannot send to MGCP GW:"
957 " message too large: %d\n", len);
958 return NULL;
959 }
960
961 msg = msgb_alloc_headroom(4096, 128, "MGCP tx");
962 OSMO_ASSERT(msg);
963
964 char *dst = (char*)msgb_put(msg, len);
965 memcpy(dst, buf, len);
966 msg->l2h = msg->data;
967 msg->cb[MSGB_CB_MGCP_TRANS_ID] = trans_id;
968
969 return msg;
970}
971
972static struct msgb *mgcp_msg_from_str(mgcp_trans_id_t trans_id,
973 const char *fmt, ...)
974{
975 static char compose[4096 - 128];
976 va_list ap;
977 int len;
978 OSMO_ASSERT(fmt);
979
980 va_start(ap, fmt);
981 len = vsnprintf(compose, sizeof(compose), fmt, ap);
982 va_end(ap);
983 if (len >= sizeof(compose)) {
984 LOGP(DLMGCP, LOGL_ERROR,
985 "Message too large: trans_id=%u len=%d\n",
986 trans_id, len);
987 return NULL;
988 }
989 if (len < 1) {
990 LOGP(DLMGCP, LOGL_ERROR,
991 "Failed to compose message: trans_id=%u len=%d\n",
992 trans_id, len);
993 return NULL;
994 }
995 return mgcp_msg_from_buf(trans_id, compose, len);
996}
997
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200998static mgcp_trans_id_t mgcp_client_next_trans_id(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200999{
1000 /* avoid zero trans_id to distinguish from unset trans_id */
1001 if (!mgcp->next_trans_id)
1002 mgcp->next_trans_id ++;
1003 return mgcp->next_trans_id ++;
1004}
1005
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +02001006struct msgb *mgcp_msg_crcx(struct mgcp_client *mgcp,
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001007 uint16_t rtp_endpoint, unsigned int call_id,
1008 enum mgcp_connection_mode mode)
1009{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +02001010 mgcp_trans_id_t trans_id = mgcp_client_next_trans_id(mgcp);
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001011 return mgcp_msg_from_str(trans_id,
1012 "CRCX %u %x@mgw MGCP 1.0\r\n"
1013 "C: %x\r\n"
1014 "L: p:20, a:AMR, nt:IN\r\n"
1015 "M: %s\r\n"
1016 ,
1017 trans_id,
1018 rtp_endpoint,
1019 call_id,
Neels Hofmeyrd95ab1e2017-09-22 00:52:54 +02001020 mgcp_client_cmode_name(mode));
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001021}
1022
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +02001023struct msgb *mgcp_msg_mdcx(struct mgcp_client *mgcp,
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001024 uint16_t rtp_endpoint, const char *rtp_conn_addr,
1025 uint16_t rtp_port, enum mgcp_connection_mode mode)
1026
1027{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +02001028 mgcp_trans_id_t trans_id = mgcp_client_next_trans_id(mgcp);
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001029 return mgcp_msg_from_str(trans_id,
1030 "MDCX %u %x@mgw MGCP 1.0\r\n"
1031 "M: %s\r\n"
1032 "\r\n"
1033 "c=IN IP4 %s\r\n"
1034 "m=audio %u RTP/AVP 255\r\n"
1035 ,
1036 trans_id,
1037 rtp_endpoint,
Neels Hofmeyrd95ab1e2017-09-22 00:52:54 +02001038 mgcp_client_cmode_name(mode),
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001039 rtp_conn_addr,
1040 rtp_port);
1041}
1042
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +02001043struct msgb *mgcp_msg_dlcx(struct mgcp_client *mgcp, uint16_t rtp_endpoint,
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001044 unsigned int call_id)
1045{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +02001046 mgcp_trans_id_t trans_id = mgcp_client_next_trans_id(mgcp);
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001047 return mgcp_msg_from_str(trans_id,
1048 "DLCX %u %x@mgw MGCP 1.0\r\n"
1049 "C: %x\r\n", trans_id, rtp_endpoint, call_id);
1050}
1051
Philipp Maier1dc6be62017-10-05 18:25:37 +02001052#define MGCP_CRCX_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT | \
1053 MGCP_MSG_PRESENCE_CALL_ID | \
Philipp Maier1dc6be62017-10-05 18:25:37 +02001054 MGCP_MSG_PRESENCE_CONN_MODE)
1055#define MGCP_MDCX_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT | \
Philipp Maier490cbaa2018-01-22 17:32:38 +01001056 MGCP_MSG_PRESENCE_CALL_ID | \
Philipp Maier1dc6be62017-10-05 18:25:37 +02001057 MGCP_MSG_PRESENCE_CONN_ID)
1058#define MGCP_DLCX_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT)
1059#define MGCP_AUEP_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT)
1060#define MGCP_RSIP_MANDATORY 0 /* none */
1061
Philipp Maier704c4f02018-06-07 18:51:31 +02001062/* Helper function for mgcp_msg_gen(): Add LCO information to MGCP message */
1063static int add_lco(struct msgb *msg, struct mgcp_msg *mgcp_msg)
1064{
1065 unsigned int i;
1066 int rc = 0;
1067 const char *codec;
1068 unsigned int pt;
1069
1070 rc += msgb_printf(msg, "L:");
1071
1072 if (mgcp_msg->ptime)
1073 rc += msgb_printf(msg, " p:%u,", mgcp_msg->ptime);
1074
1075 if (mgcp_msg->codecs_len) {
1076 rc += msgb_printf(msg, " a:");
1077 for (i = 0; i < mgcp_msg->codecs_len; i++) {
1078 pt = mgcp_msg->codecs[i];
1079 codec = get_value_string_or_null(codec_table, pt);
1080
1081 /* Note: Use codec descriptors from enum mgcp_codecs
1082 * in mgcp_client only! */
1083 OSMO_ASSERT(codec);
1084 rc += msgb_printf(msg, "%s", extract_codec_name(codec));
1085 if (i < mgcp_msg->codecs_len - 1)
1086 rc += msgb_printf(msg, ";");
1087 }
1088 rc += msgb_printf(msg, ",");
1089 }
1090
1091 rc += msgb_printf(msg, " nt:IN\r\n");
1092
1093 return rc;
1094}
1095
1096/* Helper function for mgcp_msg_gen(): Add SDP information to MGCP message */
1097static int add_sdp(struct msgb *msg, struct mgcp_msg *mgcp_msg, struct mgcp_client *mgcp)
1098{
1099 unsigned int i;
1100 int rc = 0;
1101 char local_ip[INET_ADDRSTRLEN];
1102 const char *codec;
1103 unsigned int pt;
1104
1105 /* Add separator to mark the beginning of the SDP block */
1106 rc += msgb_printf(msg, "\r\n");
1107
1108 /* Add SDP protocol version */
1109 rc += msgb_printf(msg, "v=0\r\n");
1110
1111 /* Determine local IP-Address */
1112 if (osmo_sock_local_ip(local_ip, mgcp->actual.remote_addr) < 0) {
1113 LOGP(DLMGCP, LOGL_ERROR,
1114 "Could not determine local IP-Address!\n");
1115 msgb_free(msg);
1116 return -2;
1117 }
1118
1119 /* Add owner/creator (SDP) */
1120 rc += msgb_printf(msg, "o=- %x 23 IN IP4 %s\r\n",
1121 mgcp_msg->call_id, local_ip);
1122
1123 /* Add session name (none) */
1124 rc += msgb_printf(msg, "s=-\r\n");
1125
1126 /* Add RTP address and port */
1127 if (mgcp_msg->audio_port == 0) {
1128 LOGP(DLMGCP, LOGL_ERROR,
1129 "Invalid port number, can not generate MGCP message\n");
1130 msgb_free(msg);
1131 return -2;
1132 }
1133 if (strlen(mgcp_msg->audio_ip) <= 0) {
1134 LOGP(DLMGCP, LOGL_ERROR,
1135 "Empty ip address, can not generate MGCP message\n");
1136 msgb_free(msg);
1137 return -2;
1138 }
1139 rc += msgb_printf(msg, "c=IN IP4 %s\r\n", mgcp_msg->audio_ip);
1140
1141 /* Add time description, active time (SDP) */
1142 rc += msgb_printf(msg, "t=0 0\r\n");
1143
1144 rc += msgb_printf(msg, "m=audio %u RTP/AVP", mgcp_msg->audio_port);
1145 for (i = 0; i < mgcp_msg->codecs_len; i++) {
1146 pt = map_codec_to_pt(mgcp_msg->ptmap, mgcp_msg->ptmap_len, mgcp_msg->codecs[i]);
1147 rc += msgb_printf(msg, " %u", pt);
1148
1149 }
1150 rc += msgb_printf(msg, "\r\n");
1151
1152 for (i = 0; i < mgcp_msg->codecs_len; i++) {
1153 pt = map_codec_to_pt(mgcp_msg->ptmap, mgcp_msg->ptmap_len, mgcp_msg->codecs[i]);
1154
1155 /* Note: Only dynamic payload type from the range 96-127
1156 * require to be explained further via rtpmap. All others
1157 * are implcitly definedby the number in m=audio */
1158 if (pt >= 96 && pt <= 127) {
1159 codec = get_value_string_or_null(codec_table, mgcp_msg->codecs[i]);
1160
1161 /* Note: Use codec descriptors from enum mgcp_codecs
1162 * in mgcp_client only! */
1163 OSMO_ASSERT(codec);
1164
1165 rc += msgb_printf(msg, "a=rtpmap:%u %s\r\n", pt, codec);
1166 }
1167 }
1168
1169 if (mgcp_msg->ptime)
1170 rc += msgb_printf(msg, "a=ptime:%u\r\n", mgcp_msg->ptime);
1171
1172 return rc;
1173}
1174
Philipp Maier275ac972018-01-20 00:58:16 +01001175/*! Generate an MGCP message
1176 * \param[in] mgcp MGCP client descriptor.
1177 * \param[in] mgcp_msg Message description
1178 * \returns message buffer on success, NULL on error. */
Philipp Maier1dc6be62017-10-05 18:25:37 +02001179struct msgb *mgcp_msg_gen(struct mgcp_client *mgcp, struct mgcp_msg *mgcp_msg)
1180{
1181 mgcp_trans_id_t trans_id = mgcp_client_next_trans_id(mgcp);
1182 uint32_t mandatory_mask;
1183 struct msgb *msg = msgb_alloc_headroom(4096, 128, "MGCP tx");
1184 int rc = 0;
Philipp Maier704c4f02018-06-07 18:51:31 +02001185 int rc_sdp;
1186 bool use_sdp = false;
Philipp Maier1dc6be62017-10-05 18:25:37 +02001187
1188 msg->l2h = msg->data;
1189 msg->cb[MSGB_CB_MGCP_TRANS_ID] = trans_id;
1190
1191 /* Add command verb */
1192 switch (mgcp_msg->verb) {
1193 case MGCP_VERB_CRCX:
1194 mandatory_mask = MGCP_CRCX_MANDATORY;
1195 rc += msgb_printf(msg, "CRCX %u", trans_id);
1196 break;
1197 case MGCP_VERB_MDCX:
1198 mandatory_mask = MGCP_MDCX_MANDATORY;
1199 rc += msgb_printf(msg, "MDCX %u", trans_id);
1200 break;
1201 case MGCP_VERB_DLCX:
1202 mandatory_mask = MGCP_DLCX_MANDATORY;
1203 rc += msgb_printf(msg, "DLCX %u", trans_id);
1204 break;
1205 case MGCP_VERB_AUEP:
1206 mandatory_mask = MGCP_AUEP_MANDATORY;
1207 rc += msgb_printf(msg, "AUEP %u", trans_id);
1208 break;
1209 case MGCP_VERB_RSIP:
1210 mandatory_mask = MGCP_RSIP_MANDATORY;
1211 rc += msgb_printf(msg, "RSIP %u", trans_id);
1212 break;
1213 default:
1214 LOGP(DLMGCP, LOGL_ERROR,
1215 "Invalid command verb, can not generate MGCP message\n");
1216 msgb_free(msg);
1217 return NULL;
1218 }
1219
1220 /* Check if mandatory fields are missing */
1221 if (!((mgcp_msg->presence & mandatory_mask) == mandatory_mask)) {
1222 LOGP(DLMGCP, LOGL_ERROR,
1223 "One or more missing mandatory fields, can not generate MGCP message\n");
1224 msgb_free(msg);
1225 return NULL;
1226 }
1227
1228 /* Add endpoint name */
Philipp Maier7bc55522017-12-10 22:52:22 +01001229 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_ENDPOINT) {
1230 if (strlen(mgcp_msg->endpoint) <= 0) {
1231 LOGP(DLMGCP, LOGL_ERROR,
1232 "Empty endpoint name, can not generate MGCP message\n");
1233 msgb_free(msg);
1234 return NULL;
1235 }
Philipp Maier3aa81572018-01-31 14:13:45 +01001236
1237 if (strstr(mgcp_msg->endpoint, "@") == NULL) {
1238 LOGP(DLMGCP, LOGL_ERROR,
1239 "Endpoint name (%s) lacks separator (@), can not generate MGCP message\n",
1240 mgcp_msg->endpoint);
1241 msgb_free(msg);
1242 }
1243
Philipp Maier1dc6be62017-10-05 18:25:37 +02001244 rc += msgb_printf(msg, " %s", mgcp_msg->endpoint);
Philipp Maier7bc55522017-12-10 22:52:22 +01001245 }
Philipp Maier1dc6be62017-10-05 18:25:37 +02001246
1247 /* Add protocol version */
1248 rc += msgb_printf(msg, " MGCP 1.0\r\n");
1249
1250 /* Add call id */
1251 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_CALL_ID)
1252 rc += msgb_printf(msg, "C: %x\r\n", mgcp_msg->call_id);
1253
1254 /* Add connection id */
Philipp Maier7bc55522017-12-10 22:52:22 +01001255 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_CONN_ID) {
1256 if (strlen(mgcp_msg->conn_id) <= 0) {
1257 LOGP(DLMGCP, LOGL_ERROR,
1258 "Empty connection id, can not generate MGCP message\n");
1259 msgb_free(msg);
1260 return NULL;
1261 }
Philipp Maier01d24a32017-11-21 17:26:09 +01001262 rc += msgb_printf(msg, "I: %s\r\n", mgcp_msg->conn_id);
Philipp Maier7bc55522017-12-10 22:52:22 +01001263 }
Philipp Maier1dc6be62017-10-05 18:25:37 +02001264
Philipp Maier704c4f02018-06-07 18:51:31 +02001265 /* Using SDP makes sense when a valid IP/Port combination is specifiec,
1266 * if we do not know this information yet, we fall back to LCO */
1267 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_AUDIO_IP
1268 && mgcp_msg->presence & MGCP_MSG_PRESENCE_AUDIO_PORT)
1269 use_sdp = true;
1270
1271 /* Add local connection options (LCO) */
1272 if (!use_sdp
1273 && (mgcp_msg->verb == MGCP_VERB_CRCX
1274 || mgcp_msg->verb == MGCP_VERB_MDCX))
1275 rc += add_lco(msg, mgcp_msg);
Philipp Maier1dc6be62017-10-05 18:25:37 +02001276
1277 /* Add mode */
1278 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_CONN_MODE)
1279 rc +=
1280 msgb_printf(msg, "M: %s\r\n",
1281 mgcp_client_cmode_name(mgcp_msg->conn_mode));
1282
Philipp Maier704c4f02018-06-07 18:51:31 +02001283 /* Add session description protocol (SDP) */
1284 if (use_sdp
1285 && (mgcp_msg->verb == MGCP_VERB_CRCX
1286 || mgcp_msg->verb == MGCP_VERB_MDCX)) {
1287 rc_sdp = add_sdp(msg, mgcp_msg, mgcp);
1288 if (rc_sdp == -2)
Philipp Maier9d25d7a2018-01-22 17:31:10 +01001289 return NULL;
Philipp Maier704c4f02018-06-07 18:51:31 +02001290 else
1291 rc += rc_sdp;
Philipp Maier1dc6be62017-10-05 18:25:37 +02001292 }
1293
1294 if (rc != 0) {
1295 LOGP(DLMGCP, LOGL_ERROR,
1296 "message buffer to small, can not generate MGCP message\n");
1297 msgb_free(msg);
1298 msg = NULL;
1299 }
1300
1301 return msg;
1302}
1303
Philipp Maier275ac972018-01-20 00:58:16 +01001304/*! Retrieve the MGCP transaction ID from a msgb generated by mgcp_msg_gen()
1305 * \param[in] msg message buffer
1306 * \returns Transaction id. */
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +01001307mgcp_trans_id_t mgcp_msg_trans_id(struct msgb *msg)
1308{
1309 return (mgcp_trans_id_t)msg->cb[MSGB_CB_MGCP_TRANS_ID];
1310}
1311
Philipp Maier275ac972018-01-20 00:58:16 +01001312/*! Get the configuration parameters a given MGCP client instance
1313 * \param[in] mgcp MGCP client descriptor.
1314 * \returns configuration */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +02001315struct mgcp_client_conf *mgcp_client_conf_actual(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +02001316{
1317 return &mgcp->actual;
1318}
Neels Hofmeyrd95ab1e2017-09-22 00:52:54 +02001319
1320const struct value_string mgcp_client_connection_mode_strs[] = {
1321 { MGCP_CONN_NONE, "none" },
1322 { MGCP_CONN_RECV_SEND, "sendrecv" },
1323 { MGCP_CONN_SEND_ONLY, "sendonly" },
1324 { MGCP_CONN_RECV_ONLY, "recvonly" },
1325 { MGCP_CONN_LOOPBACK, "loopback" },
1326 { 0, NULL }
1327};