Mercurial > mplayer.hg
annotate libmpdemux/realrtsp/rtsp.c @ 15172:56541efe420b
Fix potential buffer overflow if server answers with too many lines
author | rtognimp |
---|---|
date | Fri, 15 Apr 2005 23:30:44 +0000 |
parents | 2e8b1e13b8c3 |
children | 281d155fb37f |
rev | line source |
---|---|
9922 | 1 /* |
2 * This file was ported to MPlayer from xine CVS rtsp.c,v 1.9 2003/04/10 02:30:48 | |
3 */ | |
4 | |
5 /* | |
6 * Copyright (C) 2000-2002 the xine project | |
7 * | |
8 * This file is part of xine, a free video player. | |
9 * | |
10 * xine is free software; you can redistribute it and/or modify | |
11 * it under the terms of the GNU General Public License as published by | |
12 * the Free Software Foundation; either version 2 of the License, or | |
13 * (at your option) any later version. | |
14 * | |
15 * xine is distributed in the hope that it will be useful, | |
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 * GNU General Public License for more details. | |
19 * | |
20 * You should have received a copy of the GNU General Public License | |
21 * along with this program; if not, write to the Free Software | |
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA | |
23 * | |
24 * | |
25 * a minimalistic implementation of rtsp protocol, | |
26 * *not* RFC 2326 compilant yet. | |
27 */ | |
28 | |
29 #include <unistd.h> | |
30 #include <stdio.h> | |
31 #include <assert.h> | |
10281 | 32 #include "config.h" |
33 #ifndef HAVE_WINSOCK2 | |
34 #define closesocket close | |
9922 | 35 #include <sys/socket.h> |
36 #include <netinet/in.h> | |
37 #include <netdb.h> | |
10281 | 38 #else |
39 #include <winsock2.h> | |
40 #endif | |
9922 | 41 #include <string.h> |
42 #include <sys/stat.h> | |
43 #include <fcntl.h> | |
44 #include <errno.h> | |
45 #include <stdlib.h> | |
46 #include <time.h> | |
47 #include <sys/time.h> | |
48 #include <sys/types.h> | |
9939
6b88f67564ce
Fix compilation for *BSD, Mac OS X and maybe others (info by Steven M. Schultz and Dan Christiansen)
rtognimp
parents:
9922
diff
changeset
|
49 #include <inttypes.h> |
9922 | 50 |
51 #include "rtsp.h" | |
52 | |
53 /* | |
54 #define LOG | |
55 */ | |
56 | |
57 #define BUF_SIZE 4096 | |
58 #define HEADER_SIZE 1024 | |
59 #define MAX_FIELDS 256 | |
60 | |
61 struct rtsp_s { | |
62 | |
63 int s; | |
64 | |
65 char *host; | |
66 int port; | |
67 char *path; | |
11506
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
68 char *param; |
9922 | 69 char *mrl; |
70 char *user_agent; | |
71 | |
72 char *server; | |
73 unsigned int server_state; | |
74 uint32_t server_caps; | |
75 | |
76 unsigned int cseq; | |
77 char *session; | |
78 | |
79 char *answers[MAX_FIELDS]; /* data of last message */ | |
80 char *scheduled[MAX_FIELDS]; /* will be sent with next message */ | |
81 }; | |
82 | |
83 /* | |
84 * constants | |
85 */ | |
86 | |
87 const char rtsp_protocol_version[]="RTSP/1.0"; | |
88 | |
89 /* server states */ | |
90 #define RTSP_CONNECTED 1 | |
91 #define RTSP_INIT 2 | |
92 #define RTSP_READY 4 | |
93 #define RTSP_PLAYING 8 | |
94 #define RTSP_RECORDING 16 | |
95 | |
96 /* server capabilities */ | |
97 #define RTSP_OPTIONS 0x001 | |
98 #define RTSP_DESCRIBE 0x002 | |
99 #define RTSP_ANNOUNCE 0x004 | |
100 #define RTSP_SETUP 0x008 | |
101 #define RTSP_GET_PARAMETER 0x010 | |
102 #define RTSP_SET_PARAMETER 0x020 | |
103 #define RTSP_TEARDOWN 0x040 | |
104 #define RTSP_PLAY 0x080 | |
105 #define RTSP_RECORD 0x100 | |
106 | |
107 /* | |
108 * network utilities | |
109 */ | |
110 | |
111 static int host_connect_attempt(struct in_addr ia, int port) { | |
112 | |
113 int s; | |
114 struct sockaddr_in sin; | |
115 | |
116 s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); | |
117 if (s == -1) { | |
118 printf ("rtsp: socket(): %s\n", strerror(errno)); | |
119 return -1; | |
120 } | |
121 | |
122 sin.sin_family = AF_INET; | |
123 sin.sin_addr = ia; | |
124 sin.sin_port = htons(port); | |
125 | |
126 if (connect(s, (struct sockaddr *)&sin, sizeof(sin))==-1 | |
10281 | 127 #ifndef HAVE_WINSOCK2 |
9922 | 128 && errno != EINPROGRESS) { |
10281 | 129 #else |
130 && WSAGetLastError() == WSAEINPROGRESS) { | |
131 #endif | |
9922 | 132 printf ("rtsp: connect(): %s\n", strerror(errno)); |
10281 | 133 closesocket(s); |
9922 | 134 return -1; |
135 } | |
136 | |
137 return s; | |
138 } | |
139 | |
140 static int host_connect(const char *host, int port) { | |
141 | |
142 struct hostent *h; | |
143 int i, s; | |
144 | |
145 h = gethostbyname(host); | |
146 if (h == NULL) { | |
147 printf ("rtsp: unable to resolve '%s'.\n", host); | |
148 return -1; | |
149 } | |
150 | |
151 for (i = 0; h->h_addr_list[i]; i++) { | |
152 struct in_addr ia; | |
153 | |
154 memcpy (&ia, h->h_addr_list[i], 4); | |
155 s = host_connect_attempt(ia, port); | |
156 if(s != -1) | |
157 return s; | |
158 } | |
159 printf ("rtsp: unable to connect to '%s'.\n", host); | |
160 return -1; | |
161 } | |
162 | |
163 static int write_stream(int s, const char *buf, int len) { | |
164 int total, timeout; | |
165 | |
166 total = 0; timeout = 30; | |
167 while (total < len){ | |
168 int n; | |
169 | |
10206
35e306346e59
Using recv/send instead read/write for proper MinGW support (it's a 4.2BSD standard). Patch by FloDt <flodt8@yahoo.de>
alex
parents:
9939
diff
changeset
|
170 n = send (s, &buf[total], len - total, 0); |
9922 | 171 |
172 if (n > 0) | |
173 total += n; | |
174 else if (n < 0) { | |
10281 | 175 #ifndef HAVE_WINSOCK2 |
9922 | 176 if ((timeout>0) && ((errno == EAGAIN) || (errno == EINPROGRESS))) { |
10281 | 177 #else |
178 if ((timeout>0) && ((errno == EAGAIN) || (WSAGetLastError() == WSAEINPROGRESS))) { | |
179 #endif | |
9922 | 180 sleep (1); timeout--; |
181 } else | |
182 return -1; | |
183 } | |
184 } | |
185 | |
186 return total; | |
187 } | |
188 | |
189 static ssize_t read_stream(int fd, void *buf, size_t count) { | |
190 | |
191 ssize_t ret, total; | |
192 | |
193 total = 0; | |
194 | |
195 while (total < count) { | |
196 | |
10206
35e306346e59
Using recv/send instead read/write for proper MinGW support (it's a 4.2BSD standard). Patch by FloDt <flodt8@yahoo.de>
alex
parents:
9939
diff
changeset
|
197 ret=recv (fd, ((uint8_t*)buf)+total, count-total, 0); |
9922 | 198 |
199 if (ret<0) { | |
200 if(errno == EAGAIN) { | |
201 fd_set rset; | |
202 struct timeval timeout; | |
203 | |
204 FD_ZERO (&rset); | |
205 FD_SET (fd, &rset); | |
206 | |
207 timeout.tv_sec = 30; | |
208 timeout.tv_usec = 0; | |
209 | |
210 if (select (fd+1, &rset, NULL, NULL, &timeout) <= 0) { | |
211 return -1; | |
212 } | |
213 continue; | |
214 } | |
215 | |
216 printf ("rtsp: read error.\n"); | |
217 return ret; | |
218 } else | |
219 total += ret; | |
220 | |
221 /* end of stream */ | |
222 if (!ret) break; | |
223 } | |
224 | |
225 return total; | |
226 } | |
227 | |
228 /* | |
229 * debugging utilities | |
230 */ | |
231 #if 0 | |
232 static void hexdump (char *buf, int length) { | |
233 | |
234 int i; | |
235 | |
236 printf ("rtsp: ascii>"); | |
237 for (i = 0; i < length; i++) { | |
238 unsigned char c = buf[i]; | |
239 | |
240 if ((c >= 32) && (c <= 128)) | |
241 printf ("%c", c); | |
242 else | |
243 printf ("."); | |
244 } | |
245 printf ("\n"); | |
246 | |
247 printf ("rtsp: hexdump> "); | |
248 for (i = 0; i < length; i++) { | |
249 unsigned char c = buf[i]; | |
250 | |
251 printf ("%02x", c); | |
252 | |
253 if ((i % 16) == 15) | |
254 printf ("\nrtsp: "); | |
255 | |
256 if ((i % 2) == 1) | |
257 printf (" "); | |
258 | |
259 } | |
260 printf ("\n"); | |
261 } | |
262 #endif | |
263 | |
264 /* | |
265 * rtsp_get gets a line from stream | |
266 * and returns a null terminated string. | |
267 */ | |
268 | |
269 static char *rtsp_get(rtsp_t *s) { | |
270 | |
271 int n=0; | |
12271
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
272 char *buffer = malloc(BUF_SIZE); |
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
273 char *string = NULL; |
9922 | 274 |
275 while (n<BUF_SIZE) { | |
12271
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
276 read_stream(s->s, &(buffer[n]), 1); |
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
277 if ((buffer[n-1]==0x0d)&&(buffer[n]==0x0a)) break; |
9922 | 278 n++; |
279 } | |
280 | |
281 if (n>=BUF_SIZE) { | |
282 printf("librtsp: buffer overflow in rtsp_get\n"); | |
283 exit(1); | |
284 } | |
285 string=malloc(sizeof(char)*n); | |
12271
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
286 memcpy(string,buffer,n-1); |
9922 | 287 string[n-1]=0; |
288 | |
289 #ifdef LOG | |
290 printf("librtsp: << '%s'\n", string); | |
291 #endif | |
292 | |
293 | |
12271
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
294 free(buffer); |
9922 | 295 return string; |
296 } | |
297 | |
298 /* | |
299 * rtsp_put puts a line on stream | |
300 */ | |
301 | |
302 static void rtsp_put(rtsp_t *s, const char *string) { | |
303 | |
304 int len=strlen(string); | |
305 char *buf=malloc(sizeof(char)*len+2); | |
306 | |
307 #ifdef LOG | |
308 printf("librtsp: >> '%s'", string); | |
309 #endif | |
310 | |
311 memcpy(buf,string,len); | |
312 buf[len]=0x0d; | |
313 buf[len+1]=0x0a; | |
314 | |
315 write_stream(s->s, buf, len+2); | |
316 | |
317 #ifdef LOG | |
318 printf(" done.\n"); | |
319 #endif | |
320 | |
321 free(buf); | |
322 } | |
323 | |
324 /* | |
325 * extract server status code | |
326 */ | |
327 | |
328 static int rtsp_get_code(const char *string) { | |
329 | |
330 char buf[4]; | |
331 int code=0; | |
332 | |
333 if (!strncmp(string, rtsp_protocol_version, strlen(rtsp_protocol_version))) | |
334 { | |
335 memcpy(buf, string+strlen(rtsp_protocol_version)+1, 3); | |
336 buf[3]=0; | |
337 code=atoi(buf); | |
338 } else if (!strncmp(string, "SET_PARAMETER",8)) | |
339 { | |
340 return RTSP_STATUS_SET_PARAMETER; | |
341 } | |
342 | |
343 if(code != 200) printf("librtsp: server responds: '%s'\n",string); | |
344 | |
345 return code; | |
346 } | |
347 | |
348 /* | |
349 * send a request | |
350 */ | |
351 | |
352 static void rtsp_send_request(rtsp_t *s, const char *type, const char *what) { | |
353 | |
354 char **payload=s->scheduled; | |
12271
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
355 char *buf; |
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
356 |
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
357 buf = malloc(strlen(type)+strlen(what)+strlen(rtsp_protocol_version)+3); |
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
358 |
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
359 sprintf(buf,"%s %s %s",type, what, rtsp_protocol_version); |
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
360 rtsp_put(s,buf); |
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
361 free(buf); |
9922 | 362 if (payload) |
363 while (*payload) { | |
364 rtsp_put(s,*payload); | |
365 payload++; | |
366 } | |
367 rtsp_put(s,""); | |
368 rtsp_unschedule_all(s); | |
369 } | |
370 | |
371 /* | |
372 * schedule standard fields | |
373 */ | |
374 | |
375 static void rtsp_schedule_standard(rtsp_t *s) { | |
376 | |
12271
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
377 char tmp[16]; |
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
378 |
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
379 snprintf(tmp, 16, "Cseq: %u", s->cseq); |
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
380 rtsp_schedule_field(s, tmp); |
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
381 |
9922 | 382 if (s->session) { |
12271
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
383 char *buf; |
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
384 buf = malloc(strlen(s->session)+15); |
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
385 sprintf(buf, "Session: %s", s->session); |
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
386 rtsp_schedule_field(s, buf); |
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
387 free(buf); |
9922 | 388 } |
389 } | |
390 /* | |
391 * get the answers, if server responses with something != 200, return NULL | |
392 */ | |
393 | |
394 static int rtsp_get_answers(rtsp_t *s) { | |
395 | |
396 char *answer=NULL; | |
397 unsigned int answer_seq; | |
398 char **answer_ptr=s->answers; | |
399 int code; | |
15172
56541efe420b
Fix potential buffer overflow if server answers with too many lines
rtognimp
parents:
13677
diff
changeset
|
400 int ans_count = 0; |
9922 | 401 |
402 answer=rtsp_get(s); | |
12271
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
403 if (!answer) |
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
404 return 0; |
9922 | 405 code=rtsp_get_code(answer); |
406 free(answer); | |
407 | |
408 rtsp_free_answers(s); | |
409 | |
410 do { /* while we get answer lines */ | |
411 | |
412 answer=rtsp_get(s); | |
12271
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
413 if (!answer) |
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
414 return 0; |
9922 | 415 |
416 if (!strncmp(answer,"Cseq:",5)) { | |
417 sscanf(answer,"Cseq: %u",&answer_seq); | |
418 if (s->cseq != answer_seq) { | |
419 #ifdef LOG | |
420 printf("librtsp: warning: Cseq mismatch. got %u, assumed %u", answer_seq, s->cseq); | |
421 #endif | |
422 s->cseq=answer_seq; | |
423 } | |
424 } | |
425 if (!strncmp(answer,"Server:",7)) { | |
12271
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
426 char *buf = malloc(strlen(answer)); |
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
427 sscanf(answer,"Server: %s",buf); |
9922 | 428 if (s->server) free(s->server); |
12271
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
429 s->server=strdup(buf); |
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
430 free(buf); |
9922 | 431 } |
432 if (!strncmp(answer,"Session:",8)) { | |
12271
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
433 char *buf = calloc(1, strlen(answer)); |
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
434 sscanf(answer,"Session: %s",buf); |
9922 | 435 if (s->session) { |
12271
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
436 if (strcmp(buf, s->session)) { |
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
437 printf("rtsp: warning: setting NEW session: %s\n", buf); |
9922 | 438 free(s->session); |
12271
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
439 s->session=strdup(buf); |
9922 | 440 } |
441 } else | |
442 { | |
443 #ifdef LOG | |
13677 | 444 printf("rtsp: setting session id to: %s\n", buf); |
9922 | 445 #endif |
12271
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
446 s->session=strdup(buf); |
9922 | 447 } |
12271
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
448 free(buf); |
9922 | 449 } |
450 *answer_ptr=answer; | |
451 answer_ptr++; | |
15172
56541efe420b
Fix potential buffer overflow if server answers with too many lines
rtognimp
parents:
13677
diff
changeset
|
452 } while ((strlen(answer)!=0) && (++ans_count < MAX_FIELDS)); |
9922 | 453 |
454 s->cseq++; | |
455 | |
456 *answer_ptr=NULL; | |
457 rtsp_schedule_standard(s); | |
458 | |
459 return code; | |
460 } | |
461 | |
462 /* | |
463 * send an ok message | |
464 */ | |
465 | |
466 int rtsp_send_ok(rtsp_t *s) { | |
467 char cseq[16]; | |
468 | |
469 rtsp_put(s, "RTSP/1.0 200 OK"); | |
470 sprintf(cseq,"CSeq: %u", s->cseq); | |
471 rtsp_put(s, cseq); | |
472 rtsp_put(s, ""); | |
473 return 0; | |
474 } | |
475 | |
476 /* | |
477 * implementation of must-have rtsp requests; functions return | |
478 * server status code. | |
479 */ | |
480 | |
481 int rtsp_request_options(rtsp_t *s, const char *what) { | |
482 | |
483 char *buf; | |
484 | |
485 if (what) { | |
486 buf=strdup(what); | |
487 } else | |
488 { | |
489 buf=malloc(sizeof(char)*(strlen(s->host)+16)); | |
490 sprintf(buf,"rtsp://%s:%i", s->host, s->port); | |
491 } | |
492 rtsp_send_request(s,"OPTIONS",buf); | |
493 free(buf); | |
494 | |
495 return rtsp_get_answers(s); | |
496 } | |
497 | |
498 int rtsp_request_describe(rtsp_t *s, const char *what) { | |
499 | |
500 char *buf; | |
501 | |
502 if (what) { | |
503 buf=strdup(what); | |
504 } else | |
505 { | |
506 buf=malloc(sizeof(char)*(strlen(s->host)+strlen(s->path)+16)); | |
507 sprintf(buf,"rtsp://%s:%i/%s", s->host, s->port, s->path); | |
508 } | |
509 rtsp_send_request(s,"DESCRIBE",buf); | |
510 free(buf); | |
511 | |
512 return rtsp_get_answers(s); | |
513 } | |
514 | |
515 int rtsp_request_setup(rtsp_t *s, const char *what) { | |
516 | |
517 rtsp_send_request(s,"SETUP",what); | |
518 | |
519 return rtsp_get_answers(s); | |
520 } | |
521 | |
522 int rtsp_request_setparameter(rtsp_t *s, const char *what) { | |
523 | |
524 char *buf; | |
525 | |
526 if (what) { | |
527 buf=strdup(what); | |
528 } else | |
529 { | |
530 buf=malloc(sizeof(char)*(strlen(s->host)+strlen(s->path)+16)); | |
531 sprintf(buf,"rtsp://%s:%i/%s", s->host, s->port, s->path); | |
532 } | |
533 rtsp_send_request(s,"SET_PARAMETER",buf); | |
534 free(buf); | |
535 | |
536 return rtsp_get_answers(s); | |
537 } | |
538 | |
539 int rtsp_request_play(rtsp_t *s, const char *what) { | |
540 | |
541 char *buf; | |
542 | |
543 if (what) { | |
544 buf=strdup(what); | |
545 } else | |
546 { | |
547 buf=malloc(sizeof(char)*(strlen(s->host)+strlen(s->path)+16)); | |
548 sprintf(buf,"rtsp://%s:%i/%s", s->host, s->port, s->path); | |
549 } | |
550 rtsp_send_request(s,"PLAY",buf); | |
551 free(buf); | |
552 | |
553 return rtsp_get_answers(s); | |
554 } | |
555 | |
556 int rtsp_request_tearoff(rtsp_t *s, const char *what) { | |
557 | |
558 rtsp_send_request(s,"TEAROFF",what); | |
559 | |
560 return rtsp_get_answers(s); | |
561 } | |
562 | |
563 /* | |
564 * read opaque data from stream | |
565 */ | |
566 | |
567 int rtsp_read_data(rtsp_t *s, char *buffer, unsigned int size) { | |
568 | |
569 int i,seq; | |
570 | |
571 if (size>=4) { | |
572 i=read_stream(s->s, buffer, 4); | |
573 if (i<4) return i; | |
574 if ((buffer[0]=='S')&&(buffer[1]=='E')&&(buffer[2]=='T')&&(buffer[3]=='_')) | |
575 { | |
576 char *rest=rtsp_get(s); | |
12271
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
577 if (!rest) |
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
578 return -1; |
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
579 |
9922 | 580 seq=-1; |
581 do { | |
582 free(rest); | |
583 rest=rtsp_get(s); | |
12271
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
584 if (!rest) |
4adb4a3b52a2
More bounds checking fixes (thnaks to Miguel Freitas)
rtognimp
parents:
11663
diff
changeset
|
585 return -1; |
9922 | 586 if (!strncmp(rest,"Cseq:",5)) |
587 sscanf(rest,"Cseq: %u",&seq); | |
588 } while (strlen(rest)!=0); | |
589 free(rest); | |
590 if (seq<0) { | |
591 #ifdef LOG | |
592 printf("rtsp: warning: cseq not recognized!\n"); | |
593 #endif | |
594 seq=1; | |
595 } | |
11000 | 596 /* let's make the server happy */ |
9922 | 597 rtsp_put(s, "RTSP/1.0 451 Parameter Not Understood"); |
598 rest=malloc(sizeof(char)*16); | |
599 sprintf(rest,"CSeq: %u", seq); | |
600 rtsp_put(s, rest); | |
601 rtsp_put(s, ""); | |
602 i=read_stream(s->s, buffer, size); | |
603 } else | |
604 { | |
605 i=read_stream(s->s, buffer+4, size-4); | |
606 i+=4; | |
607 } | |
608 } else | |
609 i=read_stream(s->s, buffer, size); | |
610 #ifdef LOG | |
611 printf("librtsp: << %d of %d bytes\n", i, size); | |
612 #endif | |
613 | |
614 return i; | |
615 } | |
616 | |
617 /* | |
618 * connect to a rtsp server | |
619 */ | |
620 | |
621 //rtsp_t *rtsp_connect(const char *mrl, const char *user_agent) { | |
622 rtsp_t *rtsp_connect(int fd, char* mrl, char *path, char *host, int port, char *user_agent) { | |
623 | |
624 rtsp_t *s=malloc(sizeof(rtsp_t)); | |
625 int i; | |
626 | |
627 for (i=0; i<MAX_FIELDS; i++) { | |
628 s->answers[i]=NULL; | |
629 s->scheduled[i]=NULL; | |
630 } | |
631 | |
632 s->server=NULL; | |
633 s->server_state=0; | |
634 s->server_caps=0; | |
635 | |
12678
9cdff452833b
cseq starts from 1 according to the standard, patch by Yoshinori Sato
alex
parents:
12271
diff
changeset
|
636 s->cseq=1; |
9922 | 637 s->session=NULL; |
638 | |
639 if (user_agent) | |
640 s->user_agent=strdup(user_agent); | |
641 else | |
642 s->user_agent=strdup("User-Agent: RealMedia Player Version 6.0.9.1235 (linux-2.0-libc6-i386-gcc2.95)"); | |
643 | |
644 s->mrl = strdup(mrl); | |
645 s->host = strdup(host); | |
646 s->port = port; | |
11663
d700a93ab34c
10l let path behave like before the start/stop patch
rtognimp
parents:
11506
diff
changeset
|
647 s->path = strdup(path); |
11506
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
648 while (*path == '/') |
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
649 path++; |
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
650 if ((s->param = strchr(s->path, '?')) != NULL) |
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
651 s->param++; |
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
652 //printf("path=%s\n", s->path); |
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
653 //printf("param=%s\n", s->param ? s->param : "NULL"); |
9922 | 654 s->s = fd; |
655 | |
656 if (s->s < 0) { | |
657 printf ("rtsp: failed to connect to '%s'\n", s->host); | |
658 rtsp_close(s); | |
659 return NULL; | |
660 } | |
661 | |
662 s->server_state=RTSP_CONNECTED; | |
663 | |
11000 | 664 /* now let's send an options request. */ |
9922 | 665 rtsp_schedule_field(s, "CSeq: 1"); |
666 rtsp_schedule_field(s, s->user_agent); | |
667 rtsp_schedule_field(s, "ClientChallenge: 9e26d33f2984236010ef6253fb1887f7"); | |
668 rtsp_schedule_field(s, "PlayerStarttime: [28/03/2003:22:50:23 00:00]"); | |
669 rtsp_schedule_field(s, "CompanyID: KnKV4M4I/B2FjJ1TToLycw=="); | |
670 rtsp_schedule_field(s, "GUID: 00000000-0000-0000-0000-000000000000"); | |
671 rtsp_schedule_field(s, "RegionData: 0"); | |
672 rtsp_schedule_field(s, "ClientID: Linux_2.4_6.0.9.1235_play32_RN01_EN_586"); | |
673 /*rtsp_schedule_field(s, "Pragma: initiate-session");*/ | |
674 rtsp_request_options(s, NULL); | |
675 | |
676 return s; | |
677 } | |
678 | |
679 | |
680 /* | |
681 * closes an rtsp connection | |
682 */ | |
683 | |
684 void rtsp_close(rtsp_t *s) { | |
685 | |
10281 | 686 if (s->server_state) closesocket(s->s); /* TODO: send a TEAROFF */ |
9922 | 687 if (s->path) free(s->path); |
688 if (s->host) free(s->host); | |
689 if (s->mrl) free(s->mrl); | |
690 if (s->session) free(s->session); | |
691 if (s->user_agent) free(s->user_agent); | |
692 rtsp_free_answers(s); | |
693 rtsp_unschedule_all(s); | |
694 free(s); | |
695 } | |
696 | |
697 /* | |
698 * search in answers for tags. returns a pointer to the content | |
699 * after the first matched tag. returns NULL if no match found. | |
700 */ | |
701 | |
702 char *rtsp_search_answers(rtsp_t *s, const char *tag) { | |
703 | |
704 char **answer; | |
705 char *ptr; | |
706 | |
707 if (!s->answers) return NULL; | |
708 answer=s->answers; | |
709 | |
710 while (*answer) { | |
711 if (!strncasecmp(*answer,tag,strlen(tag))) { | |
712 ptr=strchr(*answer,':'); | |
713 ptr++; | |
714 while(*ptr==' ') ptr++; | |
715 return ptr; | |
716 } | |
717 answer++; | |
718 } | |
719 | |
720 return NULL; | |
721 } | |
722 | |
723 /* | |
724 * session id management | |
725 */ | |
726 | |
727 void rtsp_set_session(rtsp_t *s, const char *id) { | |
728 | |
729 if (s->session) free(s->session); | |
730 | |
731 s->session=strdup(id); | |
732 | |
733 } | |
734 | |
735 char *rtsp_get_session(rtsp_t *s) { | |
736 | |
737 return s->session; | |
738 | |
739 } | |
740 | |
741 char *rtsp_get_mrl(rtsp_t *s) { | |
742 | |
743 return s->mrl; | |
744 | |
745 } | |
746 | |
11506
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
747 char *rtsp_get_param(rtsp_t *s, char *p) { |
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
748 int len; |
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
749 char *param; |
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
750 if (!s->param) |
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
751 return NULL; |
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
752 if (!p) |
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
753 return strdup(s->param); |
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
754 len = strlen(p); |
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
755 param = s->param; |
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
756 while (param && *param) { |
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
757 char *nparam = strchr(param, '&'); |
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
758 if (strncmp(param, p, len) == 0 && param[len] == '=') { |
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
759 param += len + 1; |
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
760 len = nparam ? nparam - param : strlen(param); |
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
761 nparam = malloc(len + 1); |
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
762 memcpy(nparam, param, len); |
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
763 nparam[len] = 0; |
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
764 return nparam; |
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
765 } |
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
766 param = nparam ? nparam + 1 : NULL; |
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
767 } |
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
768 return NULL; |
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
769 } |
fff1b6f1a9cc
Real rtsp Range parameter (Start and End) support.
rtognimp
parents:
11000
diff
changeset
|
770 |
9922 | 771 /* |
772 * schedules a field for transmission | |
773 */ | |
774 | |
775 void rtsp_schedule_field(rtsp_t *s, const char *string) { | |
776 | |
777 int i=0; | |
778 | |
779 if (!string) return; | |
780 | |
781 while(s->scheduled[i]) { | |
782 i++; | |
783 } | |
784 s->scheduled[i]=strdup(string); | |
785 } | |
786 | |
787 /* | |
788 * removes the first scheduled field which prefix matches string. | |
789 */ | |
790 | |
791 void rtsp_unschedule_field(rtsp_t *s, const char *string) { | |
792 | |
793 char **ptr=s->scheduled; | |
794 | |
795 if (!string) return; | |
796 | |
797 while(*ptr) { | |
798 if (!strncmp(*ptr, string, strlen(string))) | |
799 break; | |
800 } | |
801 if (*ptr) free(*ptr); | |
802 ptr++; | |
803 do { | |
804 *(ptr-1)=*ptr; | |
805 } while(*ptr); | |
806 } | |
807 | |
808 /* | |
809 * unschedule all fields | |
810 */ | |
811 | |
812 void rtsp_unschedule_all(rtsp_t *s) { | |
813 | |
814 char **ptr; | |
815 | |
816 if (!s->scheduled) return; | |
817 ptr=s->scheduled; | |
818 | |
819 while (*ptr) { | |
820 free(*ptr); | |
821 *ptr=NULL; | |
822 ptr++; | |
823 } | |
824 } | |
825 /* | |
826 * free answers | |
827 */ | |
828 | |
829 void rtsp_free_answers(rtsp_t *s) { | |
830 | |
831 char **answer; | |
832 | |
833 if (!s->answers) return; | |
834 answer=s->answers; | |
835 | |
836 while (*answer) { | |
837 free(*answer); | |
838 *answer=NULL; | |
839 answer++; | |
840 } | |
841 } |