rtsp.c 26 KB

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