rtsp.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2022, Daniel Stenberg, <[email protected]>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "curl_setup.h"
  25. #if !defined(CURL_DISABLE_RTSP) && !defined(USE_HYPER)
  26. #include "urldata.h"
  27. #include <curl/curl.h>
  28. #include "transfer.h"
  29. #include "sendf.h"
  30. #include "multiif.h"
  31. #include "http.h"
  32. #include "url.h"
  33. #include "progress.h"
  34. #include "rtsp.h"
  35. #include "strcase.h"
  36. #include "select.h"
  37. #include "connect.h"
  38. #include "strdup.h"
  39. /* The last 3 #include files should be in this order */
  40. #include "curl_printf.h"
  41. #include "curl_memory.h"
  42. #include "memdebug.h"
  43. #define RTP_PKT_CHANNEL(p) ((int)((unsigned char)((p)[1])))
  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. /*
  84. * RTSP handler interface.
  85. */
  86. const struct Curl_handler Curl_handler_rtsp = {
  87. "RTSP", /* scheme */
  88. rtsp_setup_connection, /* setup_connection */
  89. rtsp_do, /* do_it */
  90. rtsp_done, /* done */
  91. ZERO_NULL, /* do_more */
  92. rtsp_connect, /* connect_it */
  93. ZERO_NULL, /* connecting */
  94. ZERO_NULL, /* doing */
  95. ZERO_NULL, /* proto_getsock */
  96. rtsp_getsock_do, /* doing_getsock */
  97. ZERO_NULL, /* domore_getsock */
  98. ZERO_NULL, /* perform_getsock */
  99. rtsp_disconnect, /* disconnect */
  100. rtsp_rtp_readwrite, /* readwrite */
  101. rtsp_conncheck, /* connection_check */
  102. ZERO_NULL, /* attach connection */
  103. PORT_RTSP, /* defport */
  104. CURLPROTO_RTSP, /* protocol */
  105. CURLPROTO_RTSP, /* family */
  106. PROTOPT_NONE /* flags */
  107. };
  108. static CURLcode rtsp_setup_connection(struct Curl_easy *data,
  109. struct connectdata *conn)
  110. {
  111. struct RTSP *rtsp;
  112. (void)conn;
  113. data->req.p.rtsp = rtsp = calloc(1, sizeof(struct RTSP));
  114. if(!rtsp)
  115. return CURLE_OUT_OF_MEMORY;
  116. return CURLE_OK;
  117. }
  118. /*
  119. * The server may send us RTP data at any point, and RTSPREQ_RECEIVE does not
  120. * want to block the application forever while receiving a stream. Therefore,
  121. * we cannot assume that an RTSP socket is dead just because it is readable.
  122. *
  123. * Instead, if it is readable, run Curl_connalive() to peek at the socket
  124. * and distinguish between closed and data.
  125. */
  126. static bool rtsp_connisdead(struct Curl_easy *data, struct connectdata *check)
  127. {
  128. int sval;
  129. bool ret_val = TRUE;
  130. sval = SOCKET_READABLE(check->sock[FIRSTSOCKET], 0);
  131. if(sval == 0) {
  132. /* timeout */
  133. ret_val = FALSE;
  134. }
  135. else if(sval & CURL_CSELECT_ERR) {
  136. /* socket is in an error state */
  137. ret_val = TRUE;
  138. }
  139. else if(sval & CURL_CSELECT_IN) {
  140. /* readable with no error. could still be closed */
  141. ret_val = !Curl_connalive(data, check);
  142. }
  143. return ret_val;
  144. }
  145. /*
  146. * Function to check on various aspects of a connection.
  147. */
  148. static unsigned int rtsp_conncheck(struct Curl_easy *data,
  149. struct connectdata *conn,
  150. unsigned int checks_to_perform)
  151. {
  152. unsigned int ret_val = CONNRESULT_NONE;
  153. (void)data;
  154. if(checks_to_perform & CONNCHECK_ISDEAD) {
  155. if(rtsp_connisdead(data, conn))
  156. ret_val |= CONNRESULT_DEAD;
  157. }
  158. return ret_val;
  159. }
  160. static CURLcode rtsp_connect(struct Curl_easy *data, bool *done)
  161. {
  162. CURLcode httpStatus;
  163. httpStatus = Curl_http_connect(data, done);
  164. /* Initialize the CSeq if not already done */
  165. if(data->state.rtsp_next_client_CSeq == 0)
  166. data->state.rtsp_next_client_CSeq = 1;
  167. if(data->state.rtsp_next_server_CSeq == 0)
  168. data->state.rtsp_next_server_CSeq = 1;
  169. data->conn->proto.rtspc.rtp_channel = -1;
  170. return httpStatus;
  171. }
  172. static CURLcode rtsp_disconnect(struct Curl_easy *data,
  173. struct connectdata *conn, bool dead)
  174. {
  175. (void) dead;
  176. (void) data;
  177. Curl_safefree(conn->proto.rtspc.rtp_buf);
  178. return CURLE_OK;
  179. }
  180. static CURLcode rtsp_done(struct Curl_easy *data,
  181. CURLcode status, bool premature)
  182. {
  183. struct RTSP *rtsp = data->req.p.rtsp;
  184. CURLcode httpStatus;
  185. /* Bypass HTTP empty-reply checks on receive */
  186. if(data->set.rtspreq == RTSPREQ_RECEIVE)
  187. premature = TRUE;
  188. httpStatus = Curl_http_done(data, status, premature);
  189. if(rtsp && !status && !httpStatus) {
  190. /* Check the sequence numbers */
  191. long CSeq_sent = rtsp->CSeq_sent;
  192. long CSeq_recv = rtsp->CSeq_recv;
  193. if((data->set.rtspreq != RTSPREQ_RECEIVE) && (CSeq_sent != CSeq_recv)) {
  194. failf(data,
  195. "The CSeq of this request %ld did not match the response %ld",
  196. CSeq_sent, CSeq_recv);
  197. return CURLE_RTSP_CSEQ_ERROR;
  198. }
  199. if(data->set.rtspreq == RTSPREQ_RECEIVE &&
  200. (data->conn->proto.rtspc.rtp_channel == -1)) {
  201. infof(data, "Got an RTP Receive with a CSeq of %ld", CSeq_recv);
  202. }
  203. }
  204. return httpStatus;
  205. }
  206. static CURLcode rtsp_do(struct Curl_easy *data, bool *done)
  207. {
  208. struct connectdata *conn = data->conn;
  209. CURLcode result = CURLE_OK;
  210. Curl_RtspReq rtspreq = data->set.rtspreq;
  211. struct RTSP *rtsp = data->req.p.rtsp;
  212. struct dynbuf req_buffer;
  213. curl_off_t postsize = 0; /* for ANNOUNCE and SET_PARAMETER */
  214. curl_off_t putsize = 0; /* for ANNOUNCE and SET_PARAMETER */
  215. const char *p_request = NULL;
  216. const char *p_session_id = NULL;
  217. const char *p_accept = NULL;
  218. const char *p_accept_encoding = NULL;
  219. const char *p_range = NULL;
  220. const char *p_referrer = NULL;
  221. const char *p_stream_uri = NULL;
  222. const char *p_transport = NULL;
  223. const char *p_uagent = NULL;
  224. const char *p_proxyuserpwd = NULL;
  225. const char *p_userpwd = NULL;
  226. *done = TRUE;
  227. rtsp->CSeq_sent = data->state.rtsp_next_client_CSeq;
  228. rtsp->CSeq_recv = 0;
  229. /* Setup the first_* fields to allow auth details get sent
  230. to this origin */
  231. if(!data->state.first_host) {
  232. data->state.first_host = strdup(conn->host.name);
  233. if(!data->state.first_host)
  234. return CURLE_OUT_OF_MEMORY;
  235. data->state.first_remote_port = conn->remote_port;
  236. data->state.first_remote_protocol = conn->handler->protocol;
  237. }
  238. /* Setup the 'p_request' pointer to the proper p_request string
  239. * Since all RTSP requests are included here, there is no need to
  240. * support custom requests like HTTP.
  241. **/
  242. data->req.no_body = TRUE; /* most requests don't contain a body */
  243. switch(rtspreq) {
  244. default:
  245. failf(data, "Got invalid RTSP request");
  246. return CURLE_BAD_FUNCTION_ARGUMENT;
  247. case RTSPREQ_OPTIONS:
  248. p_request = "OPTIONS";
  249. break;
  250. case RTSPREQ_DESCRIBE:
  251. p_request = "DESCRIBE";
  252. data->req.no_body = FALSE;
  253. break;
  254. case RTSPREQ_ANNOUNCE:
  255. p_request = "ANNOUNCE";
  256. break;
  257. case RTSPREQ_SETUP:
  258. p_request = "SETUP";
  259. break;
  260. case RTSPREQ_PLAY:
  261. p_request = "PLAY";
  262. break;
  263. case RTSPREQ_PAUSE:
  264. p_request = "PAUSE";
  265. break;
  266. case RTSPREQ_TEARDOWN:
  267. p_request = "TEARDOWN";
  268. break;
  269. case RTSPREQ_GET_PARAMETER:
  270. /* GET_PARAMETER's no_body status is determined later */
  271. p_request = "GET_PARAMETER";
  272. data->req.no_body = FALSE;
  273. break;
  274. case RTSPREQ_SET_PARAMETER:
  275. p_request = "SET_PARAMETER";
  276. break;
  277. case RTSPREQ_RECORD:
  278. p_request = "RECORD";
  279. break;
  280. case RTSPREQ_RECEIVE:
  281. p_request = "";
  282. /* Treat interleaved RTP as body */
  283. data->req.no_body = FALSE;
  284. break;
  285. case RTSPREQ_LAST:
  286. failf(data, "Got invalid RTSP request: RTSPREQ_LAST");
  287. return CURLE_BAD_FUNCTION_ARGUMENT;
  288. }
  289. if(rtspreq == RTSPREQ_RECEIVE) {
  290. Curl_setup_transfer(data, FIRSTSOCKET, -1, TRUE, -1);
  291. return result;
  292. }
  293. p_session_id = data->set.str[STRING_RTSP_SESSION_ID];
  294. if(!p_session_id &&
  295. (rtspreq & ~(RTSPREQ_OPTIONS | RTSPREQ_DESCRIBE | RTSPREQ_SETUP))) {
  296. failf(data, "Refusing to issue an RTSP request [%s] without a session ID.",
  297. p_request);
  298. return CURLE_BAD_FUNCTION_ARGUMENT;
  299. }
  300. /* Stream URI. Default to server '*' if not specified */
  301. if(data->set.str[STRING_RTSP_STREAM_URI]) {
  302. p_stream_uri = data->set.str[STRING_RTSP_STREAM_URI];
  303. }
  304. else {
  305. p_stream_uri = "*";
  306. }
  307. /* Transport Header for SETUP requests */
  308. p_transport = Curl_checkheaders(data, STRCONST("Transport"));
  309. if(rtspreq == RTSPREQ_SETUP && !p_transport) {
  310. /* New Transport: setting? */
  311. if(data->set.str[STRING_RTSP_TRANSPORT]) {
  312. Curl_safefree(data->state.aptr.rtsp_transport);
  313. data->state.aptr.rtsp_transport =
  314. aprintf("Transport: %s\r\n",
  315. data->set.str[STRING_RTSP_TRANSPORT]);
  316. if(!data->state.aptr.rtsp_transport)
  317. return CURLE_OUT_OF_MEMORY;
  318. }
  319. else {
  320. failf(data,
  321. "Refusing to issue an RTSP SETUP without a Transport: header.");
  322. return CURLE_BAD_FUNCTION_ARGUMENT;
  323. }
  324. p_transport = data->state.aptr.rtsp_transport;
  325. }
  326. /* Accept Headers for DESCRIBE requests */
  327. if(rtspreq == RTSPREQ_DESCRIBE) {
  328. /* Accept Header */
  329. p_accept = Curl_checkheaders(data, STRCONST("Accept"))?
  330. NULL:"Accept: application/sdp\r\n";
  331. /* Accept-Encoding header */
  332. if(!Curl_checkheaders(data, STRCONST("Accept-Encoding")) &&
  333. data->set.str[STRING_ENCODING]) {
  334. Curl_safefree(data->state.aptr.accept_encoding);
  335. data->state.aptr.accept_encoding =
  336. aprintf("Accept-Encoding: %s\r\n", data->set.str[STRING_ENCODING]);
  337. if(!data->state.aptr.accept_encoding)
  338. return CURLE_OUT_OF_MEMORY;
  339. p_accept_encoding = data->state.aptr.accept_encoding;
  340. }
  341. }
  342. /* The User-Agent string might have been allocated in url.c already, because
  343. it might have been used in the proxy connect, but if we have got a header
  344. with the user-agent string specified, we erase the previously made string
  345. here. */
  346. if(Curl_checkheaders(data, STRCONST("User-Agent")) &&
  347. data->state.aptr.uagent) {
  348. Curl_safefree(data->state.aptr.uagent);
  349. data->state.aptr.uagent = NULL;
  350. }
  351. else if(!Curl_checkheaders(data, STRCONST("User-Agent")) &&
  352. data->set.str[STRING_USERAGENT]) {
  353. p_uagent = data->state.aptr.uagent;
  354. }
  355. /* setup the authentication headers */
  356. result = Curl_http_output_auth(data, conn, p_request, HTTPREQ_GET,
  357. p_stream_uri, FALSE);
  358. if(result)
  359. return result;
  360. p_proxyuserpwd = data->state.aptr.proxyuserpwd;
  361. p_userpwd = data->state.aptr.userpwd;
  362. /* Referrer */
  363. Curl_safefree(data->state.aptr.ref);
  364. if(data->state.referer && !Curl_checkheaders(data, STRCONST("Referer")))
  365. data->state.aptr.ref = aprintf("Referer: %s\r\n", data->state.referer);
  366. else
  367. data->state.aptr.ref = NULL;
  368. p_referrer = data->state.aptr.ref;
  369. /*
  370. * Range Header
  371. * Only applies to PLAY, PAUSE, RECORD
  372. *
  373. * Go ahead and use the Range stuff supplied for HTTP
  374. */
  375. if(data->state.use_range &&
  376. (rtspreq & (RTSPREQ_PLAY | RTSPREQ_PAUSE | RTSPREQ_RECORD))) {
  377. /* Check to see if there is a range set in the custom headers */
  378. if(!Curl_checkheaders(data, STRCONST("Range")) && data->state.range) {
  379. Curl_safefree(data->state.aptr.rangeline);
  380. data->state.aptr.rangeline = aprintf("Range: %s\r\n", data->state.range);
  381. p_range = data->state.aptr.rangeline;
  382. }
  383. }
  384. /*
  385. * Sanity check the custom headers
  386. */
  387. if(Curl_checkheaders(data, STRCONST("CSeq"))) {
  388. failf(data, "CSeq cannot be set as a custom header.");
  389. return CURLE_RTSP_CSEQ_ERROR;
  390. }
  391. if(Curl_checkheaders(data, STRCONST("Session"))) {
  392. failf(data, "Session ID cannot be set as a custom header.");
  393. return CURLE_BAD_FUNCTION_ARGUMENT;
  394. }
  395. /* Initialize a dynamic send buffer */
  396. Curl_dyn_init(&req_buffer, DYN_RTSP_REQ_HEADER);
  397. result =
  398. Curl_dyn_addf(&req_buffer,
  399. "%s %s RTSP/1.0\r\n" /* Request Stream-URI RTSP/1.0 */
  400. "CSeq: %ld\r\n", /* CSeq */
  401. p_request, p_stream_uri, rtsp->CSeq_sent);
  402. if(result)
  403. return result;
  404. /*
  405. * Rather than do a normal alloc line, keep the session_id unformatted
  406. * to make comparison easier
  407. */
  408. if(p_session_id) {
  409. result = Curl_dyn_addf(&req_buffer, "Session: %s\r\n", p_session_id);
  410. if(result)
  411. return result;
  412. }
  413. /*
  414. * Shared HTTP-like options
  415. */
  416. result = Curl_dyn_addf(&req_buffer,
  417. "%s" /* transport */
  418. "%s" /* accept */
  419. "%s" /* accept-encoding */
  420. "%s" /* range */
  421. "%s" /* referrer */
  422. "%s" /* user-agent */
  423. "%s" /* proxyuserpwd */
  424. "%s" /* userpwd */
  425. ,
  426. p_transport ? p_transport : "",
  427. p_accept ? p_accept : "",
  428. p_accept_encoding ? p_accept_encoding : "",
  429. p_range ? p_range : "",
  430. p_referrer ? p_referrer : "",
  431. p_uagent ? p_uagent : "",
  432. p_proxyuserpwd ? p_proxyuserpwd : "",
  433. p_userpwd ? p_userpwd : "");
  434. /*
  435. * Free userpwd now --- cannot reuse this for Negotiate and possibly NTLM
  436. * with basic and digest, it will be freed anyway by the next request
  437. */
  438. Curl_safefree(data->state.aptr.userpwd);
  439. data->state.aptr.userpwd = NULL;
  440. if(result)
  441. return result;
  442. if((rtspreq == RTSPREQ_SETUP) || (rtspreq == RTSPREQ_DESCRIBE)) {
  443. result = Curl_add_timecondition(data, &req_buffer);
  444. if(result)
  445. return result;
  446. }
  447. result = Curl_add_custom_headers(data, FALSE, &req_buffer);
  448. if(result)
  449. return result;
  450. if(rtspreq == RTSPREQ_ANNOUNCE ||
  451. rtspreq == RTSPREQ_SET_PARAMETER ||
  452. rtspreq == RTSPREQ_GET_PARAMETER) {
  453. if(data->set.upload) {
  454. putsize = data->state.infilesize;
  455. data->state.httpreq = HTTPREQ_PUT;
  456. }
  457. else {
  458. postsize = (data->state.infilesize != -1)?
  459. data->state.infilesize:
  460. (data->set.postfields? (curl_off_t)strlen(data->set.postfields):0);
  461. data->state.httpreq = HTTPREQ_POST;
  462. }
  463. if(putsize > 0 || postsize > 0) {
  464. /* As stated in the http comments, it is probably not wise to
  465. * actually set a custom Content-Length in the headers */
  466. if(!Curl_checkheaders(data, STRCONST("Content-Length"))) {
  467. result =
  468. Curl_dyn_addf(&req_buffer,
  469. "Content-Length: %" CURL_FORMAT_CURL_OFF_T"\r\n",
  470. (data->set.upload ? putsize : postsize));
  471. if(result)
  472. return result;
  473. }
  474. if(rtspreq == RTSPREQ_SET_PARAMETER ||
  475. rtspreq == RTSPREQ_GET_PARAMETER) {
  476. if(!Curl_checkheaders(data, STRCONST("Content-Type"))) {
  477. result = Curl_dyn_addn(&req_buffer,
  478. STRCONST("Content-Type: "
  479. "text/parameters\r\n"));
  480. if(result)
  481. return result;
  482. }
  483. }
  484. if(rtspreq == RTSPREQ_ANNOUNCE) {
  485. if(!Curl_checkheaders(data, STRCONST("Content-Type"))) {
  486. result = Curl_dyn_addn(&req_buffer,
  487. STRCONST("Content-Type: "
  488. "application/sdp\r\n"));
  489. if(result)
  490. return result;
  491. }
  492. }
  493. data->state.expect100header = FALSE; /* RTSP posts are simple/small */
  494. }
  495. else if(rtspreq == RTSPREQ_GET_PARAMETER) {
  496. /* Check for an empty GET_PARAMETER (heartbeat) request */
  497. data->state.httpreq = HTTPREQ_HEAD;
  498. data->req.no_body = TRUE;
  499. }
  500. }
  501. /* RTSP never allows chunked transfer */
  502. data->req.forbidchunk = TRUE;
  503. /* Finish the request buffer */
  504. result = Curl_dyn_addn(&req_buffer, STRCONST("\r\n"));
  505. if(result)
  506. return result;
  507. if(postsize > 0) {
  508. result = Curl_dyn_addn(&req_buffer, data->set.postfields,
  509. (size_t)postsize);
  510. if(result)
  511. return result;
  512. }
  513. /* issue the request */
  514. result = Curl_buffer_send(&req_buffer, data,
  515. &data->info.request_size, 0, FIRSTSOCKET);
  516. if(result) {
  517. failf(data, "Failed sending RTSP request");
  518. return result;
  519. }
  520. Curl_setup_transfer(data, FIRSTSOCKET, -1, TRUE, putsize?FIRSTSOCKET:-1);
  521. /* Increment the CSeq on success */
  522. data->state.rtsp_next_client_CSeq++;
  523. if(data->req.writebytecount) {
  524. /* if a request-body has been sent off, we make sure this progress is
  525. noted properly */
  526. Curl_pgrsSetUploadCounter(data, data->req.writebytecount);
  527. if(Curl_pgrsUpdate(data))
  528. result = CURLE_ABORTED_BY_CALLBACK;
  529. }
  530. return result;
  531. }
  532. static CURLcode rtsp_rtp_readwrite(struct Curl_easy *data,
  533. struct connectdata *conn,
  534. ssize_t *nread,
  535. bool *readmore) {
  536. struct SingleRequest *k = &data->req;
  537. struct rtsp_conn *rtspc = &(conn->proto.rtspc);
  538. char *rtp; /* moving pointer to rtp data */
  539. ssize_t rtp_dataleft; /* how much data left to parse in this round */
  540. char *scratch;
  541. CURLcode result;
  542. if(rtspc->rtp_buf) {
  543. /* There was some leftover data the last time. Merge buffers */
  544. char *newptr = Curl_saferealloc(rtspc->rtp_buf,
  545. rtspc->rtp_bufsize + *nread);
  546. if(!newptr) {
  547. rtspc->rtp_buf = NULL;
  548. rtspc->rtp_bufsize = 0;
  549. return CURLE_OUT_OF_MEMORY;
  550. }
  551. rtspc->rtp_buf = newptr;
  552. memcpy(rtspc->rtp_buf + rtspc->rtp_bufsize, k->str, *nread);
  553. rtspc->rtp_bufsize += *nread;
  554. rtp = rtspc->rtp_buf;
  555. rtp_dataleft = rtspc->rtp_bufsize;
  556. }
  557. else {
  558. /* Just parse the request buffer directly */
  559. rtp = k->str;
  560. rtp_dataleft = *nread;
  561. }
  562. while((rtp_dataleft > 0) &&
  563. (rtp[0] == '$')) {
  564. if(rtp_dataleft > 4) {
  565. int rtp_length;
  566. /* Parse the header */
  567. /* The channel identifier immediately follows and is 1 byte */
  568. rtspc->rtp_channel = RTP_PKT_CHANNEL(rtp);
  569. /* The length is two bytes */
  570. rtp_length = RTP_PKT_LENGTH(rtp);
  571. if(rtp_dataleft < rtp_length + 4) {
  572. /* Need more - incomplete payload */
  573. *readmore = TRUE;
  574. break;
  575. }
  576. /* We have the full RTP interleaved packet
  577. * Write out the header including the leading '$' */
  578. DEBUGF(infof(data, "RTP write channel %d rtp_length %d",
  579. rtspc->rtp_channel, rtp_length));
  580. result = rtp_client_write(data, &rtp[0], rtp_length + 4);
  581. if(result) {
  582. failf(data, "Got an error writing an RTP packet");
  583. *readmore = FALSE;
  584. Curl_safefree(rtspc->rtp_buf);
  585. rtspc->rtp_buf = NULL;
  586. rtspc->rtp_bufsize = 0;
  587. return result;
  588. }
  589. /* Move forward in the buffer */
  590. rtp_dataleft -= rtp_length + 4;
  591. rtp += rtp_length + 4;
  592. if(data->set.rtspreq == RTSPREQ_RECEIVE) {
  593. /* If we are in a passive receive, give control back
  594. * to the app as often as we can.
  595. */
  596. k->keepon &= ~KEEP_RECV;
  597. }
  598. }
  599. else {
  600. /* Need more - incomplete header */
  601. *readmore = TRUE;
  602. break;
  603. }
  604. }
  605. if(rtp_dataleft && rtp[0] == '$') {
  606. DEBUGF(infof(data, "RTP Rewinding %zd %s", rtp_dataleft,
  607. *readmore ? "(READMORE)" : ""));
  608. /* Store the incomplete RTP packet for a "rewind" */
  609. scratch = malloc(rtp_dataleft);
  610. if(!scratch) {
  611. Curl_safefree(rtspc->rtp_buf);
  612. rtspc->rtp_buf = NULL;
  613. rtspc->rtp_bufsize = 0;
  614. return CURLE_OUT_OF_MEMORY;
  615. }
  616. memcpy(scratch, rtp, rtp_dataleft);
  617. Curl_safefree(rtspc->rtp_buf);
  618. rtspc->rtp_buf = scratch;
  619. rtspc->rtp_bufsize = rtp_dataleft;
  620. /* As far as the transfer is concerned, this data is consumed */
  621. *nread = 0;
  622. return CURLE_OK;
  623. }
  624. /* Fix up k->str to point just after the last RTP packet */
  625. k->str += *nread - rtp_dataleft;
  626. /* either all of the data has been read or...
  627. * rtp now points at the next byte to parse
  628. */
  629. if(rtp_dataleft > 0)
  630. DEBUGASSERT(k->str[0] == rtp[0]);
  631. DEBUGASSERT(rtp_dataleft <= *nread); /* sanity check */
  632. *nread = rtp_dataleft;
  633. /* If we get here, we have finished with the leftover/merge buffer */
  634. Curl_safefree(rtspc->rtp_buf);
  635. rtspc->rtp_buf = NULL;
  636. rtspc->rtp_bufsize = 0;
  637. return CURLE_OK;
  638. }
  639. static
  640. CURLcode rtp_client_write(struct Curl_easy *data, char *ptr, size_t len)
  641. {
  642. size_t wrote;
  643. curl_write_callback writeit;
  644. void *user_ptr;
  645. if(len == 0) {
  646. failf(data, "Cannot write a 0 size RTP packet.");
  647. return CURLE_WRITE_ERROR;
  648. }
  649. /* If the user has configured CURLOPT_INTERLEAVEFUNCTION then use that
  650. function and any configured CURLOPT_INTERLEAVEDATA to write out the RTP
  651. data. Otherwise, use the CURLOPT_WRITEFUNCTION with the CURLOPT_WRITEDATA
  652. pointer to write out the RTP data. */
  653. if(data->set.fwrite_rtp) {
  654. writeit = data->set.fwrite_rtp;
  655. user_ptr = data->set.rtp_out;
  656. }
  657. else {
  658. writeit = data->set.fwrite_func;
  659. user_ptr = data->set.out;
  660. }
  661. Curl_set_in_callback(data, true);
  662. wrote = writeit(ptr, 1, len, user_ptr);
  663. Curl_set_in_callback(data, false);
  664. if(CURL_WRITEFUNC_PAUSE == wrote) {
  665. failf(data, "Cannot pause RTP");
  666. return CURLE_WRITE_ERROR;
  667. }
  668. if(wrote != len) {
  669. failf(data, "Failed writing RTP data");
  670. return CURLE_WRITE_ERROR;
  671. }
  672. return CURLE_OK;
  673. }
  674. CURLcode Curl_rtsp_parseheader(struct Curl_easy *data, char *header)
  675. {
  676. long CSeq = 0;
  677. if(checkprefix("CSeq:", header)) {
  678. /* Store the received CSeq. Match is verified in rtsp_done */
  679. int nc = sscanf(&header[4], ": %ld", &CSeq);
  680. if(nc == 1) {
  681. struct RTSP *rtsp = data->req.p.rtsp;
  682. rtsp->CSeq_recv = CSeq; /* mark the request */
  683. data->state.rtsp_CSeq_recv = CSeq; /* update the handle */
  684. }
  685. else {
  686. failf(data, "Unable to read the CSeq header: [%s]", header);
  687. return CURLE_RTSP_CSEQ_ERROR;
  688. }
  689. }
  690. else if(checkprefix("Session:", header)) {
  691. char *start;
  692. char *end;
  693. size_t idlen;
  694. /* Find the first non-space letter */
  695. start = header + 8;
  696. while(*start && ISBLANK(*start))
  697. start++;
  698. if(!*start) {
  699. failf(data, "Got a blank Session ID");
  700. return CURLE_RTSP_SESSION_ERROR;
  701. }
  702. /* Find the end of Session ID
  703. *
  704. * Allow any non whitespace content, up to the field separator or end of
  705. * line. RFC 2326 isn't 100% clear on the session ID and for example
  706. * gstreamer does url-encoded session ID's not covered by the standard.
  707. */
  708. end = start;
  709. while(*end && *end != ';' && !ISSPACE(*end))
  710. end++;
  711. idlen = end - start;
  712. if(data->set.str[STRING_RTSP_SESSION_ID]) {
  713. /* If the Session ID is set, then compare */
  714. if(strlen(data->set.str[STRING_RTSP_SESSION_ID]) != idlen ||
  715. strncmp(start, data->set.str[STRING_RTSP_SESSION_ID], idlen) != 0) {
  716. failf(data, "Got RTSP Session ID Line [%s], but wanted ID [%s]",
  717. start, data->set.str[STRING_RTSP_SESSION_ID]);
  718. return CURLE_RTSP_SESSION_ERROR;
  719. }
  720. }
  721. else {
  722. /* If the Session ID is not set, and we find it in a response, then set
  723. * it.
  724. */
  725. /* Copy the id substring into a new buffer */
  726. data->set.str[STRING_RTSP_SESSION_ID] = malloc(idlen + 1);
  727. if(!data->set.str[STRING_RTSP_SESSION_ID])
  728. return CURLE_OUT_OF_MEMORY;
  729. memcpy(data->set.str[STRING_RTSP_SESSION_ID], start, idlen);
  730. (data->set.str[STRING_RTSP_SESSION_ID])[idlen] = '\0';
  731. }
  732. }
  733. return CURLE_OK;
  734. }
  735. #endif /* CURL_DISABLE_RTSP or using Hyper */