Annotation of src/usr.bin/cdio/cddb.c, Revision 1.4
1.4 ! espie 1: /* $OpenBSD: cddb.c,v 1.3 2002/04/18 22:07:04 espie Exp $ */
1.1 espie 2: /*
3: * Copyright (c) 2002 Marc Espie.
4: *
5: * Redistribution and use in source and binary forms, with or without
6: * modification, are permitted provided that the following conditions
7: * are met:
8: * 1. Redistributions of source code must retain the above copyright
9: * notice, this list of conditions and the following disclaimer.
10: * 2. Redistributions in binary form must reproduce the above copyright
11: * notice, this list of conditions and the following disclaimer in the
12: * documentation and/or other materials provided with the distribution.
13: *
14: * THIS SOFTWARE IS PROVIDED BY THE OPENBSD PROJECT AND CONTRIBUTORS
15: * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
16: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
17: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OPENBSD
18: * PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
20: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25: */
26:
27: #include <sys/types.h>
28: #include <sys/socket.h>
29: #include <netinet/in.h>
30: #include <sys/cdio.h>
31: #include <err.h>
32: #include <netdb.h>
33: #include <stdio.h>
34: #include <stdlib.h>
35: #include <string.h>
36: #include <unistd.h>
37: #include <vis.h>
38: #include "extern.h"
39:
1.3 espie 40: unsigned long cddb_sum(unsigned long);
41: void send_hello(FILE *);
42: void send_query(FILE *, int, struct cd_toc_entry *);
43: int further_query(FILE *, char *);
44: int connect_to(const char *, const char *);
45: int parse_connect_to(const char *, const char *);
46: char * get_line(FILE *);
1.1 espie 47: char * get_answer(FILE *);
1.3 espie 48: void verify_track_names(char **, int, struct cd_toc_entry *);
49: char * safe_copy(const char *);
1.1 espie 50:
51: unsigned long
52: cddb_sum(unsigned long v)
53: {
54: unsigned long sum = 0;
55:
56: while (v > 0) {
57: sum += v % 10;
58: v /= 10;
59: }
60: return (sum);
61: }
62:
1.3 espie 63: unsigned long
1.1 espie 64: cddb_discid(int n, struct cd_toc_entry *e)
65: {
66: unsigned long sum;
67: int i;
68:
69: sum = 0;
1.3 espie 70: for (i =0; i < n; i++)
1.1 espie 71: sum += cddb_sum(entry2time(e+i));
1.3 espie 72: return (((sum % 0xff) << 24) |
1.1 espie 73: ((entry2time(e+n) - entry2time(e)) << 8) | n);
74: }
75:
76: void
77: send_hello(FILE *cout)
78: {
79: char hostname[MAXHOSTNAMELEN];
80:
81: if (gethostname(hostname, sizeof(hostname)) == -1)
82: strcpy(hostname, "unknown");
1.3 espie 83: fprintf(cout, "CDDB HELLO %s %s cdio " VERSION "\r\n",
1.1 espie 84: getlogin(), hostname);
85: fflush(cout);
86: }
87:
1.3 espie 88: void
1.1 espie 89: send_query(FILE *f, int n, struct cd_toc_entry *e)
90: {
91: int i;
92:
93: fprintf(f, "cddb query %8lx %d", cddb_discid(n, e), n);
94: for (i = 0; i < n; i++)
95: fprintf(f, " %lu", entry2frames(e+i));
96: fprintf(f, " %lu\r\n", (entry2frames(e+n)-entry2frames(e)) /75);
97: }
98:
99: #define MAXSIZE 256
100: char copy_buffer[MAXSIZE];
101:
102: char *
103: safe_copy(const char *title)
104: {
105: strnvis(copy_buffer, title, MAXSIZE-1, VIS_TAB|VIS_NL);
106: return strdup(copy_buffer);
107: }
108:
109: int
110: further_query(FILE *cout, char *line)
111: {
112: char *key;
113: char *title;
114:
115: key = strchr(line, ' ');
116: if (!key)
117: return 0;
118: *key++ = 0;
119: title = strchr(key, ' ');
120: if (!title)
121: return 0;
122: *title++ = 0;
123: strnvis(copy_buffer, title, MAXSIZE-1, VIS_TAB|VIS_NL);
124: printf("%s", copy_buffer);
125: strnvis(copy_buffer, line, MAXSIZE-1, VIS_TAB|VIS_NL);
126: printf("(%s)\n", copy_buffer);
127: fprintf(cout, "CDDB READ %s %s\r\n", line, key);
128: fflush(cout);
129: return 1;
130: }
131:
132:
133: int
134: connect_to(const char *host, const char *serv)
135: {
136: int s = -1;
137: struct addrinfo hints, *res0 = NULL, *res;
138: int error;
139:
140: memset(&hints, 0, sizeof hints);
141: hints.ai_family = PF_UNSPEC;
142: hints.ai_socktype = SOCK_STREAM;
143:
144: error = getaddrinfo(host, serv, &hints, &res0);
145: if (error) {
146: warnx("%s", gai_strerror(error));
147: return -1;
148: }
149:
150: for (res = res0; res; res = res->ai_next) {
151: s = socket(res->ai_family, res->ai_socktype,
152: res->ai_protocol);
153: if (s == -1)
154: continue;
155: if (connect(s, res->ai_addr, res->ai_addrlen) == -1) {
156: close(s);
157: s = -1;
158: continue;
159: }
160: break;
161: }
162: if (s == -1)
163: warn("cddb");
164: freeaddrinfo(res0);
165: return s;
166: }
167:
168: int
169: parse_connect_to(const char *host_port, const char *port)
170: {
171: int s;
172: char *last, *host;
173:
174: host = (char *)host_port;
175:
176: last = strrchr(host_port, ':');
177: if (last != 0 && !(last != host && last[-1] == ':')) {
178: port = last + 1;
179: host = malloc(last - host_port + 1);
180: if (!host)
181: return -1;
182: memcpy(host, host_port, last-host_port);
183: host[last-host_port] = 0;
184: }
185: s = connect_to(host, port);
186: if (host != host_port)
187: free(host);
188: return s;
189: }
190:
191: char *
192: get_line(FILE *cin)
193: {
194: char *line;
195: size_t len;
196:
197: line = fgetln(cin, &len);
198: if (!line)
199: return NULL;
200: if (len == 0)
201: return NULL;
202: if (line[len-1] == '\n')
203: line[--len] = 0;
204: if (len != 0 && line[len-1] == '\r')
205: line[--len] = 0;
206: return line;
207: }
208:
209: char *
210: get_answer(FILE *cin)
211: {
212: char *line;
213:
214: line = get_line(cin);
215: if (*line != '2')
216: return NULL;
217: else
218: return line;
219: }
220:
221: void
222: verify_track_names(char **names, int n, struct cd_toc_entry *e)
223: {
224: int i;
225:
226: for (i = 0; i < n; i++) {
227: if (names[i] == 0)
228: names[i] = strdup(e->control & 4 ? "data" : "audio");
229: }
230: }
231:
1.3 espie 232: char **
1.1 espie 233: cddb(const char *host_port, int n, struct cd_toc_entry *e, char *arg)
234: {
235: int s = -1;
236: FILE *cin = NULL;
237: FILE *cout = NULL;
238: char *type;
239: char *line;
240: char **result = NULL;
241: int i;
242:
243: s = parse_connect_to(host_port, "cddb");
244: if (s == -1)
245: goto end;
246: cin = fdopen(s, "r");
247: if (!cin) {
248: warn("cddb: fdopen");
249: goto end;
250: }
251: cout = fdopen(s, "w");
252: s = -1;
253: if (!cout) {
254: warn("cddb: fdopen");
255: goto end;
256: }
257: line = get_answer(cin);
258: if (!line) {
259: warnx("cddb: won't talk to us");
260: goto end;
261: }
262:
263: send_hello(cout);
264: line = get_answer(cin);
265: if (!line) {
266: warnx("cddb: problem in hello");
267: goto end;
268: }
269:
270: send_query(cout, n, e);
271: fflush(cout);
272: line = get_answer(cin);
273: if (!line) {
274: warnx("cddb: problem in query");
275: goto end;
276: }
277: type = strchr(line, ' ');
278: if (!type)
279: goto end;
280: *type++ = 0;
281: /* no match or other issue */
282: if (strcmp(line, "202") == 0) {
283: printf("cddb: No match in database\n");
284: goto end;
285: }
286: if (strcmp(line, "211") == 0 || strcmp(line, "212") == 0) {
287: int number = atoi(arg);
288: if (number == 0) {
289: if (strcmp(line, "211") == 0)
290: printf("cddb: multiple matches\n");
291: else {
292: printf("cddb: inexact match\n");
293: number = 1;
294: }
295: }
296: if (number == 0) {
297: for (i = 1;; i++) {
298: line = get_line(cin);
299: if (strcmp(line, ".") == 0)
300: goto end;
301: printf("%d: %s\n", i, line);
302: }
303: } else {
304: int ok = 0;
305:
306: for (i = 1;; i++) {
307: line = get_line(cin);
308: if (!line)
309: break;
310: if (strcmp(line, ".") == 0)
311: break;
312: if (i == number)
313: ok = further_query(cout, line);
314: }
315: if (!ok)
316: goto end;
317: }
318: } else if (strcmp(line, "200") != 0 || !further_query(cout, type))
319: goto end;
320: result = malloc(sizeof(char *) * n+1);
321: if (!result)
322: goto end;
323: for (i = 0; i <= n; i++)
324: result[i] = NULL;
325: line = get_answer(cin);
326: if (!line)
327: goto end2;
328: for (;;) {
329: long k;
330: char *end;
331:
332: line = get_line(cin);
333: if (!line)
334: goto end2;
335: if (strcmp(line, ".") == 0)
336: goto end;
337: if (strncmp(line, "TTITLE", 6) != 0)
338: continue;
339: line += 6;
340: k = strtol(line, &end, 10);
341: if (*end++ != '=')
342: continue;
343: if (k >= n)
344: continue;
345: result[k] = safe_copy(end);
346: }
347: fprintf(cout, "QUIT\r\n");
348: verify_track_names(result, n, e);
349: goto end;
350: end2:
351: free(result);
352: result = NULL;
353: end:
354: if (cout)
1.2 deraadt 355: fclose(cout);
1.1 espie 356: if (cin)
1.2 deraadt 357: fclose(cin);
1.1 espie 358: if (s != -1)
1.2 deraadt 359: close(s);
1.1 espie 360: return result;
361: }
362:
363: void
364: free_names(char **names)
365: {
366: int i;
367:
368: for (i = 0; names[i]; i++)
369: free(names[i]);
370: free(names);
371: }