Mercurial > libavformat.hg
annotate tcp.c @ 592:f28e99a6ac99 libavformat
rv20 demuxing fix
author | michael |
---|---|
date | Fri, 19 Nov 2004 16:36:30 +0000 |
parents | 056991ab9f10 |
children | 095009fc2f35 |
rev | line source |
---|---|
0 | 1 /* |
2 * TCP protocol | |
3 * Copyright (c) 2002 Fabrice Bellard. | |
4 * | |
5 * This library is free software; you can redistribute it and/or | |
6 * modify it under the terms of the GNU Lesser General Public | |
7 * License as published by the Free Software Foundation; either | |
8 * version 2 of the License, or (at your option) any later version. | |
9 * | |
10 * This library is distributed in the hope that it will be useful, | |
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 * Lesser General Public License for more details. | |
14 * | |
15 * You should have received a copy of the GNU Lesser General Public | |
16 * License along with this library; if not, write to the Free Software | |
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
18 */ | |
19 #include "avformat.h" | |
20 #include <unistd.h> | |
21 #include <sys/types.h> | |
22 #include <sys/socket.h> | |
23 #include <netinet/in.h> | |
182 | 24 #if defined(__APPLE__) || defined(__BEOS__) |
25 typedef int socklen_t; | |
26 #endif | |
0 | 27 #ifndef __BEOS__ |
28 # include <arpa/inet.h> | |
29 #else | |
30 # include "barpainet.h" | |
31 #endif | |
32 #include <netdb.h> | |
180 | 33 #include <sys/time.h> |
34 #include <fcntl.h> | |
0 | 35 |
36 typedef struct TCPContext { | |
37 int fd; | |
38 } TCPContext; | |
39 | |
40 /* resolve host with also IP address parsing */ | |
41 int resolve_host(struct in_addr *sin_addr, const char *hostname) | |
42 { | |
43 struct hostent *hp; | |
44 | |
45 if ((inet_aton(hostname, sin_addr)) == 0) { | |
46 hp = gethostbyname(hostname); | |
47 if (!hp) | |
48 return -1; | |
49 memcpy (sin_addr, hp->h_addr, sizeof(struct in_addr)); | |
50 } | |
51 return 0; | |
52 } | |
53 | |
54 /* return non zero if error */ | |
55 static int tcp_open(URLContext *h, const char *uri, int flags) | |
56 { | |
57 struct sockaddr_in dest_addr; | |
58 char hostname[1024], *q; | |
59 int port, fd = -1; | |
60 TCPContext *s; | |
61 const char *p; | |
180 | 62 fd_set wfds; |
63 int fd_max, ret; | |
64 struct timeval tv; | |
65 socklen_t optlen; | |
511
056991ab9f10
HTTP Authentication Patch by (Petr Doubek <doubek at vision dot ee dot ethz dot ch>)
michael
parents:
482
diff
changeset
|
66 char proto[1024],path[1024],tmp[1024]; // PETR: protocol and path strings |
056991ab9f10
HTTP Authentication Patch by (Petr Doubek <doubek at vision dot ee dot ethz dot ch>)
michael
parents:
482
diff
changeset
|
67 |
056991ab9f10
HTTP Authentication Patch by (Petr Doubek <doubek at vision dot ee dot ethz dot ch>)
michael
parents:
482
diff
changeset
|
68 url_split(proto, sizeof(proto), NULL, 0, hostname, sizeof(hostname), |
056991ab9f10
HTTP Authentication Patch by (Petr Doubek <doubek at vision dot ee dot ethz dot ch>)
michael
parents:
482
diff
changeset
|
69 &port, path, sizeof(path), uri); // PETR: use url_split |
056991ab9f10
HTTP Authentication Patch by (Petr Doubek <doubek at vision dot ee dot ethz dot ch>)
michael
parents:
482
diff
changeset
|
70 if (strcmp(proto,"tcp")) goto fail; // PETR: check protocol |
056991ab9f10
HTTP Authentication Patch by (Petr Doubek <doubek at vision dot ee dot ethz dot ch>)
michael
parents:
482
diff
changeset
|
71 if ((q = strchr(hostname,'@'))) { strcpy(tmp,q+1); strcpy(hostname,tmp); } // PETR: take only the part after '@' for tcp protocol |
180 | 72 |
0 | 73 s = av_malloc(sizeof(TCPContext)); |
74 if (!s) | |
75 return -ENOMEM; | |
76 h->priv_data = s; | |
511
056991ab9f10
HTTP Authentication Patch by (Petr Doubek <doubek at vision dot ee dot ethz dot ch>)
michael
parents:
482
diff
changeset
|
77 |
0 | 78 if (port <= 0 || port >= 65536) |
79 goto fail; | |
80 | |
81 dest_addr.sin_family = AF_INET; | |
82 dest_addr.sin_port = htons(port); | |
83 if (resolve_host(&dest_addr.sin_addr, hostname) < 0) | |
84 goto fail; | |
85 | |
86 fd = socket(PF_INET, SOCK_STREAM, 0); | |
87 if (fd < 0) | |
88 goto fail; | |
180 | 89 fcntl(fd, F_SETFL, O_NONBLOCK); |
90 | |
91 redo: | |
92 ret = connect(fd, (struct sockaddr *)&dest_addr, | |
93 sizeof(dest_addr)); | |
94 if (ret < 0) { | |
95 if (errno == EINTR) | |
96 goto redo; | |
97 if (errno != EINPROGRESS) | |
98 goto fail; | |
0 | 99 |
180 | 100 /* wait until we are connected or until abort */ |
101 for(;;) { | |
102 if (url_interrupt_cb()) { | |
103 ret = -EINTR; | |
104 goto fail1; | |
105 } | |
106 fd_max = fd; | |
107 FD_ZERO(&wfds); | |
108 FD_SET(fd, &wfds); | |
109 tv.tv_sec = 0; | |
110 tv.tv_usec = 100 * 1000; | |
111 ret = select(fd_max + 1, NULL, &wfds, NULL, &tv); | |
112 if (ret > 0 && FD_ISSET(fd, &wfds)) | |
113 break; | |
114 } | |
115 | |
116 /* test error */ | |
117 optlen = sizeof(ret); | |
118 getsockopt (fd, SOL_SOCKET, SO_ERROR, &ret, &optlen); | |
119 if (ret != 0) | |
120 goto fail; | |
121 } | |
0 | 122 s->fd = fd; |
123 return 0; | |
124 | |
125 fail: | |
482 | 126 ret = AVERROR_IO; |
180 | 127 fail1: |
0 | 128 if (fd >= 0) |
129 close(fd); | |
130 av_free(s); | |
180 | 131 return ret; |
0 | 132 } |
133 | |
65 | 134 static int tcp_read(URLContext *h, uint8_t *buf, int size) |
0 | 135 { |
136 TCPContext *s = h->priv_data; | |
388
9af30d452a0a
tcp select() check and enables pressing 'q' when reading/(writing) from
michael
parents:
385
diff
changeset
|
137 int len, fd_max, ret; |
180 | 138 fd_set rfds; |
139 struct timeval tv; | |
0 | 140 |
385
2f56d366a787
no read loop tcp/http and http CRLF fix by (Leon van Stuivenberg <l dot vanstuivenberg at chello dot nl>)
michael
parents:
261
diff
changeset
|
141 for (;;) { |
180 | 142 if (url_interrupt_cb()) |
143 return -EINTR; | |
144 fd_max = s->fd; | |
145 FD_ZERO(&rfds); | |
146 FD_SET(s->fd, &rfds); | |
147 tv.tv_sec = 0; | |
148 tv.tv_usec = 100 * 1000; | |
388
9af30d452a0a
tcp select() check and enables pressing 'q' when reading/(writing) from
michael
parents:
385
diff
changeset
|
149 ret = select(fd_max + 1, &rfds, NULL, NULL, &tv); |
9af30d452a0a
tcp select() check and enables pressing 'q' when reading/(writing) from
michael
parents:
385
diff
changeset
|
150 if (ret > 0 && FD_ISSET(s->fd, &rfds)) { |
180 | 151 #ifdef __BEOS__ |
388
9af30d452a0a
tcp select() check and enables pressing 'q' when reading/(writing) from
michael
parents:
385
diff
changeset
|
152 len = recv(s->fd, buf, size, 0); |
0 | 153 #else |
388
9af30d452a0a
tcp select() check and enables pressing 'q' when reading/(writing) from
michael
parents:
385
diff
changeset
|
154 len = read(s->fd, buf, size); |
0 | 155 #endif |
388
9af30d452a0a
tcp select() check and enables pressing 'q' when reading/(writing) from
michael
parents:
385
diff
changeset
|
156 if (len < 0) { |
9af30d452a0a
tcp select() check and enables pressing 'q' when reading/(writing) from
michael
parents:
385
diff
changeset
|
157 if (errno != EINTR && errno != EAGAIN) |
0 | 158 #ifdef __BEOS__ |
388
9af30d452a0a
tcp select() check and enables pressing 'q' when reading/(writing) from
michael
parents:
385
diff
changeset
|
159 return errno; |
0 | 160 #else |
388
9af30d452a0a
tcp select() check and enables pressing 'q' when reading/(writing) from
michael
parents:
385
diff
changeset
|
161 return -errno; |
0 | 162 #endif |
388
9af30d452a0a
tcp select() check and enables pressing 'q' when reading/(writing) from
michael
parents:
385
diff
changeset
|
163 } else return len; |
9af30d452a0a
tcp select() check and enables pressing 'q' when reading/(writing) from
michael
parents:
385
diff
changeset
|
164 } else if (ret < 0) { |
9af30d452a0a
tcp select() check and enables pressing 'q' when reading/(writing) from
michael
parents:
385
diff
changeset
|
165 return -1; |
9af30d452a0a
tcp select() check and enables pressing 'q' when reading/(writing) from
michael
parents:
385
diff
changeset
|
166 } |
0 | 167 } |
168 } | |
169 | |
65 | 170 static int tcp_write(URLContext *h, uint8_t *buf, int size) |
0 | 171 { |
172 TCPContext *s = h->priv_data; | |
388
9af30d452a0a
tcp select() check and enables pressing 'q' when reading/(writing) from
michael
parents:
385
diff
changeset
|
173 int ret, size1, fd_max, len; |
180 | 174 fd_set wfds; |
175 struct timeval tv; | |
0 | 176 |
177 size1 = size; | |
178 while (size > 0) { | |
180 | 179 if (url_interrupt_cb()) |
180 return -EINTR; | |
181 fd_max = s->fd; | |
182 FD_ZERO(&wfds); | |
183 FD_SET(s->fd, &wfds); | |
184 tv.tv_sec = 0; | |
185 tv.tv_usec = 100 * 1000; | |
388
9af30d452a0a
tcp select() check and enables pressing 'q' when reading/(writing) from
michael
parents:
385
diff
changeset
|
186 ret = select(fd_max + 1, NULL, &wfds, NULL, &tv); |
9af30d452a0a
tcp select() check and enables pressing 'q' when reading/(writing) from
michael
parents:
385
diff
changeset
|
187 if (ret > 0 && FD_ISSET(s->fd, &wfds)) { |
180 | 188 #ifdef __BEOS__ |
388
9af30d452a0a
tcp select() check and enables pressing 'q' when reading/(writing) from
michael
parents:
385
diff
changeset
|
189 len = send(s->fd, buf, size, 0); |
0 | 190 #else |
388
9af30d452a0a
tcp select() check and enables pressing 'q' when reading/(writing) from
michael
parents:
385
diff
changeset
|
191 len = write(s->fd, buf, size); |
0 | 192 #endif |
388
9af30d452a0a
tcp select() check and enables pressing 'q' when reading/(writing) from
michael
parents:
385
diff
changeset
|
193 if (len < 0) { |
9af30d452a0a
tcp select() check and enables pressing 'q' when reading/(writing) from
michael
parents:
385
diff
changeset
|
194 if (errno != EINTR && errno != EAGAIN) { |
0 | 195 #ifdef __BEOS__ |
388
9af30d452a0a
tcp select() check and enables pressing 'q' when reading/(writing) from
michael
parents:
385
diff
changeset
|
196 return errno; |
0 | 197 #else |
388
9af30d452a0a
tcp select() check and enables pressing 'q' when reading/(writing) from
michael
parents:
385
diff
changeset
|
198 return -errno; |
0 | 199 #endif |
388
9af30d452a0a
tcp select() check and enables pressing 'q' when reading/(writing) from
michael
parents:
385
diff
changeset
|
200 } |
9af30d452a0a
tcp select() check and enables pressing 'q' when reading/(writing) from
michael
parents:
385
diff
changeset
|
201 continue; |
261
5f27f90ed496
Fix a very nasty problem with extra bytes appearing in TCP data streams.
philipjsg
parents:
229
diff
changeset
|
202 } |
388
9af30d452a0a
tcp select() check and enables pressing 'q' when reading/(writing) from
michael
parents:
385
diff
changeset
|
203 size -= len; |
9af30d452a0a
tcp select() check and enables pressing 'q' when reading/(writing) from
michael
parents:
385
diff
changeset
|
204 buf += len; |
9af30d452a0a
tcp select() check and enables pressing 'q' when reading/(writing) from
michael
parents:
385
diff
changeset
|
205 } else if (ret < 0) { |
9af30d452a0a
tcp select() check and enables pressing 'q' when reading/(writing) from
michael
parents:
385
diff
changeset
|
206 return -1; |
261
5f27f90ed496
Fix a very nasty problem with extra bytes appearing in TCP data streams.
philipjsg
parents:
229
diff
changeset
|
207 } |
0 | 208 } |
209 return size1 - size; | |
210 } | |
211 | |
212 static int tcp_close(URLContext *h) | |
213 { | |
214 TCPContext *s = h->priv_data; | |
215 #ifdef CONFIG_BEOS_NETSERVER | |
216 closesocket(s->fd); | |
217 #else | |
218 close(s->fd); | |
219 #endif | |
220 av_free(s); | |
221 return 0; | |
222 } | |
223 | |
224 URLProtocol tcp_protocol = { | |
225 "tcp", | |
226 tcp_open, | |
227 tcp_read, | |
228 tcp_write, | |
229 NULL, /* seek */ | |
230 tcp_close, | |
231 }; |