rtsp.c 25 KB

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