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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #include <sys/types.h>
22 #include <sys/socket.h>
23 #include <netinet/in.h>
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;
65 char proto[1024],path[1024],tmp[1024]; // PETR: protocol and path strings
67 url_split(proto, sizeof(proto), NULL, 0, hostname, sizeof(hostname),
68 &port, path, sizeof(path), uri); // PETR: use url_split
69 if (strcmp(proto,"tcp")) goto fail; // PETR: check protocol
70 if ((q = strchr(hostname,'@'))) { strcpy(tmp,q+1); strcpy(hostname,tmp); } // PETR: take only the part after '@' for tcp protocol
72 s = av_malloc(sizeof(TCPContext));
77 if (port <= 0 || port >= 65536)
80 dest_addr.sin_family = AF_INET;
81 dest_addr.sin_port = htons(port);
82 if (resolve_host(&dest_addr.sin_addr, hostname) < 0)
85 fd = socket(PF_INET, SOCK_STREAM, 0);
88 fcntl(fd, F_SETFL, O_NONBLOCK);
91 ret = connect(fd, (struct sockaddr *)&dest_addr,
96 if (errno != EINPROGRESS)
99 /* wait until we are connected or until abort */
101 if (url_interrupt_cb()) {
109 tv.tv_usec = 100 * 1000;
110 ret = select(fd_max + 1, NULL, &wfds, NULL, &tv);
111 if (ret > 0 && FD_ISSET(fd, &wfds))
116 optlen = sizeof(ret);
117 getsockopt (fd, SOL_SOCKET, SO_ERROR, &ret, &optlen);
133 static int tcp_read(URLContext *h, uint8_t *buf, int size)
135 TCPContext *s = h->priv_data;
136 int len, fd_max, ret;
141 if (url_interrupt_cb())
145 FD_SET(s->fd, &rfds);
147 tv.tv_usec = 100 * 1000;
148 ret = select(fd_max + 1, &rfds, NULL, NULL, &tv);
149 if (ret > 0 && FD_ISSET(s->fd, &rfds)) {
151 len = recv(s->fd, buf, size, 0);
153 len = read(s->fd, buf, size);
156 if (errno != EINTR && errno != EAGAIN)
163 } else if (ret < 0) {
169 static int tcp_write(URLContext *h, uint8_t *buf, int size)
171 TCPContext *s = h->priv_data;
172 int ret, size1, fd_max, len;
178 if (url_interrupt_cb())
182 FD_SET(s->fd, &wfds);
184 tv.tv_usec = 100 * 1000;
185 ret = select(fd_max + 1, NULL, &wfds, NULL, &tv);
186 if (ret > 0 && FD_ISSET(s->fd, &wfds)) {
188 len = send(s->fd, buf, size, 0);
190 len = write(s->fd, buf, size);
193 if (errno != EINTR && errno != EAGAIN) {
204 } else if (ret < 0) {
211 static int tcp_close(URLContext *h)
213 TCPContext *s = h->priv_data;
214 #ifdef CONFIG_BEOS_NETSERVER
223 URLProtocol tcp_protocol = {