]> code.delx.au - pulseaudio/blob - src/modules/module-protocol-stub.c
Merge dead branch 'liboil-test'
[pulseaudio] / src / modules / module-protocol-stub.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2004-2006 Lennart Poettering
5 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
6
7 PulseAudio is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published
9 by the Free Software Foundation; either version 2 of the License,
10 or (at your option) any later version.
11
12 PulseAudio is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with PulseAudio; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 USA.
21 ***/
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <string.h>
28 #include <errno.h>
29 #include <stdio.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 <pulse/xmalloc.h>
44
45 #include <pulsecore/winsock.h>
46 #include <pulsecore/core-error.h>
47 #include <pulsecore/module.h>
48 #include <pulsecore/socket-server.h>
49 #include <pulsecore/socket-util.h>
50 #include <pulsecore/core-util.h>
51 #include <pulsecore/modargs.h>
52 #include <pulsecore/log.h>
53 #include <pulsecore/native-common.h>
54 #include <pulsecore/creds.h>
55
56 #ifdef USE_TCP_SOCKETS
57 #define SOCKET_DESCRIPTION "(TCP sockets)"
58 #define SOCKET_USAGE "port=<TCP port number> listen=<address to listen on>"
59 #else
60 #define SOCKET_DESCRIPTION "(UNIX sockets)"
61 #define SOCKET_USAGE "socket=<path to UNIX socket>"
62 #endif
63
64 #if defined(USE_PROTOCOL_SIMPLE)
65 #include <pulsecore/protocol-simple.h>
66 #define protocol_new pa_protocol_simple_new
67 #define protocol_free pa_protocol_simple_free
68 #define TCPWRAP_SERVICE "pulseaudio-simple"
69 #define IPV4_PORT 4711
70 #define UNIX_SOCKET "simple"
71 #define MODULE_ARGUMENTS "rate", "format", "channels", "sink", "source", "playback", "record",
72 #if defined(USE_TCP_SOCKETS)
73 #include "module-simple-protocol-tcp-symdef.h"
74 #else
75 #include "module-simple-protocol-unix-symdef.h"
76 #endif
77 PA_MODULE_DESCRIPTION("Simple protocol "SOCKET_DESCRIPTION);
78 PA_MODULE_USAGE("rate=<sample rate> "
79 "format=<sample format> "
80 "channels=<number of channels> "
81 "sink=<sink to connect to> "
82 "source=<source to connect to> "
83 "playback=<enable playback?> "
84 "record=<enable record?> "
85 SOCKET_USAGE);
86 #elif defined(USE_PROTOCOL_CLI)
87 #include <pulsecore/protocol-cli.h>
88 #define protocol_new pa_protocol_cli_new
89 #define protocol_free pa_protocol_cli_free
90 #define TCPWRAP_SERVICE "pulseaudio-cli"
91 #define IPV4_PORT 4712
92 #define UNIX_SOCKET "cli"
93 #define MODULE_ARGUMENTS
94 #ifdef USE_TCP_SOCKETS
95 #include "module-cli-protocol-tcp-symdef.h"
96 #else
97 #include "module-cli-protocol-unix-symdef.h"
98 #endif
99 PA_MODULE_DESCRIPTION("Command line interface protocol "SOCKET_DESCRIPTION);
100 PA_MODULE_USAGE(SOCKET_USAGE);
101 #elif defined(USE_PROTOCOL_HTTP)
102 #include <pulsecore/protocol-http.h>
103 #define protocol_new pa_protocol_http_new
104 #define protocol_free pa_protocol_http_free
105 #define TCPWRAP_SERVICE "pulseaudio-http"
106 #define IPV4_PORT 4714
107 #define UNIX_SOCKET "http"
108 #define MODULE_ARGUMENTS
109 #ifdef USE_TCP_SOCKETS
110 #include "module-http-protocol-tcp-symdef.h"
111 #else
112 #include "module-http-protocol-unix-symdef.h"
113 #endif
114 PA_MODULE_DESCRIPTION("HTTP "SOCKET_DESCRIPTION);
115 PA_MODULE_USAGE(SOCKET_USAGE);
116 #elif defined(USE_PROTOCOL_NATIVE)
117 #include <pulsecore/protocol-native.h>
118 #define protocol_new pa_protocol_native_new
119 #define protocol_free pa_protocol_native_free
120 #define TCPWRAP_SERVICE "pulseaudio-native"
121 #define IPV4_PORT PA_NATIVE_DEFAULT_PORT
122 #define UNIX_SOCKET PA_NATIVE_DEFAULT_UNIX_SOCKET
123 #define MODULE_ARGUMENTS_COMMON "cookie", "auth-anonymous",
124 #ifdef USE_TCP_SOCKETS
125 #include "module-native-protocol-tcp-symdef.h"
126 #else
127 #include "module-native-protocol-unix-symdef.h"
128 #endif
129
130 #if defined(HAVE_CREDS) && !defined(USE_TCP_SOCKETS)
131 #define MODULE_ARGUMENTS MODULE_ARGUMENTS_COMMON "auth-group", "auth-group-enable",
132 #define AUTH_USAGE "auth-group=<system group to allow access> auth-group-enable=<enable auth by UNIX group?> "
133 #elif defined(USE_TCP_SOCKETS)
134 #define MODULE_ARGUMENTS MODULE_ARGUMENTS_COMMON "auth-ip-acl",
135 #define AUTH_USAGE "auth-ip-acl=<IP address ACL to allow access> "
136 #else
137 #define MODULE_ARGUMENTS MODULE_ARGUMENTS_COMMON
138 #define AUTH_USAGE
139 #endif
140
141 PA_MODULE_DESCRIPTION("Native protocol "SOCKET_DESCRIPTION);
142 PA_MODULE_USAGE("auth-anonymous=<don't check for cookies?> "
143 "cookie=<path to cookie file> "
144 AUTH_USAGE
145 SOCKET_USAGE);
146 #elif defined(USE_PROTOCOL_ESOUND)
147 #include <pulsecore/protocol-esound.h>
148 #include <pulsecore/esound.h>
149 #define protocol_new pa_protocol_esound_new
150 #define protocol_free pa_protocol_esound_free
151 #define TCPWRAP_SERVICE "esound"
152 #define IPV4_PORT ESD_DEFAULT_PORT
153 #define MODULE_ARGUMENTS_COMMON "sink", "source", "auth-anonymous", "cookie",
154 #ifdef USE_TCP_SOCKETS
155 #include "module-esound-protocol-tcp-symdef.h"
156 #else
157 #include "module-esound-protocol-unix-symdef.h"
158 #endif
159
160 #if defined(USE_TCP_SOCKETS)
161 #define MODULE_ARGUMENTS MODULE_ARGUMENTS_COMMON "auth-ip-acl",
162 #define AUTH_USAGE "auth-ip-acl=<IP address ACL to allow access> "
163 #else
164 #define MODULE_ARGUMENTS MODULE_ARGUMENTS_COMMON
165 #define AUTH_USAGE
166 #endif
167
168 PA_MODULE_DESCRIPTION("ESOUND protocol "SOCKET_DESCRIPTION);
169 PA_MODULE_USAGE("sink=<sink to connect to> "
170 "source=<source to connect to> "
171 "auth-anonymous=<don't verify cookies?> "
172 "cookie=<path to cookie file> "
173 AUTH_USAGE
174 SOCKET_USAGE);
175 #else
176 #error "Broken build system"
177 #endif
178
179 PA_MODULE_LOAD_ONCE(FALSE);
180 PA_MODULE_AUTHOR("Lennart Poettering");
181 PA_MODULE_VERSION(PACKAGE_VERSION);
182
183 static const char* const valid_modargs[] = {
184 MODULE_ARGUMENTS
185 #if defined(USE_TCP_SOCKETS)
186 "port",
187 "listen",
188 #else
189 "socket",
190 #endif
191 NULL
192 };
193
194 struct userdata {
195 #if defined(USE_TCP_SOCKETS)
196 void *protocol_ipv4;
197 void *protocol_ipv6;
198 #else
199 void *protocol_unix;
200 char *socket_path;
201 #endif
202 };
203
204 int pa__init(pa_module*m) {
205 pa_modargs *ma = NULL;
206 int ret = -1;
207 struct userdata *u = NULL;
208
209 #if defined(USE_TCP_SOCKETS)
210 pa_socket_server *s_ipv4 = NULL, *s_ipv6 = NULL;
211 uint32_t port = IPV4_PORT;
212 const char *listen_on;
213 #else
214 pa_socket_server *s;
215 int r;
216 #endif
217
218 pa_assert(m);
219
220 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
221 pa_log("Failed to parse module arguments");
222 goto finish;
223 }
224
225 u = pa_xnew0(struct userdata, 1);
226
227 #if defined(USE_TCP_SOCKETS)
228 if (pa_modargs_get_value_u32(ma, "port", &port) < 0 || port < 1 || port > 0xFFFF) {
229 pa_log("port= expects a numerical argument between 1 and 65535.");
230 goto fail;
231 }
232
233 listen_on = pa_modargs_get_value(ma, "listen", NULL);
234
235 if (listen_on) {
236 s_ipv6 = pa_socket_server_new_ipv6_string(m->core->mainloop, listen_on, port, TCPWRAP_SERVICE);
237 s_ipv4 = pa_socket_server_new_ipv4_string(m->core->mainloop, listen_on, port, TCPWRAP_SERVICE);
238 } else {
239 s_ipv6 = pa_socket_server_new_ipv6_any(m->core->mainloop, port, TCPWRAP_SERVICE);
240 s_ipv4 = pa_socket_server_new_ipv4_any(m->core->mainloop, port, TCPWRAP_SERVICE);
241 }
242
243 if (!s_ipv4 && !s_ipv6)
244 goto fail;
245
246 if (s_ipv4)
247 u->protocol_ipv4 = protocol_new(m->core, s_ipv4, m, ma);
248 if (s_ipv6)
249 u->protocol_ipv6 = protocol_new(m->core, s_ipv6, m, ma);
250
251 if (!u->protocol_ipv4 && !u->protocol_ipv6)
252 goto fail;
253
254 if (s_ipv6)
255 pa_socket_server_unref(s_ipv6);
256 if (s_ipv6)
257 pa_socket_server_unref(s_ipv4);
258
259 #else
260
261 #if defined(USE_PROTOCOL_ESOUND)
262
263 #if defined(USE_PER_USER_ESOUND_SOCKET)
264 u->socket_path = pa_sprintf_malloc("/tmp/.esd-%lu/socket", (unsigned long) getuid());
265 #else
266 u->socket_path = pa_xstrdup("/tmp/.esd/socket");
267 #endif
268
269 /* This socket doesn't reside in our own runtime dir but in
270 * /tmp/.esd/, hence we have to create the dir first */
271
272 if (pa_make_secure_parent_dir(u->socket_path, pa_in_system_mode() ? 0755 : 0700, (uid_t)-1, (gid_t)-1) < 0) {
273 pa_log("Failed to create socket directory '%s': %s\n", u->socket_path, pa_cstrerror(errno));
274 goto fail;
275 }
276
277 #else
278 if (!(u->socket_path = pa_runtime_path(pa_modargs_get_value(ma, "socket", UNIX_SOCKET)))) {
279 pa_log("Failed to generate socket path.");
280 goto fail;
281 }
282 #endif
283
284 if ((r = pa_unix_socket_remove_stale(u->socket_path)) < 0) {
285 pa_log("Failed to remove stale UNIX socket '%s': %s", u->socket_path, pa_cstrerror(errno));
286 goto fail;
287 } else if (r > 0)
288 pa_log_info("Removed stale UNIX socket '%s'.", u->socket_path);
289
290 if (!(s = pa_socket_server_new_unix(m->core->mainloop, u->socket_path)))
291 goto fail;
292
293 if (!(u->protocol_unix = protocol_new(m->core, s, m, ma)))
294 goto fail;
295
296 pa_socket_server_unref(s);
297
298 #endif
299
300 m->userdata = u;
301
302 ret = 0;
303
304 finish:
305 if (ma)
306 pa_modargs_free(ma);
307
308 return ret;
309
310 fail:
311 if (u) {
312 #if defined(USE_TCP_SOCKETS)
313 if (u->protocol_ipv4)
314 protocol_free(u->protocol_ipv4);
315 if (u->protocol_ipv6)
316 protocol_free(u->protocol_ipv6);
317 #else
318 if (u->protocol_unix)
319 protocol_free(u->protocol_unix);
320 pa_xfree(u->socket_path);
321 #endif
322
323 pa_xfree(u);
324 }
325
326 #if defined(USE_TCP_SOCKETS)
327 if (s_ipv4)
328 pa_socket_server_unref(s_ipv4);
329 if (s_ipv6)
330 pa_socket_server_unref(s_ipv6);
331 #else
332 if (s)
333 pa_socket_server_unref(s);
334 #endif
335
336 goto finish;
337 }
338
339 void pa__done(pa_module*m) {
340 struct userdata *u;
341
342 pa_assert(m);
343
344 u = m->userdata;
345
346 #if defined(USE_TCP_SOCKETS)
347 if (u->protocol_ipv4)
348 protocol_free(u->protocol_ipv4);
349 if (u->protocol_ipv6)
350 protocol_free(u->protocol_ipv6);
351 #else
352 if (u->protocol_unix)
353 protocol_free(u->protocol_unix);
354
355 #if defined(USE_PROTOCOL_ESOUND) && !defined(USE_PER_USER_ESOUND_SOCKET)
356 if (u->socket_path) {
357 char *p = pa_parent_dir(u->socket_path);
358 rmdir(p);
359 pa_xfree(p);
360 }
361 #endif
362
363 pa_xfree(u->socket_path);
364 #endif
365
366 pa_xfree(u);
367 }