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