blob: ee8d2675dff17db41e62ba6e70b9c50983f3f4cc [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 Hofmeyre9920f22017-07-10 15:07:22 +0200314 struct mgcp_response *r)
315{
316 struct mgcp_response_pending *pending;
317 if (!r)
318 return NULL;
319 llist_for_each_entry(pending, &mgcp->responses_pending, entry) {
320 if (pending->trans_id == r->head.trans_id) {
321 llist_del(&pending->entry);
322 return pending;
323 }
324 }
325 return NULL;
326}
327
328/* Feed an MGCP message into the receive processing.
329 * Parse the head and call any callback registered for the transaction id found
330 * in the MGCP message. This is normally called directly from the internal
331 * mgcp_do_read that reads from the socket connected to the MGCP gateway. This
332 * function is published mainly to be able to feed data from the test suite.
333 */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200334int mgcp_client_rx(struct mgcp_client *mgcp, struct msgb *msg)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200335{
336 struct mgcp_response r = { 0 };
337 struct mgcp_response_pending *pending;
338 int rc;
339
340 rc = mgcp_response_parse_head(&r, msg);
341 if (rc) {
Philipp Maierffd75e42017-11-22 11:44:50 +0100342 LOGP(DLMGCP, LOGL_ERROR, "Cannot parse MGCP response (head)\n");
343 return -1;
344 }
345
346 rc = parse_head_params(&r);
347 if (rc) {
348 LOGP(DLMGCP, LOGL_ERROR, "Cannot parse MGCP response (head parameters)\n");
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200349 return -1;
350 }
351
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200352 pending = mgcp_client_response_pending_get(mgcp, &r);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200353 if (!pending) {
354 LOGP(DLMGCP, LOGL_ERROR,
355 "Cannot find matching MGCP transaction for trans_id %d\n",
356 r.head.trans_id);
357 return -1;
358 }
359
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200360 mgcp_client_handle_response(mgcp, pending, &r);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200361 return 0;
362}
363
364static int mgcp_do_read(struct osmo_fd *fd)
365{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200366 struct mgcp_client *mgcp = fd->data;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200367 struct msgb *msg;
368 int ret;
369
370 msg = msgb_alloc_headroom(4096, 128, "mgcp_from_gw");
371 if (!msg) {
372 LOGP(DLMGCP, LOGL_ERROR, "Failed to allocate MGCP message.\n");
373 return -1;
374 }
375
376 ret = read(fd->fd, msg->data, 4096 - 128);
377 if (ret <= 0) {
378 LOGP(DLMGCP, LOGL_ERROR, "Failed to read: %d/%s\n", errno, strerror(errno));
379 msgb_free(msg);
380 return -1;
381 } else if (ret > 4096 - 128) {
382 LOGP(DLMGCP, LOGL_ERROR, "Too much data: %d\n", ret);
383 msgb_free(msg);
384 return -1;
Harald Welte9bf7c532017-11-17 14:14:31 +0100385 }
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200386
387 msg->l2h = msgb_put(msg, ret);
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200388 ret = mgcp_client_rx(mgcp, msg);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200389 talloc_free(msg);
390 return ret;
391}
392
393static int mgcp_do_write(struct osmo_fd *fd, struct msgb *msg)
394{
395 int ret;
396 static char strbuf[4096];
397 unsigned int l = msg->len < sizeof(strbuf) ? msg->len : sizeof(strbuf);
398 unsigned int i;
399
Philipp Maierf8bfbe82017-11-23 19:32:31 +0100400 osmo_strlcpy(strbuf, (const char*)msg->data, l);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200401 for (i = 0; i < sizeof(strbuf); i++) {
402 if (strbuf[i] == '\n' || strbuf[i] == '\r') {
403 strbuf[i] = '\0';
404 break;
405 }
406 }
407 DEBUGP(DLMGCP, "Tx MGCP msg to MGCP GW: '%s'\n", strbuf);
408
409 LOGP(DLMGCP, LOGL_DEBUG, "Sending msg to MGCP GW size: %u\n", msg->len);
410
411 ret = write(fd->fd, msg->data, msg->len);
412 if (ret != msg->len)
413 LOGP(DLMGCP, LOGL_ERROR, "Failed to forward message to MGCP"
414 " GW: %s\n", strerror(errno));
415
416 return ret;
417}
418
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200419struct mgcp_client *mgcp_client_init(void *ctx,
420 struct mgcp_client_conf *conf)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200421{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200422 struct mgcp_client *mgcp;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200423
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200424 mgcp = talloc_zero(ctx, struct mgcp_client);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200425
426 INIT_LLIST_HEAD(&mgcp->responses_pending);
427 INIT_LLIST_HEAD(&mgcp->inuse_endpoints);
428
429 mgcp->next_trans_id = 1;
430
431 mgcp->actual.local_addr = conf->local_addr ? conf->local_addr :
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200432 MGCP_CLIENT_LOCAL_ADDR_DEFAULT;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200433 mgcp->actual.local_port = conf->local_port >= 0 ? (uint16_t)conf->local_port :
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200434 MGCP_CLIENT_LOCAL_PORT_DEFAULT;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200435
436 mgcp->actual.remote_addr = conf->remote_addr ? conf->remote_addr :
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200437 MGCP_CLIENT_REMOTE_ADDR_DEFAULT;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200438 mgcp->actual.remote_port = conf->remote_port >= 0 ? (uint16_t)conf->remote_port :
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200439 MGCP_CLIENT_REMOTE_PORT_DEFAULT;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200440
441 mgcp->actual.first_endpoint = conf->first_endpoint > 0 ? (uint16_t)conf->first_endpoint : 0;
442 mgcp->actual.last_endpoint = conf->last_endpoint > 0 ? (uint16_t)conf->last_endpoint : 0;
Neels Hofmeyrc0dcc3c2017-12-02 18:31:34 +0000443 mgcp->actual.bts_base = conf->bts_base > 0 ? (uint16_t)conf->bts_base : 4000;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200444
445 return mgcp;
446}
447
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200448int mgcp_client_connect(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200449{
450 int on;
451 struct sockaddr_in addr;
452 struct osmo_wqueue *wq;
453 int rc;
454
455 if (!mgcp) {
456 LOGP(DLMGCP, LOGL_FATAL, "MGCPGW client not initialized properly\n");
457 return -EINVAL;
458 }
459
460 wq = &mgcp->wq;
461
462 wq->bfd.fd = socket(AF_INET, SOCK_DGRAM, 0);
463 if (wq->bfd.fd < 0) {
464 LOGP(DLMGCP, LOGL_FATAL, "Failed to create UDP socket errno: %d\n", errno);
465 return -errno;
466 }
467
468 on = 1;
469 if (setsockopt(wq->bfd.fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) {
470 LOGP(DLMGCP, LOGL_FATAL,
471 "Failed to initialize socket for MGCP GW: %s\n",
472 strerror(errno));
473 rc = -errno;
474 goto error_close_fd;
475 }
476
477 /* bind socket */
478 memset(&addr, 0, sizeof(addr));
479 addr.sin_family = AF_INET;
480 inet_aton(mgcp->actual.local_addr, &addr.sin_addr);
481 addr.sin_port = htons(mgcp->actual.local_port);
482 if (bind(wq->bfd.fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
483 LOGP(DLMGCP, LOGL_FATAL,
484 "Failed to bind for MGCP GW to %s %u\n",
485 mgcp->actual.local_addr, mgcp->actual.local_port);
486 rc = -errno;
487 goto error_close_fd;
488 }
489
490 /* connect to the remote */
491 inet_aton(mgcp->actual.remote_addr, &addr.sin_addr);
492 addr.sin_port = htons(mgcp->actual.remote_port);
493 if (connect(wq->bfd.fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
494 LOGP(DLMGCP, LOGL_FATAL,
495 "Failed to connect to MGCP GW at %s %u: %s\n",
496 mgcp->actual.remote_addr, mgcp->actual.remote_port,
497 strerror(errno));
498 rc = -errno;
499 goto error_close_fd;
500 }
501
502 mgcp->remote_addr = htonl(addr.sin_addr.s_addr);
503
504 osmo_wqueue_init(wq, 10);
505 wq->bfd.when = BSC_FD_READ;
506 wq->bfd.data = mgcp;
507 wq->read_cb = mgcp_do_read;
508 wq->write_cb = mgcp_do_write;
509
510 if (osmo_fd_register(&wq->bfd) != 0) {
511 LOGP(DLMGCP, LOGL_FATAL, "Failed to register BFD\n");
512 rc = -EIO;
513 goto error_close_fd;
514 }
515 LOGP(DLMGCP, LOGL_INFO, "MGCP GW connection: %s:%u -> %s:%u\n",
516 mgcp->actual.local_addr, mgcp->actual.local_port,
517 mgcp->actual.remote_addr, mgcp->actual.remote_port);
518
519 return 0;
520error_close_fd:
521 close(wq->bfd.fd);
522 wq->bfd.fd = -1;
523 return rc;
524}
525
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200526const char *mgcp_client_remote_addr_str(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200527{
528 return mgcp->actual.remote_addr;
529}
530
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200531uint16_t mgcp_client_remote_port(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200532{
533 return mgcp->actual.remote_port;
534}
535
536/* Return the MGCP GW binary IPv4 address in network byte order. */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200537uint32_t mgcp_client_remote_addr_n(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200538{
539 return mgcp->remote_addr;
540}
541
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200542struct mgcp_response_pending * mgcp_client_pending_add(
543 struct mgcp_client *mgcp,
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200544 mgcp_trans_id_t trans_id,
545 mgcp_response_cb_t response_cb,
546 void *priv)
547{
548 struct mgcp_response_pending *pending;
549
550 pending = talloc_zero(mgcp, struct mgcp_response_pending);
551 pending->trans_id = trans_id;
552 pending->response_cb = response_cb;
553 pending->priv = priv;
554 llist_add_tail(&pending->entry, &mgcp->responses_pending);
555
556 return pending;
557}
558
559/* Send the MGCP message in msg to the MGCP GW and handle a response with
560 * response_cb. NOTE: the response_cb still needs to call
561 * mgcp_response_parse_params(response) to get the parsed parameters -- to
562 * potentially save some CPU cycles, only the head line has been parsed when
563 * the response_cb is invoked. */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200564int mgcp_client_tx(struct mgcp_client *mgcp, struct msgb *msg,
565 mgcp_response_cb_t response_cb, void *priv)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200566{
567 struct mgcp_response_pending *pending;
568 mgcp_trans_id_t trans_id;
569 int rc;
570
571 trans_id = msg->cb[MSGB_CB_MGCP_TRANS_ID];
572 if (!trans_id) {
573 LOGP(DLMGCP, LOGL_ERROR,
574 "Unset transaction id in mgcp send request\n");
575 talloc_free(msg);
576 return -EINVAL;
577 }
578
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200579 pending = mgcp_client_pending_add(mgcp, trans_id, response_cb, priv);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200580
581 if (msgb_l2len(msg) > 4096) {
582 LOGP(DLMGCP, LOGL_ERROR,
583 "Cannot send, MGCP message too large: %u\n",
584 msgb_l2len(msg));
585 msgb_free(msg);
586 rc = -EINVAL;
587 goto mgcp_tx_error;
588 }
589
590 rc = osmo_wqueue_enqueue(&mgcp->wq, msg);
591 if (rc) {
592 LOGP(DLMGCP, LOGL_FATAL, "Could not queue message to MGCP GW\n");
593 msgb_free(msg);
594 goto mgcp_tx_error;
595 } else
596 LOGP(DLMGCP, LOGL_INFO, "Queued %u bytes for MGCP GW\n",
597 msgb_l2len(msg));
598 return 0;
599
600mgcp_tx_error:
601 /* Pass NULL to response cb to indicate an error */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200602 mgcp_client_handle_response(mgcp, pending, NULL);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200603 return -1;
604}
605
606static struct msgb *mgcp_msg_from_buf(mgcp_trans_id_t trans_id,
607 const char *buf, int len)
608{
609 struct msgb *msg;
610
611 if (len > (4096 - 128)) {
612 LOGP(DLMGCP, LOGL_ERROR, "Cannot send to MGCP GW:"
613 " message too large: %d\n", len);
614 return NULL;
615 }
616
617 msg = msgb_alloc_headroom(4096, 128, "MGCP tx");
618 OSMO_ASSERT(msg);
619
620 char *dst = (char*)msgb_put(msg, len);
621 memcpy(dst, buf, len);
622 msg->l2h = msg->data;
623 msg->cb[MSGB_CB_MGCP_TRANS_ID] = trans_id;
624
625 return msg;
626}
627
628static struct msgb *mgcp_msg_from_str(mgcp_trans_id_t trans_id,
629 const char *fmt, ...)
630{
631 static char compose[4096 - 128];
632 va_list ap;
633 int len;
634 OSMO_ASSERT(fmt);
635
636 va_start(ap, fmt);
637 len = vsnprintf(compose, sizeof(compose), fmt, ap);
638 va_end(ap);
639 if (len >= sizeof(compose)) {
640 LOGP(DLMGCP, LOGL_ERROR,
641 "Message too large: trans_id=%u len=%d\n",
642 trans_id, len);
643 return NULL;
644 }
645 if (len < 1) {
646 LOGP(DLMGCP, LOGL_ERROR,
647 "Failed to compose message: trans_id=%u len=%d\n",
648 trans_id, len);
649 return NULL;
650 }
651 return mgcp_msg_from_buf(trans_id, compose, len);
652}
653
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200654static mgcp_trans_id_t mgcp_client_next_trans_id(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200655{
656 /* avoid zero trans_id to distinguish from unset trans_id */
657 if (!mgcp->next_trans_id)
658 mgcp->next_trans_id ++;
659 return mgcp->next_trans_id ++;
660}
661
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200662struct msgb *mgcp_msg_crcx(struct mgcp_client *mgcp,
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200663 uint16_t rtp_endpoint, unsigned int call_id,
664 enum mgcp_connection_mode mode)
665{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200666 mgcp_trans_id_t trans_id = mgcp_client_next_trans_id(mgcp);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200667 return mgcp_msg_from_str(trans_id,
668 "CRCX %u %x@mgw MGCP 1.0\r\n"
669 "C: %x\r\n"
670 "L: p:20, a:AMR, nt:IN\r\n"
671 "M: %s\r\n"
672 ,
673 trans_id,
674 rtp_endpoint,
675 call_id,
Neels Hofmeyrd95ab1e2017-09-22 00:52:54 +0200676 mgcp_client_cmode_name(mode));
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200677}
678
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200679struct msgb *mgcp_msg_mdcx(struct mgcp_client *mgcp,
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200680 uint16_t rtp_endpoint, const char *rtp_conn_addr,
681 uint16_t rtp_port, enum mgcp_connection_mode mode)
682
683{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200684 mgcp_trans_id_t trans_id = mgcp_client_next_trans_id(mgcp);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200685 return mgcp_msg_from_str(trans_id,
686 "MDCX %u %x@mgw MGCP 1.0\r\n"
687 "M: %s\r\n"
688 "\r\n"
689 "c=IN IP4 %s\r\n"
690 "m=audio %u RTP/AVP 255\r\n"
691 ,
692 trans_id,
693 rtp_endpoint,
Neels Hofmeyrd95ab1e2017-09-22 00:52:54 +0200694 mgcp_client_cmode_name(mode),
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200695 rtp_conn_addr,
696 rtp_port);
697}
698
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200699struct msgb *mgcp_msg_dlcx(struct mgcp_client *mgcp, uint16_t rtp_endpoint,
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200700 unsigned int call_id)
701{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200702 mgcp_trans_id_t trans_id = mgcp_client_next_trans_id(mgcp);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200703 return mgcp_msg_from_str(trans_id,
704 "DLCX %u %x@mgw MGCP 1.0\r\n"
705 "C: %x\r\n", trans_id, rtp_endpoint, call_id);
706}
707
Philipp Maier1dc6be62017-10-05 18:25:37 +0200708#define MGCP_CRCX_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT | \
709 MGCP_MSG_PRESENCE_CALL_ID | \
Philipp Maier1dc6be62017-10-05 18:25:37 +0200710 MGCP_MSG_PRESENCE_CONN_MODE)
711#define MGCP_MDCX_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT | \
712 MGCP_MSG_PRESENCE_CONN_ID)
713#define MGCP_DLCX_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT)
714#define MGCP_AUEP_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT)
715#define MGCP_RSIP_MANDATORY 0 /* none */
716
717struct msgb *mgcp_msg_gen(struct mgcp_client *mgcp, struct mgcp_msg *mgcp_msg)
718{
719 mgcp_trans_id_t trans_id = mgcp_client_next_trans_id(mgcp);
720 uint32_t mandatory_mask;
721 struct msgb *msg = msgb_alloc_headroom(4096, 128, "MGCP tx");
722 int rc = 0;
723
724 msg->l2h = msg->data;
725 msg->cb[MSGB_CB_MGCP_TRANS_ID] = trans_id;
726
727 /* Add command verb */
728 switch (mgcp_msg->verb) {
729 case MGCP_VERB_CRCX:
730 mandatory_mask = MGCP_CRCX_MANDATORY;
731 rc += msgb_printf(msg, "CRCX %u", trans_id);
732 break;
733 case MGCP_VERB_MDCX:
734 mandatory_mask = MGCP_MDCX_MANDATORY;
735 rc += msgb_printf(msg, "MDCX %u", trans_id);
736 break;
737 case MGCP_VERB_DLCX:
738 mandatory_mask = MGCP_DLCX_MANDATORY;
739 rc += msgb_printf(msg, "DLCX %u", trans_id);
740 break;
741 case MGCP_VERB_AUEP:
742 mandatory_mask = MGCP_AUEP_MANDATORY;
743 rc += msgb_printf(msg, "AUEP %u", trans_id);
744 break;
745 case MGCP_VERB_RSIP:
746 mandatory_mask = MGCP_RSIP_MANDATORY;
747 rc += msgb_printf(msg, "RSIP %u", trans_id);
748 break;
749 default:
750 LOGP(DLMGCP, LOGL_ERROR,
751 "Invalid command verb, can not generate MGCP message\n");
752 msgb_free(msg);
753 return NULL;
754 }
755
756 /* Check if mandatory fields are missing */
757 if (!((mgcp_msg->presence & mandatory_mask) == mandatory_mask)) {
758 LOGP(DLMGCP, LOGL_ERROR,
759 "One or more missing mandatory fields, can not generate MGCP message\n");
760 msgb_free(msg);
761 return NULL;
762 }
763
764 /* Add endpoint name */
765 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_ENDPOINT)
766 rc += msgb_printf(msg, " %s", mgcp_msg->endpoint);
767
768 /* Add protocol version */
769 rc += msgb_printf(msg, " MGCP 1.0\r\n");
770
771 /* Add call id */
772 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_CALL_ID)
773 rc += msgb_printf(msg, "C: %x\r\n", mgcp_msg->call_id);
774
775 /* Add connection id */
776 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_CONN_ID)
Philipp Maier01d24a32017-11-21 17:26:09 +0100777 rc += msgb_printf(msg, "I: %s\r\n", mgcp_msg->conn_id);
Philipp Maier1dc6be62017-10-05 18:25:37 +0200778
779 /* Add local connection options */
Philipp Maierffd75e42017-11-22 11:44:50 +0100780 if (mgcp_msg->verb == MGCP_VERB_CRCX)
Philipp Maier1dc6be62017-10-05 18:25:37 +0200781 rc += msgb_printf(msg, "L: p:20, a:AMR, nt:IN\r\n");
782
783 /* Add mode */
784 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_CONN_MODE)
785 rc +=
786 msgb_printf(msg, "M: %s\r\n",
787 mgcp_client_cmode_name(mgcp_msg->conn_mode));
788
789 /* Add RTP address and port (SDP) */
790 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_AUDIO_IP
791 && mgcp_msg->presence & MGCP_MSG_PRESENCE_AUDIO_PORT) {
792 rc += msgb_printf(msg, "\r\n");
793 rc += msgb_printf(msg, "c=IN IP4 %s\r\n", mgcp_msg->audio_ip);
794 rc +=
795 msgb_printf(msg, "m=audio %u RTP/AVP 255\r\n",
796 mgcp_msg->audio_port);
797 }
798
799 if (rc != 0) {
800 LOGP(DLMGCP, LOGL_ERROR,
801 "message buffer to small, can not generate MGCP message\n");
802 msgb_free(msg);
803 msg = NULL;
804 }
805
806 return msg;
807}
808
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200809struct mgcp_client_conf *mgcp_client_conf_actual(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200810{
811 return &mgcp->actual;
812}
Neels Hofmeyrd95ab1e2017-09-22 00:52:54 +0200813
814const struct value_string mgcp_client_connection_mode_strs[] = {
815 { MGCP_CONN_NONE, "none" },
816 { MGCP_CONN_RECV_SEND, "sendrecv" },
817 { MGCP_CONN_SEND_ONLY, "sendonly" },
818 { MGCP_CONN_RECV_ONLY, "recvonly" },
819 { MGCP_CONN_LOOPBACK, "loopback" },
820 { 0, NULL }
821};