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
22 #include <sys/types.h>
23 #include <sys/socket.h>
24 #include <netinet/in.h>
26 # include <arpa/inet.h>
28 # include "barpainet.h"
32 typedef struct TCPContext {
36 /* resolve host with also IP address parsing */
37 int resolve_host(struct in_addr *sin_addr, const char *hostname)
41 if ((inet_aton(hostname, sin_addr)) == 0) {
42 hp = gethostbyname(hostname);
45 memcpy (sin_addr, hp->h_addr, sizeof(struct in_addr));
50 /* return non zero if error */
51 static int tcp_open(URLContext *h, const char *uri, int flags)
53 struct sockaddr_in dest_addr;
54 char hostname[1024], *q;
59 s = av_malloc(sizeof(TCPContext));
64 if (!strstart(p, "tcp://", &p))
67 while (*p != ':' && *p != '/' && *p != '\0') {
68 if ((q - hostname) < sizeof(hostname) - 1)
76 port = strtoul(p, (char **)&p, 10);
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);
89 if (connect(fd, (struct sockaddr *)&dest_addr,
90 sizeof(dest_addr)) < 0)
103 static int tcp_read(URLContext *h, uint8_t *buf, int size)
105 TCPContext *s = h->priv_data;
110 #ifdef CONFIG_BEOS_NETSERVER
111 len = recv (s->fd, buf, size, 0);
113 len = read (s->fd, buf, size);
116 if (errno != EINTR && errno != EAGAIN)
124 } else if (len == 0) {
133 static int tcp_write(URLContext *h, uint8_t *buf, int size)
135 TCPContext *s = h->priv_data;
140 #ifdef CONFIG_BEOS_NETSERVER
141 ret = send (s->fd, buf, size, 0);
143 ret = write (s->fd, buf, size);
145 if (ret < 0 && errno != EINTR && errno != EAGAIN)
157 static int tcp_close(URLContext *h)
159 TCPContext *s = h->priv_data;
160 #ifdef CONFIG_BEOS_NETSERVER
169 URLProtocol tcp_protocol = {