added raw tcp support
[libav.git] / libav / tcp.c
CommitLineData
171bbb03
FB
1/*
2 * TCP protocol
3 * Copyright (c) 2002 Fabrice Bellard.
4 *
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.
9 *
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.
14 *
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
18 */
19#include "avformat.h"
20#include <unistd.h>
21#include <ctype.h>
22#include <sys/types.h>
23#include <sys/socket.h>
24#include <netinet/in.h>
25#include <arpa/inet.h>
26#include <netdb.h>
27
28typedef struct TCPContext {
29 int fd;
30} TCPContext;
31
32/* resolve host with also IP address parsing */
33int resolve_host(struct in_addr *sin_addr, const char *hostname)
34{
35 struct hostent *hp;
36
37 if ((inet_aton(hostname, sin_addr)) == 0) {
38 hp = gethostbyname(hostname);
39 if (!hp)
40 return -1;
41 memcpy (sin_addr, hp->h_addr, sizeof(struct in_addr));
42 }
43 return 0;
44}
45
46/* return non zero if error */
47static int tcp_open(URLContext *h, const char *uri, int flags)
48{
49 struct sockaddr_in dest_addr;
50 char hostname[1024], *q;
51 int port, fd = -1;
52 TCPContext *s;
53 const char *p;
54
55 s = av_malloc(sizeof(TCPContext));
56 if (!s)
57 return -ENOMEM;
58 h->priv_data = s;
59 p = uri;
60 if (!strstart(p, "tcp://", &p))
61 goto fail;
62 q = hostname;
63 while (*p != ':' && *p != '/' && *p != '\0') {
64 if ((q - hostname) < sizeof(hostname) - 1)
65 *q++ = *p;
66 p++;
67 }
68 *q = '\0';
69 if (*p != ':')
70 goto fail;
71 p++;
72 port = strtoul(p, (char **)&p, 10);
73 if (port <= 0 || port >= 65536)
74 goto fail;
75
76 dest_addr.sin_family = AF_INET;
77 dest_addr.sin_port = htons(port);
78 if (resolve_host(&dest_addr.sin_addr, hostname) < 0)
79 goto fail;
80
81 fd = socket(PF_INET, SOCK_STREAM, 0);
82 if (fd < 0)
83 goto fail;
84
85 if (connect(fd, (struct sockaddr *)&dest_addr,
86 sizeof(dest_addr)) < 0)
87 goto fail;
88
89 s->fd = fd;
90 return 0;
91
92 fail:
93 if (fd >= 0)
94 close(fd);
95 av_free(s);
96 return -EIO;
97}
98
99static int tcp_read(URLContext *h, UINT8 *buf, int size)
100{
101 TCPContext *s = h->priv_data;
102 int size1, len;
103
104 size1 = size;
105 while (size > 0) {
106 len = read (s->fd, buf, size);
107 if (len < 0) {
108 if (errno != EINTR && errno != EAGAIN)
109 return -errno;
110 else
111 continue;
112 } else if (len == 0) {
113 break;
114 }
115 size -= len;
116 buf += len;
117 }
118 return size1 - size;
119}
120
121static int tcp_write(URLContext *h, UINT8 *buf, int size)
122{
123 TCPContext *s = h->priv_data;
124 int ret, size1;
125
126 size1 = size;
127 while (size > 0) {
128 ret = write (s->fd, buf, size);
129 if (ret < 0 && errno != EINTR && errno != EAGAIN)
130 return -errno;
131 size -= ret;
132 buf += ret;
133 }
134 return size1 - size;
135}
136
137static int tcp_close(URLContext *h)
138{
139 TCPContext *s = h->priv_data;
140 close(s->fd);
141 av_free(s);
142 return 0;
143}
144
145URLProtocol tcp_protocol = {
146 "tcp",
147 tcp_open,
148 tcp_read,
149 tcp_write,
150 NULL, /* seek */
151 tcp_close,
152};