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