blob: 5cd14f76d89a3724a305bedac510925ab3290edd [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 Hofmeyre9920f22017-07-10 15:07:22 +020048 };
49}
50
51/* Test if a given endpoint id is currently in use */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +020052static bool endpoint_in_use(uint16_t id, struct mgcp_client *client)
Neels Hofmeyre9920f22017-07-10 15:07:22 +020053{
54 struct mgcp_inuse_endpoint *endpoint;
55 llist_for_each_entry(endpoint, &client->inuse_endpoints, entry) {
56 if (endpoint->id == id)
57 return true;
58 }
59
60 return false;
61}
62
63/* Find and seize an unsused endpoint id */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +020064int mgcp_client_next_endpoint(struct mgcp_client *client)
Neels Hofmeyre9920f22017-07-10 15:07:22 +020065{
66 int i;
67 uint16_t first_endpoint = client->actual.first_endpoint;
68 uint16_t last_endpoint = client->actual.last_endpoint;
69 struct mgcp_inuse_endpoint *endpoint;
70
71 /* Use the maximum permitted range if the VTY
72 * configuration does not specify a range */
73 if (client->actual.last_endpoint == 0) {
74 first_endpoint = 1;
75 last_endpoint = 65534;
76 }
77
78 /* Test the permitted endpoint range for an endpoint
79 * number that is not in use. When a suitable endpoint
80 * number can be found, seize it by adding it to the
81 * inuse list. */
82 for (i=first_endpoint;i<last_endpoint;i++)
83 {
84 if (endpoint_in_use(i,client) == false) {
85 endpoint = talloc_zero(client, struct mgcp_inuse_endpoint);
86 endpoint->id = i;
87 llist_add_tail(&endpoint->entry, &client->inuse_endpoints);
88 return endpoint->id;
89 }
90 }
91
92 /* All endpoints are busy! */
93 return -EINVAL;
94}
95
96/* Release a seized endpoint id to make it available again for other calls */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +020097void mgcp_client_release_endpoint(uint16_t id, struct mgcp_client *client)
Neels Hofmeyre9920f22017-07-10 15:07:22 +020098{
99 struct mgcp_inuse_endpoint *endpoint;
100 struct mgcp_inuse_endpoint *endpoint_tmp;
101 llist_for_each_entry_safe(endpoint, endpoint_tmp, &client->inuse_endpoints, entry) {
102 if (endpoint->id == id) {
103 llist_del(&endpoint->entry);
104 talloc_free(endpoint);
105 }
106 }
107}
108
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200109static void mgcp_client_handle_response(struct mgcp_client *mgcp,
110 struct mgcp_response_pending *pending,
111 struct mgcp_response *response)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200112{
113 if (!pending) {
114 LOGP(DLMGCP, LOGL_ERROR,
115 "Cannot handle NULL response\n");
116 return;
117 }
118 if (pending->response_cb)
119 pending->response_cb(response, pending->priv);
120 else
121 LOGP(DLMGCP, LOGL_INFO, "MGCP response ignored (NULL cb)\n");
122 talloc_free(pending);
123}
124
125static int mgcp_response_parse_head(struct mgcp_response *r, struct msgb *msg)
126{
127 int comment_pos;
128 char *end;
129
130 if (mgcp_msg_terminate_nul(msg))
131 goto response_parse_failure;
132
133 r->body = (char *)msg->data;
134
Harald Welte9bf7c532017-11-17 14:14:31 +0100135 if (sscanf(r->body, "%3d %u %n",
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200136 &r->head.response_code, &r->head.trans_id,
137 &comment_pos) != 2)
138 goto response_parse_failure;
139
140 r->head.comment = r->body + comment_pos;
141 end = strchr(r->head.comment, '\r');
142 if (!end)
143 goto response_parse_failure;
144 /* Mark the end of the comment */
145 *end = '\0';
146 r->body = end + 1;
147 if (r->body[0] == '\n')
148 r->body ++;
149 return 0;
150
151response_parse_failure:
152 LOGP(DLMGCP, LOGL_ERROR,
153 "Failed to parse MGCP response header\n");
154 return -EINVAL;
155}
156
157/* TODO undup against mgcp_protocol.c:mgcp_check_param() */
158static bool mgcp_line_is_valid(const char *line)
159{
160 const size_t line_len = strlen(line);
161 if (line[0] == '\0')
162 return true;
163
164 if (line_len < 2
165 || line[1] != '=') {
166 LOGP(DLMGCP, LOGL_ERROR,
167 "Wrong MGCP option format: '%s'\n",
168 line);
169 return false;
170 }
171
172 return true;
173}
174
175/* Parse a line like "m=audio 16002 RTP/AVP 98" */
Philipp Maier06da85e2017-10-05 18:49:24 +0200176static int mgcp_parse_audio_port(struct mgcp_response *r, const char *line)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200177{
Harald Welte9bf7c532017-11-17 14:14:31 +0100178 if (sscanf(line, "m=audio %hu",
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200179 &r->audio_port) != 1)
180 goto response_parse_failure;
181
182 return 0;
183
184response_parse_failure:
185 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier06da85e2017-10-05 18:49:24 +0200186 "Failed to parse MGCP response header (audio port)\n");
187 return -EINVAL;
188}
189
190/* Parse a line like "c=IN IP4 10.11.12.13" */
191static int mgcp_parse_audio_ip(struct mgcp_response *r, const char *line)
192{
193 struct in_addr ip_test;
194
195 if (strlen(line) < 16)
196 goto response_parse_failure;
197
198 /* The current implementation strictly supports IPV4 only ! */
199 if (memcmp("c=IN IP4 ", line, 9) != 0)
200 goto response_parse_failure;
201
202 /* Extract IP-Address */
Philipp Maierf8bfbe82017-11-23 19:32:31 +0100203 osmo_strlcpy(r->audio_ip, line + 9, sizeof(r->audio_ip));
Philipp Maier06da85e2017-10-05 18:49:24 +0200204
205 /* Check IP-Address */
206 if (inet_aton(r->audio_ip, &ip_test) == 0)
207 goto response_parse_failure;
208
209 return 0;
210
211response_parse_failure:
212 LOGP(DLMGCP, LOGL_ERROR,
213 "Failed to parse MGCP response header (audio ip)\n");
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200214 return -EINVAL;
215}
216
217int mgcp_response_parse_params(struct mgcp_response *r)
218{
219 char *line;
220 int rc;
221 OSMO_ASSERT(r->body);
222 char *data = strstr(r->body, "\n\n");
223
224 if (!data) {
225 LOGP(DLMGCP, LOGL_ERROR,
226 "MGCP response: cannot find start of parameters\n");
227 return -EINVAL;
228 }
229
230 /* Advance to after the \n\n, replace the second \n with \0. That's
231 * where the parameters start. */
232 data ++;
233 *data = '\0';
234 data ++;
235
Neels Hofmeyrd95ab1e2017-09-22 00:52:54 +0200236 for_each_non_empty_line(line, data) {
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200237 if (!mgcp_line_is_valid(line))
238 return -EINVAL;
239
240 switch (line[0]) {
241 case 'm':
Philipp Maier06da85e2017-10-05 18:49:24 +0200242 rc = mgcp_parse_audio_port(r, line);
243 if (rc)
244 return rc;
245 break;
246 case 'c':
247 rc = mgcp_parse_audio_ip(r, line);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200248 if (rc)
249 return rc;
250 break;
251 default:
252 /* skip unhandled parameters */
253 break;
254 }
255 }
256 return 0;
257}
258
Philipp Maierffd75e42017-11-22 11:44:50 +0100259/* Parse a line like "I: 0cedfd5a19542d197af9afe5231f1d61" */
260static int mgcp_parse_conn_id(struct mgcp_response *r, const char *line)
261{
262 if (strlen(line) < 4)
263 goto response_parse_failure;
264
265 if (memcmp("I: ", line, 3) != 0)
266 goto response_parse_failure;
267
268 osmo_strlcpy(r->head.conn_id, line + 3, sizeof(r->head.conn_id));
269 return 0;
270
271response_parse_failure:
272 LOGP(DLMGCP, LOGL_ERROR,
273 "Failed to parse MGCP response (connectionIdentifier)\n");
274 return -EINVAL;
275}
276
277/* Parse MGCP parameters of the response */
278static int parse_head_params(struct mgcp_response *r)
279{
280 char *line;
281 int rc = 0;
282 OSMO_ASSERT(r->body);
283 char *data = r->body;
284 char *data_end = strstr(r->body, "\n\n");
285
286 /* Protect SDP body, for_each_non_empty_line() will
287 * only parse until it hits \0 mark. */
288 if (data_end)
289 *data_end = '\0';
290
291 for_each_non_empty_line(line, data) {
292 switch (line[0]) {
293 case 'I':
294 rc = mgcp_parse_conn_id(r, line);
295 if (rc)
296 goto exit;
297 break;
298 default:
299 /* skip unhandled parameters */
300 break;
301 }
302 }
303exit:
304 /* Restore original state */
305 if (data_end)
306 *data_end = '\n';
307
308 return rc;
309}
310
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200311static struct mgcp_response_pending *mgcp_client_response_pending_get(
312 struct mgcp_client *mgcp,
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200313 struct mgcp_response *r)
314{
315 struct mgcp_response_pending *pending;
316 if (!r)
317 return NULL;
318 llist_for_each_entry(pending, &mgcp->responses_pending, entry) {
319 if (pending->trans_id == r->head.trans_id) {
320 llist_del(&pending->entry);
321 return pending;
322 }
323 }
324 return NULL;
325}
326
327/* Feed an MGCP message into the receive processing.
328 * Parse the head and call any callback registered for the transaction id found
329 * in the MGCP message. This is normally called directly from the internal
330 * mgcp_do_read that reads from the socket connected to the MGCP gateway. This
331 * function is published mainly to be able to feed data from the test suite.
332 */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200333int mgcp_client_rx(struct mgcp_client *mgcp, struct msgb *msg)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200334{
335 struct mgcp_response r = { 0 };
336 struct mgcp_response_pending *pending;
337 int rc;
338
339 rc = mgcp_response_parse_head(&r, msg);
340 if (rc) {
Philipp Maierffd75e42017-11-22 11:44:50 +0100341 LOGP(DLMGCP, LOGL_ERROR, "Cannot parse MGCP response (head)\n");
342 return -1;
343 }
344
345 rc = parse_head_params(&r);
346 if (rc) {
347 LOGP(DLMGCP, LOGL_ERROR, "Cannot parse MGCP response (head parameters)\n");
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200348 return -1;
349 }
350
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200351 pending = mgcp_client_response_pending_get(mgcp, &r);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200352 if (!pending) {
353 LOGP(DLMGCP, LOGL_ERROR,
354 "Cannot find matching MGCP transaction for trans_id %d\n",
355 r.head.trans_id);
356 return -1;
357 }
358
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200359 mgcp_client_handle_response(mgcp, pending, &r);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200360 return 0;
361}
362
363static int mgcp_do_read(struct osmo_fd *fd)
364{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200365 struct mgcp_client *mgcp = fd->data;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200366 struct msgb *msg;
367 int ret;
368
369 msg = msgb_alloc_headroom(4096, 128, "mgcp_from_gw");
370 if (!msg) {
371 LOGP(DLMGCP, LOGL_ERROR, "Failed to allocate MGCP message.\n");
372 return -1;
373 }
374
375 ret = read(fd->fd, msg->data, 4096 - 128);
376 if (ret <= 0) {
377 LOGP(DLMGCP, LOGL_ERROR, "Failed to read: %d/%s\n", errno, strerror(errno));
378 msgb_free(msg);
379 return -1;
380 } else if (ret > 4096 - 128) {
381 LOGP(DLMGCP, LOGL_ERROR, "Too much data: %d\n", ret);
382 msgb_free(msg);
383 return -1;
Harald Welte9bf7c532017-11-17 14:14:31 +0100384 }
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200385
386 msg->l2h = msgb_put(msg, ret);
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200387 ret = mgcp_client_rx(mgcp, msg);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200388 talloc_free(msg);
389 return ret;
390}
391
392static int mgcp_do_write(struct osmo_fd *fd, struct msgb *msg)
393{
394 int ret;
395 static char strbuf[4096];
396 unsigned int l = msg->len < sizeof(strbuf) ? msg->len : sizeof(strbuf);
397 unsigned int i;
398
Philipp Maierf8bfbe82017-11-23 19:32:31 +0100399 osmo_strlcpy(strbuf, (const char*)msg->data, l);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200400 for (i = 0; i < sizeof(strbuf); i++) {
401 if (strbuf[i] == '\n' || strbuf[i] == '\r') {
402 strbuf[i] = '\0';
403 break;
404 }
405 }
406 DEBUGP(DLMGCP, "Tx MGCP msg to MGCP GW: '%s'\n", strbuf);
407
408 LOGP(DLMGCP, LOGL_DEBUG, "Sending msg to MGCP GW size: %u\n", msg->len);
409
410 ret = write(fd->fd, msg->data, msg->len);
411 if (ret != msg->len)
412 LOGP(DLMGCP, LOGL_ERROR, "Failed to forward message to MGCP"
413 " GW: %s\n", strerror(errno));
414
415 return ret;
416}
417
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200418struct mgcp_client *mgcp_client_init(void *ctx,
419 struct mgcp_client_conf *conf)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200420{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200421 struct mgcp_client *mgcp;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200422
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200423 mgcp = talloc_zero(ctx, struct mgcp_client);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200424
425 INIT_LLIST_HEAD(&mgcp->responses_pending);
426 INIT_LLIST_HEAD(&mgcp->inuse_endpoints);
427
428 mgcp->next_trans_id = 1;
429
430 mgcp->actual.local_addr = conf->local_addr ? conf->local_addr :
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200431 MGCP_CLIENT_LOCAL_ADDR_DEFAULT;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200432 mgcp->actual.local_port = conf->local_port >= 0 ? (uint16_t)conf->local_port :
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200433 MGCP_CLIENT_LOCAL_PORT_DEFAULT;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200434
435 mgcp->actual.remote_addr = conf->remote_addr ? conf->remote_addr :
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200436 MGCP_CLIENT_REMOTE_ADDR_DEFAULT;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200437 mgcp->actual.remote_port = conf->remote_port >= 0 ? (uint16_t)conf->remote_port :
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200438 MGCP_CLIENT_REMOTE_PORT_DEFAULT;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200439
440 mgcp->actual.first_endpoint = conf->first_endpoint > 0 ? (uint16_t)conf->first_endpoint : 0;
441 mgcp->actual.last_endpoint = conf->last_endpoint > 0 ? (uint16_t)conf->last_endpoint : 0;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200442
443 return mgcp;
444}
445
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200446int mgcp_client_connect(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200447{
448 int on;
449 struct sockaddr_in addr;
450 struct osmo_wqueue *wq;
451 int rc;
452
453 if (!mgcp) {
454 LOGP(DLMGCP, LOGL_FATAL, "MGCPGW client not initialized properly\n");
455 return -EINVAL;
456 }
457
458 wq = &mgcp->wq;
459
460 wq->bfd.fd = socket(AF_INET, SOCK_DGRAM, 0);
461 if (wq->bfd.fd < 0) {
462 LOGP(DLMGCP, LOGL_FATAL, "Failed to create UDP socket errno: %d\n", errno);
463 return -errno;
464 }
465
466 on = 1;
467 if (setsockopt(wq->bfd.fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) {
468 LOGP(DLMGCP, LOGL_FATAL,
469 "Failed to initialize socket for MGCP GW: %s\n",
470 strerror(errno));
471 rc = -errno;
472 goto error_close_fd;
473 }
474
475 /* bind socket */
476 memset(&addr, 0, sizeof(addr));
477 addr.sin_family = AF_INET;
478 inet_aton(mgcp->actual.local_addr, &addr.sin_addr);
479 addr.sin_port = htons(mgcp->actual.local_port);
480 if (bind(wq->bfd.fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
481 LOGP(DLMGCP, LOGL_FATAL,
482 "Failed to bind for MGCP GW to %s %u\n",
483 mgcp->actual.local_addr, mgcp->actual.local_port);
484 rc = -errno;
485 goto error_close_fd;
486 }
487
488 /* connect to the remote */
489 inet_aton(mgcp->actual.remote_addr, &addr.sin_addr);
490 addr.sin_port = htons(mgcp->actual.remote_port);
491 if (connect(wq->bfd.fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
492 LOGP(DLMGCP, LOGL_FATAL,
493 "Failed to connect to MGCP GW at %s %u: %s\n",
494 mgcp->actual.remote_addr, mgcp->actual.remote_port,
495 strerror(errno));
496 rc = -errno;
497 goto error_close_fd;
498 }
499
500 mgcp->remote_addr = htonl(addr.sin_addr.s_addr);
501
502 osmo_wqueue_init(wq, 10);
503 wq->bfd.when = BSC_FD_READ;
504 wq->bfd.data = mgcp;
505 wq->read_cb = mgcp_do_read;
506 wq->write_cb = mgcp_do_write;
507
508 if (osmo_fd_register(&wq->bfd) != 0) {
509 LOGP(DLMGCP, LOGL_FATAL, "Failed to register BFD\n");
510 rc = -EIO;
511 goto error_close_fd;
512 }
513 LOGP(DLMGCP, LOGL_INFO, "MGCP GW connection: %s:%u -> %s:%u\n",
514 mgcp->actual.local_addr, mgcp->actual.local_port,
515 mgcp->actual.remote_addr, mgcp->actual.remote_port);
516
517 return 0;
518error_close_fd:
519 close(wq->bfd.fd);
520 wq->bfd.fd = -1;
521 return rc;
522}
523
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200524const char *mgcp_client_remote_addr_str(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200525{
526 return mgcp->actual.remote_addr;
527}
528
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200529uint16_t mgcp_client_remote_port(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200530{
531 return mgcp->actual.remote_port;
532}
533
534/* Return the MGCP GW binary IPv4 address in network byte order. */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200535uint32_t mgcp_client_remote_addr_n(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200536{
537 return mgcp->remote_addr;
538}
539
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200540struct mgcp_response_pending * mgcp_client_pending_add(
541 struct mgcp_client *mgcp,
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200542 mgcp_trans_id_t trans_id,
543 mgcp_response_cb_t response_cb,
544 void *priv)
545{
546 struct mgcp_response_pending *pending;
547
548 pending = talloc_zero(mgcp, struct mgcp_response_pending);
549 pending->trans_id = trans_id;
550 pending->response_cb = response_cb;
551 pending->priv = priv;
552 llist_add_tail(&pending->entry, &mgcp->responses_pending);
553
554 return pending;
555}
556
557/* Send the MGCP message in msg to the MGCP GW and handle a response with
558 * response_cb. NOTE: the response_cb still needs to call
559 * mgcp_response_parse_params(response) to get the parsed parameters -- to
560 * potentially save some CPU cycles, only the head line has been parsed when
561 * the response_cb is invoked. */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200562int mgcp_client_tx(struct mgcp_client *mgcp, struct msgb *msg,
563 mgcp_response_cb_t response_cb, void *priv)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200564{
565 struct mgcp_response_pending *pending;
566 mgcp_trans_id_t trans_id;
567 int rc;
568
569 trans_id = msg->cb[MSGB_CB_MGCP_TRANS_ID];
570 if (!trans_id) {
571 LOGP(DLMGCP, LOGL_ERROR,
572 "Unset transaction id in mgcp send request\n");
573 talloc_free(msg);
574 return -EINVAL;
575 }
576
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200577 pending = mgcp_client_pending_add(mgcp, trans_id, response_cb, priv);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200578
579 if (msgb_l2len(msg) > 4096) {
580 LOGP(DLMGCP, LOGL_ERROR,
581 "Cannot send, MGCP message too large: %u\n",
582 msgb_l2len(msg));
583 msgb_free(msg);
584 rc = -EINVAL;
585 goto mgcp_tx_error;
586 }
587
588 rc = osmo_wqueue_enqueue(&mgcp->wq, msg);
589 if (rc) {
590 LOGP(DLMGCP, LOGL_FATAL, "Could not queue message to MGCP GW\n");
591 msgb_free(msg);
592 goto mgcp_tx_error;
593 } else
594 LOGP(DLMGCP, LOGL_INFO, "Queued %u bytes for MGCP GW\n",
595 msgb_l2len(msg));
596 return 0;
597
598mgcp_tx_error:
599 /* Pass NULL to response cb to indicate an error */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200600 mgcp_client_handle_response(mgcp, pending, NULL);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200601 return -1;
602}
603
604static 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 */
763 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_ENDPOINT)
764 rc += msgb_printf(msg, " %s", mgcp_msg->endpoint);
765
766 /* Add protocol version */
767 rc += msgb_printf(msg, " MGCP 1.0\r\n");
768
769 /* Add call id */
770 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_CALL_ID)
771 rc += msgb_printf(msg, "C: %x\r\n", mgcp_msg->call_id);
772
773 /* Add connection id */
774 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_CONN_ID)
Philipp Maier01d24a32017-11-21 17:26:09 +0100775 rc += msgb_printf(msg, "I: %s\r\n", mgcp_msg->conn_id);
Philipp Maier1dc6be62017-10-05 18:25:37 +0200776
777 /* Add local connection options */
Philipp Maierffd75e42017-11-22 11:44:50 +0100778 if (mgcp_msg->verb == MGCP_VERB_CRCX)
Philipp Maier1dc6be62017-10-05 18:25:37 +0200779 rc += msgb_printf(msg, "L: p:20, a:AMR, nt:IN\r\n");
780
781 /* Add mode */
782 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_CONN_MODE)
783 rc +=
784 msgb_printf(msg, "M: %s\r\n",
785 mgcp_client_cmode_name(mgcp_msg->conn_mode));
786
787 /* Add RTP address and port (SDP) */
788 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_AUDIO_IP
789 && mgcp_msg->presence & MGCP_MSG_PRESENCE_AUDIO_PORT) {
790 rc += msgb_printf(msg, "\r\n");
791 rc += msgb_printf(msg, "c=IN IP4 %s\r\n", mgcp_msg->audio_ip);
792 rc +=
793 msgb_printf(msg, "m=audio %u RTP/AVP 255\r\n",
794 mgcp_msg->audio_port);
795 }
796
797 if (rc != 0) {
798 LOGP(DLMGCP, LOGL_ERROR,
799 "message buffer to small, can not generate MGCP message\n");
800 msgb_free(msg);
801 msg = NULL;
802 }
803
804 return msg;
805}
806
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200807struct mgcp_client_conf *mgcp_client_conf_actual(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200808{
809 return &mgcp->actual;
810}
Neels Hofmeyrd95ab1e2017-09-22 00:52:54 +0200811
812const struct value_string mgcp_client_connection_mode_strs[] = {
813 { MGCP_CONN_NONE, "none" },
814 { MGCP_CONN_RECV_SEND, "sendrecv" },
815 { MGCP_CONN_SEND_ONLY, "sendonly" },
816 { MGCP_CONN_RECV_ONLY, "recvonly" },
817 { MGCP_CONN_LOOPBACK, "loopback" },
818 { 0, NULL }
819};