summaryrefslogtreecommitdiffstats
path: root/kscd/libwm/cddb.c
blob: e972ef0d73160659143afce83d7f7600c038d974 (plain)
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
/*
 * $Id$
 *
 * This file is part of WorkMan, the civilized CD player library
 * (c) 1991-1997 by Steven Grimm (original author)
 * (c) by Dirk F�rsterling (current 'author' = maintainer)
 * The maintainer can be contacted by his e-mail address:
 * [email protected] 
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the Free
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 *
 * establish connection to cddb server and get data
 * socket stuff gotten from gnu port of the finger command
 *
 * provided by Sven Oliver Moll
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/signal.h>
#include <ctype.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>

#include "include/wm_config.h"
#include "include/wm_struct.h"
#include "include/wm_cdinfo.h"
#include "include/wm_helpers.h"
#include "include/wm_cddb.h"

struct wm_cddb cddb;

extern struct wm_cdinfo thiscd;
static int cur_cddb_protocol;
static char *cur_cddb_server;
static char *cur_cddb_mail_adress;
static char *cur_cddb_path_to_cgi;
static char *cur_cddb_proxy_server;

/* local prototypes */
int cddb_sum(int);
char *string_split(char *line, char delim);
void string_makehello(char *line, char delim);
int connect_open(void);
void connect_close(void);
void connect_getline(char *line);
void connect_read_entry(void);
void cddbp_send(const char *line);
void cddbp_read(char *category, unsigned int id);
void http_send(char* line);
void http_read(char *category, unsigned int id);
void cddb_request(void);
/* local prototypes END */

static int Socket;
static FILE *Connection;

/*
 *
 */
void
cddb_cur2struct(void)
{
	cddb.protocol = cur_cddb_protocol;
	strcpy(cddb.cddb_server,  cur_cddb_server);
	strcpy(cddb.mail_adress,  cur_cddb_mail_adress);
	strcpy(cddb.path_to_cgi,  cur_cddb_path_to_cgi);
	strcpy(cddb.proxy_server, cur_cddb_proxy_server);
} /* cddb_cur2struct() */

/*
 *
 */
void
cddb_struct2cur(void)
{
	cur_cddb_protocol = cddb.protocol;
	cur_cddb_server =       (cddb.cddb_server);
	cur_cddb_mail_adress =  (cddb.mail_adress);
	cur_cddb_path_to_cgi =  (cddb.path_to_cgi);
	cur_cddb_proxy_server = (cddb.proxy_server);
} /* cddb_struct2cur() */


/*
 * Subroutine from cddb_discid
 */
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);
} /* cddb_sum() */


/*
 * Calculate the discid of a CD according to cddb
 */
unsigned long
cddb_discid(void)
{
	int	i,
		t,
		n = 0;

	/* For backward compatibility this algorithm must not change */
	for (i = 0; i < thiscd.ntracks; i++) {
	    
		n += cddb_sum(thiscd.trk[i].start / 75);
	/* 
	 * Just for demonstration (See below)
	 * 
	 *	t += (thiscd.trk[i+1].start / 75) -
	 *	     (thiscd.trk[i  ].start / 75);
	 */	    
	}

	/*
	 * Mathematics can be fun. Example: How to reduce a full loop to
	 * a simple statement. The discid algorhythm is so half-hearted
	 * developed that it doesn't even use the full 32bit range.
	 * But it seems to be always this way: The bad standards will be
	 * accepted, the good ones turned down.
         * Boy, you pulled out the /75. This is not correct here, because
         * this calculation needs the integer division for both .start
         * fields.
         */

        t = (thiscd.trk[thiscd.ntracks].start / 75)
          - (thiscd.trk[0].start / 75);
	return ((n % 0xff) << 24 | t << 8 | thiscd.ntracks);
} /* cddb_discid() */

/*
 * Split a string into two components according to the first occurrence of
 * the delimiter.
 */
