rtsp.c 25 KB

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