blob: 7e83f950b5660fd94ba4eab778d34ba97723a4a4 [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 */
203 strncpy(r->audio_ip, line + 9, sizeof(r->audio_ip));
204 r->audio_ip[sizeof(r->audio_ip) - 1] = '\0';
205
206 /* Check IP-Address */
207 if (inet_aton(r->audio_ip, &ip_test) == 0)
208 goto response_parse_failure;
209
210 return 0;
211
212response_parse_failure:
213 LOGP(DLMGCP, LOGL_ERROR,
214 "Failed to parse MGCP response header (audio ip)\n");
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200215 return -EINVAL;
216}
217
218int mgcp_response_parse_params(struct mgcp_response *r)
219{
220 char *line;
221 int rc;
222 OSMO_ASSERT(r->body);
223 char *data = strstr(r->body, "\n\n");
224
225 if (!data) {
226 LOGP(DLMGCP, LOGL_ERROR,
227 "MGCP response: cannot find start of parameters\n");
228 return -EINVAL;
229 }
230
231 /* Advance to after the \n\n, replace the second \n with \0. That's
232 * where the parameters start. */
233 data ++;
234 *data = '\0';
235 data ++;
236
Neels Hofmeyrd95ab1e2017-09-22 00:52:54 +0200237 for_each_non_empty_line(line, data) {
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200238 if (!mgcp_line_is_valid(line))
239 return -EINVAL;
240
241 switch (line[0]) {
242 case 'm':
Philipp Maier06da85e2017-10-05 18:49:24 +0200243 rc = mgcp_parse_audio_port(r, line);
244 if (rc)
245 return rc;
246 break;
247 case 'c':
248 rc = mgcp_parse_audio_ip(r, line);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200249 if (rc)
250 return rc;
251 break;
252 default:
253 /* skip unhandled parameters */
254 break;
255 }
256 }
257 return 0;
258}
259
Philipp Maierffd75e42017-11-22 11:44:50 +0100260/* Parse a line like "I: 0cedfd5a19542d197af9afe5231f1d61" */
261static int mgcp_parse_conn_id(struct mgcp_response *r, const char *line)
262{
263 if (strlen(line) < 4)
264 goto response_parse_failure;
265
266 if (memcmp("I: ", line, 3) != 0)
267 goto response_parse_failure;
268
269 osmo_strlcpy(r->head.conn_id, line + 3, sizeof(r->head.conn_id));
270 return 0;
271
272response_parse_failure:
273 LOGP(DLMGCP, LOGL_ERROR,
274 "Failed to parse MGCP response (connectionIdentifier)\n");
275 return -EINVAL;
276}
277
278/* Parse MGCP parameters of the response */
279static int parse_head_params(struct mgcp_response *r)
280{
281 char *line;
282 int rc = 0;
283 OSMO_ASSERT(r->body);
284 char *data = r->body;
285 char *data_end = strstr(r->body, "\n\n");
286
287 /* Protect SDP body, for_each_non_empty_line() will
288 * only parse until it hits \0 mark. */
289 if (data_end)
290 *data_end = '\0';
291
292 for_each_non_empty_line(line, data) {
293 switch (line[0]) {
294 case 'I':
295 rc = mgcp_parse_conn_id(r, line);
296 if (rc)
297 goto exit;
298 break;
299 default:
300 /* skip unhandled parameters */
301 break;
302 }
303 }
304exit:
305 /* Restore original state */
306 if (data_end)
307 *data_end = '\n';
308
309 return rc;
310}
311
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200312static struct mgcp_response_pending *mgcp_client_response_pending_get(
313 struct mgcp_client *mgcp,
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200314 struct mgcp_response *r)
315{
316 struct mgcp_response_pending *pending;
317 if (!r)
318 return NULL;
319 llist_for_each_entry(pending, &mgcp->responses_pending, entry) {
320 if (pending->trans_id == r->head.trans_id) {
321 llist_del(&pending->entry);
322 return pending;
323 }
324 }
325 return NULL;
326}
327
328/* Feed an MGCP message into the receive processing.
329 * Parse the head and call any callback registered for the transaction id found
330 * in the MGCP message. This is normally called directly from the internal
331 * mgcp_do_read that reads from the socket connected to the MGCP gateway. This
332 * function is published mainly to be able to feed data from the test suite.
333 */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200334int mgcp_client_rx(struct mgcp_client *mgcp, struct msgb *msg)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200335{
336 struct mgcp_response r = { 0 };
337 struct mgcp_response_pending *pending;
338 int rc;
339
340 rc = mgcp_response_parse_head(&r, msg);
341 if (rc) {
Philipp Maierffd75e42017-11-22 11:44:50 +0100342 LOGP(DLMGCP, LOGL_ERROR, "Cannot parse MGCP response (head)\n");
343 return -1;
344 }
345
346 rc = parse_head_params(&r);
347 if (rc) {
348 LOGP(DLMGCP, LOGL_ERROR, "Cannot parse MGCP response (head parameters)\n");
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200349 return -1;
350 }
351
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200352 pending = mgcp_client_response_pending_get(mgcp, &r);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200353 if (!pending) {
354 LOGP(DLMGCP, LOGL_ERROR,
355 "Cannot find matching MGCP transaction for trans_id %d\n",
356 r.head.trans_id);
357 return -1;
358 }
359
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200360 mgcp_client_handle_response(mgcp, pending, &r);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200361 return 0;
362}
363
364static int mgcp_do_read(struct osmo_fd *fd)
365{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200366 struct mgcp_client *mgcp = fd->data;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200367 struct msgb *msg;
368 int ret;
369
370 msg = msgb_alloc_headroom(4096, 128, "mgcp_from_gw");
371 if (!msg) {
372 LOGP(DLMGCP, LOGL_ERROR, "Failed to allocate MGCP message.\n");
373 return -1;
374 }
375
376 ret = read(fd->fd, msg->data, 4096 - 128);
377 if (ret <= 0) {
378 LOGP(DLMGCP, LOGL_ERROR, "Failed to read: %d/%s\n", errno, strerror(errno));
379 msgb_free(msg);
380 return -1;
381 } else if (ret > 4096 - 128) {
382 LOGP(DLMGCP, LOGL_ERROR, "Too much data: %d\n", ret);
383 msgb_free(msg);
384 return -1;
Harald Welte9bf7c532017-11-17 14:14:31 +0100385 }
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200386
387 msg->l2h = msgb_put(msg, ret);
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200388 ret = mgcp_client_rx(mgcp, msg);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200389 talloc_free(msg);
390 return ret;
391}
392
393static int mgcp_do_write(struct osmo_fd *fd, struct msgb *msg)
394{
395 int ret;
396 static char strbuf[4096];
397 unsigned int l = msg->len < sizeof(strbuf) ? msg->len : sizeof(strbuf);
398 unsigned int i;
399
400 strncpy(strbuf, (const char*)msg->data, l);
401 for (i = 0; i < sizeof(strbuf); i++) {
402 if (strbuf[i] == '\n' || strbuf[i] == '\r') {
403 strbuf[i] = '\0';
404 break;
405 }
406 }
407 DEBUGP(DLMGCP, "Tx MGCP msg to MGCP GW: '%s'\n", strbuf);
408
409 LOGP(DLMGCP, LOGL_DEBUG, "Sending msg to MGCP GW size: %u\n", msg->len);
410
411 ret = write(fd->fd, msg->data, msg->len);
412 if (ret != msg->len)
413 LOGP(DLMGCP, LOGL_ERROR, "Failed to forward message to MGCP"
414 " GW: %s\n", strerror(errno));
415
416 return ret;
417}
418
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200419struct mgcp_client *mgcp_client_init(void *ctx,
420 struct mgcp_client_conf *conf)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200421{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200422 struct mgcp_client *mgcp;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200423
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200424 mgcp = talloc_zero(ctx, struct mgcp_client);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200425
426 INIT_LLIST_HEAD(&mgcp->responses_pending);
427 INIT_LLIST_HEAD(&mgcp->inuse_endpoints);
428
429 mgcp->next_trans_id = 1;
430
431 mgcp->actual.local_addr = conf->local_addr ? conf->local_addr :
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200432 MGCP_CLIENT_LOCAL_ADDR_DEFAULT;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200433 mgcp->actual.local_port = conf->local_port >= 0 ? (uint16_t)conf->local_port :
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200434 MGCP_CLIENT_LOCAL_PORT_DEFAULT;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200435
436 mgcp->actual.remote_addr = conf->remote_addr ? conf->remote_addr :
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200437 MGCP_CLIENT_REMOTE_ADDR_DEFAULT;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200438 mgcp->actual.remote_port = conf->remote_port >= 0 ? (uint16_t)conf->remote_port :
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200439 MGCP_CLIENT_REMOTE_PORT_DEFAULT;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200440
441 mgcp->actual.first_endpoint = conf->first_endpoint > 0 ? (uint16_t)conf->first_endpoint : 0;
442 mgcp->actual.last_endpoint = conf->last_endpoint > 0 ? (uint16_t)conf->last_endpoint : 0;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200443
444 return mgcp;
445}
446
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200447int mgcp_client_connect(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200448{
449 int on;
450 struct sockaddr_in addr;
451 struct osmo_wqueue *wq;
452 int rc;
453
454 if (!mgcp) {
455 LOGP(DLMGCP, LOGL_FATAL, "MGCPGW client not initialized properly\n");
456 return -EINVAL;
457 }
458
459 wq = &mgcp->wq;
460
461 wq->bfd.fd = socket(AF_INET, SOCK_DGRAM, 0);
462 if (wq->bfd.fd < 0) {
463 LOGP(DLMGCP, LOGL_FATAL, "Failed to create UDP socket errno: %d\n", errno);
464 return -errno;
465 }
466
467 on = 1;
468 if (setsockopt(wq->bfd.fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) {
469 LOGP(DLMGCP, LOGL_FATAL,
470 "Failed to initialize socket for MGCP GW: %s\n",
471 strerror(errno));
472 rc = -errno;
473 goto error_close_fd;
474 }
475
476 /* bind socket */
477 memset(&addr, 0, sizeof(addr));
478 addr.sin_family = AF_INET;
479 inet_aton(mgcp->actual.local_addr, &addr.sin_addr);
480 addr.sin_port = htons(mgcp->actual.local_port);
481 if (bind(wq->bfd.fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
482 LOGP(DLMGCP, LOGL_FATAL,
483 "Failed to bind for MGCP GW to %s %u\n",
484 mgcp->actual.local_addr, mgcp->actual.local_port);
485 rc = -errno;
486 goto error_close_fd;
487 }
488
489 /* connect to the remote */
490 inet_aton(mgcp->actual.remote_addr, &addr.sin_addr);
491 addr.sin_port = htons(mgcp->actual.remote_port);
492 if (connect(wq->bfd.fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
493 LOGP(DLMGCP, LOGL_FATAL,
494 "Failed to connect to MGCP GW at %s %u: %s\n",
495 mgcp->actual.remote_addr, mgcp->actual.remote_port,
496 strerror(errno));
497 rc = -errno;
498 goto error_close_fd;
499 }
500
501 mgcp->remote_addr = htonl(addr.sin_addr.s_addr);
502
503 osmo_wqueue_init(wq, 10);
504 wq->bfd.when = BSC_FD_READ;
505 wq->bfd.data = mgcp;
506 wq->read_cb = mgcp_do_read;
507 wq->write_cb = mgcp_do_write;
508
509 if (osmo_fd_register(&wq->bfd) != 0) {
510 LOGP(DLMGCP, LOGL_FATAL, "Failed to register BFD\n");
511 rc = -EIO;
512 goto error_close_fd;
513 }
514 LOGP(DLMGCP, LOGL_INFO, "MGCP GW connection: %s:%u -> %s:%u\n",
515 mgcp->actual.local_addr, mgcp->actual.local_port,
516 mgcp->actual.remote_addr, mgcp->actual.remote_port);
517
518 return 0;
519error_close_fd:
520 close(wq->bfd.fd);
521 wq->bfd.fd = -1;
522 return rc;
523}
524
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200525const char *mgcp_client_remote_addr_str(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200526{
527 return mgcp->actual.remote_addr;
528}
529
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200530uint16_t mgcp_client_remote_port(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200531{
532 return mgcp->actual.remote_port;
533}
534
535/* Return the MGCP GW binary IPv4 address in network byte order. */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200536uint32_t mgcp_client_remote_addr_n(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200537{
538 return mgcp->remote_addr;
539}
540
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200541struct mgcp_response_pending * mgcp_client_pending_add(
542 struct mgcp_client *mgcp,
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200543 mgcp_trans_id_t trans_id,
544 mgcp_response_cb_t response_cb,
545 void *priv)
546{
547 struct mgcp_response_pending *pending;
548
549 pending = talloc_zero(mgcp, struct mgcp_response_pending);
550 pending->trans_id = trans_id;
551 pending->response_cb = response_cb;
552 pending->priv = priv;
553 llist_add_tail(&pending->entry, &mgcp->responses_pending);
554
555 return pending;
556}
557
558/* Send the MGCP message in msg to the MGCP GW and handle a response with
559 * response_cb. NOTE: the response_cb still needs to call
560 * mgcp_response_parse_params(response) to get the parsed parameters -- to
561 * potentially save some CPU cycles, only the head line has been parsed when
562 * the response_cb is invoked. */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200563int mgcp_client_tx(struct mgcp_client *mgcp, struct msgb *msg,
564 mgcp_response_cb_t response_cb, void *priv)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200565{
566 struct mgcp_response_pending *pending;
567 mgcp_trans_id_t trans_id;
568 int rc;
569
570 trans_id = msg->cb[MSGB_CB_MGCP_TRANS_ID];
571 if (!trans_id) {
572 LOGP(DLMGCP, LOGL_ERROR,
573 "Unset transaction id in mgcp send request\n");
574 talloc_free(msg);
575 return -EINVAL;
576 }
577
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200578 pending = mgcp_client_pending_add(mgcp, trans_id, response_cb, priv);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200579
580 if (msgb_l2len(msg) > 4096) {
581 LOGP(DLMGCP, LOGL_ERROR,
582 "Cannot send, MGCP message too large: %u\n",
583 msgb_l2len(msg));
584 msgb_free(msg);
585 rc = -EINVAL;
586 goto mgcp_tx_error;
587 }
588
589 rc = osmo_wqueue_enqueue(&mgcp->wq, msg);
590 if (rc) {
591 LOGP(DLMGCP, LOGL_FATAL, "Could not queue message to MGCP GW\n");
592 msgb_free(msg);
593 goto mgcp_tx_error;
594 } else
595 LOGP(DLMGCP, LOGL_INFO, "Queued %u bytes for MGCP GW\n",
596 msgb_l2len(msg));
597 return 0;
598
599mgcp_tx_error:
600 /* Pass NULL to response cb to indicate an error */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200601 mgcp_client_handle_response(mgcp, pending, NULL);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200602 return -1;
603}
604
605static struct msgb *mgcp_msg_from_buf(mgcp_trans_id_t trans_id,
606 const char *buf, int len)
607{
608 struct msgb *msg;
609
610 if (len > (4096 - 128)) {
611 LOGP(DLMGCP, LOGL_ERROR, "Cannot send to MGCP GW:"
612 " message too large: %d\n", len);
613 return NULL;
614 }
615
616 msg = msgb_alloc_headroom(4096, 128, "MGCP tx");
617 OSMO_ASSERT(msg);
618
619 char *dst = (char*)msgb_put(msg, len);
620 memcpy(dst, buf, len);
621 msg->l2h = msg->data;
622 msg->cb[MSGB_CB_MGCP_TRANS_ID] = trans_id;
623
624 return msg;
625}
626
627static struct msgb *mgcp_msg_from_str(mgcp_trans_id_t trans_id,
628 const char *fmt, ...)
629{
630 static char compose[4096 - 128];
631 va_list ap;
632 int len;
633 OSMO_ASSERT(fmt);
634
635 va_start(ap, fmt);
636 len = vsnprintf(compose, sizeof(compose), fmt, ap);
637 va_end(ap);
638 if (len >= sizeof(compose)) {
639 LOGP(DLMGCP, LOGL_ERROR,
640 "Message too large: trans_id=%u len=%d\n",
641 trans_id, len);
642 return NULL;
643 }
644 if (len < 1) {
645 LOGP(DLMGCP, LOGL_ERROR,
646 "Failed to compose message: trans_id=%u len=%d\n",
647 trans_id, len);
648 return NULL;
649 }
650 return mgcp_msg_from_buf(trans_id, compose, len);
651}
652
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200653static mgcp_trans_id_t mgcp_client_next_trans_id(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200654{
655 /* avoid zero trans_id to distinguish from unset trans_id */
656 if (!mgcp->next_trans_id)
657 mgcp->next_trans_id ++;
658 return mgcp->next_trans_id ++;
659}
660
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200661struct msgb *mgcp_msg_crcx(struct mgcp_client *mgcp,
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200662 uint16_t rtp_endpoint, unsigned int call_id,
663 enum mgcp_connection_mode mode)
664{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200665 mgcp_trans_id_t trans_id = mgcp_client_next_trans_id(mgcp);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200666 return mgcp_msg_from_str(trans_id,
667 "CRCX %u %x@mgw MGCP 1.0\r\n"
668 "C: %x\r\n"
669 "L: p:20, a:AMR, nt:IN\r\n"
670 "M: %s\r\n"
671 ,
672 trans_id,
673 rtp_endpoint,
674 call_id,
Neels Hofmeyrd95ab1e2017-09-22 00:52:54 +0200675 mgcp_client_cmode_name(mode));
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200676}
677
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200678struct msgb *mgcp_msg_mdcx(struct mgcp_client *mgcp,
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200679 uint16_t rtp_endpoint, const char *rtp_conn_addr,
680 uint16_t rtp_port, enum mgcp_connection_mode mode)
681
682{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200683 mgcp_trans_id_t trans_id = mgcp_client_next_trans_id(mgcp);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200684 return mgcp_msg_from_str(trans_id,
685 "MDCX %u %x@mgw MGCP 1.0\r\n"
686 "M: %s\r\n"
687 "\r\n"
688 "c=IN IP4 %s\r\n"
689 "m=audio %u RTP/AVP 255\r\n"
690 ,
691 trans_id,
692 rtp_endpoint,
Neels Hofmeyrd95ab1e2017-09-22 00:52:54 +0200693 mgcp_client_cmode_name(mode),
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200694 rtp_conn_addr,
695 rtp_port);
696}
697
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200698struct msgb *mgcp_msg_dlcx(struct mgcp_client *mgcp, uint16_t rtp_endpoint,
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200699 unsigned int call_id)
700{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200701 mgcp_trans_id_t trans_id = mgcp_client_next_trans_id(mgcp);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200702 return mgcp_msg_from_str(trans_id,
703 "DLCX %u %x@mgw MGCP 1.0\r\n"
704 "C: %x\r\n", trans_id, rtp_endpoint, call_id);
705}
706
Philipp Maier1dc6be62017-10-05 18:25:37 +0200707#define MGCP_CRCX_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT | \
708 MGCP_MSG_PRESENCE_CALL_ID | \
Philipp Maier1dc6be62017-10-05 18:25:37 +0200709 MGCP_MSG_PRESENCE_CONN_MODE)
710#define MGCP_MDCX_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT | \
711 MGCP_MSG_PRESENCE_CONN_ID)
712#define MGCP_DLCX_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT)
713#define MGCP_AUEP_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT)
714#define MGCP_RSIP_MANDATORY 0 /* none */
715
716struct msgb *mgcp_msg_gen(struct mgcp_client *mgcp, struct mgcp_msg *mgcp_msg)
717{
718 mgcp_trans_id_t trans_id = mgcp_client_next_trans_id(mgcp);
719 uint32_t mandatory_mask;
720 struct msgb *msg = msgb_alloc_headroom(4096, 128, "MGCP tx");
721 int rc = 0;
722
723 msg->l2h = msg->data;
724 msg->cb[MSGB_CB_MGCP_TRANS_ID] = trans_id;
725
726 /* Add command verb */
727 switch (mgcp_msg->verb) {
728 case MGCP_VERB_CRCX:
729 mandatory_mask = MGCP_CRCX_MANDATORY;
730 rc += msgb_printf(msg, "CRCX %u", trans_id);
731 break;
732 case MGCP_VERB_MDCX:
733 mandatory_mask = MGCP_MDCX_MANDATORY;
734 rc += msgb_printf(msg, "MDCX %u", trans_id);
735 break;
736 case MGCP_VERB_DLCX:
737 mandatory_mask = MGCP_DLCX_MANDATORY;
738 rc += msgb_printf(msg, "DLCX %u", trans_id);
739 break;
740 case MGCP_VERB_AUEP:
741 mandatory_mask = MGCP_AUEP_MANDATORY;
742 rc += msgb_printf(msg, "AUEP %u", trans_id);
743 break;
744 case MGCP_VERB_RSIP:
745 mandatory_mask = MGCP_RSIP_MANDATORY;
746 rc += msgb_printf(msg, "RSIP %u", trans_id);
747 break;
748 default:
749 LOGP(DLMGCP, LOGL_ERROR,
750 "Invalid command verb, can not generate MGCP message\n");
751 msgb_free(msg);
752 return NULL;
753 }
754
755 /* Check if mandatory fields are missing */
756 if (!((mgcp_msg->presence & mandatory_mask) == mandatory_mask)) {
757 LOGP(DLMGCP, LOGL_ERROR,
758 "One or more missing mandatory fields, can not generate MGCP message\n");
759 msgb_free(msg);
760 return NULL;
761 }
762
763 /* Add endpoint name */
764 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_ENDPOINT)
765 rc += msgb_printf(msg, " %s", mgcp_msg->endpoint);
766
767 /* Add protocol version */
768 rc += msgb_printf(msg, " MGCP 1.0\r\n");
769
770 /* Add call id */
771 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_CALL_ID)
772 rc += msgb_printf(msg, "C: %x\r\n", mgcp_msg->call_id);
773
774 /* Add connection id */
775 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_CONN_ID)
Philipp Maier01d24a32017-11-21 17:26:09 +0100776 rc += msgb_printf(msg, "I: %s\r\n", mgcp_msg->conn_id);
Philipp Maier1dc6be62017-10-05 18:25:37 +0200777
778 /* Add local connection options */
Philipp Maierffd75e42017-11-22 11:44:50 +0100779 if (mgcp_msg->verb == MGCP_VERB_CRCX)
Philipp Maier1dc6be62017-10-05 18:25:37 +0200780 rc += msgb_printf(msg, "L: p:20, a:AMR, nt:IN\r\n");
781
782 /* Add mode */
783 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_CONN_MODE)
784 rc +=
785 msgb_printf(msg, "M: %s\r\n",
786 mgcp_client_cmode_name(mgcp_msg->conn_mode));
787
788 /* Add RTP address and port (SDP) */
789 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_AUDIO_IP
790 && mgcp_msg->presence & MGCP_MSG_PRESENCE_AUDIO_PORT) {
791 rc += msgb_printf(msg, "\r\n");
792 rc += msgb_printf(msg, "c=IN IP4 %s\r\n", mgcp_msg->audio_ip);
793 rc +=
794 msgb_printf(msg, "m=audio %u RTP/AVP 255\r\n",
795 mgcp_msg->audio_port);
796 }
797
798 if (rc != 0) {
799 LOGP(DLMGCP, LOGL_ERROR,
800 "message buffer to small, can not generate MGCP message\n");
801 msgb_free(msg);
802 msg = NULL;
803 }
804
805 return msg;
806}
807
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200808struct mgcp_client_conf *mgcp_client_conf_actual(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200809{
810 return &mgcp->actual;
811}
Neels Hofmeyrd95ab1e2017-09-22 00:52:54 +0200812
813const struct value_string mgcp_client_connection_mode_strs[] = {
814 { MGCP_CONN_NONE, "none" },
815 { MGCP_CONN_RECV_SEND, "sendrecv" },
816 { MGCP_CONN_SEND_ONLY, "sendonly" },
817 { MGCP_CONN_RECV_ONLY, "recvonly" },
818 { MGCP_CONN_LOOPBACK, "loopback" },
819 { 0, NULL }
820};