Mercurial > audlegacy
annotate Plugins/Input/mpg123/http.c @ 990:05de825c2765 trunk
[svn] Use correct format string; gsize != long unsigned int.
author | chainsaw |
---|---|
date | Sun, 30 Apr 2006 16:32:42 -0700 |
parents | a7b53e6a71e0 |
children | 03dbf5494d40 |
rev | line source |
---|---|
61 | 1 /* BMP - Cross-platform multimedia player |
2 * Copyright (C) 2003-2004 BMP development team. | |
3 * | |
4 * Based on XMMS: | |
5 * Copyright (C) 1998-2003 XMMS development team. | |
6 * | |
7 * This program is free software; you can redistribute it and/or modify | |
8 * it under the terms of the GNU General Public License as published by | |
9 * the Free Software Foundation; either version 2 of the License, or | |
10 * (at your option) any later version. | |
11 * | |
12 * This program is distributed in the hope that it will be useful, | |
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 * GNU General Public License for more details. | |
16 * | |
17 * You should have received a copy of the GNU General Public License | |
18 * along with this program; if not, write to the Free Software | |
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
20 */ | |
21 | |
22 #include <glib.h> | |
23 #include <glib/gi18n.h> | |
24 #include <gtk/gtk.h> | |
25 #include <stdlib.h> | |
26 #include <string.h> | |
27 #include <stdio.h> | |
28 | |
29 #include <fcntl.h> | |
30 #include <unistd.h> | |
31 #include <errno.h> | |
32 #include <sys/types.h> | |
33 #include <sys/socket.h> | |
34 #include <sys/time.h> | |
35 #include <netinet/in.h> | |
36 #include <arpa/inet.h> | |
37 #include <netdb.h> | |
38 | |
39 #include <libaudacious/util.h> | |
40 | |
41 #include "mpg123.h" | |
42 | |
43 | |
44 #define min(x,y) ((x)<(y)?(x):(y)) | |
45 #define min3(x,y,z) (min(x,y)<(z)?min(x,y):(z)) | |
46 #define min4(x,y,z,w) (min3(x,y,z)<(w)?min3(x,y,z):(w)) | |
47 | |
48 static gchar *icy_name = NULL; | |
49 static gint icy_metaint = 0; | |
50 | |
51 #undef DEBUG_UDP | |
52 | |
53 /* Static udp channel functions */ | |
54 static gint udp_establish_listener(gint * sock); | |
55 static gint udp_check_for_data(gint sock); | |
56 | |
57 extern gint mpg123_bitrate, mpg123_frequency, mpg123_stereo; | |
58 extern gboolean mpg123_stereo; | |
59 | |
60 static gboolean prebuffering, going, eof = FALSE; | |
701
d539e5c5f730
[svn] Fixes of the remaining GCC 4.1 warnings from external contributor Diego "Flameeyes" Petteno (Gentoo).
chainsaw
parents:
377
diff
changeset
|
61 static gint sock; |
d539e5c5f730
[svn] Fixes of the remaining GCC 4.1 warnings from external contributor Diego "Flameeyes" Petteno (Gentoo).
chainsaw
parents:
377
diff
changeset
|
62 static gsize rd_index, wr_index, buffer_length, prebuffer_length; |
61 | 63 static guint64 buffer_read = 0; |
64 static gchar *buffer; | |
65 static GThread *thread; | |
66 static GtkWidget *error_dialog = NULL; | |
67 | |
68 static VFSFile *output_file = NULL; | |
69 | |
70 #define BASE64_LENGTH(len) (4 * (((len) + 2) / 3)) | |
71 | |
72 /* Encode the string S of length LENGTH to base64 format and place it | |
73 to STORE. STORE will be 0-terminated, and must point to a writable | |
74 buffer of at least 1+BASE64_LENGTH(length) bytes. */ | |
75 static void | |
76 base64_encode(const gchar * s, gchar * store, gint length) | |
77 { | |
78 /* Conversion table. */ | |
79 static gchar tbl[64] = { | |
80 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', | |
81 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', | |
82 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', | |
83 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', | |
84 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', | |
85 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', | |
86 'w', 'x', 'y', 'z', '0', '1', '2', '3', | |
87 '4', '5', '6', '7', '8', '9', '+', '/' | |
88 }; | |
89 gint i; | |
90 guchar *p = (guchar *) store; | |
91 | |
92 /* Transform the 3x8 bits to 4x6 bits, as required by base64. */ | |
93 for (i = 0; i < length; i += 3) { | |
94 *p++ = tbl[s[0] >> 2]; | |
95 *p++ = tbl[((s[0] & 3) << 4) + (s[1] >> 4)]; | |
96 *p++ = tbl[((s[1] & 0xf) << 2) + (s[2] >> 6)]; | |
97 *p++ = tbl[s[2] & 0x3f]; | |
98 s += 3; | |
99 } | |
100 /* Pad the result if necessary... */ | |
101 if (i == length + 1) | |
102 *(p - 1) = '='; | |
103 else if (i == length + 2) | |
104 *(p - 1) = *(p - 2) = '='; | |
105 /* ...and zero-terminate it. */ | |
106 *p = '\0'; | |
107 } | |
108 | |
109 /* Create the authentication header contents for the `Basic' scheme. | |
110 This is done by encoding the string `USER:PASS' in base64 and | |
111 prepending `HEADER: Basic ' to it. */ | |
112 static gchar * | |
113 basic_authentication_encode(const gchar * user, | |
114 const gchar * passwd, const gchar * header) | |
115 { | |
116 gchar *t1, *t2, *res; | |
117 gint len1 = strlen(user) + 1 + strlen(passwd); | |
118 gint len2 = BASE64_LENGTH(len1); | |
119 | |
120 t1 = g_strdup_printf("%s:%s", user, passwd); | |
121 t2 = g_malloc0(len2 + 1); | |
122 base64_encode(t1, t2, len1); | |
123 res = g_strdup_printf("%s: Basic %s\r\n", header, t2); | |
124 g_free(t2); | |
125 g_free(t1); | |
126 | |
127 return res; | |
128 } | |
129 | |
354
e2775c9b8b13
[svn] very rudementary http stream detection support for mpg123-clone.
nenolod
parents:
198
diff
changeset
|
130 void |
61 | 131 parse_url(const gchar * url, gchar ** user, gchar ** pass, |
132 gchar ** host, gint * port, gchar ** filename) | |
133 { | |
134 gchar *h, *p, *pt, *f, *temp, *ptr; | |
135 | |
136 temp = g_strdup(url); | |
137 ptr = temp; | |
138 | |
139 if (!strncasecmp("http://", ptr, 7)) | |
140 ptr += 7; | |
141 h = strchr(ptr, '@'); | |
142 f = strchr(ptr, '/'); | |
143 if (h != NULL && (!f || h < f)) { | |
144 *h = '\0'; | |
145 p = strchr(ptr, ':'); | |
146 if (p != NULL && p < h) { | |
147 *p = '\0'; | |
148 p++; | |
149 *pass = g_strdup(p); | |
150 } | |
151 else | |
152 *pass = NULL; | |
153 *user = g_strdup(ptr); | |
154 h++; | |
155 ptr = h; | |
156 } | |
157 else { | |
158 *user = NULL; | |
159 *pass = NULL; | |
160 h = ptr; | |
161 } | |
162 pt = strchr(ptr, ':'); | |
163 if (pt != NULL && (f == NULL || pt < f)) { | |
164 *pt = '\0'; | |
165 *port = atoi(pt + 1); | |
166 } | |
167 else { | |
168 if (f) | |
169 *f = '\0'; | |
170 *port = 80; | |
171 } | |
172 *host = g_strdup(h); | |
173 | |
174 if (f) | |
175 *filename = g_strdup(f + 1); | |
176 else | |
177 *filename = NULL; | |
178 g_free(temp); | |
179 } | |
180 | |
181 void | |
182 mpg123_http_close(void) | |
183 { | |
184 going = FALSE; | |
185 | |
186 g_thread_join(thread); | |
187 g_free(icy_name); | |
188 icy_name = NULL; | |
189 } | |
190 | |
191 | |
701
d539e5c5f730
[svn] Fixes of the remaining GCC 4.1 warnings from external contributor Diego "Flameeyes" Petteno (Gentoo).
chainsaw
parents:
377
diff
changeset
|
192 static gsize |
61 | 193 http_used(void) |
194 { | |
195 if (wr_index >= rd_index) | |
196 return wr_index - rd_index; | |
197 return buffer_length - (rd_index - wr_index); | |
198 } | |
199 | |
701
d539e5c5f730
[svn] Fixes of the remaining GCC 4.1 warnings from external contributor Diego "Flameeyes" Petteno (Gentoo).
chainsaw
parents:
377
diff
changeset
|
200 static gsize |
61 | 201 http_free(void) |
202 { | |
203 if (rd_index > wr_index) | |
204 return (rd_index - wr_index) - 1; | |
205 return (buffer_length - (wr_index - rd_index)) - 1; | |
206 } | |
207 | |
208 static void | |
701
d539e5c5f730
[svn] Fixes of the remaining GCC 4.1 warnings from external contributor Diego "Flameeyes" Petteno (Gentoo).
chainsaw
parents:
377
diff
changeset
|
209 http_wait_for_data(gsize bytes) |
61 | 210 { |
211 while ((prebuffering || http_used() < bytes) && !eof && going | |
212 && mpg123_info->going) | |
213 xmms_usleep(10000); | |
214 } | |
215 | |
216 static void | |
217 show_error_message(gchar * error) | |
218 { | |
219 if (!error_dialog) { | |
220 GDK_THREADS_ENTER(); | |
221 error_dialog = xmms_show_message(_("Error"), error, _("Ok"), FALSE, | |
222 NULL, NULL); | |
223 g_signal_connect(G_OBJECT(error_dialog), | |
224 "destroy", | |
225 G_CALLBACK(gtk_widget_destroyed), &error_dialog); | |
226 GDK_THREADS_LEAVE(); | |
227 } | |
228 } | |
229 | |
230 int | |
701
d539e5c5f730
[svn] Fixes of the remaining GCC 4.1 warnings from external contributor Diego "Flameeyes" Petteno (Gentoo).
chainsaw
parents:
377
diff
changeset
|
231 mpg123_http_read(gpointer data, gsize length) |
61 | 232 { |
701
d539e5c5f730
[svn] Fixes of the remaining GCC 4.1 warnings from external contributor Diego "Flameeyes" Petteno (Gentoo).
chainsaw
parents:
377
diff
changeset
|
233 gsize len, cnt, off = 0, meta_len, meta_off = 0, i; |
61 | 234 gchar *meta_data, **tags; |
235 | |
236 http_wait_for_data(length); | |
237 | |
238 if (!going && !mpg123_info->going) | |
239 return 0; | |
240 len = min(http_used(), length); | |
241 | |
242 while (len && http_used()) { | |
243 if ((icy_metaint > 0) && (buffer_read % icy_metaint) == 0 && | |
244 (buffer_read > 0)) { | |
245 meta_len = *((guchar *) buffer + rd_index) * 16; | |
246 rd_index = (rd_index + 1) % buffer_length; | |
247 if (meta_len > 0) { | |
248 http_wait_for_data(meta_len); | |
249 meta_data = g_malloc0(meta_len); | |
250 if (http_used() >= meta_len) { | |
251 while (meta_len) { | |
252 cnt = min(meta_len, buffer_length - rd_index); | |
253 memcpy(meta_data + meta_off, buffer + rd_index, cnt); | |
254 rd_index = (rd_index + cnt) % buffer_length; | |
255 meta_len -= cnt; | |
256 meta_off += cnt; | |
257 } | |
258 tags = g_strsplit(meta_data, "';", 0); | |
259 | |
260 for (i = 0; tags[i]; i++) { | |
261 if (!strncasecmp(tags[i], "StreamTitle=", 12)) { | |
262 gchar *temp = tags[i] + 13; | |
263 gchar *title = | |
264 g_strdup_printf("%s (%s)", temp, icy_name); | |
265 mpg123_ip.set_info(title, -1, | |
266 mpg123_bitrate * 1000, | |
267 mpg123_frequency, | |
268 mpg123_stereo); | |
269 g_free(title); | |
270 } | |
271 | |
272 } | |
273 g_strfreev(tags); | |
274 | |
275 } | |
276 g_free(meta_data); | |
277 } | |
278 if (!http_used()) | |
279 http_wait_for_data(length - off); | |
280 cnt = min3(len, buffer_length - rd_index, http_used()); | |
281 } | |
282 else if (icy_metaint > 0) | |
283 cnt = | |
284 min4(len, buffer_length - rd_index, http_used(), | |
285 icy_metaint - (buffer_read % icy_metaint)); | |
286 else | |
287 cnt = min3(len, buffer_length - rd_index, http_used()); | |
288 if (output_file) | |
289 vfs_fwrite(buffer + rd_index, 1, cnt, output_file); | |
290 | |
291 memcpy((gchar *) data + off, buffer + rd_index, cnt); | |
292 rd_index = (rd_index + cnt) % buffer_length; | |
293 buffer_read += cnt; | |
294 len -= cnt; | |
295 off += cnt; | |
296 } | |
297 return off; | |
298 } | |
299 | |
300 static gboolean | |
301 http_check_for_data(void) | |
302 { | |
303 fd_set set; | |
304 struct timeval tv; | |
305 gint ret; | |
306 | |
307 tv.tv_sec = 0; | |
308 tv.tv_usec = 20000; | |
309 FD_ZERO(&set); | |
310 FD_SET(sock, &set); | |
311 ret = select(sock + 1, &set, NULL, NULL, &tv); | |
312 if (ret > 0) | |
313 return TRUE; | |
314 return FALSE; | |
315 } | |
316 | |
317 gint | |
318 mpg123_http_read_line(gchar * buf, gint size) | |
319 { | |
320 gint i = 0; | |
321 | |
322 while (going && i < size - 1) { | |
323 if (http_check_for_data()) { | |
324 if (read(sock, buf + i, 1) <= 0) | |
325 return -1; | |
326 if (buf[i] == '\n') | |
327 break; | |
328 if (buf[i] != '\r') | |
329 i++; | |
330 } | |
331 } | |
332 if (!going) | |
333 return -1; | |
334 buf[i] = '\0'; | |
335 return i; | |
336 } | |
337 | |
338 static gpointer | |
339 http_buffer_loop(gpointer arg) | |
340 { | |
341 gchar line[1024], *user, *pass, *host, *filename, | |
342 *status, *url, *temp, *file; | |
343 gchar *chost; | |
377 | 344 gint cnt, written, error, port, cport; |
345 guint err_len; | |
61 | 346 gboolean redirect; |
347 gint udp_sock = 0; | |
348 fd_set set; | |
978 | 349 #ifdef USE_IPV6 |
350 struct addrinfo hints, *res, *res0; | |
351 char service[6]; | |
352 #else | |
61 | 353 struct hostent *hp; |
354 struct sockaddr_in address; | |
978 | 355 #endif |
61 | 356 struct timeval tv; |
357 | |
358 url = (gchar *) arg; | |
359 do { | |
360 redirect = FALSE; | |
361 | |
362 g_strstrip(url); | |
363 | |
364 parse_url(url, &user, &pass, &host, &port, &filename); | |
365 | |
366 if ((!filename || !*filename) && url[strlen(url) - 1] != '/') | |
367 temp = g_strconcat(url, "/", NULL); | |
368 else | |
369 temp = g_strdup(url); | |
370 g_free(url); | |
371 url = temp; | |
372 | |
373 chost = mpg123_cfg.use_proxy ? mpg123_cfg.proxy_host : host; | |
374 cport = mpg123_cfg.use_proxy ? mpg123_cfg.proxy_port : port; | |
375 | |
978 | 376 #ifdef USE_IPV6 |
377 snprintf(service, 6, "%d", cport); | |
378 memset(&hints, 0, sizeof(hints)); | |
379 hints.ai_socktype = SOCK_STREAM; | |
380 if (! getaddrinfo(chost, service, &hints, &res0)) { | |
381 eof = TRUE; | |
382 for (res = res0; res; res = res->ai_next) { | |
383 if ((sock = socket (res->ai_family, res->ai_socktype, res->ai_protocol)) < 0) | |
384 continue; | |
385 fcntl(sock, F_SETFL, O_NONBLOCK); | |
386 status = g_strdup_printf(_("CONNECTING TO %s:%d"), chost, cport); | |
387 mpg123_ip.set_info_text(status); | |
388 g_free(status); | |
389 ((struct sockaddr_in6 *)res->ai_addr)->sin6_port = htons(cport); | |
390 if (connect(sock, res->ai_addr, res->ai_addrlen) < 0) { | |
391 if (errno != EINPROGRESS) { | |
392 close(sock); | |
393 continue; | |
394 } | |
395 } | |
396 eof = FALSE; | |
397 break; | |
398 } | |
399 freeaddrinfo(res0); | |
400 if (eof) { | |
401 status = g_strdup_printf(_("Couldn't connect to host %s:%d"), chost, cport); | |
402 show_error_message(status); | |
403 g_free(status); | |
404 mpg123_ip.set_info_text(NULL); | |
405 } | |
406 } else { | |
407 status = g_strdup_printf(_("Couldn't look up host %s"), chost); | |
408 show_error_message(status); | |
409 g_free(status); | |
410 | |
411 mpg123_ip.set_info_text(NULL); | |
412 eof = TRUE; | |
413 } | |
414 #else | |
61 | 415 sock = socket(AF_INET, SOCK_STREAM, 0); |
416 fcntl(sock, F_SETFL, O_NONBLOCK); | |
417 address.sin_family = AF_INET; | |
418 | |
419 status = g_strdup_printf(_("LOOKING UP %s"), chost); | |
420 mpg123_ip.set_info_text(status); | |
421 g_free(status); | |
422 | |
423 if (!(hp = gethostbyname(chost))) { | |
424 status = g_strdup_printf(_("Couldn't look up host %s"), chost); | |
425 show_error_message(status); | |
426 g_free(status); | |
427 | |
428 mpg123_ip.set_info_text(NULL); | |
429 eof = TRUE; | |
430 } | |
978 | 431 #endif |
61 | 432 |
433 if (!eof) { | |
978 | 434 #ifndef USE_IPV6 |
61 | 435 memcpy(&address.sin_addr.s_addr, *(hp->h_addr_list), |
436 sizeof(address.sin_addr.s_addr)); | |
437 address.sin_port = g_htons(cport); | |
438 | |
439 status = g_strdup_printf(_("CONNECTING TO %s:%d"), chost, cport); | |
440 mpg123_ip.set_info_text(status); | |
441 g_free(status); | |
442 if (connect | |
443 (sock, (struct sockaddr *) &address, | |
444 sizeof(struct sockaddr_in)) == -1) { | |
445 if (errno != EINPROGRESS) { | |
446 status = | |
447 g_strdup_printf(_("Couldn't connect to host %s"), | |
448 chost); | |
449 show_error_message(status); | |
450 g_free(status); | |
451 | |
452 mpg123_ip.set_info_text(NULL); | |
453 eof = TRUE; | |
454 } | |
455 } | |
978 | 456 #endif |
61 | 457 while (going) { |
458 tv.tv_sec = 0; | |
459 tv.tv_usec = 10000; | |
460 FD_ZERO(&set); | |
461 FD_SET(sock, &set); | |
462 if (select(sock + 1, NULL, &set, NULL, &tv) > 0) { | |
463 err_len = sizeof(error); | |
464 getsockopt(sock, SOL_SOCKET, SO_ERROR, &error, &err_len); | |
465 if (error) { | |
466 status = | |
467 g_strdup_printf(_ | |
468 ("Couldn't connect to host %s"), | |
469 chost); | |
470 show_error_message(status); | |
471 g_free(status); | |
472 | |
473 mpg123_ip.set_info_text(NULL); | |
474 eof = TRUE; | |
475 | |
476 } | |
477 break; | |
478 } | |
479 } | |
480 if (!eof) { | |
481 gchar *auth = NULL, *proxy_auth = NULL; | |
482 gchar udpspace[30]; | |
483 gint udp_port; | |
484 | |
485 if (mpg123_cfg.use_udp_channel) { | |
486 udp_port = udp_establish_listener(&udp_sock); | |
487 if (udp_port > 0) | |
488 sprintf(udpspace, "x-audiocast-udpport: %d\r\n", | |
489 udp_port); | |
490 else | |
491 udp_sock = 0; | |
492 } | |
493 | |
494 if (user && pass) | |
495 auth = | |
496 basic_authentication_encode(user, pass, | |
497 "Authorization"); | |
498 | |
499 if (mpg123_cfg.use_proxy) { | |
500 file = g_strdup(url); | |
501 if (mpg123_cfg.proxy_use_auth && mpg123_cfg.proxy_user | |
502 && mpg123_cfg.proxy_pass) { | |
503 proxy_auth = | |
504 basic_authentication_encode(mpg123_cfg. | |
505 proxy_user, | |
506 mpg123_cfg. | |
507 proxy_pass, | |
508 "Proxy-Authorization"); | |
509 } | |
510 } | |
511 else | |
512 file = g_strconcat("/", filename, NULL); | |
513 temp = g_strdup_printf("GET %s HTTP/1.0\r\n" | |
514 "Host: %s\r\n" | |
515 "User-Agent: %s/%s\r\n" | |
516 "%s%s%s%s\r\n", | |
748
71189eb31ea9
[svn] use PACKAGE_NAME instead of PACKAGE for autoconf compatibility
nenolod
parents:
701
diff
changeset
|
517 file, host, PACKAGE_NAME, PACKAGE_VERSION, |
61 | 518 proxy_auth ? proxy_auth : "", |
519 auth ? auth : "", | |
520 "Icy-MetaData:1\r\n", | |
521 mpg123_cfg. | |
522 use_udp_channel ? udpspace : ""); | |
523 | |
524 g_free(file); | |
525 if (proxy_auth) | |
526 g_free(proxy_auth); | |
527 if (auth) | |
528 g_free(auth); | |
529 write(sock, temp, strlen(temp)); | |
530 g_free(temp); | |
531 mpg123_ip.set_info_text(_("CONNECTED: WAITING FOR REPLY")); | |
532 while (going && !eof) { | |
533 if (http_check_for_data()) { | |
534 if (mpg123_http_read_line(line, 1024)) { | |
535 status = strchr(line, ' '); | |
536 if (status) { | |
537 if (status[1] == '2') | |
538 break; | |
539 else if (status[1] == '3' | |
540 && status[2] == '0' | |
541 && status[3] == '2') { | |
542 while (going) { | |
543 if (http_check_for_data()) { | |
544 if ((cnt = | |
545 mpg123_http_read_line | |
546 (line, 1024)) != -1) { | |
547 if (!cnt) | |
548 break; | |
549 if (!strncmp | |
550 (line, "Location:", 9)) { | |
551 g_free(url); | |
552 url = g_strdup(line + 10); | |
553 } | |
554 } | |
555 else { | |
556 eof = TRUE; | |
557 mpg123_ip.set_info_text(NULL); | |
558 break; | |
559 } | |
560 } | |
561 } | |
562 redirect = TRUE; | |
563 break; | |
564 } | |
565 else { | |
566 status = | |
567 g_strdup_printf(_ | |
568 ("Couldn't connect to host %s\nServer reported: %s"), | |
569 chost, status); | |
570 show_error_message(status); | |
571 g_free(status); | |
572 break; | |
573 } | |
574 } | |
575 } | |
576 else { | |
577 eof = TRUE; | |
578 mpg123_ip.set_info_text(NULL); | |
579 } | |
580 } | |
581 } | |
582 | |
583 while (going && !redirect) { | |
584 if (http_check_for_data()) { | |
585 if ((cnt = mpg123_http_read_line(line, 1024)) != -1) { | |
586 if (!cnt) | |
587 break; | |
588 if (!strncmp(line, "icy-name:", 9)) | |
589 icy_name = g_strdup(line + 9); | |
590 else if (!strncmp(line, "x-audiocast-name:", 17)) | |
591 icy_name = g_strdup(line + 17); | |
592 if (!strncmp(line, "icy-metaint:", 12)) | |
593 icy_metaint = atoi(line + 12); | |
594 if (!strncmp(line, "x-audiocast-udpport:", 20)) { | |
595 #ifdef DEBUG_UDP | |
596 fprintf(stderr, | |
597 "Server wants udp messages on port %d\n", | |
598 atoi(line + 20)); | |
599 #endif | |
600 /* udp_serverport = atoi (line + 20); */ | |
601 } | |
602 | |
603 } | |
604 else { | |
605 eof = TRUE; | |
606 mpg123_ip.set_info_text(NULL); | |
607 break; | |
608 } | |
609 } | |
610 } | |
611 } | |
612 } | |
613 | |
614 if (redirect) { | |
615 if (output_file) { | |
616 vfs_fclose(output_file); | |
617 output_file = NULL; | |
618 } | |
619 close(sock); | |
620 g_free(user); | |
621 g_free(pass); | |
622 g_free(host); | |
623 g_free(filename); | |
624 } | |
625 } while (redirect); | |
626 | |
627 if (mpg123_cfg.save_http_stream) { | |
628 gchar *output_name; | |
629 gint i = 1; | |
630 | |
631 file = mpg123_http_get_title(url); | |
632 output_name = file; | |
633 if (!strncasecmp(output_name, "http://", 7)) | |
634 output_name += 7; | |
635 temp = strrchr(output_name, '.'); | |
636 if (temp && !strcasecmp(temp, ".mp3")) | |
637 *temp = '\0'; | |
638 | |
639 while ((temp = strchr(output_name, '/'))) | |
640 *temp = '_'; | |
641 output_name = g_strdup_printf("%s/%s.mp3", | |
642 mpg123_cfg.save_http_path, output_name); | |
643 while (!access(output_name, F_OK) && i < 100000) { | |
644 g_free(output_name); | |
645 output_name = g_strdup_printf("%s/%s-%d.mp3", | |
646 mpg123_cfg.save_http_path, | |
647 output_name, i++); | |
648 } | |
649 | |
650 g_free(file); | |
651 | |
652 output_file = vfs_fopen(output_name, "wb"); | |
653 g_free(output_name); | |
654 } | |
655 | |
656 while (going) { | |
657 | |
658 if (!http_used() && !mpg123_ip.output->buffer_playing()) | |
659 prebuffering = TRUE; | |
660 if (http_free() > 0 && !eof) { | |
661 if (http_check_for_data()) { | |
662 cnt = min(http_free(), buffer_length - wr_index); | |
663 if (cnt > 1024) | |
664 cnt = 1024; | |
665 written = read(sock, buffer + wr_index, cnt); | |
666 if (written <= 0) { | |
667 eof = TRUE; | |
668 if (prebuffering) { | |
669 prebuffering = FALSE; | |
670 | |
671 mpg123_ip.set_info_text(NULL); | |
672 } | |
673 | |
674 } | |
675 else | |
676 wr_index = (wr_index + written) % buffer_length; | |
677 } | |
678 | |
679 if (prebuffering) { | |
680 if (http_used() > prebuffer_length) { | |
681 prebuffering = FALSE; | |
682 mpg123_ip.set_info_text(NULL); | |
683 } | |
684 else { | |
685 status = | |
990
05de825c2765
[svn] Use correct format string; gsize != long unsigned int.
chainsaw
parents:
978
diff
changeset
|
686 g_strdup_printf(_("PRE-BUFFERING: %zuKB/%zuKB"), |
61 | 687 http_used() / 1024, |
688 prebuffer_length / 1024); | |
689 mpg123_ip.set_info_text(status); | |
690 g_free(status); | |
691 } | |
692 | |
693 } | |
694 } | |
695 else | |
696 xmms_usleep(10000); | |
697 | |
698 if (mpg123_cfg.use_udp_channel && udp_sock != 0) | |
699 if (udp_check_for_data(udp_sock) < 0) { | |
700 close(udp_sock); | |
701 udp_sock = 0; | |
702 } | |
703 } | |
704 if (output_file) { | |
705 vfs_fclose(output_file); | |
706 output_file = NULL; | |
707 } | |
708 close(sock); | |
709 if (udp_sock != 0) | |
710 close(udp_sock); | |
711 | |
712 g_free(user); | |
713 g_free(pass); | |
714 g_free(host); | |
715 g_free(filename); | |
716 g_free(buffer); | |
717 g_free(url); | |
718 | |
719 return NULL; | |
720 } | |
721 | |
722 int | |
723 mpg123_http_open(gchar * _url) | |
724 { | |
725 gchar *url; | |
726 | |
727 url = g_strdup(_url); | |
728 | |
729 rd_index = 0; | |
730 wr_index = 0; | |
731 buffer_length = mpg123_cfg.http_buffer_size * 1024; | |
732 prebuffer_length = (buffer_length * mpg123_cfg.http_prebuffer) / 100; | |
733 buffer_read = 0; | |
734 icy_metaint = 0; | |
735 prebuffering = TRUE; | |
736 going = TRUE; | |
737 eof = FALSE; | |
738 buffer = g_malloc(buffer_length); | |
739 | |
740 thread = g_thread_create(http_buffer_loop, url, TRUE, NULL); | |
741 | |
742 return 0; | |
743 } | |
744 | |
745 char * | |
746 mpg123_http_get_title(gchar * url) | |
747 { | |
748 if (icy_name) | |
749 return g_strdup(icy_name); | |
750 if (g_basename(url) && strlen(g_basename(url)) > 0) | |
751 return g_strdup(g_basename(url)); | |
752 return g_strdup(url); | |
753 } | |
754 | |
755 /* Start UDP Channel specific stuff */ | |
756 | |
757 /* Find a good local udp port and bind udp_sock to it, return the port */ | |
758 static gint | |
759 udp_establish_listener(gint * sock) | |
760 { | |
978 | 761 #ifdef USE_IPV6 |
762 struct sockaddr_in6 sin; | |
763 socklen_t sinlen = sizeof(struct sockaddr_in6); | |
764 #else | |
61 | 765 struct sockaddr_in sin; |
766 socklen_t sinlen = sizeof(struct sockaddr_in); | |
978 | 767 #endif |
61 | 768 |
769 #ifdef DEBUG_UDP | |
770 fprintf(stderr, "Establishing udp listener\n"); | |
771 #endif | |
772 | |
978 | 773 #ifdef USE_IPV6 |
774 if ((*sock = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) { | |
775 #else | |
61 | 776 if ((*sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { |
978 | 777 #endif |
61 | 778 g_log(NULL, G_LOG_LEVEL_CRITICAL, |
779 "udp_establish_listener(): unable to create socket"); | |
780 return -1; | |
781 } | |
782 | |
783 memset(&sin, 0, sinlen); | |
978 | 784 #ifdef USE_IPV6 |
785 sin.sin6_family = AF_INET6; | |
786 #else | |
61 | 787 sin.sin_family = AF_INET; |
788 sin.sin_addr.s_addr = g_htonl(INADDR_ANY); | |
978 | 789 #endif |
61 | 790 |
791 if (bind(*sock, (struct sockaddr *) &sin, sinlen) < 0) { | |
792 g_log(NULL, G_LOG_LEVEL_CRITICAL, | |
793 "udp_establish_listener(): Failed to bind socket to localhost: %s", | |
794 strerror(errno)); | |
795 close(*sock); | |
796 return -1; | |
797 } | |
798 if (fcntl(*sock, F_SETFL, O_NONBLOCK) < 0) { | |
799 g_log(NULL, G_LOG_LEVEL_CRITICAL, | |
800 "udp_establish_listener(): Failed to set flags: %s", | |
801 strerror(errno)); | |
802 close(*sock); | |
803 return -1; | |
804 } | |
805 | |
806 memset(&sin, 0, sinlen); | |
807 if (getsockname(*sock, (struct sockaddr *) &sin, &sinlen) < 0) { | |
808 g_log(NULL, G_LOG_LEVEL_CRITICAL, | |
809 "udp_establish_listener(): Failed to retrieve socket info: %s", | |
810 strerror(errno)); | |
811 close(*sock); | |
812 return -1; | |
813 } | |
814 #ifdef DEBUG_UDP | |
815 fprintf(stderr, "Listening on local %s:%d\n", inet_ntoa(sin.sin_addr), | |
816 g_ntohs(sin.sin_port)); | |
817 #endif | |
818 | |
978 | 819 #ifdef USE_IPV6 |
820 return g_ntohs(sin.sin6_port); | |
821 #else | |
61 | 822 return g_ntohs(sin.sin_port); |
978 | 823 #endif |
61 | 824 } |
825 | |
826 static int | |
827 udp_check_for_data(int sock) | |
828 { | |
829 char buf[1025], **lines; | |
830 char *valptr; | |
831 gchar *title; | |
832 gint len, i; | |
978 | 833 #ifdef USE_IPV6 |
834 struct sockaddr_in6 from; | |
835 #else | |
61 | 836 struct sockaddr_in from; |
978 | 837 #endif |
61 | 838 socklen_t fromlen; |
839 | |
978 | 840 fromlen = sizeof from; |
61 | 841 |
842 if ((len = | |
843 recvfrom(sock, buf, 1024, 0, (struct sockaddr *) &from, | |
844 &fromlen)) < 0) { | |
845 if (errno != EAGAIN) { | |
846 g_log(NULL, G_LOG_LEVEL_CRITICAL, | |
847 "udp_read_data(): Error reading from socket: %s", | |
848 strerror(errno)); | |
849 return -1; | |
850 } | |
851 return 0; | |
852 } | |
853 buf[len] = '\0'; | |
854 #ifdef DEBUG_UDP | |
855 fprintf(stderr, "Received: [%s]\n", buf); | |
856 #endif | |
857 lines = g_strsplit(buf, "\n", 0); | |
858 if (!lines) | |
859 return 0; | |
860 | |
861 for (i = 0; lines[i]; i++) { | |
862 while ((lines[i][strlen(lines[i]) - 1] == '\n') || | |
863 (lines[i][strlen(lines[i]) - 1] == '\r')) | |
864 lines[i][strlen(lines[i]) - 1] = '\0'; | |
865 | |
866 valptr = strchr(lines[i], ':'); | |
867 | |
868 if (!valptr) | |
869 continue; | |
870 else | |
871 valptr++; | |
872 | |
873 g_strstrip(valptr); | |
874 if (!strlen(valptr)) | |
875 continue; | |
876 | |
877 if (strstr(lines[i], "x-audiocast-streamtitle") != NULL) { | |
878 title = g_strdup_printf("%s (%s)", valptr, icy_name); | |
879 if (going) | |
880 mpg123_ip.set_info(title, -1, mpg123_bitrate * 1000, | |
881 mpg123_frequency, mpg123_stereo); | |
882 g_free(title); | |
883 } | |
884 #if 0 | |
885 else if (strstr(lines[i], "x-audiocast-streamlength") != NULL) { | |
886 if (atoi(valptr) != -1) | |
887 mpg123_ip.set_info(NULL, atoi(valptr), | |
888 mpg123_bitrate * 1000, mpg123_frequency, | |
889 mpg123_stereo); | |
890 } | |
891 #endif | |
892 | |
893 else if (strstr(lines[i], "x-audiocast-streammsg") != NULL) { | |
894 /* mpg123_ip.set_info(title, -1, mpg123_bitrate * 1000, mpg123_frequency, mpg123_stereo); */ | |
895 /* xmms_show_message(_("Message"), valptr, _("Ok"), */ | |
896 /* FALSE, NULL, NULL); */ | |
897 g_message("Stream_message: %s", valptr); | |
898 } | |
899 #if 0 | |
900 /* Use this to direct your webbrowser.. yeah right.. */ | |
901 else if (strstr(lines[i], "x-audiocast-streamurl") != NULL) { | |
902 if (lasturl && g_strcmp(valptr, lasturl)) { | |
903 c_message(stderr, "Song URL: %s\n", valptr); | |
904 g_free(lasturl); | |
905 lasturl = g_strdup(valptr); | |
906 } | |
907 } | |
908 #endif | |
909 else if (strstr(lines[i], "x-audiocast-udpseqnr:") != NULL) { | |
910 gchar obuf[60]; | |
911 sprintf(obuf, "x-audiocast-ack: %ld \r\n", atol(valptr)); | |
912 if (sendto | |
913 (sock, obuf, strlen(obuf), 0, (struct sockaddr *) &from, | |
914 fromlen) < 0) { | |
915 g_log(NULL, G_LOG_LEVEL_WARNING, | |
916 "udp_check_for_data(): Unable to send ack to server: %s", | |
917 strerror(errno)); | |
918 } | |
919 #ifdef DEBUG_UDP | |
920 else | |
921 fprintf(stderr, "Sent ack: %s", obuf); | |
978 | 922 #ifdef USE_IPV6 |
923 { | |
924 char adr[INET6_ADDRSTRLEN]; | |
925 inet_ntop(AF_INET6, &from.sin6_addr, adr, INET6_ADDRSTRLEN); | |
926 fprintf(stderr, "Remote: [%s]:%d\n", adr, | |
927 g_ntohs(from.sin6_port)); | |
928 } | |
929 #else | |
61 | 930 fprintf(stderr, "Remote: %s:%d\n", inet_ntoa(from.sin_addr), |
931 g_ntohs(from.sin_port)); | |
932 #endif | |
978 | 933 #endif |
61 | 934 } |
935 } | |
936 g_strfreev(lines); | |
937 return 0; | |
938 } |