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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
---|
18 | */
|
---|
19 | #include "avformat.h"
|
---|
20 | #include <unistd.h>
|
---|
21 | #include <sys/types.h>
|
---|
22 | #include <sys/socket.h>
|
---|
23 | #include <netinet/in.h>
|
---|
24 | #if defined(__BEOS__) || defined(__INNOTEK_LIBC__)
|
---|
25 | typedef int socklen_t;
|
---|
26 | #endif
|
---|
27 | #ifndef __BEOS__
|
---|
28 | # include <arpa/inet.h>
|
---|
29 | #else
|
---|
30 | # include "barpainet.h"
|
---|
31 | #endif
|
---|
32 | #include <netdb.h>
|
---|
33 | #include <sys/time.h>
|
---|
34 | #include <fcntl.h>
|
---|
35 |
|
---|
36 | typedef struct TCPContext {
|
---|
37 | int fd;
|
---|
38 | } TCPContext;
|
---|
39 |
|
---|
40 | /* resolve host with also IP address parsing */
|
---|
41 | int resolve_host(struct in_addr *sin_addr, const char *hostname)
|
---|
42 | {
|
---|
43 | struct hostent *hp;
|
---|
44 |
|
---|
45 | if ((inet_aton(hostname, sin_addr)) == 0) {
|
---|
46 | hp = gethostbyname(hostname);
|
---|
47 | if (!hp)
|
---|
48 | return -1;
|
---|
49 | memcpy (sin_addr, hp->h_addr, sizeof(struct in_addr));
|
---|
50 | }
|
---|
51 | return 0;
|
---|
52 | }
|
---|
53 |
|
---|
54 | /* return non zero if error */
|
---|
55 | static int tcp_open(URLContext *h, const char *uri, int flags)
|
---|
56 | {
|
---|
57 | struct sockaddr_in dest_addr;
|
---|
58 | char hostname[1024], *q;
|
---|
59 | int port, fd = -1;
|
---|
60 | TCPContext *s = NULL;
|
---|
61 | fd_set wfds;
|
---|
62 | int fd_max, ret;
|
---|
63 | struct timeval tv;
|
---|
64 | socklen_t optlen;
|
---|
65 | char proto[1024],path[1024],tmp[1024]; // PETR: protocol and path strings
|
---|
66 |
|
---|
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
|
---|
71 |
|
---|
72 | s = av_malloc(sizeof(TCPContext));
|
---|
73 | if (!s)
|
---|
74 | return -ENOMEM;
|
---|
75 | h->priv_data = s;
|
---|
76 |
|
---|
77 | if (port <= 0 || port >= 65536)
|
---|
78 | goto fail;
|
---|
79 |
|
---|
80 | dest_addr.sin_family = AF_INET;
|
---|
81 | dest_addr.sin_port = htons(port);
|
---|
82 | if (resolve_host(&dest_addr.sin_addr, hostname) < 0)
|
---|
83 | goto fail;
|
---|
84 |
|
---|
85 | fd = socket(PF_INET, SOCK_STREAM, 0);
|
---|
86 | if (fd < 0)
|
---|
87 | goto fail;
|
---|
88 | fcntl(fd, F_SETFL, O_NONBLOCK);
|
---|
89 |
|
---|
90 | redo:
|
---|
91 | ret = connect(fd, (struct sockaddr *)&dest_addr,
|
---|
92 | sizeof(dest_addr));
|
---|
93 | if (ret < 0) {
|
---|
94 | if (errno == EINTR)
|
---|
95 | goto redo;
|
---|
96 | if (errno != EINPROGRESS)
|
---|
97 | goto fail;
|
---|
98 |
|
---|
99 | /* wait until we are connected or until abort */
|
---|
100 | for(;;) {
|
---|
101 | if (url_interrupt_cb()) {
|
---|
102 | ret = -EINTR;
|
---|
103 | goto fail1;
|
---|
104 | }
|
---|
105 | fd_max = fd;
|
---|
106 | FD_ZERO(&wfds);
|
---|
107 | FD_SET(fd, &wfds);
|
---|
108 | tv.tv_sec = 0;
|
---|
109 | tv.tv_usec = 100 * 1000;
|
---|
110 | ret = select(fd_max + 1, NULL, &wfds, NULL, &tv);
|
---|
111 | if (ret > 0 && FD_ISSET(fd, &wfds))
|
---|
112 | break;
|
---|
113 | }
|
---|
114 |
|
---|
115 | /* test error */
|
---|
116 | optlen = sizeof(ret);
|
---|
117 | getsockopt (fd, SOL_SOCKET, SO_ERROR, &ret, &optlen);
|
---|
118 | if (ret != 0)
|
---|
119 | goto fail;
|
---|
120 | }
|
---|
121 | s->fd = fd;
|
---|
122 | return 0;
|
---|
123 |
|
---|
124 | fail:
|
---|
125 | ret = AVERROR_IO;
|
---|
126 | fail1:
|
---|
127 | if (fd >= 0)
|
---|
128 | close(fd);
|
---|
129 | av_free(s);
|
---|
130 | return ret;
|
---|
131 | }
|
---|
132 |
|
---|
133 | static int tcp_read(URLContext *h, uint8_t *buf, int size)
|
---|
134 | {
|
---|
135 | TCPContext *s = h->priv_data;
|
---|
136 | int len, fd_max, ret;
|
---|
137 | fd_set rfds;
|
---|
138 | struct timeval tv;
|
---|
139 |
|
---|
140 | for (;;) {
|
---|
141 | if (url_interrupt_cb())
|
---|
142 | return -EINTR;
|
---|
143 | fd_max = s->fd;
|
---|
144 | FD_ZERO(&rfds);
|
---|
145 | FD_SET(s->fd, &rfds);
|
---|
146 | tv.tv_sec = 0;
|
---|
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)) {
|
---|
150 | #ifdef __BEOS__
|
---|
151 | len = recv(s->fd, buf, size, 0);
|
---|
152 | #else
|
---|
153 | len = read(s->fd, buf, size);
|
---|
154 | #endif
|
---|
155 | if (len < 0) {
|
---|
156 | if (errno != EINTR && errno != EAGAIN)
|
---|
157 | #ifdef __BEOS__
|
---|
158 | return errno;
|
---|
159 | #else
|
---|
160 | return -errno;
|
---|
161 | #endif
|
---|
162 | } else return len;
|
---|
163 | } else if (ret < 0) {
|
---|
164 | return -1;
|
---|
165 | }
|
---|
166 | }
|
---|
167 | }
|
---|
168 |
|
---|
169 | static int tcp_write(URLContext *h, uint8_t *buf, int size)
|
---|
170 | {
|
---|
171 | TCPContext *s = h->priv_data;
|
---|
172 | int ret, size1, fd_max, len;
|
---|
173 | fd_set wfds;
|
---|
174 | struct timeval tv;
|
---|
175 |
|
---|
176 | size1 = size;
|
---|
177 | while (size > 0) {
|
---|
178 | if (url_interrupt_cb())
|
---|
179 | return -EINTR;
|
---|
180 | fd_max = s->fd;
|
---|
181 | FD_ZERO(&wfds);
|
---|
182 | FD_SET(s->fd, &wfds);
|
---|
183 | tv.tv_sec = 0;
|
---|
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)) {
|
---|
187 | #ifdef __BEOS__
|
---|
188 | len = send(s->fd, buf, size, 0);
|
---|
189 | #else
|
---|
190 | len = write(s->fd, buf, size);
|
---|
191 | #endif
|
---|
192 | if (len < 0) {
|
---|
193 | if (errno != EINTR && errno != EAGAIN) {
|
---|
194 | #ifdef __BEOS__
|
---|
195 | return errno;
|
---|
196 | #else
|
---|
197 | return -errno;
|
---|
198 | #endif
|
---|
199 | }
|
---|
200 | continue;
|
---|
201 | }
|
---|
202 | size -= len;
|
---|
203 | buf += len;
|
---|
204 | } else if (ret < 0) {
|
---|
205 | return -1;
|
---|
206 | }
|
---|
207 | }
|
---|
208 | return size1 - size;
|
---|
209 | }
|
---|
210 |
|
---|
211 | static int tcp_close(URLContext *h)
|
---|
212 | {
|
---|
213 | TCPContext *s = h->priv_data;
|
---|
214 | #ifdef CONFIG_BEOS_NETSERVER
|
---|
215 | closesocket(s->fd);
|
---|
216 | #else
|
---|
217 | close(s->fd);
|
---|
218 | #endif
|
---|
219 | av_free(s);
|
---|
220 | return 0;
|
---|
221 | }
|
---|
222 |
|
---|
223 | URLProtocol tcp_protocol = {
|
---|
224 | "tcp",
|
---|
225 | tcp_open,
|
---|
226 | tcp_read,
|
---|
227 | tcp_write,
|
---|
228 | NULL, /* seek */
|
---|
229 | tcp_close,
|
---|
230 | };
|
---|