Fix memleak on some OSes in case network initialization fails. See
[libav.git] / libavformat / tcp.c
CommitLineData
171bbb03
FB
1/*
2 * TCP protocol
3 * Copyright (c) 2002 Fabrice Bellard.
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"
09787fb8 25#include <sys/time.h>
171bbb03
FB
26
27typedef struct TCPContext {
28 int fd;
29} TCPContext;
30
171bbb03
FB
31/* return non zero if error */
32static int tcp_open(URLContext *h, const char *uri, int flags)
33{
34 struct sockaddr_in dest_addr;
35 char hostname[1024], *q;
36 int port, fd = -1;
88730be6 37 TCPContext *s = NULL;
09787fb8
FB
38 fd_set wfds;
39 int fd_max, ret;
40 struct timeval tv;
41 socklen_t optlen;
ad33bfef 42 char proto[1024],path[1024],tmp[1024];
6ba5cbc6 43
e6c13819
RB
44 if(!ff_network_init())
45 return AVERROR(EIO);
46
6ba5cbc6 47 url_split(proto, sizeof(proto), NULL, 0, hostname, sizeof(hostname),
ad33bfef
RB
48 &port, path, sizeof(path), uri);
49 if (strcmp(proto,"tcp")) goto fail;
50 if ((q = strchr(hostname,'@'))) { strcpy(tmp,q+1); strcpy(hostname,tmp); }
115329f1 51
171bbb03
FB
52 s = av_malloc(sizeof(TCPContext));
53 if (!s)
8fa36ae0 54 return AVERROR(ENOMEM);
171bbb03 55 h->priv_data = s;
d78d72da 56 h->is_streamed = 1;
115329f1 57
171bbb03
FB
58 if (port <= 0 || port >= 65536)
59 goto fail;
115329f1 60
171bbb03
FB
61 dest_addr.sin_family = AF_INET;
62 dest_addr.sin_port = htons(port);
63 if (resolve_host(&dest_addr.sin_addr, hostname) < 0)
64 goto fail;
65
b5cccea0 66 fd = socket(AF_INET, SOCK_STREAM, 0);
171bbb03
FB
67 if (fd < 0)
68 goto fail;
ba472aaf 69 ff_socket_nonblock(fd, 1);
115329f1 70
09787fb8 71 redo:
115329f1 72 ret = connect(fd, (struct sockaddr *)&dest_addr,
09787fb8
FB
73 sizeof(dest_addr));
74 if (ret < 0) {
8da4034f 75 if (ff_neterrno() == FF_NETERROR(EINTR))
09787fb8 76 goto redo;
85060fe6
RP
77 if (ff_neterrno() != FF_NETERROR(EINPROGRESS) &&
78 ff_neterrno() != FF_NETERROR(EAGAIN))
09787fb8 79 goto fail;
171bbb03 80
09787fb8
FB
81 /* wait until we are connected or until abort */
82 for(;;) {
83 if (url_interrupt_cb()) {
8fa36ae0 84 ret = AVERROR(EINTR);
09787fb8
FB
85 goto fail1;
86 }
87 fd_max = fd;
88 FD_ZERO(&wfds);
89 FD_SET(fd, &wfds);
90 tv.tv_sec = 0;
91 tv.tv_usec = 100 * 1000;
92 ret = select(fd_max + 1, NULL, &wfds, NULL, &tv);
93 if (ret > 0 && FD_ISSET(fd, &wfds))
94 break;
95 }
115329f1 96
09787fb8
FB
97 /* test error */
98 optlen = sizeof(ret);
99 getsockopt (fd, SOL_SOCKET, SO_ERROR, &ret, &optlen);
100 if (ret != 0)
101 goto fail;
102 }
171bbb03
FB
103 s->fd = fd;
104 return 0;
105
106 fail:
6f3e0b21 107 ret = AVERROR(EIO);
09787fb8 108 fail1:
171bbb03 109 if (fd >= 0)
e9d511dc 110 closesocket(fd);
171bbb03 111 av_free(s);
09787fb8 112 return ret;
171bbb03
FB
113}
114
0c1a9eda 115static int tcp_read(URLContext *h, uint8_t *buf, int size)
171bbb03
FB
116{
117 TCPContext *s = h->priv_data;
b51469a0 118 int len, fd_max, ret;
09787fb8
FB
119 fd_set rfds;
120 struct timeval tv;
171bbb03 121
9eef2b77 122 for (;;) {
09787fb8 123 if (url_interrupt_cb())
8fa36ae0 124 return AVERROR(EINTR);
09787fb8
FB
125 fd_max = s->fd;
126 FD_ZERO(&rfds);
127 FD_SET(s->fd, &rfds);
128 tv.tv_sec = 0;
129 tv.tv_usec = 100 * 1000;
b51469a0
LS
130 ret = select(fd_max + 1, &rfds, NULL, NULL, &tv);
131 if (ret > 0 && FD_ISSET(s->fd, &rfds)) {
b51469a0 132 len = recv(s->fd, buf, size, 0);
b51469a0 133 if (len < 0) {
8da4034f
AB
134 if (ff_neterrno() != FF_NETERROR(EINTR) &&
135 ff_neterrno() != FF_NETERROR(EAGAIN))
8fa36ae0 136 return AVERROR(errno);
b51469a0
LS
137 } else return len;
138 } else if (ret < 0) {
139 return -1;
140 }
171bbb03 141 }
171bbb03
FB
142}
143
0c1a9eda 144static int tcp_write(URLContext *h, uint8_t *buf, int size)
171bbb03
FB
145{
146 TCPContext *s = h->priv_data;
b51469a0 147 int ret, size1, fd_max, len;
09787fb8
FB
148 fd_set wfds;
149 struct timeval tv;
171bbb03
FB
150
151 size1 = size;
152 while (size > 0) {
09787fb8 153 if (url_interrupt_cb())
8fa36ae0 154 return AVERROR(EINTR);
09787fb8
FB
155 fd_max = s->fd;
156 FD_ZERO(&wfds);
157 FD_SET(s->fd, &wfds);
158 tv.tv_sec = 0;
159 tv.tv_usec = 100 * 1000;
b51469a0
LS
160 ret = select(fd_max + 1, NULL, &wfds, NULL, &tv);
161 if (ret > 0 && FD_ISSET(s->fd, &wfds)) {
b51469a0 162 len = send(s->fd, buf, size, 0);
b51469a0 163 if (len < 0) {
8da4034f
AB
164 if (ff_neterrno() != FF_NETERROR(EINTR) &&
165 ff_neterrno() != FF_NETERROR(EAGAIN))
8fa36ae0 166 return AVERROR(errno);
b51469a0 167 continue;
6c8e0d4d 168 }
b51469a0
LS
169 size -= len;
170 buf += len;
171 } else if (ret < 0) {
172 return -1;
6c8e0d4d 173 }
171bbb03
FB
174 }
175 return size1 - size;
176}
177
178static int tcp_close(URLContext *h)
179{
180 TCPContext *s = h->priv_data;
9ddd71fc 181 closesocket(s->fd);
1642cb6b 182 ff_network_close();
171bbb03
FB
183 av_free(s);
184 return 0;
185}
186
187URLProtocol tcp_protocol = {
188 "tcp",
189 tcp_open,
190 tcp_read,
191 tcp_write,
192 NULL, /* seek */
193 tcp_close,
194};