[go: up one dir, main page]

File: cdown.c

package info (click to toggle)
cdtool 2.1.5-4
  • links: PTS
  • area: main
  • in suites: potato
  • size: 284 kB
  • ctags: 176
  • sloc: ansic: 2,216; makefile: 167; sh: 121
file content (743 lines) | stat: -rw-r--r-- 21,457 bytes parent folder | download | duplicates (2)
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
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
/* cdown.c
 *
 * utility to contact a CDDB server and download information about the
 * currently loaded CD in a format suitable for use with the cdtools
 * package, and specifically cdir.
 *
 * version 0.2
 *
 * Copyright (C) 1998 Byron Ellacott
 * No express or implied warranties pertain to the use of this source
 * code and/or binaries generated from it.  You are free to distribute
 * this code.  You are free to modify this code, providing the first
 * 24 lines remain unchanged.  Any modifications in distributed source
 * must be clearly stated immediately following this comment block.
 *
 * this software is a work in progress.  Many things are
 * unimplemented.  Many values are hardcoded.  Do not expect this
 * software to work for you.
 *
 *
 * Some documentation and inclusion in cdtools package by Wade Hampton
 *
 */

/* CHANGELOG
 * 0.2		Modifications by Wade Hampton
 *		Environment checking for CDDBHOST
 *		Added verbosity
 *		Added -l option to list available hosts
 *
 * 0.1a		Initial release
 */

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <stdarg.h>
#include <errno.h>
#include <netdb.h>
#include <unistd.h>
#include <string.h>
#include <pwd.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

#include <linux/cdrom.h>

/* constants */
#ifndef TRUE
  #define TRUE (1==1)
#endif

#ifndef FALSE
  #define FALSE (1==0)
#endif

/* program name:  */
#define CDOWN "cdown"

/* version */
#define VERSIONSTRING "0.2"

/* default query host and port:  */
#define CDDB_HOST  "cddb.cddb.com"
#define CDDB_PORT 888

/* default CD-ROM:  */
#define CD_DEVICE "/dev/cdrom"
#define CD_DEVICE_BASE "/dev/cdrom"

typedef struct toc {
  int min, sec, frame;
} CDTocType;

CDTocType cdtoc[100];

static int sock;
static int iVerbose = FALSE;

char *rcsid="$Id$";

/************************************************************************/
/* Procedure:  read_toc
 * Purpose:    to read the CD-ROM's TOC
 * 
 * Inputs:     cd name
 * Outputs:    to TOC structure
 * Returns:    -1 or tochdr.cdth_trk1 (total tracks)
 * Notes:  
 *   1.
 */
/************************************************************************/
int read_toc(char *pszCD) 
{
    int drive;
    struct cdrom_tochdr tochdr;
    struct cdrom_tocentry tocentry;
    int i;

    /* check command line args */
    if (pszCD == NULL)
	return (-1);

    /* open the CD-ROM and query it for the TOC */
    drive = open(pszCD, O_RDONLY | O_NONBLOCK);
    if (drive < 0)
	return (-2);

    if (ioctl(drive, CDROMREADTOCHDR, &tochdr) == -1)
	return (-3);
  
    /* parse the TOC and fill int the cddb TOC table */
    for (i = tochdr.cdth_trk0; i <= tochdr.cdth_trk1; i++) {
	tocentry.cdte_track = i;
	tocentry.cdte_format = CDROM_MSF;
	ioctl(drive, CDROMREADTOCENTRY, &tocentry);
	cdtoc[i-1].min = tocentry.cdte_addr.msf.minute;
	cdtoc[i-1].sec = tocentry.cdte_addr.msf.second;
	cdtoc[i-1].frame = tocentry.cdte_addr.msf.frame;
	cdtoc[i-1].frame += cdtoc[i-1].min*60*75;
	cdtoc[i-1].frame += cdtoc[i-1].sec*75;
    }
    tocentry.cdte_track = 0xAA;
    tocentry.cdte_format = CDROM_MSF;
    ioctl(drive, CDROMREADTOCENTRY, &tocentry);
    cdtoc[tochdr.cdth_trk1].min = tocentry.cdte_addr.msf.minute;
    cdtoc[tochdr.cdth_trk1].sec = tocentry.cdte_addr.msf.second;
    cdtoc[tochdr.cdth_trk1].frame = tocentry.cdte_addr.msf.frame;
    cdtoc[tochdr.cdth_trk1].frame += cdtoc[tochdr.cdth_trk1].min*60*75;
    cdtoc[tochdr.cdth_trk1].frame += cdtoc[tochdr.cdth_trk1].sec*75;
    close(drive);

    return tochdr.cdth_trk1;
}

