1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2020, 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.haxx.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 ***************************************************************************/
22
23 #include "curl_setup.h"
24
25 #ifndef CURL_DISABLE_RTSP
26
27 #include "urldata.h"
28 #include <curl/curl.h>
29 #include "transfer.h"
30 #include "sendf.h"
31 #include "multiif.h"
32 #include "http.h"
33 #include "url.h"
34 #include "progress.h"
35 #include "rtsp.h"
36 #include "strcase.h"
37 #include "select.h"
38 #include "connect.h"
39 #include "strdup.h"
40 /* The last 3 #include files should be in this order */
41 #include "curl_printf.h"
42 #include "curl_memory.h"
43 #include "memdebug.h"
44
45 #define RTP_PKT_CHANNEL(p) ((int)((unsigned char)((p)[1])))
46
47 #define RTP_PKT_LENGTH(p) ((((int)((unsigned char)((p)[2]))) << 8) | \
48 ((int)((unsigned char)((p)[3]))))
49
50 /* protocol-specific functions set up to be called by the main engine */
51 static CURLcode rtsp_do(struct connectdata *conn, bool *done);
52 static CURLcode rtsp_done(struct connectdata *conn, CURLcode, bool premature);
53 static CURLcode rtsp_connect(struct connectdata *conn, bool *done);
54 static CURLcode rtsp_disconnect(struct connectdata *conn, bool dead);
55 static int rtsp_getsock_do(struct connectdata *conn, curl_socket_t *socks);
56
57 /*
58 * Parse and write out any available RTP data.
59 *
60 * nread: amount of data left after k->str. will be modified if RTP
61 * data is parsed and k->str is moved up
62 * readmore: whether or not the RTP parser needs more data right away
63 */
64 static CURLcode rtsp_rtp_readwrite(struct Curl_easy *data,
65 struct connectdata *conn,
66 ssize_t *nread,
67 bool *readmore);
68
69 static CURLcode rtsp_setup_connection(struct connectdata *conn);
70 static unsigned int rtsp_conncheck(struct connectdata *check,
71 unsigned int checks_to_perform);
72
73 /* this returns the socket to wait for in the DO and DOING state for the multi
74 interface and then we're always _sending_ a request and thus we wait for
75 the single socket to become writable only */
rtsp_getsock_do(struct connectdata * conn,curl_socket_t * socks)76 static int rtsp_getsock_do(struct connectdata *conn,
77 curl_socket_t *socks)
78 {
79 /* write mode */
80 socks[0] = conn->sock[FIRSTSOCKET];
81 return GETSOCK_WRITESOCK(0);
82 }
83
84 static
85 CURLcode rtp_client_write(struct connectdata *conn, char *ptr, size_t len);
86
87
88 /*
89 * RTSP handler interface.
90 */
91 const struct Curl_handler Curl_handler_rtsp = {
92 "RTSP", /* scheme */
93 rtsp_setup_connection, /* setup_connection */
94 rtsp_do, /* do_it */
95 rtsp_done, /* done */
96 ZERO_NULL, /* do_more */
97 rtsp_connect, /* connect_it */
98 ZERO_NULL, /* connecting */
99 ZERO_NULL, /* doing */
100 ZERO_NULL, /* proto_getsock */
101 rtsp_getsock_do, /* doing_getsock */
102 ZERO_NULL, /* domore_getsock */
103 ZERO_NULL, /* perform_getsock */
104 rtsp_disconnect, /* disconnect */
105 rtsp_rtp_readwrite, /* readwrite */
106 rtsp_conncheck, /* connection_check */
107 PORT_RTSP, /* defport */
108 CURLPROTO_RTSP, /* protocol */
109 CURLPROTO_RTSP, /* family */
110 PROTOPT_NONE /* flags */
111 };
112
113
rtsp_setup_connection(struct connectdata * conn)114 static CURLcode rtsp_setup_connection(struct connectdata *conn)
115 {
116 struct RTSP *rtsp;
117
118 conn->data->req.protop = rtsp = calloc(1, sizeof(struct RTSP));
119 if(!rtsp)
120 return CURLE_OUT_OF_MEMORY;
121
122 return CURLE_OK;
123 }
124
125
126 /*
127 * The server may send us RTP data at any point, and RTSPREQ_RECEIVE does not
128 * want to block the application forever while receiving a stream. Therefore,
129 * we cannot assume that an RTSP socket is dead just because it is readable.
130 *
131 * Instead, if it is readable, run Curl_connalive() to peek at the socket
132 * and distinguish between closed and data.
133 */
rtsp_connisdead(struct connectdata * check)134 static bool rtsp_connisdead(struct connectdata *check)
135 {
136 int sval;
137 bool ret_val = TRUE;
138
139 sval = SOCKET_READABLE(check->sock[FIRSTSOCKET], 0);
140 if(sval == 0) {
141 /* timeout */
142 ret_val = FALSE;
143 }
144 else if(sval & CURL_CSELECT_ERR) {
145 /* socket is in an error state */
146 ret_val = TRUE;
147 }
148 else if(sval & CURL_CSELECT_IN) {
149 /* readable with no error. could still be closed */
150 ret_val = !Curl_connalive(check);
151 }
152
153 return ret_val;
154 }
155
156 /*
157 * Function to check on various aspects of a connection.
158 */
rtsp_conncheck(struct connectdata * check,unsigned int checks_to_perform)159 static unsigned int rtsp_conncheck(struct connectdata *check,
160 unsigned int checks_to_perform)
161 {
162 unsigned int ret_val = CONNRESULT_NONE;
163
164 if(checks_to_perform & CONNCHECK_ISDEAD) {
165 if(rtsp_connisdead(check))
166 ret_val |= CONNRESULT_DEAD;
167 }
168
169 return ret_val;
170 }
171
172
rtsp_connect(struct connectdata * conn,bool * done)173 static CURLcode rtsp_connect(struct connectdata *conn, bool *done)
174 {
175 CURLcode httpStatus;
176 struct Curl_easy *data = conn->data;
177
178 httpStatus = Curl_http_connect(conn, done);
179
180 /* Initialize the CSeq if not already done */
181 if(data->state.rtsp_next_client_CSeq == 0)
182 data->state.rtsp_next_client_CSeq = 1;
183 if(data->state.rtsp_next_server_CSeq == 0)
184 data->state.rtsp_next_server_CSeq = 1;
185
186 conn->proto.rtspc.rtp_channel = -1;
187
188 return httpStatus;
189 }
190
rtsp_disconnect(struct connectdata * conn,bool dead)191 static CURLcode rtsp_disconnect(struct connectdata *conn, bool dead)
192 {
193 (void) dead;
194 Curl_safefree(conn->proto.rtspc.rtp_buf);
195 return CURLE_OK;
196 }
197
198
rtsp_done(struct connectdata * conn,CURLcode status,bool premature)199 static CURLcode rtsp_done(struct connectdata *conn,
200 CURLcode status, bool premature)
201 {
202 struct Curl_easy *data = conn->data;
203 struct RTSP *rtsp = data->req.protop;
204 CURLcode httpStatus;
205
206 /* Bypass HTTP empty-reply checks on receive */
207 if(data->set.rtspreq == RTSPREQ_RECEIVE)
208 premature = TRUE;
209
210 httpStatus = Curl_http_done(conn, status, premature);
211
212 if(rtsp) {
213 /* Check the sequence numbers */
214 long CSeq_sent = rtsp->CSeq_sent;
215 long CSeq_recv = rtsp->CSeq_recv;
216 if((data->set.rtspreq != RTSPREQ_RECEIVE) && (CSeq_sent != CSeq_recv)) {
217 failf(data,
218 "The CSeq of this request %ld did not match the response %ld",
219 CSeq_sent, CSeq_recv);
220 return CURLE_RTSP_CSEQ_ERROR;
221 }
222 if(data->set.rtspreq == RTSPREQ_RECEIVE &&
223 (conn->proto.rtspc.rtp_channel == -1)) {
224 infof(data, "Got an RTP Receive with a CSeq of %ld\n", CSeq_recv);
225 }
226 }
227
228 return httpStatus;
229 }
230
rtsp_do(struct connectdata * conn,bool * done)231 static CURLcode rtsp_do(struct connectdata *conn, bool *done)
232 {
233 struct Curl_easy *data = conn->data;
234 CURLcode result = CURLE_OK;
235 Curl_RtspReq rtspreq = data->set.rtspreq;
236 struct RTSP *rtsp = data->req.protop;
237 struct dynbuf req_buffer;
238 curl_off_t postsize = 0; /* for ANNOUNCE and SET_PARAMETER */
239 curl_off_t putsize = 0; /* for ANNOUNCE and SET_PARAMETER */
240
241 const char *p_request = NULL;
242 const char *p_session_id = NULL;
243 const char *p_accept = NULL;
244 const char *p_accept_encoding = NULL;
245 const char *p_range = NULL;
246 const char *p_referrer = NULL;
247 const char *p_stream_uri = NULL;
248 const char *p_transport = NULL;
249 const char *p_uagent = NULL;
250 const char *p_proxyuserpwd = NULL;
251 const char *p_userpwd = NULL;
252
253 *done = TRUE;
254
255 rtsp->CSeq_sent = data->state.rtsp_next_client_CSeq;
256 rtsp->CSeq_recv = 0;
257
258 /* Setup the 'p_request' pointer to the proper p_request string
259 * Since all RTSP requests are included here, there is no need to
260 * support custom requests like HTTP.
261 **/
262 data->set.opt_no_body = TRUE; /* most requests don't contain a body */
263 switch(rtspreq) {
264 default:
265 failf(data, "Got invalid RTSP request");
266 return CURLE_BAD_FUNCTION_ARGUMENT;
267 case RTSPREQ_OPTIONS:
268 p_request = "OPTIONS";
269 break;
270 case RTSPREQ_DESCRIBE:
271 p_request = "DESCRIBE";
272 data->set.opt_no_body = FALSE;
273 break;
274 case RTSPREQ_ANNOUNCE:
275 p_request = "ANNOUNCE";
276 break;
277 case RTSPREQ_SETUP:
278 p_request = "SETUP";
279 break;
280 case RTSPREQ_PLAY:
281 p_request = "PLAY";
282 break;
283 case RTSPREQ_PAUSE:
284 p_request = "PAUSE";
285 break;
286 case RTSPREQ_TEARDOWN:
287 p_request = "TEARDOWN";
288 break;
289 case RTSPREQ_GET_PARAMETER:
290 /* GET_PARAMETER's no_body status is determined later */
291 p_request = "GET_PARAMETER";
292 data->set.opt_no_body = FALSE;
293 break;
294 case RTSPREQ_SET_PARAMETER:
295 p_request = "SET_PARAMETER";
296 break;
297 case RTSPREQ_RECORD:
298 p_request = "RECORD";
299 break;
300 case RTSPREQ_RECEIVE:
301 p_request = "";
302 /* Treat interleaved RTP as body*/
303 data->set.opt_no_body = FALSE;
304 break;
305 case RTSPREQ_LAST:
306 failf(data, "Got invalid RTSP request: RTSPREQ_LAST");
307 return CURLE_BAD_FUNCTION_ARGUMENT;
308 }
309
310 if(rtspreq == RTSPREQ_RECEIVE) {
311 Curl_setup_transfer(data, FIRSTSOCKET, -1, TRUE, -1);
312
313 return result;
314 }
315
316 p_session_id = data->set.str[STRING_RTSP_SESSION_ID];
317 if(!p_session_id &&
318 (rtspreq & ~(RTSPREQ_OPTIONS | RTSPREQ_DESCRIBE | RTSPREQ_SETUP))) {
319 failf(data, "Refusing to issue an RTSP request [%s] without a session ID.",
320 p_request);
321 return CURLE_BAD_FUNCTION_ARGUMENT;
322 }
323
324 /* Stream URI. Default to server '*' if not specified */
325 if(data->set.str[STRING_RTSP_STREAM_URI]) {
326 p_stream_uri = data->set.str[STRING_RTSP_STREAM_URI];
327 }
328 else {
329 p_stream_uri = "*";
330 }
331
332 /* Transport Header for SETUP requests */
333 p_transport = Curl_checkheaders(conn, "Transport");
334 if(rtspreq == RTSPREQ_SETUP && !p_transport) {
335 /* New Transport: setting? */
336 if(data->set.str[STRING_RTSP_TRANSPORT]) {
337 Curl_safefree(data->state.aptr.rtsp_transport);
338
339 data->state.aptr.rtsp_transport =
340 aprintf("Transport: %s\r\n",
341 data->set.str[STRING_RTSP_TRANSPORT]);
342 if(!data->state.aptr.rtsp_transport)
343 return CURLE_OUT_OF_MEMORY;
344 }
345 else {
346 failf(data,
347 "Refusing to issue an RTSP SETUP without a Transport: header.");
348 return CURLE_BAD_FUNCTION_ARGUMENT;
349 }
350
351 p_transport = data->state.aptr.rtsp_transport;
352 }
353
354 /* Accept Headers for DESCRIBE requests */
355 if(rtspreq == RTSPREQ_DESCRIBE) {
356 /* Accept Header */
357 p_accept = Curl_checkheaders(conn, "Accept")?
358 NULL:"Accept: application/sdp\r\n";
359
360 /* Accept-Encoding header */
361 if(!Curl_checkheaders(conn, "Accept-Encoding") &&
362 data->set.str[STRING_ENCODING]) {
363 Curl_safefree(data->state.aptr.accept_encoding);
364 data->state.aptr.accept_encoding =
365 aprintf("Accept-Encoding: %s\r\n", data->set.str[STRING_ENCODING]);
366
367 if(!data->state.aptr.accept_encoding)
368 return CURLE_OUT_OF_MEMORY;
369
370 p_accept_encoding = data->state.aptr.accept_encoding;
371 }
372 }
373
374 /* The User-Agent string might have been allocated in url.c already, because
375 it might have been used in the proxy connect, but if we have got a header
376 with the user-agent string specified, we erase the previously made string
377 here. */
378 if(Curl_checkheaders(conn, "User-Agent") && data->state.aptr.uagent) {
379 Curl_safefree(data->state.aptr.uagent);
380 data->state.aptr.uagent = NULL;
381 }
382 else if(!Curl_checkheaders(conn, "User-Agent") &&
383 data->set.str[STRING_USERAGENT]) {
384 p_uagent = data->state.aptr.uagent;
385 }
386
387 /* setup the authentication headers */
388 result = Curl_http_output_auth(conn, p_request, p_stream_uri, FALSE);
389 if(result)
390 return result;
391
392 p_proxyuserpwd = data->state.aptr.proxyuserpwd;
393 p_userpwd = data->state.aptr.userpwd;
394
395 /* Referrer */
396 Curl_safefree(data->state.aptr.ref);
397 if(data->change.referer && !Curl_checkheaders(conn, "Referer"))
398 data->state.aptr.ref = aprintf("Referer: %s\r\n", data->change.referer);
399 else
400 data->state.aptr.ref = NULL;
401
402 p_referrer = data->state.aptr.ref;
403
404 /*
405 * Range Header
406 * Only applies to PLAY, PAUSE, RECORD
407 *
408 * Go ahead and use the Range stuff supplied for HTTP
409 */
410 if(data->state.use_range &&
411 (rtspreq & (RTSPREQ_PLAY | RTSPREQ_PAUSE | RTSPREQ_RECORD))) {
412
413 /* Check to see if there is a range set in the custom headers */
414 if(!Curl_checkheaders(conn, "Range") && data->state.range) {
415 Curl_safefree(data->state.aptr.rangeline);
416 data->state.aptr.rangeline = aprintf("Range: %s\r\n", data->state.range);
417 p_range = data->state.aptr.rangeline;
418 }
419 }
420
421 /*
422 * Sanity check the custom headers
423 */
424 if(Curl_checkheaders(conn, "CSeq")) {
425 failf(data, "CSeq cannot be set as a custom header.");
426 return CURLE_RTSP_CSEQ_ERROR;
427 }
428 if(Curl_checkheaders(conn, "Session")) {
429 failf(data, "Session ID cannot be set as a custom header.");
430 return CURLE_BAD_FUNCTION_ARGUMENT;
431 }
432
433 /* Initialize a dynamic send buffer */
434 Curl_dyn_init(&req_buffer, DYN_RTSP_REQ_HEADER);
435
436 result =
437 Curl_dyn_addf(&req_buffer,
438 "%s %s RTSP/1.0\r\n" /* Request Stream-URI RTSP/1.0 */
439 "CSeq: %ld\r\n", /* CSeq */
440 p_request, p_stream_uri, rtsp->CSeq_sent);
441 if(result)
442 return result;
443
444 /*
445 * Rather than do a normal alloc line, keep the session_id unformatted
446 * to make comparison easier
447 */
448 if(p_session_id) {
449 result = Curl_dyn_addf(&req_buffer, "Session: %s\r\n", p_session_id);
450 if(result)
451 return result;
452 }
453
454 /*
455 * Shared HTTP-like options
456 */
457 result = Curl_dyn_addf(&req_buffer,
458 "%s" /* transport */
459 "%s" /* accept */
460 "%s" /* accept-encoding */
461 "%s" /* range */
462 "%s" /* referrer */
463 "%s" /* user-agent */
464 "%s" /* proxyuserpwd */
465 "%s" /* userpwd */
466 ,
467 p_transport ? p_transport : "",
468 p_accept ? p_accept : "",
469 p_accept_encoding ? p_accept_encoding : "",
470 p_range ? p_range : "",
471 p_referrer ? p_referrer : "",
472 p_uagent ? p_uagent : "",
473 p_proxyuserpwd ? p_proxyuserpwd : "",
474 p_userpwd ? p_userpwd : "");
475
476 /*
477 * Free userpwd now --- cannot reuse this for Negotiate and possibly NTLM
478 * with basic and digest, it will be freed anyway by the next request
479 */
480 Curl_safefree(data->state.aptr.userpwd);
481 data->state.aptr.userpwd = NULL;
482
483 if(result)
484 return result;
485
486 if((rtspreq == RTSPREQ_SETUP) || (rtspreq == RTSPREQ_DESCRIBE)) {
487 result = Curl_add_timecondition(conn, &req_buffer);
488 if(result)
489 return result;
490 }
491
492 result = Curl_add_custom_headers(conn, FALSE, &req_buffer);
493 if(result)
494 return result;
495
496 if(rtspreq == RTSPREQ_ANNOUNCE ||
497 rtspreq == RTSPREQ_SET_PARAMETER ||
498 rtspreq == RTSPREQ_GET_PARAMETER) {
499
500 if(data->set.upload) {
501 putsize = data->state.infilesize;
502 data->state.httpreq = HTTPREQ_PUT;
503
504 }
505 else {
506 postsize = (data->state.infilesize != -1)?
507 data->state.infilesize:
508 (data->set.postfields? (curl_off_t)strlen(data->set.postfields):0);
509 data->state.httpreq = HTTPREQ_POST;
510 }
511
512 if(putsize > 0 || postsize > 0) {
513 /* As stated in the http comments, it is probably not wise to
514 * actually set a custom Content-Length in the headers */
515 if(!Curl_checkheaders(conn, "Content-Length")) {
516 result =
517 Curl_dyn_addf(&req_buffer,
518 "Content-Length: %" CURL_FORMAT_CURL_OFF_T"\r\n",
519 (data->set.upload ? putsize : postsize));
520 if(result)
521 return result;
522 }
523
524 if(rtspreq == RTSPREQ_SET_PARAMETER ||
525 rtspreq == RTSPREQ_GET_PARAMETER) {
526 if(!Curl_checkheaders(conn, "Content-Type")) {
527 result = Curl_dyn_addf(&req_buffer,
528 "Content-Type: text/parameters\r\n");
529 if(result)
530 return result;
531 }
532 }
533
534 if(rtspreq == RTSPREQ_ANNOUNCE) {
535 if(!Curl_checkheaders(conn, "Content-Type")) {
536 result = Curl_dyn_addf(&req_buffer,
537 "Content-Type: application/sdp\r\n");
538 if(result)
539 return result;
540 }
541 }
542
543 data->state.expect100header = FALSE; /* RTSP posts are simple/small */
544 }
545 else if(rtspreq == RTSPREQ_GET_PARAMETER) {
546 /* Check for an empty GET_PARAMETER (heartbeat) request */
547 data->state.httpreq = HTTPREQ_HEAD;
548 data->set.opt_no_body = TRUE;
549 }
550 }
551
552 /* RTSP never allows chunked transfer */
553 data->req.forbidchunk = TRUE;
554 /* Finish the request buffer */
555 result = Curl_dyn_add(&req_buffer, "\r\n");
556 if(result)
557 return result;
558
559 if(postsize > 0) {
560 result = Curl_dyn_addn(&req_buffer, data->set.postfields,
561 (size_t)postsize);
562 if(result)
563 return result;
564 }
565
566 /* issue the request */
567 result = Curl_buffer_send(&req_buffer, conn,
568 &data->info.request_size, 0, FIRSTSOCKET);
569 if(result) {
570 failf(data, "Failed sending RTSP request");
571 return result;
572 }
573
574 Curl_setup_transfer(data, FIRSTSOCKET, -1, TRUE, putsize?FIRSTSOCKET:-1);
575
576 /* Increment the CSeq on success */
577 data->state.rtsp_next_client_CSeq++;
578
579 if(data->req.writebytecount) {
580 /* if a request-body has been sent off, we make sure this progress is
581 noted properly */
582 Curl_pgrsSetUploadCounter(data, data->req.writebytecount);
583 if(Curl_pgrsUpdate(conn))
584 result = CURLE_ABORTED_BY_CALLBACK;
585 }
586
587 return result;
588 }
589
590
rtsp_rtp_readwrite(struct Curl_easy * data,struct connectdata * conn,ssize_t * nread,bool * readmore)591 static CURLcode rtsp_rtp_readwrite(struct Curl_easy *data,
592 struct connectdata *conn,
593 ssize_t *nread,
594 bool *readmore) {
595 struct SingleRequest *k = &data->req;
596 struct rtsp_conn *rtspc = &(conn->proto.rtspc);
597
598 char *rtp; /* moving pointer to rtp data */
599 ssize_t rtp_dataleft; /* how much data left to parse in this round */
600 char *scratch;
601 CURLcode result;
602
603 if(rtspc->rtp_buf) {
604 /* There was some leftover data the last time. Merge buffers */
605 char *newptr = Curl_saferealloc(rtspc->rtp_buf,
606 rtspc->rtp_bufsize + *nread);
607 if(!newptr) {
608 rtspc->rtp_buf = NULL;
609 rtspc->rtp_bufsize = 0;
610 return CURLE_OUT_OF_MEMORY;
611 }
612 rtspc->rtp_buf = newptr;
613 memcpy(rtspc->rtp_buf + rtspc->rtp_bufsize, k->str, *nread);
614 rtspc->rtp_bufsize += *nread;
615 rtp = rtspc->rtp_buf;
616 rtp_dataleft = rtspc->rtp_bufsize;
617 }
618 else {
619 /* Just parse the request buffer directly */
620 rtp = k->str;
621 rtp_dataleft = *nread;
622 }
623
624 while((rtp_dataleft > 0) &&
625 (rtp[0] == '$')) {
626 if(rtp_dataleft > 4) {
627 int rtp_length;
628
629 /* Parse the header */
630 /* The channel identifier immediately follows and is 1 byte */
631 rtspc->rtp_channel = RTP_PKT_CHANNEL(rtp);
632
633 /* The length is two bytes */
634 rtp_length = RTP_PKT_LENGTH(rtp);
635
636 if(rtp_dataleft < rtp_length + 4) {
637 /* Need more - incomplete payload*/
638 *readmore = TRUE;
639 break;
640 }
641 /* We have the full RTP interleaved packet
642 * Write out the header including the leading '$' */
643 DEBUGF(infof(data, "RTP write channel %d rtp_length %d\n",
644 rtspc->rtp_channel, rtp_length));
645 result = rtp_client_write(conn, &rtp[0], rtp_length + 4);
646 if(result) {
647 failf(data, "Got an error writing an RTP packet");
648 *readmore = FALSE;
649 Curl_safefree(rtspc->rtp_buf);
650 rtspc->rtp_buf = NULL;
651 rtspc->rtp_bufsize = 0;
652 return result;
653 }
654
655 /* Move forward in the buffer */
656 rtp_dataleft -= rtp_length + 4;
657 rtp += rtp_length + 4;
658
659 if(data->set.rtspreq == RTSPREQ_RECEIVE) {
660 /* If we are in a passive receive, give control back
661 * to the app as often as we can.
662 */
663 k->keepon &= ~KEEP_RECV;
664 }
665 }
666 else {
667 /* Need more - incomplete header */
668 *readmore = TRUE;
669 break;
670 }
671 }
672
673 if(rtp_dataleft != 0 && rtp[0] == '$') {
674 DEBUGF(infof(data, "RTP Rewinding %zd %s\n", rtp_dataleft,
675 *readmore ? "(READMORE)" : ""));
676
677 /* Store the incomplete RTP packet for a "rewind" */
678 scratch = malloc(rtp_dataleft);
679 if(!scratch) {
680 Curl_safefree(rtspc->rtp_buf);
681 rtspc->rtp_buf = NULL;
682 rtspc->rtp_bufsize = 0;
683 return CURLE_OUT_OF_MEMORY;
684 }
685 memcpy(scratch, rtp, rtp_dataleft);
686 Curl_safefree(rtspc->rtp_buf);
687 rtspc->rtp_buf = scratch;
688 rtspc->rtp_bufsize = rtp_dataleft;
689
690 /* As far as the transfer is concerned, this data is consumed */
691 *nread = 0;
692 return CURLE_OK;
693 }
694 /* Fix up k->str to point just after the last RTP packet */
695 k->str += *nread - rtp_dataleft;
696
697 /* either all of the data has been read or...
698 * rtp now points at the next byte to parse
699 */
700 if(rtp_dataleft > 0)
701 DEBUGASSERT(k->str[0] == rtp[0]);
702
703 DEBUGASSERT(rtp_dataleft <= *nread); /* sanity check */
704
705 *nread = rtp_dataleft;
706
707 /* If we get here, we have finished with the leftover/merge buffer */
708 Curl_safefree(rtspc->rtp_buf);
709 rtspc->rtp_buf = NULL;
710 rtspc->rtp_bufsize = 0;
711
712 return CURLE_OK;
713 }
714
715 static
rtp_client_write(struct connectdata * conn,char * ptr,size_t len)716 CURLcode rtp_client_write(struct connectdata *conn, char *ptr, size_t len)
717 {
718 struct Curl_easy *data = conn->data;
719 size_t wrote;
720 curl_write_callback writeit;
721 void *user_ptr;
722
723 if(len == 0) {
724 failf(data, "Cannot write a 0 size RTP packet.");
725 return CURLE_WRITE_ERROR;
726 }
727
728 /* If the user has configured CURLOPT_INTERLEAVEFUNCTION then use that
729 function and any configured CURLOPT_INTERLEAVEDATA to write out the RTP
730 data. Otherwise, use the CURLOPT_WRITEFUNCTION with the CURLOPT_WRITEDATA
731 pointer to write out the RTP data. */
732 if(data->set.fwrite_rtp) {
733 writeit = data->set.fwrite_rtp;
734 user_ptr = data->set.rtp_out;
735 }
736 else {
737 writeit = data->set.fwrite_func;
738 user_ptr = data->set.out;
739 }
740
741 Curl_set_in_callback(data, true);
742 wrote = writeit(ptr, 1, len, user_ptr);
743 Curl_set_in_callback(data, false);
744
745 if(CURL_WRITEFUNC_PAUSE == wrote) {
746 failf(data, "Cannot pause RTP");
747 return CURLE_WRITE_ERROR;
748 }
749
750 if(wrote != len) {
751 failf(data, "Failed writing RTP data");
752 return CURLE_WRITE_ERROR;
753 }
754
755 return CURLE_OK;
756 }
757
Curl_rtsp_parseheader(struct connectdata * conn,char * header)758 CURLcode Curl_rtsp_parseheader(struct connectdata *conn,
759 char *header)
760 {
761 struct Curl_easy *data = conn->data;
762 long CSeq = 0;
763
764 if(checkprefix("CSeq:", header)) {
765 /* Store the received CSeq. Match is verified in rtsp_done */
766 int nc = sscanf(&header[4], ": %ld", &CSeq);
767 if(nc == 1) {
768 struct RTSP *rtsp = data->req.protop;
769 rtsp->CSeq_recv = CSeq; /* mark the request */
770 data->state.rtsp_CSeq_recv = CSeq; /* update the handle */
771 }
772 else {
773 failf(data, "Unable to read the CSeq header: [%s]", header);
774 return CURLE_RTSP_CSEQ_ERROR;
775 }
776 }
777 else if(checkprefix("Session:", header)) {
778 char *start;
779
780 /* Find the first non-space letter */
781 start = header + 8;
782 while(*start && ISSPACE(*start))
783 start++;
784
785 if(!*start) {
786 failf(data, "Got a blank Session ID");
787 }
788 else if(data->set.str[STRING_RTSP_SESSION_ID]) {
789 /* If the Session ID is set, then compare */
790 if(strncmp(start, data->set.str[STRING_RTSP_SESSION_ID],
791 strlen(data->set.str[STRING_RTSP_SESSION_ID])) != 0) {
792 failf(data, "Got RTSP Session ID Line [%s], but wanted ID [%s]",
793 start, data->set.str[STRING_RTSP_SESSION_ID]);
794 return CURLE_RTSP_SESSION_ERROR;
795 }
796 }
797 else {
798 /* If the Session ID is not set, and we find it in a response, then set
799 * it.
800 *
801 * Allow any non whitespace content, up to the field separator or end of
802 * line. RFC 2326 isn't 100% clear on the session ID and for example
803 * gstreamer does url-encoded session ID's not covered by the standard.
804 */
805 char *end = start;
806 while(*end && *end != ';' && !ISSPACE(*end))
807 end++;
808
809 /* Copy the id substring into a new buffer */
810 data->set.str[STRING_RTSP_SESSION_ID] = malloc(end - start + 1);
811 if(data->set.str[STRING_RTSP_SESSION_ID] == NULL)
812 return CURLE_OUT_OF_MEMORY;
813 memcpy(data->set.str[STRING_RTSP_SESSION_ID], start, end - start);
814 (data->set.str[STRING_RTSP_SESSION_ID])[end - start] = '\0';
815 }
816 }
817 return CURLE_OK;
818 }
819
820 #endif /* CURL_DISABLE_RTSP */
821