rtsp.c 31 KB

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