3 * Copyright (c) 2002 Fabrice Bellard.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include <sys/types.h>
22 #include <sys/socket.h>
23 #include <netinet/in.h>
24 #if defined(__APPLE__) || defined(__BEOS__)
25 typedef int socklen_t
;
28 # include <arpa/inet.h>
30 # include "barpainet.h"
36 typedef struct TCPContext
{
40 /* resolve host with also IP address parsing */
41 int resolve_host(struct in_addr
*sin_addr
, const char *hostname
)
45 if ((inet_aton(hostname
, sin_addr
)) == 0) {
46 hp
= gethostbyname(hostname
);
49 memcpy (sin_addr
, hp
->h_addr
, sizeof(struct in_addr
));
54 /* return non zero if error */
55 static int tcp_open(URLContext
*h
, const char *uri
, int flags
)
57 struct sockaddr_in dest_addr
;
58 char hostname
[1024], *q
;
67 s
= av_malloc(sizeof(TCPContext
));
72 if (!strstart(p
, "tcp://", &p
))
75 while (*p
!= ':' && *p
!= '/' && *p
!= '\0') {
76 if ((q
- hostname
) < sizeof(hostname
) - 1)
84 port
= strtoul(p
, (char **)&p
, 10);
85 if (port
<= 0 || port
>= 65536)
88 dest_addr
.sin_family
= AF_INET
;
89 dest_addr
.sin_port
= htons(port
);
90 if (resolve_host(&dest_addr
.sin_addr
, hostname
) < 0)
93 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
96 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
99 ret
= connect(fd
, (struct sockaddr
*)&dest_addr
,
104 if (errno
!= EINPROGRESS
)
107 /* wait until we are connected or until abort */
109 if (url_interrupt_cb()) {
117 tv
.tv_usec
= 100 * 1000;
118 ret
= select(fd_max
+ 1, NULL
, &wfds
, NULL
, &tv
);
119 if (ret
> 0 && FD_ISSET(fd
, &wfds
))
124 optlen
= sizeof(ret
);
125 getsockopt (fd
, SOL_SOCKET
, SO_ERROR
, &ret
, &optlen
);
141 static int tcp_read(URLContext
*h
, uint8_t *buf
, int size
)
143 TCPContext
*s
= h
->priv_data
;
149 if (url_interrupt_cb())
153 FD_SET(s
->fd
, &rfds
);
155 tv
.tv_usec
= 100 * 1000;
156 select(fd_max
+ 1, &rfds
, NULL
, NULL
, &tv
);
158 len
= recv(s
->fd
, buf
, size
, 0);
160 len
= read(s
->fd
, buf
, size
);
163 if (errno
!= EINTR
&& errno
!= EAGAIN
)
174 static int tcp_write(URLContext
*h
, uint8_t *buf
, int size
)
176 TCPContext
*s
= h
->priv_data
;
177 int ret
, size1
, fd_max
;
183 if (url_interrupt_cb())
187 FD_SET(s
->fd
, &wfds
);
189 tv
.tv_usec
= 100 * 1000;
190 select(fd_max
+ 1, NULL
, &wfds
, NULL
, &tv
);
192 ret
= send(s
->fd
, buf
, size
, 0);
194 ret
= write(s
->fd
, buf
, size
);
197 if (errno
!= EINTR
&& errno
!= EAGAIN
) {
212 static int tcp_close(URLContext
*h
)
214 TCPContext
*s
= h
->priv_data
;
215 #ifdef CONFIG_BEOS_NETSERVER
224 URLProtocol tcp_protocol
= {