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 |
Harald Welte | 220f487 | 2018-02-04 09:04:16 +0100 | [diff] [blame] | 7 | * 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 Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 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 |
Harald Welte | 220f487 | 2018-02-04 09:04:16 +0100 | [diff] [blame] | 14 | * GNU General Public License for more details. |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 15 | * |
Harald Welte | 220f487 | 2018-02-04 09:04:16 +0100 | [diff] [blame] | 16 | * You should have received a copy of the GNU General Public License |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 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 | |
Philipp Maier | 275ac97 | 2018-01-20 00:58:16 +0100 | [diff] [blame] | 39 | /*! Initalize MGCP client configuration struct with default values. |
| 40 | * \param[out] conf Client configuration.*/ |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 41 | void mgcp_client_conf_init(struct mgcp_client_conf *conf) |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 42 | { |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 43 | /* NULL and -1 default to MGCP_CLIENT_*_DEFAULT values */ |
| 44 | *conf = (struct mgcp_client_conf){ |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 45 | .local_addr = NULL, |
| 46 | .local_port = -1, |
| 47 | .remote_addr = NULL, |
| 48 | .remote_port = -1, |
| 49 | .first_endpoint = 0, |
| 50 | .last_endpoint = 0, |
Neels Hofmeyr | c0dcc3c | 2017-12-02 18:31:34 +0000 | [diff] [blame] | 51 | .bts_base = 0, |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 52 | }; |
| 53 | } |
| 54 | |
| 55 | /* Test if a given endpoint id is currently in use */ |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 56 | static bool endpoint_in_use(uint16_t id, struct mgcp_client *client) |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 57 | { |
| 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 Maier | 275ac97 | 2018-01-20 00:58:16 +0100 | [diff] [blame] | 67 | /*! Pick next free endpoint ID. |
| 68 | * \param[in,out] client MGCP client descriptor. |
| 69 | * \returns 0 on success, -EINVAL on error. */ |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 70 | int mgcp_client_next_endpoint(struct mgcp_client *client) |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 71 | { |
| 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 Maier | 275ac97 | 2018-01-20 00:58:16 +0100 | [diff] [blame] | 102 | /*! 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 Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 105 | /* 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] | 106 | void mgcp_client_release_endpoint(uint16_t id, struct mgcp_client *client) |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 107 | { |
| 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 Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 118 | static void mgcp_client_handle_response(struct mgcp_client *mgcp, |
| 119 | struct mgcp_response_pending *pending, |
| 120 | struct mgcp_response *response) |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 121 | { |
| 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 | |
| 134 | static 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 Welte | 9bf7c53 | 2017-11-17 14:14:31 +0100 | [diff] [blame] | 144 | if (sscanf(r->body, "%3d %u %n", |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 145 | &r->head.response_code, &r->head.trans_id, |
| 146 | &comment_pos) != 2) |
| 147 | goto response_parse_failure; |
| 148 | |
Philipp Maier | abe8c89 | 2018-01-20 00:15:12 +0100 | [diff] [blame] | 149 | osmo_strlcpy(r->head.comment, r->body + comment_pos, sizeof(r->head.comment)); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 150 | 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 Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 155 | return 0; |
| 156 | |
| 157 | response_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() */ |
| 164 | static 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 Maier | 06da85e | 2017-10-05 18:49:24 +0200 | [diff] [blame] | 182 | 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] | 183 | { |
Harald Welte | 9bf7c53 | 2017-11-17 14:14:31 +0100 | [diff] [blame] | 184 | if (sscanf(line, "m=audio %hu", |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 185 | &r->audio_port) != 1) |
| 186 | goto response_parse_failure; |
| 187 | |
Philipp Maier | 10f32db | 2017-12-13 12:34:34 +0100 | [diff] [blame] | 188 | if (r->audio_port == 0) |
| 189 | goto response_parse_failure; |
| 190 | |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 191 | return 0; |
| 192 | |
| 193 | response_parse_failure: |
| 194 | LOGP(DLMGCP, LOGL_ERROR, |
Philipp Maier | 06da85e | 2017-10-05 18:49:24 +0200 | [diff] [blame] | 195 | "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" */ |
| 200 | static 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 Maier | f8bfbe8 | 2017-11-23 19:32:31 +0100 | [diff] [blame] | 212 | osmo_strlcpy(r->audio_ip, line + 9, sizeof(r->audio_ip)); |
Philipp Maier | 06da85e | 2017-10-05 18:49:24 +0200 | [diff] [blame] | 213 | |
| 214 | /* Check IP-Address */ |
| 215 | if (inet_aton(r->audio_ip, &ip_test) == 0) |
| 216 | goto response_parse_failure; |
| 217 | |
| 218 | return 0; |
| 219 | |
| 220 | response_parse_failure: |
| 221 | LOGP(DLMGCP, LOGL_ERROR, |
| 222 | "Failed to parse MGCP response header (audio ip)\n"); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 223 | return -EINVAL; |
| 224 | } |
| 225 | |
Philipp Maier | 3b12e1b | 2018-01-18 15:16:13 +0100 | [diff] [blame] | 226 | /* A new section is marked by a double line break, check a few more |
| 227 | * patterns as there may be variants */ |
| 228 | static 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 Maier | 275ac97 | 2018-01-20 00:58:16 +0100 | [diff] [blame] | 247 | /*! Parse body (SDP) parameters of the MGCP response |
| 248 | * \param[in,out] r Response data |
| 249 | * \returns 0 on success, -EINVAL on error. */ |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 250 | int mgcp_response_parse_params(struct mgcp_response *r) |
| 251 | { |
| 252 | char *line; |
| 253 | int rc; |
Neels Hofmeyr | 0793d2f | 2018-02-21 14:55:34 +0100 | [diff] [blame] | 254 | char *data; |
Philipp Maier | e9d645b | 2018-01-19 23:54:08 +0100 | [diff] [blame] | 255 | char *data_ptr; |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 256 | |
Philipp Maier | e9d645b | 2018-01-19 23:54:08 +0100 | [diff] [blame] | 257 | /* Since this functions performs a destructive parsing, we create a |
| 258 | * local copy of the body data */ |
Neels Hofmeyr | 0793d2f | 2018-02-21 14:55:34 +0100 | [diff] [blame] | 259 | OSMO_ASSERT(r->body); |
| 260 | data = talloc_strdup(r, r->body); |
Philipp Maier | e9d645b | 2018-01-19 23:54:08 +0100 | [diff] [blame] | 261 | OSMO_ASSERT(data); |
Philipp Maier | 55295f7 | 2018-01-15 14:00:28 +0100 | [diff] [blame] | 262 | |
Philipp Maier | e9d645b | 2018-01-19 23:54:08 +0100 | [diff] [blame] | 263 | /* Find beginning of the parameter (SDP) section */ |
| 264 | data_ptr = mgcp_find_section_end(data); |
Neels Hofmeyr | 1083533 | 2018-02-21 14:55:34 +0100 | [diff] [blame] | 265 | if (!data_ptr) { |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 266 | LOGP(DLMGCP, LOGL_ERROR, |
Neels Hofmeyr | 0793d2f | 2018-02-21 14:55:34 +0100 | [diff] [blame] | 267 | "MGCP response: cannot find start of SDP parameters\n"); |
Philipp Maier | e9d645b | 2018-01-19 23:54:08 +0100 | [diff] [blame] | 268 | rc = -EINVAL; |
| 269 | goto exit; |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 270 | } |
| 271 | |
Neels Hofmeyr | 0793d2f | 2018-02-21 14:55:34 +0100 | [diff] [blame] | 272 | /* 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 Maier | e9d645b | 2018-01-19 23:54:08 +0100 | [diff] [blame] | 275 | for_each_non_empty_line(line, data_ptr) { |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 276 | if (!mgcp_line_is_valid(line)) |
| 277 | return -EINVAL; |
| 278 | |
| 279 | switch (line[0]) { |
| 280 | case 'm': |
Philipp Maier | 06da85e | 2017-10-05 18:49:24 +0200 | [diff] [blame] | 281 | rc = mgcp_parse_audio_port(r, line); |
| 282 | if (rc) |
Philipp Maier | e9d645b | 2018-01-19 23:54:08 +0100 | [diff] [blame] | 283 | goto exit; |
Philipp Maier | 06da85e | 2017-10-05 18:49:24 +0200 | [diff] [blame] | 284 | break; |
| 285 | case 'c': |
| 286 | rc = mgcp_parse_audio_ip(r, line); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 287 | if (rc) |
Philipp Maier | e9d645b | 2018-01-19 23:54:08 +0100 | [diff] [blame] | 288 | goto exit; |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 289 | break; |
| 290 | default: |
| 291 | /* skip unhandled parameters */ |
| 292 | break; |
| 293 | } |
| 294 | } |
Philipp Maier | e9d645b | 2018-01-19 23:54:08 +0100 | [diff] [blame] | 295 | |
| 296 | rc = 0; |
| 297 | exit: |
| 298 | talloc_free(data); |
| 299 | return rc; |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 300 | } |
| 301 | |
Philipp Maier | 55295f7 | 2018-01-15 14:00:28 +0100 | [diff] [blame] | 302 | /* Parse a line like "X: something" */ |
| 303 | static int mgcp_parse_head_param(char *result, unsigned int result_len, |
| 304 | char label, const char *line) |
Philipp Maier | ffd75e4 | 2017-11-22 11:44:50 +0100 | [diff] [blame] | 305 | { |
Philipp Maier | 55295f7 | 2018-01-15 14:00:28 +0100 | [diff] [blame] | 306 | char label_string[4]; |
| 307 | |
| 308 | /* Detect empty parameters */ |
Philipp Maier | ffd75e4 | 2017-11-22 11:44:50 +0100 | [diff] [blame] | 309 | if (strlen(line) < 4) |
| 310 | goto response_parse_failure; |
| 311 | |
Philipp Maier | 55295f7 | 2018-01-15 14:00:28 +0100 | [diff] [blame] | 312 | /* Check if the label matches */ |
| 313 | snprintf(label_string, sizeof(label_string), "%c: ", label); |
| 314 | if (memcmp(label_string, line, 3) != 0) |
Philipp Maier | ffd75e4 | 2017-11-22 11:44:50 +0100 | [diff] [blame] | 315 | goto response_parse_failure; |
| 316 | |
Philipp Maier | 55295f7 | 2018-01-15 14:00:28 +0100 | [diff] [blame] | 317 | /* 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 Maier | ffd75e4 | 2017-11-22 11:44:50 +0100 | [diff] [blame] | 320 | return 0; |
| 321 | |
| 322 | response_parse_failure: |
| 323 | LOGP(DLMGCP, LOGL_ERROR, |
Philipp Maier | 55295f7 | 2018-01-15 14:00:28 +0100 | [diff] [blame] | 324 | "Failed to parse MGCP response (parameter label: %c)\n", label); |
Philipp Maier | ffd75e4 | 2017-11-22 11:44:50 +0100 | [diff] [blame] | 325 | return -EINVAL; |
| 326 | } |
| 327 | |
| 328 | /* Parse MGCP parameters of the response */ |
| 329 | static int parse_head_params(struct mgcp_response *r) |
| 330 | { |
| 331 | char *line; |
| 332 | int rc = 0; |
| 333 | OSMO_ASSERT(r->body); |
Philipp Maier | 55295f7 | 2018-01-15 14:00:28 +0100 | [diff] [blame] | 334 | char *data; |
| 335 | char *data_ptr; |
| 336 | char *data_end; |
Philipp Maier | ffd75e4 | 2017-11-22 11:44:50 +0100 | [diff] [blame] | 337 | |
Philipp Maier | 55295f7 | 2018-01-15 14:00:28 +0100 | [diff] [blame] | 338 | /* Since this functions performs a destructive parsing, we create a |
| 339 | * local copy of the body data */ |
Philipp Maier | 1fc6999 | 2018-01-31 15:32:55 +0100 | [diff] [blame] | 340 | data = talloc_zero_size(r, strlen(r->body)+1); |
Philipp Maier | 55295f7 | 2018-01-15 14:00:28 +0100 | [diff] [blame] | 341 | 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 Maier | ffd75e4 | 2017-11-22 11:44:50 +0100 | [diff] [blame] | 349 | if (data_end) |
| 350 | *data_end = '\0'; |
| 351 | |
Philipp Maier | 55295f7 | 2018-01-15 14:00:28 +0100 | [diff] [blame] | 352 | for_each_non_empty_line(line, data_ptr) { |
Philipp Maier | ffd75e4 | 2017-11-22 11:44:50 +0100 | [diff] [blame] | 353 | switch (line[0]) { |
Philipp Maier | 55295f7 | 2018-01-15 14:00:28 +0100 | [diff] [blame] | 354 | 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 Maier | 771b26a | 2018-01-31 13:53:11 +0100 | [diff] [blame] | 360 | |
| 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 Maier | 3261dc7 | 2018-01-31 14:03:13 +0100 | [diff] [blame] | 367 | |
| 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 Maier | 55295f7 | 2018-01-15 14:00:28 +0100 | [diff] [blame] | 374 | break; |
Philipp Maier | ffd75e4 | 2017-11-22 11:44:50 +0100 | [diff] [blame] | 375 | case 'I': |
Philipp Maier | 55295f7 | 2018-01-15 14:00:28 +0100 | [diff] [blame] | 376 | rc = mgcp_parse_head_param(r->head.conn_id, |
| 377 | sizeof(r->head.conn_id), |
| 378 | 'I', line); |
Philipp Maier | ffd75e4 | 2017-11-22 11:44:50 +0100 | [diff] [blame] | 379 | if (rc) |
| 380 | goto exit; |
| 381 | break; |
| 382 | default: |
| 383 | /* skip unhandled parameters */ |
| 384 | break; |
| 385 | } |
| 386 | } |
| 387 | exit: |
Philipp Maier | 55295f7 | 2018-01-15 14:00:28 +0100 | [diff] [blame] | 388 | talloc_free(data); |
Philipp Maier | ffd75e4 | 2017-11-22 11:44:50 +0100 | [diff] [blame] | 389 | return rc; |
| 390 | } |
| 391 | |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 392 | static struct mgcp_response_pending *mgcp_client_response_pending_get( |
| 393 | struct mgcp_client *mgcp, |
Neels Hofmeyr | c8f37cb | 2017-11-30 13:43:11 +0100 | [diff] [blame] | 394 | mgcp_trans_id_t trans_id) |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 395 | { |
| 396 | struct mgcp_response_pending *pending; |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 397 | llist_for_each_entry(pending, &mgcp->responses_pending, entry) { |
Neels Hofmeyr | c8f37cb | 2017-11-30 13:43:11 +0100 | [diff] [blame] | 398 | if (pending->trans_id == trans_id) { |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 399 | 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 Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 412 | int mgcp_client_rx(struct mgcp_client *mgcp, struct msgb *msg) |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 413 | { |
Philipp Maier | 1fc6999 | 2018-01-31 15:32:55 +0100 | [diff] [blame] | 414 | struct mgcp_response *r; |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 415 | struct mgcp_response_pending *pending; |
| 416 | int rc; |
| 417 | |
Philipp Maier | 1fc6999 | 2018-01-31 15:32:55 +0100 | [diff] [blame] | 418 | r = talloc_zero(mgcp, struct mgcp_response); |
| 419 | OSMO_ASSERT(r); |
| 420 | |
| 421 | rc = mgcp_response_parse_head(r, msg); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 422 | if (rc) { |
Philipp Maier | ffd75e4 | 2017-11-22 11:44:50 +0100 | [diff] [blame] | 423 | LOGP(DLMGCP, LOGL_ERROR, "Cannot parse MGCP response (head)\n"); |
Philipp Maier | 1fc6999 | 2018-01-31 15:32:55 +0100 | [diff] [blame] | 424 | rc = 1; |
| 425 | goto error; |
Philipp Maier | ffd75e4 | 2017-11-22 11:44:50 +0100 | [diff] [blame] | 426 | } |
| 427 | |
Philipp Maier | 1fc6999 | 2018-01-31 15:32:55 +0100 | [diff] [blame] | 428 | rc = parse_head_params(r); |
Philipp Maier | ffd75e4 | 2017-11-22 11:44:50 +0100 | [diff] [blame] | 429 | if (rc) { |
| 430 | LOGP(DLMGCP, LOGL_ERROR, "Cannot parse MGCP response (head parameters)\n"); |
Philipp Maier | 1fc6999 | 2018-01-31 15:32:55 +0100 | [diff] [blame] | 431 | rc = 1; |
| 432 | goto error; |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 433 | } |
| 434 | |
Philipp Maier | 1fc6999 | 2018-01-31 15:32:55 +0100 | [diff] [blame] | 435 | pending = mgcp_client_response_pending_get(mgcp, r->head.trans_id); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 436 | if (!pending) { |
| 437 | LOGP(DLMGCP, LOGL_ERROR, |
| 438 | "Cannot find matching MGCP transaction for trans_id %d\n", |
Philipp Maier | 1fc6999 | 2018-01-31 15:32:55 +0100 | [diff] [blame] | 439 | r->head.trans_id); |
| 440 | rc = -ENOENT; |
| 441 | goto error; |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 442 | } |
| 443 | |
Philipp Maier | 1fc6999 | 2018-01-31 15:32:55 +0100 | [diff] [blame] | 444 | mgcp_client_handle_response(mgcp, pending, r); |
| 445 | rc = 0; |
| 446 | |
| 447 | error: |
| 448 | talloc_free(r); |
| 449 | return rc; |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | static int mgcp_do_read(struct osmo_fd *fd) |
| 453 | { |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 454 | struct mgcp_client *mgcp = fd->data; |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 455 | 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 Welte | 9bf7c53 | 2017-11-17 14:14:31 +0100 | [diff] [blame] | 473 | } |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 474 | |
| 475 | msg->l2h = msgb_put(msg, ret); |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 476 | ret = mgcp_client_rx(mgcp, msg); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 477 | talloc_free(msg); |
| 478 | return ret; |
| 479 | } |
| 480 | |
| 481 | static 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 Maier | f8bfbe8 | 2017-11-23 19:32:31 +0100 | [diff] [blame] | 488 | osmo_strlcpy(strbuf, (const char*)msg->data, l); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 489 | 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 Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 507 | struct mgcp_client *mgcp_client_init(void *ctx, |
| 508 | struct mgcp_client_conf *conf) |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 509 | { |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 510 | struct mgcp_client *mgcp; |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 511 | |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 512 | mgcp = talloc_zero(ctx, struct mgcp_client); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 513 | |
| 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 Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 520 | MGCP_CLIENT_LOCAL_ADDR_DEFAULT; |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 521 | 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] | 522 | MGCP_CLIENT_LOCAL_PORT_DEFAULT; |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 523 | |
| 524 | mgcp->actual.remote_addr = conf->remote_addr ? conf->remote_addr : |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 525 | MGCP_CLIENT_REMOTE_ADDR_DEFAULT; |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 526 | 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] | 527 | MGCP_CLIENT_REMOTE_PORT_DEFAULT; |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 528 | |
| 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 Hofmeyr | c0dcc3c | 2017-12-02 18:31:34 +0000 | [diff] [blame] | 531 | 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] | 532 | |
| 533 | return mgcp; |
| 534 | } |
| 535 | |
Philipp Maier | 275ac97 | 2018-01-20 00:58:16 +0100 | [diff] [blame] | 536 | /*! 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 Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 539 | int mgcp_client_connect(struct mgcp_client *mgcp) |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 540 | { |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 541 | 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 Welte | 8890dfa | 2017-11-17 15:09:30 +0100 | [diff] [blame] | 552 | 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 Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 557 | LOGP(DLMGCP, LOGL_FATAL, |
Harald Welte | 8890dfa | 2017-11-17 15:09:30 +0100 | [diff] [blame] | 558 | "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 Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 561 | goto error_close_fd; |
| 562 | } |
| 563 | |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 564 | inet_aton(mgcp->actual.remote_addr, &addr.sin_addr); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 565 | 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 Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 573 | 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; |
| 578 | error_close_fd: |
| 579 | close(wq->bfd.fd); |
| 580 | wq->bfd.fd = -1; |
| 581 | return rc; |
| 582 | } |
| 583 | |
Philipp Maier | 275ac97 | 2018-01-20 00:58:16 +0100 | [diff] [blame] | 584 | /*! 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 Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 587 | const char *mgcp_client_remote_addr_str(struct mgcp_client *mgcp) |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 588 | { |
| 589 | return mgcp->actual.remote_addr; |
| 590 | } |
| 591 | |
Philipp Maier | 275ac97 | 2018-01-20 00:58:16 +0100 | [diff] [blame] | 592 | /*! Get the IP-Port of the associated MGW. |
| 593 | * \param[in] mgcp MGCP client descriptor. |
| 594 | * \returns port number. */ |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 595 | uint16_t mgcp_client_remote_port(struct mgcp_client *mgcp) |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 596 | { |
| 597 | return mgcp->actual.remote_port; |
| 598 | } |
| 599 | |
Philipp Maier | 275ac97 | 2018-01-20 00:58:16 +0100 | [diff] [blame] | 600 | /*! 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 Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 603 | uint32_t mgcp_client_remote_addr_n(struct mgcp_client *mgcp) |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 604 | { |
| 605 | return mgcp->remote_addr; |
| 606 | } |
| 607 | |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 608 | struct mgcp_response_pending * mgcp_client_pending_add( |
| 609 | struct mgcp_client *mgcp, |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 610 | 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 Hofmeyr | c8f37cb | 2017-11-30 13:43:11 +0100 | [diff] [blame] | 629 | * 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 Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 633 | int mgcp_client_tx(struct mgcp_client *mgcp, struct msgb *msg, |
| 634 | mgcp_response_cb_t response_cb, void *priv) |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 635 | { |
| 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 Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 648 | pending = mgcp_client_pending_add(mgcp, trans_id, response_cb, priv); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 649 | |
| 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 | |
| 669 | mgcp_tx_error: |
| 670 | /* Pass NULL to response cb to indicate an error */ |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 671 | mgcp_client_handle_response(mgcp, pending, NULL); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 672 | return -1; |
| 673 | } |
| 674 | |
Philipp Maier | 275ac97 | 2018-01-20 00:58:16 +0100 | [diff] [blame] | 675 | /*! 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 Hofmeyr | c8f37cb | 2017-11-30 13:43:11 +0100 | [diff] [blame] | 680 | * 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 Maier | 275ac97 | 2018-01-20 00:58:16 +0100 | [diff] [blame] | 683 | * mgcp_msg_trans_id() on the msgb produced by mgcp_msg_gen(). */ |
Neels Hofmeyr | c8f37cb | 2017-11-30 13:43:11 +0100 | [diff] [blame] | 684 | int 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 Maier | 275ac97 | 2018-01-20 00:58:16 +0100 | [diff] [blame] | 688 | /*! Note: it is not harmful to cancel a transaction twice. */ |
Neels Hofmeyr | c8f37cb | 2017-11-30 13:43:11 +0100 | [diff] [blame] | 689 | 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 Maier | 275ac97 | 2018-01-20 00:58:16 +0100 | [diff] [blame] | 695 | /*! 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 Hofmeyr | c8f37cb | 2017-11-30 13:43:11 +0100 | [diff] [blame] | 701 | */ |
| 702 | } |
| 703 | |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 704 | static 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 | |
| 726 | static 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 Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 752 | 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] | 753 | { |
| 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 Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 760 | struct msgb *mgcp_msg_crcx(struct mgcp_client *mgcp, |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 761 | uint16_t rtp_endpoint, unsigned int call_id, |
| 762 | enum mgcp_connection_mode mode) |
| 763 | { |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 764 | mgcp_trans_id_t trans_id = mgcp_client_next_trans_id(mgcp); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 765 | 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 Hofmeyr | d95ab1e | 2017-09-22 00:52:54 +0200 | [diff] [blame] | 774 | mgcp_client_cmode_name(mode)); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 775 | } |
| 776 | |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 777 | struct msgb *mgcp_msg_mdcx(struct mgcp_client *mgcp, |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 778 | uint16_t rtp_endpoint, const char *rtp_conn_addr, |
| 779 | uint16_t rtp_port, enum mgcp_connection_mode mode) |
| 780 | |
| 781 | { |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 782 | mgcp_trans_id_t trans_id = mgcp_client_next_trans_id(mgcp); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 783 | 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 Hofmeyr | d95ab1e | 2017-09-22 00:52:54 +0200 | [diff] [blame] | 792 | mgcp_client_cmode_name(mode), |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 793 | rtp_conn_addr, |
| 794 | rtp_port); |
| 795 | } |
| 796 | |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 797 | 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] | 798 | unsigned int call_id) |
| 799 | { |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 800 | mgcp_trans_id_t trans_id = mgcp_client_next_trans_id(mgcp); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 801 | 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 Maier | 1dc6be6 | 2017-10-05 18:25:37 +0200 | [diff] [blame] | 806 | #define MGCP_CRCX_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT | \ |
| 807 | MGCP_MSG_PRESENCE_CALL_ID | \ |
Philipp Maier | 1dc6be6 | 2017-10-05 18:25:37 +0200 | [diff] [blame] | 808 | MGCP_MSG_PRESENCE_CONN_MODE) |
| 809 | #define MGCP_MDCX_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT | \ |
Philipp Maier | 490cbaa | 2018-01-22 17:32:38 +0100 | [diff] [blame] | 810 | MGCP_MSG_PRESENCE_CALL_ID | \ |
Philipp Maier | 1dc6be6 | 2017-10-05 18:25:37 +0200 | [diff] [blame] | 811 | 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 Maier | 275ac97 | 2018-01-20 00:58:16 +0100 | [diff] [blame] | 816 | /*! 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 Maier | 1dc6be6 | 2017-10-05 18:25:37 +0200 | [diff] [blame] | 820 | struct 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 Maier | 9d25d7a | 2018-01-22 17:31:10 +0100 | [diff] [blame] | 826 | char local_ip[INET_ADDRSTRLEN]; |
Philipp Maier | 1dc6be6 | 2017-10-05 18:25:37 +0200 | [diff] [blame] | 827 | |
| 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 Maier | 7bc5552 | 2017-12-10 22:52:22 +0100 | [diff] [blame] | 869 | 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 Maier | 3aa8157 | 2018-01-31 14:13:45 +0100 | [diff] [blame] | 876 | |
| 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 Maier | 1dc6be6 | 2017-10-05 18:25:37 +0200 | [diff] [blame] | 884 | rc += msgb_printf(msg, " %s", mgcp_msg->endpoint); |
Philipp Maier | 7bc5552 | 2017-12-10 22:52:22 +0100 | [diff] [blame] | 885 | } |
Philipp Maier | 1dc6be6 | 2017-10-05 18:25:37 +0200 | [diff] [blame] | 886 | |
| 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 Maier | 7bc5552 | 2017-12-10 22:52:22 +0100 | [diff] [blame] | 895 | 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 Maier | 01d24a3 | 2017-11-21 17:26:09 +0100 | [diff] [blame] | 902 | rc += msgb_printf(msg, "I: %s\r\n", mgcp_msg->conn_id); |
Philipp Maier | 7bc5552 | 2017-12-10 22:52:22 +0100 | [diff] [blame] | 903 | } |
Philipp Maier | 1dc6be6 | 2017-10-05 18:25:37 +0200 | [diff] [blame] | 904 | |
| 905 | /* Add local connection options */ |
Philipp Maier | ffd75e4 | 2017-11-22 11:44:50 +0100 | [diff] [blame] | 906 | if (mgcp_msg->verb == MGCP_VERB_CRCX) |
Philipp Maier | 1dc6be6 | 2017-10-05 18:25:37 +0200 | [diff] [blame] | 907 | 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 Maier | 9d25d7a | 2018-01-22 17:31:10 +0100 | [diff] [blame] | 915 | /* Add SDP body */ |
Philipp Maier | 1dc6be6 | 2017-10-05 18:25:37 +0200 | [diff] [blame] | 916 | if (mgcp_msg->presence & MGCP_MSG_PRESENCE_AUDIO_IP |
| 917 | && mgcp_msg->presence & MGCP_MSG_PRESENCE_AUDIO_PORT) { |
Philipp Maier | 9d25d7a | 2018-01-22 17:31:10 +0100 | [diff] [blame] | 918 | |
| 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 Maier | 9d25d7a | 2018-01-22 17:31:10 +0100 | [diff] [blame] | 925 | /* 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 Maier | b759473 | 2018-01-31 16:44:00 +0100 | [diff] [blame] | 937 | /* Add session name (none) */ |
| 938 | rc += msgb_printf(msg, "s=-\r\n"); |
| 939 | |
Philipp Maier | 9d25d7a | 2018-01-22 17:31:10 +0100 | [diff] [blame] | 940 | /* Add RTP address and port */ |
Philipp Maier | 7bc5552 | 2017-12-10 22:52:22 +0100 | [diff] [blame] | 941 | 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 Maier | 1dc6be6 | 2017-10-05 18:25:37 +0200 | [diff] [blame] | 953 | rc += msgb_printf(msg, "c=IN IP4 %s\r\n", mgcp_msg->audio_ip); |
Philipp Maier | 9d25d7a | 2018-01-22 17:31:10 +0100 | [diff] [blame] | 954 | |
| 955 | /* Add time description, active time (SDP) */ |
| 956 | rc += msgb_printf(msg, "t=0 0\r\n"); |
Philipp Maier | b759473 | 2018-01-31 16:44:00 +0100 | [diff] [blame] | 957 | |
| 958 | rc += |
| 959 | msgb_printf(msg, "m=audio %u RTP/AVP 255\r\n", |
| 960 | mgcp_msg->audio_port); |
Philipp Maier | 1dc6be6 | 2017-10-05 18:25:37 +0200 | [diff] [blame] | 961 | } |
| 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 Maier | 275ac97 | 2018-01-20 00:58:16 +0100 | [diff] [blame] | 973 | /*! Retrieve the MGCP transaction ID from a msgb generated by mgcp_msg_gen() |
| 974 | * \param[in] msg message buffer |
| 975 | * \returns Transaction id. */ |
Neels Hofmeyr | c8f37cb | 2017-11-30 13:43:11 +0100 | [diff] [blame] | 976 | mgcp_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 Maier | 275ac97 | 2018-01-20 00:58:16 +0100 | [diff] [blame] | 981 | /*! Get the configuration parameters a given MGCP client instance |
| 982 | * \param[in] mgcp MGCP client descriptor. |
| 983 | * \returns configuration */ |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 984 | struct mgcp_client_conf *mgcp_client_conf_actual(struct mgcp_client *mgcp) |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 985 | { |
| 986 | return &mgcp->actual; |
| 987 | } |
Neels Hofmeyr | d95ab1e | 2017-09-22 00:52:54 +0200 | [diff] [blame] | 988 | |
| 989 | const 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 | }; |