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
|
/*
cddb.c
31.5.99 tn
stuff to access the cddb-database via the cddbp-protocol
standalone version
*/
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#if defined(linux) || defined(__CYGWIN32__)
#include <getopt.h>
#endif
#include <sys/time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <gtk/gtk.h>
#include <gdk/gdk.h>
#include "xcdrdata.h"
#include "xcdroast.h"
#include "main.h"
#ifndef INADDR_NONE
#define INADDR_NONE 0xFFFFFFFF
#endif
gchar hostname[MAXLINE];
gchar username[MAXLINE];
cddb_match_t **cddbmatch;
gint matchnr;
gint oldtnr;
/* extract the 3 digit code from a cddb-answer */
gint get_cddb_code(gchar *line) {
gchar tmp[MAXLINE];
if (line == NULL || strlen(line) < 3) {
return 0;
}
strncpy(tmp,line,3);
tmp[3] = '\0';
return(atoi(tmp));
}
/* connect to the cddb-server and return socket-fd:
return -1 on "hostname lookup failure"
-2 on "cant open stream socket"
-3 on "connection refused"
-4 on "no response from server"
-5 on "timeout while connect"
*/
gint connect_cddb_server(gchar *server, gint port) {
struct sockaddr_in serv_addr;
struct hostent *myhost;
gchar tmp[MAXLINE];
gint sockfd,n;
/* resolv ipnumber from hostname if neccessary */
if ((serv_addr.sin_addr.s_addr = inet_addr(server)) == INADDR_NONE) {
/* no valid ip-nummer, look up name */
if ((myhost = gethostbyname(server)) == NULL) {
return -1;
}
memcpy(&serv_addr.sin_addr, myhost->h_addr, myhost->h_length);
}
/* fill structure serv_addr */
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(port);
/* open tcp socket (internet stream socket) */
if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
return -2;
}
/* connect */
if (connect(sockfd, (struct sockaddr *) &serv_addr,
sizeof(serv_addr)) < 0) {
close(sockfd);
return -3;
}
/* read connect response */
n = read_line2(sockfd, tmp, MAXLINE, 1);
if (n == -2) {
/* timeout */
close(sockfd);
return -5;
}
if (n < 0) {
g_print("-11: Error: read error on socket when connecting\n");
fflush(stdout);
close(sockfd);
return -4;
}
return sockfd;
}
/* perform cddbp-handshaking */
/* return 0 on success, 1 on error */
gint cddb_handshake(gint sockfd) {
gchar tmp[MAXLINE];
gint n, code;
g_snprintf(tmp,MAXLINE,"cddb hello %s %s xcdroast %s",
username,hostname,XCDROAST_VERSION);
/* now send string */
if (writen(sockfd,tmp,strlen(tmp), 1) != strlen(tmp)) {
g_print("-10: Error: write error on socket when cddb handshaking\n");
fflush(stdout);
return 1;
}
/* read response */
n = read_line2(sockfd, tmp, MAXLINE, 1);
if (n < 0) {
g_print("-11: Error: read error on socket when cddb handshaking\n");
fflush(stdout);
return 1;
}
code = get_cddb_code(tmp);
switch(code) {
/* all ok */
case 200:
return 0;
/* handshake failed */
case 431:
return 1;
/* alread shook hands */
case 402:
return 0;
default:
return 1;
}
}
/* extract a match of the cddb-query command */
void extract_cddb_match(gchar *match, gint exact) {
gchar *p1;
/* allocate memory and free old */
if (cddbmatch[matchnr] != NULL) {
g_free(cddbmatch[matchnr]->categ);
g_free(cddbmatch[matchnr]->discid);
g_free(cddbmatch[matchnr]->dtitle);
g_free(cddbmatch[matchnr]);
}
cddbmatch[matchnr] = g_new0(cddb_match_t,1);
cddbmatch[matchnr]->exact = exact;
p1 = strtok(match," ");
cddbmatch[matchnr]->categ = g_strdup(p1);
p1 = strtok(NULL," ");
cddbmatch[matchnr]->discid = g_strdup(p1);
p1 = strtok(NULL,"");
cddbmatch[matchnr]->dtitle = g_strdup(p1);
matchnr++;
}
/* perform cddbp-query */
/* return 0 on success, 1 on error */
gint cddb_query(gint sockfd, gchar *querystring) {
gchar tmp[MAXLINE];
gint n, code;
matchnr = 0;
oldtnr = -1;
/* construct query */
strcpy(tmp,querystring);
/* now send string */
if (writen(sockfd,tmp,strlen(tmp), 1) != strlen(tmp)) {
g_print("-10: Error: write error on socket when cddb query\n");
fflush(stdout);
return 1;
}
/* read response */
n = read_line2(sockfd, tmp, MAXLINE, 1);
if (n < 0) {
g_print("-11: Error: read error on socket when cddb query\n");
fflush(stdout);
return 1;
}
code = get_cddb_code(tmp);
switch(code) {
/* exact match */
case 200:
extract_cddb_match(tmp+4,1);
return 0;
/* no match found */
case 202:
return 0;
/* Database entry is corrupt */
case 403:
return 0;
/* No handshake */
case 409:
return 1;
/* list of matches found */
case 210:
case 211: {
while (matchnr < MAXCDDB) {
n = read_line2(sockfd, tmp, MAXLINE, 1);
if (n < 0) {
g_print("-11: Error: read error on socket when cddb query\n");
fflush(stdout);
return 1;
}
if (tmp[0] == '.') {
/* done with list */
return 0;
}
if (code == 210) {
extract_cddb_match(tmp,1);
} else {
extract_cddb_match(tmp,0);
}
}
}
default:
return 1;
}
}
/* parse the received cddb-entry into info-structures */
/* return 0 on success, 1 on error */
gint parse_cddb_entry(gint sockfd) {
gchar tmp[MAXLINE];
gchar tmp2[MAXLINE];
gchar *p1;
gint n, tnr;
while (1) {
n = read_line2(sockfd, tmp, MAXLINE, 1);
if (n < 0) {
g_print("-11: Error: read error on socket when cddb read\n");
fflush(stdout);
return 1;
}
/* end marker found */
if (tmp[0] == '.') {
/* no more tracks, close old track */
g_print("\"\n");
fflush(stdout);
return 0;
}
if (strncmp(tmp,"DTITLE",6) == 0) {
p1 = strtok(tmp,"=");
p1 = strtok(NULL,"");
strcpy(tmp2,p1);
strip_string(tmp2);
g_print("07: \"%s\"\n", convert_escape(tmp2));
fflush(stdout);
continue;
}
if (strncmp(tmp,"TTITLE",6) == 0) {
strcpy(tmp2,tmp+6);
p1 = strtok(tmp2,"=");
tnr = atoi(p1);
/* got a new track? close line of previous one */
if (oldtnr != -1 && oldtnr != tnr) {
g_print("\"\n");
fflush(stdout);
}
p1 = strtok(NULL,"");
strcpy(tmp,p1);
strip_string(tmp);
if (oldtnr != -1 && oldtnr == tnr) {
/* continue old line */
g_print("%s", convert_escape(tmp));
} else {
/* new track line (to be continue) */
g_print("08: \"%s", convert_escape(tmp));
}
oldtnr = tnr;
continue;
}
}
}
/* perform cddbp-read */
/* return 0 on success, 1 on error */
gint cddb_read(gint sockfd, gint nr) {
gchar tmp[MAXLINE];
gint n, code;
if (cddbmatch[nr] == 0) {
/* no such match */
return 1;
}
/* construct query */
g_snprintf(tmp,MAXLINE,"cddb read %s %s",cddbmatch[nr]->categ,
cddbmatch[nr]->discid);
/* now send string */
if (writen(sockfd,tmp,strlen(tmp), 1) != strlen(tmp)) {
g_print("-10: Error: write error on socket when cddb read\n");
fflush(stdout);
return 1;
}
/* read response */
n = read_line2(sockfd, tmp, MAXLINE, 1);
if (n < 0) {
g_print("-11: Error: read error on socket when cddb read\n");
fflush(stdout);
return 1;
}
code = get_cddb_code(tmp);
switch(code) {
/* database entry follow */
case 210:
parse_cddb_entry(sockfd);
return 0;
/* no entry found */
case 401:
return 1;
/* server error */
case 402:
return 1;
/* Database entry is corrupt */
case 403:
return 1;
/* No handshake */
case 409:
return 1;
/* Access limit exceeded, explanation follows */
case 417: {
while (1) {
n = read_line2(sockfd, tmp, MAXLINE, 1);
if (n < 0) {
g_print("-11: Error: read error on socket when cddb query\n");
fflush(stdout);
return 1;
}
if (tmp[0] == '.') {
/* done with list */
return 1;
}
/* output error-message line of server */
g_print("-12: Server: %s\n",tmp);
fflush(stdout);
}
}
default:
return 1;
}
}
/* perform cddbp-quit */
/* return 0 on success, 1 on error */
gint cddb_quit(gint sockfd) {
gchar tmp[MAXLINE];
/* construct query */
strcpy(tmp,"quit");
/* now send string */
if (writen(sockfd,tmp,strlen(tmp), 1) != strlen(tmp)) {
g_print("-10: Error: write error on socket when cddb quit\n");
fflush(stdout);
return 1;
}
return 0;
}
/* close the socket when user canceled-out */
void close_cddb(sock) {
if (sock > 0) {
close(sock);
}
}
/* do the cddb-work and output info to keep user informed */
/* return 1 on trouble, 0 if all ok */
gint start_cddb_query(gint *sockpnt, gchar *host, gint port, gchar *querystring) {
gint i;
gint sock;
gchar tmptmp[MAXLINE];
/* connect */
g_print("00: Connecting to %s:%d\n",host,port);
fflush(stdout);
sock = connect_cddb_server(host,port);
*sockpnt = sock;
if (sock < 0) {
switch (sock) {
case -1:
/* lookup error */
g_print("-1: Error: Hostname lookup failure\n");
break;
case -2:
/* socket error */
g_print("-2: Error: Can't open stream socket\n");
break;
case -3:
/* connection refused */
g_print("-3: Error: Connection refused\n");
break;
case -4:
/* no response from server */
g_print("-4: Error: No response from server\n");
break;
case -5:
/* timeout */
g_print("-5: Error: No answer within timeout\n");
break;
}
fflush(stdout);
return 1;
}
/* connect was ok, now send handshake */
g_print("01: Connect ok: Sending handshake\n");
fflush(stdout);
if (cddb_handshake(sock) != 0) {
g_print("-6: Handshake failed - No valid CDDB-server?\n");
fflush(stdout);
return 1;
}
/* start query */
g_print("02: Sending CDDB-query\n");
fflush(stdout);
if (cddb_query(sock, querystring) != 0) {
g_print("-7: Error: CDDB-query failed\n");
fflush(stdout);
return 1;
}
/* now the global match-structure contains all hits */
if (matchnr == 0) {
/* no matches found */
g_print("03: No matches found - unknown CD\n");
fflush(stdout);
/* all the user can do now is to cancel */
return 1;
} else {
/* output number of matches */
g_print("%% %d\n",matchnr);
fflush(stdout);
/* what type of matches */
if (cddbmatch[0]->exact == 0) {
/* close */
g_print("04: Found %d close matches - Please select (enter number and press enter)\n", matchnr);
fflush(stdout);
} else {
/* exact */
g_print("05: Found %d exact matches - Please select (enter number and press enter)\n", matchnr);
fflush(stdout);
}
/* now add all matches to clist */
for (i = 0; i < matchnr; i++) {
strcpy(tmptmp,cddbmatch[i]->dtitle);
g_print("#%02d: \"%s\"\n", i,
convert_escape(tmptmp));
}
g_print("#--\n");
fflush(stdout);
}
/* so..now wait until the user select a match */
return 0;
}
/* user selected a match - fetch the corresponding data */
/* return 1 on trouble, 0 if all ok */
gint continue_cddb_query(gint sock, gint match) {
/* get data only when user manually not forbid it */
if (match != -1) {
g_print("06: Requesting data - Please wait\n");
fflush(stdout);
/* get the data */
if (cddb_read(sock, match) != 0) {
g_print("-8: Error: CDDB-read failed\n");
fflush(stdout);
return 1;
}
}
if (cddb_quit(sock) != 0) {
g_print("-9: Warning: CDDB-logout failed\n");
fflush(stdout);
return 1;
}
close_cddb(sock);
return 0;
}
void usage(gchar *cmd) {
g_print("Usage: %s [options] (Version: %s)\n",cmd, XCDROAST_VERSION);
g_print("Options:\n");
g_print("\t-s <cddb-server>\n");
g_print("\t-p <cddb-port>\n");
g_print("\t-u <id username>\n");
g_print("\t-h <id hostname>\n");
g_print("\t-m <match nr> get info for this match number\n\t (Do not wait for user input - set to -1 for match list only)\n");
g_print("\t-q <cddb-query-string>\n");
}
gint main(gint argc, gchar **argv) {
gint c;
gchar server[MAXLINE];
gchar selectbuffer[MAXLINE];
gchar querystring[MAXLINE];
gint port;
gint ret, match;
gint sock;
gint automatch;
/* do some defaults */
strcpy(server,"freedb.freedb.org");
port = 888;
strcpy(username,"undef");
strcpy(hostname,"localhost");
strcpy(querystring, "");
automatch = -2;
cddbmatch = g_new0(cddb_match_t *,MAXCDDB);
while ((c = getopt(argc,argv,"s:p:u:h:q:m:")) != EOF)
switch((gchar)c) {
case 's':
strncpy(server,optarg,MAXLINE);
break;
case 'u':
strncpy(username,optarg,MAXLINE);
break;
case 'h':
strncpy(hostname,optarg,MAXLINE);
break;
case 'q':
strncpy(querystring,optarg,MAXLINE);
break;
case 'p':
port = atoi(optarg);
break;
case 'm':
automatch = atoi(optarg);
break;
default:
usage(argv[0]);
exit(-1);
}
/* any parameters at all given? */
if (strcmp(querystring,"") == 0) {
usage(argv[0]);
exit(-1);
}
ret = start_cddb_query(&sock, server, port, querystring);
if (ret == 0) {
/* ok, now let the user select an match */
if (automatch == -2) {
read_line2(STDIN_FILENO, selectbuffer, MAXLINE, 0);
match = atoi(selectbuffer);
} else {
match = automatch;
}
ret = continue_cddb_query(sock, match);
if (ret != 0) {
/* some error */
exit(1);
}
}
return 0;
}
|