char *
string_split(char *line, char delim)
{
	char *p1;

	for (p1=line;*p1;p1++)
	{
		if(*p1 == delim)
		{
			*p1 = 0;
			return ++p1;
		}
	}
	return (NULL);
} /* string_split() */

/*
 * Generate the hello string according to the cddb protocol
 * delimiter is either ' ' (cddbp) or '+' (http)
 */
void
string_makehello(char *line,char delim)
{
	char mail[84],*host;
	
	strcpy(mail,cddb.mail_adress);
	host=string_split(mail,'@');
	
	sprintf(line,"%shello%c%s%c%s%c%s%c%s",
		delim == ' ' ? "cddb " : "&",
		delim == ' ' ? ' ' : '=',
		mail,delim,
		host,delim,
		WORKMAN_NAME,delim,
		WORKMAN_VERSION);
} /* string_makehello() */

/*
 * Open the TCP connection to the cddb/proxy server
 */
int
connect_open(void)
{
	char *host;
	struct hostent *hp;
	struct sockaddr_in soc_in;
	int port;
	
	if(cddb.protocol == 3) /* http proxy */
	  host = wm_strdup(cddb.proxy_server);
	else
	  host = wm_strdup(cddb.cddb_server);
	/*	
	 * t=string_split(host,':'); 
	 */
	port=atoi(string_split(host,':'));
	if(!port)
	  port=8880;

#ifndef NDEBUG
	printf("%s:%d\n",host,port);
#endif
	hp  =gethostbyname(host);
  
	if (hp == NULL)
	{
		static struct hostent def;
		static struct in_addr defaddr;
		static char *alist[1];
		static char namebuf[128];
		int inet_addr();
		
		defaddr.s_addr = inet_addr(host);
		if ((int) defaddr.s_addr == -1) 
		{
#ifndef NDEBUG
			printf("unknown host: %s\n", host);
#endif
			return (-1);
		}
		strcpy(namebuf, host);
		def.h_name = namebuf;
		def.h_addr_list = alist, def.h_addr = (char *)&defaddr;
		def.h_length = sizeof (struct in_addr);
		def.h_addrtype = AF_INET;
		def.h_aliases = 0;
		hp = &def;
	}
	soc_in.sin_family = hp->h_addrtype;
	bcopy(hp->h_addr, (char *)&soc_in.sin_addr, hp->h_length);
	soc_in.sin_port = htons(port);
	Socket = socket(hp->h_addrtype, SOCK_STREAM, 0);
	if (Socket < 0) 
	{ 
		perror("socket");
		return (-1);
	}
	fflush(stdout);
	if (connect(Socket, (struct sockaddr *)&soc_in, sizeof (soc_in)) < 0) 
	{   
		perror("connect");
		close(Socket);
		return (-1);
	}
	
	Connection = fdopen(Socket, "r");
	return (0);
} /* connect_open() */


/*
 * Close the connection
 */
void
connect_close(void)
{	   
	(void)fclose(Connection);
	close(Socket);
} /* connect_close() */

/*
 * Get a line from the connection with CR and LF stripped
 */
void
connect_getline(char *line)
{
	char c;
    
	while ((c = getc(Connection)) != '\n')
	{
		*line = c;
		if ((c != '\r') && (c != (char)0xff))
		  line++;
	}
	*line=0;
} /* connect_getline() */

/*
 * Read the CD data from the server and place them into the cd struct
 */
