Gentoo Websites Logo
Go to: Gentoo Home Documentation Forums Lists Bugs Planet Store Wiki Get Gentoo!
View | Details | Raw Unified | Return to bug 205278 | Differences between
and this patch

Collapse All | Expand All

(-)libdvdcss-1.2.9/src/Makefile.am (+1 lines)
Lines 4-9 Link Here
4
4
5
libdvdcss_la_SOURCES = \
5
libdvdcss_la_SOURCES = \
6
	libdvdcss.c libdvdcss.h \
6
	libdvdcss.c libdvdcss.h \
7
	network.c network.h \
7
	device.c device.h \
8
	device.c device.h \
8
	css.c css.h csstables.h \
9
	css.c css.h csstables.h \
9
	ioctl.c ioctl.h \
10
	ioctl.c ioctl.h \
(-)libdvdcss-1.2.9/src/libdvdcss.c (-10 / +57 lines)
Lines 131-136 Link Here
131
#include "libdvdcss.h"
131
#include "libdvdcss.h"
132
#include "ioctl.h"
132
#include "ioctl.h"
133
#include "device.h"
133
#include "device.h"
134
#include "network.h"
135
136
#define MAX_ERR_MSG 300
134
137
135
/**
138
/**
136
 * \brief Symbol for version checks.
139
 * \brief Symbol for version checks.
Lines 206-211 Link Here
206
        if( i >= 1 ) dvdcss->b_errors = 1;
209
        if( i >= 1 ) dvdcss->b_errors = 1;
207
    }
210
    }
208
211
212
    dvdcss->i_socket_fd = dvdcss_network_connect( dvdcss->psz_device );
213
    if( dvdcss->i_socket_fd != -1 )
214
      return dvdcss;
215
209
    /*
216
    /*
210
     *  Find method from DVDCSS_METHOD environment variable
217
     *  Find method from DVDCSS_METHOD environment variable
211
     */
218
     */
Lines 555-560 Link Here
555
 */
562
 */
556
extern char * dvdcss_error ( dvdcss_t dvdcss )
563
extern char * dvdcss_error ( dvdcss_t dvdcss )
557
{
564
{
565
    if( dvdcss->i_socket_fd != -1 )
566
    {
567
      static char buf[MAX_ERR_MSG];
568
569
      if( dvdcss_network_command( dvdcss->i_socket_fd, buf, "dvd_error") < 0 )
570
        return "(network error)";
571
      else
572
        return buf;
573
    }
574
558
    return dvdcss->psz_error;
575
    return dvdcss->psz_error;
559
}
576
}
560
577
Lines 584-589 Link Here
584
 */
601
 */
585
extern int dvdcss_seek ( dvdcss_t dvdcss, int i_blocks, int i_flags )
602
extern int dvdcss_seek ( dvdcss_t dvdcss, int i_blocks, int i_flags )
586
{
603
{
604
    if( dvdcss->i_socket_fd != -1 )
605
    {
606
      return dvdcss_network_command( dvdcss->i_socket_fd, NULL,
607
                              "dvd_seek %d %d", i_blocks, i_flags);
608
    }
609
587
    /* title cracking method is too slow to be used at each seek */
610
    /* title cracking method is too slow to be used at each seek */
588
    if( ( ( i_flags & DVDCSS_SEEK_MPEG )
611
    if( ( ( i_flags & DVDCSS_SEEK_MPEG )
589
             && ( dvdcss->i_method != DVDCSS_METHOD_TITLE ) )
612
             && ( dvdcss->i_method != DVDCSS_METHOD_TITLE ) )
Lines 628-633 Link Here
628
{
651
{
629
    int i_ret, i_index;
652
    int i_ret, i_index;
630
653
654
    if( dvdcss->i_socket_fd != -1 )
655
    {
656
      return dvdcss_network_command( dvdcss->i_socket_fd, p_buffer,
657
                              "dvd_read %d %d", i_blocks, i_flags);
658
    }
659
631
    i_ret = dvdcss->pf_read( dvdcss, p_buffer, i_blocks );
660
    i_ret = dvdcss->pf_read( dvdcss, p_buffer, i_blocks );
632
661
633
    if( i_ret <= 0
662
    if( i_ret <= 0
Lines 704-709 Link Here
704
    void *iov_base;
733
    void *iov_base;
705
    size_t iov_len;
734
    size_t iov_len;
706
735
736
    if( dvdcss->i_socket_fd != -1 )
737
    {
738
      printf("error: network dvdcss_readv not implemented\n");
739
      return -1;
740
    }
741
707
    i_ret = dvdcss->pf_readv( dvdcss, _p_iovec, i_blocks );
742
    i_ret = dvdcss->pf_readv( dvdcss, _p_iovec, i_blocks );
708
743
709
    if( i_ret <= 0
744
    if( i_ret <= 0
Lines 758-777 Link Here
758
    dvd_title_t *p_title;
793
    dvd_title_t *p_title;
759
    int i_ret;
794
    int i_ret;
760
795
761
    /* Free our list of keys */
796
    if( dvdcss->i_socket_fd != -1 )
762
    p_title = dvdcss->p_titles;
763
    while( p_title )
764
    {
797
    {
765
        dvd_title_t *p_tmptitle = p_title->p_next;
798
        close(dvdcss->i_socket_fd);
766
        free( p_title );
767
        p_title = p_tmptitle;
768
    }
799
    }
800
    else
801
    {
802
        /* Free our list of keys */
803
        p_title = dvdcss->p_titles;
804
        while( p_title )
805
        {
806
            dvd_title_t *p_tmptitle = p_title->p_next;
807
            free( p_title );
808
            p_title = p_tmptitle;
809
        }
769
810
770
    i_ret = _dvdcss_close( dvdcss );
811
        i_ret = _dvdcss_close( dvdcss );
771
812
772
    if( i_ret < 0 )
813
        if( i_ret < 0 )
773
    {
814
        {
774
        return i_ret;
815
            return i_ret;
816
        }
775
    }
817
    }
776
818
777
    free( dvdcss->psz_device );
819
    free( dvdcss->psz_device );
Lines 786-791 Link Here
786
#undef dvdcss_title
828
#undef dvdcss_title
787
extern int dvdcss_title ( dvdcss_t dvdcss, int i_block )
829
extern int dvdcss_title ( dvdcss_t dvdcss, int i_block )
788
{
830
{
831
    if( dvdcss->i_socket_fd != -1 )
832
    {
833
      return dvdcss_network_command( dvdcss->i_socket_fd, NULL, 
834
                              "dvd_title %d", i_block);
835
    }
789
    return _dvdcss_title( dvdcss, i_block );
836
    return _dvdcss_title( dvdcss, i_block );
790
}
837
}
791
838
(-)libdvdcss-1.2.9/src/libdvdcss.h (+1 lines)
Lines 34-39 Link Here
34
    int    i_fd;
34
    int    i_fd;
35
    int    i_read_fd;
35
    int    i_read_fd;
36
    int    i_pos;
36
    int    i_pos;
37
    int    i_socket_fd;
37
38
38
    /* File handling */
39
    /* File handling */
39
    int ( * pf_seek )  ( dvdcss_t, int );
40
    int ( * pf_seek )  ( dvdcss_t, int );
(-)libdvdcss-1.2.9/src/network.c (+331 lines)
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
}
(-)libdvdcss-1.2.9/src/network.h (+20 lines)
Line 0 Link Here
1
/***************************************************************************
2
                          network.h  -  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
int dvdcss_network_connect( char *url );
19
20
int dvdcss_network_command( int socket, char *data_buf, char *msg, ...);

Return to bug 205278