Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 1 | /* 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 Maier | 1dc6be6 | 2017-10-05 18:25:37 +0200 | [diff] [blame] | 26 | #include <osmocom/core/byteswap.h> |
Harald Welte | 8890dfa | 2017-11-17 15:09:30 +0100 | [diff] [blame^] | 27 | #include <osmocom/core/socket.h> |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 28 | |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 29 | #include <osmocom/mgcp_client/mgcp_client.h> |
| 30 | #include <osmocom/mgcp_client/mgcp_client_internal.h> |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 31 | |
| 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 Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 39 | void mgcp_client_conf_init(struct mgcp_client_conf *conf) |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 40 | { |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 41 | /* NULL and -1 default to MGCP_CLIENT_*_DEFAULT values */ |
| 42 | *conf = (struct mgcp_client_conf){ |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 43 | .local_addr = NULL, |
| 44 | .local_port = -1, |
| 45 | .remote_addr = NULL, |
| 46 | .remote_port = -1, |
| 47 | .first_endpoint = 0, |
| 48 | .last_endpoint = 0, |
Neels Hofmeyr | c0dcc3c | 2017-12-02 18:31:34 +0000 | [diff] [blame] | 49 | .bts_base = 0, |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 50 | }; |
| 51 | } |
| 52 | |
| 53 | /* Test if a given endpoint id is currently in use */ |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 54 | static bool endpoint_in_use(uint16_t id, struct mgcp_client *client) |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 55 | { |
| 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 Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 66 | int mgcp_client_next_endpoint(struct mgcp_client *client) |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 67 | { |
| 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 Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 99 | void mgcp_client_release_endpoint(uint16_t id, struct mgcp_client *client) |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 100 | { |
| 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 Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 111 | static void mgcp_client_handle_response(struct mgcp_client *mgcp, |
| 112 | struct mgcp_response_pending *pending, |
| 113 | struct mgcp_response *response) |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 114 | { |
| 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 | |
| 127 | static 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 Welte | 9bf7c53 | 2017-11-17 14:14:31 +0100 | [diff] [blame] | 137 | if (sscanf(r->body, "%3d %u %n", |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 138 | &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; |
| 149 | if (r->body[0] == '\n') |
| 150 | r->body ++; |
| 151 | return 0; |
| 152 | |
| 153 | response_parse_failure: |
| 154 | LOGP(DLMGCP, LOGL_ERROR, |
| 155 | "Failed to parse MGCP response header\n"); |
| 156 | return -EINVAL; |
| 157 | } |
| 158 | |
| 159 | /* TODO undup against mgcp_protocol.c:mgcp_check_param() */ |
| 160 | static bool mgcp_line_is_valid(const char *line) |
| 161 | { |
| 162 | const size_t line_len = strlen(line); |
| 163 | if (line[0] == '\0') |
| 164 | return true; |
| 165 | |
| 166 | if (line_len < 2 |
| 167 | || line[1] != '=') { |
| 168 | LOGP(DLMGCP, LOGL_ERROR, |
| 169 | "Wrong MGCP option format: '%s'\n", |
| 170 | line); |
| 171 | return false; |
| 172 | } |
| 173 | |
| 174 | return true; |
| 175 | } |
| 176 | |
| 177 | /* Parse a line like "m=audio 16002 RTP/AVP 98" */ |
Philipp Maier | 06da85e | 2017-10-05 18:49:24 +0200 | [diff] [blame] | 178 | static int mgcp_parse_audio_port(struct mgcp_response *r, const char *line) |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 179 | { |
Harald Welte | 9bf7c53 | 2017-11-17 14:14:31 +0100 | [diff] [blame] | 180 | if (sscanf(line, "m=audio %hu", |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 181 | &r->audio_port) != 1) |
| 182 | goto response_parse_failure; |
| 183 | |
Philipp Maier | 10f32db | 2017-12-13 12:34:34 +0100 | [diff] [blame] | 184 | if (r->audio_port == 0) |
| 185 | goto response_parse_failure; |
| 186 | |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 187 | return 0; |
| 188 | |
| 189 | response_parse_failure: |
| 190 | LOGP(DLMGCP, LOGL_ERROR, |
Philipp Maier | 06da85e | 2017-10-05 18:49:24 +0200 | [diff] [blame] | 191 | "Failed to parse MGCP response header (audio port)\n"); |
| 192 | return -EINVAL; |
| 193 | } |
| 194 | |
| 195 | /* Parse a line like "c=IN IP4 10.11.12.13" */ |
| 196 | static int mgcp_parse_audio_ip(struct mgcp_response *r, const char *line) |
| 197 | { |
| 198 | struct in_addr ip_test; |
| 199 | |
| 200 | if (strlen(line) < 16) |
| 201 | goto response_parse_failure; |
| 202 | |
| 203 | /* The current implementation strictly supports IPV4 only ! */ |
| 204 | if (memcmp("c=IN IP4 ", line, 9) != 0) |
| 205 | goto response_parse_failure; |
| 206 | |
| 207 | /* Extract IP-Address */ |
Philipp Maier | f8bfbe8 | 2017-11-23 19:32:31 +0100 | [diff] [blame] | 208 | osmo_strlcpy(r->audio_ip, line + 9, sizeof(r->audio_ip)); |
Philipp Maier | 06da85e | 2017-10-05 18:49:24 +0200 | [diff] [blame] | 209 | |
| 210 | /* Check IP-Address */ |
| 211 | if (inet_aton(r->audio_ip, &ip_test) == 0) |
| 212 | goto response_parse_failure; |
| 213 | |
| 214 | return 0; |
| 215 | |
| 216 | response_parse_failure: |
| 217 | LOGP(DLMGCP, LOGL_ERROR, |
| 218 | "Failed to parse MGCP response header (audio ip)\n"); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 219 | return -EINVAL; |
| 220 | } |
| 221 | |
| 222 | int mgcp_response_parse_params(struct mgcp_response *r) |
| 223 | { |
| 224 | char *line; |
| 225 | int rc; |
| 226 | OSMO_ASSERT(r->body); |
| 227 | char *data = strstr(r->body, "\n\n"); |
| 228 | |
| 229 | if (!data) { |
| 230 | LOGP(DLMGCP, LOGL_ERROR, |
| 231 | "MGCP response: cannot find start of parameters\n"); |
| 232 | return -EINVAL; |
| 233 | } |
| 234 | |
| 235 | /* Advance to after the \n\n, replace the second \n with \0. That's |
| 236 | * where the parameters start. */ |
| 237 | data ++; |
| 238 | *data = '\0'; |
| 239 | data ++; |
| 240 | |
Neels Hofmeyr | d95ab1e | 2017-09-22 00:52:54 +0200 | [diff] [blame] | 241 | for_each_non_empty_line(line, data) { |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 242 | if (!mgcp_line_is_valid(line)) |
| 243 | return -EINVAL; |
| 244 | |
| 245 | switch (line[0]) { |
| 246 | case 'm': |
Philipp Maier | 06da85e | 2017-10-05 18:49:24 +0200 | [diff] [blame] | 247 | rc = mgcp_parse_audio_port(r, line); |
| 248 | if (rc) |
| 249 | return rc; |
| 250 | break; |
| 251 | case 'c': |
| 252 | rc = mgcp_parse_audio_ip(r, line); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 253 | if (rc) |
| 254 | return rc; |
| 255 | break; |
| 256 | default: |
| 257 | /* skip unhandled parameters */ |
| 258 | break; |
| 259 | } |
| 260 | } |
| 261 | return 0; |
| 262 | } |
| 263 | |
Philipp Maier | ffd75e4 | 2017-11-22 11:44:50 +0100 | [diff] [blame] | 264 | /* Parse a line like "I: 0cedfd5a19542d197af9afe5231f1d61" */ |
| 265 | static int mgcp_parse_conn_id(struct mgcp_response *r, const char *line) |
| 266 | { |
| 267 | if (strlen(line) < 4) |
| 268 | goto response_parse_failure; |
| 269 | |
| 270 | if (memcmp("I: ", line, 3) != 0) |
| 271 | goto response_parse_failure; |
| 272 | |
| 273 | osmo_strlcpy(r->head.conn_id, line + 3, sizeof(r->head.conn_id)); |
| 274 | return 0; |
| 275 | |
| 276 | response_parse_failure: |
| 277 | LOGP(DLMGCP, LOGL_ERROR, |
| 278 | "Failed to parse MGCP response (connectionIdentifier)\n"); |
| 279 | return -EINVAL; |
| 280 | } |
| 281 | |
| 282 | /* Parse MGCP parameters of the response */ |
| 283 | static int parse_head_params(struct mgcp_response *r) |
| 284 | { |
| 285 | char *line; |
| 286 | int rc = 0; |
| 287 | OSMO_ASSERT(r->body); |
| 288 | char *data = r->body; |
| 289 | char *data_end = strstr(r->body, "\n\n"); |
| 290 | |
| 291 | /* Protect SDP body, for_each_non_empty_line() will |
| 292 | * only parse until it hits \0 mark. */ |
| 293 | if (data_end) |
| 294 | *data_end = '\0'; |
| 295 | |
| 296 | for_each_non_empty_line(line, data) { |
| 297 | switch (line[0]) { |
| 298 | case 'I': |
| 299 | rc = mgcp_parse_conn_id(r, line); |
| 300 | if (rc) |
| 301 | goto exit; |
| 302 | break; |
| 303 | default: |
| 304 | /* skip unhandled parameters */ |
| 305 | break; |
| 306 | } |
| 307 | } |
| 308 | exit: |
| 309 | /* Restore original state */ |
| 310 | if (data_end) |
| 311 | *data_end = '\n'; |
| 312 | |
| 313 | return rc; |
| 314 | } |
| 315 | |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 316 | static struct mgcp_response_pending *mgcp_client_response_pending_get( |
| 317 | struct mgcp_client *mgcp, |
Neels Hofmeyr | c8f37cb | 2017-11-30 13:43:11 +0100 | [diff] [blame] | 318 | mgcp_trans_id_t trans_id) |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 319 | { |
| 320 | struct mgcp_response_pending *pending; |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 321 | llist_for_each_entry(pending, &mgcp->responses_pending, entry) { |
Neels Hofmeyr | c8f37cb | 2017-11-30 13:43:11 +0100 | [diff] [blame] | 322 | if (pending->trans_id == trans_id) { |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 323 | llist_del(&pending->entry); |
| 324 | return pending; |
| 325 | } |
| 326 | } |
| 327 | return NULL; |
| 328 | } |
| 329 | |
| 330 | /* Feed an MGCP message into the receive processing. |
| 331 | * Parse the head and call any callback registered for the transaction id found |
| 332 | * in the MGCP message. This is normally called directly from the internal |
| 333 | * mgcp_do_read that reads from the socket connected to the MGCP gateway. This |
| 334 | * function is published mainly to be able to feed data from the test suite. |
| 335 | */ |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 336 | int mgcp_client_rx(struct mgcp_client *mgcp, struct msgb *msg) |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 337 | { |
| 338 | struct mgcp_response r = { 0 }; |
| 339 | struct mgcp_response_pending *pending; |
| 340 | int rc; |
| 341 | |
| 342 | rc = mgcp_response_parse_head(&r, msg); |
| 343 | if (rc) { |
Philipp Maier | ffd75e4 | 2017-11-22 11:44:50 +0100 | [diff] [blame] | 344 | LOGP(DLMGCP, LOGL_ERROR, "Cannot parse MGCP response (head)\n"); |
| 345 | return -1; |
| 346 | } |
| 347 | |
| 348 | rc = parse_head_params(&r); |
| 349 | if (rc) { |
| 350 | LOGP(DLMGCP, LOGL_ERROR, "Cannot parse MGCP response (head parameters)\n"); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 351 | return -1; |
| 352 | } |
| 353 | |
Neels Hofmeyr | c8f37cb | 2017-11-30 13:43:11 +0100 | [diff] [blame] | 354 | pending = mgcp_client_response_pending_get(mgcp, r.head.trans_id); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 355 | if (!pending) { |
| 356 | LOGP(DLMGCP, LOGL_ERROR, |
| 357 | "Cannot find matching MGCP transaction for trans_id %d\n", |
| 358 | r.head.trans_id); |
Neels Hofmeyr | c8f37cb | 2017-11-30 13:43:11 +0100 | [diff] [blame] | 359 | return -ENOENT; |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 360 | } |
| 361 | |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 362 | mgcp_client_handle_response(mgcp, pending, &r); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 363 | return 0; |
| 364 | } |
| 365 | |
| 366 | static int mgcp_do_read(struct osmo_fd *fd) |
| 367 | { |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 368 | struct mgcp_client *mgcp = fd->data; |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 369 | struct msgb *msg; |
| 370 | int ret; |
| 371 | |
| 372 | msg = msgb_alloc_headroom(4096, 128, "mgcp_from_gw"); |
| 373 | if (!msg) { |
| 374 | LOGP(DLMGCP, LOGL_ERROR, "Failed to allocate MGCP message.\n"); |
| 375 | return -1; |
| 376 | } |
| 377 | |
| 378 | ret = read(fd->fd, msg->data, 4096 - 128); |
| 379 | if (ret <= 0) { |
| 380 | LOGP(DLMGCP, LOGL_ERROR, "Failed to read: %d/%s\n", errno, strerror(errno)); |
| 381 | msgb_free(msg); |
| 382 | return -1; |
| 383 | } else if (ret > 4096 - 128) { |
| 384 | LOGP(DLMGCP, LOGL_ERROR, "Too much data: %d\n", ret); |
| 385 | msgb_free(msg); |
| 386 | return -1; |
Harald Welte | 9bf7c53 | 2017-11-17 14:14:31 +0100 | [diff] [blame] | 387 | } |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 388 | |
| 389 | msg->l2h = msgb_put(msg, ret); |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 390 | ret = mgcp_client_rx(mgcp, msg); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 391 | talloc_free(msg); |
| 392 | return ret; |
| 393 | } |
| 394 | |
| 395 | static int mgcp_do_write(struct osmo_fd *fd, struct msgb *msg) |
| 396 | { |
| 397 | int ret; |
| 398 | static char strbuf[4096]; |
| 399 | unsigned int l = msg->len < sizeof(strbuf) ? msg->len : sizeof(strbuf); |
| 400 | unsigned int i; |
| 401 | |
Philipp Maier | f8bfbe8 | 2017-11-23 19:32:31 +0100 | [diff] [blame] | 402 | osmo_strlcpy(strbuf, (const char*)msg->data, l); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 403 | for (i = 0; i < sizeof(strbuf); i++) { |
| 404 | if (strbuf[i] == '\n' || strbuf[i] == '\r') { |
| 405 | strbuf[i] = '\0'; |
| 406 | break; |
| 407 | } |
| 408 | } |
| 409 | DEBUGP(DLMGCP, "Tx MGCP msg to MGCP GW: '%s'\n", strbuf); |
| 410 | |
| 411 | LOGP(DLMGCP, LOGL_DEBUG, "Sending msg to MGCP GW size: %u\n", msg->len); |
| 412 | |
| 413 | ret = write(fd->fd, msg->data, msg->len); |
| 414 | if (ret != msg->len) |
| 415 | LOGP(DLMGCP, LOGL_ERROR, "Failed to forward message to MGCP" |
| 416 | " GW: %s\n", strerror(errno)); |
| 417 | |
| 418 | return ret; |
| 419 | } |
| 420 | |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 421 | struct mgcp_client *mgcp_client_init(void *ctx, |
| 422 | struct mgcp_client_conf *conf) |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 423 | { |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 424 | struct mgcp_client *mgcp; |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 425 | |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 426 | mgcp = talloc_zero(ctx, struct mgcp_client); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 427 | |
| 428 | INIT_LLIST_HEAD(&mgcp->responses_pending); |
| 429 | INIT_LLIST_HEAD(&mgcp->inuse_endpoints); |
| 430 | |
| 431 | mgcp->next_trans_id = 1; |
| 432 | |
| 433 | mgcp->actual.local_addr = conf->local_addr ? conf->local_addr : |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 434 | MGCP_CLIENT_LOCAL_ADDR_DEFAULT; |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 435 | mgcp->actual.local_port = conf->local_port >= 0 ? (uint16_t)conf->local_port : |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 436 | MGCP_CLIENT_LOCAL_PORT_DEFAULT; |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 437 | |
| 438 | mgcp->actual.remote_addr = conf->remote_addr ? conf->remote_addr : |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 439 | MGCP_CLIENT_REMOTE_ADDR_DEFAULT; |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 440 | mgcp->actual.remote_port = conf->remote_port >= 0 ? (uint16_t)conf->remote_port : |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 441 | MGCP_CLIENT_REMOTE_PORT_DEFAULT; |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 442 | |
| 443 | mgcp->actual.first_endpoint = conf->first_endpoint > 0 ? (uint16_t)conf->first_endpoint : 0; |
| 444 | mgcp->actual.last_endpoint = conf->last_endpoint > 0 ? (uint16_t)conf->last_endpoint : 0; |
Neels Hofmeyr | c0dcc3c | 2017-12-02 18:31:34 +0000 | [diff] [blame] | 445 | mgcp->actual.bts_base = conf->bts_base > 0 ? (uint16_t)conf->bts_base : 4000; |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 446 | |
| 447 | return mgcp; |
| 448 | } |
| 449 | |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 450 | int mgcp_client_connect(struct mgcp_client *mgcp) |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 451 | { |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 452 | struct sockaddr_in addr; |
| 453 | struct osmo_wqueue *wq; |
| 454 | int rc; |
| 455 | |
| 456 | if (!mgcp) { |
| 457 | LOGP(DLMGCP, LOGL_FATAL, "MGCPGW client not initialized properly\n"); |
| 458 | return -EINVAL; |
| 459 | } |
| 460 | |
| 461 | wq = &mgcp->wq; |
| 462 | |
Harald Welte | 8890dfa | 2017-11-17 15:09:30 +0100 | [diff] [blame^] | 463 | rc = osmo_sock_init2_ofd(&wq->bfd, AF_INET, SOCK_DGRAM, IPPROTO_UDP, |
| 464 | mgcp->actual.local_addr, mgcp->actual.local_port, |
| 465 | mgcp->actual.remote_addr, mgcp->actual.remote_port, |
| 466 | OSMO_SOCK_F_BIND | OSMO_SOCK_F_CONNECT); |
| 467 | if (rc < 0) { |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 468 | LOGP(DLMGCP, LOGL_FATAL, |
Harald Welte | 8890dfa | 2017-11-17 15:09:30 +0100 | [diff] [blame^] | 469 | "Failed to initialize socket %s:%u -> %s:%u for MGCP GW: %s\n", |
| 470 | mgcp->actual.local_addr, mgcp->actual.local_port, |
| 471 | mgcp->actual.remote_addr, mgcp->actual.remote_port, strerror(errno)); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 472 | goto error_close_fd; |
| 473 | } |
| 474 | |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 475 | inet_aton(mgcp->actual.remote_addr, &addr.sin_addr); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 476 | mgcp->remote_addr = htonl(addr.sin_addr.s_addr); |
| 477 | |
| 478 | osmo_wqueue_init(wq, 10); |
| 479 | wq->bfd.when = BSC_FD_READ; |
| 480 | wq->bfd.data = mgcp; |
| 481 | wq->read_cb = mgcp_do_read; |
| 482 | wq->write_cb = mgcp_do_write; |
| 483 | |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 484 | LOGP(DLMGCP, LOGL_INFO, "MGCP GW connection: %s:%u -> %s:%u\n", |
| 485 | mgcp->actual.local_addr, mgcp->actual.local_port, |
| 486 | mgcp->actual.remote_addr, mgcp->actual.remote_port); |
| 487 | |
| 488 | return 0; |
| 489 | error_close_fd: |
| 490 | close(wq->bfd.fd); |
| 491 | wq->bfd.fd = -1; |
| 492 | return rc; |
| 493 | } |
| 494 | |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 495 | const char *mgcp_client_remote_addr_str(struct mgcp_client *mgcp) |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 496 | { |
| 497 | return mgcp->actual.remote_addr; |
| 498 | } |
| 499 | |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 500 | uint16_t mgcp_client_remote_port(struct mgcp_client *mgcp) |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 501 | { |
| 502 | return mgcp->actual.remote_port; |
| 503 | } |
| 504 | |
| 505 | /* Return the MGCP GW binary IPv4 address in network byte order. */ |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 506 | uint32_t mgcp_client_remote_addr_n(struct mgcp_client *mgcp) |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 507 | { |
| 508 | return mgcp->remote_addr; |
| 509 | } |
| 510 | |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 511 | struct mgcp_response_pending * mgcp_client_pending_add( |
| 512 | struct mgcp_client *mgcp, |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 513 | mgcp_trans_id_t trans_id, |
| 514 | mgcp_response_cb_t response_cb, |
| 515 | void *priv) |
| 516 | { |
| 517 | struct mgcp_response_pending *pending; |
| 518 | |
| 519 | pending = talloc_zero(mgcp, struct mgcp_response_pending); |
| 520 | pending->trans_id = trans_id; |
| 521 | pending->response_cb = response_cb; |
| 522 | pending->priv = priv; |
| 523 | llist_add_tail(&pending->entry, &mgcp->responses_pending); |
| 524 | |
| 525 | return pending; |
| 526 | } |
| 527 | |
| 528 | /* Send the MGCP message in msg to the MGCP GW and handle a response with |
| 529 | * response_cb. NOTE: the response_cb still needs to call |
| 530 | * mgcp_response_parse_params(response) to get the parsed parameters -- to |
| 531 | * potentially save some CPU cycles, only the head line has been parsed when |
Neels Hofmeyr | c8f37cb | 2017-11-30 13:43:11 +0100 | [diff] [blame] | 532 | * the response_cb is invoked. |
| 533 | * Before the priv pointer becomes invalid, e.g. due to transaction timeout, |
| 534 | * mgcp_client_cancel() needs to be called for this transaction. |
| 535 | */ |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 536 | int mgcp_client_tx(struct mgcp_client *mgcp, struct msgb *msg, |
| 537 | mgcp_response_cb_t response_cb, void *priv) |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 538 | { |
| 539 | struct mgcp_response_pending *pending; |
| 540 | mgcp_trans_id_t trans_id; |
| 541 | int rc; |
| 542 | |
| 543 | trans_id = msg->cb[MSGB_CB_MGCP_TRANS_ID]; |
| 544 | if (!trans_id) { |
| 545 | LOGP(DLMGCP, LOGL_ERROR, |
| 546 | "Unset transaction id in mgcp send request\n"); |
| 547 | talloc_free(msg); |
| 548 | return -EINVAL; |
| 549 | } |
| 550 | |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 551 | pending = mgcp_client_pending_add(mgcp, trans_id, response_cb, priv); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 552 | |
| 553 | if (msgb_l2len(msg) > 4096) { |
| 554 | LOGP(DLMGCP, LOGL_ERROR, |
| 555 | "Cannot send, MGCP message too large: %u\n", |
| 556 | msgb_l2len(msg)); |
| 557 | msgb_free(msg); |
| 558 | rc = -EINVAL; |
| 559 | goto mgcp_tx_error; |
| 560 | } |
| 561 | |
| 562 | rc = osmo_wqueue_enqueue(&mgcp->wq, msg); |
| 563 | if (rc) { |
| 564 | LOGP(DLMGCP, LOGL_FATAL, "Could not queue message to MGCP GW\n"); |
| 565 | msgb_free(msg); |
| 566 | goto mgcp_tx_error; |
| 567 | } else |
| 568 | LOGP(DLMGCP, LOGL_INFO, "Queued %u bytes for MGCP GW\n", |
| 569 | msgb_l2len(msg)); |
| 570 | return 0; |
| 571 | |
| 572 | mgcp_tx_error: |
| 573 | /* Pass NULL to response cb to indicate an error */ |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 574 | mgcp_client_handle_response(mgcp, pending, NULL); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 575 | return -1; |
| 576 | } |
| 577 | |
Neels Hofmeyr | c8f37cb | 2017-11-30 13:43:11 +0100 | [diff] [blame] | 578 | /* Cancel a pending transaction. |
| 579 | * Should a priv pointer passed to mgcp_client_tx() become invalid, this function must be called. In |
| 580 | * practical terms, if the caller of mgcp_client_tx() wishes to tear down a transaction without having |
| 581 | * received a response this function must be called. The trans_id can be obtained by calling |
| 582 | * mgcp_msg_trans_id() on the msgb produced by mgcp_msg_gen(). |
| 583 | */ |
| 584 | int mgcp_client_cancel(struct mgcp_client *mgcp, mgcp_trans_id_t trans_id) |
| 585 | { |
| 586 | struct mgcp_response_pending *pending = mgcp_client_response_pending_get(mgcp, trans_id); |
| 587 | if (!pending) { |
| 588 | /* INFO is sufficient, it is not harmful to cancel a transaction twice. */ |
| 589 | LOGP(DLMGCP, LOGL_INFO, "Cannot cancel, no such transaction: %u\n", trans_id); |
| 590 | return -ENOENT; |
| 591 | } |
| 592 | LOGP(DLMGCP, LOGL_INFO, "Canceled transaction %u\n", trans_id); |
| 593 | talloc_free(pending); |
| 594 | return 0; |
| 595 | /* We don't really need to clean up the wqueue: In all sane cases, the msgb has already been sent |
| 596 | * out and is no longer in the wqueue. If it still is in the wqueue, then sending MGCP messages |
| 597 | * per se is broken and the program should notice so by a full wqueue. Even if this was called |
| 598 | * before we had a chance to send out the message and it is still going to be sent, we will just |
| 599 | * ignore the reply to it later. Removing a msgb from the wqueue here would just introduce more |
| 600 | * bug surface in terms of failing to update wqueue API's counters or some such. |
| 601 | */ |
| 602 | } |
| 603 | |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 604 | static struct msgb *mgcp_msg_from_buf(mgcp_trans_id_t trans_id, |
| 605 | const char *buf, int len) |
| 606 | { |
| 607 | struct msgb *msg; |
| 608 | |
| 609 | if (len > (4096 - 128)) { |
| 610 | LOGP(DLMGCP, LOGL_ERROR, "Cannot send to MGCP GW:" |
| 611 | " message too large: %d\n", len); |
| 612 | return NULL; |
| 613 | } |
| 614 | |
| 615 | msg = msgb_alloc_headroom(4096, 128, "MGCP tx"); |
| 616 | OSMO_ASSERT(msg); |
| 617 | |
| 618 | char *dst = (char*)msgb_put(msg, len); |
| 619 | memcpy(dst, buf, len); |
| 620 | msg->l2h = msg->data; |
| 621 | msg->cb[MSGB_CB_MGCP_TRANS_ID] = trans_id; |
| 622 | |
| 623 | return msg; |
| 624 | } |
| 625 | |
| 626 | static struct msgb *mgcp_msg_from_str(mgcp_trans_id_t trans_id, |
| 627 | const char *fmt, ...) |
| 628 | { |
| 629 | static char compose[4096 - 128]; |
| 630 | va_list ap; |
| 631 | int len; |
| 632 | OSMO_ASSERT(fmt); |
| 633 | |
| 634 | va_start(ap, fmt); |
| 635 | len = vsnprintf(compose, sizeof(compose), fmt, ap); |
| 636 | va_end(ap); |
| 637 | if (len >= sizeof(compose)) { |
| 638 | LOGP(DLMGCP, LOGL_ERROR, |
| 639 | "Message too large: trans_id=%u len=%d\n", |
| 640 | trans_id, len); |
| 641 | return NULL; |
| 642 | } |
| 643 | if (len < 1) { |
| 644 | LOGP(DLMGCP, LOGL_ERROR, |
| 645 | "Failed to compose message: trans_id=%u len=%d\n", |
| 646 | trans_id, len); |
| 647 | return NULL; |
| 648 | } |
| 649 | return mgcp_msg_from_buf(trans_id, compose, len); |
| 650 | } |
| 651 | |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 652 | static mgcp_trans_id_t mgcp_client_next_trans_id(struct mgcp_client *mgcp) |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 653 | { |
| 654 | /* avoid zero trans_id to distinguish from unset trans_id */ |
| 655 | if (!mgcp->next_trans_id) |
| 656 | mgcp->next_trans_id ++; |
| 657 | return mgcp->next_trans_id ++; |
| 658 | } |
| 659 | |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 660 | struct msgb *mgcp_msg_crcx(struct mgcp_client *mgcp, |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 661 | uint16_t rtp_endpoint, unsigned int call_id, |
| 662 | enum mgcp_connection_mode mode) |
| 663 | { |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 664 | mgcp_trans_id_t trans_id = mgcp_client_next_trans_id(mgcp); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 665 | return mgcp_msg_from_str(trans_id, |
| 666 | "CRCX %u %x@mgw MGCP 1.0\r\n" |
| 667 | "C: %x\r\n" |
| 668 | "L: p:20, a:AMR, nt:IN\r\n" |
| 669 | "M: %s\r\n" |
| 670 | , |
| 671 | trans_id, |
| 672 | rtp_endpoint, |
| 673 | call_id, |
Neels Hofmeyr | d95ab1e | 2017-09-22 00:52:54 +0200 | [diff] [blame] | 674 | mgcp_client_cmode_name(mode)); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 675 | } |
| 676 | |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 677 | struct msgb *mgcp_msg_mdcx(struct mgcp_client *mgcp, |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 678 | uint16_t rtp_endpoint, const char *rtp_conn_addr, |
| 679 | uint16_t rtp_port, enum mgcp_connection_mode mode) |
| 680 | |
| 681 | { |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 682 | mgcp_trans_id_t trans_id = mgcp_client_next_trans_id(mgcp); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 683 | return mgcp_msg_from_str(trans_id, |
| 684 | "MDCX %u %x@mgw MGCP 1.0\r\n" |
| 685 | "M: %s\r\n" |
| 686 | "\r\n" |
| 687 | "c=IN IP4 %s\r\n" |
| 688 | "m=audio %u RTP/AVP 255\r\n" |
| 689 | , |
| 690 | trans_id, |
| 691 | rtp_endpoint, |
Neels Hofmeyr | d95ab1e | 2017-09-22 00:52:54 +0200 | [diff] [blame] | 692 | mgcp_client_cmode_name(mode), |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 693 | rtp_conn_addr, |
| 694 | rtp_port); |
| 695 | } |
| 696 | |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 697 | struct msgb *mgcp_msg_dlcx(struct mgcp_client *mgcp, uint16_t rtp_endpoint, |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 698 | unsigned int call_id) |
| 699 | { |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 700 | mgcp_trans_id_t trans_id = mgcp_client_next_trans_id(mgcp); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 701 | return mgcp_msg_from_str(trans_id, |
| 702 | "DLCX %u %x@mgw MGCP 1.0\r\n" |
| 703 | "C: %x\r\n", trans_id, rtp_endpoint, call_id); |
| 704 | } |
| 705 | |
Philipp Maier | 1dc6be6 | 2017-10-05 18:25:37 +0200 | [diff] [blame] | 706 | #define MGCP_CRCX_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT | \ |
| 707 | MGCP_MSG_PRESENCE_CALL_ID | \ |
Philipp Maier | 1dc6be6 | 2017-10-05 18:25:37 +0200 | [diff] [blame] | 708 | MGCP_MSG_PRESENCE_CONN_MODE) |
| 709 | #define MGCP_MDCX_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT | \ |
| 710 | MGCP_MSG_PRESENCE_CONN_ID) |
| 711 | #define MGCP_DLCX_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT) |
| 712 | #define MGCP_AUEP_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT) |
| 713 | #define MGCP_RSIP_MANDATORY 0 /* none */ |
| 714 | |
| 715 | struct msgb *mgcp_msg_gen(struct mgcp_client *mgcp, struct mgcp_msg *mgcp_msg) |
| 716 | { |
| 717 | mgcp_trans_id_t trans_id = mgcp_client_next_trans_id(mgcp); |
| 718 | uint32_t mandatory_mask; |
| 719 | struct msgb *msg = msgb_alloc_headroom(4096, 128, "MGCP tx"); |
| 720 | int rc = 0; |
| 721 | |
| 722 | msg->l2h = msg->data; |
| 723 | msg->cb[MSGB_CB_MGCP_TRANS_ID] = trans_id; |
| 724 | |
| 725 | /* Add command verb */ |
| 726 | switch (mgcp_msg->verb) { |
| 727 | case MGCP_VERB_CRCX: |
| 728 | mandatory_mask = MGCP_CRCX_MANDATORY; |
| 729 | rc += msgb_printf(msg, "CRCX %u", trans_id); |
| 730 | break; |
| 731 | case MGCP_VERB_MDCX: |
| 732 | mandatory_mask = MGCP_MDCX_MANDATORY; |
| 733 | rc += msgb_printf(msg, "MDCX %u", trans_id); |
| 734 | break; |
| 735 | case MGCP_VERB_DLCX: |
| 736 | mandatory_mask = MGCP_DLCX_MANDATORY; |
| 737 | rc += msgb_printf(msg, "DLCX %u", trans_id); |
| 738 | break; |
| 739 | case MGCP_VERB_AUEP: |
| 740 | mandatory_mask = MGCP_AUEP_MANDATORY; |
| 741 | rc += msgb_printf(msg, "AUEP %u", trans_id); |
| 742 | break; |
| 743 | case MGCP_VERB_RSIP: |
| 744 | mandatory_mask = MGCP_RSIP_MANDATORY; |
| 745 | rc += msgb_printf(msg, "RSIP %u", trans_id); |
| 746 | break; |
| 747 | default: |
| 748 | LOGP(DLMGCP, LOGL_ERROR, |
| 749 | "Invalid command verb, can not generate MGCP message\n"); |
| 750 | msgb_free(msg); |
| 751 | return NULL; |
| 752 | } |
| 753 | |
| 754 | /* Check if mandatory fields are missing */ |
| 755 | if (!((mgcp_msg->presence & mandatory_mask) == mandatory_mask)) { |
| 756 | LOGP(DLMGCP, LOGL_ERROR, |
| 757 | "One or more missing mandatory fields, can not generate MGCP message\n"); |
| 758 | msgb_free(msg); |
| 759 | return NULL; |
| 760 | } |
| 761 | |
| 762 | /* Add endpoint name */ |
Philipp Maier | 7bc5552 | 2017-12-10 22:52:22 +0100 | [diff] [blame] | 763 | if (mgcp_msg->presence & MGCP_MSG_PRESENCE_ENDPOINT) { |
| 764 | if (strlen(mgcp_msg->endpoint) <= 0) { |
| 765 | LOGP(DLMGCP, LOGL_ERROR, |
| 766 | "Empty endpoint name, can not generate MGCP message\n"); |
| 767 | msgb_free(msg); |
| 768 | return NULL; |
| 769 | } |
Philipp Maier | 1dc6be6 | 2017-10-05 18:25:37 +0200 | [diff] [blame] | 770 | rc += msgb_printf(msg, " %s", mgcp_msg->endpoint); |
Philipp Maier | 7bc5552 | 2017-12-10 22:52:22 +0100 | [diff] [blame] | 771 | } |
Philipp Maier | 1dc6be6 | 2017-10-05 18:25:37 +0200 | [diff] [blame] | 772 | |
| 773 | /* Add protocol version */ |
| 774 | rc += msgb_printf(msg, " MGCP 1.0\r\n"); |
| 775 | |
| 776 | /* Add call id */ |
| 777 | if (mgcp_msg->presence & MGCP_MSG_PRESENCE_CALL_ID) |
| 778 | rc += msgb_printf(msg, "C: %x\r\n", mgcp_msg->call_id); |
| 779 | |
| 780 | /* Add connection id */ |
Philipp Maier | 7bc5552 | 2017-12-10 22:52:22 +0100 | [diff] [blame] | 781 | if (mgcp_msg->presence & MGCP_MSG_PRESENCE_CONN_ID) { |
| 782 | if (strlen(mgcp_msg->conn_id) <= 0) { |
| 783 | LOGP(DLMGCP, LOGL_ERROR, |
| 784 | "Empty connection id, can not generate MGCP message\n"); |
| 785 | msgb_free(msg); |
| 786 | return NULL; |
| 787 | } |
Philipp Maier | 01d24a3 | 2017-11-21 17:26:09 +0100 | [diff] [blame] | 788 | rc += msgb_printf(msg, "I: %s\r\n", mgcp_msg->conn_id); |
Philipp Maier | 7bc5552 | 2017-12-10 22:52:22 +0100 | [diff] [blame] | 789 | } |
Philipp Maier | 1dc6be6 | 2017-10-05 18:25:37 +0200 | [diff] [blame] | 790 | |
| 791 | /* Add local connection options */ |
Philipp Maier | ffd75e4 | 2017-11-22 11:44:50 +0100 | [diff] [blame] | 792 | if (mgcp_msg->verb == MGCP_VERB_CRCX) |
Philipp Maier | 1dc6be6 | 2017-10-05 18:25:37 +0200 | [diff] [blame] | 793 | rc += msgb_printf(msg, "L: p:20, a:AMR, nt:IN\r\n"); |
| 794 | |
| 795 | /* Add mode */ |
| 796 | if (mgcp_msg->presence & MGCP_MSG_PRESENCE_CONN_MODE) |
| 797 | rc += |
| 798 | msgb_printf(msg, "M: %s\r\n", |
| 799 | mgcp_client_cmode_name(mgcp_msg->conn_mode)); |
| 800 | |
| 801 | /* Add RTP address and port (SDP) */ |
| 802 | if (mgcp_msg->presence & MGCP_MSG_PRESENCE_AUDIO_IP |
| 803 | && mgcp_msg->presence & MGCP_MSG_PRESENCE_AUDIO_PORT) { |
Philipp Maier | 7bc5552 | 2017-12-10 22:52:22 +0100 | [diff] [blame] | 804 | if (mgcp_msg->audio_port == 0) { |
| 805 | LOGP(DLMGCP, LOGL_ERROR, |
| 806 | "Invalid port number, can not generate MGCP message\n"); |
| 807 | msgb_free(msg); |
| 808 | return NULL; |
| 809 | } |
| 810 | if (strlen(mgcp_msg->audio_ip) <= 0) { |
| 811 | LOGP(DLMGCP, LOGL_ERROR, |
| 812 | "Empty ip address, can not generate MGCP message\n"); |
| 813 | msgb_free(msg); |
| 814 | return NULL; |
| 815 | } |
Philipp Maier | 1dc6be6 | 2017-10-05 18:25:37 +0200 | [diff] [blame] | 816 | rc += msgb_printf(msg, "\r\n"); |
| 817 | rc += msgb_printf(msg, "c=IN IP4 %s\r\n", mgcp_msg->audio_ip); |
| 818 | rc += |
| 819 | msgb_printf(msg, "m=audio %u RTP/AVP 255\r\n", |
| 820 | mgcp_msg->audio_port); |
| 821 | } |
| 822 | |
| 823 | if (rc != 0) { |
| 824 | LOGP(DLMGCP, LOGL_ERROR, |
| 825 | "message buffer to small, can not generate MGCP message\n"); |
| 826 | msgb_free(msg); |
| 827 | msg = NULL; |
| 828 | } |
| 829 | |
| 830 | return msg; |
| 831 | } |
| 832 | |
Neels Hofmeyr | c8f37cb | 2017-11-30 13:43:11 +0100 | [diff] [blame] | 833 | /* Retrieve the MGCP transaction ID from a msgb generated by mgcp_msg_gen() */ |
| 834 | mgcp_trans_id_t mgcp_msg_trans_id(struct msgb *msg) |
| 835 | { |
| 836 | return (mgcp_trans_id_t)msg->cb[MSGB_CB_MGCP_TRANS_ID]; |
| 837 | } |
| 838 | |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 839 | struct mgcp_client_conf *mgcp_client_conf_actual(struct mgcp_client *mgcp) |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 840 | { |
| 841 | return &mgcp->actual; |
| 842 | } |
Neels Hofmeyr | d95ab1e | 2017-09-22 00:52:54 +0200 | [diff] [blame] | 843 | |
| 844 | const struct value_string mgcp_client_connection_mode_strs[] = { |
| 845 | { MGCP_CONN_NONE, "none" }, |
| 846 | { MGCP_CONN_RECV_SEND, "sendrecv" }, |
| 847 | { MGCP_CONN_SEND_ONLY, "sendonly" }, |
| 848 | { MGCP_CONN_RECV_ONLY, "recvonly" }, |
| 849 | { MGCP_CONN_LOOPBACK, "loopback" }, |
| 850 | { 0, NULL } |
| 851 | }; |