blob: c340303c3ccc7d457618c4466d7c15401522e1d3 [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
Harald Welte220f4872018-02-04 09:04:16 +01007 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
Neels Hofmeyre9920f22017-07-10 15:07:22 +02009 * (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
Harald Welte220f4872018-02-04 09:04:16 +010014 * GNU General Public License for more details.
Neels Hofmeyre9920f22017-07-10 15:07:22 +020015 *
Harald Welte220f4872018-02-04 09:04:16 +010016 * You should have received a copy of the GNU General Public License
Neels Hofmeyre9920f22017-07-10 15:07:22 +020017 * 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 */
Philipp Maier1fc69992018-01-31 15:32:55 +0100260 data = talloc_zero_size(r, strlen(r->body)+1);
Philipp Maiere9d645b2018-01-19 23:54:08 +0100261 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 */
Philipp Maier1fc69992018-01-31 15:32:55 +0100339 data = talloc_zero_size(r, strlen(r->body)+1);
Philipp Maier55295f72018-01-15 14:00:28 +0100340 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{
Philipp Maier1fc69992018-01-31 15:32:55 +0100413 struct mgcp_response *r;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200414 struct mgcp_response_pending *pending;
415 int rc;
416
Philipp Maier1fc69992018-01-31 15:32:55 +0100417 r = talloc_zero(mgcp, struct mgcp_response);
418 OSMO_ASSERT(r);
419
420 rc = mgcp_response_parse_head(r, msg);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200421 if (rc) {
Philipp Maierffd75e42017-11-22 11:44:50 +0100422 LOGP(DLMGCP, LOGL_ERROR, "Cannot parse MGCP response (head)\n");
Philipp Maier1fc69992018-01-31 15:32:55 +0100423 rc = 1;
424 goto error;
Philipp Maierffd75e42017-11-22 11:44:50 +0100425 }
426
Philipp Maier1fc69992018-01-31 15:32:55 +0100427 rc = parse_head_params(r);
Philipp Maierffd75e42017-11-22 11:44:50 +0100428 if (rc) {
429 LOGP(DLMGCP, LOGL_ERROR, "Cannot parse MGCP response (head parameters)\n");
Philipp Maier1fc69992018-01-31 15:32:55 +0100430 rc = 1;
431 goto error;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200432 }
433
Philipp Maier1fc69992018-01-31 15:32:55 +0100434 pending = mgcp_client_response_pending_get(mgcp, r->head.trans_id);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200435 if (!pending) {
436 LOGP(DLMGCP, LOGL_ERROR,
437 "Cannot find matching MGCP transaction for trans_id %d\n",
Philipp Maier1fc69992018-01-31 15:32:55 +0100438 r->head.trans_id);
439 rc = -ENOENT;
440 goto error;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200441 }
442
Philipp Maier1fc69992018-01-31 15:32:55 +0100443 mgcp_client_handle_response(mgcp, pending, r);
444 rc = 0;
445
446error:
447 talloc_free(r);
448 return rc;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200449}
450
451static int mgcp_do_read(struct osmo_fd *fd)
452{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200453 struct mgcp_client *mgcp = fd->data;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200454 struct msgb *msg;
455 int ret;
456
457 msg = msgb_alloc_headroom(4096, 128, "mgcp_from_gw");
458 if (!msg) {
459 LOGP(DLMGCP, LOGL_ERROR, "Failed to allocate MGCP message.\n");
460 return -1;
461 }
462
463 ret = read(fd->fd, msg->data, 4096 - 128);
464 if (ret <= 0) {
465 LOGP(DLMGCP, LOGL_ERROR, "Failed to read: %d/%s\n", errno, strerror(errno));
466 msgb_free(msg);
467 return -1;
468 } else if (ret > 4096 - 128) {
469 LOGP(DLMGCP, LOGL_ERROR, "Too much data: %d\n", ret);
470 msgb_free(msg);
471 return -1;
Harald Welte9bf7c532017-11-17 14:14:31 +0100472 }
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200473
474 msg->l2h = msgb_put(msg, ret);
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200475 ret = mgcp_client_rx(mgcp, msg);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200476 talloc_free(msg);
477 return ret;
478}
479
480static int mgcp_do_write(struct osmo_fd *fd, struct msgb *msg)
481{
482 int ret;
483 static char strbuf[4096];
484 unsigned int l = msg->len < sizeof(strbuf) ? msg->len : sizeof(strbuf);
485 unsigned int i;
486
Philipp Maierf8bfbe82017-11-23 19:32:31 +0100487 osmo_strlcpy(strbuf, (const char*)msg->data, l);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200488 for (i = 0; i < sizeof(strbuf); i++) {
489 if (strbuf[i] == '\n' || strbuf[i] == '\r') {
490 strbuf[i] = '\0';
491 break;
492 }
493 }
494 DEBUGP(DLMGCP, "Tx MGCP msg to MGCP GW: '%s'\n", strbuf);
495
496 LOGP(DLMGCP, LOGL_DEBUG, "Sending msg to MGCP GW size: %u\n", msg->len);
497
498 ret = write(fd->fd, msg->data, msg->len);
499 if (ret != msg->len)
500 LOGP(DLMGCP, LOGL_ERROR, "Failed to forward message to MGCP"
501 " GW: %s\n", strerror(errno));
502
503 return ret;
504}
505
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200506struct mgcp_client *mgcp_client_init(void *ctx,
507 struct mgcp_client_conf *conf)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200508{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200509 struct mgcp_client *mgcp;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200510
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200511 mgcp = talloc_zero(ctx, struct mgcp_client);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200512
513 INIT_LLIST_HEAD(&mgcp->responses_pending);
514 INIT_LLIST_HEAD(&mgcp->inuse_endpoints);
515
516 mgcp->next_trans_id = 1;
517
518 mgcp->actual.local_addr = conf->local_addr ? conf->local_addr :
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200519 MGCP_CLIENT_LOCAL_ADDR_DEFAULT;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200520 mgcp->actual.local_port = conf->local_port >= 0 ? (uint16_t)conf->local_port :
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200521 MGCP_CLIENT_LOCAL_PORT_DEFAULT;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200522
523 mgcp->actual.remote_addr = conf->remote_addr ? conf->remote_addr :
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200524 MGCP_CLIENT_REMOTE_ADDR_DEFAULT;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200525 mgcp->actual.remote_port = conf->remote_port >= 0 ? (uint16_t)conf->remote_port :
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200526 MGCP_CLIENT_REMOTE_PORT_DEFAULT;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200527
528 mgcp->actual.first_endpoint = conf->first_endpoint > 0 ? (uint16_t)conf->first_endpoint : 0;
529 mgcp->actual.last_endpoint = conf->last_endpoint > 0 ? (uint16_t)conf->last_endpoint : 0;
Neels Hofmeyrc0dcc3c2017-12-02 18:31:34 +0000530 mgcp->actual.bts_base = conf->bts_base > 0 ? (uint16_t)conf->bts_base : 4000;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200531
532 return mgcp;
533}
534
Philipp Maier275ac972018-01-20 00:58:16 +0100535/*! Initalize client connection (opens socket only, no request is sent yet)
536 * \param[in,out] mgcp MGCP client descriptor.
537 * \returns 0 on success, -EINVAL on error. */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200538int mgcp_client_connect(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200539{
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200540 struct sockaddr_in addr;
541 struct osmo_wqueue *wq;
542 int rc;
543
544 if (!mgcp) {
545 LOGP(DLMGCP, LOGL_FATAL, "MGCPGW client not initialized properly\n");
546 return -EINVAL;
547 }
548
549 wq = &mgcp->wq;
550
Harald Welte8890dfa2017-11-17 15:09:30 +0100551 rc = osmo_sock_init2_ofd(&wq->bfd, AF_INET, SOCK_DGRAM, IPPROTO_UDP,
552 mgcp->actual.local_addr, mgcp->actual.local_port,
553 mgcp->actual.remote_addr, mgcp->actual.remote_port,
554 OSMO_SOCK_F_BIND | OSMO_SOCK_F_CONNECT);
555 if (rc < 0) {
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200556 LOGP(DLMGCP, LOGL_FATAL,
Harald Welte8890dfa2017-11-17 15:09:30 +0100557 "Failed to initialize socket %s:%u -> %s:%u for MGCP GW: %s\n",
558 mgcp->actual.local_addr, mgcp->actual.local_port,
559 mgcp->actual.remote_addr, mgcp->actual.remote_port, strerror(errno));
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200560 goto error_close_fd;
561 }
562
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200563 inet_aton(mgcp->actual.remote_addr, &addr.sin_addr);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200564 mgcp->remote_addr = htonl(addr.sin_addr.s_addr);
565
566 osmo_wqueue_init(wq, 10);
567 wq->bfd.when = BSC_FD_READ;
568 wq->bfd.data = mgcp;
569 wq->read_cb = mgcp_do_read;
570 wq->write_cb = mgcp_do_write;
571
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200572 LOGP(DLMGCP, LOGL_INFO, "MGCP GW connection: %s:%u -> %s:%u\n",
573 mgcp->actual.local_addr, mgcp->actual.local_port,
574 mgcp->actual.remote_addr, mgcp->actual.remote_port);
575
576 return 0;
577error_close_fd:
578 close(wq->bfd.fd);
579 wq->bfd.fd = -1;
580 return rc;
581}
582
Philipp Maier275ac972018-01-20 00:58:16 +0100583/*! Get the IP-Aaddress of the associated MGW as string.
584 * \param[in] mgcp MGCP client descriptor.
585 * \returns a pointer to the address string. */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200586const char *mgcp_client_remote_addr_str(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200587{
588 return mgcp->actual.remote_addr;
589}
590
Philipp Maier275ac972018-01-20 00:58:16 +0100591/*! Get the IP-Port of the associated MGW.
592 * \param[in] mgcp MGCP client descriptor.
593 * \returns port number. */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200594uint16_t mgcp_client_remote_port(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200595{
596 return mgcp->actual.remote_port;
597}
598
Philipp Maier275ac972018-01-20 00:58:16 +0100599/*! Get the IP-Aaddress of the associated MGW as its numeric representation.
600 * \param[in] mgcp MGCP client descriptor.
601 * \returns IP-Address as 32 bit integer (network byte order) */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200602uint32_t mgcp_client_remote_addr_n(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200603{
604 return mgcp->remote_addr;
605}
606
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200607struct mgcp_response_pending * mgcp_client_pending_add(
608 struct mgcp_client *mgcp,
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200609 mgcp_trans_id_t trans_id,
610 mgcp_response_cb_t response_cb,
611 void *priv)
612{
613 struct mgcp_response_pending *pending;
614
615 pending = talloc_zero(mgcp, struct mgcp_response_pending);
616 pending->trans_id = trans_id;
617 pending->response_cb = response_cb;
618 pending->priv = priv;
619 llist_add_tail(&pending->entry, &mgcp->responses_pending);
620
621 return pending;
622}
623
624/* Send the MGCP message in msg to the MGCP GW and handle a response with
625 * response_cb. NOTE: the response_cb still needs to call
626 * mgcp_response_parse_params(response) to get the parsed parameters -- to
627 * potentially save some CPU cycles, only the head line has been parsed when
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100628 * the response_cb is invoked.
629 * Before the priv pointer becomes invalid, e.g. due to transaction timeout,
630 * mgcp_client_cancel() needs to be called for this transaction.
631 */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200632int mgcp_client_tx(struct mgcp_client *mgcp, struct msgb *msg,
633 mgcp_response_cb_t response_cb, void *priv)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200634{
635 struct mgcp_response_pending *pending;
636 mgcp_trans_id_t trans_id;
637 int rc;
638
639 trans_id = msg->cb[MSGB_CB_MGCP_TRANS_ID];
640 if (!trans_id) {
641 LOGP(DLMGCP, LOGL_ERROR,
642 "Unset transaction id in mgcp send request\n");
643 talloc_free(msg);
644 return -EINVAL;
645 }
646
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200647 pending = mgcp_client_pending_add(mgcp, trans_id, response_cb, priv);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200648
649 if (msgb_l2len(msg) > 4096) {
650 LOGP(DLMGCP, LOGL_ERROR,
651 "Cannot send, MGCP message too large: %u\n",
652 msgb_l2len(msg));
653 msgb_free(msg);
654 rc = -EINVAL;
655 goto mgcp_tx_error;
656 }
657
658 rc = osmo_wqueue_enqueue(&mgcp->wq, msg);
659 if (rc) {
660 LOGP(DLMGCP, LOGL_FATAL, "Could not queue message to MGCP GW\n");
661 msgb_free(msg);
662 goto mgcp_tx_error;
663 } else
664 LOGP(DLMGCP, LOGL_INFO, "Queued %u bytes for MGCP GW\n",
665 msgb_l2len(msg));
666 return 0;
667
668mgcp_tx_error:
669 /* Pass NULL to response cb to indicate an error */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200670 mgcp_client_handle_response(mgcp, pending, NULL);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200671 return -1;
672}
673
Philipp Maier275ac972018-01-20 00:58:16 +0100674/*! Cancel a pending transaction.
675 * \param[in] mgcp MGCP client descriptor.
676 * \param[in,out] trans_id Transaction id.
677 * \returns 0 on success, -ENOENT on error.
678 *
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100679 * Should a priv pointer passed to mgcp_client_tx() become invalid, this function must be called. In
680 * practical terms, if the caller of mgcp_client_tx() wishes to tear down a transaction without having
681 * received a response this function must be called. The trans_id can be obtained by calling
Philipp Maier275ac972018-01-20 00:58:16 +0100682 * mgcp_msg_trans_id() on the msgb produced by mgcp_msg_gen(). */
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100683int mgcp_client_cancel(struct mgcp_client *mgcp, mgcp_trans_id_t trans_id)
684{
685 struct mgcp_response_pending *pending = mgcp_client_response_pending_get(mgcp, trans_id);
686 if (!pending) {
Philipp Maier275ac972018-01-20 00:58:16 +0100687 /*! Note: it is not harmful to cancel a transaction twice. */
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100688 LOGP(DLMGCP, LOGL_INFO, "Cannot cancel, no such transaction: %u\n", trans_id);
689 return -ENOENT;
690 }
691 LOGP(DLMGCP, LOGL_INFO, "Canceled transaction %u\n", trans_id);
692 talloc_free(pending);
693 return 0;
Philipp Maier275ac972018-01-20 00:58:16 +0100694 /*! We don't really need to clean up the wqueue: In all sane cases, the msgb has already been sent
695 * out and is no longer in the wqueue. If it still is in the wqueue, then sending MGCP messages
696 * per se is broken and the program should notice so by a full wqueue. Even if this was called
697 * before we had a chance to send out the message and it is still going to be sent, we will just
698 * ignore the reply to it later. Removing a msgb from the wqueue here would just introduce more
699 * bug surface in terms of failing to update wqueue API's counters or some such.
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100700 */
701}
702
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200703static struct msgb *mgcp_msg_from_buf(mgcp_trans_id_t trans_id,
704 const char *buf, int len)
705{
706 struct msgb *msg;
707
708 if (len > (4096 - 128)) {
709 LOGP(DLMGCP, LOGL_ERROR, "Cannot send to MGCP GW:"
710 " message too large: %d\n", len);
711 return NULL;
712 }
713
714 msg = msgb_alloc_headroom(4096, 128, "MGCP tx");
715 OSMO_ASSERT(msg);
716
717 char *dst = (char*)msgb_put(msg, len);
718 memcpy(dst, buf, len);
719 msg->l2h = msg->data;
720 msg->cb[MSGB_CB_MGCP_TRANS_ID] = trans_id;
721
722 return msg;
723}
724
725static struct msgb *mgcp_msg_from_str(mgcp_trans_id_t trans_id,
726 const char *fmt, ...)
727{
728 static char compose[4096 - 128];
729 va_list ap;
730 int len;
731 OSMO_ASSERT(fmt);
732
733 va_start(ap, fmt);
734 len = vsnprintf(compose, sizeof(compose), fmt, ap);
735 va_end(ap);
736 if (len >= sizeof(compose)) {
737 LOGP(DLMGCP, LOGL_ERROR,
738 "Message too large: trans_id=%u len=%d\n",
739 trans_id, len);
740 return NULL;
741 }
742 if (len < 1) {
743 LOGP(DLMGCP, LOGL_ERROR,
744 "Failed to compose message: trans_id=%u len=%d\n",
745 trans_id, len);
746 return NULL;
747 }
748 return mgcp_msg_from_buf(trans_id, compose, len);
749}
750
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200751static mgcp_trans_id_t mgcp_client_next_trans_id(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200752{
753 /* avoid zero trans_id to distinguish from unset trans_id */
754 if (!mgcp->next_trans_id)
755 mgcp->next_trans_id ++;
756 return mgcp->next_trans_id ++;
757}
758
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200759struct msgb *mgcp_msg_crcx(struct mgcp_client *mgcp,
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200760 uint16_t rtp_endpoint, unsigned int call_id,
761 enum mgcp_connection_mode mode)
762{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200763 mgcp_trans_id_t trans_id = mgcp_client_next_trans_id(mgcp);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200764 return mgcp_msg_from_str(trans_id,
765 "CRCX %u %x@mgw MGCP 1.0\r\n"
766 "C: %x\r\n"
767 "L: p:20, a:AMR, nt:IN\r\n"
768 "M: %s\r\n"
769 ,
770 trans_id,
771 rtp_endpoint,
772 call_id,
Neels Hofmeyrd95ab1e2017-09-22 00:52:54 +0200773 mgcp_client_cmode_name(mode));
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200774}
775
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200776struct msgb *mgcp_msg_mdcx(struct mgcp_client *mgcp,
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200777 uint16_t rtp_endpoint, const char *rtp_conn_addr,
778 uint16_t rtp_port, enum mgcp_connection_mode mode)
779
780{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200781 mgcp_trans_id_t trans_id = mgcp_client_next_trans_id(mgcp);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200782 return mgcp_msg_from_str(trans_id,
783 "MDCX %u %x@mgw MGCP 1.0\r\n"
784 "M: %s\r\n"
785 "\r\n"
786 "c=IN IP4 %s\r\n"
787 "m=audio %u RTP/AVP 255\r\n"
788 ,
789 trans_id,
790 rtp_endpoint,
Neels Hofmeyrd95ab1e2017-09-22 00:52:54 +0200791 mgcp_client_cmode_name(mode),
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200792 rtp_conn_addr,
793 rtp_port);
794}
795
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200796struct msgb *mgcp_msg_dlcx(struct mgcp_client *mgcp, uint16_t rtp_endpoint,
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200797 unsigned int call_id)
798{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200799 mgcp_trans_id_t trans_id = mgcp_client_next_trans_id(mgcp);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200800 return mgcp_msg_from_str(trans_id,
801 "DLCX %u %x@mgw MGCP 1.0\r\n"
802 "C: %x\r\n", trans_id, rtp_endpoint, call_id);
803}
804
Philipp Maier1dc6be62017-10-05 18:25:37 +0200805#define MGCP_CRCX_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT | \
806 MGCP_MSG_PRESENCE_CALL_ID | \
Philipp Maier1dc6be62017-10-05 18:25:37 +0200807 MGCP_MSG_PRESENCE_CONN_MODE)
808#define MGCP_MDCX_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT | \
Philipp Maier490cbaa2018-01-22 17:32:38 +0100809 MGCP_MSG_PRESENCE_CALL_ID | \
Philipp Maier1dc6be62017-10-05 18:25:37 +0200810 MGCP_MSG_PRESENCE_CONN_ID)
811#define MGCP_DLCX_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT)
812#define MGCP_AUEP_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT)
813#define MGCP_RSIP_MANDATORY 0 /* none */
814
Philipp Maier275ac972018-01-20 00:58:16 +0100815/*! Generate an MGCP message
816 * \param[in] mgcp MGCP client descriptor.
817 * \param[in] mgcp_msg Message description
818 * \returns message buffer on success, NULL on error. */
Philipp Maier1dc6be62017-10-05 18:25:37 +0200819struct msgb *mgcp_msg_gen(struct mgcp_client *mgcp, struct mgcp_msg *mgcp_msg)
820{
821 mgcp_trans_id_t trans_id = mgcp_client_next_trans_id(mgcp);
822 uint32_t mandatory_mask;
823 struct msgb *msg = msgb_alloc_headroom(4096, 128, "MGCP tx");
824 int rc = 0;
Philipp Maier9d25d7a2018-01-22 17:31:10 +0100825 char local_ip[INET_ADDRSTRLEN];
Philipp Maier1dc6be62017-10-05 18:25:37 +0200826
827 msg->l2h = msg->data;
828 msg->cb[MSGB_CB_MGCP_TRANS_ID] = trans_id;
829
830 /* Add command verb */
831 switch (mgcp_msg->verb) {
832 case MGCP_VERB_CRCX:
833 mandatory_mask = MGCP_CRCX_MANDATORY;
834 rc += msgb_printf(msg, "CRCX %u", trans_id);
835 break;
836 case MGCP_VERB_MDCX:
837 mandatory_mask = MGCP_MDCX_MANDATORY;
838 rc += msgb_printf(msg, "MDCX %u", trans_id);
839 break;
840 case MGCP_VERB_DLCX:
841 mandatory_mask = MGCP_DLCX_MANDATORY;
842 rc += msgb_printf(msg, "DLCX %u", trans_id);
843 break;
844 case MGCP_VERB_AUEP:
845 mandatory_mask = MGCP_AUEP_MANDATORY;
846 rc += msgb_printf(msg, "AUEP %u", trans_id);
847 break;
848 case MGCP_VERB_RSIP:
849 mandatory_mask = MGCP_RSIP_MANDATORY;
850 rc += msgb_printf(msg, "RSIP %u", trans_id);
851 break;
852 default:
853 LOGP(DLMGCP, LOGL_ERROR,
854 "Invalid command verb, can not generate MGCP message\n");
855 msgb_free(msg);
856 return NULL;
857 }
858
859 /* Check if mandatory fields are missing */
860 if (!((mgcp_msg->presence & mandatory_mask) == mandatory_mask)) {
861 LOGP(DLMGCP, LOGL_ERROR,
862 "One or more missing mandatory fields, can not generate MGCP message\n");
863 msgb_free(msg);
864 return NULL;
865 }
866
867 /* Add endpoint name */
Philipp Maier7bc55522017-12-10 22:52:22 +0100868 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_ENDPOINT) {
869 if (strlen(mgcp_msg->endpoint) <= 0) {
870 LOGP(DLMGCP, LOGL_ERROR,
871 "Empty endpoint name, can not generate MGCP message\n");
872 msgb_free(msg);
873 return NULL;
874 }
Philipp Maier3aa81572018-01-31 14:13:45 +0100875
876 if (strstr(mgcp_msg->endpoint, "@") == NULL) {
877 LOGP(DLMGCP, LOGL_ERROR,
878 "Endpoint name (%s) lacks separator (@), can not generate MGCP message\n",
879 mgcp_msg->endpoint);
880 msgb_free(msg);
881 }
882
Philipp Maier1dc6be62017-10-05 18:25:37 +0200883 rc += msgb_printf(msg, " %s", mgcp_msg->endpoint);
Philipp Maier7bc55522017-12-10 22:52:22 +0100884 }
Philipp Maier1dc6be62017-10-05 18:25:37 +0200885
886 /* Add protocol version */
887 rc += msgb_printf(msg, " MGCP 1.0\r\n");
888
889 /* Add call id */
890 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_CALL_ID)
891 rc += msgb_printf(msg, "C: %x\r\n", mgcp_msg->call_id);
892
893 /* Add connection id */
Philipp Maier7bc55522017-12-10 22:52:22 +0100894 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_CONN_ID) {
895 if (strlen(mgcp_msg->conn_id) <= 0) {
896 LOGP(DLMGCP, LOGL_ERROR,
897 "Empty connection id, can not generate MGCP message\n");
898 msgb_free(msg);
899 return NULL;
900 }
Philipp Maier01d24a32017-11-21 17:26:09 +0100901 rc += msgb_printf(msg, "I: %s\r\n", mgcp_msg->conn_id);
Philipp Maier7bc55522017-12-10 22:52:22 +0100902 }
Philipp Maier1dc6be62017-10-05 18:25:37 +0200903
904 /* Add local connection options */
Philipp Maierffd75e42017-11-22 11:44:50 +0100905 if (mgcp_msg->verb == MGCP_VERB_CRCX)
Philipp Maier1dc6be62017-10-05 18:25:37 +0200906 rc += msgb_printf(msg, "L: p:20, a:AMR, nt:IN\r\n");
907
908 /* Add mode */
909 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_CONN_MODE)
910 rc +=
911 msgb_printf(msg, "M: %s\r\n",
912 mgcp_client_cmode_name(mgcp_msg->conn_mode));
913
Philipp Maier9d25d7a2018-01-22 17:31:10 +0100914 /* Add SDP body */
Philipp Maier1dc6be62017-10-05 18:25:37 +0200915 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_AUDIO_IP
916 && mgcp_msg->presence & MGCP_MSG_PRESENCE_AUDIO_PORT) {
Philipp Maier9d25d7a2018-01-22 17:31:10 +0100917
918 /* Add separator to mark the beginning of the SDP block */
919 rc += msgb_printf(msg, "\r\n");
920
921 /* Add SDP protocol version */
922 rc += msgb_printf(msg, "v=0\r\n");
923
Philipp Maier9d25d7a2018-01-22 17:31:10 +0100924 /* Determine local IP-Address */
925 if (osmo_sock_local_ip(local_ip, mgcp->actual.remote_addr) < 0) {
926 LOGP(DLMGCP, LOGL_ERROR,
927 "Could not determine local IP-Address!\n");
928 msgb_free(msg);
929 return NULL;
930 }
931
932 /* Add owner/creator (SDP) */
933 rc += msgb_printf(msg, "o=- %x 23 IN IP4 %s\r\n",
934 mgcp_msg->call_id, local_ip);
935
Philipp Maierb7594732018-01-31 16:44:00 +0100936 /* Add session name (none) */
937 rc += msgb_printf(msg, "s=-\r\n");
938
Philipp Maier9d25d7a2018-01-22 17:31:10 +0100939 /* Add RTP address and port */
Philipp Maier7bc55522017-12-10 22:52:22 +0100940 if (mgcp_msg->audio_port == 0) {
941 LOGP(DLMGCP, LOGL_ERROR,
942 "Invalid port number, can not generate MGCP message\n");
943 msgb_free(msg);
944 return NULL;
945 }
946 if (strlen(mgcp_msg->audio_ip) <= 0) {
947 LOGP(DLMGCP, LOGL_ERROR,
948 "Empty ip address, can not generate MGCP message\n");
949 msgb_free(msg);
950 return NULL;
951 }
Philipp Maier1dc6be62017-10-05 18:25:37 +0200952 rc += msgb_printf(msg, "c=IN IP4 %s\r\n", mgcp_msg->audio_ip);
Philipp Maier9d25d7a2018-01-22 17:31:10 +0100953
954 /* Add time description, active time (SDP) */
955 rc += msgb_printf(msg, "t=0 0\r\n");
Philipp Maierb7594732018-01-31 16:44:00 +0100956
957 rc +=
958 msgb_printf(msg, "m=audio %u RTP/AVP 255\r\n",
959 mgcp_msg->audio_port);
Philipp Maier1dc6be62017-10-05 18:25:37 +0200960 }
961
962 if (rc != 0) {
963 LOGP(DLMGCP, LOGL_ERROR,
964 "message buffer to small, can not generate MGCP message\n");
965 msgb_free(msg);
966 msg = NULL;
967 }
968
969 return msg;
970}
971
Philipp Maier275ac972018-01-20 00:58:16 +0100972/*! Retrieve the MGCP transaction ID from a msgb generated by mgcp_msg_gen()
973 * \param[in] msg message buffer
974 * \returns Transaction id. */
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100975mgcp_trans_id_t mgcp_msg_trans_id(struct msgb *msg)
976{
977 return (mgcp_trans_id_t)msg->cb[MSGB_CB_MGCP_TRANS_ID];
978}
979
Philipp Maier275ac972018-01-20 00:58:16 +0100980/*! Get the configuration parameters a given MGCP client instance
981 * \param[in] mgcp MGCP client descriptor.
982 * \returns configuration */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200983struct mgcp_client_conf *mgcp_client_conf_actual(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200984{
985 return &mgcp->actual;
986}
Neels Hofmeyrd95ab1e2017-09-22 00:52:54 +0200987
988const struct value_string mgcp_client_connection_mode_strs[] = {
989 { MGCP_CONN_NONE, "none" },
990 { MGCP_CONN_RECV_SEND, "sendrecv" },
991 { MGCP_CONN_SEND_ONLY, "sendonly" },
992 { MGCP_CONN_RECV_ONLY, "recvonly" },
993 { MGCP_CONN_LOOPBACK, "loopback" },
994 { 0, NULL }
995};