Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 1 | /* mgcp_utils - common functions to setup an MGCP connection |
| 2 | */ |
| 3 | /* (C) 2016 by sysmocom s.f.m.c. GmbH <info@sysmocom.de> |
| 4 | * All Rights Reserved |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
Harald Welte | 220f487 | 2018-02-04 09:04:16 +0100 | [diff] [blame] | 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
Harald Welte | 220f487 | 2018-02-04 09:04:16 +0100 | [diff] [blame] | 14 | * GNU General Public License for more details. |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 15 | * |
Harald Welte | 220f487 | 2018-02-04 09:04:16 +0100 | [diff] [blame] | 16 | * You should have received a copy of the GNU General Public License |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | */ |
| 20 | |
| 21 | #include <osmocom/core/linuxlist.h> |
| 22 | #include <osmocom/core/select.h> |
| 23 | #include <osmocom/core/write_queue.h> |
| 24 | #include <osmocom/core/msgb.h> |
| 25 | #include <osmocom/core/logging.h> |
Philipp Maier | 1dc6be6 | 2017-10-05 18:25:37 +0200 | [diff] [blame] | 26 | #include <osmocom/core/byteswap.h> |
Harald Welte | 8890dfa | 2017-11-17 15:09:30 +0100 | [diff] [blame] | 27 | #include <osmocom/core/socket.h> |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 28 | |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 29 | #include <osmocom/mgcp_client/mgcp_client.h> |
| 30 | #include <osmocom/mgcp_client/mgcp_client_internal.h> |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 31 | |
| 32 | #include <netinet/in.h> |
| 33 | #include <arpa/inet.h> |
| 34 | |
| 35 | #include <errno.h> |
| 36 | #include <unistd.h> |
| 37 | #include <string.h> |
| 38 | |
Philipp Maier | 704c4f0 | 2018-06-07 18:51:31 +0200 | [diff] [blame] | 39 | /* Codec descripton for dynamic payload types (SDP) */ |
| 40 | static const struct value_string codec_table[] = { |
| 41 | { CODEC_PCMU_8000_1, "PCMU/8000/1" }, |
| 42 | { CODEC_GSM_8000_1, "GSM/8000/1" }, |
| 43 | { CODEC_PCMA_8000_1, "PCMA/8000/1" }, |
| 44 | { CODEC_G729_8000_1, "G729/8000/1" }, |
| 45 | { CODEC_GSMEFR_8000_1, "GSM-EFR/8000/1" }, |
| 46 | { CODEC_GSMHR_8000_1, "GSM-HR-08/8000/1" }, |
| 47 | { CODEC_AMR_8000_1, "AMR/8000/1" }, |
| 48 | { CODEC_AMRWB_16000_1, "AMR-WB/16000/1" }, |
| 49 | { 0, NULL }, |
| 50 | }; |
| 51 | |
| 52 | /* Get encoding name from a full codec string e,g. |
| 53 | * ("CODEC/8000/2" => returns "CODEC") */ |
| 54 | static char *extract_codec_name(const char *str) |
| 55 | { |
| 56 | static char buf[64]; |
| 57 | unsigned int i; |
| 58 | |
| 59 | if (!str) |
| 60 | return NULL; |
| 61 | |
| 62 | /* FIXME osmo_strlcpy */ |
| 63 | osmo_strlcpy(buf, str, sizeof(buf)); |
| 64 | |
| 65 | for (i = 0; i < strlen(buf); i++) { |
| 66 | if (buf[i] == '/') |
| 67 | buf[i] = '\0'; |
| 68 | } |
| 69 | |
| 70 | return buf; |
| 71 | } |
| 72 | |
| 73 | /*! Map a string to a codec. |
| 74 | * \ptmap[in] str input string (e.g "GSM/8000/1", "GSM/8000" or "GSM") |
| 75 | * \returns codec that corresponds to the given string representation. */ |
| 76 | enum mgcp_codecs map_str_to_codec(const char *str) |
| 77 | { |
| 78 | unsigned int i; |
| 79 | char *codec_name; |
| 80 | char str_buf[64]; |
| 81 | |
| 82 | osmo_strlcpy(str_buf, extract_codec_name(str), sizeof(str_buf)); |
| 83 | |
| 84 | for (i = 0; i < ARRAY_SIZE(codec_table); i++) { |
| 85 | codec_name = extract_codec_name(codec_table[i].str); |
| 86 | if (!codec_name) |
| 87 | continue; |
| 88 | if (strcmp(codec_name, str_buf) == 0) |
| 89 | return codec_table[i].value; |
| 90 | } |
| 91 | |
| 92 | return -1; |
| 93 | } |
| 94 | |
| 95 | /* Check the ptmap for illegal mappings */ |
| 96 | static int check_ptmap(struct ptmap *ptmap) |
| 97 | { |
| 98 | /* Check if there are mappings that leave the IANA assigned dynamic |
| 99 | * payload type range. Under normal conditions such mappings should |
| 100 | * not occur */ |
| 101 | |
| 102 | /* Its ok to have a 1:1 mapping in the statically defined |
| 103 | * range, this won't hurt */ |
| 104 | if (ptmap->codec == ptmap->pt) |
| 105 | return 0; |
| 106 | |
| 107 | if (ptmap->codec < 96 || ptmap->codec > 127) |
| 108 | goto error; |
| 109 | if (ptmap->pt < 96 || ptmap->pt > 127) |
| 110 | goto error; |
| 111 | |
| 112 | return 0; |
| 113 | error: |
| 114 | LOGP(DLMGCP, LOGL_ERROR, |
| 115 | "ptmap contains illegal mapping: codec=%u maps to pt=%u\n", |
| 116 | ptmap->codec, ptmap->pt); |
| 117 | return -1; |
| 118 | } |
| 119 | |
| 120 | /*! Map a codec to a payload type. |
| 121 | * \ptmap[in] payload pointer to payload type map with specified payload types. |
| 122 | * \ptmap[in] ptmap_len length of the payload type map. |
| 123 | * \ptmap[in] codec the codec for which the payload type should be looked up. |
| 124 | * \returns assigned payload type */ |
| 125 | unsigned int map_codec_to_pt(struct ptmap *ptmap, unsigned int ptmap_len, |
| 126 | enum mgcp_codecs codec) |
| 127 | { |
| 128 | unsigned int i; |
| 129 | |
| 130 | /*! Note: If the payload type map is empty or the codec is not found |
| 131 | * in the map, then a 1:1 mapping is performed. If the codec falls |
| 132 | * into the statically defined range or if the mapping table isself |
| 133 | * tries to map to the statically defined range, then the mapping |
| 134 | * is also ignored and a 1:1 mapping is performed instead. */ |
| 135 | |
| 136 | /* we may return the codec directly since enum mgcp_codecs directly |
| 137 | * corresponds to the statićally assigned payload types */ |
| 138 | if (codec < 96 || codec > 127) |
| 139 | return codec; |
| 140 | |
| 141 | for (i = 0; i < ptmap_len; i++) { |
| 142 | /* Skip illegal map entries */ |
| 143 | if (check_ptmap(ptmap) == 0 && ptmap->codec == codec) |
| 144 | return ptmap->pt; |
| 145 | ptmap++; |
| 146 | } |
| 147 | |
| 148 | /* If nothing is found, do not perform any mapping */ |
| 149 | return codec; |
| 150 | } |
| 151 | |
| 152 | /*! Map a payload type to a codec. |
| 153 | * \ptmap[in] payload pointer to payload type map with specified payload types. |
| 154 | * \ptmap[in] ptmap_len length of the payload type map. |
| 155 | * \ptmap[in] payload type for which the codec should be looked up. |
| 156 | * \returns codec that corresponds to the specified payload type */ |
| 157 | enum mgcp_codecs map_pt_to_codec(struct ptmap *ptmap, unsigned int ptmap_len, |
| 158 | unsigned int pt) |
| 159 | { |
| 160 | unsigned int i; |
| 161 | |
| 162 | /*! Note: If the payload type map is empty or the payload type is not |
| 163 | * found in the map, then a 1:1 mapping is performed. If the payload |
| 164 | * type falls into the statically defined range or if the mapping |
| 165 | * table isself tries to map to the statically defined range, then |
| 166 | * the mapping is also ignored and a 1:1 mapping is performed |
| 167 | * instead. */ |
| 168 | |
| 169 | /* See also note in map_codec_to_pt() */ |
| 170 | if (pt < 96 || pt > 127) |
| 171 | return pt; |
| 172 | |
| 173 | for (i = 0; i < ptmap_len; i++) { |
| 174 | if (check_ptmap(ptmap) == 0 && ptmap->pt == pt) |
| 175 | return ptmap->codec; |
| 176 | ptmap++; |
| 177 | } |
| 178 | |
| 179 | /* If nothing is found, do not perform any mapping */ |
| 180 | return pt; |
| 181 | } |
| 182 | |
Philipp Maier | 275ac97 | 2018-01-20 00:58:16 +0100 | [diff] [blame] | 183 | /*! Initalize MGCP client configuration struct with default values. |
| 184 | * \param[out] conf Client configuration.*/ |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 185 | void mgcp_client_conf_init(struct mgcp_client_conf *conf) |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 186 | { |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 187 | /* NULL and -1 default to MGCP_CLIENT_*_DEFAULT values */ |
| 188 | *conf = (struct mgcp_client_conf){ |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 189 | .local_addr = NULL, |
| 190 | .local_port = -1, |
| 191 | .remote_addr = NULL, |
| 192 | .remote_port = -1, |
| 193 | .first_endpoint = 0, |
| 194 | .last_endpoint = 0, |
Neels Hofmeyr | c0dcc3c | 2017-12-02 18:31:34 +0000 | [diff] [blame] | 195 | .bts_base = 0, |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 196 | }; |
| 197 | } |
| 198 | |
| 199 | /* Test if a given endpoint id is currently in use */ |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 200 | static bool endpoint_in_use(uint16_t id, struct mgcp_client *client) |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 201 | { |
| 202 | struct mgcp_inuse_endpoint *endpoint; |
| 203 | llist_for_each_entry(endpoint, &client->inuse_endpoints, entry) { |
| 204 | if (endpoint->id == id) |
| 205 | return true; |
| 206 | } |
| 207 | |
| 208 | return false; |
| 209 | } |
| 210 | |
Philipp Maier | 275ac97 | 2018-01-20 00:58:16 +0100 | [diff] [blame] | 211 | /*! Pick next free endpoint ID. |
| 212 | * \param[in,out] client MGCP client descriptor. |
| 213 | * \returns 0 on success, -EINVAL on error. */ |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 214 | int mgcp_client_next_endpoint(struct mgcp_client *client) |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 215 | { |
| 216 | int i; |
| 217 | uint16_t first_endpoint = client->actual.first_endpoint; |
| 218 | uint16_t last_endpoint = client->actual.last_endpoint; |
| 219 | struct mgcp_inuse_endpoint *endpoint; |
| 220 | |
| 221 | /* Use the maximum permitted range if the VTY |
| 222 | * configuration does not specify a range */ |
| 223 | if (client->actual.last_endpoint == 0) { |
| 224 | first_endpoint = 1; |
| 225 | last_endpoint = 65534; |
| 226 | } |
| 227 | |
| 228 | /* Test the permitted endpoint range for an endpoint |
| 229 | * number that is not in use. When a suitable endpoint |
| 230 | * number can be found, seize it by adding it to the |
| 231 | * inuse list. */ |
| 232 | for (i=first_endpoint;i<last_endpoint;i++) |
| 233 | { |
| 234 | if (endpoint_in_use(i,client) == false) { |
| 235 | endpoint = talloc_zero(client, struct mgcp_inuse_endpoint); |
| 236 | endpoint->id = i; |
| 237 | llist_add_tail(&endpoint->entry, &client->inuse_endpoints); |
| 238 | return endpoint->id; |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | /* All endpoints are busy! */ |
| 243 | return -EINVAL; |
| 244 | } |
| 245 | |
Philipp Maier | 275ac97 | 2018-01-20 00:58:16 +0100 | [diff] [blame] | 246 | /*! Release a seized endpoint ID to make it available again for other calls. |
| 247 | * \param[in] id Endpoint ID |
| 248 | * \param[in,out] client MGCP client descriptor. */ |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 249 | /* 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] | 250 | void mgcp_client_release_endpoint(uint16_t id, struct mgcp_client *client) |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 251 | { |
| 252 | struct mgcp_inuse_endpoint *endpoint; |
| 253 | struct mgcp_inuse_endpoint *endpoint_tmp; |
| 254 | llist_for_each_entry_safe(endpoint, endpoint_tmp, &client->inuse_endpoints, entry) { |
| 255 | if (endpoint->id == id) { |
| 256 | llist_del(&endpoint->entry); |
| 257 | talloc_free(endpoint); |
| 258 | } |
| 259 | } |
| 260 | } |
| 261 | |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 262 | static void mgcp_client_handle_response(struct mgcp_client *mgcp, |
| 263 | struct mgcp_response_pending *pending, |
| 264 | struct mgcp_response *response) |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 265 | { |
| 266 | if (!pending) { |
| 267 | LOGP(DLMGCP, LOGL_ERROR, |
| 268 | "Cannot handle NULL response\n"); |
| 269 | return; |
| 270 | } |
| 271 | if (pending->response_cb) |
| 272 | pending->response_cb(response, pending->priv); |
| 273 | else |
| 274 | LOGP(DLMGCP, LOGL_INFO, "MGCP response ignored (NULL cb)\n"); |
| 275 | talloc_free(pending); |
| 276 | } |
| 277 | |
| 278 | static int mgcp_response_parse_head(struct mgcp_response *r, struct msgb *msg) |
| 279 | { |
| 280 | int comment_pos; |
| 281 | char *end; |
| 282 | |
| 283 | if (mgcp_msg_terminate_nul(msg)) |
| 284 | goto response_parse_failure; |
| 285 | |
| 286 | r->body = (char *)msg->data; |
| 287 | |
Harald Welte | 9bf7c53 | 2017-11-17 14:14:31 +0100 | [diff] [blame] | 288 | if (sscanf(r->body, "%3d %u %n", |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 289 | &r->head.response_code, &r->head.trans_id, |
| 290 | &comment_pos) != 2) |
| 291 | goto response_parse_failure; |
| 292 | |
Philipp Maier | abe8c89 | 2018-01-20 00:15:12 +0100 | [diff] [blame] | 293 | 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] | 294 | end = strchr(r->head.comment, '\r'); |
| 295 | if (!end) |
| 296 | goto response_parse_failure; |
| 297 | /* Mark the end of the comment */ |
| 298 | *end = '\0'; |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 299 | return 0; |
| 300 | |
| 301 | response_parse_failure: |
| 302 | LOGP(DLMGCP, LOGL_ERROR, |
| 303 | "Failed to parse MGCP response header\n"); |
| 304 | return -EINVAL; |
| 305 | } |
| 306 | |
| 307 | /* TODO undup against mgcp_protocol.c:mgcp_check_param() */ |
| 308 | static bool mgcp_line_is_valid(const char *line) |
| 309 | { |
| 310 | const size_t line_len = strlen(line); |
| 311 | if (line[0] == '\0') |
| 312 | return true; |
| 313 | |
| 314 | if (line_len < 2 |
| 315 | || line[1] != '=') { |
| 316 | LOGP(DLMGCP, LOGL_ERROR, |
| 317 | "Wrong MGCP option format: '%s'\n", |
| 318 | line); |
| 319 | return false; |
| 320 | } |
| 321 | |
| 322 | return true; |
| 323 | } |
| 324 | |
Philipp Maier | 704c4f0 | 2018-06-07 18:51:31 +0200 | [diff] [blame] | 325 | /* Parse a line like "m=audio 16002 RTP/AVP 98", extract port and payload types */ |
| 326 | static int mgcp_parse_audio_port_pt(struct mgcp_response *r, char *line) |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 327 | { |
Philipp Maier | 704c4f0 | 2018-06-07 18:51:31 +0200 | [diff] [blame] | 328 | char *pt_str; |
| 329 | unsigned int pt; |
| 330 | unsigned int count = 0; |
| 331 | unsigned int i; |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 332 | |
Philipp Maier | 704c4f0 | 2018-06-07 18:51:31 +0200 | [diff] [blame] | 333 | /* Extract port information */ |
| 334 | if (sscanf(line, "m=audio %hu", &r->audio_port) != 1) |
| 335 | goto response_parse_failure_port; |
Philipp Maier | 10f32db | 2017-12-13 12:34:34 +0100 | [diff] [blame] | 336 | if (r->audio_port == 0) |
Philipp Maier | 704c4f0 | 2018-06-07 18:51:31 +0200 | [diff] [blame] | 337 | goto response_parse_failure_port; |
Philipp Maier | 10f32db | 2017-12-13 12:34:34 +0100 | [diff] [blame] | 338 | |
Philipp Maier | 704c4f0 | 2018-06-07 18:51:31 +0200 | [diff] [blame] | 339 | /* Extract payload types */ |
| 340 | line = strstr(line, "RTP/AVP "); |
| 341 | if (!line) |
| 342 | goto exit; |
| 343 | |
| 344 | pt_str = strtok(line, " "); |
| 345 | while (1) { |
| 346 | /* Do not allow excessive payload types */ |
| 347 | if (count > ARRAY_SIZE(r->codecs)) |
| 348 | goto response_parse_failure_pt; |
| 349 | |
| 350 | pt_str = strtok(NULL, " "); |
| 351 | if (!pt_str) |
| 352 | break; |
| 353 | pt = atoi(pt_str); |
| 354 | |
| 355 | /* Do not allow duplicate payload types */ |
| 356 | for (i = 0; i < count; i++) |
| 357 | if (r->codecs[i] == pt) |
| 358 | goto response_parse_failure_pt; |
| 359 | |
| 360 | /* Note: The payload type we store may not necessarly match |
| 361 | * the codec types we have defined in enum mgcp_codecs. To |
| 362 | * ensure that the end result only contains codec types which |
| 363 | * match enum mgcp_codecs, we will go through afterwards and |
| 364 | * remap the affected entries with the inrofmation we learn |
| 365 | * from rtpmap */ |
| 366 | r->codecs[count] = pt; |
| 367 | count++; |
| 368 | } |
| 369 | |
| 370 | r->codecs_len = count; |
| 371 | |
| 372 | exit: |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 373 | return 0; |
| 374 | |
Philipp Maier | 704c4f0 | 2018-06-07 18:51:31 +0200 | [diff] [blame] | 375 | response_parse_failure_port: |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 376 | LOGP(DLMGCP, LOGL_ERROR, |
Philipp Maier | 704c4f0 | 2018-06-07 18:51:31 +0200 | [diff] [blame] | 377 | "Failed to parse SDP parameter port (%s)\n", line); |
Philipp Maier | 06da85e | 2017-10-05 18:49:24 +0200 | [diff] [blame] | 378 | return -EINVAL; |
Philipp Maier | 704c4f0 | 2018-06-07 18:51:31 +0200 | [diff] [blame] | 379 | |
| 380 | response_parse_failure_pt: |
| 381 | LOGP(DLMGCP, LOGL_ERROR, |
| 382 | "Failed to parse SDP parameter payload types (%s)\n", line); |
| 383 | return -EINVAL; |
| 384 | } |
| 385 | |
| 386 | /* Parse a line like "m=audio 16002 RTP/AVP 98", extract port and payload types */ |
| 387 | static int mgcp_parse_audio_ptime_rtpmap(struct mgcp_response *r, const char *line) |
| 388 | { |
| 389 | unsigned int pt; |
| 390 | char codec_resp[64]; |
| 391 | unsigned int codec; |
| 392 | |
| 393 | |
| 394 | if (strstr(line, "ptime")) { |
| 395 | if (sscanf(line, "a=ptime:%u", &r->ptime) != 1) |
| 396 | goto response_parse_failure_ptime; |
| 397 | } else if (strstr(line, "rtpmap")) { |
| 398 | if (sscanf(line, "a=rtpmap:%d %63s", &pt, codec_resp) == 2) { |
| 399 | /* The MGW may assign an own payload type in the |
| 400 | * response if the choosen codec falls into the IANA |
| 401 | * assigned dynamic payload type range (96-127). |
| 402 | * Normally the MGW should obey the 3gpp payload type |
| 403 | * assignments, which are fixed, so we likely wont see |
| 404 | * anything unexpected here. In order to be sure that |
| 405 | * we will now check the codec string and if the result |
| 406 | * does not match to what is IANA / 3gpp assigned, we |
| 407 | * will create an entry in the ptmap table so we can |
| 408 | * lookup later what has been assigned. */ |
| 409 | codec = map_str_to_codec(codec_resp); |
| 410 | if (codec != pt) { |
| 411 | if (r->ptmap_len < ARRAY_SIZE(r->ptmap)) { |
| 412 | r->ptmap[r->ptmap_len].pt = pt; |
| 413 | r->ptmap[r->ptmap_len].codec = codec; |
| 414 | r->ptmap_len++; |
| 415 | } else |
| 416 | goto response_parse_failure_rtpmap; |
| 417 | } |
| 418 | |
| 419 | } else |
| 420 | goto response_parse_failure_rtpmap; |
| 421 | } |
| 422 | |
| 423 | return 0; |
| 424 | |
| 425 | response_parse_failure_ptime: |
| 426 | LOGP(DLMGCP, LOGL_ERROR, |
| 427 | "Failed to parse SDP parameter, invalid ptime (%s)\n", line); |
| 428 | return -EINVAL; |
| 429 | response_parse_failure_rtpmap: |
| 430 | LOGP(DLMGCP, LOGL_ERROR, |
| 431 | "Failed to parse SDP parameter, invalid rtpmap (%s)\n", line); |
| 432 | return -EINVAL; |
Philipp Maier | 06da85e | 2017-10-05 18:49:24 +0200 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | /* Parse a line like "c=IN IP4 10.11.12.13" */ |
| 436 | static int mgcp_parse_audio_ip(struct mgcp_response *r, const char *line) |
| 437 | { |
| 438 | struct in_addr ip_test; |
| 439 | |
| 440 | if (strlen(line) < 16) |
| 441 | goto response_parse_failure; |
| 442 | |
| 443 | /* The current implementation strictly supports IPV4 only ! */ |
| 444 | if (memcmp("c=IN IP4 ", line, 9) != 0) |
| 445 | goto response_parse_failure; |
| 446 | |
| 447 | /* Extract IP-Address */ |
Philipp Maier | f8bfbe8 | 2017-11-23 19:32:31 +0100 | [diff] [blame] | 448 | osmo_strlcpy(r->audio_ip, line + 9, sizeof(r->audio_ip)); |
Philipp Maier | 06da85e | 2017-10-05 18:49:24 +0200 | [diff] [blame] | 449 | |
| 450 | /* Check IP-Address */ |
| 451 | if (inet_aton(r->audio_ip, &ip_test) == 0) |
| 452 | goto response_parse_failure; |
| 453 | |
| 454 | return 0; |
| 455 | |
| 456 | response_parse_failure: |
| 457 | LOGP(DLMGCP, LOGL_ERROR, |
| 458 | "Failed to parse MGCP response header (audio ip)\n"); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 459 | return -EINVAL; |
| 460 | } |
| 461 | |
Philipp Maier | 3b12e1b | 2018-01-18 15:16:13 +0100 | [diff] [blame] | 462 | /* A new section is marked by a double line break, check a few more |
| 463 | * patterns as there may be variants */ |
| 464 | static char *mgcp_find_section_end(char *string) |
| 465 | { |
| 466 | char *rc; |
| 467 | |
| 468 | rc = strstr(string, "\n\n"); |
| 469 | if (rc) |
| 470 | return rc; |
| 471 | |
| 472 | rc = strstr(string, "\n\r\n\r"); |
| 473 | if (rc) |
| 474 | return rc; |
| 475 | |
| 476 | rc = strstr(string, "\r\n\r\n"); |
| 477 | if (rc) |
| 478 | return rc; |
| 479 | |
| 480 | return NULL; |
| 481 | } |
| 482 | |
Philipp Maier | 275ac97 | 2018-01-20 00:58:16 +0100 | [diff] [blame] | 483 | /*! Parse body (SDP) parameters of the MGCP response |
| 484 | * \param[in,out] r Response data |
| 485 | * \returns 0 on success, -EINVAL on error. */ |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 486 | int mgcp_response_parse_params(struct mgcp_response *r) |
| 487 | { |
| 488 | char *line; |
| 489 | int rc; |
Neels Hofmeyr | 0793d2f | 2018-02-21 14:55:34 +0100 | [diff] [blame] | 490 | char *data; |
Philipp Maier | e9d645b | 2018-01-19 23:54:08 +0100 | [diff] [blame] | 491 | char *data_ptr; |
Philipp Maier | 704c4f0 | 2018-06-07 18:51:31 +0200 | [diff] [blame] | 492 | int i; |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 493 | |
Philipp Maier | e9d645b | 2018-01-19 23:54:08 +0100 | [diff] [blame] | 494 | /* Since this functions performs a destructive parsing, we create a |
| 495 | * local copy of the body data */ |
Neels Hofmeyr | 0793d2f | 2018-02-21 14:55:34 +0100 | [diff] [blame] | 496 | OSMO_ASSERT(r->body); |
| 497 | data = talloc_strdup(r, r->body); |
Philipp Maier | e9d645b | 2018-01-19 23:54:08 +0100 | [diff] [blame] | 498 | OSMO_ASSERT(data); |
Philipp Maier | 55295f7 | 2018-01-15 14:00:28 +0100 | [diff] [blame] | 499 | |
Philipp Maier | e9d645b | 2018-01-19 23:54:08 +0100 | [diff] [blame] | 500 | /* Find beginning of the parameter (SDP) section */ |
| 501 | data_ptr = mgcp_find_section_end(data); |
Neels Hofmeyr | 1083533 | 2018-02-21 14:55:34 +0100 | [diff] [blame] | 502 | if (!data_ptr) { |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 503 | LOGP(DLMGCP, LOGL_ERROR, |
Neels Hofmeyr | 0793d2f | 2018-02-21 14:55:34 +0100 | [diff] [blame] | 504 | "MGCP response: cannot find start of SDP parameters\n"); |
Philipp Maier | e9d645b | 2018-01-19 23:54:08 +0100 | [diff] [blame] | 505 | rc = -EINVAL; |
| 506 | goto exit; |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 507 | } |
| 508 | |
Neels Hofmeyr | 0793d2f | 2018-02-21 14:55:34 +0100 | [diff] [blame] | 509 | /* data_ptr now points to the beginning of the section-end-marker; for_each_non_empty_line() |
| 510 | * skips any \r and \n characters for free, so we don't need to skip the marker. */ |
| 511 | |
Philipp Maier | e9d645b | 2018-01-19 23:54:08 +0100 | [diff] [blame] | 512 | for_each_non_empty_line(line, data_ptr) { |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 513 | if (!mgcp_line_is_valid(line)) |
| 514 | return -EINVAL; |
| 515 | |
| 516 | switch (line[0]) { |
Philipp Maier | 704c4f0 | 2018-06-07 18:51:31 +0200 | [diff] [blame] | 517 | case 'a': |
| 518 | rc = mgcp_parse_audio_ptime_rtpmap(r, line); |
| 519 | if (rc) |
| 520 | goto exit; |
| 521 | break; |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 522 | case 'm': |
Philipp Maier | 704c4f0 | 2018-06-07 18:51:31 +0200 | [diff] [blame] | 523 | rc = mgcp_parse_audio_port_pt(r, line); |
Philipp Maier | 06da85e | 2017-10-05 18:49:24 +0200 | [diff] [blame] | 524 | if (rc) |
Philipp Maier | e9d645b | 2018-01-19 23:54:08 +0100 | [diff] [blame] | 525 | goto exit; |
Philipp Maier | 06da85e | 2017-10-05 18:49:24 +0200 | [diff] [blame] | 526 | break; |
| 527 | case 'c': |
| 528 | rc = mgcp_parse_audio_ip(r, line); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 529 | if (rc) |
Philipp Maier | e9d645b | 2018-01-19 23:54:08 +0100 | [diff] [blame] | 530 | goto exit; |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 531 | break; |
| 532 | default: |
| 533 | /* skip unhandled parameters */ |
| 534 | break; |
| 535 | } |
| 536 | } |
Philipp Maier | e9d645b | 2018-01-19 23:54:08 +0100 | [diff] [blame] | 537 | |
Philipp Maier | 704c4f0 | 2018-06-07 18:51:31 +0200 | [diff] [blame] | 538 | /* See also note in mgcp_parse_audio_port_pt() */ |
| 539 | for (i = 0; i < r->codecs_len; i++) |
| 540 | r->codecs[i] = map_pt_to_codec(r->ptmap, r->ptmap_len, r->codecs[i]); |
| 541 | |
Philipp Maier | e9d645b | 2018-01-19 23:54:08 +0100 | [diff] [blame] | 542 | rc = 0; |
| 543 | exit: |
| 544 | talloc_free(data); |
| 545 | return rc; |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 546 | } |
| 547 | |
Philipp Maier | 55295f7 | 2018-01-15 14:00:28 +0100 | [diff] [blame] | 548 | /* Parse a line like "X: something" */ |
| 549 | static int mgcp_parse_head_param(char *result, unsigned int result_len, |
| 550 | char label, const char *line) |
Philipp Maier | ffd75e4 | 2017-11-22 11:44:50 +0100 | [diff] [blame] | 551 | { |
Philipp Maier | 55295f7 | 2018-01-15 14:00:28 +0100 | [diff] [blame] | 552 | char label_string[4]; |
Neels Hofmeyr | 23e7bf1 | 2018-09-03 21:26:22 +0200 | [diff] [blame] | 553 | size_t rc; |
Philipp Maier | 55295f7 | 2018-01-15 14:00:28 +0100 | [diff] [blame] | 554 | |
| 555 | /* Detect empty parameters */ |
Philipp Maier | ffd75e4 | 2017-11-22 11:44:50 +0100 | [diff] [blame] | 556 | if (strlen(line) < 4) |
| 557 | goto response_parse_failure; |
| 558 | |
Philipp Maier | 55295f7 | 2018-01-15 14:00:28 +0100 | [diff] [blame] | 559 | /* Check if the label matches */ |
| 560 | snprintf(label_string, sizeof(label_string), "%c: ", label); |
| 561 | if (memcmp(label_string, line, 3) != 0) |
Philipp Maier | ffd75e4 | 2017-11-22 11:44:50 +0100 | [diff] [blame] | 562 | goto response_parse_failure; |
| 563 | |
Philipp Maier | 55295f7 | 2018-01-15 14:00:28 +0100 | [diff] [blame] | 564 | /* Copy payload part of the string to destinations (the label string |
| 565 | * is always 3 chars long) */ |
Neels Hofmeyr | 23e7bf1 | 2018-09-03 21:26:22 +0200 | [diff] [blame] | 566 | rc = osmo_strlcpy(result, line + 3, result_len); |
| 567 | if (rc >= result_len) { |
| 568 | LOGP(DLMGCP, LOGL_ERROR, |
| 569 | "Failed to parse MGCP response (parameter label: %c):" |
| 570 | " the received conn ID is too long: %zu, maximum is %u characters\n", |
| 571 | label, rc, result_len - 1); |
| 572 | return -ENOSPC; |
| 573 | } |
Philipp Maier | ffd75e4 | 2017-11-22 11:44:50 +0100 | [diff] [blame] | 574 | return 0; |
| 575 | |
| 576 | response_parse_failure: |
| 577 | LOGP(DLMGCP, LOGL_ERROR, |
Philipp Maier | 55295f7 | 2018-01-15 14:00:28 +0100 | [diff] [blame] | 578 | "Failed to parse MGCP response (parameter label: %c)\n", label); |
Philipp Maier | ffd75e4 | 2017-11-22 11:44:50 +0100 | [diff] [blame] | 579 | return -EINVAL; |
| 580 | } |
| 581 | |
| 582 | /* Parse MGCP parameters of the response */ |
| 583 | static int parse_head_params(struct mgcp_response *r) |
| 584 | { |
| 585 | char *line; |
| 586 | int rc = 0; |
| 587 | OSMO_ASSERT(r->body); |
Philipp Maier | 55295f7 | 2018-01-15 14:00:28 +0100 | [diff] [blame] | 588 | char *data; |
| 589 | char *data_ptr; |
| 590 | char *data_end; |
Philipp Maier | ffd75e4 | 2017-11-22 11:44:50 +0100 | [diff] [blame] | 591 | |
Philipp Maier | 55295f7 | 2018-01-15 14:00:28 +0100 | [diff] [blame] | 592 | /* Since this functions performs a destructive parsing, we create a |
| 593 | * local copy of the body data */ |
Philipp Maier | 1fc6999 | 2018-01-31 15:32:55 +0100 | [diff] [blame] | 594 | data = talloc_zero_size(r, strlen(r->body)+1); |
Philipp Maier | 55295f7 | 2018-01-15 14:00:28 +0100 | [diff] [blame] | 595 | OSMO_ASSERT(data); |
| 596 | data_ptr = data; |
| 597 | osmo_strlcpy(data, r->body, strlen(r->body)); |
| 598 | |
| 599 | /* If there is an SDP body attached, prevent for_each_non_empty_line() |
| 600 | * into running in there, we are not yet interested in the parameters |
| 601 | * stored there. */ |
| 602 | data_end = mgcp_find_section_end(data); |
Philipp Maier | ffd75e4 | 2017-11-22 11:44:50 +0100 | [diff] [blame] | 603 | if (data_end) |
| 604 | *data_end = '\0'; |
| 605 | |
Philipp Maier | 55295f7 | 2018-01-15 14:00:28 +0100 | [diff] [blame] | 606 | for_each_non_empty_line(line, data_ptr) { |
Philipp Maier | ffd75e4 | 2017-11-22 11:44:50 +0100 | [diff] [blame] | 607 | switch (line[0]) { |
Philipp Maier | 55295f7 | 2018-01-15 14:00:28 +0100 | [diff] [blame] | 608 | case 'Z': |
| 609 | rc = mgcp_parse_head_param(r->head.endpoint, |
| 610 | sizeof(r->head.endpoint), |
| 611 | 'Z', line); |
| 612 | if (rc) |
| 613 | goto exit; |
Philipp Maier | 771b26a | 2018-01-31 13:53:11 +0100 | [diff] [blame] | 614 | |
| 615 | /* A specific endpoint identifier returned by the MGW |
| 616 | * must not contain any wildcard characters */ |
| 617 | if (strstr(r->head.endpoint, "*") != NULL) { |
| 618 | rc = -EINVAL; |
| 619 | goto exit; |
| 620 | } |
Philipp Maier | 3261dc7 | 2018-01-31 14:03:13 +0100 | [diff] [blame] | 621 | |
| 622 | /* A specific endpoint identifier returned by the MGW |
| 623 | * must contain an @ character */ |
| 624 | if (strstr(r->head.endpoint, "@") == NULL) { |
| 625 | rc = -EINVAL; |
| 626 | goto exit; |
| 627 | } |
Philipp Maier | 55295f7 | 2018-01-15 14:00:28 +0100 | [diff] [blame] | 628 | break; |
Philipp Maier | ffd75e4 | 2017-11-22 11:44:50 +0100 | [diff] [blame] | 629 | case 'I': |
Philipp Maier | 55295f7 | 2018-01-15 14:00:28 +0100 | [diff] [blame] | 630 | rc = mgcp_parse_head_param(r->head.conn_id, |
| 631 | sizeof(r->head.conn_id), |
| 632 | 'I', line); |
Philipp Maier | ffd75e4 | 2017-11-22 11:44:50 +0100 | [diff] [blame] | 633 | if (rc) |
| 634 | goto exit; |
| 635 | break; |
| 636 | default: |
| 637 | /* skip unhandled parameters */ |
| 638 | break; |
| 639 | } |
| 640 | } |
| 641 | exit: |
Philipp Maier | 55295f7 | 2018-01-15 14:00:28 +0100 | [diff] [blame] | 642 | talloc_free(data); |
Philipp Maier | ffd75e4 | 2017-11-22 11:44:50 +0100 | [diff] [blame] | 643 | return rc; |
| 644 | } |
| 645 | |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 646 | static struct mgcp_response_pending *mgcp_client_response_pending_get( |
| 647 | struct mgcp_client *mgcp, |
Neels Hofmeyr | c8f37cb | 2017-11-30 13:43:11 +0100 | [diff] [blame] | 648 | mgcp_trans_id_t trans_id) |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 649 | { |
| 650 | struct mgcp_response_pending *pending; |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 651 | llist_for_each_entry(pending, &mgcp->responses_pending, entry) { |
Neels Hofmeyr | c8f37cb | 2017-11-30 13:43:11 +0100 | [diff] [blame] | 652 | if (pending->trans_id == trans_id) { |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 653 | llist_del(&pending->entry); |
| 654 | return pending; |
| 655 | } |
| 656 | } |
| 657 | return NULL; |
| 658 | } |
| 659 | |
| 660 | /* Feed an MGCP message into the receive processing. |
| 661 | * Parse the head and call any callback registered for the transaction id found |
| 662 | * in the MGCP message. This is normally called directly from the internal |
| 663 | * mgcp_do_read that reads from the socket connected to the MGCP gateway. This |
| 664 | * function is published mainly to be able to feed data from the test suite. |
| 665 | */ |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 666 | int mgcp_client_rx(struct mgcp_client *mgcp, struct msgb *msg) |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 667 | { |
Philipp Maier | 1fc6999 | 2018-01-31 15:32:55 +0100 | [diff] [blame] | 668 | struct mgcp_response *r; |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 669 | struct mgcp_response_pending *pending; |
| 670 | int rc; |
| 671 | |
Philipp Maier | 1fc6999 | 2018-01-31 15:32:55 +0100 | [diff] [blame] | 672 | r = talloc_zero(mgcp, struct mgcp_response); |
| 673 | OSMO_ASSERT(r); |
| 674 | |
| 675 | rc = mgcp_response_parse_head(r, msg); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 676 | if (rc) { |
Philipp Maier | ffd75e4 | 2017-11-22 11:44:50 +0100 | [diff] [blame] | 677 | LOGP(DLMGCP, LOGL_ERROR, "Cannot parse MGCP response (head)\n"); |
Philipp Maier | 1fc6999 | 2018-01-31 15:32:55 +0100 | [diff] [blame] | 678 | rc = 1; |
| 679 | goto error; |
Philipp Maier | ffd75e4 | 2017-11-22 11:44:50 +0100 | [diff] [blame] | 680 | } |
| 681 | |
Philipp Maier | 1fc6999 | 2018-01-31 15:32:55 +0100 | [diff] [blame] | 682 | rc = parse_head_params(r); |
Philipp Maier | ffd75e4 | 2017-11-22 11:44:50 +0100 | [diff] [blame] | 683 | if (rc) { |
| 684 | LOGP(DLMGCP, LOGL_ERROR, "Cannot parse MGCP response (head parameters)\n"); |
Philipp Maier | 1fc6999 | 2018-01-31 15:32:55 +0100 | [diff] [blame] | 685 | rc = 1; |
| 686 | goto error; |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 687 | } |
| 688 | |
Philipp Maier | 1fc6999 | 2018-01-31 15:32:55 +0100 | [diff] [blame] | 689 | pending = mgcp_client_response_pending_get(mgcp, r->head.trans_id); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 690 | if (!pending) { |
| 691 | LOGP(DLMGCP, LOGL_ERROR, |
| 692 | "Cannot find matching MGCP transaction for trans_id %d\n", |
Philipp Maier | 1fc6999 | 2018-01-31 15:32:55 +0100 | [diff] [blame] | 693 | r->head.trans_id); |
| 694 | rc = -ENOENT; |
| 695 | goto error; |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 696 | } |
| 697 | |
Philipp Maier | 1fc6999 | 2018-01-31 15:32:55 +0100 | [diff] [blame] | 698 | mgcp_client_handle_response(mgcp, pending, r); |
| 699 | rc = 0; |
| 700 | |
| 701 | error: |
| 702 | talloc_free(r); |
| 703 | return rc; |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 704 | } |
| 705 | |
| 706 | static int mgcp_do_read(struct osmo_fd *fd) |
| 707 | { |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 708 | struct mgcp_client *mgcp = fd->data; |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 709 | struct msgb *msg; |
| 710 | int ret; |
| 711 | |
| 712 | msg = msgb_alloc_headroom(4096, 128, "mgcp_from_gw"); |
| 713 | if (!msg) { |
| 714 | LOGP(DLMGCP, LOGL_ERROR, "Failed to allocate MGCP message.\n"); |
| 715 | return -1; |
| 716 | } |
| 717 | |
| 718 | ret = read(fd->fd, msg->data, 4096 - 128); |
| 719 | if (ret <= 0) { |
| 720 | LOGP(DLMGCP, LOGL_ERROR, "Failed to read: %d/%s\n", errno, strerror(errno)); |
| 721 | msgb_free(msg); |
| 722 | return -1; |
| 723 | } else if (ret > 4096 - 128) { |
| 724 | LOGP(DLMGCP, LOGL_ERROR, "Too much data: %d\n", ret); |
| 725 | msgb_free(msg); |
| 726 | return -1; |
Harald Welte | 9bf7c53 | 2017-11-17 14:14:31 +0100 | [diff] [blame] | 727 | } |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 728 | |
| 729 | msg->l2h = msgb_put(msg, ret); |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 730 | ret = mgcp_client_rx(mgcp, msg); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 731 | talloc_free(msg); |
| 732 | return ret; |
| 733 | } |
| 734 | |
| 735 | static int mgcp_do_write(struct osmo_fd *fd, struct msgb *msg) |
| 736 | { |
| 737 | int ret; |
| 738 | static char strbuf[4096]; |
| 739 | unsigned int l = msg->len < sizeof(strbuf) ? msg->len : sizeof(strbuf); |
| 740 | unsigned int i; |
| 741 | |
Philipp Maier | f8bfbe8 | 2017-11-23 19:32:31 +0100 | [diff] [blame] | 742 | osmo_strlcpy(strbuf, (const char*)msg->data, l); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 743 | for (i = 0; i < sizeof(strbuf); i++) { |
| 744 | if (strbuf[i] == '\n' || strbuf[i] == '\r') { |
| 745 | strbuf[i] = '\0'; |
| 746 | break; |
| 747 | } |
| 748 | } |
| 749 | DEBUGP(DLMGCP, "Tx MGCP msg to MGCP GW: '%s'\n", strbuf); |
| 750 | |
| 751 | LOGP(DLMGCP, LOGL_DEBUG, "Sending msg to MGCP GW size: %u\n", msg->len); |
| 752 | |
| 753 | ret = write(fd->fd, msg->data, msg->len); |
| 754 | if (ret != msg->len) |
| 755 | LOGP(DLMGCP, LOGL_ERROR, "Failed to forward message to MGCP" |
| 756 | " GW: %s\n", strerror(errno)); |
| 757 | |
| 758 | return ret; |
| 759 | } |
| 760 | |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 761 | struct mgcp_client *mgcp_client_init(void *ctx, |
| 762 | struct mgcp_client_conf *conf) |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 763 | { |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 764 | struct mgcp_client *mgcp; |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 765 | |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 766 | mgcp = talloc_zero(ctx, struct mgcp_client); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 767 | |
| 768 | INIT_LLIST_HEAD(&mgcp->responses_pending); |
| 769 | INIT_LLIST_HEAD(&mgcp->inuse_endpoints); |
| 770 | |
| 771 | mgcp->next_trans_id = 1; |
| 772 | |
| 773 | mgcp->actual.local_addr = conf->local_addr ? conf->local_addr : |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 774 | MGCP_CLIENT_LOCAL_ADDR_DEFAULT; |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 775 | 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] | 776 | MGCP_CLIENT_LOCAL_PORT_DEFAULT; |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 777 | |
| 778 | mgcp->actual.remote_addr = conf->remote_addr ? conf->remote_addr : |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 779 | MGCP_CLIENT_REMOTE_ADDR_DEFAULT; |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 780 | 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] | 781 | MGCP_CLIENT_REMOTE_PORT_DEFAULT; |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 782 | |
| 783 | mgcp->actual.first_endpoint = conf->first_endpoint > 0 ? (uint16_t)conf->first_endpoint : 0; |
| 784 | 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] | 785 | 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] | 786 | |
| 787 | return mgcp; |
| 788 | } |
| 789 | |
Philipp Maier | 6a26c16 | 2018-08-01 16:37:06 +0200 | [diff] [blame] | 790 | static int init_socket(struct mgcp_client *mgcp) |
| 791 | { |
| 792 | int rc; |
| 793 | struct osmo_wqueue *wq; |
| 794 | int i; |
| 795 | |
| 796 | wq = &mgcp->wq; |
| 797 | |
| 798 | for (i = 0; i < 100; i++) { |
| 799 | |
| 800 | /* Initalize socket with the currently configured port |
| 801 | * number */ |
| 802 | rc = osmo_sock_init2_ofd(&wq->bfd, AF_INET, SOCK_DGRAM, IPPROTO_UDP, mgcp->actual.local_addr, |
| 803 | mgcp->actual.local_port, mgcp->actual.remote_addr, mgcp->actual.remote_port, |
| 804 | OSMO_SOCK_F_BIND | OSMO_SOCK_F_CONNECT); |
| 805 | if (rc > 0) |
| 806 | return rc; |
| 807 | |
| 808 | /* If there is a different port than the default port |
| 809 | * configured then we assume that the user has choosen |
| 810 | * that port conciously and we will not try to resolve |
| 811 | * this by silently choosing a different port. */ |
Philipp Maier | 9a7ccc3 | 2018-08-09 11:41:19 +0200 | [diff] [blame] | 812 | if (mgcp->actual.local_port != MGCP_CLIENT_LOCAL_PORT_DEFAULT && i == 0) |
Philipp Maier | 6a26c16 | 2018-08-01 16:37:06 +0200 | [diff] [blame] | 813 | return -EINVAL; |
| 814 | |
| 815 | /* Choose a new port number to try next */ |
| 816 | LOGP(DLMGCP, LOGL_NOTICE, |
| 817 | "MGCPGW faild to bind to port %u, retrying with port %u -- check configuration!\n", |
| 818 | mgcp->actual.local_port, mgcp->actual.local_port + 1); |
| 819 | mgcp->actual.local_port++; |
| 820 | } |
| 821 | |
| 822 | LOGP(DLMGCP, LOGL_FATAL, "MGCPGW faild to find a port to bind on %i times.\n", i); |
| 823 | return -EINVAL; |
| 824 | } |
| 825 | |
Philipp Maier | 275ac97 | 2018-01-20 00:58:16 +0100 | [diff] [blame] | 826 | /*! Initalize client connection (opens socket only, no request is sent yet) |
| 827 | * \param[in,out] mgcp MGCP client descriptor. |
| 828 | * \returns 0 on success, -EINVAL on error. */ |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 829 | int mgcp_client_connect(struct mgcp_client *mgcp) |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 830 | { |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 831 | struct sockaddr_in addr; |
| 832 | struct osmo_wqueue *wq; |
| 833 | int rc; |
| 834 | |
| 835 | if (!mgcp) { |
| 836 | LOGP(DLMGCP, LOGL_FATAL, "MGCPGW client not initialized properly\n"); |
| 837 | return -EINVAL; |
| 838 | } |
| 839 | |
| 840 | wq = &mgcp->wq; |
| 841 | |
Philipp Maier | 6a26c16 | 2018-08-01 16:37:06 +0200 | [diff] [blame] | 842 | rc = init_socket(mgcp); |
Harald Welte | 8890dfa | 2017-11-17 15:09:30 +0100 | [diff] [blame] | 843 | if (rc < 0) { |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 844 | LOGP(DLMGCP, LOGL_FATAL, |
Harald Welte | 8890dfa | 2017-11-17 15:09:30 +0100 | [diff] [blame] | 845 | "Failed to initialize socket %s:%u -> %s:%u for MGCP GW: %s\n", |
| 846 | mgcp->actual.local_addr, mgcp->actual.local_port, |
| 847 | mgcp->actual.remote_addr, mgcp->actual.remote_port, strerror(errno)); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 848 | goto error_close_fd; |
| 849 | } |
| 850 | |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 851 | inet_aton(mgcp->actual.remote_addr, &addr.sin_addr); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 852 | mgcp->remote_addr = htonl(addr.sin_addr.s_addr); |
| 853 | |
| 854 | osmo_wqueue_init(wq, 10); |
| 855 | wq->bfd.when = BSC_FD_READ; |
| 856 | wq->bfd.data = mgcp; |
| 857 | wq->read_cb = mgcp_do_read; |
| 858 | wq->write_cb = mgcp_do_write; |
| 859 | |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 860 | LOGP(DLMGCP, LOGL_INFO, "MGCP GW connection: %s:%u -> %s:%u\n", |
| 861 | mgcp->actual.local_addr, mgcp->actual.local_port, |
| 862 | mgcp->actual.remote_addr, mgcp->actual.remote_port); |
| 863 | |
| 864 | return 0; |
| 865 | error_close_fd: |
| 866 | close(wq->bfd.fd); |
| 867 | wq->bfd.fd = -1; |
| 868 | return rc; |
| 869 | } |
| 870 | |
Philipp Maier | 275ac97 | 2018-01-20 00:58:16 +0100 | [diff] [blame] | 871 | /*! Get the IP-Aaddress of the associated MGW as string. |
| 872 | * \param[in] mgcp MGCP client descriptor. |
| 873 | * \returns a pointer to the address string. */ |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 874 | const char *mgcp_client_remote_addr_str(struct mgcp_client *mgcp) |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 875 | { |
| 876 | return mgcp->actual.remote_addr; |
| 877 | } |
| 878 | |
Philipp Maier | 275ac97 | 2018-01-20 00:58:16 +0100 | [diff] [blame] | 879 | /*! Get the IP-Port of the associated MGW. |
| 880 | * \param[in] mgcp MGCP client descriptor. |
| 881 | * \returns port number. */ |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 882 | uint16_t mgcp_client_remote_port(struct mgcp_client *mgcp) |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 883 | { |
| 884 | return mgcp->actual.remote_port; |
| 885 | } |
| 886 | |
Philipp Maier | 275ac97 | 2018-01-20 00:58:16 +0100 | [diff] [blame] | 887 | /*! Get the IP-Aaddress of the associated MGW as its numeric representation. |
| 888 | * \param[in] mgcp MGCP client descriptor. |
| 889 | * \returns IP-Address as 32 bit integer (network byte order) */ |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 890 | uint32_t mgcp_client_remote_addr_n(struct mgcp_client *mgcp) |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 891 | { |
| 892 | return mgcp->remote_addr; |
| 893 | } |
| 894 | |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 895 | struct mgcp_response_pending * mgcp_client_pending_add( |
| 896 | struct mgcp_client *mgcp, |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 897 | mgcp_trans_id_t trans_id, |
| 898 | mgcp_response_cb_t response_cb, |
| 899 | void *priv) |
| 900 | { |
| 901 | struct mgcp_response_pending *pending; |
| 902 | |
| 903 | pending = talloc_zero(mgcp, struct mgcp_response_pending); |
| 904 | pending->trans_id = trans_id; |
| 905 | pending->response_cb = response_cb; |
| 906 | pending->priv = priv; |
| 907 | llist_add_tail(&pending->entry, &mgcp->responses_pending); |
| 908 | |
| 909 | return pending; |
| 910 | } |
| 911 | |
| 912 | /* Send the MGCP message in msg to the MGCP GW and handle a response with |
| 913 | * response_cb. NOTE: the response_cb still needs to call |
| 914 | * mgcp_response_parse_params(response) to get the parsed parameters -- to |
| 915 | * 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] | 916 | * the response_cb is invoked. |
| 917 | * Before the priv pointer becomes invalid, e.g. due to transaction timeout, |
| 918 | * mgcp_client_cancel() needs to be called for this transaction. |
| 919 | */ |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 920 | int mgcp_client_tx(struct mgcp_client *mgcp, struct msgb *msg, |
| 921 | mgcp_response_cb_t response_cb, void *priv) |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 922 | { |
| 923 | struct mgcp_response_pending *pending; |
| 924 | mgcp_trans_id_t trans_id; |
| 925 | int rc; |
| 926 | |
| 927 | trans_id = msg->cb[MSGB_CB_MGCP_TRANS_ID]; |
| 928 | if (!trans_id) { |
| 929 | LOGP(DLMGCP, LOGL_ERROR, |
| 930 | "Unset transaction id in mgcp send request\n"); |
| 931 | talloc_free(msg); |
| 932 | return -EINVAL; |
| 933 | } |
| 934 | |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 935 | pending = mgcp_client_pending_add(mgcp, trans_id, response_cb, priv); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 936 | |
| 937 | if (msgb_l2len(msg) > 4096) { |
| 938 | LOGP(DLMGCP, LOGL_ERROR, |
| 939 | "Cannot send, MGCP message too large: %u\n", |
| 940 | msgb_l2len(msg)); |
| 941 | msgb_free(msg); |
| 942 | rc = -EINVAL; |
| 943 | goto mgcp_tx_error; |
| 944 | } |
| 945 | |
| 946 | rc = osmo_wqueue_enqueue(&mgcp->wq, msg); |
| 947 | if (rc) { |
| 948 | LOGP(DLMGCP, LOGL_FATAL, "Could not queue message to MGCP GW\n"); |
| 949 | msgb_free(msg); |
| 950 | goto mgcp_tx_error; |
| 951 | } else |
| 952 | LOGP(DLMGCP, LOGL_INFO, "Queued %u bytes for MGCP GW\n", |
| 953 | msgb_l2len(msg)); |
| 954 | return 0; |
| 955 | |
| 956 | mgcp_tx_error: |
| 957 | /* Pass NULL to response cb to indicate an error */ |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 958 | mgcp_client_handle_response(mgcp, pending, NULL); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 959 | return -1; |
| 960 | } |
| 961 | |
Philipp Maier | 275ac97 | 2018-01-20 00:58:16 +0100 | [diff] [blame] | 962 | /*! Cancel a pending transaction. |
| 963 | * \param[in] mgcp MGCP client descriptor. |
| 964 | * \param[in,out] trans_id Transaction id. |
| 965 | * \returns 0 on success, -ENOENT on error. |
| 966 | * |
Neels Hofmeyr | c8f37cb | 2017-11-30 13:43:11 +0100 | [diff] [blame] | 967 | * Should a priv pointer passed to mgcp_client_tx() become invalid, this function must be called. In |
| 968 | * practical terms, if the caller of mgcp_client_tx() wishes to tear down a transaction without having |
| 969 | * 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] | 970 | * mgcp_msg_trans_id() on the msgb produced by mgcp_msg_gen(). */ |
Neels Hofmeyr | c8f37cb | 2017-11-30 13:43:11 +0100 | [diff] [blame] | 971 | int mgcp_client_cancel(struct mgcp_client *mgcp, mgcp_trans_id_t trans_id) |
| 972 | { |
| 973 | struct mgcp_response_pending *pending = mgcp_client_response_pending_get(mgcp, trans_id); |
| 974 | if (!pending) { |
Philipp Maier | 275ac97 | 2018-01-20 00:58:16 +0100 | [diff] [blame] | 975 | /*! Note: it is not harmful to cancel a transaction twice. */ |
Neels Hofmeyr | c8f37cb | 2017-11-30 13:43:11 +0100 | [diff] [blame] | 976 | LOGP(DLMGCP, LOGL_INFO, "Cannot cancel, no such transaction: %u\n", trans_id); |
| 977 | return -ENOENT; |
| 978 | } |
| 979 | LOGP(DLMGCP, LOGL_INFO, "Canceled transaction %u\n", trans_id); |
| 980 | talloc_free(pending); |
| 981 | return 0; |
Philipp Maier | 275ac97 | 2018-01-20 00:58:16 +0100 | [diff] [blame] | 982 | /*! We don't really need to clean up the wqueue: In all sane cases, the msgb has already been sent |
| 983 | * out and is no longer in the wqueue. If it still is in the wqueue, then sending MGCP messages |
| 984 | * per se is broken and the program should notice so by a full wqueue. Even if this was called |
| 985 | * before we had a chance to send out the message and it is still going to be sent, we will just |
| 986 | * ignore the reply to it later. Removing a msgb from the wqueue here would just introduce more |
| 987 | * 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] | 988 | */ |
| 989 | } |
| 990 | |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 991 | static struct msgb *mgcp_msg_from_buf(mgcp_trans_id_t trans_id, |
| 992 | const char *buf, int len) |
| 993 | { |
| 994 | struct msgb *msg; |
| 995 | |
| 996 | if (len > (4096 - 128)) { |
| 997 | LOGP(DLMGCP, LOGL_ERROR, "Cannot send to MGCP GW:" |
| 998 | " message too large: %d\n", len); |
| 999 | return NULL; |
| 1000 | } |
| 1001 | |
| 1002 | msg = msgb_alloc_headroom(4096, 128, "MGCP tx"); |
| 1003 | OSMO_ASSERT(msg); |
| 1004 | |
| 1005 | char *dst = (char*)msgb_put(msg, len); |
| 1006 | memcpy(dst, buf, len); |
| 1007 | msg->l2h = msg->data; |
| 1008 | msg->cb[MSGB_CB_MGCP_TRANS_ID] = trans_id; |
| 1009 | |
| 1010 | return msg; |
| 1011 | } |
| 1012 | |
| 1013 | static struct msgb *mgcp_msg_from_str(mgcp_trans_id_t trans_id, |
| 1014 | const char *fmt, ...) |
| 1015 | { |
| 1016 | static char compose[4096 - 128]; |
| 1017 | va_list ap; |
| 1018 | int len; |
| 1019 | OSMO_ASSERT(fmt); |
| 1020 | |
| 1021 | va_start(ap, fmt); |
| 1022 | len = vsnprintf(compose, sizeof(compose), fmt, ap); |
| 1023 | va_end(ap); |
| 1024 | if (len >= sizeof(compose)) { |
| 1025 | LOGP(DLMGCP, LOGL_ERROR, |
| 1026 | "Message too large: trans_id=%u len=%d\n", |
| 1027 | trans_id, len); |
| 1028 | return NULL; |
| 1029 | } |
| 1030 | if (len < 1) { |
| 1031 | LOGP(DLMGCP, LOGL_ERROR, |
| 1032 | "Failed to compose message: trans_id=%u len=%d\n", |
| 1033 | trans_id, len); |
| 1034 | return NULL; |
| 1035 | } |
| 1036 | return mgcp_msg_from_buf(trans_id, compose, len); |
| 1037 | } |
| 1038 | |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 1039 | 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] | 1040 | { |
| 1041 | /* avoid zero trans_id to distinguish from unset trans_id */ |
| 1042 | if (!mgcp->next_trans_id) |
| 1043 | mgcp->next_trans_id ++; |
| 1044 | return mgcp->next_trans_id ++; |
| 1045 | } |
| 1046 | |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 1047 | struct msgb *mgcp_msg_crcx(struct mgcp_client *mgcp, |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 1048 | uint16_t rtp_endpoint, unsigned int call_id, |
| 1049 | enum mgcp_connection_mode mode) |
| 1050 | { |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 1051 | mgcp_trans_id_t trans_id = mgcp_client_next_trans_id(mgcp); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 1052 | return mgcp_msg_from_str(trans_id, |
| 1053 | "CRCX %u %x@mgw MGCP 1.0\r\n" |
| 1054 | "C: %x\r\n" |
| 1055 | "L: p:20, a:AMR, nt:IN\r\n" |
| 1056 | "M: %s\r\n" |
| 1057 | , |
| 1058 | trans_id, |
| 1059 | rtp_endpoint, |
| 1060 | call_id, |
Neels Hofmeyr | d95ab1e | 2017-09-22 00:52:54 +0200 | [diff] [blame] | 1061 | mgcp_client_cmode_name(mode)); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 1062 | } |
| 1063 | |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 1064 | struct msgb *mgcp_msg_mdcx(struct mgcp_client *mgcp, |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 1065 | uint16_t rtp_endpoint, const char *rtp_conn_addr, |
| 1066 | uint16_t rtp_port, enum mgcp_connection_mode mode) |
| 1067 | |
| 1068 | { |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 1069 | mgcp_trans_id_t trans_id = mgcp_client_next_trans_id(mgcp); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 1070 | return mgcp_msg_from_str(trans_id, |
| 1071 | "MDCX %u %x@mgw MGCP 1.0\r\n" |
| 1072 | "M: %s\r\n" |
| 1073 | "\r\n" |
| 1074 | "c=IN IP4 %s\r\n" |
| 1075 | "m=audio %u RTP/AVP 255\r\n" |
| 1076 | , |
| 1077 | trans_id, |
| 1078 | rtp_endpoint, |
Neels Hofmeyr | d95ab1e | 2017-09-22 00:52:54 +0200 | [diff] [blame] | 1079 | mgcp_client_cmode_name(mode), |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 1080 | rtp_conn_addr, |
| 1081 | rtp_port); |
| 1082 | } |
| 1083 | |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 1084 | 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] | 1085 | unsigned int call_id) |
| 1086 | { |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 1087 | mgcp_trans_id_t trans_id = mgcp_client_next_trans_id(mgcp); |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 1088 | return mgcp_msg_from_str(trans_id, |
| 1089 | "DLCX %u %x@mgw MGCP 1.0\r\n" |
| 1090 | "C: %x\r\n", trans_id, rtp_endpoint, call_id); |
| 1091 | } |
| 1092 | |
Philipp Maier | 1dc6be6 | 2017-10-05 18:25:37 +0200 | [diff] [blame] | 1093 | #define MGCP_CRCX_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT | \ |
| 1094 | MGCP_MSG_PRESENCE_CALL_ID | \ |
Philipp Maier | 1dc6be6 | 2017-10-05 18:25:37 +0200 | [diff] [blame] | 1095 | MGCP_MSG_PRESENCE_CONN_MODE) |
| 1096 | #define MGCP_MDCX_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT | \ |
Philipp Maier | 490cbaa | 2018-01-22 17:32:38 +0100 | [diff] [blame] | 1097 | MGCP_MSG_PRESENCE_CALL_ID | \ |
Philipp Maier | 1dc6be6 | 2017-10-05 18:25:37 +0200 | [diff] [blame] | 1098 | MGCP_MSG_PRESENCE_CONN_ID) |
| 1099 | #define MGCP_DLCX_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT) |
| 1100 | #define MGCP_AUEP_MANDATORY (MGCP_MSG_PRESENCE_ENDPOINT) |
| 1101 | #define MGCP_RSIP_MANDATORY 0 /* none */ |
| 1102 | |
Philipp Maier | 704c4f0 | 2018-06-07 18:51:31 +0200 | [diff] [blame] | 1103 | /* Helper function for mgcp_msg_gen(): Add LCO information to MGCP message */ |
| 1104 | static int add_lco(struct msgb *msg, struct mgcp_msg *mgcp_msg) |
| 1105 | { |
| 1106 | unsigned int i; |
| 1107 | int rc = 0; |
| 1108 | const char *codec; |
| 1109 | unsigned int pt; |
| 1110 | |
| 1111 | rc += msgb_printf(msg, "L:"); |
| 1112 | |
| 1113 | if (mgcp_msg->ptime) |
| 1114 | rc += msgb_printf(msg, " p:%u,", mgcp_msg->ptime); |
| 1115 | |
| 1116 | if (mgcp_msg->codecs_len) { |
| 1117 | rc += msgb_printf(msg, " a:"); |
| 1118 | for (i = 0; i < mgcp_msg->codecs_len; i++) { |
| 1119 | pt = mgcp_msg->codecs[i]; |
| 1120 | codec = get_value_string_or_null(codec_table, pt); |
| 1121 | |
| 1122 | /* Note: Use codec descriptors from enum mgcp_codecs |
| 1123 | * in mgcp_client only! */ |
| 1124 | OSMO_ASSERT(codec); |
| 1125 | rc += msgb_printf(msg, "%s", extract_codec_name(codec)); |
| 1126 | if (i < mgcp_msg->codecs_len - 1) |
| 1127 | rc += msgb_printf(msg, ";"); |
| 1128 | } |
| 1129 | rc += msgb_printf(msg, ","); |
| 1130 | } |
| 1131 | |
| 1132 | rc += msgb_printf(msg, " nt:IN\r\n"); |
| 1133 | |
| 1134 | return rc; |
| 1135 | } |
| 1136 | |
| 1137 | /* Helper function for mgcp_msg_gen(): Add SDP information to MGCP message */ |
| 1138 | static int add_sdp(struct msgb *msg, struct mgcp_msg *mgcp_msg, struct mgcp_client *mgcp) |
| 1139 | { |
| 1140 | unsigned int i; |
| 1141 | int rc = 0; |
| 1142 | char local_ip[INET_ADDRSTRLEN]; |
| 1143 | const char *codec; |
| 1144 | unsigned int pt; |
| 1145 | |
| 1146 | /* Add separator to mark the beginning of the SDP block */ |
| 1147 | rc += msgb_printf(msg, "\r\n"); |
| 1148 | |
| 1149 | /* Add SDP protocol version */ |
| 1150 | rc += msgb_printf(msg, "v=0\r\n"); |
| 1151 | |
| 1152 | /* Determine local IP-Address */ |
| 1153 | if (osmo_sock_local_ip(local_ip, mgcp->actual.remote_addr) < 0) { |
| 1154 | LOGP(DLMGCP, LOGL_ERROR, |
| 1155 | "Could not determine local IP-Address!\n"); |
| 1156 | msgb_free(msg); |
| 1157 | return -2; |
| 1158 | } |
| 1159 | |
| 1160 | /* Add owner/creator (SDP) */ |
| 1161 | rc += msgb_printf(msg, "o=- %x 23 IN IP4 %s\r\n", |
| 1162 | mgcp_msg->call_id, local_ip); |
| 1163 | |
| 1164 | /* Add session name (none) */ |
| 1165 | rc += msgb_printf(msg, "s=-\r\n"); |
| 1166 | |
| 1167 | /* Add RTP address and port */ |
| 1168 | if (mgcp_msg->audio_port == 0) { |
| 1169 | LOGP(DLMGCP, LOGL_ERROR, |
| 1170 | "Invalid port number, can not generate MGCP message\n"); |
| 1171 | msgb_free(msg); |
| 1172 | return -2; |
| 1173 | } |
| 1174 | if (strlen(mgcp_msg->audio_ip) <= 0) { |
| 1175 | LOGP(DLMGCP, LOGL_ERROR, |
| 1176 | "Empty ip address, can not generate MGCP message\n"); |
| 1177 | msgb_free(msg); |
| 1178 | return -2; |
| 1179 | } |
| 1180 | rc += msgb_printf(msg, "c=IN IP4 %s\r\n", mgcp_msg->audio_ip); |
| 1181 | |
| 1182 | /* Add time description, active time (SDP) */ |
| 1183 | rc += msgb_printf(msg, "t=0 0\r\n"); |
| 1184 | |
| 1185 | rc += msgb_printf(msg, "m=audio %u RTP/AVP", mgcp_msg->audio_port); |
| 1186 | for (i = 0; i < mgcp_msg->codecs_len; i++) { |
| 1187 | pt = map_codec_to_pt(mgcp_msg->ptmap, mgcp_msg->ptmap_len, mgcp_msg->codecs[i]); |
| 1188 | rc += msgb_printf(msg, " %u", pt); |
| 1189 | |
| 1190 | } |
| 1191 | rc += msgb_printf(msg, "\r\n"); |
| 1192 | |
| 1193 | for (i = 0; i < mgcp_msg->codecs_len; i++) { |
| 1194 | pt = map_codec_to_pt(mgcp_msg->ptmap, mgcp_msg->ptmap_len, mgcp_msg->codecs[i]); |
| 1195 | |
| 1196 | /* Note: Only dynamic payload type from the range 96-127 |
| 1197 | * require to be explained further via rtpmap. All others |
| 1198 | * are implcitly definedby the number in m=audio */ |
| 1199 | if (pt >= 96 && pt <= 127) { |
| 1200 | codec = get_value_string_or_null(codec_table, mgcp_msg->codecs[i]); |
| 1201 | |
| 1202 | /* Note: Use codec descriptors from enum mgcp_codecs |
| 1203 | * in mgcp_client only! */ |
| 1204 | OSMO_ASSERT(codec); |
| 1205 | |
| 1206 | rc += msgb_printf(msg, "a=rtpmap:%u %s\r\n", pt, codec); |
| 1207 | } |
| 1208 | } |
| 1209 | |
| 1210 | if (mgcp_msg->ptime) |
| 1211 | rc += msgb_printf(msg, "a=ptime:%u\r\n", mgcp_msg->ptime); |
| 1212 | |
| 1213 | return rc; |
| 1214 | } |
| 1215 | |
Philipp Maier | 275ac97 | 2018-01-20 00:58:16 +0100 | [diff] [blame] | 1216 | /*! Generate an MGCP message |
| 1217 | * \param[in] mgcp MGCP client descriptor. |
| 1218 | * \param[in] mgcp_msg Message description |
| 1219 | * \returns message buffer on success, NULL on error. */ |
Philipp Maier | 1dc6be6 | 2017-10-05 18:25:37 +0200 | [diff] [blame] | 1220 | struct msgb *mgcp_msg_gen(struct mgcp_client *mgcp, struct mgcp_msg *mgcp_msg) |
| 1221 | { |
| 1222 | mgcp_trans_id_t trans_id = mgcp_client_next_trans_id(mgcp); |
| 1223 | uint32_t mandatory_mask; |
| 1224 | struct msgb *msg = msgb_alloc_headroom(4096, 128, "MGCP tx"); |
| 1225 | int rc = 0; |
Philipp Maier | 704c4f0 | 2018-06-07 18:51:31 +0200 | [diff] [blame] | 1226 | int rc_sdp; |
| 1227 | bool use_sdp = false; |
Philipp Maier | 1dc6be6 | 2017-10-05 18:25:37 +0200 | [diff] [blame] | 1228 | |
| 1229 | msg->l2h = msg->data; |
| 1230 | msg->cb[MSGB_CB_MGCP_TRANS_ID] = trans_id; |
| 1231 | |
| 1232 | /* Add command verb */ |
| 1233 | switch (mgcp_msg->verb) { |
| 1234 | case MGCP_VERB_CRCX: |
| 1235 | mandatory_mask = MGCP_CRCX_MANDATORY; |
| 1236 | rc += msgb_printf(msg, "CRCX %u", trans_id); |
| 1237 | break; |
| 1238 | case MGCP_VERB_MDCX: |
| 1239 | mandatory_mask = MGCP_MDCX_MANDATORY; |
| 1240 | rc += msgb_printf(msg, "MDCX %u", trans_id); |
| 1241 | break; |
| 1242 | case MGCP_VERB_DLCX: |
| 1243 | mandatory_mask = MGCP_DLCX_MANDATORY; |
| 1244 | rc += msgb_printf(msg, "DLCX %u", trans_id); |
| 1245 | break; |
| 1246 | case MGCP_VERB_AUEP: |
| 1247 | mandatory_mask = MGCP_AUEP_MANDATORY; |
| 1248 | rc += msgb_printf(msg, "AUEP %u", trans_id); |
| 1249 | break; |
| 1250 | case MGCP_VERB_RSIP: |
| 1251 | mandatory_mask = MGCP_RSIP_MANDATORY; |
| 1252 | rc += msgb_printf(msg, "RSIP %u", trans_id); |
| 1253 | break; |
| 1254 | default: |
| 1255 | LOGP(DLMGCP, LOGL_ERROR, |
| 1256 | "Invalid command verb, can not generate MGCP message\n"); |
| 1257 | msgb_free(msg); |
| 1258 | return NULL; |
| 1259 | } |
| 1260 | |
| 1261 | /* Check if mandatory fields are missing */ |
| 1262 | if (!((mgcp_msg->presence & mandatory_mask) == mandatory_mask)) { |
| 1263 | LOGP(DLMGCP, LOGL_ERROR, |
| 1264 | "One or more missing mandatory fields, can not generate MGCP message\n"); |
| 1265 | msgb_free(msg); |
| 1266 | return NULL; |
| 1267 | } |
| 1268 | |
| 1269 | /* Add endpoint name */ |
Philipp Maier | 7bc5552 | 2017-12-10 22:52:22 +0100 | [diff] [blame] | 1270 | if (mgcp_msg->presence & MGCP_MSG_PRESENCE_ENDPOINT) { |
| 1271 | if (strlen(mgcp_msg->endpoint) <= 0) { |
| 1272 | LOGP(DLMGCP, LOGL_ERROR, |
| 1273 | "Empty endpoint name, can not generate MGCP message\n"); |
| 1274 | msgb_free(msg); |
| 1275 | return NULL; |
| 1276 | } |
Philipp Maier | 3aa8157 | 2018-01-31 14:13:45 +0100 | [diff] [blame] | 1277 | |
| 1278 | if (strstr(mgcp_msg->endpoint, "@") == NULL) { |
| 1279 | LOGP(DLMGCP, LOGL_ERROR, |
| 1280 | "Endpoint name (%s) lacks separator (@), can not generate MGCP message\n", |
| 1281 | mgcp_msg->endpoint); |
| 1282 | msgb_free(msg); |
| 1283 | } |
| 1284 | |
Philipp Maier | 1dc6be6 | 2017-10-05 18:25:37 +0200 | [diff] [blame] | 1285 | rc += msgb_printf(msg, " %s", mgcp_msg->endpoint); |
Philipp Maier | 7bc5552 | 2017-12-10 22:52:22 +0100 | [diff] [blame] | 1286 | } |
Philipp Maier | 1dc6be6 | 2017-10-05 18:25:37 +0200 | [diff] [blame] | 1287 | |
| 1288 | /* Add protocol version */ |
| 1289 | rc += msgb_printf(msg, " MGCP 1.0\r\n"); |
| 1290 | |
| 1291 | /* Add call id */ |
| 1292 | if (mgcp_msg->presence & MGCP_MSG_PRESENCE_CALL_ID) |
| 1293 | rc += msgb_printf(msg, "C: %x\r\n", mgcp_msg->call_id); |
| 1294 | |
| 1295 | /* Add connection id */ |
Philipp Maier | 7bc5552 | 2017-12-10 22:52:22 +0100 | [diff] [blame] | 1296 | if (mgcp_msg->presence & MGCP_MSG_PRESENCE_CONN_ID) { |
| 1297 | if (strlen(mgcp_msg->conn_id) <= 0) { |
| 1298 | LOGP(DLMGCP, LOGL_ERROR, |
| 1299 | "Empty connection id, can not generate MGCP message\n"); |
| 1300 | msgb_free(msg); |
| 1301 | return NULL; |
| 1302 | } |
Philipp Maier | 01d24a3 | 2017-11-21 17:26:09 +0100 | [diff] [blame] | 1303 | rc += msgb_printf(msg, "I: %s\r\n", mgcp_msg->conn_id); |
Philipp Maier | 7bc5552 | 2017-12-10 22:52:22 +0100 | [diff] [blame] | 1304 | } |
Philipp Maier | 1dc6be6 | 2017-10-05 18:25:37 +0200 | [diff] [blame] | 1305 | |
Philipp Maier | 704c4f0 | 2018-06-07 18:51:31 +0200 | [diff] [blame] | 1306 | /* Using SDP makes sense when a valid IP/Port combination is specifiec, |
| 1307 | * if we do not know this information yet, we fall back to LCO */ |
| 1308 | if (mgcp_msg->presence & MGCP_MSG_PRESENCE_AUDIO_IP |
| 1309 | && mgcp_msg->presence & MGCP_MSG_PRESENCE_AUDIO_PORT) |
| 1310 | use_sdp = true; |
| 1311 | |
| 1312 | /* Add local connection options (LCO) */ |
| 1313 | if (!use_sdp |
| 1314 | && (mgcp_msg->verb == MGCP_VERB_CRCX |
| 1315 | || mgcp_msg->verb == MGCP_VERB_MDCX)) |
| 1316 | rc += add_lco(msg, mgcp_msg); |
Philipp Maier | 1dc6be6 | 2017-10-05 18:25:37 +0200 | [diff] [blame] | 1317 | |
| 1318 | /* Add mode */ |
| 1319 | if (mgcp_msg->presence & MGCP_MSG_PRESENCE_CONN_MODE) |
| 1320 | rc += |
| 1321 | msgb_printf(msg, "M: %s\r\n", |
| 1322 | mgcp_client_cmode_name(mgcp_msg->conn_mode)); |
| 1323 | |
Neels Hofmeyr | e6d8e91 | 2018-08-23 16:36:48 +0200 | [diff] [blame] | 1324 | /* Add X-Osmo-IGN */ |
| 1325 | if ((mgcp_msg->presence & MGCP_MSG_PRESENCE_X_OSMO_IGN) |
| 1326 | && (mgcp_msg->x_osmo_ign != 0)) |
| 1327 | rc += |
| 1328 | msgb_printf(msg, MGCP_X_OSMO_IGN_HEADER "%s\r\n", |
| 1329 | mgcp_msg->x_osmo_ign & MGCP_X_OSMO_IGN_CALLID ? " C": ""); |
| 1330 | |
Philipp Maier | 704c4f0 | 2018-06-07 18:51:31 +0200 | [diff] [blame] | 1331 | /* Add session description protocol (SDP) */ |
| 1332 | if (use_sdp |
| 1333 | && (mgcp_msg->verb == MGCP_VERB_CRCX |
| 1334 | || mgcp_msg->verb == MGCP_VERB_MDCX)) { |
| 1335 | rc_sdp = add_sdp(msg, mgcp_msg, mgcp); |
| 1336 | if (rc_sdp == -2) |
Philipp Maier | 9d25d7a | 2018-01-22 17:31:10 +0100 | [diff] [blame] | 1337 | return NULL; |
Philipp Maier | 704c4f0 | 2018-06-07 18:51:31 +0200 | [diff] [blame] | 1338 | else |
| 1339 | rc += rc_sdp; |
Philipp Maier | 1dc6be6 | 2017-10-05 18:25:37 +0200 | [diff] [blame] | 1340 | } |
| 1341 | |
| 1342 | if (rc != 0) { |
| 1343 | LOGP(DLMGCP, LOGL_ERROR, |
| 1344 | "message buffer to small, can not generate MGCP message\n"); |
| 1345 | msgb_free(msg); |
| 1346 | msg = NULL; |
| 1347 | } |
| 1348 | |
| 1349 | return msg; |
| 1350 | } |
| 1351 | |
Philipp Maier | 275ac97 | 2018-01-20 00:58:16 +0100 | [diff] [blame] | 1352 | /*! Retrieve the MGCP transaction ID from a msgb generated by mgcp_msg_gen() |
| 1353 | * \param[in] msg message buffer |
| 1354 | * \returns Transaction id. */ |
Neels Hofmeyr | c8f37cb | 2017-11-30 13:43:11 +0100 | [diff] [blame] | 1355 | mgcp_trans_id_t mgcp_msg_trans_id(struct msgb *msg) |
| 1356 | { |
| 1357 | return (mgcp_trans_id_t)msg->cb[MSGB_CB_MGCP_TRANS_ID]; |
| 1358 | } |
| 1359 | |
Philipp Maier | 275ac97 | 2018-01-20 00:58:16 +0100 | [diff] [blame] | 1360 | /*! Get the configuration parameters a given MGCP client instance |
| 1361 | * \param[in] mgcp MGCP client descriptor. |
| 1362 | * \returns configuration */ |
Neels Hofmeyr | 3a8e723 | 2017-09-04 01:02:56 +0200 | [diff] [blame] | 1363 | struct mgcp_client_conf *mgcp_client_conf_actual(struct mgcp_client *mgcp) |
Neels Hofmeyr | e9920f2 | 2017-07-10 15:07:22 +0200 | [diff] [blame] | 1364 | { |
| 1365 | return &mgcp->actual; |
| 1366 | } |
Neels Hofmeyr | d95ab1e | 2017-09-22 00:52:54 +0200 | [diff] [blame] | 1367 | |
| 1368 | const struct value_string mgcp_client_connection_mode_strs[] = { |
| 1369 | { MGCP_CONN_NONE, "none" }, |
| 1370 | { MGCP_CONN_RECV_SEND, "sendrecv" }, |
| 1371 | { MGCP_CONN_SEND_ONLY, "sendonly" }, |
| 1372 | { MGCP_CONN_RECV_ONLY, "recvonly" }, |
| 1373 | { MGCP_CONN_LOOPBACK, "loopback" }, |
| 1374 | { 0, NULL } |
| 1375 | }; |