void
connect_read_entry(void)
{
	char type;
	int trknr;
	
	char *t,*t2,tempbuf[2000];
	
	while(strcmp(tempbuf,"."))
	{
		connect_getline(tempbuf);
		
		t=string_split(tempbuf,'=');
		if(t != NULL)
		{
			type=tempbuf[0];
			
			if(strncmp("TITLE",tempbuf+1,5))
			  continue;
			
			if('D' == type)
			{
				/*
				 * Annahme: "Interpret / Titel" ist falsch.
				 * Daher: NULL-String erwarten.
                                 */
				t2=string_split(t,'/');
				if(t2 == NULL)
					t2 = t;
				if(*t2 == ' ')
				  t2++;
				strncpy(cd->cdname,t2,sizeof(cd->cdname)-1);
                                cd->cdname[sizeof(cd->cdname)-1]='\0';
				
				for(t2=t;*t2;t2++)
				{
					if((*t2 == ' ') && (*(t2+1) == 0))
					  *t2=0;
				}
				strncpy(cd->artist,t,sizeof(cd->artist)-1);
                                cd->artist[sizeof(cd->artist)-1]='\0';
			}
			
			if('T' == type)
			{
				trknr=atoi(tempbuf+6);
				/*
				 * printf("Track %d:%s\n",trknr,t);
				 */
				wm_strmcpy(&cd->trk[trknr].songname,t);
			}
			/*
			 * fprintf(stderr, "%s %s\n",tempbuf,t);
			 */
		}
	}
} /* connect_read_entry() */

/*
 * Send a command to the server using cddbp
 */
void
cddbp_send(const char *line)
{
	write(Socket, line, strlen(line));
	write(Socket, "\n", 1);
} /* cddbp_send() */

/*
 * Send the "read from cddb" command to the server using cddbp
 */
void
cddbp_read(char *category, unsigned int id)
{
	char tempbuf[84];
	sprintf(tempbuf, "cddb read %s %08x", category, id);
	cddbp_send(tempbuf);
} /* cddbp_read() */

/*
 * Send a command to the server using http
 */
void
http_send(char* line)
{
	char tempbuf[2000];
	
	write(Socket, "GET ", 4);
#ifndef NDEBUG
	printf("GET ");
#endif
	if(cddb.protocol == 3)
	{
		write(Socket, "http://", 7);
		write(Socket, cddb.cddb_server, strlen(cddb.cddb_server));
#ifndef NDEBUG
		printf("http://%s",cddb.cddb_server);
#endif
	}
	write(Socket, cddb.path_to_cgi, strlen(cddb.path_to_cgi));
	write(Socket, "?cmd=" ,5);
	write(Socket, line, strlen(line));
#ifndef NDEBUG
	printf("%s?cmd=%s",cddb.path_to_cgi,line);
#endif	
    string_makehello(tempbuf,'+');
	write(Socket, tempbuf, strlen(tempbuf));
#ifndef NDEBUG
	printf("%s",tempbuf);
#endif	
    write(Socket, "&proto=1 HTTP/1.0\n\n", 19);
#ifndef NDEBUG
	printf("&proto=1 HTTP/1.0\n");
#endif
	do
	  connect_getline(tempbuf);
	while(strcmp(tempbuf,""));
} /* http_send() */

/*
 * Send the "read from cddb" command to the server using http
 */
void
http_read(char *category, unsigned int id)
{
	char tempbuf[84];
	sprintf(tempbuf, "cddb+read+%s+%08x", category, id);
	http_send(tempbuf);
} /* http_read() */

/*
 * The main routine called from the ui
 */
