rtsp.c 25 KB

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