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 | |
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; |
| 254 | OSMO_ASSERT(r->body); |
Philipp Maier | 3b12e1b | 2018-01-18 15:16:13 +0100 | [diff] [blame] | 255 | char *data = mgcp_find_section_end(r->body); |
Philipp Maier | e9d645b | 2018-01-19 23:54:08 +0100 | [diff] [blame] | 256 | char *data_ptr; |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 257 | |
Philipp Maier | e9d645b | 2018-01-19 23:54:08 +0100 | [diff] [blame] | 258 | /* Since this functions performs a destructive parsing, we create a |
| 259 | * local copy of the body data */ |
| 260 | data = talloc_zero_size(NULL, strlen(r->body)+1); |
| 261 | OSMO_ASSERT(data); |
| 262 | data_ptr = data; |
| 263 | osmo_strlcpy(data, r->body, strlen(r->body)); |
Philipp Maier | 55295f7 | 2018-01-15 14:00:28 +0100 | [diff] [blame] | 264 | |
Philipp Maier | e9d645b | 2018-01-19 23:54:08 +0100 | [diff] [blame] | 265 | /* Find beginning of the parameter (SDP) section */ |
| 266 | data_ptr = mgcp_find_section_end(data); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 267 | if (!data) { |
| 268 | LOGP(DLMGCP, LOGL_ERROR, |
| 269 | "MGCP response: cannot find start of parameters\n"); |
Philipp Maier | e9d645b | 2018-01-19 23:54:08 +0100 | [diff] [blame] | 270 | rc = -EINVAL; |
| 271 | goto exit; |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 272 | } |
| 273 | |
Philipp Maier | e9d645b | 2018-01-19 23:54:08 +0100 | [diff] [blame] | 274 | for_each_non_empty_line(line, data_ptr) { |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 275 | if (!mgcp_line_is_valid(line)) |
| 276 | return -EINVAL; |
| 277 | |
| 278 | switch (line[0]) { |
| 279 | case 'm': |
Philipp Maier | 06da85e | 2017-10-05 18:49:24 +0200 | [diff] [blame] | 280 | rc = mgcp_parse_audio_port(r, line); |
| 281 | if (rc) |
Philipp Maier | e9d645b | 2018-01-19 23:54:08 +0100 | [diff] [blame] | 282 | goto exit; |
Philipp Maier | 06da85e | 2017-10-05 18:49:24 +0200 | [diff] [blame] | 283 | break; |
| 284 | case 'c': |
| 285 | rc = mgcp_parse_audio_ip(r, line); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 286 | if (rc) |
Philipp Maier | e9d645b | 2018-01-19 23:54:08 +0100 | [diff] [blame] | 287 | goto exit; |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 288 | break; |
| 289 | default: |
| 290 | /* skip unhandled parameters */ |
| 291 | break; |
| 292 | } |
| 293 | } |
Philipp Maier | e9d645b | 2018-01-19 23:54:08 +0100 | [diff] [blame] | 294 | |
| 295 | rc = 0; |
| 296 | exit: |
| 297 | talloc_free(data); |
| 298 | return rc; |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 299 | } |
| 300 | |
Philipp Maier | 55295f7 | 2018-01-15 14:00:28 +0100 | [diff] [blame] | 301 | /* Parse a line like "X: something" */ |
| 302 | static int mgcp_parse_head_param(char *result, unsigned int result_len, |
| 303 | char label, const char *line) |
Philipp Maier | ffd75e4 | 2017-11-22 11:44:50 +0100 | [diff] [blame] | 304 | { |
Philipp Maier | 55295f7 | 2018-01-15 14:00:28 +0100 | [diff] [blame] | 305 | char label_string[4]; |
| 306 | |
| 307 | /* Detect empty parameters */ |
Philipp Maier | ffd75e4 | 2017-11-22 11:44:50 +0100 | [diff] [blame] | 308 | if (strlen(line) < 4) |
| 309 | goto response_parse_failure; |
| 310 | |
Philipp Maier | 55295f7 | 2018-01-15 14:00:28 +0100 | [diff] [blame] | 311 | /* Check if the label matches */ |
| 312 | snprintf(label_string, sizeof(label_string), "%c: ", label); |
| 313 | if (memcmp(label_string, line, 3) != 0) |
Philipp Maier | ffd75e4 | 2017-11-22 11:44:50 +0100 | [diff] [blame] | 314 | goto response_parse_failure; |
| 315 | |
Philipp Maier | 55295f7 | 2018-01-15 14:00:28 +0100 | [diff] [blame] | 316 | /* Copy payload part of the string to destinations (the label string |
| 317 | * is always 3 chars long) */ |
| 318 | osmo_strlcpy(result, line + 3, result_len); |
Philipp Maier | ffd75e4 | 2017-11-22 11:44:50 +0100 | [diff] [blame] | 319 | return 0; |
| 320 | |
| 321 | response_parse_failure: |
| 322 | LOGP(DLMGCP, LOGL_ERROR, |
Philipp Maier | 55295f7 | 2018-01-15 14:00:28 +0100 | [diff] [blame] | 323 | "Failed to parse MGCP response (parameter label: %c)\n", label); |
Philipp Maier | ffd75e4 | 2017-11-22 11:44:50 +0100 | [diff] [blame] | 324 | return -EINVAL; |
| 325 | } |
| 326 | |
| 327 | /* Parse MGCP parameters of the response */ |
| 328 | static int parse_head_params(struct mgcp_response *r) |
| 329 | { |
| 330 | char *line; |
| 331 | int rc = 0; |
| 332 | OSMO_ASSERT(r->body); |
Philipp Maier | 55295f7 | 2018-01-15 14:00:28 +0100 | [diff] [blame] | 333 | char *data; |
| 334 | char *data_ptr; |
| 335 | char *data_end; |
Philipp Maier | ffd75e4 | 2017-11-22 11:44:50 +0100 | [diff] [blame] | 336 | |
Philipp Maier | 55295f7 | 2018-01-15 14:00:28 +0100 | [diff] [blame] | 337 | /* Since this functions performs a destructive parsing, we create a |
| 338 | * local copy of the body data */ |
| 339 | data = talloc_zero_size(NULL, strlen(r->body)+1); |
| 340 | OSMO_ASSERT(data); |
| 341 | data_ptr = data; |
| 342 | osmo_strlcpy(data, r->body, strlen(r->body)); |
| 343 | |
| 344 | /* If there is an SDP body attached, prevent for_each_non_empty_line() |
| 345 | * into running in there, we are not yet interested in the parameters |
| 346 | * stored there. */ |
| 347 | data_end = mgcp_find_section_end(data); |
Philipp Maier | ffd75e4 | 2017-11-22 11:44:50 +0100 | [diff] [blame] | 348 | if (data_end) |
| 349 | *data_end = '\0'; |
| 350 | |
Philipp Maier | 55295f7 | 2018-01-15 14:00:28 +0100 | [diff] [blame] | 351 | for_each_non_empty_line(line, data_ptr) { |
Philipp Maier | ffd75e4 | 2017-11-22 11:44:50 +0100 | [diff] [blame] | 352 | switch (line[0]) { |
Philipp Maier | 55295f7 | 2018-01-15 14:00:28 +0100 | [diff] [blame] | 353 | case 'Z': |
| 354 | rc = mgcp_parse_head_param(r->head.endpoint, |
| 355 | sizeof(r->head.endpoint), |
| 356 | 'Z', line); |
| 357 | if (rc) |
| 358 | goto exit; |
Philipp Maier | 771b26a | 2018-01-31 13:53:11 +0100 | [diff] [blame] | 359 | |
| 360 | /* A specific endpoint identifier returned by the MGW |
| 361 | * must not contain any wildcard characters */ |
| 362 | if (strstr(r->head.endpoint, "*") != NULL) { |
| 363 | rc = -EINVAL; |
| 364 | goto exit; |
| 365 | } |
Philipp Maier | 3261dc7 | 2018-01-31 14:03:13 +0100 | [diff] [blame] | 366 | |
| 367 | /* A specific endpoint identifier returned by the MGW |
| 368 | * must contain an @ character */ |
| 369 | if (strstr(r->head.endpoint, "@") == NULL) { |
| 370 | rc = -EINVAL; |
| 371 | goto exit; |
| 372 | } |
Philipp Maier | 55295f7 | 2018-01-15 14:00:28 +0100 | [diff] [blame] | 373 | break; |
Philipp Maier | ffd75e4 | 2017-11-22 11:44:50 +0100 | [diff] [blame] | 374 | case 'I': |
Philipp Maier | 55295f7 | 2018-01-15 14:00:28 +0100 | [diff] [blame] | 375 | rc = mgcp_parse_head_param(r->head.conn_id, |
| 376 | sizeof(r->head.conn_id), |
| 377 | 'I', line); |
Philipp Maier | ffd75e4 | 2017-11-22 11:44:50 +0100 | [diff] [blame] | 378 | if (rc) |
| 379 | goto exit; |
| 380 | break; |
| 381 | default: |
| 382 | /* skip unhandled parameters */ |
| 383 | break; |
| 384 | } |
| 385 | } |
| 386 | exit: |
Philipp Maier | 55295f7 | 2018-01-15 14:00:28 +0100 | [diff] [blame] | 387 | talloc_free(data); |
Philipp Maier | ffd75e4 | 2017-11-22 11:44:50 +0100 | [diff] [blame] | 388 | return rc; |
| 389 | } |
| 390 | |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 391 | static struct mgcp_response_pending *mgcp_client_response_pending_get( |
| 392 | struct mgcp_client *mgcp, |
Neels Hofmeyr | c8f37cb | 2017-11-30 13:43:11 +0100 | [diff] [blame] | 393 | mgcp_trans_id_t trans_id) |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 394 | { |
| 395 | struct mgcp_response_pending *pending; |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 396 | llist_for_each_entry(pending, &mgcp->responses_pending, entry) { |
Neels Hofmeyr | c8f37cb | 2017-11-30 13:43:11 +0100 | [diff] [blame] | 397 | if (pending->trans_id == trans_id) { |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 398 | llist_del(&pending->entry); |
| 399 | return pending; |
| 400 | } |
| 401 | } |
| 402 | return NULL; |
| 403 | } |
| 404 | |
| 405 | /* Feed an MGCP message into the receive processing. |
| 406 | * Parse the head and call any callback registered for the transaction id found |
| 407 | * in the MGCP message. This is normally called directly from the internal |
| 408 | * mgcp_do_read that reads from the socket connected to the MGCP gateway. This |
| 409 | * function is published mainly to be able to feed data from the test suite. |
| 410 | */ |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 411 | int mgcp_client_rx(struct mgcp_client *mgcp, struct msgb *msg) |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 412 | { |
| 413 | struct mgcp_response r = { 0 }; |
| 414 | struct mgcp_response_pending *pending; |
| 415 | int rc; |
| 416 | |
| 417 | rc = mgcp_response_parse_head(&r, msg); |
| 418 | if (rc) { |
Philipp Maier | ffd75e4 | 2017-11-22 11:44:50 +0100 | [diff] [blame] | 419 | LOGP(DLMGCP, LOGL_ERROR, "Cannot parse MGCP response (head)\n"); |
| 420 | return -1; |
| 421 | } |
| 422 | |
| 423 | rc = parse_head_params(&r); |
| 424 | if (rc) { |
| 425 | LOGP(DLMGCP, LOGL_ERROR, "Cannot parse MGCP response (head parameters)\n"); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 426 | return -1; |
| 427 | } |
| 428 | |
Neels Hofmeyr | c8f37cb | 2017-11-30 13:43:11 +0100 | [diff] [blame] | 429 | pending = mgcp_client_response_pending_get(mgcp, r.head.trans_id); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 430 | if (!pending) { |
| 431 | LOGP(DLMGCP, LOGL_ERROR, |
| 432 | "Cannot find matching MGCP transaction for trans_id %d\n", |
| 433 | r.head.trans_id); |
Neels Hofmeyr | c8f37cb | 2017-11-30 13:43:11 +0100 | [diff] [blame] | 434 | return -ENOENT; |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 435 | } |
| 436 | |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 437 | mgcp_client_handle_response(mgcp, pending, &r); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 438 | return 0; |
| 439 | } |
| 440 | |
| 441 | static int mgcp_do_read(struct osmo_fd *fd) |
| 442 | { |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 443 | struct mgcp_client *mgcp = fd->data; |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 444 | struct msgb *msg; |
| 445 | int ret; |
| 446 | |
| 447 | msg = msgb_alloc_headroom(4096, 128, "mgcp_from_gw"); |
| 448 | if (!msg) { |
| 449 | LOGP(DLMGCP, LOGL_ERROR, "Failed to allocate MGCP message.\n"); |
| 450 | return -1; |
| 451 | } |
| 452 | |
| 453 | ret = read(fd->fd, msg->data, 4096 - 128); |
| 454 | if (ret <= 0) { |
| 455 | LOGP(DLMGCP, LOGL_ERROR, "Failed to read: %d/%s\n", errno, strerror(errno)); |
| 456 | msgb_free(msg); |
| 457 | return -1; |
| 458 | } else if (ret > 4096 - 128) { |
| 459 | LOGP(DLMGCP, LOGL_ERROR, "Too much data: %d\n", ret); |
| 460 | msgb_free(msg); |
| 461 | return -1; |
Harald Welte | 9bf7c53 | 2017-11-17 14:14:31 +0100 | [diff] [blame] | 462 | } |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 463 | |
| 464 | msg->l2h = msgb_put(msg, ret); |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 465 | ret = mgcp_client_rx(mgcp, msg); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 466 | talloc_free(msg); |
| 467 | return ret; |
| 468 | } |
| 469 | |
| 470 | static int mgcp_do_write(struct osmo_fd *fd, struct msgb *msg) |
| 471 | { |
| 472 | int ret; |
| 473 | static char strbuf[4096]; |
| 474 | unsigned int l = msg->len < sizeof(strbuf) ? msg->len : sizeof(strbuf); |
| 475 | unsigned int i; |
| 476 | |
Philipp Maier | f8bfbe8 | 2017-11-23 19:32:31 +0100 | [diff] [blame] | 477 | osmo_strlcpy(strbuf, (const char*)msg->data, l); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 478 | for (i = 0; i < sizeof(strbuf); i++) { |
| 479 | if (strbuf[i] == '\n' || strbuf[i] == '\r') { |
| 480 | strbuf[i] = '\0'; |
| 481 | break; |
| 482 | } |
| 483 | } |
| 484 | DEBUGP(DLMGCP, "Tx MGCP msg to MGCP GW: '%s'\n", strbuf); |
| 485 | |
| 486 | LOGP(DLMGCP, LOGL_DEBUG, "Sending msg to MGCP GW size: %u\n", msg->len); |
| 487 | |
| 488 | ret = write(fd->fd, msg->data, msg->len); |
| 489 | if (ret != msg->len) |
| 490 | LOGP(DLMGCP, LOGL_ERROR, "Failed to forward message to MGCP" |
| 491 | " GW: %s\n", strerror(errno)); |
| 492 | |
| 493 | return ret; |
| 494 | } |
| 495 | |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 496 | struct mgcp_client *mgcp_client_init(void *ctx, |
| 497 | struct mgcp_client_conf *conf) |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 498 | { |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 499 | struct mgcp_client *mgcp; |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 500 | |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 501 | mgcp = talloc_zero(ctx, struct mgcp_client); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 502 | |
| 503 | INIT_LLIST_HEAD(&mgcp->responses_pending); |
| 504 | INIT_LLIST_HEAD(&mgcp->inuse_endpoints); |
| 505 | |
| 506 | mgcp->next_trans_id = 1; |
| 507 | |
| 508 | mgcp->actual.local_addr = conf->local_addr ? conf->local_addr : |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 509 | MGCP_CLIENT_LOCAL_ADDR_DEFAULT; |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 510 | 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] | 511 | MGCP_CLIENT_LOCAL_PORT_DEFAULT; |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 512 | |
| 513 | mgcp->actual.remote_addr = conf->remote_addr ? conf->remote_addr : |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 514 | MGCP_CLIENT_REMOTE_ADDR_DEFAULT; |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 515 | 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] | 516 | MGCP_CLIENT_REMOTE_PORT_DEFAULT; |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 517 | |
| 518 | mgcp->actual.first_endpoint = conf->first_endpoint > 0 ? (uint16_t)conf->first_endpoint : 0; |
| 519 | 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] | 520 | 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] | 521 | |
| 522 | return mgcp; |
| 523 | } |
| 524 | |
Philipp Maier | 275ac97 | 2018-01-20 00:58:16 +0100 | [diff] [blame] | 525 | /*! Initalize client connection (opens socket only, no request is sent yet) |
| 526 | * \param[in,out] mgcp MGCP client descriptor. |
| 527 | * \returns 0 on success, -EINVAL on error. */ |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 528 | int mgcp_client_connect(struct mgcp_client *mgcp) |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 529 | { |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 530 | struct sockaddr_in addr; |
| 531 | struct osmo_wqueue *wq; |
| 532 | int rc; |
| 533 | |
| 534 | if (!mgcp) { |
| 535 | LOGP(DLMGCP, LOGL_FATAL, "MGCPGW client not initialized properly\n"); |
| 536 | return -EINVAL; |
| 537 | } |
| 538 | |
| 539 | wq = &mgcp->wq; |
| 540 | |
Harald Welte | 8890dfa | 2017-11-17 15:09:30 +0100 | [diff] [blame] | 541 | rc = osmo_sock_init2_ofd(&wq->bfd, AF_INET, SOCK_DGRAM, IPPROTO_UDP, |
| 542 | mgcp->actual.local_addr, mgcp->actual.local_port, |
| 543 | mgcp->actual.remote_addr, mgcp->actual.remote_port, |
| 544 | OSMO_SOCK_F_BIND | OSMO_SOCK_F_CONNECT); |
| 545 | if (rc < 0) { |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 546 | LOGP(DLMGCP, LOGL_FATAL, |
Harald Welte | 8890dfa | 2017-11-17 15:09:30 +0100 | [diff] [blame] | 547 | "Failed to initialize socket %s:%u -> %s:%u for MGCP GW: %s\n", |
| 548 | mgcp->actual.local_addr, mgcp->actual.local_port, |
| 549 | mgcp->actual.remote_addr, mgcp->actual.remote_port, strerror(errno)); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 550 | goto error_close_fd; |
| 551 | } |
| 552 | |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 553 | inet_aton(mgcp->actual.remote_addr, &addr.sin_addr); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 554 | mgcp->remote_addr = htonl(addr.sin_addr.s_addr); |
| 555 | |
| 556 | osmo_wqueue_init(wq, 10); |
| 557 | wq->bfd.when = BSC_FD_READ; |
| 558 | wq->bfd.data = mgcp; |
| 559 | wq->read_cb = mgcp_do_read; |
| 560 | wq->write_cb = mgcp_do_write; |
| 561 | |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 562 | LOGP(DLMGCP, LOGL_INFO, "MGCP GW connection: %s:%u -> %s:%u\n", |
| 563 | mgcp->actual.local_addr, mgcp->actual.local_port, |
| 564 | mgcp->actual.remote_addr, mgcp->actual.remote_port); |
| 565 | |
| 566 | return 0; |
| 567 | error_close_fd: |
| 568 | close(wq->bfd.fd); |
| 569 | wq->bfd.fd = -1; |
| 570 | return rc; |
| 571 | } |
| 572 | |
Philipp Maier | 275ac97 | 2018-01-20 00:58:16 +0100 | [diff] [blame] | 573 | /*! Get the IP-Aaddress of the associated MGW as string. |
| 574 | * \param[in] mgcp MGCP client descriptor. |
| 575 | * \returns a pointer to the address string. */ |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 576 | const char *mgcp_client_remote_addr_str(struct mgcp_client *mgcp) |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 577 | { |
| 578 | return mgcp->actual.remote_addr; |
| 579 | } |
| 580 | |
Philipp Maier | 275ac97 | 2018-01-20 00:58:16 +0100 | [diff] [blame] | 581 | /*! Get the IP-Port of the associated MGW. |
| 582 | * \param[in] mgcp MGCP client descriptor. |
| 583 | * \returns port number. */ |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 584 | uint16_t mgcp_client_remote_port(struct mgcp_client *mgcp) |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 585 | { |
| 586 | return mgcp->actual.remote_port; |
| 587 | } |
| 588 | |
Philipp Maier | 275ac97 | 2018-01-20 00:58:16 +0100 | [diff] [blame] | 589 | /*! Get the IP-Aaddress of the associated MGW as its numeric representation. |
| 590 | * \param[in] mgcp MGCP client descriptor. |
| 591 | * \returns IP-Address as 32 bit integer (network byte order) */ |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 592 | uint32_t mgcp_client_remote_addr_n(struct mgcp_client *mgcp) |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 593 | { |
| 594 | return mgcp->remote_addr; |
| 595 | } |
| 596 | |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 597 | struct mgcp_response_pending * mgcp_client_pending_add( |
| 598 | struct mgcp_client *mgcp, |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 599 | mgcp_trans_id_t trans_id, |
| 600 | mgcp_response_cb_t response_cb, |
| 601 | void *priv) |
| 602 | { |
| 603 | struct mgcp_response_pending *pending; |
| 604 | |
| 605 | pending = talloc_zero(mgcp, struct mgcp_response_pending); |
| 606 | pending->trans_id = trans_id; |
| 607 | pending->response_cb = response_cb; |
| 608 | pending->priv = priv; |
| 609 | llist_add_tail(&pending->entry, &mgcp->responses_pending); |
| 610 | |
| 611 | return pending; |
| 612 | } |
| 613 | |
| 614 | /* Send the MGCP message in msg to the MGCP GW and handle a response with |
| 615 | * response_cb. NOTE: the response_cb still needs to call |
| 616 | * mgcp_response_parse_params(response) to get the parsed parameters -- to |
| 617 | * 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] | 618 | * the response_cb is invoked. |
| 619 | * Before the priv pointer becomes invalid, e.g. due to transaction timeout, |
| 620 | * mgcp_client_cancel() needs to be called for this transaction. |
| 621 | */ |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 622 | int mgcp_client_tx(struct mgcp_client *mgcp, struct msgb *msg, |
| 623 | mgcp_response_cb_t response_cb, void *priv) |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 624 | { |
| 625 | struct mgcp_response_pending *pending; |
| 626 | mgcp_trans_id_t trans_id; |
| 627 | int rc; |
| 628 | |
| 629 | trans_id = msg->cb[MSGB_CB_MGCP_TRANS_ID]; |
| 630 | if (!trans_id) { |
| 631 | LOGP(DLMGCP, LOGL_ERROR, |
| 632 | "Unset transaction id in mgcp send request\n"); |
| 633 | talloc_free(msg); |
| 634 | return -EINVAL; |
| 635 | } |
| 636 | |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 637 | pending = mgcp_client_pending_add(mgcp, trans_id, response_cb, priv); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 638 | |
| 639 | if (msgb_l2len(msg) > 4096) { |
| 640 | LOGP(DLMGCP, LOGL_ERROR, |
| 641 | "Cannot send, MGCP message too large: %u\n", |
| 642 | msgb_l2len(msg)); |
| 643 | msgb_free(msg); |
| 644 | rc = -EINVAL; |
| 645 | goto mgcp_tx_error; |
| 646 | } |
| 647 | |
| 648 | rc = osmo_wqueue_enqueue(&mgcp->wq, msg); |
| 649 | if (rc) { |
| 650 | LOGP(DLMGCP, LOGL_FATAL, "Could not queue message to MGCP GW\n"); |
| 651 | msgb_free(msg); |
| 652 | goto mgcp_tx_error; |
| 653 | } else |
| 654 | LOGP(DLMGCP, LOGL_INFO, "Queued %u bytes for MGCP GW\n", |
| 655 | msgb_l2len(msg)); |
| 656 | return 0; |
| 657 | |
| 658 | mgcp_tx_error: |
| 659 | /* Pass NULL to response cb to indicate an error */ |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 660 | mgcp_client_handle_response(mgcp, pending, NULL); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 661 | return -1; |
| 662 | } |
| 663 | |
Philipp Maier | 275ac97 | 2018-01-20 00:58:16 +0100 | [diff] [blame] | 664 | /*! Cancel a pending transaction. |
| 665 | * \param[in] mgcp MGCP client descriptor. |
| 666 | * \param[in,out] trans_id Transaction id. |
| 667 | * \returns 0 on success, -ENOENT on error. |
| 668 | * |
Neels Hofmeyr | c8f37cb | 2017-11-30 13:43:11 +0100 | [diff] [blame] | 669 | * Should a priv pointer passed to mgcp_client_tx() become invalid, this function must be called. In |
| 670 | * practical terms, if the caller of mgcp_client_tx() wishes to tear down a transaction without having |
| 671 | * 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] | 672 | * mgcp_msg_trans_id() on the msgb produced by mgcp_msg_gen(). */ |
Neels Hofmeyr | c8f37cb | 2017-11-30 13:43:11 +0100 | [diff] [blame] | 673 | int mgcp_client_cancel(struct mgcp_client *mgcp, mgcp_trans_id_t trans_id) |
| 674 | { |
| 675 | struct mgcp_response_pending *pending = mgcp_client_response_pending_get(mgcp, trans_id); |
| 676 | if (!pending) { |
Philipp Maier | 275ac97 | 2018-01-20 00:58:16 +0100 | [diff] [blame] | 677 | /*! Note: it is not harmful to cancel a transaction twice. */ |
Neels Hofmeyr | c8f37cb | 2017-11-30 13:43:11 +0100 | [diff] [blame] | 678 | LOGP(DLMGCP, LOGL_INFO, "Cannot cancel, no such transaction: %u\n", trans_id); |
| 679 | return -ENOENT; |
| 680 | } |
| 681 | LOGP(DLMGCP, LOGL_INFO, "Canceled transaction %u\n", trans_id); |
| 682 | talloc_free(pending); |
| 683 | return 0; |
Philipp Maier | 275ac97 | 2018-01-20 00:58:16 +0100 | [diff] [blame] | 684 | /*! We don't really need to clean up the wqueue: In all sane cases, the msgb has already been sent |
| 685 | * out and is no longer in the wqueue. If it still is in the wqueue, then sending MGCP messages |
| 686 | * per se is broken and the program should notice so by a full wqueue. Even if this was called |
| 687 | * before we had a chance to send out the message and it is still going to be sent, we will just |
| 688 | * ignore the reply to it later. Removing a msgb from the wqueue here would just introduce more |
| 689 | * 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] | 690 | */ |
| 691 | } |
| 692 | |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 693 | static struct msgb *mgcp_msg_from_buf(mgcp_trans_id_t trans_id, |
| 694 | const char *buf, int len) |
| 695 | { |
| 696 | struct msgb *msg; |
| 697 | |
| 698 | if (len > (4096 - 128)) { |
| 699 | LOGP(DLMGCP, LOGL_ERROR, "Cannot send to MGCP GW:" |
| 700 | " message too large: %d\n", len); |
| 701 | return NULL; |
| 702 | } |
| 703 | |
| 704 | msg = msgb_alloc_headroom(4096, 128, "MGCP tx"); |
| 705 | OSMO_ASSERT(msg); |
| 706 | |
| 707 | char *dst = (char*)msgb_put(msg, len); |
| 708 | memcpy(dst, buf, len); |
| 709 | msg->l2h = msg->data; |
| 710 | msg->cb[MSGB_CB_MGCP_TRANS_ID] = trans_id; |
| 711 | |
| 712 | return msg; |
| 713 | } |
| 714 | |
| 715 | static struct msgb *mgcp_msg_from_str(mgcp_trans_id_t trans_id, |
| 716 | const char *fmt, ...) |
| 717 | { |
| 718 | static char compose[4096 - 128]; |
| 719 | va_list ap; |
| 720 | int len; |
| 721 | OSMO_ASSERT(fmt); |
| 722 | |
| 723 | va_start(ap, fmt); |
| 724 | len = vsnprintf(compose, sizeof(compose), fmt, ap); |
| 725 | va_end(ap); |
| 726 | if (len >= sizeof(compose)) { |
| 727 | LOGP(DLMGCP, LOGL_ERROR, |
| 728 | "Message too large: trans_id=%u len=%d\n", |
| 729 | trans_id, len); |
| 730 | return NULL; |
| 731 | } |
| 732 | if (len < 1) { |
| 733 | LOGP(DLMGCP, LOGL_ERROR, |
| 734 | "Failed to compose message: trans_id=%u len=%d\n", |
| 735 | trans_id, len); |
| 736 | return NULL; |
| 737 | } |
| 738 | return mgcp_msg_from_buf(trans_id, compose, len); |
| 739 | } |
| 740 | |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 741 | 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] | 742 | { |
| 743 | /* avoid zero trans_id to distinguish from unset trans_id */ |
| 744 | if (!mgcp->next_trans_id) |
| 745 | mgcp->next_trans_id ++; |
| 746 | return mgcp->next_trans_id ++; |
| 747 | } |
| 748 | |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 749 | struct msgb *mgcp_msg_crcx(struct mgcp_client *mgcp, |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 750 | uint16_t rtp_endpoint, unsigned int call_id, |
| 751 | enum mgcp_connection_mode mode) |
| 752 | { |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 753 | mgcp_trans_id_t trans_id = mgcp_client_next_trans_id(mgcp); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 754 | return mgcp_msg_from_str(trans_id, |
| 755 | "CRCX %u %x@mgw MGCP 1.0\r\n" |
| 756 | "C: %x\r\n" |
| 757 | "L: p:20, a:AMR, nt:IN\r\n" |
| 758 | "M: %s\r\n" |
| 759 | , |
| 760 | trans_id, |
| 761 | rtp_endpoint, |
| 762 | call_id, |
Neels Hofmeyr | d95ab1e | 2017-09-22 00:52:54 +0200 | [diff] [blame] | 763 | mgcp_client_cmode_name(mode)); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 764 | } |
| 765 | |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 766 | struct msgb *mgcp_msg_mdcx(struct mgcp_client *mgcp, |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 767 | uint16_t rtp_endpoint, const char *rtp_conn_addr, |
| 768 | uint16_t rtp_port, enum mgcp_connection_mode mode) |
| 769 | |
| 770 | { |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 771 | mgcp_trans_id_t trans_id = mgcp_client_next_trans_id(mgcp); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 772 | return mgcp_msg_from_str(trans_id, |
| 773 | "MDCX %u %x@mgw MGCP 1.0\r\n" |
| 774 | "M: %s\r\n" |
| 775 | "\r\n" |
| 776 | "c=IN IP4 %s\r\n" |
| 777 | "m=audio %u RTP/AVP 255\r\n" |
| 778 | , |
| 779 | trans_id, |
| 780 | rtp_endpoint, |
Neels Hofmeyr | d95ab1e | 2017-09-22 00:52:54 +0200 | [diff] [blame] | 781 | mgcp_client_cmode_name(mode), |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 782 | rtp_conn_addr, |
| 783 | rtp_port); |
| 784 | } |
| 785 | |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 786 | 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] | 787 | unsigned int call_id) |
| 788 | { |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 789 | mgcp_trans_id_t trans_id = mgcp_client_next_trans_id(mgcp); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 790 | return mgcp_msg_from_str(trans_id, |
| 791 | "DLCX %u %x@mgw MGCP 1.0\r\n" |
| 792 | "C: %x\r\n", trans_id, rtp_endpoint, call_id); |
| 793 | } |
| 794 | |
Philipp Maier | 1dc6be6 | 2017-10-05 18:25:37 +0200 | [diff] [blame] | 795 | #define MGCP_CRCX_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT | \ |
| 796 | MGCP_MSG_PRESENCE_CALL_ID | \ |
Philipp Maier | 1dc6be6 | 2017-10-05 18:25:37 +0200 | [diff] [blame] | 797 | MGCP_MSG_PRESENCE_CONN_MODE) |
| 798 | #define MGCP_MDCX_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT | \ |
Philipp Maier | 490cbaa | 2018-01-22 17:32:38 +0100 | [diff] [blame] | 799 | MGCP_MSG_PRESENCE_CALL_ID | \ |
Philipp Maier | 1dc6be6 | 2017-10-05 18:25:37 +0200 | [diff] [blame] | 800 | MGCP_MSG_PRESENCE_CONN_ID) |
| 801 | #define MGCP_DLCX_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT) |
| 802 | #define MGCP_AUEP_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT) |
| 803 | #define MGCP_RSIP_MANDATORY 0 /* none */ |
| 804 | |
Philipp Maier | 275ac97 | 2018-01-20 00:58:16 +0100 | [diff] [blame] | 805 | /*! Generate an MGCP message |
| 806 | * \param[in] mgcp MGCP client descriptor. |
| 807 | * \param[in] mgcp_msg Message description |
| 808 | * \returns message buffer on success, NULL on error. */ |
Philipp Maier | 1dc6be6 | 2017-10-05 18:25:37 +0200 | [diff] [blame] | 809 | struct msgb *mgcp_msg_gen(struct mgcp_client *mgcp, struct mgcp_msg *mgcp_msg) |
| 810 | { |
| 811 | mgcp_trans_id_t trans_id = mgcp_client_next_trans_id(mgcp); |
| 812 | uint32_t mandatory_mask; |
| 813 | struct msgb *msg = msgb_alloc_headroom(4096, 128, "MGCP tx"); |
| 814 | int rc = 0; |
Philipp Maier | 9d25d7a | 2018-01-22 17:31:10 +0100 | [diff] [blame] | 815 | char local_ip[INET_ADDRSTRLEN]; |
Philipp Maier | 1dc6be6 | 2017-10-05 18:25:37 +0200 | [diff] [blame] | 816 | |
| 817 | msg->l2h = msg->data; |
| 818 | msg->cb[MSGB_CB_MGCP_TRANS_ID] = trans_id; |
| 819 | |
| 820 | /* Add command verb */ |
| 821 | switch (mgcp_msg->verb) { |
| 822 | case MGCP_VERB_CRCX: |
| 823 | mandatory_mask = MGCP_CRCX_MANDATORY; |
| 824 | rc += msgb_printf(msg, "CRCX %u", trans_id); |
| 825 | break; |
| 826 | case MGCP_VERB_MDCX: |
| 827 | mandatory_mask = MGCP_MDCX_MANDATORY; |
| 828 | rc += msgb_printf(msg, "MDCX %u", trans_id); |
| 829 | break; |
| 830 | case MGCP_VERB_DLCX: |
| 831 | mandatory_mask = MGCP_DLCX_MANDATORY; |
| 832 | rc += msgb_printf(msg, "DLCX %u", trans_id); |
| 833 | break; |
| 834 | case MGCP_VERB_AUEP: |
| 835 | mandatory_mask = MGCP_AUEP_MANDATORY; |
| 836 | rc += msgb_printf(msg, "AUEP %u", trans_id); |
| 837 | break; |
| 838 | case MGCP_VERB_RSIP: |
| 839 | mandatory_mask = MGCP_RSIP_MANDATORY; |
| 840 | rc += msgb_printf(msg, "RSIP %u", trans_id); |
| 841 | break; |
| 842 | default: |
| 843 | LOGP(DLMGCP, LOGL_ERROR, |
| 844 | "Invalid command verb, can not generate MGCP message\n"); |
| 845 | msgb_free(msg); |
| 846 | return NULL; |
| 847 | } |
| 848 | |
| 849 | /* Check if mandatory fields are missing */ |
| 850 | if (!((mgcp_msg->presence & mandatory_mask) == mandatory_mask)) { |
| 851 | LOGP(DLMGCP, LOGL_ERROR, |
| 852 | "One or more missing mandatory fields, can not generate MGCP message\n"); |
| 853 | msgb_free(msg); |
| 854 | return NULL; |
| 855 | } |
| 856 | |
| 857 | /* Add endpoint name */ |
Philipp Maier | 7bc5552 | 2017-12-10 22:52:22 +0100 | [diff] [blame] | 858 | if (mgcp_msg->presence & MGCP_MSG_PRESENCE_ENDPOINT) { |
| 859 | if (strlen(mgcp_msg->endpoint) <= 0) { |
| 860 | LOGP(DLMGCP, LOGL_ERROR, |
| 861 | "Empty endpoint name, can not generate MGCP message\n"); |
| 862 | msgb_free(msg); |
| 863 | return NULL; |
| 864 | } |
Philipp Maier | 3aa8157 | 2018-01-31 14:13:45 +0100 | [diff] [blame] | 865 | |
| 866 | if (strstr(mgcp_msg->endpoint, "@") == NULL) { |
| 867 | LOGP(DLMGCP, LOGL_ERROR, |
| 868 | "Endpoint name (%s) lacks separator (@), can not generate MGCP message\n", |
| 869 | mgcp_msg->endpoint); |
| 870 | msgb_free(msg); |
| 871 | } |
| 872 | |
Philipp Maier | 1dc6be6 | 2017-10-05 18:25:37 +0200 | [diff] [blame] | 873 | rc += msgb_printf(msg, " %s", mgcp_msg->endpoint); |
Philipp Maier | 7bc5552 | 2017-12-10 22:52:22 +0100 | [diff] [blame] | 874 | } |
Philipp Maier | 1dc6be6 | 2017-10-05 18:25:37 +0200 | [diff] [blame] | 875 | |
| 876 | /* Add protocol version */ |
| 877 | rc += msgb_printf(msg, " MGCP 1.0\r\n"); |
| 878 | |
| 879 | /* Add call id */ |
| 880 | if (mgcp_msg->presence & MGCP_MSG_PRESENCE_CALL_ID) |
| 881 | rc += msgb_printf(msg, "C: %x\r\n", mgcp_msg->call_id); |
| 882 | |
| 883 | /* Add connection id */ |
Philipp Maier | 7bc5552 | 2017-12-10 22:52:22 +0100 | [diff] [blame] | 884 | if (mgcp_msg->presence & MGCP_MSG_PRESENCE_CONN_ID) { |
| 885 | if (strlen(mgcp_msg->conn_id) <= 0) { |
| 886 | LOGP(DLMGCP, LOGL_ERROR, |
| 887 | "Empty connection id, can not generate MGCP message\n"); |
| 888 | msgb_free(msg); |
| 889 | return NULL; |
| 890 | } |
Philipp Maier | 01d24a3 | 2017-11-21 17:26:09 +0100 | [diff] [blame] | 891 | rc += msgb_printf(msg, "I: %s\r\n", mgcp_msg->conn_id); |
Philipp Maier | 7bc5552 | 2017-12-10 22:52:22 +0100 | [diff] [blame] | 892 | } |
Philipp Maier | 1dc6be6 | 2017-10-05 18:25:37 +0200 | [diff] [blame] | 893 | |
| 894 | /* Add local connection options */ |
Philipp Maier | ffd75e4 | 2017-11-22 11:44:50 +0100 | [diff] [blame] | 895 | if (mgcp_msg->verb == MGCP_VERB_CRCX) |
Philipp Maier | 1dc6be6 | 2017-10-05 18:25:37 +0200 | [diff] [blame] | 896 | rc += msgb_printf(msg, "L: p:20, a:AMR, nt:IN\r\n"); |
| 897 | |
| 898 | /* Add mode */ |
| 899 | if (mgcp_msg->presence & MGCP_MSG_PRESENCE_CONN_MODE) |
| 900 | rc += |
| 901 | msgb_printf(msg, "M: %s\r\n", |
| 902 | mgcp_client_cmode_name(mgcp_msg->conn_mode)); |
| 903 | |
Philipp Maier | 9d25d7a | 2018-01-22 17:31:10 +0100 | [diff] [blame] | 904 | /* Add SDP body */ |
Philipp Maier | 1dc6be6 | 2017-10-05 18:25:37 +0200 | [diff] [blame] | 905 | if (mgcp_msg->presence & MGCP_MSG_PRESENCE_AUDIO_IP |
| 906 | && mgcp_msg->presence & MGCP_MSG_PRESENCE_AUDIO_PORT) { |
Philipp Maier | 9d25d7a | 2018-01-22 17:31:10 +0100 | [diff] [blame] | 907 | |
| 908 | /* Add separator to mark the beginning of the SDP block */ |
| 909 | rc += msgb_printf(msg, "\r\n"); |
| 910 | |
| 911 | /* Add SDP protocol version */ |
| 912 | rc += msgb_printf(msg, "v=0\r\n"); |
| 913 | |
Philipp Maier | 9d25d7a | 2018-01-22 17:31:10 +0100 | [diff] [blame] | 914 | /* Determine local IP-Address */ |
| 915 | if (osmo_sock_local_ip(local_ip, mgcp->actual.remote_addr) < 0) { |
| 916 | LOGP(DLMGCP, LOGL_ERROR, |
| 917 | "Could not determine local IP-Address!\n"); |
| 918 | msgb_free(msg); |
| 919 | return NULL; |
| 920 | } |
| 921 | |
| 922 | /* Add owner/creator (SDP) */ |
| 923 | rc += msgb_printf(msg, "o=- %x 23 IN IP4 %s\r\n", |
| 924 | mgcp_msg->call_id, local_ip); |
| 925 | |
Philipp Maier | b759473 | 2018-01-31 16:44:00 +0100 | [diff] [blame] | 926 | /* Add session name (none) */ |
| 927 | rc += msgb_printf(msg, "s=-\r\n"); |
| 928 | |
Philipp Maier | 9d25d7a | 2018-01-22 17:31:10 +0100 | [diff] [blame] | 929 | /* Add RTP address and port */ |
Philipp Maier | 7bc5552 | 2017-12-10 22:52:22 +0100 | [diff] [blame] | 930 | if (mgcp_msg->audio_port == 0) { |
| 931 | LOGP(DLMGCP, LOGL_ERROR, |
| 932 | "Invalid port number, can not generate MGCP message\n"); |
| 933 | msgb_free(msg); |
| 934 | return NULL; |
| 935 | } |
| 936 | if (strlen(mgcp_msg->audio_ip) <= 0) { |
| 937 | LOGP(DLMGCP, LOGL_ERROR, |
| 938 | "Empty ip address, can not generate MGCP message\n"); |
| 939 | msgb_free(msg); |
| 940 | return NULL; |
| 941 | } |
Philipp Maier | 1dc6be6 | 2017-10-05 18:25:37 +0200 | [diff] [blame] | 942 | 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] | 943 | |
| 944 | /* Add time description, active time (SDP) */ |
| 945 | rc += msgb_printf(msg, "t=0 0\r\n"); |
Philipp Maier | b759473 | 2018-01-31 16:44:00 +0100 | [diff] [blame] | 946 | |
| 947 | rc += |
| 948 | msgb_printf(msg, "m=audio %u RTP/AVP 255\r\n", |
| 949 | mgcp_msg->audio_port); |
Philipp Maier | 1dc6be6 | 2017-10-05 18:25:37 +0200 | [diff] [blame] | 950 | } |
| 951 | |
| 952 | if (rc != 0) { |
| 953 | LOGP(DLMGCP, LOGL_ERROR, |
| 954 | "message buffer to small, can not generate MGCP message\n"); |
| 955 | msgb_free(msg); |
| 956 | msg = NULL; |
| 957 | } |
| 958 | |
| 959 | return msg; |
| 960 | } |
| 961 | |
Philipp Maier | 275ac97 | 2018-01-20 00:58:16 +0100 | [diff] [blame] | 962 | /*! Retrieve the MGCP transaction ID from a msgb generated by mgcp_msg_gen() |
| 963 | * \param[in] msg message buffer |
| 964 | * \returns Transaction id. */ |
Neels Hofmeyr | c8f37cb | 2017-11-30 13:43:11 +0100 | [diff] [blame] | 965 | mgcp_trans_id_t mgcp_msg_trans_id(struct msgb *msg) |
| 966 | { |
| 967 | return (mgcp_trans_id_t)msg->cb[MSGB_CB_MGCP_TRANS_ID]; |
| 968 | } |
| 969 | |
Philipp Maier | 275ac97 | 2018-01-20 00:58:16 +0100 | [diff] [blame] | 970 | /*! Get the configuration parameters a given MGCP client instance |
| 971 | * \param[in] mgcp MGCP client descriptor. |
| 972 | * \returns configuration */ |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 973 | struct mgcp_client_conf *mgcp_client_conf_actual(struct mgcp_client *mgcp) |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 974 | { |
| 975 | return &mgcp->actual; |
| 976 | } |
Neels Hofmeyr | d95ab1e | 2017-09-22 00:52:54 +0200 | [diff] [blame] | 977 | |
| 978 | const struct value_string mgcp_client_connection_mode_strs[] = { |
| 979 | { MGCP_CONN_NONE, "none" }, |
| 980 | { MGCP_CONN_RECV_SEND, "sendrecv" }, |
| 981 | { MGCP_CONN_SEND_ONLY, "sendonly" }, |
| 982 | { MGCP_CONN_RECV_ONLY, "recvonly" }, |
| 983 | { MGCP_CONN_LOOPBACK, "loopback" }, |
| 984 | { 0, NULL } |
| 985 | }; |