/************************************************************************/
/* Procedure:  cddb_sum
 * Purpose:    Checksum generation routine used by cddb_discid
 * 
 * Inputs:     n is the offset of the track to checksum, in seconds
 * Outputs:    
 * Returns:    the checksum value for n
 * Notes:  
 *   1. Algorithm and code from http://www.cddb.com/
 */
/************************************************************************/
unsigned int cddb_sum(int n) {
  unsigned int ret;

  ret = 0;
  while (n > 0) {
    ret += (n % 10);
    n /= 10;
  }
  return ret;
}

/************************************************************************/
/* Procedure:  cddb_discid
 * Purpose:    Generates the CDDB disc ID
 * 
 * Inputs:     tot_trks
 * Outputs:    
 * Returns:    The disc ID, as a 32 bit number
 * Notes:  
 *   1. Algorithm and code from http://www.cddb.com/
 */
/************************************************************************/
unsigned long cddb_discid(int tot_trks) {
  unsigned int i, t = 0, n = 0;

  i = 0;
  while (i < tot_trks) {
    n = n + cddb_sum((cdtoc[i].min * 60) + cdtoc[i].sec);
    i++;
  }
  t = ((cdtoc[tot_trks].min * 60) + cdtoc[tot_trks].sec) -
      ((cdtoc[0].min * 60) + cdtoc[0].sec);
  return ((n % 0xff) << 24 | t << 8 | tot_trks);
}

/************************************************************************/
/* Procedure:  netwrite
 * Purpose:    Completely write a buffer to a socket
 * 
 * Inputs:     sock - the socket
 *	       text - data to send
 *	       nchars - length of data to send
 * Outputs:    to sock
 * Returns:    -1 on error, 0 on success
 * Notes:  
 *   1.
 */
/************************************************************************/
int netwrite(int sock, char *text, int nchars) {
  char *strLeft;
  int len;

  strLeft = text;
  do {
    len = write(sock, strLeft, nchars);         /* write as much as we can */
    if (len < 0) return -1;                     /* socket error, damn */
    strLeft += len;                             /* move along the string */
    nchars -= len;                              /* fewer characters to send */
  } while (nchars > 0);                         /* until done entire string */
  return 0;
}

/************************************************************************/
/* Procedure:  netprintf
 * Purpose:    printf() that outputs using netwrite()
 * 
 * Inputs:     sock - the socket to write to
 *	       fmt - format arg, passed to vasprintf()
 *             ... - other arguments to vasprintf()
 * Outputs:    
 * Returns:    -1 on error, 0 on success
 * Notes:  
 *   1.
 */
/************************************************************************/
int netprintf(int sock, char *fmt, ...) {
  char *buffer;
  va_list args;

  va_start(args, fmt);
  vasprintf(&buffer, fmt, args);
  va_end(args);
  return netwrite(sock, buffer, strlen(buffer));
}

/************************************************************************/
/* Procedure:  netconnect
 * Purpose:    attempt to open a TCP connection to hostname, on port
 *             port
 * 
 * Inputs:     progname - used in error reporting
 *             hostname - host to connect to
 *             port - port number to connect to
 * Outputs:    to stderr
 * Returns:    the newly created socket
 * Notes:  
 *   1. calls exit(3) on error
 */
