rtsp.c 32 KB

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