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