blob: 5844d41c30d8a78169c0da29627dcc147440f4a5 [file] [log] [blame]
Philipp Maier87bd9be2017-08-22 16:35:41 +02001/* 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 Hofmeyr67793542017-09-08 04:25:16 +020028#include <osmocom/mgcp/mgcp_common.h>
Philipp Maier87bd9be2017-08-22 16:35:41 +020029#include <osmocom/mgcp/mgcp_msg.h>
30#include <osmocom/mgcp/mgcp_conn.h>
Philipp Maier37d11c82018-02-01 14:38:12 +010031#include <osmocom/mgcp/mgcp_endp.h>
Philipp Maier87bd9be2017-08-22 16:35:41 +020032
33/*! Display an mgcp message on the log output.
34 * \param[in] message mgcp message string
35 * \param[in] len message mgcp message string length
36 * \param[in] preamble string to display in logtext in front of each line */
37void mgcp_disp_msg(unsigned char *message, unsigned int len, char *preamble)
38{
39 unsigned char line[80];
40 unsigned char *ptr;
41 unsigned int consumed = 0;
42 unsigned int consumed_line = 0;
43 unsigned int line_count = 0;
44
45 if (!log_check_level(DLMGCP, LOGL_DEBUG))
46 return;
47
48 while (1) {
49 memset(line, 0, sizeof(line));
50 ptr = line;
51 consumed_line = 0;
52 do {
53 if (*message != '\n' && *message != '\r') {
54 *ptr = *message;
55 ptr++;
56 }
57 message++;
58 consumed++;
59 consumed_line++;
60 } while (*message != '\n' && consumed < len
61 && consumed_line < sizeof(line));
62
63 if (strlen((const char *)line)) {
64 LOGP(DLMGCP, LOGL_DEBUG, "%s: line #%02u: %s\n",
65 preamble, line_count, line);
66 line_count++;
67 }
68
69 if (consumed >= len)
70 return;
71 }
72}
73
74/*! Parse connection mode.
75 * \param[in] mode as string (recvonly, sendrecv, sendonly or loopback)
76 * \param[in] endp pointer to endpoint (only used for log output)
77 * \param[out] associated connection to be modified accordingly
78 * \returns 0 on success, -1 on error */
79int mgcp_parse_conn_mode(const char *mode, struct mgcp_endpoint *endp,
80 struct mgcp_conn *conn)
81{
82 int ret = 0;
83
84 if (!mode) {
85 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +010086 "endpoint:0x%x missing connection mode\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +020087 ENDPOINT_NUMBER(endp));
88 return -1;
89 }
90 if (!conn)
91 return -1;
92 if (!endp)
93 return -1;
94
95 if (strcmp(mode, "recvonly") == 0)
96 conn->mode = MGCP_CONN_RECV_ONLY;
97 else if (strcmp(mode, "sendrecv") == 0)
98 conn->mode = MGCP_CONN_RECV_SEND;
99 else if (strcmp(mode, "sendonly") == 0)
100 conn->mode = MGCP_CONN_SEND_ONLY;
101 else if (strcmp(mode, "loopback") == 0)
102 conn->mode = MGCP_CONN_LOOPBACK;
103 else {
104 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100105 "endpoint:0x%x unknown connection mode: '%s'\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200106 ENDPOINT_NUMBER(endp), mode);
107 ret = -1;
108 }
109
110 /* Special handling für RTP connections */
111 if (conn->type == MGCP_CONN_TYPE_RTP) {
112 conn->u.rtp.end.output_enabled =
113 conn->mode & MGCP_CONN_SEND_ONLY ? 1 : 0;
114 }
115
116 LOGP(DLMGCP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100117 "endpoint:0x%x conn:%s\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200118 ENDPOINT_NUMBER(endp), mgcp_conn_dump(conn));
119
120 LOGP(DLMGCP, LOGL_DEBUG,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100121 "endpoint:0x%x connection mode '%s' %d\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200122 ENDPOINT_NUMBER(endp), mode, conn->mode);
123
124 /* Special handling für RTP connections */
125 if (conn->type == MGCP_CONN_TYPE_RTP) {
Philipp Maier230e4fc2017-11-28 09:38:45 +0100126 LOGP(DLMGCP, LOGL_DEBUG, "endpoint:0x%x output_enabled %d\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200127 ENDPOINT_NUMBER(endp), conn->u.rtp.end.output_enabled);
128 }
129
130 /* The VTY might change the connection mode at any time, so we have
131 * to hold a copy of the original connection mode */
132 conn->mode_orig = conn->mode;
133
134 return ret;
135}
136
137/* We have a null terminated string with the endpoint name here. We only
138 * support two kinds. Simple ones as seen on the BSC level and the ones
139 * seen on the trunk side. (helper function for find_endpoint()) */
140static struct mgcp_endpoint *find_e1_endpoint(struct mgcp_config *cfg,
141 const char *mgcp)
142{
143 char *rest = NULL;
144 struct mgcp_trunk_config *tcfg;
145 int trunk, endp;
Philipp Maier207ab512018-02-02 14:19:26 +0100146 struct mgcp_endpoint *endp_ptr;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200147
148 trunk = strtoul(mgcp + 6, &rest, 10);
149 if (rest == NULL || rest[0] != '/' || trunk < 1) {
150 LOGP(DLMGCP, LOGL_ERROR, "Wrong trunk name '%s'\n", mgcp);
151 return NULL;
152 }
153
154 endp = strtoul(rest + 1, &rest, 10);
155 if (rest == NULL || rest[0] != '@') {
156 LOGP(DLMGCP, LOGL_ERROR, "Wrong endpoint name '%s'\n", mgcp);
157 return NULL;
158 }
159
160 /* signalling is on timeslot 1 */
161 if (endp == 1)
162 return NULL;
163
164 tcfg = mgcp_trunk_num(cfg, trunk);
165 if (!tcfg) {
166 LOGP(DLMGCP, LOGL_ERROR, "The trunk %d is not declared.\n",
167 trunk);
168 return NULL;
169 }
170
171 if (!tcfg->endpoints) {
172 LOGP(DLMGCP, LOGL_ERROR,
173 "Endpoints of trunk %d not allocated.\n", trunk);
174 return NULL;
175 }
176
177 if (endp < 1 || endp >= tcfg->number_endpoints) {
178 LOGP(DLMGCP, LOGL_ERROR, "Failed to find endpoint '%s'\n",
179 mgcp);
180 return NULL;
181 }
182
Philipp Maier207ab512018-02-02 14:19:26 +0100183 endp_ptr = &tcfg->endpoints[endp];
184 endp_ptr->wildcarded_req = false;
185 return endp_ptr;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200186}
187
Philipp Maier55295f72018-01-15 14:00:28 +0100188/* Find an endpoint that is not in use. Do this by going through the endpoint
189 * array, check the callid. A callid nullpointer indicates that the endpoint
190 * is free */
191static struct mgcp_endpoint *find_free_endpoint(struct mgcp_endpoint *endpoints,
192 unsigned int number_endpoints)
193{
194 struct mgcp_endpoint *endp;
195 unsigned int i;
196
197 for (i = 0; i < number_endpoints; i++) {
198 if (endpoints[i].callid == NULL) {
199 endp = &endpoints[i];
200 LOGP(DLMGCP, LOGL_DEBUG,
201 "endpoint:0x%x found free endpoint\n",
202 ENDPOINT_NUMBER(endp));
Philipp Maier207ab512018-02-02 14:19:26 +0100203 endp->wildcarded_req = true;
Philipp Maier55295f72018-01-15 14:00:28 +0100204 return endp;
205 }
206 }
207
Philipp Maierb911b872018-02-02 11:48:16 +0100208 LOGP(DLMGCP, LOGL_ERROR, "Not able to find a free endpoint\n");
Philipp Maier55295f72018-01-15 14:00:28 +0100209 return NULL;
210}
211
Philipp Maier12943ea2018-01-17 15:40:25 +0100212/* Check if the domain name, which is supplied with the endpoint name
213 * matches the configuration. */
214static int check_domain_name(struct mgcp_config *cfg, const char *mgcp)
215{
216 char *domain_to_check;
217
218 domain_to_check = strstr(mgcp, "@");
219 if (!domain_to_check)
220 return -EINVAL;
221
Neels Hofmeyr352eed02018-08-20 23:59:32 +0200222 /* Accept any domain if configured as "*" */
223 if (!strcmp(cfg->domain, "*"))
224 return 0;
225
Neels Hofmeyr96c31072018-12-19 00:31:48 +0100226 if (strcmp(domain_to_check+1, cfg->domain) != 0) {
227 LOGP(DLMGCP, LOGL_ERROR, "Wrong domain name '%s', expecting '%s'\n", mgcp, cfg->domain);
Philipp Maier12943ea2018-01-17 15:40:25 +0100228 return -EINVAL;
Neels Hofmeyr96c31072018-12-19 00:31:48 +0100229 }
Philipp Maier12943ea2018-01-17 15:40:25 +0100230
231 return 0;
232}
233
Philipp Maier87bd9be2017-08-22 16:35:41 +0200234/* Search the endpoint pool for the endpoint that had been selected via the
235 * MGCP message (helper function for mgcp_analyze_header()) */
236static struct mgcp_endpoint *find_endpoint(struct mgcp_config *cfg,
Philipp Maiera49e32a2018-02-01 18:18:50 +0100237 const char *mgcp,
238 int *cause)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200239{
240 char *endptr = NULL;
241 unsigned int gw = INT_MAX;
Philipp Maier7f0966c2018-01-17 18:18:12 +0100242 const char *endpoint_number_str;
Philipp Maiera49e32a2018-02-01 18:18:50 +0100243 struct mgcp_endpoint *endp;
244
245 *cause = 0;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200246
Philipp Maier7f0966c2018-01-17 18:18:12 +0100247 /* Check if the domainname in the request is correct */
Philipp Maier12943ea2018-01-17 15:40:25 +0100248 if (check_domain_name(cfg, mgcp)) {
Neels Hofmeyr0a89e922018-08-20 22:39:53 +0200249 *cause = -500;
Philipp Maier12943ea2018-01-17 15:40:25 +0100250 return NULL;
251 }
252
Philipp Maier7f0966c2018-01-17 18:18:12 +0100253 /* Check if the E1 trunk is requested */
Philipp Maiera49e32a2018-02-01 18:18:50 +0100254 if (strncmp(mgcp, "ds/e1", 5) == 0) {
255 endp = find_e1_endpoint(cfg, mgcp);
256 if (!endp)
257 *cause = -500;
258 return endp;
259 }
Philipp Maier87bd9be2017-08-22 16:35:41 +0200260
Philipp Maier7f0966c2018-01-17 18:18:12 +0100261 /* Check if the virtual trunk is addressed (new, correct way with prefix) */
262 if (strncmp
263 (mgcp, MGCP_ENDPOINT_PREFIX_VIRTUAL_TRUNK,
264 strlen(MGCP_ENDPOINT_PREFIX_VIRTUAL_TRUNK)) == 0) {
265 endpoint_number_str =
266 mgcp + strlen(MGCP_ENDPOINT_PREFIX_VIRTUAL_TRUNK);
267 if (endpoint_number_str[0] == '*') {
Philipp Maiera49e32a2018-02-01 18:18:50 +0100268 endp = find_free_endpoint(cfg->trunk.endpoints,
Philipp Maier7f0966c2018-01-17 18:18:12 +0100269 cfg->trunk.number_endpoints);
Philipp Maiera49e32a2018-02-01 18:18:50 +0100270 if (!endp)
271 *cause = -403;
272 return endp;
Philipp Maier7f0966c2018-01-17 18:18:12 +0100273 }
Philipp Maier7f0966c2018-01-17 18:18:12 +0100274 gw = strtoul(endpoint_number_str, &endptr, 16);
Philipp Maier207ab512018-02-02 14:19:26 +0100275 if (gw < cfg->trunk.number_endpoints && endptr[0] == '@') {
276 endp = &cfg->trunk.endpoints[gw];
277 endp->wildcarded_req = false;
278 return endp;
279 }
Philipp Maier55295f72018-01-15 14:00:28 +0100280 }
281
Philipp Maier7f0966c2018-01-17 18:18:12 +0100282 /* Deprecated method without prefix */
283 LOGP(DLMGCP, LOGL_NOTICE,
284 "Addressing virtual trunk without prefix (deprecated), please use %s: '%s'\n",
285 MGCP_ENDPOINT_PREFIX_VIRTUAL_TRUNK, mgcp);
Harald Welte33eafe02017-12-28 03:15:27 +0100286 gw = strtoul(mgcp, &endptr, 16);
Philipp Maier207ab512018-02-02 14:19:26 +0100287 if (gw < cfg->trunk.number_endpoints && endptr[0] == '@') {
288 endp = &cfg->trunk.endpoints[gw];
289 endp->wildcarded_req = false;
290 return endp;
291 }
Philipp Maier87bd9be2017-08-22 16:35:41 +0200292
293 LOGP(DLMGCP, LOGL_ERROR, "Not able to find the endpoint: '%s'\n", mgcp);
Philipp Maiera49e32a2018-02-01 18:18:50 +0100294 *cause = -500;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200295 return NULL;
296}
297
298/*! Analyze and parse the the hader of an MGCP messeage string.
299 * \param[out] pdata caller provided memory to store the parsing results
300 * \param[in] data mgcp message string
301 * \returns when the status line was complete and transaction_id and
302 * endp out parameters are set, -1 on error */
303int mgcp_parse_header(struct mgcp_parse_data *pdata, char *data)
304{
305 int i = 0;
306 char *elem, *save = NULL;
Philipp Maiera49e32a2018-02-01 18:18:50 +0100307 int cause;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200308
309 /*! This function will parse the header part of the received
310 * MGCP message. The parsing results are stored in pdata.
311 * The function will also automatically search the pool with
312 * available endpoints in order to find an endpoint that matches
313 * the endpoint string in in the header */
314
315 OSMO_ASSERT(data);
316 pdata->trans = "000000";
317
318 for (elem = strtok_r(data, " ", &save); elem;
319 elem = strtok_r(NULL, " ", &save)) {
320 switch (i) {
321 case 0:
322 pdata->trans = elem;
323 break;
324 case 1:
Philipp Maiera49e32a2018-02-01 18:18:50 +0100325 pdata->endp = find_endpoint(pdata->cfg, elem, &cause);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200326 if (!pdata->endp) {
327 LOGP(DLMGCP, LOGL_ERROR,
328 "Unable to find Endpoint `%s'\n", elem);
Neels Hofmeyr0a89e922018-08-20 22:39:53 +0200329 OSMO_ASSERT(cause < 0);
Philipp Maiera49e32a2018-02-01 18:18:50 +0100330 return cause;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200331 }
332 break;
333 case 2:
334 if (strcmp("MGCP", elem)) {
335 LOGP(DLMGCP, LOGL_ERROR,
336 "MGCP header parsing error\n");
Harald Welteabbb6b92017-12-28 13:13:50 +0100337 return -510;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200338 }
339 break;
340 case 3:
341 if (strcmp("1.0", elem)) {
342 LOGP(DLMGCP, LOGL_ERROR, "MGCP version `%s' "
343 "not supported\n", elem);
Harald Welteabbb6b92017-12-28 13:13:50 +0100344 return -528;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200345 }
346 break;
347 }
348 i++;
349 }
350
351 if (i != 4) {
352 LOGP(DLMGCP, LOGL_ERROR, "MGCP status line too short.\n");
353 pdata->trans = "000000";
354 pdata->endp = NULL;
Harald Welteabbb6b92017-12-28 13:13:50 +0100355 return -510;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200356 }
357
358 return 0;
359}
360
361/*! Extract OSMUX CID from an MGCP parameter line (string).
362 * \param[in] line single parameter line from the MGCP message
363 * \returns OSMUX CID, -1 on error */
364int mgcp_parse_osmux_cid(const char *line)
365{
366 int osmux_cid;
367
Pau Espin Pedrolef6304e2019-04-23 13:24:37 +0200368 if (sscanf(line + 2, "Osmux: %u", &osmux_cid) != 1) {
369 LOGP(DLMGCP, LOGL_ERROR, "Failed parsing Osmux in MGCP msg line: %s\n",
370 line);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200371 return -1;
Pau Espin Pedrolef6304e2019-04-23 13:24:37 +0200372 }
Philipp Maier87bd9be2017-08-22 16:35:41 +0200373
374 if (osmux_cid > OSMUX_CID_MAX) {
375 LOGP(DLMGCP, LOGL_ERROR, "Osmux ID too large: %u > %u\n",
376 osmux_cid, OSMUX_CID_MAX);
377 return -1;
378 }
379 LOGP(DLMGCP, LOGL_DEBUG, "bsc-nat offered Osmux CID %u\n", osmux_cid);
380
381 return osmux_cid;
382}
383
384/*! Check MGCP parameter line (string) for plausibility.
385 * \param[in] endp pointer to endpoint (only used for log output)
386 * \param[in] line single parameter line from the MGCP message
387 * \returns 1 when line seems plausible, 0 on error */
388int mgcp_check_param(const struct mgcp_endpoint *endp, const char *line)
389{
390 const size_t line_len = strlen(line);
391 if (line[0] != '\0' && line_len < 2) {
392 LOGP(DLMGCP, LOGL_ERROR,
393 "Wrong MGCP option format: '%s' on 0x%x\n",
394 line, ENDPOINT_NUMBER(endp));
395 return 0;
396 }
397
398 /* FIXME: A couple more checks wouldn't hurt... */
399
400 return 1;
401}
402
403/*! Check if the specified callid seems plausible.
404 * \param[in] endp pointer to endpoint
405 * \param{in] callid to verify
406 * \returns 1 when callid seems plausible, 0 on error */
407int mgcp_verify_call_id(struct mgcp_endpoint *endp, const char *callid)
408{
409 /*! This function compares the supplied callid with the called that is
410 * stored in the endpoint structure. */
411
412 if (!endp)
413 return -1;
Neels Hofmeyre6d8e912018-08-23 16:36:48 +0200414
415 /* Accept any CallID for "X-Osmo-IGN: C" */
416 if (endp->x_osmo_ign & MGCP_X_OSMO_IGN_CALLID)
417 return 0;
418
Philipp Maier87bd9be2017-08-22 16:35:41 +0200419 if (!callid)
420 return -1;
421 if (!endp->callid)
422 return -1;
423
424 if (strcmp(endp->callid, callid) != 0) {
425 LOGP(DLMGCP, LOGL_ERROR,
Neels Hofmeyr610fda62018-08-21 00:03:48 +0200426 "endpoint:0x%x CallIDs mismatch: '%s' != '%s'\n",
Philipp Maier87bd9be2017-08-22 16:35:41 +0200427 ENDPOINT_NUMBER(endp), endp->callid, callid);
428 return -1;
429 }
430
431 return 0;
432}
433
434/*! Check if the specified connection id seems plausible.
435 * \param[in] endp pointer to endpoint
436 * \param{in] connection id to verify
Neels Hofmeyreb72ff02018-09-03 23:00:07 +0200437 * \returns 0 when connection id is valid and exists, an RFC3435 error code on error.
Neels Hofmeyr8a91d2c2018-09-03 22:51:30 +0200438 */
Philipp Maier01d24a32017-11-21 17:26:09 +0100439int mgcp_verify_ci(struct mgcp_endpoint *endp, const char *conn_id)
Philipp Maier87bd9be2017-08-22 16:35:41 +0200440{
Neels Hofmeyreb72ff02018-09-03 23:00:07 +0200441 /* For invalid conn_ids, return 510 "The transaction could not be executed, because some
442 * unspecified protocol error was detected." */
443
Philipp Maier01d24a32017-11-21 17:26:09 +0100444 /* Check for null identifiers */
445 if (!conn_id) {
446 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100447 "endpoint:0x%x invalid ConnectionIdentifier (missing)\n",
Philipp Maier01d24a32017-11-21 17:26:09 +0100448 ENDPOINT_NUMBER(endp));
Neels Hofmeyreb72ff02018-09-03 23:00:07 +0200449 return 510;
Philipp Maier01d24a32017-11-21 17:26:09 +0100450 }
Philipp Maier87bd9be2017-08-22 16:35:41 +0200451
Philipp Maier01d24a32017-11-21 17:26:09 +0100452 /* Check for empty connection identifiers */
453 if (strlen(conn_id) == 0) {
454 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100455 "endpoint:0x%x invalid ConnectionIdentifier (empty)\n",
Philipp Maier01d24a32017-11-21 17:26:09 +0100456 ENDPOINT_NUMBER(endp));
Neels Hofmeyreb72ff02018-09-03 23:00:07 +0200457 return 510;
Philipp Maier01d24a32017-11-21 17:26:09 +0100458 }
Philipp Maier87bd9be2017-08-22 16:35:41 +0200459
Philipp Maier01d24a32017-11-21 17:26:09 +0100460 /* Check for over long connection identifiers */
Neels Hofmeyr5336f572018-09-03 22:05:48 +0200461 if (strlen(conn_id) > (MGCP_CONN_ID_MAXLEN-1)) {
Philipp Maier01d24a32017-11-21 17:26:09 +0100462 LOGP(DLMGCP, LOGL_ERROR,
Neels Hofmeyr5336f572018-09-03 22:05:48 +0200463 "endpoint:0x%x invalid ConnectionIdentifier (too long: %zu > %d) 0x%s\n",
464 ENDPOINT_NUMBER(endp), strlen(conn_id), MGCP_CONN_ID_MAXLEN-1, conn_id);
Neels Hofmeyreb72ff02018-09-03 23:00:07 +0200465 return 510;
Philipp Maier01d24a32017-11-21 17:26:09 +0100466 }
467
468 /* Check if connection exists */
469 if (mgcp_conn_get(endp, conn_id))
Philipp Maier87bd9be2017-08-22 16:35:41 +0200470 return 0;
471
472 LOGP(DLMGCP, LOGL_ERROR,
Philipp Maier230e4fc2017-11-28 09:38:45 +0100473 "endpoint:0x%x no connection found under ConnectionIdentifier 0x%s\n",
Philipp Maier01d24a32017-11-21 17:26:09 +0100474 ENDPOINT_NUMBER(endp), conn_id);
Philipp Maier87bd9be2017-08-22 16:35:41 +0200475
Neels Hofmeyreb72ff02018-09-03 23:00:07 +0200476 /* When the conn_id was not found, return error code 515 "The transaction refers to an incorrect
477 * connection-id (may have been already deleted)." */
478 return 515;
Philipp Maier87bd9be2017-08-22 16:35:41 +0200479}
480
481/*! Extract individual lines from MCGP message.
482 * \param[in] str MGCP message string, consisting of multiple lines
483 * \param{in] saveptr pointer to next line in str
484 * \returns line, NULL when done */
485char *mgcp_strline(char *str, char **saveptr)
486{
487 char *result;
488
489 /*! The function must be called with *str set to the input string
490 * for the first line. After that saveptr will be initalized.
491 * all consecutive lines are extracted by calling the function
492 * with str set to NULL. When done, the function will return NULL
493 * to indicate that all lines have been parsed. */
494
495 if (str)
496 *saveptr = str;
497
498 result = *saveptr;
499
500 if (*saveptr != NULL) {
501 *saveptr = strpbrk(*saveptr, "\r\n");
502
503 if (*saveptr != NULL) {
504 char *eos = *saveptr;
505
506 if ((*saveptr)[0] == '\r' && (*saveptr)[1] == '\n')
507 (*saveptr)++;
508 (*saveptr)++;
509 if ((*saveptr)[0] == '\0')
510 *saveptr = NULL;
511
512 *eos = '\0';
513 }
514 }
515
516 return result;
517}