glib mainloop fix
[profile/ivi/pulseaudio-panda.git] / polyp / protocol-native.c
1 /* $Id$ */
2
3 /***
4   This file is part of polypaudio.
5  
6   polypaudio is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published
8   by the Free Software Foundation; either version 2 of the License,
9   or (at your option) any later version.
10  
11   polypaudio is distributed in the hope that it will be useful, but
12   WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14   General Public License for more details.
15  
16   You should have received a copy of the GNU General Public License
17   along with polypaudio; if not, write to the Free Software
18   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19   USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <string.h>
27 #include <stdio.h>
28 #include <assert.h>
29 #include <stdlib.h>
30
31 #include "protocol-native.h"
32 #include "native-common.h"
33 #include "packet.h"
34 #include "client.h"
35 #include "source-output.h"
36 #include "sink-input.h"
37 #include "pstream.h"
38 #include "tagstruct.h"
39 #include "pdispatch.h"
40 #include "pstream-util.h"
41 #include "authkey.h"
42 #include "namereg.h"
43 #include "scache.h"
44 #include "xmalloc.h"
45 #include "util.h"
46
47 struct connection;
48 struct pa_protocol_native;
49
50 struct record_stream {
51     struct connection *connection;
52     uint32_t index;
53     struct pa_source_output *source_output;
54     struct pa_memblockq *memblockq;
55     size_t fragment_size;
56 };
57
58 struct playback_stream {
59     int type;
60     struct connection *connection;
61     uint32_t index;
62     struct pa_sink_input *sink_input;
63     struct pa_memblockq *memblockq;
64     size_t requested_bytes;
65     int drain_request;
66     uint32_t drain_tag;
67 };
68
69 struct upload_stream {
70     int type;
71     struct connection *connection;
72     uint32_t index;
73     struct pa_memchunk memchunk;
74     size_t length;
75     char *name;
76     struct pa_sample_spec sample_spec;
77 };
78
79 struct output_stream {
80     int type;
81 };
82
83 enum {
84     UPLOAD_STREAM,
85     PLAYBACK_STREAM
86 };
87
88 struct connection {
89     int authorized;
90     struct pa_protocol_native *protocol;
91     struct pa_client *client;
92     struct pa_pstream *pstream;
93     struct pa_pdispatch *pdispatch;
94     struct pa_idxset *record_streams, *output_streams;
95     uint32_t rrobin_index;
96 };
97
98 struct pa_protocol_native {
99     struct pa_module *module;
100     int public;
101     struct pa_core *core;
102     struct pa_socket_server *server;
103     struct pa_idxset *connections;
104     uint8_t auth_cookie[PA_NATIVE_COOKIE_LENGTH];
105 };
106
107 static int sink_input_peek_cb(struct pa_sink_input *i, struct pa_memchunk *chunk);
108 static void sink_input_drop_cb(struct pa_sink_input *i, size_t length);
109 static void sink_input_kill_cb(struct pa_sink_input *i);
110 static uint32_t sink_input_get_latency_cb(struct pa_sink_input *i);
111
112 static void request_bytes(struct playback_stream*s);
113
114 static void source_output_kill_cb(struct pa_source_output *o);
115 static void source_output_push_cb(struct pa_source_output *o, const struct pa_memchunk *chunk);
116
117 static void command_exit(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
118 static void command_create_playback_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
119 static void command_drain_playback_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
120 static void command_create_record_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
121 static void command_delete_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
122 static void command_auth(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
123 static void command_set_name(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
124 static void command_lookup(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
125 static void command_stat(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
126 static void command_get_playback_latency(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
127 static void command_create_upload_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
128 static void command_finish_upload_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
129 static void command_play_sample(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
130 static void command_remove_sample(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
131 static void command_get_info(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
132 static void command_get_info_list(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
133 static void command_get_server_info(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
134
135 static const struct pa_pdispatch_command command_table[PA_COMMAND_MAX] = {
136     [PA_COMMAND_ERROR] = { NULL },
137     [PA_COMMAND_TIMEOUT] = { NULL },
138     [PA_COMMAND_REPLY] = { NULL },
139     [PA_COMMAND_CREATE_PLAYBACK_STREAM] = { command_create_playback_stream },
140     [PA_COMMAND_DELETE_PLAYBACK_STREAM] = { command_delete_stream },
141     [PA_COMMAND_DRAIN_PLAYBACK_STREAM] = { command_drain_playback_stream },
142     [PA_COMMAND_CREATE_RECORD_STREAM] = { command_create_record_stream },
143     [PA_COMMAND_DELETE_RECORD_STREAM] = { command_delete_stream },
144     [PA_COMMAND_AUTH] = { command_auth },
145     [PA_COMMAND_REQUEST] = { NULL },
146     [PA_COMMAND_EXIT] = { command_exit },
147     [PA_COMMAND_SET_NAME] = { command_set_name },
148     [PA_COMMAND_LOOKUP_SINK] = { command_lookup },
149     [PA_COMMAND_LOOKUP_SOURCE] = { command_lookup },
150     [PA_COMMAND_STAT] = { command_stat },
151     [PA_COMMAND_GET_PLAYBACK_LATENCY] = { command_get_playback_latency },
152     [PA_COMMAND_CREATE_UPLOAD_STREAM] = { command_create_upload_stream },
153     [PA_COMMAND_DELETE_UPLOAD_STREAM] = { command_delete_stream },
154     [PA_COMMAND_FINISH_UPLOAD_STREAM] = { command_finish_upload_stream },
155     [PA_COMMAND_PLAY_SAMPLE] = { command_play_sample },
156     [PA_COMMAND_REMOVE_SAMPLE] = { command_remove_sample },
157     [PA_COMMAND_GET_SINK_INFO] = { command_get_info },
158     [PA_COMMAND_GET_SOURCE_INFO] = { command_get_info },
159     [PA_COMMAND_GET_SINK_INFO_LIST] = { command_get_info_list },
160     [PA_COMMAND_GET_SOURCE_INFO_LIST] = { command_get_info_list },
161     [PA_COMMAND_GET_SERVER_INFO] = { command_get_server_info },
162 };
163
164 /* structure management */
165
166 static struct upload_stream* upload_stream_new(struct connection *c, const struct pa_sample_spec *ss, const char *name, size_t length) {
167     struct upload_stream *s;
168     assert(c && ss && name && length);
169     
170     s = pa_xmalloc(sizeof(struct upload_stream));
171     s->type = UPLOAD_STREAM;
172     s->connection = c;
173     s->sample_spec = *ss;
174     s->name = pa_xstrdup(name);
175
176     s->memchunk.memblock = NULL;
177     s->memchunk.index = 0;
178     s->memchunk.length = 0;
179
180     s->length = length;
181     
182     pa_idxset_put(c->output_streams, s, &s->index);
183     return s;
184 }
185
186 static void upload_stream_free(struct upload_stream *o) {
187     assert(o && o->connection);
188
189     pa_idxset_remove_by_data(o->connection->output_streams, o, NULL);
190
191     pa_xfree(o->name);
192     
193     if (o->memchunk.memblock)
194         pa_memblock_unref(o->memchunk.memblock);
195     
196     pa_xfree(o);
197 }
198
199 static struct record_stream* record_stream_new(struct connection *c, struct pa_source *source, const struct pa_sample_spec *ss, const char *name, size_t maxlength, size_t fragment_size) {
200     struct record_stream *s;
201     struct pa_source_output *source_output;
202     size_t base;
203     assert(c && source && ss && name && maxlength);
204
205     if (!(source_output = pa_source_output_new(source, name, ss)))
206         return NULL;
207
208     s = pa_xmalloc(sizeof(struct record_stream));
209     s->connection = c;
210     s->source_output = source_output;
211     s->source_output->push = source_output_push_cb;
212     s->source_output->kill = source_output_kill_cb;
213     s->source_output->userdata = s;
214     s->source_output->owner = c->protocol->module;
215     s->source_output->client = c->client;
216
217     s->memblockq = pa_memblockq_new(maxlength, 0, base = pa_frame_size(ss), 0, 0);
218     assert(s->memblockq);
219
220     s->fragment_size = (fragment_size/base)*base;
221     if (!s->fragment_size)
222         s->fragment_size = base;
223
224     pa_idxset_put(c->record_streams, s, &s->index);
225     return s;
226 }
227
228 static void record_stream_free(struct record_stream* r) {
229     assert(r && r->connection);
230
231     pa_idxset_remove_by_data(r->connection->record_streams, r, NULL);
232     pa_source_output_free(r->source_output);
233     pa_memblockq_free(r->memblockq);
234     pa_xfree(r);
235 }
236
237 static struct playback_stream* playback_stream_new(struct connection *c, struct pa_sink *sink, const struct pa_sample_spec *ss, const char *name,
238                                                    size_t maxlength,
239                                                    size_t tlength,
240                                                    size_t prebuf,
241                                                    size_t minreq) {
242     struct playback_stream *s;
243     struct pa_sink_input *sink_input;
244     assert(c && sink && ss && name && maxlength);
245
246     if (!(sink_input = pa_sink_input_new(sink, name, ss)))
247         return NULL;
248     
249     s = pa_xmalloc(sizeof(struct playback_stream));
250     s->type = PLAYBACK_STREAM;
251     s->connection = c;
252     s->sink_input = sink_input;
253     
254     s->sink_input->peek = sink_input_peek_cb;
255     s->sink_input->drop = sink_input_drop_cb;
256     s->sink_input->kill = sink_input_kill_cb;
257     s->sink_input->get_latency = sink_input_get_latency_cb;
258     s->sink_input->userdata = s;
259     s->sink_input->owner = c->protocol->module;
260     s->sink_input->client = c->client;
261     
262     s->memblockq = pa_memblockq_new(maxlength, tlength, pa_frame_size(ss), prebuf, minreq);
263     assert(s->memblockq);
264
265     s->requested_bytes = 0;
266     s->drain_request = 0;
267     
268     pa_idxset_put(c->output_streams, s, &s->index);
269     return s;
270 }
271
272 static void playback_stream_free(struct playback_stream* p) {
273     assert(p && p->connection);
274
275     if (p->drain_request)
276         pa_pstream_send_error(p->connection->pstream, p->drain_tag, PA_ERROR_NOENTITY);
277
278     pa_idxset_remove_by_data(p->connection->output_streams, p, NULL);
279     pa_sink_input_free(p->sink_input);
280     pa_memblockq_free(p->memblockq);
281     pa_xfree(p);
282 }
283
284 static void connection_free(struct connection *c) {
285     struct record_stream *r;
286     struct output_stream *o;
287     assert(c && c->protocol);
288
289     pa_idxset_remove_by_data(c->protocol->connections, c, NULL);
290     while ((r = pa_idxset_first(c->record_streams, NULL)))
291         record_stream_free(r);
292     pa_idxset_free(c->record_streams, NULL, NULL);
293
294     while ((o = pa_idxset_first(c->output_streams, NULL)))
295         if (o->type == PLAYBACK_STREAM)
296             playback_stream_free((struct playback_stream*) o);
297         else
298             upload_stream_free((struct upload_stream*) o);
299     pa_idxset_free(c->output_streams, NULL, NULL);
300
301     pa_pdispatch_free(c->pdispatch);
302     pa_pstream_free(c->pstream);
303     pa_client_free(c->client);
304     pa_xfree(c);
305 }
306
307 static void request_bytes(struct playback_stream *s) {
308     struct pa_tagstruct *t;
309     size_t l;
310     assert(s);
311
312     if (!(l = pa_memblockq_missing(s->memblockq)))
313         return;
314
315     if (l <= s->requested_bytes)
316         return;
317
318     l -= s->requested_bytes;
319
320     if (l < pa_memblockq_get_minreq(s->memblockq))
321         return;
322     
323     s->requested_bytes += l;
324
325     t = pa_tagstruct_new(NULL, 0);
326     assert(t);
327     pa_tagstruct_putu32(t, PA_COMMAND_REQUEST);
328     pa_tagstruct_putu32(t, (uint32_t) -1); /* tag */
329     pa_tagstruct_putu32(t, s->index);
330     pa_tagstruct_putu32(t, l);
331     pa_pstream_send_tagstruct(s->connection->pstream, t);
332
333     /*fprintf(stderr, "Requesting %u bytes\n", l);*/
334 }
335
336 static void send_memblock(struct connection *c) {
337     uint32_t start;
338     struct record_stream *r;
339
340     start = PA_IDXSET_INVALID;
341     for (;;) {
342         struct pa_memchunk chunk;
343         
344         if (!(r = pa_idxset_rrobin(c->record_streams, &c->rrobin_index)))
345             return;
346
347         if (start == PA_IDXSET_INVALID)
348             start = c->rrobin_index;
349         else if (start == c->rrobin_index)
350             return;
351
352         if (pa_memblockq_peek(r->memblockq,  &chunk) >= 0) {
353             if (chunk.length > r->fragment_size)
354                 chunk.length = r->fragment_size;
355
356             pa_pstream_send_memblock(c->pstream, r->index, 0, &chunk);
357             pa_memblockq_drop(r->memblockq, chunk.length);
358             pa_memblock_unref(chunk.memblock);
359             
360             return;
361         }
362     }
363 }
364
365 static void send_playback_stream_killed(struct playback_stream *p) {
366     struct pa_tagstruct *t;
367     assert(p);
368
369     t = pa_tagstruct_new(NULL, 0);
370     assert(t);
371     pa_tagstruct_putu32(t, PA_COMMAND_PLAYBACK_STREAM_KILLED);
372     pa_tagstruct_putu32(t, (uint32_t) -1); /* tag */
373     pa_tagstruct_putu32(t, p->index);
374     pa_pstream_send_tagstruct(p->connection->pstream, t);
375 }
376
377 static void send_record_stream_killed(struct record_stream *r) {
378     struct pa_tagstruct *t;
379     assert(r);
380
381     t = pa_tagstruct_new(NULL, 0);
382     assert(t);
383     pa_tagstruct_putu32(t, PA_COMMAND_RECORD_STREAM_KILLED);
384     pa_tagstruct_putu32(t, (uint32_t) -1); /* tag */
385     pa_tagstruct_putu32(t, r->index);
386     pa_pstream_send_tagstruct(r->connection->pstream, t);
387 }
388
389
390 /*** sinkinput callbacks ***/
391
392 static int sink_input_peek_cb(struct pa_sink_input *i, struct pa_memchunk *chunk) {
393     struct playback_stream *s;
394     assert(i && i->userdata && chunk);
395     s = i->userdata;
396
397     if (pa_memblockq_peek(s->memblockq, chunk) < 0)
398         return -1;
399
400     return 0;
401 }
402
403 static void sink_input_drop_cb(struct pa_sink_input *i, size_t length) {
404     struct playback_stream *s;
405     assert(i && i->userdata && length);
406     s = i->userdata;
407
408     pa_memblockq_drop(s->memblockq, length);
409     request_bytes(s);
410
411     if (s->drain_request && !pa_memblockq_is_readable(s->memblockq)) {
412         pa_pstream_send_simple_ack(s->connection->pstream, s->drain_tag);
413         s->drain_request = 0;
414     }
415 }
416
417 static void sink_input_kill_cb(struct pa_sink_input *i) {
418     assert(i && i->userdata);
419     send_playback_stream_killed((struct playback_stream *) i->userdata);
420     playback_stream_free((struct playback_stream *) i->userdata);
421 }
422
423 static uint32_t sink_input_get_latency_cb(struct pa_sink_input *i) {
424     struct playback_stream *s;
425     assert(i && i->userdata);
426     s = i->userdata;
427
428     return pa_bytes_to_usec(pa_memblockq_get_length(s->memblockq), &s->sink_input->sample_spec);
429 }
430
431 /*** source_output callbacks ***/
432
433 static void source_output_push_cb(struct pa_source_output *o, const struct pa_memchunk *chunk) {
434     struct record_stream *s;
435     assert(o && o->userdata && chunk);
436     s = o->userdata;
437     
438     pa_memblockq_push(s->memblockq, chunk, 0);
439     if (!pa_pstream_is_pending(s->connection->pstream))
440         send_memblock(s->connection);
441 }
442
443 static void source_output_kill_cb(struct pa_source_output *o) {
444     assert(o && o->userdata);
445     send_record_stream_killed((struct record_stream *) o->userdata);
446     record_stream_free((struct record_stream *) o->userdata);
447 }
448
449 /*** pdispatch callbacks ***/
450
451 static void protocol_error(struct connection *c) {
452     fprintf(stderr, __FILE__": protocol error, kicking client\n");
453     connection_free(c);
454 }
455
456 static void command_create_playback_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
457     struct connection *c = userdata;
458     struct playback_stream *s;
459     size_t maxlength, tlength, prebuf, minreq;
460     uint32_t sink_index;
461     const char *name, *sink_name;
462     struct pa_sample_spec ss;
463     struct pa_tagstruct *reply;
464     struct pa_sink *sink;
465     assert(c && t && c->protocol && c->protocol->core);
466     
467     if (pa_tagstruct_gets(t, &name) < 0 ||
468         pa_tagstruct_get_sample_spec(t, &ss) < 0 ||
469         pa_tagstruct_getu32(t, &sink_index) < 0 ||
470         pa_tagstruct_gets(t, &sink_name) < 0 ||
471         pa_tagstruct_getu32(t, &maxlength) < 0 ||
472         pa_tagstruct_getu32(t, &tlength) < 0 ||
473         pa_tagstruct_getu32(t, &prebuf) < 0 ||
474         pa_tagstruct_getu32(t, &minreq) < 0 ||
475         !pa_tagstruct_eof(t)) {
476         protocol_error(c);
477         return;
478     }
479
480     if (!c->authorized) {
481         pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
482         return;
483     }
484
485     if (sink_index != (uint32_t) -1)
486         sink = pa_idxset_get_by_index(c->protocol->core->sinks, sink_index);
487     else
488         sink = pa_namereg_get(c->protocol->core, *sink_name ? sink_name : NULL, PA_NAMEREG_SINK, 1);
489
490     if (!sink) {
491         pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
492         return;
493     }
494     
495     if (!(s = playback_stream_new(c, sink, &ss, name, maxlength, tlength, prebuf, minreq))) {
496         pa_pstream_send_error(c->pstream, tag, PA_ERROR_INVALID);
497         return;
498     }
499     
500     reply = pa_tagstruct_new(NULL, 0);
501     assert(reply);
502     pa_tagstruct_putu32(reply, PA_COMMAND_REPLY);
503     pa_tagstruct_putu32(reply, tag);
504     pa_tagstruct_putu32(reply, s->index);
505     assert(s->sink_input);
506     pa_tagstruct_putu32(reply, s->sink_input->index);
507     pa_pstream_send_tagstruct(c->pstream, reply);
508     request_bytes(s);
509 }
510
511 static void command_delete_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
512     struct connection *c = userdata;
513     uint32_t channel;
514     assert(c && t);
515     
516     if (pa_tagstruct_getu32(t, &channel) < 0 ||
517         !pa_tagstruct_eof(t)) {
518         protocol_error(c);
519         return;
520     }
521
522     if (!c->authorized) {
523         pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
524         return;
525     }
526
527     if (command == PA_COMMAND_DELETE_PLAYBACK_STREAM) {
528         struct playback_stream *s;
529         if (!(s = pa_idxset_get_by_index(c->output_streams, channel)) || (s->type != PLAYBACK_STREAM)) {
530             pa_pstream_send_error(c->pstream, tag, PA_ERROR_EXIST);
531             return;
532         }
533
534         playback_stream_free(s);
535     } else if (command == PA_COMMAND_DELETE_RECORD_STREAM) {
536         struct record_stream *s;
537         if (!(s = pa_idxset_get_by_index(c->record_streams, channel))) {
538             pa_pstream_send_error(c->pstream, tag, PA_ERROR_EXIST);
539             return;
540         }
541
542         record_stream_free(s);
543     } else {
544         struct upload_stream *s;
545         assert(command == PA_COMMAND_DELETE_UPLOAD_STREAM);
546         if (!(s = pa_idxset_get_by_index(c->output_streams, channel)) || (s->type != UPLOAD_STREAM)) {
547             pa_pstream_send_error(c->pstream, tag, PA_ERROR_EXIST);
548             return;
549         }
550
551         upload_stream_free(s);
552     }
553             
554     pa_pstream_send_simple_ack(c->pstream, tag);
555 }
556
557 static void command_create_record_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
558     struct connection *c = userdata;
559     struct record_stream *s;
560     size_t maxlength, fragment_size;
561     uint32_t source_index;
562     const char *name, *source_name;
563     struct pa_sample_spec ss;
564     struct pa_tagstruct *reply;
565     struct pa_source *source;
566     assert(c && t && c->protocol && c->protocol->core);
567     
568     if (pa_tagstruct_gets(t, &name) < 0 ||
569         pa_tagstruct_get_sample_spec(t, &ss) < 0 ||
570         pa_tagstruct_getu32(t, &source_index) < 0 ||
571         pa_tagstruct_gets(t, &source_name) < 0 ||
572         pa_tagstruct_getu32(t, &maxlength) < 0 ||
573         pa_tagstruct_getu32(t, &fragment_size) < 0 ||
574         !pa_tagstruct_eof(t)) {
575         protocol_error(c);
576         return;
577     }
578
579     if (!c->authorized) {
580         pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
581         return;
582     }
583
584     if (source_index != (uint32_t) -1)
585         source = pa_idxset_get_by_index(c->protocol->core->sources, source_index);
586     else
587         source = pa_namereg_get(c->protocol->core, *source_name ? source_name : NULL, PA_NAMEREG_SOURCE, 1);
588
589     if (!source) {
590         pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
591         return;
592     }
593     
594     if (!(s = record_stream_new(c, source, &ss, name, maxlength, fragment_size))) {
595         pa_pstream_send_error(c->pstream, tag, PA_ERROR_INVALID);
596         return;
597     }
598     
599     reply = pa_tagstruct_new(NULL, 0);
600     assert(reply);
601     pa_tagstruct_putu32(reply, PA_COMMAND_REPLY);
602     pa_tagstruct_putu32(reply, tag);
603     pa_tagstruct_putu32(reply, s->index);
604     assert(s->source_output);
605     pa_tagstruct_putu32(reply, s->source_output->index);
606     pa_pstream_send_tagstruct(c->pstream, reply);
607 }
608
609 static void command_exit(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
610     struct connection *c = userdata;
611     assert(c && t);
612     
613     if (!pa_tagstruct_eof(t)) {
614         protocol_error(c);
615         return;
616     }
617
618     if (!c->authorized) {
619         pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
620         return;
621     }
622     
623     assert(c->protocol && c->protocol->core && c->protocol->core->mainloop);
624     c->protocol->core->mainloop->quit(c->protocol->core->mainloop, 0);
625     pa_pstream_send_simple_ack(c->pstream, tag); /* nonsense */
626     return;
627 }
628
629 static void command_auth(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
630     struct connection *c = userdata;
631     const void*cookie;
632     assert(c && t);
633
634     if (pa_tagstruct_get_arbitrary(t, &cookie, PA_NATIVE_COOKIE_LENGTH) < 0 ||
635         !pa_tagstruct_eof(t)) {
636         protocol_error(c);
637         return;
638     }
639         
640     if (memcmp(c->protocol->auth_cookie, cookie, PA_NATIVE_COOKIE_LENGTH) != 0) {
641         fprintf(stderr, "protocol-native.c: Denied access to client with invalid authorization key.\n");
642         pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
643         return;
644     }
645
646     c->authorized = 1;
647     pa_pstream_send_simple_ack(c->pstream, tag);
648     return;
649 }
650
651 static void command_set_name(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
652     struct connection *c = userdata;
653     const char *name;
654     assert(c && t);
655
656     if (pa_tagstruct_gets(t, &name) < 0 ||
657         !pa_tagstruct_eof(t)) {
658         protocol_error(c);
659         return;
660     }
661
662     pa_client_rename(c->client, name);
663     pa_pstream_send_simple_ack(c->pstream, tag);
664     return;
665 }
666
667 static void command_lookup(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
668     struct connection *c = userdata;
669     const char *name;
670     uint32_t index = PA_IDXSET_INVALID;
671     assert(c && t);
672
673     if (pa_tagstruct_gets(t, &name) < 0 ||
674         !pa_tagstruct_eof(t)) {
675         protocol_error(c);
676         return;
677     }
678
679     if (!c->authorized) {
680         pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
681         return;
682     }
683
684     if (command == PA_COMMAND_LOOKUP_SINK) {
685         struct pa_sink *sink;
686         if ((sink = pa_namereg_get(c->protocol->core, name, PA_NAMEREG_SINK, 1)))
687             index = sink->index;
688     } else {
689         struct pa_source *source;
690         assert(command == PA_COMMAND_LOOKUP_SOURCE);
691         if ((source = pa_namereg_get(c->protocol->core, name, PA_NAMEREG_SOURCE, 1)))
692             index = source->index;
693     }
694
695     if (index == PA_IDXSET_INVALID)
696         pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
697     else {
698         struct pa_tagstruct *reply;
699         reply = pa_tagstruct_new(NULL, 0);
700         assert(reply);
701         pa_tagstruct_putu32(reply, PA_COMMAND_REPLY);
702         pa_tagstruct_putu32(reply, tag);
703         pa_tagstruct_putu32(reply, index);
704         pa_pstream_send_tagstruct(c->pstream, reply);
705     }
706 }
707
708 static void command_drain_playback_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
709     struct connection *c = userdata;
710     uint32_t index;
711     struct playback_stream *s;
712     assert(c && t);
713
714     if (pa_tagstruct_getu32(t, &index) < 0 ||
715         !pa_tagstruct_eof(t)) {
716         protocol_error(c);
717         return;
718     }
719
720     if (!c->authorized) {
721         pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
722         return;
723     }
724
725     if (!(s = pa_idxset_get_by_index(c->output_streams, index)) || s->type != PLAYBACK_STREAM) {
726         pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
727         return;
728     }
729
730     s->drain_request = 0;
731     
732     if (!pa_memblockq_is_readable(s->memblockq))
733         pa_pstream_send_simple_ack(c->pstream, tag);
734     else {
735         s->drain_request = 1;
736         s->drain_tag = tag;
737     }
738
739
740 static void command_stat(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
741     struct connection *c = userdata;
742     assert(c && t);
743     struct pa_tagstruct *reply;
744
745     if (!pa_tagstruct_eof(t)) {
746         protocol_error(c);
747         return;
748     }
749
750     if (!c->authorized) {
751         pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
752         return;
753     }
754
755     reply = pa_tagstruct_new(NULL, 0);
756     assert(reply);
757     pa_tagstruct_putu32(reply, PA_COMMAND_REPLY);
758     pa_tagstruct_putu32(reply, tag);
759     pa_tagstruct_putu32(reply, pa_memblock_get_count());
760     pa_tagstruct_putu32(reply, pa_memblock_get_total());
761     pa_pstream_send_tagstruct(c->pstream, reply);
762 }
763
764 static void command_get_playback_latency(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
765     struct connection *c = userdata;
766     assert(c && t);
767     struct pa_tagstruct *reply;
768     struct playback_stream *s;
769     uint32_t index, latency;
770
771     if (pa_tagstruct_getu32(t, &index) < 0 ||
772         !pa_tagstruct_eof(t)) {
773         protocol_error(c);
774         return;
775     }
776
777     if (!c->authorized) {
778         pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
779         return;
780     }
781
782     if (!(s = pa_idxset_get_by_index(c->output_streams, index)) || s->type != PLAYBACK_STREAM) {
783         pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
784         return;
785     }
786
787     latency = pa_sink_input_get_latency(s->sink_input);
788     reply = pa_tagstruct_new(NULL, 0);
789     assert(reply);
790     pa_tagstruct_putu32(reply, PA_COMMAND_REPLY);
791     pa_tagstruct_putu32(reply, tag);
792     pa_tagstruct_putu32(reply, latency);
793     pa_pstream_send_tagstruct(c->pstream, reply);
794 }
795
796 static void command_create_upload_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
797     struct connection *c = userdata;
798     struct upload_stream *s;
799     size_t length;
800     const char *name;
801     struct pa_sample_spec ss;
802     struct pa_tagstruct *reply;
803     assert(c && t && c->protocol && c->protocol->core);
804     
805     if (pa_tagstruct_gets(t, &name) < 0 ||
806         pa_tagstruct_get_sample_spec(t, &ss) < 0 ||
807         pa_tagstruct_getu32(t, &length) < 0 ||
808         !pa_tagstruct_eof(t)) {
809         protocol_error(c);
810         return;
811     }
812
813     if (!c->authorized) {
814         pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
815         return;
816     }
817
818     if ((length % pa_frame_size(&ss)) != 0 || length <= 0 || !*name) {
819         pa_pstream_send_error(c->pstream, tag, PA_ERROR_INVALID);
820         return;
821     }
822     
823     if (!(s = upload_stream_new(c, &ss, name, length))) {
824         pa_pstream_send_error(c->pstream, tag, PA_ERROR_INVALID);
825         return;
826     }
827     
828     reply = pa_tagstruct_new(NULL, 0);
829     assert(reply);
830     pa_tagstruct_putu32(reply, PA_COMMAND_REPLY);
831     pa_tagstruct_putu32(reply, tag);
832     pa_tagstruct_putu32(reply, s->index);
833     pa_pstream_send_tagstruct(c->pstream, reply);
834     
835     reply = pa_tagstruct_new(NULL, 0);
836     assert(reply);
837     pa_tagstruct_putu32(reply, PA_COMMAND_REQUEST);
838     pa_tagstruct_putu32(reply, (uint32_t) -1); /* tag */
839     pa_tagstruct_putu32(reply, s->index);
840     pa_tagstruct_putu32(reply, length);
841     pa_pstream_send_tagstruct(c->pstream, reply);
842 }
843
844 static void command_finish_upload_stream(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
845     struct connection *c = userdata;
846     uint32_t channel;
847     struct upload_stream *s;
848     uint32_t index;
849     assert(c && t);
850     
851     if (pa_tagstruct_getu32(t, &channel) < 0 ||
852         !pa_tagstruct_eof(t)) {
853         protocol_error(c);
854         return;
855     }
856
857     if (!c->authorized) {
858         pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
859         return;
860     }
861
862     if (!(s = pa_idxset_get_by_index(c->output_streams, channel)) || (s->type != UPLOAD_STREAM)) {
863         pa_pstream_send_error(c->pstream, tag, PA_ERROR_EXIST);
864         return;
865     }
866
867     pa_scache_add_item(c->protocol->core, s->name, &s->sample_spec, &s->memchunk, &index);
868     pa_pstream_send_simple_ack(c->pstream, tag);
869     upload_stream_free(s);
870 }
871
872 static void command_play_sample(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
873     struct connection *c = userdata;
874     uint32_t sink_index, volume;
875     struct pa_sink *sink;
876     const char *name, *sink_name;
877     assert(c && t);
878
879     if (pa_tagstruct_getu32(t, &sink_index) < 0 ||
880         pa_tagstruct_gets(t, &sink_name) < 0 ||
881         pa_tagstruct_getu32(t, &volume) < 0 ||
882         pa_tagstruct_gets(t, &name) < 0 ||
883         !pa_tagstruct_eof(t)) {
884         protocol_error(c);
885         return;
886     }
887     
888     if (!c->authorized) {
889         pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
890         return;
891     }
892
893     if (sink_index != (uint32_t) -1)
894         sink = pa_idxset_get_by_index(c->protocol->core->sinks, sink_index);
895     else
896         sink = pa_namereg_get(c->protocol->core, *sink_name ? sink_name : NULL, PA_NAMEREG_SINK, 1);
897
898     if (!sink) {
899         pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
900         return;
901     }
902
903     if (pa_scache_play_item(c->protocol->core, name, sink, volume) < 0) {
904         pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
905         return;
906     }
907
908     pa_pstream_send_simple_ack(c->pstream, tag);
909 }
910
911 static void command_remove_sample(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
912     struct connection *c = userdata;
913     const char *name;
914     assert(c && t);
915
916     if (pa_tagstruct_gets(t, &name) < 0 ||
917         !pa_tagstruct_eof(t)) {
918         protocol_error(c);
919         return;
920     }
921
922     if (!c->authorized) {
923         pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
924         return;
925     }
926
927     if (pa_scache_remove_item(c->protocol->core, name) < 0) {
928         pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
929         return;
930     }
931
932     pa_pstream_send_simple_ack(c->pstream, tag);
933 }
934
935 static void sink_fill_tagstruct(struct pa_tagstruct *t, struct pa_sink *sink) {
936     assert(t && sink);
937     pa_tagstruct_putu32(t, sink->index);
938     pa_tagstruct_puts(t, sink->name);
939     pa_tagstruct_puts(t, sink->description ? sink->description : "");
940     pa_tagstruct_put_sample_spec(t, &sink->sample_spec);
941     pa_tagstruct_putu32(t, sink->owner ? sink->owner->index : (uint32_t) -1);
942     pa_tagstruct_putu32(t, sink->volume);
943     pa_tagstruct_putu32(t, sink->monitor_source->index);
944     pa_tagstruct_puts(t, sink->monitor_source->name);
945     pa_tagstruct_putu32(t, pa_sink_get_latency(sink));
946 }
947
948 static void source_fill_tagstruct(struct pa_tagstruct *t, struct pa_source *source) {
949     assert(t && source);
950     pa_tagstruct_putu32(t, source->index);
951     pa_tagstruct_puts(t, source->name);
952     pa_tagstruct_puts(t, source->description ? source->description : "");
953     pa_tagstruct_put_sample_spec(t, &source->sample_spec);
954     pa_tagstruct_putu32(t, source->owner ? source->owner->index : (uint32_t) -1);
955     pa_tagstruct_putu32(t, source->monitor_of ? source->monitor_of->index : (uint32_t) -1);
956     pa_tagstruct_puts(t, source->monitor_of ? source->monitor_of->name : "");
957 }
958
959 static void command_get_info(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
960     struct connection *c = userdata;
961     uint32_t index;
962     struct pa_sink *sink = NULL;
963     struct pa_source *source = NULL;
964     const char *name;
965     struct pa_tagstruct *reply;
966     assert(c && t);
967
968     if (pa_tagstruct_getu32(t, &index) < 0 ||
969         pa_tagstruct_gets(t, &name) < 0 ||
970         !pa_tagstruct_eof(t)) {
971         protocol_error(c);
972         return;
973     }
974     
975     if (!c->authorized) {
976         pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
977         return;
978     }
979
980     if (command == PA_COMMAND_GET_SINK_INFO) {
981         if (index != (uint32_t) -1)
982             sink = pa_idxset_get_by_index(c->protocol->core->sinks, index);
983         else
984             sink = pa_namereg_get(c->protocol->core, *name ? name : NULL, PA_NAMEREG_SINK, 1);
985     } else {
986         assert(command == PA_COMMAND_GET_SOURCE_INFO);
987         if (index != (uint32_t) -1)
988             source = pa_idxset_get_by_index(c->protocol->core->sources, index);
989         else
990             source = pa_namereg_get(c->protocol->core, *name ? name : NULL, PA_NAMEREG_SOURCE, 1);
991     }
992
993     if (!sink && !source) {
994         pa_pstream_send_error(c->pstream, tag, PA_ERROR_NOENTITY);
995         return;
996     }
997
998     reply = pa_tagstruct_new(NULL, 0);
999     assert(reply);
1000     pa_tagstruct_putu32(reply, PA_COMMAND_REPLY);
1001     pa_tagstruct_putu32(reply, tag); 
1002     if (sink)
1003         sink_fill_tagstruct(reply, sink);
1004     else
1005         source_fill_tagstruct(reply, source);
1006     pa_pstream_send_tagstruct(c->pstream, reply);
1007 }
1008
1009 static void command_get_info_list(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
1010     struct connection *c = userdata;
1011     struct pa_idxset *i;
1012     uint32_t index;
1013     void *p;
1014     struct pa_tagstruct *reply;
1015     assert(c && t);
1016
1017     if (!pa_tagstruct_eof(t)) {
1018         protocol_error(c);
1019         return;
1020     }
1021     
1022     if (!c->authorized) {
1023         pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
1024         return;
1025     }
1026
1027     reply = pa_tagstruct_new(NULL, 0);
1028     assert(reply);
1029     pa_tagstruct_putu32(reply, PA_COMMAND_REPLY);
1030     pa_tagstruct_putu32(reply, tag);
1031
1032     if (command == PA_COMMAND_GET_SINK_INFO_LIST)
1033         i = c->protocol->core->sinks;
1034     else {
1035         assert(command == PA_COMMAND_GET_SOURCE_INFO_LIST);
1036         i = c->protocol->core->sources;
1037     }
1038
1039     for (p = pa_idxset_first(i, &index); p; p = pa_idxset_next(i, &index)) {
1040         if (command == PA_COMMAND_GET_SINK_INFO_LIST)
1041             sink_fill_tagstruct(reply, p);
1042         else {
1043             assert(command == PA_COMMAND_GET_SOURCE_INFO_LIST);
1044             source_fill_tagstruct(reply, p);
1045         }
1046     } 
1047     
1048     pa_pstream_send_tagstruct(c->pstream, reply);
1049 }
1050
1051 static void command_get_server_info(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata) {
1052     struct connection *c = userdata;
1053     struct pa_tagstruct *reply;
1054     char txt[256];
1055     assert(c && t);
1056
1057     if (!pa_tagstruct_eof(t)) {
1058         protocol_error(c);
1059         return;
1060     }
1061     
1062     if (!c->authorized) {
1063         pa_pstream_send_error(c->pstream, tag, PA_ERROR_ACCESS);
1064         return;
1065     }
1066
1067     reply = pa_tagstruct_new(NULL, 0);
1068     assert(reply);
1069     pa_tagstruct_putu32(reply, PA_COMMAND_REPLY);
1070     pa_tagstruct_putu32(reply, tag);
1071     pa_tagstruct_puts(reply, PACKAGE_NAME);
1072     pa_tagstruct_puts(reply, PACKAGE_VERSION);
1073     pa_tagstruct_puts(reply, pa_get_user_name(txt, sizeof(txt)));
1074     pa_tagstruct_puts(reply, pa_get_host_name(txt, sizeof(txt)));
1075     pa_tagstruct_put_sample_spec(reply, &c->protocol->core->default_sample_spec);
1076     pa_pstream_send_tagstruct(c->pstream, reply);
1077 }
1078
1079 /*** pstream callbacks ***/
1080
1081 static void pstream_packet_callback(struct pa_pstream *p, struct pa_packet *packet, void *userdata) {
1082     struct connection *c = userdata;
1083     assert(p && packet && packet->data && c);
1084
1085     if (pa_pdispatch_run(c->pdispatch, packet, c) < 0) {
1086         fprintf(stderr, "protocol-native: invalid packet.\n");
1087         connection_free(c);
1088     }
1089 }
1090
1091 static void pstream_memblock_callback(struct pa_pstream *p, uint32_t channel, int32_t delta, const struct pa_memchunk *chunk, void *userdata) {
1092     struct connection *c = userdata;
1093     struct output_stream *stream;
1094     assert(p && chunk && userdata);
1095
1096     if (!(stream = pa_idxset_get_by_index(c->output_streams, channel))) {
1097         fprintf(stderr, "protocol-native: client sent block for invalid stream.\n");
1098         connection_free(c);
1099         return;
1100     }
1101
1102     if (stream->type == PLAYBACK_STREAM) {
1103         struct playback_stream *p = (struct playback_stream*) stream;
1104         if (chunk->length >= p->requested_bytes)
1105             p->requested_bytes = 0;
1106         else
1107             p->requested_bytes -= chunk->length;
1108         
1109         pa_memblockq_push_align(p->memblockq, chunk, delta);
1110         assert(p->sink_input);
1111         pa_sink_notify(p->sink_input->sink);
1112         /*fprintf(stderr, "Recieved %u bytes.\n", chunk->length);*/
1113     } else {
1114         struct upload_stream *u = (struct upload_stream*) stream;
1115         size_t l;
1116         assert(u->type == UPLOAD_STREAM);
1117
1118         if (!u->memchunk.memblock) {
1119             if (u->length == chunk->length) {
1120                 u->memchunk = *chunk;
1121                 pa_memblock_ref(u->memchunk.memblock);
1122                 u->length = 0;
1123                 fprintf(stderr, "COPY\n");
1124             } else {
1125                 u->memchunk.memblock = pa_memblock_new(u->length);
1126                 u->memchunk.index = u->memchunk.length = 0;
1127             }
1128         }
1129         
1130         assert(u->memchunk.memblock);
1131         
1132         l = u->length; 
1133         if (l > chunk->length)
1134             l = chunk->length;
1135
1136         if (l > 0) {
1137             memcpy(u->memchunk.memblock->data + u->memchunk.index + u->memchunk.length, chunk->memblock->data+chunk->index, l);
1138             u->memchunk.length += l;
1139             u->length -= l;
1140         }
1141     }
1142 }
1143
1144 static void pstream_die_callback(struct pa_pstream *p, void *userdata) {
1145     struct connection *c = userdata;
1146     assert(p && c);
1147     connection_free(c);
1148
1149     fprintf(stderr, "protocol-native: connection died.\n");
1150 }
1151
1152
1153 static void pstream_drain_callback(struct pa_pstream *p, void *userdata) {
1154     struct connection *c = userdata;
1155     assert(p && c);
1156
1157     send_memblock(c);
1158 }
1159
1160 /*** client callbacks ***/
1161
1162 static void client_kill_cb(struct pa_client *c) {
1163     assert(c && c->userdata);
1164     connection_free(c->userdata);
1165 }
1166
1167 /*** socket server callbacks ***/
1168
1169 static void on_connection(struct pa_socket_server*s, struct pa_iochannel *io, void *userdata) {
1170     struct pa_protocol_native *p = userdata;
1171     struct connection *c;
1172     assert(s && io && p);
1173
1174     c = pa_xmalloc(sizeof(struct connection));
1175     c->authorized = p->public;
1176     c->protocol = p;
1177     assert(p->core);
1178     c->client = pa_client_new(p->core, "NATIVE", "Client");
1179     assert(c->client);
1180     c->client->kill = client_kill_cb;
1181     c->client->userdata = c;
1182     c->client->owner = p->module;
1183     
1184     c->pstream = pa_pstream_new(p->core->mainloop, io);
1185     assert(c->pstream);
1186
1187     pa_pstream_set_recieve_packet_callback(c->pstream, pstream_packet_callback, c);
1188     pa_pstream_set_recieve_memblock_callback(c->pstream, pstream_memblock_callback, c);
1189     pa_pstream_set_die_callback(c->pstream, pstream_die_callback, c);
1190     pa_pstream_set_drain_callback(c->pstream, pstream_drain_callback, c);
1191
1192     c->pdispatch = pa_pdispatch_new(p->core->mainloop, command_table, PA_COMMAND_MAX);
1193     assert(c->pdispatch);
1194
1195     c->record_streams = pa_idxset_new(NULL, NULL);
1196     c->output_streams = pa_idxset_new(NULL, NULL);
1197     assert(c->record_streams && c->output_streams);
1198
1199     c->rrobin_index = PA_IDXSET_INVALID;
1200
1201     pa_idxset_put(p->connections, c, NULL);
1202 }
1203
1204 /*** module entry points ***/
1205
1206 struct pa_protocol_native* pa_protocol_native_new(struct pa_core *core, struct pa_socket_server *server, struct pa_module *m, struct pa_modargs *ma) {
1207     struct pa_protocol_native *p;
1208     uint32_t public;
1209     assert(core && server && ma);
1210
1211     if (pa_modargs_get_value_u32(ma, "public", &public) < 0) {
1212         fprintf(stderr, __FILE__": public= expects numeric argument.\n");
1213         return NULL;
1214     }
1215     
1216     p = pa_xmalloc(sizeof(struct pa_protocol_native));
1217
1218     if (pa_authkey_load_from_home(pa_modargs_get_value(ma, "cookie", PA_NATIVE_COOKIE_FILE), p->auth_cookie, sizeof(p->auth_cookie)) < 0) {
1219         pa_xfree(p);
1220         return NULL;
1221     }
1222
1223     p->module = m;
1224     p->public = public;
1225     p->server = server;
1226     p->core = core;
1227     p->connections = pa_idxset_new(NULL, NULL);
1228     assert(p->connections);
1229
1230     pa_socket_server_set_callback(p->server, on_connection, p);
1231     
1232     return p;
1233 }
1234
1235 void pa_protocol_native_free(struct pa_protocol_native *p) {
1236     struct connection *c;
1237     assert(p);
1238
1239     while ((c = pa_idxset_first(p->connections, NULL)))
1240         connection_free(c);
1241     pa_idxset_free(p->connections, NULL, NULL);
1242     pa_socket_server_free(p->server);
1243     pa_xfree(p);
1244 }