malloc padding to avoid reading past the malloc()ed area.
[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>
01cf54f0 24#if defined(__BEOS__)
438fcb75
FR
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;
88730be6 60 TCPContext *s = NULL;
09787fb8
FB
61 fd_set wfds;
62 int fd_max, ret;
63 struct timeval tv;
64 socklen_t optlen;
6ba5cbc6
PD
65 char proto[1024],path[1024],tmp[1024]; // PETR: protocol and path strings
66
67 url_split(proto, sizeof(proto), NULL, 0, hostname, sizeof(hostname),
68 &port, path, sizeof(path), uri); // PETR: use url_split
69 if (strcmp(proto,"tcp")) goto fail; // PETR: check protocol
70 if ((q = strchr(hostname,'@'))) { strcpy(tmp,q+1); strcpy(hostname,tmp); } // PETR: take only the part after '@' for tcp protocol
09787fb8 71
171bbb03
FB
72 s = av_malloc(sizeof(TCPContext));
73 if (!s)
74 return -ENOMEM;
75 h->priv_data = s;
6ba5cbc6 76
171bbb03
FB
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;
09787fb8
FB
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;
171bbb03 98
09787fb8
FB
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 }
171bbb03
FB
121 s->fd = fd;
122 return 0;
123
124 fail:
0bd586c5 125 ret = AVERROR_IO;
09787fb8 126 fail1:
171bbb03
FB
127 if (fd >= 0)
128 close(fd);
129 av_free(s);
09787fb8 130 return ret;
171bbb03
FB
131}
132
0c1a9eda 133static int tcp_read(URLContext *h, uint8_t *buf, int size)
171bbb03
FB
134{
135 TCPContext *s = h->priv_data;
b51469a0 136 int len, fd_max, ret;
09787fb8
FB
137 fd_set rfds;
138 struct timeval tv;
171bbb03 139
9eef2b77 140 for (;;) {
09787fb8
FB
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;
b51469a0
LS
148 ret = select(fd_max + 1, &rfds, NULL, NULL, &tv);
149 if (ret > 0 && FD_ISSET(s->fd, &rfds)) {
09787fb8 150#ifdef __BEOS__
b51469a0 151 len = recv(s->fd, buf, size, 0);
9ddd71fc 152#else
b51469a0 153 len = read(s->fd, buf, size);
9ddd71fc 154#endif
b51469a0
LS
155 if (len < 0) {
156 if (errno != EINTR && errno != EAGAIN)
9ddd71fc 157#ifdef __BEOS__
b51469a0 158 return errno;
9ddd71fc 159#else
b51469a0 160 return -errno;
9ddd71fc 161#endif
b51469a0
LS
162 } else return len;
163 } else if (ret < 0) {
164 return -1;
165 }
171bbb03 166 }
171bbb03
FB
167}
168
0c1a9eda 169static int tcp_write(URLContext *h, uint8_t *buf, int size)
171bbb03
FB
170{
171 TCPContext *s = h->priv_data;
b51469a0 172 int ret, size1, fd_max, len;
09787fb8
FB
173 fd_set wfds;
174 struct timeval tv;
171bbb03
FB
175
176 size1 = size;
177 while (size > 0) {
09787fb8
FB
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;
b51469a0
LS
185 ret = select(fd_max + 1, NULL, &wfds, NULL, &tv);
186 if (ret > 0 && FD_ISSET(s->fd, &wfds)) {
09787fb8 187#ifdef __BEOS__
b51469a0 188 len = send(s->fd, buf, size, 0);
9ddd71fc 189#else
b51469a0 190 len = write(s->fd, buf, size);
9ddd71fc 191#endif
b51469a0
LS
192 if (len < 0) {
193 if (errno != EINTR && errno != EAGAIN) {
9ddd71fc 194#ifdef __BEOS__
b51469a0 195 return errno;
9ddd71fc 196#else
b51469a0 197 return -errno;
9ddd71fc 198#endif
b51469a0
LS
199 }
200 continue;
6c8e0d4d 201 }
b51469a0
LS
202 size -= len;
203 buf += len;
204 } else if (ret < 0) {
205 return -1;
6c8e0d4d 206 }
171bbb03
FB
207 }
208 return size1 - size;
209}
210
211static int tcp_close(URLContext *h)
212{
213 TCPContext *s = h->priv_data;
9ddd71fc
FR
214#ifdef CONFIG_BEOS_NETSERVER
215 closesocket(s->fd);
216#else
171bbb03 217 close(s->fd);
9ddd71fc 218#endif
171bbb03
FB
219 av_free(s);
220 return 0;
221}
222
223URLProtocol tcp_protocol = {
224 "tcp",
225 tcp_open,
226 tcp_read,
227 tcp_write,
228 NULL, /* seek */
229 tcp_close,
230};