]> git.sesse.net Git - ffmpeg/blob - libavformat/tcp.c
libavformat: Handle error return from ff_listen_bind
[ffmpeg] / libavformat / tcp.c
1 /*
2  * TCP protocol
3  * Copyright (c) 2002 Fabrice Bellard
4  *
5  * This file is part of Libav.
6  *
7  * Libav 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  * Libav 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 Libav; 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 "libavutil/parseutils.h"
23 #include "libavutil/opt.h"
24
25 #include "internal.h"
26 #include "network.h"
27 #include "os_support.h"
28 #include "url.h"
29 #if HAVE_POLL_H
30 #include <poll.h>
31 #endif
32
33 typedef struct TCPContext {
34     const AVClass *class;
35     int fd;
36     int listen;
37     int timeout;
38     int listen_timeout;
39 } TCPContext;
40
41 #define OFFSET(x) offsetof(TCPContext, x)
42 #define D AV_OPT_FLAG_DECODING_PARAM
43 #define E AV_OPT_FLAG_ENCODING_PARAM
44 static const AVOption options[] = {
45     { "listen",          "Listen for incoming connections",  OFFSET(listen),         AV_OPT_TYPE_INT, { .i64 = 0 },     0,       1,       .flags = D|E },
46     { "timeout",         "Connection timeout (in milliseconds)", OFFSET(timeout),    AV_OPT_TYPE_INT, { .i64 = 10000 }, INT_MIN, INT_MAX, .flags = D|E },
47     { "listen_timeout",  "Bind timeout (in milliseconds)",   OFFSET(listen_timeout), AV_OPT_TYPE_INT, { .i64 = -1 },    INT_MIN, INT_MAX, .flags = D|E },
48     { NULL }
49 };
50
51 static const AVClass tcp_class = {
52     .class_name = "tcp",
53     .item_name  = av_default_item_name,
54     .option     = options,
55     .version    = LIBAVUTIL_VERSION_INT,
56 };
57
58 /* return non zero if error */
59 static int tcp_open(URLContext *h, const char *uri, int flags)
60 {
61     struct addrinfo hints = { 0 }, *ai, *cur_ai;
62     int port, fd = -1;
63     TCPContext *s = h->priv_data;
64     const char *p;
65     char buf[256];
66     int ret;
67     char hostname[1024],proto[1024],path[1024];
68     char portstr[10];
69
70     av_url_split(proto, sizeof(proto), NULL, 0, hostname, sizeof(hostname),
71         &port, path, sizeof(path), uri);
72     if (strcmp(proto, "tcp"))
73         return AVERROR(EINVAL);
74     if (port <= 0 || port >= 65536) {
75         av_log(h, AV_LOG_ERROR, "Port missing in uri\n");
76         return AVERROR(EINVAL);
77     }
78     p = strchr(uri, '?');
79     if (p) {
80         if (av_find_info_tag(buf, sizeof(buf), "listen", p))
81             s->listen = 1;
82         if (av_find_info_tag(buf, sizeof(buf), "timeout", p)) {
83             s->timeout = strtol(buf, NULL, 10) * 100;
84         }
85         if (av_find_info_tag(buf, sizeof(buf), "listen_timeout", p)) {
86             s->listen_timeout = strtol(buf, NULL, 10);
87         }
88     }
89     hints.ai_family = AF_UNSPEC;
90     hints.ai_socktype = SOCK_STREAM;
91     snprintf(portstr, sizeof(portstr), "%d", port);
92     if (s->listen)
93         hints.ai_flags |= AI_PASSIVE;
94     if (!hostname[0])
95         ret = getaddrinfo(NULL, portstr, &hints, &ai);
96     else
97         ret = getaddrinfo(hostname, portstr, &hints, &ai);
98     if (ret) {
99         av_log(h, AV_LOG_ERROR,
100                "Failed to resolve hostname %s: %s\n",
101                hostname, gai_strerror(ret));
102         return AVERROR(EIO);
103     }
104
105     cur_ai = ai;
106
107  restart:
108     fd = ff_socket(cur_ai->ai_family,
109                    cur_ai->ai_socktype,
110                    cur_ai->ai_protocol);
111     if (fd < 0) {
112         ret = ff_neterrno();
113         goto fail;
114     }
115
116     if (s->listen) {
117         if ((ret = ff_listen_bind(fd, cur_ai->ai_addr, cur_ai->ai_addrlen,
118                                   s->listen_timeout, h)) < 0) {
119             goto fail1;
120         }
121         fd = ret;
122     } else {
123         if ((ret = ff_listen_connect(fd, cur_ai->ai_addr, cur_ai->ai_addrlen,
124                                      s->timeout, h, !!cur_ai->ai_next)) < 0) {
125
126             if (ret == AVERROR_EXIT)
127                 goto fail1;
128             else
129                 goto fail;
130         }
131     }
132
133     h->is_streamed = 1;
134     s->fd = fd;
135     freeaddrinfo(ai);
136     return 0;
137
138  fail:
139     if (cur_ai->ai_next) {
140         /* Retry with the next sockaddr */
141         cur_ai = cur_ai->ai_next;
142         if (fd >= 0)
143             closesocket(fd);
144         ret = 0;
145         goto restart;
146     }
147  fail1:
148     if (fd >= 0)
149         closesocket(fd);
150     freeaddrinfo(ai);
151     return ret;
152 }
153
154 static int tcp_read(URLContext *h, uint8_t *buf, int size)
155 {
156     TCPContext *s = h->priv_data;
157     int ret;
158
159     if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
160         ret = ff_network_wait_fd(s->fd, 0);
161         if (ret < 0)
162             return ret;
163     }
164     ret = recv(s->fd, buf, size, 0);
165     return ret < 0 ? ff_neterrno() : ret;
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;
172
173     if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
174         ret = ff_network_wait_fd(s->fd, 1);
175         if (ret < 0)
176             return ret;
177     }
178     ret = send(s->fd, buf, size, MSG_NOSIGNAL);
179     return ret < 0 ? ff_neterrno() : ret;
180 }
181
182 static int tcp_shutdown(URLContext *h, int flags)
183 {
184     TCPContext *s = h->priv_data;
185     int how;
186
187     if (flags & AVIO_FLAG_WRITE && flags & AVIO_FLAG_READ) {
188         how = SHUT_RDWR;
189     } else if (flags & AVIO_FLAG_WRITE) {
190         how = SHUT_WR;
191     } else {
192         how = SHUT_RD;
193     }
194
195     return shutdown(s->fd, how);
196 }
197
198 static int tcp_close(URLContext *h)
199 {
200     TCPContext *s = h->priv_data;
201     closesocket(s->fd);
202     return 0;
203 }
204
205 static int tcp_get_file_handle(URLContext *h)
206 {
207     TCPContext *s = h->priv_data;
208     return s->fd;
209 }
210
211 URLProtocol ff_tcp_protocol = {
212     .name                = "tcp",
213     .url_open            = tcp_open,
214     .url_read            = tcp_read,
215     .url_write           = tcp_write,
216     .url_close           = tcp_close,
217     .url_get_file_handle = tcp_get_file_handle,
218     .url_shutdown        = tcp_shutdown,
219     .priv_data_size      = sizeof(TCPContext),
220     .flags               = URL_PROTOCOL_FLAG_NETWORK,
221     .priv_data_class     = &tcp_class,
222 };