rtsp.c 32 KB

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