]> git.sesse.net Git - ffmpeg/blob - libavformat/tcp.c
udp: Add an option for connecting the udp socket
[ffmpeg] / 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             goto redo;
78         if (ff_neterrno() != FF_NETERROR(EINPROGRESS) &&
79             ff_neterrno() != FF_NETERROR(EAGAIN))
80             goto fail;
81
82         /* wait until we are connected or until abort */
83         for(;;) {
84             if (url_interrupt_cb()) {
85                 ret = AVERROR(EINTR);
86                 goto fail1;
87             }
88             fd_max = fd;
89             FD_ZERO(&wfds);
90             FD_ZERO(&efds);
91             FD_SET(fd, &wfds);
92             FD_SET(fd, &efds);
93             tv.tv_sec = 0;
94             tv.tv_usec = 100 * 1000;
95             ret = select(fd_max + 1, NULL, &wfds, &efds, &tv);
96             if (ret > 0 && (FD_ISSET(fd, &wfds) || FD_ISSET(fd, &efds)))
97                 break;
98         }
99
100         /* test error */
101         optlen = sizeof(ret);
102         getsockopt (fd, SOL_SOCKET, SO_ERROR, &ret, &optlen);
103         if (ret != 0) {
104             av_log(NULL, AV_LOG_ERROR,
105                    "TCP connection to %s:%d failed: %s\n",
106                    hostname, port, strerror(ret));
107             goto fail;
108         }
109     }
110     s = av_malloc(sizeof(TCPContext));
111     if (!s) {
112         freeaddrinfo(ai);
113         return AVERROR(ENOMEM);
114     }
115     h->priv_data = s;
116     h->is_streamed = 1;
117     s->fd = fd;
118     freeaddrinfo(ai);
119     return 0;
120
121  fail:
122     if (cur_ai->ai_next) {
123         /* Retry with the next sockaddr */
124         cur_ai = cur_ai->ai_next;
125         if (fd >= 0)
126             closesocket(fd);
127         goto restart;
128     }
129     ret = AVERROR(EIO);
130  fail1:
131     if (fd >= 0)
132         closesocket(fd);
133     freeaddrinfo(ai);
134     return ret;
135 }
136
137 static int tcp_read(URLContext *h, uint8_t *buf, int size)
138 {
139     TCPContext *s = h->priv_data;
140     int len, fd_max, ret;
141     fd_set rfds;
142     struct timeval tv;
143
144     for (;;) {
145         if (url_interrupt_cb())
146             return AVERROR(EINTR);
147         fd_max = s->fd;
148         FD_ZERO(&rfds);
149         FD_SET(s->fd, &rfds);
150         tv.tv_sec = 0;
151         tv.tv_usec = 100 * 1000;
152         ret = select(fd_max + 1, &rfds, NULL, NULL, &tv);
153         if (ret > 0 && FD_ISSET(s->fd, &rfds)) {
154             len = recv(s->fd, buf, size, 0);
155             if (len < 0) {
156                 if (ff_neterrno() != FF_NETERROR(EINTR) &&
157                     ff_neterrno() != FF_NETERROR(EAGAIN))
158                     return AVERROR(ff_neterrno());
159             } else return len;
160         } else if (ret < 0) {
161             if (ff_neterrno() == FF_NETERROR(EINTR))
162                 continue;
163             return -1;
164         }
165     }
166 }
167
168 static int tcp_write(URLContext *h, const uint8_t *buf, int size)
169 {
170     TCPContext *s = h->priv_data;
171     int ret, size1, fd_max, len;
172     fd_set wfds;
173     struct timeval tv;
174
175     size1 = size;
176     while (size > 0) {
177         if (url_interrupt_cb())
178             return AVERROR(EINTR);
179         fd_max = s->fd;
180         FD_ZERO(&wfds);
181         FD_SET(s->fd, &wfds);
182         tv.tv_sec = 0;
183         tv.tv_usec = 100 * 1000;
184         ret = select(fd_max + 1, NULL, &wfds, NULL, &tv);
185         if (ret > 0 && FD_ISSET(s->fd, &wfds)) {
186             len = send(s->fd, buf, size, 0);
187             if (len < 0) {
188                 if (ff_neterrno() != FF_NETERROR(EINTR) &&
189                     ff_neterrno() != FF_NETERROR(EAGAIN))
190                     return AVERROR(ff_neterrno());
191                 continue;
192             }
193             size -= len;
194             buf += len;
195         } else if (ret < 0) {
196             if (ff_neterrno() == FF_NETERROR(EINTR))
197                 continue;
198             return -1;
199         }
200     }
201     return size1 - size;
202 }
203
204 static int tcp_close(URLContext *h)
205 {
206     TCPContext *s = h->priv_data;
207     closesocket(s->fd);
208     av_free(s);
209     return 0;
210 }
211
212 static int tcp_get_file_handle(URLContext *h)
213 {
214     TCPContext *s = h->priv_data;
215     return s->fd;
216 }
217
218 URLProtocol tcp_protocol = {
219     "tcp",
220     tcp_open,
221     tcp_read,
222     tcp_write,
223     NULL, /* seek */
224     tcp_close,
225     .url_get_file_handle = tcp_get_file_handle,
226 };