no read loop tcp/http and http CRLF fix by (Leon van Stuivenberg <l dot vanstuivenber...
[libav.git] / libavformat / tcp.c
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>
24 #if defined(__APPLE__) || defined(__BEOS__)
25 typedef int socklen_t;
26 #endif
27 #ifndef __BEOS__
28 # include <arpa/inet.h>
29 #else
30 # include "barpainet.h"
31 #endif
32 #include <netdb.h>
33 #include <sys/time.h>
34 #include <fcntl.h>
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;
62 fd_set wfds;
63 int fd_max, ret;
64 struct timeval tv;
65 socklen_t optlen;
66
67 s = av_malloc(sizeof(TCPContext));
68 if (!s)
69 return -ENOMEM;
70 h->priv_data = s;
71 p = uri;
72 if (!strstart(p, "tcp://", &p))
73 goto fail;
74 q = hostname;
75 while (*p != ':' && *p != '/' && *p != '\0') {
76 if ((q - hostname) < sizeof(hostname) - 1)
77 *q++ = *p;
78 p++;
79 }
80 *q = '\0';
81 if (*p != ':')
82 goto fail;
83 p++;
84 port = strtoul(p, (char **)&p, 10);
85 if (port <= 0 || port >= 65536)
86 goto fail;
87
88 dest_addr.sin_family = AF_INET;
89 dest_addr.sin_port = htons(port);
90 if (resolve_host(&dest_addr.sin_addr, hostname) < 0)
91 goto fail;
92
93 fd = socket(PF_INET, SOCK_STREAM, 0);
94 if (fd < 0)
95 goto fail;
96 fcntl(fd, F_SETFL, O_NONBLOCK);
97
98 redo:
99 ret = connect(fd, (struct sockaddr *)&dest_addr,
100 sizeof(dest_addr));
101 if (ret < 0) {
102 if (errno == EINTR)
103 goto redo;
104 if (errno != EINPROGRESS)
105 goto fail;
106
107 /* wait until we are connected or until abort */
108 for(;;) {
109 if (url_interrupt_cb()) {
110 ret = -EINTR;
111 goto fail1;
112 }
113 fd_max = fd;
114 FD_ZERO(&wfds);
115 FD_SET(fd, &wfds);
116 tv.tv_sec = 0;
117 tv.tv_usec = 100 * 1000;
118 ret = select(fd_max + 1, NULL, &wfds, NULL, &tv);
119 if (ret > 0 && FD_ISSET(fd, &wfds))
120 break;
121 }
122
123 /* test error */
124 optlen = sizeof(ret);
125 getsockopt (fd, SOL_SOCKET, SO_ERROR, &ret, &optlen);
126 if (ret != 0)
127 goto fail;
128 }
129 s->fd = fd;
130 return 0;
131
132 fail:
133 ret = -EIO;
134 fail1:
135 if (fd >= 0)
136 close(fd);
137 av_free(s);
138 return ret;
139 }
140
141 static int tcp_read(URLContext *h, uint8_t *buf, int size)
142 {
143 TCPContext *s = h->priv_data;
144 int len, fd_max;
145 fd_set rfds;
146 struct timeval tv;
147
148 for (;;) {
149 if (url_interrupt_cb())
150 return -EINTR;
151 fd_max = s->fd;
152 FD_ZERO(&rfds);
153 FD_SET(s->fd, &rfds);
154 tv.tv_sec = 0;
155 tv.tv_usec = 100 * 1000;
156 select(fd_max + 1, &rfds, NULL, NULL, &tv);
157 #ifdef __BEOS__
158 len = recv(s->fd, buf, size, 0);
159 #else
160 len = read(s->fd, buf, size);
161 #endif
162 if (len < 0) {
163 if (errno != EINTR && errno != EAGAIN)
164 #ifdef __BEOS__
165 return errno;
166 #else
167 return -errno;
168 #endif
169 } else break;
170 }
171 return len;
172 }
173
174 static int tcp_write(URLContext *h, uint8_t *buf, int size)
175 {
176 TCPContext *s = h->priv_data;
177 int ret, size1, fd_max;
178 fd_set wfds;
179 struct timeval tv;
180
181 size1 = size;
182 while (size > 0) {
183 if (url_interrupt_cb())
184 return -EINTR;
185 fd_max = s->fd;
186 FD_ZERO(&wfds);
187 FD_SET(s->fd, &wfds);
188 tv.tv_sec = 0;
189 tv.tv_usec = 100 * 1000;
190 select(fd_max + 1, NULL, &wfds, NULL, &tv);
191 #ifdef __BEOS__
192 ret = send(s->fd, buf, size, 0);
193 #else
194 ret = write(s->fd, buf, size);
195 #endif
196 if (ret < 0) {
197 if (errno != EINTR && errno != EAGAIN) {
198 #ifdef __BEOS__
199 return errno;
200 #else
201 return -errno;
202 #endif
203 }
204 continue;
205 }
206 size -= ret;
207 buf += ret;
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 };