Line 0
Link Here
|
|
|
1 |
/*************************************************************************** |
2 |
network.c - description |
3 |
------------------- |
4 |
begin : Wed Mar 19 2003 |
5 |
copyright : (C) 2003 by miguel |
6 |
email : miguel@miguel |
7 |
***************************************************************************/ |
8 |
|
9 |
/*************************************************************************** |
10 |
* * |
11 |
* This program is free software; you can redistribute it and/or modify * |
12 |
* it under the terms of the GNU General Public License as published by * |
13 |
* the Free Software Foundation; either version 2 of the License, or * |
14 |
* (at your option) any later version. * |
15 |
* * |
16 |
***************************************************************************/ |
17 |
|
18 |
|
19 |
#include <stdio.h> |
20 |
#include <stdlib.h> |
21 |
#include <stdarg.h> |
22 |
#include <unistd.h> |
23 |
#include <string.h> |
24 |
#include <signal.h> |
25 |
#include <errno.h> |
26 |
#include <sys/time.h> |
27 |
#include <sys/socket.h> |
28 |
#include <netinet/in.h> |
29 |
#include <arpa/inet.h> |
30 |
#include <netdb.h> |
31 |
|
32 |
|
33 |
#define _BUFSIZ 300 |
34 |
|
35 |
static int host_connect_attempt (struct in_addr ia, int port) |
36 |
{ |
37 |
int s; |
38 |
struct sockaddr_in sin; |
39 |
|
40 |
s=socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); |
41 |
|
42 |
if (s==-1) { |
43 |
printf("network: failed to open socket\n"); |
44 |
return -1; |
45 |
} |
46 |
|
47 |
sin.sin_family = AF_INET; |
48 |
sin.sin_addr = ia; |
49 |
sin.sin_port = htons(port); |
50 |
|
51 |
if (connect(s, (struct sockaddr *)&sin, sizeof(sin))==-1 && errno != EINPROGRESS) { |
52 |
printf("network: cannot connect to host\n"); |
53 |
close(s); |
54 |
return -1; |
55 |
} |
56 |
|
57 |
return s; |
58 |
} |
59 |
|
60 |
static int host_connect (const char *host, int port) |
61 |
{ |
62 |
struct hostent *h; |
63 |
int i; |
64 |
int s; |
65 |
|
66 |
h=gethostbyname(host); |
67 |
if (h==NULL) { |
68 |
printf("network: unable to resolve >%s<\n", host); |
69 |
return -1; |
70 |
} |
71 |
|
72 |
for(i=0; h->h_addr_list[i]; i++) { |
73 |
struct in_addr ia; |
74 |
memcpy(&ia, h->h_addr_list[i], 4); |
75 |
s=host_connect_attempt(ia, port); |
76 |
if(s != -1) { |
77 |
signal( SIGPIPE, SIG_IGN ); |
78 |
return s; |
79 |
} |
80 |
} |
81 |
|
82 |
printf("network: unable to connect to >%s<\n", host); |
83 |
return -1; |
84 |
} |
85 |
|
86 |
|
87 |
static int parse_url (char *urlbuf, char** host, int *port) { |
88 |
char *start = NULL; |
89 |
char *portcolon = NULL; |
90 |
|
91 |
if (host != NULL) |
92 |
*host = NULL; |
93 |
|
94 |
if (port != NULL) |
95 |
*port = 0; |
96 |
|
97 |
start = strstr(urlbuf, "://"); |
98 |
if (start != NULL) |
99 |
start += 3; |
100 |
else |
101 |
start = urlbuf; |
102 |
|
103 |
while( *start == '/' ) |
104 |
start++; |
105 |
|
106 |
portcolon = strchr(start, ':'); |
107 |
|
108 |
if (host != NULL) |
109 |
*host = start; |
110 |
|
111 |
if (portcolon != NULL) |
112 |
{ |
113 |
*portcolon = '\0'; |
114 |
|
115 |
if (port != NULL) |
116 |
*port = atoi(portcolon + 1); |
117 |
} |
118 |
|
119 |
return 0; |
120 |
} |
121 |
|
122 |
static int sock_check_opened(int socket) { |
123 |
fd_set readfds, writefds, exceptfds; |
124 |
int retval; |
125 |
struct timeval timeout; |
126 |
|
127 |
for(;;) { |
128 |
FD_ZERO(&readfds); |
129 |
FD_ZERO(&writefds); |
130 |
FD_ZERO(&exceptfds); |
131 |
FD_SET(socket, &exceptfds); |
132 |
|
133 |
timeout.tv_sec = 0; |
134 |
timeout.tv_usec = 0; |
135 |
|
136 |
retval = select(socket + 1, &readfds, &writefds, &exceptfds, &timeout); |
137 |
|
138 |
if(retval == -1 && (errno != EAGAIN && errno != EINTR)) |
139 |
return 0; |
140 |
|
141 |
if (retval != -1) |
142 |
return 1; |
143 |
} |
144 |
|
145 |
return 0; |
146 |
} |
147 |
|
148 |
/* |
149 |
* read binary data from socket |
150 |
*/ |
151 |
static int sock_data_read (int socket, char *buf, int nlen) { |
152 |
int n, num_bytes; |
153 |
|
154 |
if((socket < 0) || (buf == NULL)) |
155 |
return -1; |
156 |
|
157 |
if(!sock_check_opened(socket)) |
158 |
return -1; |
159 |
|
160 |
num_bytes = 0; |
161 |
|
162 |
while (num_bytes < nlen) { |
163 |
|
164 |
n = read (socket, &buf[num_bytes], nlen - num_bytes); |
165 |
|
166 |
/* read errors */ |
167 |
if (n < 0) { |
168 |
if(errno == EAGAIN) { |
169 |
fd_set rset; |
170 |
struct timeval timeout; |
171 |
|
172 |
FD_ZERO (&rset); |
173 |
FD_SET (socket, &rset); |
174 |
|
175 |
timeout.tv_sec = 30; |
176 |
timeout.tv_usec = 0; |
177 |
|
178 |
if (select (socket+1, &rset, NULL, NULL, &timeout) <= 0) { |
179 |
printf ("network: timeout on read\n"); |
180 |
return 0; |
181 |
} |
182 |
continue; |
183 |
} |
184 |
printf ("network: read error %d\n", errno); |
185 |
return 0; |
186 |
} |
187 |
|
188 |
num_bytes += n; |
189 |
|
190 |
/* end of stream */ |
191 |
if (!n) break; |
192 |
} |
193 |
|
194 |
return num_bytes; |
195 |
} |
196 |
|
197 |
/* |
198 |
* read a line (\n-terminated) from socket |
199 |
*/ |
200 |
static int sock_string_read(int socket, char *buf, int len) { |
201 |
char *pbuf; |
202 |
int r, rr; |
203 |
void *nl; |
204 |
|
205 |
if((socket < 0) || (buf == NULL)) |
206 |
return -1; |
207 |
|
208 |
if(!sock_check_opened(socket)) |
209 |
return -1; |
210 |
|
211 |
if (--len < 1) |
212 |
return(-1); |
213 |
|
214 |
pbuf = buf; |
215 |
|
216 |
do { |
217 |
|
218 |
if((r = recv(socket, pbuf, len, MSG_PEEK)) <= 0) |
219 |
return -1; |
220 |
|
221 |
if((nl = memchr(pbuf, '\n', r)) != NULL) |
222 |
r = ((char *) nl) - pbuf + 1; |
223 |
|
224 |
if((rr = read(socket, pbuf, r)) < 0) |
225 |
return -1; |
226 |
|
227 |
pbuf += rr; |
228 |
len -= rr; |
229 |
|
230 |
} while((nl == NULL) && len); |
231 |
|
232 |
if (pbuf > buf && *(pbuf-1) == '\n'){ |
233 |
*(pbuf-1) = '\0'; |
234 |
} |
235 |
*pbuf = '\0'; |
236 |
return (pbuf - buf); |
237 |
} |
238 |
|
239 |
/* |
240 |
* Write to socket. |
241 |
*/ |
242 |
static int sock_data_write(int socket, char *buf, int len) { |
243 |
ssize_t size; |
244 |
int wlen = 0; |
245 |
|
246 |
if((socket < 0) || (buf == NULL)) |
247 |
return -1; |
248 |
|
249 |
if(!sock_check_opened(socket)) |
250 |
return -1; |
251 |
|
252 |
while(len) { |
253 |
size = write(socket, buf, len); |
254 |
|
255 |
if(size <= 0) |
256 |
return -1; |
257 |
|
258 |
len -= size; |
259 |
wlen += size; |
260 |
buf += size; |
261 |
} |
262 |
|
263 |
return wlen; |
264 |
} |
265 |
|
266 |
int dvdcss_network_command( int socket, char *data_buf, char *msg, ...) |
267 |
{ |
268 |
char buf[_BUFSIZ]; |
269 |
va_list args; |
270 |
int ret, n; |
271 |
|
272 |
va_start(args, msg); |
273 |
vsnprintf(buf, _BUFSIZ - 1, msg, args); |
274 |
va_end(args); |
275 |
|
276 |
/* Each line sent is '\n' terminated */ |
277 |
if((buf[strlen(buf)] == '\0') && (buf[strlen(buf) - 1] != '\n')) |
278 |
strcat(buf, "\n"); |
279 |
|
280 |
if( sock_data_write(socket, buf, strlen(buf)) < (int)strlen(buf) ) |
281 |
{ |
282 |
printf("network: error writing to socket\n"); |
283 |
return -1; |
284 |
} |
285 |
|
286 |
if( sock_string_read(socket, buf, _BUFSIZ) <= 0 ) |
287 |
{ |
288 |
printf("network: error reading from socket\n"); |
289 |
return -1; |
290 |
} |
291 |
|
292 |
sscanf(buf, "%d %d", &ret, &n ); |
293 |
|
294 |
if( n ) { |
295 |
if( !data_buf ) { |
296 |
printf("network: protocol error, data returned but no buffer provided.\n"); |
297 |
return -1; |
298 |
} |
299 |
if( sock_data_read(socket, data_buf, n) < n ) |
300 |
return -1; |
301 |
} |
302 |
|
303 |
return ret; |
304 |
} |
305 |
|
306 |
int dvdcss_network_connect( char *url ) |
307 |
{ |
308 |
char *host; |
309 |
int port; |
310 |
int fd; |
311 |
|
312 |
url = strdup(url); |
313 |
parse_url(url, &host, &port); |
314 |
|
315 |
if( !host || !strlen(host) || !port ) |
316 |
{ |
317 |
free(url); |
318 |
return -1; |
319 |
} |
320 |
|
321 |
fd = host_connect( host, port ); |
322 |
free(url); |
323 |
|
324 |
if( fd != -1 ) { |
325 |
if( dvdcss_network_command(fd, NULL, "dvd_open") < 0 ) { |
326 |
close(fd); |
327 |
return -1; |
328 |
} |
329 |
} |
330 |
return fd; |
331 |
} |