rtsp.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827
  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_CHANNEL(p) ((int)((unsigned char)((p)[1])))
  45. #define RTP_PKT_LENGTH(p) ((((int)((unsigned char)((p)[2]))) << 8) | \
  46. ((int)((unsigned char)((p)[3]))))
  47. /* protocol-specific functions set up to be called by the main engine */
  48. static CURLcode rtsp_do(struct Curl_easy *data, bool *done);
  49. static CURLcode rtsp_done(struct Curl_easy *data, CURLcode, bool premature);
  50. static CURLcode rtsp_connect(struct Curl_easy *data, bool *done);
  51. static CURLcode rtsp_disconnect(struct Curl_easy *data,
  52. struct connectdata *conn, bool dead);
  53. static int rtsp_getsock_do(struct Curl_easy *data,
  54. struct connectdata *conn, curl_socket_t *socks);
  55. /*
  56. * Parse and write out any available RTP data.
  57. *
  58. * nread: amount of data left after k->str. will be modified if RTP
  59. * data is parsed and k->str is moved up
  60. * readmore: whether or not the RTP parser needs more data right away
  61. */
  62. static CURLcode rtsp_rtp_readwrite(struct Curl_easy *data,
  63. struct connectdata *conn,
  64. ssize_t *nread,
  65. bool *readmore);
  66. static CURLcode rtsp_setup_connection(struct Curl_easy *data,
  67. struct connectdata *conn);
  68. static unsigned int rtsp_conncheck(struct Curl_easy *data,
  69. struct connectdata *check,
  70. unsigned int checks_to_perform);
  71. /* this returns the socket to wait for in the DO and DOING state for the multi
  72. interface and then we're always _sending_ a request and thus we wait for
  73. the single socket to become writable only */
  74. static int rtsp_getsock_do(struct Curl_easy *data, struct connectdata *conn,
  75. curl_socket_t *socks)
  76. {
  77. /* write mode */
  78. (void)data;
  79. socks[0] = conn->sock[FIRSTSOCKET];
  80. return GETSOCK_WRITESOCK(0);
  81. }
  82. static
  83. CURLcode rtp_client_write(struct Curl_easy *data, char *ptr, size_t len);
  84. /*
  85. * RTSP handler interface.
  86. */
  87. const struct Curl_handler Curl_handler_rtsp = {
  88. "RTSP", /* scheme */
  89. rtsp_setup_connection, /* setup_connection */
  90. rtsp_do, /* do_it */
  91. rtsp_done, /* done */
  92. ZERO_NULL, /* do_more */
  93. rtsp_connect, /* connect_it */
  94. ZERO_NULL, /* connecting */
  95. ZERO_NULL, /* doing */
  96. ZERO_NULL, /* proto_getsock */
  97. rtsp_getsock_do, /* doing_getsock */
  98. ZERO_NULL, /* domore_getsock */
  99. ZERO_NULL, /* perform_getsock */
  100. rtsp_disconnect, /* disconnect */
  101. rtsp_rtp_readwrite, /* readwrite */
  102. rtsp_conncheck, /* connection_check */
  103. ZERO_NULL, /* attach connection */
  104. PORT_RTSP, /* defport */
  105. CURLPROTO_RTSP, /* protocol */
  106. CURLPROTO_RTSP, /* family */
  107. PROTOPT_NONE /* flags */
  108. };
  109. static CURLcode rtsp_setup_connection(struct Curl_easy *data,
  110. struct connectdata *conn)
  111. {
  112. struct RTSP *rtsp;
  113. (void)conn;
  114. data->req.p.rtsp = rtsp = calloc(1, sizeof(struct RTSP));
  115. if(!rtsp)
  116. return CURLE_OUT_OF_MEMORY;
  117. return CURLE_OK;
  118. }
  119. /*
  120. * Function to check on various aspects of a connection.
  121. */
  122. static unsigned int rtsp_conncheck(struct Curl_easy *data,
  123. struct connectdata *conn,
  124. unsigned int checks_to_perform)
  125. {
  126. unsigned int ret_val = CONNRESULT_NONE;
  127. (void)data;
  128. if(checks_to_perform & CONNCHECK_ISDEAD) {
  129. if(!Curl_conn_is_alive(data, conn))
  130. ret_val |= CONNRESULT_DEAD;
  131. }
  132. return ret_val;
  133. }
  134. static CURLcode rtsp_connect(struct Curl_easy *data, bool *done)
  135. {
  136. CURLcode httpStatus;
  137. httpStatus = Curl_http_connect(data, done);
  138. /* Initialize the CSeq if not already done */
  139. if(data->state.rtsp_next_client_CSeq == 0)
  140. data->state.rtsp_next_client_CSeq = 1;
  141. if(data->state.rtsp_next_server_CSeq == 0)
  142. data->state.rtsp_next_server_CSeq = 1;
  143. data->conn->proto.rtspc.rtp_channel = -1;
  144. return httpStatus;
  145. }
  146. static CURLcode rtsp_disconnect(struct Curl_easy *data,
  147. struct connectdata *conn, bool dead)
  148. {
  149. (void) dead;
  150. (void) data;
  151. Curl_safefree(conn->proto.rtspc.rtp_buf);
  152. return CURLE_OK;
  153. }
  154. static CURLcode rtsp_done(struct Curl_easy *data,
  155. CURLcode status, bool premature)
  156. {
  157. struct RTSP *rtsp = data->req.p.rtsp;
  158. CURLcode httpStatus;
  159. /* Bypass HTTP empty-reply checks on receive */
  160. if(data->set.rtspreq == RTSPREQ_RECEIVE)
  161. premature = TRUE;
  162. httpStatus = Curl_http_done(data, status, premature);
  163. if(rtsp && !status && !httpStatus) {
  164. /* Check the sequence numbers */
  165. long CSeq_sent = rtsp->CSeq_sent;
  166. long CSeq_recv = rtsp->CSeq_recv;
  167. if((data->set.rtspreq != RTSPREQ_RECEIVE) && (CSeq_sent != CSeq_recv)) {
  168. failf(data,
  169. "The CSeq of this request %ld did not match the response %ld",
  170. CSeq_sent, CSeq_recv);
  171. return CURLE_RTSP_CSEQ_ERROR;
  172. }
  173. if(data->set.rtspreq == RTSPREQ_RECEIVE &&
  174. (data->conn->proto.rtspc.rtp_channel == -1)) {
  175. infof(data, "Got an RTP Receive with a CSeq of %ld", CSeq_recv);
  176. }
  177. }
  178. return httpStatus;
  179. }
  180. static CURLcode rtsp_do(struct Curl_easy *data, bool *done)
  181. {
  182. struct connectdata *conn = data->conn;
  183. CURLcode result = CURLE_OK;
  184. Curl_RtspReq rtspreq = data->set.rtspreq;
  185. struct RTSP *rtsp = data->req.p.rtsp;
  186. struct dynbuf req_buffer;
  187. curl_off_t postsize = 0; /* for ANNOUNCE and SET_PARAMETER */
  188. curl_off_t putsize = 0; /* for ANNOUNCE and SET_PARAMETER */
  189. const char *p_request = NULL;
  190. const char *p_session_id = NULL;
  191. const char *p_accept = NULL;
  192. const char *p_accept_encoding = NULL;
  193. const char *p_range = NULL;
  194. const char *p_referrer = NULL;
  195. const char *p_stream_uri = NULL;
  196. const char *p_transport = NULL;
  197. const char *p_uagent = NULL;
  198. const char *p_proxyuserpwd = NULL;
  199. const char *p_userpwd = NULL;
  200. *done = TRUE;
  201. rtsp->CSeq_sent = data->state.rtsp_next_client_CSeq;
  202. rtsp->CSeq_recv = 0;
  203. /* Setup the first_* fields to allow auth details get sent
  204. to this origin */
  205. if(!data->state.first_host) {
  206. data->state.first_host = strdup(conn->host.name);
  207. if(!data->state.first_host)
  208. return CURLE_OUT_OF_MEMORY;
  209. data->state.first_remote_port = conn->remote_port;
  210. data->state.first_remote_protocol = conn->handler->protocol;
  211. }
  212. /* Setup the 'p_request' pointer to the proper p_request string
  213. * Since all RTSP requests are included here, there is no need to
  214. * support custom requests like HTTP.
  215. **/
  216. data->req.no_body = TRUE; /* most requests don't contain a body */
  217. switch(rtspreq) {
  218. default:
  219. failf(data, "Got invalid RTSP request");
  220. return CURLE_BAD_FUNCTION_ARGUMENT;
  221. case RTSPREQ_OPTIONS:
  222. p_request = "OPTIONS";
  223. break;
  224. case RTSPREQ_DESCRIBE:
  225. p_request = "DESCRIBE";
  226. data->req.no_body = FALSE;
  227. break;
  228. case RTSPREQ_ANNOUNCE:
  229. p_request = "ANNOUNCE";
  230. break;
  231. case RTSPREQ_SETUP:
  232. p_request = "SETUP";
  233. break;
  234. case RTSPREQ_PLAY:
  235. p_request = "PLAY";
  236. break;
  237. case RTSPREQ_PAUSE:
  238. p_request = "PAUSE";
  239. break;
  240. case RTSPREQ_TEARDOWN:
  241. p_request = "TEARDOWN";
  242. break;
  243. case RTSPREQ_GET_PARAMETER:
  244. /* GET_PARAMETER's no_body status is determined later */
  245. p_request = "GET_PARAMETER";
  246. data->req.no_body = FALSE;
  247. break;
  248. case RTSPREQ_SET_PARAMETER:
  249. p_request = "SET_PARAMETER";
  250. break;
  251. case RTSPREQ_RECORD:
  252. p_request = "RECORD";
  253. break;
  254. case RTSPREQ_RECEIVE:
  255. p_request = "";
  256. /* Treat interleaved RTP as body */
  257. data->req.no_body = FALSE;
  258. break;
  259. case RTSPREQ_LAST:
  260. failf(data, "Got invalid RTSP request: RTSPREQ_LAST");
  261. return CURLE_BAD_FUNCTION_ARGUMENT;
  262. }
  263. if(rtspreq == RTSPREQ_RECEIVE) {
  264. Curl_setup_transfer(data, FIRSTSOCKET, -1, TRUE, -1);
  265. return result;
  266. }
  267. p_session_id = data->set.str[STRING_RTSP_SESSION_ID];
  268. if(!p_session_id &&
  269. (rtspreq & ~(RTSPREQ_OPTIONS | RTSPREQ_DESCRIBE | RTSPREQ_SETUP))) {
  270. failf(data, "Refusing to issue an RTSP request [%s] without a session ID.",
  271. p_request);
  272. return CURLE_BAD_FUNCTION_ARGUMENT;
  273. }
  274. /* Stream URI. Default to server '*' if not specified */
  275. if(data->set.str[STRING_RTSP_STREAM_URI]) {
  276. p_stream_uri = data->set.str[STRING_RTSP_STREAM_URI];
  277. }
  278. else {
  279. p_stream_uri = "*";
  280. }
  281. /* Transport Header for SETUP requests */
  282. p_transport = Curl_checkheaders(data, STRCONST("Transport"));
  283. if(rtspreq == RTSPREQ_SETUP && !p_transport) {
  284. /* New Transport: setting? */
  285. if(data->set.str[STRING_RTSP_TRANSPORT]) {
  286. Curl_safefree(data->state.aptr.rtsp_transport);
  287. data->state.aptr.rtsp_transport =
  288. aprintf("Transport: %s\r\n",
  289. data->set.str[STRING_RTSP_TRANSPORT]);
  290. if(!data->state.aptr.rtsp_transport)
  291. return CURLE_OUT_OF_MEMORY;
  292. }
  293. else {
  294. failf(data,
  295. "Refusing to issue an RTSP SETUP without a Transport: header.");
  296. return CURLE_BAD_FUNCTION_ARGUMENT;
  297. }
  298. p_transport = data->state.aptr.rtsp_transport;
  299. }
  300. /* Accept Headers for DESCRIBE requests */
  301. if(rtspreq == RTSPREQ_DESCRIBE) {
  302. /* Accept Header */
  303. p_accept = Curl_checkheaders(data, STRCONST("Accept"))?
  304. NULL:"Accept: application/sdp\r\n";
  305. /* Accept-Encoding header */
  306. if(!Curl_checkheaders(data, STRCONST("Accept-Encoding")) &&
  307. data->set.str[STRING_ENCODING]) {
  308. Curl_safefree(data->state.aptr.accept_encoding);
  309. data->state.aptr.accept_encoding =
  310. aprintf("Accept-Encoding: %s\r\n", data->set.str[STRING_ENCODING]);
  311. if(!data->state.aptr.accept_encoding)
  312. return CURLE_OUT_OF_MEMORY;
  313. p_accept_encoding = data->state.aptr.accept_encoding;
  314. }
  315. }
  316. /* The User-Agent string might have been allocated in url.c already, because
  317. it might have been used in the proxy connect, but if we have got a header
  318. with the user-agent string specified, we erase the previously made string
  319. here. */
  320. if(Curl_checkheaders(data, STRCONST("User-Agent")) &&
  321. data->state.aptr.uagent) {
  322. Curl_safefree(data->state.aptr.uagent);
  323. data->state.aptr.uagent = NULL;
  324. }
  325. else if(!Curl_checkheaders(data, STRCONST("User-Agent")) &&
  326. data->set.str[STRING_USERAGENT]) {
  327. p_uagent = data->state.aptr.uagent;
  328. }
  329. /* setup the authentication headers */
  330. result = Curl_http_output_auth(data, conn, p_request, HTTPREQ_GET,
  331. p_stream_uri, FALSE);
  332. if(result)
  333. return result;
  334. p_proxyuserpwd = data->state.aptr.proxyuserpwd;
  335. p_userpwd = data->state.aptr.userpwd;
  336. /* Referrer */
  337. Curl_safefree(data->state.aptr.ref);
  338. if(data->state.referer && !Curl_checkheaders(data, STRCONST("Referer")))
  339. data->state.aptr.ref = aprintf("Referer: %s\r\n", data->state.referer);
  340. else
  341. data->state.aptr.ref = NULL;
  342. p_referrer = data->state.aptr.ref;
  343. /*
  344. * Range Header
  345. * Only applies to PLAY, PAUSE, RECORD
  346. *
  347. * Go ahead and use the Range stuff supplied for HTTP
  348. */
  349. if(data->state.use_range &&
  350. (rtspreq & (RTSPREQ_PLAY | RTSPREQ_PAUSE | RTSPREQ_RECORD))) {
  351. /* Check to see if there is a range set in the custom headers */
  352. if(!Curl_checkheaders(data, STRCONST("Range")) && data->state.range) {
  353. Curl_safefree(data->state.aptr.rangeline);
  354. data->state.aptr.rangeline = aprintf("Range: %s\r\n", data->state.range);
  355. p_range = data->state.aptr.rangeline;
  356. }
  357. }
  358. /*
  359. * Sanity check the custom headers
  360. */
  361. if(Curl_checkheaders(data, STRCONST("CSeq"))) {
  362. failf(data, "CSeq cannot be set as a custom header.");
  363. return CURLE_RTSP_CSEQ_ERROR;
  364. }
  365. if(Curl_checkheaders(data, STRCONST("Session"))) {
  366. failf(data, "Session ID cannot be set as a custom header.");
  367. return CURLE_BAD_FUNCTION_ARGUMENT;
  368. }
  369. /* Initialize a dynamic send buffer */
  370. Curl_dyn_init(&req_buffer, DYN_RTSP_REQ_HEADER);
  371. result =
  372. Curl_dyn_addf(&req_buffer,
  373. "%s %s RTSP/1.0\r\n" /* Request Stream-URI RTSP/1.0 */
  374. "CSeq: %ld\r\n", /* CSeq */
  375. p_request, p_stream_uri, rtsp->CSeq_sent);
  376. if(result)
  377. return result;
  378. /*
  379. * Rather than do a normal alloc line, keep the session_id unformatted
  380. * to make comparison easier
  381. */
  382. if(p_session_id) {
  383. result = Curl_dyn_addf(&req_buffer, "Session: %s\r\n", p_session_id);
  384. if(result)
  385. return result;
  386. }
  387. /*
  388. * Shared HTTP-like options
  389. */
  390. result = Curl_dyn_addf(&req_buffer,
  391. "%s" /* transport */
  392. "%s" /* accept */
  393. "%s" /* accept-encoding */
  394. "%s" /* range */
  395. "%s" /* referrer */
  396. "%s" /* user-agent */
  397. "%s" /* proxyuserpwd */
  398. "%s" /* userpwd */
  399. ,
  400. p_transport ? p_transport : "",
  401. p_accept ? p_accept : "",
  402. p_accept_encoding ? p_accept_encoding : "",
  403. p_range ? p_range : "",
  404. p_referrer ? p_referrer : "",
  405. p_uagent ? p_uagent : "",
  406. p_proxyuserpwd ? p_proxyuserpwd : "",
  407. p_userpwd ? p_userpwd : "");
  408. /*
  409. * Free userpwd now --- cannot reuse this for Negotiate and possibly NTLM
  410. * with basic and digest, it will be freed anyway by the next request
  411. */
  412. Curl_safefree(data->state.aptr.userpwd);
  413. data->state.aptr.userpwd = NULL;
  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->set.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->set.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. char *rtp; /* moving pointer to rtp data */
  513. ssize_t rtp_dataleft; /* how much data left to parse in this round */
  514. char *scratch;
  515. CURLcode result;
  516. if(rtspc->rtp_buf) {
  517. /* There was some leftover data the last time. Merge buffers */
  518. char *newptr = Curl_saferealloc(rtspc->rtp_buf,
  519. rtspc->rtp_bufsize + *nread);
  520. if(!newptr) {
  521. rtspc->rtp_buf = NULL;
  522. rtspc->rtp_bufsize = 0;
  523. return CURLE_OUT_OF_MEMORY;
  524. }
  525. rtspc->rtp_buf = newptr;
  526. memcpy(rtspc->rtp_buf + rtspc->rtp_bufsize, k->str, *nread);
  527. rtspc->rtp_bufsize += *nread;
  528. rtp = rtspc->rtp_buf;
  529. rtp_dataleft = rtspc->rtp_bufsize;
  530. }
  531. else {
  532. /* Just parse the request buffer directly */
  533. rtp = k->str;
  534. rtp_dataleft = *nread;
  535. }
  536. while((rtp_dataleft > 0) &&
  537. (rtp[0] == '$')) {
  538. if(rtp_dataleft > 4) {
  539. int rtp_length;
  540. /* Parse the header */
  541. /* The channel identifier immediately follows and is 1 byte */
  542. rtspc->rtp_channel = RTP_PKT_CHANNEL(rtp);
  543. /* The length is two bytes */
  544. rtp_length = RTP_PKT_LENGTH(rtp);
  545. if(rtp_dataleft < rtp_length + 4) {
  546. /* Need more - incomplete payload */
  547. *readmore = TRUE;
  548. break;
  549. }
  550. /* We have the full RTP interleaved packet
  551. * Write out the header including the leading '$' */
  552. DEBUGF(infof(data, "RTP write channel %d rtp_length %d",
  553. rtspc->rtp_channel, rtp_length));
  554. result = rtp_client_write(data, &rtp[0], rtp_length + 4);
  555. if(result) {
  556. failf(data, "Got an error writing an RTP packet");
  557. *readmore = FALSE;
  558. Curl_safefree(rtspc->rtp_buf);
  559. rtspc->rtp_buf = NULL;
  560. rtspc->rtp_bufsize = 0;
  561. return result;
  562. }
  563. /* Move forward in the buffer */
  564. rtp_dataleft -= rtp_length + 4;
  565. rtp += rtp_length + 4;
  566. if(data->set.rtspreq == RTSPREQ_RECEIVE) {
  567. /* If we are in a passive receive, give control back
  568. * to the app as often as we can.
  569. */
  570. k->keepon &= ~KEEP_RECV;
  571. }
  572. }
  573. else {
  574. /* Need more - incomplete header */
  575. *readmore = TRUE;
  576. break;
  577. }
  578. }
  579. if(rtp_dataleft && rtp[0] == '$') {
  580. DEBUGF(infof(data, "RTP Rewinding %zd %s", rtp_dataleft,
  581. *readmore ? "(READMORE)" : ""));
  582. /* Store the incomplete RTP packet for a "rewind" */
  583. scratch = malloc(rtp_dataleft);
  584. if(!scratch) {
  585. Curl_safefree(rtspc->rtp_buf);
  586. rtspc->rtp_buf = NULL;
  587. rtspc->rtp_bufsize = 0;
  588. return CURLE_OUT_OF_MEMORY;
  589. }
  590. memcpy(scratch, rtp, rtp_dataleft);
  591. Curl_safefree(rtspc->rtp_buf);
  592. rtspc->rtp_buf = scratch;
  593. rtspc->rtp_bufsize = rtp_dataleft;
  594. /* As far as the transfer is concerned, this data is consumed */
  595. *nread = 0;
  596. return CURLE_OK;
  597. }
  598. /* Fix up k->str to point just after the last RTP packet */
  599. k->str += *nread - rtp_dataleft;
  600. /* either all of the data has been read or...
  601. * rtp now points at the next byte to parse
  602. */
  603. if(rtp_dataleft > 0)
  604. DEBUGASSERT(k->str[0] == rtp[0]);
  605. DEBUGASSERT(rtp_dataleft <= *nread); /* sanity check */
  606. *nread = rtp_dataleft;
  607. /* If we get here, we have finished with the leftover/merge buffer */
  608. Curl_safefree(rtspc->rtp_buf);
  609. rtspc->rtp_buf = NULL;
  610. rtspc->rtp_bufsize = 0;
  611. return CURLE_OK;
  612. }
  613. static
  614. CURLcode rtp_client_write(struct Curl_easy *data, char *ptr, size_t len)
  615. {
  616. size_t wrote;
  617. curl_write_callback writeit;
  618. void *user_ptr;
  619. if(len == 0) {
  620. failf(data, "Cannot write a 0 size RTP packet.");
  621. return CURLE_WRITE_ERROR;
  622. }
  623. /* If the user has configured CURLOPT_INTERLEAVEFUNCTION then use that
  624. function and any configured CURLOPT_INTERLEAVEDATA to write out the RTP
  625. data. Otherwise, use the CURLOPT_WRITEFUNCTION with the CURLOPT_WRITEDATA
  626. pointer to write out the RTP data. */
  627. if(data->set.fwrite_rtp) {
  628. writeit = data->set.fwrite_rtp;
  629. user_ptr = data->set.rtp_out;
  630. }
  631. else {
  632. writeit = data->set.fwrite_func;
  633. user_ptr = data->set.out;
  634. }
  635. Curl_set_in_callback(data, true);
  636. wrote = writeit(ptr, 1, len, user_ptr);
  637. Curl_set_in_callback(data, false);
  638. if(CURL_WRITEFUNC_PAUSE == wrote) {
  639. failf(data, "Cannot pause RTP");
  640. return CURLE_WRITE_ERROR;
  641. }
  642. if(wrote != len) {
  643. failf(data, "Failed writing RTP data");
  644. return CURLE_WRITE_ERROR;
  645. }
  646. return CURLE_OK;
  647. }
  648. CURLcode Curl_rtsp_parseheader(struct Curl_easy *data, char *header)
  649. {
  650. if(checkprefix("CSeq:", header)) {
  651. long CSeq = 0;
  652. char *endp;
  653. char *p = &header[5];
  654. while(ISBLANK(*p))
  655. p++;
  656. CSeq = strtol(p, &endp, 10);
  657. if(p != endp) {
  658. struct RTSP *rtsp = data->req.p.rtsp;
  659. rtsp->CSeq_recv = CSeq; /* mark the request */
  660. data->state.rtsp_CSeq_recv = CSeq; /* update the handle */
  661. }
  662. else {
  663. failf(data, "Unable to read the CSeq header: [%s]", header);
  664. return CURLE_RTSP_CSEQ_ERROR;
  665. }
  666. }
  667. else if(checkprefix("Session:", header)) {
  668. char *start;
  669. char *end;
  670. size_t idlen;
  671. /* Find the first non-space letter */
  672. start = header + 8;
  673. while(*start && ISBLANK(*start))
  674. start++;
  675. if(!*start) {
  676. failf(data, "Got a blank Session ID");
  677. return CURLE_RTSP_SESSION_ERROR;
  678. }
  679. /* Find the end of Session ID
  680. *
  681. * Allow any non whitespace content, up to the field separator or end of
  682. * line. RFC 2326 isn't 100% clear on the session ID and for example
  683. * gstreamer does url-encoded session ID's not covered by the standard.
  684. */
  685. end = start;
  686. while(*end && *end != ';' && !ISSPACE(*end))
  687. end++;
  688. idlen = end - start;
  689. if(data->set.str[STRING_RTSP_SESSION_ID]) {
  690. /* If the Session ID is set, then compare */
  691. if(strlen(data->set.str[STRING_RTSP_SESSION_ID]) != idlen ||
  692. strncmp(start, data->set.str[STRING_RTSP_SESSION_ID], idlen) != 0) {
  693. failf(data, "Got RTSP Session ID Line [%s], but wanted ID [%s]",
  694. start, data->set.str[STRING_RTSP_SESSION_ID]);
  695. return CURLE_RTSP_SESSION_ERROR;
  696. }
  697. }
  698. else {
  699. /* If the Session ID is not set, and we find it in a response, then set
  700. * it.
  701. */
  702. /* Copy the id substring into a new buffer */
  703. data->set.str[STRING_RTSP_SESSION_ID] = malloc(idlen + 1);
  704. if(!data->set.str[STRING_RTSP_SESSION_ID])
  705. return CURLE_OUT_OF_MEMORY;
  706. memcpy(data->set.str[STRING_RTSP_SESSION_ID], start, idlen);
  707. (data->set.str[STRING_RTSP_SESSION_ID])[idlen] = '\0';
  708. }
  709. }
  710. return CURLE_OK;
  711. }
  712. #endif /* CURL_DISABLE_RTSP or using Hyper */