1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
|
/* CDDA / DVD server
*
* This is a TCP server that can be used with xine's cdda input plugin to
* play audio CDs over the network. It also supports playing DVDs with a
* patched version of libdvdcss.
*
* quick howto:
* - compile it:
* gcc -o cdda_server cdda_server.c -ldl
*
* - start the server:
* ./cdda_server /dev/cdrom 3000
*
* - start the client:
* xine cdda://server:3000/1
*
* to play the entire cd (using GUI's "CD" button) just change
* input.cdda_device to the server's mrl.
*
* 6 May 2003 - Miguel Freitas
* This feature was sponsored by 1Control
*
* note: see also libdvdcss-1.2.6-network.patch
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <stdarg.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>
#include <errno.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <dlfcn.h>
#define QLEN 5 /* maximum connection queue length */
#define _BUFSIZ 300
/* CD-relevant defines and data structures */
#define CD_SECONDS_PER_MINUTE 60
#define CD_FRAMES_PER_SECOND 75
#define CD_RAW_FRAME_SIZE 2352
#define CD_LEADOUT_TRACK 0xAA
#define DVD_BLOCK_SIZE 2048
/* functions from external DVD lib */
typedef struct dvd_s *dvd_handle;
static dvd_handle (*dvd_open) (const char *);
static int (*dvd_close) (dvd_handle);
static int (*dvd_seek) (dvd_handle, int, int);
static int (*dvd_title) (dvd_handle, int);
static int (*dvd_read) (dvd_handle, void *, int, int);
static char * (*dvd_error) (dvd_handle);
static int dvd_support;
static int msock;
static int cdda_fd;
static dvd_handle dvd;
static char *cdrom_device;
#if defined (__linux__)
#include <linux/cdrom.h>
static int read_cdrom_toc_header(int fd, int *first_track, int *last_track) {
struct cdrom_tochdr tochdr;
/* fetch the table of contents */
if (ioctl(fd, CDROMREADTOCHDR, &tochdr) == -1) {
perror("CDROMREADTOCHDR");
return -1;
}
*first_track = tochdr.cdth_trk0;
*last_track = tochdr.cdth_trk1;
return 0;
}
static int read_cdrom_toc_entry(int fd, int track, int *track_mode,
int *first_frame_minute, int *first_frame_second, int *first_frame_frame ) {
struct cdrom_tocentry tocentry;
memset(&tocentry, 0, sizeof(tocentry));
tocentry.cdte_track = track;
tocentry.cdte_format = CDROM_MSF;
if (ioctl(fd, CDROMREADTOCENTRY, &tocentry) == -1) {
perror("CDROMREADTOCENTRY");
return -1;
}
*track_mode = (tocentry.cdte_ctrl & 0x04) ? 1 : 0;
*first_frame_minute = tocentry.cdte_addr.msf.minute;
*first_frame_second = tocentry.cdte_addr.msf.second;
*first_frame_frame = tocentry.cdte_addr.msf.frame;
return 0;
}
static int read_cdrom_frames(int fd, int frame, int num_frames,
unsigned char *data) {
struct cdrom_msf msf;
while( num_frames ) {
/* read from starting frame... */
msf.cdmsf_min0 = frame / CD_SECONDS_PER_MINUTE / CD_FRAMES_PER_SECOND;
msf.cdmsf_sec0 = (frame / CD_FRAMES_PER_SECOND) % CD_SECONDS_PER_MINUTE;
msf.cdmsf_frame0 = frame % CD_FRAMES_PER_SECOND;
/* read until ending track (starting frame + 1)... */
msf.cdmsf_min1 = (frame + 1) / CD_SECONDS_PER_MINUTE / CD_FRAMES_PER_SECOND;
msf.cdmsf_sec1 = ((frame + 1) / CD_FRAMES_PER_SECOND) % CD_SECONDS_PER_MINUTE;
msf.cdmsf_frame1 = (frame + 1) % CD_FRAMES_PER_SECOND;
/* MSF structure is the input to the ioctl */
memcpy(data, &msf, sizeof(msf));
/* read a frame */
if(ioctl(fd, CDROMREADRAW, data, data) < 0) {
perror("CDROMREADRAW");
return -1;
}
data += CD_RAW_FRAME_SIZE;
frame++;
num_frames--;
}
return 0;
}
#endif
/* network functions */
static int sock_has_data(int socket){
fd_set readfds;
fd_set writefds;
fd_set exceptfds;
struct timeval tv;
tv.tv_sec = 0;
tv.tv_usec = 0;
FD_ZERO(&readfds);
FD_ZERO(&writefds);
FD_ZERO(&exceptfds);
FD_SET(socket, &readfds);
return (select(socket+1, &readfds, &writefds, &exceptfds, &tv) > 0);
}
static int sock_check_opened(int socket) {
fd_set readfds, writefds, exceptfds;
int retval;
struct timeval timeout;
for(;;) {
FD_ZERO(&readfds);
FD_ZERO(&writefds);
FD_ZERO(&exceptfds);
FD_SET(socket, &exceptfds);
timeout.tv_sec = 0;
timeout.tv_usec = 0;
retval = select(socket + 1, &readfds, &writefds, &exceptfds, &timeout);
if(retval == -1 && (errno != EAGAIN && errno != EINTR))
return 0;
if (retval != -1)
return 1;
}
return 0;
}
#if 0
/*
* read binary data from socket
*/
static int sock_data_read (int socket, char *buf, int nlen) {
int n, num_bytes;
if((socket < 0) || (buf == NULL))
return -1;
if(!sock_check_opened(socket))
return -1;
num_bytes = 0;
while (num_bytes < nlen) {
n = read (socket, &buf[num_bytes], nlen - num_bytes);
/* read errors */
if (n < 0) {
if(errno == EAGAIN) {
fd_set rset;
struct timeval timeout;
FD_ZERO (&rset);
FD_SET (socket, &rset);
timeout.tv_sec = 30;
timeout.tv_usec = 0;
if (select (socket+1, &rset, NULL, NULL, &timeout) <= 0) {
printf ("network: timeout on read\n");
return 0;
}
continue;
}
printf ("network: read error %d\n", errno);
return 0;
}
num_bytes += n;
/* end of stream */
if (!n) break;
}
return num_bytes;
}
#endif
/*
* read a line (\n-terminated) from socket
*/
static int sock_string_read(int socket, char *buf, int len) {
char *pbuf;
int r, rr;
void *nl;
if((socket < 0) || (buf == NULL))
return -1;
if(!sock_check_opened(socket))
return -1;
if (--len < 1)
return(-1);
pbuf = buf;
do {
if((r = recv(socket, pbuf, len, MSG_PEEK)) <= 0)
return -1;
if((nl = memchr(pbuf, '\n', r)) != NULL)
r = ((char *) nl) - pbuf + 1;
if((rr = read(socket, pbuf, r)) < 0)
return -1;
pbuf += rr;
len -= rr;
} while((nl == NULL) && len);
if (pbuf > buf && *(pbuf-1) == '\n'){
*(pbuf-1) = '\0';
}
*pbuf = '\0';
return (pbuf - buf);
}
/*
* Write to socket.
*/
static int sock_data_write(int socket, char *buf, int len) {
ssize_t size;
int wlen = 0;
if((socket < 0) || (buf == NULL))
return -1;
if(!sock_check_opened(socket))
return -1;
while(len) {
size = write(socket, buf, len);
if(size <= 0) {
printf("error writing to socket %d\n",socket);
return -1;
}
len -= size;
wlen += size;
buf += size;
}
return wlen;
}
int sock_string_write(int socket, char *msg, ...) {
char buf[_BUFSIZ];
va_list args;
va_start(args, msg);
vsnprintf(buf, _BUFSIZ - 1, msg, args);
va_end(args);
/* Each line sent is '\n' terminated */
if((buf[strlen(buf)] == '\0') && (buf[strlen(buf) - 1] != '\n'))
strcat(buf, "\n");
return sock_data_write(socket, buf, strlen(buf));
}
/**
* Setup dvd read functions
*/
int dvdinput_setup(void)
{
void *dvdcss_library = NULL;
char **dvdcss_version = NULL;
/* dlopening libdvdcss */
#ifndef _MSC_VER
dvdcss_library = dlopen("libdvdcss.so.2", RTLD_LAZY);
#else
dvdcss_library = dlopen("libdvdcss.dll", RTLD_LAZY);
#endif
if(dvdcss_library != NULL) {
#if defined(__OpenBSD__) && !defined(__ELF__)
#define U_S "_"
#else
#define U_S
#endif
dvd_open = (dvd_handle (*)(const char*))
dlsym(dvdcss_library, U_S "dvdcss_open");
dvd_close = (int (*)(dvd_handle))
dlsym(dvdcss_library, U_S "dvdcss_close");
dvd_title = (int (*)(dvd_handle, int))
dlsym(dvdcss_library, U_S "dvdcss_title");
dvd_seek = (int (*)(dvd_handle, int, int))
dlsym(dvdcss_library, U_S "dvdcss_seek");
dvd_read = (int (*)(dvd_handle, void*, int, int))
dlsym(dvdcss_library, U_S "dvdcss_read");
dvd_error = (char* (*)(dvd_handle))
dlsym(dvdcss_library, U_S "dvdcss_error");
dvdcss_version = (char **)dlsym(dvdcss_library, U_S "dvdcss_interface_2");
if(dlsym(dvdcss_library, U_S "dvdcss_crack")) {
fprintf(stderr,
"libdvdread: Old (pre-0.0.2) version of libdvdcss found.\n"
"libdvdread: You should get the latest version from "
"http://www.videolan.org/\n" );
dlclose(dvdcss_library);
dvdcss_library = NULL;
} else if(!dvd_open || !dvd_close || !dvd_title || !dvd_seek
|| !dvd_read || !dvd_error || !dvdcss_version) {
fprintf(stderr, "libdvdread: Missing symbols in libdvdcss.so.2, "
"this shouldn't happen !\n");
dlclose(dvdcss_library);
}
}
if(dvdcss_library != NULL) {
printf("Using libdvdcss version %s for DVD access\n",
*dvdcss_version);
return 1;
} else {
printf("No libdvdcss: DVD support unavailable.\n");
return 0;
}
}
#define CMD_CDDA_OPEN "cdda_open"
#define CMD_CDDA_READ "cdda_read"
#define CMD_CDDA_TOCHDR "cdda_tochdr"
#define CMD_CDDA_TOCENTRY "cdda_tocentry"
#define CMD_DVD_OPEN "dvd_open"
#define CMD_DVD_ERROR "dvd_error"
#define CMD_DVD_SEEK "dvd_seek"
#define CMD_DVD_READ "dvd_read"
#define CMD_DVD_TITLE "dvd_title"
static int process_commands( int socket )
{
char cmd[_BUFSIZ];
int start_frame, num_frames, i;
int blocks, flags;
int ret, n;
while( sock_has_data(socket) )
{
if( sock_string_read(socket, cmd, _BUFSIZ) <= 0 )
return -1;
if( !strncmp(cmd, CMD_CDDA_OPEN, strlen(CMD_CDDA_OPEN)) ) {
if( cdda_fd != -1 )
close(cdda_fd);
cdda_fd = open ( cdrom_device, O_RDONLY);
if( cdda_fd == -1 )
{
printf( "argh ! couldn't open CD (%s)\n", cdrom_device );
if( sock_string_write(socket,"-1 0") < 0 )
return -1;
}
else {
if( sock_string_write(socket,"0 0") < 0 )
return -1;
}
continue;
}
if( dvd_support && !strncmp(cmd, CMD_DVD_OPEN, strlen(CMD_DVD_OPEN)) ) {
if( dvd != NULL )
dvd_close(dvd);
dvd = dvd_open ( cdrom_device );
if( !dvd )
{
printf( "argh ! couldn't open DVD (%s)\n", cdrom_device );
if( sock_string_write(socket,"-1 0") < 0 )
return -1;
}
else {
if( sock_string_write(socket,"0 0") < 0 )
return -1;
}
continue;
}
if( cdda_fd != -1 ) {
if( !strncmp(cmd, CMD_CDDA_READ, strlen(CMD_CDDA_READ)) ) {
char *buf;
sscanf(cmd,"%*s %d %d", &start_frame, &num_frames);
n = num_frames * CD_RAW_FRAME_SIZE;
buf = malloc( n );
if( !buf )
{
printf("fatal error: could not allocate memory\n");
exit(1);
}
ret = read_cdrom_frames(cdda_fd, start_frame, num_frames, buf );
if( sock_string_write(socket,"%d %d", ret, n) < 0 )
return -1;
if( sock_data_write(socket,buf,n) < 0 )
return -1;
free(buf);
continue;
}
if( !strncmp(cmd, CMD_CDDA_TOCHDR, strlen(CMD_CDDA_TOCHDR)) ) {
int first_track, last_track;
ret = read_cdrom_toc_header(cdda_fd, &first_track, &last_track);
if( sock_string_write(socket,"%d 0 %d %d", ret, first_track, last_track) < 0 )
return -1;
continue;
}
if( !strncmp(cmd, CMD_CDDA_TOCENTRY, strlen(CMD_CDDA_TOCENTRY)) ) {
int track_mode, first_frame_minute, first_frame_second, first_frame_frame;
sscanf(cmd,"%*s %d", &i);
ret = read_cdrom_toc_entry(cdda_fd, i, &track_mode,
&first_frame_minute, &first_frame_second, &first_frame_frame );
if( sock_string_write(socket,"%d 0 %d %d %d %d", ret,
track_mode, first_frame_minute, first_frame_second, first_frame_frame) < 0 )
return -1;
continue;
}
} else if ( dvd != NULL ) {
if( !strncmp(cmd, CMD_DVD_ERROR, strlen(CMD_DVD_ERROR)) ) {
char *errmsg = dvd_error( dvd );
n = strlen(errmsg)+1;
if( sock_string_write(socket,"0 %d", n) < 0 )
return -1;
if( sock_data_write(socket,errmsg,n) < 0 )
return -1;
continue;
}
if( !strncmp(cmd, CMD_DVD_SEEK, strlen(CMD_DVD_SEEK)) ) {
sscanf(cmd,"%*s %d %d", &blocks, &flags);
ret = dvd_seek(dvd, blocks, flags);
if( sock_string_write(socket,"%d 0", ret) < 0 )
return -1;
continue;
}
if( !strncmp(cmd, CMD_DVD_READ, strlen(CMD_DVD_READ)) ) {
char *buf;
sscanf(cmd,"%*s %d %d", &blocks, &flags);
n = blocks * DVD_BLOCK_SIZE;
buf = malloc( n );
if( !buf )
{
printf("fatal error: could not allocate memory\n");
exit(1);
}
ret = dvd_read(dvd, buf, blocks, flags);
if( sock_string_write(socket,"%d %d", ret, n) < 0 )
return -1;
if( sock_data_write(socket,buf,n) < 0 )
return -1;
free(buf);
continue;
}
if( !strncmp(cmd, CMD_DVD_TITLE, strlen(CMD_DVD_TITLE)) ) {
sscanf(cmd,"%*s %d", &blocks);
ret = dvd_title(dvd, blocks);
if( sock_string_write(socket,"%d 0", ret) < 0 )
return -1;
continue;
}
}
/* no device open, or unknown command. return error */
if( sock_string_write(socket,"-1 0") < 0 )
return -1;
}
return 0;
}
static void server_loop()
{
struct sockaddr_in fsin; /* the from address of a client */
int alen; /* from-address length */
int fd, nfds;
fd_set rfds; /* read file descriptor set */
fd_set afds; /* active file descriptor set */
/* SIGPIPE when connection closed during write call */
signal( SIGPIPE, SIG_IGN );
nfds = getdtablesize();
FD_ZERO(&afds);
FD_SET(msock, &afds);
while (1)
{
memcpy( &rfds, &afds, sizeof(rfds) );
if (select(nfds, &rfds, (fd_set *)0, (fd_set *)0, (struct timeval *)0) < 0)
continue; /* erro?? */
if (FD_ISSET(msock, &rfds))
{
int ssock;
alen = sizeof(fsin);
ssock = accept(msock, (struct sockaddr *)&fsin, &alen);
if (ssock < 0)
continue; /* erro?? */
FD_SET(ssock, &afds);
printf("new connection socket %d\n", ssock);
}
for (fd=0; fd<nfds; ++fd)
{
if (fd != msock && FD_ISSET(fd, &rfds))
if (process_commands(fd) < 0)
{
printf("closing socket %d\n", fd);
close(fd);
FD_CLR(fd, &afds);
if( cdda_fd != -1 ) {
close(cdda_fd);
cdda_fd = -1;
}
if( dvd != NULL ) {
dvd_close(dvd);
dvd = NULL;
}
}
}
}
}
int main( int argc, char *argv[] )
{
unsigned int port;
struct sockaddr_in servAddr;
/* Print version number */
printf( "CDDA / DVD tcp server v0.1\n" );
dvd_support = dvdinput_setup();
/* Check for 2 arguments */
if( argc != 3 )
{
printf( "usage: %s <device> <server_port>\n", argv[0] );
return -1;
}
port = atoi( argv[2] );
cdda_fd = -1;
dvd = NULL;
cdrom_device = argv[1];
msock = socket(PF_INET, SOCK_STREAM, 0);
if( msock < 0 )
{
printf("error opening master socket.\n");
return 0;
}
servAddr.sin_family = AF_INET;
servAddr.sin_addr.s_addr = htonl(INADDR_ANY);
servAddr.sin_port = htons(port);
if(bind(msock, (struct sockaddr *) &servAddr, sizeof(servAddr))<0)
{
printf("bind port %d error\n", port);
return 0;
}
listen(msock,QLEN);
printf("listening on port %d...\n", port);
server_loop();
close(msock);
return 0;
}
|