rtsp.c 25 KB

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