3 * Copyright (c) 2002 Fabrice Bellard
5 * This file is part of FFmpeg.
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.
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.
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
24 #include "os_support.h"
26 #include <sys/select.h>
30 typedef struct TCPContext
{
34 /* return non zero if error */
35 static int tcp_open(URLContext
*h
, const char *uri
, int flags
)
37 struct sockaddr_in dest_addr
;
44 char hostname
[1024],proto
[1024],path
[1024];
46 if(!ff_network_init())
49 url_split(proto
, sizeof(proto
), NULL
, 0, hostname
, sizeof(hostname
),
50 &port
, path
, sizeof(path
), uri
);
51 if (strcmp(proto
,"tcp") || port
<= 0 || port
>= 65536)
52 return AVERROR(EINVAL
);
54 dest_addr
.sin_family
= AF_INET
;
55 dest_addr
.sin_port
= htons(port
);
56 if (resolve_host(&dest_addr
.sin_addr
, hostname
) < 0)
59 fd
= socket(AF_INET
, SOCK_STREAM
, 0);
62 ff_socket_nonblock(fd
, 1);
65 ret
= connect(fd
, (struct sockaddr
*)&dest_addr
,
68 if (ff_neterrno() == FF_NETERROR(EINTR
))
70 if (ff_neterrno() != FF_NETERROR(EINPROGRESS
) &&
71 ff_neterrno() != FF_NETERROR(EAGAIN
))
74 /* wait until we are connected or until abort */
76 if (url_interrupt_cb()) {
84 tv
.tv_usec
= 100 * 1000;
85 ret
= select(fd_max
+ 1, NULL
, &wfds
, NULL
, &tv
);
86 if (ret
> 0 && FD_ISSET(fd
, &wfds
))
92 getsockopt (fd
, SOL_SOCKET
, SO_ERROR
, &ret
, &optlen
);
96 s
= av_malloc(sizeof(TCPContext
));
98 return AVERROR(ENOMEM
);
112 static int tcp_read(URLContext
*h
, uint8_t *buf
, int size
)
114 TCPContext
*s
= h
->priv_data
;
115 int len
, fd_max
, ret
;
120 if (url_interrupt_cb())
121 return AVERROR(EINTR
);
124 FD_SET(s
->fd
, &rfds
);
126 tv
.tv_usec
= 100 * 1000;
127 ret
= select(fd_max
+ 1, &rfds
, NULL
, NULL
, &tv
);
128 if (ret
> 0 && FD_ISSET(s
->fd
, &rfds
)) {
129 len
= recv(s
->fd
, buf
, size
, 0);
131 if (ff_neterrno() != FF_NETERROR(EINTR
) &&
132 ff_neterrno() != FF_NETERROR(EAGAIN
))
133 return AVERROR(errno
);
135 } else if (ret
< 0) {
141 static int tcp_write(URLContext
*h
, uint8_t *buf
, int size
)
143 TCPContext
*s
= h
->priv_data
;
144 int ret
, size1
, fd_max
, len
;
150 if (url_interrupt_cb())
151 return AVERROR(EINTR
);
154 FD_SET(s
->fd
, &wfds
);
156 tv
.tv_usec
= 100 * 1000;
157 ret
= select(fd_max
+ 1, NULL
, &wfds
, NULL
, &tv
);
158 if (ret
> 0 && FD_ISSET(s
->fd
, &wfds
)) {
159 len
= send(s
->fd
, buf
, size
, 0);
161 if (ff_neterrno() != FF_NETERROR(EINTR
) &&
162 ff_neterrno() != FF_NETERROR(EAGAIN
))
163 return AVERROR(errno
);
168 } else if (ret
< 0) {
175 static int tcp_close(URLContext
*h
)
177 TCPContext
*s
= h
->priv_data
;
184 static int tcp_get_file_handle(URLContext
*h
)
186 TCPContext
*s
= h
->priv_data
;
190 URLProtocol tcp_protocol
= {
197 .url_get_file_handle
= tcp_get_file_handle
,