blob: de940c050368244bf00e706a4a34aa10e96d038e [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;
Neels Hofmeyr0793d2f2018-02-21 14:55:34 +0100254 char *data;
Philipp Maiere9d645b2018-01-19 23:54:08 +0100255 char *data_ptr;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200256
Philipp Maiere9d645b2018-01-19 23:54:08 +0100257 /* Since this functions performs a destructive parsing, we create a
258 * local copy of the body data */
Neels Hofmeyr0793d2f2018-02-21 14:55:34 +0100259 OSMO_ASSERT(r->body);
260 data = talloc_strdup(r, r->body);
Philipp Maiere9d645b2018-01-19 23:54:08 +0100261 OSMO_ASSERT(data);
Philipp Maier55295f72018-01-15 14:00:28 +0100262
Philipp Maiere9d645b2018-01-19 23:54:08 +0100263 /* Find beginning of the parameter (SDP) section */
264 data_ptr = mgcp_find_section_end(data);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200265 if (!data) {
266 LOGP(DLMGCP, LOGL_ERROR,
Neels Hofmeyr0793d2f2018-02-21 14:55:34 +0100267 "MGCP response: cannot find start of SDP parameters\n");
Philipp Maiere9d645b2018-01-19 23:54:08 +0100268 rc = -EINVAL;
269 goto exit;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200270 }
271
Neels Hofmeyr0793d2f2018-02-21 14:55:34 +0100272 /* data_ptr now points to the beginning of the section-end-marker; for_each_non_empty_line()
273 * skips any \r and \n characters for free, so we don't need to skip the marker. */
274
Philipp Maiere9d645b2018-01-19 23:54:08 +0100275 for_each_non_empty_line(line, data_ptr) {
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200276 if (!mgcp_line_is_valid(line))
277 return -EINVAL;
278
279 switch (line[0]) {
280 case 'm':
Philipp Maier06da85e2017-10-05 18:49:24 +0200281 rc = mgcp_parse_audio_port(r, line);
282 if (rc)
Philipp Maiere9d645b2018-01-19 23:54:08 +0100283 goto exit;
Philipp Maier06da85e2017-10-05 18:49:24 +0200284 break;
285 case 'c':
286 rc = mgcp_parse_audio_ip(r, line);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200287 if (rc)
Philipp Maiere9d645b2018-01-19 23:54:08 +0100288 goto exit;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200289 break;
290 default:
291 /* skip unhandled parameters */
292 break;
293 }
294 }
Philipp Maiere9d645b2018-01-19 23:54:08 +0100295
296 rc = 0;
297exit:
298 talloc_free(data);
299 return rc;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200300}
301
Philipp Maier55295f72018-01-15 14:00:28 +0100302/* Parse a line like "X: something" */
303static int mgcp_parse_head_param(char *result, unsigned int result_len,
304 char label, const char *line)
Philipp Maierffd75e42017-11-22 11:44:50 +0100305{
Philipp Maier55295f72018-01-15 14:00:28 +0100306 char label_string[4];
307
308 /* Detect empty parameters */
Philipp Maierffd75e42017-11-22 11:44:50 +0100309 if (strlen(line) < 4)
310 goto response_parse_failure;
311
Philipp Maier55295f72018-01-15 14:00:28 +0100312 /* Check if the label matches */
313 snprintf(label_string, sizeof(label_string), "%c: ", label);
314 if (memcmp(label_string, line, 3) != 0)
Philipp Maierffd75e42017-11-22 11:44:50 +0100315 goto response_parse_failure;
316
Philipp Maier55295f72018-01-15 14:00:28 +0100317 /* Copy payload part of the string to destinations (the label string
318 * is always 3 chars long) */
319 osmo_strlcpy(result, line + 3, result_len);
Philipp Maierffd75e42017-11-22 11:44:50 +0100320 return 0;
321
322response_parse_failure:
323 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier55295f72018-01-15 14:00:28 +0100324 "Failed to parse MGCP response (parameter label: %c)\n", label);
Philipp Maierffd75e42017-11-22 11:44:50 +0100325 return -EINVAL;
326}
327
328/* Parse MGCP parameters of the response */
329static int parse_head_params(struct mgcp_response *r)
330{
331 char *line;
332 int rc = 0;
333 OSMO_ASSERT(r->body);
Philipp Maier55295f72018-01-15 14:00:28 +0100334 char *data;
335 char *data_ptr;
336 char *data_end;
Philipp Maierffd75e42017-11-22 11:44:50 +0100337
Philipp Maier55295f72018-01-15 14:00:28 +0100338 /* Since this functions performs a destructive parsing, we create a
339 * local copy of the body data */
Philipp Maier1fc69992018-01-31 15:32:55 +0100340 data = talloc_zero_size(r, strlen(r->body)+1);
Philipp Maier55295f72018-01-15 14:00:28 +0100341 OSMO_ASSERT(data);
342 data_ptr = data;
343 osmo_strlcpy(data, r->body, strlen(r->body));
344
345 /* If there is an SDP body attached, prevent for_each_non_empty_line()
346 * into running in there, we are not yet interested in the parameters
347 * stored there. */
348 data_end = mgcp_find_section_end(data);
Philipp Maierffd75e42017-11-22 11:44:50 +0100349 if (data_end)
350 *data_end = '\0';
351
Philipp Maier55295f72018-01-15 14:00:28 +0100352 for_each_non_empty_line(line, data_ptr) {
Philipp Maierffd75e42017-11-22 11:44:50 +0100353 switch (line[0]) {
Philipp Maier55295f72018-01-15 14:00:28 +0100354 case 'Z':
355 rc = mgcp_parse_head_param(r->head.endpoint,
356 sizeof(r->head.endpoint),
357 'Z', line);
358 if (rc)
359 goto exit;
Philipp Maier771b26a2018-01-31 13:53:11 +0100360
361 /* A specific endpoint identifier returned by the MGW
362 * must not contain any wildcard characters */
363 if (strstr(r->head.endpoint, "*") != NULL) {
364 rc = -EINVAL;
365 goto exit;
366 }
Philipp Maier3261dc72018-01-31 14:03:13 +0100367
368 /* A specific endpoint identifier returned by the MGW
369 * must contain an @ character */
370 if (strstr(r->head.endpoint, "@") == NULL) {
371 rc = -EINVAL;
372 goto exit;
373 }
Philipp Maier55295f72018-01-15 14:00:28 +0100374 break;
Philipp Maierffd75e42017-11-22 11:44:50 +0100375 case 'I':
Philipp Maier55295f72018-01-15 14:00:28 +0100376 rc = mgcp_parse_head_param(r->head.conn_id,
377 sizeof(r->head.conn_id),
378 'I', line);
Philipp Maierffd75e42017-11-22 11:44:50 +0100379 if (rc)
380 goto exit;
381 break;
382 default:
383 /* skip unhandled parameters */
384 break;
385 }
386 }
387exit:
Philipp Maier55295f72018-01-15 14:00:28 +0100388 talloc_free(data);
Philipp Maierffd75e42017-11-22 11:44:50 +0100389 return rc;
390}
391
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200392static struct mgcp_response_pending *mgcp_client_response_pending_get(
393 struct mgcp_client *mgcp,
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100394 mgcp_trans_id_t trans_id)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200395{
396 struct mgcp_response_pending *pending;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200397 llist_for_each_entry(pending, &mgcp->responses_pending, entry) {
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100398 if (pending->trans_id == trans_id) {
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200399 llist_del(&pending->entry);
400 return pending;
401 }
402 }
403 return NULL;
404}
405
406/* Feed an MGCP message into the receive processing.
407 * Parse the head and call any callback registered for the transaction id found
408 * in the MGCP message. This is normally called directly from the internal
409 * mgcp_do_read that reads from the socket connected to the MGCP gateway. This
410 * function is published mainly to be able to feed data from the test suite.
411 */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200412int mgcp_client_rx(struct mgcp_client *mgcp, struct msgb *msg)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200413{
Philipp Maier1fc69992018-01-31 15:32:55 +0100414 struct mgcp_response *r;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200415 struct mgcp_response_pending *pending;
416 int rc;
417
Philipp Maier1fc69992018-01-31 15:32:55 +0100418 r = talloc_zero(mgcp, struct mgcp_response);
419 OSMO_ASSERT(r);
420
421 rc = mgcp_response_parse_head(r, msg);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200422 if (rc) {
Philipp Maierffd75e42017-11-22 11:44:50 +0100423 LOGP(DLMGCP, LOGL_ERROR, "Cannot parse MGCP response (head)\n");
Philipp Maier1fc69992018-01-31 15:32:55 +0100424 rc = 1;
425 goto error;
Philipp Maierffd75e42017-11-22 11:44:50 +0100426 }
427
Philipp Maier1fc69992018-01-31 15:32:55 +0100428 rc = parse_head_params(r);
Philipp Maierffd75e42017-11-22 11:44:50 +0100429 if (rc) {
430 LOGP(DLMGCP, LOGL_ERROR, "Cannot parse MGCP response (head parameters)\n");
Philipp Maier1fc69992018-01-31 15:32:55 +0100431 rc = 1;
432 goto error;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200433 }
434
Philipp Maier1fc69992018-01-31 15:32:55 +0100435 pending = mgcp_client_response_pending_get(mgcp, r->head.trans_id);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200436 if (!pending) {
437 LOGP(DLMGCP, LOGL_ERROR,
438 "Cannot find matching MGCP transaction for trans_id %d\n",
Philipp Maier1fc69992018-01-31 15:32:55 +0100439 r->head.trans_id);
440 rc = -ENOENT;
441 goto error;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200442 }
443
Philipp Maier1fc69992018-01-31 15:32:55 +0100444 mgcp_client_handle_response(mgcp, pending, r);
445 rc = 0;
446
447error:
448 talloc_free(r);
449 return rc;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200450}
451
452static int mgcp_do_read(struct osmo_fd *fd)
453{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200454 struct mgcp_client *mgcp = fd->data;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200455 struct msgb *msg;
456 int ret;
457
458 msg = msgb_alloc_headroom(4096, 128, "mgcp_from_gw");
459 if (!msg) {
460 LOGP(DLMGCP, LOGL_ERROR, "Failed to allocate MGCP message.\n");
461 return -1;
462 }
463
464 ret = read(fd->fd, msg->data, 4096 - 128);
465 if (ret <= 0) {
466 LOGP(DLMGCP, LOGL_ERROR, "Failed to read: %d/%s\n", errno, strerror(errno));
467 msgb_free(msg);
468 return -1;
469 } else if (ret > 4096 - 128) {
470 LOGP(DLMGCP, LOGL_ERROR, "Too much data: %d\n", ret);
471 msgb_free(msg);
472 return -1;
Harald Welte9bf7c532017-11-17 14:14:31 +0100473 }
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200474
475 msg->l2h = msgb_put(msg, ret);
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200476 ret = mgcp_client_rx(mgcp, msg);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200477 talloc_free(msg);
478 return ret;
479}
480
481static int mgcp_do_write(struct osmo_fd *fd, struct msgb *msg)
482{
483 int ret;
484 static char strbuf[4096];
485 unsigned int l = msg->len < sizeof(strbuf) ? msg->len : sizeof(strbuf);
486 unsigned int i;
487
Philipp Maierf8bfbe82017-11-23 19:32:31 +0100488 osmo_strlcpy(strbuf, (const char*)msg->data, l);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200489 for (i = 0; i < sizeof(strbuf); i++) {
490 if (strbuf[i] == '\n' || strbuf[i] == '\r') {
491 strbuf[i] = '\0';
492 break;
493 }
494 }
495 DEBUGP(DLMGCP, "Tx MGCP msg to MGCP GW: '%s'\n", strbuf);
496
497 LOGP(DLMGCP, LOGL_DEBUG, "Sending msg to MGCP GW size: %u\n", msg->len);
498
499 ret = write(fd->fd, msg->data, msg->len);
500 if (ret != msg->len)
501 LOGP(DLMGCP, LOGL_ERROR, "Failed to forward message to MGCP"
502 " GW: %s\n", strerror(errno));
503
504 return ret;
505}
506
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200507struct mgcp_client *mgcp_client_init(void *ctx,
508 struct mgcp_client_conf *conf)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200509{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200510 struct mgcp_client *mgcp;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200511
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200512 mgcp = talloc_zero(ctx, struct mgcp_client);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200513
514 INIT_LLIST_HEAD(&mgcp->responses_pending);
515 INIT_LLIST_HEAD(&mgcp->inuse_endpoints);
516
517 mgcp->next_trans_id = 1;
518
519 mgcp->actual.local_addr = conf->local_addr ? conf->local_addr :
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200520 MGCP_CLIENT_LOCAL_ADDR_DEFAULT;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200521 mgcp->actual.local_port = conf->local_port >= 0 ? (uint16_t)conf->local_port :
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200522 MGCP_CLIENT_LOCAL_PORT_DEFAULT;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200523
524 mgcp->actual.remote_addr = conf->remote_addr ? conf->remote_addr :
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200525 MGCP_CLIENT_REMOTE_ADDR_DEFAULT;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200526 mgcp->actual.remote_port = conf->remote_port >= 0 ? (uint16_t)conf->remote_port :
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200527 MGCP_CLIENT_REMOTE_PORT_DEFAULT;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200528
529 mgcp->actual.first_endpoint = conf->first_endpoint > 0 ? (uint16_t)conf->first_endpoint : 0;
530 mgcp->actual.last_endpoint = conf->last_endpoint > 0 ? (uint16_t)conf->last_endpoint : 0;
Neels Hofmeyrc0dcc3c2017-12-02 18:31:34 +0000531 mgcp->actual.bts_base = conf->bts_base > 0 ? (uint16_t)conf->bts_base : 4000;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200532
533 return mgcp;
534}
535
Philipp Maier275ac972018-01-20 00:58:16 +0100536/*! Initalize client connection (opens socket only, no request is sent yet)
537 * \param[in,out] mgcp MGCP client descriptor.
538 * \returns 0 on success, -EINVAL on error. */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200539int mgcp_client_connect(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200540{
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200541 struct sockaddr_in addr;
542 struct osmo_wqueue *wq;
543 int rc;
544
545 if (!mgcp) {
546 LOGP(DLMGCP, LOGL_FATAL, "MGCPGW client not initialized properly\n");
547 return -EINVAL;
548 }
549
550 wq = &mgcp->wq;
551
Harald Welte8890dfa2017-11-17 15:09:30 +0100552 rc = osmo_sock_init2_ofd(&wq->bfd, AF_INET, SOCK_DGRAM, IPPROTO_UDP,
553 mgcp->actual.local_addr, mgcp->actual.local_port,
554 mgcp->actual.remote_addr, mgcp->actual.remote_port,
555 OSMO_SOCK_F_BIND | OSMO_SOCK_F_CONNECT);
556 if (rc < 0) {
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200557 LOGP(DLMGCP, LOGL_FATAL,
Harald Welte8890dfa2017-11-17 15:09:30 +0100558 "Failed to initialize socket %s:%u -> %s:%u for MGCP GW: %s\n",
559 mgcp->actual.local_addr, mgcp->actual.local_port,
560 mgcp->actual.remote_addr, mgcp->actual.remote_port, strerror(errno));
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200561 goto error_close_fd;
562 }
563
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200564 inet_aton(mgcp->actual.remote_addr, &addr.sin_addr);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200565 mgcp->remote_addr = htonl(addr.sin_addr.s_addr);
566
567 osmo_wqueue_init(wq, 10);
568 wq->bfd.when = BSC_FD_READ;
569 wq->bfd.data = mgcp;
570 wq->read_cb = mgcp_do_read;
571 wq->write_cb = mgcp_do_write;
572
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200573 LOGP(DLMGCP, LOGL_INFO, "MGCP GW connection: %s:%u -> %s:%u\n",
574 mgcp->actual.local_addr, mgcp->actual.local_port,
575 mgcp->actual.remote_addr, mgcp->actual.remote_port);
576
577 return 0;
578error_close_fd:
579 close(wq->bfd.fd);
580 wq->bfd.fd = -1;
581 return rc;
582}
583
Philipp Maier275ac972018-01-20 00:58:16 +0100584/*! Get the IP-Aaddress of the associated MGW as string.
585 * \param[in] mgcp MGCP client descriptor.
586 * \returns a pointer to the address string. */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200587const char *mgcp_client_remote_addr_str(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200588{
589 return mgcp->actual.remote_addr;
590}
591
Philipp Maier275ac972018-01-20 00:58:16 +0100592/*! Get the IP-Port of the associated MGW.
593 * \param[in] mgcp MGCP client descriptor.
594 * \returns port number. */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200595uint16_t mgcp_client_remote_port(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200596{
597 return mgcp->actual.remote_port;
598}
599
Philipp Maier275ac972018-01-20 00:58:16 +0100600/*! Get the IP-Aaddress of the associated MGW as its numeric representation.
601 * \param[in] mgcp MGCP client descriptor.
602 * \returns IP-Address as 32 bit integer (network byte order) */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200603uint32_t mgcp_client_remote_addr_n(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200604{
605 return mgcp->remote_addr;
606}
607
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200608struct mgcp_response_pending * mgcp_client_pending_add(
609 struct mgcp_client *mgcp,
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200610 mgcp_trans_id_t trans_id,
611 mgcp_response_cb_t response_cb,
612 void *priv)
613{
614 struct mgcp_response_pending *pending;
615
616 pending = talloc_zero(mgcp, struct mgcp_response_pending);
617 pending->trans_id = trans_id;
618 pending->response_cb = response_cb;
619 pending->priv = priv;
620 llist_add_tail(&pending->entry, &mgcp->responses_pending);
621
622 return pending;
623}
624
625/* Send the MGCP message in msg to the MGCP GW and handle a response with
626 * response_cb. NOTE: the response_cb still needs to call
627 * mgcp_response_parse_params(response) to get the parsed parameters -- to
628 * potentially save some CPU cycles, only the head line has been parsed when
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100629 * the response_cb is invoked.
630 * Before the priv pointer becomes invalid, e.g. due to transaction timeout,
631 * mgcp_client_cancel() needs to be called for this transaction.
632 */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200633int mgcp_client_tx(struct mgcp_client *mgcp, struct msgb *msg,
634 mgcp_response_cb_t response_cb, void *priv)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200635{
636 struct mgcp_response_pending *pending;
637 mgcp_trans_id_t trans_id;
638 int rc;
639
640 trans_id = msg->cb[MSGB_CB_MGCP_TRANS_ID];
641 if (!trans_id) {
642 LOGP(DLMGCP, LOGL_ERROR,
643 "Unset transaction id in mgcp send request\n");
644 talloc_free(msg);
645 return -EINVAL;
646 }
647
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200648 pending = mgcp_client_pending_add(mgcp, trans_id, response_cb, priv);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200649
650 if (msgb_l2len(msg) > 4096) {
651 LOGP(DLMGCP, LOGL_ERROR,
652 "Cannot send, MGCP message too large: %u\n",
653 msgb_l2len(msg));
654 msgb_free(msg);
655 rc = -EINVAL;
656 goto mgcp_tx_error;
657 }
658
659 rc = osmo_wqueue_enqueue(&mgcp->wq, msg);
660 if (rc) {
661 LOGP(DLMGCP, LOGL_FATAL, "Could not queue message to MGCP GW\n");
662 msgb_free(msg);
663 goto mgcp_tx_error;
664 } else
665 LOGP(DLMGCP, LOGL_INFO, "Queued %u bytes for MGCP GW\n",
666 msgb_l2len(msg));
667 return 0;
668
669mgcp_tx_error:
670 /* Pass NULL to response cb to indicate an error */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200671 mgcp_client_handle_response(mgcp, pending, NULL);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200672 return -1;
673}
674
Philipp Maier275ac972018-01-20 00:58:16 +0100675/*! Cancel a pending transaction.
676 * \param[in] mgcp MGCP client descriptor.
677 * \param[in,out] trans_id Transaction id.
678 * \returns 0 on success, -ENOENT on error.
679 *
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100680 * Should a priv pointer passed to mgcp_client_tx() become invalid, this function must be called. In
681 * practical terms, if the caller of mgcp_client_tx() wishes to tear down a transaction without having
682 * received a response this function must be called. The trans_id can be obtained by calling
Philipp Maier275ac972018-01-20 00:58:16 +0100683 * mgcp_msg_trans_id() on the msgb produced by mgcp_msg_gen(). */
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100684int mgcp_client_cancel(struct mgcp_client *mgcp, mgcp_trans_id_t trans_id)
685{
686 struct mgcp_response_pending *pending = mgcp_client_response_pending_get(mgcp, trans_id);
687 if (!pending) {
Philipp Maier275ac972018-01-20 00:58:16 +0100688 /*! Note: it is not harmful to cancel a transaction twice. */
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100689 LOGP(DLMGCP, LOGL_INFO, "Cannot cancel, no such transaction: %u\n", trans_id);
690 return -ENOENT;
691 }
692 LOGP(DLMGCP, LOGL_INFO, "Canceled transaction %u\n", trans_id);
693 talloc_free(pending);
694 return 0;
Philipp Maier275ac972018-01-20 00:58:16 +0100695 /*! We don't really need to clean up the wqueue: In all sane cases, the msgb has already been sent
696 * out and is no longer in the wqueue. If it still is in the wqueue, then sending MGCP messages
697 * per se is broken and the program should notice so by a full wqueue. Even if this was called
698 * before we had a chance to send out the message and it is still going to be sent, we will just
699 * ignore the reply to it later. Removing a msgb from the wqueue here would just introduce more
700 * bug surface in terms of failing to update wqueue API's counters or some such.
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100701 */
702}
703
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200704static struct msgb *mgcp_msg_from_buf(mgcp_trans_id_t trans_id,
705 const char *buf, int len)
706{
707 struct msgb *msg;
708
709 if (len > (4096 - 128)) {
710 LOGP(DLMGCP, LOGL_ERROR, "Cannot send to MGCP GW:"
711 " message too large: %d\n", len);
712 return NULL;
713 }
714
715 msg = msgb_alloc_headroom(4096, 128, "MGCP tx");
716 OSMO_ASSERT(msg);
717
718 char *dst = (char*)msgb_put(msg, len);
719 memcpy(dst, buf, len);
720 msg->l2h = msg->data;
721 msg->cb[MSGB_CB_MGCP_TRANS_ID] = trans_id;
722
723 return msg;
724}
725
726static struct msgb *mgcp_msg_from_str(mgcp_trans_id_t trans_id,
727 const char *fmt, ...)
728{
729 static char compose[4096 - 128];
730 va_list ap;
731 int len;
732 OSMO_ASSERT(fmt);
733
734 va_start(ap, fmt);
735 len = vsnprintf(compose, sizeof(compose), fmt, ap);
736 va_end(ap);
737 if (len >= sizeof(compose)) {
738 LOGP(DLMGCP, LOGL_ERROR,
739 "Message too large: trans_id=%u len=%d\n",
740 trans_id, len);
741 return NULL;
742 }
743 if (len < 1) {
744 LOGP(DLMGCP, LOGL_ERROR,
745 "Failed to compose message: trans_id=%u len=%d\n",
746 trans_id, len);
747 return NULL;
748 }
749 return mgcp_msg_from_buf(trans_id, compose, len);
750}
751
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200752static mgcp_trans_id_t mgcp_client_next_trans_id(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200753{
754 /* avoid zero trans_id to distinguish from unset trans_id */
755 if (!mgcp->next_trans_id)
756 mgcp->next_trans_id ++;
757 return mgcp->next_trans_id ++;
758}
759
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200760struct msgb *mgcp_msg_crcx(struct mgcp_client *mgcp,
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200761 uint16_t rtp_endpoint, unsigned int call_id,
762 enum mgcp_connection_mode mode)
763{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200764 mgcp_trans_id_t trans_id = mgcp_client_next_trans_id(mgcp);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200765 return mgcp_msg_from_str(trans_id,
766 "CRCX %u %x@mgw MGCP 1.0\r\n"
767 "C: %x\r\n"
768 "L: p:20, a:AMR, nt:IN\r\n"
769 "M: %s\r\n"
770 ,
771 trans_id,
772 rtp_endpoint,
773 call_id,
Neels Hofmeyrd95ab1e2017-09-22 00:52:54 +0200774 mgcp_client_cmode_name(mode));
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200775}
776
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200777struct msgb *mgcp_msg_mdcx(struct mgcp_client *mgcp,
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200778 uint16_t rtp_endpoint, const char *rtp_conn_addr,
779 uint16_t rtp_port, enum mgcp_connection_mode mode)
780
781{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200782 mgcp_trans_id_t trans_id = mgcp_client_next_trans_id(mgcp);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200783 return mgcp_msg_from_str(trans_id,
784 "MDCX %u %x@mgw MGCP 1.0\r\n"
785 "M: %s\r\n"
786 "\r\n"
787 "c=IN IP4 %s\r\n"
788 "m=audio %u RTP/AVP 255\r\n"
789 ,
790 trans_id,
791 rtp_endpoint,
Neels Hofmeyrd95ab1e2017-09-22 00:52:54 +0200792 mgcp_client_cmode_name(mode),
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200793 rtp_conn_addr,
794 rtp_port);
795}
796
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200797struct msgb *mgcp_msg_dlcx(struct mgcp_client *mgcp, uint16_t rtp_endpoint,
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200798 unsigned int call_id)
799{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200800 mgcp_trans_id_t trans_id = mgcp_client_next_trans_id(mgcp);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200801 return mgcp_msg_from_str(trans_id,
802 "DLCX %u %x@mgw MGCP 1.0\r\n"
803 "C: %x\r\n", trans_id, rtp_endpoint, call_id);
804}
805
Philipp Maier1dc6be62017-10-05 18:25:37 +0200806#define MGCP_CRCX_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT | \
807 MGCP_MSG_PRESENCE_CALL_ID | \
Philipp Maier1dc6be62017-10-05 18:25:37 +0200808 MGCP_MSG_PRESENCE_CONN_MODE)
809#define MGCP_MDCX_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT | \
Philipp Maier490cbaa2018-01-22 17:32:38 +0100810 MGCP_MSG_PRESENCE_CALL_ID | \
Philipp Maier1dc6be62017-10-05 18:25:37 +0200811 MGCP_MSG_PRESENCE_CONN_ID)
812#define MGCP_DLCX_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT)
813#define MGCP_AUEP_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT)
814#define MGCP_RSIP_MANDATORY 0 /* none */
815
Philipp Maier275ac972018-01-20 00:58:16 +0100816/*! Generate an MGCP message
817 * \param[in] mgcp MGCP client descriptor.
818 * \param[in] mgcp_msg Message description
819 * \returns message buffer on success, NULL on error. */
Philipp Maier1dc6be62017-10-05 18:25:37 +0200820struct msgb *mgcp_msg_gen(struct mgcp_client *mgcp, struct mgcp_msg *mgcp_msg)
821{
822 mgcp_trans_id_t trans_id = mgcp_client_next_trans_id(mgcp);
823 uint32_t mandatory_mask;
824 struct msgb *msg = msgb_alloc_headroom(4096, 128, "MGCP tx");
825 int rc = 0;
Philipp Maier9d25d7a2018-01-22 17:31:10 +0100826 char local_ip[INET_ADDRSTRLEN];
Philipp Maier1dc6be62017-10-05 18:25:37 +0200827
828 msg->l2h = msg->data;
829 msg->cb[MSGB_CB_MGCP_TRANS_ID] = trans_id;
830
831 /* Add command verb */
832 switch (mgcp_msg->verb) {
833 case MGCP_VERB_CRCX:
834 mandatory_mask = MGCP_CRCX_MANDATORY;
835 rc += msgb_printf(msg, "CRCX %u", trans_id);
836 break;
837 case MGCP_VERB_MDCX:
838 mandatory_mask = MGCP_MDCX_MANDATORY;
839 rc += msgb_printf(msg, "MDCX %u", trans_id);
840 break;
841 case MGCP_VERB_DLCX:
842 mandatory_mask = MGCP_DLCX_MANDATORY;
843 rc += msgb_printf(msg, "DLCX %u", trans_id);
844 break;
845 case MGCP_VERB_AUEP:
846 mandatory_mask = MGCP_AUEP_MANDATORY;
847 rc += msgb_printf(msg, "AUEP %u", trans_id);
848 break;
849 case MGCP_VERB_RSIP:
850 mandatory_mask = MGCP_RSIP_MANDATORY;
851 rc += msgb_printf(msg, "RSIP %u", trans_id);
852 break;
853 default:
854 LOGP(DLMGCP, LOGL_ERROR,
855 "Invalid command verb, can not generate MGCP message\n");
856 msgb_free(msg);
857 return NULL;
858 }
859
860 /* Check if mandatory fields are missing */
861 if (!((mgcp_msg->presence & mandatory_mask) == mandatory_mask)) {
862 LOGP(DLMGCP, LOGL_ERROR,
863 "One or more missing mandatory fields, can not generate MGCP message\n");
864 msgb_free(msg);
865 return NULL;
866 }
867
868 /* Add endpoint name */
Philipp Maier7bc55522017-12-10 22:52:22 +0100869 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_ENDPOINT) {
870 if (strlen(mgcp_msg->endpoint) <= 0) {
871 LOGP(DLMGCP, LOGL_ERROR,
872 "Empty endpoint name, can not generate MGCP message\n");
873 msgb_free(msg);
874 return NULL;
875 }
Philipp Maier3aa81572018-01-31 14:13:45 +0100876
877 if (strstr(mgcp_msg->endpoint, "@") == NULL) {
878 LOGP(DLMGCP, LOGL_ERROR,
879 "Endpoint name (%s) lacks separator (@), can not generate MGCP message\n",
880 mgcp_msg->endpoint);
881 msgb_free(msg);
882 }
883
Philipp Maier1dc6be62017-10-05 18:25:37 +0200884 rc += msgb_printf(msg, " %s", mgcp_msg->endpoint);
Philipp Maier7bc55522017-12-10 22:52:22 +0100885 }
Philipp Maier1dc6be62017-10-05 18:25:37 +0200886
887 /* Add protocol version */
888 rc += msgb_printf(msg, " MGCP 1.0\r\n");
889
890 /* Add call id */
891 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_CALL_ID)
892 rc += msgb_printf(msg, "C: %x\r\n", mgcp_msg->call_id);
893
894 /* Add connection id */
Philipp Maier7bc55522017-12-10 22:52:22 +0100895 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_CONN_ID) {
896 if (strlen(mgcp_msg->conn_id) <= 0) {
897 LOGP(DLMGCP, LOGL_ERROR,
898 "Empty connection id, can not generate MGCP message\n");
899 msgb_free(msg);
900 return NULL;
901 }
Philipp Maier01d24a32017-11-21 17:26:09 +0100902 rc += msgb_printf(msg, "I: %s\r\n", mgcp_msg->conn_id);
Philipp Maier7bc55522017-12-10 22:52:22 +0100903 }
Philipp Maier1dc6be62017-10-05 18:25:37 +0200904
905 /* Add local connection options */
Philipp Maierffd75e42017-11-22 11:44:50 +0100906 if (mgcp_msg->verb == MGCP_VERB_CRCX)
Philipp Maier1dc6be62017-10-05 18:25:37 +0200907 rc += msgb_printf(msg, "L: p:20, a:AMR, nt:IN\r\n");
908
909 /* Add mode */
910 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_CONN_MODE)
911 rc +=
912 msgb_printf(msg, "M: %s\r\n",
913 mgcp_client_cmode_name(mgcp_msg->conn_mode));
914
Philipp Maier9d25d7a2018-01-22 17:31:10 +0100915 /* Add SDP body */
Philipp Maier1dc6be62017-10-05 18:25:37 +0200916 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_AUDIO_IP
917 && mgcp_msg->presence & MGCP_MSG_PRESENCE_AUDIO_PORT) {
Philipp Maier9d25d7a2018-01-22 17:31:10 +0100918
919 /* Add separator to mark the beginning of the SDP block */
920 rc += msgb_printf(msg, "\r\n");
921
922 /* Add SDP protocol version */
923 rc += msgb_printf(msg, "v=0\r\n");
924
Philipp Maier9d25d7a2018-01-22 17:31:10 +0100925 /* Determine local IP-Address */
926 if (osmo_sock_local_ip(local_ip, mgcp->actual.remote_addr) < 0) {
927 LOGP(DLMGCP, LOGL_ERROR,
928 "Could not determine local IP-Address!\n");
929 msgb_free(msg);
930 return NULL;
931 }
932
933 /* Add owner/creator (SDP) */
934 rc += msgb_printf(msg, "o=- %x 23 IN IP4 %s\r\n",
935 mgcp_msg->call_id, local_ip);
936
Philipp Maierb7594732018-01-31 16:44:00 +0100937 /* Add session name (none) */
938 rc += msgb_printf(msg, "s=-\r\n");
939
Philipp Maier9d25d7a2018-01-22 17:31:10 +0100940 /* Add RTP address and port */
Philipp Maier7bc55522017-12-10 22:52:22 +0100941 if (mgcp_msg->audio_port == 0) {
942 LOGP(DLMGCP, LOGL_ERROR,
943 "Invalid port number, can not generate MGCP message\n");
944 msgb_free(msg);
945 return NULL;
946 }
947 if (strlen(mgcp_msg->audio_ip) <= 0) {
948 LOGP(DLMGCP, LOGL_ERROR,
949 "Empty ip address, can not generate MGCP message\n");
950 msgb_free(msg);
951 return NULL;
952 }
Philipp Maier1dc6be62017-10-05 18:25:37 +0200953 rc += msgb_printf(msg, "c=IN IP4 %s\r\n", mgcp_msg->audio_ip);
Philipp Maier9d25d7a2018-01-22 17:31:10 +0100954
955 /* Add time description, active time (SDP) */
956 rc += msgb_printf(msg, "t=0 0\r\n");
Philipp Maierb7594732018-01-31 16:44:00 +0100957
958 rc +=
959 msgb_printf(msg, "m=audio %u RTP/AVP 255\r\n",
960 mgcp_msg->audio_port);
Philipp Maier1dc6be62017-10-05 18:25:37 +0200961 }
962
963 if (rc != 0) {
964 LOGP(DLMGCP, LOGL_ERROR,
965 "message buffer to small, can not generate MGCP message\n");
966 msgb_free(msg);
967 msg = NULL;
968 }
969
970 return msg;
971}
972
Philipp Maier275ac972018-01-20 00:58:16 +0100973/*! Retrieve the MGCP transaction ID from a msgb generated by mgcp_msg_gen()
974 * \param[in] msg message buffer
975 * \returns Transaction id. */
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100976mgcp_trans_id_t mgcp_msg_trans_id(struct msgb *msg)
977{
978 return (mgcp_trans_id_t)msg->cb[MSGB_CB_MGCP_TRANS_ID];
979}
980
Philipp Maier275ac972018-01-20 00:58:16 +0100981/*! Get the configuration parameters a given MGCP client instance
982 * \param[in] mgcp MGCP client descriptor.
983 * \returns configuration */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200984struct mgcp_client_conf *mgcp_client_conf_actual(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200985{
986 return &mgcp->actual;
987}
Neels Hofmeyrd95ab1e2017-09-22 00:52:54 +0200988
989const struct value_string mgcp_client_connection_mode_strs[] = {
990 { MGCP_CONN_NONE, "none" },
991 { MGCP_CONN_RECV_SEND, "sendrecv" },
992 { MGCP_CONN_SEND_ONLY, "sendonly" },
993 { MGCP_CONN_RECV_ONLY, "recvonly" },
994 { MGCP_CONN_LOOPBACK, "loopback" },
995 { 0, NULL }
996};