rtsp.c 25 KB

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