tcp: Pass NULL as hostname to getaddrinfo if the string is empty
[libav.git] / 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 "internal.h"
24 #include "network.h"
25 #include "os_support.h"
26 #include "url.h"
27 #if HAVE_POLL_H
28 #include <poll.h>
29 #endif
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 = { 0 }, *ai, *cur_ai;
39 int port, fd = -1;
40 TCPContext *s = h->priv_data;
41 int listen_socket = 0;
42 const char *p;
43 char buf[256];
44 int ret;
45 socklen_t optlen;
46 int timeout = 100;
47 char hostname[1024],proto[1024],path[1024];
48 char portstr[10];
49
50 av_url_split(proto, sizeof(proto), NULL, 0, hostname, sizeof(hostname),
51 &port, path, sizeof(path), uri);
52 if (strcmp(proto,"tcp") || port <= 0 || port >= 65536)
53 return AVERROR(EINVAL);
54
55 p = strchr(uri, '?');
56 if (p) {
57 if (av_find_info_tag(buf, sizeof(buf), "listen", p))
58 listen_socket = 1;
59 if (av_find_info_tag(buf, sizeof(buf), "timeout", p)) {
60 timeout = strtol(buf, NULL, 10);
61 }
62 }
63 hints.ai_family = AF_UNSPEC;
64 hints.ai_socktype = SOCK_STREAM;
65 snprintf(portstr, sizeof(portstr), "%d", port);
66 if (listen_socket)
67 hints.ai_flags |= AI_PASSIVE;
68 if (!hostname[0])
69 ret = getaddrinfo(NULL, portstr, &hints, &ai);
70 else
71 ret = getaddrinfo(hostname, portstr, &hints, &ai);
72 if (ret) {
73 av_log(h, AV_LOG_ERROR,
74 "Failed to resolve hostname %s: %s\n",
75 hostname, gai_strerror(ret));
76 return AVERROR(EIO);
77 }
78
79 cur_ai = ai;
80
81 restart:
82 ret = AVERROR(EIO);
83 fd = socket(cur_ai->ai_family, cur_ai->ai_socktype, cur_ai->ai_protocol);
84 if (fd < 0)
85 goto fail;
86
87 if (listen_socket) {
88 int fd1;
89 int reuse = 1;
90 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse));
91 ret = bind(fd, cur_ai->ai_addr, cur_ai->ai_addrlen);
92 if (ret) {
93 ret = ff_neterrno();
94 goto fail1;
95 }
96 ret = listen(fd, 1);
97 if (ret) {
98 ret = ff_neterrno();
99 goto fail1;
100 }
101 fd1 = accept(fd, NULL, NULL);
102 if (fd1 < 0) {
103 ret = ff_neterrno();
104 goto fail1;
105 }
106 closesocket(fd);
107 fd = fd1;
108 ff_socket_nonblock(fd, 1);
109 } else {
110 redo:
111 ff_socket_nonblock(fd, 1);
112 ret = connect(fd, cur_ai->ai_addr, cur_ai->ai_addrlen);
113 }
114
115 if (ret < 0) {
116 struct pollfd p = {fd, POLLOUT, 0};
117 ret = ff_neterrno();
118 if (ret == AVERROR(EINTR)) {
119 if (ff_check_interrupt(&h->interrupt_callback)) {
120 ret = AVERROR_EXIT;
121 goto fail1;
122 }
123 goto redo;
124 }
125 if (ret != AVERROR(EINPROGRESS) &&
126 ret != AVERROR(EAGAIN))
127 goto fail;
128
129 /* wait until we are connected or until abort */
130 while(timeout--) {
131 if (ff_check_interrupt(&h->interrupt_callback)) {
132 ret = AVERROR_EXIT;
133 goto fail1;
134 }
135 ret = poll(&p, 1, 100);
136 if (ret > 0)
137 break;
138 }
139 if (ret <= 0) {
140 ret = AVERROR(ETIMEDOUT);
141 goto fail;
142 }
143 /* test error */
144 optlen = sizeof(ret);
145 if (getsockopt (fd, SOL_SOCKET, SO_ERROR, &ret, &optlen))
146 ret = AVUNERROR(ff_neterrno());
147 if (ret != 0) {
148 char errbuf[100];
149 ret = AVERROR(ret);
150 av_strerror(ret, errbuf, sizeof(errbuf));
151 av_log(h, AV_LOG_ERROR,
152 "TCP connection to %s:%d failed: %s\n",
153 hostname, port, errbuf);
154 goto fail;
155 }
156 }
157 h->is_streamed = 1;
158 s->fd = fd;
159 freeaddrinfo(ai);
160 return 0;
161
162 fail:
163 if (cur_ai->ai_next) {
164 /* Retry with the next sockaddr */
165 cur_ai = cur_ai->ai_next;
166 if (fd >= 0)
167 closesocket(fd);
168 goto restart;
169 }
170 fail1:
171 if (fd >= 0)
172 closesocket(fd);
173 freeaddrinfo(ai);
174 return ret;
175 }
176
177 static int tcp_read(URLContext *h, uint8_t *buf, int size)
178 {
179 TCPContext *s = h->priv_data;
180 int ret;
181
182 if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
183 ret = ff_network_wait_fd(s->fd, 0);
184 if (ret < 0)
185 return ret;
186 }
187 ret = recv(s->fd, buf, size, 0);
188 return ret < 0 ? ff_neterrno() : ret;
189 }
190
191 static int tcp_write(URLContext *h, const uint8_t *buf, int size)
192 {
193 TCPContext *s = h->priv_data;
194 int ret;
195
196 if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
197 ret = ff_network_wait_fd(s->fd, 1);
198 if (ret < 0)
199 return ret;
200 }
201 ret = send(s->fd, buf, size, 0);
202 return ret < 0 ? ff_neterrno() : ret;
203 }
204
205 static int tcp_shutdown(URLContext *h, int flags)
206 {
207 TCPContext *s = h->priv_data;
208 int how;
209
210 if (flags & AVIO_FLAG_WRITE && flags & AVIO_FLAG_READ) {
211 how = SHUT_RDWR;
212 } else if (flags & AVIO_FLAG_WRITE) {
213 how = SHUT_WR;
214 } else {
215 how = SHUT_RD;
216 }
217
218 return shutdown(s->fd, how);
219 }
220
221 static int tcp_close(URLContext *h)
222 {
223 TCPContext *s = h->priv_data;
224 closesocket(s->fd);
225 return 0;
226 }
227
228 static int tcp_get_file_handle(URLContext *h)
229 {
230 TCPContext *s = h->priv_data;
231 return s->fd;
232 }
233
234 URLProtocol ff_tcp_protocol = {
235 .name = "tcp",
236 .url_open = tcp_open,
237 .url_read = tcp_read,
238 .url_write = tcp_write,
239 .url_close = tcp_close,
240 .url_get_file_handle = tcp_get_file_handle,
241 .url_shutdown = tcp_shutdown,
242 .priv_data_size = sizeof(TCPContext),
243 .flags = URL_PROTOCOL_FLAG_NETWORK,
244 };