OSDN Git Service

wtv: display warning if scrambled stream is detected
[coroid/libav_saccubus.git] / libavformat / tcp.c
1 /*
2  * TCP protocol
3  * Copyright (c) 2002 Fabrice Bellard
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 #include "avformat.h"
22 #include <unistd.h>
23 #include "internal.h"
24 #include "network.h"
25 #include "os_support.h"
26 #if HAVE_SYS_SELECT_H
27 #include <sys/select.h>
28 #endif
29 #include <sys/time.h>
30
31 typedef struct TCPContext {
32     int fd;
33 } TCPContext;
34
35 /* return non zero if error */
36 static int tcp_open(URLContext *h, const char *uri, int flags)
37 {
38     struct addrinfo hints, *ai, *cur_ai;
39     int port, fd = -1;
40     TCPContext *s = NULL;
41     fd_set wfds, efds;
42     int fd_max, ret;
43     struct timeval tv;
44     socklen_t optlen;
45     char hostname[1024],proto[1024],path[1024];
46     char portstr[10];
47
48     av_url_split(proto, sizeof(proto), NULL, 0, hostname, sizeof(hostname),
49         &port, path, sizeof(path), uri);
50     if (strcmp(proto,"tcp") || port <= 0 || port >= 65536)
51         return AVERROR(EINVAL);
52
53     memset(&hints, 0, sizeof(hints));
54     hints.ai_family = AF_UNSPEC;
55     hints.ai_socktype = SOCK_STREAM;
56     snprintf(portstr, sizeof(portstr), "%d", port);
57     ret = getaddrinfo(hostname, portstr, &hints, &ai);
58     if (ret) {
59         av_log(NULL, AV_LOG_ERROR,
60                "Failed to resolve hostname %s: %s\n",
61                hostname, gai_strerror(ret));
62         return AVERROR(EIO);
63     }
64
65     cur_ai = ai;
66
67  restart:
68     fd = socket(cur_ai->ai_family, cur_ai->ai_socktype, cur_ai->ai_protocol);
69     if (fd < 0)
70         goto fail;
71     ff_socket_nonblock(fd, 1);
72
73  redo:
74     ret = connect(fd, cur_ai->ai_addr, cur_ai->ai_addrlen);
75     if (ret < 0) {
76         if (ff_neterrno() == FF_NETERROR(EINTR)) {
77             if (url_interrupt_cb())
78                 goto fail1;
79             goto redo;
80         }
81         if (ff_neterrno() != FF_NETERROR(EINPROGRESS) &&
82             ff_neterrno() != FF_NETERROR(EAGAIN))
83             goto fail;
84
85         /* wait until we are connected or until abort */
86         for(;;) {
87             if (url_interrupt_cb()) {
88                 ret = AVERROR(EINTR);
89                 goto fail1;
90             }
91             fd_max = fd;
92             FD_ZERO(&wfds);
93             FD_ZERO(&efds);
94             FD_SET(fd, &wfds);
95             FD_SET(fd, &efds);
96             tv.tv_sec = 0;
97             tv.tv_usec = 100 * 1000;
98             ret = select(fd_max + 1, NULL, &wfds, &efds, &tv);
99             if (ret > 0 && (FD_ISSET(fd, &wfds) || FD_ISSET(fd, &efds)))
100                 break;
101         }
102
103         /* test error */
104         optlen = sizeof(ret);
105         getsockopt (fd, SOL_SOCKET, SO_ERROR, &ret, &optlen);
106         if (ret != 0) {
107             av_log(NULL, AV_LOG_ERROR,
108                    "TCP connection to %s:%d failed: %s\n",
109                    hostname, port, strerror(ret));
110             goto fail;
111         }
112     }
113     s = av_malloc(sizeof(TCPContext));
114     if (!s) {
115         freeaddrinfo(ai);
116         return AVERROR(ENOMEM);
117     }
118     h->priv_data = s;
119     h->is_streamed = 1;
120     s->fd = fd;
121     freeaddrinfo(ai);
122     return 0;
123
124  fail:
125     if (cur_ai->ai_next) {
126         /* Retry with the next sockaddr */
127         cur_ai = cur_ai->ai_next;
128         if (fd >= 0)
129             closesocket(fd);
130         goto restart;
131     }
132     ret = AVERROR(EIO);
133  fail1:
134     if (fd >= 0)
135         closesocket(fd);
136     freeaddrinfo(ai);
137     return ret;
138 }
139
140 static int tcp_read(URLContext *h, uint8_t *buf, int size)
141 {
142     TCPContext *s = h->priv_data;
143     int len, fd_max, ret;
144     fd_set rfds;
145     struct timeval tv;
146
147     for (;;) {
148         if (url_interrupt_cb())
149             return AVERROR(EINTR);
150         fd_max = s->fd;
151         FD_ZERO(&rfds);
152         FD_SET(s->fd, &rfds);
153         tv.tv_sec = 0;
154         tv.tv_usec = 100 * 1000;
155         ret = select(fd_max + 1, &rfds, NULL, NULL, &tv);
156         if (ret > 0 && FD_ISSET(s->fd, &rfds)) {
157             len = recv(s->fd, buf, size, 0);
158             if (len < 0) {
159                 if (ff_neterrno() != FF_NETERROR(EINTR) &&
160                     ff_neterrno() != FF_NETERROR(EAGAIN))
161                     return ff_neterrno();
162             } else return len;
163         } else if (ret < 0) {
164             if (ff_neterrno() == FF_NETERROR(EINTR))
165                 continue;
166             return -1;
167         }
168     }
169 }
170
171 static int tcp_write(URLContext *h, const uint8_t *buf, int size)
172 {
173     TCPContext *s = h->priv_data;
174     int ret, size1, fd_max, len;
175     fd_set wfds;
176     struct timeval tv;
177
178     size1 = size;
179     while (size > 0) {
180         if (url_interrupt_cb())
181             return AVERROR(EINTR);
182         fd_max = s->fd;
183         FD_ZERO(&wfds);
184         FD_SET(s->fd, &wfds);
185         tv.tv_sec = 0;
186         tv.tv_usec = 100 * 1000;
187         ret = select(fd_max + 1, NULL, &wfds, NULL, &tv);
188         if (ret > 0 && FD_ISSET(s->fd, &wfds)) {
189             len = send(s->fd, buf, size, 0);
190             if (len < 0) {
191                 if (ff_neterrno() != FF_NETERROR(EINTR) &&
192                     ff_neterrno() != FF_NETERROR(EAGAIN))
193                     return ff_neterrno();
194                 continue;
195             }
196             size -= len;
197             buf += len;
198         } else if (ret < 0) {
199             if (ff_neterrno() == FF_NETERROR(EINTR))
200                 continue;
201             return -1;
202         }
203     }
204     return size1 - size;
205 }
206
207 static int tcp_close(URLContext *h)
208 {
209     TCPContext *s = h->priv_data;
210     closesocket(s->fd);
211     av_free(s);
212     return 0;
213 }
214
215 static int tcp_get_file_handle(URLContext *h)
216 {
217     TCPContext *s = h->priv_data;
218     return s->fd;
219 }
220
221 URLProtocol tcp_protocol = {
222     "tcp",
223     tcp_open,
224     tcp_read,
225     tcp_write,
226     NULL, /* seek */
227     tcp_close,
228     .url_get_file_handle = tcp_get_file_handle,
229 };