rtsp.c 25 KB

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