Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 1 | /* mgcp_utils - common functions to setup an MGCP connection |
| 2 | */ |
| 3 | /* (C) 2016 by sysmocom s.f.m.c. GmbH <info@sysmocom.de> |
| 4 | * All Rights Reserved |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU Affero General Public License as published by |
| 8 | * the Free Software Foundation; either version 3 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU Affero General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU Affero General Public License |
| 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | */ |
| 20 | |
| 21 | #include <osmocom/core/linuxlist.h> |
| 22 | #include <osmocom/core/select.h> |
| 23 | #include <osmocom/core/write_queue.h> |
| 24 | #include <osmocom/core/msgb.h> |
| 25 | #include <osmocom/core/logging.h> |
Philipp Maier | 1dc6be6 | 2017-10-05 18:25:37 +0200 | [diff] [blame] | 26 | #include <osmocom/core/byteswap.h> |
Harald Welte | 8890dfa | 2017-11-17 15:09:30 +0100 | [diff] [blame] | 27 | #include <osmocom/core/socket.h> |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 28 | |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 29 | #include <osmocom/mgcp_client/mgcp_client.h> |
| 30 | #include <osmocom/mgcp_client/mgcp_client_internal.h> |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 31 | |
| 32 | #include <netinet/in.h> |
| 33 | #include <arpa/inet.h> |
| 34 | |
| 35 | #include <errno.h> |
| 36 | #include <unistd.h> |
| 37 | #include <string.h> |
| 38 | |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 39 | void mgcp_client_conf_init(struct mgcp_client_conf *conf) |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 40 | { |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 41 | /* NULL and -1 default to MGCP_CLIENT_*_DEFAULT values */ |
| 42 | *conf = (struct mgcp_client_conf){ |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 43 | .local_addr = NULL, |
| 44 | .local_port = -1, |
| 45 | .remote_addr = NULL, |
| 46 | .remote_port = -1, |
| 47 | .first_endpoint = 0, |
| 48 | .last_endpoint = 0, |
Neels Hofmeyr | c0dcc3c | 2017-12-02 18:31:34 +0000 | [diff] [blame] | 49 | .bts_base = 0, |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 50 | }; |
| 51 | } |
| 52 | |
| 53 | /* Test if a given endpoint id is currently in use */ |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 54 | static bool endpoint_in_use(uint16_t id, struct mgcp_client *client) |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 55 | { |
| 56 | struct mgcp_inuse_endpoint *endpoint; |
| 57 | llist_for_each_entry(endpoint, &client->inuse_endpoints, entry) { |
| 58 | if (endpoint->id == id) |
| 59 | return true; |
| 60 | } |
| 61 | |
| 62 | return false; |
| 63 | } |
| 64 | |
| 65 | /* Find and seize an unsused endpoint id */ |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 66 | int mgcp_client_next_endpoint(struct mgcp_client *client) |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 67 | { |
| 68 | int i; |
| 69 | uint16_t first_endpoint = client->actual.first_endpoint; |
| 70 | uint16_t last_endpoint = client->actual.last_endpoint; |
| 71 | struct mgcp_inuse_endpoint *endpoint; |
| 72 | |
| 73 | /* Use the maximum permitted range if the VTY |
| 74 | * configuration does not specify a range */ |
| 75 | if (client->actual.last_endpoint == 0) { |
| 76 | first_endpoint = 1; |
| 77 | last_endpoint = 65534; |
| 78 | } |
| 79 | |
| 80 | /* Test the permitted endpoint range for an endpoint |
| 81 | * number that is not in use. When a suitable endpoint |
| 82 | * number can be found, seize it by adding it to the |
| 83 | * inuse list. */ |
| 84 | for (i=first_endpoint;i<last_endpoint;i++) |
| 85 | { |
| 86 | if (endpoint_in_use(i,client) == false) { |
| 87 | endpoint = talloc_zero(client, struct mgcp_inuse_endpoint); |
| 88 | endpoint->id = i; |
| 89 | llist_add_tail(&endpoint->entry, &client->inuse_endpoints); |
| 90 | return endpoint->id; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | /* All endpoints are busy! */ |
| 95 | return -EINVAL; |
| 96 | } |
| 97 | |
| 98 | /* Release a seized endpoint id to make it available again for other calls */ |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 99 | void mgcp_client_release_endpoint(uint16_t id, struct mgcp_client *client) |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 100 | { |
| 101 | struct mgcp_inuse_endpoint *endpoint; |
| 102 | struct mgcp_inuse_endpoint *endpoint_tmp; |
| 103 | llist_for_each_entry_safe(endpoint, endpoint_tmp, &client->inuse_endpoints, entry) { |
| 104 | if (endpoint->id == id) { |
| 105 | llist_del(&endpoint->entry); |
| 106 | talloc_free(endpoint); |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 111 | static void mgcp_client_handle_response(struct mgcp_client *mgcp, |
| 112 | struct mgcp_response_pending *pending, |
| 113 | struct mgcp_response *response) |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 114 | { |
| 115 | if (!pending) { |
| 116 | LOGP(DLMGCP, LOGL_ERROR, |
| 117 | "Cannot handle NULL response\n"); |
| 118 | return; |
| 119 | } |
| 120 | if (pending->response_cb) |
| 121 | pending->response_cb(response, pending->priv); |
| 122 | else |
| 123 | LOGP(DLMGCP, LOGL_INFO, "MGCP response ignored (NULL cb)\n"); |
| 124 | talloc_free(pending); |
| 125 | } |
| 126 | |
| 127 | static int mgcp_response_parse_head(struct mgcp_response *r, struct msgb *msg) |
| 128 | { |
| 129 | int comment_pos; |
| 130 | char *end; |
| 131 | |
| 132 | if (mgcp_msg_terminate_nul(msg)) |
| 133 | goto response_parse_failure; |
| 134 | |
| 135 | r->body = (char *)msg->data; |
| 136 | |
Harald Welte | 9bf7c53 | 2017-11-17 14:14:31 +0100 | [diff] [blame] | 137 | if (sscanf(r->body, "%3d %u %n", |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 138 | &r->head.response_code, &r->head.trans_id, |
| 139 | &comment_pos) != 2) |
| 140 | goto response_parse_failure; |
| 141 | |
| 142 | r->head.comment = r->body + comment_pos; |
| 143 | end = strchr(r->head.comment, '\r'); |
| 144 | if (!end) |
| 145 | goto response_parse_failure; |
| 146 | /* Mark the end of the comment */ |
| 147 | *end = '\0'; |
| 148 | r->body = end + 1; |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 149 | return 0; |
| 150 | |
| 151 | response_parse_failure: |
| 152 | LOGP(DLMGCP, LOGL_ERROR, |
| 153 | "Failed to parse MGCP response header\n"); |
| 154 | return -EINVAL; |
| 155 | } |
| 156 | |
| 157 | /* TODO undup against mgcp_protocol.c:mgcp_check_param() */ |
| 158 | static bool mgcp_line_is_valid(const char *line) |
| 159 | { |
| 160 | const size_t line_len = strlen(line); |
| 161 | if (line[0] == '\0') |
| 162 | return true; |
| 163 | |
| 164 | if (line_len < 2 |
| 165 | || line[1] != '=') { |
| 166 | LOGP(DLMGCP, LOGL_ERROR, |
| 167 | "Wrong MGCP option format: '%s'\n", |
| 168 | line); |
| 169 | return false; |
| 170 | } |
| 171 | |
| 172 | return true; |
| 173 | } |
| 174 | |
| 175 | /* Parse a line like "m=audio 16002 RTP/AVP 98" */ |
Philipp Maier | 06da85e | 2017-10-05 18:49:24 +0200 | [diff] [blame] | 176 | 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] | 177 | { |
Harald Welte | 9bf7c53 | 2017-11-17 14:14:31 +0100 | [diff] [blame] | 178 | if (sscanf(line, "m=audio %hu", |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 179 | &r->audio_port) != 1) |
| 180 | goto response_parse_failure; |
| 181 | |
Philipp Maier | 10f32db | 2017-12-13 12:34:34 +0100 | [diff] [blame] | 182 | if (r->audio_port == 0) |
| 183 | goto response_parse_failure; |
| 184 | |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 185 | return 0; |
| 186 | |
| 187 | response_parse_failure: |
| 188 | LOGP(DLMGCP, LOGL_ERROR, |
Philipp Maier | 06da85e | 2017-10-05 18:49:24 +0200 | [diff] [blame] | 189 | "Failed to parse MGCP response header (audio port)\n"); |
| 190 | return -EINVAL; |
| 191 | } |
| 192 | |
| 193 | /* Parse a line like "c=IN IP4 10.11.12.13" */ |
| 194 | static int mgcp_parse_audio_ip(struct mgcp_response *r, const char *line) |
| 195 | { |
| 196 | struct in_addr ip_test; |
| 197 | |
| 198 | if (strlen(line) < 16) |
| 199 | goto response_parse_failure; |
| 200 | |
| 201 | /* The current implementation strictly supports IPV4 only ! */ |
| 202 | if (memcmp("c=IN IP4 ", line, 9) != 0) |
| 203 | goto response_parse_failure; |
| 204 | |
| 205 | /* Extract IP-Address */ |
Philipp Maier | f8bfbe8 | 2017-11-23 19:32:31 +0100 | [diff] [blame] | 206 | osmo_strlcpy(r->audio_ip, line + 9, sizeof(r->audio_ip)); |
Philipp Maier | 06da85e | 2017-10-05 18:49:24 +0200 | [diff] [blame] | 207 | |
| 208 | /* Check IP-Address */ |
| 209 | if (inet_aton(r->audio_ip, &ip_test) == 0) |
| 210 | goto response_parse_failure; |
| 211 | |
| 212 | return 0; |
| 213 | |
| 214 | response_parse_failure: |
| 215 | LOGP(DLMGCP, LOGL_ERROR, |
| 216 | "Failed to parse MGCP response header (audio ip)\n"); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 217 | return -EINVAL; |
| 218 | } |
| 219 | |
Philipp Maier | 3b12e1b | 2018-01-18 15:16:13 +0100 | [diff] [blame] | 220 | /* A new section is marked by a double line break, check a few more |
| 221 | * patterns as there may be variants */ |
| 222 | static char *mgcp_find_section_end(char *string) |
| 223 | { |
| 224 | char *rc; |
| 225 | |
| 226 | rc = strstr(string, "\n\n"); |
| 227 | if (rc) |
| 228 | return rc; |
| 229 | |
| 230 | rc = strstr(string, "\n\r\n\r"); |
| 231 | if (rc) |
| 232 | return rc; |
| 233 | |
| 234 | rc = strstr(string, "\r\n\r\n"); |
| 235 | if (rc) |
| 236 | return rc; |
| 237 | |
| 238 | return NULL; |
| 239 | } |
| 240 | |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 241 | int mgcp_response_parse_params(struct mgcp_response *r) |
| 242 | { |
| 243 | char *line; |
| 244 | int rc; |
| 245 | OSMO_ASSERT(r->body); |
Philipp Maier | 3b12e1b | 2018-01-18 15:16:13 +0100 | [diff] [blame] | 246 | char *data = mgcp_find_section_end(r->body); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 247 | |
Philipp Maier | 55295f7 | 2018-01-15 14:00:28 +0100 | [diff] [blame^] | 248 | /* Warning: This function performs a destructive parsing on r->body. |
| 249 | * Since this function is called at the very end of the persing |
| 250 | * process, destructive parsing is acceptable. */ |
| 251 | |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 252 | if (!data) { |
| 253 | LOGP(DLMGCP, LOGL_ERROR, |
| 254 | "MGCP response: cannot find start of parameters\n"); |
| 255 | return -EINVAL; |
| 256 | } |
| 257 | |
| 258 | /* Advance to after the \n\n, replace the second \n with \0. That's |
| 259 | * where the parameters start. */ |
| 260 | data ++; |
| 261 | *data = '\0'; |
| 262 | data ++; |
| 263 | |
Neels Hofmeyr | d95ab1e | 2017-09-22 00:52:54 +0200 | [diff] [blame] | 264 | for_each_non_empty_line(line, data) { |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 265 | if (!mgcp_line_is_valid(line)) |
| 266 | return -EINVAL; |
| 267 | |
| 268 | switch (line[0]) { |
| 269 | case 'm': |
Philipp Maier | 06da85e | 2017-10-05 18:49:24 +0200 | [diff] [blame] | 270 | rc = mgcp_parse_audio_port(r, line); |
| 271 | if (rc) |
| 272 | return rc; |
| 273 | break; |
| 274 | case 'c': |
| 275 | rc = mgcp_parse_audio_ip(r, line); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 276 | if (rc) |
| 277 | return rc; |
| 278 | break; |
| 279 | default: |
| 280 | /* skip unhandled parameters */ |
| 281 | break; |
| 282 | } |
| 283 | } |
| 284 | return 0; |
| 285 | } |
| 286 | |
Philipp Maier | 55295f7 | 2018-01-15 14:00:28 +0100 | [diff] [blame^] | 287 | /* Parse a line like "X: something" */ |
| 288 | static int mgcp_parse_head_param(char *result, unsigned int result_len, |
| 289 | char label, const char *line) |
Philipp Maier | ffd75e4 | 2017-11-22 11:44:50 +0100 | [diff] [blame] | 290 | { |
Philipp Maier | 55295f7 | 2018-01-15 14:00:28 +0100 | [diff] [blame^] | 291 | char label_string[4]; |
| 292 | |
| 293 | /* Detect empty parameters */ |
Philipp Maier | ffd75e4 | 2017-11-22 11:44:50 +0100 | [diff] [blame] | 294 | if (strlen(line) < 4) |
| 295 | goto response_parse_failure; |
| 296 | |
Philipp Maier | 55295f7 | 2018-01-15 14:00:28 +0100 | [diff] [blame^] | 297 | /* Check if the label matches */ |
| 298 | snprintf(label_string, sizeof(label_string), "%c: ", label); |
| 299 | if (memcmp(label_string, line, 3) != 0) |
Philipp Maier | ffd75e4 | 2017-11-22 11:44:50 +0100 | [diff] [blame] | 300 | goto response_parse_failure; |
| 301 | |
Philipp Maier | 55295f7 | 2018-01-15 14:00:28 +0100 | [diff] [blame^] | 302 | /* Copy payload part of the string to destinations (the label string |
| 303 | * is always 3 chars long) */ |
| 304 | osmo_strlcpy(result, line + 3, result_len); |
Philipp Maier | ffd75e4 | 2017-11-22 11:44:50 +0100 | [diff] [blame] | 305 | return 0; |
| 306 | |
| 307 | response_parse_failure: |
| 308 | LOGP(DLMGCP, LOGL_ERROR, |
Philipp Maier | 55295f7 | 2018-01-15 14:00:28 +0100 | [diff] [blame^] | 309 | "Failed to parse MGCP response (parameter label: %c)\n", label); |
Philipp Maier | ffd75e4 | 2017-11-22 11:44:50 +0100 | [diff] [blame] | 310 | return -EINVAL; |
| 311 | } |
| 312 | |
| 313 | /* Parse MGCP parameters of the response */ |
| 314 | static int parse_head_params(struct mgcp_response *r) |
| 315 | { |
| 316 | char *line; |
| 317 | int rc = 0; |
| 318 | OSMO_ASSERT(r->body); |
Philipp Maier | 55295f7 | 2018-01-15 14:00:28 +0100 | [diff] [blame^] | 319 | char *data; |
| 320 | char *data_ptr; |
| 321 | char *data_end; |
Philipp Maier | ffd75e4 | 2017-11-22 11:44:50 +0100 | [diff] [blame] | 322 | |
Philipp Maier | 55295f7 | 2018-01-15 14:00:28 +0100 | [diff] [blame^] | 323 | /* Since this functions performs a destructive parsing, we create a |
| 324 | * local copy of the body data */ |
| 325 | data = talloc_zero_size(NULL, strlen(r->body)+1); |
| 326 | OSMO_ASSERT(data); |
| 327 | data_ptr = data; |
| 328 | osmo_strlcpy(data, r->body, strlen(r->body)); |
| 329 | |
| 330 | /* If there is an SDP body attached, prevent for_each_non_empty_line() |
| 331 | * into running in there, we are not yet interested in the parameters |
| 332 | * stored there. */ |
| 333 | data_end = mgcp_find_section_end(data); |
Philipp Maier | ffd75e4 | 2017-11-22 11:44:50 +0100 | [diff] [blame] | 334 | if (data_end) |
| 335 | *data_end = '\0'; |
| 336 | |
Philipp Maier | 55295f7 | 2018-01-15 14:00:28 +0100 | [diff] [blame^] | 337 | for_each_non_empty_line(line, data_ptr) { |
Philipp Maier | ffd75e4 | 2017-11-22 11:44:50 +0100 | [diff] [blame] | 338 | switch (line[0]) { |
Philipp Maier | 55295f7 | 2018-01-15 14:00:28 +0100 | [diff] [blame^] | 339 | case 'Z': |
| 340 | rc = mgcp_parse_head_param(r->head.endpoint, |
| 341 | sizeof(r->head.endpoint), |
| 342 | 'Z', line); |
| 343 | if (rc) |
| 344 | goto exit; |
| 345 | break; |
Philipp Maier | ffd75e4 | 2017-11-22 11:44:50 +0100 | [diff] [blame] | 346 | case 'I': |
Philipp Maier | 55295f7 | 2018-01-15 14:00:28 +0100 | [diff] [blame^] | 347 | rc = mgcp_parse_head_param(r->head.conn_id, |
| 348 | sizeof(r->head.conn_id), |
| 349 | 'I', line); |
Philipp Maier | ffd75e4 | 2017-11-22 11:44:50 +0100 | [diff] [blame] | 350 | if (rc) |
| 351 | goto exit; |
| 352 | break; |
| 353 | default: |
| 354 | /* skip unhandled parameters */ |
| 355 | break; |
| 356 | } |
| 357 | } |
| 358 | exit: |
Philipp Maier | 55295f7 | 2018-01-15 14:00:28 +0100 | [diff] [blame^] | 359 | talloc_free(data); |
Philipp Maier | ffd75e4 | 2017-11-22 11:44:50 +0100 | [diff] [blame] | 360 | return rc; |
| 361 | } |
| 362 | |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 363 | static struct mgcp_response_pending *mgcp_client_response_pending_get( |
| 364 | struct mgcp_client *mgcp, |
Neels Hofmeyr | c8f37cb | 2017-11-30 13:43:11 +0100 | [diff] [blame] | 365 | mgcp_trans_id_t trans_id) |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 366 | { |
| 367 | struct mgcp_response_pending *pending; |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 368 | llist_for_each_entry(pending, &mgcp->responses_pending, entry) { |
Neels Hofmeyr | c8f37cb | 2017-11-30 13:43:11 +0100 | [diff] [blame] | 369 | if (pending->trans_id == trans_id) { |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 370 | llist_del(&pending->entry); |
| 371 | return pending; |
| 372 | } |
| 373 | } |
| 374 | return NULL; |
| 375 | } |
| 376 | |
| 377 | /* Feed an MGCP message into the receive processing. |
| 378 | * Parse the head and call any callback registered for the transaction id found |
| 379 | * in the MGCP message. This is normally called directly from the internal |
| 380 | * mgcp_do_read that reads from the socket connected to the MGCP gateway. This |
| 381 | * function is published mainly to be able to feed data from the test suite. |
| 382 | */ |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 383 | int mgcp_client_rx(struct mgcp_client *mgcp, struct msgb *msg) |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 384 | { |
| 385 | struct mgcp_response r = { 0 }; |
| 386 | struct mgcp_response_pending *pending; |
| 387 | int rc; |
| 388 | |
| 389 | rc = mgcp_response_parse_head(&r, msg); |
| 390 | if (rc) { |
Philipp Maier | ffd75e4 | 2017-11-22 11:44:50 +0100 | [diff] [blame] | 391 | LOGP(DLMGCP, LOGL_ERROR, "Cannot parse MGCP response (head)\n"); |
| 392 | return -1; |
| 393 | } |
| 394 | |
| 395 | rc = parse_head_params(&r); |
| 396 | if (rc) { |
| 397 | LOGP(DLMGCP, LOGL_ERROR, "Cannot parse MGCP response (head parameters)\n"); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 398 | return -1; |
| 399 | } |
| 400 | |
Neels Hofmeyr | c8f37cb | 2017-11-30 13:43:11 +0100 | [diff] [blame] | 401 | pending = mgcp_client_response_pending_get(mgcp, r.head.trans_id); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 402 | if (!pending) { |
| 403 | LOGP(DLMGCP, LOGL_ERROR, |
| 404 | "Cannot find matching MGCP transaction for trans_id %d\n", |
| 405 | r.head.trans_id); |
Neels Hofmeyr | c8f37cb | 2017-11-30 13:43:11 +0100 | [diff] [blame] | 406 | return -ENOENT; |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 407 | } |
| 408 | |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 409 | mgcp_client_handle_response(mgcp, pending, &r); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 410 | return 0; |
| 411 | } |
| 412 | |
| 413 | static int mgcp_do_read(struct osmo_fd *fd) |
| 414 | { |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 415 | struct mgcp_client *mgcp = fd->data; |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 416 | struct msgb *msg; |
| 417 | int ret; |
| 418 | |
| 419 | msg = msgb_alloc_headroom(4096, 128, "mgcp_from_gw"); |
| 420 | if (!msg) { |
| 421 | LOGP(DLMGCP, LOGL_ERROR, "Failed to allocate MGCP message.\n"); |
| 422 | return -1; |
| 423 | } |
| 424 | |
| 425 | ret = read(fd->fd, msg->data, 4096 - 128); |
| 426 | if (ret <= 0) { |
| 427 | LOGP(DLMGCP, LOGL_ERROR, "Failed to read: %d/%s\n", errno, strerror(errno)); |
| 428 | msgb_free(msg); |
| 429 | return -1; |
| 430 | } else if (ret > 4096 - 128) { |
| 431 | LOGP(DLMGCP, LOGL_ERROR, "Too much data: %d\n", ret); |
| 432 | msgb_free(msg); |
| 433 | return -1; |
Harald Welte | 9bf7c53 | 2017-11-17 14:14:31 +0100 | [diff] [blame] | 434 | } |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 435 | |
| 436 | msg->l2h = msgb_put(msg, ret); |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 437 | ret = mgcp_client_rx(mgcp, msg); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 438 | talloc_free(msg); |
| 439 | return ret; |
| 440 | } |
| 441 | |
| 442 | static int mgcp_do_write(struct osmo_fd *fd, struct msgb *msg) |
| 443 | { |
| 444 | int ret; |
| 445 | static char strbuf[4096]; |
| 446 | unsigned int l = msg->len < sizeof(strbuf) ? msg->len : sizeof(strbuf); |
| 447 | unsigned int i; |
| 448 | |
Philipp Maier | f8bfbe8 | 2017-11-23 19:32:31 +0100 | [diff] [blame] | 449 | osmo_strlcpy(strbuf, (const char*)msg->data, l); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 450 | for (i = 0; i < sizeof(strbuf); i++) { |
| 451 | if (strbuf[i] == '\n' || strbuf[i] == '\r') { |
| 452 | strbuf[i] = '\0'; |
| 453 | break; |
| 454 | } |
| 455 | } |
| 456 | DEBUGP(DLMGCP, "Tx MGCP msg to MGCP GW: '%s'\n", strbuf); |
| 457 | |
| 458 | LOGP(DLMGCP, LOGL_DEBUG, "Sending msg to MGCP GW size: %u\n", msg->len); |
| 459 | |
| 460 | ret = write(fd->fd, msg->data, msg->len); |
| 461 | if (ret != msg->len) |
| 462 | LOGP(DLMGCP, LOGL_ERROR, "Failed to forward message to MGCP" |
| 463 | " GW: %s\n", strerror(errno)); |
| 464 | |
| 465 | return ret; |
| 466 | } |
| 467 | |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 468 | struct mgcp_client *mgcp_client_init(void *ctx, |
| 469 | struct mgcp_client_conf *conf) |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 470 | { |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 471 | struct mgcp_client *mgcp; |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 472 | |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 473 | mgcp = talloc_zero(ctx, struct mgcp_client); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 474 | |
| 475 | INIT_LLIST_HEAD(&mgcp->responses_pending); |
| 476 | INIT_LLIST_HEAD(&mgcp->inuse_endpoints); |
| 477 | |
| 478 | mgcp->next_trans_id = 1; |
| 479 | |
| 480 | mgcp->actual.local_addr = conf->local_addr ? conf->local_addr : |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 481 | MGCP_CLIENT_LOCAL_ADDR_DEFAULT; |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 482 | 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] | 483 | MGCP_CLIENT_LOCAL_PORT_DEFAULT; |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 484 | |
| 485 | mgcp->actual.remote_addr = conf->remote_addr ? conf->remote_addr : |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 486 | MGCP_CLIENT_REMOTE_ADDR_DEFAULT; |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 487 | 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] | 488 | MGCP_CLIENT_REMOTE_PORT_DEFAULT; |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 489 | |
| 490 | mgcp->actual.first_endpoint = conf->first_endpoint > 0 ? (uint16_t)conf->first_endpoint : 0; |
| 491 | mgcp->actual.last_endpoint = conf->last_endpoint > 0 ? (uint16_t)conf->last_endpoint : 0; |
Neels Hofmeyr | c0dcc3c | 2017-12-02 18:31:34 +0000 | [diff] [blame] | 492 | 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] | 493 | |
| 494 | return mgcp; |
| 495 | } |
| 496 | |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 497 | int mgcp_client_connect(struct mgcp_client *mgcp) |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 498 | { |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 499 | struct sockaddr_in addr; |
| 500 | struct osmo_wqueue *wq; |
| 501 | int rc; |
| 502 | |
| 503 | if (!mgcp) { |
| 504 | LOGP(DLMGCP, LOGL_FATAL, "MGCPGW client not initialized properly\n"); |
| 505 | return -EINVAL; |
| 506 | } |
| 507 | |
| 508 | wq = &mgcp->wq; |
| 509 | |
Harald Welte | 8890dfa | 2017-11-17 15:09:30 +0100 | [diff] [blame] | 510 | rc = osmo_sock_init2_ofd(&wq->bfd, AF_INET, SOCK_DGRAM, IPPROTO_UDP, |
| 511 | mgcp->actual.local_addr, mgcp->actual.local_port, |
| 512 | mgcp->actual.remote_addr, mgcp->actual.remote_port, |
| 513 | OSMO_SOCK_F_BIND | OSMO_SOCK_F_CONNECT); |
| 514 | if (rc < 0) { |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 515 | LOGP(DLMGCP, LOGL_FATAL, |
Harald Welte | 8890dfa | 2017-11-17 15:09:30 +0100 | [diff] [blame] | 516 | "Failed to initialize socket %s:%u -> %s:%u for MGCP GW: %s\n", |
| 517 | mgcp->actual.local_addr, mgcp->actual.local_port, |
| 518 | mgcp->actual.remote_addr, mgcp->actual.remote_port, strerror(errno)); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 519 | goto error_close_fd; |
| 520 | } |
| 521 | |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 522 | inet_aton(mgcp->actual.remote_addr, &addr.sin_addr); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 523 | mgcp->remote_addr = htonl(addr.sin_addr.s_addr); |
| 524 | |
| 525 | osmo_wqueue_init(wq, 10); |
| 526 | wq->bfd.when = BSC_FD_READ; |
| 527 | wq->bfd.data = mgcp; |
| 528 | wq->read_cb = mgcp_do_read; |
| 529 | wq->write_cb = mgcp_do_write; |
| 530 | |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 531 | LOGP(DLMGCP, LOGL_INFO, "MGCP GW connection: %s:%u -> %s:%u\n", |
| 532 | mgcp->actual.local_addr, mgcp->actual.local_port, |
| 533 | mgcp->actual.remote_addr, mgcp->actual.remote_port); |
| 534 | |
| 535 | return 0; |
| 536 | error_close_fd: |
| 537 | close(wq->bfd.fd); |
| 538 | wq->bfd.fd = -1; |
| 539 | return rc; |
| 540 | } |
| 541 | |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 542 | const char *mgcp_client_remote_addr_str(struct mgcp_client *mgcp) |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 543 | { |
| 544 | return mgcp->actual.remote_addr; |
| 545 | } |
| 546 | |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 547 | uint16_t mgcp_client_remote_port(struct mgcp_client *mgcp) |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 548 | { |
| 549 | return mgcp->actual.remote_port; |
| 550 | } |
| 551 | |
| 552 | /* Return the MGCP GW binary IPv4 address in network byte order. */ |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 553 | uint32_t mgcp_client_remote_addr_n(struct mgcp_client *mgcp) |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 554 | { |
| 555 | return mgcp->remote_addr; |
| 556 | } |
| 557 | |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 558 | struct mgcp_response_pending * mgcp_client_pending_add( |
| 559 | struct mgcp_client *mgcp, |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 560 | mgcp_trans_id_t trans_id, |
| 561 | mgcp_response_cb_t response_cb, |
| 562 | void *priv) |
| 563 | { |
| 564 | struct mgcp_response_pending *pending; |
| 565 | |
| 566 | pending = talloc_zero(mgcp, struct mgcp_response_pending); |
| 567 | pending->trans_id = trans_id; |
| 568 | pending->response_cb = response_cb; |
| 569 | pending->priv = priv; |
| 570 | llist_add_tail(&pending->entry, &mgcp->responses_pending); |
| 571 | |
| 572 | return pending; |
| 573 | } |
| 574 | |
| 575 | /* Send the MGCP message in msg to the MGCP GW and handle a response with |
| 576 | * response_cb. NOTE: the response_cb still needs to call |
| 577 | * mgcp_response_parse_params(response) to get the parsed parameters -- to |
| 578 | * potentially save some CPU cycles, only the head line has been parsed when |
Neels Hofmeyr | c8f37cb | 2017-11-30 13:43:11 +0100 | [diff] [blame] | 579 | * the response_cb is invoked. |
| 580 | * Before the priv pointer becomes invalid, e.g. due to transaction timeout, |
| 581 | * mgcp_client_cancel() needs to be called for this transaction. |
| 582 | */ |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 583 | int mgcp_client_tx(struct mgcp_client *mgcp, struct msgb *msg, |
| 584 | mgcp_response_cb_t response_cb, void *priv) |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 585 | { |
| 586 | struct mgcp_response_pending *pending; |
| 587 | mgcp_trans_id_t trans_id; |
| 588 | int rc; |
| 589 | |
| 590 | trans_id = msg->cb[MSGB_CB_MGCP_TRANS_ID]; |
| 591 | if (!trans_id) { |
| 592 | LOGP(DLMGCP, LOGL_ERROR, |
| 593 | "Unset transaction id in mgcp send request\n"); |
| 594 | talloc_free(msg); |
| 595 | return -EINVAL; |
| 596 | } |
| 597 | |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 598 | pending = mgcp_client_pending_add(mgcp, trans_id, response_cb, priv); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 599 | |
| 600 | if (msgb_l2len(msg) > 4096) { |
| 601 | LOGP(DLMGCP, LOGL_ERROR, |
| 602 | "Cannot send, MGCP message too large: %u\n", |
| 603 | msgb_l2len(msg)); |
| 604 | msgb_free(msg); |
| 605 | rc = -EINVAL; |
| 606 | goto mgcp_tx_error; |
| 607 | } |
| 608 | |
| 609 | rc = osmo_wqueue_enqueue(&mgcp->wq, msg); |
| 610 | if (rc) { |
| 611 | LOGP(DLMGCP, LOGL_FATAL, "Could not queue message to MGCP GW\n"); |
| 612 | msgb_free(msg); |
| 613 | goto mgcp_tx_error; |
| 614 | } else |
| 615 | LOGP(DLMGCP, LOGL_INFO, "Queued %u bytes for MGCP GW\n", |
| 616 | msgb_l2len(msg)); |
| 617 | return 0; |
| 618 | |
| 619 | mgcp_tx_error: |
| 620 | /* Pass NULL to response cb to indicate an error */ |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 621 | mgcp_client_handle_response(mgcp, pending, NULL); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 622 | return -1; |
| 623 | } |
| 624 | |
Neels Hofmeyr | c8f37cb | 2017-11-30 13:43:11 +0100 | [diff] [blame] | 625 | /* Cancel a pending transaction. |
| 626 | * Should a priv pointer passed to mgcp_client_tx() become invalid, this function must be called. In |
| 627 | * practical terms, if the caller of mgcp_client_tx() wishes to tear down a transaction without having |
| 628 | * received a response this function must be called. The trans_id can be obtained by calling |
| 629 | * mgcp_msg_trans_id() on the msgb produced by mgcp_msg_gen(). |
| 630 | */ |
| 631 | int mgcp_client_cancel(struct mgcp_client *mgcp, mgcp_trans_id_t trans_id) |
| 632 | { |
| 633 | struct mgcp_response_pending *pending = mgcp_client_response_pending_get(mgcp, trans_id); |
| 634 | if (!pending) { |
| 635 | /* INFO is sufficient, it is not harmful to cancel a transaction twice. */ |
| 636 | LOGP(DLMGCP, LOGL_INFO, "Cannot cancel, no such transaction: %u\n", trans_id); |
| 637 | return -ENOENT; |
| 638 | } |
| 639 | LOGP(DLMGCP, LOGL_INFO, "Canceled transaction %u\n", trans_id); |
| 640 | talloc_free(pending); |
| 641 | return 0; |
| 642 | /* We don't really need to clean up the wqueue: In all sane cases, the msgb has already been sent |
| 643 | * out and is no longer in the wqueue. If it still is in the wqueue, then sending MGCP messages |
| 644 | * per se is broken and the program should notice so by a full wqueue. Even if this was called |
| 645 | * before we had a chance to send out the message and it is still going to be sent, we will just |
| 646 | * ignore the reply to it later. Removing a msgb from the wqueue here would just introduce more |
| 647 | * bug surface in terms of failing to update wqueue API's counters or some such. |
| 648 | */ |
| 649 | } |
| 650 | |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 651 | static struct msgb *mgcp_msg_from_buf(mgcp_trans_id_t trans_id, |
| 652 | const char *buf, int len) |
| 653 | { |
| 654 | struct msgb *msg; |
| 655 | |
| 656 | if (len > (4096 - 128)) { |
| 657 | LOGP(DLMGCP, LOGL_ERROR, "Cannot send to MGCP GW:" |
| 658 | " message too large: %d\n", len); |
| 659 | return NULL; |
| 660 | } |
| 661 | |
| 662 | msg = msgb_alloc_headroom(4096, 128, "MGCP tx"); |
| 663 | OSMO_ASSERT(msg); |
| 664 | |
| 665 | char *dst = (char*)msgb_put(msg, len); |
| 666 | memcpy(dst, buf, len); |
| 667 | msg->l2h = msg->data; |
| 668 | msg->cb[MSGB_CB_MGCP_TRANS_ID] = trans_id; |
| 669 | |
| 670 | return msg; |
| 671 | } |
| 672 | |
| 673 | static struct msgb *mgcp_msg_from_str(mgcp_trans_id_t trans_id, |
| 674 | const char *fmt, ...) |
| 675 | { |
| 676 | static char compose[4096 - 128]; |
| 677 | va_list ap; |
| 678 | int len; |
| 679 | OSMO_ASSERT(fmt); |
| 680 | |
| 681 | va_start(ap, fmt); |
| 682 | len = vsnprintf(compose, sizeof(compose), fmt, ap); |
| 683 | va_end(ap); |
| 684 | if (len >= sizeof(compose)) { |
| 685 | LOGP(DLMGCP, LOGL_ERROR, |
| 686 | "Message too large: trans_id=%u len=%d\n", |
| 687 | trans_id, len); |
| 688 | return NULL; |
| 689 | } |
| 690 | if (len < 1) { |
| 691 | LOGP(DLMGCP, LOGL_ERROR, |
| 692 | "Failed to compose message: trans_id=%u len=%d\n", |
| 693 | trans_id, len); |
| 694 | return NULL; |
| 695 | } |
| 696 | return mgcp_msg_from_buf(trans_id, compose, len); |
| 697 | } |
| 698 | |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 699 | 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] | 700 | { |
| 701 | /* avoid zero trans_id to distinguish from unset trans_id */ |
| 702 | if (!mgcp->next_trans_id) |
| 703 | mgcp->next_trans_id ++; |
| 704 | return mgcp->next_trans_id ++; |
| 705 | } |
| 706 | |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 707 | struct msgb *mgcp_msg_crcx(struct mgcp_client *mgcp, |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 708 | uint16_t rtp_endpoint, unsigned int call_id, |
| 709 | enum mgcp_connection_mode mode) |
| 710 | { |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 711 | mgcp_trans_id_t trans_id = mgcp_client_next_trans_id(mgcp); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 712 | return mgcp_msg_from_str(trans_id, |
| 713 | "CRCX %u %x@mgw MGCP 1.0\r\n" |
| 714 | "C: %x\r\n" |
| 715 | "L: p:20, a:AMR, nt:IN\r\n" |
| 716 | "M: %s\r\n" |
| 717 | , |
| 718 | trans_id, |
| 719 | rtp_endpoint, |
| 720 | call_id, |
Neels Hofmeyr | d95ab1e | 2017-09-22 00:52:54 +0200 | [diff] [blame] | 721 | mgcp_client_cmode_name(mode)); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 722 | } |
| 723 | |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 724 | struct msgb *mgcp_msg_mdcx(struct mgcp_client *mgcp, |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 725 | uint16_t rtp_endpoint, const char *rtp_conn_addr, |
| 726 | uint16_t rtp_port, enum mgcp_connection_mode mode) |
| 727 | |
| 728 | { |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 729 | mgcp_trans_id_t trans_id = mgcp_client_next_trans_id(mgcp); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 730 | return mgcp_msg_from_str(trans_id, |
| 731 | "MDCX %u %x@mgw MGCP 1.0\r\n" |
| 732 | "M: %s\r\n" |
| 733 | "\r\n" |
| 734 | "c=IN IP4 %s\r\n" |
| 735 | "m=audio %u RTP/AVP 255\r\n" |
| 736 | , |
| 737 | trans_id, |
| 738 | rtp_endpoint, |
Neels Hofmeyr | d95ab1e | 2017-09-22 00:52:54 +0200 | [diff] [blame] | 739 | mgcp_client_cmode_name(mode), |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 740 | rtp_conn_addr, |
| 741 | rtp_port); |
| 742 | } |
| 743 | |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 744 | 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] | 745 | unsigned int call_id) |
| 746 | { |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 747 | mgcp_trans_id_t trans_id = mgcp_client_next_trans_id(mgcp); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 748 | return mgcp_msg_from_str(trans_id, |
| 749 | "DLCX %u %x@mgw MGCP 1.0\r\n" |
| 750 | "C: %x\r\n", trans_id, rtp_endpoint, call_id); |
| 751 | } |
| 752 | |
Philipp Maier | 1dc6be6 | 2017-10-05 18:25:37 +0200 | [diff] [blame] | 753 | #define MGCP_CRCX_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT | \ |
| 754 | MGCP_MSG_PRESENCE_CALL_ID | \ |
Philipp Maier | 1dc6be6 | 2017-10-05 18:25:37 +0200 | [diff] [blame] | 755 | MGCP_MSG_PRESENCE_CONN_MODE) |
| 756 | #define MGCP_MDCX_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT | \ |
Philipp Maier | 490cbaa | 2018-01-22 17:32:38 +0100 | [diff] [blame] | 757 | MGCP_MSG_PRESENCE_CALL_ID | \ |
Philipp Maier | 1dc6be6 | 2017-10-05 18:25:37 +0200 | [diff] [blame] | 758 | MGCP_MSG_PRESENCE_CONN_ID) |
| 759 | #define MGCP_DLCX_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT) |
| 760 | #define MGCP_AUEP_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT) |
| 761 | #define MGCP_RSIP_MANDATORY 0 /* none */ |
| 762 | |
| 763 | struct msgb *mgcp_msg_gen(struct mgcp_client *mgcp, struct mgcp_msg *mgcp_msg) |
| 764 | { |
| 765 | mgcp_trans_id_t trans_id = mgcp_client_next_trans_id(mgcp); |
| 766 | uint32_t mandatory_mask; |
| 767 | struct msgb *msg = msgb_alloc_headroom(4096, 128, "MGCP tx"); |
| 768 | int rc = 0; |
Philipp Maier | 9d25d7a | 2018-01-22 17:31:10 +0100 | [diff] [blame] | 769 | char local_ip[INET_ADDRSTRLEN]; |
Philipp Maier | 1dc6be6 | 2017-10-05 18:25:37 +0200 | [diff] [blame] | 770 | |
| 771 | msg->l2h = msg->data; |
| 772 | msg->cb[MSGB_CB_MGCP_TRANS_ID] = trans_id; |
| 773 | |
| 774 | /* Add command verb */ |
| 775 | switch (mgcp_msg->verb) { |
| 776 | case MGCP_VERB_CRCX: |
| 777 | mandatory_mask = MGCP_CRCX_MANDATORY; |
| 778 | rc += msgb_printf(msg, "CRCX %u", trans_id); |
| 779 | break; |
| 780 | case MGCP_VERB_MDCX: |
| 781 | mandatory_mask = MGCP_MDCX_MANDATORY; |
| 782 | rc += msgb_printf(msg, "MDCX %u", trans_id); |
| 783 | break; |
| 784 | case MGCP_VERB_DLCX: |
| 785 | mandatory_mask = MGCP_DLCX_MANDATORY; |
| 786 | rc += msgb_printf(msg, "DLCX %u", trans_id); |
| 787 | break; |
| 788 | case MGCP_VERB_AUEP: |
| 789 | mandatory_mask = MGCP_AUEP_MANDATORY; |
| 790 | rc += msgb_printf(msg, "AUEP %u", trans_id); |
| 791 | break; |
| 792 | case MGCP_VERB_RSIP: |
| 793 | mandatory_mask = MGCP_RSIP_MANDATORY; |
| 794 | rc += msgb_printf(msg, "RSIP %u", trans_id); |
| 795 | break; |
| 796 | default: |
| 797 | LOGP(DLMGCP, LOGL_ERROR, |
| 798 | "Invalid command verb, can not generate MGCP message\n"); |
| 799 | msgb_free(msg); |
| 800 | return NULL; |
| 801 | } |
| 802 | |
| 803 | /* Check if mandatory fields are missing */ |
| 804 | if (!((mgcp_msg->presence & mandatory_mask) == mandatory_mask)) { |
| 805 | LOGP(DLMGCP, LOGL_ERROR, |
| 806 | "One or more missing mandatory fields, can not generate MGCP message\n"); |
| 807 | msgb_free(msg); |
| 808 | return NULL; |
| 809 | } |
| 810 | |
| 811 | /* Add endpoint name */ |
Philipp Maier | 7bc5552 | 2017-12-10 22:52:22 +0100 | [diff] [blame] | 812 | if (mgcp_msg->presence & MGCP_MSG_PRESENCE_ENDPOINT) { |
| 813 | if (strlen(mgcp_msg->endpoint) <= 0) { |
| 814 | LOGP(DLMGCP, LOGL_ERROR, |
| 815 | "Empty endpoint name, can not generate MGCP message\n"); |
| 816 | msgb_free(msg); |
| 817 | return NULL; |
| 818 | } |
Philipp Maier | 1dc6be6 | 2017-10-05 18:25:37 +0200 | [diff] [blame] | 819 | rc += msgb_printf(msg, " %s", mgcp_msg->endpoint); |
Philipp Maier | 7bc5552 | 2017-12-10 22:52:22 +0100 | [diff] [blame] | 820 | } |
Philipp Maier | 1dc6be6 | 2017-10-05 18:25:37 +0200 | [diff] [blame] | 821 | |
| 822 | /* Add protocol version */ |
| 823 | rc += msgb_printf(msg, " MGCP 1.0\r\n"); |
| 824 | |
| 825 | /* Add call id */ |
| 826 | if (mgcp_msg->presence & MGCP_MSG_PRESENCE_CALL_ID) |
| 827 | rc += msgb_printf(msg, "C: %x\r\n", mgcp_msg->call_id); |
| 828 | |
| 829 | /* Add connection id */ |
Philipp Maier | 7bc5552 | 2017-12-10 22:52:22 +0100 | [diff] [blame] | 830 | if (mgcp_msg->presence & MGCP_MSG_PRESENCE_CONN_ID) { |
| 831 | if (strlen(mgcp_msg->conn_id) <= 0) { |
| 832 | LOGP(DLMGCP, LOGL_ERROR, |
| 833 | "Empty connection id, can not generate MGCP message\n"); |
| 834 | msgb_free(msg); |
| 835 | return NULL; |
| 836 | } |
Philipp Maier | 01d24a3 | 2017-11-21 17:26:09 +0100 | [diff] [blame] | 837 | rc += msgb_printf(msg, "I: %s\r\n", mgcp_msg->conn_id); |
Philipp Maier | 7bc5552 | 2017-12-10 22:52:22 +0100 | [diff] [blame] | 838 | } |
Philipp Maier | 1dc6be6 | 2017-10-05 18:25:37 +0200 | [diff] [blame] | 839 | |
| 840 | /* Add local connection options */ |
Philipp Maier | ffd75e4 | 2017-11-22 11:44:50 +0100 | [diff] [blame] | 841 | if (mgcp_msg->verb == MGCP_VERB_CRCX) |
Philipp Maier | 1dc6be6 | 2017-10-05 18:25:37 +0200 | [diff] [blame] | 842 | rc += msgb_printf(msg, "L: p:20, a:AMR, nt:IN\r\n"); |
| 843 | |
| 844 | /* Add mode */ |
| 845 | if (mgcp_msg->presence & MGCP_MSG_PRESENCE_CONN_MODE) |
| 846 | rc += |
| 847 | msgb_printf(msg, "M: %s\r\n", |
| 848 | mgcp_client_cmode_name(mgcp_msg->conn_mode)); |
| 849 | |
Philipp Maier | 9d25d7a | 2018-01-22 17:31:10 +0100 | [diff] [blame] | 850 | /* Add SDP body */ |
Philipp Maier | 1dc6be6 | 2017-10-05 18:25:37 +0200 | [diff] [blame] | 851 | if (mgcp_msg->presence & MGCP_MSG_PRESENCE_AUDIO_IP |
| 852 | && mgcp_msg->presence & MGCP_MSG_PRESENCE_AUDIO_PORT) { |
Philipp Maier | 9d25d7a | 2018-01-22 17:31:10 +0100 | [diff] [blame] | 853 | |
| 854 | /* Add separator to mark the beginning of the SDP block */ |
| 855 | rc += msgb_printf(msg, "\r\n"); |
| 856 | |
| 857 | /* Add SDP protocol version */ |
| 858 | rc += msgb_printf(msg, "v=0\r\n"); |
| 859 | |
| 860 | /* Add session name (none) */ |
| 861 | rc += msgb_printf(msg, "s=-\r\n"); |
| 862 | |
| 863 | /* Determine local IP-Address */ |
| 864 | if (osmo_sock_local_ip(local_ip, mgcp->actual.remote_addr) < 0) { |
| 865 | LOGP(DLMGCP, LOGL_ERROR, |
| 866 | "Could not determine local IP-Address!\n"); |
| 867 | msgb_free(msg); |
| 868 | return NULL; |
| 869 | } |
| 870 | |
| 871 | /* Add owner/creator (SDP) */ |
| 872 | rc += msgb_printf(msg, "o=- %x 23 IN IP4 %s\r\n", |
| 873 | mgcp_msg->call_id, local_ip); |
| 874 | |
| 875 | /* Add RTP address and port */ |
Philipp Maier | 7bc5552 | 2017-12-10 22:52:22 +0100 | [diff] [blame] | 876 | if (mgcp_msg->audio_port == 0) { |
| 877 | LOGP(DLMGCP, LOGL_ERROR, |
| 878 | "Invalid port number, can not generate MGCP message\n"); |
| 879 | msgb_free(msg); |
| 880 | return NULL; |
| 881 | } |
| 882 | if (strlen(mgcp_msg->audio_ip) <= 0) { |
| 883 | LOGP(DLMGCP, LOGL_ERROR, |
| 884 | "Empty ip address, can not generate MGCP message\n"); |
| 885 | msgb_free(msg); |
| 886 | return NULL; |
| 887 | } |
Philipp Maier | 1dc6be6 | 2017-10-05 18:25:37 +0200 | [diff] [blame] | 888 | rc += msgb_printf(msg, "c=IN IP4 %s\r\n", mgcp_msg->audio_ip); |
| 889 | rc += |
| 890 | msgb_printf(msg, "m=audio %u RTP/AVP 255\r\n", |
| 891 | mgcp_msg->audio_port); |
Philipp Maier | 9d25d7a | 2018-01-22 17:31:10 +0100 | [diff] [blame] | 892 | |
| 893 | /* Add time description, active time (SDP) */ |
| 894 | rc += msgb_printf(msg, "t=0 0\r\n"); |
Philipp Maier | 1dc6be6 | 2017-10-05 18:25:37 +0200 | [diff] [blame] | 895 | } |
| 896 | |
| 897 | if (rc != 0) { |
| 898 | LOGP(DLMGCP, LOGL_ERROR, |
| 899 | "message buffer to small, can not generate MGCP message\n"); |
| 900 | msgb_free(msg); |
| 901 | msg = NULL; |
| 902 | } |
| 903 | |
| 904 | return msg; |
| 905 | } |
| 906 | |
Neels Hofmeyr | c8f37cb | 2017-11-30 13:43:11 +0100 | [diff] [blame] | 907 | /* Retrieve the MGCP transaction ID from a msgb generated by mgcp_msg_gen() */ |
| 908 | mgcp_trans_id_t mgcp_msg_trans_id(struct msgb *msg) |
| 909 | { |
| 910 | return (mgcp_trans_id_t)msg->cb[MSGB_CB_MGCP_TRANS_ID]; |
| 911 | } |
| 912 | |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 913 | struct mgcp_client_conf *mgcp_client_conf_actual(struct mgcp_client *mgcp) |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 914 | { |
| 915 | return &mgcp->actual; |
| 916 | } |
Neels Hofmeyr | d95ab1e | 2017-09-22 00:52:54 +0200 | [diff] [blame] | 917 | |
| 918 | const struct value_string mgcp_client_connection_mode_strs[] = { |
| 919 | { MGCP_CONN_NONE, "none" }, |
| 920 | { MGCP_CONN_RECV_SEND, "sendrecv" }, |
| 921 | { MGCP_CONN_SEND_ONLY, "sendonly" }, |
| 922 | { MGCP_CONN_RECV_ONLY, "recvonly" }, |
| 923 | { MGCP_CONN_LOOPBACK, "loopback" }, |
| 924 | { 0, NULL } |
| 925 | }; |