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
|
/*
* cddb implementation routines are contained in here.
*
* the disc id computation routines were taken from the cddb implementation
* document. it's a wonderful world we have. the internet rules. :)
*
* - Mark
*
*/
#include <netdb.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <dirent.h>
#include "gcd.h"
#define READ_WRITE 0x01
#define READ_ONLY 0x02
extern struct cdinfo thiscd;
extern char *prog_name, *prog_ver;
int code = 0; /* Return value for sending data to the server */
char cddb_msg[255]; /* Return message of server info */
int sock = 0; /* Descriptor for our socket */
int sock_mode = 0; /* Server read/write status */
FILE *sk; /* Stream descriptor for our socket */
FILE *ldb; /* Local database stream */
#define SEND(x) fprintf(sk, "%s\n", x); \
fflush(sk);
void parse_trails(char *ss)
{
int i;
for (i=0; i<strlen(ss); i++)
{
if (ss[i] == '\r' || ss[i] == '\n')
ss[i] = 0;
}
}
/**** START CALCULATION ROUTINES FOR DISC ID *****************************/
int cddb_sum(int n)
{
char buf[12],
*p;
int ret = 0;
/* For backward compatibility this algorithm must not change */
sprintf(buf, "%lu", (unsigned long) n);
for (p = buf; *p != '\0'; p++)
ret += (*p - '0');
return (ret);
}
unsigned long cddb_discid(int tot_trks)
{
int i,
t = 0,
n = 0;
struct trackinfo *cdtoc = thiscd.trk;
/* For backward compatibility this algorithm must not change */
for (i = 0; i < tot_trks; i++)
n += cddb_sum((cdtoc[i].min * 60) + cdtoc[i].sec);
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);
}
/**** END CALCULATION ROUTINES *******************************************/
/**** START GENERAL NETWORK CODE *****************************************/
void cddb_code()
{
char s[255];
code = 0;
if (fgets(s, 255, sk) == NULL)
return;
s[3] = 0;
code = atoi(s);
strcpy(cddb_msg, &s[4]);
}
FILE *get_entry(char *dir, int idx, unsigned int id, int tracks)
{
struct dirent *entry;
struct stat st;
FILE *fd;
DIR *dfd;
/* id + tracks */
sprintf(&dir[idx],"/%08x", id);
fd = fopen(dir, "r");
if (fd)
return fd;
fd = fopen(dir, "r");
if (fd)
return fd;
/* recurse subdirectories */
dir[idx] = 0;
dfd = opendir(dir);
dir[idx++] = '/';
while((entry = readdir(dfd)))
{
/* ignore dot-files (inc current and parent dirs) */
if (entry->d_name[0] == '.')
continue;
strcpy(&dir[idx], entry->d_name);
if(!stat(dir, &st) && S_ISDIR(st.st_mode))
{
fd = get_entry(dir, idx + strlen(&dir[idx]), id, tracks);
if (fd)
return fd;
}
}
closedir(dfd);
return 0;
}
int cddb_connect(char *host, int port)
{
char *dir, s[PATH_MAX+NAME_MAX+1];
struct sockaddr_in sin;
struct hostent *h;
DIR *d;
int idx;
/* Before connecting, let's check to make sure we don't already
have a match in the local CD database
*/
idx = 0;
dir = CDDB_DIR;
if (*dir == '~')
{
dir++;
strcpy(s, getenv("HOME"));
idx = strlen(s);
}
strcpy(&s[idx], dir);
if ((d=opendir(s)) == NULL)
mkdir(s, S_IRUSR|S_IWUSR|S_IXUSR);
else
closedir(d);
attrset(COLOR_PAIR(C_BLUE_HL)|A_BOLD);
idx = strlen(s);
ldb = get_entry(s, idx, (unsigned int) thiscd.cddb_id, thiscd.ntracks);
if (ldb) /* If the file exists, no need to login */
{
mvprintw(23, 1, "Reading CD info from cache...");
refresh();
code = 69;
cddb_readcdinfo(ldb);
fclose(ldb);
return(-1);
}
// printf("Connecting to %s: ",host);
mvprintw(23, 1, "Resolving host...");
refresh();
if ((h = gethostbyname(host)) == NULL)
return(0);
bcopy(h->h_addr, (char *) &sin.sin_addr, h->h_length);
sin.sin_family = h->h_addrtype;
sin.sin_port = htons(port);
mvprintw(23, 1, "Connecting to CDDB server...");
refresh();
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
return(0);
if (connect(sock, (struct sockaddr *) &sin, sizeof(sin)) < 0)
return(0);
sk = fdopen(sock, "r+");
if (sk == NULL)
{
close(sock);
return(0);
}
cddb_code();
if (code == 200)
sock_mode = READ_WRITE;
else {
if (code == 201)
sock_mode = READ_ONLY;
else {
printf("Connection refused after connect: %d\n",code);
fflush(stdout);
getchar();
return(0);
}
}
return(1);
}
void cddb_disconnect()
{
SEND("quit");
close(sock);
fclose(sk);
}
int cddb_login()
{
char *hostname, *logname, s[255];
mvprintw(23, 1, "%-40s", "Logging in...");
refresh();
hostname = getenv("HOSTNAME");
logname = getenv("LOGNAME");
if (hostname == NULL || logname == NULL)
{
default_track_info(1);
return(0);
}
sprintf(s,"cddb hello %s %s %s %s", logname, hostname, prog_name, prog_ver);
SEND(s);
cddb_code();
if (code == 200 || code == 402)
return(1);
else {
mvprintw(24, 1, "CDDB access denied (code %d).",code);
refresh();
getchar();
}
default_track_info(2);
return(0);
}
/**** END GENERAL NETWORK CODE *******************************************/
/**** START CDDB INTENSIVE ROUTINES **************************************/
/* Sends query to server -- this is the first thing to be done */
void cddb_query()
{
char s[255], s1[81], *ss;
int i;
mvprintw(23, 1, "%-40s", "Querying database...");
refresh();
sprintf(s,"cddb query %08x %d ",(unsigned int) thiscd.cddb_id, thiscd.ntracks);
for (i=0; i<thiscd.ntracks; i++)
{
sprintf(s1,"%d ", thiscd.trk[i].start);
strcat(s, s1);
}
sprintf(s1,"%d", thiscd.length);
strcat(s, s1);
SEND(s);
cddb_code();
switch(code)
{
case 200: /* Success, get the catagory ID */
ss = strchr(cddb_msg, 32);
*ss = 0;
strcpy(thiscd.catagory, cddb_msg);
break;
default:
default_track_info(3);
cddb_msg[strlen(cddb_msg)-1] = 0;
attrset(COLOR_PAIR(C_BLUE_HL));
mvprintw(24, 1, "(%02d): %s", code, cddb_msg);
refresh();
getchar();
return;
}
cddb_readcdinfo(sk);
}
/* Ask the server to send the entire CD information (track names, ect)
* This can ONLY be called directly after cddb_query(), since it uses
* the same return code from the server as cddb_query().
*/
void default_track_info(int i)
{
int t;
char s[255];
strcpy(thiscd.cdnames,"CD Artist / CD Name (Change Me!)");
for (t=0; t<thiscd.ntracks; t++)
{
sprintf(s, "Track %02d", t+1);
if ((thiscd.trk[t].songname = malloc(strlen(s)+1)) == NULL)
{
perror("allocate_trackname");
exit(0);
}
strcpy(thiscd.trk[t].songname, s);
}
}
void cddb_readcdinfo(FILE *desc)
{
char s[255], *ss;
int t;
FILE *f = NULL;
if (code != 69)
{
if (code == 200) // cddb_query was a success, request info
{
mvprintw(23, 1, "%-40s", "Downloading CD info...");
refresh();
sprintf(s,"cddb read %s %08x",thiscd.catagory, (unsigned int) thiscd.cddb_id);
SEND(s);
cddb_code();
if (code != 210)
{
printf("(%d): %s",code,cddb_msg);
default_track_info(4);
return;
}
} else
default_track_info(5);
}
if (ldb == NULL)
{
sprintf(s,"%s/.groovycd/%08x",getenv("HOME"),
(unsigned int) thiscd.cddb_id);
f = fopen(s,"w");
}
s[0] = 0;
while (strncmp(s,".", 1)!=0)
{
if (!fgets(s, 255, desc))
break;
if (f)
fputs(s, f);
if ((ss=strstr(s, "DTITLE")) != NULL)
{
ss += 7;
ss[strlen(ss)-2] = 0;
strcpy(thiscd.cdnames, ss);
ss = strchr(s,'/');
*ss = 0;
strcpy(thiscd.artist,&s[7]);
strcpy(thiscd.cdname,ss+2);
}
if ((ss=strstr(s, "TTITLE")) == NULL)
continue;
/* Yeah, yeah. Cheap hack, but it's guarenteed to work! :)
* The following just hacks the returned track name into the variable
*/
ss += 6;
t = atoi(ss);
if (t < 100)
ss += t < 10 ? 2 : 3;
else
ss += 4;
parse_trails(ss);
if (thiscd.trk[t].songname)
{
if ((thiscd.trk[t].songname = realloc(thiscd.trk[t].songname,
strlen(thiscd.trk[t].songname)+strlen(ss)+1)) != NULL)
strcat(thiscd.trk[t].songname, ss);
} else {
if ((thiscd.trk[t].songname = malloc(strlen(ss)+1)) == NULL)
{
perror("allocate_trackname");
exit(0);
}
strcpy(thiscd.trk[t].songname, ss);
}
}
if (f)
fclose(f);
}
|