master
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#include "curl_setup.h"
25#include "urldata.h"
26#include "rtsp.h"
28#ifndef CURL_DISABLE_RTSP
30#include "transfer.h"
31#include "sendf.h"
32#include "curl_trc.h"
33#include "multiif.h"
34#include "http.h"
35#include "url.h"
36#include "progress.h"
37#include "strcase.h"
38#include "select.h"
39#include "connect.h"
40#include "cfilters.h"
41#include "curlx/strdup.h"
42#include "bufref.h"
43#include "curlx/strparse.h"
44#include "peer.h"
46/* meta key for storing protocol meta at easy handle */
47#define CURL_META_RTSP_EASY "meta:proto:rtsp:easy"
48/* meta key for storing protocol meta at connection */
49#define CURL_META_RTSP_CONN "meta:proto:rtsp:conn"
51typedef enum {
52 RTP_PARSE_SKIP,
53 RTP_PARSE_CHANNEL,
54 RTP_PARSE_LEN,
55 RTP_PARSE_DATA
56} rtp_parse_st;
58/* RTSP Connection data
59 * Currently, only used for tracking incomplete RTP data reads */
60struct rtsp_conn {
61 struct dynbuf buf;
62 int rtp_channel;
63 size_t rtp_len;
64 rtp_parse_st state;
65 BIT(in_header);
66};
68/* RTSP transfer data */
69struct RTSP {
70 uint32_t CSeq_sent; /* CSeq of this request */
71 uint32_t CSeq_recv; /* CSeq received */
72};
74#define RTP_PKT_LENGTH(p) ((((unsigned int)((unsigned char)((p)[2]))) << 8) | \
75 ((unsigned int)((unsigned char)((p)[3]))))
77/* this returns the socket to wait for in the DO and DOING state for the multi
78 interface and then we are always _sending_ a request and thus we wait for
79 the single socket to become writable only */
80static CURLcode rtsp_do_pollset(struct Curl_easy *data,
81 struct easy_pollset *ps)
82{
83 /* write mode */
84 return Curl_pollset_add_out(data, ps, data->conn->sock[FIRSTSOCKET]);
85}
87#define MAX_RTP_BUFFERSIZE 1000000 /* arbitrary */
89static void rtsp_easy_dtor(const void *key, size_t klen, void *entry)
90{
91 struct RTSP *rtsp = entry;
92 (void)key;
93 (void)klen;
94 curlx_free(rtsp);
95}
97static void rtsp_conn_dtor(const void *key, size_t klen, void *entry)
98{
99 struct rtsp_conn *rtspc = entry;
100 (void)key;
101 (void)klen;
102 curlx_dyn_free(&rtspc->buf);
103 curlx_free(rtspc);
104}
106static CURLcode rtsp_setup_connection(struct Curl_easy *data,
107 struct connectdata *conn)
108{
109 struct rtsp_conn *rtspc;
110 struct RTSP *rtsp;
112 if(!(data->state.http_neg.wanted & CURL_HTTP_V1x) ||
113 !(data->state.http_neg.allowed & CURL_HTTP_V1x)) {
114 failf(data, "RTSP is only supported over HTTP/1.x");
115 return CURLE_FAILED_INIT;
116 }
117 /* zap any other http version */
118 data->state.http_neg.accept_09 = FALSE;
119 data->state.http_neg.only_10 = FALSE;
120 data->state.http_neg.wanted = CURL_HTTP_V1x;
121 data->state.http_neg.allowed = CURL_HTTP_V1x;
123 rtspc = curlx_calloc(1, sizeof(*rtspc));
124 if(!rtspc)
125 return CURLE_OUT_OF_MEMORY;
126 curlx_dyn_init(&rtspc->buf, MAX_RTP_BUFFERSIZE);
127 if(Curl_conn_meta_set(conn, CURL_META_RTSP_CONN, rtspc, rtsp_conn_dtor))
128 return CURLE_OUT_OF_MEMORY;
130 rtsp = curlx_calloc(1, sizeof(struct RTSP));
131 if(!rtsp ||
132 Curl_meta_set(data, CURL_META_RTSP_EASY, rtsp, rtsp_easy_dtor))
133 return CURLE_OUT_OF_MEMORY;
135 return CURLE_OK;
136}
138/*
139 * Function to check on various aspects of a connection.
140 */
141static bool rtsp_conn_is_dead(struct Curl_easy *data,
142 struct connectdata *conn)
143{
144 bool input_pending;
145 /* Contrary to default handling, this protocol allows pending
146 * input on an unused connection. */
147 return !Curl_conn_is_alive(data, conn, &input_pending);
148}
150static CURLcode rtsp_connect(struct Curl_easy *data, bool *done)
151{
152 struct rtsp_conn *rtspc =
153 Curl_conn_meta_get(data->conn, CURL_META_RTSP_CONN);
155 if(!rtspc)
156 return CURLE_FAILED_INIT;
158 /* Initialize the CSeq if not already done */
159 if(data->state.rtsp_next_client_CSeq == 0)
160 data->state.rtsp_next_client_CSeq = 1;
161 if(data->state.rtsp_next_server_CSeq == 0)
162 data->state.rtsp_next_server_CSeq = 1;
164 rtspc->rtp_channel = -1;
165 *done = TRUE;
166 return CURLE_OK;
167}
169static CURLcode rtsp_done(struct Curl_easy *data,
170 CURLcode status, bool premature)
171{
172 struct rtsp_conn *rtspc =
173 Curl_conn_meta_get(data->conn, CURL_META_RTSP_CONN);
174 struct RTSP *rtsp = Curl_meta_get(data, CURL_META_RTSP_EASY);
175 CURLcode result;
177 if(!rtspc || !rtsp)
178 return CURLE_FAILED_INIT;
180 /* Bypass HTTP empty-reply checks on receive */
181 if(data->set.rtspreq == RTSPREQ_RECEIVE)
182 premature = TRUE;
184 result = Curl_http_done(data, status, premature);
186 if(!status && !result) {
187 /* Check the sequence numbers */
188 uint32_t CSeq_sent = rtsp->CSeq_sent;
189 uint32_t CSeq_recv = rtsp->CSeq_recv;
190 if((data->set.rtspreq != RTSPREQ_RECEIVE) && (CSeq_sent != CSeq_recv)) {
191 failf(data,
192 "The CSeq of this request %u did not match the response %u",
193 CSeq_sent, CSeq_recv);
194 return CURLE_RTSP_CSEQ_ERROR;
195 }
196 if(data->set.rtspreq == RTSPREQ_RECEIVE && (rtspc->rtp_channel == -1)) {
197 infof(data, "Got an RTP Receive with a CSeq of %u", CSeq_recv);
198 }
199 if(data->set.rtspreq == RTSPREQ_RECEIVE &&
200 data->req.eos_written) {
201 failf(data, "Server prematurely closed the RTSP connection.");
202 return CURLE_RECV_ERROR;
203 }
204 }
206 return result;
207}
209static CURLcode rtsp_setup_body(struct Curl_easy *data,
210 unsigned char rtspreq,
211 struct dynbuf *reqp)
212{
213 CURLcode result;
214 if(rtspreq == RTSPREQ_ANNOUNCE ||
215 rtspreq == RTSPREQ_SET_PARAMETER ||
216 rtspreq == RTSPREQ_GET_PARAMETER) {
217 curl_off_t req_clen; /* request content length */
219 if(data->state.upload) {
220 req_clen = data->state.infilesize;
221 data->state.httpreq = HTTPREQ_PUT;
222 result = Curl_creader_set_fread(data, req_clen);
223 if(result)
224 return result;
225 }
226 else {
227 if(data->set.postfields) {
228 size_t plen = (data->set.postfieldsize >= 0) ?
229 (size_t)data->set.postfieldsize : strlen(data->set.postfields);
230 req_clen = (curl_off_t)plen;
231 result = Curl_creader_set_buf(data, data->set.postfields, plen);
232 }
233 else if(data->state.infilesize >= 0) {
234 req_clen = data->state.infilesize;
235 result = Curl_creader_set_fread(data, req_clen);
236 }
237 else {
238 req_clen = 0;
239 result = Curl_creader_set_null(data);
240 }
241 if(result)
242 return result;
243 }
245 if(req_clen > 0) {
246 if(data->set.crlf) {
247 failf(data, "Cannot do CRLF conversions on upload");
248 return CURLE_UPLOAD_FAILED;
249 }
250 /* As stated in the http comments, it is probably not wise to
251 * actually set a custom Content-Length in the headers */
252 if(!Curl_checkheaders(data, STRCONST("Content-Length"))) {
253 result = curlx_dyn_addf(reqp, "Content-Length: %" FMT_OFF_T "\r\n",
254 req_clen);
255 if(result)
256 return result;
257 }
259 if(rtspreq == RTSPREQ_SET_PARAMETER ||
260 rtspreq == RTSPREQ_GET_PARAMETER) {
261 if(!Curl_checkheaders(data, STRCONST("Content-Type"))) {
262 result = curlx_dyn_addn(reqp, STRCONST("Content-Type: "
263 "text/parameters\r\n"));
264 if(result)
265 return result;
266 }
267 }
269 if(rtspreq == RTSPREQ_ANNOUNCE) {
270 if(!Curl_checkheaders(data, STRCONST("Content-Type"))) {
271 result = curlx_dyn_addn(reqp, STRCONST("Content-Type: "
272 "application/sdp\r\n"));
273 if(result)
274 return result;
275 }
276 }
277 }
278 else if(rtspreq == RTSPREQ_GET_PARAMETER) {
279 /* Check for an empty GET_PARAMETER (heartbeat) request */
280 data->state.httpreq = HTTPREQ_HEAD;
281 data->req.no_body = TRUE;
282 }
283 }
284 else
285 result = Curl_creader_set_null(data);
286 return result;
287}
289struct rtspselect {
290 const char *method;
291 bool no_body;
292};
294static CURLcode pick_method(struct Curl_easy *data,
295 const unsigned char rtspreq,
296 const char **p)
297{
298 static const struct rtspselect req[] = {
299 { "OPTIONS", TRUE },
300 { "DESCRIBE", FALSE },
301 { "ANNOUNCE", TRUE },
302 { "SETUP", TRUE },
303 { "PLAY", TRUE },
304 { "PAUSE", TRUE },
305 { "TEARDOWN", TRUE },
306 { "GET_PARAMETER", FALSE },
307 { "SET_PARAMETER", TRUE },
308 { "RECORD", TRUE },
309 { "", FALSE }, /* RECEIVE: treat interleaved RTP as body */
310 };
311 /* this is verified already in setopt, this is just added precaution */
312 DEBUGASSERT((rtspreq > RTSPREQ_NONE) && (rtspreq < RTSPREQ_LAST));
313 if((rtspreq <= RTSPREQ_NONE) || (rtspreq >= RTSPREQ_LAST))
314 return CURLE_BAD_FUNCTION_ARGUMENT;
315 *p = req[rtspreq - 1].method;
316 data->req.no_body = req[rtspreq - 1].no_body;
317 return CURLE_OK;
318}
320/* Allocate and store a header string. */
321static CURLcode rtsp_header_alloc(const char *header_name,
322 const char *value,
323 char **target)
324{
325 if(!value)
326 return CURLE_OK;
327 curlx_free(*target);
328 *target = curl_maprintf("%s: %s\r\n", header_name, value);
329 if(!*target)
330 return CURLE_OUT_OF_MEMORY;
331 return CURLE_OK;
332}
334struct rtsp_blocks {
335 const char *request;
336 const char *session_id;
337 const char *accept;
338 const char *range;
339 const char *stream_uri;
340 const char *hd_proxy_auth;
341 const char *hd_auth;
342 char *referrer;
343 char *accept_encoding;
344 char *transport;
345 BIT(transport_alloc); /* if 'transport' is allocated */
346};
348static CURLcode rtsp_setup_request(struct Curl_easy *data,
349 struct rtsp_blocks *b,
350 const unsigned char rtspreq)
351{
352 CURLcode result = CURLE_OK;
353 struct connectdata *conn = data->conn;
355 b->session_id = CURL_EASY_STR(data, STRING_RTSP_SESSION_ID);
357 /* Stream URI. Default to server '*' if not specified */
358 if(CURL_EASY_STR(data, STRING_RTSP_STREAM_URI))
359 b->stream_uri = CURL_EASY_STR(data, STRING_RTSP_STREAM_URI);
360 else
361 b->stream_uri = "*";
363 /* Transport Header for SETUP requests */
364 b->transport = Curl_checkheaders(data, STRCONST("Transport"));
365 if(rtspreq == RTSPREQ_SETUP && !b->transport) {
366 /* New Transport: setting? */
367 if(CURL_EASY_STR(data, STRING_RTSP_TRANSPORT)) {
368 result = rtsp_header_alloc(
369 "Transport", CURL_EASY_STR(data, STRING_RTSP_TRANSPORT),
370 &b->transport);
371 if(result)
372 return result;
373 b->transport_alloc = TRUE;
374 }
375 else {
376 failf(data,
377 "Refusing to issue an RTSP SETUP without a Transport: header.");
378 return CURLE_BAD_FUNCTION_ARGUMENT;
379 }
380 }
382 /* Accept Headers for DESCRIBE requests */
383 if(rtspreq == RTSPREQ_DESCRIBE) {
384 /* Accept Header */
385 b->accept = Curl_checkheaders(data, STRCONST("Accept")) ?
386 NULL : "Accept: application/sdp\r\n";
388 /* Accept-Encoding header */
389 if(!Curl_checkheaders(data, STRCONST("Accept-Encoding")) &&
390 CURL_EASY_STR(data, STRING_ENCODING)) {
391 result = rtsp_header_alloc("Accept-Encoding",
392 CURL_EASY_STR(data, STRING_ENCODING),
393 &b->accept_encoding);
394 if(result)
395 return result;
396 }
397 }
399 /* setup the authentication headers */
400 result = Curl_http_output_auth(data, conn, b->request, HTTPREQ_GET,
401 b->stream_uri, NULL, FALSE);
402 if(result)
403 return result;
405#ifndef CURL_DISABLE_PROXY
406 b->hd_proxy_auth = data->req.hd_proxy_auth;
407#endif
408 b->hd_auth = data->req.hd_auth;
410 /* Referrer */
411 if(Curl_bufref_ptr(&data->state.referer) &&
412 !Curl_checkheaders(data, STRCONST("Referer"))) {
413 b->referrer =
414 curl_maprintf("Referer: %s\r\n", Curl_bufref_ptr(&data->state.referer));
415 if(!b->referrer)
416 result = CURLE_OUT_OF_MEMORY;
417 }
419 /*
420 * Range Header
421 * Only applies to PLAY, PAUSE, RECORD
422 *
423 * Go ahead and use the Range stuff supplied for HTTP
424 */
425 if(!result &&
426 data->state.use_range &&
427 ((rtspreq == RTSPREQ_PLAY) ||
428 (rtspreq == RTSPREQ_PAUSE) ||
429 (rtspreq == RTSPREQ_RECORD)) &&
430 /* Check to see if there is a range set in the custom headers */
431 !Curl_checkheaders(data, STRCONST("Range")) && data->state.range) {
432 result = rtsp_header_alloc("Range",
433 data->state.range,
434 &data->state.rangeline);
435 if(!result)
436 b->range = data->state.rangeline;
437 }
438 return result;
439}
441#define HTTPVERSION 11 /* RTSP is close to HTTP/1.1, sort of... */
443static CURLcode rtsp_do(struct Curl_easy *data, bool *done)
444{
445 CURLcode result = CURLE_OK;
446 const unsigned char rtspreq = data->set.rtspreq;
447 const char *str;
448 struct RTSP *rtsp = Curl_meta_get(data, CURL_META_RTSP_EASY);
449 struct dynbuf req_buffer;
450 struct rtsp_blocks block;
451 memset(&block, 0, sizeof(block));
453 *done = TRUE;
454 if(!rtsp)
455 return CURLE_FAILED_INIT;
457 /* Initialize a dynamic send buffer */
458 curlx_dyn_init(&req_buffer, DYN_RTSP_REQ_HEADER);
460 rtsp->CSeq_sent = data->state.rtsp_next_client_CSeq;
461 rtsp->CSeq_recv = 0;
463 /* Setup the 'p_request' pointer to the proper method. */
464 result = pick_method(data, rtspreq, &block.request);
465 if(result)
466 goto out;
468 if(rtspreq == RTSPREQ_RECEIVE) {
469 Curl_xfer_setup_recv(data, FIRSTSOCKET, -1);
470 goto out;
471 }
473 result = rtsp_setup_request(data, &block, rtspreq);
474 if(result)
475 goto out;
477 if(block.session_id && data->state.rtsp_session_origin &&
478 !data->set.allow_auth_to_other_hosts &&
479 !Curl_peer_equal(data->state.origin, data->state.rtsp_session_origin))
480 block.session_id = NULL;
482 /*
483 * Sanity check the custom headers
484 */
485 if(Curl_checkheaders(data, STRCONST("CSeq"))) {
486 failf(data, "CSeq cannot be set as a custom header.");
487 result = CURLE_RTSP_CSEQ_ERROR;
488 goto out;
489 }
490 if(Curl_checkheaders(data, STRCONST("Session"))) {
491 failf(data, "Session ID cannot be set as a custom header.");
492 result = CURLE_BAD_FUNCTION_ARGUMENT;
493 goto out;
494 }
496 result =
497 curlx_dyn_addf(&req_buffer,
498 "%s %s RTSP/1.0\r\n" /* Request Stream-URI RTSP/1.0 */
499 "CSeq: %u\r\n", /* CSeq */
500 block.request, block.stream_uri, rtsp->CSeq_sent);
501 if(result)
502 goto out;
504 /*
505 * Rather than do a normal alloc line, keep the session_id unformatted
506 * to make comparison easier
507 */
508 if(block.session_id) {
509 result = curlx_dyn_addf(&req_buffer, "Session: %s\r\n", block.session_id);
510 if(result)
511 goto out;
512 }
514 /*
515 * Shared HTTP-like options
516 */
517 result = curlx_dyn_addf(&req_buffer,
518 "%s" /* transport */
519 "%s" /* accept */
520 "%s" /* accept-encoding */
521 "%s" /* range */
522 "%s" /* referrer */
523 ,
524 block.transport ? block.transport : "",
525 block.accept ? block.accept : "",
526 block.accept_encoding ? block.accept_encoding : "",
527 block.range ? block.range : "",
528 block.referrer ? block.referrer : "");
530 str = CURL_EASY_STR(data, STRING_USERAGENT);
531 if(!result && str && *str &&
532 !Curl_checkheaders(data, STRCONST("User-Agent")))
533 result = curlx_dyn_addf(&req_buffer,
534 "User-Agent: %s\r\n", str);
536 if(!result)
537 result = curlx_dyn_addf(&req_buffer,
538 "%s" /* hd_proxy_auth */
539 "%s", /* hd_auth */
540 block.hd_proxy_auth ? block.hd_proxy_auth : "",
541 block.hd_auth ? block.hd_auth : "");
542 if(result)
543 goto out;
545 if((rtspreq == RTSPREQ_SETUP) || (rtspreq == RTSPREQ_DESCRIBE)) {
546 result = Curl_add_timecondition(data, &req_buffer);
547 if(result)
548 goto out;
549 }
551 result = Curl_add_custom_headers(data, FALSE, HTTPVERSION, &req_buffer);
552 if(result)
553 goto out;
555 result = rtsp_setup_body(data, rtspreq, &req_buffer);
556 if(result)
557 goto out;
559 /* Finish the request buffer */
560 result = curlx_dyn_addn(&req_buffer, STRCONST("\r\n"));
561 if(result)
562 goto out;
564 Curl_xfer_setup_sendrecv(data, FIRSTSOCKET, -1);
566 /* issue the request */
567 result = Curl_req_send(data, &req_buffer, HTTPVERSION);
568 if(result) {
569 failf(data, "Failed sending RTSP request");
570 goto out;
571 }
573 /* Increment the CSeq on success */
574 data->state.rtsp_next_client_CSeq++;
576 if(data->req.writebytecount) {
577 /* if a request-body has been sent off, we make sure this progress is
578 noted properly */
579 Curl_pgrsSetUploadCounter(data, data->req.writebytecount);
580 result = Curl_pgrsUpdate(data);
581 }
582out:
583 if(block.transport_alloc)
584 curlx_free(block.transport);
585 curlx_free(block.accept_encoding);
586 curlx_free(block.referrer);
587 curlx_dyn_free(&req_buffer);
588 return result;
589}
591/**
592 * write any BODY bytes missing to the client, ignore the rest.
593 */
594static CURLcode rtp_write_body_junk(struct Curl_easy *data,
595 struct rtsp_conn *rtspc,
596 const char *buf,
597 size_t blen)
598{
599 curl_off_t body_remain;
600 bool in_body;
602 in_body = (data->req.headerline && !rtspc->in_header) &&
603 (data->req.size >= 0) &&
604 (data->req.bytecount < data->req.size);
605 body_remain = in_body ? (data->req.size - data->req.bytecount) : 0;
606 DEBUGASSERT(body_remain >= 0);
607 if(body_remain) {
608 if((curl_off_t)blen > body_remain)
609 blen = (size_t)body_remain;
610 return Curl_client_write(data, CLIENTWRITE_BODY, buf, blen);
611 }
612 return CURLE_OK;
613}
615static CURLcode rtp_client_write(struct Curl_easy *data, const char *ptr,
616 size_t len)
617{
618 struct Curl_mapi_guard guard;
619 size_t wrote;
620 curl_write_callback writeit;
621 void *user_ptr;
623 if(len == 0) {
624 failf(data, "Cannot write a 0 size RTP packet.");
625 return CURLE_WRITE_ERROR;
626 }
628 /* If the user has configured CURLOPT_INTERLEAVEFUNCTION then use that
629 function and any configured CURLOPT_INTERLEAVEDATA to write out the RTP
630 data. Otherwise, use the CURLOPT_WRITEFUNCTION with the CURLOPT_WRITEDATA
631 pointer to write out the RTP data. */
632 if(data->set.fwrite_rtp) {
633 writeit = data->set.fwrite_rtp;
634 user_ptr = data->set.rtp_out;
635 }
636 else {
637 writeit = data->set.fwrite_func;
638 user_ptr = data->set.out;
639 }
641 CURL_CBAPI_START(&guard, data, easy_fwrite_rtp);
642 wrote = writeit((char *)CURL_UNCONST(ptr), 1, len, user_ptr);
643 CURL_CBAPI_END(&guard);
645 if(wrote == CURL_WRITEFUNC_PAUSE) {
646 failf(data, "Cannot pause RTP");
647 return CURLE_WRITE_ERROR;
648 }
650 if(wrote != len) {
651 failf(data, "Failed writing RTP data");
652 return CURLE_WRITE_ERROR;
653 }
655 return CURLE_OK;
656}
658static CURLcode rtsp_filter_rtp(struct Curl_easy *data,
659 struct rtsp_conn *rtspc,
660 const char *buf,
661 size_t blen,
662 size_t *pconsumed)
663{
664 CURLcode result = CURLE_OK;
665 size_t skip_len = 0;
667 *pconsumed = 0;
668 while(blen) {
669 bool in_body = (data->req.headerline && !rtspc->in_header) &&
670 (data->req.size >= 0) &&
671 (data->req.bytecount < data->req.size);
672 switch(rtspc->state) {
674 case RTP_PARSE_SKIP: {
675 DEBUGASSERT(curlx_dyn_len(&rtspc->buf) == 0);
676 while(blen && buf[0] != '$') {
677 if(!in_body && buf[0] == 'R' &&
678 data->set.rtspreq != RTSPREQ_RECEIVE) {
679 if(!strncmp(buf, "RTSP/", (blen < 5) ? blen : 5)) {
680 /* This could be the next response, no consume and return */
681 if(*pconsumed) {
682 DEBUGF(infof(data, "RTP rtsp_filter_rtp[SKIP] RTSP/ prefix, "
683 "skipping %zu bytes of junk", *pconsumed));
684 }
685 rtspc->state = RTP_PARSE_SKIP;
686 rtspc->in_header = TRUE;
687 goto out;
688 }
689 }
690 /* junk/BODY, consume without buffering */
691 *pconsumed += 1;
692 ++buf;
693 --blen;
694 ++skip_len;
695 }
696 if(blen && buf[0] == '$') {
697 /* possible start of an RTP message, buffer */
698 if(skip_len) {
699 /* end of junk/BODY bytes, flush */
700 result = rtp_write_body_junk(data, rtspc, buf - skip_len, skip_len);
701 skip_len = 0;
702 if(result)
703 goto out;
704 }
705 if(curlx_dyn_addn(&rtspc->buf, buf, 1)) {
706 result = CURLE_OUT_OF_MEMORY;
707 goto out;
708 }
709 *pconsumed += 1;
710 ++buf;
711 --blen;
712 rtspc->state = RTP_PARSE_CHANNEL;
713 }
714 break;
715 }
717 case RTP_PARSE_CHANNEL: {
718 int idx = ((unsigned char)buf[0]) / 8;
719 int off = ((unsigned char)buf[0]) % 8;
720 DEBUGASSERT(curlx_dyn_len(&rtspc->buf) == 1);
721 if(!(data->state.rtp_channel_mask[idx] & (1 << off))) {
722 /* invalid channel number, junk or BODY data */
723 rtspc->state = RTP_PARSE_SKIP;
724 DEBUGASSERT(skip_len == 0);
725 /* we do not consume this byte, it is BODY data */
726 DEBUGF(infof(data, "RTSP: invalid RTP channel %d, skipping", idx));
727 if(*pconsumed == 0) {
728 /* We did not consume the initial '$' in our buffer, but had
729 * it from an earlier call. We cannot un-consume it and have
730 * to write it directly as BODY data */
731 result = rtp_write_body_junk(data, rtspc,
732 curlx_dyn_ptr(&rtspc->buf), 1);
733 if(result)
734 goto out;
735 }
736 else {
737 /* count the '$' as skip and continue */
738 skip_len = 1;
739 }
740 curlx_dyn_free(&rtspc->buf);
741 break;
742 }
743 /* a valid channel, so we expect this to be a real RTP message */
744 rtspc->rtp_channel = (unsigned char)buf[0];
745 if(curlx_dyn_addn(&rtspc->buf, buf, 1)) {
746 result = CURLE_OUT_OF_MEMORY;
747 goto out;
748 }
749 *pconsumed += 1;
750 ++buf;
751 --blen;
752 rtspc->state = RTP_PARSE_LEN;
753 break;
754 }
756 case RTP_PARSE_LEN: {
757 size_t rtp_len = curlx_dyn_len(&rtspc->buf);
758 const char *rtp_buf;
759 DEBUGASSERT(rtp_len >= 2 && rtp_len < 4);
760 if(curlx_dyn_addn(&rtspc->buf, buf, 1)) {
761 result = CURLE_OUT_OF_MEMORY;
762 goto out;
763 }
764 *pconsumed += 1;
765 ++buf;
766 --blen;
767 if(rtp_len == 2)
768 break;
769 rtp_buf = curlx_dyn_ptr(&rtspc->buf);
770 rtspc->rtp_len = RTP_PKT_LENGTH(rtp_buf) + 4;
771 if(rtspc->rtp_len == 4) {
772 /* zero-length payload, the 4-byte header is the complete RTP
773 message. Dispatch immediately without entering RTP_PARSE_DATA. */
774 DEBUGF(infof(data, "RTP write channel %d rtp_len %zu (no payload)",
775 rtspc->rtp_channel, rtspc->rtp_len));
776 result = rtp_client_write(data, rtp_buf, rtspc->rtp_len);
777 curlx_dyn_free(&rtspc->buf);
778 rtspc->state = RTP_PARSE_SKIP;
779 if(result)
780 goto out;
781 break;
782 }
783 rtspc->state = RTP_PARSE_DATA;
784 break;
785 }
787 case RTP_PARSE_DATA: {
788 size_t rtp_len = curlx_dyn_len(&rtspc->buf);
789 size_t needed;
790 DEBUGASSERT(rtp_len < rtspc->rtp_len);
791 needed = rtspc->rtp_len - rtp_len;
792 if(needed <= blen) {
793 if(curlx_dyn_addn(&rtspc->buf, buf, needed)) {
794 result = CURLE_OUT_OF_MEMORY;
795 goto out;
796 }
797 *pconsumed += needed;
798 buf += needed;
799 blen -= needed;
800 /* complete RTP message in buffer */
801 DEBUGF(infof(data, "RTP write channel %d rtp_len %zu",
802 rtspc->rtp_channel, rtspc->rtp_len));
803 result = rtp_client_write(data, curlx_dyn_ptr(&rtspc->buf),
804 rtspc->rtp_len);
805 curlx_dyn_free(&rtspc->buf);
806 rtspc->state = RTP_PARSE_SKIP;
807 if(result)
808 goto out;
809 }
810 else {
811 if(curlx_dyn_addn(&rtspc->buf, buf, blen)) {
812 result = CURLE_OUT_OF_MEMORY;
813 goto out;
814 }
815 *pconsumed += blen;
816 buf += blen;
817 blen = 0;
818 }
819 break;
820 }
822 default:
823 DEBUGASSERT(0);
824 return CURLE_RECV_ERROR;
825 }
826 }
827out:
828 if(!result && skip_len)
829 result = rtp_write_body_junk(data, rtspc, buf - skip_len, skip_len);
830 return result;
831}
833/*
834 * Parse and write out an RTSP response.
835 * @param data the transfer
836 * @param conn the connection
837 * @param buf data read from connection
838 * @param blen amount of data in buf
839 * @param is_eos TRUE iff this is the last write
840 * @param readmore out, TRUE iff complete buf was consumed and more data
841 * is needed
842 */
843static CURLcode rtsp_rtp_write_resp(struct Curl_easy *data,
844 const char *buf,
845 size_t blen,
846 bool is_eos)
847{
848 struct rtsp_conn *rtspc =
849 Curl_conn_meta_get(data->conn, CURL_META_RTSP_CONN);
850 CURLcode result = CURLE_OK;
851 size_t consumed = 0;
853 if(!rtspc)
854 return CURLE_FAILED_INIT;
856 if(!data->req.header)
857 rtspc->in_header = FALSE;
858 if(!blen) {
859 goto out;
860 }
862 DEBUGF(infof(data, "rtsp_rtp_write_resp(len=%zu, in_header=%d, eos=%d)",
863 blen, rtspc->in_header, is_eos));
865 /* If header parsing is not ongoing, extract RTP messages */
866 if(!rtspc->in_header) {
867 result = rtsp_filter_rtp(data, rtspc, buf, blen, &consumed);
868 if(result)
869 goto out;
870 buf += consumed;
871 blen -= consumed;
872 /* either we consumed all or are at the start of header parsing */
873 if(blen && !data->req.header)
874 DEBUGF(infof(data, "RTSP: %zu bytes, possibly excess in response body",
875 blen));
876 }
878 /* we want to parse headers, do so */
879 if(data->req.header && blen) {
880 rtspc->in_header = TRUE;
881 result = Curl_http_write_resp_hds(data, buf, blen, &consumed);
882 if(result)
883 goto out;
885 buf += consumed;
886 blen -= consumed;
888 if(!data->req.header)
889 rtspc->in_header = FALSE;
891 if(!rtspc->in_header) {
892 /* If header parsing is done, extract interleaved RTP messages */
893 if(data->req.size <= -1) {
894 /* Respect section 4.4 of rfc2326: If the Content-Length header is
895 absent, a length 0 must be assumed. */
896 data->req.size = 0;
897 data->req.download_done = TRUE;
898 }
899 result = rtsp_filter_rtp(data, rtspc, buf, blen, &consumed);
900 if(result)
901 goto out;
902 buf += consumed;
903 blen -= consumed;
904 }
905 }
907 if(rtspc->state != RTP_PARSE_SKIP)
908 data->req.done = FALSE;
909 /* we SHOULD have consumed all bytes, unless the response is borked.
910 * In which case we write out the left over bytes, letting the client
911 * writer deal with it (it will report EXCESS and fail the transfer). */
912 DEBUGF(infof(data, "rtsp_rtp_write_resp(len=%zu, in_header=%d, done=%d, "
913 "rtspc->state=%d, req.size=%" FMT_OFF_T ")",
914 blen, rtspc->in_header, data->req.done, (int)rtspc->state,
915 data->req.size));
916 if(!result && (is_eos || blen)) {
917 result = Curl_client_write(data, CLIENTWRITE_BODY |
918 (is_eos ? CLIENTWRITE_EOS : 0), buf, blen);
919 }
921out:
922 if((data->set.rtspreq == RTSPREQ_RECEIVE) &&
923 (rtspc->state == RTP_PARSE_SKIP)) {
924 /* In special mode RECEIVE, we process one chunk of network
925 * data, so we stop the transfer here, if we have no incomplete
926 * RTP message pending. */
927 data->req.download_done = TRUE;
928 }
929 return result;
930}
932static CURLcode rtsp_rtp_write_resp_hd(struct Curl_easy *data,
933 const char *buf,
934 size_t blen,
935 bool is_eos)
936{
937 return rtsp_rtp_write_resp(data, buf, blen, is_eos);
938}
940static CURLcode rtsp_parse_transport(struct Curl_easy *data,
941 const char *transport)
942{
943 /* If we receive multiple Transport response-headers, the interleaved
944 channels of each response header is recorded and used together for
945 subsequent data validity checks.*/
946 /* e.g.: ' RTP/AVP/TCP;unicast;interleaved=5-6' */
947 const char *start, *end;
948 start = transport;
949 while(start && *start) {
950 curlx_str_passblanks(&start);
951 end = strchr(start, ';');
952 if(checkprefix("interleaved=", start)) {
953 curl_off_t chan1, chan2, chan;
954 const char *p = start + 12;
955 if(!curlx_str_number(&p, &chan1, 255)) {
956 unsigned char *rtp_channel_mask = data->state.rtp_channel_mask;
957 chan2 = chan1;
958 if(!curlx_str_single(&p, '-') && curlx_str_number(&p, &chan2, 255)) {
959 infof(data, "Unable to read the interleaved parameter from "
960 "Transport header: [%s]", transport);
961 chan2 = chan1;
962 }
963 for(chan = chan1; chan <= chan2; chan++) {
964 int idx = (int)chan / 8;
965 int off = (int)chan % 8;
966 rtp_channel_mask[idx] |= (unsigned char)(1 << off);
967 }
968 }
969 else {
970 infof(data, "Unable to read the interleaved parameter from "
971 "Transport header: [%s]", transport);
972 }
973 break;
974 }
975 /* skip to next parameter */
976 start = (!end) ? end : (end + 1);
977 }
978 return CURLE_OK;
979}
981CURLcode Curl_rtsp_parseheader(struct Curl_easy *data, const char *header)
982{
983 if(checkprefix("CSeq:", header)) {
984 curl_off_t CSeq = 0;
985 struct RTSP *rtsp = Curl_meta_get(data, CURL_META_RTSP_EASY);
986 const char *p = &header[5];
987 if(!rtsp)
988 return CURLE_FAILED_INIT;
989 curlx_str_passblanks(&p);
990 if(curlx_str_number(&p, &CSeq, UINT_MAX)) {
991 failf(data, "Unable to read the CSeq header: [%s]", header);
992 return CURLE_RTSP_CSEQ_ERROR;
993 }
994 data->state.rtsp_CSeq_recv = rtsp->CSeq_recv = (uint32_t)CSeq;
995 }
996 else if(checkprefix("Session:", header)) {
997 const char *start, *end, *str;
998 size_t idlen;
1000 /* Find the first non-space letter */
1001 start = header + 8;
1002 curlx_str_passblanks(&start);
1004 if(!*start) {
1005 failf(data, "Got a blank Session ID");
1006 return CURLE_RTSP_SESSION_ERROR;
1007 }
1009 /* Find the end of Session ID
1010 *
1011 * Allow any non whitespace content, up to the field separator or end of
1012 * line. RFC 2326 is not 100% clear on the session ID and for example
1013 * gstreamer does URL-encoded session ID's not covered by the standard.
1014 */
1015 end = start;
1016 while((*end > ' ') && (*end != ';'))
1017 end++;
1018 idlen = end - start;
1020 str = CURL_EASY_STR(data, STRING_RTSP_SESSION_ID);
1021 if(str) {
1023 /* If the Session ID is set, then compare */
1024 if(strlen(str) != idlen ||
1025 strncmp(start, str, idlen)) {
1026 failf(data, "Got RTSP Session ID Line [%s], but wanted ID [%s]",
1027 start, str);
1028 return CURLE_RTSP_SESSION_ERROR;
1029 }
1030 }
1031 else {
1032 /* If the Session ID is not set, and we find it in a response, then set
1033 * it.
1034 * Copy the id substring into a new buffer */
1035 void *mem = curlx_memdup0(start, idlen);
1036 if(!mem ||
1037 CURL_EASY_STR_SETN(data, STRING_RTSP_SESSION_ID, mem))
1038 return CURLE_OUT_OF_MEMORY;
1039 Curl_peer_link(&data->state.rtsp_session_origin, data->state.origin);
1040 }
1041 }
1042 else if(checkprefix("Transport:", header)) {
1043 CURLcode result;
1044 result = rtsp_parse_transport(data, header + 10);
1045 if(result)
1046 return result;
1047 }
1048 return CURLE_OK;
1049}
1051/*
1052 * RTSP handler interface.
1053 */
1054const struct Curl_protocol Curl_protocol_rtsp = {
1055 rtsp_setup_connection, /* setup_connection */
1056 Curl_http_setup_filters, /* setup_filters */
1057 rtsp_do, /* do_it */
1058 rtsp_done, /* done */
1059 ZERO_NULL, /* do_more */
1060 rtsp_connect, /* connect_it */
1061 ZERO_NULL, /* connecting */
1062 ZERO_NULL, /* doing */
1063 ZERO_NULL, /* proto_pollset */
1064 rtsp_do_pollset, /* doing_pollset */
1065 ZERO_NULL, /* domore_pollset */
1066 Curl_http_perform_pollset, /* perform_pollset */
1067 ZERO_NULL, /* disconnect */
1068 rtsp_rtp_write_resp, /* write_resp */
1069 rtsp_rtp_write_resp_hd, /* write_resp_hd */
1070 rtsp_conn_is_dead, /* connection_is_dead */
1071 ZERO_NULL, /* attach connection */
1072 Curl_http_follow, /* follow */
1073};
1075#endif /* CURL_DISABLE_RTSP */
Diagnostics4 git calls · 49 ms
CallStatusSizeTime049 ms
discover200152 B3 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=3 git.response.bytes=152 git.server.agent=gitty/1.0 git.server.object_format=sha1 git.server.caps=agent ls-refs fetch object-format git.server.fetch=shallow filter sideband-all git.protocol.version=2 git.pkt.frames=8 git.pkt.data=6 git.pkt.flush=2 git.pkt.delim=0 git.pkt.bytes=152 git.pkt.payload.max=34
ls-refs200149 B · hit3 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=3 git.response.bytes=149 git.response.cache=HIT git.response.duration_ms=3 git.protocol.version=2 git.pkt.frames=3 git.pkt.data=2 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=149 git.pkt.payload.max=78 git.ls_refs.count=2 git.ls_refs.head=refs/heads/master
fetchblob:none · deepen 10200287 KiB · hit20 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=3 git.response.bytes=293470 git.response.cache=HIT git.response.duration_ms=20 git.fetch.filter=blob:none git.fetch.filter.requested=tree:0 git.fetch.filter.fallback=blob:none git.fetch.filter.learned=false git.fetch.wants=31c911efe5652725e930f4d2e60c9060e5f6646d git.fetch.haves.count=0 git.fetch.deepen=10 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=10 git.pkt.data=8 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=293470 git.pkt.payload.max=65516 git.sideband.band1.bytes=293354 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=87 git.pack.bytes=293354 git.pack.ofs_delta=0 git.pack.ref_delta=1 git.pack.chain.max=1 git.pack.trailer=6a646bcda6740bab42e321b51998b52ea15146c0 git.pack.full=86Open pack map
fetch2009.8 KiB · hit26 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=24 git.response.bytes=10044 git.response.cache=HIT git.response.duration_ms=26 git.fetch.wants=d7c9719d847d35892331c4f1ff11b01d8b0212a7 git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=3 git.pkt.data=2 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=10044 git.pkt.payload.max=10023 git.sideband.band1.bytes=10022 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0
Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.
- Remote
- https://5ad2236b39de4c8bd8f6270ea63f3ea9.artifacts.cloudflare.net/git/public-v0/curl.git
- Ref
- refs/heads/master
- Commit
- 31c911efe5652725e930f4d2e60c9060e5f6646d
- cf-ray
- a5e26f246d2ef4e0
- Request
- ba563aea…
- Protocol
- v2 · sha1
- Agent
- gitty/1.0
- Filter
- advertised
- Fetch
- shallow filter sideband-all
- Capabilities
- agent ls-refs fetch object-format