Add a missing pa_xfree.
[pulseaudio.git] / src / modules / module-protocol-stub.c
bloba9bd850b09a2ad86002cf921b0026309fc6e3c46
1 /* $Id$ */
3 /***
4 This file is part of PulseAudio.
6 Copyright 2004-2006 Lennart Poettering
7 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
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.
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.
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 ***/
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
29 #include <string.h>
30 #include <errno.h>
31 #include <stdio.h>
32 #include <unistd.h>
33 #include <limits.h>
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
45 #include <pulse/xmalloc.h>
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>
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
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
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
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
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
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
181 PA_MODULE_LOAD_ONCE(FALSE);
182 PA_MODULE_AUTHOR("Lennart Poettering");
183 PA_MODULE_VERSION(PACKAGE_VERSION);
185 static const char* const valid_modargs[] = {
186 MODULE_ARGUMENTS
187 #if defined(USE_TCP_SOCKETS)
188 "port",
189 "listen",
190 #else
191 "socket",
192 #endif
193 NULL
196 struct userdata {
197 #if defined(USE_TCP_SOCKETS)
198 void *protocol_ipv4;
199 void *protocol_ipv6;
200 #else
201 void *protocol_unix;
202 char *socket_path;
203 #endif
206 int pa__init(pa_module*m) {
207 pa_modargs *ma = NULL;
208 int ret = -1;
209 struct userdata *u = NULL;
211 #if defined(USE_TCP_SOCKETS)
212 pa_socket_server *s_ipv4 = NULL, *s_ipv6 = NULL;
213 uint32_t port = IPV4_PORT;
214 const char *listen_on;
215 #else
216 pa_socket_server *s;
217 int r;
218 char tmp[PATH_MAX];
220 #if defined(USE_PROTOCOL_ESOUND)
221 #if defined(USE_PERUSER_ESOUND_SOCKET)
222 char esdsocketpath[PATH_MAX];
223 #else
224 const char esdsocketpath[] = "/tmp/.esd/socket";
225 #endif
226 #endif
227 #endif
229 pa_assert(m);
231 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
232 pa_log("Failed to parse module arguments");
233 goto finish;
236 u = pa_xnew0(struct userdata, 1);
238 #if defined(USE_TCP_SOCKETS)
239 if (pa_modargs_get_value_u32(ma, "port", &port) < 0 || port < 1 || port > 0xFFFF) {
240 pa_log("port= expects a numerical argument between 1 and 65535.");
241 goto fail;
244 listen_on = pa_modargs_get_value(ma, "listen", NULL);
246 if (listen_on) {
247 s_ipv6 = pa_socket_server_new_ipv6_string(m->core->mainloop, listen_on, port, TCPWRAP_SERVICE);
248 s_ipv4 = pa_socket_server_new_ipv4_string(m->core->mainloop, listen_on, port, TCPWRAP_SERVICE);
249 } else {
250 s_ipv6 = pa_socket_server_new_ipv6_any(m->core->mainloop, port, TCPWRAP_SERVICE);
251 s_ipv4 = pa_socket_server_new_ipv4_any(m->core->mainloop, port, TCPWRAP_SERVICE);
254 if (!s_ipv4 && !s_ipv6)
255 goto fail;
257 if (s_ipv4)
258 if (!(u->protocol_ipv4 = protocol_new(m->core, s_ipv4, m, ma)))
259 pa_socket_server_unref(s_ipv4);
261 if (s_ipv6)
262 if (!(u->protocol_ipv6 = protocol_new(m->core, s_ipv6, m, ma)))
263 pa_socket_server_unref(s_ipv6);
265 if (!u->protocol_ipv4 && !u->protocol_ipv6)
266 goto fail;
268 #else
270 #if defined(USE_PROTOCOL_ESOUND)
272 #if defined(USE_PERUSER_ESOUND_SOCKET)
273 snprintf(esdsocketpath, sizeof(esdsocketpath), "/tmp/.esd-%lu/socket", (unsigned long) getuid());
274 #endif
275 pa_runtime_path(pa_modargs_get_value(ma, "socket", esdsocketpath), tmp, sizeof(tmp));
276 u->socket_path = pa_xstrdup(tmp);
278 /* This socket doesn't reside in our own runtime dir but in
279 * /tmp/.esd/, hence we have to create the dir first */
281 if (pa_make_secure_parent_dir(u->socket_path, m->core->is_system_instance ? 0755 : 0700, (uid_t)-1, (gid_t)-1) < 0) {
282 pa_log("Failed to create socket directory '%s': %s\n", u->socket_path, pa_cstrerror(errno));
283 goto fail;
286 #else
287 pa_runtime_path(pa_modargs_get_value(ma, "socket", UNIX_SOCKET), tmp, sizeof(tmp));
288 u->socket_path = pa_xstrdup(tmp);
289 #endif
291 if ((r = pa_unix_socket_remove_stale(tmp)) < 0) {
292 pa_log("Failed to remove stale UNIX socket '%s': %s", tmp, pa_cstrerror(errno));
293 goto fail;
296 if (r)
297 pa_log("Removed stale UNIX socket '%s'.", tmp);
299 if (!(s = pa_socket_server_new_unix(m->core->mainloop, tmp)))
300 goto fail;
302 if (!(u->protocol_unix = protocol_new(m->core, s, m, ma)))
303 goto fail;
305 #endif
307 m->userdata = u;
309 ret = 0;
311 finish:
312 if (ma)
313 pa_modargs_free(ma);
315 return ret;
317 fail:
318 if (u) {
319 #if defined(USE_TCP_SOCKETS)
320 if (u->protocol_ipv4)
321 protocol_free(u->protocol_ipv4);
322 if (u->protocol_ipv6)
323 protocol_free(u->protocol_ipv6);
324 #else
325 if (u->protocol_unix)
326 protocol_free(u->protocol_unix);
328 if (u->socket_path)
329 pa_xfree(u->socket_path);
330 #endif
332 pa_xfree(u);
333 } else {
334 #if defined(USE_TCP_SOCKETS)
335 if (s_ipv4)
336 pa_socket_server_unref(s_ipv4);
337 if (s_ipv6)
338 pa_socket_server_unref(s_ipv6);
339 #else
340 if (s)
341 pa_socket_server_unref(s);
342 #endif
345 goto finish;
348 void pa__done(pa_module*m) {
349 struct userdata *u;
351 pa_assert(m);
353 u = m->userdata;
355 #if defined(USE_TCP_SOCKETS)
356 if (u->protocol_ipv4)
357 protocol_free(u->protocol_ipv4);
358 if (u->protocol_ipv6)
359 protocol_free(u->protocol_ipv6);
360 #else
361 if (u->protocol_unix)
362 protocol_free(u->protocol_unix);
364 #if defined(USE_PROTOCOL_ESOUND)
365 if (u->socket_path) {
366 char *p = pa_parent_dir(u->socket_path);
367 rmdir(p);
368 pa_xfree(p);
370 #endif
372 pa_xfree(u->socket_path);
373 #endif
375 pa_xfree(u);