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