altivec swscale under Linux patch by (Magnus Damm <damm at opensource dot se>)
[libav.git] / libavformat / tcp.c
CommitLineData
171bbb03
FB
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>
171bbb03 21#include <sys/types.h>
b8a78f41 22#include <sys/socket.h>
171bbb03 23#include <netinet/in.h>
438fcb75
FR
24#if defined(__APPLE__) || defined(__BEOS__)
25typedef int socklen_t;
26#endif
9ddd71fc
FR
27#ifndef __BEOS__
28# include <arpa/inet.h>
29#else
30# include "barpainet.h"
31#endif
171bbb03 32#include <netdb.h>
09787fb8
FB
33#include <sys/time.h>
34#include <fcntl.h>
171bbb03
FB
35
36typedef struct TCPContext {
37 int fd;
38} TCPContext;
39
40/* resolve host with also IP address parsing */
41int 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 */
55static 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;
09787fb8
FB
62 fd_set wfds;
63 int fd_max, ret;
64 struct timeval tv;
65 socklen_t optlen;
66
171bbb03
FB
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;
09787fb8
FB
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;
171bbb03 106
09787fb8
FB
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 }
171bbb03
FB
129 s->fd = fd;
130 return 0;
131
132 fail:
09787fb8
FB
133 ret = -EIO;
134 fail1:
171bbb03
FB
135 if (fd >= 0)
136 close(fd);
137 av_free(s);
09787fb8 138 return ret;
171bbb03
FB
139}
140
0c1a9eda 141static int tcp_read(URLContext *h, uint8_t *buf, int size)
171bbb03
FB
142{
143 TCPContext *s = h->priv_data;
9eef2b77 144 int len, fd_max;
09787fb8
FB
145 fd_set rfds;
146 struct timeval tv;
171bbb03 147
9eef2b77 148 for (;;) {
09787fb8
FB
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);
9ddd71fc 159#else
09787fb8 160 len = read(s->fd, buf, size);
9ddd71fc 161#endif
171bbb03
FB
162 if (len < 0) {
163 if (errno != EINTR && errno != EAGAIN)
9ddd71fc
FR
164#ifdef __BEOS__
165 return errno;
166#else
171bbb03 167 return -errno;
9ddd71fc 168#endif
9eef2b77 169 } else break;
171bbb03 170 }
9eef2b77 171 return len;
171bbb03
FB
172}
173
0c1a9eda 174static int tcp_write(URLContext *h, uint8_t *buf, int size)
171bbb03
FB
175{
176 TCPContext *s = h->priv_data;
09787fb8
FB
177 int ret, size1, fd_max;
178 fd_set wfds;
179 struct timeval tv;
171bbb03
FB
180
181 size1 = size;
182 while (size > 0) {
09787fb8
FB
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);
9ddd71fc 193#else
09787fb8 194 ret = write(s->fd, buf, size);
9ddd71fc 195#endif
6c8e0d4d
PG
196 if (ret < 0) {
197 if (errno != EINTR && errno != EAGAIN) {
9ddd71fc 198#ifdef __BEOS__
6c8e0d4d 199 return errno;
9ddd71fc 200#else
6c8e0d4d 201 return -errno;
9ddd71fc 202#endif
6c8e0d4d
PG
203 }
204 continue;
205 }
171bbb03
FB
206 size -= ret;
207 buf += ret;
208 }
209 return size1 - size;
210}
211
212static int tcp_close(URLContext *h)
213{
214 TCPContext *s = h->priv_data;
9ddd71fc
FR
215#ifdef CONFIG_BEOS_NETSERVER
216 closesocket(s->fd);
217#else
171bbb03 218 close(s->fd);
9ddd71fc 219#endif
171bbb03
FB
220 av_free(s);
221 return 0;
222}
223
224URLProtocol tcp_protocol = {
225 "tcp",
226 tcp_open,
227 tcp_read,
228 tcp_write,
229 NULL, /* seek */
230 tcp_close,
231};