void
cddb_request(void)
{
	int i;
	char tempbuf[2000];
	extern int cur_ntracks;
	
	int status;
	char category[21];
	unsigned int id;
	
	strcpy(cddb.cddb_server,"localhost:888");
	strcpy(cddb.mail_adress,"[email protected]");
	/*
	 * cddb.protocol = 1;
	 */
	wipe_cdinfo();
	
	switch(cddb.protocol)
	{
	 case 1: /* cddbp */
#ifndef NDEBUG
		printf("USING CDDBP\n");
		printf("open\n");
#endif
		connect_open();
		connect_getline(tempbuf);
#ifndef NDEBUG
		printf("[%s]\n",tempbuf);
#endif        
		/*
		 * if(atoi(tempbuf) == 201) return;
		 */

		/*
		 * strcpy(tempbuf,"cddb hello svolli bigfoot.com Eierkratzer eins");
		 */
		string_makehello(tempbuf,' ');
#ifndef NDEBUG
		fprintf(stderr, "%s\n", tempbuf);
#endif
		cddbp_send(tempbuf);
		connect_getline(tempbuf);
#ifndef NDEBUG
		printf("[%s]\n",tempbuf);
		printf("query\n");
#endif
		sprintf(tempbuf, "cddb query %08x %d",thiscd.cddbid,thiscd.ntracks);
		for (i = 0; i < cur_ntracks; i++)
		  if (thiscd.trk[i].section < 2)
		    sprintf(tempbuf + strlen(tempbuf), " %d",
			    thiscd.trk[i].start);
		sprintf(tempbuf + strlen(tempbuf), " %d\n", thiscd.length);
#ifndef NDEBUG
		printf(">%s<\n",tempbuf);
#endif
		cddbp_send(tempbuf);
		connect_getline(tempbuf);
#ifndef NDEBUG
		printf("[%s]\n",tempbuf);
#endif
		status=atoi(tempbuf);
		/*
		 * fprintf(stderr, "status:%d\n",status);
		 * fprintf(stderr,"category:%s\n",category);
		 * fprintf(stderr,"id:%s\n",id);
		 */
		if(status == 200) /* Exact match */
		{
			sscanf(tempbuf,"%d %20s %08x",&status,category,&id);
			cddbp_read(category,id);
			connect_read_entry();
		}
		
		if(status == 211) /* Unexact match, multiple possible
				   * Hack: always use first. */
		{
			connect_getline(tempbuf);
			sscanf(tempbuf,"%20s %08x",category,&id);
			while(strcmp(tempbuf,"."))
			  connect_getline(tempbuf);
			cddbp_read(category,id);
			connect_read_entry();
		}
		
		cddbp_send("quit");
		connect_close();
#ifndef NDEBUG
		printf("close\n");
#endif
		break;
	 case 2: /* http */
	 case 3: /* http proxy */
#ifndef NDEBUG
		printf("USING HTTP%s\n",
		       (cddb.protocol == 3) ? " WITH PROXY" : "");
		printf("query\n");
#endif
		sprintf(tempbuf, "cddb+query+%08x+%d",thiscd.cddbid,thiscd.ntracks);
		for (i = 0; i < cur_ntracks; i++)
		  if (thiscd.trk[i].section < 2)
		    sprintf(tempbuf + strlen(tempbuf), "+%d",
			    thiscd.trk[i].start);
		sprintf(tempbuf + strlen(tempbuf), "+%d", thiscd.length);
#ifndef NDEBUG
		printf(">%s<\n",tempbuf);
#endif
		connect_open();
		http_send(tempbuf);
		connect_getline(tempbuf);
#ifndef NDEBUG
		printf("[%s]\n",tempbuf);
#endif
		status=atoi(tempbuf);
		/*
		 * fprintf(stderr, "status:%d\n",status);
		 * fprintf(stderr, "category:%s\n",category);
		 * fprintf(stderr, "id:%s\n",id);
		 */

		if(status == 200) /* Exact match */
		{
			connect_close();
			connect_open();
			sscanf(tempbuf,"%d %20s %08x",&status,category,&id);
			http_read(category,id);
			connect_read_entry();
		}
		
		if(status == 211) /* Unexact match, multiple possible
				   * Hack: always use first. */
		{
			connect_getline(tempbuf);
			sscanf(tempbuf,"%20s %08x",category,&id);
			while(strcmp(tempbuf,"."))
			  connect_getline(tempbuf);
			connect_close();
			connect_open();
			http_read(category,id);
			connect_read_entry();
		}
		/* moved close above break */
		connect_close();
		break;
	 default: /* off */
		break;
	}
} /* cddb_request() */