]> git.sesse.net Git - ffmpeg/blob - libavformat/tcp.c
memleak fix
[ffmpeg] / 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     char proto[1024],path[1024],tmp[1024];  // PETR: protocol and path strings
67
68     url_split(proto, sizeof(proto), NULL, 0, hostname, sizeof(hostname),
69       &port, path, sizeof(path), uri);  // PETR: use url_split
70     if (strcmp(proto,"tcp")) goto fail; // PETR: check protocol
71     if ((q = strchr(hostname,'@'))) { strcpy(tmp,q+1); strcpy(hostname,tmp); } // PETR: take only the part after '@' for tcp protocol
72     
73     s = av_malloc(sizeof(TCPContext));
74     if (!s)
75         return -ENOMEM;
76     h->priv_data = s;
77     
78     if (port <= 0 || port >= 65536)
79         goto fail;
80     
81     dest_addr.sin_family = AF_INET;
82     dest_addr.sin_port = htons(port);
83     if (resolve_host(&dest_addr.sin_addr, hostname) < 0)
84         goto fail;
85
86     fd = socket(PF_INET, SOCK_STREAM, 0);
87     if (fd < 0)
88         goto fail;
89     fcntl(fd, F_SETFL, O_NONBLOCK);
90     
91  redo:
92     ret = connect(fd, (struct sockaddr *)&dest_addr, 
93                   sizeof(dest_addr));
94     if (ret < 0) {
95         if (errno == EINTR)
96             goto redo;
97         if (errno != EINPROGRESS)
98             goto fail;
99
100         /* wait until we are connected or until abort */
101         for(;;) {
102             if (url_interrupt_cb()) {
103                 ret = -EINTR;
104                 goto fail1;
105             }
106             fd_max = fd;
107             FD_ZERO(&wfds);
108             FD_SET(fd, &wfds);
109             tv.tv_sec = 0;
110             tv.tv_usec = 100 * 1000;
111             ret = select(fd_max + 1, NULL, &wfds, NULL, &tv);
112             if (ret > 0 && FD_ISSET(fd, &wfds))
113                 break;
114         }
115         
116         /* test error */
117         optlen = sizeof(ret);
118         getsockopt (fd, SOL_SOCKET, SO_ERROR, &ret, &optlen);
119         if (ret != 0)
120             goto fail;
121     }
122     s->fd = fd;
123     return 0;
124
125  fail:
126     ret = AVERROR_IO;
127  fail1:
128     if (fd >= 0)
129         close(fd);
130     av_free(s);
131     return ret;
132 }
133
134 static int tcp_read(URLContext *h, uint8_t *buf, int size)
135 {
136     TCPContext *s = h->priv_data;
137     int len, fd_max, ret;
138     fd_set rfds;
139     struct timeval tv;
140
141     for (;;) {
142         if (url_interrupt_cb())
143             return -EINTR;
144         fd_max = s->fd;
145         FD_ZERO(&rfds);
146         FD_SET(s->fd, &rfds);
147         tv.tv_sec = 0;
148         tv.tv_usec = 100 * 1000;
149         ret = select(fd_max + 1, &rfds, NULL, NULL, &tv);
150         if (ret > 0 && FD_ISSET(s->fd, &rfds)) {
151 #ifdef __BEOS__
152             len = recv(s->fd, buf, size, 0);
153 #else
154             len = read(s->fd, buf, size);
155 #endif
156             if (len < 0) {
157                 if (errno != EINTR && errno != EAGAIN)
158 #ifdef __BEOS__
159                     return errno;
160 #else
161                     return -errno;
162 #endif
163             } else return len;
164         } else if (ret < 0) {
165             return -1;
166         }
167     }
168 }
169
170 static int tcp_write(URLContext *h, uint8_t *buf, int size)
171 {
172     TCPContext *s = h->priv_data;
173     int ret, size1, fd_max, len;
174     fd_set wfds;
175     struct timeval tv;
176
177     size1 = size;
178     while (size > 0) {
179         if (url_interrupt_cb())
180             return -EINTR;
181         fd_max = s->fd;
182         FD_ZERO(&wfds);
183         FD_SET(s->fd, &wfds);
184         tv.tv_sec = 0;
185         tv.tv_usec = 100 * 1000;
186         ret = select(fd_max + 1, NULL, &wfds, NULL, &tv);
187         if (ret > 0 && FD_ISSET(s->fd, &wfds)) {
188 #ifdef __BEOS__
189             len = send(s->fd, buf, size, 0);
190 #else
191             len = write(s->fd, buf, size);
192 #endif
193             if (len < 0) {
194                 if (errno != EINTR && errno != EAGAIN) {
195 #ifdef __BEOS__
196                     return errno;
197 #else
198                     return -errno;
199 #endif
200                 }
201                 continue;
202             }
203             size -= len;
204             buf += len;
205         } else if (ret < 0) {
206             return -1;
207         }
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 };