Remember to store the struct with module info.
[profile/ivi/pulseaudio-panda.git] / src / modules / module-protocol-stub.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 Lesser 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 Lesser 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 <errno.h>
28 #include <stdio.h>
29 #include <assert.h>
30 #include <unistd.h>
31 #include <limits.h>
32
33 #ifdef HAVE_SYS_SOCKET_H
34 #include <sys/socket.h>
35 #endif
36 #ifdef HAVE_ARPA_INET_H
37 #include <arpa/inet.h>
38 #endif
39 #ifdef HAVE_NETINET_IN_H
40 #include <netinet/in.h>
41 #endif
42
43 #include "../polypcore/winsock.h"
44
45 #include <polypcore/module.h>
46 #include <polypcore/socket-server.h>
47 #include <polypcore/socket-util.h>
48 #include <polypcore/util.h>
49 #include <polypcore/modargs.h>
50 #include <polypcore/log.h>
51 #include <polypcore/native-common.h>
52 #include <polypcore/xmalloc.h>
53
54 #ifdef USE_TCP_SOCKETS
55 #define SOCKET_DESCRIPTION "(TCP sockets)"
56 #define SOCKET_USAGE "port=<TCP port number> loopback=<listen on loopback device only?> listen=<address to listen on>"
57 #else
58 #define SOCKET_DESCRIPTION "(UNIX sockets)"
59 #define SOCKET_USAGE "socket=<path to UNIX socket>"
60 #endif
61
62 #if defined(USE_PROTOCOL_SIMPLE)
63   #include <polypcore/protocol-simple.h>
64   #define protocol_new pa_protocol_simple_new
65   #define protocol_free pa_protocol_simple_free
66   #define TCPWRAP_SERVICE "polypaudio-simple"
67   #define IPV4_PORT 4711
68   #define UNIX_SOCKET "simple"
69   #define MODULE_ARGUMENTS "rate", "format", "channels", "sink", "source", "playback", "record",
70   #if defined(USE_TCP_SOCKETS)
71     #include "module-simple-protocol-tcp-symdef.h"
72   #else
73     #include "module-simple-protocol-unix-symdef.h"
74   #endif
75   PA_MODULE_DESCRIPTION("Simple protocol "SOCKET_DESCRIPTION)
76   PA_MODULE_USAGE("rate=<sample rate> format=<sample format> channels=<number of channels> sink=<sink to connect to> source=<source to connect to> playback=<enable playback?> record=<enable record?> "SOCKET_USAGE)
77 #elif defined(USE_PROTOCOL_CLI)
78   #include <polypcore/protocol-cli.h> 
79   #define protocol_new pa_protocol_cli_new
80   #define protocol_free pa_protocol_cli_free
81   #define TCPWRAP_SERVICE "polypaudio-cli"
82   #define IPV4_PORT 4712
83   #define UNIX_SOCKET "cli"
84   #define MODULE_ARGUMENTS 
85   #ifdef USE_TCP_SOCKETS
86     #include "module-cli-protocol-tcp-symdef.h"
87   #else
88     #include "module-cli-protocol-unix-symdef.h"
89   #endif
90   PA_MODULE_DESCRIPTION("Command line interface protocol "SOCKET_DESCRIPTION)
91   PA_MODULE_USAGE(SOCKET_USAGE)
92 #elif defined(USE_PROTOCOL_HTTP)
93   #include <polypcore/protocol-http.h>
94   #define protocol_new pa_protocol_http_new
95   #define protocol_free pa_protocol_http_free
96   #define TCPWRAP_SERVICE "polypaudio-http"
97   #define IPV4_PORT 4714
98   #define UNIX_SOCKET "http"
99   #define MODULE_ARGUMENTS 
100   #ifdef USE_TCP_SOCKETS
101     #include "module-http-protocol-tcp-symdef.h"
102   #else
103     #include "module-http-protocol-unix-symdef.h"
104   #endif
105   PA_MODULE_DESCRIPTION("HTTP "SOCKET_DESCRIPTION)
106   PA_MODULE_USAGE(SOCKET_USAGE)
107 #elif defined(USE_PROTOCOL_NATIVE)
108   #include <polypcore/protocol-native.h>
109   #define protocol_new pa_protocol_native_new
110   #define protocol_free pa_protocol_native_free
111   #define TCPWRAP_SERVICE "polypaudio-native"
112   #define IPV4_PORT PA_NATIVE_DEFAULT_PORT
113   #define UNIX_SOCKET PA_NATIVE_DEFAULT_UNIX_SOCKET
114   #define MODULE_ARGUMENTS_COMMON "cookie", "auth-anonymous",
115   #ifdef USE_TCP_SOCKETS
116     #include "module-native-protocol-tcp-symdef.h"
117   #else
118     #include "module-native-protocol-unix-symdef.h"
119   #endif
120
121   #if defined(SCM_CREDENTIALS) && !defined(USE_TCP_SOCKETS)
122     #define MODULE_ARGUMENTS MODULE_ARGUMENTS_COMMON "auth-group",
123     #define AUTH_USAGE "auth-group=<local group to allow access>"
124   #else
125     #define MODULE_ARGUMENTS MODULE_ARGUMENTS_COMMON
126     #define AUTH_USAGE
127   #endif
128   
129   PA_MODULE_DESCRIPTION("Native protocol "SOCKET_DESCRIPTION)
130   PA_MODULE_USAGE("auth-anonymous=<don't check for cookies?> cookie=<path to cookie file> "AUTH_USAGE SOCKET_USAGE)
131 #elif defined(USE_PROTOCOL_ESOUND)
132   #include <polypcore/protocol-esound.h>
133   #include <polypcore/esound.h>
134   #define protocol_new pa_protocol_esound_new
135   #define protocol_free pa_protocol_esound_free
136   #define TCPWRAP_SERVICE "esound"
137   #define IPV4_PORT ESD_DEFAULT_PORT
138   #define UNIX_SOCKET ESD_UNIX_SOCKET_NAME
139   #define MODULE_ARGUMENTS "sink", "source", "auth-anonymous", "cookie",
140   #ifdef USE_TCP_SOCKETS
141     #include "module-esound-protocol-tcp-symdef.h"
142   #else
143     #include "module-esound-protocol-unix-symdef.h"
144   #endif
145   PA_MODULE_DESCRIPTION("ESOUND protocol "SOCKET_DESCRIPTION)
146   PA_MODULE_USAGE("sink=<sink to connect to> source=<source to connect to> auth-anonymous=<don't check for cookies?> cookie=<path to cookie file> "SOCKET_USAGE)
147 #else
148   #error "Broken build system" 
149 #endif
150
151 PA_MODULE_AUTHOR("Lennart Poettering")
152 PA_MODULE_VERSION(PACKAGE_VERSION)
153
154 static const char* const valid_modargs[] = {
155     MODULE_ARGUMENTS
156 #if defined(USE_TCP_SOCKETS)
157     "port",
158     "loopback",
159     "listen",
160 #else
161     "socket",
162 #endif
163     NULL
164 };
165
166 struct userdata {
167 #if defined(USE_TCP_SOCKETS)
168     void *protocol_ipv4;
169     void *protocol_ipv6;
170 #else
171     void *protocol_unix;
172 #endif
173 };
174
175 int pa__init(pa_core *c, pa_module*m) {
176     pa_modargs *ma = NULL;
177     int ret = -1;
178
179     struct userdata *u = NULL;
180
181 #if defined(USE_TCP_SOCKETS)
182     pa_socket_server *s_ipv4 = NULL;
183     pa_socket_server *s_ipv6 = NULL;
184     int loopback = 1;
185     uint32_t port = IPV4_PORT;
186     const char *listen_on;
187 #else
188     pa_socket_server *s;
189     int r;
190     const char *v;
191     char tmp[PATH_MAX];
192 #endif
193
194     assert(c && m);
195
196 #if defined(USE_TCP_SOCKETS)
197     if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
198         pa_log(__FILE__": Failed to parse module arguments");
199         goto finish;
200     }
201
202     if (pa_modargs_get_value_boolean(ma, "loopback", &loopback) < 0) {
203         pa_log(__FILE__": loopback= expects a boolean argument.");
204         goto fail;
205     }
206
207     if (pa_modargs_get_value_u32(ma, "port", &port) < 0 || port < 1 || port > 0xFFFF) {
208         pa_log(__FILE__": port= expects a numerical argument between 1 and 65535.");
209         goto fail;
210     }
211
212     listen_on = pa_modargs_get_value(ma, "listen", NULL);
213
214     if (listen_on) {
215         s_ipv4 = pa_socket_server_new_ipv4_string(c->mainloop, listen_on, port, TCPWRAP_SERVICE);
216         s_ipv6 = pa_socket_server_new_ipv6_string(c->mainloop, listen_on, port, TCPWRAP_SERVICE);
217         if (!s_ipv4 && !s_ipv6)
218             goto fail;
219     } else if (loopback) {
220         s_ipv4 = pa_socket_server_new_ipv4_loopback(c->mainloop, port, TCPWRAP_SERVICE);
221         s_ipv6 = pa_socket_server_new_ipv6_loopback(c->mainloop, port, TCPWRAP_SERVICE);
222         if (!s_ipv4 && !s_ipv6)
223             goto fail;
224     } else {
225         s_ipv4 = pa_socket_server_new_ipv4_any(c->mainloop, port, TCPWRAP_SERVICE);
226         s_ipv6 = pa_socket_server_new_ipv6_any(c->mainloop, port, TCPWRAP_SERVICE);
227         if (!s_ipv4 && !s_ipv6)
228             goto fail;
229     }
230 #else
231     v = pa_modargs_get_value(ma, "socket", UNIX_SOCKET);
232     assert(v);
233
234     pa_runtime_path(v, tmp, sizeof(tmp));
235
236     if (pa_make_secure_parent_dir(tmp) < 0) {
237         pa_log(__FILE__": Failed to create secure socket directory.");
238         goto fail;
239     }
240
241     if ((r = pa_unix_socket_remove_stale(tmp)) < 0) {
242         pa_log(__FILE__": Failed to remove stale UNIX socket '%s': %s", tmp, strerror(errno));
243         goto fail;
244     }
245     
246     if (r)
247         pa_log(__FILE__": Removed stale UNIX socket '%s'.", tmp);
248     
249     if (!(s = pa_socket_server_new_unix(c->mainloop, tmp)))
250         goto fail;
251 #endif
252
253     u = pa_xmalloc0(sizeof(struct userdata));
254
255 #if defined(USE_TCP_SOCKETS)
256     if (s_ipv4) {
257         u->protocol_ipv4 = protocol_new(c, s_ipv4, m, ma);
258         if (!u->protocol_ipv4)
259             pa_socket_server_unref(s_ipv4);
260     }
261
262     if (s_ipv6) {
263         u->protocol_ipv6 = protocol_new(c, s_ipv4, m, ma);
264         if (!u->protocol_ipv6)
265             pa_socket_server_unref(s_ipv6);
266     }
267
268     if (!u->protocol_ipv4 && !u->protocol_ipv6)
269         goto fail;
270 #else
271     if (!(u->protocol_unix = protocol_new(c, s, m, ma)))
272         goto fail;
273 #endif
274
275     m->userdata = u;
276
277     ret = 0;
278
279 finish:
280     if (ma)
281         pa_modargs_free(ma);
282
283     return ret;
284
285 fail:
286     if (u) {
287 #if defined(USE_TCP_SOCKETS)
288         if (u->protocol_ipv4)
289             protocol_free(u->protocol_ipv4);
290         if (u->protocol_ipv6)
291             protocol_free(u->protocol_ipv6);
292 #else
293         if (u->protocol_unix)
294             protocol_free(u->protocol_unix);
295 #endif
296         pa_xfree(u);
297     } else {
298 #if defined(USE_TCP_SOCKETS)
299         if (s_ipv4)
300             pa_socket_server_unref(s_ipv4);
301         if (s_ipv6)
302             pa_socket_server_unref(s_ipv6);
303 #else
304         if (s)
305             pa_socket_server_unref(s);
306 #endif
307     }
308
309     goto finish;
310 }
311
312 void pa__done(pa_core *c, pa_module*m) {
313     struct userdata *u;
314     assert(c && m);
315
316 #if defined(USE_PROTOCOL_ESOUND) && !defined(USE_TCP_SOCKETS)
317         if (remove(ESD_UNIX_SOCKET_NAME) != 0)
318                 pa_log("%s: Failed to remove %s : %s.", __FILE__, ESD_UNIX_SOCKET_NAME, strerror (errno));
319         if (remove(ESD_UNIX_SOCKET_DIR) != 0)
320                 pa_log("%s: Failed to remove %s : %s.", __FILE__, ESD_UNIX_SOCKET_DIR, strerror (errno));
321 #endif
322
323     u = m->userdata;
324     assert(u);
325
326 #if defined(USE_TCP_SOCKETS)
327     if (u->protocol_ipv4)
328         protocol_free(u->protocol_ipv4);
329     if (u->protocol_ipv6)
330         protocol_free(u->protocol_ipv6);
331 #else
332     if (u->protocol_unix)
333         protocol_free(u->protocol_unix);
334 #endif
335
336     pa_xfree(u);
337 }