1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
|
/* $Id$ */
/***
This file is part of polypaudio.
polypaudio is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
polypaudio is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with polypaudio; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
USA.
***/
#include <assert.h>
#include <howl.h>
#include <polypcore/xmalloc.h>
#include <polypcore/log.h>
#include <polypcore/util.h>
#include "browser.h"
#define SERVICE_NAME_SINK "_polypaudio-sink._tcp."
#define SERVICE_NAME_SOURCE "_polypaudio-source._tcp."
#define SERVICE_NAME_SERVER "_polypaudio-server._tcp."
struct pa_browser {
int ref;
pa_mainloop_api *mainloop;
pa_browse_cb_t callback;
void *userdata;
sw_discovery discovery;
pa_io_event *io_event;
};
static void io_callback(pa_mainloop_api*a, pa_io_event*e, int fd, pa_io_event_flags_t events, void *userdata) {
pa_browser *b = userdata;
assert(a && b && b->mainloop == a);
if (events != PA_IO_EVENT_INPUT || sw_discovery_read_socket(b->discovery) != SW_OKAY) {
pa_log(__FILE__": connection to HOWL daemon failed.\n");
b->mainloop->io_free(b->io_event);
b->io_event = NULL;
return;
}
}
static int type_equal(const char *a, const char *b) {
size_t la, lb;
if (strcasecmp(a, b) == 0)
return 1;
la = strlen(a);
lb = strlen(b);
if (la > 0 && a[la-1] == '.' && la == lb+1 && strncasecmp(a, b, la-1) == 0)
return 1;
if (lb > 0 && b[lb-1] == '.' && lb == la+1 && strncasecmp(a, b, lb-1) == 0)
return 1;
return 0;
}
static int map_to_opcode(const char *type, int new) {
if (type_equal(type, SERVICE_NAME_SINK))
return new ? PA_BROWSE_NEW_SINK : PA_BROWSE_REMOVE_SINK;
else if (type_equal(type, SERVICE_NAME_SOURCE))
return new ? PA_BROWSE_NEW_SOURCE : PA_BROWSE_REMOVE_SOURCE;
else if (type_equal(type, SERVICE_NAME_SERVER))
return new ? PA_BROWSE_NEW_SERVER : PA_BROWSE_REMOVE_SERVER;
return -1;
}
static sw_result resolve_reply(
sw_discovery discovery,
sw_discovery_oid oid,
sw_uint32 interface_index,
sw_const_string name,
sw_const_string type,
sw_const_string domain,
sw_ipv4_address address,
sw_port port,
sw_octets text_record,
sw_ulong text_record_len,
sw_opaque extra) {
pa_browser *b = extra;
pa_browse_info i;
char ip[256], a[256];
int opcode;
int device_found = 0;
uint32_t cookie;
pa_sample_spec ss;
int ss_valid = 0;
sw_text_record_iterator iterator;
int free_iterator = 0;
char *c = NULL;
assert(b);
sw_discovery_cancel(discovery, oid);
memset(&i, 0, sizeof(i));
i.name = name;
if (!b->callback)
goto fail;
opcode = map_to_opcode(type, 1);
assert(opcode >= 0);
snprintf(a, sizeof(a), "tcp:%s:%u", sw_ipv4_address_name(address, ip, sizeof(ip)), port);
i.server = a;
if (text_record && text_record_len) {
char key[SW_TEXT_RECORD_MAX_LEN];
uint8_t val[SW_TEXT_RECORD_MAX_LEN];
uint32_t val_len;
if (sw_text_record_iterator_init(&iterator, text_record, text_record_len) != SW_OKAY) {
pa_log("sw_text_record_string_iterator_init() failed.\n");
goto fail;
}
free_iterator = 1;
while (sw_text_record_iterator_next(iterator, key, val, &val_len) == SW_OKAY) {
c = pa_xstrndup((char*) val, val_len);
if (!strcmp(key, "device")) {
device_found = 1;
pa_xfree((char*) i.device);
i.device = c;
c = NULL;
} else if (!strcmp(key, "server-version")) {
pa_xfree((char*) i.server_version);
i.server_version = c;
c = NULL;
} else if (!strcmp(key, "user-name")) {
pa_xfree((char*) i.user_name);
i.user_name = c;
c = NULL;
} else if (!strcmp(key, "fqdn")) {
size_t l;
pa_xfree((char*) i.fqdn);
i.fqdn = c;
c = NULL;
l = strlen(a);
assert(l+1 <= sizeof(a));
strncat(a, " ", sizeof(a)-l-1);
strncat(a, i.fqdn, sizeof(a)-l-2);
} else if (!strcmp(key, "cookie")) {
if (pa_atou(c, &cookie) < 0)
goto fail;
i.cookie = &cookie;
} else if (!strcmp(key, "description")) {
pa_xfree((char*) i.description);
i.description = c;
c = NULL;
} else if (!strcmp(key, "channels")) {
uint32_t ch;
if (pa_atou(c, &ch) < 0 || ch <= 0 || ch > 255)
goto fail;
ss.channels = (uint8_t) ch;
ss_valid |= 1;
} else if (!strcmp(key, "rate")) {
if (pa_atou(c, &ss.rate) < 0)
goto fail;
ss_valid |= 2;
} else if (!strcmp(key, "format")) {
if ((ss.format = pa_parse_sample_format(c)) == PA_SAMPLE_INVALID)
goto fail;
ss_valid |= 4;
}
pa_xfree(c);
c = NULL;
}
}
/* No device txt record was sent for a sink or source service */
if (opcode != PA_BROWSE_NEW_SERVER && !device_found)
goto fail;
if (ss_valid == 7)
i.sample_spec = &ss;
b->callback(b, opcode, &i, b->userdata);
fail:
pa_xfree((void*) i.device);
pa_xfree((void*) i.fqdn);
pa_xfree((void*) i.server_version);
pa_xfree((void*) i.user_name);
pa_xfree((void*) i.description);
pa_xfree(c);
if (free_iterator)
sw_text_record_iterator_fina(iterator);
return SW_OKAY;
}
static sw_result browse_reply(
sw_discovery discovery,
sw_discovery_oid id,
sw_discovery_browse_status status,
sw_uint32 interface_index,
sw_const_string name,
sw_const_string type,
sw_const_string domain,
sw_opaque extra) {
pa_browser *b = extra;
assert(b);
switch (status) {
case SW_DISCOVERY_BROWSE_ADD_SERVICE: {
sw_discovery_oid oid;
if (sw_discovery_resolve(b->discovery, 0, name, type, domain, resolve_reply, b, &oid) != SW_OKAY)
pa_log("sw_discovery_resolve() failed\n");
break;
}
case SW_DISCOVERY_BROWSE_REMOVE_SERVICE:
if (b->callback) {
pa_browse_info i;
int opcode;
memset(&i, 0, sizeof(i));
i.name = name;
opcode = map_to_opcode(type, 0);
assert(opcode >= 0);
b->callback(b, opcode, &i, b->userdata);
}
break;
default:
;
}
return SW_OKAY;
}
pa_browser *pa_browser_new(pa_mainloop_api *mainloop) {
pa_browser *b;
sw_discovery_oid oid;
b = pa_xnew(pa_browser, 1);
b->mainloop = mainloop;
b->ref = 1;
b->callback = NULL;
b->userdata = NULL;
if (sw_discovery_init(&b->discovery) != SW_OKAY) {
pa_log("sw_discovery_init() failed.\n");
pa_xfree(b);
return NULL;
}
if (sw_discovery_browse(b->discovery, 0, SERVICE_NAME_SERVER, NULL, browse_reply, b, &oid) != SW_OKAY ||
sw_discovery_browse(b->discovery, 0, SERVICE_NAME_SINK, NULL, browse_reply, b, &oid) != SW_OKAY ||
sw_discovery_browse(b->discovery, 0, SERVICE_NAME_SOURCE, NULL, browse_reply, b, &oid) != SW_OKAY) {
pa_log("sw_discovery_browse() failed.\n");
sw_discovery_fina(b->discovery);
pa_xfree(b);
return NULL;
}
b->io_event = mainloop->io_new(mainloop, sw_discovery_socket(b->discovery), PA_IO_EVENT_INPUT, io_callback, b);
return b;
}
static void browser_free(pa_browser *b) {
assert(b && b->mainloop);
if (b->io_event)
b->mainloop->io_free(b->io_event);
sw_discovery_fina(b->discovery);
pa_xfree(b);
}
pa_browser *pa_browser_ref(pa_browser *b) {
assert(b && b->ref >= 1);
b->ref++;
return b;
}
void pa_browser_unref(pa_browser *b) {
assert(b && b->ref >= 1);
if ((-- (b->ref)) <= 0)
browser_free(b);
}
void pa_browser_set_callback(pa_browser *b, pa_browse_cb_t cb, void *userdata) {
assert(b);
b->callback = cb;
b->userdata = userdata;
}
|