2
0

rtsp.c 32 KB

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