10l (dont store 2 different things at the same place ...)
[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:
0bd586c5 133 ret = AVERROR_IO;
09787fb8 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;
b51469a0 144 int len, fd_max, ret;
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;
b51469a0
LS
156 ret = select(fd_max + 1, &rfds, NULL, NULL, &tv);
157 if (ret > 0 && FD_ISSET(s->fd, &rfds)) {
09787fb8 158#ifdef __BEOS__
b51469a0 159 len = recv(s->fd, buf, size, 0);
9ddd71fc 160#else
b51469a0 161 len = read(s->fd, buf, size);
9ddd71fc 162#endif
b51469a0
LS
163 if (len < 0) {
164 if (errno != EINTR && errno != EAGAIN)
9ddd71fc 165#ifdef __BEOS__
b51469a0 166 return errno;
9ddd71fc 167#else
b51469a0 168 return -errno;
9ddd71fc 169#endif
b51469a0
LS
170 } else return len;
171 } else if (ret < 0) {
172 return -1;
173 }
171bbb03 174 }
171bbb03
FB
175}
176
0c1a9eda 177static int tcp_write(URLContext *h, uint8_t *buf, int size)
171bbb03
FB
178{
179 TCPContext *s = h->priv_data;
b51469a0 180 int ret, size1, fd_max, len;
09787fb8
FB
181 fd_set wfds;
182 struct timeval tv;
171bbb03
FB
183
184 size1 = size;
185 while (size > 0) {
09787fb8
FB
186 if (url_interrupt_cb())
187 return -EINTR;
188 fd_max = s->fd;
189 FD_ZERO(&wfds);
190 FD_SET(s->fd, &wfds);
191 tv.tv_sec = 0;
192 tv.tv_usec = 100 * 1000;
b51469a0
LS
193 ret = select(fd_max + 1, NULL, &wfds, NULL, &tv);
194 if (ret > 0 && FD_ISSET(s->fd, &wfds)) {
09787fb8 195#ifdef __BEOS__
b51469a0 196 len = send(s->fd, buf, size, 0);
9ddd71fc 197#else
b51469a0 198 len = write(s->fd, buf, size);
9ddd71fc 199#endif
b51469a0
LS
200 if (len < 0) {
201 if (errno != EINTR && errno != EAGAIN) {
9ddd71fc 202#ifdef __BEOS__
b51469a0 203 return errno;
9ddd71fc 204#else
b51469a0 205 return -errno;
9ddd71fc 206#endif
b51469a0
LS
207 }
208 continue;
6c8e0d4d 209 }
b51469a0
LS
210 size -= len;
211 buf += len;
212 } else if (ret < 0) {
213 return -1;
6c8e0d4d 214 }
171bbb03
FB
215 }
216 return size1 - size;
217}
218
219static int tcp_close(URLContext *h)
220{
221 TCPContext *s = h->priv_data;
9ddd71fc
FR
222#ifdef CONFIG_BEOS_NETSERVER
223 closesocket(s->fd);
224#else
171bbb03 225 close(s->fd);
9ddd71fc 226#endif
171bbb03
FB
227 av_free(s);
228 return 0;
229}
230
231URLProtocol tcp_protocol = {
232 "tcp",
233 tcp_open,
234 tcp_read,
235 tcp_write,
236 NULL, /* seek */
237 tcp_close,
238};