rtsp.c 26 KB

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