blob: 593d30562fbee6470b9fbf037f04620b867db02a [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
Philipp Maier275ac972018-01-20 00:58:16 +010039/*! Initalize MGCP client configuration struct with default values.
40 * \param[out] conf Client configuration.*/
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +020041void mgcp_client_conf_init(struct mgcp_client_conf *conf)
Neels Hofmeyre9920f22017-07-10 15:07:22 +020042{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +020043 /* NULL and -1 default to MGCP_CLIENT_*_DEFAULT values */
44 *conf = (struct mgcp_client_conf){
Neels Hofmeyre9920f22017-07-10 15:07:22 +020045 .local_addr = NULL,
46 .local_port = -1,
47 .remote_addr = NULL,
48 .remote_port = -1,
49 .first_endpoint = 0,
50 .last_endpoint = 0,
Neels Hofmeyrc0dcc3c2017-12-02 18:31:34 +000051 .bts_base = 0,
Neels Hofmeyre9920f22017-07-10 15:07:22 +020052 };
53}
54
55/* Test if a given endpoint id is currently in use */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +020056static bool endpoint_in_use(uint16_t id, struct mgcp_client *client)
Neels Hofmeyre9920f22017-07-10 15:07:22 +020057{
58 struct mgcp_inuse_endpoint *endpoint;
59 llist_for_each_entry(endpoint, &client->inuse_endpoints, entry) {
60 if (endpoint->id == id)
61 return true;
62 }
63
64 return false;
65}
66
Philipp Maier275ac972018-01-20 00:58:16 +010067/*! Pick next free endpoint ID.
68 * \param[in,out] client MGCP client descriptor.
69 * \returns 0 on success, -EINVAL on error. */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +020070int mgcp_client_next_endpoint(struct mgcp_client *client)
Neels Hofmeyre9920f22017-07-10 15:07:22 +020071{
72 int i;
73 uint16_t first_endpoint = client->actual.first_endpoint;
74 uint16_t last_endpoint = client->actual.last_endpoint;
75 struct mgcp_inuse_endpoint *endpoint;
76
77 /* Use the maximum permitted range if the VTY
78 * configuration does not specify a range */
79 if (client->actual.last_endpoint == 0) {
80 first_endpoint = 1;
81 last_endpoint = 65534;
82 }
83
84 /* Test the permitted endpoint range for an endpoint
85 * number that is not in use. When a suitable endpoint
86 * number can be found, seize it by adding it to the
87 * inuse list. */
88 for (i=first_endpoint;i<last_endpoint;i++)
89 {
90 if (endpoint_in_use(i,client) == false) {
91 endpoint = talloc_zero(client, struct mgcp_inuse_endpoint);
92 endpoint->id = i;
93 llist_add_tail(&endpoint->entry, &client->inuse_endpoints);
94 return endpoint->id;
95 }
96 }
97
98 /* All endpoints are busy! */
99 return -EINVAL;
100}
101
Philipp Maier275ac972018-01-20 00:58:16 +0100102/*! Release a seized endpoint ID to make it available again for other calls.
103 * \param[in] id Endpoint ID
104 * \param[in,out] client MGCP client descriptor. */
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200105/* Release a seized endpoint id to make it available again for other calls */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200106void mgcp_client_release_endpoint(uint16_t id, struct mgcp_client *client)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200107{
108 struct mgcp_inuse_endpoint *endpoint;
109 struct mgcp_inuse_endpoint *endpoint_tmp;
110 llist_for_each_entry_safe(endpoint, endpoint_tmp, &client->inuse_endpoints, entry) {
111 if (endpoint->id == id) {
112 llist_del(&endpoint->entry);
113 talloc_free(endpoint);
114 }
115 }
116}
117
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200118static void mgcp_client_handle_response(struct mgcp_client *mgcp,
119 struct mgcp_response_pending *pending,
120 struct mgcp_response *response)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200121{
122 if (!pending) {
123 LOGP(DLMGCP, LOGL_ERROR,
124 "Cannot handle NULL response\n");
125 return;
126 }
127 if (pending->response_cb)
128 pending->response_cb(response, pending->priv);
129 else
130 LOGP(DLMGCP, LOGL_INFO, "MGCP response ignored (NULL cb)\n");
131 talloc_free(pending);
132}
133
134static int mgcp_response_parse_head(struct mgcp_response *r, struct msgb *msg)
135{
136 int comment_pos;
137 char *end;
138
139 if (mgcp_msg_terminate_nul(msg))
140 goto response_parse_failure;
141
142 r->body = (char *)msg->data;
143
Harald Welte9bf7c532017-11-17 14:14:31 +0100144 if (sscanf(r->body, "%3d %u %n",
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200145 &r->head.response_code, &r->head.trans_id,
146 &comment_pos) != 2)
147 goto response_parse_failure;
148
Philipp Maierabe8c892018-01-20 00:15:12 +0100149 osmo_strlcpy(r->head.comment, r->body + comment_pos, sizeof(r->head.comment));
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200150 end = strchr(r->head.comment, '\r');
151 if (!end)
152 goto response_parse_failure;
153 /* Mark the end of the comment */
154 *end = '\0';
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200155 return 0;
156
157response_parse_failure:
158 LOGP(DLMGCP, LOGL_ERROR,
159 "Failed to parse MGCP response header\n");
160 return -EINVAL;
161}
162
163/* TODO undup against mgcp_protocol.c:mgcp_check_param() */
164static bool mgcp_line_is_valid(const char *line)
165{
166 const size_t line_len = strlen(line);
167 if (line[0] == '\0')
168 return true;
169
170 if (line_len < 2
171 || line[1] != '=') {
172 LOGP(DLMGCP, LOGL_ERROR,
173 "Wrong MGCP option format: '%s'\n",
174 line);
175 return false;
176 }
177
178 return true;
179}
180
181/* Parse a line like "m=audio 16002 RTP/AVP 98" */
Philipp Maier06da85e2017-10-05 18:49:24 +0200182static int mgcp_parse_audio_port(struct mgcp_response *r, const char *line)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200183{
Harald Welte9bf7c532017-11-17 14:14:31 +0100184 if (sscanf(line, "m=audio %hu",
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200185 &r->audio_port) != 1)
186 goto response_parse_failure;
187
Philipp Maier10f32db2017-12-13 12:34:34 +0100188 if (r->audio_port == 0)
189 goto response_parse_failure;
190
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200191 return 0;
192
193response_parse_failure:
194 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier06da85e2017-10-05 18:49:24 +0200195 "Failed to parse MGCP response header (audio port)\n");
196 return -EINVAL;
197}
198
199/* Parse a line like "c=IN IP4 10.11.12.13" */
200static int mgcp_parse_audio_ip(struct mgcp_response *r, const char *line)
201{
202 struct in_addr ip_test;
203
204 if (strlen(line) < 16)
205 goto response_parse_failure;
206
207 /* The current implementation strictly supports IPV4 only ! */
208 if (memcmp("c=IN IP4 ", line, 9) != 0)
209 goto response_parse_failure;
210
211 /* Extract IP-Address */
Philipp Maierf8bfbe82017-11-23 19:32:31 +0100212 osmo_strlcpy(r->audio_ip, line + 9, sizeof(r->audio_ip));
Philipp Maier06da85e2017-10-05 18:49:24 +0200213
214 /* Check IP-Address */
215 if (inet_aton(r->audio_ip, &ip_test) == 0)
216 goto response_parse_failure;
217
218 return 0;
219
220response_parse_failure:
221 LOGP(DLMGCP, LOGL_ERROR,
222 "Failed to parse MGCP response header (audio ip)\n");
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200223 return -EINVAL;
224}
225
Philipp Maier3b12e1b2018-01-18 15:16:13 +0100226/* A new section is marked by a double line break, check a few more
227 * patterns as there may be variants */
228static char *mgcp_find_section_end(char *string)
229{
230 char *rc;
231
232 rc = strstr(string, "\n\n");
233 if (rc)
234 return rc;
235
236 rc = strstr(string, "\n\r\n\r");
237 if (rc)
238 return rc;
239
240 rc = strstr(string, "\r\n\r\n");
241 if (rc)
242 return rc;
243
244 return NULL;
245}
246
Philipp Maier275ac972018-01-20 00:58:16 +0100247/*! Parse body (SDP) parameters of the MGCP response
248 * \param[in,out] r Response data
249 * \returns 0 on success, -EINVAL on error. */
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200250int mgcp_response_parse_params(struct mgcp_response *r)
251{
252 char *line;
253 int rc;
254 OSMO_ASSERT(r->body);
Philipp Maier3b12e1b2018-01-18 15:16:13 +0100255 char *data = mgcp_find_section_end(r->body);
Philipp Maiere9d645b2018-01-19 23:54:08 +0100256 char *data_ptr;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200257
Philipp Maiere9d645b2018-01-19 23:54:08 +0100258 /* Since this functions performs a destructive parsing, we create a
259 * local copy of the body data */
260 data = talloc_zero_size(NULL, strlen(r->body)+1);
261 OSMO_ASSERT(data);
262 data_ptr = data;
263 osmo_strlcpy(data, r->body, strlen(r->body));
Philipp Maier55295f72018-01-15 14:00:28 +0100264
Philipp Maiere9d645b2018-01-19 23:54:08 +0100265 /* Find beginning of the parameter (SDP) section */
266 data_ptr = mgcp_find_section_end(data);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200267 if (!data) {
268 LOGP(DLMGCP, LOGL_ERROR,
269 "MGCP response: cannot find start of parameters\n");
Philipp Maiere9d645b2018-01-19 23:54:08 +0100270 rc = -EINVAL;
271 goto exit;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200272 }
273
Philipp Maiere9d645b2018-01-19 23:54:08 +0100274 for_each_non_empty_line(line, data_ptr) {
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200275 if (!mgcp_line_is_valid(line))
276 return -EINVAL;
277
278 switch (line[0]) {
279 case 'm':
Philipp Maier06da85e2017-10-05 18:49:24 +0200280 rc = mgcp_parse_audio_port(r, line);
281 if (rc)
Philipp Maiere9d645b2018-01-19 23:54:08 +0100282 goto exit;
Philipp Maier06da85e2017-10-05 18:49:24 +0200283 break;
284 case 'c':
285 rc = mgcp_parse_audio_ip(r, line);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200286 if (rc)
Philipp Maiere9d645b2018-01-19 23:54:08 +0100287 goto exit;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200288 break;
289 default:
290 /* skip unhandled parameters */
291 break;
292 }
293 }
Philipp Maiere9d645b2018-01-19 23:54:08 +0100294
295 rc = 0;
296exit:
297 talloc_free(data);
298 return rc;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200299}
300
Philipp Maier55295f72018-01-15 14:00:28 +0100301/* Parse a line like "X: something" */
302static int mgcp_parse_head_param(char *result, unsigned int result_len,
303 char label, const char *line)
Philipp Maierffd75e42017-11-22 11:44:50 +0100304{
Philipp Maier55295f72018-01-15 14:00:28 +0100305 char label_string[4];
306
307 /* Detect empty parameters */
Philipp Maierffd75e42017-11-22 11:44:50 +0100308 if (strlen(line) < 4)
309 goto response_parse_failure;
310
Philipp Maier55295f72018-01-15 14:00:28 +0100311 /* Check if the label matches */
312 snprintf(label_string, sizeof(label_string), "%c: ", label);
313 if (memcmp(label_string, line, 3) != 0)
Philipp Maierffd75e42017-11-22 11:44:50 +0100314 goto response_parse_failure;
315
Philipp Maier55295f72018-01-15 14:00:28 +0100316 /* Copy payload part of the string to destinations (the label string
317 * is always 3 chars long) */
318 osmo_strlcpy(result, line + 3, result_len);
Philipp Maierffd75e42017-11-22 11:44:50 +0100319 return 0;
320
321response_parse_failure:
322 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier55295f72018-01-15 14:00:28 +0100323 "Failed to parse MGCP response (parameter label: %c)\n", label);
Philipp Maierffd75e42017-11-22 11:44:50 +0100324 return -EINVAL;
325}
326
327/* Parse MGCP parameters of the response */
328static int parse_head_params(struct mgcp_response *r)
329{
330 char *line;
331 int rc = 0;
332 OSMO_ASSERT(r->body);
Philipp Maier55295f72018-01-15 14:00:28 +0100333 char *data;
334 char *data_ptr;
335 char *data_end;
Philipp Maierffd75e42017-11-22 11:44:50 +0100336
Philipp Maier55295f72018-01-15 14:00:28 +0100337 /* Since this functions performs a destructive parsing, we create a
338 * local copy of the body data */
339 data = talloc_zero_size(NULL, strlen(r->body)+1);
340 OSMO_ASSERT(data);
341 data_ptr = data;
342 osmo_strlcpy(data, r->body, strlen(r->body));
343
344 /* If there is an SDP body attached, prevent for_each_non_empty_line()
345 * into running in there, we are not yet interested in the parameters
346 * stored there. */
347 data_end = mgcp_find_section_end(data);
Philipp Maierffd75e42017-11-22 11:44:50 +0100348 if (data_end)
349 *data_end = '\0';
350
Philipp Maier55295f72018-01-15 14:00:28 +0100351 for_each_non_empty_line(line, data_ptr) {
Philipp Maierffd75e42017-11-22 11:44:50 +0100352 switch (line[0]) {
Philipp Maier55295f72018-01-15 14:00:28 +0100353 case 'Z':
354 rc = mgcp_parse_head_param(r->head.endpoint,
355 sizeof(r->head.endpoint),
356 'Z', line);
357 if (rc)
358 goto exit;
Philipp Maier771b26a2018-01-31 13:53:11 +0100359
360 /* A specific endpoint identifier returned by the MGW
361 * must not contain any wildcard characters */
362 if (strstr(r->head.endpoint, "*") != NULL) {
363 rc = -EINVAL;
364 goto exit;
365 }
Philipp Maier3261dc72018-01-31 14:03:13 +0100366
367 /* A specific endpoint identifier returned by the MGW
368 * must contain an @ character */
369 if (strstr(r->head.endpoint, "@") == NULL) {
370 rc = -EINVAL;
371 goto exit;
372 }
Philipp Maier55295f72018-01-15 14:00:28 +0100373 break;
Philipp Maierffd75e42017-11-22 11:44:50 +0100374 case 'I':
Philipp Maier55295f72018-01-15 14:00:28 +0100375 rc = mgcp_parse_head_param(r->head.conn_id,
376 sizeof(r->head.conn_id),
377 'I', line);
Philipp Maierffd75e42017-11-22 11:44:50 +0100378 if (rc)
379 goto exit;
380 break;
381 default:
382 /* skip unhandled parameters */
383 break;
384 }
385 }
386exit:
Philipp Maier55295f72018-01-15 14:00:28 +0100387 talloc_free(data);
Philipp Maierffd75e42017-11-22 11:44:50 +0100388 return rc;
389}
390
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200391static struct mgcp_response_pending *mgcp_client_response_pending_get(
392 struct mgcp_client *mgcp,
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100393 mgcp_trans_id_t trans_id)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200394{
395 struct mgcp_response_pending *pending;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200396 llist_for_each_entry(pending, &mgcp->responses_pending, entry) {
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100397 if (pending->trans_id == trans_id) {
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200398 llist_del(&pending->entry);
399 return pending;
400 }
401 }
402 return NULL;
403}
404
405/* Feed an MGCP message into the receive processing.
406 * Parse the head and call any callback registered for the transaction id found
407 * in the MGCP message. This is normally called directly from the internal
408 * mgcp_do_read that reads from the socket connected to the MGCP gateway. This
409 * function is published mainly to be able to feed data from the test suite.
410 */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200411int mgcp_client_rx(struct mgcp_client *mgcp, struct msgb *msg)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200412{
413 struct mgcp_response r = { 0 };
414 struct mgcp_response_pending *pending;
415 int rc;
416
417 rc = mgcp_response_parse_head(&r, msg);
418 if (rc) {
Philipp Maierffd75e42017-11-22 11:44:50 +0100419 LOGP(DLMGCP, LOGL_ERROR, "Cannot parse MGCP response (head)\n");
420 return -1;
421 }
422
423 rc = parse_head_params(&r);
424 if (rc) {
425 LOGP(DLMGCP, LOGL_ERROR, "Cannot parse MGCP response (head parameters)\n");
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200426 return -1;
427 }
428
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100429 pending = mgcp_client_response_pending_get(mgcp, r.head.trans_id);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200430 if (!pending) {
431 LOGP(DLMGCP, LOGL_ERROR,
432 "Cannot find matching MGCP transaction for trans_id %d\n",
433 r.head.trans_id);
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100434 return -ENOENT;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200435 }
436
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200437 mgcp_client_handle_response(mgcp, pending, &r);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200438 return 0;
439}
440
441static int mgcp_do_read(struct osmo_fd *fd)
442{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200443 struct mgcp_client *mgcp = fd->data;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200444 struct msgb *msg;
445 int ret;
446
447 msg = msgb_alloc_headroom(4096, 128, "mgcp_from_gw");
448 if (!msg) {
449 LOGP(DLMGCP, LOGL_ERROR, "Failed to allocate MGCP message.\n");
450 return -1;
451 }
452
453 ret = read(fd->fd, msg->data, 4096 - 128);
454 if (ret <= 0) {
455 LOGP(DLMGCP, LOGL_ERROR, "Failed to read: %d/%s\n", errno, strerror(errno));
456 msgb_free(msg);
457 return -1;
458 } else if (ret > 4096 - 128) {
459 LOGP(DLMGCP, LOGL_ERROR, "Too much data: %d\n", ret);
460 msgb_free(msg);
461 return -1;
Harald Welte9bf7c532017-11-17 14:14:31 +0100462 }
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200463
464 msg->l2h = msgb_put(msg, ret);
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200465 ret = mgcp_client_rx(mgcp, msg);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200466 talloc_free(msg);
467 return ret;
468}
469
470static int mgcp_do_write(struct osmo_fd *fd, struct msgb *msg)
471{
472 int ret;
473 static char strbuf[4096];
474 unsigned int l = msg->len < sizeof(strbuf) ? msg->len : sizeof(strbuf);
475 unsigned int i;
476
Philipp Maierf8bfbe82017-11-23 19:32:31 +0100477 osmo_strlcpy(strbuf, (const char*)msg->data, l);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200478 for (i = 0; i < sizeof(strbuf); i++) {
479 if (strbuf[i] == '\n' || strbuf[i] == '\r') {
480 strbuf[i] = '\0';
481 break;
482 }
483 }
484 DEBUGP(DLMGCP, "Tx MGCP msg to MGCP GW: '%s'\n", strbuf);
485
486 LOGP(DLMGCP, LOGL_DEBUG, "Sending msg to MGCP GW size: %u\n", msg->len);
487
488 ret = write(fd->fd, msg->data, msg->len);
489 if (ret != msg->len)
490 LOGP(DLMGCP, LOGL_ERROR, "Failed to forward message to MGCP"
491 " GW: %s\n", strerror(errno));
492
493 return ret;
494}
495
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200496struct mgcp_client *mgcp_client_init(void *ctx,
497 struct mgcp_client_conf *conf)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200498{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200499 struct mgcp_client *mgcp;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200500
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200501 mgcp = talloc_zero(ctx, struct mgcp_client);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200502
503 INIT_LLIST_HEAD(&mgcp->responses_pending);
504 INIT_LLIST_HEAD(&mgcp->inuse_endpoints);
505
506 mgcp->next_trans_id = 1;
507
508 mgcp->actual.local_addr = conf->local_addr ? conf->local_addr :
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200509 MGCP_CLIENT_LOCAL_ADDR_DEFAULT;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200510 mgcp->actual.local_port = conf->local_port >= 0 ? (uint16_t)conf->local_port :
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200511 MGCP_CLIENT_LOCAL_PORT_DEFAULT;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200512
513 mgcp->actual.remote_addr = conf->remote_addr ? conf->remote_addr :
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200514 MGCP_CLIENT_REMOTE_ADDR_DEFAULT;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200515 mgcp->actual.remote_port = conf->remote_port >= 0 ? (uint16_t)conf->remote_port :
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200516 MGCP_CLIENT_REMOTE_PORT_DEFAULT;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200517
518 mgcp->actual.first_endpoint = conf->first_endpoint > 0 ? (uint16_t)conf->first_endpoint : 0;
519 mgcp->actual.last_endpoint = conf->last_endpoint > 0 ? (uint16_t)conf->last_endpoint : 0;
Neels Hofmeyrc0dcc3c2017-12-02 18:31:34 +0000520 mgcp->actual.bts_base = conf->bts_base > 0 ? (uint16_t)conf->bts_base : 4000;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200521
522 return mgcp;
523}
524
Philipp Maier275ac972018-01-20 00:58:16 +0100525/*! Initalize client connection (opens socket only, no request is sent yet)
526 * \param[in,out] mgcp MGCP client descriptor.
527 * \returns 0 on success, -EINVAL on error. */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200528int mgcp_client_connect(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200529{
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200530 struct sockaddr_in addr;
531 struct osmo_wqueue *wq;
532 int rc;
533
534 if (!mgcp) {
535 LOGP(DLMGCP, LOGL_FATAL, "MGCPGW client not initialized properly\n");
536 return -EINVAL;
537 }
538
539 wq = &mgcp->wq;
540
Harald Welte8890dfa2017-11-17 15:09:30 +0100541 rc = osmo_sock_init2_ofd(&wq->bfd, AF_INET, SOCK_DGRAM, IPPROTO_UDP,
542 mgcp->actual.local_addr, mgcp->actual.local_port,
543 mgcp->actual.remote_addr, mgcp->actual.remote_port,
544 OSMO_SOCK_F_BIND | OSMO_SOCK_F_CONNECT);
545 if (rc < 0) {
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200546 LOGP(DLMGCP, LOGL_FATAL,
Harald Welte8890dfa2017-11-17 15:09:30 +0100547 "Failed to initialize socket %s:%u -> %s:%u for MGCP GW: %s\n",
548 mgcp->actual.local_addr, mgcp->actual.local_port,
549 mgcp->actual.remote_addr, mgcp->actual.remote_port, strerror(errno));
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200550 goto error_close_fd;
551 }
552
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200553 inet_aton(mgcp->actual.remote_addr, &addr.sin_addr);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200554 mgcp->remote_addr = htonl(addr.sin_addr.s_addr);
555
556 osmo_wqueue_init(wq, 10);
557 wq->bfd.when = BSC_FD_READ;
558 wq->bfd.data = mgcp;
559 wq->read_cb = mgcp_do_read;
560 wq->write_cb = mgcp_do_write;
561
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200562 LOGP(DLMGCP, LOGL_INFO, "MGCP GW connection: %s:%u -> %s:%u\n",
563 mgcp->actual.local_addr, mgcp->actual.local_port,
564 mgcp->actual.remote_addr, mgcp->actual.remote_port);
565
566 return 0;
567error_close_fd:
568 close(wq->bfd.fd);
569 wq->bfd.fd = -1;
570 return rc;
571}
572
Philipp Maier275ac972018-01-20 00:58:16 +0100573/*! Get the IP-Aaddress of the associated MGW as string.
574 * \param[in] mgcp MGCP client descriptor.
575 * \returns a pointer to the address string. */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200576const char *mgcp_client_remote_addr_str(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200577{
578 return mgcp->actual.remote_addr;
579}
580
Philipp Maier275ac972018-01-20 00:58:16 +0100581/*! Get the IP-Port of the associated MGW.
582 * \param[in] mgcp MGCP client descriptor.
583 * \returns port number. */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200584uint16_t mgcp_client_remote_port(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200585{
586 return mgcp->actual.remote_port;
587}
588
Philipp Maier275ac972018-01-20 00:58:16 +0100589/*! Get the IP-Aaddress of the associated MGW as its numeric representation.
590 * \param[in] mgcp MGCP client descriptor.
591 * \returns IP-Address as 32 bit integer (network byte order) */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200592uint32_t mgcp_client_remote_addr_n(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200593{
594 return mgcp->remote_addr;
595}
596
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200597struct mgcp_response_pending * mgcp_client_pending_add(
598 struct mgcp_client *mgcp,
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200599 mgcp_trans_id_t trans_id,
600 mgcp_response_cb_t response_cb,
601 void *priv)
602{
603 struct mgcp_response_pending *pending;
604
605 pending = talloc_zero(mgcp, struct mgcp_response_pending);
606 pending->trans_id = trans_id;
607 pending->response_cb = response_cb;
608 pending->priv = priv;
609 llist_add_tail(&pending->entry, &mgcp->responses_pending);
610
611 return pending;
612}
613
614/* Send the MGCP message in msg to the MGCP GW and handle a response with
615 * response_cb. NOTE: the response_cb still needs to call
616 * mgcp_response_parse_params(response) to get the parsed parameters -- to
617 * potentially save some CPU cycles, only the head line has been parsed when
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100618 * the response_cb is invoked.
619 * Before the priv pointer becomes invalid, e.g. due to transaction timeout,
620 * mgcp_client_cancel() needs to be called for this transaction.
621 */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200622int mgcp_client_tx(struct mgcp_client *mgcp, struct msgb *msg,
623 mgcp_response_cb_t response_cb, void *priv)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200624{
625 struct mgcp_response_pending *pending;
626 mgcp_trans_id_t trans_id;
627 int rc;
628
629 trans_id = msg->cb[MSGB_CB_MGCP_TRANS_ID];
630 if (!trans_id) {
631 LOGP(DLMGCP, LOGL_ERROR,
632 "Unset transaction id in mgcp send request\n");
633 talloc_free(msg);
634 return -EINVAL;
635 }
636
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200637 pending = mgcp_client_pending_add(mgcp, trans_id, response_cb, priv);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200638
639 if (msgb_l2len(msg) > 4096) {
640 LOGP(DLMGCP, LOGL_ERROR,
641 "Cannot send, MGCP message too large: %u\n",
642 msgb_l2len(msg));
643 msgb_free(msg);
644 rc = -EINVAL;
645 goto mgcp_tx_error;
646 }
647
648 rc = osmo_wqueue_enqueue(&mgcp->wq, msg);
649 if (rc) {
650 LOGP(DLMGCP, LOGL_FATAL, "Could not queue message to MGCP GW\n");
651 msgb_free(msg);
652 goto mgcp_tx_error;
653 } else
654 LOGP(DLMGCP, LOGL_INFO, "Queued %u bytes for MGCP GW\n",
655 msgb_l2len(msg));
656 return 0;
657
658mgcp_tx_error:
659 /* Pass NULL to response cb to indicate an error */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200660 mgcp_client_handle_response(mgcp, pending, NULL);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200661 return -1;
662}
663
Philipp Maier275ac972018-01-20 00:58:16 +0100664/*! Cancel a pending transaction.
665 * \param[in] mgcp MGCP client descriptor.
666 * \param[in,out] trans_id Transaction id.
667 * \returns 0 on success, -ENOENT on error.
668 *
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100669 * Should a priv pointer passed to mgcp_client_tx() become invalid, this function must be called. In
670 * practical terms, if the caller of mgcp_client_tx() wishes to tear down a transaction without having
671 * received a response this function must be called. The trans_id can be obtained by calling
Philipp Maier275ac972018-01-20 00:58:16 +0100672 * mgcp_msg_trans_id() on the msgb produced by mgcp_msg_gen(). */
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100673int mgcp_client_cancel(struct mgcp_client *mgcp, mgcp_trans_id_t trans_id)
674{
675 struct mgcp_response_pending *pending = mgcp_client_response_pending_get(mgcp, trans_id);
676 if (!pending) {
Philipp Maier275ac972018-01-20 00:58:16 +0100677 /*! Note: it is not harmful to cancel a transaction twice. */
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100678 LOGP(DLMGCP, LOGL_INFO, "Cannot cancel, no such transaction: %u\n", trans_id);
679 return -ENOENT;
680 }
681 LOGP(DLMGCP, LOGL_INFO, "Canceled transaction %u\n", trans_id);
682 talloc_free(pending);
683 return 0;
Philipp Maier275ac972018-01-20 00:58:16 +0100684 /*! We don't really need to clean up the wqueue: In all sane cases, the msgb has already been sent
685 * out and is no longer in the wqueue. If it still is in the wqueue, then sending MGCP messages
686 * per se is broken and the program should notice so by a full wqueue. Even if this was called
687 * before we had a chance to send out the message and it is still going to be sent, we will just
688 * ignore the reply to it later. Removing a msgb from the wqueue here would just introduce more
689 * bug surface in terms of failing to update wqueue API's counters or some such.
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100690 */
691}
692
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200693static struct msgb *mgcp_msg_from_buf(mgcp_trans_id_t trans_id,
694 const char *buf, int len)
695{
696 struct msgb *msg;
697
698 if (len > (4096 - 128)) {
699 LOGP(DLMGCP, LOGL_ERROR, "Cannot send to MGCP GW:"
700 " message too large: %d\n", len);
701 return NULL;
702 }
703
704 msg = msgb_alloc_headroom(4096, 128, "MGCP tx");
705 OSMO_ASSERT(msg);
706
707 char *dst = (char*)msgb_put(msg, len);
708 memcpy(dst, buf, len);
709 msg->l2h = msg->data;
710 msg->cb[MSGB_CB_MGCP_TRANS_ID] = trans_id;
711
712 return msg;
713}
714
715static struct msgb *mgcp_msg_from_str(mgcp_trans_id_t trans_id,
716 const char *fmt, ...)
717{
718 static char compose[4096 - 128];
719 va_list ap;
720 int len;
721 OSMO_ASSERT(fmt);
722
723 va_start(ap, fmt);
724 len = vsnprintf(compose, sizeof(compose), fmt, ap);
725 va_end(ap);
726 if (len >= sizeof(compose)) {
727 LOGP(DLMGCP, LOGL_ERROR,
728 "Message too large: trans_id=%u len=%d\n",
729 trans_id, len);
730 return NULL;
731 }
732 if (len < 1) {
733 LOGP(DLMGCP, LOGL_ERROR,
734 "Failed to compose message: trans_id=%u len=%d\n",
735 trans_id, len);
736 return NULL;
737 }
738 return mgcp_msg_from_buf(trans_id, compose, len);
739}
740
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200741static mgcp_trans_id_t mgcp_client_next_trans_id(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200742{
743 /* avoid zero trans_id to distinguish from unset trans_id */
744 if (!mgcp->next_trans_id)
745 mgcp->next_trans_id ++;
746 return mgcp->next_trans_id ++;
747}
748
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200749struct msgb *mgcp_msg_crcx(struct mgcp_client *mgcp,
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200750 uint16_t rtp_endpoint, unsigned int call_id,
751 enum mgcp_connection_mode mode)
752{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200753 mgcp_trans_id_t trans_id = mgcp_client_next_trans_id(mgcp);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200754 return mgcp_msg_from_str(trans_id,
755 "CRCX %u %x@mgw MGCP 1.0\r\n"
756 "C: %x\r\n"
757 "L: p:20, a:AMR, nt:IN\r\n"
758 "M: %s\r\n"
759 ,
760 trans_id,
761 rtp_endpoint,
762 call_id,
Neels Hofmeyrd95ab1e2017-09-22 00:52:54 +0200763 mgcp_client_cmode_name(mode));
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200764}
765
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200766struct msgb *mgcp_msg_mdcx(struct mgcp_client *mgcp,
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200767 uint16_t rtp_endpoint, const char *rtp_conn_addr,
768 uint16_t rtp_port, enum mgcp_connection_mode mode)
769
770{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200771 mgcp_trans_id_t trans_id = mgcp_client_next_trans_id(mgcp);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200772 return mgcp_msg_from_str(trans_id,
773 "MDCX %u %x@mgw MGCP 1.0\r\n"
774 "M: %s\r\n"
775 "\r\n"
776 "c=IN IP4 %s\r\n"
777 "m=audio %u RTP/AVP 255\r\n"
778 ,
779 trans_id,
780 rtp_endpoint,
Neels Hofmeyrd95ab1e2017-09-22 00:52:54 +0200781 mgcp_client_cmode_name(mode),
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200782 rtp_conn_addr,
783 rtp_port);
784}
785
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200786struct msgb *mgcp_msg_dlcx(struct mgcp_client *mgcp, uint16_t rtp_endpoint,
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200787 unsigned int call_id)
788{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200789 mgcp_trans_id_t trans_id = mgcp_client_next_trans_id(mgcp);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200790 return mgcp_msg_from_str(trans_id,
791 "DLCX %u %x@mgw MGCP 1.0\r\n"
792 "C: %x\r\n", trans_id, rtp_endpoint, call_id);
793}
794
Philipp Maier1dc6be62017-10-05 18:25:37 +0200795#define MGCP_CRCX_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT | \
796 MGCP_MSG_PRESENCE_CALL_ID | \
Philipp Maier1dc6be62017-10-05 18:25:37 +0200797 MGCP_MSG_PRESENCE_CONN_MODE)
798#define MGCP_MDCX_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT | \
Philipp Maier490cbaa2018-01-22 17:32:38 +0100799 MGCP_MSG_PRESENCE_CALL_ID | \
Philipp Maier1dc6be62017-10-05 18:25:37 +0200800 MGCP_MSG_PRESENCE_CONN_ID)
801#define MGCP_DLCX_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT)
802#define MGCP_AUEP_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT)
803#define MGCP_RSIP_MANDATORY 0 /* none */
804
Philipp Maier275ac972018-01-20 00:58:16 +0100805/*! Generate an MGCP message
806 * \param[in] mgcp MGCP client descriptor.
807 * \param[in] mgcp_msg Message description
808 * \returns message buffer on success, NULL on error. */
Philipp Maier1dc6be62017-10-05 18:25:37 +0200809struct msgb *mgcp_msg_gen(struct mgcp_client *mgcp, struct mgcp_msg *mgcp_msg)
810{
811 mgcp_trans_id_t trans_id = mgcp_client_next_trans_id(mgcp);
812 uint32_t mandatory_mask;
813 struct msgb *msg = msgb_alloc_headroom(4096, 128, "MGCP tx");
814 int rc = 0;
Philipp Maier9d25d7a2018-01-22 17:31:10 +0100815 char local_ip[INET_ADDRSTRLEN];
Philipp Maier1dc6be62017-10-05 18:25:37 +0200816
817 msg->l2h = msg->data;
818 msg->cb[MSGB_CB_MGCP_TRANS_ID] = trans_id;
819
820 /* Add command verb */
821 switch (mgcp_msg->verb) {
822 case MGCP_VERB_CRCX:
823 mandatory_mask = MGCP_CRCX_MANDATORY;
824 rc += msgb_printf(msg, "CRCX %u", trans_id);
825 break;
826 case MGCP_VERB_MDCX:
827 mandatory_mask = MGCP_MDCX_MANDATORY;
828 rc += msgb_printf(msg, "MDCX %u", trans_id);
829 break;
830 case MGCP_VERB_DLCX:
831 mandatory_mask = MGCP_DLCX_MANDATORY;
832 rc += msgb_printf(msg, "DLCX %u", trans_id);
833 break;
834 case MGCP_VERB_AUEP:
835 mandatory_mask = MGCP_AUEP_MANDATORY;
836 rc += msgb_printf(msg, "AUEP %u", trans_id);
837 break;
838 case MGCP_VERB_RSIP:
839 mandatory_mask = MGCP_RSIP_MANDATORY;
840 rc += msgb_printf(msg, "RSIP %u", trans_id);
841 break;
842 default:
843 LOGP(DLMGCP, LOGL_ERROR,
844 "Invalid command verb, can not generate MGCP message\n");
845 msgb_free(msg);
846 return NULL;
847 }
848
849 /* Check if mandatory fields are missing */
850 if (!((mgcp_msg->presence & mandatory_mask) == mandatory_mask)) {
851 LOGP(DLMGCP, LOGL_ERROR,
852 "One or more missing mandatory fields, can not generate MGCP message\n");
853 msgb_free(msg);
854 return NULL;
855 }
856
857 /* Add endpoint name */
Philipp Maier7bc55522017-12-10 22:52:22 +0100858 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_ENDPOINT) {
859 if (strlen(mgcp_msg->endpoint) <= 0) {
860 LOGP(DLMGCP, LOGL_ERROR,
861 "Empty endpoint name, can not generate MGCP message\n");
862 msgb_free(msg);
863 return NULL;
864 }
Philipp Maier1dc6be62017-10-05 18:25:37 +0200865 rc += msgb_printf(msg, " %s", mgcp_msg->endpoint);
Philipp Maier7bc55522017-12-10 22:52:22 +0100866 }
Philipp Maier1dc6be62017-10-05 18:25:37 +0200867
868 /* Add protocol version */
869 rc += msgb_printf(msg, " MGCP 1.0\r\n");
870
871 /* Add call id */
872 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_CALL_ID)
873 rc += msgb_printf(msg, "C: %x\r\n", mgcp_msg->call_id);
874
875 /* Add connection id */
Philipp Maier7bc55522017-12-10 22:52:22 +0100876 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_CONN_ID) {
877 if (strlen(mgcp_msg->conn_id) <= 0) {
878 LOGP(DLMGCP, LOGL_ERROR,
879 "Empty connection id, can not generate MGCP message\n");
880 msgb_free(msg);
881 return NULL;
882 }
Philipp Maier01d24a32017-11-21 17:26:09 +0100883 rc += msgb_printf(msg, "I: %s\r\n", mgcp_msg->conn_id);
Philipp Maier7bc55522017-12-10 22:52:22 +0100884 }
Philipp Maier1dc6be62017-10-05 18:25:37 +0200885
886 /* Add local connection options */
Philipp Maierffd75e42017-11-22 11:44:50 +0100887 if (mgcp_msg->verb == MGCP_VERB_CRCX)
Philipp Maier1dc6be62017-10-05 18:25:37 +0200888 rc += msgb_printf(msg, "L: p:20, a:AMR, nt:IN\r\n");
889
890 /* Add mode */
891 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_CONN_MODE)
892 rc +=
893 msgb_printf(msg, "M: %s\r\n",
894 mgcp_client_cmode_name(mgcp_msg->conn_mode));
895
Philipp Maier9d25d7a2018-01-22 17:31:10 +0100896 /* Add SDP body */
Philipp Maier1dc6be62017-10-05 18:25:37 +0200897 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_AUDIO_IP
898 && mgcp_msg->presence & MGCP_MSG_PRESENCE_AUDIO_PORT) {
Philipp Maier9d25d7a2018-01-22 17:31:10 +0100899
900 /* Add separator to mark the beginning of the SDP block */
901 rc += msgb_printf(msg, "\r\n");
902
903 /* Add SDP protocol version */
904 rc += msgb_printf(msg, "v=0\r\n");
905
Philipp Maier9d25d7a2018-01-22 17:31:10 +0100906 /* Determine local IP-Address */
907 if (osmo_sock_local_ip(local_ip, mgcp->actual.remote_addr) < 0) {
908 LOGP(DLMGCP, LOGL_ERROR,
909 "Could not determine local IP-Address!\n");
910 msgb_free(msg);
911 return NULL;
912 }
913
914 /* Add owner/creator (SDP) */
915 rc += msgb_printf(msg, "o=- %x 23 IN IP4 %s\r\n",
916 mgcp_msg->call_id, local_ip);
917
Philipp Maierb7594732018-01-31 16:44:00 +0100918 /* Add session name (none) */
919 rc += msgb_printf(msg, "s=-\r\n");
920
Philipp Maier9d25d7a2018-01-22 17:31:10 +0100921 /* Add RTP address and port */
Philipp Maier7bc55522017-12-10 22:52:22 +0100922 if (mgcp_msg->audio_port == 0) {
923 LOGP(DLMGCP, LOGL_ERROR,
924 "Invalid port number, can not generate MGCP message\n");
925 msgb_free(msg);
926 return NULL;
927 }
928 if (strlen(mgcp_msg->audio_ip) <= 0) {
929 LOGP(DLMGCP, LOGL_ERROR,
930 "Empty ip address, can not generate MGCP message\n");
931 msgb_free(msg);
932 return NULL;
933 }
Philipp Maier1dc6be62017-10-05 18:25:37 +0200934 rc += msgb_printf(msg, "c=IN IP4 %s\r\n", mgcp_msg->audio_ip);
Philipp Maier9d25d7a2018-01-22 17:31:10 +0100935
936 /* Add time description, active time (SDP) */
937 rc += msgb_printf(msg, "t=0 0\r\n");
Philipp Maierb7594732018-01-31 16:44:00 +0100938
939 rc +=
940 msgb_printf(msg, "m=audio %u RTP/AVP 255\r\n",
941 mgcp_msg->audio_port);
Philipp Maier1dc6be62017-10-05 18:25:37 +0200942 }
943
944 if (rc != 0) {
945 LOGP(DLMGCP, LOGL_ERROR,
946 "message buffer to small, can not generate MGCP message\n");
947 msgb_free(msg);
948 msg = NULL;
949 }
950
951 return msg;
952}
953
Philipp Maier275ac972018-01-20 00:58:16 +0100954/*! Retrieve the MGCP transaction ID from a msgb generated by mgcp_msg_gen()
955 * \param[in] msg message buffer
956 * \returns Transaction id. */
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100957mgcp_trans_id_t mgcp_msg_trans_id(struct msgb *msg)
958{
959 return (mgcp_trans_id_t)msg->cb[MSGB_CB_MGCP_TRANS_ID];
960}
961
Philipp Maier275ac972018-01-20 00:58:16 +0100962/*! Get the configuration parameters a given MGCP client instance
963 * \param[in] mgcp MGCP client descriptor.
964 * \returns configuration */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200965struct mgcp_client_conf *mgcp_client_conf_actual(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200966{
967 return &mgcp->actual;
968}
Neels Hofmeyrd95ab1e2017-09-22 00:52:54 +0200969
970const struct value_string mgcp_client_connection_mode_strs[] = {
971 { MGCP_CONN_NONE, "none" },
972 { MGCP_CONN_RECV_SEND, "sendrecv" },
973 { MGCP_CONN_SEND_ONLY, "sendonly" },
974 { MGCP_CONN_RECV_ONLY, "recvonly" },
975 { MGCP_CONN_LOOPBACK, "loopback" },
976 { 0, NULL }
977};