1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
9 *
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at https://curl.se/docs/copyright.html.
13 *
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
17 *
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
20 *
21 * SPDX-License-Identifier: curl
22 *
23 ***************************************************************************/
24
25 #include "curl_setup.h"
26
27 #if !defined(CURL_DISABLE_RTSP) && !defined(USE_HYPER)
28
29 #include "urldata.h"
30 #include <curl/curl.h>
31 #include "transfer.h"
32 #include "sendf.h"
33 #include "multiif.h"
34 #include "http.h"
35 #include "url.h"
36 #include "progress.h"
37 #include "rtsp.h"
38 #include "strcase.h"
39 #include "select.h"
40 #include "connect.h"
41 #include "cfilters.h"
42 #include "strdup.h"
43 /* The last 3 #include files should be in this order */
44 #include "curl_printf.h"
45 #include "curl_memory.h"
46 #include "memdebug.h"
47
48 #define RTP_PKT_LENGTH(p) ((((unsigned int)((unsigned char)((p)[2]))) << 8) | \
49 ((unsigned int)((unsigned char)((p)[3]))))
50
51 /* protocol-specific functions set up to be called by the main engine */
52 static CURLcode rtsp_do(struct Curl_easy *data, bool *done);
53 static CURLcode rtsp_done(struct Curl_easy *data, CURLcode, bool premature);
54 static CURLcode rtsp_connect(struct Curl_easy *data, bool *done);
55 static CURLcode rtsp_disconnect(struct Curl_easy *data,
56 struct connectdata *conn, bool dead);
57 static int rtsp_getsock_do(struct Curl_easy *data,
58 struct connectdata *conn, curl_socket_t *socks);
59
60 /*
61 * Parse and write out an RTSP response.
62 * @param data the transfer
63 * @param conn the connection
64 * @param buf data read from connection
65 * @param blen amount of data in buf
66 * @param is_eos TRUE iff this is the last write
67 * @param readmore out, TRUE iff complete buf was consumed and more data
68 * is needed
69 */
70 static CURLcode rtsp_rtp_write_resp(struct Curl_easy *data,
71 const char *buf,
72 size_t blen,
73 bool is_eos);
74
75 static CURLcode rtsp_setup_connection(struct Curl_easy *data,
76 struct connectdata *conn);
77 static unsigned int rtsp_conncheck(struct Curl_easy *data,
78 struct connectdata *check,
79 unsigned int checks_to_perform);
80
81 /* this returns the socket to wait for in the DO and DOING state for the multi
82 interface and then we're always _sending_ a request and thus we wait for
83 the single socket to become writable only */
rtsp_getsock_do(struct Curl_easy * data,struct connectdata * conn,curl_socket_t * socks)84 static int rtsp_getsock_do(struct Curl_easy *data, struct connectdata *conn,
85 curl_socket_t *socks)
86 {
87 /* write mode */
88 (void)data;
89 socks[0] = conn->sock[FIRSTSOCKET];
90 return GETSOCK_WRITESOCK(0);
91 }
92
93 static
94 CURLcode rtp_client_write(struct Curl_easy *data, const char *ptr, size_t len);
95 static
96 CURLcode rtsp_parse_transport(struct Curl_easy *data, const char *transport);
97
98
99 /*
100 * RTSP handler interface.
101 */
102 const struct Curl_handler Curl_handler_rtsp = {
103 "rtsp", /* scheme */
104 rtsp_setup_connection, /* setup_connection */
105 rtsp_do, /* do_it */
106 rtsp_done, /* done */
107 ZERO_NULL, /* do_more */
108 rtsp_connect, /* connect_it */
109 ZERO_NULL, /* connecting */
110 ZERO_NULL, /* doing */
111 ZERO_NULL, /* proto_getsock */
112 rtsp_getsock_do, /* doing_getsock */
113 ZERO_NULL, /* domore_getsock */
114 ZERO_NULL, /* perform_getsock */
115 rtsp_disconnect, /* disconnect */
116 rtsp_rtp_write_resp, /* write_resp */
117 ZERO_NULL, /* write_resp_hd */
118 rtsp_conncheck, /* connection_check */
119 ZERO_NULL, /* attach connection */
120 PORT_RTSP, /* defport */
121 CURLPROTO_RTSP, /* protocol */
122 CURLPROTO_RTSP, /* family */
123 PROTOPT_NONE /* flags */
124 };
125
126 #define MAX_RTP_BUFFERSIZE 1000000 /* arbitrary */
127
rtsp_setup_connection(struct Curl_easy * data,struct connectdata * conn)128 static CURLcode rtsp_setup_connection(struct Curl_easy *data,
129 struct connectdata *conn)
130 {
131 struct RTSP *rtsp;
132 (void)conn;
133
134 data->req.p.rtsp = rtsp = calloc(1, sizeof(struct RTSP));
135 if(!rtsp)
136 return CURLE_OUT_OF_MEMORY;
137
138 Curl_dyn_init(&conn->proto.rtspc.buf, MAX_RTP_BUFFERSIZE);
139 return CURLE_OK;
140 }
141
142
143 /*
144 * Function to check on various aspects of a connection.
145 */
rtsp_conncheck(struct Curl_easy * data,struct connectdata * conn,unsigned int checks_to_perform)146 static unsigned int rtsp_conncheck(struct Curl_easy *data,
147 struct connectdata *conn,
148 unsigned int checks_to_perform)
149 {
150 unsigned int ret_val = CONNRESULT_NONE;
151 (void)data;
152
153 if(checks_to_perform & CONNCHECK_ISDEAD) {
154 bool input_pending;
155 if(!Curl_conn_is_alive(data, conn, &input_pending))
156 ret_val |= CONNRESULT_DEAD;
157 }
158
159 return ret_val;
160 }
161
162
rtsp_connect(struct Curl_easy * data,bool * done)163 static CURLcode rtsp_connect(struct Curl_easy *data, bool *done)
164 {
165 CURLcode httpStatus;
166
167 httpStatus = Curl_http_connect(data, done);
168
169 /* Initialize the CSeq if not already done */
170 if(data->state.rtsp_next_client_CSeq == 0)
171 data->state.rtsp_next_client_CSeq = 1;
172 if(data->state.rtsp_next_server_CSeq == 0)
173 data->state.rtsp_next_server_CSeq = 1;
174
175 data->conn->proto.rtspc.rtp_channel = -1;
176
177 return httpStatus;
178 }
179
rtsp_disconnect(struct Curl_easy * data,struct connectdata * conn,bool dead)180 static CURLcode rtsp_disconnect(struct Curl_easy *data,
181 struct connectdata *conn, bool dead)
182 {
183 (void) dead;
184 (void) data;
185 Curl_dyn_free(&conn->proto.rtspc.buf);
186 return CURLE_OK;
187 }
188
189
rtsp_done(struct Curl_easy * data,CURLcode status,bool premature)190 static CURLcode rtsp_done(struct Curl_easy *data,
191 CURLcode status, bool premature)
192 {
193 struct RTSP *rtsp = data->req.p.rtsp;
194 CURLcode httpStatus;
195
196 /* Bypass HTTP empty-reply checks on receive */
197 if(data->set.rtspreq == RTSPREQ_RECEIVE)
198 premature = TRUE;
199
200 httpStatus = Curl_http_done(data, status, premature);
201
202 if(rtsp && !status && !httpStatus) {
203 /* Check the sequence numbers */
204 long CSeq_sent = rtsp->CSeq_sent;
205 long CSeq_recv = rtsp->CSeq_recv;
206 if((data->set.rtspreq != RTSPREQ_RECEIVE) && (CSeq_sent != CSeq_recv)) {
207 failf(data,
208 "The CSeq of this request %ld did not match the response %ld",
209 CSeq_sent, CSeq_recv);
210 return CURLE_RTSP_CSEQ_ERROR;
211 }
212 if(data->set.rtspreq == RTSPREQ_RECEIVE &&
213 (data->conn->proto.rtspc.rtp_channel == -1)) {
214 infof(data, "Got an RTP Receive with a CSeq of %ld", CSeq_recv);
215 }
216 }
217
218 return httpStatus;
219 }
220
rtsp_do(struct Curl_easy * data,bool * done)221 static CURLcode rtsp_do(struct Curl_easy *data, bool *done)
222 {
223 struct connectdata *conn = data->conn;
224 CURLcode result = CURLE_OK;
225 Curl_RtspReq rtspreq = data->set.rtspreq;
226 struct RTSP *rtsp = data->req.p.rtsp;
227 struct dynbuf req_buffer;
228
229 const char *p_request = NULL;
230 const char *p_session_id = NULL;
231 const char *p_accept = NULL;
232 const char *p_accept_encoding = NULL;
233 const char *p_range = NULL;
234 const char *p_referrer = NULL;
235 const char *p_stream_uri = NULL;
236 const char *p_transport = NULL;
237 const char *p_uagent = NULL;
238 const char *p_proxyuserpwd = NULL;
239 const char *p_userpwd = NULL;
240
241 *done = TRUE;
242 /* Initialize a dynamic send buffer */
243 Curl_dyn_init(&req_buffer, DYN_RTSP_REQ_HEADER);
244
245 rtsp->CSeq_sent = data->state.rtsp_next_client_CSeq;
246 rtsp->CSeq_recv = 0;
247
248 /* Setup the first_* fields to allow auth details get sent
249 to this origin */
250
251 if(!data->state.first_host) {
252 data->state.first_host = strdup(conn->host.name);
253 if(!data->state.first_host)
254 return CURLE_OUT_OF_MEMORY;
255
256 data->state.first_remote_port = conn->remote_port;
257 data->state.first_remote_protocol = conn->handler->protocol;
258 }
259
260 /* Setup the 'p_request' pointer to the proper p_request string
261 * Since all RTSP requests are included here, there is no need to
262 * support custom requests like HTTP.
263 **/
264 data->req.no_body = TRUE; /* most requests don't contain a body */
265 switch(rtspreq) {
266 default:
267 failf(data, "Got invalid RTSP request");
268 return CURLE_BAD_FUNCTION_ARGUMENT;
269 case RTSPREQ_OPTIONS:
270 p_request = "OPTIONS";
271 break;
272 case RTSPREQ_DESCRIBE:
273 p_request = "DESCRIBE";
274 data->req.no_body = FALSE;
275 break;
276 case RTSPREQ_ANNOUNCE:
277 p_request = "ANNOUNCE";
278 break;
279 case RTSPREQ_SETUP:
280 p_request = "SETUP";
281 break;
282 case RTSPREQ_PLAY:
283 p_request = "PLAY";
284 break;
285 case RTSPREQ_PAUSE:
286 p_request = "PAUSE";
287 break;
288 case RTSPREQ_TEARDOWN:
289 p_request = "TEARDOWN";
290 break;
291 case RTSPREQ_GET_PARAMETER:
292 /* GET_PARAMETER's no_body status is determined later */
293 p_request = "GET_PARAMETER";
294 data->req.no_body = FALSE;
295 break;
296 case RTSPREQ_SET_PARAMETER:
297 p_request = "SET_PARAMETER";
298 break;
299 case RTSPREQ_RECORD:
300 p_request = "RECORD";
301 break;
302 case RTSPREQ_RECEIVE:
303 p_request = "";
304 /* Treat interleaved RTP as body */
305 data->req.no_body = FALSE;
306 break;
307 case RTSPREQ_LAST:
308 failf(data, "Got invalid RTSP request: RTSPREQ_LAST");
309 return CURLE_BAD_FUNCTION_ARGUMENT;
310 }
311
312 if(rtspreq == RTSPREQ_RECEIVE) {
313 Curl_xfer_setup(data, FIRSTSOCKET, -1, TRUE, -1);
314 goto out;
315 }
316
317 p_session_id = data->set.str[STRING_RTSP_SESSION_ID];
318 if(!p_session_id &&
319 (rtspreq & ~(RTSPREQ_OPTIONS | RTSPREQ_DESCRIBE | RTSPREQ_SETUP))) {
320 failf(data, "Refusing to issue an RTSP request [%s] without a session ID.",
321 p_request);
322 result = CURLE_BAD_FUNCTION_ARGUMENT;
323 goto out;
324 }
325
326 /* Stream URI. Default to server '*' if not specified */
327 if(data->set.str[STRING_RTSP_STREAM_URI]) {
328 p_stream_uri = data->set.str[STRING_RTSP_STREAM_URI];
329 }
330 else {
331 p_stream_uri = "*";
332 }
333
334 /* Transport Header for SETUP requests */
335 p_transport = Curl_checkheaders(data, STRCONST("Transport"));
336 if(rtspreq == RTSPREQ_SETUP && !p_transport) {
337 /* New Transport: setting? */
338 if(data->set.str[STRING_RTSP_TRANSPORT]) {
339 Curl_safefree(data->state.aptr.rtsp_transport);
340
341 data->state.aptr.rtsp_transport =
342 aprintf("Transport: %s\r\n",
343 data->set.str[STRING_RTSP_TRANSPORT]);
344 if(!data->state.aptr.rtsp_transport)
345 return CURLE_OUT_OF_MEMORY;
346 }
347 else {
348 failf(data,
349 "Refusing to issue an RTSP SETUP without a Transport: header.");
350 result = CURLE_BAD_FUNCTION_ARGUMENT;
351 goto out;
352 }
353
354 p_transport = data->state.aptr.rtsp_transport;
355 }
356
357 /* Accept Headers for DESCRIBE requests */
358 if(rtspreq == RTSPREQ_DESCRIBE) {
359 /* Accept Header */
360 p_accept = Curl_checkheaders(data, STRCONST("Accept"))?
361 NULL:"Accept: application/sdp\r\n";
362
363 /* Accept-Encoding header */
364 if(!Curl_checkheaders(data, STRCONST("Accept-Encoding")) &&
365 data->set.str[STRING_ENCODING]) {
366 Curl_safefree(data->state.aptr.accept_encoding);
367 data->state.aptr.accept_encoding =
368 aprintf("Accept-Encoding: %s\r\n", data->set.str[STRING_ENCODING]);
369
370 if(!data->state.aptr.accept_encoding) {
371 result = CURLE_OUT_OF_MEMORY;
372 goto out;
373 }
374 p_accept_encoding = data->state.aptr.accept_encoding;
375 }
376 }
377
378 /* The User-Agent string might have been allocated in url.c already, because
379 it might have been used in the proxy connect, but if we have got a header
380 with the user-agent string specified, we erase the previously made string
381 here. */
382 if(Curl_checkheaders(data, STRCONST("User-Agent")) &&
383 data->state.aptr.uagent) {
384 Curl_safefree(data->state.aptr.uagent);
385 }
386 else if(!Curl_checkheaders(data, STRCONST("User-Agent")) &&
387 data->set.str[STRING_USERAGENT]) {
388 p_uagent = data->state.aptr.uagent;
389 }
390
391 /* setup the authentication headers */
392 result = Curl_http_output_auth(data, conn, p_request, HTTPREQ_GET,
393 p_stream_uri, FALSE);
394 if(result)
395 goto out;
396
397 #ifndef CURL_DISABLE_PROXY
398 p_proxyuserpwd = data->state.aptr.proxyuserpwd;
399 #endif
400 p_userpwd = data->state.aptr.userpwd;
401
402 /* Referrer */
403 Curl_safefree(data->state.aptr.ref);
404 if(data->state.referer && !Curl_checkheaders(data, STRCONST("Referer")))
405 data->state.aptr.ref = aprintf("Referer: %s\r\n", data->state.referer);
406
407 p_referrer = data->state.aptr.ref;
408
409 /*
410 * Range Header
411 * Only applies to PLAY, PAUSE, RECORD
412 *
413 * Go ahead and use the Range stuff supplied for HTTP
414 */
415 if(data->state.use_range &&
416 (rtspreq & (RTSPREQ_PLAY | RTSPREQ_PAUSE | RTSPREQ_RECORD))) {
417
418 /* Check to see if there is a range set in the custom headers */
419 if(!Curl_checkheaders(data, STRCONST("Range")) && data->state.range) {
420 Curl_safefree(data->state.aptr.rangeline);
421 data->state.aptr.rangeline = aprintf("Range: %s\r\n", data->state.range);
422 p_range = data->state.aptr.rangeline;
423 }
424 }
425
426 /*
427 * Sanity check the custom headers
428 */
429 if(Curl_checkheaders(data, STRCONST("CSeq"))) {
430 failf(data, "CSeq cannot be set as a custom header.");
431 result = CURLE_RTSP_CSEQ_ERROR;
432 goto out;
433 }
434 if(Curl_checkheaders(data, STRCONST("Session"))) {
435 failf(data, "Session ID cannot be set as a custom header.");
436 result = CURLE_BAD_FUNCTION_ARGUMENT;
437 goto out;
438 }
439
440 result =
441 Curl_dyn_addf(&req_buffer,
442 "%s %s RTSP/1.0\r\n" /* Request Stream-URI RTSP/1.0 */
443 "CSeq: %ld\r\n", /* CSeq */
444 p_request, p_stream_uri, rtsp->CSeq_sent);
445 if(result)
446 goto out;
447
448 /*
449 * Rather than do a normal alloc line, keep the session_id unformatted
450 * to make comparison easier
451 */
452 if(p_session_id) {
453 result = Curl_dyn_addf(&req_buffer, "Session: %s\r\n", p_session_id);
454 if(result)
455 goto out;
456 }
457
458 /*
459 * Shared HTTP-like options
460 */
461 result = Curl_dyn_addf(&req_buffer,
462 "%s" /* transport */
463 "%s" /* accept */
464 "%s" /* accept-encoding */
465 "%s" /* range */
466 "%s" /* referrer */
467 "%s" /* user-agent */
468 "%s" /* proxyuserpwd */
469 "%s" /* userpwd */
470 ,
471 p_transport ? p_transport : "",
472 p_accept ? p_accept : "",
473 p_accept_encoding ? p_accept_encoding : "",
474 p_range ? p_range : "",
475 p_referrer ? p_referrer : "",
476 p_uagent ? p_uagent : "",
477 p_proxyuserpwd ? p_proxyuserpwd : "",
478 p_userpwd ? p_userpwd : "");
479
480 /*
481 * Free userpwd now --- cannot reuse this for Negotiate and possibly NTLM
482 * with basic and digest, it will be freed anyway by the next request
483 */
484 Curl_safefree(data->state.aptr.userpwd);
485
486 if(result)
487 goto out;
488
489 if((rtspreq == RTSPREQ_SETUP) || (rtspreq == RTSPREQ_DESCRIBE)) {
490 result = Curl_add_timecondition(data, &req_buffer);
491 if(result)
492 goto out;
493 }
494
495 result = Curl_add_custom_headers(data, FALSE, &req_buffer);
496 if(result)
497 goto out;
498
499 if(rtspreq == RTSPREQ_ANNOUNCE ||
500 rtspreq == RTSPREQ_SET_PARAMETER ||
501 rtspreq == RTSPREQ_GET_PARAMETER) {
502 curl_off_t req_clen; /* request content length */
503
504 if(data->state.upload) {
505 req_clen = data->state.infilesize;
506 data->state.httpreq = HTTPREQ_PUT;
507 result = Curl_creader_set_fread(data, req_clen);
508 if(result)
509 goto out;
510 }
511 else {
512 if(data->set.postfields) {
513 size_t plen = strlen(data->set.postfields);
514 req_clen = (curl_off_t)plen;
515 result = Curl_creader_set_buf(data, data->set.postfields, plen);
516 }
517 else if(data->state.infilesize >= 0) {
518 req_clen = data->state.infilesize;
519 result = Curl_creader_set_fread(data, req_clen);
520 }
521 else {
522 req_clen = 0;
523 result = Curl_creader_set_null(data);
524 }
525 if(result)
526 goto out;
527 }
528
529 if(req_clen > 0) {
530 /* As stated in the http comments, it is probably not wise to
531 * actually set a custom Content-Length in the headers */
532 if(!Curl_checkheaders(data, STRCONST("Content-Length"))) {
533 result =
534 Curl_dyn_addf(&req_buffer,
535 "Content-Length: %" CURL_FORMAT_CURL_OFF_T"\r\n",
536 req_clen);
537 if(result)
538 goto out;
539 }
540
541 if(rtspreq == RTSPREQ_SET_PARAMETER ||
542 rtspreq == RTSPREQ_GET_PARAMETER) {
543 if(!Curl_checkheaders(data, STRCONST("Content-Type"))) {
544 result = Curl_dyn_addn(&req_buffer,
545 STRCONST("Content-Type: "
546 "text/parameters\r\n"));
547 if(result)
548 goto out;
549 }
550 }
551
552 if(rtspreq == RTSPREQ_ANNOUNCE) {
553 if(!Curl_checkheaders(data, STRCONST("Content-Type"))) {
554 result = Curl_dyn_addn(&req_buffer,
555 STRCONST("Content-Type: "
556 "application/sdp\r\n"));
557 if(result)
558 goto out;
559 }
560 }
561 }
562 else if(rtspreq == RTSPREQ_GET_PARAMETER) {
563 /* Check for an empty GET_PARAMETER (heartbeat) request */
564 data->state.httpreq = HTTPREQ_HEAD;
565 data->req.no_body = TRUE;
566 }
567 }
568 else {
569 result = Curl_creader_set_null(data);
570 if(result)
571 goto out;
572 }
573
574 /* Finish the request buffer */
575 result = Curl_dyn_addn(&req_buffer, STRCONST("\r\n"));
576 if(result)
577 goto out;
578
579 Curl_xfer_setup(data, FIRSTSOCKET, -1, TRUE, FIRSTSOCKET);
580
581 /* issue the request */
582 result = Curl_req_send(data, &req_buffer);
583 if(result) {
584 failf(data, "Failed sending RTSP request");
585 goto out;
586 }
587
588 /* Increment the CSeq on success */
589 data->state.rtsp_next_client_CSeq++;
590
591 if(data->req.writebytecount) {
592 /* if a request-body has been sent off, we make sure this progress is
593 noted properly */
594 Curl_pgrsSetUploadCounter(data, data->req.writebytecount);
595 if(Curl_pgrsUpdate(data))
596 result = CURLE_ABORTED_BY_CALLBACK;
597 }
598 out:
599 Curl_dyn_free(&req_buffer);
600 return result;
601 }
602
603 /**
604 * write any BODY bytes missing to the client, ignore the rest.
605 */
rtp_write_body_junk(struct Curl_easy * data,const char * buf,size_t blen)606 static CURLcode rtp_write_body_junk(struct Curl_easy *data,
607 const char *buf,
608 size_t blen)
609 {
610 struct rtsp_conn *rtspc = &(data->conn->proto.rtspc);
611 curl_off_t body_remain;
612 bool in_body;
613
614 in_body = (data->req.headerline && !rtspc->in_header) &&
615 (data->req.size >= 0) &&
616 (data->req.bytecount < data->req.size);
617 body_remain = in_body? (data->req.size - data->req.bytecount) : 0;
618 DEBUGASSERT(body_remain >= 0);
619 if(body_remain) {
620 if((curl_off_t)blen > body_remain)
621 blen = (size_t)body_remain;
622 return Curl_client_write(data, CLIENTWRITE_BODY, (char *)buf, blen);
623 }
624 return CURLE_OK;
625 }
626
rtsp_filter_rtp(struct Curl_easy * data,const char * buf,size_t blen,size_t * pconsumed)627 static CURLcode rtsp_filter_rtp(struct Curl_easy *data,
628 const char *buf,
629 size_t blen,
630 size_t *pconsumed)
631 {
632 struct rtsp_conn *rtspc = &(data->conn->proto.rtspc);
633 CURLcode result = CURLE_OK;
634 size_t skip_len = 0;
635
636 *pconsumed = 0;
637 while(blen) {
638 bool in_body = (data->req.headerline && !rtspc->in_header) &&
639 (data->req.size >= 0) &&
640 (data->req.bytecount < data->req.size);
641 switch(rtspc->state) {
642
643 case RTP_PARSE_SKIP: {
644 DEBUGASSERT(Curl_dyn_len(&rtspc->buf) == 0);
645 while(blen && buf[0] != '$') {
646 if(!in_body && buf[0] == 'R' &&
647 data->set.rtspreq != RTSPREQ_RECEIVE) {
648 if(strncmp(buf, "RTSP/", (blen < 5) ? blen : 5) == 0) {
649 /* This could be the next response, no consume and return */
650 if(*pconsumed) {
651 DEBUGF(infof(data, "RTP rtsp_filter_rtp[SKIP] RTSP/ prefix, "
652 "skipping %zd bytes of junk", *pconsumed));
653 }
654 rtspc->state = RTP_PARSE_SKIP;
655 rtspc->in_header = TRUE;
656 goto out;
657 }
658 }
659 /* junk/BODY, consume without buffering */
660 *pconsumed += 1;
661 ++buf;
662 --blen;
663 ++skip_len;
664 }
665 if(blen && buf[0] == '$') {
666 /* possible start of an RTP message, buffer */
667 if(skip_len) {
668 /* end of junk/BODY bytes, flush */
669 result = rtp_write_body_junk(data,
670 (char *)(buf - skip_len), skip_len);
671 skip_len = 0;
672 if(result)
673 goto out;
674 }
675 if(Curl_dyn_addn(&rtspc->buf, buf, 1)) {
676 result = CURLE_OUT_OF_MEMORY;
677 goto out;
678 }
679 *pconsumed += 1;
680 ++buf;
681 --blen;
682 rtspc->state = RTP_PARSE_CHANNEL;
683 }
684 break;
685 }
686
687 case RTP_PARSE_CHANNEL: {
688 int idx = ((unsigned char)buf[0]) / 8;
689 int off = ((unsigned char)buf[0]) % 8;
690 DEBUGASSERT(Curl_dyn_len(&rtspc->buf) == 1);
691 if(!(data->state.rtp_channel_mask[idx] & (1 << off))) {
692 /* invalid channel number, junk or BODY data */
693 rtspc->state = RTP_PARSE_SKIP;
694 DEBUGASSERT(skip_len == 0);
695 /* we do not consume this byte, it is BODY data */
696 DEBUGF(infof(data, "RTSP: invalid RTP channel %d, skipping", idx));
697 if(*pconsumed == 0) {
698 /* We did not consume the initial '$' in our buffer, but had
699 * it from an earlier call. We cannot un-consume it and have
700 * to write it directly as BODY data */
701 result = rtp_write_body_junk(data, Curl_dyn_ptr(&rtspc->buf), 1);
702 if(result)
703 goto out;
704 }
705 else {
706 /* count the '$' as skip and continue */
707 skip_len = 1;
708 }
709 Curl_dyn_free(&rtspc->buf);
710 break;
711 }
712 /* a valid channel, so we expect this to be a real RTP message */
713 rtspc->rtp_channel = (unsigned char)buf[0];
714 if(Curl_dyn_addn(&rtspc->buf, buf, 1)) {
715 result = CURLE_OUT_OF_MEMORY;
716 goto out;
717 }
718 *pconsumed += 1;
719 ++buf;
720 --blen;
721 rtspc->state = RTP_PARSE_LEN;
722 break;
723 }
724
725 case RTP_PARSE_LEN: {
726 size_t rtp_len = Curl_dyn_len(&rtspc->buf);
727 const char *rtp_buf;
728 DEBUGASSERT(rtp_len >= 2 && rtp_len < 4);
729 if(Curl_dyn_addn(&rtspc->buf, buf, 1)) {
730 result = CURLE_OUT_OF_MEMORY;
731 goto out;
732 }
733 *pconsumed += 1;
734 ++buf;
735 --blen;
736 if(rtp_len == 2)
737 break;
738 rtp_buf = Curl_dyn_ptr(&rtspc->buf);
739 rtspc->rtp_len = RTP_PKT_LENGTH(rtp_buf) + 4;
740 rtspc->state = RTP_PARSE_DATA;
741 break;
742 }
743
744 case RTP_PARSE_DATA: {
745 size_t rtp_len = Curl_dyn_len(&rtspc->buf);
746 size_t needed;
747 DEBUGASSERT(rtp_len < rtspc->rtp_len);
748 needed = rtspc->rtp_len - rtp_len;
749 if(needed <= blen) {
750 if(Curl_dyn_addn(&rtspc->buf, buf, needed)) {
751 result = CURLE_OUT_OF_MEMORY;
752 goto out;
753 }
754 *pconsumed += needed;
755 buf += needed;
756 blen -= needed;
757 /* complete RTP message in buffer */
758 DEBUGF(infof(data, "RTP write channel %d rtp_len %zu",
759 rtspc->rtp_channel, rtspc->rtp_len));
760 result = rtp_client_write(data, Curl_dyn_ptr(&rtspc->buf),
761 rtspc->rtp_len);
762 Curl_dyn_free(&rtspc->buf);
763 rtspc->state = RTP_PARSE_SKIP;
764 if(result)
765 goto out;
766 }
767 else {
768 if(Curl_dyn_addn(&rtspc->buf, buf, blen)) {
769 result = CURLE_OUT_OF_MEMORY;
770 goto out;
771 }
772 *pconsumed += blen;
773 buf += blen;
774 blen = 0;
775 }
776 break;
777 }
778
779 default:
780 DEBUGASSERT(0);
781 return CURLE_RECV_ERROR;
782 }
783 }
784 out:
785 if(!result && skip_len)
786 result = rtp_write_body_junk(data, (char *)(buf - skip_len), skip_len);
787 return result;
788 }
789
rtsp_rtp_write_resp(struct Curl_easy * data,const char * buf,size_t blen,bool is_eos)790 static CURLcode rtsp_rtp_write_resp(struct Curl_easy *data,
791 const char *buf,
792 size_t blen,
793 bool is_eos)
794 {
795 struct rtsp_conn *rtspc = &(data->conn->proto.rtspc);
796 CURLcode result = CURLE_OK;
797 size_t consumed = 0;
798
799 if(!data->req.header)
800 rtspc->in_header = FALSE;
801 if(!blen) {
802 goto out;
803 }
804
805 DEBUGF(infof(data, "rtsp_rtp_write_resp(len=%zu, in_header=%d, eos=%d)",
806 blen, rtspc->in_header, is_eos));
807
808 /* If header parsing is not ongoing, extract RTP messages */
809 if(!rtspc->in_header) {
810 result = rtsp_filter_rtp(data, buf, blen, &consumed);
811 if(result)
812 goto out;
813 buf += consumed;
814 blen -= consumed;
815 /* either we consumed all or are at the start of header parsing */
816 if(blen && !data->req.header)
817 DEBUGF(infof(data, "RTSP: %zu bytes, possibly excess in response body",
818 blen));
819 }
820
821 /* we want to parse headers, do so */
822 if(data->req.header && blen) {
823 rtspc->in_header = TRUE;
824 result = Curl_http_write_resp_hds(data, buf, blen, &consumed);
825 if(result)
826 goto out;
827
828 buf += consumed;
829 blen -= consumed;
830
831 if(!data->req.header)
832 rtspc->in_header = FALSE;
833
834 if(!rtspc->in_header) {
835 /* If header parsing is done, extract interleaved RTP messages */
836 if(data->req.size <= -1) {
837 /* Respect section 4.4 of rfc2326: If the Content-Length header is
838 absent, a length 0 must be assumed. */
839 data->req.size = 0;
840 data->req.download_done = TRUE;
841 }
842 result = rtsp_filter_rtp(data, buf, blen, &consumed);
843 if(result)
844 goto out;
845 blen -= consumed;
846 }
847 }
848
849 if(rtspc->state != RTP_PARSE_SKIP)
850 data->req.done = FALSE;
851 /* we SHOULD have consumed all bytes, unless the response is borked.
852 * In which case we write out the left over bytes, letting the client
853 * writer deal with it (it will report EXCESS and fail the transfer). */
854 DEBUGF(infof(data, "rtsp_rtp_write_resp(len=%zu, in_header=%d, done=%d "
855 " rtspc->state=%d, req.size=%" CURL_FORMAT_CURL_OFF_T ")",
856 blen, rtspc->in_header, data->req.done, rtspc->state,
857 data->req.size));
858 if(!result && (is_eos || blen)) {
859 result = Curl_client_write(data, CLIENTWRITE_BODY|
860 (is_eos? CLIENTWRITE_EOS:0),
861 (char *)buf, blen);
862 }
863
864 out:
865 if((data->set.rtspreq == RTSPREQ_RECEIVE) &&
866 (rtspc->state == RTP_PARSE_SKIP)) {
867 /* In special mode RECEIVE, we just process one chunk of network
868 * data, so we stop the transfer here, if we have no incomplete
869 * RTP message pending. */
870 data->req.download_done = TRUE;
871 }
872 return result;
873 }
874
875 static
rtp_client_write(struct Curl_easy * data,const char * ptr,size_t len)876 CURLcode rtp_client_write(struct Curl_easy *data, const char *ptr, size_t len)
877 {
878 size_t wrote;
879 curl_write_callback writeit;
880 void *user_ptr;
881
882 if(len == 0) {
883 failf(data, "Cannot write a 0 size RTP packet.");
884 return CURLE_WRITE_ERROR;
885 }
886
887 /* If the user has configured CURLOPT_INTERLEAVEFUNCTION then use that
888 function and any configured CURLOPT_INTERLEAVEDATA to write out the RTP
889 data. Otherwise, use the CURLOPT_WRITEFUNCTION with the CURLOPT_WRITEDATA
890 pointer to write out the RTP data. */
891 if(data->set.fwrite_rtp) {
892 writeit = data->set.fwrite_rtp;
893 user_ptr = data->set.rtp_out;
894 }
895 else {
896 writeit = data->set.fwrite_func;
897 user_ptr = data->set.out;
898 }
899
900 Curl_set_in_callback(data, true);
901 wrote = writeit((char *)ptr, 1, len, user_ptr);
902 Curl_set_in_callback(data, false);
903
904 if(CURL_WRITEFUNC_PAUSE == wrote) {
905 failf(data, "Cannot pause RTP");
906 return CURLE_WRITE_ERROR;
907 }
908
909 if(wrote != len) {
910 failf(data, "Failed writing RTP data");
911 return CURLE_WRITE_ERROR;
912 }
913
914 return CURLE_OK;
915 }
916
Curl_rtsp_parseheader(struct Curl_easy * data,const char * header)917 CURLcode Curl_rtsp_parseheader(struct Curl_easy *data, const char *header)
918 {
919 if(checkprefix("CSeq:", header)) {
920 long CSeq = 0;
921 char *endp;
922 const char *p = &header[5];
923 while(ISBLANK(*p))
924 p++;
925 CSeq = strtol(p, &endp, 10);
926 if(p != endp) {
927 struct RTSP *rtsp = data->req.p.rtsp;
928 rtsp->CSeq_recv = CSeq; /* mark the request */
929 data->state.rtsp_CSeq_recv = CSeq; /* update the handle */
930 }
931 else {
932 failf(data, "Unable to read the CSeq header: [%s]", header);
933 return CURLE_RTSP_CSEQ_ERROR;
934 }
935 }
936 else if(checkprefix("Session:", header)) {
937 const char *start, *end;
938 size_t idlen;
939
940 /* Find the first non-space letter */
941 start = header + 8;
942 while(*start && ISBLANK(*start))
943 start++;
944
945 if(!*start) {
946 failf(data, "Got a blank Session ID");
947 return CURLE_RTSP_SESSION_ERROR;
948 }
949
950 /* Find the end of Session ID
951 *
952 * Allow any non whitespace content, up to the field separator or end of
953 * line. RFC 2326 isn't 100% clear on the session ID and for example
954 * gstreamer does url-encoded session ID's not covered by the standard.
955 */
956 end = start;
957 while(*end && *end != ';' && !ISSPACE(*end))
958 end++;
959 idlen = end - start;
960
961 if(data->set.str[STRING_RTSP_SESSION_ID]) {
962
963 /* If the Session ID is set, then compare */
964 if(strlen(data->set.str[STRING_RTSP_SESSION_ID]) != idlen ||
965 strncmp(start, data->set.str[STRING_RTSP_SESSION_ID], idlen)) {
966 failf(data, "Got RTSP Session ID Line [%s], but wanted ID [%s]",
967 start, data->set.str[STRING_RTSP_SESSION_ID]);
968 return CURLE_RTSP_SESSION_ERROR;
969 }
970 }
971 else {
972 /* If the Session ID is not set, and we find it in a response, then set
973 * it.
974 */
975
976 /* Copy the id substring into a new buffer */
977 data->set.str[STRING_RTSP_SESSION_ID] = Curl_memdup0(start, idlen);
978 if(!data->set.str[STRING_RTSP_SESSION_ID])
979 return CURLE_OUT_OF_MEMORY;
980 }
981 }
982 else if(checkprefix("Transport:", header)) {
983 CURLcode result;
984 result = rtsp_parse_transport(data, header + 10);
985 if(result)
986 return result;
987 }
988 return CURLE_OK;
989 }
990
991 static
rtsp_parse_transport(struct Curl_easy * data,const char * transport)992 CURLcode rtsp_parse_transport(struct Curl_easy *data, const char *transport)
993 {
994 /* If we receive multiple Transport response-headers, the linterleaved
995 channels of each response header is recorded and used together for
996 subsequent data validity checks.*/
997 /* e.g.: ' RTP/AVP/TCP;unicast;interleaved=5-6' */
998 const char *start, *end;
999 start = transport;
1000 while(start && *start) {
1001 while(*start && ISBLANK(*start) )
1002 start++;
1003 end = strchr(start, ';');
1004 if(checkprefix("interleaved=", start)) {
1005 long chan1, chan2, chan;
1006 char *endp;
1007 const char *p = start + 12;
1008 chan1 = strtol(p, &endp, 10);
1009 if(p != endp && chan1 >= 0 && chan1 <= 255) {
1010 unsigned char *rtp_channel_mask = data->state.rtp_channel_mask;
1011 chan2 = chan1;
1012 if(*endp == '-') {
1013 p = endp + 1;
1014 chan2 = strtol(p, &endp, 10);
1015 if(p == endp || chan2 < 0 || chan2 > 255) {
1016 infof(data, "Unable to read the interleaved parameter from "
1017 "Transport header: [%s]", transport);
1018 chan2 = chan1;
1019 }
1020 }
1021 for(chan = chan1; chan <= chan2; chan++) {
1022 long idx = chan / 8;
1023 long off = chan % 8;
1024 rtp_channel_mask[idx] |= (unsigned char)(1 << off);
1025 }
1026 }
1027 else {
1028 infof(data, "Unable to read the interleaved parameter from "
1029 "Transport header: [%s]", transport);
1030 }
1031 break;
1032 }
1033 /* skip to next parameter */
1034 start = (!end) ? end : (end + 1);
1035 }
1036 return CURLE_OK;
1037 }
1038
1039
1040 #endif /* CURL_DISABLE_RTSP or using Hyper */
1041