rtsp.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 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.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "curl_setup.h"
  25. #ifndef CURL_DISABLE_RTSP
  26. #include "urldata.h"
  27. #include <curl/curl.h>
  28. #include "transfer.h"
  29. #include "sendf.h"
  30. #include "multiif.h"
  31. #include "http.h"
  32. #include "url.h"
  33. #include "progress.h"
  34. #include "rtsp.h"
  35. #include "strcase.h"
  36. #include "select.h"
  37. #include "connect.h"
  38. #include "cfilters.h"
  39. #include "strdup.h"
  40. #include "curlx/strparse.h"
  41. /* The last 2 #include files should be in this order */
  42. #include "curl_memory.h"
  43. #include "memdebug.h"
  44. /* meta key for storing protocol meta at easy handle */
  45. #define CURL_META_RTSP_EASY "meta:proto:rtsp:easy"
  46. /* meta key for storing protocol meta at connection */
  47. #define CURL_META_RTSP_CONN "meta:proto:rtsp:conn"
  48. typedef enum {
  49. RTP_PARSE_SKIP,
  50. RTP_PARSE_CHANNEL,
  51. RTP_PARSE_LEN,
  52. RTP_PARSE_DATA
  53. } rtp_parse_st;
  54. /* RTSP Connection data
  55. * Currently, only used for tracking incomplete RTP data reads */
  56. struct rtsp_conn {
  57. struct dynbuf buf;
  58. int rtp_channel;
  59. size_t rtp_len;
  60. rtp_parse_st state;
  61. BIT(in_header);
  62. };
  63. /* RTSP transfer data */
  64. struct RTSP {
  65. long CSeq_sent; /* CSeq of this request */
  66. long CSeq_recv; /* CSeq received */
  67. };
  68. #define RTP_PKT_LENGTH(p) ((((unsigned int)((unsigned char)((p)[2]))) << 8) | \
  69. ((unsigned int)((unsigned char)((p)[3]))))
  70. /* protocol-specific functions set up to be called by the main engine */
  71. static CURLcode rtsp_do(struct Curl_easy *data, bool *done);
  72. static CURLcode rtsp_done(struct Curl_easy *data, CURLcode, bool premature);
  73. static CURLcode rtsp_connect(struct Curl_easy *data, bool *done);
  74. static CURLcode rtsp_do_pollset(struct Curl_easy *data,
  75. struct easy_pollset *ps);
  76. /*
  77. * Parse and write out an RTSP response.
  78. * @param data the transfer
  79. * @param conn the connection
  80. * @param buf data read from connection
  81. * @param blen amount of data in buf
  82. * @param is_eos TRUE iff this is the last write
  83. * @param readmore out, TRUE iff complete buf was consumed and more data
  84. * is needed
  85. */
  86. static CURLcode rtsp_rtp_write_resp(struct Curl_easy *data,
  87. const char *buf,
  88. size_t blen,
  89. bool is_eos);
  90. static CURLcode rtsp_rtp_write_resp_hd(struct Curl_easy *data,
  91. const char *buf,
  92. size_t blen,
  93. bool is_eos);
  94. static CURLcode rtsp_setup_connection(struct Curl_easy *data,
  95. struct connectdata *conn);
  96. static unsigned int rtsp_conncheck(struct Curl_easy *data,
  97. struct connectdata *check,
  98. unsigned int checks_to_perform);
  99. /* this returns the socket to wait for in the DO and DOING state for the multi
  100. interface and then we are always _sending_ a request and thus we wait for
  101. the single socket to become writable only */
  102. static CURLcode rtsp_do_pollset(struct Curl_easy *data,
  103. struct easy_pollset *ps)
  104. {
  105. /* write mode */
  106. return Curl_pollset_add_out(data, ps, data->conn->sock[FIRSTSOCKET]);
  107. }
  108. static
  109. CURLcode rtp_client_write(struct Curl_easy *data, const char *ptr, size_t len);
  110. static
  111. CURLcode rtsp_parse_transport(struct Curl_easy *data, const char *transport);
  112. /*
  113. * RTSP handler interface.
  114. */
  115. const struct Curl_handler Curl_handler_rtsp = {
  116. "rtsp", /* scheme */
  117. rtsp_setup_connection, /* setup_connection */
  118. rtsp_do, /* do_it */
  119. rtsp_done, /* done */
  120. ZERO_NULL, /* do_more */
  121. rtsp_connect, /* connect_it */
  122. ZERO_NULL, /* connecting */
  123. ZERO_NULL, /* doing */
  124. ZERO_NULL, /* proto_pollset */
  125. rtsp_do_pollset, /* doing_pollset */
  126. ZERO_NULL, /* domore_pollset */
  127. ZERO_NULL, /* perform_pollset */
  128. ZERO_NULL, /* disconnect */
  129. rtsp_rtp_write_resp, /* write_resp */
  130. rtsp_rtp_write_resp_hd, /* write_resp_hd */
  131. rtsp_conncheck, /* connection_check */
  132. ZERO_NULL, /* attach connection */
  133. Curl_http_follow, /* follow */
  134. PORT_RTSP, /* defport */
  135. CURLPROTO_RTSP, /* protocol */
  136. CURLPROTO_RTSP, /* family */
  137. PROTOPT_NONE /* flags */
  138. };
  139. #define MAX_RTP_BUFFERSIZE 1000000 /* arbitrary */
  140. static void rtsp_easy_dtor(void *key, size_t klen, void *entry)
  141. {
  142. struct RTSP *rtsp = entry;
  143. (void)key;
  144. (void)klen;
  145. free(rtsp);
  146. }
  147. static void rtsp_conn_dtor(void *key, size_t klen, void *entry)
  148. {
  149. struct rtsp_conn *rtspc = entry;
  150. (void)key;
  151. (void)klen;
  152. curlx_dyn_free(&rtspc->buf);
  153. free(rtspc);
  154. }
  155. static CURLcode rtsp_setup_connection(struct Curl_easy *data,
  156. struct connectdata *conn)
  157. {
  158. struct rtsp_conn *rtspc;
  159. struct RTSP *rtsp;
  160. rtspc = calloc(1, sizeof(*rtspc));
  161. if(!rtspc)
  162. return CURLE_OUT_OF_MEMORY;
  163. curlx_dyn_init(&rtspc->buf, MAX_RTP_BUFFERSIZE);
  164. if(Curl_conn_meta_set(conn, CURL_META_RTSP_CONN, rtspc, rtsp_conn_dtor))
  165. return CURLE_OUT_OF_MEMORY;
  166. rtsp = calloc(1, sizeof(struct RTSP));
  167. if(!rtsp ||
  168. Curl_meta_set(data, CURL_META_RTSP_EASY, rtsp, rtsp_easy_dtor))
  169. return CURLE_OUT_OF_MEMORY;
  170. return CURLE_OK;
  171. }
  172. /*
  173. * Function to check on various aspects of a connection.
  174. */
  175. static unsigned int rtsp_conncheck(struct Curl_easy *data,
  176. struct connectdata *conn,
  177. unsigned int checks_to_perform)
  178. {
  179. unsigned int ret_val = CONNRESULT_NONE;
  180. (void)data;
  181. if(checks_to_perform & CONNCHECK_ISDEAD) {
  182. bool input_pending;
  183. if(!Curl_conn_is_alive(data, conn, &input_pending))
  184. ret_val |= CONNRESULT_DEAD;
  185. }
  186. return ret_val;
  187. }
  188. static CURLcode rtsp_connect(struct Curl_easy *data, bool *done)
  189. {
  190. struct rtsp_conn *rtspc =
  191. Curl_conn_meta_get(data->conn, CURL_META_RTSP_CONN);
  192. CURLcode httpStatus;
  193. if(!rtspc)
  194. return CURLE_FAILED_INIT;
  195. httpStatus = Curl_http_connect(data, done);
  196. /* Initialize the CSeq if not already done */
  197. if(data->state.rtsp_next_client_CSeq == 0)
  198. data->state.rtsp_next_client_CSeq = 1;
  199. if(data->state.rtsp_next_server_CSeq == 0)
  200. data->state.rtsp_next_server_CSeq = 1;
  201. rtspc->rtp_channel = -1;
  202. return httpStatus;
  203. }
  204. static CURLcode rtsp_done(struct Curl_easy *data,
  205. CURLcode status, bool premature)
  206. {
  207. struct rtsp_conn *rtspc =
  208. Curl_conn_meta_get(data->conn, CURL_META_RTSP_CONN);
  209. struct RTSP *rtsp = Curl_meta_get(data, CURL_META_RTSP_EASY);
  210. CURLcode httpStatus;
  211. if(!rtspc || !rtsp)
  212. return CURLE_FAILED_INIT;
  213. /* Bypass HTTP empty-reply checks on receive */
  214. if(data->set.rtspreq == RTSPREQ_RECEIVE)
  215. premature = TRUE;
  216. httpStatus = Curl_http_done(data, status, premature);
  217. if(!status && !httpStatus) {
  218. /* Check the sequence numbers */
  219. long CSeq_sent = rtsp->CSeq_sent;
  220. long CSeq_recv = rtsp->CSeq_recv;
  221. if((data->set.rtspreq != RTSPREQ_RECEIVE) && (CSeq_sent != CSeq_recv)) {
  222. failf(data,
  223. "The CSeq of this request %ld did not match the response %ld",
  224. CSeq_sent, CSeq_recv);
  225. return CURLE_RTSP_CSEQ_ERROR;
  226. }
  227. if(data->set.rtspreq == RTSPREQ_RECEIVE && (rtspc->rtp_channel == -1)) {
  228. infof(data, "Got an RTP Receive with a CSeq of %ld", CSeq_recv);
  229. }
  230. if(data->set.rtspreq == RTSPREQ_RECEIVE &&
  231. data->req.eos_written) {
  232. failf(data, "Server prematurely closed the RTSP connection.");
  233. return CURLE_RECV_ERROR;
  234. }
  235. }
  236. return httpStatus;
  237. }
  238. static CURLcode rtsp_setup_body(struct Curl_easy *data,
  239. Curl_RtspReq rtspreq,
  240. struct dynbuf *reqp)
  241. {
  242. CURLcode result;
  243. if(rtspreq == RTSPREQ_ANNOUNCE ||
  244. rtspreq == RTSPREQ_SET_PARAMETER ||
  245. rtspreq == RTSPREQ_GET_PARAMETER) {
  246. curl_off_t req_clen; /* request content length */
  247. if(data->state.upload) {
  248. req_clen = data->state.infilesize;
  249. data->state.httpreq = HTTPREQ_PUT;
  250. result = Curl_creader_set_fread(data, req_clen);
  251. if(result)
  252. return result;
  253. }
  254. else {
  255. if(data->set.postfields) {
  256. size_t plen = (data->set.postfieldsize >= 0) ?
  257. (size_t)data->set.postfieldsize : strlen(data->set.postfields);
  258. req_clen = (curl_off_t)plen;
  259. result = Curl_creader_set_buf(data, data->set.postfields, plen);
  260. }
  261. else if(data->state.infilesize >= 0) {
  262. req_clen = data->state.infilesize;
  263. result = Curl_creader_set_fread(data, req_clen);
  264. }
  265. else {
  266. req_clen = 0;
  267. result = Curl_creader_set_null(data);
  268. }
  269. if(result)
  270. return result;
  271. }
  272. if(req_clen > 0) {
  273. /* As stated in the http comments, it is probably not wise to
  274. * actually set a custom Content-Length in the headers */
  275. if(!Curl_checkheaders(data, STRCONST("Content-Length"))) {
  276. result = curlx_dyn_addf(reqp, "Content-Length: %" FMT_OFF_T"\r\n",
  277. req_clen);
  278. if(result)
  279. return result;
  280. }
  281. if(rtspreq == RTSPREQ_SET_PARAMETER ||
  282. rtspreq == RTSPREQ_GET_PARAMETER) {
  283. if(!Curl_checkheaders(data, STRCONST("Content-Type"))) {
  284. result = curlx_dyn_addn(reqp,
  285. STRCONST("Content-Type: "
  286. "text/parameters\r\n"));
  287. if(result)
  288. return result;
  289. }
  290. }
  291. if(rtspreq == RTSPREQ_ANNOUNCE) {
  292. if(!Curl_checkheaders(data, STRCONST("Content-Type"))) {
  293. result = curlx_dyn_addn(reqp,
  294. STRCONST("Content-Type: "
  295. "application/sdp\r\n"));
  296. if(result)
  297. return result;
  298. }
  299. }
  300. }
  301. else if(rtspreq == RTSPREQ_GET_PARAMETER) {
  302. /* Check for an empty GET_PARAMETER (heartbeat) request */
  303. data->state.httpreq = HTTPREQ_HEAD;
  304. data->req.no_body = TRUE;
  305. }
  306. }
  307. else
  308. result = Curl_creader_set_null(data);
  309. return result;
  310. }
  311. static CURLcode rtsp_do(struct Curl_easy *data, bool *done)
  312. {
  313. struct connectdata *conn = data->conn;
  314. CURLcode result = CURLE_OK;
  315. Curl_RtspReq rtspreq = data->set.rtspreq;
  316. struct RTSP *rtsp = Curl_meta_get(data, CURL_META_RTSP_EASY);
  317. struct dynbuf req_buffer;
  318. unsigned char httpversion = 11; /* RTSP is close to HTTP/1.1, sort of... */
  319. const char *p_request = NULL;
  320. const char *p_session_id = NULL;
  321. const char *p_accept = NULL;
  322. const char *p_accept_encoding = NULL;
  323. const char *p_range = NULL;
  324. const char *p_referrer = NULL;
  325. const char *p_stream_uri = NULL;
  326. const char *p_transport = NULL;
  327. const char *p_uagent = NULL;
  328. const char *p_proxyuserpwd = NULL;
  329. const char *p_userpwd = NULL;
  330. *done = TRUE;
  331. if(!rtsp)
  332. return CURLE_FAILED_INIT;
  333. /* Initialize a dynamic send buffer */
  334. curlx_dyn_init(&req_buffer, DYN_RTSP_REQ_HEADER);
  335. rtsp->CSeq_sent = data->state.rtsp_next_client_CSeq;
  336. rtsp->CSeq_recv = 0;
  337. /* Setup the first_* fields to allow auth details get sent
  338. to this origin */
  339. if(!data->state.first_host) {
  340. data->state.first_host = strdup(conn->host.name);
  341. if(!data->state.first_host)
  342. return CURLE_OUT_OF_MEMORY;
  343. data->state.first_remote_port = conn->remote_port;
  344. data->state.first_remote_protocol = conn->handler->protocol;
  345. }
  346. /* Setup the 'p_request' pointer to the proper p_request string
  347. * Since all RTSP requests are included here, there is no need to
  348. * support custom requests like HTTP.
  349. **/
  350. data->req.no_body = TRUE; /* most requests do not contain a body */
  351. switch(rtspreq) {
  352. default:
  353. failf(data, "Got invalid RTSP request");
  354. return CURLE_BAD_FUNCTION_ARGUMENT;
  355. case RTSPREQ_OPTIONS:
  356. p_request = "OPTIONS";
  357. break;
  358. case RTSPREQ_DESCRIBE:
  359. p_request = "DESCRIBE";
  360. data->req.no_body = FALSE;
  361. break;
  362. case RTSPREQ_ANNOUNCE:
  363. p_request = "ANNOUNCE";
  364. break;
  365. case RTSPREQ_SETUP:
  366. p_request = "SETUP";
  367. break;
  368. case RTSPREQ_PLAY:
  369. p_request = "PLAY";
  370. break;
  371. case RTSPREQ_PAUSE:
  372. p_request = "PAUSE";
  373. break;
  374. case RTSPREQ_TEARDOWN:
  375. p_request = "TEARDOWN";
  376. break;
  377. case RTSPREQ_GET_PARAMETER:
  378. /* GET_PARAMETER's no_body status is determined later */
  379. p_request = "GET_PARAMETER";
  380. data->req.no_body = FALSE;
  381. break;
  382. case RTSPREQ_SET_PARAMETER:
  383. p_request = "SET_PARAMETER";
  384. break;
  385. case RTSPREQ_RECORD:
  386. p_request = "RECORD";
  387. break;
  388. case RTSPREQ_RECEIVE:
  389. p_request = "";
  390. /* Treat interleaved RTP as body */
  391. data->req.no_body = FALSE;
  392. break;
  393. case RTSPREQ_LAST:
  394. failf(data, "Got invalid RTSP request: RTSPREQ_LAST");
  395. return CURLE_BAD_FUNCTION_ARGUMENT;
  396. }
  397. if(rtspreq == RTSPREQ_RECEIVE) {
  398. Curl_xfer_setup_recv(data, FIRSTSOCKET, -1);
  399. goto out;
  400. }
  401. p_session_id = data->set.str[STRING_RTSP_SESSION_ID];
  402. if(!p_session_id &&
  403. (rtspreq & ~(Curl_RtspReq)(RTSPREQ_OPTIONS |
  404. RTSPREQ_DESCRIBE |
  405. RTSPREQ_SETUP))) {
  406. failf(data, "Refusing to issue an RTSP request [%s] without a session ID.",
  407. p_request);
  408. result = CURLE_BAD_FUNCTION_ARGUMENT;
  409. goto out;
  410. }
  411. /* Stream URI. Default to server '*' if not specified */
  412. if(data->set.str[STRING_RTSP_STREAM_URI]) {
  413. p_stream_uri = data->set.str[STRING_RTSP_STREAM_URI];
  414. }
  415. else {
  416. p_stream_uri = "*";
  417. }
  418. /* Transport Header for SETUP requests */
  419. p_transport = Curl_checkheaders(data, STRCONST("Transport"));
  420. if(rtspreq == RTSPREQ_SETUP && !p_transport) {
  421. /* New Transport: setting? */
  422. if(data->set.str[STRING_RTSP_TRANSPORT]) {
  423. free(data->state.aptr.rtsp_transport);
  424. data->state.aptr.rtsp_transport =
  425. curl_maprintf("Transport: %s\r\n",
  426. data->set.str[STRING_RTSP_TRANSPORT]);
  427. if(!data->state.aptr.rtsp_transport)
  428. return CURLE_OUT_OF_MEMORY;
  429. }
  430. else {
  431. failf(data,
  432. "Refusing to issue an RTSP SETUP without a Transport: header.");
  433. result = CURLE_BAD_FUNCTION_ARGUMENT;
  434. goto out;
  435. }
  436. p_transport = data->state.aptr.rtsp_transport;
  437. }
  438. /* Accept Headers for DESCRIBE requests */
  439. if(rtspreq == RTSPREQ_DESCRIBE) {
  440. /* Accept Header */
  441. p_accept = Curl_checkheaders(data, STRCONST("Accept")) ?
  442. NULL : "Accept: application/sdp\r\n";
  443. /* Accept-Encoding header */
  444. if(!Curl_checkheaders(data, STRCONST("Accept-Encoding")) &&
  445. data->set.str[STRING_ENCODING]) {
  446. free(data->state.aptr.accept_encoding);
  447. data->state.aptr.accept_encoding =
  448. curl_maprintf("Accept-Encoding: %s\r\n",
  449. data->set.str[STRING_ENCODING]);
  450. if(!data->state.aptr.accept_encoding) {
  451. result = CURLE_OUT_OF_MEMORY;
  452. goto out;
  453. }
  454. p_accept_encoding = data->state.aptr.accept_encoding;
  455. }
  456. }
  457. /* The User-Agent string might have been allocated in url.c already, because
  458. it might have been used in the proxy connect, but if we have got a header
  459. with the user-agent string specified, we erase the previously made string
  460. here. */
  461. if(Curl_checkheaders(data, STRCONST("User-Agent")) &&
  462. data->state.aptr.uagent) {
  463. Curl_safefree(data->state.aptr.uagent);
  464. }
  465. else if(!Curl_checkheaders(data, STRCONST("User-Agent")) &&
  466. data->set.str[STRING_USERAGENT]) {
  467. p_uagent = data->state.aptr.uagent;
  468. }
  469. /* setup the authentication headers */
  470. result = Curl_http_output_auth(data, conn, p_request, HTTPREQ_GET,
  471. p_stream_uri, FALSE);
  472. if(result)
  473. goto out;
  474. #ifndef CURL_DISABLE_PROXY
  475. p_proxyuserpwd = data->state.aptr.proxyuserpwd;
  476. #endif
  477. p_userpwd = data->state.aptr.userpwd;
  478. /* Referrer */
  479. Curl_safefree(data->state.aptr.ref);
  480. if(data->state.referer && !Curl_checkheaders(data, STRCONST("Referer")))
  481. data->state.aptr.ref = curl_maprintf("Referer: %s\r\n",
  482. data->state.referer);
  483. p_referrer = data->state.aptr.ref;
  484. /*
  485. * Range Header
  486. * Only applies to PLAY, PAUSE, RECORD
  487. *
  488. * Go ahead and use the Range stuff supplied for HTTP
  489. */
  490. if(data->state.use_range &&
  491. (rtspreq & (RTSPREQ_PLAY | RTSPREQ_PAUSE | RTSPREQ_RECORD))) {
  492. /* Check to see if there is a range set in the custom headers */
  493. if(!Curl_checkheaders(data, STRCONST("Range")) && data->state.range) {
  494. free(data->state.aptr.rangeline);
  495. data->state.aptr.rangeline = curl_maprintf("Range: %s\r\n",
  496. data->state.range);
  497. p_range = data->state.aptr.rangeline;
  498. }
  499. }
  500. /*
  501. * Sanity check the custom headers
  502. */
  503. if(Curl_checkheaders(data, STRCONST("CSeq"))) {
  504. failf(data, "CSeq cannot be set as a custom header.");
  505. result = CURLE_RTSP_CSEQ_ERROR;
  506. goto out;
  507. }
  508. if(Curl_checkheaders(data, STRCONST("Session"))) {
  509. failf(data, "Session ID cannot be set as a custom header.");
  510. result = CURLE_BAD_FUNCTION_ARGUMENT;
  511. goto out;
  512. }
  513. result =
  514. curlx_dyn_addf(&req_buffer,
  515. "%s %s RTSP/1.0\r\n" /* Request Stream-URI RTSP/1.0 */
  516. "CSeq: %ld\r\n", /* CSeq */
  517. p_request, p_stream_uri, rtsp->CSeq_sent);
  518. if(result)
  519. goto out;
  520. /*
  521. * Rather than do a normal alloc line, keep the session_id unformatted
  522. * to make comparison easier
  523. */
  524. if(p_session_id) {
  525. result = curlx_dyn_addf(&req_buffer, "Session: %s\r\n", p_session_id);
  526. if(result)
  527. goto out;
  528. }
  529. /*
  530. * Shared HTTP-like options
  531. */
  532. result = curlx_dyn_addf(&req_buffer,
  533. "%s" /* transport */
  534. "%s" /* accept */
  535. "%s" /* accept-encoding */
  536. "%s" /* range */
  537. "%s" /* referrer */
  538. "%s" /* user-agent */
  539. "%s" /* proxyuserpwd */
  540. "%s" /* userpwd */
  541. ,
  542. p_transport ? p_transport : "",
  543. p_accept ? p_accept : "",
  544. p_accept_encoding ? p_accept_encoding : "",
  545. p_range ? p_range : "",
  546. p_referrer ? p_referrer : "",
  547. p_uagent ? p_uagent : "",
  548. p_proxyuserpwd ? p_proxyuserpwd : "",
  549. p_userpwd ? p_userpwd : "");
  550. /*
  551. * Free userpwd now --- cannot reuse this for Negotiate and possibly NTLM
  552. * with basic and digest, it will be freed anyway by the next request
  553. */
  554. Curl_safefree(data->state.aptr.userpwd);
  555. if(result)
  556. goto out;
  557. if((rtspreq == RTSPREQ_SETUP) || (rtspreq == RTSPREQ_DESCRIBE)) {
  558. result = Curl_add_timecondition(data, &req_buffer);
  559. if(result)
  560. goto out;
  561. }
  562. result = Curl_add_custom_headers(data, FALSE, httpversion, &req_buffer);
  563. if(result)
  564. goto out;
  565. result = rtsp_setup_body(data, rtspreq, &req_buffer);
  566. if(result)
  567. goto out;
  568. /* Finish the request buffer */
  569. result = curlx_dyn_addn(&req_buffer, STRCONST("\r\n"));
  570. if(result)
  571. goto out;
  572. Curl_xfer_setup_sendrecv(data, FIRSTSOCKET, -1);
  573. /* issue the request */
  574. result = Curl_req_send(data, &req_buffer, httpversion);
  575. if(result) {
  576. failf(data, "Failed sending RTSP request");
  577. goto out;
  578. }
  579. /* Increment the CSeq on success */
  580. data->state.rtsp_next_client_CSeq++;
  581. if(data->req.writebytecount) {
  582. /* if a request-body has been sent off, we make sure this progress is
  583. noted properly */
  584. Curl_pgrsSetUploadCounter(data, data->req.writebytecount);
  585. if(Curl_pgrsUpdate(data))
  586. result = CURLE_ABORTED_BY_CALLBACK;
  587. }
  588. out:
  589. curlx_dyn_free(&req_buffer);
  590. return result;
  591. }
  592. /**
  593. * write any BODY bytes missing to the client, ignore the rest.
  594. */
  595. static CURLcode rtp_write_body_junk(struct Curl_easy *data,
  596. struct rtsp_conn *rtspc,
  597. const char *buf,
  598. size_t blen)
  599. {
  600. curl_off_t body_remain;
  601. bool in_body;
  602. in_body = (data->req.headerline && !rtspc->in_header) &&
  603. (data->req.size >= 0) &&
  604. (data->req.bytecount < data->req.size);
  605. body_remain = in_body ? (data->req.size - data->req.bytecount) : 0;
  606. DEBUGASSERT(body_remain >= 0);
  607. if(body_remain) {
  608. if((curl_off_t)blen > body_remain)
  609. blen = (size_t)body_remain;
  610. return Curl_client_write(data, CLIENTWRITE_BODY, buf, blen);
  611. }
  612. return CURLE_OK;
  613. }
  614. static CURLcode rtsp_filter_rtp(struct Curl_easy *data,
  615. struct rtsp_conn *rtspc,
  616. const char *buf,
  617. size_t blen,
  618. size_t *pconsumed)
  619. {
  620. CURLcode result = CURLE_OK;
  621. size_t skip_len = 0;
  622. *pconsumed = 0;
  623. while(blen) {
  624. bool in_body = (data->req.headerline && !rtspc->in_header) &&
  625. (data->req.size >= 0) &&
  626. (data->req.bytecount < data->req.size);
  627. switch(rtspc->state) {
  628. case RTP_PARSE_SKIP: {
  629. DEBUGASSERT(curlx_dyn_len(&rtspc->buf) == 0);
  630. while(blen && buf[0] != '$') {
  631. if(!in_body && buf[0] == 'R' &&
  632. data->set.rtspreq != RTSPREQ_RECEIVE) {
  633. if(strncmp(buf, "RTSP/", (blen < 5) ? blen : 5) == 0) {
  634. /* This could be the next response, no consume and return */
  635. if(*pconsumed) {
  636. DEBUGF(infof(data, "RTP rtsp_filter_rtp[SKIP] RTSP/ prefix, "
  637. "skipping %zd bytes of junk", *pconsumed));
  638. }
  639. rtspc->state = RTP_PARSE_SKIP;
  640. rtspc->in_header = TRUE;
  641. goto out;
  642. }
  643. }
  644. /* junk/BODY, consume without buffering */
  645. *pconsumed += 1;
  646. ++buf;
  647. --blen;
  648. ++skip_len;
  649. }
  650. if(blen && buf[0] == '$') {
  651. /* possible start of an RTP message, buffer */
  652. if(skip_len) {
  653. /* end of junk/BODY bytes, flush */
  654. result = rtp_write_body_junk(data, rtspc, buf - skip_len, skip_len);
  655. skip_len = 0;
  656. if(result)
  657. goto out;
  658. }
  659. if(curlx_dyn_addn(&rtspc->buf, buf, 1)) {
  660. result = CURLE_OUT_OF_MEMORY;
  661. goto out;
  662. }
  663. *pconsumed += 1;
  664. ++buf;
  665. --blen;
  666. rtspc->state = RTP_PARSE_CHANNEL;
  667. }
  668. break;
  669. }
  670. case RTP_PARSE_CHANNEL: {
  671. int idx = ((unsigned char)buf[0]) / 8;
  672. int off = ((unsigned char)buf[0]) % 8;
  673. DEBUGASSERT(curlx_dyn_len(&rtspc->buf) == 1);
  674. if(!(data->state.rtp_channel_mask[idx] & (1 << off))) {
  675. /* invalid channel number, junk or BODY data */
  676. rtspc->state = RTP_PARSE_SKIP;
  677. DEBUGASSERT(skip_len == 0);
  678. /* we do not consume this byte, it is BODY data */
  679. DEBUGF(infof(data, "RTSP: invalid RTP channel %d, skipping", idx));
  680. if(*pconsumed == 0) {
  681. /* We did not consume the initial '$' in our buffer, but had
  682. * it from an earlier call. We cannot un-consume it and have
  683. * to write it directly as BODY data */
  684. result = rtp_write_body_junk(data, rtspc,
  685. curlx_dyn_ptr(&rtspc->buf), 1);
  686. if(result)
  687. goto out;
  688. }
  689. else {
  690. /* count the '$' as skip and continue */
  691. skip_len = 1;
  692. }
  693. curlx_dyn_free(&rtspc->buf);
  694. break;
  695. }
  696. /* a valid channel, so we expect this to be a real RTP message */
  697. rtspc->rtp_channel = (unsigned char)buf[0];
  698. if(curlx_dyn_addn(&rtspc->buf, buf, 1)) {
  699. result = CURLE_OUT_OF_MEMORY;
  700. goto out;
  701. }
  702. *pconsumed += 1;
  703. ++buf;
  704. --blen;
  705. rtspc->state = RTP_PARSE_LEN;
  706. break;
  707. }
  708. case RTP_PARSE_LEN: {
  709. size_t rtp_len = curlx_dyn_len(&rtspc->buf);
  710. const char *rtp_buf;
  711. DEBUGASSERT(rtp_len >= 2 && rtp_len < 4);
  712. if(curlx_dyn_addn(&rtspc->buf, buf, 1)) {
  713. result = CURLE_OUT_OF_MEMORY;
  714. goto out;
  715. }
  716. *pconsumed += 1;
  717. ++buf;
  718. --blen;
  719. if(rtp_len == 2)
  720. break;
  721. rtp_buf = curlx_dyn_ptr(&rtspc->buf);
  722. rtspc->rtp_len = RTP_PKT_LENGTH(rtp_buf) + 4;
  723. rtspc->state = RTP_PARSE_DATA;
  724. break;
  725. }
  726. case RTP_PARSE_DATA: {
  727. size_t rtp_len = curlx_dyn_len(&rtspc->buf);
  728. size_t needed;
  729. DEBUGASSERT(rtp_len < rtspc->rtp_len);
  730. needed = rtspc->rtp_len - rtp_len;
  731. if(needed <= blen) {
  732. if(curlx_dyn_addn(&rtspc->buf, buf, needed)) {
  733. result = CURLE_OUT_OF_MEMORY;
  734. goto out;
  735. }
  736. *pconsumed += needed;
  737. buf += needed;
  738. blen -= needed;
  739. /* complete RTP message in buffer */
  740. DEBUGF(infof(data, "RTP write channel %d rtp_len %zu",
  741. rtspc->rtp_channel, rtspc->rtp_len));
  742. result = rtp_client_write(data, curlx_dyn_ptr(&rtspc->buf),
  743. rtspc->rtp_len);
  744. curlx_dyn_free(&rtspc->buf);
  745. rtspc->state = RTP_PARSE_SKIP;
  746. if(result)
  747. goto out;
  748. }
  749. else {
  750. if(curlx_dyn_addn(&rtspc->buf, buf, blen)) {
  751. result = CURLE_OUT_OF_MEMORY;
  752. goto out;
  753. }
  754. *pconsumed += blen;
  755. buf += blen;
  756. blen = 0;
  757. }
  758. break;
  759. }
  760. default:
  761. DEBUGASSERT(0);
  762. return CURLE_RECV_ERROR;
  763. }
  764. }
  765. out:
  766. if(!result && skip_len)
  767. result = rtp_write_body_junk(data, rtspc, buf - skip_len, skip_len);
  768. return result;
  769. }
  770. static CURLcode rtsp_rtp_write_resp(struct Curl_easy *data,
  771. const char *buf,
  772. size_t blen,
  773. bool is_eos)
  774. {
  775. struct rtsp_conn *rtspc =
  776. Curl_conn_meta_get(data->conn, CURL_META_RTSP_CONN);
  777. CURLcode result = CURLE_OK;
  778. size_t consumed = 0;
  779. if(!rtspc)
  780. return CURLE_FAILED_INIT;
  781. if(!data->req.header)
  782. rtspc->in_header = FALSE;
  783. if(!blen) {
  784. goto out;
  785. }
  786. DEBUGF(infof(data, "rtsp_rtp_write_resp(len=%zu, in_header=%d, eos=%d)",
  787. blen, rtspc->in_header, is_eos));
  788. /* If header parsing is not ongoing, extract RTP messages */
  789. if(!rtspc->in_header) {
  790. result = rtsp_filter_rtp(data, rtspc, buf, blen, &consumed);
  791. if(result)
  792. goto out;
  793. buf += consumed;
  794. blen -= consumed;
  795. /* either we consumed all or are at the start of header parsing */
  796. if(blen && !data->req.header)
  797. DEBUGF(infof(data, "RTSP: %zu bytes, possibly excess in response body",
  798. blen));
  799. }
  800. /* we want to parse headers, do so */
  801. if(data->req.header && blen) {
  802. rtspc->in_header = TRUE;
  803. result = Curl_http_write_resp_hds(data, buf, blen, &consumed);
  804. if(result)
  805. goto out;
  806. buf += consumed;
  807. blen -= consumed;
  808. if(!data->req.header)
  809. rtspc->in_header = FALSE;
  810. if(!rtspc->in_header) {
  811. /* If header parsing is done, extract interleaved RTP messages */
  812. if(data->req.size <= -1) {
  813. /* Respect section 4.4 of rfc2326: If the Content-Length header is
  814. absent, a length 0 must be assumed. */
  815. data->req.size = 0;
  816. data->req.download_done = TRUE;
  817. }
  818. result = rtsp_filter_rtp(data, rtspc, buf, blen, &consumed);
  819. if(result)
  820. goto out;
  821. blen -= consumed;
  822. }
  823. }
  824. if(rtspc->state != RTP_PARSE_SKIP)
  825. data->req.done = FALSE;
  826. /* we SHOULD have consumed all bytes, unless the response is borked.
  827. * In which case we write out the left over bytes, letting the client
  828. * writer deal with it (it will report EXCESS and fail the transfer). */
  829. DEBUGF(infof(data, "rtsp_rtp_write_resp(len=%zu, in_header=%d, done=%d "
  830. " rtspc->state=%d, req.size=%" FMT_OFF_T ")",
  831. blen, rtspc->in_header, data->req.done, rtspc->state,
  832. data->req.size));
  833. if(!result && (is_eos || blen)) {
  834. result = Curl_client_write(data, CLIENTWRITE_BODY|
  835. (is_eos ? CLIENTWRITE_EOS : 0), buf, blen);
  836. }
  837. out:
  838. if((data->set.rtspreq == RTSPREQ_RECEIVE) &&
  839. (rtspc->state == RTP_PARSE_SKIP)) {
  840. /* In special mode RECEIVE, we just process one chunk of network
  841. * data, so we stop the transfer here, if we have no incomplete
  842. * RTP message pending. */
  843. data->req.download_done = TRUE;
  844. }
  845. return result;
  846. }
  847. static CURLcode rtsp_rtp_write_resp_hd(struct Curl_easy *data,
  848. const char *buf,
  849. size_t blen,
  850. bool is_eos)
  851. {
  852. return rtsp_rtp_write_resp(data, buf, blen, is_eos);
  853. }
  854. static
  855. CURLcode rtp_client_write(struct Curl_easy *data, const char *ptr, size_t len)
  856. {
  857. size_t wrote;
  858. curl_write_callback writeit;
  859. void *user_ptr;
  860. if(len == 0) {
  861. failf(data, "Cannot write a 0 size RTP packet.");
  862. return CURLE_WRITE_ERROR;
  863. }
  864. /* If the user has configured CURLOPT_INTERLEAVEFUNCTION then use that
  865. function and any configured CURLOPT_INTERLEAVEDATA to write out the RTP
  866. data. Otherwise, use the CURLOPT_WRITEFUNCTION with the CURLOPT_WRITEDATA
  867. pointer to write out the RTP data. */
  868. if(data->set.fwrite_rtp) {
  869. writeit = data->set.fwrite_rtp;
  870. user_ptr = data->set.rtp_out;
  871. }
  872. else {
  873. writeit = data->set.fwrite_func;
  874. user_ptr = data->set.out;
  875. }
  876. Curl_set_in_callback(data, TRUE);
  877. wrote = writeit((char *)CURL_UNCONST(ptr), 1, len, user_ptr);
  878. Curl_set_in_callback(data, FALSE);
  879. if(CURL_WRITEFUNC_PAUSE == wrote) {
  880. failf(data, "Cannot pause RTP");
  881. return CURLE_WRITE_ERROR;
  882. }
  883. if(wrote != len) {
  884. failf(data, "Failed writing RTP data");
  885. return CURLE_WRITE_ERROR;
  886. }
  887. return CURLE_OK;
  888. }
  889. CURLcode Curl_rtsp_parseheader(struct Curl_easy *data, const char *header)
  890. {
  891. if(checkprefix("CSeq:", header)) {
  892. curl_off_t CSeq = 0;
  893. struct RTSP *rtsp = Curl_meta_get(data, CURL_META_RTSP_EASY);
  894. const char *p = &header[5];
  895. if(!rtsp)
  896. return CURLE_FAILED_INIT;
  897. curlx_str_passblanks(&p);
  898. if(curlx_str_number(&p, &CSeq, LONG_MAX)) {
  899. failf(data, "Unable to read the CSeq header: [%s]", header);
  900. return CURLE_RTSP_CSEQ_ERROR;
  901. }
  902. rtsp->CSeq_recv = (long)CSeq; /* mark the request */
  903. data->state.rtsp_CSeq_recv = (long)CSeq; /* update the handle */
  904. }
  905. else if(checkprefix("Session:", header)) {
  906. const char *start, *end;
  907. size_t idlen;
  908. /* Find the first non-space letter */
  909. start = header + 8;
  910. curlx_str_passblanks(&start);
  911. if(!*start) {
  912. failf(data, "Got a blank Session ID");
  913. return CURLE_RTSP_SESSION_ERROR;
  914. }
  915. /* Find the end of Session ID
  916. *
  917. * Allow any non whitespace content, up to the field separator or end of
  918. * line. RFC 2326 is not 100% clear on the session ID and for example
  919. * gstreamer does url-encoded session ID's not covered by the standard.
  920. */
  921. end = start;
  922. while((*end > ' ') && (*end != ';'))
  923. end++;
  924. idlen = end - start;
  925. if(data->set.str[STRING_RTSP_SESSION_ID]) {
  926. /* If the Session ID is set, then compare */
  927. if(strlen(data->set.str[STRING_RTSP_SESSION_ID]) != idlen ||
  928. strncmp(start, data->set.str[STRING_RTSP_SESSION_ID], idlen)) {
  929. failf(data, "Got RTSP Session ID Line [%s], but wanted ID [%s]",
  930. start, data->set.str[STRING_RTSP_SESSION_ID]);
  931. return CURLE_RTSP_SESSION_ERROR;
  932. }
  933. }
  934. else {
  935. /* If the Session ID is not set, and we find it in a response, then set
  936. * it.
  937. */
  938. /* Copy the id substring into a new buffer */
  939. data->set.str[STRING_RTSP_SESSION_ID] = Curl_memdup0(start, idlen);
  940. if(!data->set.str[STRING_RTSP_SESSION_ID])
  941. return CURLE_OUT_OF_MEMORY;
  942. }
  943. }
  944. else if(checkprefix("Transport:", header)) {
  945. CURLcode result;
  946. result = rtsp_parse_transport(data, header + 10);
  947. if(result)
  948. return result;
  949. }
  950. return CURLE_OK;
  951. }
  952. static
  953. CURLcode rtsp_parse_transport(struct Curl_easy *data, const char *transport)
  954. {
  955. /* If we receive multiple Transport response-headers, the linterleaved
  956. channels of each response header is recorded and used together for
  957. subsequent data validity checks.*/
  958. /* e.g.: ' RTP/AVP/TCP;unicast;interleaved=5-6' */
  959. const char *start, *end;
  960. start = transport;
  961. while(start && *start) {
  962. curlx_str_passblanks(&start);
  963. end = strchr(start, ';');
  964. if(checkprefix("interleaved=", start)) {
  965. curl_off_t chan1, chan2, chan;
  966. const char *p = start + 12;
  967. if(!curlx_str_number(&p, &chan1, 255)) {
  968. unsigned char *rtp_channel_mask = data->state.rtp_channel_mask;
  969. chan2 = chan1;
  970. if(!curlx_str_single(&p, '-')) {
  971. if(curlx_str_number(&p, &chan2, 255)) {
  972. infof(data, "Unable to read the interleaved parameter from "
  973. "Transport header: [%s]", transport);
  974. chan2 = chan1;
  975. }
  976. }
  977. for(chan = chan1; chan <= chan2; chan++) {
  978. int idx = (int)chan / 8;
  979. int off = (int)chan % 8;
  980. rtp_channel_mask[idx] |= (unsigned char)(1 << off);
  981. }
  982. }
  983. else {
  984. infof(data, "Unable to read the interleaved parameter from "
  985. "Transport header: [%s]", transport);
  986. }
  987. break;
  988. }
  989. /* skip to next parameter */
  990. start = (!end) ? end : (end + 1);
  991. }
  992. return CURLE_OK;
  993. }
  994. #endif /* CURL_DISABLE_RTSP */