rtsp.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "curl_setup.h"
  25. #if !defined(CURL_DISABLE_RTSP) && !defined(USE_HYPER)
  26. #include "urldata.h"
  27. #include <curl/curl.h>
  28. #include "transfer.h"
  29. #include "sendf.h"
  30. #include "multiif.h"
  31. #include "http.h"
  32. #include "url.h"
  33. #include "progress.h"
  34. #include "rtsp.h"
  35. #include "strcase.h"
  36. #include "select.h"
  37. #include "connect.h"
  38. #include "cfilters.h"
  39. #include "strdup.h"
  40. /* The last 3 #include files should be in this order */
  41. #include "curl_printf.h"
  42. #include "curl_memory.h"
  43. #include "memdebug.h"
  44. #define RTP_PKT_LENGTH(p) ((((unsigned int)((unsigned char)((p)[2]))) << 8) | \
  45. ((unsigned 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. * @param data the transfer
  57. * @param conn the connection
  58. * @param buf data read from connection
  59. * @param blen amount of data in buf
  60. * @param consumed out, number of blen consumed
  61. * @param readmore out, TRUE iff complete buf was consumed and more data
  62. * is needed
  63. */
  64. static CURLcode rtsp_rtp_readwrite(struct Curl_easy *data,
  65. struct connectdata *conn,
  66. const char *buf,
  67. size_t blen,
  68. size_t *pconsumed,
  69. bool *readmore);
  70. static CURLcode rtsp_setup_connection(struct Curl_easy *data,
  71. struct connectdata *conn);
  72. static unsigned int rtsp_conncheck(struct Curl_easy *data,
  73. struct connectdata *check,
  74. unsigned int checks_to_perform);
  75. /* this returns the socket to wait for in the DO and DOING state for the multi
  76. interface and then we're always _sending_ a request and thus we wait for
  77. the single socket to become writable only */
  78. static int rtsp_getsock_do(struct Curl_easy *data, struct connectdata *conn,
  79. curl_socket_t *socks)
  80. {
  81. /* write mode */
  82. (void)data;
  83. socks[0] = conn->sock[FIRSTSOCKET];
  84. return GETSOCK_WRITESOCK(0);
  85. }
  86. static
  87. CURLcode rtp_client_write(struct Curl_easy *data, const char *ptr, size_t len);
  88. static
  89. CURLcode rtsp_parse_transport(struct Curl_easy *data, char *transport);
  90. /*
  91. * RTSP handler interface.
  92. */
  93. const struct Curl_handler Curl_handler_rtsp = {
  94. "RTSP", /* scheme */
  95. rtsp_setup_connection, /* setup_connection */
  96. rtsp_do, /* do_it */
  97. rtsp_done, /* done */
  98. ZERO_NULL, /* do_more */
  99. rtsp_connect, /* connect_it */
  100. ZERO_NULL, /* connecting */
  101. ZERO_NULL, /* doing */
  102. ZERO_NULL, /* proto_getsock */
  103. rtsp_getsock_do, /* doing_getsock */
  104. ZERO_NULL, /* domore_getsock */
  105. ZERO_NULL, /* perform_getsock */
  106. rtsp_disconnect, /* disconnect */
  107. rtsp_rtp_readwrite, /* readwrite */
  108. rtsp_conncheck, /* connection_check */
  109. ZERO_NULL, /* attach connection */
  110. PORT_RTSP, /* defport */
  111. CURLPROTO_RTSP, /* protocol */
  112. CURLPROTO_RTSP, /* family */
  113. PROTOPT_NONE /* flags */
  114. };
  115. #define MAX_RTP_BUFFERSIZE 1000000 /* arbitrary */
  116. static CURLcode rtsp_setup_connection(struct Curl_easy *data,
  117. struct connectdata *conn)
  118. {
  119. struct RTSP *rtsp;
  120. (void)conn;
  121. data->req.p.rtsp = rtsp = calloc(1, sizeof(struct RTSP));
  122. if(!rtsp)
  123. return CURLE_OUT_OF_MEMORY;
  124. Curl_dyn_init(&conn->proto.rtspc.buf, MAX_RTP_BUFFERSIZE);
  125. return CURLE_OK;
  126. }
  127. /*
  128. * Function to check on various aspects of a connection.
  129. */
  130. static unsigned int rtsp_conncheck(struct Curl_easy *data,
  131. struct connectdata *conn,
  132. unsigned int checks_to_perform)
  133. {
  134. unsigned int ret_val = CONNRESULT_NONE;
  135. (void)data;
  136. if(checks_to_perform & CONNCHECK_ISDEAD) {
  137. bool input_pending;
  138. if(!Curl_conn_is_alive(data, conn, &input_pending))
  139. ret_val |= CONNRESULT_DEAD;
  140. }
  141. return ret_val;
  142. }
  143. static CURLcode rtsp_connect(struct Curl_easy *data, bool *done)
  144. {
  145. CURLcode httpStatus;
  146. httpStatus = Curl_http_connect(data, done);
  147. /* Initialize the CSeq if not already done */
  148. if(data->state.rtsp_next_client_CSeq == 0)
  149. data->state.rtsp_next_client_CSeq = 1;
  150. if(data->state.rtsp_next_server_CSeq == 0)
  151. data->state.rtsp_next_server_CSeq = 1;
  152. data->conn->proto.rtspc.rtp_channel = -1;
  153. return httpStatus;
  154. }
  155. static CURLcode rtsp_disconnect(struct Curl_easy *data,
  156. struct connectdata *conn, bool dead)
  157. {
  158. (void) dead;
  159. (void) data;
  160. Curl_dyn_free(&conn->proto.rtspc.buf);
  161. return CURLE_OK;
  162. }
  163. static CURLcode rtsp_done(struct Curl_easy *data,
  164. CURLcode status, bool premature)
  165. {
  166. struct RTSP *rtsp = data->req.p.rtsp;
  167. CURLcode httpStatus;
  168. /* Bypass HTTP empty-reply checks on receive */
  169. if(data->set.rtspreq == RTSPREQ_RECEIVE)
  170. premature = TRUE;
  171. httpStatus = Curl_http_done(data, status, premature);
  172. if(rtsp && !status && !httpStatus) {
  173. /* Check the sequence numbers */
  174. long CSeq_sent = rtsp->CSeq_sent;
  175. long CSeq_recv = rtsp->CSeq_recv;
  176. if((data->set.rtspreq != RTSPREQ_RECEIVE) && (CSeq_sent != CSeq_recv)) {
  177. failf(data,
  178. "The CSeq of this request %ld did not match the response %ld",
  179. CSeq_sent, CSeq_recv);
  180. return CURLE_RTSP_CSEQ_ERROR;
  181. }
  182. if(data->set.rtspreq == RTSPREQ_RECEIVE &&
  183. (data->conn->proto.rtspc.rtp_channel == -1)) {
  184. infof(data, "Got an RTP Receive with a CSeq of %ld", CSeq_recv);
  185. }
  186. }
  187. return httpStatus;
  188. }
  189. static CURLcode rtsp_do(struct Curl_easy *data, bool *done)
  190. {
  191. struct connectdata *conn = data->conn;
  192. CURLcode result = CURLE_OK;
  193. Curl_RtspReq rtspreq = data->set.rtspreq;
  194. struct RTSP *rtsp = data->req.p.rtsp;
  195. struct dynbuf req_buffer;
  196. curl_off_t postsize = 0; /* for ANNOUNCE and SET_PARAMETER */
  197. curl_off_t putsize = 0; /* for ANNOUNCE and SET_PARAMETER */
  198. const char *p_request = NULL;
  199. const char *p_session_id = NULL;
  200. const char *p_accept = NULL;
  201. const char *p_accept_encoding = NULL;
  202. const char *p_range = NULL;
  203. const char *p_referrer = NULL;
  204. const char *p_stream_uri = NULL;
  205. const char *p_transport = NULL;
  206. const char *p_uagent = NULL;
  207. const char *p_proxyuserpwd = NULL;
  208. const char *p_userpwd = NULL;
  209. *done = TRUE;
  210. rtsp->CSeq_sent = data->state.rtsp_next_client_CSeq;
  211. rtsp->CSeq_recv = 0;
  212. /* Setup the first_* fields to allow auth details get sent
  213. to this origin */
  214. if(!data->state.first_host) {
  215. data->state.first_host = strdup(conn->host.name);
  216. if(!data->state.first_host)
  217. return CURLE_OUT_OF_MEMORY;
  218. data->state.first_remote_port = conn->remote_port;
  219. data->state.first_remote_protocol = conn->handler->protocol;
  220. }
  221. /* Setup the 'p_request' pointer to the proper p_request string
  222. * Since all RTSP requests are included here, there is no need to
  223. * support custom requests like HTTP.
  224. **/
  225. data->req.no_body = TRUE; /* most requests don't contain a body */
  226. switch(rtspreq) {
  227. default:
  228. failf(data, "Got invalid RTSP request");
  229. return CURLE_BAD_FUNCTION_ARGUMENT;
  230. case RTSPREQ_OPTIONS:
  231. p_request = "OPTIONS";
  232. break;
  233. case RTSPREQ_DESCRIBE:
  234. p_request = "DESCRIBE";
  235. data->req.no_body = FALSE;
  236. break;
  237. case RTSPREQ_ANNOUNCE:
  238. p_request = "ANNOUNCE";
  239. break;
  240. case RTSPREQ_SETUP:
  241. p_request = "SETUP";
  242. break;
  243. case RTSPREQ_PLAY:
  244. p_request = "PLAY";
  245. break;
  246. case RTSPREQ_PAUSE:
  247. p_request = "PAUSE";
  248. break;
  249. case RTSPREQ_TEARDOWN:
  250. p_request = "TEARDOWN";
  251. break;
  252. case RTSPREQ_GET_PARAMETER:
  253. /* GET_PARAMETER's no_body status is determined later */
  254. p_request = "GET_PARAMETER";
  255. data->req.no_body = FALSE;
  256. break;
  257. case RTSPREQ_SET_PARAMETER:
  258. p_request = "SET_PARAMETER";
  259. break;
  260. case RTSPREQ_RECORD:
  261. p_request = "RECORD";
  262. break;
  263. case RTSPREQ_RECEIVE:
  264. p_request = "";
  265. /* Treat interleaved RTP as body */
  266. data->req.no_body = FALSE;
  267. break;
  268. case RTSPREQ_LAST:
  269. failf(data, "Got invalid RTSP request: RTSPREQ_LAST");
  270. return CURLE_BAD_FUNCTION_ARGUMENT;
  271. }
  272. if(rtspreq == RTSPREQ_RECEIVE) {
  273. Curl_setup_transfer(data, FIRSTSOCKET, -1, TRUE, -1);
  274. return result;
  275. }
  276. p_session_id = data->set.str[STRING_RTSP_SESSION_ID];
  277. if(!p_session_id &&
  278. (rtspreq & ~(RTSPREQ_OPTIONS | RTSPREQ_DESCRIBE | RTSPREQ_SETUP))) {
  279. failf(data, "Refusing to issue an RTSP request [%s] without a session ID.",
  280. p_request);
  281. return CURLE_BAD_FUNCTION_ARGUMENT;
  282. }
  283. /* Stream URI. Default to server '*' if not specified */
  284. if(data->set.str[STRING_RTSP_STREAM_URI]) {
  285. p_stream_uri = data->set.str[STRING_RTSP_STREAM_URI];
  286. }
  287. else {
  288. p_stream_uri = "*";
  289. }
  290. /* Transport Header for SETUP requests */
  291. p_transport = Curl_checkheaders(data, STRCONST("Transport"));
  292. if(rtspreq == RTSPREQ_SETUP && !p_transport) {
  293. /* New Transport: setting? */
  294. if(data->set.str[STRING_RTSP_TRANSPORT]) {
  295. Curl_safefree(data->state.aptr.rtsp_transport);
  296. data->state.aptr.rtsp_transport =
  297. aprintf("Transport: %s\r\n",
  298. data->set.str[STRING_RTSP_TRANSPORT]);
  299. if(!data->state.aptr.rtsp_transport)
  300. return CURLE_OUT_OF_MEMORY;
  301. }
  302. else {
  303. failf(data,
  304. "Refusing to issue an RTSP SETUP without a Transport: header.");
  305. return CURLE_BAD_FUNCTION_ARGUMENT;
  306. }
  307. p_transport = data->state.aptr.rtsp_transport;
  308. }
  309. /* Accept Headers for DESCRIBE requests */
  310. if(rtspreq == RTSPREQ_DESCRIBE) {
  311. /* Accept Header */
  312. p_accept = Curl_checkheaders(data, STRCONST("Accept"))?
  313. NULL:"Accept: application/sdp\r\n";
  314. /* Accept-Encoding header */
  315. if(!Curl_checkheaders(data, STRCONST("Accept-Encoding")) &&
  316. data->set.str[STRING_ENCODING]) {
  317. Curl_safefree(data->state.aptr.accept_encoding);
  318. data->state.aptr.accept_encoding =
  319. aprintf("Accept-Encoding: %s\r\n", data->set.str[STRING_ENCODING]);
  320. if(!data->state.aptr.accept_encoding)
  321. return CURLE_OUT_OF_MEMORY;
  322. p_accept_encoding = data->state.aptr.accept_encoding;
  323. }
  324. }
  325. /* The User-Agent string might have been allocated in url.c already, because
  326. it might have been used in the proxy connect, but if we have got a header
  327. with the user-agent string specified, we erase the previously made string
  328. here. */
  329. if(Curl_checkheaders(data, STRCONST("User-Agent")) &&
  330. data->state.aptr.uagent) {
  331. Curl_safefree(data->state.aptr.uagent);
  332. }
  333. else if(!Curl_checkheaders(data, STRCONST("User-Agent")) &&
  334. data->set.str[STRING_USERAGENT]) {
  335. p_uagent = data->state.aptr.uagent;
  336. }
  337. /* setup the authentication headers */
  338. result = Curl_http_output_auth(data, conn, p_request, HTTPREQ_GET,
  339. p_stream_uri, FALSE);
  340. if(result)
  341. return result;
  342. p_proxyuserpwd = data->state.aptr.proxyuserpwd;
  343. p_userpwd = data->state.aptr.userpwd;
  344. /* Referrer */
  345. Curl_safefree(data->state.aptr.ref);
  346. if(data->state.referer && !Curl_checkheaders(data, STRCONST("Referer")))
  347. data->state.aptr.ref = aprintf("Referer: %s\r\n", data->state.referer);
  348. p_referrer = data->state.aptr.ref;
  349. /*
  350. * Range Header
  351. * Only applies to PLAY, PAUSE, RECORD
  352. *
  353. * Go ahead and use the Range stuff supplied for HTTP
  354. */
  355. if(data->state.use_range &&
  356. (rtspreq & (RTSPREQ_PLAY | RTSPREQ_PAUSE | RTSPREQ_RECORD))) {
  357. /* Check to see if there is a range set in the custom headers */
  358. if(!Curl_checkheaders(data, STRCONST("Range")) && data->state.range) {
  359. Curl_safefree(data->state.aptr.rangeline);
  360. data->state.aptr.rangeline = aprintf("Range: %s\r\n", data->state.range);
  361. p_range = data->state.aptr.rangeline;
  362. }
  363. }
  364. /*
  365. * Sanity check the custom headers
  366. */
  367. if(Curl_checkheaders(data, STRCONST("CSeq"))) {
  368. failf(data, "CSeq cannot be set as a custom header.");
  369. return CURLE_RTSP_CSEQ_ERROR;
  370. }
  371. if(Curl_checkheaders(data, STRCONST("Session"))) {
  372. failf(data, "Session ID cannot be set as a custom header.");
  373. return CURLE_BAD_FUNCTION_ARGUMENT;
  374. }
  375. /* Initialize a dynamic send buffer */
  376. Curl_dyn_init(&req_buffer, DYN_RTSP_REQ_HEADER);
  377. result =
  378. Curl_dyn_addf(&req_buffer,
  379. "%s %s RTSP/1.0\r\n" /* Request Stream-URI RTSP/1.0 */
  380. "CSeq: %ld\r\n", /* CSeq */
  381. p_request, p_stream_uri, rtsp->CSeq_sent);
  382. if(result)
  383. return result;
  384. /*
  385. * Rather than do a normal alloc line, keep the session_id unformatted
  386. * to make comparison easier
  387. */
  388. if(p_session_id) {
  389. result = Curl_dyn_addf(&req_buffer, "Session: %s\r\n", p_session_id);
  390. if(result)
  391. return result;
  392. }
  393. /*
  394. * Shared HTTP-like options
  395. */
  396. result = Curl_dyn_addf(&req_buffer,
  397. "%s" /* transport */
  398. "%s" /* accept */
  399. "%s" /* accept-encoding */
  400. "%s" /* range */
  401. "%s" /* referrer */
  402. "%s" /* user-agent */
  403. "%s" /* proxyuserpwd */
  404. "%s" /* userpwd */
  405. ,
  406. p_transport ? p_transport : "",
  407. p_accept ? p_accept : "",
  408. p_accept_encoding ? p_accept_encoding : "",
  409. p_range ? p_range : "",
  410. p_referrer ? p_referrer : "",
  411. p_uagent ? p_uagent : "",
  412. p_proxyuserpwd ? p_proxyuserpwd : "",
  413. p_userpwd ? p_userpwd : "");
  414. /*
  415. * Free userpwd now --- cannot reuse this for Negotiate and possibly NTLM
  416. * with basic and digest, it will be freed anyway by the next request
  417. */
  418. Curl_safefree(data->state.aptr.userpwd);
  419. if(result)
  420. return result;
  421. if((rtspreq == RTSPREQ_SETUP) || (rtspreq == RTSPREQ_DESCRIBE)) {
  422. result = Curl_add_timecondition(data, &req_buffer);
  423. if(result)
  424. return result;
  425. }
  426. result = Curl_add_custom_headers(data, FALSE, &req_buffer);
  427. if(result)
  428. return result;
  429. if(rtspreq == RTSPREQ_ANNOUNCE ||
  430. rtspreq == RTSPREQ_SET_PARAMETER ||
  431. rtspreq == RTSPREQ_GET_PARAMETER) {
  432. if(data->state.upload) {
  433. putsize = data->state.infilesize;
  434. data->state.httpreq = HTTPREQ_PUT;
  435. }
  436. else {
  437. postsize = (data->state.infilesize != -1)?
  438. data->state.infilesize:
  439. (data->set.postfields? (curl_off_t)strlen(data->set.postfields):0);
  440. data->state.httpreq = HTTPREQ_POST;
  441. }
  442. if(putsize > 0 || postsize > 0) {
  443. /* As stated in the http comments, it is probably not wise to
  444. * actually set a custom Content-Length in the headers */
  445. if(!Curl_checkheaders(data, STRCONST("Content-Length"))) {
  446. result =
  447. Curl_dyn_addf(&req_buffer,
  448. "Content-Length: %" CURL_FORMAT_CURL_OFF_T"\r\n",
  449. (data->state.upload ? putsize : postsize));
  450. if(result)
  451. return result;
  452. }
  453. if(rtspreq == RTSPREQ_SET_PARAMETER ||
  454. rtspreq == RTSPREQ_GET_PARAMETER) {
  455. if(!Curl_checkheaders(data, STRCONST("Content-Type"))) {
  456. result = Curl_dyn_addn(&req_buffer,
  457. STRCONST("Content-Type: "
  458. "text/parameters\r\n"));
  459. if(result)
  460. return result;
  461. }
  462. }
  463. if(rtspreq == RTSPREQ_ANNOUNCE) {
  464. if(!Curl_checkheaders(data, STRCONST("Content-Type"))) {
  465. result = Curl_dyn_addn(&req_buffer,
  466. STRCONST("Content-Type: "
  467. "application/sdp\r\n"));
  468. if(result)
  469. return result;
  470. }
  471. }
  472. data->state.expect100header = FALSE; /* RTSP posts are simple/small */
  473. }
  474. else if(rtspreq == RTSPREQ_GET_PARAMETER) {
  475. /* Check for an empty GET_PARAMETER (heartbeat) request */
  476. data->state.httpreq = HTTPREQ_HEAD;
  477. data->req.no_body = TRUE;
  478. }
  479. }
  480. /* RTSP never allows chunked transfer */
  481. data->req.forbidchunk = TRUE;
  482. /* Finish the request buffer */
  483. result = Curl_dyn_addn(&req_buffer, STRCONST("\r\n"));
  484. if(result)
  485. return result;
  486. if(postsize > 0) {
  487. result = Curl_dyn_addn(&req_buffer, data->set.postfields,
  488. (size_t)postsize);
  489. if(result)
  490. return result;
  491. }
  492. /* issue the request */
  493. result = Curl_buffer_send(&req_buffer, data, data->req.p.http,
  494. &data->info.request_size, 0, FIRSTSOCKET);
  495. if(result) {
  496. failf(data, "Failed sending RTSP request");
  497. return result;
  498. }
  499. Curl_setup_transfer(data, FIRSTSOCKET, -1, TRUE, putsize?FIRSTSOCKET:-1);
  500. /* Increment the CSeq on success */
  501. data->state.rtsp_next_client_CSeq++;
  502. if(data->req.writebytecount) {
  503. /* if a request-body has been sent off, we make sure this progress is
  504. noted properly */
  505. Curl_pgrsSetUploadCounter(data, data->req.writebytecount);
  506. if(Curl_pgrsUpdate(data))
  507. result = CURLE_ABORTED_BY_CALLBACK;
  508. }
  509. return result;
  510. }
  511. static CURLcode rtsp_filter_rtp(struct Curl_easy *data,
  512. struct connectdata *conn,
  513. const char *buf,
  514. size_t blen,
  515. bool in_body,
  516. size_t *pconsumed)
  517. {
  518. struct rtsp_conn *rtspc = &(conn->proto.rtspc);
  519. CURLcode result = CURLE_OK;
  520. *pconsumed = 0;
  521. while(blen) {
  522. switch(rtspc->state) {
  523. case RTP_PARSE_SKIP: {
  524. DEBUGASSERT(Curl_dyn_len(&rtspc->buf) == 0);
  525. if(in_body && buf[0] != '$') {
  526. /* in BODY and no valid start, do not consume and return */
  527. goto out;
  528. }
  529. while(blen && buf[0] != '$') {
  530. if(!in_body && buf[0] == 'R' &&
  531. data->set.rtspreq != RTSPREQ_RECEIVE) {
  532. if(strncmp(buf, "RTSP/", (blen < 5) ? blen : 5) == 0) {
  533. /* This could be the next response, no consume and return */
  534. if(*pconsumed) {
  535. DEBUGF(infof(data, "RTP rtsp_filter_rtp[SKIP] RTSP/ prefix, "
  536. "skipping %zd bytes of junk", *pconsumed));
  537. }
  538. rtspc->state = RTP_PARSE_SKIP;
  539. rtspc->in_header = TRUE;
  540. goto out;
  541. }
  542. }
  543. /* junk, consume without buffering */
  544. *pconsumed += 1;
  545. ++buf;
  546. --blen;
  547. }
  548. if(blen && buf[0] == '$') {
  549. /* possible start of an RTP message, buffer */
  550. if(Curl_dyn_addn(&rtspc->buf, buf, 1)) {
  551. result = CURLE_OUT_OF_MEMORY;
  552. goto out;
  553. }
  554. *pconsumed += 1;
  555. ++buf;
  556. --blen;
  557. rtspc->state = RTP_PARSE_CHANNEL;
  558. }
  559. break;
  560. }
  561. case RTP_PARSE_CHANNEL: {
  562. int idx = ((unsigned char)buf[0]) / 8;
  563. int off = ((unsigned char)buf[0]) % 8;
  564. DEBUGASSERT(Curl_dyn_len(&rtspc->buf) == 1);
  565. if(!(data->state.rtp_channel_mask[idx] & (1 << off))) {
  566. /* invalid channel number, junk or BODY data */
  567. rtspc->state = RTP_PARSE_SKIP;
  568. if(in_body) {
  569. /* we do not consume this byte, it is BODY data */
  570. DEBUGF(infof(data, "RTSP: invalid RTP channel %d in BODY, "
  571. "treating as BODY data", idx));
  572. if(*pconsumed == 0) {
  573. /* We did not consume the initial '$' in our buffer, but had
  574. * it from an earlier call. We cannot un-consume it and have
  575. * to write it directly as BODY data */
  576. result = Curl_client_write(data, CLIENTWRITE_BODY,
  577. Curl_dyn_ptr(&rtspc->buf), 1);
  578. Curl_dyn_free(&rtspc->buf);
  579. if(result)
  580. goto out;
  581. }
  582. else {
  583. /* un-consume the '$' and leave */
  584. Curl_dyn_free(&rtspc->buf);
  585. *pconsumed -= 1;
  586. --buf;
  587. ++blen;
  588. goto out;
  589. }
  590. }
  591. else {
  592. /* not BODY, forget the junk '$'. Do not consume this byte,
  593. * it might be a start */
  594. infof(data, "RTSP: invalid RTP channel %d, skipping", idx);
  595. Curl_dyn_free(&rtspc->buf);
  596. }
  597. break;
  598. }
  599. /* a valid channel, so we expect this to be a real RTP message */
  600. rtspc->rtp_channel = (unsigned char)buf[0];
  601. if(Curl_dyn_addn(&rtspc->buf, buf, 1)) {
  602. result = CURLE_OUT_OF_MEMORY;
  603. goto out;
  604. }
  605. *pconsumed += 1;
  606. ++buf;
  607. --blen;
  608. rtspc->state = RTP_PARSE_LEN;
  609. break;
  610. }
  611. case RTP_PARSE_LEN: {
  612. size_t rtp_len = Curl_dyn_len(&rtspc->buf);
  613. const char *rtp_buf;
  614. DEBUGASSERT(rtp_len >= 2 && rtp_len < 4);
  615. if(Curl_dyn_addn(&rtspc->buf, buf, 1)) {
  616. result = CURLE_OUT_OF_MEMORY;
  617. goto out;
  618. }
  619. *pconsumed += 1;
  620. ++buf;
  621. --blen;
  622. if(rtp_len == 2)
  623. break;
  624. rtp_buf = Curl_dyn_ptr(&rtspc->buf);
  625. rtspc->rtp_len = RTP_PKT_LENGTH(rtp_buf) + 4;
  626. rtspc->state = RTP_PARSE_DATA;
  627. break;
  628. }
  629. case RTP_PARSE_DATA: {
  630. size_t rtp_len = Curl_dyn_len(&rtspc->buf);
  631. size_t needed;
  632. DEBUGASSERT(rtp_len < rtspc->rtp_len);
  633. needed = rtspc->rtp_len - rtp_len;
  634. if(needed <= blen) {
  635. if(Curl_dyn_addn(&rtspc->buf, buf, needed)) {
  636. result = CURLE_OUT_OF_MEMORY;
  637. goto out;
  638. }
  639. *pconsumed += needed;
  640. buf += needed;
  641. blen -= needed;
  642. /* complete RTP message in buffer */
  643. DEBUGF(infof(data, "RTP write channel %d rtp_len %zu",
  644. rtspc->rtp_channel, rtspc->rtp_len));
  645. result = rtp_client_write(data, Curl_dyn_ptr(&rtspc->buf),
  646. rtspc->rtp_len);
  647. Curl_dyn_free(&rtspc->buf);
  648. rtspc->state = RTP_PARSE_SKIP;
  649. if(result)
  650. goto out;
  651. }
  652. else {
  653. if(Curl_dyn_addn(&rtspc->buf, buf, blen)) {
  654. result = CURLE_OUT_OF_MEMORY;
  655. goto out;
  656. }
  657. *pconsumed += blen;
  658. buf += blen;
  659. blen = 0;
  660. }
  661. break;
  662. }
  663. default:
  664. DEBUGASSERT(0);
  665. return CURLE_RECV_ERROR;
  666. }
  667. }
  668. out:
  669. return result;
  670. }
  671. static CURLcode rtsp_rtp_readwrite(struct Curl_easy *data,
  672. struct connectdata *conn,
  673. const char *buf,
  674. size_t blen,
  675. size_t *pconsumed,
  676. bool *readmore)
  677. {
  678. struct rtsp_conn *rtspc = &(conn->proto.rtspc);
  679. CURLcode result = CURLE_OK;
  680. size_t consumed = 0;
  681. bool in_body;
  682. if(!data->req.header)
  683. rtspc->in_header = FALSE;
  684. in_body = (data->req.headerline && !rtspc->in_header) &&
  685. (data->req.size >= 0) &&
  686. (data->req.bytecount < data->req.size);
  687. *readmore = FALSE;
  688. *pconsumed = 0;
  689. if(!blen) {
  690. goto out;
  691. }
  692. /* If header parsing is not onging, extract RTP messages */
  693. if(!rtspc->in_header) {
  694. result = rtsp_filter_rtp(data, conn, buf, blen, in_body, &consumed);
  695. if(result)
  696. goto out;
  697. *pconsumed += consumed;
  698. buf += consumed;
  699. blen -= consumed;
  700. }
  701. /* we want to parse headers, do so */
  702. if(data->req.header && blen) {
  703. rtspc->in_header = TRUE;
  704. result = Curl_http_readwrite_headers(data, conn, buf, blen,
  705. &consumed);
  706. if(result)
  707. goto out;
  708. *pconsumed += consumed;
  709. buf += consumed;
  710. blen -= consumed;
  711. if(!data->req.header)
  712. rtspc->in_header = FALSE;
  713. if(!rtspc->in_header) {
  714. /* If header parsing is done and data left, extract RTP messages */
  715. in_body = (data->req.headerline && !rtspc->in_header) &&
  716. (data->req.size >= 0) &&
  717. (data->req.bytecount < data->req.size);
  718. result = rtsp_filter_rtp(data, conn, buf, blen, in_body, &consumed);
  719. if(result)
  720. goto out;
  721. *pconsumed += consumed;
  722. }
  723. }
  724. if(rtspc->state != RTP_PARSE_SKIP)
  725. *readmore = TRUE;
  726. out:
  727. if(!*readmore && data->set.rtspreq == RTSPREQ_RECEIVE) {
  728. /* In special mode RECEIVE, we just process one chunk of network
  729. * data, so we stop the transfer here, if we have no incomplete
  730. * RTP message pending. */
  731. data->req.keepon &= ~KEEP_RECV;
  732. }
  733. return result;
  734. }
  735. static
  736. CURLcode rtp_client_write(struct Curl_easy *data, const char *ptr, size_t len)
  737. {
  738. size_t wrote;
  739. curl_write_callback writeit;
  740. void *user_ptr;
  741. if(len == 0) {
  742. failf(data, "Cannot write a 0 size RTP packet.");
  743. return CURLE_WRITE_ERROR;
  744. }
  745. /* If the user has configured CURLOPT_INTERLEAVEFUNCTION then use that
  746. function and any configured CURLOPT_INTERLEAVEDATA to write out the RTP
  747. data. Otherwise, use the CURLOPT_WRITEFUNCTION with the CURLOPT_WRITEDATA
  748. pointer to write out the RTP data. */
  749. if(data->set.fwrite_rtp) {
  750. writeit = data->set.fwrite_rtp;
  751. user_ptr = data->set.rtp_out;
  752. }
  753. else {
  754. writeit = data->set.fwrite_func;
  755. user_ptr = data->set.out;
  756. }
  757. Curl_set_in_callback(data, true);
  758. wrote = writeit((char *)ptr, 1, len, user_ptr);
  759. Curl_set_in_callback(data, false);
  760. if(CURL_WRITEFUNC_PAUSE == wrote) {
  761. failf(data, "Cannot pause RTP");
  762. return CURLE_WRITE_ERROR;
  763. }
  764. if(wrote != len) {
  765. failf(data, "Failed writing RTP data");
  766. return CURLE_WRITE_ERROR;
  767. }
  768. return CURLE_OK;
  769. }
  770. CURLcode Curl_rtsp_parseheader(struct Curl_easy *data, char *header)
  771. {
  772. if(checkprefix("CSeq:", header)) {
  773. long CSeq = 0;
  774. char *endp;
  775. char *p = &header[5];
  776. while(ISBLANK(*p))
  777. p++;
  778. CSeq = strtol(p, &endp, 10);
  779. if(p != endp) {
  780. struct RTSP *rtsp = data->req.p.rtsp;
  781. rtsp->CSeq_recv = CSeq; /* mark the request */
  782. data->state.rtsp_CSeq_recv = CSeq; /* update the handle */
  783. }
  784. else {
  785. failf(data, "Unable to read the CSeq header: [%s]", header);
  786. return CURLE_RTSP_CSEQ_ERROR;
  787. }
  788. }
  789. else if(checkprefix("Session:", header)) {
  790. char *start;
  791. char *end;
  792. size_t idlen;
  793. /* Find the first non-space letter */
  794. start = header + 8;
  795. while(*start && ISBLANK(*start))
  796. start++;
  797. if(!*start) {
  798. failf(data, "Got a blank Session ID");
  799. return CURLE_RTSP_SESSION_ERROR;
  800. }
  801. /* Find the end of Session ID
  802. *
  803. * Allow any non whitespace content, up to the field separator or end of
  804. * line. RFC 2326 isn't 100% clear on the session ID and for example
  805. * gstreamer does url-encoded session ID's not covered by the standard.
  806. */
  807. end = start;
  808. while(*end && *end != ';' && !ISSPACE(*end))
  809. end++;
  810. idlen = end - start;
  811. if(data->set.str[STRING_RTSP_SESSION_ID]) {
  812. /* If the Session ID is set, then compare */
  813. if(strlen(data->set.str[STRING_RTSP_SESSION_ID]) != idlen ||
  814. strncmp(start, data->set.str[STRING_RTSP_SESSION_ID], idlen) != 0) {
  815. failf(data, "Got RTSP Session ID Line [%s], but wanted ID [%s]",
  816. start, data->set.str[STRING_RTSP_SESSION_ID]);
  817. return CURLE_RTSP_SESSION_ERROR;
  818. }
  819. }
  820. else {
  821. /* If the Session ID is not set, and we find it in a response, then set
  822. * it.
  823. */
  824. /* Copy the id substring into a new buffer */
  825. data->set.str[STRING_RTSP_SESSION_ID] = malloc(idlen + 1);
  826. if(!data->set.str[STRING_RTSP_SESSION_ID])
  827. return CURLE_OUT_OF_MEMORY;
  828. memcpy(data->set.str[STRING_RTSP_SESSION_ID], start, idlen);
  829. (data->set.str[STRING_RTSP_SESSION_ID])[idlen] = '\0';
  830. }
  831. }
  832. else if(checkprefix("Transport:", header)) {
  833. CURLcode result;
  834. result = rtsp_parse_transport(data, header + 10);
  835. if(result)
  836. return result;
  837. }
  838. return CURLE_OK;
  839. }
  840. static
  841. CURLcode rtsp_parse_transport(struct Curl_easy *data, char *transport)
  842. {
  843. /* If we receive multiple Transport response-headers, the linterleaved
  844. channels of each response header is recorded and used together for
  845. subsequent data validity checks.*/
  846. /* e.g.: ' RTP/AVP/TCP;unicast;interleaved=5-6' */
  847. char *start;
  848. char *end;
  849. start = transport;
  850. while(start && *start) {
  851. while(*start && ISBLANK(*start) )
  852. start++;
  853. end = strchr(start, ';');
  854. if(checkprefix("interleaved=", start)) {
  855. long chan1, chan2, chan;
  856. char *endp;
  857. char *p = start + 12;
  858. chan1 = strtol(p, &endp, 10);
  859. if(p != endp && chan1 >= 0 && chan1 <= 255) {
  860. unsigned char *rtp_channel_mask = data->state.rtp_channel_mask;
  861. chan2 = chan1;
  862. if(*endp == '-') {
  863. p = endp + 1;
  864. chan2 = strtol(p, &endp, 10);
  865. if(p == endp || chan2 < 0 || chan2 > 255) {
  866. infof(data, "Unable to read the interleaved parameter from "
  867. "Transport header: [%s]", transport);
  868. chan2 = chan1;
  869. }
  870. }
  871. for(chan = chan1; chan <= chan2; chan++) {
  872. long idx = chan / 8;
  873. long off = chan % 8;
  874. rtp_channel_mask[idx] |= (unsigned char)(1 << off);
  875. }
  876. }
  877. else {
  878. infof(data, "Unable to read the interleaved parameter from "
  879. "Transport header: [%s]", transport);
  880. }
  881. break;
  882. }
  883. /* skip to next parameter */
  884. start = (!end) ? end : (end + 1);
  885. }
  886. return CURLE_OK;
  887. }
  888. #endif /* CURL_DISABLE_RTSP or using Hyper */