rtsp.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2020, 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.haxx.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. #ifndef CURL_DISABLE_RTSP
  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 connectdata *conn, bool *done);
  46. static CURLcode rtsp_done(struct connectdata *conn, CURLcode, bool premature);
  47. static CURLcode rtsp_connect(struct connectdata *conn, bool *done);
  48. static CURLcode rtsp_disconnect(struct connectdata *conn, bool dead);
  49. static int rtsp_getsock_do(struct connectdata *conn, curl_socket_t *socks);
  50. /*
  51. * Parse and write out any available RTP data.
  52. *
  53. * nread: amount of data left after k->str. will be modified if RTP
  54. * data is parsed and k->str is moved up
  55. * readmore: whether or not the RTP parser needs more data right away
  56. */
  57. static CURLcode rtsp_rtp_readwrite(struct Curl_easy *data,
  58. struct connectdata *conn,
  59. ssize_t *nread,
  60. bool *readmore);
  61. static CURLcode rtsp_setup_connection(struct connectdata *conn);
  62. static unsigned int rtsp_conncheck(struct connectdata *check,
  63. unsigned int checks_to_perform);
  64. /* this returns the socket to wait for in the DO and DOING state for the multi
  65. interface and then we're always _sending_ a request and thus we wait for
  66. the single socket to become writable only */
  67. static int rtsp_getsock_do(struct connectdata *conn,
  68. curl_socket_t *socks)
  69. {
  70. /* write mode */
  71. socks[0] = conn->sock[FIRSTSOCKET];
  72. return GETSOCK_WRITESOCK(0);
  73. }
  74. static
  75. CURLcode rtp_client_write(struct connectdata *conn, char *ptr, size_t len);
  76. /*
  77. * RTSP handler interface.
  78. */
  79. const struct Curl_handler Curl_handler_rtsp = {
  80. "RTSP", /* scheme */
  81. rtsp_setup_connection, /* setup_connection */
  82. rtsp_do, /* do_it */
  83. rtsp_done, /* done */
  84. ZERO_NULL, /* do_more */
  85. rtsp_connect, /* connect_it */
  86. ZERO_NULL, /* connecting */
  87. ZERO_NULL, /* doing */
  88. ZERO_NULL, /* proto_getsock */
  89. rtsp_getsock_do, /* doing_getsock */
  90. ZERO_NULL, /* domore_getsock */
  91. ZERO_NULL, /* perform_getsock */
  92. rtsp_disconnect, /* disconnect */
  93. rtsp_rtp_readwrite, /* readwrite */
  94. rtsp_conncheck, /* connection_check */
  95. PORT_RTSP, /* defport */
  96. CURLPROTO_RTSP, /* protocol */
  97. PROTOPT_NONE /* flags */
  98. };
  99. static CURLcode rtsp_setup_connection(struct connectdata *conn)
  100. {
  101. struct RTSP *rtsp;
  102. conn->data->req.protop = rtsp = calloc(1, sizeof(struct RTSP));
  103. if(!rtsp)
  104. return CURLE_OUT_OF_MEMORY;
  105. return CURLE_OK;
  106. }
  107. /*
  108. * The server may send us RTP data at any point, and RTSPREQ_RECEIVE does not
  109. * want to block the application forever while receiving a stream. Therefore,
  110. * we cannot assume that an RTSP socket is dead just because it is readable.
  111. *
  112. * Instead, if it is readable, run Curl_connalive() to peek at the socket
  113. * and distinguish between closed and data.
  114. */
  115. static bool rtsp_connisdead(struct connectdata *check)
  116. {
  117. int sval;
  118. bool ret_val = TRUE;
  119. sval = SOCKET_READABLE(check->sock[FIRSTSOCKET], 0);
  120. if(sval == 0) {
  121. /* timeout */
  122. ret_val = FALSE;
  123. }
  124. else if(sval & CURL_CSELECT_ERR) {
  125. /* socket is in an error state */
  126. ret_val = TRUE;
  127. }
  128. else if(sval & CURL_CSELECT_IN) {
  129. /* readable with no error. could still be closed */
  130. ret_val = !Curl_connalive(check);
  131. }
  132. return ret_val;
  133. }
  134. /*
  135. * Function to check on various aspects of a connection.
  136. */
  137. static unsigned int rtsp_conncheck(struct connectdata *check,
  138. unsigned int checks_to_perform)
  139. {
  140. unsigned int ret_val = CONNRESULT_NONE;
  141. if(checks_to_perform & CONNCHECK_ISDEAD) {
  142. if(rtsp_connisdead(check))
  143. ret_val |= CONNRESULT_DEAD;
  144. }
  145. return ret_val;
  146. }
  147. static CURLcode rtsp_connect(struct connectdata *conn, bool *done)
  148. {
  149. CURLcode httpStatus;
  150. struct Curl_easy *data = conn->data;
  151. httpStatus = Curl_http_connect(conn, done);
  152. /* Initialize the CSeq if not already done */
  153. if(data->state.rtsp_next_client_CSeq == 0)
  154. data->state.rtsp_next_client_CSeq = 1;
  155. if(data->state.rtsp_next_server_CSeq == 0)
  156. data->state.rtsp_next_server_CSeq = 1;
  157. conn->proto.rtspc.rtp_channel = -1;
  158. return httpStatus;
  159. }
  160. static CURLcode rtsp_disconnect(struct connectdata *conn, bool dead)
  161. {
  162. (void) dead;
  163. Curl_safefree(conn->proto.rtspc.rtp_buf);
  164. return CURLE_OK;
  165. }
  166. static CURLcode rtsp_done(struct connectdata *conn,
  167. CURLcode status, bool premature)
  168. {
  169. struct Curl_easy *data = conn->data;
  170. struct RTSP *rtsp = data->req.protop;
  171. CURLcode httpStatus;
  172. /* Bypass HTTP empty-reply checks on receive */
  173. if(data->set.rtspreq == RTSPREQ_RECEIVE)
  174. premature = TRUE;
  175. httpStatus = Curl_http_done(conn, status, premature);
  176. if(rtsp) {
  177. /* Check the sequence numbers */
  178. long CSeq_sent = rtsp->CSeq_sent;
  179. long CSeq_recv = rtsp->CSeq_recv;
  180. if((data->set.rtspreq != RTSPREQ_RECEIVE) && (CSeq_sent != CSeq_recv)) {
  181. failf(data,
  182. "The CSeq of this request %ld did not match the response %ld",
  183. CSeq_sent, CSeq_recv);
  184. return CURLE_RTSP_CSEQ_ERROR;
  185. }
  186. if(data->set.rtspreq == RTSPREQ_RECEIVE &&
  187. (conn->proto.rtspc.rtp_channel == -1)) {
  188. infof(data, "Got an RTP Receive with a CSeq of %ld\n", CSeq_recv);
  189. }
  190. }
  191. return httpStatus;
  192. }
  193. static CURLcode rtsp_do(struct connectdata *conn, bool *done)
  194. {
  195. struct Curl_easy *data = conn->data;
  196. CURLcode result = CURLE_OK;
  197. Curl_RtspReq rtspreq = data->set.rtspreq;
  198. struct RTSP *rtsp = data->req.protop;
  199. struct dynbuf req_buffer;
  200. curl_off_t postsize = 0; /* for ANNOUNCE and SET_PARAMETER */
  201. curl_off_t putsize = 0; /* for ANNOUNCE and SET_PARAMETER */
  202. const char *p_request = NULL;
  203. const char *p_session_id = NULL;
  204. const char *p_accept = NULL;
  205. const char *p_accept_encoding = NULL;
  206. const char *p_range = NULL;
  207. const char *p_referrer = NULL;
  208. const char *p_stream_uri = NULL;
  209. const char *p_transport = NULL;
  210. const char *p_uagent = NULL;
  211. const char *p_proxyuserpwd = NULL;
  212. const char *p_userpwd = NULL;
  213. *done = TRUE;
  214. rtsp->CSeq_sent = data->state.rtsp_next_client_CSeq;
  215. rtsp->CSeq_recv = 0;
  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->set.opt_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->set.opt_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->set.opt_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->set.opt_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(conn, "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(conn, "Accept")?
  308. NULL:"Accept: application/sdp\r\n";
  309. /* Accept-Encoding header */
  310. if(!Curl_checkheaders(conn, "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(conn, "User-Agent") && data->state.aptr.uagent) {
  325. Curl_safefree(data->state.aptr.uagent);
  326. data->state.aptr.uagent = NULL;
  327. }
  328. else if(!Curl_checkheaders(conn, "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(conn, p_request, p_stream_uri, FALSE);
  334. if(result)
  335. return result;
  336. p_proxyuserpwd = data->state.aptr.proxyuserpwd;
  337. p_userpwd = data->state.aptr.userpwd;
  338. /* Referrer */
  339. Curl_safefree(data->state.aptr.ref);
  340. if(data->change.referer && !Curl_checkheaders(conn, "Referer"))
  341. data->state.aptr.ref = aprintf("Referer: %s\r\n", data->change.referer);
  342. else
  343. data->state.aptr.ref = NULL;
  344. p_referrer = data->state.aptr.ref;
  345. /*
  346. * Range Header
  347. * Only applies to PLAY, PAUSE, RECORD
  348. *
  349. * Go ahead and use the Range stuff supplied for HTTP
  350. */
  351. if(data->state.use_range &&
  352. (rtspreq & (RTSPREQ_PLAY | RTSPREQ_PAUSE | RTSPREQ_RECORD))) {
  353. /* Check to see if there is a range set in the custom headers */
  354. if(!Curl_checkheaders(conn, "Range") && data->state.range) {
  355. Curl_safefree(data->state.aptr.rangeline);
  356. data->state.aptr.rangeline = aprintf("Range: %s\r\n", data->state.range);
  357. p_range = data->state.aptr.rangeline;
  358. }
  359. }
  360. /*
  361. * Sanity check the custom headers
  362. */
  363. if(Curl_checkheaders(conn, "CSeq")) {
  364. failf(data, "CSeq cannot be set as a custom header.");
  365. return CURLE_RTSP_CSEQ_ERROR;
  366. }
  367. if(Curl_checkheaders(conn, "Session")) {
  368. failf(data, "Session ID cannot be set as a custom header.");
  369. return CURLE_BAD_FUNCTION_ARGUMENT;
  370. }
  371. /* Initialize a dynamic send buffer */
  372. Curl_dyn_init(&req_buffer, DYN_RTSP_REQ_HEADER);
  373. result =
  374. Curl_dyn_addf(&req_buffer,
  375. "%s %s RTSP/1.0\r\n" /* Request Stream-URI RTSP/1.0 */
  376. "CSeq: %ld\r\n", /* CSeq */
  377. p_request, p_stream_uri, rtsp->CSeq_sent);
  378. if(result)
  379. return result;
  380. /*
  381. * Rather than do a normal alloc line, keep the session_id unformatted
  382. * to make comparison easier
  383. */
  384. if(p_session_id) {
  385. result = Curl_dyn_addf(&req_buffer, "Session: %s\r\n", p_session_id);
  386. if(result)
  387. return result;
  388. }
  389. /*
  390. * Shared HTTP-like options
  391. */
  392. result = Curl_dyn_addf(&req_buffer,
  393. "%s" /* transport */
  394. "%s" /* accept */
  395. "%s" /* accept-encoding */
  396. "%s" /* range */
  397. "%s" /* referrer */
  398. "%s" /* user-agent */
  399. "%s" /* proxyuserpwd */
  400. "%s" /* userpwd */
  401. ,
  402. p_transport ? p_transport : "",
  403. p_accept ? p_accept : "",
  404. p_accept_encoding ? p_accept_encoding : "",
  405. p_range ? p_range : "",
  406. p_referrer ? p_referrer : "",
  407. p_uagent ? p_uagent : "",
  408. p_proxyuserpwd ? p_proxyuserpwd : "",
  409. p_userpwd ? p_userpwd : "");
  410. /*
  411. * Free userpwd now --- cannot reuse this for Negotiate and possibly NTLM
  412. * with basic and digest, it will be freed anyway by the next request
  413. */
  414. Curl_safefree(data->state.aptr.userpwd);
  415. data->state.aptr.userpwd = NULL;
  416. if(result)
  417. return result;
  418. if((rtspreq == RTSPREQ_SETUP) || (rtspreq == RTSPREQ_DESCRIBE)) {
  419. result = Curl_add_timecondition(conn, &req_buffer);
  420. if(result)
  421. return result;
  422. }
  423. result = Curl_add_custom_headers(conn, FALSE, &req_buffer);
  424. if(result)
  425. return result;
  426. if(rtspreq == RTSPREQ_ANNOUNCE ||
  427. rtspreq == RTSPREQ_SET_PARAMETER ||
  428. rtspreq == RTSPREQ_GET_PARAMETER) {
  429. if(data->set.upload) {
  430. putsize = data->state.infilesize;
  431. data->state.httpreq = HTTPREQ_PUT;
  432. }
  433. else {
  434. postsize = (data->state.infilesize != -1)?
  435. data->state.infilesize:
  436. (data->set.postfields? (curl_off_t)strlen(data->set.postfields):0);
  437. data->state.httpreq = HTTPREQ_POST;
  438. }
  439. if(putsize > 0 || postsize > 0) {
  440. /* As stated in the http comments, it is probably not wise to
  441. * actually set a custom Content-Length in the headers */
  442. if(!Curl_checkheaders(conn, "Content-Length")) {
  443. result =
  444. Curl_dyn_addf(&req_buffer,
  445. "Content-Length: %" CURL_FORMAT_CURL_OFF_T"\r\n",
  446. (data->set.upload ? putsize : postsize));
  447. if(result)
  448. return result;
  449. }
  450. if(rtspreq == RTSPREQ_SET_PARAMETER ||
  451. rtspreq == RTSPREQ_GET_PARAMETER) {
  452. if(!Curl_checkheaders(conn, "Content-Type")) {
  453. result = Curl_dyn_addf(&req_buffer,
  454. "Content-Type: text/parameters\r\n");
  455. if(result)
  456. return result;
  457. }
  458. }
  459. if(rtspreq == RTSPREQ_ANNOUNCE) {
  460. if(!Curl_checkheaders(conn, "Content-Type")) {
  461. result = Curl_dyn_addf(&req_buffer,
  462. "Content-Type: 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->set.opt_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_add(&req_buffer, "\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, conn,
  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(conn))
  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\n",
  553. rtspc->rtp_channel, rtp_length));
  554. result = rtp_client_write(conn, &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 != 0 && rtp[0] == '$') {
  580. DEBUGF(infof(data, "RTP Rewinding %zd %s\n", 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 connectdata *conn, char *ptr, size_t len)
  615. {
  616. struct Curl_easy *data = conn->data;
  617. size_t wrote;
  618. curl_write_callback writeit;
  619. void *user_ptr;
  620. if(len == 0) {
  621. failf(data, "Cannot write a 0 size RTP packet.");
  622. return CURLE_WRITE_ERROR;
  623. }
  624. /* If the user has configured CURLOPT_INTERLEAVEFUNCTION then use that
  625. function and any configured CURLOPT_INTERLEAVEDATA to write out the RTP
  626. data. Otherwise, use the CURLOPT_WRITEFUNCTION with the CURLOPT_WRITEDATA
  627. pointer to write out the RTP data. */
  628. if(data->set.fwrite_rtp) {
  629. writeit = data->set.fwrite_rtp;
  630. user_ptr = data->set.rtp_out;
  631. }
  632. else {
  633. writeit = data->set.fwrite_func;
  634. user_ptr = data->set.out;
  635. }
  636. Curl_set_in_callback(data, true);
  637. wrote = writeit(ptr, 1, len, user_ptr);
  638. Curl_set_in_callback(data, false);
  639. if(CURL_WRITEFUNC_PAUSE == wrote) {
  640. failf(data, "Cannot pause RTP");
  641. return CURLE_WRITE_ERROR;
  642. }
  643. if(wrote != len) {
  644. failf(data, "Failed writing RTP data");
  645. return CURLE_WRITE_ERROR;
  646. }
  647. return CURLE_OK;
  648. }
  649. CURLcode Curl_rtsp_parseheader(struct connectdata *conn,
  650. char *header)
  651. {
  652. struct Curl_easy *data = conn->data;
  653. long CSeq = 0;
  654. if(checkprefix("CSeq:", header)) {
  655. /* Store the received CSeq. Match is verified in rtsp_done */
  656. int nc = sscanf(&header[4], ": %ld", &CSeq);
  657. if(nc == 1) {
  658. struct RTSP *rtsp = data->req.protop;
  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. /* Find the first non-space letter */
  670. start = header + 8;
  671. while(*start && ISSPACE(*start))
  672. start++;
  673. if(!*start) {
  674. failf(data, "Got a blank Session ID");
  675. }
  676. else if(data->set.str[STRING_RTSP_SESSION_ID]) {
  677. /* If the Session ID is set, then compare */
  678. if(strncmp(start, data->set.str[STRING_RTSP_SESSION_ID],
  679. strlen(data->set.str[STRING_RTSP_SESSION_ID])) != 0) {
  680. failf(data, "Got RTSP Session ID Line [%s], but wanted ID [%s]",
  681. start, data->set.str[STRING_RTSP_SESSION_ID]);
  682. return CURLE_RTSP_SESSION_ERROR;
  683. }
  684. }
  685. else {
  686. /* If the Session ID is not set, and we find it in a response, then set
  687. * it.
  688. *
  689. * Allow any non whitespace content, up to the field separator or end of
  690. * line. RFC 2326 isn't 100% clear on the session ID and for example
  691. * gstreamer does url-encoded session ID's not covered by the standard.
  692. */
  693. char *end = start;
  694. while(*end && *end != ';' && !ISSPACE(*end))
  695. end++;
  696. /* Copy the id substring into a new buffer */
  697. data->set.str[STRING_RTSP_SESSION_ID] = malloc(end - start + 1);
  698. if(data->set.str[STRING_RTSP_SESSION_ID] == NULL)
  699. return CURLE_OUT_OF_MEMORY;
  700. memcpy(data->set.str[STRING_RTSP_SESSION_ID], start, end - start);
  701. (data->set.str[STRING_RTSP_SESSION_ID])[end - start] = '\0';
  702. }
  703. }
  704. return CURLE_OK;
  705. }
  706. #endif /* CURL_DISABLE_RTSP */