Coverage Report

Created: 2024-05-04 12:45

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