[2] | 1 | /*
|
---|
| 2 | * This file is (C) Chris Wohlgemuth 1999/2000
|
---|
| 3 | */
|
---|
| 4 | /*
|
---|
| 5 | * This program is free software; you can redistribute it and/or modify
|
---|
| 6 | * it under the terms of the GNU General Public License as published by
|
---|
| 7 | * the Free Software Foundation; either version 2, or (at your option)
|
---|
| 8 | * any later version.
|
---|
| 9 | *
|
---|
| 10 | * This program is distributed in the hope that it will be useful,
|
---|
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 13 | * GNU General Public License for more details.
|
---|
| 14 | *
|
---|
| 15 | * You should have received a copy of the GNU General Public License
|
---|
| 16 | * along with this program; see the file COPYING. If not, write to
|
---|
| 17 | * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
| 18 | */
|
---|
| 19 |
|
---|
| 20 | #include <os2.h>
|
---|
| 21 | #include <stdio.h> /* For stderr */
|
---|
| 22 | #include <stdlib.h> /* For atoi() */
|
---|
| 23 | #include <string.h>
|
---|
| 24 | #include <io.h>
|
---|
| 25 | #include <fcntl.h>
|
---|
[127] | 26 | //#include <sys/itypes.h>
|
---|
| 27 | #include <types.h>
|
---|
[2] | 28 | #include <sys/socket.h>
|
---|
| 29 | #include <netdb.h>
|
---|
| 30 | #include <errno.h>
|
---|
[127] | 31 | #include <nerrno.h>
|
---|
[2] | 32 | #include <netinet/in.h>
|
---|
| 33 |
|
---|
| 34 | #include "cddb.h"
|
---|
| 35 | #include "cddb.hh"
|
---|
| 36 |
|
---|
| 37 | #define CDDB_OK 0
|
---|
| 38 | #define CDDB_MORE 1
|
---|
| 39 | #define CDDB_ERROR -1
|
---|
| 40 |
|
---|
| 41 | #define verbose 2
|
---|
| 42 |
|
---|
| 43 | extern int h_errno;
|
---|
| 44 | extern int errno;
|
---|
| 45 | LONG extern CDDBDiscID(char * drive,CDDBINFO *cddbInfo);
|
---|
[127] | 46 | //unsigned short _Optlink _bswap(unsigned short s);
|
---|
[2] | 47 | extern int port;
|
---|
| 48 | extern char host[100];
|
---|
| 49 | extern char username[100];
|
---|
| 50 | extern char hostname[100];
|
---|
| 51 | extern char clientname[100];
|
---|
| 52 | extern char version[100];
|
---|
| 53 |
|
---|
| 54 | void extern printError(char* errorString);
|
---|
| 55 | void extern printErrorCR(char* errorString);
|
---|
| 56 | void extern printData(char* outString);
|
---|
| 57 |
|
---|
| 58 |
|
---|
| 59 | /*****************************************************/
|
---|
| 60 |
|
---|
| 61 | clsTrack::clsTrack(char * title,int trackNo)
|
---|
| 62 | {
|
---|
| 63 | strncpy(trackname,title,sizeof(trackname));
|
---|
| 64 | iTrackNo=trackNo;
|
---|
| 65 | nextTrack=0;
|
---|
| 66 | }
|
---|
| 67 | /*****************************************************/
|
---|
| 68 |
|
---|
| 69 |
|
---|
| 70 | /* If iFuz==0 then we have one exact match. If iFuz==1
|
---|
| 71 | we have one of n exact matches or a fuzzy match. */
|
---|
| 72 | cddb::cddb(char * chrTitle,char *chrArtist, char *category, int iFuz=0)
|
---|
| 73 | {
|
---|
| 74 | firstTrack=0;
|
---|
| 75 | nextCddb=0;
|
---|
| 76 | iFuzzy=iFuz;
|
---|
| 77 | strncpy(title,chrTitle,sizeof(title));
|
---|
| 78 | strncpy(artist,chrArtist,sizeof(artist));
|
---|
| 79 |
|
---|
| 80 | return;
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | cddb::~cddb()
|
---|
| 84 | {
|
---|
| 85 | clsTrack * tempTrack;
|
---|
| 86 |
|
---|
| 87 | while(firstTrack) {
|
---|
| 88 | tempTrack=firstTrack;
|
---|
| 89 | firstTrack=firstTrack->nextTrack;
|
---|
| 90 | delete(tempTrack);
|
---|
| 91 | }
|
---|
[104] | 92 |
|
---|
[2] | 93 | if(nextCddb)delete nextCddb;
|
---|
| 94 |
|
---|
| 95 | return;
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | /* Method to link different matches together */
|
---|
| 99 | void cddb::linkCddb(cddb * Cddb)
|
---|
| 100 | {
|
---|
| 101 | /* The new cddb goes to the end of the list */
|
---|
| 102 | if(nextCddb){
|
---|
| 103 | nextCddb->linkCddb(Cddb);
|
---|
| 104 | return;
|
---|
| 105 | }
|
---|
| 106 | nextCddb=Cddb;
|
---|
| 107 | return;
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | int cddb::newTrack(char * trackTitle, int trackNo)
|
---|
| 111 | {
|
---|
| 112 | clsTrack * tempTrack;
|
---|
| 113 |
|
---|
| 114 | tempTrack=new clsTrack(trackTitle, trackNo);
|
---|
| 115 | if(!tempTrack)return 1;
|
---|
| 116 | addTrack(tempTrack);
|
---|
| 117 | return 0;
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | void cddb::addTrack(clsTrack * track)
|
---|
| 121 | {
|
---|
| 122 | clsTrack * tempTrack;
|
---|
| 123 |
|
---|
| 124 | if(!firstTrack) {
|
---|
| 125 | firstTrack=track;
|
---|
| 126 | return;
|
---|
| 127 | }
|
---|
| 128 |
|
---|
| 129 | /* Check if this is another part of title information spawned over several lines */
|
---|
| 130 | tempTrack=cddbFindTrack(track->iTrackNo);
|
---|
| 131 | if(tempTrack) {
|
---|
| 132 | /* Yes, add the rest of the title to that what we already have */
|
---|
[104] | 133 | strncat(tempTrack->trackname, track->trackname, sizeof(tempTrack->trackname)-strlen(tempTrack->trackname)-1);
|
---|
[2] | 134 | delete(track);
|
---|
| 135 | return;
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 | tempTrack=firstTrack;
|
---|
| 139 | while(tempTrack->nextTrack) {
|
---|
[104] | 140 | tempTrack=tempTrack->nextTrack;
|
---|
[2] | 141 | }
|
---|
| 142 | tempTrack->nextTrack=track;
|
---|
[104] | 143 | return;
|
---|
[2] | 144 | }
|
---|
| 145 |
|
---|
| 146 | /* This method finds the track with the specified num (starting with 0) */
|
---|
| 147 | clsTrack* cddb::cddbFindTrack(int iTrack)
|
---|
| 148 | {
|
---|
| 149 | clsTrack * tempTrack;
|
---|
| 150 |
|
---|
| 151 | tempTrack=firstTrack;
|
---|
| 152 | while(tempTrack) {
|
---|
| 153 | if(tempTrack->iTrackNo==iTrack)
|
---|
| 154 | return tempTrack;
|
---|
| 155 |
|
---|
[104] | 156 | tempTrack=tempTrack->nextTrack;
|
---|
[2] | 157 | }
|
---|
| 158 |
|
---|
| 159 | return NULL;
|
---|
| 160 | }
|
---|
| 161 |
|
---|
| 162 | clsTrack * cddb::cddbNextTrack(clsTrack * track)
|
---|
| 163 | {
|
---|
| 164 | if(!track)return 0;
|
---|
| 165 | return track->nextTrack;
|
---|
| 166 | }
|
---|
| 167 |
|
---|
| 168 | /**********************************************************/
|
---|
| 169 | /* Some helper functions
|
---|
| 170 | */
|
---|
| 171 |
|
---|
| 172 | #if 0
|
---|
[104] | 173 | /* now defined in printHelper.cpp
|
---|
[2] | 174 | or cddbhelper.cpp. By choosing the right module to link to, it's possible
|
---|
| 175 | to redirect the messages without changing this module. Used for different
|
---|
| 176 | output with querycddb.exe and pmcddb.exe. */
|
---|
| 177 | void printError(char* errorString)
|
---|
| 178 | {
|
---|
| 179 | fprintf(stderr,"%s",errorString);
|
---|
| 180 | }
|
---|
| 181 |
|
---|
| 182 | void printErrorCR(char* errorString)
|
---|
| 183 | {
|
---|
| 184 | fprintf(stderr,"%s\n",errorString);
|
---|
| 185 | }
|
---|
| 186 |
|
---|
| 187 |
|
---|
| 188 | void printData(char* outString)
|
---|
| 189 | {
|
---|
| 190 | int fileHandle;
|
---|
| 191 | int rc;
|
---|
| 192 |
|
---|
| 193 | fileHandle=open("cddb.out",O_APPEND|O_TEXT|O_WRONLY);
|
---|
| 194 | if(fileHandle==-1) {
|
---|
[104] | 195 | printError((char *)"Cannot open cddb.out");
|
---|
[2] | 196 | return;
|
---|
| 197 | }
|
---|
| 198 | rc= write(fileHandle,outString,strlen(outString));
|
---|
| 199 | // write(fileHandle,"\n",sizeof("\n"));
|
---|
| 200 | close(fileHandle);
|
---|
| 201 | return;
|
---|
| 202 | }
|
---|
| 203 | #endif
|
---|
| 204 |
|
---|
| 205 | /**********************************************************/
|
---|
| 206 | /* CDDB Stuff */
|
---|
| 207 |
|
---|
| 208 | int readResponse(int s, char * buffer,int size)
|
---|
[104] | 209 | {
|
---|
[2] | 210 | int a;
|
---|
| 211 | int rc;
|
---|
[104] | 212 |
|
---|
[2] | 213 | rc=1;
|
---|
| 214 | for(a=0;a<size&&rc>0;a++) {
|
---|
| 215 | rc=read(s,&buffer[a],1);
|
---|
| 216 | if(buffer[a]=='\n') {
|
---|
| 217 | buffer[a]=0;
|
---|
| 218 | return CDDB_MORE;
|
---|
| 219 | }
|
---|
| 220 | if(rc==-1)
|
---|
[104] | 221 | return CDDB_ERROR;
|
---|
[2] | 222 | }
|
---|
[104] | 223 |
|
---|
[2] | 224 | return CDDB_ERROR;
|
---|
| 225 | }
|
---|
| 226 |
|
---|
| 227 | /* Returns the cddb code. */
|
---|
| 228 | int get_cddb_code( char * chrResponse)
|
---|
| 229 | {
|
---|
| 230 | return atoi(chrResponse);
|
---|
| 231 | }
|
---|
| 232 |
|
---|
| 233 | /* Check error codes coming from the server*/
|
---|
| 234 | int check_cddb_code(char * chrResponse)
|
---|
| 235 | {
|
---|
| 236 | switch (chrResponse[0])
|
---|
| 237 | {
|
---|
| 238 | case '2': return CDDB_OK;
|
---|
| 239 | case '3': return CDDB_MORE;
|
---|
| 240 | case '4':
|
---|
[104] | 241 | printError((char *)"CDDB server error");
|
---|
[2] | 242 | return CDDB_ERROR;
|
---|
| 243 | case '5':
|
---|
[104] | 244 | printError((char *)"CDDB client error");
|
---|
[2] | 245 | return CDDB_ERROR;
|
---|
| 246 | default:
|
---|
[104] | 247 | printError((char *)"Unknown error");
|
---|
[2] | 248 | break;
|
---|
| 249 | }
|
---|
| 250 | return CDDB_ERROR;
|
---|
| 251 | }
|
---|
| 252 |
|
---|
| 253 | /* Performing the handshake procedure with the CDDB server */
|
---|
| 254 | /* s: socket handle */
|
---|
| 255 | int cddb_handshaking(int s)
|
---|
| 256 | {
|
---|
| 257 | char buffer[512*2];
|
---|
| 258 | int rc;
|
---|
| 259 |
|
---|
| 260 | /* Handshaking */
|
---|
| 261 | if(verbose)
|
---|
[104] | 262 | printError((char *)"Handshaking... ");
|
---|
[2] | 263 | sprintf(buffer,"cddb hello %s %s %s %s\n",username,hostname,clientname,version);
|
---|
[104] | 264 | if(verbose==2)
|
---|
[2] | 265 | printError(buffer);
|
---|
| 266 | rc=write(s,buffer,strlen(buffer));
|
---|
| 267 | if(rc==-1) {
|
---|
[104] | 268 | printError((char *)"Handshaking: cannot write to CDDB server");
|
---|
[2] | 269 | return -1;
|
---|
| 270 | }
|
---|
| 271 | if(verbose)
|
---|
[104] | 272 | printError((char *)"done\n");
|
---|
[2] | 273 | if(verbose==2)
|
---|
[104] | 274 | printError((char *)"Response: ");
|
---|
| 275 |
|
---|
[2] | 276 | rc=readResponse(s,buffer,sizeof(buffer));
|
---|
| 277 | if(rc==CDDB_ERROR) {
|
---|
[104] | 278 | printError((char *)"Handshaking: cannot get response");
|
---|
[2] | 279 | return -1;
|
---|
| 280 | }
|
---|
[104] | 281 | if(verbose==2)
|
---|
[2] | 282 | printErrorCR(buffer);
|
---|
[104] | 283 |
|
---|
[2] | 284 | if(check_cddb_code(buffer)==CDDB_ERROR)
|
---|
| 285 | return -1;
|
---|
| 286 |
|
---|
| 287 | return 0;
|
---|
| 288 | }
|
---|
| 289 |
|
---|
| 290 | /* s: socket handle */
|
---|
| 291 | int cddb_setting_proto(int s)
|
---|
| 292 | {
|
---|
| 293 | char buffer[512];
|
---|
| 294 | int rc;
|
---|
| 295 | int iProtLevel;
|
---|
| 296 |
|
---|
| 297 | iProtLevel=4;/* Startlevel */
|
---|
| 298 | /* Setting protocol level */
|
---|
| 299 | do {
|
---|
| 300 | if(verbose)
|
---|
[104] | 301 | printError((char *)"\nSetting protocol level... ");
|
---|
[2] | 302 | sprintf(buffer,"proto %d\n",iProtLevel);
|
---|
| 303 | printError(buffer);
|
---|
| 304 | rc=write(s,buffer,strlen(buffer));
|
---|
| 305 | if(rc==-1) {
|
---|
[104] | 306 | printError((char *)"Protocol: cannot write to CDDB server");
|
---|
[2] | 307 | return -1;
|
---|
| 308 | }
|
---|
| 309 | if(verbose)
|
---|
[104] | 310 | printError((char *)"done\n");
|
---|
[2] | 311 | if(verbose==2)
|
---|
[104] | 312 | printError((char *)"Response: ");
|
---|
| 313 |
|
---|
[2] | 314 | rc=readResponse(s,buffer,sizeof(buffer));
|
---|
| 315 | if(rc==CDDB_ERROR) {
|
---|
[104] | 316 | printError((char *)"Set protocol: cannot get response");
|
---|
[2] | 317 | return -1;
|
---|
| 318 | }
|
---|
| 319 | if(verbose==2)
|
---|
| 320 | printErrorCR(buffer);
|
---|
[104] | 321 |
|
---|
[2] | 322 | /* Check cddb code */
|
---|
| 323 | if(check_cddb_code(buffer)==CDDB_ERROR) {
|
---|
| 324 | if(get_cddb_code(buffer)!=501)
|
---|
| 325 | return -1;/* Unknown error */
|
---|
| 326 | }
|
---|
| 327 | else
|
---|
| 328 | break;
|
---|
| 329 | /* protocol not supported. Decrement level and try again. */
|
---|
[104] | 330 | iProtLevel--;
|
---|
[2] | 331 | }while(iProtLevel>0);
|
---|
[104] | 332 |
|
---|
[2] | 333 | return 0;
|
---|
| 334 | }
|
---|
| 335 |
|
---|
| 336 | /* This is called if the CDDB database has got several matches for our
|
---|
| 337 | discid. We take all matches and decide later, which to use. */
|
---|
| 338 | cddb * query_get_all_matches(int s)
|
---|
| 339 | {
|
---|
| 340 | cddb * root=0;
|
---|
| 341 | cddb * Cddb=0;
|
---|
| 342 | int rc;
|
---|
| 343 | char buffer[512];
|
---|
| 344 |
|
---|
| 345 | do {
|
---|
| 346 | rc=readResponse(s,buffer,sizeof(buffer)); /* Get next line */
|
---|
[104] | 347 |
|
---|
[2] | 348 | if(rc==CDDB_ERROR)
|
---|
| 349 | return root; /* return what we have so far */
|
---|
| 350 |
|
---|
| 351 | if(buffer[0]=='.') /* End of data */
|
---|
| 352 | return root; /* return what we have */
|
---|
| 353 |
|
---|
| 354 | /* A new cddb instance for every match */
|
---|
[104] | 355 | Cddb=new cddb((char *)"",(char *)"",(char *)"",1); /* One fuzzy match or one of several matches */
|
---|
[2] | 356 | /* Get the data for this match */
|
---|
| 357 | sscanf(buffer, " %20s %x %100[^/\r\n] / %100[^\r\n]", Cddb->category,&Cddb->discid,Cddb->artist,Cddb->title);
|
---|
| 358 | /* Link the cddb instance into the list */
|
---|
| 359 | if(!root)
|
---|
| 360 | root=Cddb;
|
---|
| 361 | else
|
---|
| 362 | root->linkCddb(Cddb);
|
---|
[104] | 363 |
|
---|
[127] | 364 | }while(1);
|
---|
[2] | 365 |
|
---|
| 366 | }
|
---|
| 367 |
|
---|
| 368 | /* Query some data */
|
---|
| 369 | /* With the data given we later query the
|
---|
| 370 | tracknames */
|
---|
| 371 | cddb * cddb_query(int s, CDDBINFO *cddbInfo)
|
---|
| 372 | {
|
---|
| 373 | char buffer[512*2]={0};
|
---|
| 374 | char track[20]={0};
|
---|
| 375 | char title[100]={0};
|
---|
| 376 | char artist[100]={0};
|
---|
| 377 | char categ[20]={0};
|
---|
| 378 | int rc;
|
---|
| 379 | int code;
|
---|
| 380 | int a;
|
---|
| 381 | cddb * Cddb;
|
---|
| 382 | cddb * Cddb2;
|
---|
| 383 | char * chrPtr;
|
---|
| 384 |
|
---|
| 385 | /* Sending query */
|
---|
| 386 | if(verbose)
|
---|
[104] | 387 | printError((char *)"Sending query cmd... ");
|
---|
[2] | 388 | /* Build cmd-line */
|
---|
| 389 | sprintf(buffer,"cddb query %08x %d",cddbInfo->discid,cddbInfo->numTracks);
|
---|
| 390 | for(a=0;a<cddbInfo->numTracks;a++) {
|
---|
[127] | 391 | rc=sprintf(track,/*sizeof(track),*/" %d",cddbInfo->startSector[a]);
|
---|
[2] | 392 | if(rc==EOF||rc>=sizeof(track)) {
|
---|
[104] | 393 | printError((char *)"Query: buffer overrun while formatting track offsets.\nPlease report this bug.\n");
|
---|
[2] | 394 | return 0;
|
---|
| 395 | }
|
---|
| 396 | if(sizeof(buffer)-strlen(buffer) <= strlen(track)) {
|
---|
[104] | 397 | printError((char *)"Query: buffer overrun while adding track offsets to command.\nPlease report this bug.\n");
|
---|
[2] | 398 | return 0;
|
---|
| 399 | }
|
---|
| 400 | strncat(buffer,track, sizeof(buffer)-strlen(buffer));
|
---|
| 401 | }
|
---|
| 402 |
|
---|
[127] | 403 | rc=sprintf(track,/*sizeof(track),*/ " %d\n",cddbInfo->discLength);
|
---|
[2] | 404 | if(rc==EOF||rc>=sizeof(track)) {
|
---|
[104] | 405 | printError((char *)"Query: buffer overrun while formatting disclength.\nPlease report this bug.\n");
|
---|
[2] | 406 | return 0;
|
---|
| 407 | }
|
---|
| 408 | if(sizeof(buffer)-strlen(buffer) <= strlen(track)) {
|
---|
[104] | 409 | printError((char *)"Query: buffer overrun while adding disclength to command.\nPlease report this bug.\n");
|
---|
[2] | 410 | return 0;
|
---|
| 411 | }
|
---|
| 412 | strncat(buffer,track, sizeof(buffer)-strlen(buffer));
|
---|
| 413 |
|
---|
| 414 |
|
---|
| 415 | if(verbose==2)
|
---|
| 416 | printErrorCR(buffer);
|
---|
| 417 | rc=write(s,buffer,strlen(buffer));
|
---|
| 418 | if(rc==-1) {
|
---|
[104] | 419 | printError((char *)"Query: cannot write to CDDB server");
|
---|
[2] | 420 | return 0;
|
---|
| 421 | }
|
---|
| 422 | if(verbose)
|
---|
[104] | 423 | printError((char *)" done\n");
|
---|
[2] | 424 | if(verbose==2)
|
---|
[104] | 425 | printError((char *)"Response: ");
|
---|
| 426 |
|
---|
[2] | 427 | rc=readResponse(s,buffer,sizeof(buffer));
|
---|
| 428 | //rc=read(s,buffer,sizeof(buffer)-1);
|
---|
| 429 | if(rc==CDDB_ERROR) {
|
---|
[104] | 430 | printError((char *)"Query: cannot get response");
|
---|
[2] | 431 | return 0;
|
---|
| 432 | }
|
---|
| 433 | if(verbose==2) {
|
---|
| 434 | printErrorCR(buffer);
|
---|
| 435 | }
|
---|
| 436 | if(check_cddb_code(buffer)==CDDB_ERROR)
|
---|
| 437 | return 0;
|
---|
| 438 | code=get_cddb_code(buffer);
|
---|
| 439 |
|
---|
| 440 | /* Check output */
|
---|
| 441 | switch(code)
|
---|
| 442 | {
|
---|
| 443 | case 200: /* exact match */
|
---|
[104] | 444 | Cddb=new cddb((char *)"",(char *)"",(char *)"");
|
---|
[2] | 445 |
|
---|
| 446 | sscanf(buffer+4, "%20s %x %100[^/\r\n] / %100[^\r\n]", Cddb->category,&Cddb->discid,Cddb->artist,Cddb->title);
|
---|
| 447 |
|
---|
| 448 | return Cddb;
|
---|
| 449 | break;
|
---|
| 450 | case 210: /* several exact matches */
|
---|
| 451 | case 211: /* several inexact matches */
|
---|
| 452 | return query_get_all_matches(s); /* if there's an error this call returns 0 */
|
---|
| 453 | break;
|
---|
| 454 | case 202:
|
---|
| 455 | default:
|
---|
| 456 | /* We shouldn't reach this */
|
---|
| 457 | return 0;
|
---|
| 458 | }
|
---|
| 459 | /* We shouldn't reach this */
|
---|
| 460 | return 0;
|
---|
[104] | 461 | }
|
---|
[2] | 462 |
|
---|
| 463 | /* Reads the CDDB banner from the server after connecting */
|
---|
| 464 | /* We don't really need it so it is only shown on stderr */
|
---|
| 465 | int cddb_banner(int s)
|
---|
| 466 | {
|
---|
| 467 | char buffer[512];
|
---|
| 468 | int rc;
|
---|
| 469 |
|
---|
| 470 | /* Get CDDB-Banner */
|
---|
| 471 | if(verbose)
|
---|
| 472 | printErrorCR("Querying CDDB-Banner...");
|
---|
| 473 | rc=readResponse(s,buffer,sizeof(buffer));
|
---|
| 474 | if(rc==CDDB_ERROR) {
|
---|
[104] | 475 | printError((char *)"Cannot read CDDB-Banner");
|
---|
[2] | 476 | return -1;
|
---|
| 477 | }
|
---|
| 478 | if(verbose==2)
|
---|
| 479 | printErrorCR(buffer);
|
---|
| 480 |
|
---|
| 481 | if(check_cddb_code(buffer)==CDDB_ERROR)
|
---|
| 482 | return -1;
|
---|
| 483 | return 0;
|
---|
| 484 | }
|
---|
| 485 |
|
---|
| 486 | /* Ask for the information about our CD. Parse it and
|
---|
| 487 | save it in the data file. */
|
---|
| 488 | int read_and_parse(int s,cddb * Cddb)
|
---|
| 489 | {
|
---|
| 490 | int rc;
|
---|
| 491 | char buffer[512]={0};
|
---|
| 492 | char key[20]={0};
|
---|
| 493 | char trackName[100]={0};
|
---|
| 494 | char fullTrackName[100]={0};
|
---|
| 495 | int iTrack=0;
|
---|
| 496 | int iTemp=0;
|
---|
[104] | 497 |
|
---|
[2] | 498 | do{
|
---|
| 499 | rc=readResponse(s,buffer,sizeof(buffer));
|
---|
| 500 | if(rc==CDDB_ERROR) {
|
---|
[104] | 501 | printError((char *)"Read request: cannot get response");
|
---|
[2] | 502 | return CDDB_ERROR;
|
---|
| 503 | }
|
---|
| 504 | /* Print the data to the outfile. The terminating '.' isn't printed out so the datafile
|
---|
| 505 | is usable for a cddb commit. */
|
---|
| 506 | if(buffer[0]!='.')
|
---|
| 507 | printData(buffer);
|
---|
[104] | 508 |
|
---|
[2] | 509 | if(buffer[0]=='#') /* This is a comment */
|
---|
| 510 | { }
|
---|
| 511 | // printf("Found Comment: %s\n",buffer);
|
---|
| 512 | else{
|
---|
| 513 | if(buffer[0]=='.') /* End of data */
|
---|
| 514 | return CDDB_OK; /* Done */
|
---|
[104] | 515 |
|
---|
[2] | 516 | //printf("Found data: %s\n",buffer);
|
---|
| 517 | sscanf(buffer,"%20[^0-9=]",key);
|
---|
| 518 | /* We put track names into a linked list in the cddb instance */
|
---|
| 519 | if(!strcmp(strupr(key),"TTITLE")) {
|
---|
| 520 | iTrack=0;
|
---|
| 521 | /* We have a track title */
|
---|
| 522 | sscanf(buffer,"%20[^0-9]%d=%100[^\n\r]",key,&iTrack,trackName);
|
---|
| 523 | Cddb->newTrack(trackName,iTrack); /* New track entry in cddb instance */
|
---|
| 524 | }
|
---|
| 525 | }
|
---|
| 526 | }while(rc==CDDB_MORE);
|
---|
| 527 |
|
---|
[104] | 528 | return CDDB_OK;
|
---|
[2] | 529 | }
|
---|
| 530 |
|
---|
[40] | 531 | int cddb_read(int s,cddb * Cddb)
|
---|
[2] | 532 | {
|
---|
| 533 | char buffer[512];
|
---|
| 534 | int rc;
|
---|
| 535 |
|
---|
| 536 | /* Sending read */
|
---|
| 537 |
|
---|
| 538 | /* Build cmd-line */
|
---|
[104] | 539 | sprintf(buffer,"cddb read %s %08x\n",Cddb->category,Cddb->discid);
|
---|
| 540 | if(verbose)
|
---|
| 541 | printError((char *)"\nSending read cmd... ");
|
---|
[2] | 542 | if(verbose==2)
|
---|
| 543 | printError(buffer);
|
---|
[104] | 544 |
|
---|
[2] | 545 | rc=write(s,buffer,strlen(buffer));
|
---|
| 546 | if(rc==-1) {
|
---|
[104] | 547 | printError((char *)"Read request: cannot write to CDDB server");
|
---|
[2] | 548 | return -1;
|
---|
| 549 | }
|
---|
[104] | 550 | if(verbose)
|
---|
| 551 | printError((char *)"done\n");
|
---|
[2] | 552 | if(verbose==2)
|
---|
[104] | 553 | printError((char *)"Response: ");
|
---|
| 554 |
|
---|
[2] | 555 | rc=readResponse(s,buffer,sizeof(buffer));
|
---|
| 556 | if(rc==CDDB_ERROR) {
|
---|
[104] | 557 | printError((char *)"Read request: cannot get response");
|
---|
[2] | 558 | return -1;
|
---|
| 559 | }
|
---|
[104] | 560 | if(verbose==2)
|
---|
[2] | 561 | printError(buffer);
|
---|
| 562 | if(check_cddb_code(buffer)==CDDB_ERROR)
|
---|
| 563 | return CDDB_ERROR;
|
---|
| 564 |
|
---|
[104] | 565 | /* Get the data */
|
---|
[2] | 566 | read_and_parse(s,Cddb);
|
---|
[104] | 567 | return CDDB_OK;
|
---|
[2] | 568 | }
|
---|
| 569 |
|
---|
| 570 | /****************************************************/
|
---|
| 571 |
|
---|
| 572 |
|
---|
| 573 | int cddbConnectToHost(int * skt) {
|
---|
| 574 |
|
---|
| 575 | struct hostent *he;
|
---|
| 576 | struct sockaddr_in sa_in;
|
---|
| 577 | char buffer[512];
|
---|
| 578 | int s;
|
---|
| 579 |
|
---|
| 580 | /* Creating socket */
|
---|
| 581 | if(verbose)
|
---|
[104] | 582 | printError((char *)"Creating socket... ");
|
---|
[2] | 583 | s=socket(AF_INET,SOCK_STREAM,0);
|
---|
| 584 | if(s==-1) {
|
---|
[104] | 585 | printError((char *)"Cannot create socket!");
|
---|
[2] | 586 | return -1;
|
---|
| 587 | }
|
---|
| 588 | *skt=s;
|
---|
| 589 | if(verbose)
|
---|
[104] | 590 | printError((char *)"Socket created\n");
|
---|
[2] | 591 |
|
---|
| 592 | /* Querying host */
|
---|
| 593 | if(verbose)
|
---|
[104] | 594 | printError((char *)"Querying host... ");
|
---|
[2] | 595 | he = gethostbyname(host);
|
---|
| 596 | if(he==NULL) {
|
---|
[104] | 597 | printError((char *)"Cannot get host address.\ngethostbyname() returned error: ");
|
---|
[2] | 598 | switch (h_errno)
|
---|
| 599 | {
|
---|
| 600 | case HOST_NOT_FOUND:
|
---|
[104] | 601 | printError((char *)"Host not found\n");
|
---|
[2] | 602 | break;
|
---|
| 603 | case TRY_AGAIN:
|
---|
[104] | 604 | printError((char *)"Temporary error. Try again later.\n");
|
---|
[2] | 605 | break;
|
---|
| 606 | case NO_DATA:
|
---|
[104] | 607 | printError((char *)"Name valid but name server has no associated address.\n");
|
---|
[2] | 608 | break;
|
---|
| 609 | case NO_RECOVERY:
|
---|
[104] | 610 | printError((char *)"Unrecoverable error.\n");
|
---|
[2] | 611 | break;
|
---|
| 612 | default:
|
---|
[104] | 613 | printError((char *)"Unknown error\n");
|
---|
[2] | 614 | }
|
---|
| 615 | /* Close the socket */
|
---|
| 616 | close(s);
|
---|
| 617 | return -1;
|
---|
[104] | 618 | }
|
---|
[2] | 619 |
|
---|
| 620 | if(verbose)
|
---|
[104] | 621 | printError((char *)"Host found.\n");
|
---|
[2] | 622 |
|
---|
| 623 |
|
---|
| 624 | /* Connecting to host */
|
---|
| 625 | if(verbose)
|
---|
[104] | 626 | printError((char *)"Connecting to host... ");
|
---|
[2] | 627 | memset(&sa_in,0, sizeof(struct sockaddr_in));
|
---|
| 628 |
|
---|
| 629 | sa_in.sin_family=AF_INET;
|
---|
| 630 | sa_in.sin_port=htons((unsigned short)port);
|
---|
| 631 | memcpy(&sa_in.sin_addr, he->h_addr, he->h_length);
|
---|
| 632 |
|
---|
| 633 | if(connect(s,(struct sockaddr*)&sa_in,sizeof(struct sockaddr_in))==-1) {
|
---|
| 634 | sprintf(buffer,"errno: %d\n",errno);
|
---|
| 635 | printError(buffer);
|
---|
| 636 | switch (errno)
|
---|
| 637 | {
|
---|
| 638 | case EBADF:
|
---|
[104] | 639 | printError((char *)"No valid socket descriptor");
|
---|
[2] | 640 | break;
|
---|
| 641 | case EISCONN:
|
---|
[104] | 642 | printError((char *)"The socket is already connected\n");
|
---|
[2] | 643 | break;
|
---|
| 644 | case ETIMEDOUT:
|
---|
[104] | 645 | printError((char *)"No connection. Timed out.\n");
|
---|
[2] | 646 | break;
|
---|
| 647 | case ECONNREFUSED:
|
---|
[104] | 648 | printError((char *)"Connection refused by host.\n");
|
---|
[2] | 649 | break;
|
---|
| 650 | case EADDRNOTAVAIL:
|
---|
[104] | 651 | printError((char *)"Address not avaiable.\n");
|
---|
[2] | 652 | break;
|
---|
| 653 |
|
---|
| 654 | default:
|
---|
[104] | 655 | printError((char *)"General failure: Cannot connect to host\n");
|
---|
[2] | 656 | }
|
---|
| 657 | close(s);
|
---|
| 658 | return -1;
|
---|
| 659 | }
|
---|
| 660 | if(verbose)
|
---|
[104] | 661 | printError((char *)"connected.\n");
|
---|
[2] | 662 | return 0;
|
---|
| 663 | }
|
---|
| 664 |
|
---|
| 665 |
|
---|
| 666 |
|
---|
| 667 |
|
---|
| 668 |
|
---|
| 669 |
|
---|
| 670 |
|
---|
| 671 |
|
---|
| 672 |
|
---|
| 673 |
|
---|
| 674 |
|
---|