Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 1 | /* A Media Gateway Control Protocol Media Gateway: RFC 3435 */ |
| 2 | /* Message parser/generator utilities */ |
| 3 | |
| 4 | /* |
| 5 | * (C) 2009-2012 by Holger Hans Peter Freyther <zecke@selfish.org> |
| 6 | * (C) 2009-2012 by On-Waves |
| 7 | * (C) 2017 by sysmocom s.f.m.c. GmbH <info@sysmocom.de> |
| 8 | * All Rights Reserved |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU Affero General Public License as published by |
| 12 | * the Free Software Foundation; either version 3 of the License, or |
| 13 | * (at your option) any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU Affero General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU Affero General Public License |
| 21 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 22 | * |
| 23 | */ |
| 24 | |
| 25 | #include <limits.h> |
| 26 | |
| 27 | #include <osmocom/mgcp/mgcp_internal.h> |
Neels Hofmeyr | 6779354 | 2017-09-08 04:25:16 +0200 | [diff] [blame] | 28 | #include <osmocom/mgcp/mgcp_common.h> |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 29 | #include <osmocom/mgcp/mgcp_msg.h> |
| 30 | #include <osmocom/mgcp/mgcp_conn.h> |
| 31 | |
| 32 | /*! Display an mgcp message on the log output. |
| 33 | * \param[in] message mgcp message string |
| 34 | * \param[in] len message mgcp message string length |
| 35 | * \param[in] preamble string to display in logtext in front of each line */ |
| 36 | void mgcp_disp_msg(unsigned char *message, unsigned int len, char *preamble) |
| 37 | { |
| 38 | unsigned char line[80]; |
| 39 | unsigned char *ptr; |
| 40 | unsigned int consumed = 0; |
| 41 | unsigned int consumed_line = 0; |
| 42 | unsigned int line_count = 0; |
| 43 | |
| 44 | if (!log_check_level(DLMGCP, LOGL_DEBUG)) |
| 45 | return; |
| 46 | |
| 47 | while (1) { |
| 48 | memset(line, 0, sizeof(line)); |
| 49 | ptr = line; |
| 50 | consumed_line = 0; |
| 51 | do { |
| 52 | if (*message != '\n' && *message != '\r') { |
| 53 | *ptr = *message; |
| 54 | ptr++; |
| 55 | } |
| 56 | message++; |
| 57 | consumed++; |
| 58 | consumed_line++; |
| 59 | } while (*message != '\n' && consumed < len |
| 60 | && consumed_line < sizeof(line)); |
| 61 | |
| 62 | if (strlen((const char *)line)) { |
| 63 | LOGP(DLMGCP, LOGL_DEBUG, "%s: line #%02u: %s\n", |
| 64 | preamble, line_count, line); |
| 65 | line_count++; |
| 66 | } |
| 67 | |
| 68 | if (consumed >= len) |
| 69 | return; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | /*! Parse connection mode. |
| 74 | * \param[in] mode as string (recvonly, sendrecv, sendonly or loopback) |
| 75 | * \param[in] endp pointer to endpoint (only used for log output) |
| 76 | * \param[out] associated connection to be modified accordingly |
| 77 | * \returns 0 on success, -1 on error */ |
| 78 | int mgcp_parse_conn_mode(const char *mode, struct mgcp_endpoint *endp, |
| 79 | struct mgcp_conn *conn) |
| 80 | { |
| 81 | int ret = 0; |
| 82 | |
| 83 | if (!mode) { |
| 84 | LOGP(DLMGCP, LOGL_ERROR, |
Philipp Maier | 230e4fc | 2017-11-28 09:38:45 +0100 | [diff] [blame] | 85 | "endpoint:0x%x missing connection mode\n", |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 86 | ENDPOINT_NUMBER(endp)); |
| 87 | return -1; |
| 88 | } |
| 89 | if (!conn) |
| 90 | return -1; |
| 91 | if (!endp) |
| 92 | return -1; |
| 93 | |
| 94 | if (strcmp(mode, "recvonly") == 0) |
| 95 | conn->mode = MGCP_CONN_RECV_ONLY; |
| 96 | else if (strcmp(mode, "sendrecv") == 0) |
| 97 | conn->mode = MGCP_CONN_RECV_SEND; |
| 98 | else if (strcmp(mode, "sendonly") == 0) |
| 99 | conn->mode = MGCP_CONN_SEND_ONLY; |
| 100 | else if (strcmp(mode, "loopback") == 0) |
| 101 | conn->mode = MGCP_CONN_LOOPBACK; |
| 102 | else { |
| 103 | LOGP(DLMGCP, LOGL_ERROR, |
Philipp Maier | 230e4fc | 2017-11-28 09:38:45 +0100 | [diff] [blame] | 104 | "endpoint:0x%x unknown connection mode: '%s'\n", |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 105 | ENDPOINT_NUMBER(endp), mode); |
| 106 | ret = -1; |
| 107 | } |
| 108 | |
| 109 | /* Special handling für RTP connections */ |
| 110 | if (conn->type == MGCP_CONN_TYPE_RTP) { |
| 111 | conn->u.rtp.end.output_enabled = |
| 112 | conn->mode & MGCP_CONN_SEND_ONLY ? 1 : 0; |
| 113 | } |
| 114 | |
| 115 | LOGP(DLMGCP, LOGL_DEBUG, |
Philipp Maier | 230e4fc | 2017-11-28 09:38:45 +0100 | [diff] [blame] | 116 | "endpoint:0x%x conn:%s\n", |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 117 | ENDPOINT_NUMBER(endp), mgcp_conn_dump(conn)); |
| 118 | |
| 119 | LOGP(DLMGCP, LOGL_DEBUG, |
Philipp Maier | 230e4fc | 2017-11-28 09:38:45 +0100 | [diff] [blame] | 120 | "endpoint:0x%x connection mode '%s' %d\n", |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 121 | ENDPOINT_NUMBER(endp), mode, conn->mode); |
| 122 | |
| 123 | /* Special handling für RTP connections */ |
| 124 | if (conn->type == MGCP_CONN_TYPE_RTP) { |
Philipp Maier | 230e4fc | 2017-11-28 09:38:45 +0100 | [diff] [blame] | 125 | LOGP(DLMGCP, LOGL_DEBUG, "endpoint:0x%x output_enabled %d\n", |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 126 | ENDPOINT_NUMBER(endp), conn->u.rtp.end.output_enabled); |
| 127 | } |
| 128 | |
| 129 | /* The VTY might change the connection mode at any time, so we have |
| 130 | * to hold a copy of the original connection mode */ |
| 131 | conn->mode_orig = conn->mode; |
| 132 | |
| 133 | return ret; |
| 134 | } |
| 135 | |
| 136 | /* We have a null terminated string with the endpoint name here. We only |
| 137 | * support two kinds. Simple ones as seen on the BSC level and the ones |
| 138 | * seen on the trunk side. (helper function for find_endpoint()) */ |
| 139 | static struct mgcp_endpoint *find_e1_endpoint(struct mgcp_config *cfg, |
| 140 | const char *mgcp) |
| 141 | { |
| 142 | char *rest = NULL; |
| 143 | struct mgcp_trunk_config *tcfg; |
| 144 | int trunk, endp; |
| 145 | |
| 146 | trunk = strtoul(mgcp + 6, &rest, 10); |
| 147 | if (rest == NULL || rest[0] != '/' || trunk < 1) { |
| 148 | LOGP(DLMGCP, LOGL_ERROR, "Wrong trunk name '%s'\n", mgcp); |
| 149 | return NULL; |
| 150 | } |
| 151 | |
| 152 | endp = strtoul(rest + 1, &rest, 10); |
| 153 | if (rest == NULL || rest[0] != '@') { |
| 154 | LOGP(DLMGCP, LOGL_ERROR, "Wrong endpoint name '%s'\n", mgcp); |
| 155 | return NULL; |
| 156 | } |
| 157 | |
| 158 | /* signalling is on timeslot 1 */ |
| 159 | if (endp == 1) |
| 160 | return NULL; |
| 161 | |
| 162 | tcfg = mgcp_trunk_num(cfg, trunk); |
| 163 | if (!tcfg) { |
| 164 | LOGP(DLMGCP, LOGL_ERROR, "The trunk %d is not declared.\n", |
| 165 | trunk); |
| 166 | return NULL; |
| 167 | } |
| 168 | |
| 169 | if (!tcfg->endpoints) { |
| 170 | LOGP(DLMGCP, LOGL_ERROR, |
| 171 | "Endpoints of trunk %d not allocated.\n", trunk); |
| 172 | return NULL; |
| 173 | } |
| 174 | |
| 175 | if (endp < 1 || endp >= tcfg->number_endpoints) { |
| 176 | LOGP(DLMGCP, LOGL_ERROR, "Failed to find endpoint '%s'\n", |
| 177 | mgcp); |
| 178 | return NULL; |
| 179 | } |
| 180 | |
| 181 | return &tcfg->endpoints[endp]; |
| 182 | } |
| 183 | |
Philipp Maier | 55295f7 | 2018-01-15 14:00:28 +0100 | [diff] [blame] | 184 | /* Find an endpoint that is not in use. Do this by going through the endpoint |
| 185 | * array, check the callid. A callid nullpointer indicates that the endpoint |
| 186 | * is free */ |
| 187 | static struct mgcp_endpoint *find_free_endpoint(struct mgcp_endpoint *endpoints, |
| 188 | unsigned int number_endpoints) |
| 189 | { |
| 190 | struct mgcp_endpoint *endp; |
| 191 | unsigned int i; |
| 192 | |
| 193 | for (i = 0; i < number_endpoints; i++) { |
| 194 | if (endpoints[i].callid == NULL) { |
| 195 | endp = &endpoints[i]; |
| 196 | LOGP(DLMGCP, LOGL_DEBUG, |
| 197 | "endpoint:0x%x found free endpoint\n", |
| 198 | ENDPOINT_NUMBER(endp)); |
| 199 | endp->wildcarded_crcx = true; |
| 200 | return endp; |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | LOGP(DLMGCP, LOGL_ERROR, "Not able to find a free endpoint"); |
| 205 | return NULL; |
| 206 | } |
| 207 | |
Philipp Maier | 12943ea | 2018-01-17 15:40:25 +0100 | [diff] [blame] | 208 | /* Check if the domain name, which is supplied with the endpoint name |
| 209 | * matches the configuration. */ |
| 210 | static int check_domain_name(struct mgcp_config *cfg, const char *mgcp) |
| 211 | { |
| 212 | char *domain_to_check; |
| 213 | |
| 214 | domain_to_check = strstr(mgcp, "@"); |
| 215 | if (!domain_to_check) |
| 216 | return -EINVAL; |
| 217 | |
| 218 | if (strcmp(domain_to_check+1, cfg->domain) != 0) |
| 219 | return -EINVAL; |
| 220 | |
| 221 | return 0; |
| 222 | } |
| 223 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 224 | /* Search the endpoint pool for the endpoint that had been selected via the |
| 225 | * MGCP message (helper function for mgcp_analyze_header()) */ |
| 226 | static struct mgcp_endpoint *find_endpoint(struct mgcp_config *cfg, |
Philipp Maier | a49e32a | 2018-02-01 18:18:50 +0100 | [diff] [blame] | 227 | const char *mgcp, |
| 228 | int *cause) |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 229 | { |
| 230 | char *endptr = NULL; |
| 231 | unsigned int gw = INT_MAX; |
Philipp Maier | 7f0966c | 2018-01-17 18:18:12 +0100 | [diff] [blame] | 232 | const char *endpoint_number_str; |
Philipp Maier | a49e32a | 2018-02-01 18:18:50 +0100 | [diff] [blame] | 233 | struct mgcp_endpoint *endp; |
| 234 | |
| 235 | *cause = 0; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 236 | |
Philipp Maier | 7f0966c | 2018-01-17 18:18:12 +0100 | [diff] [blame] | 237 | /* Check if the domainname in the request is correct */ |
Philipp Maier | 12943ea | 2018-01-17 15:40:25 +0100 | [diff] [blame] | 238 | if (check_domain_name(cfg, mgcp)) { |
| 239 | LOGP(DLMGCP, LOGL_ERROR, "Wrong domain name '%s'\n", mgcp); |
| 240 | return NULL; |
| 241 | } |
| 242 | |
Philipp Maier | 7f0966c | 2018-01-17 18:18:12 +0100 | [diff] [blame] | 243 | /* Check if the E1 trunk is requested */ |
Philipp Maier | a49e32a | 2018-02-01 18:18:50 +0100 | [diff] [blame] | 244 | if (strncmp(mgcp, "ds/e1", 5) == 0) { |
| 245 | endp = find_e1_endpoint(cfg, mgcp); |
| 246 | if (!endp) |
| 247 | *cause = -500; |
| 248 | return endp; |
| 249 | } |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 250 | |
Philipp Maier | 7f0966c | 2018-01-17 18:18:12 +0100 | [diff] [blame] | 251 | /* Check if the virtual trunk is addressed (new, correct way with prefix) */ |
| 252 | if (strncmp |
| 253 | (mgcp, MGCP_ENDPOINT_PREFIX_VIRTUAL_TRUNK, |
| 254 | strlen(MGCP_ENDPOINT_PREFIX_VIRTUAL_TRUNK)) == 0) { |
| 255 | endpoint_number_str = |
| 256 | mgcp + strlen(MGCP_ENDPOINT_PREFIX_VIRTUAL_TRUNK); |
| 257 | if (endpoint_number_str[0] == '*') { |
Philipp Maier | a49e32a | 2018-02-01 18:18:50 +0100 | [diff] [blame] | 258 | endp = find_free_endpoint(cfg->trunk.endpoints, |
Philipp Maier | 7f0966c | 2018-01-17 18:18:12 +0100 | [diff] [blame] | 259 | cfg->trunk.number_endpoints); |
Philipp Maier | a49e32a | 2018-02-01 18:18:50 +0100 | [diff] [blame] | 260 | if (!endp) |
| 261 | *cause = -403; |
| 262 | return endp; |
Philipp Maier | 7f0966c | 2018-01-17 18:18:12 +0100 | [diff] [blame] | 263 | } |
Philipp Maier | 7f0966c | 2018-01-17 18:18:12 +0100 | [diff] [blame] | 264 | gw = strtoul(endpoint_number_str, &endptr, 16); |
| 265 | if (gw < cfg->trunk.number_endpoints && endptr[0] == '@') |
| 266 | return &cfg->trunk.endpoints[gw]; |
Philipp Maier | 55295f7 | 2018-01-15 14:00:28 +0100 | [diff] [blame] | 267 | } |
| 268 | |
Philipp Maier | 7f0966c | 2018-01-17 18:18:12 +0100 | [diff] [blame] | 269 | /* Deprecated method without prefix */ |
| 270 | LOGP(DLMGCP, LOGL_NOTICE, |
| 271 | "Addressing virtual trunk without prefix (deprecated), please use %s: '%s'\n", |
| 272 | MGCP_ENDPOINT_PREFIX_VIRTUAL_TRUNK, mgcp); |
Harald Welte | 33eafe0 | 2017-12-28 03:15:27 +0100 | [diff] [blame] | 273 | gw = strtoul(mgcp, &endptr, 16); |
Philipp Maier | 03cc484 | 2018-01-17 16:59:38 +0100 | [diff] [blame] | 274 | if (gw < cfg->trunk.number_endpoints && endptr[0] == '@') |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 275 | return &cfg->trunk.endpoints[gw]; |
| 276 | |
| 277 | LOGP(DLMGCP, LOGL_ERROR, "Not able to find the endpoint: '%s'\n", mgcp); |
Philipp Maier | a49e32a | 2018-02-01 18:18:50 +0100 | [diff] [blame] | 278 | *cause = -500; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 279 | return NULL; |
| 280 | } |
| 281 | |
| 282 | /*! Analyze and parse the the hader of an MGCP messeage string. |
| 283 | * \param[out] pdata caller provided memory to store the parsing results |
| 284 | * \param[in] data mgcp message string |
| 285 | * \returns when the status line was complete and transaction_id and |
| 286 | * endp out parameters are set, -1 on error */ |
| 287 | int mgcp_parse_header(struct mgcp_parse_data *pdata, char *data) |
| 288 | { |
| 289 | int i = 0; |
| 290 | char *elem, *save = NULL; |
Philipp Maier | a49e32a | 2018-02-01 18:18:50 +0100 | [diff] [blame] | 291 | int cause; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 292 | |
| 293 | /*! This function will parse the header part of the received |
| 294 | * MGCP message. The parsing results are stored in pdata. |
| 295 | * The function will also automatically search the pool with |
| 296 | * available endpoints in order to find an endpoint that matches |
| 297 | * the endpoint string in in the header */ |
| 298 | |
| 299 | OSMO_ASSERT(data); |
| 300 | pdata->trans = "000000"; |
| 301 | |
| 302 | for (elem = strtok_r(data, " ", &save); elem; |
| 303 | elem = strtok_r(NULL, " ", &save)) { |
| 304 | switch (i) { |
| 305 | case 0: |
| 306 | pdata->trans = elem; |
| 307 | break; |
| 308 | case 1: |
Philipp Maier | a49e32a | 2018-02-01 18:18:50 +0100 | [diff] [blame] | 309 | pdata->endp = find_endpoint(pdata->cfg, elem, &cause); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 310 | if (!pdata->endp) { |
| 311 | LOGP(DLMGCP, LOGL_ERROR, |
| 312 | "Unable to find Endpoint `%s'\n", elem); |
Philipp Maier | a49e32a | 2018-02-01 18:18:50 +0100 | [diff] [blame] | 313 | return cause; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 314 | } |
| 315 | break; |
| 316 | case 2: |
| 317 | if (strcmp("MGCP", elem)) { |
| 318 | LOGP(DLMGCP, LOGL_ERROR, |
| 319 | "MGCP header parsing error\n"); |
Harald Welte | abbb6b9 | 2017-12-28 13:13:50 +0100 | [diff] [blame] | 320 | return -510; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 321 | } |
| 322 | break; |
| 323 | case 3: |
| 324 | if (strcmp("1.0", elem)) { |
| 325 | LOGP(DLMGCP, LOGL_ERROR, "MGCP version `%s' " |
| 326 | "not supported\n", elem); |
Harald Welte | abbb6b9 | 2017-12-28 13:13:50 +0100 | [diff] [blame] | 327 | return -528; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 328 | } |
| 329 | break; |
| 330 | } |
| 331 | i++; |
| 332 | } |
| 333 | |
| 334 | if (i != 4) { |
| 335 | LOGP(DLMGCP, LOGL_ERROR, "MGCP status line too short.\n"); |
| 336 | pdata->trans = "000000"; |
| 337 | pdata->endp = NULL; |
Harald Welte | abbb6b9 | 2017-12-28 13:13:50 +0100 | [diff] [blame] | 338 | return -510; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | return 0; |
| 342 | } |
| 343 | |
| 344 | /*! Extract OSMUX CID from an MGCP parameter line (string). |
| 345 | * \param[in] line single parameter line from the MGCP message |
| 346 | * \returns OSMUX CID, -1 on error */ |
| 347 | int mgcp_parse_osmux_cid(const char *line) |
| 348 | { |
| 349 | int osmux_cid; |
| 350 | |
| 351 | if (sscanf(line + 2, "Osmux: %u", &osmux_cid) != 1) |
| 352 | return -1; |
| 353 | |
| 354 | if (osmux_cid > OSMUX_CID_MAX) { |
| 355 | LOGP(DLMGCP, LOGL_ERROR, "Osmux ID too large: %u > %u\n", |
| 356 | osmux_cid, OSMUX_CID_MAX); |
| 357 | return -1; |
| 358 | } |
| 359 | LOGP(DLMGCP, LOGL_DEBUG, "bsc-nat offered Osmux CID %u\n", osmux_cid); |
| 360 | |
| 361 | return osmux_cid; |
| 362 | } |
| 363 | |
| 364 | /*! Check MGCP parameter line (string) for plausibility. |
| 365 | * \param[in] endp pointer to endpoint (only used for log output) |
| 366 | * \param[in] line single parameter line from the MGCP message |
| 367 | * \returns 1 when line seems plausible, 0 on error */ |
| 368 | int mgcp_check_param(const struct mgcp_endpoint *endp, const char *line) |
| 369 | { |
| 370 | const size_t line_len = strlen(line); |
| 371 | if (line[0] != '\0' && line_len < 2) { |
| 372 | LOGP(DLMGCP, LOGL_ERROR, |
| 373 | "Wrong MGCP option format: '%s' on 0x%x\n", |
| 374 | line, ENDPOINT_NUMBER(endp)); |
| 375 | return 0; |
| 376 | } |
| 377 | |
| 378 | /* FIXME: A couple more checks wouldn't hurt... */ |
| 379 | |
| 380 | return 1; |
| 381 | } |
| 382 | |
| 383 | /*! Check if the specified callid seems plausible. |
| 384 | * \param[in] endp pointer to endpoint |
| 385 | * \param{in] callid to verify |
| 386 | * \returns 1 when callid seems plausible, 0 on error */ |
| 387 | int mgcp_verify_call_id(struct mgcp_endpoint *endp, const char *callid) |
| 388 | { |
| 389 | /*! This function compares the supplied callid with the called that is |
| 390 | * stored in the endpoint structure. */ |
| 391 | |
| 392 | if (!endp) |
| 393 | return -1; |
| 394 | if (!callid) |
| 395 | return -1; |
| 396 | if (!endp->callid) |
| 397 | return -1; |
| 398 | |
| 399 | if (strcmp(endp->callid, callid) != 0) { |
| 400 | LOGP(DLMGCP, LOGL_ERROR, |
Philipp Maier | 230e4fc | 2017-11-28 09:38:45 +0100 | [diff] [blame] | 401 | "endpoint:0x%x CallIDs does not match '%s' != '%s'\n", |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 402 | ENDPOINT_NUMBER(endp), endp->callid, callid); |
| 403 | return -1; |
| 404 | } |
| 405 | |
| 406 | return 0; |
| 407 | } |
| 408 | |
| 409 | /*! Check if the specified connection id seems plausible. |
| 410 | * \param[in] endp pointer to endpoint |
| 411 | * \param{in] connection id to verify |
| 412 | * \returns 1 when connection id seems plausible, 0 on error */ |
Philipp Maier | 01d24a3 | 2017-11-21 17:26:09 +0100 | [diff] [blame] | 413 | int mgcp_verify_ci(struct mgcp_endpoint *endp, const char *conn_id) |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 414 | { |
Philipp Maier | 01d24a3 | 2017-11-21 17:26:09 +0100 | [diff] [blame] | 415 | /* Check for null identifiers */ |
| 416 | if (!conn_id) { |
| 417 | LOGP(DLMGCP, LOGL_ERROR, |
Philipp Maier | 230e4fc | 2017-11-28 09:38:45 +0100 | [diff] [blame] | 418 | "endpoint:0x%x invalid ConnectionIdentifier (missing)\n", |
Philipp Maier | 01d24a3 | 2017-11-21 17:26:09 +0100 | [diff] [blame] | 419 | ENDPOINT_NUMBER(endp)); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 420 | return -1; |
Philipp Maier | 01d24a3 | 2017-11-21 17:26:09 +0100 | [diff] [blame] | 421 | } |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 422 | |
Philipp Maier | 01d24a3 | 2017-11-21 17:26:09 +0100 | [diff] [blame] | 423 | /* Check for empty connection identifiers */ |
| 424 | if (strlen(conn_id) == 0) { |
| 425 | LOGP(DLMGCP, LOGL_ERROR, |
Philipp Maier | 230e4fc | 2017-11-28 09:38:45 +0100 | [diff] [blame] | 426 | "endpoint:0x%x invalid ConnectionIdentifier (empty)\n", |
Philipp Maier | 01d24a3 | 2017-11-21 17:26:09 +0100 | [diff] [blame] | 427 | ENDPOINT_NUMBER(endp)); |
| 428 | return -1; |
| 429 | } |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 430 | |
Philipp Maier | 01d24a3 | 2017-11-21 17:26:09 +0100 | [diff] [blame] | 431 | /* Check for over long connection identifiers */ |
| 432 | if (strlen(conn_id) > MGCP_CONN_ID_LENGTH) { |
| 433 | LOGP(DLMGCP, LOGL_ERROR, |
Philipp Maier | 230e4fc | 2017-11-28 09:38:45 +0100 | [diff] [blame] | 434 | "endpoint:0x%x invalid ConnectionIdentifier (too long) 0x%s\n", |
Philipp Maier | 01d24a3 | 2017-11-21 17:26:09 +0100 | [diff] [blame] | 435 | ENDPOINT_NUMBER(endp), conn_id); |
| 436 | return -1; |
| 437 | } |
| 438 | |
| 439 | /* Check if connection exists */ |
| 440 | if (mgcp_conn_get(endp, conn_id)) |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 441 | return 0; |
| 442 | |
| 443 | LOGP(DLMGCP, LOGL_ERROR, |
Philipp Maier | 230e4fc | 2017-11-28 09:38:45 +0100 | [diff] [blame] | 444 | "endpoint:0x%x no connection found under ConnectionIdentifier 0x%s\n", |
Philipp Maier | 01d24a3 | 2017-11-21 17:26:09 +0100 | [diff] [blame] | 445 | ENDPOINT_NUMBER(endp), conn_id); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 446 | |
| 447 | return -1; |
| 448 | } |
| 449 | |
| 450 | /*! Extract individual lines from MCGP message. |
| 451 | * \param[in] str MGCP message string, consisting of multiple lines |
| 452 | * \param{in] saveptr pointer to next line in str |
| 453 | * \returns line, NULL when done */ |
| 454 | char *mgcp_strline(char *str, char **saveptr) |
| 455 | { |
| 456 | char *result; |
| 457 | |
| 458 | /*! The function must be called with *str set to the input string |
| 459 | * for the first line. After that saveptr will be initalized. |
| 460 | * all consecutive lines are extracted by calling the function |
| 461 | * with str set to NULL. When done, the function will return NULL |
| 462 | * to indicate that all lines have been parsed. */ |
| 463 | |
| 464 | if (str) |
| 465 | *saveptr = str; |
| 466 | |
| 467 | result = *saveptr; |
| 468 | |
| 469 | if (*saveptr != NULL) { |
| 470 | *saveptr = strpbrk(*saveptr, "\r\n"); |
| 471 | |
| 472 | if (*saveptr != NULL) { |
| 473 | char *eos = *saveptr; |
| 474 | |
| 475 | if ((*saveptr)[0] == '\r' && (*saveptr)[1] == '\n') |
| 476 | (*saveptr)++; |
| 477 | (*saveptr)++; |
| 478 | if ((*saveptr)[0] == '\0') |
| 479 | *saveptr = NULL; |
| 480 | |
| 481 | *eos = '\0'; |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | return result; |
| 486 | } |