/************************************************************************/
int netconnect(char *progname, char *hostname, int port) {
  int sock;
  struct hostent *host;
  struct sockaddr_in iaddr;

  if (iVerbose) fprintf(stderr, "Resolving host %s\n", hostname);

  /* resolve the host name */
  host = gethostbyname(hostname);
  if (host == NULL) {
      fprintf (stderr, "%s:  error: Getting host info for %s\n", 
	       progname, hostname);
      perror("        error");
      exit(3);
  }

  iaddr.sin_port = htons(port);
  iaddr.sin_addr.s_addr = *(unsigned int *)host->h_addr;
  iaddr.sin_family = AF_INET;

  /* create the socket */
  if ((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1) {
      fprintf (stderr, "%s:  error: Creating socket\n", 
	       progname);
      perror("        error");
      exit(3);
  }

  if (iVerbose) fprintf(stderr, "Connecting...\n");

  /* make the connection */
  if (connect(sock, (struct sockaddr *)&iaddr, sizeof(iaddr)) == -1) {
      fprintf (stderr, "%s:  error: Oonnecting to host %s port %d\n", 
	       progname, hostname, port);
      perror("        error");
      close(sock);
      exit(3);
  }
  return sock;
}

/************************************************************************/
/* Procedure:  getline
 * Purpose:    read a line of text from the CDDB server
 * 
 * Inputs:     
 * Outputs:    
 * Returns:    The line of text
 * Notes:  
 *   1. The CDDB databases use \r\n to end lines.  All lines returned
 *      by getline() have the \r and \n stripped.
 *   2. Non-reentrant: the memory buffer returned previously is
 *      recycled
 *   3. Do NOT modify the contents of the returned buffer past the
 *      NULL - data from the network is stored there
 */
/************************************************************************/
char *getline(void) {
  static char *buffer = NULL;
  static char *lastline = NULL;
  static int buffered = 0;
  char newdata[1024];
  char *nl;
  int r, len;

  /* Copy the previously buffered data and resize the buffer */
  if (lastline != NULL) {
    memmove(lastline, buffer, buffered+1);
    buffer = realloc(lastline, buffered+1);
  }

  /* while an end of line marker isn't reached */
  while (buffer == NULL || (nl = strchr(buffer, '\n')) == NULL) {
    len = read(sock, newdata, 1024);
    buffer = realloc(buffer, buffered+len+1);	/* grow the buffer */
    memcpy(buffer+buffered, newdata, len);	/* add the new data */
    buffer[buffered+len] = '\0';		/* make it asciiz */
    buffered += len;
  }

  lastline = buffer;			/* remember the buffer */
  buffer = nl+1;			/* this is the unused data */
  *nl = '\0';				/* EOL marking */
  buffered -= strlen(lastline)+1;	/* this data is no longer buffered */
  nl -= 1;				/* adjust for the NULL */
  if (*nl == 0x0d) *nl = '\0';		/* strip any \r's */
  return lastline;			/* return the data */
}

/************************************************************************/
/* Procedure:  signon
 * Purpose:    parse the initial connection message
 * 
 * Inputs:     message - the response from the server
 * Outputs:    to stderr
 * Returns:    1 on successful connect, 0 on failed connect
 * Notes:  
 *   1. Displays an error message to stdout in the event of an error
 */
/************************************************************************/
int signon(char *message) {
  switch (atoi(message)) {
    case 200: return 1;
    case 201: return 1;
    case 432: fprintf(stderr, "error: permission denied\n"); return 0;
    case 433: fprintf(stderr, "error: server is full\n"); return 0;
    case 434: fprintf(stderr, "error: server load is too high\n"); return 0;
    default: fprintf(stderr, "error: bad message: %s\n", message); return 0;
  }
}

/************************************************************************/
/* Procedure:  welcome
 * Purpose:    parse the server response to ``cddb hello ...''
 * 
 * Inputs:     message - the response from the server
 * Outputs:    to stderr
 * Returns:    1 on success, 0 on failure
 * Notes:  
 *   1. Should never get a 402, check present just in case
 */
/************************************************************************/
int welcome(char *message) {
  int status = 1;

  switch (atoi(message)) {
    case 431: fprintf(stderr, "error: handshake failed\n"); status = 0; break;
    case 402: fprintf(stderr, "warning: already shook hands?\n"); break;
  }
  return status;
}

/************************************************************************/
/* Procedure:  printcd
 * Purpose:    Tidily displays the title of a CD
 * 
 * Inputs:     message - the CD title as returned by CDDB
 * Outputs:    to stderr
 * Returns:    
 * Notes:  
 *   1. Seems neat enough for the time being
 */
/************************************************************************/
void printcd(char *message) {
  fprintf(stderr, "%s\n", message);
}

/************************************************************************/
/* Procedure:  readinfo
 * Purpose:    download and output the track information for a CD
 * 
 * Inputs:     categ, discid - the identifiers used by CDDB
 * Outputs:    to stderr and stdout
 * Returns:    nothing
 * Notes:  
 *   1. Nice to make this append to .cdtooldb when it's close enough
 *      to bug free
 */
/************************************************************************/
void readinfo(char *categ, char *discid) {
  char *message;
  char *firstword;
  char *cdtitle;

  netprintf(sock, "cddb read %s %s\n", categ, discid);
  message = getline();
  switch (atoi(message)) {
    case 401: fprintf(stderr, "error: entry not found\n"); return;
    case 402: fprintf(stderr, "error: server error\n"); return;
    case 403: fprintf(stderr, "error: database corrupt\n"); return;
    case 409: fprintf(stderr, "error: no handshake\n"); return;
    case 210: break;
    default: fprintf(stderr, "error: bad message: %s\n", message); return;
  }
  do {
    message = getline();
    firstword = strchr(message, '=');
    if (firstword != NULL) {
      *firstword = '\0';
      if (strcmp(message, "DTITLE") == 0) {
        cdtitle = strchr(firstword+1, '/');
	if (cdtitle == NULL) {
	  cdtitle = firstword+1;
	} else {
	  *cdtitle = '\0';
	  cdtitle += 1;
	  while (*cdtitle == ' ') cdtitle++;
	}
        printf("cdname %s\n", cdtitle);
	printf("artist %s\n", firstword+1);
      } else if (strncmp(message, "TTITLE", 6) == 0) {
        printf("track %s\n", firstword+1);
      }
    }
/*    printf("%s\n", message);*/
  } while (strcmp(message, ".") != 0);
  printf("\n");
}

/************************************************************************/
/* Procedure:  query
 * Purpose:    ask cddb server about CDs matching the generated discid
 * 
 * Inputs:     
 * Outputs:    to stderr
 * Returns:    
 * Notes:  
 *   1. queries user if discid has multiple matches
 */
/************************************************************************/
void query(void) {
  char *message;
  char *categ, *discid;
  char **matches = NULL;
  int count = 0;
  int input = 0;
  int exact = 1;

  message = getline();
  switch (atoi(message)) {
    case 403: fprintf(stderr, "error: database entry is corrupt\n"); return;
    case 409: fprintf(stderr, "error: no handshake\n"); return;
    case 202: fprintf(stderr, "error: no match\n"); return;
    case 200: message += 4; break;
    case 211: exact = 0; break;
    default: fprintf(stderr, "error: bad message: %s\n", message); return;
  }
  if (!exact) {
    fprintf(stderr, "CD has no exact match\n 0: None\n");
    do {
      message = getline();
      if (strcmp(message, ".") != 0) {
        matches = realloc(matches, sizeof(char*) * (++count));
        fprintf(stderr, " %d: ", count); printcd(message);
        matches[count-1] = malloc(strlen(message)+1);
        strcpy(matches[count-1], message);
      }
    } while (strcmp(message, ".") != 0);
    fprintf(stderr, "Enter the number corresponding to your CD? ");
    scanf("%d", &input);	/* could be done better? */
    input--;
    if (input == -1) return;
    message = matches[input];
  }
  categ = message;
  discid = strchr(categ, ' ');
  *discid++ = '\0';
  message = strchr(discid, ' ');
  *message++ = '\0';
  if (iVerbose) fprintf(stderr, "Reading information for %s\n", message);
  readinfo(categ, discid);
}

/************************************************************************/
/* Procedure:  PrintHelp
 * Purpose:    print help 
 * 
 * Inputs:     prog name
 * Outputs:    to stderr
 * Returns:    none
 * Notes:  
 *   1.
 */
/************************************************************************/
void PrintHelp(char *pszName)
{
    fprintf (stderr, "%s:  get CD info from cddb database\n", pszName);
    fprintf (stderr, "usage:  cddb [-{1..9}] [-d D] [-H H] [-P P] [-v] [-V|-h|-?]\n");
    fprintf (stderr, "  cddb         get database for default CDROM\n");
    fprintf (stderr, " -d device     specify the CDROM device\n");
    fprintf (stderr, " -H host       get database using HOST (def=%s)\n",
	     CDDB_HOST);
    fprintf (stderr, " -P port       get database using PORT (def=%d)\n",
	     CDDB_PORT);
    fprintf (stderr, " -l            list available cddb hosts\n");
    fprintf (stderr, " -N, N={1..9}  specify /dev/cdromN (def=%s)\n",
	     CD_DEVICE_BASE);
    fprintf (stderr, " -v            verbose output\n");
    fprintf (stderr, " -V            print version, exit\n");
    fprintf (stderr, " -?|-h         print help, exit\n\n");
}

/************************************************************************/
/* Procedure:  getuname
 * Purpose:    get the name of the user who ran the program
 * 
 * Inputs:     
 * Outputs:    
 * Returns:    buffer containing username
 * Notes:  
 *   1.
 */
/************************************************************************/
char *getuname(void) {
  struct passwd *pwd;

  pwd = getpwuid(getuid());
  return pwd->pw_name;
}

/************************************************************************/
/* Procedure:  gethname
 * Purpose:    get the name of the host computer
 * 
 * Inputs:     
 * Outputs:    
 * Returns:    buffer containing hostname
 * Notes:  
 *   1.
 */
/************************************************************************/
char *gethname(void) {
  static char buffer[80];	/* is 80 long enough? */

  /* assuming this works is a bad idea ..? */
  gethostname(buffer, sizeof(buffer));
  return buffer;
}

/************************************************************************/
/* Procedure:  parsesites
 * Purpose:    read the list of sites from the server
 * 
 * Inputs:     command line args
 * Outputs:    to stdout (!)
 * Returns:    
 * Notes:  
 *   1.
 */
/************************************************************************/
void parsesites(void) {
  char *buffer;

  buffer = getline();
  switch (atoi(buffer)) {
    case 210: break;		/* OK */
    case 401: fprintf(stderr, "error: no site list available\n"); return;
  }
  buffer = getline();
  do {
    printf("%s\n", buffer);
  } while (strcmp(buffer = getline(), ".") != 0);
}

/************************************************************************/
/* Procedure:  main
 * Purpose:    C code main function
 * 
 * Inputs:     command line args
 * Outputs:    to stdout and stderr
 * Returns:    error code (0 if exit is OK)
 * Notes:  
 *   1.
 */
/************************************************************************/
int main(int argc, char *argv[]) {


  extern int	optind, getopt();
  extern char	*optarg;

  char		*progname   = CDOWN;
  int		iListMode   = FALSE;
  int           error       = 0;
  char          *pszHost    = NULL;
  int           iPort       = CDDB_PORT;
  char          *cd_device  = NULL;

  int           opt;

  unsigned long discid;
  int tracks, i;
  char *bleh;

  /* parse environment and command line args */
  if ( (cd_device=getenv("CDTOOLDEV")) == NULL) 
    cd_device = strdup(CD_DEVICE);

  if ( (pszHost = getenv("CDDBHOST")) == NULL)
    pszHost = strdup(CDDB_HOST);

  while((opt = getopt(argc,argv,"0123456789?hlVvH:P:d:")) != EOF) {
      switch(opt) {    
      case '0':
      case '1':
      case '2':
      case '3':
      case '4':
      case '5':
      case '6':
      case '7':
      case '8':
      case '9':
	  cd_device=strdup(CD_DEVICE_BASE "?");
	  *(cd_device+sizeof(CD_DEVICE_BASE)-1)=opt;
	  break;

      case 'd':
	  cd_device = optarg;
	  break;
	  
	  /* print verison number and then standard HELP */
      case 'H':
	  pszHost = optarg;
	  break;

      case 'P':
	  iPort = atoi(optarg);
	  break;

      case 'l':
      	  iListMode = TRUE;
	  break;

      case 'v':
	  iVerbose = TRUE;
	  break;

      case 'V':
	  printf ("%s %s\n   ID=%s\n", progname, VERSIONSTRING, rcsid);
	  printf ("  Build:  %s %s\n",__DATE__,__TIME__);
	  
	  /* general options */
      case '?':
      case 'h':
      default:
	  error = 1;
	  break;
      }
  }

  /* if error, print help and exit */
  if (error == 1) {
      PrintHelp(progname);
      exit(1);
  }

  /* connect to the host, if we fail, print error and exit */
  sock = netconnect(progname, pszHost, iPort);
  bleh = getline();
  if (!signon(bleh)) { 
      fprintf (stderr, "%s:  error connecting to host %s\n", 
	       progname, pszHost);
      close(sock); 
      exit(3); 
  }
  
  if (iVerbose) fprintf(stderr, "Logging in\n");

  /* send connect string, get welcome msg */
  netprintf(sock, "cddb hello %s %s %s %s\n",
    getuname(), gethname(), progname, VERSIONSTRING);
  bleh = getline();

  if (!welcome(bleh)) { 
      fprintf (stderr, "%s:  bad welcome message \n", progname);
      close(sock); 
      exit(3); 
  }

  if (iVerbose) fprintf(stderr, "Querying\n");

  /* Listing available hosts or querying database? */
  if (iListMode) {
    netprintf(sock, "sites\n");
    parsesites();
  } else {
    /* open CD, read tracks, if fail, report error and exit */
    tracks = read_toc(cd_device);
    if (tracks < 0) {
        fprintf (stderr, "%s:  error reading cdrom %s\n",
	         progname, cd_device);
        exit(-1);
    }

    /* generate discid */
    discid = cddb_discid(tracks);

    /* do the query */
    netprintf(sock, "cddb query %08x %d", discid, tracks);

    /* TODO don't print this if there are no matches, or if None is
     * selected */
    printf("tracks %d", tracks);
    for (i = 0; i < tracks; i++) {
      netprintf(sock, " %d", cdtoc[i].frame);
      printf(" %d", cdtoc[i].frame);
    }
    netprintf(sock, " %d\n", (cdtoc[tracks].frame-cdtoc[0].frame)/75);
    printf(" %d\n", (cdtoc[tracks].frame)/75);
    query();
    netprintf(sock, "quit\n");
  }

  exit(0);
}