rtsp.c 32 KB

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