blob: 7e07d00b3e9eecc0a061fd7929457f7c3a89b066 [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>
Neels Hofmeyre9920f22017-07-10 15:07:22 +020027
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +020028#include <osmocom/mgcp_client/mgcp_client.h>
29#include <osmocom/mgcp_client/mgcp_client_internal.h>
Neels Hofmeyre9920f22017-07-10 15:07:22 +020030
31#include <netinet/in.h>
32#include <arpa/inet.h>
33
34#include <errno.h>
35#include <unistd.h>
36#include <string.h>
37
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +020038void mgcp_client_conf_init(struct mgcp_client_conf *conf)
Neels Hofmeyre9920f22017-07-10 15:07:22 +020039{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +020040 /* NULL and -1 default to MGCP_CLIENT_*_DEFAULT values */
41 *conf = (struct mgcp_client_conf){
Neels Hofmeyre9920f22017-07-10 15:07:22 +020042 .local_addr = NULL,
43 .local_port = -1,
44 .remote_addr = NULL,
45 .remote_port = -1,
46 .first_endpoint = 0,
47 .last_endpoint = 0,
Neels Hofmeyrc0dcc3c2017-12-02 18:31:34 +000048 .bts_base = 0,
Neels Hofmeyre9920f22017-07-10 15:07:22 +020049 };
50}
51
52/* Test if a given endpoint id is currently in use */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +020053static bool endpoint_in_use(uint16_t id, struct mgcp_client *client)
Neels Hofmeyre9920f22017-07-10 15:07:22 +020054{
55 struct mgcp_inuse_endpoint *endpoint;
56 llist_for_each_entry(endpoint, &client->inuse_endpoints, entry) {
57 if (endpoint->id == id)
58 return true;
59 }
60
61 return false;
62}
63
64/* Find and seize an unsused endpoint id */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +020065int mgcp_client_next_endpoint(struct mgcp_client *client)
Neels Hofmeyre9920f22017-07-10 15:07:22 +020066{
67 int i;
68 uint16_t first_endpoint = client->actual.first_endpoint;
69 uint16_t last_endpoint = client->actual.last_endpoint;
70 struct mgcp_inuse_endpoint *endpoint;
71
72 /* Use the maximum permitted range if the VTY
73 * configuration does not specify a range */
74 if (client->actual.last_endpoint == 0) {
75 first_endpoint = 1;
76 last_endpoint = 65534;
77 }
78
79 /* Test the permitted endpoint range for an endpoint
80 * number that is not in use. When a suitable endpoint
81 * number can be found, seize it by adding it to the
82 * inuse list. */
83 for (i=first_endpoint;i<last_endpoint;i++)
84 {
85 if (endpoint_in_use(i,client) == false) {
86 endpoint = talloc_zero(client, struct mgcp_inuse_endpoint);
87 endpoint->id = i;
88 llist_add_tail(&endpoint->entry, &client->inuse_endpoints);
89 return endpoint->id;
90 }
91 }
92
93 /* All endpoints are busy! */
94 return -EINVAL;
95}
96
97/* Release a seized endpoint id to make it available again for other calls */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +020098void mgcp_client_release_endpoint(uint16_t id, struct mgcp_client *client)
Neels Hofmeyre9920f22017-07-10 15:07:22 +020099{
100 struct mgcp_inuse_endpoint *endpoint;
101 struct mgcp_inuse_endpoint *endpoint_tmp;
102 llist_for_each_entry_safe(endpoint, endpoint_tmp, &client->inuse_endpoints, entry) {
103 if (endpoint->id == id) {
104 llist_del(&endpoint->entry);
105 talloc_free(endpoint);
106 }
107 }
108}
109
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200110static void mgcp_client_handle_response(struct mgcp_client *mgcp,
111 struct mgcp_response_pending *pending,
112 struct mgcp_response *response)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200113{
114 if (!pending) {
115 LOGP(DLMGCP, LOGL_ERROR,
116 "Cannot handle NULL response\n");
117 return;
118 }
119 if (pending->response_cb)
120 pending->response_cb(response, pending->priv);
121 else
122 LOGP(DLMGCP, LOGL_INFO, "MGCP response ignored (NULL cb)\n");
123 talloc_free(pending);
124}
125
126static int mgcp_response_parse_head(struct mgcp_response *r, struct msgb *msg)
127{
128 int comment_pos;
129 char *end;
130
131 if (mgcp_msg_terminate_nul(msg))
132 goto response_parse_failure;
133
134 r->body = (char *)msg->data;
135
Harald Welte9bf7c532017-11-17 14:14:31 +0100136 if (sscanf(r->body, "%3d %u %n",
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200137 &r->head.response_code, &r->head.trans_id,
138 &comment_pos) != 2)
139 goto response_parse_failure;
140
141 r->head.comment = r->body + comment_pos;
142 end = strchr(r->head.comment, '\r');
143 if (!end)
144 goto response_parse_failure;
145 /* Mark the end of the comment */
146 *end = '\0';
147 r->body = end + 1;
148 if (r->body[0] == '\n')
149 r->body ++;
150 return 0;
151
152response_parse_failure:
153 LOGP(DLMGCP, LOGL_ERROR,
154 "Failed to parse MGCP response header\n");
155 return -EINVAL;
156}
157
158/* TODO undup against mgcp_protocol.c:mgcp_check_param() */
159static bool mgcp_line_is_valid(const char *line)
160{
161 const size_t line_len = strlen(line);
162 if (line[0] == '\0')
163 return true;
164
165 if (line_len < 2
166 || line[1] != '=') {
167 LOGP(DLMGCP, LOGL_ERROR,
168 "Wrong MGCP option format: '%s'\n",
169 line);
170 return false;
171 }
172
173 return true;
174}
175
176/* Parse a line like "m=audio 16002 RTP/AVP 98" */
Philipp Maier06da85e2017-10-05 18:49:24 +0200177static int mgcp_parse_audio_port(struct mgcp_response *r, const char *line)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200178{
Harald Welte9bf7c532017-11-17 14:14:31 +0100179 if (sscanf(line, "m=audio %hu",
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200180 &r->audio_port) != 1)
181 goto response_parse_failure;
182
Philipp Maier10f32db2017-12-13 12:34:34 +0100183 if (r->audio_port == 0)
184 goto response_parse_failure;
185
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200186 return 0;
187
188response_parse_failure:
189 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier06da85e2017-10-05 18:49:24 +0200190 "Failed to parse MGCP response header (audio port)\n");
191 return -EINVAL;
192}
193
194/* Parse a line like "c=IN IP4 10.11.12.13" */
195static int mgcp_parse_audio_ip(struct mgcp_response *r, const char *line)
196{
197 struct in_addr ip_test;
198
199 if (strlen(line) < 16)
200 goto response_parse_failure;
201
202 /* The current implementation strictly supports IPV4 only ! */
203 if (memcmp("c=IN IP4 ", line, 9) != 0)
204 goto response_parse_failure;
205
206 /* Extract IP-Address */
Philipp Maierf8bfbe82017-11-23 19:32:31 +0100207 osmo_strlcpy(r->audio_ip, line + 9, sizeof(r->audio_ip));
Philipp Maier06da85e2017-10-05 18:49:24 +0200208
209 /* Check IP-Address */
210 if (inet_aton(r->audio_ip, &ip_test) == 0)
211 goto response_parse_failure;
212
213 return 0;
214
215response_parse_failure:
216 LOGP(DLMGCP, LOGL_ERROR,
217 "Failed to parse MGCP response header (audio ip)\n");
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200218 return -EINVAL;
219}
220
221int mgcp_response_parse_params(struct mgcp_response *r)
222{
223 char *line;
224 int rc;
225 OSMO_ASSERT(r->body);
226 char *data = strstr(r->body, "\n\n");
227
228 if (!data) {
229 LOGP(DLMGCP, LOGL_ERROR,
230 "MGCP response: cannot find start of parameters\n");
231 return -EINVAL;
232 }
233
234 /* Advance to after the \n\n, replace the second \n with \0. That's
235 * where the parameters start. */
236 data ++;
237 *data = '\0';
238 data ++;
239
Neels Hofmeyrd95ab1e2017-09-22 00:52:54 +0200240 for_each_non_empty_line(line, data) {
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200241 if (!mgcp_line_is_valid(line))
242 return -EINVAL;
243
244 switch (line[0]) {
245 case 'm':
Philipp Maier06da85e2017-10-05 18:49:24 +0200246 rc = mgcp_parse_audio_port(r, line);
247 if (rc)
248 return rc;
249 break;
250 case 'c':
251 rc = mgcp_parse_audio_ip(r, line);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200252 if (rc)
253 return rc;
254 break;
255 default:
256 /* skip unhandled parameters */
257 break;
258 }
259 }
260 return 0;
261}
262
Philipp Maierffd75e42017-11-22 11:44:50 +0100263/* Parse a line like "I: 0cedfd5a19542d197af9afe5231f1d61" */
264static int mgcp_parse_conn_id(struct mgcp_response *r, const char *line)
265{
266 if (strlen(line) < 4)
267 goto response_parse_failure;
268
269 if (memcmp("I: ", line, 3) != 0)
270 goto response_parse_failure;
271
272 osmo_strlcpy(r->head.conn_id, line + 3, sizeof(r->head.conn_id));
273 return 0;
274
275response_parse_failure:
276 LOGP(DLMGCP, LOGL_ERROR,
277 "Failed to parse MGCP response (connectionIdentifier)\n");
278 return -EINVAL;
279}
280
281/* Parse MGCP parameters of the response */
282static int parse_head_params(struct mgcp_response *r)
283{
284 char *line;
285 int rc = 0;
286 OSMO_ASSERT(r->body);
287 char *data = r->body;
288 char *data_end = strstr(r->body, "\n\n");
289
290 /* Protect SDP body, for_each_non_empty_line() will
291 * only parse until it hits \0 mark. */
292 if (data_end)
293 *data_end = '\0';
294
295 for_each_non_empty_line(line, data) {
296 switch (line[0]) {
297 case 'I':
298 rc = mgcp_parse_conn_id(r, line);
299 if (rc)
300 goto exit;
301 break;
302 default:
303 /* skip unhandled parameters */
304 break;
305 }
306 }
307exit:
308 /* Restore original state */
309 if (data_end)
310 *data_end = '\n';
311
312 return rc;
313}
314
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200315static struct mgcp_response_pending *mgcp_client_response_pending_get(
316 struct mgcp_client *mgcp,
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100317 mgcp_trans_id_t trans_id)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200318{
319 struct mgcp_response_pending *pending;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200320 llist_for_each_entry(pending, &mgcp->responses_pending, entry) {
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100321 if (pending->trans_id == trans_id) {
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200322 llist_del(&pending->entry);
323 return pending;
324 }
325 }
326 return NULL;
327}
328
329/* Feed an MGCP message into the receive processing.
330 * Parse the head and call any callback registered for the transaction id found
331 * in the MGCP message. This is normally called directly from the internal
332 * mgcp_do_read that reads from the socket connected to the MGCP gateway. This
333 * function is published mainly to be able to feed data from the test suite.
334 */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200335int mgcp_client_rx(struct mgcp_client *mgcp, struct msgb *msg)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200336{
337 struct mgcp_response r = { 0 };
338 struct mgcp_response_pending *pending;
339 int rc;
340
341 rc = mgcp_response_parse_head(&r, msg);
342 if (rc) {
Philipp Maierffd75e42017-11-22 11:44:50 +0100343 LOGP(DLMGCP, LOGL_ERROR, "Cannot parse MGCP response (head)\n");
344 return -1;
345 }
346
347 rc = parse_head_params(&r);
348 if (rc) {
349 LOGP(DLMGCP, LOGL_ERROR, "Cannot parse MGCP response (head parameters)\n");
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200350 return -1;
351 }
352
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100353 pending = mgcp_client_response_pending_get(mgcp, r.head.trans_id);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200354 if (!pending) {
355 LOGP(DLMGCP, LOGL_ERROR,
356 "Cannot find matching MGCP transaction for trans_id %d\n",
357 r.head.trans_id);
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100358 return -ENOENT;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200359 }
360
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200361 mgcp_client_handle_response(mgcp, pending, &r);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200362 return 0;
363}
364
365static int mgcp_do_read(struct osmo_fd *fd)
366{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200367 struct mgcp_client *mgcp = fd->data;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200368 struct msgb *msg;
369 int ret;
370
371 msg = msgb_alloc_headroom(4096, 128, "mgcp_from_gw");
372 if (!msg) {
373 LOGP(DLMGCP, LOGL_ERROR, "Failed to allocate MGCP message.\n");
374 return -1;
375 }
376
377 ret = read(fd->fd, msg->data, 4096 - 128);
378 if (ret <= 0) {
379 LOGP(DLMGCP, LOGL_ERROR, "Failed to read: %d/%s\n", errno, strerror(errno));
380 msgb_free(msg);
381 return -1;
382 } else if (ret > 4096 - 128) {
383 LOGP(DLMGCP, LOGL_ERROR, "Too much data: %d\n", ret);
384 msgb_free(msg);
385 return -1;
Harald Welte9bf7c532017-11-17 14:14:31 +0100386 }
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200387
388 msg->l2h = msgb_put(msg, ret);
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200389 ret = mgcp_client_rx(mgcp, msg);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200390 talloc_free(msg);
391 return ret;
392}
393
394static int mgcp_do_write(struct osmo_fd *fd, struct msgb *msg)
395{
396 int ret;
397 static char strbuf[4096];
398 unsigned int l = msg->len < sizeof(strbuf) ? msg->len : sizeof(strbuf);
399 unsigned int i;
400
Philipp Maierf8bfbe82017-11-23 19:32:31 +0100401 osmo_strlcpy(strbuf, (const char*)msg->data, l);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200402 for (i = 0; i < sizeof(strbuf); i++) {
403 if (strbuf[i] == '\n' || strbuf[i] == '\r') {
404 strbuf[i] = '\0';
405 break;
406 }
407 }
408 DEBUGP(DLMGCP, "Tx MGCP msg to MGCP GW: '%s'\n", strbuf);
409
410 LOGP(DLMGCP, LOGL_DEBUG, "Sending msg to MGCP GW size: %u\n", msg->len);
411
412 ret = write(fd->fd, msg->data, msg->len);
413 if (ret != msg->len)
414 LOGP(DLMGCP, LOGL_ERROR, "Failed to forward message to MGCP"
415 " GW: %s\n", strerror(errno));
416
417 return ret;
418}
419
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200420struct mgcp_client *mgcp_client_init(void *ctx,
421 struct mgcp_client_conf *conf)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200422{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200423 struct mgcp_client *mgcp;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200424
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200425 mgcp = talloc_zero(ctx, struct mgcp_client);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200426
427 INIT_LLIST_HEAD(&mgcp->responses_pending);
428 INIT_LLIST_HEAD(&mgcp->inuse_endpoints);
429
430 mgcp->next_trans_id = 1;
431
432 mgcp->actual.local_addr = conf->local_addr ? conf->local_addr :
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200433 MGCP_CLIENT_LOCAL_ADDR_DEFAULT;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200434 mgcp->actual.local_port = conf->local_port >= 0 ? (uint16_t)conf->local_port :
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200435 MGCP_CLIENT_LOCAL_PORT_DEFAULT;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200436
437 mgcp->actual.remote_addr = conf->remote_addr ? conf->remote_addr :
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200438 MGCP_CLIENT_REMOTE_ADDR_DEFAULT;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200439 mgcp->actual.remote_port = conf->remote_port >= 0 ? (uint16_t)conf->remote_port :
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200440 MGCP_CLIENT_REMOTE_PORT_DEFAULT;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200441
442 mgcp->actual.first_endpoint = conf->first_endpoint > 0 ? (uint16_t)conf->first_endpoint : 0;
443 mgcp->actual.last_endpoint = conf->last_endpoint > 0 ? (uint16_t)conf->last_endpoint : 0;
Neels Hofmeyrc0dcc3c2017-12-02 18:31:34 +0000444 mgcp->actual.bts_base = conf->bts_base > 0 ? (uint16_t)conf->bts_base : 4000;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200445
446 return mgcp;
447}
448
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200449int mgcp_client_connect(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200450{
451 int on;
452 struct sockaddr_in addr;
453 struct osmo_wqueue *wq;
454 int rc;
455
456 if (!mgcp) {
457 LOGP(DLMGCP, LOGL_FATAL, "MGCPGW client not initialized properly\n");
458 return -EINVAL;
459 }
460
461 wq = &mgcp->wq;
462
463 wq->bfd.fd = socket(AF_INET, SOCK_DGRAM, 0);
464 if (wq->bfd.fd < 0) {
465 LOGP(DLMGCP, LOGL_FATAL, "Failed to create UDP socket errno: %d\n", errno);
466 return -errno;
467 }
468
469 on = 1;
470 if (setsockopt(wq->bfd.fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) {
471 LOGP(DLMGCP, LOGL_FATAL,
472 "Failed to initialize socket for MGCP GW: %s\n",
473 strerror(errno));
474 rc = -errno;
475 goto error_close_fd;
476 }
477
478 /* bind socket */
479 memset(&addr, 0, sizeof(addr));
480 addr.sin_family = AF_INET;
481 inet_aton(mgcp->actual.local_addr, &addr.sin_addr);
482 addr.sin_port = htons(mgcp->actual.local_port);
483 if (bind(wq->bfd.fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
484 LOGP(DLMGCP, LOGL_FATAL,
485 "Failed to bind for MGCP GW to %s %u\n",
486 mgcp->actual.local_addr, mgcp->actual.local_port);
487 rc = -errno;
488 goto error_close_fd;
489 }
490
491 /* connect to the remote */
492 inet_aton(mgcp->actual.remote_addr, &addr.sin_addr);
493 addr.sin_port = htons(mgcp->actual.remote_port);
494 if (connect(wq->bfd.fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
495 LOGP(DLMGCP, LOGL_FATAL,
496 "Failed to connect to MGCP GW at %s %u: %s\n",
497 mgcp->actual.remote_addr, mgcp->actual.remote_port,
498 strerror(errno));
499 rc = -errno;
500 goto error_close_fd;
501 }
502
503 mgcp->remote_addr = htonl(addr.sin_addr.s_addr);
504
505 osmo_wqueue_init(wq, 10);
506 wq->bfd.when = BSC_FD_READ;
507 wq->bfd.data = mgcp;
508 wq->read_cb = mgcp_do_read;
509 wq->write_cb = mgcp_do_write;
510
511 if (osmo_fd_register(&wq->bfd) != 0) {
512 LOGP(DLMGCP, LOGL_FATAL, "Failed to register BFD\n");
513 rc = -EIO;
514 goto error_close_fd;
515 }
516 LOGP(DLMGCP, LOGL_INFO, "MGCP GW connection: %s:%u -> %s:%u\n",
517 mgcp->actual.local_addr, mgcp->actual.local_port,
518 mgcp->actual.remote_addr, mgcp->actual.remote_port);
519
520 return 0;
521error_close_fd:
522 close(wq->bfd.fd);
523 wq->bfd.fd = -1;
524 return rc;
525}
526
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200527const char *mgcp_client_remote_addr_str(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200528{
529 return mgcp->actual.remote_addr;
530}
531
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200532uint16_t mgcp_client_remote_port(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200533{
534 return mgcp->actual.remote_port;
535}
536
537/* Return the MGCP GW binary IPv4 address in network byte order. */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200538uint32_t mgcp_client_remote_addr_n(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200539{
540 return mgcp->remote_addr;
541}
542
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200543struct mgcp_response_pending * mgcp_client_pending_add(
544 struct mgcp_client *mgcp,
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200545 mgcp_trans_id_t trans_id,
546 mgcp_response_cb_t response_cb,
547 void *priv)
548{
549 struct mgcp_response_pending *pending;
550
551 pending = talloc_zero(mgcp, struct mgcp_response_pending);
552 pending->trans_id = trans_id;
553 pending->response_cb = response_cb;
554 pending->priv = priv;
555 llist_add_tail(&pending->entry, &mgcp->responses_pending);
556
557 return pending;
558}
559
560/* Send the MGCP message in msg to the MGCP GW and handle a response with
561 * response_cb. NOTE: the response_cb still needs to call
562 * mgcp_response_parse_params(response) to get the parsed parameters -- to
563 * potentially save some CPU cycles, only the head line has been parsed when
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100564 * the response_cb is invoked.
565 * Before the priv pointer becomes invalid, e.g. due to transaction timeout,
566 * mgcp_client_cancel() needs to be called for this transaction.
567 */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200568int mgcp_client_tx(struct mgcp_client *mgcp, struct msgb *msg,
569 mgcp_response_cb_t response_cb, void *priv)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200570{
571 struct mgcp_response_pending *pending;
572 mgcp_trans_id_t trans_id;
573 int rc;
574
575 trans_id = msg->cb[MSGB_CB_MGCP_TRANS_ID];
576 if (!trans_id) {
577 LOGP(DLMGCP, LOGL_ERROR,
578 "Unset transaction id in mgcp send request\n");
579 talloc_free(msg);
580 return -EINVAL;
581 }
582
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200583 pending = mgcp_client_pending_add(mgcp, trans_id, response_cb, priv);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200584
585 if (msgb_l2len(msg) > 4096) {
586 LOGP(DLMGCP, LOGL_ERROR,
587 "Cannot send, MGCP message too large: %u\n",
588 msgb_l2len(msg));
589 msgb_free(msg);
590 rc = -EINVAL;
591 goto mgcp_tx_error;
592 }
593
594 rc = osmo_wqueue_enqueue(&mgcp->wq, msg);
595 if (rc) {
596 LOGP(DLMGCP, LOGL_FATAL, "Could not queue message to MGCP GW\n");
597 msgb_free(msg);
598 goto mgcp_tx_error;
599 } else
600 LOGP(DLMGCP, LOGL_INFO, "Queued %u bytes for MGCP GW\n",
601 msgb_l2len(msg));
602 return 0;
603
604mgcp_tx_error:
605 /* Pass NULL to response cb to indicate an error */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200606 mgcp_client_handle_response(mgcp, pending, NULL);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200607 return -1;
608}
609
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100610/* Cancel a pending transaction.
611 * Should a priv pointer passed to mgcp_client_tx() become invalid, this function must be called. In
612 * practical terms, if the caller of mgcp_client_tx() wishes to tear down a transaction without having
613 * received a response this function must be called. The trans_id can be obtained by calling
614 * mgcp_msg_trans_id() on the msgb produced by mgcp_msg_gen().
615 */
616int mgcp_client_cancel(struct mgcp_client *mgcp, mgcp_trans_id_t trans_id)
617{
618 struct mgcp_response_pending *pending = mgcp_client_response_pending_get(mgcp, trans_id);
619 if (!pending) {
620 /* INFO is sufficient, it is not harmful to cancel a transaction twice. */
621 LOGP(DLMGCP, LOGL_INFO, "Cannot cancel, no such transaction: %u\n", trans_id);
622 return -ENOENT;
623 }
624 LOGP(DLMGCP, LOGL_INFO, "Canceled transaction %u\n", trans_id);
625 talloc_free(pending);
626 return 0;
627 /* We don't really need to clean up the wqueue: In all sane cases, the msgb has already been sent
628 * out and is no longer in the wqueue. If it still is in the wqueue, then sending MGCP messages
629 * per se is broken and the program should notice so by a full wqueue. Even if this was called
630 * before we had a chance to send out the message and it is still going to be sent, we will just
631 * ignore the reply to it later. Removing a msgb from the wqueue here would just introduce more
632 * bug surface in terms of failing to update wqueue API's counters or some such.
633 */
634}
635
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200636static struct msgb *mgcp_msg_from_buf(mgcp_trans_id_t trans_id,
637 const char *buf, int len)
638{
639 struct msgb *msg;
640
641 if (len > (4096 - 128)) {
642 LOGP(DLMGCP, LOGL_ERROR, "Cannot send to MGCP GW:"
643 " message too large: %d\n", len);
644 return NULL;
645 }
646
647 msg = msgb_alloc_headroom(4096, 128, "MGCP tx");
648 OSMO_ASSERT(msg);
649
650 char *dst = (char*)msgb_put(msg, len);
651 memcpy(dst, buf, len);
652 msg->l2h = msg->data;
653 msg->cb[MSGB_CB_MGCP_TRANS_ID] = trans_id;
654
655 return msg;
656}
657
658static struct msgb *mgcp_msg_from_str(mgcp_trans_id_t trans_id,
659 const char *fmt, ...)
660{
661 static char compose[4096 - 128];
662 va_list ap;
663 int len;
664 OSMO_ASSERT(fmt);
665
666 va_start(ap, fmt);
667 len = vsnprintf(compose, sizeof(compose), fmt, ap);
668 va_end(ap);
669 if (len >= sizeof(compose)) {
670 LOGP(DLMGCP, LOGL_ERROR,
671 "Message too large: trans_id=%u len=%d\n",
672 trans_id, len);
673 return NULL;
674 }
675 if (len < 1) {
676 LOGP(DLMGCP, LOGL_ERROR,
677 "Failed to compose message: trans_id=%u len=%d\n",
678 trans_id, len);
679 return NULL;
680 }
681 return mgcp_msg_from_buf(trans_id, compose, len);
682}
683
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200684static mgcp_trans_id_t mgcp_client_next_trans_id(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200685{
686 /* avoid zero trans_id to distinguish from unset trans_id */
687 if (!mgcp->next_trans_id)
688 mgcp->next_trans_id ++;
689 return mgcp->next_trans_id ++;
690}
691
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200692struct msgb *mgcp_msg_crcx(struct mgcp_client *mgcp,
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200693 uint16_t rtp_endpoint, unsigned int call_id,
694 enum mgcp_connection_mode mode)
695{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200696 mgcp_trans_id_t trans_id = mgcp_client_next_trans_id(mgcp);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200697 return mgcp_msg_from_str(trans_id,
698 "CRCX %u %x@mgw MGCP 1.0\r\n"
699 "C: %x\r\n"
700 "L: p:20, a:AMR, nt:IN\r\n"
701 "M: %s\r\n"
702 ,
703 trans_id,
704 rtp_endpoint,
705 call_id,
Neels Hofmeyrd95ab1e2017-09-22 00:52:54 +0200706 mgcp_client_cmode_name(mode));
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200707}
708
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200709struct msgb *mgcp_msg_mdcx(struct mgcp_client *mgcp,
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200710 uint16_t rtp_endpoint, const char *rtp_conn_addr,
711 uint16_t rtp_port, enum mgcp_connection_mode mode)
712
713{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200714 mgcp_trans_id_t trans_id = mgcp_client_next_trans_id(mgcp);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200715 return mgcp_msg_from_str(trans_id,
716 "MDCX %u %x@mgw MGCP 1.0\r\n"
717 "M: %s\r\n"
718 "\r\n"
719 "c=IN IP4 %s\r\n"
720 "m=audio %u RTP/AVP 255\r\n"
721 ,
722 trans_id,
723 rtp_endpoint,
Neels Hofmeyrd95ab1e2017-09-22 00:52:54 +0200724 mgcp_client_cmode_name(mode),
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200725 rtp_conn_addr,
726 rtp_port);
727}
728
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200729struct msgb *mgcp_msg_dlcx(struct mgcp_client *mgcp, uint16_t rtp_endpoint,
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200730 unsigned int call_id)
731{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200732 mgcp_trans_id_t trans_id = mgcp_client_next_trans_id(mgcp);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200733 return mgcp_msg_from_str(trans_id,
734 "DLCX %u %x@mgw MGCP 1.0\r\n"
735 "C: %x\r\n", trans_id, rtp_endpoint, call_id);
736}
737
Philipp Maier1dc6be62017-10-05 18:25:37 +0200738#define MGCP_CRCX_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT | \
739 MGCP_MSG_PRESENCE_CALL_ID | \
Philipp Maier1dc6be62017-10-05 18:25:37 +0200740 MGCP_MSG_PRESENCE_CONN_MODE)
741#define MGCP_MDCX_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT | \
742 MGCP_MSG_PRESENCE_CONN_ID)
743#define MGCP_DLCX_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT)
744#define MGCP_AUEP_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT)
745#define MGCP_RSIP_MANDATORY 0 /* none */
746
747struct msgb *mgcp_msg_gen(struct mgcp_client *mgcp, struct mgcp_msg *mgcp_msg)
748{
749 mgcp_trans_id_t trans_id = mgcp_client_next_trans_id(mgcp);
750 uint32_t mandatory_mask;
751 struct msgb *msg = msgb_alloc_headroom(4096, 128, "MGCP tx");
752 int rc = 0;
753
754 msg->l2h = msg->data;
755 msg->cb[MSGB_CB_MGCP_TRANS_ID] = trans_id;
756
757 /* Add command verb */
758 switch (mgcp_msg->verb) {
759 case MGCP_VERB_CRCX:
760 mandatory_mask = MGCP_CRCX_MANDATORY;
761 rc += msgb_printf(msg, "CRCX %u", trans_id);
762 break;
763 case MGCP_VERB_MDCX:
764 mandatory_mask = MGCP_MDCX_MANDATORY;
765 rc += msgb_printf(msg, "MDCX %u", trans_id);
766 break;
767 case MGCP_VERB_DLCX:
768 mandatory_mask = MGCP_DLCX_MANDATORY;
769 rc += msgb_printf(msg, "DLCX %u", trans_id);
770 break;
771 case MGCP_VERB_AUEP:
772 mandatory_mask = MGCP_AUEP_MANDATORY;
773 rc += msgb_printf(msg, "AUEP %u", trans_id);
774 break;
775 case MGCP_VERB_RSIP:
776 mandatory_mask = MGCP_RSIP_MANDATORY;
777 rc += msgb_printf(msg, "RSIP %u", trans_id);
778 break;
779 default:
780 LOGP(DLMGCP, LOGL_ERROR,
781 "Invalid command verb, can not generate MGCP message\n");
782 msgb_free(msg);
783 return NULL;
784 }
785
786 /* Check if mandatory fields are missing */
787 if (!((mgcp_msg->presence & mandatory_mask) == mandatory_mask)) {
788 LOGP(DLMGCP, LOGL_ERROR,
789 "One or more missing mandatory fields, can not generate MGCP message\n");
790 msgb_free(msg);
791 return NULL;
792 }
793
794 /* Add endpoint name */
Philipp Maier7bc55522017-12-10 22:52:22 +0100795 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_ENDPOINT) {
796 if (strlen(mgcp_msg->endpoint) <= 0) {
797 LOGP(DLMGCP, LOGL_ERROR,
798 "Empty endpoint name, can not generate MGCP message\n");
799 msgb_free(msg);
800 return NULL;
801 }
Philipp Maier1dc6be62017-10-05 18:25:37 +0200802 rc += msgb_printf(msg, " %s", mgcp_msg->endpoint);
Philipp Maier7bc55522017-12-10 22:52:22 +0100803 }
Philipp Maier1dc6be62017-10-05 18:25:37 +0200804
805 /* Add protocol version */
806 rc += msgb_printf(msg, " MGCP 1.0\r\n");
807
808 /* Add call id */
809 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_CALL_ID)
810 rc += msgb_printf(msg, "C: %x\r\n", mgcp_msg->call_id);
811
812 /* Add connection id */
Philipp Maier7bc55522017-12-10 22:52:22 +0100813 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_CONN_ID) {
814 if (strlen(mgcp_msg->conn_id) <= 0) {
815 LOGP(DLMGCP, LOGL_ERROR,
816 "Empty connection id, can not generate MGCP message\n");
817 msgb_free(msg);
818 return NULL;
819 }
Philipp Maier01d24a32017-11-21 17:26:09 +0100820 rc += msgb_printf(msg, "I: %s\r\n", mgcp_msg->conn_id);
Philipp Maier7bc55522017-12-10 22:52:22 +0100821 }
Philipp Maier1dc6be62017-10-05 18:25:37 +0200822
823 /* Add local connection options */
Philipp Maierffd75e42017-11-22 11:44:50 +0100824 if (mgcp_msg->verb == MGCP_VERB_CRCX)
Philipp Maier1dc6be62017-10-05 18:25:37 +0200825 rc += msgb_printf(msg, "L: p:20, a:AMR, nt:IN\r\n");
826
827 /* Add mode */
828 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_CONN_MODE)
829 rc +=
830 msgb_printf(msg, "M: %s\r\n",
831 mgcp_client_cmode_name(mgcp_msg->conn_mode));
832
833 /* Add RTP address and port (SDP) */
834 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_AUDIO_IP
835 && mgcp_msg->presence & MGCP_MSG_PRESENCE_AUDIO_PORT) {
Philipp Maier7bc55522017-12-10 22:52:22 +0100836 if (mgcp_msg->audio_port == 0) {
837 LOGP(DLMGCP, LOGL_ERROR,
838 "Invalid port number, can not generate MGCP message\n");
839 msgb_free(msg);
840 return NULL;
841 }
842 if (strlen(mgcp_msg->audio_ip) <= 0) {
843 LOGP(DLMGCP, LOGL_ERROR,
844 "Empty ip address, can not generate MGCP message\n");
845 msgb_free(msg);
846 return NULL;
847 }
Philipp Maier1dc6be62017-10-05 18:25:37 +0200848 rc += msgb_printf(msg, "\r\n");
849 rc += msgb_printf(msg, "c=IN IP4 %s\r\n", mgcp_msg->audio_ip);
850 rc +=
851 msgb_printf(msg, "m=audio %u RTP/AVP 255\r\n",
852 mgcp_msg->audio_port);
853 }
854
855 if (rc != 0) {
856 LOGP(DLMGCP, LOGL_ERROR,
857 "message buffer to small, can not generate MGCP message\n");
858 msgb_free(msg);
859 msg = NULL;
860 }
861
862 return msg;
863}
864
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100865/* Retrieve the MGCP transaction ID from a msgb generated by mgcp_msg_gen() */
866mgcp_trans_id_t mgcp_msg_trans_id(struct msgb *msg)
867{
868 return (mgcp_trans_id_t)msg->cb[MSGB_CB_MGCP_TRANS_ID];
869}
870
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200871struct mgcp_client_conf *mgcp_client_conf_actual(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200872{
873 return &mgcp->actual;
874}
Neels Hofmeyrd95ab1e2017-09-22 00:52:54 +0200875
876const struct value_string mgcp_client_connection_mode_strs[] = {
877 { MGCP_CONN_NONE, "none" },
878 { MGCP_CONN_RECV_SEND, "sendrecv" },
879 { MGCP_CONN_SEND_ONLY, "sendonly" },
880 { MGCP_CONN_RECV_ONLY, "recvonly" },
881 { MGCP_CONN_LOOPBACK, "loopback" },
882 { 0, NULL }
883};