2
0

rtsp.c 26 KB

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