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