blob: 9fc414d39e63ce9132186add46d27e98d4f1788f [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
142 r->head.comment = r->body + comment_pos;
143 end = strchr(r->head.comment, '\r');
144 if (!end)
145 goto response_parse_failure;
146 /* Mark the end of the comment */
147 *end = '\0';
148 r->body = end + 1;
149 if (r->body[0] == '\n')
150 r->body ++;
151 return 0;
152
153response_parse_failure:
154 LOGP(DLMGCP, LOGL_ERROR,
155 "Failed to parse MGCP response header\n");
156 return -EINVAL;
157}
158
159/* TODO undup against mgcp_protocol.c:mgcp_check_param() */
160static bool mgcp_line_is_valid(const char *line)
161{
162 const size_t line_len = strlen(line);
163 if (line[0] == '\0')
164 return true;
165
166 if (line_len < 2
167 || line[1] != '=') {
168 LOGP(DLMGCP, LOGL_ERROR,
169 "Wrong MGCP option format: '%s'\n",
170 line);
171 return false;
172 }
173
174 return true;
175}
176
177/* Parse a line like "m=audio 16002 RTP/AVP 98" */
Philipp Maier06da85e2017-10-05 18:49:24 +0200178static int mgcp_parse_audio_port(struct mgcp_response *r, const char *line)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200179{
Harald Welte9bf7c532017-11-17 14:14:31 +0100180 if (sscanf(line, "m=audio %hu",
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200181 &r->audio_port) != 1)
182 goto response_parse_failure;
183
Philipp Maier10f32db2017-12-13 12:34:34 +0100184 if (r->audio_port == 0)
185 goto response_parse_failure;
186
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200187 return 0;
188
189response_parse_failure:
190 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier06da85e2017-10-05 18:49:24 +0200191 "Failed to parse MGCP response header (audio port)\n");
192 return -EINVAL;
193}
194
195/* Parse a line like "c=IN IP4 10.11.12.13" */
196static int mgcp_parse_audio_ip(struct mgcp_response *r, const char *line)
197{
198 struct in_addr ip_test;
199
200 if (strlen(line) < 16)
201 goto response_parse_failure;
202
203 /* The current implementation strictly supports IPV4 only ! */
204 if (memcmp("c=IN IP4 ", line, 9) != 0)
205 goto response_parse_failure;
206
207 /* Extract IP-Address */
Philipp Maierf8bfbe82017-11-23 19:32:31 +0100208 osmo_strlcpy(r->audio_ip, line + 9, sizeof(r->audio_ip));
Philipp Maier06da85e2017-10-05 18:49:24 +0200209
210 /* Check IP-Address */
211 if (inet_aton(r->audio_ip, &ip_test) == 0)
212 goto response_parse_failure;
213
214 return 0;
215
216response_parse_failure:
217 LOGP(DLMGCP, LOGL_ERROR,
218 "Failed to parse MGCP response header (audio ip)\n");
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200219 return -EINVAL;
220}
221
222int mgcp_response_parse_params(struct mgcp_response *r)
223{
224 char *line;
225 int rc;
226 OSMO_ASSERT(r->body);
227 char *data = strstr(r->body, "\n\n");
228
229 if (!data) {
230 LOGP(DLMGCP, LOGL_ERROR,
231 "MGCP response: cannot find start of parameters\n");
232 return -EINVAL;
233 }
234
235 /* Advance to after the \n\n, replace the second \n with \0. That's
236 * where the parameters start. */
237 data ++;
238 *data = '\0';
239 data ++;
240
Neels Hofmeyrd95ab1e2017-09-22 00:52:54 +0200241 for_each_non_empty_line(line, data) {
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200242 if (!mgcp_line_is_valid(line))
243 return -EINVAL;
244
245 switch (line[0]) {
246 case 'm':
Philipp Maier06da85e2017-10-05 18:49:24 +0200247 rc = mgcp_parse_audio_port(r, line);
248 if (rc)
249 return rc;
250 break;
251 case 'c':
252 rc = mgcp_parse_audio_ip(r, line);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200253 if (rc)
254 return rc;
255 break;
256 default:
257 /* skip unhandled parameters */
258 break;
259 }
260 }
261 return 0;
262}
263
Philipp Maierffd75e42017-11-22 11:44:50 +0100264/* Parse a line like "I: 0cedfd5a19542d197af9afe5231f1d61" */
265static int mgcp_parse_conn_id(struct mgcp_response *r, const char *line)
266{
267 if (strlen(line) < 4)
268 goto response_parse_failure;
269
270 if (memcmp("I: ", line, 3) != 0)
271 goto response_parse_failure;
272
273 osmo_strlcpy(r->head.conn_id, line + 3, sizeof(r->head.conn_id));
274 return 0;
275
276response_parse_failure:
277 LOGP(DLMGCP, LOGL_ERROR,
278 "Failed to parse MGCP response (connectionIdentifier)\n");
279 return -EINVAL;
280}
281
282/* Parse MGCP parameters of the response */
283static int parse_head_params(struct mgcp_response *r)
284{
285 char *line;
286 int rc = 0;
287 OSMO_ASSERT(r->body);
288 char *data = r->body;
289 char *data_end = strstr(r->body, "\n\n");
290
291 /* Protect SDP body, for_each_non_empty_line() will
292 * only parse until it hits \0 mark. */
293 if (data_end)
294 *data_end = '\0';
295
296 for_each_non_empty_line(line, data) {
297 switch (line[0]) {
298 case 'I':
299 rc = mgcp_parse_conn_id(r, line);
300 if (rc)
301 goto exit;
302 break;
303 default:
304 /* skip unhandled parameters */
305 break;
306 }
307 }
308exit:
309 /* Restore original state */
310 if (data_end)
311 *data_end = '\n';
312
313 return rc;
314}
315
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200316static struct mgcp_response_pending *mgcp_client_response_pending_get(
317 struct mgcp_client *mgcp,
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100318 mgcp_trans_id_t trans_id)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200319{
320 struct mgcp_response_pending *pending;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200321 llist_for_each_entry(pending, &mgcp->responses_pending, entry) {
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100322 if (pending->trans_id == trans_id) {
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200323 llist_del(&pending->entry);
324 return pending;
325 }
326 }
327 return NULL;
328}
329
330/* Feed an MGCP message into the receive processing.
331 * Parse the head and call any callback registered for the transaction id found
332 * in the MGCP message. This is normally called directly from the internal
333 * mgcp_do_read that reads from the socket connected to the MGCP gateway. This
334 * function is published mainly to be able to feed data from the test suite.
335 */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200336int mgcp_client_rx(struct mgcp_client *mgcp, struct msgb *msg)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200337{
338 struct mgcp_response r = { 0 };
339 struct mgcp_response_pending *pending;
340 int rc;
341
342 rc = mgcp_response_parse_head(&r, msg);
343 if (rc) {
Philipp Maierffd75e42017-11-22 11:44:50 +0100344 LOGP(DLMGCP, LOGL_ERROR, "Cannot parse MGCP response (head)\n");
345 return -1;
346 }
347
348 rc = parse_head_params(&r);
349 if (rc) {
350 LOGP(DLMGCP, LOGL_ERROR, "Cannot parse MGCP response (head parameters)\n");
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200351 return -1;
352 }
353
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100354 pending = mgcp_client_response_pending_get(mgcp, r.head.trans_id);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200355 if (!pending) {
356 LOGP(DLMGCP, LOGL_ERROR,
357 "Cannot find matching MGCP transaction for trans_id %d\n",
358 r.head.trans_id);
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100359 return -ENOENT;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200360 }
361
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200362 mgcp_client_handle_response(mgcp, pending, &r);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200363 return 0;
364}
365
366static int mgcp_do_read(struct osmo_fd *fd)
367{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200368 struct mgcp_client *mgcp = fd->data;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200369 struct msgb *msg;
370 int ret;
371
372 msg = msgb_alloc_headroom(4096, 128, "mgcp_from_gw");
373 if (!msg) {
374 LOGP(DLMGCP, LOGL_ERROR, "Failed to allocate MGCP message.\n");
375 return -1;
376 }
377
378 ret = read(fd->fd, msg->data, 4096 - 128);
379 if (ret <= 0) {
380 LOGP(DLMGCP, LOGL_ERROR, "Failed to read: %d/%s\n", errno, strerror(errno));
381 msgb_free(msg);
382 return -1;
383 } else if (ret > 4096 - 128) {
384 LOGP(DLMGCP, LOGL_ERROR, "Too much data: %d\n", ret);
385 msgb_free(msg);
386 return -1;
Harald Welte9bf7c532017-11-17 14:14:31 +0100387 }
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200388
389 msg->l2h = msgb_put(msg, ret);
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200390 ret = mgcp_client_rx(mgcp, msg);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200391 talloc_free(msg);
392 return ret;
393}
394
395static int mgcp_do_write(struct osmo_fd *fd, struct msgb *msg)
396{
397 int ret;
398 static char strbuf[4096];
399 unsigned int l = msg->len < sizeof(strbuf) ? msg->len : sizeof(strbuf);
400 unsigned int i;
401
Philipp Maierf8bfbe82017-11-23 19:32:31 +0100402 osmo_strlcpy(strbuf, (const char*)msg->data, l);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200403 for (i = 0; i < sizeof(strbuf); i++) {
404 if (strbuf[i] == '\n' || strbuf[i] == '\r') {
405 strbuf[i] = '\0';
406 break;
407 }
408 }
409 DEBUGP(DLMGCP, "Tx MGCP msg to MGCP GW: '%s'\n", strbuf);
410
411 LOGP(DLMGCP, LOGL_DEBUG, "Sending msg to MGCP GW size: %u\n", msg->len);
412
413 ret = write(fd->fd, msg->data, msg->len);
414 if (ret != msg->len)
415 LOGP(DLMGCP, LOGL_ERROR, "Failed to forward message to MGCP"
416 " GW: %s\n", strerror(errno));
417
418 return ret;
419}
420
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200421struct mgcp_client *mgcp_client_init(void *ctx,
422 struct mgcp_client_conf *conf)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200423{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200424 struct mgcp_client *mgcp;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200425
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200426 mgcp = talloc_zero(ctx, struct mgcp_client);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200427
428 INIT_LLIST_HEAD(&mgcp->responses_pending);
429 INIT_LLIST_HEAD(&mgcp->inuse_endpoints);
430
431 mgcp->next_trans_id = 1;
432
433 mgcp->actual.local_addr = conf->local_addr ? conf->local_addr :
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200434 MGCP_CLIENT_LOCAL_ADDR_DEFAULT;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200435 mgcp->actual.local_port = conf->local_port >= 0 ? (uint16_t)conf->local_port :
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200436 MGCP_CLIENT_LOCAL_PORT_DEFAULT;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200437
438 mgcp->actual.remote_addr = conf->remote_addr ? conf->remote_addr :
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200439 MGCP_CLIENT_REMOTE_ADDR_DEFAULT;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200440 mgcp->actual.remote_port = conf->remote_port >= 0 ? (uint16_t)conf->remote_port :
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200441 MGCP_CLIENT_REMOTE_PORT_DEFAULT;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200442
443 mgcp->actual.first_endpoint = conf->first_endpoint > 0 ? (uint16_t)conf->first_endpoint : 0;
444 mgcp->actual.last_endpoint = conf->last_endpoint > 0 ? (uint16_t)conf->last_endpoint : 0;
Neels Hofmeyrc0dcc3c2017-12-02 18:31:34 +0000445 mgcp->actual.bts_base = conf->bts_base > 0 ? (uint16_t)conf->bts_base : 4000;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200446
447 return mgcp;
448}
449
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200450int mgcp_client_connect(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200451{
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200452 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
Harald Welte8890dfa2017-11-17 15:09:30 +0100463 rc = osmo_sock_init2_ofd(&wq->bfd, AF_INET, SOCK_DGRAM, IPPROTO_UDP,
464 mgcp->actual.local_addr, mgcp->actual.local_port,
465 mgcp->actual.remote_addr, mgcp->actual.remote_port,
466 OSMO_SOCK_F_BIND | OSMO_SOCK_F_CONNECT);
467 if (rc < 0) {
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200468 LOGP(DLMGCP, LOGL_FATAL,
Harald Welte8890dfa2017-11-17 15:09:30 +0100469 "Failed to initialize socket %s:%u -> %s:%u for MGCP GW: %s\n",
470 mgcp->actual.local_addr, mgcp->actual.local_port,
471 mgcp->actual.remote_addr, mgcp->actual.remote_port, strerror(errno));
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200472 goto error_close_fd;
473 }
474
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200475 inet_aton(mgcp->actual.remote_addr, &addr.sin_addr);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200476 mgcp->remote_addr = htonl(addr.sin_addr.s_addr);
477
478 osmo_wqueue_init(wq, 10);
479 wq->bfd.when = BSC_FD_READ;
480 wq->bfd.data = mgcp;
481 wq->read_cb = mgcp_do_read;
482 wq->write_cb = mgcp_do_write;
483
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200484 LOGP(DLMGCP, LOGL_INFO, "MGCP GW connection: %s:%u -> %s:%u\n",
485 mgcp->actual.local_addr, mgcp->actual.local_port,
486 mgcp->actual.remote_addr, mgcp->actual.remote_port);
487
488 return 0;
489error_close_fd:
490 close(wq->bfd.fd);
491 wq->bfd.fd = -1;
492 return rc;
493}
494
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200495const char *mgcp_client_remote_addr_str(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200496{
497 return mgcp->actual.remote_addr;
498}
499
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200500uint16_t mgcp_client_remote_port(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200501{
502 return mgcp->actual.remote_port;
503}
504
505/* Return the MGCP GW binary IPv4 address in network byte order. */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200506uint32_t mgcp_client_remote_addr_n(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200507{
508 return mgcp->remote_addr;
509}
510
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200511struct mgcp_response_pending * mgcp_client_pending_add(
512 struct mgcp_client *mgcp,
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200513 mgcp_trans_id_t trans_id,
514 mgcp_response_cb_t response_cb,
515 void *priv)
516{
517 struct mgcp_response_pending *pending;
518
519 pending = talloc_zero(mgcp, struct mgcp_response_pending);
520 pending->trans_id = trans_id;
521 pending->response_cb = response_cb;
522 pending->priv = priv;
523 llist_add_tail(&pending->entry, &mgcp->responses_pending);
524
525 return pending;
526}
527
528/* Send the MGCP message in msg to the MGCP GW and handle a response with
529 * response_cb. NOTE: the response_cb still needs to call
530 * mgcp_response_parse_params(response) to get the parsed parameters -- to
531 * potentially save some CPU cycles, only the head line has been parsed when
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100532 * the response_cb is invoked.
533 * Before the priv pointer becomes invalid, e.g. due to transaction timeout,
534 * mgcp_client_cancel() needs to be called for this transaction.
535 */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200536int mgcp_client_tx(struct mgcp_client *mgcp, struct msgb *msg,
537 mgcp_response_cb_t response_cb, void *priv)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200538{
539 struct mgcp_response_pending *pending;
540 mgcp_trans_id_t trans_id;
541 int rc;
542
543 trans_id = msg->cb[MSGB_CB_MGCP_TRANS_ID];
544 if (!trans_id) {
545 LOGP(DLMGCP, LOGL_ERROR,
546 "Unset transaction id in mgcp send request\n");
547 talloc_free(msg);
548 return -EINVAL;
549 }
550
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200551 pending = mgcp_client_pending_add(mgcp, trans_id, response_cb, priv);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200552
553 if (msgb_l2len(msg) > 4096) {
554 LOGP(DLMGCP, LOGL_ERROR,
555 "Cannot send, MGCP message too large: %u\n",
556 msgb_l2len(msg));
557 msgb_free(msg);
558 rc = -EINVAL;
559 goto mgcp_tx_error;
560 }
561
562 rc = osmo_wqueue_enqueue(&mgcp->wq, msg);
563 if (rc) {
564 LOGP(DLMGCP, LOGL_FATAL, "Could not queue message to MGCP GW\n");
565 msgb_free(msg);
566 goto mgcp_tx_error;
567 } else
568 LOGP(DLMGCP, LOGL_INFO, "Queued %u bytes for MGCP GW\n",
569 msgb_l2len(msg));
570 return 0;
571
572mgcp_tx_error:
573 /* Pass NULL to response cb to indicate an error */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200574 mgcp_client_handle_response(mgcp, pending, NULL);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200575 return -1;
576}
577
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100578/* Cancel a pending transaction.
579 * Should a priv pointer passed to mgcp_client_tx() become invalid, this function must be called. In
580 * practical terms, if the caller of mgcp_client_tx() wishes to tear down a transaction without having
581 * received a response this function must be called. The trans_id can be obtained by calling
582 * mgcp_msg_trans_id() on the msgb produced by mgcp_msg_gen().
583 */
584int mgcp_client_cancel(struct mgcp_client *mgcp, mgcp_trans_id_t trans_id)
585{
586 struct mgcp_response_pending *pending = mgcp_client_response_pending_get(mgcp, trans_id);
587 if (!pending) {
588 /* INFO is sufficient, it is not harmful to cancel a transaction twice. */
589 LOGP(DLMGCP, LOGL_INFO, "Cannot cancel, no such transaction: %u\n", trans_id);
590 return -ENOENT;
591 }
592 LOGP(DLMGCP, LOGL_INFO, "Canceled transaction %u\n", trans_id);
593 talloc_free(pending);
594 return 0;
595 /* We don't really need to clean up the wqueue: In all sane cases, the msgb has already been sent
596 * out and is no longer in the wqueue. If it still is in the wqueue, then sending MGCP messages
597 * per se is broken and the program should notice so by a full wqueue. Even if this was called
598 * before we had a chance to send out the message and it is still going to be sent, we will just
599 * ignore the reply to it later. Removing a msgb from the wqueue here would just introduce more
600 * bug surface in terms of failing to update wqueue API's counters or some such.
601 */
602}
603
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200604static struct msgb *mgcp_msg_from_buf(mgcp_trans_id_t trans_id,
605 const char *buf, int len)
606{
607 struct msgb *msg;
608
609 if (len > (4096 - 128)) {
610 LOGP(DLMGCP, LOGL_ERROR, "Cannot send to MGCP GW:"
611 " message too large: %d\n", len);
612 return NULL;
613 }
614
615 msg = msgb_alloc_headroom(4096, 128, "MGCP tx");
616 OSMO_ASSERT(msg);
617
618 char *dst = (char*)msgb_put(msg, len);
619 memcpy(dst, buf, len);
620 msg->l2h = msg->data;
621 msg->cb[MSGB_CB_MGCP_TRANS_ID] = trans_id;
622
623 return msg;
624}
625
626static struct msgb *mgcp_msg_from_str(mgcp_trans_id_t trans_id,
627 const char *fmt, ...)
628{
629 static char compose[4096 - 128];
630 va_list ap;
631 int len;
632 OSMO_ASSERT(fmt);
633
634 va_start(ap, fmt);
635 len = vsnprintf(compose, sizeof(compose), fmt, ap);
636 va_end(ap);
637 if (len >= sizeof(compose)) {
638 LOGP(DLMGCP, LOGL_ERROR,
639 "Message too large: trans_id=%u len=%d\n",
640 trans_id, len);
641 return NULL;
642 }
643 if (len < 1) {
644 LOGP(DLMGCP, LOGL_ERROR,
645 "Failed to compose message: trans_id=%u len=%d\n",
646 trans_id, len);
647 return NULL;
648 }
649 return mgcp_msg_from_buf(trans_id, compose, len);
650}
651
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200652static mgcp_trans_id_t mgcp_client_next_trans_id(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200653{
654 /* avoid zero trans_id to distinguish from unset trans_id */
655 if (!mgcp->next_trans_id)
656 mgcp->next_trans_id ++;
657 return mgcp->next_trans_id ++;
658}
659
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200660struct msgb *mgcp_msg_crcx(struct mgcp_client *mgcp,
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200661 uint16_t rtp_endpoint, unsigned int call_id,
662 enum mgcp_connection_mode mode)
663{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200664 mgcp_trans_id_t trans_id = mgcp_client_next_trans_id(mgcp);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200665 return mgcp_msg_from_str(trans_id,
666 "CRCX %u %x@mgw MGCP 1.0\r\n"
667 "C: %x\r\n"
668 "L: p:20, a:AMR, nt:IN\r\n"
669 "M: %s\r\n"
670 ,
671 trans_id,
672 rtp_endpoint,
673 call_id,
Neels Hofmeyrd95ab1e2017-09-22 00:52:54 +0200674 mgcp_client_cmode_name(mode));
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200675}
676
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200677struct msgb *mgcp_msg_mdcx(struct mgcp_client *mgcp,
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200678 uint16_t rtp_endpoint, const char *rtp_conn_addr,
679 uint16_t rtp_port, enum mgcp_connection_mode mode)
680
681{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200682 mgcp_trans_id_t trans_id = mgcp_client_next_trans_id(mgcp);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200683 return mgcp_msg_from_str(trans_id,
684 "MDCX %u %x@mgw MGCP 1.0\r\n"
685 "M: %s\r\n"
686 "\r\n"
687 "c=IN IP4 %s\r\n"
688 "m=audio %u RTP/AVP 255\r\n"
689 ,
690 trans_id,
691 rtp_endpoint,
Neels Hofmeyrd95ab1e2017-09-22 00:52:54 +0200692 mgcp_client_cmode_name(mode),
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200693 rtp_conn_addr,
694 rtp_port);
695}
696
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200697struct msgb *mgcp_msg_dlcx(struct mgcp_client *mgcp, uint16_t rtp_endpoint,
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200698 unsigned int call_id)
699{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200700 mgcp_trans_id_t trans_id = mgcp_client_next_trans_id(mgcp);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200701 return mgcp_msg_from_str(trans_id,
702 "DLCX %u %x@mgw MGCP 1.0\r\n"
703 "C: %x\r\n", trans_id, rtp_endpoint, call_id);
704}
705
Philipp Maier1dc6be62017-10-05 18:25:37 +0200706#define MGCP_CRCX_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT | \
707 MGCP_MSG_PRESENCE_CALL_ID | \
Philipp Maier1dc6be62017-10-05 18:25:37 +0200708 MGCP_MSG_PRESENCE_CONN_MODE)
709#define MGCP_MDCX_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT | \
710 MGCP_MSG_PRESENCE_CONN_ID)
711#define MGCP_DLCX_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT)
712#define MGCP_AUEP_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT)
713#define MGCP_RSIP_MANDATORY 0 /* none */
714
715struct msgb *mgcp_msg_gen(struct mgcp_client *mgcp, struct mgcp_msg *mgcp_msg)
716{
717 mgcp_trans_id_t trans_id = mgcp_client_next_trans_id(mgcp);
718 uint32_t mandatory_mask;
719 struct msgb *msg = msgb_alloc_headroom(4096, 128, "MGCP tx");
720 int rc = 0;
721
722 msg->l2h = msg->data;
723 msg->cb[MSGB_CB_MGCP_TRANS_ID] = trans_id;
724
725 /* Add command verb */
726 switch (mgcp_msg->verb) {
727 case MGCP_VERB_CRCX:
728 mandatory_mask = MGCP_CRCX_MANDATORY;
729 rc += msgb_printf(msg, "CRCX %u", trans_id);
730 break;
731 case MGCP_VERB_MDCX:
732 mandatory_mask = MGCP_MDCX_MANDATORY;
733 rc += msgb_printf(msg, "MDCX %u", trans_id);
734 break;
735 case MGCP_VERB_DLCX:
736 mandatory_mask = MGCP_DLCX_MANDATORY;
737 rc += msgb_printf(msg, "DLCX %u", trans_id);
738 break;
739 case MGCP_VERB_AUEP:
740 mandatory_mask = MGCP_AUEP_MANDATORY;
741 rc += msgb_printf(msg, "AUEP %u", trans_id);
742 break;
743 case MGCP_VERB_RSIP:
744 mandatory_mask = MGCP_RSIP_MANDATORY;
745 rc += msgb_printf(msg, "RSIP %u", trans_id);
746 break;
747 default:
748 LOGP(DLMGCP, LOGL_ERROR,
749 "Invalid command verb, can not generate MGCP message\n");
750 msgb_free(msg);
751 return NULL;
752 }
753
754 /* Check if mandatory fields are missing */
755 if (!((mgcp_msg->presence & mandatory_mask) == mandatory_mask)) {
756 LOGP(DLMGCP, LOGL_ERROR,
757 "One or more missing mandatory fields, can not generate MGCP message\n");
758 msgb_free(msg);
759 return NULL;
760 }
761
762 /* Add endpoint name */
Philipp Maier7bc55522017-12-10 22:52:22 +0100763 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_ENDPOINT) {
764 if (strlen(mgcp_msg->endpoint) <= 0) {
765 LOGP(DLMGCP, LOGL_ERROR,
766 "Empty endpoint name, can not generate MGCP message\n");
767 msgb_free(msg);
768 return NULL;
769 }
Philipp Maier1dc6be62017-10-05 18:25:37 +0200770 rc += msgb_printf(msg, " %s", mgcp_msg->endpoint);
Philipp Maier7bc55522017-12-10 22:52:22 +0100771 }
Philipp Maier1dc6be62017-10-05 18:25:37 +0200772
773 /* Add protocol version */
774 rc += msgb_printf(msg, " MGCP 1.0\r\n");
775
776 /* Add call id */
777 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_CALL_ID)
778 rc += msgb_printf(msg, "C: %x\r\n", mgcp_msg->call_id);
779
780 /* Add connection id */
Philipp Maier7bc55522017-12-10 22:52:22 +0100781 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_CONN_ID) {
782 if (strlen(mgcp_msg->conn_id) <= 0) {
783 LOGP(DLMGCP, LOGL_ERROR,
784 "Empty connection id, can not generate MGCP message\n");
785 msgb_free(msg);
786 return NULL;
787 }
Philipp Maier01d24a32017-11-21 17:26:09 +0100788 rc += msgb_printf(msg, "I: %s\r\n", mgcp_msg->conn_id);
Philipp Maier7bc55522017-12-10 22:52:22 +0100789 }
Philipp Maier1dc6be62017-10-05 18:25:37 +0200790
791 /* Add local connection options */
Philipp Maierffd75e42017-11-22 11:44:50 +0100792 if (mgcp_msg->verb == MGCP_VERB_CRCX)
Philipp Maier1dc6be62017-10-05 18:25:37 +0200793 rc += msgb_printf(msg, "L: p:20, a:AMR, nt:IN\r\n");
794
795 /* Add mode */
796 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_CONN_MODE)
797 rc +=
798 msgb_printf(msg, "M: %s\r\n",
799 mgcp_client_cmode_name(mgcp_msg->conn_mode));
800
801 /* Add RTP address and port (SDP) */
802 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_AUDIO_IP
803 && mgcp_msg->presence & MGCP_MSG_PRESENCE_AUDIO_PORT) {
Philipp Maier7bc55522017-12-10 22:52:22 +0100804 if (mgcp_msg->audio_port == 0) {
805 LOGP(DLMGCP, LOGL_ERROR,
806 "Invalid port number, can not generate MGCP message\n");
807 msgb_free(msg);
808 return NULL;
809 }
810 if (strlen(mgcp_msg->audio_ip) <= 0) {
811 LOGP(DLMGCP, LOGL_ERROR,
812 "Empty ip address, can not generate MGCP message\n");
813 msgb_free(msg);
814 return NULL;
815 }
Philipp Maier1dc6be62017-10-05 18:25:37 +0200816 rc += msgb_printf(msg, "\r\n");
817 rc += msgb_printf(msg, "c=IN IP4 %s\r\n", mgcp_msg->audio_ip);
818 rc +=
819 msgb_printf(msg, "m=audio %u RTP/AVP 255\r\n",
820 mgcp_msg->audio_port);
821 }
822
823 if (rc != 0) {
824 LOGP(DLMGCP, LOGL_ERROR,
825 "message buffer to small, can not generate MGCP message\n");
826 msgb_free(msg);
827 msg = NULL;
828 }
829
830 return msg;
831}
832
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100833/* Retrieve the MGCP transaction ID from a msgb generated by mgcp_msg_gen() */
834mgcp_trans_id_t mgcp_msg_trans_id(struct msgb *msg)
835{
836 return (mgcp_trans_id_t)msg->cb[MSGB_CB_MGCP_TRANS_ID];
837}
838
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200839struct mgcp_client_conf *mgcp_client_conf_actual(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200840{
841 return &mgcp->actual;
842}
Neels Hofmeyrd95ab1e2017-09-22 00:52:54 +0200843
844const struct value_string mgcp_client_connection_mode_strs[] = {
845 { MGCP_CONN_NONE, "none" },
846 { MGCP_CONN_RECV_SEND, "sendrecv" },
847 { MGCP_CONN_SEND_ONLY, "sendonly" },
848 { MGCP_CONN_RECV_ONLY, "recvonly" },
849 { MGCP_CONN_LOOPBACK, "loopback" },
850 { 0, NULL }
851};