blob: 2f3d0d12047b68ec5c6cebea942fb227cc7bab61 [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
183 return 0;
184
185response_parse_failure:
186 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier06da85e2017-10-05 18:49:24 +0200187 "Failed to parse MGCP response header (audio port)\n");
188 return -EINVAL;
189}
190
191/* Parse a line like "c=IN IP4 10.11.12.13" */
192static int mgcp_parse_audio_ip(struct mgcp_response *r, const char *line)
193{
194 struct in_addr ip_test;
195
196 if (strlen(line) < 16)
197 goto response_parse_failure;
198
199 /* The current implementation strictly supports IPV4 only ! */
200 if (memcmp("c=IN IP4 ", line, 9) != 0)
201 goto response_parse_failure;
202
203 /* Extract IP-Address */
Philipp Maierf8bfbe82017-11-23 19:32:31 +0100204 osmo_strlcpy(r->audio_ip, line + 9, sizeof(r->audio_ip));
Philipp Maier06da85e2017-10-05 18:49:24 +0200205
206 /* Check IP-Address */
207 if (inet_aton(r->audio_ip, &ip_test) == 0)
208 goto response_parse_failure;
209
210 return 0;
211
212response_parse_failure:
213 LOGP(DLMGCP, LOGL_ERROR,
214 "Failed to parse MGCP response header (audio ip)\n");
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200215 return -EINVAL;
216}
217
218int mgcp_response_parse_params(struct mgcp_response *r)
219{
220 char *line;
221 int rc;
222 OSMO_ASSERT(r->body);
223 char *data = strstr(r->body, "\n\n");
224
225 if (!data) {
226 LOGP(DLMGCP, LOGL_ERROR,
227 "MGCP response: cannot find start of parameters\n");
228 return -EINVAL;
229 }
230
231 /* Advance to after the \n\n, replace the second \n with \0. That's
232 * where the parameters start. */
233 data ++;
234 *data = '\0';
235 data ++;
236
Neels Hofmeyrd95ab1e2017-09-22 00:52:54 +0200237 for_each_non_empty_line(line, data) {
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200238 if (!mgcp_line_is_valid(line))
239 return -EINVAL;
240
241 switch (line[0]) {
242 case 'm':
Philipp Maier06da85e2017-10-05 18:49:24 +0200243 rc = mgcp_parse_audio_port(r, line);
244 if (rc)
245 return rc;
246 break;
247 case 'c':
248 rc = mgcp_parse_audio_ip(r, line);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200249 if (rc)
250 return rc;
251 break;
252 default:
253 /* skip unhandled parameters */
254 break;
255 }
256 }
257 return 0;
258}
259
Philipp Maierffd75e42017-11-22 11:44:50 +0100260/* Parse a line like "I: 0cedfd5a19542d197af9afe5231f1d61" */
261static int mgcp_parse_conn_id(struct mgcp_response *r, const char *line)
262{
263 if (strlen(line) < 4)
264 goto response_parse_failure;
265
266 if (memcmp("I: ", line, 3) != 0)
267 goto response_parse_failure;
268
269 osmo_strlcpy(r->head.conn_id, line + 3, sizeof(r->head.conn_id));
270 return 0;
271
272response_parse_failure:
273 LOGP(DLMGCP, LOGL_ERROR,
274 "Failed to parse MGCP response (connectionIdentifier)\n");
275 return -EINVAL;
276}
277
278/* Parse MGCP parameters of the response */
279static int parse_head_params(struct mgcp_response *r)
280{
281 char *line;
282 int rc = 0;
283 OSMO_ASSERT(r->body);
284 char *data = r->body;
285 char *data_end = strstr(r->body, "\n\n");
286
287 /* Protect SDP body, for_each_non_empty_line() will
288 * only parse until it hits \0 mark. */
289 if (data_end)
290 *data_end = '\0';
291
292 for_each_non_empty_line(line, data) {
293 switch (line[0]) {
294 case 'I':
295 rc = mgcp_parse_conn_id(r, line);
296 if (rc)
297 goto exit;
298 break;
299 default:
300 /* skip unhandled parameters */
301 break;
302 }
303 }
304exit:
305 /* Restore original state */
306 if (data_end)
307 *data_end = '\n';
308
309 return rc;
310}
311
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200312static struct mgcp_response_pending *mgcp_client_response_pending_get(
313 struct mgcp_client *mgcp,
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100314 mgcp_trans_id_t trans_id)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200315{
316 struct mgcp_response_pending *pending;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200317 llist_for_each_entry(pending, &mgcp->responses_pending, entry) {
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100318 if (pending->trans_id == trans_id) {
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200319 llist_del(&pending->entry);
320 return pending;
321 }
322 }
323 return NULL;
324}
325
326/* Feed an MGCP message into the receive processing.
327 * Parse the head and call any callback registered for the transaction id found
328 * in the MGCP message. This is normally called directly from the internal
329 * mgcp_do_read that reads from the socket connected to the MGCP gateway. This
330 * function is published mainly to be able to feed data from the test suite.
331 */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200332int mgcp_client_rx(struct mgcp_client *mgcp, struct msgb *msg)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200333{
334 struct mgcp_response r = { 0 };
335 struct mgcp_response_pending *pending;
336 int rc;
337
338 rc = mgcp_response_parse_head(&r, msg);
339 if (rc) {
Philipp Maierffd75e42017-11-22 11:44:50 +0100340 LOGP(DLMGCP, LOGL_ERROR, "Cannot parse MGCP response (head)\n");
341 return -1;
342 }
343
344 rc = parse_head_params(&r);
345 if (rc) {
346 LOGP(DLMGCP, LOGL_ERROR, "Cannot parse MGCP response (head parameters)\n");
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200347 return -1;
348 }
349
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100350 pending = mgcp_client_response_pending_get(mgcp, r.head.trans_id);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200351 if (!pending) {
352 LOGP(DLMGCP, LOGL_ERROR,
353 "Cannot find matching MGCP transaction for trans_id %d\n",
354 r.head.trans_id);
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100355 return -ENOENT;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200356 }
357
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200358 mgcp_client_handle_response(mgcp, pending, &r);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200359 return 0;
360}
361
362static int mgcp_do_read(struct osmo_fd *fd)
363{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200364 struct mgcp_client *mgcp = fd->data;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200365 struct msgb *msg;
366 int ret;
367
368 msg = msgb_alloc_headroom(4096, 128, "mgcp_from_gw");
369 if (!msg) {
370 LOGP(DLMGCP, LOGL_ERROR, "Failed to allocate MGCP message.\n");
371 return -1;
372 }
373
374 ret = read(fd->fd, msg->data, 4096 - 128);
375 if (ret <= 0) {
376 LOGP(DLMGCP, LOGL_ERROR, "Failed to read: %d/%s\n", errno, strerror(errno));
377 msgb_free(msg);
378 return -1;
379 } else if (ret > 4096 - 128) {
380 LOGP(DLMGCP, LOGL_ERROR, "Too much data: %d\n", ret);
381 msgb_free(msg);
382 return -1;
Harald Welte9bf7c532017-11-17 14:14:31 +0100383 }
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200384
385 msg->l2h = msgb_put(msg, ret);
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200386 ret = mgcp_client_rx(mgcp, msg);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200387 talloc_free(msg);
388 return ret;
389}
390
391static int mgcp_do_write(struct osmo_fd *fd, struct msgb *msg)
392{
393 int ret;
394 static char strbuf[4096];
395 unsigned int l = msg->len < sizeof(strbuf) ? msg->len : sizeof(strbuf);
396 unsigned int i;
397
Philipp Maierf8bfbe82017-11-23 19:32:31 +0100398 osmo_strlcpy(strbuf, (const char*)msg->data, l);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200399 for (i = 0; i < sizeof(strbuf); i++) {
400 if (strbuf[i] == '\n' || strbuf[i] == '\r') {
401 strbuf[i] = '\0';
402 break;
403 }
404 }
405 DEBUGP(DLMGCP, "Tx MGCP msg to MGCP GW: '%s'\n", strbuf);
406
407 LOGP(DLMGCP, LOGL_DEBUG, "Sending msg to MGCP GW size: %u\n", msg->len);
408
409 ret = write(fd->fd, msg->data, msg->len);
410 if (ret != msg->len)
411 LOGP(DLMGCP, LOGL_ERROR, "Failed to forward message to MGCP"
412 " GW: %s\n", strerror(errno));
413
414 return ret;
415}
416
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200417struct mgcp_client *mgcp_client_init(void *ctx,
418 struct mgcp_client_conf *conf)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200419{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200420 struct mgcp_client *mgcp;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200421
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200422 mgcp = talloc_zero(ctx, struct mgcp_client);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200423
424 INIT_LLIST_HEAD(&mgcp->responses_pending);
425 INIT_LLIST_HEAD(&mgcp->inuse_endpoints);
426
427 mgcp->next_trans_id = 1;
428
429 mgcp->actual.local_addr = conf->local_addr ? conf->local_addr :
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200430 MGCP_CLIENT_LOCAL_ADDR_DEFAULT;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200431 mgcp->actual.local_port = conf->local_port >= 0 ? (uint16_t)conf->local_port :
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200432 MGCP_CLIENT_LOCAL_PORT_DEFAULT;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200433
434 mgcp->actual.remote_addr = conf->remote_addr ? conf->remote_addr :
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200435 MGCP_CLIENT_REMOTE_ADDR_DEFAULT;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200436 mgcp->actual.remote_port = conf->remote_port >= 0 ? (uint16_t)conf->remote_port :
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200437 MGCP_CLIENT_REMOTE_PORT_DEFAULT;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200438
439 mgcp->actual.first_endpoint = conf->first_endpoint > 0 ? (uint16_t)conf->first_endpoint : 0;
440 mgcp->actual.last_endpoint = conf->last_endpoint > 0 ? (uint16_t)conf->last_endpoint : 0;
Neels Hofmeyrc0dcc3c2017-12-02 18:31:34 +0000441 mgcp->actual.bts_base = conf->bts_base > 0 ? (uint16_t)conf->bts_base : 4000;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200442
443 return mgcp;
444}
445
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200446int mgcp_client_connect(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200447{
448 int on;
449 struct sockaddr_in addr;
450 struct osmo_wqueue *wq;
451 int rc;
452
453 if (!mgcp) {
454 LOGP(DLMGCP, LOGL_FATAL, "MGCPGW client not initialized properly\n");
455 return -EINVAL;
456 }
457
458 wq = &mgcp->wq;
459
460 wq->bfd.fd = socket(AF_INET, SOCK_DGRAM, 0);
461 if (wq->bfd.fd < 0) {
462 LOGP(DLMGCP, LOGL_FATAL, "Failed to create UDP socket errno: %d\n", errno);
463 return -errno;
464 }
465
466 on = 1;
467 if (setsockopt(wq->bfd.fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) {
468 LOGP(DLMGCP, LOGL_FATAL,
469 "Failed to initialize socket for MGCP GW: %s\n",
470 strerror(errno));
471 rc = -errno;
472 goto error_close_fd;
473 }
474
475 /* bind socket */
476 memset(&addr, 0, sizeof(addr));
477 addr.sin_family = AF_INET;
478 inet_aton(mgcp->actual.local_addr, &addr.sin_addr);
479 addr.sin_port = htons(mgcp->actual.local_port);
480 if (bind(wq->bfd.fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
481 LOGP(DLMGCP, LOGL_FATAL,
482 "Failed to bind for MGCP GW to %s %u\n",
483 mgcp->actual.local_addr, mgcp->actual.local_port);
484 rc = -errno;
485 goto error_close_fd;
486 }
487
488 /* connect to the remote */
489 inet_aton(mgcp->actual.remote_addr, &addr.sin_addr);
490 addr.sin_port = htons(mgcp->actual.remote_port);
491 if (connect(wq->bfd.fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
492 LOGP(DLMGCP, LOGL_FATAL,
493 "Failed to connect to MGCP GW at %s %u: %s\n",
494 mgcp->actual.remote_addr, mgcp->actual.remote_port,
495 strerror(errno));
496 rc = -errno;
497 goto error_close_fd;
498 }
499
500 mgcp->remote_addr = htonl(addr.sin_addr.s_addr);
501
502 osmo_wqueue_init(wq, 10);
503 wq->bfd.when = BSC_FD_READ;
504 wq->bfd.data = mgcp;
505 wq->read_cb = mgcp_do_read;
506 wq->write_cb = mgcp_do_write;
507
508 if (osmo_fd_register(&wq->bfd) != 0) {
509 LOGP(DLMGCP, LOGL_FATAL, "Failed to register BFD\n");
510 rc = -EIO;
511 goto error_close_fd;
512 }
513 LOGP(DLMGCP, LOGL_INFO, "MGCP GW connection: %s:%u -> %s:%u\n",
514 mgcp->actual.local_addr, mgcp->actual.local_port,
515 mgcp->actual.remote_addr, mgcp->actual.remote_port);
516
517 return 0;
518error_close_fd:
519 close(wq->bfd.fd);
520 wq->bfd.fd = -1;
521 return rc;
522}
523
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200524const char *mgcp_client_remote_addr_str(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200525{
526 return mgcp->actual.remote_addr;
527}
528
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200529uint16_t mgcp_client_remote_port(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200530{
531 return mgcp->actual.remote_port;
532}
533
534/* Return the MGCP GW binary IPv4 address in network byte order. */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200535uint32_t mgcp_client_remote_addr_n(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200536{
537 return mgcp->remote_addr;
538}
539
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200540struct mgcp_response_pending * mgcp_client_pending_add(
541 struct mgcp_client *mgcp,
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200542 mgcp_trans_id_t trans_id,
543 mgcp_response_cb_t response_cb,
544 void *priv)
545{
546 struct mgcp_response_pending *pending;
547
548 pending = talloc_zero(mgcp, struct mgcp_response_pending);
549 pending->trans_id = trans_id;
550 pending->response_cb = response_cb;
551 pending->priv = priv;
552 llist_add_tail(&pending->entry, &mgcp->responses_pending);
553
554 return pending;
555}
556
557/* Send the MGCP message in msg to the MGCP GW and handle a response with
558 * response_cb. NOTE: the response_cb still needs to call
559 * mgcp_response_parse_params(response) to get the parsed parameters -- to
560 * potentially save some CPU cycles, only the head line has been parsed when
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100561 * the response_cb is invoked.
562 * Before the priv pointer becomes invalid, e.g. due to transaction timeout,
563 * mgcp_client_cancel() needs to be called for this transaction.
564 */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200565int mgcp_client_tx(struct mgcp_client *mgcp, struct msgb *msg,
566 mgcp_response_cb_t response_cb, void *priv)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200567{
568 struct mgcp_response_pending *pending;
569 mgcp_trans_id_t trans_id;
570 int rc;
571
572 trans_id = msg->cb[MSGB_CB_MGCP_TRANS_ID];
573 if (!trans_id) {
574 LOGP(DLMGCP, LOGL_ERROR,
575 "Unset transaction id in mgcp send request\n");
576 talloc_free(msg);
577 return -EINVAL;
578 }
579
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200580 pending = mgcp_client_pending_add(mgcp, trans_id, response_cb, priv);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200581
582 if (msgb_l2len(msg) > 4096) {
583 LOGP(DLMGCP, LOGL_ERROR,
584 "Cannot send, MGCP message too large: %u\n",
585 msgb_l2len(msg));
586 msgb_free(msg);
587 rc = -EINVAL;
588 goto mgcp_tx_error;
589 }
590
591 rc = osmo_wqueue_enqueue(&mgcp->wq, msg);
592 if (rc) {
593 LOGP(DLMGCP, LOGL_FATAL, "Could not queue message to MGCP GW\n");
594 msgb_free(msg);
595 goto mgcp_tx_error;
596 } else
597 LOGP(DLMGCP, LOGL_INFO, "Queued %u bytes for MGCP GW\n",
598 msgb_l2len(msg));
599 return 0;
600
601mgcp_tx_error:
602 /* Pass NULL to response cb to indicate an error */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200603 mgcp_client_handle_response(mgcp, pending, NULL);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200604 return -1;
605}
606
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100607/* Cancel a pending transaction.
608 * Should a priv pointer passed to mgcp_client_tx() become invalid, this function must be called. In
609 * practical terms, if the caller of mgcp_client_tx() wishes to tear down a transaction without having
610 * received a response this function must be called. The trans_id can be obtained by calling
611 * mgcp_msg_trans_id() on the msgb produced by mgcp_msg_gen().
612 */
613int mgcp_client_cancel(struct mgcp_client *mgcp, mgcp_trans_id_t trans_id)
614{
615 struct mgcp_response_pending *pending = mgcp_client_response_pending_get(mgcp, trans_id);
616 if (!pending) {
617 /* INFO is sufficient, it is not harmful to cancel a transaction twice. */
618 LOGP(DLMGCP, LOGL_INFO, "Cannot cancel, no such transaction: %u\n", trans_id);
619 return -ENOENT;
620 }
621 LOGP(DLMGCP, LOGL_INFO, "Canceled transaction %u\n", trans_id);
622 talloc_free(pending);
623 return 0;
624 /* We don't really need to clean up the wqueue: In all sane cases, the msgb has already been sent
625 * out and is no longer in the wqueue. If it still is in the wqueue, then sending MGCP messages
626 * per se is broken and the program should notice so by a full wqueue. Even if this was called
627 * before we had a chance to send out the message and it is still going to be sent, we will just
628 * ignore the reply to it later. Removing a msgb from the wqueue here would just introduce more
629 * bug surface in terms of failing to update wqueue API's counters or some such.
630 */
631}
632
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200633static struct msgb *mgcp_msg_from_buf(mgcp_trans_id_t trans_id,
634 const char *buf, int len)
635{
636 struct msgb *msg;
637
638 if (len > (4096 - 128)) {
639 LOGP(DLMGCP, LOGL_ERROR, "Cannot send to MGCP GW:"
640 " message too large: %d\n", len);
641 return NULL;
642 }
643
644 msg = msgb_alloc_headroom(4096, 128, "MGCP tx");
645 OSMO_ASSERT(msg);
646
647 char *dst = (char*)msgb_put(msg, len);
648 memcpy(dst, buf, len);
649 msg->l2h = msg->data;
650 msg->cb[MSGB_CB_MGCP_TRANS_ID] = trans_id;
651
652 return msg;
653}
654
655static struct msgb *mgcp_msg_from_str(mgcp_trans_id_t trans_id,
656 const char *fmt, ...)
657{
658 static char compose[4096 - 128];
659 va_list ap;
660 int len;
661 OSMO_ASSERT(fmt);
662
663 va_start(ap, fmt);
664 len = vsnprintf(compose, sizeof(compose), fmt, ap);
665 va_end(ap);
666 if (len >= sizeof(compose)) {
667 LOGP(DLMGCP, LOGL_ERROR,
668 "Message too large: trans_id=%u len=%d\n",
669 trans_id, len);
670 return NULL;
671 }
672 if (len < 1) {
673 LOGP(DLMGCP, LOGL_ERROR,
674 "Failed to compose message: trans_id=%u len=%d\n",
675 trans_id, len);
676 return NULL;
677 }
678 return mgcp_msg_from_buf(trans_id, compose, len);
679}
680
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200681static mgcp_trans_id_t mgcp_client_next_trans_id(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200682{
683 /* avoid zero trans_id to distinguish from unset trans_id */
684 if (!mgcp->next_trans_id)
685 mgcp->next_trans_id ++;
686 return mgcp->next_trans_id ++;
687}
688
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200689struct msgb *mgcp_msg_crcx(struct mgcp_client *mgcp,
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200690 uint16_t rtp_endpoint, unsigned int call_id,
691 enum mgcp_connection_mode mode)
692{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200693 mgcp_trans_id_t trans_id = mgcp_client_next_trans_id(mgcp);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200694 return mgcp_msg_from_str(trans_id,
695 "CRCX %u %x@mgw MGCP 1.0\r\n"
696 "C: %x\r\n"
697 "L: p:20, a:AMR, nt:IN\r\n"
698 "M: %s\r\n"
699 ,
700 trans_id,
701 rtp_endpoint,
702 call_id,
Neels Hofmeyrd95ab1e2017-09-22 00:52:54 +0200703 mgcp_client_cmode_name(mode));
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200704}
705
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200706struct msgb *mgcp_msg_mdcx(struct mgcp_client *mgcp,
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200707 uint16_t rtp_endpoint, const char *rtp_conn_addr,
708 uint16_t rtp_port, enum mgcp_connection_mode mode)
709
710{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200711 mgcp_trans_id_t trans_id = mgcp_client_next_trans_id(mgcp);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200712 return mgcp_msg_from_str(trans_id,
713 "MDCX %u %x@mgw MGCP 1.0\r\n"
714 "M: %s\r\n"
715 "\r\n"
716 "c=IN IP4 %s\r\n"
717 "m=audio %u RTP/AVP 255\r\n"
718 ,
719 trans_id,
720 rtp_endpoint,
Neels Hofmeyrd95ab1e2017-09-22 00:52:54 +0200721 mgcp_client_cmode_name(mode),
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200722 rtp_conn_addr,
723 rtp_port);
724}
725
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200726struct msgb *mgcp_msg_dlcx(struct mgcp_client *mgcp, uint16_t rtp_endpoint,
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200727 unsigned int call_id)
728{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200729 mgcp_trans_id_t trans_id = mgcp_client_next_trans_id(mgcp);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200730 return mgcp_msg_from_str(trans_id,
731 "DLCX %u %x@mgw MGCP 1.0\r\n"
732 "C: %x\r\n", trans_id, rtp_endpoint, call_id);
733}
734
Philipp Maier1dc6be62017-10-05 18:25:37 +0200735#define MGCP_CRCX_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT | \
736 MGCP_MSG_PRESENCE_CALL_ID | \
Philipp Maier1dc6be62017-10-05 18:25:37 +0200737 MGCP_MSG_PRESENCE_CONN_MODE)
738#define MGCP_MDCX_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT | \
739 MGCP_MSG_PRESENCE_CONN_ID)
740#define MGCP_DLCX_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT)
741#define MGCP_AUEP_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT)
742#define MGCP_RSIP_MANDATORY 0 /* none */
743
744struct msgb *mgcp_msg_gen(struct mgcp_client *mgcp, struct mgcp_msg *mgcp_msg)
745{
746 mgcp_trans_id_t trans_id = mgcp_client_next_trans_id(mgcp);
747 uint32_t mandatory_mask;
748 struct msgb *msg = msgb_alloc_headroom(4096, 128, "MGCP tx");
749 int rc = 0;
750
751 msg->l2h = msg->data;
752 msg->cb[MSGB_CB_MGCP_TRANS_ID] = trans_id;
753
754 /* Add command verb */
755 switch (mgcp_msg->verb) {
756 case MGCP_VERB_CRCX:
757 mandatory_mask = MGCP_CRCX_MANDATORY;
758 rc += msgb_printf(msg, "CRCX %u", trans_id);
759 break;
760 case MGCP_VERB_MDCX:
761 mandatory_mask = MGCP_MDCX_MANDATORY;
762 rc += msgb_printf(msg, "MDCX %u", trans_id);
763 break;
764 case MGCP_VERB_DLCX:
765 mandatory_mask = MGCP_DLCX_MANDATORY;
766 rc += msgb_printf(msg, "DLCX %u", trans_id);
767 break;
768 case MGCP_VERB_AUEP:
769 mandatory_mask = MGCP_AUEP_MANDATORY;
770 rc += msgb_printf(msg, "AUEP %u", trans_id);
771 break;
772 case MGCP_VERB_RSIP:
773 mandatory_mask = MGCP_RSIP_MANDATORY;
774 rc += msgb_printf(msg, "RSIP %u", trans_id);
775 break;
776 default:
777 LOGP(DLMGCP, LOGL_ERROR,
778 "Invalid command verb, can not generate MGCP message\n");
779 msgb_free(msg);
780 return NULL;
781 }
782
783 /* Check if mandatory fields are missing */
784 if (!((mgcp_msg->presence & mandatory_mask) == mandatory_mask)) {
785 LOGP(DLMGCP, LOGL_ERROR,
786 "One or more missing mandatory fields, can not generate MGCP message\n");
787 msgb_free(msg);
788 return NULL;
789 }
790
791 /* Add endpoint name */
792 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_ENDPOINT)
793 rc += msgb_printf(msg, " %s", mgcp_msg->endpoint);
794
795 /* Add protocol version */
796 rc += msgb_printf(msg, " MGCP 1.0\r\n");
797
798 /* Add call id */
799 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_CALL_ID)
800 rc += msgb_printf(msg, "C: %x\r\n", mgcp_msg->call_id);
801
802 /* Add connection id */
803 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_CONN_ID)
Philipp Maier01d24a32017-11-21 17:26:09 +0100804 rc += msgb_printf(msg, "I: %s\r\n", mgcp_msg->conn_id);
Philipp Maier1dc6be62017-10-05 18:25:37 +0200805
806 /* Add local connection options */
Philipp Maierffd75e42017-11-22 11:44:50 +0100807 if (mgcp_msg->verb == MGCP_VERB_CRCX)
Philipp Maier1dc6be62017-10-05 18:25:37 +0200808 rc += msgb_printf(msg, "L: p:20, a:AMR, nt:IN\r\n");
809
810 /* Add mode */
811 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_CONN_MODE)
812 rc +=
813 msgb_printf(msg, "M: %s\r\n",
814 mgcp_client_cmode_name(mgcp_msg->conn_mode));
815
816 /* Add RTP address and port (SDP) */
817 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_AUDIO_IP
818 && mgcp_msg->presence & MGCP_MSG_PRESENCE_AUDIO_PORT) {
819 rc += msgb_printf(msg, "\r\n");
820 rc += msgb_printf(msg, "c=IN IP4 %s\r\n", mgcp_msg->audio_ip);
821 rc +=
822 msgb_printf(msg, "m=audio %u RTP/AVP 255\r\n",
823 mgcp_msg->audio_port);
824 }
825
826 if (rc != 0) {
827 LOGP(DLMGCP, LOGL_ERROR,
828 "message buffer to small, can not generate MGCP message\n");
829 msgb_free(msg);
830 msg = NULL;
831 }
832
833 return msg;
834}
835
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100836/* Retrieve the MGCP transaction ID from a msgb generated by mgcp_msg_gen() */
837mgcp_trans_id_t mgcp_msg_trans_id(struct msgb *msg)
838{
839 return (mgcp_trans_id_t)msg->cb[MSGB_CB_MGCP_TRANS_ID];
840}
841
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200842struct mgcp_client_conf *mgcp_client_conf_actual(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200843{
844 return &mgcp->actual;
845}
Neels Hofmeyrd95ab1e2017-09-22 00:52:54 +0200846
847const struct value_string mgcp_client_connection_mode_strs[] = {
848 { MGCP_CONN_NONE, "none" },
849 { MGCP_CONN_RECV_SEND, "sendrecv" },
850 { MGCP_CONN_SEND_ONLY, "sendonly" },
851 { MGCP_CONN_RECV_ONLY, "recvonly" },
852 { MGCP_CONN_LOOPBACK, "loopback" },
853 { 0, NULL }
854};