rtsp.c 25 KB

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