Deprecate resolve_host(). Patch by Martin Storsjö <$firstname()$firstname,st>.
[libav.git] / libavformat / tcp.c
CommitLineData
171bbb03
FB
1/*
2 * TCP protocol
406792e7 3 * Copyright (c) 2002 Fabrice Bellard
171bbb03 4 *
b78e7197
DB
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
171bbb03
FB
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
b78e7197 10 * version 2.1 of the License, or (at your option) any later version.
171bbb03 11 *
b78e7197 12 * FFmpeg is distributed in the hope that it will be useful,
171bbb03
FB
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
b78e7197 18 * License along with FFmpeg; if not, write to the Free Software
5509bffa 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
171bbb03
FB
20 */
21#include "avformat.h"
22#include <unistd.h>
42572ef5 23#include "network.h"
087b3272 24#include "os_support.h"
b250f9c6 25#if HAVE_SYS_SELECT_H
c75a0cce 26#include <sys/select.h>
6ad1c9c9 27#endif
09787fb8 28#include <sys/time.h>
171bbb03
FB
29
30typedef struct TCPContext {
31 int fd;
32} TCPContext;
33
171bbb03
FB
34/* return non zero if error */
35static int tcp_open(URLContext *h, const char *uri, int flags)
36{
37 struct sockaddr_in dest_addr;
171bbb03 38 int port, fd = -1;
88730be6 39 TCPContext *s = NULL;
09787fb8
FB
40 fd_set wfds;
41 int fd_max, ret;
42 struct timeval tv;
43 socklen_t optlen;
f23a9759 44 char hostname[1024],proto[1024],path[1024];
6ba5cbc6 45
e6c13819
RB
46 if(!ff_network_init())
47 return AVERROR(EIO);
48
6ba5cbc6 49 url_split(proto, sizeof(proto), NULL, 0, hostname, sizeof(hostname),
a3303add
RB
50 &port, path, sizeof(path), uri);
51 if (strcmp(proto,"tcp") || port <= 0 || port >= 65536)
8b9af28d 52 return AVERROR(EINVAL);
115329f1 53
171bbb03
FB
54 dest_addr.sin_family = AF_INET;
55 dest_addr.sin_port = htons(port);
56 if (resolve_host(&dest_addr.sin_addr, hostname) < 0)
8b9af28d 57 return AVERROR(EIO);
171bbb03 58
b5cccea0 59 fd = socket(AF_INET, SOCK_STREAM, 0);
171bbb03 60 if (fd < 0)
8b9af28d 61 return AVERROR(EIO);
ba472aaf 62 ff_socket_nonblock(fd, 1);
115329f1 63
09787fb8 64 redo:
115329f1 65 ret = connect(fd, (struct sockaddr *)&dest_addr,
09787fb8
FB
66 sizeof(dest_addr));
67 if (ret < 0) {
8da4034f 68 if (ff_neterrno() == FF_NETERROR(EINTR))
09787fb8 69 goto redo;
85060fe6
RP
70 if (ff_neterrno() != FF_NETERROR(EINPROGRESS) &&
71 ff_neterrno() != FF_NETERROR(EAGAIN))
09787fb8 72 goto fail;
171bbb03 73
09787fb8
FB
74 /* wait until we are connected or until abort */
75 for(;;) {
76 if (url_interrupt_cb()) {
8fa36ae0 77 ret = AVERROR(EINTR);
09787fb8
FB
78 goto fail1;
79 }
80 fd_max = fd;
81 FD_ZERO(&wfds);
82 FD_SET(fd, &wfds);
83 tv.tv_sec = 0;
84 tv.tv_usec = 100 * 1000;
85 ret = select(fd_max + 1, NULL, &wfds, NULL, &tv);
86 if (ret > 0 && FD_ISSET(fd, &wfds))
87 break;
88 }
115329f1 89
09787fb8
FB
90 /* test error */
91 optlen = sizeof(ret);
92 getsockopt (fd, SOL_SOCKET, SO_ERROR, &ret, &optlen);
93 if (ret != 0)
94 goto fail;
95 }
47f944a2
RB
96 s = av_malloc(sizeof(TCPContext));
97 if (!s)
98 return AVERROR(ENOMEM);
99 h->priv_data = s;
100 h->is_streamed = 1;
171bbb03
FB
101 s->fd = fd;
102 return 0;
103
104 fail:
6f3e0b21 105 ret = AVERROR(EIO);
09787fb8 106 fail1:
171bbb03 107 if (fd >= 0)
e9d511dc 108 closesocket(fd);
09787fb8 109 return ret;
171bbb03
FB
110}
111
0c1a9eda 112static int tcp_read(URLContext *h, uint8_t *buf, int size)
171bbb03
FB
113{
114 TCPContext *s = h->priv_data;
b51469a0 115 int len, fd_max, ret;
09787fb8
FB
116 fd_set rfds;
117 struct timeval tv;
171bbb03 118
9eef2b77 119 for (;;) {
09787fb8 120 if (url_interrupt_cb())
8fa36ae0 121 return AVERROR(EINTR);
09787fb8
FB
122 fd_max = s->fd;
123 FD_ZERO(&rfds);
124 FD_SET(s->fd, &rfds);
125 tv.tv_sec = 0;
126 tv.tv_usec = 100 * 1000;
b51469a0
LS
127 ret = select(fd_max + 1, &rfds, NULL, NULL, &tv);
128 if (ret > 0 && FD_ISSET(s->fd, &rfds)) {
b51469a0 129 len = recv(s->fd, buf, size, 0);
b51469a0 130 if (len < 0) {
8da4034f
AB
131 if (ff_neterrno() != FF_NETERROR(EINTR) &&
132 ff_neterrno() != FF_NETERROR(EAGAIN))
73c13268 133 return AVERROR(ff_neterrno());
b51469a0
LS
134 } else return len;
135 } else if (ret < 0) {
136 return -1;
137 }
171bbb03 138 }
171bbb03
FB
139}
140
0c1a9eda 141static int tcp_write(URLContext *h, uint8_t *buf, int size)
171bbb03
FB
142{
143 TCPContext *s = h->priv_data;
b51469a0 144 int ret, size1, fd_max, len;
09787fb8
FB
145 fd_set wfds;
146 struct timeval tv;
171bbb03
FB
147
148 size1 = size;
149 while (size > 0) {
09787fb8 150 if (url_interrupt_cb())
8fa36ae0 151 return AVERROR(EINTR);
09787fb8
FB
152 fd_max = s->fd;
153 FD_ZERO(&wfds);
154 FD_SET(s->fd, &wfds);
155 tv.tv_sec = 0;
156 tv.tv_usec = 100 * 1000;
b51469a0
LS
157 ret = select(fd_max + 1, NULL, &wfds, NULL, &tv);
158 if (ret > 0 && FD_ISSET(s->fd, &wfds)) {
b51469a0 159 len = send(s->fd, buf, size, 0);
b51469a0 160 if (len < 0) {
8da4034f
AB
161 if (ff_neterrno() != FF_NETERROR(EINTR) &&
162 ff_neterrno() != FF_NETERROR(EAGAIN))
73c13268 163 return AVERROR(ff_neterrno());
b51469a0 164 continue;
6c8e0d4d 165 }
b51469a0
LS
166 size -= len;
167 buf += len;
168 } else if (ret < 0) {
169 return -1;
6c8e0d4d 170 }
171bbb03
FB
171 }
172 return size1 - size;
173}
174
175static int tcp_close(URLContext *h)
176{
177 TCPContext *s = h->priv_data;
9ddd71fc 178 closesocket(s->fd);
1642cb6b 179 ff_network_close();
171bbb03
FB
180 av_free(s);
181 return 0;
182}
183
f0a80394
RB
184static int tcp_get_file_handle(URLContext *h)
185{
186 TCPContext *s = h->priv_data;
187 return s->fd;
188}
189
171bbb03
FB
190URLProtocol tcp_protocol = {
191 "tcp",
192 tcp_open,
193 tcp_read,
194 tcp_write,
195 NULL, /* seek */
196 tcp_close,
f0a80394 197 .url_get_file_handle = tcp_get_file_handle,
171bbb03 198};