blob: 0cf5080741b12cfe79616cb98e2c1e4b5f7f743c [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
7 * it under the terms of the GNU Affero General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (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
14 * GNU Affero General Public License for more details.
15 *
16 * You should have received a copy of the GNU Affero General Public License
17 * 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
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +020039void mgcp_client_conf_init(struct mgcp_client_conf *conf)
Neels Hofmeyre9920f22017-07-10 15:07:22 +020040{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +020041 /* NULL and -1 default to MGCP_CLIENT_*_DEFAULT values */
42 *conf = (struct mgcp_client_conf){
Neels Hofmeyre9920f22017-07-10 15:07:22 +020043 .local_addr = NULL,
44 .local_port = -1,
45 .remote_addr = NULL,
46 .remote_port = -1,
47 .first_endpoint = 0,
48 .last_endpoint = 0,
Neels Hofmeyrc0dcc3c2017-12-02 18:31:34 +000049 .bts_base = 0,
Neels Hofmeyre9920f22017-07-10 15:07:22 +020050 };
51}
52
53/* Test if a given endpoint id is currently in use */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +020054static bool endpoint_in_use(uint16_t id, struct mgcp_client *client)
Neels Hofmeyre9920f22017-07-10 15:07:22 +020055{
56 struct mgcp_inuse_endpoint *endpoint;
57 llist_for_each_entry(endpoint, &client->inuse_endpoints, entry) {
58 if (endpoint->id == id)
59 return true;
60 }
61
62 return false;
63}
64
65/* Find and seize an unsused endpoint id */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +020066int mgcp_client_next_endpoint(struct mgcp_client *client)
Neels Hofmeyre9920f22017-07-10 15:07:22 +020067{
68 int i;
69 uint16_t first_endpoint = client->actual.first_endpoint;
70 uint16_t last_endpoint = client->actual.last_endpoint;
71 struct mgcp_inuse_endpoint *endpoint;
72
73 /* Use the maximum permitted range if the VTY
74 * configuration does not specify a range */
75 if (client->actual.last_endpoint == 0) {
76 first_endpoint = 1;
77 last_endpoint = 65534;
78 }
79
80 /* Test the permitted endpoint range for an endpoint
81 * number that is not in use. When a suitable endpoint
82 * number can be found, seize it by adding it to the
83 * inuse list. */
84 for (i=first_endpoint;i<last_endpoint;i++)
85 {
86 if (endpoint_in_use(i,client) == false) {
87 endpoint = talloc_zero(client, struct mgcp_inuse_endpoint);
88 endpoint->id = i;
89 llist_add_tail(&endpoint->entry, &client->inuse_endpoints);
90 return endpoint->id;
91 }
92 }
93
94 /* All endpoints are busy! */
95 return -EINVAL;
96}
97
98/* Release a seized endpoint id to make it available again for other calls */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +020099void mgcp_client_release_endpoint(uint16_t id, struct mgcp_client *client)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200100{
101 struct mgcp_inuse_endpoint *endpoint;
102 struct mgcp_inuse_endpoint *endpoint_tmp;
103 llist_for_each_entry_safe(endpoint, endpoint_tmp, &client->inuse_endpoints, entry) {
104 if (endpoint->id == id) {
105 llist_del(&endpoint->entry);
106 talloc_free(endpoint);
107 }
108 }
109}
110
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200111static void mgcp_client_handle_response(struct mgcp_client *mgcp,
112 struct mgcp_response_pending *pending,
113 struct mgcp_response *response)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200114{
115 if (!pending) {
116 LOGP(DLMGCP, LOGL_ERROR,
117 "Cannot handle NULL response\n");
118 return;
119 }
120 if (pending->response_cb)
121 pending->response_cb(response, pending->priv);
122 else
123 LOGP(DLMGCP, LOGL_INFO, "MGCP response ignored (NULL cb)\n");
124 talloc_free(pending);
125}
126
127static int mgcp_response_parse_head(struct mgcp_response *r, struct msgb *msg)
128{
129 int comment_pos;
130 char *end;
131
132 if (mgcp_msg_terminate_nul(msg))
133 goto response_parse_failure;
134
135 r->body = (char *)msg->data;
136
Harald Welte9bf7c532017-11-17 14:14:31 +0100137 if (sscanf(r->body, "%3d %u %n",
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200138 &r->head.response_code, &r->head.trans_id,
139 &comment_pos) != 2)
140 goto response_parse_failure;
141
Philipp Maierabe8c892018-01-20 00:15:12 +0100142 osmo_strlcpy(r->head.comment, r->body + comment_pos, sizeof(r->head.comment));
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200143 end = strchr(r->head.comment, '\r');
144 if (!end)
145 goto response_parse_failure;
146 /* Mark the end of the comment */
147 *end = '\0';
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200148 return 0;
149
150response_parse_failure:
151 LOGP(DLMGCP, LOGL_ERROR,
152 "Failed to parse MGCP response header\n");
153 return -EINVAL;
154}
155
156/* TODO undup against mgcp_protocol.c:mgcp_check_param() */
157static bool mgcp_line_is_valid(const char *line)
158{
159 const size_t line_len = strlen(line);
160 if (line[0] == '\0')
161 return true;
162
163 if (line_len < 2
164 || line[1] != '=') {
165 LOGP(DLMGCP, LOGL_ERROR,
166 "Wrong MGCP option format: '%s'\n",
167 line);
168 return false;
169 }
170
171 return true;
172}
173
174/* Parse a line like "m=audio 16002 RTP/AVP 98" */
Philipp Maier06da85e2017-10-05 18:49:24 +0200175static int mgcp_parse_audio_port(struct mgcp_response *r, const char *line)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200176{
Harald Welte9bf7c532017-11-17 14:14:31 +0100177 if (sscanf(line, "m=audio %hu",
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200178 &r->audio_port) != 1)
179 goto response_parse_failure;
180
Philipp Maier10f32db2017-12-13 12:34:34 +0100181 if (r->audio_port == 0)
182 goto response_parse_failure;
183
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200184 return 0;
185
186response_parse_failure:
187 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier06da85e2017-10-05 18:49:24 +0200188 "Failed to parse MGCP response header (audio port)\n");
189 return -EINVAL;
190}
191
192/* Parse a line like "c=IN IP4 10.11.12.13" */
193static int mgcp_parse_audio_ip(struct mgcp_response *r, const char *line)
194{
195 struct in_addr ip_test;
196
197 if (strlen(line) < 16)
198 goto response_parse_failure;
199
200 /* The current implementation strictly supports IPV4 only ! */
201 if (memcmp("c=IN IP4 ", line, 9) != 0)
202 goto response_parse_failure;
203
204 /* Extract IP-Address */
Philipp Maierf8bfbe82017-11-23 19:32:31 +0100205 osmo_strlcpy(r->audio_ip, line + 9, sizeof(r->audio_ip));
Philipp Maier06da85e2017-10-05 18:49:24 +0200206
207 /* Check IP-Address */
208 if (inet_aton(r->audio_ip, &ip_test) == 0)
209 goto response_parse_failure;
210
211 return 0;
212
213response_parse_failure:
214 LOGP(DLMGCP, LOGL_ERROR,
215 "Failed to parse MGCP response header (audio ip)\n");
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200216 return -EINVAL;
217}
218
Philipp Maier3b12e1b2018-01-18 15:16:13 +0100219/* A new section is marked by a double line break, check a few more
220 * patterns as there may be variants */
221static char *mgcp_find_section_end(char *string)
222{
223 char *rc;
224
225 rc = strstr(string, "\n\n");
226 if (rc)
227 return rc;
228
229 rc = strstr(string, "\n\r\n\r");
230 if (rc)
231 return rc;
232
233 rc = strstr(string, "\r\n\r\n");
234 if (rc)
235 return rc;
236
237 return NULL;
238}
239
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200240int mgcp_response_parse_params(struct mgcp_response *r)
241{
242 char *line;
243 int rc;
244 OSMO_ASSERT(r->body);
Philipp Maier3b12e1b2018-01-18 15:16:13 +0100245 char *data = mgcp_find_section_end(r->body);
Philipp Maiere9d645b2018-01-19 23:54:08 +0100246 char *data_ptr;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200247
Philipp Maiere9d645b2018-01-19 23:54:08 +0100248 /* Since this functions performs a destructive parsing, we create a
249 * local copy of the body data */
250 data = talloc_zero_size(NULL, strlen(r->body)+1);
251 OSMO_ASSERT(data);
252 data_ptr = data;
253 osmo_strlcpy(data, r->body, strlen(r->body));
Philipp Maier55295f72018-01-15 14:00:28 +0100254
Philipp Maiere9d645b2018-01-19 23:54:08 +0100255 /* Find beginning of the parameter (SDP) section */
256 data_ptr = mgcp_find_section_end(data);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200257 if (!data) {
258 LOGP(DLMGCP, LOGL_ERROR,
259 "MGCP response: cannot find start of parameters\n");
Philipp Maiere9d645b2018-01-19 23:54:08 +0100260 rc = -EINVAL;
261 goto exit;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200262 }
263
Philipp Maiere9d645b2018-01-19 23:54:08 +0100264 for_each_non_empty_line(line, data_ptr) {
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200265 if (!mgcp_line_is_valid(line))
266 return -EINVAL;
267
268 switch (line[0]) {
269 case 'm':
Philipp Maier06da85e2017-10-05 18:49:24 +0200270 rc = mgcp_parse_audio_port(r, line);
271 if (rc)
Philipp Maiere9d645b2018-01-19 23:54:08 +0100272 goto exit;
Philipp Maier06da85e2017-10-05 18:49:24 +0200273 break;
274 case 'c':
275 rc = mgcp_parse_audio_ip(r, line);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200276 if (rc)
Philipp Maiere9d645b2018-01-19 23:54:08 +0100277 goto exit;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200278 break;
279 default:
280 /* skip unhandled parameters */
281 break;
282 }
283 }
Philipp Maiere9d645b2018-01-19 23:54:08 +0100284
285 rc = 0;
286exit:
287 talloc_free(data);
288 return rc;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200289}
290
Philipp Maier55295f72018-01-15 14:00:28 +0100291/* Parse a line like "X: something" */
292static int mgcp_parse_head_param(char *result, unsigned int result_len,
293 char label, const char *line)
Philipp Maierffd75e42017-11-22 11:44:50 +0100294{
Philipp Maier55295f72018-01-15 14:00:28 +0100295 char label_string[4];
296
297 /* Detect empty parameters */
Philipp Maierffd75e42017-11-22 11:44:50 +0100298 if (strlen(line) < 4)
299 goto response_parse_failure;
300
Philipp Maier55295f72018-01-15 14:00:28 +0100301 /* Check if the label matches */
302 snprintf(label_string, sizeof(label_string), "%c: ", label);
303 if (memcmp(label_string, line, 3) != 0)
Philipp Maierffd75e42017-11-22 11:44:50 +0100304 goto response_parse_failure;
305
Philipp Maier55295f72018-01-15 14:00:28 +0100306 /* Copy payload part of the string to destinations (the label string
307 * is always 3 chars long) */
308 osmo_strlcpy(result, line + 3, result_len);
Philipp Maierffd75e42017-11-22 11:44:50 +0100309 return 0;
310
311response_parse_failure:
312 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier55295f72018-01-15 14:00:28 +0100313 "Failed to parse MGCP response (parameter label: %c)\n", label);
Philipp Maierffd75e42017-11-22 11:44:50 +0100314 return -EINVAL;
315}
316
317/* Parse MGCP parameters of the response */
318static int parse_head_params(struct mgcp_response *r)
319{
320 char *line;
321 int rc = 0;
322 OSMO_ASSERT(r->body);
Philipp Maier55295f72018-01-15 14:00:28 +0100323 char *data;
324 char *data_ptr;
325 char *data_end;
Philipp Maierffd75e42017-11-22 11:44:50 +0100326
Philipp Maier55295f72018-01-15 14:00:28 +0100327 /* Since this functions performs a destructive parsing, we create a
328 * local copy of the body data */
329 data = talloc_zero_size(NULL, strlen(r->body)+1);
330 OSMO_ASSERT(data);
331 data_ptr = data;
332 osmo_strlcpy(data, r->body, strlen(r->body));
333
334 /* If there is an SDP body attached, prevent for_each_non_empty_line()
335 * into running in there, we are not yet interested in the parameters
336 * stored there. */
337 data_end = mgcp_find_section_end(data);
Philipp Maierffd75e42017-11-22 11:44:50 +0100338 if (data_end)
339 *data_end = '\0';
340
Philipp Maier55295f72018-01-15 14:00:28 +0100341 for_each_non_empty_line(line, data_ptr) {
Philipp Maierffd75e42017-11-22 11:44:50 +0100342 switch (line[0]) {
Philipp Maier55295f72018-01-15 14:00:28 +0100343 case 'Z':
344 rc = mgcp_parse_head_param(r->head.endpoint,
345 sizeof(r->head.endpoint),
346 'Z', line);
347 if (rc)
348 goto exit;
349 break;
Philipp Maierffd75e42017-11-22 11:44:50 +0100350 case 'I':
Philipp Maier55295f72018-01-15 14:00:28 +0100351 rc = mgcp_parse_head_param(r->head.conn_id,
352 sizeof(r->head.conn_id),
353 'I', line);
Philipp Maierffd75e42017-11-22 11:44:50 +0100354 if (rc)
355 goto exit;
356 break;
357 default:
358 /* skip unhandled parameters */
359 break;
360 }
361 }
362exit:
Philipp Maier55295f72018-01-15 14:00:28 +0100363 talloc_free(data);
Philipp Maierffd75e42017-11-22 11:44:50 +0100364 return rc;
365}
366
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200367static struct mgcp_response_pending *mgcp_client_response_pending_get(
368 struct mgcp_client *mgcp,
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100369 mgcp_trans_id_t trans_id)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200370{
371 struct mgcp_response_pending *pending;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200372 llist_for_each_entry(pending, &mgcp->responses_pending, entry) {
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100373 if (pending->trans_id == trans_id) {
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200374 llist_del(&pending->entry);
375 return pending;
376 }
377 }
378 return NULL;
379}
380
381/* Feed an MGCP message into the receive processing.
382 * Parse the head and call any callback registered for the transaction id found
383 * in the MGCP message. This is normally called directly from the internal
384 * mgcp_do_read that reads from the socket connected to the MGCP gateway. This
385 * function is published mainly to be able to feed data from the test suite.
386 */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200387int mgcp_client_rx(struct mgcp_client *mgcp, struct msgb *msg)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200388{
389 struct mgcp_response r = { 0 };
390 struct mgcp_response_pending *pending;
391 int rc;
392
393 rc = mgcp_response_parse_head(&r, msg);
394 if (rc) {
Philipp Maierffd75e42017-11-22 11:44:50 +0100395 LOGP(DLMGCP, LOGL_ERROR, "Cannot parse MGCP response (head)\n");
396 return -1;
397 }
398
399 rc = parse_head_params(&r);
400 if (rc) {
401 LOGP(DLMGCP, LOGL_ERROR, "Cannot parse MGCP response (head parameters)\n");
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200402 return -1;
403 }
404
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100405 pending = mgcp_client_response_pending_get(mgcp, r.head.trans_id);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200406 if (!pending) {
407 LOGP(DLMGCP, LOGL_ERROR,
408 "Cannot find matching MGCP transaction for trans_id %d\n",
409 r.head.trans_id);
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100410 return -ENOENT;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200411 }
412
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200413 mgcp_client_handle_response(mgcp, pending, &r);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200414 return 0;
415}
416
417static int mgcp_do_read(struct osmo_fd *fd)
418{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200419 struct mgcp_client *mgcp = fd->data;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200420 struct msgb *msg;
421 int ret;
422
423 msg = msgb_alloc_headroom(4096, 128, "mgcp_from_gw");
424 if (!msg) {
425 LOGP(DLMGCP, LOGL_ERROR, "Failed to allocate MGCP message.\n");
426 return -1;
427 }
428
429 ret = read(fd->fd, msg->data, 4096 - 128);
430 if (ret <= 0) {
431 LOGP(DLMGCP, LOGL_ERROR, "Failed to read: %d/%s\n", errno, strerror(errno));
432 msgb_free(msg);
433 return -1;
434 } else if (ret > 4096 - 128) {
435 LOGP(DLMGCP, LOGL_ERROR, "Too much data: %d\n", ret);
436 msgb_free(msg);
437 return -1;
Harald Welte9bf7c532017-11-17 14:14:31 +0100438 }
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200439
440 msg->l2h = msgb_put(msg, ret);
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200441 ret = mgcp_client_rx(mgcp, msg);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200442 talloc_free(msg);
443 return ret;
444}
445
446static int mgcp_do_write(struct osmo_fd *fd, struct msgb *msg)
447{
448 int ret;
449 static char strbuf[4096];
450 unsigned int l = msg->len < sizeof(strbuf) ? msg->len : sizeof(strbuf);
451 unsigned int i;
452
Philipp Maierf8bfbe82017-11-23 19:32:31 +0100453 osmo_strlcpy(strbuf, (const char*)msg->data, l);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200454 for (i = 0; i < sizeof(strbuf); i++) {
455 if (strbuf[i] == '\n' || strbuf[i] == '\r') {
456 strbuf[i] = '\0';
457 break;
458 }
459 }
460 DEBUGP(DLMGCP, "Tx MGCP msg to MGCP GW: '%s'\n", strbuf);
461
462 LOGP(DLMGCP, LOGL_DEBUG, "Sending msg to MGCP GW size: %u\n", msg->len);
463
464 ret = write(fd->fd, msg->data, msg->len);
465 if (ret != msg->len)
466 LOGP(DLMGCP, LOGL_ERROR, "Failed to forward message to MGCP"
467 " GW: %s\n", strerror(errno));
468
469 return ret;
470}
471
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200472struct mgcp_client *mgcp_client_init(void *ctx,
473 struct mgcp_client_conf *conf)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200474{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200475 struct mgcp_client *mgcp;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200476
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200477 mgcp = talloc_zero(ctx, struct mgcp_client);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200478
479 INIT_LLIST_HEAD(&mgcp->responses_pending);
480 INIT_LLIST_HEAD(&mgcp->inuse_endpoints);
481
482 mgcp->next_trans_id = 1;
483
484 mgcp->actual.local_addr = conf->local_addr ? conf->local_addr :
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200485 MGCP_CLIENT_LOCAL_ADDR_DEFAULT;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200486 mgcp->actual.local_port = conf->local_port >= 0 ? (uint16_t)conf->local_port :
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200487 MGCP_CLIENT_LOCAL_PORT_DEFAULT;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200488
489 mgcp->actual.remote_addr = conf->remote_addr ? conf->remote_addr :
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200490 MGCP_CLIENT_REMOTE_ADDR_DEFAULT;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200491 mgcp->actual.remote_port = conf->remote_port >= 0 ? (uint16_t)conf->remote_port :
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200492 MGCP_CLIENT_REMOTE_PORT_DEFAULT;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200493
494 mgcp->actual.first_endpoint = conf->first_endpoint > 0 ? (uint16_t)conf->first_endpoint : 0;
495 mgcp->actual.last_endpoint = conf->last_endpoint > 0 ? (uint16_t)conf->last_endpoint : 0;
Neels Hofmeyrc0dcc3c2017-12-02 18:31:34 +0000496 mgcp->actual.bts_base = conf->bts_base > 0 ? (uint16_t)conf->bts_base : 4000;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200497
498 return mgcp;
499}
500
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200501int mgcp_client_connect(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200502{
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200503 struct sockaddr_in addr;
504 struct osmo_wqueue *wq;
505 int rc;
506
507 if (!mgcp) {
508 LOGP(DLMGCP, LOGL_FATAL, "MGCPGW client not initialized properly\n");
509 return -EINVAL;
510 }
511
512 wq = &mgcp->wq;
513
Harald Welte8890dfa2017-11-17 15:09:30 +0100514 rc = osmo_sock_init2_ofd(&wq->bfd, AF_INET, SOCK_DGRAM, IPPROTO_UDP,
515 mgcp->actual.local_addr, mgcp->actual.local_port,
516 mgcp->actual.remote_addr, mgcp->actual.remote_port,
517 OSMO_SOCK_F_BIND | OSMO_SOCK_F_CONNECT);
518 if (rc < 0) {
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200519 LOGP(DLMGCP, LOGL_FATAL,
Harald Welte8890dfa2017-11-17 15:09:30 +0100520 "Failed to initialize socket %s:%u -> %s:%u for MGCP GW: %s\n",
521 mgcp->actual.local_addr, mgcp->actual.local_port,
522 mgcp->actual.remote_addr, mgcp->actual.remote_port, strerror(errno));
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200523 goto error_close_fd;
524 }
525
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200526 inet_aton(mgcp->actual.remote_addr, &addr.sin_addr);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200527 mgcp->remote_addr = htonl(addr.sin_addr.s_addr);
528
529 osmo_wqueue_init(wq, 10);
530 wq->bfd.when = BSC_FD_READ;
531 wq->bfd.data = mgcp;
532 wq->read_cb = mgcp_do_read;
533 wq->write_cb = mgcp_do_write;
534
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200535 LOGP(DLMGCP, LOGL_INFO, "MGCP GW connection: %s:%u -> %s:%u\n",
536 mgcp->actual.local_addr, mgcp->actual.local_port,
537 mgcp->actual.remote_addr, mgcp->actual.remote_port);
538
539 return 0;
540error_close_fd:
541 close(wq->bfd.fd);
542 wq->bfd.fd = -1;
543 return rc;
544}
545
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200546const char *mgcp_client_remote_addr_str(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200547{
548 return mgcp->actual.remote_addr;
549}
550
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200551uint16_t mgcp_client_remote_port(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200552{
553 return mgcp->actual.remote_port;
554}
555
556/* Return the MGCP GW binary IPv4 address in network byte order. */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200557uint32_t mgcp_client_remote_addr_n(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200558{
559 return mgcp->remote_addr;
560}
561
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200562struct mgcp_response_pending * mgcp_client_pending_add(
563 struct mgcp_client *mgcp,
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200564 mgcp_trans_id_t trans_id,
565 mgcp_response_cb_t response_cb,
566 void *priv)
567{
568 struct mgcp_response_pending *pending;
569
570 pending = talloc_zero(mgcp, struct mgcp_response_pending);
571 pending->trans_id = trans_id;
572 pending->response_cb = response_cb;
573 pending->priv = priv;
574 llist_add_tail(&pending->entry, &mgcp->responses_pending);
575
576 return pending;
577}
578
579/* Send the MGCP message in msg to the MGCP GW and handle a response with
580 * response_cb. NOTE: the response_cb still needs to call
581 * mgcp_response_parse_params(response) to get the parsed parameters -- to
582 * potentially save some CPU cycles, only the head line has been parsed when
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100583 * the response_cb is invoked.
584 * Before the priv pointer becomes invalid, e.g. due to transaction timeout,
585 * mgcp_client_cancel() needs to be called for this transaction.
586 */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200587int mgcp_client_tx(struct mgcp_client *mgcp, struct msgb *msg,
588 mgcp_response_cb_t response_cb, void *priv)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200589{
590 struct mgcp_response_pending *pending;
591 mgcp_trans_id_t trans_id;
592 int rc;
593
594 trans_id = msg->cb[MSGB_CB_MGCP_TRANS_ID];
595 if (!trans_id) {
596 LOGP(DLMGCP, LOGL_ERROR,
597 "Unset transaction id in mgcp send request\n");
598 talloc_free(msg);
599 return -EINVAL;
600 }
601
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200602 pending = mgcp_client_pending_add(mgcp, trans_id, response_cb, priv);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200603
604 if (msgb_l2len(msg) > 4096) {
605 LOGP(DLMGCP, LOGL_ERROR,
606 "Cannot send, MGCP message too large: %u\n",
607 msgb_l2len(msg));
608 msgb_free(msg);
609 rc = -EINVAL;
610 goto mgcp_tx_error;
611 }
612
613 rc = osmo_wqueue_enqueue(&mgcp->wq, msg);
614 if (rc) {
615 LOGP(DLMGCP, LOGL_FATAL, "Could not queue message to MGCP GW\n");
616 msgb_free(msg);
617 goto mgcp_tx_error;
618 } else
619 LOGP(DLMGCP, LOGL_INFO, "Queued %u bytes for MGCP GW\n",
620 msgb_l2len(msg));
621 return 0;
622
623mgcp_tx_error:
624 /* Pass NULL to response cb to indicate an error */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200625 mgcp_client_handle_response(mgcp, pending, NULL);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200626 return -1;
627}
628
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100629/* Cancel a pending transaction.
630 * Should a priv pointer passed to mgcp_client_tx() become invalid, this function must be called. In
631 * practical terms, if the caller of mgcp_client_tx() wishes to tear down a transaction without having
632 * received a response this function must be called. The trans_id can be obtained by calling
633 * mgcp_msg_trans_id() on the msgb produced by mgcp_msg_gen().
634 */
635int mgcp_client_cancel(struct mgcp_client *mgcp, mgcp_trans_id_t trans_id)
636{
637 struct mgcp_response_pending *pending = mgcp_client_response_pending_get(mgcp, trans_id);
638 if (!pending) {
639 /* INFO is sufficient, it is not harmful to cancel a transaction twice. */
640 LOGP(DLMGCP, LOGL_INFO, "Cannot cancel, no such transaction: %u\n", trans_id);
641 return -ENOENT;
642 }
643 LOGP(DLMGCP, LOGL_INFO, "Canceled transaction %u\n", trans_id);
644 talloc_free(pending);
645 return 0;
646 /* We don't really need to clean up the wqueue: In all sane cases, the msgb has already been sent
647 * out and is no longer in the wqueue. If it still is in the wqueue, then sending MGCP messages
648 * per se is broken and the program should notice so by a full wqueue. Even if this was called
649 * before we had a chance to send out the message and it is still going to be sent, we will just
650 * ignore the reply to it later. Removing a msgb from the wqueue here would just introduce more
651 * bug surface in terms of failing to update wqueue API's counters or some such.
652 */
653}
654
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200655static struct msgb *mgcp_msg_from_buf(mgcp_trans_id_t trans_id,
656 const char *buf, int len)
657{
658 struct msgb *msg;
659
660 if (len > (4096 - 128)) {
661 LOGP(DLMGCP, LOGL_ERROR, "Cannot send to MGCP GW:"
662 " message too large: %d\n", len);
663 return NULL;
664 }
665
666 msg = msgb_alloc_headroom(4096, 128, "MGCP tx");
667 OSMO_ASSERT(msg);
668
669 char *dst = (char*)msgb_put(msg, len);
670 memcpy(dst, buf, len);
671 msg->l2h = msg->data;
672 msg->cb[MSGB_CB_MGCP_TRANS_ID] = trans_id;
673
674 return msg;
675}
676
677static struct msgb *mgcp_msg_from_str(mgcp_trans_id_t trans_id,
678 const char *fmt, ...)
679{
680 static char compose[4096 - 128];
681 va_list ap;
682 int len;
683 OSMO_ASSERT(fmt);
684
685 va_start(ap, fmt);
686 len = vsnprintf(compose, sizeof(compose), fmt, ap);
687 va_end(ap);
688 if (len >= sizeof(compose)) {
689 LOGP(DLMGCP, LOGL_ERROR,
690 "Message too large: trans_id=%u len=%d\n",
691 trans_id, len);
692 return NULL;
693 }
694 if (len < 1) {
695 LOGP(DLMGCP, LOGL_ERROR,
696 "Failed to compose message: trans_id=%u len=%d\n",
697 trans_id, len);
698 return NULL;
699 }
700 return mgcp_msg_from_buf(trans_id, compose, len);
701}
702
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200703static mgcp_trans_id_t mgcp_client_next_trans_id(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200704{
705 /* avoid zero trans_id to distinguish from unset trans_id */
706 if (!mgcp->next_trans_id)
707 mgcp->next_trans_id ++;
708 return mgcp->next_trans_id ++;
709}
710
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200711struct msgb *mgcp_msg_crcx(struct mgcp_client *mgcp,
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200712 uint16_t rtp_endpoint, unsigned int call_id,
713 enum mgcp_connection_mode mode)
714{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200715 mgcp_trans_id_t trans_id = mgcp_client_next_trans_id(mgcp);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200716 return mgcp_msg_from_str(trans_id,
717 "CRCX %u %x@mgw MGCP 1.0\r\n"
718 "C: %x\r\n"
719 "L: p:20, a:AMR, nt:IN\r\n"
720 "M: %s\r\n"
721 ,
722 trans_id,
723 rtp_endpoint,
724 call_id,
Neels Hofmeyrd95ab1e2017-09-22 00:52:54 +0200725 mgcp_client_cmode_name(mode));
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200726}
727
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200728struct msgb *mgcp_msg_mdcx(struct mgcp_client *mgcp,
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200729 uint16_t rtp_endpoint, const char *rtp_conn_addr,
730 uint16_t rtp_port, enum mgcp_connection_mode mode)
731
732{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200733 mgcp_trans_id_t trans_id = mgcp_client_next_trans_id(mgcp);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200734 return mgcp_msg_from_str(trans_id,
735 "MDCX %u %x@mgw MGCP 1.0\r\n"
736 "M: %s\r\n"
737 "\r\n"
738 "c=IN IP4 %s\r\n"
739 "m=audio %u RTP/AVP 255\r\n"
740 ,
741 trans_id,
742 rtp_endpoint,
Neels Hofmeyrd95ab1e2017-09-22 00:52:54 +0200743 mgcp_client_cmode_name(mode),
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200744 rtp_conn_addr,
745 rtp_port);
746}
747
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200748struct msgb *mgcp_msg_dlcx(struct mgcp_client *mgcp, uint16_t rtp_endpoint,
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200749 unsigned int call_id)
750{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200751 mgcp_trans_id_t trans_id = mgcp_client_next_trans_id(mgcp);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200752 return mgcp_msg_from_str(trans_id,
753 "DLCX %u %x@mgw MGCP 1.0\r\n"
754 "C: %x\r\n", trans_id, rtp_endpoint, call_id);
755}
756
Philipp Maier1dc6be62017-10-05 18:25:37 +0200757#define MGCP_CRCX_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT | \
758 MGCP_MSG_PRESENCE_CALL_ID | \
Philipp Maier1dc6be62017-10-05 18:25:37 +0200759 MGCP_MSG_PRESENCE_CONN_MODE)
760#define MGCP_MDCX_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT | \
Philipp Maier490cbaa2018-01-22 17:32:38 +0100761 MGCP_MSG_PRESENCE_CALL_ID | \
Philipp Maier1dc6be62017-10-05 18:25:37 +0200762 MGCP_MSG_PRESENCE_CONN_ID)
763#define MGCP_DLCX_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT)
764#define MGCP_AUEP_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT)
765#define MGCP_RSIP_MANDATORY 0 /* none */
766
767struct msgb *mgcp_msg_gen(struct mgcp_client *mgcp, struct mgcp_msg *mgcp_msg)
768{
769 mgcp_trans_id_t trans_id = mgcp_client_next_trans_id(mgcp);
770 uint32_t mandatory_mask;
771 struct msgb *msg = msgb_alloc_headroom(4096, 128, "MGCP tx");
772 int rc = 0;
Philipp Maier9d25d7a2018-01-22 17:31:10 +0100773 char local_ip[INET_ADDRSTRLEN];
Philipp Maier1dc6be62017-10-05 18:25:37 +0200774
775 msg->l2h = msg->data;
776 msg->cb[MSGB_CB_MGCP_TRANS_ID] = trans_id;
777
778 /* Add command verb */
779 switch (mgcp_msg->verb) {
780 case MGCP_VERB_CRCX:
781 mandatory_mask = MGCP_CRCX_MANDATORY;
782 rc += msgb_printf(msg, "CRCX %u", trans_id);
783 break;
784 case MGCP_VERB_MDCX:
785 mandatory_mask = MGCP_MDCX_MANDATORY;
786 rc += msgb_printf(msg, "MDCX %u", trans_id);
787 break;
788 case MGCP_VERB_DLCX:
789 mandatory_mask = MGCP_DLCX_MANDATORY;
790 rc += msgb_printf(msg, "DLCX %u", trans_id);
791 break;
792 case MGCP_VERB_AUEP:
793 mandatory_mask = MGCP_AUEP_MANDATORY;
794 rc += msgb_printf(msg, "AUEP %u", trans_id);
795 break;
796 case MGCP_VERB_RSIP:
797 mandatory_mask = MGCP_RSIP_MANDATORY;
798 rc += msgb_printf(msg, "RSIP %u", trans_id);
799 break;
800 default:
801 LOGP(DLMGCP, LOGL_ERROR,
802 "Invalid command verb, can not generate MGCP message\n");
803 msgb_free(msg);
804 return NULL;
805 }
806
807 /* Check if mandatory fields are missing */
808 if (!((mgcp_msg->presence & mandatory_mask) == mandatory_mask)) {
809 LOGP(DLMGCP, LOGL_ERROR,
810 "One or more missing mandatory fields, can not generate MGCP message\n");
811 msgb_free(msg);
812 return NULL;
813 }
814
815 /* Add endpoint name */
Philipp Maier7bc55522017-12-10 22:52:22 +0100816 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_ENDPOINT) {
817 if (strlen(mgcp_msg->endpoint) <= 0) {
818 LOGP(DLMGCP, LOGL_ERROR,
819 "Empty endpoint name, can not generate MGCP message\n");
820 msgb_free(msg);
821 return NULL;
822 }
Philipp Maier1dc6be62017-10-05 18:25:37 +0200823 rc += msgb_printf(msg, " %s", mgcp_msg->endpoint);
Philipp Maier7bc55522017-12-10 22:52:22 +0100824 }
Philipp Maier1dc6be62017-10-05 18:25:37 +0200825
826 /* Add protocol version */
827 rc += msgb_printf(msg, " MGCP 1.0\r\n");
828
829 /* Add call id */
830 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_CALL_ID)
831 rc += msgb_printf(msg, "C: %x\r\n", mgcp_msg->call_id);
832
833 /* Add connection id */
Philipp Maier7bc55522017-12-10 22:52:22 +0100834 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_CONN_ID) {
835 if (strlen(mgcp_msg->conn_id) <= 0) {
836 LOGP(DLMGCP, LOGL_ERROR,
837 "Empty connection id, can not generate MGCP message\n");
838 msgb_free(msg);
839 return NULL;
840 }
Philipp Maier01d24a32017-11-21 17:26:09 +0100841 rc += msgb_printf(msg, "I: %s\r\n", mgcp_msg->conn_id);
Philipp Maier7bc55522017-12-10 22:52:22 +0100842 }
Philipp Maier1dc6be62017-10-05 18:25:37 +0200843
844 /* Add local connection options */
Philipp Maierffd75e42017-11-22 11:44:50 +0100845 if (mgcp_msg->verb == MGCP_VERB_CRCX)
Philipp Maier1dc6be62017-10-05 18:25:37 +0200846 rc += msgb_printf(msg, "L: p:20, a:AMR, nt:IN\r\n");
847
848 /* Add mode */
849 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_CONN_MODE)
850 rc +=
851 msgb_printf(msg, "M: %s\r\n",
852 mgcp_client_cmode_name(mgcp_msg->conn_mode));
853
Philipp Maier9d25d7a2018-01-22 17:31:10 +0100854 /* Add SDP body */
Philipp Maier1dc6be62017-10-05 18:25:37 +0200855 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_AUDIO_IP
856 && mgcp_msg->presence & MGCP_MSG_PRESENCE_AUDIO_PORT) {
Philipp Maier9d25d7a2018-01-22 17:31:10 +0100857
858 /* Add separator to mark the beginning of the SDP block */
859 rc += msgb_printf(msg, "\r\n");
860
861 /* Add SDP protocol version */
862 rc += msgb_printf(msg, "v=0\r\n");
863
864 /* Add session name (none) */
865 rc += msgb_printf(msg, "s=-\r\n");
866
867 /* Determine local IP-Address */
868 if (osmo_sock_local_ip(local_ip, mgcp->actual.remote_addr) < 0) {
869 LOGP(DLMGCP, LOGL_ERROR,
870 "Could not determine local IP-Address!\n");
871 msgb_free(msg);
872 return NULL;
873 }
874
875 /* Add owner/creator (SDP) */
876 rc += msgb_printf(msg, "o=- %x 23 IN IP4 %s\r\n",
877 mgcp_msg->call_id, local_ip);
878
879 /* Add RTP address and port */
Philipp Maier7bc55522017-12-10 22:52:22 +0100880 if (mgcp_msg->audio_port == 0) {
881 LOGP(DLMGCP, LOGL_ERROR,
882 "Invalid port number, can not generate MGCP message\n");
883 msgb_free(msg);
884 return NULL;
885 }
886 if (strlen(mgcp_msg->audio_ip) <= 0) {
887 LOGP(DLMGCP, LOGL_ERROR,
888 "Empty ip address, can not generate MGCP message\n");
889 msgb_free(msg);
890 return NULL;
891 }
Philipp Maier1dc6be62017-10-05 18:25:37 +0200892 rc += msgb_printf(msg, "c=IN IP4 %s\r\n", mgcp_msg->audio_ip);
893 rc +=
894 msgb_printf(msg, "m=audio %u RTP/AVP 255\r\n",
895 mgcp_msg->audio_port);
Philipp Maier9d25d7a2018-01-22 17:31:10 +0100896
897 /* Add time description, active time (SDP) */
898 rc += msgb_printf(msg, "t=0 0\r\n");
Philipp Maier1dc6be62017-10-05 18:25:37 +0200899 }
900
901 if (rc != 0) {
902 LOGP(DLMGCP, LOGL_ERROR,
903 "message buffer to small, can not generate MGCP message\n");
904 msgb_free(msg);
905 msg = NULL;
906 }
907
908 return msg;
909}
910
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100911/* Retrieve the MGCP transaction ID from a msgb generated by mgcp_msg_gen() */
912mgcp_trans_id_t mgcp_msg_trans_id(struct msgb *msg)
913{
914 return (mgcp_trans_id_t)msg->cb[MSGB_CB_MGCP_TRANS_ID];
915}
916
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200917struct mgcp_client_conf *mgcp_client_conf_actual(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200918{
919 return &mgcp->actual;
920}
Neels Hofmeyrd95ab1e2017-09-22 00:52:54 +0200921
922const struct value_string mgcp_client_connection_mode_strs[] = {
923 { MGCP_CONN_NONE, "none" },
924 { MGCP_CONN_RECV_SEND, "sendrecv" },
925 { MGCP_CONN_SEND_ONLY, "sendonly" },
926 { MGCP_CONN_RECV_ONLY, "recvonly" },
927 { MGCP_CONN_LOOPBACK, "loopback" },
928 { 0, NULL }
929};