rtsp.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2010, 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 http://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. * $Id$
  22. ***************************************************************************/
  23. #include "setup.h"
  24. #ifndef CURL_DISABLE_RTSP
  25. #include "urldata.h"
  26. #include <curl/curl.h>
  27. #include "transfer.h"
  28. #include "sendf.h"
  29. #include "easyif.h" /* for Curl_convert_... prototypes */
  30. #include "multiif.h"
  31. #include "http.h"
  32. #include "url.h"
  33. #include "progress.h"
  34. #include "rtsp.h"
  35. #include "rawstr.h"
  36. #include "curl_memory.h"
  37. #define _MPRINTF_REPLACE /* use our functions only */
  38. #include <curl/mprintf.h>
  39. /* The last #include file should be: */
  40. #include "memdebug.h"
  41. /*
  42. * TODO (general)
  43. * -incoming server requests
  44. * -server CSeq counter
  45. * -digest authentication
  46. * -connect thru proxy
  47. * -pipelining?
  48. */
  49. static int rtsp_getsock_do(struct connectdata *conn,
  50. curl_socket_t *socks,
  51. int numsocks);
  52. /* this returns the socket to wait for in the DO and DOING state for the multi
  53. interface and then we're always _sending_ a request and thus we wait for
  54. the single socket to become writable only */
  55. static int rtsp_getsock_do(struct connectdata *conn,
  56. curl_socket_t *socks,
  57. int numsocks)
  58. {
  59. /* write mode */
  60. (void)numsocks; /* unused, we trust it to be at least 1 */
  61. socks[0] = conn->sock[FIRSTSOCKET];
  62. return GETSOCK_WRITESOCK(0);
  63. }
  64. static
  65. CURLcode rtp_client_write(struct connectdata *conn, char *ptr, size_t len);
  66. /*
  67. * RTSP handler interface.
  68. */
  69. const struct Curl_handler Curl_handler_rtsp = {
  70. "RTSP", /* scheme */
  71. ZERO_NULL, /* setup_connection */
  72. Curl_rtsp, /* do_it */
  73. Curl_rtsp_done, /* done */
  74. ZERO_NULL, /* do_more */
  75. Curl_rtsp_connect, /* connect_it */
  76. ZERO_NULL, /* connecting */
  77. ZERO_NULL, /* doing */
  78. ZERO_NULL, /* proto_getsock */
  79. rtsp_getsock_do, /* doing_getsock */
  80. ZERO_NULL, /* perform_getsock */
  81. Curl_rtsp_disconnect, /* disconnect */
  82. PORT_RTSP, /* defport */
  83. PROT_RTSP, /* protocol */
  84. };
  85. CURLcode Curl_rtsp_connect(struct connectdata *conn, bool *done)
  86. {
  87. CURLcode httpStatus;
  88. struct SessionHandle *data = conn->data;
  89. httpStatus = Curl_http_connect(conn, done);
  90. /* Initialize the CSeq if not already done */
  91. if(data->state.rtsp_next_client_CSeq == 0)
  92. data->state.rtsp_next_client_CSeq = 1;
  93. if(data->state.rtsp_next_server_CSeq == 0)
  94. data->state.rtsp_next_server_CSeq = 1;
  95. conn->proto.rtspc.rtp_channel = -1;
  96. return httpStatus;
  97. }
  98. CURLcode Curl_rtsp_disconnect(struct connectdata *conn) {
  99. Curl_safefree(conn->proto.rtspc.rtp_buf);
  100. return CURLE_OK;
  101. }
  102. CURLcode Curl_rtsp_done(struct connectdata *conn,
  103. CURLcode status, bool premature)
  104. {
  105. CURLcode httpStatus;
  106. struct SessionHandle *data = conn->data;
  107. long CSeq_sent;
  108. long CSeq_recv;
  109. /* Bypass HTTP empty-reply checks on receive */
  110. if(data->set.rtspreq == RTSPREQ_RECEIVE)
  111. premature = TRUE;
  112. httpStatus = Curl_http_done(conn, status, premature);
  113. /* Check the sequence numbers */
  114. CSeq_sent = data->state.proto.rtsp->CSeq_sent;
  115. CSeq_recv = data->state.proto.rtsp->CSeq_recv;
  116. if((data->set.rtspreq != RTSPREQ_RECEIVE) && (CSeq_sent != CSeq_recv)) {
  117. failf(data, "The CSeq of this request %ld did not match the response %ld",
  118. CSeq_sent, CSeq_recv);
  119. return CURLE_RTSP_CSEQ_ERROR;
  120. }
  121. else if (data->set.rtspreq == RTSPREQ_RECEIVE &&
  122. (conn->proto.rtspc.rtp_channel == -1)) {
  123. infof(data, "Got an RTP Receive with a CSeq of %ld\n", CSeq_recv);
  124. /* TODO CPC: Server -> Client logic here */
  125. }
  126. return httpStatus;
  127. }
  128. CURLcode Curl_rtsp(struct connectdata *conn, bool *done)
  129. {
  130. struct SessionHandle *data = conn->data;
  131. CURLcode result=CURLE_OK;
  132. Curl_RtspReq rtspreq = data->set.rtspreq;
  133. struct RTSP *rtsp;
  134. struct HTTP *http;
  135. Curl_send_buffer *req_buffer;
  136. curl_off_t postsize = 0; /* for ANNOUNCE and SET_PARAMETER */
  137. curl_off_t putsize = 0; /* for ANNOUNCE and SET_PARAMETER */
  138. const char *p_request = NULL;
  139. const char *p_session_id = NULL;
  140. const char *p_accept = NULL;
  141. const char *p_accept_encoding = NULL;
  142. const char *p_range = NULL;
  143. const char *p_referrer = NULL;
  144. const char *p_stream_uri = NULL;
  145. const char *p_transport = NULL;
  146. const char *p_uagent = NULL;
  147. *done = TRUE;
  148. Curl_reset_reqproto(conn);
  149. if(!data->state.proto.rtsp) {
  150. /* Only allocate this struct if we don't already have it! */
  151. rtsp = calloc(sizeof(struct RTSP), 1);
  152. if(!rtsp)
  153. return CURLE_OUT_OF_MEMORY;
  154. data->state.proto.rtsp = rtsp;
  155. }
  156. else {
  157. rtsp = data->state.proto.rtsp;
  158. }
  159. http = &(rtsp->http_wrapper);
  160. /* Assert that no one has changed the RTSP struct in an evil way */
  161. DEBUGASSERT((void *)http == (void *)rtsp);
  162. rtsp->CSeq_sent = data->state.rtsp_next_client_CSeq;
  163. rtsp->CSeq_recv = 0;
  164. /* Setup the 'p_request' pointer to the proper p_request string
  165. * Since all RTSP requests are included here, there is no need to
  166. * support custom requests like HTTP.
  167. **/
  168. DEBUGASSERT((rtspreq > RTSPREQ_NONE && rtspreq < RTSPREQ_LAST));
  169. data->set.opt_no_body = TRUE; /* most requests don't contain a body */
  170. switch(rtspreq) {
  171. case RTSPREQ_NONE:
  172. failf(data, "Got invalid RTSP request: RTSPREQ_NONE");
  173. return CURLE_BAD_FUNCTION_ARGUMENT;
  174. case RTSPREQ_OPTIONS:
  175. p_request = "OPTIONS";
  176. break;
  177. case RTSPREQ_DESCRIBE:
  178. p_request = "DESCRIBE";
  179. data->set.opt_no_body = FALSE;
  180. break;
  181. case RTSPREQ_ANNOUNCE:
  182. p_request = "ANNOUNCE";
  183. break;
  184. case RTSPREQ_SETUP:
  185. p_request = "SETUP";
  186. break;
  187. case RTSPREQ_PLAY:
  188. p_request = "PLAY";
  189. break;
  190. case RTSPREQ_PAUSE:
  191. p_request = "PAUSE";
  192. break;
  193. case RTSPREQ_TEARDOWN:
  194. p_request = "TEARDOWN";
  195. break;
  196. case RTSPREQ_GET_PARAMETER:
  197. p_request = "GET_PARAMETER";
  198. data->set.opt_no_body = FALSE;
  199. break;
  200. case RTSPREQ_SET_PARAMETER:
  201. p_request = "SET_PARAMETER";
  202. break;
  203. case RTSPREQ_RECORD:
  204. p_request = "RECORD";
  205. break;
  206. case RTSPREQ_RECEIVE:
  207. p_request = "";
  208. /* Treat interleaved RTP as body*/
  209. data->set.opt_no_body = FALSE;
  210. break;
  211. case RTSPREQ_LAST:
  212. failf(data, "Got invalid RTSP request: RTSPREQ_LAST");
  213. return CURLE_BAD_FUNCTION_ARGUMENT;
  214. }
  215. if(rtspreq == RTSPREQ_RECEIVE) {
  216. result = Curl_setup_transfer(conn, FIRSTSOCKET, -1, TRUE,
  217. &http->readbytecount, -1, NULL);
  218. return result;
  219. }
  220. p_session_id = data->set.str[STRING_RTSP_SESSION_ID];
  221. if(!p_session_id &&
  222. (rtspreq & ~(RTSPREQ_OPTIONS | RTSPREQ_DESCRIBE | RTSPREQ_SETUP))) {
  223. failf(data, "Refusing to issue an RTSP request [%s] without a session ID.",
  224. p_request ? p_request : "");
  225. return CURLE_BAD_FUNCTION_ARGUMENT;
  226. }
  227. /* TODO: auth? */
  228. /* TODO: proxy? */
  229. /* Stream URI. Default to server '*' if not specified */
  230. if(data->set.str[STRING_RTSP_STREAM_URI]) {
  231. p_stream_uri = data->set.str[STRING_RTSP_STREAM_URI];
  232. }
  233. else {
  234. p_stream_uri = "*";
  235. }
  236. /* Transport Header for SETUP requests */
  237. p_transport = Curl_checkheaders(data, "Transport:");
  238. if(rtspreq == RTSPREQ_SETUP && !p_transport) {
  239. /* New Transport: setting? */
  240. if(data->set.str[STRING_RTSP_TRANSPORT]) {
  241. Curl_safefree(conn->allocptr.rtsp_transport);
  242. conn->allocptr.rtsp_transport =
  243. aprintf("Transport: %s\r\n",
  244. data->set.str[STRING_RTSP_TRANSPORT]);
  245. if(!conn->allocptr.rtsp_transport)
  246. return CURLE_OUT_OF_MEMORY;
  247. }
  248. else {
  249. failf(data,
  250. "Refusing to issue an RTSP SETUP without a Transport: header.");
  251. return CURLE_BAD_FUNCTION_ARGUMENT;
  252. }
  253. p_transport = conn->allocptr.rtsp_transport;
  254. }
  255. /* Accept Headers for DESCRIBE requests */
  256. if(rtspreq == RTSPREQ_DESCRIBE) {
  257. /* Accept Header */
  258. p_accept = Curl_checkheaders(data, "Accept:")?
  259. NULL:"Accept: application/sdp\r\n";
  260. /* Accept-Encoding header */
  261. if(!Curl_checkheaders(data, "Accept-Encoding:") &&
  262. data->set.str[STRING_ENCODING]) {
  263. Curl_safefree(conn->allocptr.accept_encoding);
  264. conn->allocptr.accept_encoding =
  265. aprintf("Accept-Encoding: %s\r\n", data->set.str[STRING_ENCODING]);
  266. if(!conn->allocptr.accept_encoding)
  267. return CURLE_OUT_OF_MEMORY;
  268. p_accept_encoding = conn->allocptr.accept_encoding;
  269. }
  270. }
  271. /* Default to text/parameters for GET_PARAMETER */
  272. if(rtspreq == RTSPREQ_GET_PARAMETER) {
  273. p_accept = Curl_checkheaders(data, "Accept:")?
  274. NULL:"Accept: text/parameters\r\n";
  275. }
  276. /* The User-Agent string might have been allocated in url.c already, because
  277. it might have been used in the proxy connect, but if we have got a header
  278. with the user-agent string specified, we erase the previously made string
  279. here. */
  280. if(Curl_checkheaders(data, "User-Agent:") && conn->allocptr.uagent) {
  281. Curl_safefree(conn->allocptr.uagent);
  282. conn->allocptr.uagent = NULL;
  283. }
  284. else if(!Curl_checkheaders(data, "User-Agent:") &&
  285. data->set.str[STRING_USERAGENT]) {
  286. p_uagent = conn->allocptr.uagent;
  287. }
  288. /* Referrer */
  289. Curl_safefree(conn->allocptr.ref);
  290. if(data->change.referer && !Curl_checkheaders(data, "Referer:"))
  291. conn->allocptr.ref = aprintf("Referer: %s\r\n", data->change.referer);
  292. else
  293. conn->allocptr.ref = NULL;
  294. p_referrer = conn->allocptr.ref;
  295. /*
  296. * Range Header
  297. * Only applies to PLAY, PAUSE, RECORD
  298. *
  299. * Go ahead and use the Range stuff supplied for HTTP
  300. */
  301. if(data->state.use_range &&
  302. (rtspreq & (RTSPREQ_PLAY | RTSPREQ_PAUSE | RTSPREQ_RECORD))) {
  303. /* Check to see if there is a range set in the custom headers */
  304. if(!Curl_checkheaders(data, "Range:") && data->state.range) {
  305. Curl_safefree(conn->allocptr.rangeline);
  306. conn->allocptr.rangeline = aprintf("Range: %s\r\n", data->state.range);
  307. p_range = conn->allocptr.rangeline;
  308. }
  309. }
  310. /*
  311. * Sanity check the custom headers
  312. */
  313. if(Curl_checkheaders(data, "CSeq:")) {
  314. failf(data, "CSeq cannot be set as a custom header.");
  315. return CURLE_RTSP_CSEQ_ERROR;
  316. }
  317. if(Curl_checkheaders(data, "Session:")) {
  318. failf(data, "Session ID cannot be set as a custom header.");
  319. return CURLE_BAD_FUNCTION_ARGUMENT;
  320. }
  321. /* Initialize a dynamic send buffer */
  322. req_buffer = Curl_add_buffer_init();
  323. if(!req_buffer)
  324. return CURLE_OUT_OF_MEMORY;
  325. result =
  326. Curl_add_bufferf(req_buffer,
  327. "%s %s RTSP/1.0\r\n" /* Request Stream-URI RTSP/1.0 */
  328. "CSeq: %ld\r\n", /* CSeq */
  329. (p_request ? p_request : ""), p_stream_uri,
  330. rtsp->CSeq_sent);
  331. if(result)
  332. return result;
  333. /*
  334. * Rather than do a normal alloc line, keep the session_id unformatted
  335. * to make comparison easier
  336. */
  337. if(p_session_id) {
  338. result = Curl_add_bufferf(req_buffer, "Session: %s\r\n", p_session_id);
  339. if(result)
  340. return result;
  341. }
  342. /*
  343. * Shared HTTP-like options
  344. */
  345. result = Curl_add_bufferf(req_buffer,
  346. "%s" /* transport */
  347. "%s" /* accept */
  348. "%s" /* accept-encoding */
  349. "%s" /* range */
  350. "%s" /* referrer */
  351. "%s" /* user-agent */
  352. ,
  353. p_transport ? p_transport : "",
  354. p_accept ? p_accept : "",
  355. p_accept_encoding ? p_accept_encoding : "",
  356. p_range ? p_range : "",
  357. p_referrer ? p_referrer : "",
  358. p_uagent ? p_uagent : "");
  359. if((rtspreq == RTSPREQ_SETUP) || (rtspreq == RTSPREQ_DESCRIBE)) {
  360. result = Curl_add_timecondition(data, req_buffer);
  361. if(result)
  362. return result;
  363. }
  364. result = Curl_add_custom_headers(conn, req_buffer);
  365. if(result)
  366. return result;
  367. if(rtspreq == RTSPREQ_ANNOUNCE || rtspreq == RTSPREQ_SET_PARAMETER) {
  368. if(data->set.upload) {
  369. putsize = data->set.infilesize;
  370. data->set.httpreq = HTTPREQ_PUT;
  371. }
  372. else {
  373. postsize = (data->set.postfieldsize != -1)?
  374. data->set.postfieldsize:
  375. (data->set.postfields? (curl_off_t)strlen(data->set.postfields):0);
  376. data->set.httpreq = HTTPREQ_POST;
  377. }
  378. /* As stated in the http comments, it is probably not wise to
  379. * actually set a custom Content-Length in the headers */
  380. if(!Curl_checkheaders(data, "Content-Length:")) {
  381. result = Curl_add_bufferf(req_buffer,
  382. "Content-Length: %" FORMAT_OFF_T"\r\n",
  383. (data->set.upload ? putsize : postsize));
  384. if(result)
  385. return result;
  386. }
  387. if(rtspreq == RTSPREQ_SET_PARAMETER) {
  388. if(!Curl_checkheaders(data, "Content-Type:")) {
  389. result = Curl_add_bufferf(req_buffer,
  390. "Content-Type: text/parameters\r\n");
  391. }
  392. if(result)
  393. return result;
  394. }
  395. if(rtspreq == RTSPREQ_ANNOUNCE) {
  396. if(!Curl_checkheaders(data, "Content-Type:")) {
  397. result = Curl_add_bufferf(req_buffer,
  398. "Content-Type: application/sdp\r\n");
  399. }
  400. }
  401. data->state.expect100header = FALSE; /* RTSP posts are simple/small */
  402. }
  403. /* RTSP never allows chunked transfer */
  404. data->req.forbidchunk = TRUE;
  405. /* Finish the request buffer */
  406. result = Curl_add_buffer(req_buffer, "\r\n", 2);
  407. if(result)
  408. return result;
  409. if(postsize > 0) {
  410. result = Curl_add_buffer(req_buffer, data->set.postfields,
  411. (size_t)postsize);
  412. if(result)
  413. return result;
  414. }
  415. /* issue the request */
  416. result = Curl_add_buffer_send(req_buffer, conn,
  417. &data->info.request_size, 0, FIRSTSOCKET);
  418. if(result) {
  419. failf(data, "Failed sending RTSP request");
  420. return result;
  421. }
  422. result = Curl_setup_transfer(conn, FIRSTSOCKET, -1, TRUE,
  423. &http->readbytecount,
  424. putsize?FIRSTSOCKET:-1,
  425. putsize?&http->writebytecount:NULL);
  426. if(result) {
  427. failf(data, "Failed RTSP transfer");
  428. return result;
  429. }
  430. /* Increment the CSeq on success */
  431. data->state.rtsp_next_client_CSeq++;
  432. if(http->writebytecount) {
  433. /* if a request-body has been sent off, we make sure this progress is
  434. noted properly */
  435. Curl_pgrsSetUploadCounter(data, http->writebytecount);
  436. if(Curl_pgrsUpdate(conn))
  437. result = CURLE_ABORTED_BY_CALLBACK;
  438. }
  439. return result;
  440. }
  441. CURLcode Curl_rtsp_rtp_readwrite(struct SessionHandle *data,
  442. struct connectdata *conn,
  443. ssize_t *nread,
  444. bool *readmore) {
  445. struct SingleRequest *k = &data->req;
  446. struct rtsp_conn *rtspc = &(conn->proto.rtspc);
  447. char *rtp; /* moving pointer to rtp data */
  448. ssize_t rtp_dataleft; /* how much data left to parse in this round */
  449. char *scratch;
  450. CURLcode result;
  451. if(rtspc->rtp_buf) {
  452. /* There was some leftover data the last time. Merge buffers */
  453. rtspc->rtp_buf = realloc(rtspc->rtp_buf, rtspc->rtp_bufsize + *nread);
  454. if(!rtspc->rtp_buf)
  455. return CURLE_OUT_OF_MEMORY;
  456. memcpy(rtspc->rtp_buf + rtspc->rtp_bufsize, k->str, *nread);
  457. rtspc->rtp_bufsize += *nread;
  458. rtp = rtspc->rtp_buf;
  459. rtp_dataleft = rtspc->rtp_bufsize;
  460. }
  461. else {
  462. /* Just parse the request buffer directly */
  463. rtp = k->str;
  464. rtp_dataleft = *nread;
  465. }
  466. while((rtp_dataleft > 0) &&
  467. (rtp[0] == '$')) {
  468. if(rtp_dataleft > 4) {
  469. char channel;
  470. int rtp_length;
  471. /* Parse the header */
  472. /* The channel identifier immediately follows and is 1 byte */
  473. channel = rtp[1];
  474. rtspc->rtp_channel = (int) channel;
  475. /* The length is two bytes */
  476. rtp_length = ntohs( *((unsigned short *)(&rtp[2])) );
  477. if(rtp_dataleft < rtp_length + 4) {
  478. /* Need more - incomplete payload*/
  479. *readmore = TRUE;
  480. break;
  481. }
  482. else {
  483. /* We have the full RTP interleaved packet
  484. * Write out the header including the leading '$' */
  485. DEBUGF(infof(data, "RTP write channel %d rtp_length %d\n",
  486. rtspc->rtp_channel, rtp_length));
  487. result = rtp_client_write(conn, &rtp[0], rtp_length + 4);
  488. if(result) {
  489. failf(data, "Got an error writing an RTP packet");
  490. *readmore = FALSE;
  491. Curl_safefree(rtspc->rtp_buf);
  492. rtspc->rtp_buf = NULL;
  493. rtspc->rtp_bufsize = 0;
  494. return result;
  495. }
  496. /* Move forward in the buffer */
  497. rtp_dataleft -= rtp_length + 4;
  498. rtp += rtp_length + 4;
  499. if(data->set.rtspreq == RTSPREQ_RECEIVE) {
  500. /* If we are in a passive receive, give control back
  501. * to the app as often as we can.
  502. */
  503. k->keepon &= ~KEEP_RECV;
  504. }
  505. }
  506. }
  507. else {
  508. /* Need more - incomplete header */
  509. *readmore = TRUE;
  510. break;
  511. }
  512. }
  513. if(rtp_dataleft != 0 && rtp[0] == '$') {
  514. DEBUGF(infof(data, "RTP Rewinding %zu %s\n", rtp_dataleft,
  515. *readmore ? "(READMORE)" : ""));
  516. /* Store the incomplete RTP packet for a "rewind" */
  517. scratch = malloc(rtp_dataleft);
  518. if(!scratch)
  519. return CURLE_OUT_OF_MEMORY;
  520. memcpy(scratch, rtp, rtp_dataleft);
  521. Curl_safefree(rtspc->rtp_buf);
  522. rtspc->rtp_buf = scratch;
  523. rtspc->rtp_bufsize = rtp_dataleft;
  524. /* As far as the transfer is concerned, this data is consumed */
  525. *nread = 0;
  526. return CURLE_OK;
  527. }
  528. else {
  529. /* Fix up k->str to point just after the last RTP packet */
  530. k->str += *nread - rtp_dataleft;
  531. /* either all of the data has been read or...
  532. * rtp now points at the next byte to parse
  533. */
  534. if(rtp_dataleft > 0)
  535. DEBUGASSERT(k->str[0] == rtp[0]);
  536. DEBUGASSERT(rtp_dataleft <= *nread); /* sanity check */
  537. *nread = rtp_dataleft;
  538. }
  539. /* If we get here, we have finished with the leftover/merge buffer */
  540. Curl_safefree(rtspc->rtp_buf);
  541. rtspc->rtp_buf = NULL;
  542. rtspc->rtp_bufsize = 0;
  543. return CURLE_OK;
  544. }
  545. static
  546. CURLcode rtp_client_write(struct connectdata *conn, char *ptr, size_t len)
  547. {
  548. struct SessionHandle *data = conn->data;
  549. size_t wrote;
  550. curl_write_callback writeit;
  551. if(len == 0) {
  552. failf (data, "Cannot write a 0 size RTP packet.");
  553. return CURLE_WRITE_ERROR;
  554. }
  555. writeit = data->set.fwrite_rtp?data->set.fwrite_rtp:data->set.fwrite_func;
  556. wrote = writeit(ptr, 1, len, data->set.rtp_out);
  557. if(CURL_WRITEFUNC_PAUSE == wrote) {
  558. failf (data, "Cannot pause RTP");
  559. return CURLE_WRITE_ERROR;
  560. }
  561. if(wrote != len) {
  562. failf (data, "Failed writing RTP data");
  563. return CURLE_WRITE_ERROR;
  564. }
  565. return CURLE_OK;
  566. }
  567. CURLcode Curl_rtsp_parseheader(struct connectdata *conn,
  568. char *header)
  569. {
  570. struct SessionHandle *data = conn->data;
  571. long CSeq = 0;
  572. if(checkprefix("CSeq:", header)) {
  573. /* Store the received CSeq. Match is verified in rtsp_done */
  574. int nc;
  575. char *temp = strdup(header);
  576. if(!temp)
  577. return CURLE_OUT_OF_MEMORY;
  578. Curl_strntoupper(temp, temp, sizeof(temp));
  579. nc = sscanf(temp, "CSEQ: %ld", &CSeq);
  580. free(temp);
  581. if(nc == 1) {
  582. data->state.proto.rtsp->CSeq_recv = CSeq; /* mark the request */
  583. data->state.rtsp_CSeq_recv = CSeq; /* update the handle */
  584. }
  585. else {
  586. failf(data, "Unable to read the CSeq header: [%s]", header);
  587. return CURLE_RTSP_CSEQ_ERROR;
  588. }
  589. }
  590. else if(checkprefix("Session:", header)) {
  591. char *start;
  592. /* Find the first non-space letter */
  593. start = header + 9;
  594. while(*start && ISSPACE(*start))
  595. start++;
  596. if(!start) {
  597. failf(data, "Got a blank Session ID");
  598. }
  599. else if(data->set.str[STRING_RTSP_SESSION_ID]) {
  600. /* If the Session ID is set, then compare */
  601. if(strncmp(start, data->set.str[STRING_RTSP_SESSION_ID],
  602. strlen(data->set.str[STRING_RTSP_SESSION_ID])) != 0) {
  603. failf(data, "Got RTSP Session ID Line [%s], but wanted ID [%s]",
  604. start, data->set.str[STRING_RTSP_SESSION_ID]);
  605. return CURLE_RTSP_SESSION_ERROR;
  606. }
  607. }
  608. else {
  609. /* If the Session ID is not set, and we find it in a response, then
  610. set it */
  611. /* The session ID can be an alphanumeric or a 'safe' character
  612. *
  613. * RFC 2326 15.1 Base Syntax:
  614. * safe = "\$" | "-" | "_" | "." | "+"
  615. * */
  616. char *end = start;
  617. while(*end &&
  618. (ISALNUM(*end) || *end == '-' || *end == '_' || *end == '.' ||
  619. *end == '+' ||
  620. (*end == '\\' && *(end + 1) && *(end + 1) == '$' && (++end, 1))))
  621. end++;
  622. /* Copy the id substring into a new buffer */
  623. data->set.str[STRING_RTSP_SESSION_ID] = malloc(end - start + 1);
  624. memcpy(data->set.str[STRING_RTSP_SESSION_ID], start, end - start);
  625. (data->set.str[STRING_RTSP_SESSION_ID])[end - start] = '\0';
  626. }
  627. }
  628. return CURLE_OK;
  629. }
  630. #endif /* CURL_DISABLE_RTSP */