rtsp.c 32 KB

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