Mercurial > libavformat.hg
annotate tcp.c @ 775:c5077fdab490 libavformat
AVPacket.pos
author | michael |
---|---|
date | Thu, 26 May 2005 20:17:12 +0000 |
parents | 1fc7b1637f90 |
children | da1d5db0ce5c |
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> | |
753
1fc7b1637f90
Mac OS X 10.4 compilation fix by Steven M. Schultz <sms at 2BSD dot COM>
diego
parents:
683
diff
changeset
|
24 #if defined(__BEOS__) |
182 | 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; | |
683
095009fc2f35
kill warnings patch by (Mns Rullgrd <mru inprovide com>)
michael
parents:
511
diff
changeset
|
60 TCPContext *s = NULL; |
180 | 61 fd_set wfds; |
62 int fd_max, ret; | |
63 struct timeval tv; | |
64 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
|
65 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
|
66 |
056991ab9f10
HTTP Authentication Patch by (Petr Doubek <doubek at vision dot ee dot ethz dot ch>)
michael
parents:
482
diff
changeset
|
67 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
|
68 &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
|
69 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
|
70 if ((q = strchr(hostname,'@'))) { strcpy(tmp,q+1); strcpy(hostname,tmp); } // PETR: take only the part after '@' for tcp protocol |
180 | 71 |
0 | 72 s = av_malloc(sizeof(TCPContext)); |
73 if (!s) | |
74 return -ENOMEM; | |
75 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
|
76 |
0 | 77 if (port <= 0 || port >= 65536) |
78 goto fail; | |
79 | |
80 dest_addr.sin_family = AF_INET; | |
81 dest_addr.sin_port = htons(port); | |
82 if (resolve_host(&dest_addr.sin_addr, hostname) < 0) | |
83 goto fail; | |
84 | |
85 fd = socket(PF_INET, SOCK_STREAM, 0); | |
86 if (fd < 0) | |
87 goto fail; | |
180 | 88 fcntl(fd, F_SETFL, O_NONBLOCK); |
89 | |
90 redo: | |
91 ret = connect(fd, (struct sockaddr *)&dest_addr, | |
92 sizeof(dest_addr)); | |
93 if (ret < 0) { | |
94 if (errno == EINTR) | |
95 goto redo; | |
96 if (errno != EINPROGRESS) | |
97 goto fail; | |
0 | 98 |
180 | 99 /* wait until we are connected or until abort */ |
100 for(;;) { | |
101 if (url_interrupt_cb()) { | |
102 ret = -EINTR; | |
103 goto fail1; | |
104 } | |
105 fd_max = fd; | |
106 FD_ZERO(&wfds); | |
107 FD_SET(fd, &wfds); | |
108 tv.tv_sec = 0; | |
109 tv.tv_usec = 100 * 1000; | |
110 ret = select(fd_max + 1, NULL, &wfds, NULL, &tv); | |
111 if (ret > 0 && FD_ISSET(fd, &wfds)) | |
112 break; | |
113 } | |
114 | |
115 /* test error */ | |
116 optlen = sizeof(ret); | |
117 getsockopt (fd, SOL_SOCKET, SO_ERROR, &ret, &optlen); | |
118 if (ret != 0) | |
119 goto fail; | |
120 } | |
0 | 121 s->fd = fd; |
122 return 0; | |
123 | |
124 fail: | |
482 | 125 ret = AVERROR_IO; |
180 | 126 fail1: |
0 | 127 if (fd >= 0) |
128 close(fd); | |
129 av_free(s); | |
180 | 130 return ret; |
0 | 131 } |
132 | |
65 | 133 static int tcp_read(URLContext *h, uint8_t *buf, int size) |
0 | 134 { |
135 TCPContext *s = h->priv_data; | |
388
9af30d452a0a
tcp select() check and enables pressing 'q' when reading/(writing) from
michael
parents:
385
diff
changeset
|
136 int len, fd_max, ret; |
180 | 137 fd_set rfds; |
138 struct timeval tv; | |
0 | 139 |
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
|
140 for (;;) { |
180 | 141 if (url_interrupt_cb()) |
142 return -EINTR; | |
143 fd_max = s->fd; | |
144 FD_ZERO(&rfds); | |
145 FD_SET(s->fd, &rfds); | |
146 tv.tv_sec = 0; | |
147 tv.tv_usec = 100 * 1000; | |
388
9af30d452a0a
tcp select() check and enables pressing 'q' when reading/(writing) from
michael
parents:
385
diff
changeset
|
148 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
|
149 if (ret > 0 && FD_ISSET(s->fd, &rfds)) { |
180 | 150 #ifdef __BEOS__ |
388
9af30d452a0a
tcp select() check and enables pressing 'q' when reading/(writing) from
michael
parents:
385
diff
changeset
|
151 len = recv(s->fd, buf, size, 0); |
0 | 152 #else |
388
9af30d452a0a
tcp select() check and enables pressing 'q' when reading/(writing) from
michael
parents:
385
diff
changeset
|
153 len = read(s->fd, buf, size); |
0 | 154 #endif |
388
9af30d452a0a
tcp select() check and enables pressing 'q' when reading/(writing) from
michael
parents:
385
diff
changeset
|
155 if (len < 0) { |
9af30d452a0a
tcp select() check and enables pressing 'q' when reading/(writing) from
michael
parents:
385
diff
changeset
|
156 if (errno != EINTR && errno != EAGAIN) |
0 | 157 #ifdef __BEOS__ |
388
9af30d452a0a
tcp select() check and enables pressing 'q' when reading/(writing) from
michael
parents:
385
diff
changeset
|
158 return errno; |
0 | 159 #else |
388
9af30d452a0a
tcp select() check and enables pressing 'q' when reading/(writing) from
michael
parents:
385
diff
changeset
|
160 return -errno; |
0 | 161 #endif |
388
9af30d452a0a
tcp select() check and enables pressing 'q' when reading/(writing) from
michael
parents:
385
diff
changeset
|
162 } else return len; |
9af30d452a0a
tcp select() check and enables pressing 'q' when reading/(writing) from
michael
parents:
385
diff
changeset
|
163 } else if (ret < 0) { |
9af30d452a0a
tcp select() check and enables pressing 'q' when reading/(writing) from
michael
parents:
385
diff
changeset
|
164 return -1; |
9af30d452a0a
tcp select() check and enables pressing 'q' when reading/(writing) from
michael
parents:
385
diff
changeset
|
165 } |
0 | 166 } |
167 } | |
168 | |
65 | 169 static int tcp_write(URLContext *h, uint8_t *buf, int size) |
0 | 170 { |
171 TCPContext *s = h->priv_data; | |
388
9af30d452a0a
tcp select() check and enables pressing 'q' when reading/(writing) from
michael
parents:
385
diff
changeset
|
172 int ret, size1, fd_max, len; |
180 | 173 fd_set wfds; |
174 struct timeval tv; | |
0 | 175 |
176 size1 = size; | |
177 while (size > 0) { | |
180 | 178 if (url_interrupt_cb()) |
179 return -EINTR; | |
180 fd_max = s->fd; | |
181 FD_ZERO(&wfds); | |
182 FD_SET(s->fd, &wfds); | |
183 tv.tv_sec = 0; | |
184 tv.tv_usec = 100 * 1000; | |
388
9af30d452a0a
tcp select() check and enables pressing 'q' when reading/(writing) from
michael
parents:
385
diff
changeset
|
185 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
|
186 if (ret > 0 && FD_ISSET(s->fd, &wfds)) { |
180 | 187 #ifdef __BEOS__ |
388
9af30d452a0a
tcp select() check and enables pressing 'q' when reading/(writing) from
michael
parents:
385
diff
changeset
|
188 len = send(s->fd, buf, size, 0); |
0 | 189 #else |
388
9af30d452a0a
tcp select() check and enables pressing 'q' when reading/(writing) from
michael
parents:
385
diff
changeset
|
190 len = write(s->fd, buf, size); |
0 | 191 #endif |
388
9af30d452a0a
tcp select() check and enables pressing 'q' when reading/(writing) from
michael
parents:
385
diff
changeset
|
192 if (len < 0) { |
9af30d452a0a
tcp select() check and enables pressing 'q' when reading/(writing) from
michael
parents:
385
diff
changeset
|
193 if (errno != EINTR && errno != EAGAIN) { |
0 | 194 #ifdef __BEOS__ |
388
9af30d452a0a
tcp select() check and enables pressing 'q' when reading/(writing) from
michael
parents:
385
diff
changeset
|
195 return errno; |
0 | 196 #else |
388
9af30d452a0a
tcp select() check and enables pressing 'q' when reading/(writing) from
michael
parents:
385
diff
changeset
|
197 return -errno; |
0 | 198 #endif |
388
9af30d452a0a
tcp select() check and enables pressing 'q' when reading/(writing) from
michael
parents:
385
diff
changeset
|
199 } |
9af30d452a0a
tcp select() check and enables pressing 'q' when reading/(writing) from
michael
parents:
385
diff
changeset
|
200 continue; |
261
5f27f90ed496
Fix a very nasty problem with extra bytes appearing in TCP data streams.
philipjsg
parents:
229
diff
changeset
|
201 } |
388
9af30d452a0a
tcp select() check and enables pressing 'q' when reading/(writing) from
michael
parents:
385
diff
changeset
|
202 size -= len; |
9af30d452a0a
tcp select() check and enables pressing 'q' when reading/(writing) from
michael
parents:
385
diff
changeset
|
203 buf += len; |
9af30d452a0a
tcp select() check and enables pressing 'q' when reading/(writing) from
michael
parents:
385
diff
changeset
|
204 } else if (ret < 0) { |
9af30d452a0a
tcp select() check and enables pressing 'q' when reading/(writing) from
michael
parents:
385
diff
changeset
|
205 return -1; |
261
5f27f90ed496
Fix a very nasty problem with extra bytes appearing in TCP data streams.
philipjsg
parents:
229
diff
changeset
|
206 } |
0 | 207 } |
208 return size1 - size; | |
209 } | |
210 | |
211 static int tcp_close(URLContext *h) | |
212 { | |
213 TCPContext *s = h->priv_data; | |
214 #ifdef CONFIG_BEOS_NETSERVER | |
215 closesocket(s->fd); | |
216 #else | |
217 close(s->fd); | |
218 #endif | |
219 av_free(s); | |
220 return 0; | |
221 } | |
222 | |
223 URLProtocol tcp_protocol = { | |
224 "tcp", | |
225 tcp_open, | |
226 tcp_read, | |
227 tcp_write, | |
228 NULL, /* seek */ | |
229 tcp_close, | |
230 }; |