]> code.delx.au - pulseaudio/blob - src/modules/module-protocol-stub.c
fa21b737e33490e61ee56c518a355e6d32b55ef2
[pulseaudio] / src / modules / module-protocol-stub.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 PulseAudio 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 PulseAudio 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 PulseAudio; 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 "../pulsecore/winsock.h"
44
45 #include <pulse/xmalloc.h>
46
47 #include <pulsecore/core-error.h>
48 #include <pulsecore/module.h>
49 #include <pulsecore/socket-server.h>
50 #include <pulsecore/socket-util.h>
51 #include <pulsecore/core-util.h>
52 #include <pulsecore/modargs.h>
53 #include <pulsecore/log.h>
54 #include <pulsecore/native-common.h>
55
56 #ifdef USE_TCP_SOCKETS
57 #define SOCKET_DESCRIPTION "(TCP sockets)"
58 #define SOCKET_USAGE "port=<TCP port number> loopback=<listen on loopback device only?> 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(SCM_CREDENTIALS) && !defined(USE_TCP_SOCKETS)
131 #define MODULE_ARGUMENTS MODULE_ARGUMENTS_COMMON "auth-group",
132 #define AUTH_USAGE "auth-group=<local group to allow access>"
133 #else
134 #define MODULE_ARGUMENTS MODULE_ARGUMENTS_COMMON
135 #define AUTH_USAGE
136 #endif
137
138 PA_MODULE_DESCRIPTION("Native protocol "SOCKET_DESCRIPTION)
139 PA_MODULE_USAGE("auth-anonymous=<don't check for cookies?> "
140 "cookie=<path to cookie file> "
141 AUTH_USAGE
142 SOCKET_USAGE)
143 #elif defined(USE_PROTOCOL_ESOUND)
144 #include <pulsecore/protocol-esound.h>
145 #include <pulsecore/esound.h>
146 #define protocol_new pa_protocol_esound_new
147 #define protocol_free pa_protocol_esound_free
148 #define TCPWRAP_SERVICE "esound"
149 #define IPV4_PORT ESD_DEFAULT_PORT
150 #define UNIX_SOCKET ESD_UNIX_SOCKET_NAME
151 #define MODULE_ARGUMENTS "sink", "source", "auth-anonymous", "cookie",
152 #ifdef USE_TCP_SOCKETS
153 #include "module-esound-protocol-tcp-symdef.h"
154 #else
155 #include "module-esound-protocol-unix-symdef.h"
156 #endif
157 PA_MODULE_DESCRIPTION("ESOUND protocol "SOCKET_DESCRIPTION)
158 PA_MODULE_USAGE("sink=<sink to connect to> "
159 "source=<source to connect to> "
160 "auth-anonymous=<don't verify cookies?> "
161 "cookie=<path to cookie file> "
162 SOCKET_USAGE)
163 #else
164 #error "Broken build system"
165 #endif
166
167 PA_MODULE_AUTHOR("Lennart Poettering")
168 PA_MODULE_VERSION(PACKAGE_VERSION)
169
170 static const char* const valid_modargs[] = {
171 MODULE_ARGUMENTS
172 #if defined(USE_TCP_SOCKETS)
173 "port",
174 "loopback",
175 "listen",
176 #else
177 "socket",
178 #endif
179 NULL
180 };
181
182 struct userdata {
183 #if defined(USE_TCP_SOCKETS)
184 void *protocol_ipv4;
185 void *protocol_ipv6;
186 #else
187 void *protocol_unix;
188 char *socket_path;
189 #endif
190 };
191
192 int pa__init(pa_core *c, pa_module*m) {
193 pa_modargs *ma = NULL;
194 int ret = -1;
195
196 struct userdata *u = NULL;
197
198 #if defined(USE_TCP_SOCKETS)
199 pa_socket_server *s_ipv4 = NULL, *s_ipv6 = NULL;
200 int loopback = 1;
201 uint32_t port = IPV4_PORT;
202 const char *listen_on;
203 #else
204 pa_socket_server *s;
205 int r;
206 char tmp[PATH_MAX];
207 #endif
208
209 assert(c && m);
210
211 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
212 pa_log(__FILE__": Failed to parse module arguments");
213 goto finish;
214 }
215
216 u = pa_xnew0(struct userdata, 1);
217
218 #if defined(USE_TCP_SOCKETS)
219 if (pa_modargs_get_value_boolean(ma, "loopback", &loopback) < 0) {
220 pa_log(__FILE__": loopback= expects a boolean argument.");
221 goto fail;
222 }
223
224 if (pa_modargs_get_value_u32(ma, "port", &port) < 0 || port < 1 || port > 0xFFFF) {
225 pa_log(__FILE__": port= expects a numerical argument between 1 and 65535.");
226 goto fail;
227 }
228
229 listen_on = pa_modargs_get_value(ma, "listen", NULL);
230
231 if (listen_on) {
232 s_ipv6 = pa_socket_server_new_ipv6_string(c->mainloop, listen_on, port, TCPWRAP_SERVICE);
233 s_ipv4 = pa_socket_server_new_ipv4_string(c->mainloop, listen_on, port, TCPWRAP_SERVICE);
234 } else if (loopback) {
235 s_ipv6 = pa_socket_server_new_ipv6_loopback(c->mainloop, port, TCPWRAP_SERVICE);
236 s_ipv4 = pa_socket_server_new_ipv4_loopback(c->mainloop, port, TCPWRAP_SERVICE);
237 } else {
238 s_ipv6 = pa_socket_server_new_ipv6_any(c->mainloop, port, TCPWRAP_SERVICE);
239 s_ipv4 = pa_socket_server_new_ipv4_any(c->mainloop, port, TCPWRAP_SERVICE);
240 }
241
242 if (!s_ipv4 && !s_ipv6)
243 goto fail;
244
245 if (s_ipv4)
246 if (!(u->protocol_ipv4 = protocol_new(c, s_ipv4, m, ma)))
247 pa_socket_server_unref(s_ipv4);
248
249 if (s_ipv6)
250 if (!(u->protocol_ipv6 = protocol_new(c, s_ipv6, m, ma)))
251 pa_socket_server_unref(s_ipv6);
252
253 if (!u->protocol_ipv4 && !u->protocol_ipv6)
254 goto fail;
255
256 #else
257
258 pa_runtime_path(pa_modargs_get_value(ma, "socket", UNIX_SOCKET), tmp, sizeof(tmp));
259 u->socket_path = pa_xstrdup(tmp);
260
261 #if defined(USE_PROTOCOL_ESOUND)
262
263 /* This socket doesn't reside in our own runtime dir but in
264 * /tmp/.esd/, hence we have to create the dir first */
265
266 if (pa_make_secure_parent_dir(u->socket_path, c->is_system_instance ? 0755 : 0700, getuid(), getgid()) < 0) {
267 pa_log(__FILE__": Failed to create socket directory: %s\n", pa_cstrerror(errno));
268 goto fail;
269 }
270 #endif
271
272 if ((r = pa_unix_socket_remove_stale(tmp)) < 0) {
273 pa_log(__FILE__": Failed to remove stale UNIX socket '%s': %s", tmp, pa_cstrerror(errno));
274 goto fail;
275 }
276
277 if (r)
278 pa_log(__FILE__": Removed stale UNIX socket '%s'.", tmp);
279
280 if (!(s = pa_socket_server_new_unix(c->mainloop, tmp)))
281 goto fail;
282
283 if (!(u->protocol_unix = protocol_new(c, s, m, ma)))
284 goto fail;
285
286 #endif
287
288 m->userdata = u;
289
290 ret = 0;
291
292 finish:
293 if (ma)
294 pa_modargs_free(ma);
295
296 return ret;
297
298 fail:
299 if (u) {
300 #if defined(USE_TCP_SOCKETS)
301 if (u->protocol_ipv4)
302 protocol_free(u->protocol_ipv4);
303 if (u->protocol_ipv6)
304 protocol_free(u->protocol_ipv6);
305 #else
306 if (u->protocol_unix)
307 protocol_free(u->protocol_unix);
308
309 if (u->socket_path)
310 pa_xfree(u->socket_path);
311 #endif
312
313 pa_xfree(u);
314 } else {
315 #if defined(USE_TCP_SOCKETS)
316 if (s_ipv4)
317 pa_socket_server_unref(s_ipv4);
318 if (s_ipv6)
319 pa_socket_server_unref(s_ipv6);
320 #else
321 if (s)
322 pa_socket_server_unref(s);
323 #endif
324 }
325
326 goto finish;
327 }
328
329 void pa__done(pa_core *c, pa_module*m) {
330 struct userdata *u;
331
332 assert(c);
333 assert(m);
334
335 u = m->userdata;
336
337 #if defined(USE_TCP_SOCKETS)
338 if (u->protocol_ipv4)
339 protocol_free(u->protocol_ipv4);
340 if (u->protocol_ipv6)
341 protocol_free(u->protocol_ipv6);
342 #else
343 if (u->protocol_unix)
344 protocol_free(u->protocol_unix);
345
346 #if defined(USE_PROTOCOL_ESOUND)
347 if (u->socket_path) {
348 char *p = pa_parent_dir(u->socket_path);
349 rmdir(p);
350 pa_xfree(p);
351 }
352 #endif
353
354
355 pa_xfree(u->socket_path);
356 #endif
357
358 pa_xfree(u);
359 }