rtsp.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902
  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) ((((int)((unsigned char)((p)[2]))) << 8) | \
  45. ((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, 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_rtp_readwrite(struct Curl_easy *data,
  507. struct connectdata *conn,
  508. ssize_t *nread,
  509. bool *readmore) {
  510. struct SingleRequest *k = &data->req;
  511. struct rtsp_conn *rtspc = &(conn->proto.rtspc);
  512. unsigned char *rtp_channel_mask = data->state.rtp_channel_mask;
  513. char *rtp; /* moving pointer to rtp data */
  514. ssize_t rtp_dataleft; /* how much data left to parse in this round */
  515. CURLcode result;
  516. bool interleaved = false;
  517. size_t skip_size = 0;
  518. if(Curl_dyn_len(&rtspc->buf)) {
  519. /* There was some leftover data the last time. Append new buffers */
  520. if(Curl_dyn_addn(&rtspc->buf, k->str, *nread))
  521. return CURLE_OUT_OF_MEMORY;
  522. rtp = Curl_dyn_ptr(&rtspc->buf);
  523. rtp_dataleft = Curl_dyn_len(&rtspc->buf);
  524. }
  525. else {
  526. /* Just parse the request buffer directly */
  527. rtp = k->str;
  528. rtp_dataleft = *nread;
  529. }
  530. while(rtp_dataleft > 0) {
  531. if(rtp[0] == '$') {
  532. if(rtp_dataleft > 4) {
  533. unsigned char rtp_channel;
  534. int rtp_length;
  535. int idx;
  536. int off;
  537. /* Parse the header */
  538. /* The channel identifier immediately follows and is 1 byte */
  539. rtp_channel = (unsigned char)rtp[1];
  540. idx = rtp_channel / 8;
  541. off = rtp_channel % 8;
  542. if(!(rtp_channel_mask[idx] & (1 << off))) {
  543. /* invalid channel number, maybe not an RTP packet */
  544. rtp++;
  545. rtp_dataleft--;
  546. skip_size++;
  547. continue;
  548. }
  549. if(skip_size > 0) {
  550. DEBUGF(infof(data, "Skip the malformed interleaved data %lu "
  551. "bytes", skip_size));
  552. }
  553. skip_size = 0;
  554. rtspc->rtp_channel = rtp_channel;
  555. /* The length is two bytes */
  556. rtp_length = RTP_PKT_LENGTH(rtp);
  557. if(rtp_dataleft < rtp_length + 4) {
  558. /* Need more - incomplete payload */
  559. *readmore = TRUE;
  560. break;
  561. }
  562. interleaved = true;
  563. /* We have the full RTP interleaved packet
  564. * Write out the header including the leading '$' */
  565. DEBUGF(infof(data, "RTP write channel %d rtp_length %d",
  566. rtspc->rtp_channel, rtp_length));
  567. result = rtp_client_write(data, &rtp[0], rtp_length + 4);
  568. if(result) {
  569. *readmore = FALSE;
  570. return result;
  571. }
  572. /* Move forward in the buffer */
  573. rtp_dataleft -= rtp_length + 4;
  574. rtp += rtp_length + 4;
  575. if(data->set.rtspreq == RTSPREQ_RECEIVE) {
  576. /* If we are in a passive receive, give control back
  577. * to the app as often as we can.
  578. */
  579. k->keepon &= ~KEEP_RECV;
  580. }
  581. }
  582. else {
  583. /* Need more - incomplete header */
  584. *readmore = TRUE;
  585. break;
  586. }
  587. }
  588. else {
  589. /* If the following data begins with 'RTSP/', which might be an RTSP
  590. message, we should stop skipping the data. */
  591. /* If `k-> headerline> 0 && !interleaved` is true, we are maybe in the
  592. middle of an RTSP message. It is difficult to determine this, so we
  593. stop skipping. */
  594. size_t prefix_len = (rtp_dataleft < 5) ? rtp_dataleft : 5;
  595. if((k->headerline > 0 && !interleaved) ||
  596. strncmp(rtp, "RTSP/", prefix_len) == 0) {
  597. if(skip_size > 0) {
  598. DEBUGF(infof(data, "Skip the malformed interleaved data %lu "
  599. "bytes", skip_size));
  600. }
  601. break; /* maybe is an RTSP message */
  602. }
  603. /* Skip incorrect data util the next RTP packet or RTSP message */
  604. do {
  605. rtp++;
  606. rtp_dataleft--;
  607. skip_size++;
  608. } while(rtp_dataleft > 0 && rtp[0] != '$' && rtp[0] != 'R');
  609. }
  610. }
  611. if(rtp_dataleft && rtp[0] == '$') {
  612. DEBUGF(infof(data, "RTP Rewinding %zd %s", rtp_dataleft,
  613. *readmore ? "(READMORE)" : ""));
  614. /* Store the incomplete RTP packet for a "rewind" */
  615. if(!Curl_dyn_len(&rtspc->buf)) {
  616. /* nothing was stored, add this data */
  617. if(Curl_dyn_addn(&rtspc->buf, rtp, rtp_dataleft))
  618. return CURLE_OUT_OF_MEMORY;
  619. }
  620. else {
  621. /* keep the remainder */
  622. Curl_dyn_tail(&rtspc->buf, rtp_dataleft);
  623. }
  624. /* As far as the transfer is concerned, this data is consumed */
  625. *nread = 0;
  626. return CURLE_OK;
  627. }
  628. /* Fix up k->str to point just after the last RTP packet */
  629. k->str += *nread - rtp_dataleft;
  630. *nread = rtp_dataleft;
  631. /* If we get here, we have finished with the leftover/merge buffer */
  632. Curl_dyn_free(&rtspc->buf);
  633. return CURLE_OK;
  634. }
  635. static
  636. CURLcode rtp_client_write(struct Curl_easy *data, char *ptr, size_t len)
  637. {
  638. size_t wrote;
  639. curl_write_callback writeit;
  640. void *user_ptr;
  641. if(len == 0) {
  642. failf(data, "Cannot write a 0 size RTP packet.");
  643. return CURLE_WRITE_ERROR;
  644. }
  645. /* If the user has configured CURLOPT_INTERLEAVEFUNCTION then use that
  646. function and any configured CURLOPT_INTERLEAVEDATA to write out the RTP
  647. data. Otherwise, use the CURLOPT_WRITEFUNCTION with the CURLOPT_WRITEDATA
  648. pointer to write out the RTP data. */
  649. if(data->set.fwrite_rtp) {
  650. writeit = data->set.fwrite_rtp;
  651. user_ptr = data->set.rtp_out;
  652. }
  653. else {
  654. writeit = data->set.fwrite_func;
  655. user_ptr = data->set.out;
  656. }
  657. Curl_set_in_callback(data, true);
  658. wrote = writeit(ptr, 1, len, user_ptr);
  659. Curl_set_in_callback(data, false);
  660. if(CURL_WRITEFUNC_PAUSE == wrote) {
  661. failf(data, "Cannot pause RTP");
  662. return CURLE_WRITE_ERROR;
  663. }
  664. if(wrote != len) {
  665. failf(data, "Failed writing RTP data");
  666. return CURLE_WRITE_ERROR;
  667. }
  668. return CURLE_OK;
  669. }
  670. CURLcode Curl_rtsp_parseheader(struct Curl_easy *data, char *header)
  671. {
  672. if(checkprefix("CSeq:", header)) {
  673. long CSeq = 0;
  674. char *endp;
  675. char *p = &header[5];
  676. while(ISBLANK(*p))
  677. p++;
  678. CSeq = strtol(p, &endp, 10);
  679. if(p != endp) {
  680. struct RTSP *rtsp = data->req.p.rtsp;
  681. rtsp->CSeq_recv = CSeq; /* mark the request */
  682. data->state.rtsp_CSeq_recv = CSeq; /* update the handle */
  683. }
  684. else {
  685. failf(data, "Unable to read the CSeq header: [%s]", header);
  686. return CURLE_RTSP_CSEQ_ERROR;
  687. }
  688. }
  689. else if(checkprefix("Session:", header)) {
  690. char *start;
  691. char *end;
  692. size_t idlen;
  693. /* Find the first non-space letter */
  694. start = header + 8;
  695. while(*start && ISBLANK(*start))
  696. start++;
  697. if(!*start) {
  698. failf(data, "Got a blank Session ID");
  699. return CURLE_RTSP_SESSION_ERROR;
  700. }
  701. /* Find the end of Session ID
  702. *
  703. * Allow any non whitespace content, up to the field separator or end of
  704. * line. RFC 2326 isn't 100% clear on the session ID and for example
  705. * gstreamer does url-encoded session ID's not covered by the standard.
  706. */
  707. end = start;
  708. while(*end && *end != ';' && !ISSPACE(*end))
  709. end++;
  710. idlen = end - start;
  711. if(data->set.str[STRING_RTSP_SESSION_ID]) {
  712. /* If the Session ID is set, then compare */
  713. if(strlen(data->set.str[STRING_RTSP_SESSION_ID]) != idlen ||
  714. strncmp(start, data->set.str[STRING_RTSP_SESSION_ID], idlen) != 0) {
  715. failf(data, "Got RTSP Session ID Line [%s], but wanted ID [%s]",
  716. start, data->set.str[STRING_RTSP_SESSION_ID]);
  717. return CURLE_RTSP_SESSION_ERROR;
  718. }
  719. }
  720. else {
  721. /* If the Session ID is not set, and we find it in a response, then set
  722. * it.
  723. */
  724. /* Copy the id substring into a new buffer */
  725. data->set.str[STRING_RTSP_SESSION_ID] = malloc(idlen + 1);
  726. if(!data->set.str[STRING_RTSP_SESSION_ID])
  727. return CURLE_OUT_OF_MEMORY;
  728. memcpy(data->set.str[STRING_RTSP_SESSION_ID], start, idlen);
  729. (data->set.str[STRING_RTSP_SESSION_ID])[idlen] = '\0';
  730. }
  731. }
  732. else if(checkprefix("Transport:", header)) {
  733. CURLcode result;
  734. result = rtsp_parse_transport(data, header + 10);
  735. if(result)
  736. return result;
  737. }
  738. return CURLE_OK;
  739. }
  740. static
  741. CURLcode rtsp_parse_transport(struct Curl_easy *data, char *transport)
  742. {
  743. /* If we receive multiple Transport response-headers, the linterleaved
  744. channels of each response header is recorded and used together for
  745. subsequent data validity checks.*/
  746. /* e.g.: ' RTP/AVP/TCP;unicast;interleaved=5-6' */
  747. char *start;
  748. char *end;
  749. start = transport;
  750. while(start && *start) {
  751. while(*start && ISBLANK(*start) )
  752. start++;
  753. end = strchr(start, ';');
  754. if(checkprefix("interleaved=", start)) {
  755. long chan1, chan2, chan;
  756. char *endp;
  757. char *p = start + 12;
  758. chan1 = strtol(p, &endp, 10);
  759. if(p != endp && chan1 >= 0 && chan1 <= 255) {
  760. unsigned char *rtp_channel_mask = data->state.rtp_channel_mask;
  761. chan2 = chan1;
  762. if(*endp == '-') {
  763. p = endp + 1;
  764. chan2 = strtol(p, &endp, 10);
  765. if(p == endp || chan2 < 0 || chan2 > 255) {
  766. infof(data, "Unable to read the interleaved parameter from "
  767. "Transport header: [%s]", transport);
  768. chan2 = chan1;
  769. }
  770. }
  771. for(chan = chan1; chan <= chan2; chan++) {
  772. long idx = chan / 8;
  773. long off = chan % 8;
  774. rtp_channel_mask[idx] |= (unsigned char)(1 << off);
  775. }
  776. }
  777. else {
  778. infof(data, "Unable to read the interleaved parameter from "
  779. "Transport header: [%s]", transport);
  780. }
  781. break;
  782. }
  783. /* skip to next parameter */
  784. start = (!end) ? end : (end + 1);
  785. }
  786. return CURLE_OK;
  787. }
  788. #endif /* CURL_DISABLE_RTSP or using Hyper */