blob: a89da9989b56c7e85a2ae58701eabd8b4f80aec0 [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
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200260static struct mgcp_response_pending *mgcp_client_response_pending_get(
261 struct mgcp_client *mgcp,
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200262 struct mgcp_response *r)
263{
264 struct mgcp_response_pending *pending;
265 if (!r)
266 return NULL;
267 llist_for_each_entry(pending, &mgcp->responses_pending, entry) {
268 if (pending->trans_id == r->head.trans_id) {
269 llist_del(&pending->entry);
270 return pending;
271 }
272 }
273 return NULL;
274}
275
276/* Feed an MGCP message into the receive processing.
277 * Parse the head and call any callback registered for the transaction id found
278 * in the MGCP message. This is normally called directly from the internal
279 * mgcp_do_read that reads from the socket connected to the MGCP gateway. This
280 * function is published mainly to be able to feed data from the test suite.
281 */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200282int mgcp_client_rx(struct mgcp_client *mgcp, struct msgb *msg)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200283{
284 struct mgcp_response r = { 0 };
285 struct mgcp_response_pending *pending;
286 int rc;
287
288 rc = mgcp_response_parse_head(&r, msg);
289 if (rc) {
290 LOGP(DLMGCP, LOGL_ERROR, "Cannot parse MGCP response\n");
291 return -1;
292 }
293
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200294 pending = mgcp_client_response_pending_get(mgcp, &r);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200295 if (!pending) {
296 LOGP(DLMGCP, LOGL_ERROR,
297 "Cannot find matching MGCP transaction for trans_id %d\n",
298 r.head.trans_id);
299 return -1;
300 }
301
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200302 mgcp_client_handle_response(mgcp, pending, &r);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200303 return 0;
304}
305
306static int mgcp_do_read(struct osmo_fd *fd)
307{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200308 struct mgcp_client *mgcp = fd->data;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200309 struct msgb *msg;
310 int ret;
311
312 msg = msgb_alloc_headroom(4096, 128, "mgcp_from_gw");
313 if (!msg) {
314 LOGP(DLMGCP, LOGL_ERROR, "Failed to allocate MGCP message.\n");
315 return -1;
316 }
317
318 ret = read(fd->fd, msg->data, 4096 - 128);
319 if (ret <= 0) {
320 LOGP(DLMGCP, LOGL_ERROR, "Failed to read: %d/%s\n", errno, strerror(errno));
321 msgb_free(msg);
322 return -1;
323 } else if (ret > 4096 - 128) {
324 LOGP(DLMGCP, LOGL_ERROR, "Too much data: %d\n", ret);
325 msgb_free(msg);
326 return -1;
Harald Welte9bf7c532017-11-17 14:14:31 +0100327 }
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200328
329 msg->l2h = msgb_put(msg, ret);
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200330 ret = mgcp_client_rx(mgcp, msg);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200331 talloc_free(msg);
332 return ret;
333}
334
335static int mgcp_do_write(struct osmo_fd *fd, struct msgb *msg)
336{
337 int ret;
338 static char strbuf[4096];
339 unsigned int l = msg->len < sizeof(strbuf) ? msg->len : sizeof(strbuf);
340 unsigned int i;
341
342 strncpy(strbuf, (const char*)msg->data, l);
343 for (i = 0; i < sizeof(strbuf); i++) {
344 if (strbuf[i] == '\n' || strbuf[i] == '\r') {
345 strbuf[i] = '\0';
346 break;
347 }
348 }
349 DEBUGP(DLMGCP, "Tx MGCP msg to MGCP GW: '%s'\n", strbuf);
350
351 LOGP(DLMGCP, LOGL_DEBUG, "Sending msg to MGCP GW size: %u\n", msg->len);
352
353 ret = write(fd->fd, msg->data, msg->len);
354 if (ret != msg->len)
355 LOGP(DLMGCP, LOGL_ERROR, "Failed to forward message to MGCP"
356 " GW: %s\n", strerror(errno));
357
358 return ret;
359}
360
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200361struct mgcp_client *mgcp_client_init(void *ctx,
362 struct mgcp_client_conf *conf)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200363{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200364 struct mgcp_client *mgcp;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200365
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200366 mgcp = talloc_zero(ctx, struct mgcp_client);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200367
368 INIT_LLIST_HEAD(&mgcp->responses_pending);
369 INIT_LLIST_HEAD(&mgcp->inuse_endpoints);
370
371 mgcp->next_trans_id = 1;
372
373 mgcp->actual.local_addr = conf->local_addr ? conf->local_addr :
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200374 MGCP_CLIENT_LOCAL_ADDR_DEFAULT;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200375 mgcp->actual.local_port = conf->local_port >= 0 ? (uint16_t)conf->local_port :
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200376 MGCP_CLIENT_LOCAL_PORT_DEFAULT;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200377
378 mgcp->actual.remote_addr = conf->remote_addr ? conf->remote_addr :
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200379 MGCP_CLIENT_REMOTE_ADDR_DEFAULT;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200380 mgcp->actual.remote_port = conf->remote_port >= 0 ? (uint16_t)conf->remote_port :
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200381 MGCP_CLIENT_REMOTE_PORT_DEFAULT;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200382
383 mgcp->actual.first_endpoint = conf->first_endpoint > 0 ? (uint16_t)conf->first_endpoint : 0;
384 mgcp->actual.last_endpoint = conf->last_endpoint > 0 ? (uint16_t)conf->last_endpoint : 0;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200385
386 return mgcp;
387}
388
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200389int mgcp_client_connect(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200390{
391 int on;
392 struct sockaddr_in addr;
393 struct osmo_wqueue *wq;
394 int rc;
395
396 if (!mgcp) {
397 LOGP(DLMGCP, LOGL_FATAL, "MGCPGW client not initialized properly\n");
398 return -EINVAL;
399 }
400
401 wq = &mgcp->wq;
402
403 wq->bfd.fd = socket(AF_INET, SOCK_DGRAM, 0);
404 if (wq->bfd.fd < 0) {
405 LOGP(DLMGCP, LOGL_FATAL, "Failed to create UDP socket errno: %d\n", errno);
406 return -errno;
407 }
408
409 on = 1;
410 if (setsockopt(wq->bfd.fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) {
411 LOGP(DLMGCP, LOGL_FATAL,
412 "Failed to initialize socket for MGCP GW: %s\n",
413 strerror(errno));
414 rc = -errno;
415 goto error_close_fd;
416 }
417
418 /* bind socket */
419 memset(&addr, 0, sizeof(addr));
420 addr.sin_family = AF_INET;
421 inet_aton(mgcp->actual.local_addr, &addr.sin_addr);
422 addr.sin_port = htons(mgcp->actual.local_port);
423 if (bind(wq->bfd.fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
424 LOGP(DLMGCP, LOGL_FATAL,
425 "Failed to bind for MGCP GW to %s %u\n",
426 mgcp->actual.local_addr, mgcp->actual.local_port);
427 rc = -errno;
428 goto error_close_fd;
429 }
430
431 /* connect to the remote */
432 inet_aton(mgcp->actual.remote_addr, &addr.sin_addr);
433 addr.sin_port = htons(mgcp->actual.remote_port);
434 if (connect(wq->bfd.fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
435 LOGP(DLMGCP, LOGL_FATAL,
436 "Failed to connect to MGCP GW at %s %u: %s\n",
437 mgcp->actual.remote_addr, mgcp->actual.remote_port,
438 strerror(errno));
439 rc = -errno;
440 goto error_close_fd;
441 }
442
443 mgcp->remote_addr = htonl(addr.sin_addr.s_addr);
444
445 osmo_wqueue_init(wq, 10);
446 wq->bfd.when = BSC_FD_READ;
447 wq->bfd.data = mgcp;
448 wq->read_cb = mgcp_do_read;
449 wq->write_cb = mgcp_do_write;
450
451 if (osmo_fd_register(&wq->bfd) != 0) {
452 LOGP(DLMGCP, LOGL_FATAL, "Failed to register BFD\n");
453 rc = -EIO;
454 goto error_close_fd;
455 }
456 LOGP(DLMGCP, LOGL_INFO, "MGCP GW connection: %s:%u -> %s:%u\n",
457 mgcp->actual.local_addr, mgcp->actual.local_port,
458 mgcp->actual.remote_addr, mgcp->actual.remote_port);
459
460 return 0;
461error_close_fd:
462 close(wq->bfd.fd);
463 wq->bfd.fd = -1;
464 return rc;
465}
466
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200467const char *mgcp_client_remote_addr_str(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200468{
469 return mgcp->actual.remote_addr;
470}
471
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200472uint16_t mgcp_client_remote_port(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200473{
474 return mgcp->actual.remote_port;
475}
476
477/* Return the MGCP GW binary IPv4 address in network byte order. */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200478uint32_t mgcp_client_remote_addr_n(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200479{
480 return mgcp->remote_addr;
481}
482
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200483struct mgcp_response_pending * mgcp_client_pending_add(
484 struct mgcp_client *mgcp,
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200485 mgcp_trans_id_t trans_id,
486 mgcp_response_cb_t response_cb,
487 void *priv)
488{
489 struct mgcp_response_pending *pending;
490
491 pending = talloc_zero(mgcp, struct mgcp_response_pending);
492 pending->trans_id = trans_id;
493 pending->response_cb = response_cb;
494 pending->priv = priv;
495 llist_add_tail(&pending->entry, &mgcp->responses_pending);
496
497 return pending;
498}
499
500/* Send the MGCP message in msg to the MGCP GW and handle a response with
501 * response_cb. NOTE: the response_cb still needs to call
502 * mgcp_response_parse_params(response) to get the parsed parameters -- to
503 * potentially save some CPU cycles, only the head line has been parsed when
504 * the response_cb is invoked. */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200505int mgcp_client_tx(struct mgcp_client *mgcp, struct msgb *msg,
506 mgcp_response_cb_t response_cb, void *priv)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200507{
508 struct mgcp_response_pending *pending;
509 mgcp_trans_id_t trans_id;
510 int rc;
511
512 trans_id = msg->cb[MSGB_CB_MGCP_TRANS_ID];
513 if (!trans_id) {
514 LOGP(DLMGCP, LOGL_ERROR,
515 "Unset transaction id in mgcp send request\n");
516 talloc_free(msg);
517 return -EINVAL;
518 }
519
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200520 pending = mgcp_client_pending_add(mgcp, trans_id, response_cb, priv);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200521
522 if (msgb_l2len(msg) > 4096) {
523 LOGP(DLMGCP, LOGL_ERROR,
524 "Cannot send, MGCP message too large: %u\n",
525 msgb_l2len(msg));
526 msgb_free(msg);
527 rc = -EINVAL;
528 goto mgcp_tx_error;
529 }
530
531 rc = osmo_wqueue_enqueue(&mgcp->wq, msg);
532 if (rc) {
533 LOGP(DLMGCP, LOGL_FATAL, "Could not queue message to MGCP GW\n");
534 msgb_free(msg);
535 goto mgcp_tx_error;
536 } else
537 LOGP(DLMGCP, LOGL_INFO, "Queued %u bytes for MGCP GW\n",
538 msgb_l2len(msg));
539 return 0;
540
541mgcp_tx_error:
542 /* Pass NULL to response cb to indicate an error */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200543 mgcp_client_handle_response(mgcp, pending, NULL);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200544 return -1;
545}
546
547static struct msgb *mgcp_msg_from_buf(mgcp_trans_id_t trans_id,
548 const char *buf, int len)
549{
550 struct msgb *msg;
551
552 if (len > (4096 - 128)) {
553 LOGP(DLMGCP, LOGL_ERROR, "Cannot send to MGCP GW:"
554 " message too large: %d\n", len);
555 return NULL;
556 }
557
558 msg = msgb_alloc_headroom(4096, 128, "MGCP tx");
559 OSMO_ASSERT(msg);
560
561 char *dst = (char*)msgb_put(msg, len);
562 memcpy(dst, buf, len);
563 msg->l2h = msg->data;
564 msg->cb[MSGB_CB_MGCP_TRANS_ID] = trans_id;
565
566 return msg;
567}
568
569static struct msgb *mgcp_msg_from_str(mgcp_trans_id_t trans_id,
570 const char *fmt, ...)
571{
572 static char compose[4096 - 128];
573 va_list ap;
574 int len;
575 OSMO_ASSERT(fmt);
576
577 va_start(ap, fmt);
578 len = vsnprintf(compose, sizeof(compose), fmt, ap);
579 va_end(ap);
580 if (len >= sizeof(compose)) {
581 LOGP(DLMGCP, LOGL_ERROR,
582 "Message too large: trans_id=%u len=%d\n",
583 trans_id, len);
584 return NULL;
585 }
586 if (len < 1) {
587 LOGP(DLMGCP, LOGL_ERROR,
588 "Failed to compose message: trans_id=%u len=%d\n",
589 trans_id, len);
590 return NULL;
591 }
592 return mgcp_msg_from_buf(trans_id, compose, len);
593}
594
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200595static mgcp_trans_id_t mgcp_client_next_trans_id(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200596{
597 /* avoid zero trans_id to distinguish from unset trans_id */
598 if (!mgcp->next_trans_id)
599 mgcp->next_trans_id ++;
600 return mgcp->next_trans_id ++;
601}
602
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200603struct msgb *mgcp_msg_crcx(struct mgcp_client *mgcp,
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200604 uint16_t rtp_endpoint, unsigned int call_id,
605 enum mgcp_connection_mode mode)
606{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200607 mgcp_trans_id_t trans_id = mgcp_client_next_trans_id(mgcp);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200608 return mgcp_msg_from_str(trans_id,
609 "CRCX %u %x@mgw MGCP 1.0\r\n"
610 "C: %x\r\n"
611 "L: p:20, a:AMR, nt:IN\r\n"
612 "M: %s\r\n"
613 ,
614 trans_id,
615 rtp_endpoint,
616 call_id,
Neels Hofmeyrd95ab1e2017-09-22 00:52:54 +0200617 mgcp_client_cmode_name(mode));
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200618}
619
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200620struct msgb *mgcp_msg_mdcx(struct mgcp_client *mgcp,
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200621 uint16_t rtp_endpoint, const char *rtp_conn_addr,
622 uint16_t rtp_port, enum mgcp_connection_mode mode)
623
624{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200625 mgcp_trans_id_t trans_id = mgcp_client_next_trans_id(mgcp);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200626 return mgcp_msg_from_str(trans_id,
627 "MDCX %u %x@mgw MGCP 1.0\r\n"
628 "M: %s\r\n"
629 "\r\n"
630 "c=IN IP4 %s\r\n"
631 "m=audio %u RTP/AVP 255\r\n"
632 ,
633 trans_id,
634 rtp_endpoint,
Neels Hofmeyrd95ab1e2017-09-22 00:52:54 +0200635 mgcp_client_cmode_name(mode),
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200636 rtp_conn_addr,
637 rtp_port);
638}
639
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200640struct msgb *mgcp_msg_dlcx(struct mgcp_client *mgcp, uint16_t rtp_endpoint,
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200641 unsigned int call_id)
642{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200643 mgcp_trans_id_t trans_id = mgcp_client_next_trans_id(mgcp);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200644 return mgcp_msg_from_str(trans_id,
645 "DLCX %u %x@mgw MGCP 1.0\r\n"
646 "C: %x\r\n", trans_id, rtp_endpoint, call_id);
647}
648
Philipp Maier1dc6be62017-10-05 18:25:37 +0200649#define MGCP_CRCX_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT | \
650 MGCP_MSG_PRESENCE_CALL_ID | \
651 MGCP_MSG_PRESENCE_CONN_ID | \
652 MGCP_MSG_PRESENCE_CONN_MODE)
653#define MGCP_MDCX_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT | \
654 MGCP_MSG_PRESENCE_CONN_ID)
655#define MGCP_DLCX_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT)
656#define MGCP_AUEP_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT)
657#define MGCP_RSIP_MANDATORY 0 /* none */
658
659struct msgb *mgcp_msg_gen(struct mgcp_client *mgcp, struct mgcp_msg *mgcp_msg)
660{
661 mgcp_trans_id_t trans_id = mgcp_client_next_trans_id(mgcp);
662 uint32_t mandatory_mask;
663 struct msgb *msg = msgb_alloc_headroom(4096, 128, "MGCP tx");
664 int rc = 0;
665
666 msg->l2h = msg->data;
667 msg->cb[MSGB_CB_MGCP_TRANS_ID] = trans_id;
668
669 /* Add command verb */
670 switch (mgcp_msg->verb) {
671 case MGCP_VERB_CRCX:
672 mandatory_mask = MGCP_CRCX_MANDATORY;
673 rc += msgb_printf(msg, "CRCX %u", trans_id);
674 break;
675 case MGCP_VERB_MDCX:
676 mandatory_mask = MGCP_MDCX_MANDATORY;
677 rc += msgb_printf(msg, "MDCX %u", trans_id);
678 break;
679 case MGCP_VERB_DLCX:
680 mandatory_mask = MGCP_DLCX_MANDATORY;
681 rc += msgb_printf(msg, "DLCX %u", trans_id);
682 break;
683 case MGCP_VERB_AUEP:
684 mandatory_mask = MGCP_AUEP_MANDATORY;
685 rc += msgb_printf(msg, "AUEP %u", trans_id);
686 break;
687 case MGCP_VERB_RSIP:
688 mandatory_mask = MGCP_RSIP_MANDATORY;
689 rc += msgb_printf(msg, "RSIP %u", trans_id);
690 break;
691 default:
692 LOGP(DLMGCP, LOGL_ERROR,
693 "Invalid command verb, can not generate MGCP message\n");
694 msgb_free(msg);
695 return NULL;
696 }
697
698 /* Check if mandatory fields are missing */
699 if (!((mgcp_msg->presence & mandatory_mask) == mandatory_mask)) {
700 LOGP(DLMGCP, LOGL_ERROR,
701 "One or more missing mandatory fields, can not generate MGCP message\n");
702 msgb_free(msg);
703 return NULL;
704 }
705
706 /* Add endpoint name */
707 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_ENDPOINT)
708 rc += msgb_printf(msg, " %s", mgcp_msg->endpoint);
709
710 /* Add protocol version */
711 rc += msgb_printf(msg, " MGCP 1.0\r\n");
712
713 /* Add call id */
714 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_CALL_ID)
715 rc += msgb_printf(msg, "C: %x\r\n", mgcp_msg->call_id);
716
717 /* Add connection id */
718 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_CONN_ID)
Philipp Maier01d24a32017-11-21 17:26:09 +0100719 rc += msgb_printf(msg, "I: %s\r\n", mgcp_msg->conn_id);
Philipp Maier1dc6be62017-10-05 18:25:37 +0200720
721 /* Add local connection options */
722 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_CONN_ID
723 && mgcp_msg->verb == MGCP_VERB_CRCX)
724 rc += msgb_printf(msg, "L: p:20, a:AMR, nt:IN\r\n");
725
726 /* Add mode */
727 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_CONN_MODE)
728 rc +=
729 msgb_printf(msg, "M: %s\r\n",
730 mgcp_client_cmode_name(mgcp_msg->conn_mode));
731
732 /* Add RTP address and port (SDP) */
733 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_AUDIO_IP
734 && mgcp_msg->presence & MGCP_MSG_PRESENCE_AUDIO_PORT) {
735 rc += msgb_printf(msg, "\r\n");
736 rc += msgb_printf(msg, "c=IN IP4 %s\r\n", mgcp_msg->audio_ip);
737 rc +=
738 msgb_printf(msg, "m=audio %u RTP/AVP 255\r\n",
739 mgcp_msg->audio_port);
740 }
741
742 if (rc != 0) {
743 LOGP(DLMGCP, LOGL_ERROR,
744 "message buffer to small, can not generate MGCP message\n");
745 msgb_free(msg);
746 msg = NULL;
747 }
748
749 return msg;
750}
751
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200752struct mgcp_client_conf *mgcp_client_conf_actual(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200753{
754 return &mgcp->actual;
755}
Neels Hofmeyrd95ab1e2017-09-22 00:52:54 +0200756
757const struct value_string mgcp_client_connection_mode_strs[] = {
758 { MGCP_CONN_NONE, "none" },
759 { MGCP_CONN_RECV_SEND, "sendrecv" },
760 { MGCP_CONN_SEND_ONLY, "sendonly" },
761 { MGCP_CONN_RECV_ONLY, "recvonly" },
762 { MGCP_CONN_LOOPBACK, "loopback" },
763 { 0, NULL }
764};