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