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 | |
Philipp Maier | 993ea6b | 2020-08-04 18:26:50 +0200 | [diff] [blame] | 27 | #include <osmocom/mgcp/mgcp.h> |
| 28 | #include <osmocom/mgcp/osmux.h> |
| 29 | #include <osmocom/mgcp/mgcp_protocol.h> |
Neels Hofmeyr | 6779354 | 2017-09-08 04:25:16 +0200 | [diff] [blame] | 30 | #include <osmocom/mgcp/mgcp_common.h> |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 31 | #include <osmocom/mgcp/mgcp_msg.h> |
| 32 | #include <osmocom/mgcp/mgcp_conn.h> |
Philipp Maier | 37d11c8 | 2018-02-01 14:38:12 +0100 | [diff] [blame] | 33 | #include <osmocom/mgcp/mgcp_endp.h> |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 34 | |
| 35 | /*! Display an mgcp message on the log output. |
| 36 | * \param[in] message mgcp message string |
| 37 | * \param[in] len message mgcp message string length |
| 38 | * \param[in] preamble string to display in logtext in front of each line */ |
| 39 | void mgcp_disp_msg(unsigned char *message, unsigned int len, char *preamble) |
| 40 | { |
| 41 | unsigned char line[80]; |
| 42 | unsigned char *ptr; |
| 43 | unsigned int consumed = 0; |
| 44 | unsigned int consumed_line = 0; |
| 45 | unsigned int line_count = 0; |
| 46 | |
| 47 | if (!log_check_level(DLMGCP, LOGL_DEBUG)) |
| 48 | return; |
| 49 | |
| 50 | while (1) { |
| 51 | memset(line, 0, sizeof(line)); |
| 52 | ptr = line; |
| 53 | consumed_line = 0; |
| 54 | do { |
| 55 | if (*message != '\n' && *message != '\r') { |
| 56 | *ptr = *message; |
| 57 | ptr++; |
| 58 | } |
| 59 | message++; |
| 60 | consumed++; |
| 61 | consumed_line++; |
| 62 | } while (*message != '\n' && consumed < len |
| 63 | && consumed_line < sizeof(line)); |
| 64 | |
| 65 | if (strlen((const char *)line)) { |
| 66 | LOGP(DLMGCP, LOGL_DEBUG, "%s: line #%02u: %s\n", |
| 67 | preamble, line_count, line); |
| 68 | line_count++; |
| 69 | } |
| 70 | |
| 71 | if (consumed >= len) |
| 72 | return; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | /*! Parse connection mode. |
| 77 | * \param[in] mode as string (recvonly, sendrecv, sendonly or loopback) |
| 78 | * \param[in] endp pointer to endpoint (only used for log output) |
| 79 | * \param[out] associated connection to be modified accordingly |
| 80 | * \returns 0 on success, -1 on error */ |
| 81 | int mgcp_parse_conn_mode(const char *mode, struct mgcp_endpoint *endp, |
| 82 | struct mgcp_conn *conn) |
| 83 | { |
| 84 | int ret = 0; |
| 85 | |
| 86 | if (!mode) { |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 87 | LOGPCONN(conn, DLMGCP, LOGL_ERROR, |
| 88 | "missing connection mode\n"); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 89 | return -1; |
| 90 | } |
| 91 | if (!conn) |
| 92 | return -1; |
| 93 | if (!endp) |
| 94 | return -1; |
| 95 | |
Pau Espin Pedrol | 1705848 | 2019-06-26 12:23:02 +0200 | [diff] [blame] | 96 | if (strcasecmp(mode, "recvonly") == 0) |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 97 | conn->mode = MGCP_CONN_RECV_ONLY; |
Pau Espin Pedrol | 1705848 | 2019-06-26 12:23:02 +0200 | [diff] [blame] | 98 | else if (strcasecmp(mode, "sendrecv") == 0) |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 99 | conn->mode = MGCP_CONN_RECV_SEND; |
Pau Espin Pedrol | 1705848 | 2019-06-26 12:23:02 +0200 | [diff] [blame] | 100 | else if (strcasecmp(mode, "sendonly") == 0) |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 101 | conn->mode = MGCP_CONN_SEND_ONLY; |
Pau Espin Pedrol | 1705848 | 2019-06-26 12:23:02 +0200 | [diff] [blame] | 102 | else if (strcasecmp(mode, "loopback") == 0) |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 103 | conn->mode = MGCP_CONN_LOOPBACK; |
| 104 | else { |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 105 | LOGPCONN(conn, DLMGCP, LOGL_ERROR, |
| 106 | "unknown connection mode: '%s'\n", mode); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 107 | ret = -1; |
| 108 | } |
| 109 | |
Pau Espin Pedrol | 30e0135 | 2020-09-21 12:29:41 +0200 | [diff] [blame] | 110 | /* Special handling for RTP connections */ |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 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 | |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 116 | LOGPENDP(endp, DLMGCP, LOGL_DEBUG, "conn:%s\n", mgcp_conn_dump(conn)); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 117 | |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 118 | LOGPCONN(conn, DLMGCP, LOGL_DEBUG, "connection mode '%s' %d\n", |
| 119 | mode, conn->mode); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 120 | |
| 121 | /* Special handling für RTP connections */ |
| 122 | if (conn->type == MGCP_CONN_TYPE_RTP) { |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 123 | LOGPCONN(conn, DLMGCP, LOGL_DEBUG, "output_enabled %d\n", |
| 124 | conn->u.rtp.end.output_enabled); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | /* The VTY might change the connection mode at any time, so we have |
| 128 | * to hold a copy of the original connection mode */ |
| 129 | conn->mode_orig = conn->mode; |
| 130 | |
| 131 | return ret; |
| 132 | } |
| 133 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 134 | /*! Analyze and parse the the hader of an MGCP messeage string. |
| 135 | * \param[out] pdata caller provided memory to store the parsing results |
| 136 | * \param[in] data mgcp message string |
| 137 | * \returns when the status line was complete and transaction_id and |
| 138 | * endp out parameters are set, -1 on error */ |
| 139 | int mgcp_parse_header(struct mgcp_parse_data *pdata, char *data) |
| 140 | { |
| 141 | int i = 0; |
| 142 | char *elem, *save = NULL; |
Philipp Maier | a49e32a | 2018-02-01 18:18:50 +0100 | [diff] [blame] | 143 | int cause; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 144 | |
| 145 | /*! This function will parse the header part of the received |
| 146 | * MGCP message. The parsing results are stored in pdata. |
| 147 | * The function will also automatically search the pool with |
| 148 | * available endpoints in order to find an endpoint that matches |
| 149 | * the endpoint string in in the header */ |
| 150 | |
| 151 | OSMO_ASSERT(data); |
| 152 | pdata->trans = "000000"; |
| 153 | |
| 154 | for (elem = strtok_r(data, " ", &save); elem; |
| 155 | elem = strtok_r(NULL, " ", &save)) { |
| 156 | switch (i) { |
| 157 | case 0: |
| 158 | pdata->trans = elem; |
| 159 | break; |
| 160 | case 1: |
Philipp Maier | c66ab2c | 2020-06-02 20:55:34 +0200 | [diff] [blame] | 161 | pdata->endp = mgcp_endp_by_name(&cause, elem, pdata->cfg); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 162 | if (!pdata->endp) { |
| 163 | LOGP(DLMGCP, LOGL_ERROR, |
| 164 | "Unable to find Endpoint `%s'\n", elem); |
Neels Hofmeyr | 0a89e92 | 2018-08-20 22:39:53 +0200 | [diff] [blame] | 165 | OSMO_ASSERT(cause < 0); |
Philipp Maier | a49e32a | 2018-02-01 18:18:50 +0100 | [diff] [blame] | 166 | return cause; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 167 | } |
| 168 | break; |
| 169 | case 2: |
Pau Espin Pedrol | 9a34592 | 2019-06-26 13:06:30 +0200 | [diff] [blame] | 170 | if (strcasecmp("MGCP", elem)) { |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 171 | LOGP(DLMGCP, LOGL_ERROR, |
| 172 | "MGCP header parsing error\n"); |
Harald Welte | abbb6b9 | 2017-12-28 13:13:50 +0100 | [diff] [blame] | 173 | return -510; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 174 | } |
| 175 | break; |
| 176 | case 3: |
| 177 | if (strcmp("1.0", elem)) { |
| 178 | LOGP(DLMGCP, LOGL_ERROR, "MGCP version `%s' " |
| 179 | "not supported\n", elem); |
Harald Welte | abbb6b9 | 2017-12-28 13:13:50 +0100 | [diff] [blame] | 180 | return -528; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 181 | } |
| 182 | break; |
| 183 | } |
| 184 | i++; |
| 185 | } |
| 186 | |
| 187 | if (i != 4) { |
| 188 | LOGP(DLMGCP, LOGL_ERROR, "MGCP status line too short.\n"); |
| 189 | pdata->trans = "000000"; |
| 190 | pdata->endp = NULL; |
Harald Welte | abbb6b9 | 2017-12-28 13:13:50 +0100 | [diff] [blame] | 191 | return -510; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | return 0; |
| 195 | } |
| 196 | |
| 197 | /*! Extract OSMUX CID from an MGCP parameter line (string). |
| 198 | * \param[in] line single parameter line from the MGCP message |
Pau Espin Pedrol | 2b89617 | 2019-04-24 13:47:23 +0200 | [diff] [blame] | 199 | * \returns OSMUX CID, -1 wildcard, -2 on error */ |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 200 | int mgcp_parse_osmux_cid(const char *line) |
| 201 | { |
| 202 | int osmux_cid; |
| 203 | |
Pau Espin Pedrol | 2b89617 | 2019-04-24 13:47:23 +0200 | [diff] [blame] | 204 | |
Pau Espin Pedrol | c1bf469 | 2019-05-14 16:23:24 +0200 | [diff] [blame] | 205 | if (strcasecmp(line + 2, "Osmux: *") == 0) { |
Pau Espin Pedrol | 2b89617 | 2019-04-24 13:47:23 +0200 | [diff] [blame] | 206 | LOGP(DLMGCP, LOGL_DEBUG, "Parsed wilcard Osmux CID\n"); |
| 207 | return -1; |
| 208 | } |
| 209 | |
Pau Espin Pedrol | c1bf469 | 2019-05-14 16:23:24 +0200 | [diff] [blame] | 210 | if (sscanf(line + 2 + 7, "%u", &osmux_cid) != 1) { |
Pau Espin Pedrol | ef6304e | 2019-04-23 13:24:37 +0200 | [diff] [blame] | 211 | LOGP(DLMGCP, LOGL_ERROR, "Failed parsing Osmux in MGCP msg line: %s\n", |
| 212 | line); |
Pau Espin Pedrol | 2b89617 | 2019-04-24 13:47:23 +0200 | [diff] [blame] | 213 | return -2; |
Pau Espin Pedrol | ef6304e | 2019-04-23 13:24:37 +0200 | [diff] [blame] | 214 | } |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 215 | |
| 216 | if (osmux_cid > OSMUX_CID_MAX) { |
| 217 | LOGP(DLMGCP, LOGL_ERROR, "Osmux ID too large: %u > %u\n", |
| 218 | osmux_cid, OSMUX_CID_MAX); |
Pau Espin Pedrol | 2b89617 | 2019-04-24 13:47:23 +0200 | [diff] [blame] | 219 | return -2; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 220 | } |
| 221 | LOGP(DLMGCP, LOGL_DEBUG, "bsc-nat offered Osmux CID %u\n", osmux_cid); |
| 222 | |
| 223 | return osmux_cid; |
| 224 | } |
| 225 | |
| 226 | /*! Check MGCP parameter line (string) for plausibility. |
| 227 | * \param[in] endp pointer to endpoint (only used for log output) |
| 228 | * \param[in] line single parameter line from the MGCP message |
| 229 | * \returns 1 when line seems plausible, 0 on error */ |
| 230 | int mgcp_check_param(const struct mgcp_endpoint *endp, const char *line) |
| 231 | { |
| 232 | const size_t line_len = strlen(line); |
| 233 | if (line[0] != '\0' && line_len < 2) { |
| 234 | LOGP(DLMGCP, LOGL_ERROR, |
Philipp Maier | c66ab2c | 2020-06-02 20:55:34 +0200 | [diff] [blame] | 235 | "Wrong MGCP option format: '%s' on %s\n", |
| 236 | line, endp->name); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 237 | return 0; |
| 238 | } |
| 239 | |
| 240 | /* FIXME: A couple more checks wouldn't hurt... */ |
| 241 | |
| 242 | return 1; |
| 243 | } |
| 244 | |
| 245 | /*! Check if the specified callid seems plausible. |
| 246 | * \param[in] endp pointer to endpoint |
| 247 | * \param{in] callid to verify |
Pau Espin Pedrol | 06624e1 | 2020-09-21 12:18:52 +0200 | [diff] [blame] | 248 | * \returns 0 when callid seems plausible, -1 on error */ |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 249 | int mgcp_verify_call_id(struct mgcp_endpoint *endp, const char *callid) |
| 250 | { |
| 251 | /*! This function compares the supplied callid with the called that is |
| 252 | * stored in the endpoint structure. */ |
| 253 | |
| 254 | if (!endp) |
| 255 | return -1; |
Neels Hofmeyr | e6d8e91 | 2018-08-23 16:36:48 +0200 | [diff] [blame] | 256 | |
| 257 | /* Accept any CallID for "X-Osmo-IGN: C" */ |
| 258 | if (endp->x_osmo_ign & MGCP_X_OSMO_IGN_CALLID) |
| 259 | return 0; |
| 260 | |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 261 | if (!callid) |
| 262 | return -1; |
| 263 | if (!endp->callid) |
| 264 | return -1; |
| 265 | |
| 266 | if (strcmp(endp->callid, callid) != 0) { |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 267 | LOGPENDP(endp, DLMGCP, LOGL_ERROR, |
| 268 | "CallIDs mismatch: '%s' != '%s'\n", |
| 269 | endp->callid, callid); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 270 | return -1; |
| 271 | } |
| 272 | |
| 273 | return 0; |
| 274 | } |
| 275 | |
| 276 | /*! Check if the specified connection id seems plausible. |
| 277 | * \param[in] endp pointer to endpoint |
| 278 | * \param{in] connection id to verify |
Neels Hofmeyr | eb72ff0 | 2018-09-03 23:00:07 +0200 | [diff] [blame] | 279 | * \returns 0 when connection id is valid and exists, an RFC3435 error code on error. |
Neels Hofmeyr | 8a91d2c | 2018-09-03 22:51:30 +0200 | [diff] [blame] | 280 | */ |
Philipp Maier | 01d24a3 | 2017-11-21 17:26:09 +0100 | [diff] [blame] | 281 | int mgcp_verify_ci(struct mgcp_endpoint *endp, const char *conn_id) |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 282 | { |
Neels Hofmeyr | eb72ff0 | 2018-09-03 23:00:07 +0200 | [diff] [blame] | 283 | /* For invalid conn_ids, return 510 "The transaction could not be executed, because some |
| 284 | * unspecified protocol error was detected." */ |
| 285 | |
Philipp Maier | 01d24a3 | 2017-11-21 17:26:09 +0100 | [diff] [blame] | 286 | /* Check for null identifiers */ |
| 287 | if (!conn_id) { |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 288 | LOGPENDP(endp, DLMGCP, LOGL_ERROR, |
| 289 | "invalid ConnectionIdentifier (missing)\n"); |
Neels Hofmeyr | eb72ff0 | 2018-09-03 23:00:07 +0200 | [diff] [blame] | 290 | return 510; |
Philipp Maier | 01d24a3 | 2017-11-21 17:26:09 +0100 | [diff] [blame] | 291 | } |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 292 | |
Philipp Maier | 01d24a3 | 2017-11-21 17:26:09 +0100 | [diff] [blame] | 293 | /* Check for empty connection identifiers */ |
| 294 | if (strlen(conn_id) == 0) { |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 295 | LOGPENDP(endp, DLMGCP, LOGL_ERROR, |
| 296 | "invalid ConnectionIdentifier (empty)\n"); |
Neels Hofmeyr | eb72ff0 | 2018-09-03 23:00:07 +0200 | [diff] [blame] | 297 | return 510; |
Philipp Maier | 01d24a3 | 2017-11-21 17:26:09 +0100 | [diff] [blame] | 298 | } |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 299 | |
Philipp Maier | 01d24a3 | 2017-11-21 17:26:09 +0100 | [diff] [blame] | 300 | /* Check for over long connection identifiers */ |
Neels Hofmeyr | 5336f57 | 2018-09-03 22:05:48 +0200 | [diff] [blame] | 301 | if (strlen(conn_id) > (MGCP_CONN_ID_MAXLEN-1)) { |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 302 | LOGPENDP(endp, DLMGCP, LOGL_ERROR, |
| 303 | "invalid ConnectionIdentifier (too long: %zu > %d) 0x%s\n", |
| 304 | strlen(conn_id), MGCP_CONN_ID_MAXLEN-1, conn_id); |
Neels Hofmeyr | eb72ff0 | 2018-09-03 23:00:07 +0200 | [diff] [blame] | 305 | return 510; |
Philipp Maier | 01d24a3 | 2017-11-21 17:26:09 +0100 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | /* Check if connection exists */ |
| 309 | if (mgcp_conn_get(endp, conn_id)) |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 310 | return 0; |
| 311 | |
Pau Espin Pedrol | 3239f62 | 2019-04-24 18:47:46 +0200 | [diff] [blame] | 312 | LOGPENDP(endp, DLMGCP, LOGL_ERROR, |
| 313 | "no connection found under ConnectionIdentifier 0x%s\n", conn_id); |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 314 | |
Neels Hofmeyr | eb72ff0 | 2018-09-03 23:00:07 +0200 | [diff] [blame] | 315 | /* When the conn_id was not found, return error code 515 "The transaction refers to an incorrect |
| 316 | * connection-id (may have been already deleted)." */ |
| 317 | return 515; |
Philipp Maier | 87bd9be | 2017-08-22 16:35:41 +0200 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | /*! Extract individual lines from MCGP message. |
| 321 | * \param[in] str MGCP message string, consisting of multiple lines |
| 322 | * \param{in] saveptr pointer to next line in str |
| 323 | * \returns line, NULL when done */ |
| 324 | char *mgcp_strline(char *str, char **saveptr) |
| 325 | { |
| 326 | char *result; |
| 327 | |
| 328 | /*! The function must be called with *str set to the input string |
| 329 | * for the first line. After that saveptr will be initalized. |
| 330 | * all consecutive lines are extracted by calling the function |
| 331 | * with str set to NULL. When done, the function will return NULL |
| 332 | * to indicate that all lines have been parsed. */ |
| 333 | |
| 334 | if (str) |
| 335 | *saveptr = str; |
| 336 | |
| 337 | result = *saveptr; |
| 338 | |
| 339 | if (*saveptr != NULL) { |
| 340 | *saveptr = strpbrk(*saveptr, "\r\n"); |
| 341 | |
| 342 | if (*saveptr != NULL) { |
| 343 | char *eos = *saveptr; |
| 344 | |
| 345 | if ((*saveptr)[0] == '\r' && (*saveptr)[1] == '\n') |
| 346 | (*saveptr)++; |
| 347 | (*saveptr)++; |
| 348 | if ((*saveptr)[0] == '\0') |
| 349 | *saveptr = NULL; |
| 350 | |
| 351 | *eos = '\0'; |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | return result; |
| 356 | } |