blob: a394f3b8b92f9132e5f5e9db6ea3d0ca1df2ca80 [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
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +020039void mgcp_client_conf_init(struct mgcp_client_conf *conf)
Neels Hofmeyre9920f22017-07-10 15:07:22 +020040{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +020041 /* NULL and -1 default to MGCP_CLIENT_*_DEFAULT values */
42 *conf = (struct mgcp_client_conf){
Neels Hofmeyre9920f22017-07-10 15:07:22 +020043 .local_addr = NULL,
44 .local_port = -1,
45 .remote_addr = NULL,
46 .remote_port = -1,
47 .first_endpoint = 0,
48 .last_endpoint = 0,
Neels Hofmeyrc0dcc3c2017-12-02 18:31:34 +000049 .bts_base = 0,
Neels Hofmeyre9920f22017-07-10 15:07:22 +020050 };
51}
52
53/* Test if a given endpoint id is currently in use */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +020054static bool endpoint_in_use(uint16_t id, struct mgcp_client *client)
Neels Hofmeyre9920f22017-07-10 15:07:22 +020055{
56 struct mgcp_inuse_endpoint *endpoint;
57 llist_for_each_entry(endpoint, &client->inuse_endpoints, entry) {
58 if (endpoint->id == id)
59 return true;
60 }
61
62 return false;
63}
64
65/* Find and seize an unsused endpoint id */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +020066int mgcp_client_next_endpoint(struct mgcp_client *client)
Neels Hofmeyre9920f22017-07-10 15:07:22 +020067{
68 int i;
69 uint16_t first_endpoint = client->actual.first_endpoint;
70 uint16_t last_endpoint = client->actual.last_endpoint;
71 struct mgcp_inuse_endpoint *endpoint;
72
73 /* Use the maximum permitted range if the VTY
74 * configuration does not specify a range */
75 if (client->actual.last_endpoint == 0) {
76 first_endpoint = 1;
77 last_endpoint = 65534;
78 }
79
80 /* Test the permitted endpoint range for an endpoint
81 * number that is not in use. When a suitable endpoint
82 * number can be found, seize it by adding it to the
83 * inuse list. */
84 for (i=first_endpoint;i<last_endpoint;i++)
85 {
86 if (endpoint_in_use(i,client) == false) {
87 endpoint = talloc_zero(client, struct mgcp_inuse_endpoint);
88 endpoint->id = i;
89 llist_add_tail(&endpoint->entry, &client->inuse_endpoints);
90 return endpoint->id;
91 }
92 }
93
94 /* All endpoints are busy! */
95 return -EINVAL;
96}
97
98/* Release a seized endpoint id to make it available again for other calls */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +020099void mgcp_client_release_endpoint(uint16_t id, struct mgcp_client *client)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200100{
101 struct mgcp_inuse_endpoint *endpoint;
102 struct mgcp_inuse_endpoint *endpoint_tmp;
103 llist_for_each_entry_safe(endpoint, endpoint_tmp, &client->inuse_endpoints, entry) {
104 if (endpoint->id == id) {
105 llist_del(&endpoint->entry);
106 talloc_free(endpoint);
107 }
108 }
109}
110
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200111static void mgcp_client_handle_response(struct mgcp_client *mgcp,
112 struct mgcp_response_pending *pending,
113 struct mgcp_response *response)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200114{
115 if (!pending) {
116 LOGP(DLMGCP, LOGL_ERROR,
117 "Cannot handle NULL response\n");
118 return;
119 }
120 if (pending->response_cb)
121 pending->response_cb(response, pending->priv);
122 else
123 LOGP(DLMGCP, LOGL_INFO, "MGCP response ignored (NULL cb)\n");
124 talloc_free(pending);
125}
126
127static int mgcp_response_parse_head(struct mgcp_response *r, struct msgb *msg)
128{
129 int comment_pos;
130 char *end;
131
132 if (mgcp_msg_terminate_nul(msg))
133 goto response_parse_failure;
134
135 r->body = (char *)msg->data;
136
Harald Welte9bf7c532017-11-17 14:14:31 +0100137 if (sscanf(r->body, "%3d %u %n",
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200138 &r->head.response_code, &r->head.trans_id,
139 &comment_pos) != 2)
140 goto response_parse_failure;
141
142 r->head.comment = r->body + comment_pos;
143 end = strchr(r->head.comment, '\r');
144 if (!end)
145 goto response_parse_failure;
146 /* Mark the end of the comment */
147 *end = '\0';
148 r->body = end + 1;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200149 return 0;
150
151response_parse_failure:
152 LOGP(DLMGCP, LOGL_ERROR,
153 "Failed to parse MGCP response header\n");
154 return -EINVAL;
155}
156
157/* TODO undup against mgcp_protocol.c:mgcp_check_param() */
158static bool mgcp_line_is_valid(const char *line)
159{
160 const size_t line_len = strlen(line);
161 if (line[0] == '\0')
162 return true;
163
164 if (line_len < 2
165 || line[1] != '=') {
166 LOGP(DLMGCP, LOGL_ERROR,
167 "Wrong MGCP option format: '%s'\n",
168 line);
169 return false;
170 }
171
172 return true;
173}
174
175/* Parse a line like "m=audio 16002 RTP/AVP 98" */
Philipp Maier06da85e2017-10-05 18:49:24 +0200176static int mgcp_parse_audio_port(struct mgcp_response *r, const char *line)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200177{
Harald Welte9bf7c532017-11-17 14:14:31 +0100178 if (sscanf(line, "m=audio %hu",
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200179 &r->audio_port) != 1)
180 goto response_parse_failure;
181
Philipp Maier10f32db2017-12-13 12:34:34 +0100182 if (r->audio_port == 0)
183 goto response_parse_failure;
184
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200185 return 0;
186
187response_parse_failure:
188 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier06da85e2017-10-05 18:49:24 +0200189 "Failed to parse MGCP response header (audio port)\n");
190 return -EINVAL;
191}
192
193/* Parse a line like "c=IN IP4 10.11.12.13" */
194static int mgcp_parse_audio_ip(struct mgcp_response *r, const char *line)
195{
196 struct in_addr ip_test;
197
198 if (strlen(line) < 16)
199 goto response_parse_failure;
200
201 /* The current implementation strictly supports IPV4 only ! */
202 if (memcmp("c=IN IP4 ", line, 9) != 0)
203 goto response_parse_failure;
204
205 /* Extract IP-Address */
Philipp Maierf8bfbe82017-11-23 19:32:31 +0100206 osmo_strlcpy(r->audio_ip, line + 9, sizeof(r->audio_ip));
Philipp Maier06da85e2017-10-05 18:49:24 +0200207
208 /* Check IP-Address */
209 if (inet_aton(r->audio_ip, &ip_test) == 0)
210 goto response_parse_failure;
211
212 return 0;
213
214response_parse_failure:
215 LOGP(DLMGCP, LOGL_ERROR,
216 "Failed to parse MGCP response header (audio ip)\n");
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200217 return -EINVAL;
218}
219
Philipp Maier3b12e1b2018-01-18 15:16:13 +0100220/* A new section is marked by a double line break, check a few more
221 * patterns as there may be variants */
222static char *mgcp_find_section_end(char *string)
223{
224 char *rc;
225
226 rc = strstr(string, "\n\n");
227 if (rc)
228 return rc;
229
230 rc = strstr(string, "\n\r\n\r");
231 if (rc)
232 return rc;
233
234 rc = strstr(string, "\r\n\r\n");
235 if (rc)
236 return rc;
237
238 return NULL;
239}
240
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200241int mgcp_response_parse_params(struct mgcp_response *r)
242{
243 char *line;
244 int rc;
245 OSMO_ASSERT(r->body);
Philipp Maier3b12e1b2018-01-18 15:16:13 +0100246 char *data = mgcp_find_section_end(r->body);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200247
Philipp Maier55295f72018-01-15 14:00:28 +0100248 /* Warning: This function performs a destructive parsing on r->body.
249 * Since this function is called at the very end of the persing
250 * process, destructive parsing is acceptable. */
251
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200252 if (!data) {
253 LOGP(DLMGCP, LOGL_ERROR,
254 "MGCP response: cannot find start of parameters\n");
255 return -EINVAL;
256 }
257
258 /* Advance to after the \n\n, replace the second \n with \0. That's
259 * where the parameters start. */
260 data ++;
261 *data = '\0';
262 data ++;
263
Neels Hofmeyrd95ab1e2017-09-22 00:52:54 +0200264 for_each_non_empty_line(line, data) {
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200265 if (!mgcp_line_is_valid(line))
266 return -EINVAL;
267
268 switch (line[0]) {
269 case 'm':
Philipp Maier06da85e2017-10-05 18:49:24 +0200270 rc = mgcp_parse_audio_port(r, line);
271 if (rc)
272 return rc;
273 break;
274 case 'c':
275 rc = mgcp_parse_audio_ip(r, line);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200276 if (rc)
277 return rc;
278 break;
279 default:
280 /* skip unhandled parameters */
281 break;
282 }
283 }
284 return 0;
285}
286
Philipp Maier55295f72018-01-15 14:00:28 +0100287/* Parse a line like "X: something" */
288static int mgcp_parse_head_param(char *result, unsigned int result_len,
289 char label, const char *line)
Philipp Maierffd75e42017-11-22 11:44:50 +0100290{
Philipp Maier55295f72018-01-15 14:00:28 +0100291 char label_string[4];
292
293 /* Detect empty parameters */
Philipp Maierffd75e42017-11-22 11:44:50 +0100294 if (strlen(line) < 4)
295 goto response_parse_failure;
296
Philipp Maier55295f72018-01-15 14:00:28 +0100297 /* Check if the label matches */
298 snprintf(label_string, sizeof(label_string), "%c: ", label);
299 if (memcmp(label_string, line, 3) != 0)
Philipp Maierffd75e42017-11-22 11:44:50 +0100300 goto response_parse_failure;
301
Philipp Maier55295f72018-01-15 14:00:28 +0100302 /* Copy payload part of the string to destinations (the label string
303 * is always 3 chars long) */
304 osmo_strlcpy(result, line + 3, result_len);
Philipp Maierffd75e42017-11-22 11:44:50 +0100305 return 0;
306
307response_parse_failure:
308 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier55295f72018-01-15 14:00:28 +0100309 "Failed to parse MGCP response (parameter label: %c)\n", label);
Philipp Maierffd75e42017-11-22 11:44:50 +0100310 return -EINVAL;
311}
312
313/* Parse MGCP parameters of the response */
314static int parse_head_params(struct mgcp_response *r)
315{
316 char *line;
317 int rc = 0;
318 OSMO_ASSERT(r->body);
Philipp Maier55295f72018-01-15 14:00:28 +0100319 char *data;
320 char *data_ptr;
321 char *data_end;
Philipp Maierffd75e42017-11-22 11:44:50 +0100322
Philipp Maier55295f72018-01-15 14:00:28 +0100323 /* Since this functions performs a destructive parsing, we create a
324 * local copy of the body data */
325 data = talloc_zero_size(NULL, strlen(r->body)+1);
326 OSMO_ASSERT(data);
327 data_ptr = data;
328 osmo_strlcpy(data, r->body, strlen(r->body));
329
330 /* If there is an SDP body attached, prevent for_each_non_empty_line()
331 * into running in there, we are not yet interested in the parameters
332 * stored there. */
333 data_end = mgcp_find_section_end(data);
Philipp Maierffd75e42017-11-22 11:44:50 +0100334 if (data_end)
335 *data_end = '\0';
336
Philipp Maier55295f72018-01-15 14:00:28 +0100337 for_each_non_empty_line(line, data_ptr) {
Philipp Maierffd75e42017-11-22 11:44:50 +0100338 switch (line[0]) {
Philipp Maier55295f72018-01-15 14:00:28 +0100339 case 'Z':
340 rc = mgcp_parse_head_param(r->head.endpoint,
341 sizeof(r->head.endpoint),
342 'Z', line);
343 if (rc)
344 goto exit;
345 break;
Philipp Maierffd75e42017-11-22 11:44:50 +0100346 case 'I':
Philipp Maier55295f72018-01-15 14:00:28 +0100347 rc = mgcp_parse_head_param(r->head.conn_id,
348 sizeof(r->head.conn_id),
349 'I', line);
Philipp Maierffd75e42017-11-22 11:44:50 +0100350 if (rc)
351 goto exit;
352 break;
353 default:
354 /* skip unhandled parameters */
355 break;
356 }
357 }
358exit:
Philipp Maier55295f72018-01-15 14:00:28 +0100359 talloc_free(data);
Philipp Maierffd75e42017-11-22 11:44:50 +0100360 return rc;
361}
362
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200363static struct mgcp_response_pending *mgcp_client_response_pending_get(
364 struct mgcp_client *mgcp,
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100365 mgcp_trans_id_t trans_id)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200366{
367 struct mgcp_response_pending *pending;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200368 llist_for_each_entry(pending, &mgcp->responses_pending, entry) {
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100369 if (pending->trans_id == trans_id) {
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200370 llist_del(&pending->entry);
371 return pending;
372 }
373 }
374 return NULL;
375}
376
377/* Feed an MGCP message into the receive processing.
378 * Parse the head and call any callback registered for the transaction id found
379 * in the MGCP message. This is normally called directly from the internal
380 * mgcp_do_read that reads from the socket connected to the MGCP gateway. This
381 * function is published mainly to be able to feed data from the test suite.
382 */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200383int mgcp_client_rx(struct mgcp_client *mgcp, struct msgb *msg)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200384{
385 struct mgcp_response r = { 0 };
386 struct mgcp_response_pending *pending;
387 int rc;
388
389 rc = mgcp_response_parse_head(&r, msg);
390 if (rc) {
Philipp Maierffd75e42017-11-22 11:44:50 +0100391 LOGP(DLMGCP, LOGL_ERROR, "Cannot parse MGCP response (head)\n");
392 return -1;
393 }
394
395 rc = parse_head_params(&r);
396 if (rc) {
397 LOGP(DLMGCP, LOGL_ERROR, "Cannot parse MGCP response (head parameters)\n");
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200398 return -1;
399 }
400
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100401 pending = mgcp_client_response_pending_get(mgcp, r.head.trans_id);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200402 if (!pending) {
403 LOGP(DLMGCP, LOGL_ERROR,
404 "Cannot find matching MGCP transaction for trans_id %d\n",
405 r.head.trans_id);
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100406 return -ENOENT;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200407 }
408
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200409 mgcp_client_handle_response(mgcp, pending, &r);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200410 return 0;
411}
412
413static int mgcp_do_read(struct osmo_fd *fd)
414{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200415 struct mgcp_client *mgcp = fd->data;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200416 struct msgb *msg;
417 int ret;
418
419 msg = msgb_alloc_headroom(4096, 128, "mgcp_from_gw");
420 if (!msg) {
421 LOGP(DLMGCP, LOGL_ERROR, "Failed to allocate MGCP message.\n");
422 return -1;
423 }
424
425 ret = read(fd->fd, msg->data, 4096 - 128);
426 if (ret <= 0) {
427 LOGP(DLMGCP, LOGL_ERROR, "Failed to read: %d/%s\n", errno, strerror(errno));
428 msgb_free(msg);
429 return -1;
430 } else if (ret > 4096 - 128) {
431 LOGP(DLMGCP, LOGL_ERROR, "Too much data: %d\n", ret);
432 msgb_free(msg);
433 return -1;
Harald Welte9bf7c532017-11-17 14:14:31 +0100434 }
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200435
436 msg->l2h = msgb_put(msg, ret);
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200437 ret = mgcp_client_rx(mgcp, msg);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200438 talloc_free(msg);
439 return ret;
440}
441
442static int mgcp_do_write(struct osmo_fd *fd, struct msgb *msg)
443{
444 int ret;
445 static char strbuf[4096];
446 unsigned int l = msg->len < sizeof(strbuf) ? msg->len : sizeof(strbuf);
447 unsigned int i;
448
Philipp Maierf8bfbe82017-11-23 19:32:31 +0100449 osmo_strlcpy(strbuf, (const char*)msg->data, l);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200450 for (i = 0; i < sizeof(strbuf); i++) {
451 if (strbuf[i] == '\n' || strbuf[i] == '\r') {
452 strbuf[i] = '\0';
453 break;
454 }
455 }
456 DEBUGP(DLMGCP, "Tx MGCP msg to MGCP GW: '%s'\n", strbuf);
457
458 LOGP(DLMGCP, LOGL_DEBUG, "Sending msg to MGCP GW size: %u\n", msg->len);
459
460 ret = write(fd->fd, msg->data, msg->len);
461 if (ret != msg->len)
462 LOGP(DLMGCP, LOGL_ERROR, "Failed to forward message to MGCP"
463 " GW: %s\n", strerror(errno));
464
465 return ret;
466}
467
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200468struct mgcp_client *mgcp_client_init(void *ctx,
469 struct mgcp_client_conf *conf)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200470{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200471 struct mgcp_client *mgcp;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200472
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200473 mgcp = talloc_zero(ctx, struct mgcp_client);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200474
475 INIT_LLIST_HEAD(&mgcp->responses_pending);
476 INIT_LLIST_HEAD(&mgcp->inuse_endpoints);
477
478 mgcp->next_trans_id = 1;
479
480 mgcp->actual.local_addr = conf->local_addr ? conf->local_addr :
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200481 MGCP_CLIENT_LOCAL_ADDR_DEFAULT;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200482 mgcp->actual.local_port = conf->local_port >= 0 ? (uint16_t)conf->local_port :
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200483 MGCP_CLIENT_LOCAL_PORT_DEFAULT;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200484
485 mgcp->actual.remote_addr = conf->remote_addr ? conf->remote_addr :
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200486 MGCP_CLIENT_REMOTE_ADDR_DEFAULT;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200487 mgcp->actual.remote_port = conf->remote_port >= 0 ? (uint16_t)conf->remote_port :
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200488 MGCP_CLIENT_REMOTE_PORT_DEFAULT;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200489
490 mgcp->actual.first_endpoint = conf->first_endpoint > 0 ? (uint16_t)conf->first_endpoint : 0;
491 mgcp->actual.last_endpoint = conf->last_endpoint > 0 ? (uint16_t)conf->last_endpoint : 0;
Neels Hofmeyrc0dcc3c2017-12-02 18:31:34 +0000492 mgcp->actual.bts_base = conf->bts_base > 0 ? (uint16_t)conf->bts_base : 4000;
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200493
494 return mgcp;
495}
496
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200497int mgcp_client_connect(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200498{
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200499 struct sockaddr_in addr;
500 struct osmo_wqueue *wq;
501 int rc;
502
503 if (!mgcp) {
504 LOGP(DLMGCP, LOGL_FATAL, "MGCPGW client not initialized properly\n");
505 return -EINVAL;
506 }
507
508 wq = &mgcp->wq;
509
Harald Welte8890dfa2017-11-17 15:09:30 +0100510 rc = osmo_sock_init2_ofd(&wq->bfd, AF_INET, SOCK_DGRAM, IPPROTO_UDP,
511 mgcp->actual.local_addr, mgcp->actual.local_port,
512 mgcp->actual.remote_addr, mgcp->actual.remote_port,
513 OSMO_SOCK_F_BIND | OSMO_SOCK_F_CONNECT);
514 if (rc < 0) {
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200515 LOGP(DLMGCP, LOGL_FATAL,
Harald Welte8890dfa2017-11-17 15:09:30 +0100516 "Failed to initialize socket %s:%u -> %s:%u for MGCP GW: %s\n",
517 mgcp->actual.local_addr, mgcp->actual.local_port,
518 mgcp->actual.remote_addr, mgcp->actual.remote_port, strerror(errno));
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200519 goto error_close_fd;
520 }
521
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200522 inet_aton(mgcp->actual.remote_addr, &addr.sin_addr);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200523 mgcp->remote_addr = htonl(addr.sin_addr.s_addr);
524
525 osmo_wqueue_init(wq, 10);
526 wq->bfd.when = BSC_FD_READ;
527 wq->bfd.data = mgcp;
528 wq->read_cb = mgcp_do_read;
529 wq->write_cb = mgcp_do_write;
530
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200531 LOGP(DLMGCP, LOGL_INFO, "MGCP GW connection: %s:%u -> %s:%u\n",
532 mgcp->actual.local_addr, mgcp->actual.local_port,
533 mgcp->actual.remote_addr, mgcp->actual.remote_port);
534
535 return 0;
536error_close_fd:
537 close(wq->bfd.fd);
538 wq->bfd.fd = -1;
539 return rc;
540}
541
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200542const char *mgcp_client_remote_addr_str(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200543{
544 return mgcp->actual.remote_addr;
545}
546
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200547uint16_t mgcp_client_remote_port(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200548{
549 return mgcp->actual.remote_port;
550}
551
552/* Return the MGCP GW binary IPv4 address in network byte order. */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200553uint32_t mgcp_client_remote_addr_n(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200554{
555 return mgcp->remote_addr;
556}
557
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200558struct mgcp_response_pending * mgcp_client_pending_add(
559 struct mgcp_client *mgcp,
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200560 mgcp_trans_id_t trans_id,
561 mgcp_response_cb_t response_cb,
562 void *priv)
563{
564 struct mgcp_response_pending *pending;
565
566 pending = talloc_zero(mgcp, struct mgcp_response_pending);
567 pending->trans_id = trans_id;
568 pending->response_cb = response_cb;
569 pending->priv = priv;
570 llist_add_tail(&pending->entry, &mgcp->responses_pending);
571
572 return pending;
573}
574
575/* Send the MGCP message in msg to the MGCP GW and handle a response with
576 * response_cb. NOTE: the response_cb still needs to call
577 * mgcp_response_parse_params(response) to get the parsed parameters -- to
578 * potentially save some CPU cycles, only the head line has been parsed when
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100579 * the response_cb is invoked.
580 * Before the priv pointer becomes invalid, e.g. due to transaction timeout,
581 * mgcp_client_cancel() needs to be called for this transaction.
582 */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200583int mgcp_client_tx(struct mgcp_client *mgcp, struct msgb *msg,
584 mgcp_response_cb_t response_cb, void *priv)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200585{
586 struct mgcp_response_pending *pending;
587 mgcp_trans_id_t trans_id;
588 int rc;
589
590 trans_id = msg->cb[MSGB_CB_MGCP_TRANS_ID];
591 if (!trans_id) {
592 LOGP(DLMGCP, LOGL_ERROR,
593 "Unset transaction id in mgcp send request\n");
594 talloc_free(msg);
595 return -EINVAL;
596 }
597
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200598 pending = mgcp_client_pending_add(mgcp, trans_id, response_cb, priv);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200599
600 if (msgb_l2len(msg) > 4096) {
601 LOGP(DLMGCP, LOGL_ERROR,
602 "Cannot send, MGCP message too large: %u\n",
603 msgb_l2len(msg));
604 msgb_free(msg);
605 rc = -EINVAL;
606 goto mgcp_tx_error;
607 }
608
609 rc = osmo_wqueue_enqueue(&mgcp->wq, msg);
610 if (rc) {
611 LOGP(DLMGCP, LOGL_FATAL, "Could not queue message to MGCP GW\n");
612 msgb_free(msg);
613 goto mgcp_tx_error;
614 } else
615 LOGP(DLMGCP, LOGL_INFO, "Queued %u bytes for MGCP GW\n",
616 msgb_l2len(msg));
617 return 0;
618
619mgcp_tx_error:
620 /* Pass NULL to response cb to indicate an error */
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200621 mgcp_client_handle_response(mgcp, pending, NULL);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200622 return -1;
623}
624
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100625/* Cancel a pending transaction.
626 * Should a priv pointer passed to mgcp_client_tx() become invalid, this function must be called. In
627 * practical terms, if the caller of mgcp_client_tx() wishes to tear down a transaction without having
628 * received a response this function must be called. The trans_id can be obtained by calling
629 * mgcp_msg_trans_id() on the msgb produced by mgcp_msg_gen().
630 */
631int mgcp_client_cancel(struct mgcp_client *mgcp, mgcp_trans_id_t trans_id)
632{
633 struct mgcp_response_pending *pending = mgcp_client_response_pending_get(mgcp, trans_id);
634 if (!pending) {
635 /* INFO is sufficient, it is not harmful to cancel a transaction twice. */
636 LOGP(DLMGCP, LOGL_INFO, "Cannot cancel, no such transaction: %u\n", trans_id);
637 return -ENOENT;
638 }
639 LOGP(DLMGCP, LOGL_INFO, "Canceled transaction %u\n", trans_id);
640 talloc_free(pending);
641 return 0;
642 /* We don't really need to clean up the wqueue: In all sane cases, the msgb has already been sent
643 * out and is no longer in the wqueue. If it still is in the wqueue, then sending MGCP messages
644 * per se is broken and the program should notice so by a full wqueue. Even if this was called
645 * before we had a chance to send out the message and it is still going to be sent, we will just
646 * ignore the reply to it later. Removing a msgb from the wqueue here would just introduce more
647 * bug surface in terms of failing to update wqueue API's counters or some such.
648 */
649}
650
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200651static struct msgb *mgcp_msg_from_buf(mgcp_trans_id_t trans_id,
652 const char *buf, int len)
653{
654 struct msgb *msg;
655
656 if (len > (4096 - 128)) {
657 LOGP(DLMGCP, LOGL_ERROR, "Cannot send to MGCP GW:"
658 " message too large: %d\n", len);
659 return NULL;
660 }
661
662 msg = msgb_alloc_headroom(4096, 128, "MGCP tx");
663 OSMO_ASSERT(msg);
664
665 char *dst = (char*)msgb_put(msg, len);
666 memcpy(dst, buf, len);
667 msg->l2h = msg->data;
668 msg->cb[MSGB_CB_MGCP_TRANS_ID] = trans_id;
669
670 return msg;
671}
672
673static struct msgb *mgcp_msg_from_str(mgcp_trans_id_t trans_id,
674 const char *fmt, ...)
675{
676 static char compose[4096 - 128];
677 va_list ap;
678 int len;
679 OSMO_ASSERT(fmt);
680
681 va_start(ap, fmt);
682 len = vsnprintf(compose, sizeof(compose), fmt, ap);
683 va_end(ap);
684 if (len >= sizeof(compose)) {
685 LOGP(DLMGCP, LOGL_ERROR,
686 "Message too large: trans_id=%u len=%d\n",
687 trans_id, len);
688 return NULL;
689 }
690 if (len < 1) {
691 LOGP(DLMGCP, LOGL_ERROR,
692 "Failed to compose message: trans_id=%u len=%d\n",
693 trans_id, len);
694 return NULL;
695 }
696 return mgcp_msg_from_buf(trans_id, compose, len);
697}
698
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200699static mgcp_trans_id_t mgcp_client_next_trans_id(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200700{
701 /* avoid zero trans_id to distinguish from unset trans_id */
702 if (!mgcp->next_trans_id)
703 mgcp->next_trans_id ++;
704 return mgcp->next_trans_id ++;
705}
706
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200707struct msgb *mgcp_msg_crcx(struct mgcp_client *mgcp,
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200708 uint16_t rtp_endpoint, unsigned int call_id,
709 enum mgcp_connection_mode mode)
710{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200711 mgcp_trans_id_t trans_id = mgcp_client_next_trans_id(mgcp);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200712 return mgcp_msg_from_str(trans_id,
713 "CRCX %u %x@mgw MGCP 1.0\r\n"
714 "C: %x\r\n"
715 "L: p:20, a:AMR, nt:IN\r\n"
716 "M: %s\r\n"
717 ,
718 trans_id,
719 rtp_endpoint,
720 call_id,
Neels Hofmeyrd95ab1e2017-09-22 00:52:54 +0200721 mgcp_client_cmode_name(mode));
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200722}
723
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200724struct msgb *mgcp_msg_mdcx(struct mgcp_client *mgcp,
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200725 uint16_t rtp_endpoint, const char *rtp_conn_addr,
726 uint16_t rtp_port, enum mgcp_connection_mode mode)
727
728{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200729 mgcp_trans_id_t trans_id = mgcp_client_next_trans_id(mgcp);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200730 return mgcp_msg_from_str(trans_id,
731 "MDCX %u %x@mgw MGCP 1.0\r\n"
732 "M: %s\r\n"
733 "\r\n"
734 "c=IN IP4 %s\r\n"
735 "m=audio %u RTP/AVP 255\r\n"
736 ,
737 trans_id,
738 rtp_endpoint,
Neels Hofmeyrd95ab1e2017-09-22 00:52:54 +0200739 mgcp_client_cmode_name(mode),
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200740 rtp_conn_addr,
741 rtp_port);
742}
743
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200744struct msgb *mgcp_msg_dlcx(struct mgcp_client *mgcp, uint16_t rtp_endpoint,
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200745 unsigned int call_id)
746{
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200747 mgcp_trans_id_t trans_id = mgcp_client_next_trans_id(mgcp);
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200748 return mgcp_msg_from_str(trans_id,
749 "DLCX %u %x@mgw MGCP 1.0\r\n"
750 "C: %x\r\n", trans_id, rtp_endpoint, call_id);
751}
752
Philipp Maier1dc6be62017-10-05 18:25:37 +0200753#define MGCP_CRCX_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT | \
754 MGCP_MSG_PRESENCE_CALL_ID | \
Philipp Maier1dc6be62017-10-05 18:25:37 +0200755 MGCP_MSG_PRESENCE_CONN_MODE)
756#define MGCP_MDCX_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT | \
Philipp Maier490cbaa2018-01-22 17:32:38 +0100757 MGCP_MSG_PRESENCE_CALL_ID | \
Philipp Maier1dc6be62017-10-05 18:25:37 +0200758 MGCP_MSG_PRESENCE_CONN_ID)
759#define MGCP_DLCX_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT)
760#define MGCP_AUEP_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT)
761#define MGCP_RSIP_MANDATORY 0 /* none */
762
763struct msgb *mgcp_msg_gen(struct mgcp_client *mgcp, struct mgcp_msg *mgcp_msg)
764{
765 mgcp_trans_id_t trans_id = mgcp_client_next_trans_id(mgcp);
766 uint32_t mandatory_mask;
767 struct msgb *msg = msgb_alloc_headroom(4096, 128, "MGCP tx");
768 int rc = 0;
Philipp Maier9d25d7a2018-01-22 17:31:10 +0100769 char local_ip[INET_ADDRSTRLEN];
Philipp Maier1dc6be62017-10-05 18:25:37 +0200770
771 msg->l2h = msg->data;
772 msg->cb[MSGB_CB_MGCP_TRANS_ID] = trans_id;
773
774 /* Add command verb */
775 switch (mgcp_msg->verb) {
776 case MGCP_VERB_CRCX:
777 mandatory_mask = MGCP_CRCX_MANDATORY;
778 rc += msgb_printf(msg, "CRCX %u", trans_id);
779 break;
780 case MGCP_VERB_MDCX:
781 mandatory_mask = MGCP_MDCX_MANDATORY;
782 rc += msgb_printf(msg, "MDCX %u", trans_id);
783 break;
784 case MGCP_VERB_DLCX:
785 mandatory_mask = MGCP_DLCX_MANDATORY;
786 rc += msgb_printf(msg, "DLCX %u", trans_id);
787 break;
788 case MGCP_VERB_AUEP:
789 mandatory_mask = MGCP_AUEP_MANDATORY;
790 rc += msgb_printf(msg, "AUEP %u", trans_id);
791 break;
792 case MGCP_VERB_RSIP:
793 mandatory_mask = MGCP_RSIP_MANDATORY;
794 rc += msgb_printf(msg, "RSIP %u", trans_id);
795 break;
796 default:
797 LOGP(DLMGCP, LOGL_ERROR,
798 "Invalid command verb, can not generate MGCP message\n");
799 msgb_free(msg);
800 return NULL;
801 }
802
803 /* Check if mandatory fields are missing */
804 if (!((mgcp_msg->presence & mandatory_mask) == mandatory_mask)) {
805 LOGP(DLMGCP, LOGL_ERROR,
806 "One or more missing mandatory fields, can not generate MGCP message\n");
807 msgb_free(msg);
808 return NULL;
809 }
810
811 /* Add endpoint name */
Philipp Maier7bc55522017-12-10 22:52:22 +0100812 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_ENDPOINT) {
813 if (strlen(mgcp_msg->endpoint) <= 0) {
814 LOGP(DLMGCP, LOGL_ERROR,
815 "Empty endpoint name, can not generate MGCP message\n");
816 msgb_free(msg);
817 return NULL;
818 }
Philipp Maier1dc6be62017-10-05 18:25:37 +0200819 rc += msgb_printf(msg, " %s", mgcp_msg->endpoint);
Philipp Maier7bc55522017-12-10 22:52:22 +0100820 }
Philipp Maier1dc6be62017-10-05 18:25:37 +0200821
822 /* Add protocol version */
823 rc += msgb_printf(msg, " MGCP 1.0\r\n");
824
825 /* Add call id */
826 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_CALL_ID)
827 rc += msgb_printf(msg, "C: %x\r\n", mgcp_msg->call_id);
828
829 /* Add connection id */
Philipp Maier7bc55522017-12-10 22:52:22 +0100830 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_CONN_ID) {
831 if (strlen(mgcp_msg->conn_id) <= 0) {
832 LOGP(DLMGCP, LOGL_ERROR,
833 "Empty connection id, can not generate MGCP message\n");
834 msgb_free(msg);
835 return NULL;
836 }
Philipp Maier01d24a32017-11-21 17:26:09 +0100837 rc += msgb_printf(msg, "I: %s\r\n", mgcp_msg->conn_id);
Philipp Maier7bc55522017-12-10 22:52:22 +0100838 }
Philipp Maier1dc6be62017-10-05 18:25:37 +0200839
840 /* Add local connection options */
Philipp Maierffd75e42017-11-22 11:44:50 +0100841 if (mgcp_msg->verb == MGCP_VERB_CRCX)
Philipp Maier1dc6be62017-10-05 18:25:37 +0200842 rc += msgb_printf(msg, "L: p:20, a:AMR, nt:IN\r\n");
843
844 /* Add mode */
845 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_CONN_MODE)
846 rc +=
847 msgb_printf(msg, "M: %s\r\n",
848 mgcp_client_cmode_name(mgcp_msg->conn_mode));
849
Philipp Maier9d25d7a2018-01-22 17:31:10 +0100850 /* Add SDP body */
Philipp Maier1dc6be62017-10-05 18:25:37 +0200851 if (mgcp_msg->presence & MGCP_MSG_PRESENCE_AUDIO_IP
852 && mgcp_msg->presence & MGCP_MSG_PRESENCE_AUDIO_PORT) {
Philipp Maier9d25d7a2018-01-22 17:31:10 +0100853
854 /* Add separator to mark the beginning of the SDP block */
855 rc += msgb_printf(msg, "\r\n");
856
857 /* Add SDP protocol version */
858 rc += msgb_printf(msg, "v=0\r\n");
859
860 /* Add session name (none) */
861 rc += msgb_printf(msg, "s=-\r\n");
862
863 /* Determine local IP-Address */
864 if (osmo_sock_local_ip(local_ip, mgcp->actual.remote_addr) < 0) {
865 LOGP(DLMGCP, LOGL_ERROR,
866 "Could not determine local IP-Address!\n");
867 msgb_free(msg);
868 return NULL;
869 }
870
871 /* Add owner/creator (SDP) */
872 rc += msgb_printf(msg, "o=- %x 23 IN IP4 %s\r\n",
873 mgcp_msg->call_id, local_ip);
874
875 /* Add RTP address and port */
Philipp Maier7bc55522017-12-10 22:52:22 +0100876 if (mgcp_msg->audio_port == 0) {
877 LOGP(DLMGCP, LOGL_ERROR,
878 "Invalid port number, can not generate MGCP message\n");
879 msgb_free(msg);
880 return NULL;
881 }
882 if (strlen(mgcp_msg->audio_ip) <= 0) {
883 LOGP(DLMGCP, LOGL_ERROR,
884 "Empty ip address, can not generate MGCP message\n");
885 msgb_free(msg);
886 return NULL;
887 }
Philipp Maier1dc6be62017-10-05 18:25:37 +0200888 rc += msgb_printf(msg, "c=IN IP4 %s\r\n", mgcp_msg->audio_ip);
889 rc +=
890 msgb_printf(msg, "m=audio %u RTP/AVP 255\r\n",
891 mgcp_msg->audio_port);
Philipp Maier9d25d7a2018-01-22 17:31:10 +0100892
893 /* Add time description, active time (SDP) */
894 rc += msgb_printf(msg, "t=0 0\r\n");
Philipp Maier1dc6be62017-10-05 18:25:37 +0200895 }
896
897 if (rc != 0) {
898 LOGP(DLMGCP, LOGL_ERROR,
899 "message buffer to small, can not generate MGCP message\n");
900 msgb_free(msg);
901 msg = NULL;
902 }
903
904 return msg;
905}
906
Neels Hofmeyrc8f37cb2017-11-30 13:43:11 +0100907/* Retrieve the MGCP transaction ID from a msgb generated by mgcp_msg_gen() */
908mgcp_trans_id_t mgcp_msg_trans_id(struct msgb *msg)
909{
910 return (mgcp_trans_id_t)msg->cb[MSGB_CB_MGCP_TRANS_ID];
911}
912
Neels Hofmeyr3a8e7232017-09-04 01:02:56 +0200913struct mgcp_client_conf *mgcp_client_conf_actual(struct mgcp_client *mgcp)
Neels Hofmeyre9920f22017-07-10 15:07:22 +0200914{
915 return &mgcp->actual;
916}
Neels Hofmeyrd95ab1e2017-09-22 00:52:54 +0200917
918const struct value_string mgcp_client_connection_mode_strs[] = {
919 { MGCP_CONN_NONE, "none" },
920 { MGCP_CONN_RECV_SEND, "sendrecv" },
921 { MGCP_CONN_SEND_ONLY, "sendonly" },
922 { MGCP_CONN_RECV_ONLY, "recvonly" },
923 { MGCP_CONN_LOOPBACK, "loopback" },
924 { 0, NULL }
925};