blob: 116baf3a3a5f2eafbf455a33fb467179ee1c0fd3 [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
Philipp Maier3b12e1b2018-01-18 15:16:13 +0100222/* A new section is marked by a double line break, check a few more
223 * patterns as there may be variants */
224static char *mgcp_find_section_end(char *string)
225{
226 char *rc;
227
228 rc = strstr(string, "\n\n");
229 if (rc)
230 return rc;
231
232 rc = strstr(string, "\n\r\n\r");
233 if (rc)
234 return rc;
235
236 rc = strstr(string, "\r\n\r\n");
237 if (rc)
238 return rc;
239
240 return NULL;
241}
242
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200243int mgcp_response_parse_params(struct mgcp_response *r)
244{
245 char *line;
246 int rc;
247 OSMO_ASSERT(r->body);
Philipp Maier3b12e1b2018-01-18 15:16:13 +0100248 char *data = mgcp_find_section_end(r->body);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200249
250 if (!data) {
251 LOGP(DLMGCP, LOGL_ERROR,
252 "MGCP response: cannot find start of parameters\n");
253 return -EINVAL;
254 }
255
256 /* Advance to after the \n\n, replace the second \n with \0. That's
257 * where the parameters start. */
258 data ++;
259 *data = '\0';
260 data ++;
261
Neels Hofmeyrd95ab1e2017-09-22 00:52:54 +0200262 for_each_non_empty_line(line, data) {
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200263 if (!mgcp_line_is_valid(line))
264 return -EINVAL;
265
266 switch (line[0]) {
267 case 'm':
Philipp Maier06da85e2017-10-05 18:49:24 +0200268 rc = mgcp_parse_audio_port(r, line);
269 if (rc)
270 return rc;
271 break;
272 case 'c':
273 rc = mgcp_parse_audio_ip(r, line);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200274 if (rc)
275 return rc;
276 break;
277 default:
278 /* skip unhandled parameters */
279 break;
280 }
281 }
282 return 0;
283}
284
Philipp Maierffd75e42017-11-22 11:44:50 +0100285/* Parse a line like "I: 0cedfd5a19542d197af9afe5231f1d61" */
286static int mgcp_parse_conn_id(struct mgcp_response *r, const char *line)
287{
288 if (strlen(line) < 4)
289 goto response_parse_failure;
290
291 if (memcmp("I: ", line, 3) != 0)
292 goto response_parse_failure;
293
294 osmo_strlcpy(r->head.conn_id, line + 3, sizeof(r->head.conn_id));
295 return 0;
296
297response_parse_failure:
298 LOGP(DLMGCP, LOGL_ERROR,
299 "Failed to parse MGCP response (connectionIdentifier)\n");
300 return -EINVAL;
301}
302
303/* Parse MGCP parameters of the response */
304static int parse_head_params(struct mgcp_response *r)
305{
306 char *line;
307 int rc = 0;
308 OSMO_ASSERT(r->body);
309 char *data = r->body;
Philipp Maier3b12e1b2018-01-18 15:16:13 +0100310 char *data_end = mgcp_find_section_end(r->body);
Philipp Maierffd75e42017-11-22 11:44:50 +0100311
312 /* Protect SDP body, for_each_non_empty_line() will
313 * only parse until it hits \0 mark. */
314 if (data_end)
315 *data_end = '\0';
316
317 for_each_non_empty_line(line, data) {
318 switch (line[0]) {
319 case 'I':
320 rc = mgcp_parse_conn_id(r, line);
321 if (rc)
322 goto exit;
323 break;
324 default:
325 /* skip unhandled parameters */
326 break;
327 }
328 }
329exit:
330 /* Restore original state */
331 if (data_end)
332 *data_end = '\n';
333
334 return rc;
335}
336
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200337static struct mgcp_response_pending *mgcp_client_response_pending_get(
338 struct mgcp_client *mgcp,
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100339 mgcp_trans_id_t trans_id)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200340{
341 struct mgcp_response_pending *pending;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200342 llist_for_each_entry(pending, &mgcp->responses_pending, entry) {
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100343 if (pending->trans_id == trans_id) {
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200344 llist_del(&pending->entry);
345 return pending;
346 }
347 }
348 return NULL;
349}
350
351/* Feed an MGCP message into the receive processing.
352 * Parse the head and call any callback registered for the transaction id found
353 * in the MGCP message. This is normally called directly from the internal
354 * mgcp_do_read that reads from the socket connected to the MGCP gateway. This
355 * function is published mainly to be able to feed data from the test suite.
356 */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200357int mgcp_client_rx(struct mgcp_client *mgcp, struct msgb *msg)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200358{
359 struct mgcp_response r = { 0 };
360 struct mgcp_response_pending *pending;
361 int rc;
362
363 rc = mgcp_response_parse_head(&r, msg);
364 if (rc) {
Philipp Maierffd75e42017-11-22 11:44:50 +0100365 LOGP(DLMGCP, LOGL_ERROR, "Cannot parse MGCP response (head)\n");
366 return -1;
367 }
368
369 rc = parse_head_params(&r);
370 if (rc) {
371 LOGP(DLMGCP, LOGL_ERROR, "Cannot parse MGCP response (head parameters)\n");
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200372 return -1;
373 }
374
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100375 pending = mgcp_client_response_pending_get(mgcp, r.head.trans_id);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200376 if (!pending) {
377 LOGP(DLMGCP, LOGL_ERROR,
378 "Cannot find matching MGCP transaction for trans_id %d\n",
379 r.head.trans_id);
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100380 return -ENOENT;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200381 }
382
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200383 mgcp_client_handle_response(mgcp, pending, &r);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200384 return 0;
385}
386
387static int mgcp_do_read(struct osmo_fd *fd)
388{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200389 struct mgcp_client *mgcp = fd->data;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200390 struct msgb *msg;
391 int ret;
392
393 msg = msgb_alloc_headroom(4096, 128, "mgcp_from_gw");
394 if (!msg) {
395 LOGP(DLMGCP, LOGL_ERROR, "Failed to allocate MGCP message.\n");
396 return -1;
397 }
398
399 ret = read(fd->fd, msg->data, 4096 - 128);
400 if (ret <= 0) {
401 LOGP(DLMGCP, LOGL_ERROR, "Failed to read: %d/%s\n", errno, strerror(errno));
402 msgb_free(msg);
403 return -1;
404 } else if (ret > 4096 - 128) {
405 LOGP(DLMGCP, LOGL_ERROR, "Too much data: %d\n", ret);
406 msgb_free(msg);
407 return -1;
Harald Welte9bf7c532017-11-17 14:14:31 +0100408 }
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200409
410 msg->l2h = msgb_put(msg, ret);
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200411 ret = mgcp_client_rx(mgcp, msg);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200412 talloc_free(msg);
413 return ret;
414}
415
416static int mgcp_do_write(struct osmo_fd *fd, struct msgb *msg)
417{
418 int ret;
419 static char strbuf[4096];
420 unsigned int l = msg->len < sizeof(strbuf) ? msg->len : sizeof(strbuf);
421 unsigned int i;
422
Philipp Maierf8bfbe82017-11-23 19:32:31 +0100423 osmo_strlcpy(strbuf, (const char*)msg->data, l);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200424 for (i = 0; i < sizeof(strbuf); i++) {
425 if (strbuf[i] == '\n' || strbuf[i] == '\r') {
426 strbuf[i] = '\0';
427 break;
428 }
429 }
430 DEBUGP(DLMGCP, "Tx MGCP msg to MGCP GW: '%s'\n", strbuf);
431
432 LOGP(DLMGCP, LOGL_DEBUG, "Sending msg to MGCP GW size: %u\n", msg->len);
433
434 ret = write(fd->fd, msg->data, msg->len);
435 if (ret != msg->len)
436 LOGP(DLMGCP, LOGL_ERROR, "Failed to forward message to MGCP"
437 " GW: %s\n", strerror(errno));
438
439 return ret;
440}
441
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200442struct mgcp_client *mgcp_client_init(void *ctx,
443 struct mgcp_client_conf *conf)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200444{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200445 struct mgcp_client *mgcp;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200446
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200447 mgcp = talloc_zero(ctx, struct mgcp_client);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200448
449 INIT_LLIST_HEAD(&mgcp->responses_pending);
450 INIT_LLIST_HEAD(&mgcp->inuse_endpoints);
451
452 mgcp->next_trans_id = 1;
453
454 mgcp->actual.local_addr = conf->local_addr ? conf->local_addr :
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200455 MGCP_CLIENT_LOCAL_ADDR_DEFAULT;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200456 mgcp->actual.local_port = conf->local_port >= 0 ? (uint16_t)conf->local_port :
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200457 MGCP_CLIENT_LOCAL_PORT_DEFAULT;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200458
459 mgcp->actual.remote_addr = conf->remote_addr ? conf->remote_addr :
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200460 MGCP_CLIENT_REMOTE_ADDR_DEFAULT;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200461 mgcp->actual.remote_port = conf->remote_port >= 0 ? (uint16_t)conf->remote_port :
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200462 MGCP_CLIENT_REMOTE_PORT_DEFAULT;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200463
464 mgcp->actual.first_endpoint = conf->first_endpoint > 0 ? (uint16_t)conf->first_endpoint : 0;
465 mgcp->actual.last_endpoint = conf->last_endpoint > 0 ? (uint16_t)conf->last_endpoint : 0;
Neels Hofmeyrc0dcc3c2017-12-02 18:31:34 +0000466 mgcp->actual.bts_base = conf->bts_base > 0 ? (uint16_t)conf->bts_base : 4000;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200467
468 return mgcp;
469}
470
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200471int mgcp_client_connect(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200472{
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200473 struct sockaddr_in addr;
474 struct osmo_wqueue *wq;
475 int rc;
476
477 if (!mgcp) {
478 LOGP(DLMGCP, LOGL_FATAL, "MGCPGW client not initialized properly\n");
479 return -EINVAL;
480 }
481
482 wq = &mgcp->wq;
483
Harald Welte8890dfa2017-11-17 15:09:30 +0100484 rc = osmo_sock_init2_ofd(&wq->bfd, AF_INET, SOCK_DGRAM, IPPROTO_UDP,
485 mgcp->actual.local_addr, mgcp->actual.local_port,
486 mgcp->actual.remote_addr, mgcp->actual.remote_port,
487 OSMO_SOCK_F_BIND | OSMO_SOCK_F_CONNECT);
488 if (rc < 0) {
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200489 LOGP(DLMGCP, LOGL_FATAL,
Harald Welte8890dfa2017-11-17 15:09:30 +0100490 "Failed to initialize socket %s:%u -> %s:%u for MGCP GW: %s\n",
491 mgcp->actual.local_addr, mgcp->actual.local_port,
492 mgcp->actual.remote_addr, mgcp->actual.remote_port, strerror(errno));
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200493 goto error_close_fd;
494 }
495
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200496 inet_aton(mgcp->actual.remote_addr, &addr.sin_addr);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200497 mgcp->remote_addr = htonl(addr.sin_addr.s_addr);
498
499 osmo_wqueue_init(wq, 10);
500 wq->bfd.when = BSC_FD_READ;
501 wq->bfd.data = mgcp;
502 wq->read_cb = mgcp_do_read;
503 wq->write_cb = mgcp_do_write;
504
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200505 LOGP(DLMGCP, LOGL_INFO, "MGCP GW connection: %s:%u -> %s:%u\n",
506 mgcp->actual.local_addr, mgcp->actual.local_port,
507 mgcp->actual.remote_addr, mgcp->actual.remote_port);
508
509 return 0;
510error_close_fd:
511 close(wq->bfd.fd);
512 wq->bfd.fd = -1;
513 return rc;
514}
515
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200516const char *mgcp_client_remote_addr_str(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200517{
518 return mgcp->actual.remote_addr;
519}
520
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200521uint16_t mgcp_client_remote_port(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200522{
523 return mgcp->actual.remote_port;
524}
525
526/* Return the MGCP GW binary IPv4 address in network byte order. */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200527uint32_t mgcp_client_remote_addr_n(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200528{
529 return mgcp->remote_addr;
530}
531
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200532struct mgcp_response_pending * mgcp_client_pending_add(
533 struct mgcp_client *mgcp,
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200534 mgcp_trans_id_t trans_id,
535 mgcp_response_cb_t response_cb,
536 void *priv)
537{
538 struct mgcp_response_pending *pending;
539
540 pending = talloc_zero(mgcp, struct mgcp_response_pending);
541 pending->trans_id = trans_id;
542 pending->response_cb = response_cb;
543 pending->priv = priv;
544 llist_add_tail(&pending->entry, &mgcp->responses_pending);
545
546 return pending;
547}
548
549/* Send the MGCP message in msg to the MGCP GW and handle a response with
550 * response_cb. NOTE: the response_cb still needs to call
551 * mgcp_response_parse_params(response) to get the parsed parameters -- to
552 * potentially save some CPU cycles, only the head line has been parsed when
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100553 * the response_cb is invoked.
554 * Before the priv pointer becomes invalid, e.g. due to transaction timeout,
555 * mgcp_client_cancel() needs to be called for this transaction.
556 */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200557int mgcp_client_tx(struct mgcp_client *mgcp, struct msgb *msg,
558 mgcp_response_cb_t response_cb, void *priv)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200559{
560 struct mgcp_response_pending *pending;
561 mgcp_trans_id_t trans_id;
562 int rc;
563
564 trans_id = msg->cb[MSGB_CB_MGCP_TRANS_ID];
565 if (!trans_id) {
566 LOGP(DLMGCP, LOGL_ERROR,
567 "Unset transaction id in mgcp send request\n");
568 talloc_free(msg);
569 return -EINVAL;
570 }
571
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200572 pending = mgcp_client_pending_add(mgcp, trans_id, response_cb, priv);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200573
574 if (msgb_l2len(msg) > 4096) {
575 LOGP(DLMGCP, LOGL_ERROR,
576 "Cannot send, MGCP message too large: %u\n",
577 msgb_l2len(msg));
578 msgb_free(msg);
579 rc = -EINVAL;
580 goto mgcp_tx_error;
581 }
582
583 rc = osmo_wqueue_enqueue(&mgcp->wq, msg);
584 if (rc) {
585 LOGP(DLMGCP, LOGL_FATAL, "Could not queue message to MGCP GW\n");
586 msgb_free(msg);
587 goto mgcp_tx_error;
588 } else
589 LOGP(DLMGCP, LOGL_INFO, "Queued %u bytes for MGCP GW\n",
590 msgb_l2len(msg));
591 return 0;
592
593mgcp_tx_error:
594 /* Pass NULL to response cb to indicate an error */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200595 mgcp_client_handle_response(mgcp, pending, NULL);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200596 return -1;
597}
598
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100599/* Cancel a pending transaction.
600 * Should a priv pointer passed to mgcp_client_tx() become invalid, this function must be called. In
601 * practical terms, if the caller of mgcp_client_tx() wishes to tear down a transaction without having
602 * received a response this function must be called. The trans_id can be obtained by calling
603 * mgcp_msg_trans_id() on the msgb produced by mgcp_msg_gen().
604 */
605int mgcp_client_cancel(struct mgcp_client *mgcp, mgcp_trans_id_t trans_id)
606{
607 struct mgcp_response_pending *pending = mgcp_client_response_pending_get(mgcp, trans_id);
608 if (!pending) {
609 /* INFO is sufficient, it is not harmful to cancel a transaction twice. */
610 LOGP(DLMGCP, LOGL_INFO, "Cannot cancel, no such transaction: %u\n", trans_id);
611 return -ENOENT;
612 }
613 LOGP(DLMGCP, LOGL_INFO, "Canceled transaction %u\n", trans_id);
614 talloc_free(pending);
615 return 0;
616 /* We don't really need to clean up the wqueue: In all sane cases, the msgb has already been sent
617 * out and is no longer in the wqueue. If it still is in the wqueue, then sending MGCP messages
618 * per se is broken and the program should notice so by a full wqueue. Even if this was called
619 * before we had a chance to send out the message and it is still going to be sent, we will just
620 * ignore the reply to it later. Removing a msgb from the wqueue here would just introduce more
621 * bug surface in terms of failing to update wqueue API's counters or some such.
622 */
623}
624
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200625static struct msgb *mgcp_msg_from_buf(mgcp_trans_id_t trans_id,
626 const char *buf, int len)
627{
628 struct msgb *msg;
629
630 if (len > (4096 - 128)) {
631 LOGP(DLMGCP, LOGL_ERROR, "Cannot send to MGCP GW:"
632 " message too large: %d\n", len);
633 return NULL;
634 }
635
636 msg = msgb_alloc_headroom(4096, 128, "MGCP tx");
637 OSMO_ASSERT(msg);
638
639 char *dst = (char*)msgb_put(msg, len);
640 memcpy(dst, buf, len);
641 msg->l2h = msg->data;
642 msg->cb[MSGB_CB_MGCP_TRANS_ID] = trans_id;
643
644 return msg;
645}
646
647static struct msgb *mgcp_msg_from_str(mgcp_trans_id_t trans_id,
648 const char *fmt, ...)
649{
650 static char compose[4096 - 128];
651 va_list ap;
652 int len;
653 OSMO_ASSERT(fmt);
654
655 va_start(ap, fmt);
656 len = vsnprintf(compose, sizeof(compose), fmt, ap);
657 va_end(ap);
658 if (len >= sizeof(compose)) {
659 LOGP(DLMGCP, LOGL_ERROR,
660 "Message too large: trans_id=%u len=%d\n",
661 trans_id, len);
662 return NULL;
663 }
664 if (len < 1) {
665 LOGP(DLMGCP, LOGL_ERROR,
666 "Failed to compose message: trans_id=%u len=%d\n",
667 trans_id, len);
668 return NULL;
669 }
670 return mgcp_msg_from_buf(trans_id, compose, len);
671}
672
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200673static mgcp_trans_id_t mgcp_client_next_trans_id(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200674{
675 /* avoid zero trans_id to distinguish from unset trans_id */
676 if (!mgcp->next_trans_id)
677 mgcp->next_trans_id ++;
678 return mgcp->next_trans_id ++;
679}
680
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200681struct msgb *mgcp_msg_crcx(struct mgcp_client *mgcp,
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200682 uint16_t rtp_endpoint, unsigned int call_id,
683 enum mgcp_connection_mode mode)
684{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200685 mgcp_trans_id_t trans_id = mgcp_client_next_trans_id(mgcp);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200686 return mgcp_msg_from_str(trans_id,
687 "CRCX %u %x@mgw MGCP 1.0\r\n"
688 "C: %x\r\n"
689 "L: p:20, a:AMR, nt:IN\r\n"
690 "M: %s\r\n"
691 ,
692 trans_id,
693 rtp_endpoint,
694 call_id,
Neels Hofmeyrd95ab1e2017-09-22 00:52:54 +0200695 mgcp_client_cmode_name(mode));
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200696}
697
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200698struct msgb *mgcp_msg_mdcx(struct mgcp_client *mgcp,
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200699 uint16_t rtp_endpoint, const char *rtp_conn_addr,
700 uint16_t rtp_port, enum mgcp_connection_mode mode)
701
702{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200703 mgcp_trans_id_t trans_id = mgcp_client_next_trans_id(mgcp);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200704 return mgcp_msg_from_str(trans_id,
705 "MDCX %u %x@mgw MGCP 1.0\r\n"
706 "M: %s\r\n"
707 "\r\n"
708 "c=IN IP4 %s\r\n"
709 "m=audio %u RTP/AVP 255\r\n"
710 ,
711 trans_id,
712 rtp_endpoint,
Neels Hofmeyrd95ab1e2017-09-22 00:52:54 +0200713 mgcp_client_cmode_name(mode),
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200714 rtp_conn_addr,
715 rtp_port);
716}
717
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200718struct msgb *mgcp_msg_dlcx(struct mgcp_client *mgcp, uint16_t rtp_endpoint,
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200719 unsigned int call_id)
720{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200721 mgcp_trans_id_t trans_id = mgcp_client_next_trans_id(mgcp);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200722 return mgcp_msg_from_str(trans_id,
723 "DLCX %u %x@mgw MGCP 1.0\r\n"
724 "C: %x\r\n", trans_id, rtp_endpoint, call_id);
725}
726
Philipp Maier1dc6be62017-10-05 18:25:37 +0200727#define MGCP_CRCX_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT | \
728 MGCP_MSG_PRESENCE_CALL_ID | \
Philipp Maier1dc6be62017-10-05 18:25:37 +0200729 MGCP_MSG_PRESENCE_CONN_MODE)
730#define MGCP_MDCX_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT | \
Philipp Maier490cbaa2018-01-22 17:32:38 +0100731 MGCP_MSG_PRESENCE_CALL_ID | \
Philipp Maier1dc6be62017-10-05 18:25:37 +0200732 MGCP_MSG_PRESENCE_CONN_ID)
733#define MGCP_DLCX_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT)
734#define MGCP_AUEP_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT)
735#define MGCP_RSIP_MANDATORY 0 /* none */
736
737struct msgb *mgcp_msg_gen(struct mgcp_client *mgcp, struct mgcp_msg *mgcp_msg)
738{
739 mgcp_trans_id_t trans_id = mgcp_client_next_trans_id(mgcp);
740 uint32_t mandatory_mask;
741 struct msgb *msg = msgb_alloc_headroom(4096, 128, "MGCP tx");
742 int rc = 0;
Philipp Maier9d25d7a2018-01-22 17:31:10 +0100743 char local_ip[INET_ADDRSTRLEN];
Philipp Maier1dc6be62017-10-05 18:25:37 +0200744
745 msg->l2h = msg->data;
746 msg->cb[MSGB_CB_MGCP_TRANS_ID] = trans_id;
747
748 /* Add command verb */
749 switch (mgcp_msg->verb) {
750 case MGCP_VERB_CRCX:
751 mandatory_mask = MGCP_CRCX_MANDATORY;
752 rc += msgb_printf(msg, "CRCX %u", trans_id);
753 break;
754 case MGCP_VERB_MDCX:
755 mandatory_mask = MGCP_MDCX_MANDATORY;
756 rc += msgb_printf(msg, "MDCX %u", trans_id);
757 break;
758 case MGCP_VERB_DLCX:
759 mandatory_mask = MGCP_DLCX_MANDATORY;
760 rc += msgb_printf(msg, "DLCX %u", trans_id);
761 break;
762 case MGCP_VERB_AUEP:
763 mandatory_mask = MGCP_AUEP_MANDATORY;
764 rc += msgb_printf(msg, "AUEP %u", trans_id);
765 break;
766 case MGCP_VERB_RSIP:
767 mandatory_mask = MGCP_RSIP_MANDATORY;
768 rc += msgb_printf(msg, "RSIP %u", trans_id);
769 break;
770 default:
771 LOGP(DLMGCP, LOGL_ERROR,
772 "Invalid command verb, can not generate MGCP message\n");
773 msgb_free(msg);
774 return NULL;
775 }
776
777 /* Check if mandatory fields are missing */
778 if (!((mgcp_msg->presence & mandatory_mask) == mandatory_mask)) {
779 LOGP(DLMGCP, LOGL_ERROR,
780 "One or more missing mandatory fields, can not generate MGCP message\n");
781 msgb_free(msg);
782 return NULL;
783 }
784
785 /* Add endpoint name */
Philipp Maier7bc55522017-12-10 22:52:22 +0100786 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_ENDPOINT) {
787 if (strlen(mgcp_msg->endpoint) <= 0) {
788 LOGP(DLMGCP, LOGL_ERROR,
789 "Empty endpoint name, can not generate MGCP message\n");
790 msgb_free(msg);
791 return NULL;
792 }
Philipp Maier1dc6be62017-10-05 18:25:37 +0200793 rc += msgb_printf(msg, " %s", mgcp_msg->endpoint);
Philipp Maier7bc55522017-12-10 22:52:22 +0100794 }
Philipp Maier1dc6be62017-10-05 18:25:37 +0200795
796 /* Add protocol version */
797 rc += msgb_printf(msg, " MGCP 1.0\r\n");
798
799 /* Add call id */
800 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_CALL_ID)
801 rc += msgb_printf(msg, "C: %x\r\n", mgcp_msg->call_id);
802
803 /* Add connection id */
Philipp Maier7bc55522017-12-10 22:52:22 +0100804 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_CONN_ID) {
805 if (strlen(mgcp_msg->conn_id) <= 0) {
806 LOGP(DLMGCP, LOGL_ERROR,
807 "Empty connection id, can not generate MGCP message\n");
808 msgb_free(msg);
809 return NULL;
810 }
Philipp Maier01d24a32017-11-21 17:26:09 +0100811 rc += msgb_printf(msg, "I: %s\r\n", mgcp_msg->conn_id);
Philipp Maier7bc55522017-12-10 22:52:22 +0100812 }
Philipp Maier1dc6be62017-10-05 18:25:37 +0200813
814 /* Add local connection options */
Philipp Maierffd75e42017-11-22 11:44:50 +0100815 if (mgcp_msg->verb == MGCP_VERB_CRCX)
Philipp Maier1dc6be62017-10-05 18:25:37 +0200816 rc += msgb_printf(msg, "L: p:20, a:AMR, nt:IN\r\n");
817
818 /* Add mode */
819 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_CONN_MODE)
820 rc +=
821 msgb_printf(msg, "M: %s\r\n",
822 mgcp_client_cmode_name(mgcp_msg->conn_mode));
823
Philipp Maier9d25d7a2018-01-22 17:31:10 +0100824 /* Add SDP body */
Philipp Maier1dc6be62017-10-05 18:25:37 +0200825 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_AUDIO_IP
826 && mgcp_msg->presence & MGCP_MSG_PRESENCE_AUDIO_PORT) {
Philipp Maier9d25d7a2018-01-22 17:31:10 +0100827
828 /* Add separator to mark the beginning of the SDP block */
829 rc += msgb_printf(msg, "\r\n");
830
831 /* Add SDP protocol version */
832 rc += msgb_printf(msg, "v=0\r\n");
833
834 /* Add session name (none) */
835 rc += msgb_printf(msg, "s=-\r\n");
836
837 /* Determine local IP-Address */
838 if (osmo_sock_local_ip(local_ip, mgcp->actual.remote_addr) < 0) {
839 LOGP(DLMGCP, LOGL_ERROR,
840 "Could not determine local IP-Address!\n");
841 msgb_free(msg);
842 return NULL;
843 }
844
845 /* Add owner/creator (SDP) */
846 rc += msgb_printf(msg, "o=- %x 23 IN IP4 %s\r\n",
847 mgcp_msg->call_id, local_ip);
848
849 /* Add RTP address and port */
Philipp Maier7bc55522017-12-10 22:52:22 +0100850 if (mgcp_msg->audio_port == 0) {
851 LOGP(DLMGCP, LOGL_ERROR,
852 "Invalid port number, can not generate MGCP message\n");
853 msgb_free(msg);
854 return NULL;
855 }
856 if (strlen(mgcp_msg->audio_ip) <= 0) {
857 LOGP(DLMGCP, LOGL_ERROR,
858 "Empty ip address, can not generate MGCP message\n");
859 msgb_free(msg);
860 return NULL;
861 }
Philipp Maier1dc6be62017-10-05 18:25:37 +0200862 rc += msgb_printf(msg, "c=IN IP4 %s\r\n", mgcp_msg->audio_ip);
863 rc +=
864 msgb_printf(msg, "m=audio %u RTP/AVP 255\r\n",
865 mgcp_msg->audio_port);
Philipp Maier9d25d7a2018-01-22 17:31:10 +0100866
867 /* Add time description, active time (SDP) */
868 rc += msgb_printf(msg, "t=0 0\r\n");
Philipp Maier1dc6be62017-10-05 18:25:37 +0200869 }
870
871 if (rc != 0) {
872 LOGP(DLMGCP, LOGL_ERROR,
873 "message buffer to small, can not generate MGCP message\n");
874 msgb_free(msg);
875 msg = NULL;
876 }
877
878 return msg;
879}
880
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100881/* Retrieve the MGCP transaction ID from a msgb generated by mgcp_msg_gen() */
882mgcp_trans_id_t mgcp_msg_trans_id(struct msgb *msg)
883{
884 return (mgcp_trans_id_t)msg->cb[MSGB_CB_MGCP_TRANS_ID];
885}
886
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200887struct mgcp_client_conf *mgcp_client_conf_actual(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200888{
889 return &mgcp->actual;
890}
Neels Hofmeyrd95ab1e2017-09-22 00:52:54 +0200891
892const struct value_string mgcp_client_connection_mode_strs[] = {
893 { MGCP_CONN_NONE, "none" },
894 { MGCP_CONN_RECV_SEND, "sendrecv" },
895 { MGCP_CONN_SEND_ONLY, "sendonly" },
896 { MGCP_CONN_RECV_ONLY, "recvonly" },
897 { MGCP_CONN_LOOPBACK, "loopback" },
898 { 0, NULL }
899};