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;
66 char proto[1024],path[1024],tmp[1024]; // PETR: protocol and path strings
68 url_split(proto, sizeof(proto), NULL, 0, hostname, sizeof(hostname),
69 &port, path, sizeof(path), uri); // PETR: use url_split
70 if (strcmp(proto,"tcp")) goto fail; // PETR: check protocol
71 if ((q = strchr(hostname,'@'))) { strcpy(tmp,q+1); strcpy(hostname,tmp); } // PETR: take only the part after '@' for tcp protocol
73 s = av_malloc(sizeof(TCPContext));
78 if (port <= 0 || port >= 65536)
81 dest_addr.sin_family = AF_INET;
82 dest_addr.sin_port = htons(port);
83 if (resolve_host(&dest_addr.sin_addr, hostname) < 0)
86 fd = socket(PF_INET, SOCK_STREAM, 0);
89 fcntl(fd, F_SETFL, O_NONBLOCK);
92 ret = connect(fd, (struct sockaddr *)&dest_addr,
97 if (errno != EINPROGRESS)
100 /* wait until we are connected or until abort */
102 if (url_interrupt_cb()) {
110 tv.tv_usec = 100 * 1000;
111 ret = select(fd_max + 1, NULL, &wfds, NULL, &tv);
112 if (ret > 0 && FD_ISSET(fd, &wfds))
117 optlen = sizeof(ret);
118 getsockopt (fd, SOL_SOCKET, SO_ERROR, &ret, &optlen);
134 static int tcp_read(URLContext *h, uint8_t *buf, int size)
136 TCPContext *s = h->priv_data;
137 int len, fd_max, ret;
142 if (url_interrupt_cb())
146 FD_SET(s->fd, &rfds);
148 tv.tv_usec = 100 * 1000;
149 ret = select(fd_max + 1, &rfds, NULL, NULL, &tv);
150 if (ret > 0 && FD_ISSET(s->fd, &rfds)) {
152 len = recv(s->fd, buf, size, 0);
154 len = read(s->fd, buf, size);
157 if (errno != EINTR && errno != EAGAIN)
164 } else if (ret < 0) {
170 static int tcp_write(URLContext *h, uint8_t *buf, int size)
172 TCPContext *s = h->priv_data;
173 int ret, size1, fd_max, len;
179 if (url_interrupt_cb())
183 FD_SET(s->fd, &wfds);
185 tv.tv_usec = 100 * 1000;
186 ret = select(fd_max + 1, NULL, &wfds, NULL, &tv);
187 if (ret > 0 && FD_ISSET(s->fd, &wfds)) {
189 len = send(s->fd, buf, size, 0);
191 len = write(s->fd, buf, size);
194 if (errno != EINTR && errno != EAGAIN) {
205 } else if (ret < 0) {
212 static int tcp_close(URLContext *h)
214 TCPContext *s = h->priv_data;
215 #ifdef CONFIG_BEOS_NETSERVER
224 URLProtocol tcp_protocol = {