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
36 #ifdef HAVE_SYS_SOCKET_H
37 #include <sys/socket.h>
39 #ifdef HAVE_ARPA_INET_H
40 #include <arpa/inet.h>
42 #ifdef HAVE_NETINET_IN_H
43 #include <netinet/in.h>
46 #include "../pulsecore/winsock.h"
48 #include <pulse/xmalloc.h>
50 #include <pulsecore/core-error.h>
51 #include <pulsecore/module.h>
52 #include <pulsecore/socket-server.h>
53 #include <pulsecore/socket-util.h>
54 #include <pulsecore/core-util.h>
55 #include <pulsecore/modargs.h>
56 #include <pulsecore/log.h>
57 #include <pulsecore/native-common.h>
58 #include <pulsecore/creds.h>
60 #ifdef USE_TCP_SOCKETS
61 #define SOCKET_DESCRIPTION "(TCP sockets)"
62 #define SOCKET_USAGE "port=<TCP port number> listen=<address to listen on>"
64 #define SOCKET_DESCRIPTION "(UNIX sockets)"
65 #define SOCKET_USAGE "socket=<path to UNIX socket>"
68 #if defined(USE_PROTOCOL_SIMPLE)
69 #include <pulsecore/protocol-simple.h>
70 #define protocol_new pa_protocol_simple_new
71 #define protocol_free pa_protocol_simple_free
72 #define TCPWRAP_SERVICE "pulseaudio-simple"
73 #define IPV4_PORT 4711
74 #define UNIX_SOCKET "simple"
75 #define MODULE_ARGUMENTS "rate", "format", "channels", "sink", "source", "playback", "record",
76 #if defined(USE_TCP_SOCKETS)
77 #include "module-simple-protocol-tcp-symdef.h"
79 #include "module-simple-protocol-unix-symdef.h"
81 PA_MODULE_DESCRIPTION("Simple protocol "SOCKET_DESCRIPTION)
82 PA_MODULE_USAGE("rate=<sample rate> "
83 "format=<sample format> "
84 "channels=<number of channels> "
85 "sink=<sink to connect to> "
86 "source=<source to connect to> "
87 "playback=<enable playback?> "
88 "record=<enable record?> "
90 #elif defined(USE_PROTOCOL_CLI)
91 #include <pulsecore/protocol-cli.h>
92 #define protocol_new pa_protocol_cli_new
93 #define protocol_free pa_protocol_cli_free
94 #define TCPWRAP_SERVICE "pulseaudio-cli"
95 #define IPV4_PORT 4712
96 #define UNIX_SOCKET "cli"
97 #define MODULE_ARGUMENTS
98 #ifdef USE_TCP_SOCKETS
99 #include "module-cli-protocol-tcp-symdef.h"
101 #include "module-cli-protocol-unix-symdef.h"
103 PA_MODULE_DESCRIPTION("Command line interface protocol "SOCKET_DESCRIPTION)
104 PA_MODULE_USAGE(SOCKET_USAGE)
105 #elif defined(USE_PROTOCOL_HTTP)
106 #include <pulsecore/protocol-http.h>
107 #define protocol_new pa_protocol_http_new
108 #define protocol_free pa_protocol_http_free
109 #define TCPWRAP_SERVICE "pulseaudio-http"
110 #define IPV4_PORT 4714
111 #define UNIX_SOCKET "http"
112 #define MODULE_ARGUMENTS
113 #ifdef USE_TCP_SOCKETS
114 #include "module-http-protocol-tcp-symdef.h"
116 #include "module-http-protocol-unix-symdef.h"
118 PA_MODULE_DESCRIPTION("HTTP "SOCKET_DESCRIPTION)
119 PA_MODULE_USAGE(SOCKET_USAGE)
120 #elif defined(USE_PROTOCOL_NATIVE)
121 #include <pulsecore/protocol-native.h>
122 #define protocol_new pa_protocol_native_new
123 #define protocol_free pa_protocol_native_free
124 #define TCPWRAP_SERVICE "pulseaudio-native"
125 #define IPV4_PORT PA_NATIVE_DEFAULT_PORT
126 #define UNIX_SOCKET PA_NATIVE_DEFAULT_UNIX_SOCKET
127 #define MODULE_ARGUMENTS_COMMON "cookie", "auth-anonymous",
128 #ifdef USE_TCP_SOCKETS
129 #include "module-native-protocol-tcp-symdef.h"
131 #include "module-native-protocol-unix-symdef.h"
134 #if defined(HAVE_CREDS) && !defined(USE_TCP_SOCKETS)
135 #define MODULE_ARGUMENTS MODULE_ARGUMENTS_COMMON "auth-group", "auth-group-enable",
136 #define AUTH_USAGE "auth-group=<system group to allow access> auth-group-enable=<enable auth by UNIX group?> "
137 #elif defined(USE_TCP_SOCKETS)
138 #define MODULE_ARGUMENTS MODULE_ARGUMENTS_COMMON "auth-ip-acl",
139 #define AUTH_USAGE "auth-ip-acl=<IP address ACL to allow access> "
141 #define MODULE_ARGUMENTS MODULE_ARGUMENTS_COMMON
145 PA_MODULE_DESCRIPTION("Native protocol "SOCKET_DESCRIPTION)
146 PA_MODULE_USAGE("auth-anonymous=<don't check for cookies?> "
147 "cookie=<path to cookie file> "
150 #elif defined(USE_PROTOCOL_ESOUND)
151 #include <pulsecore/protocol-esound.h>
152 #include <pulsecore/esound.h>
153 #define protocol_new pa_protocol_esound_new
154 #define protocol_free pa_protocol_esound_free
155 #define TCPWRAP_SERVICE "esound"
156 #define IPV4_PORT ESD_DEFAULT_PORT
157 #define UNIX_SOCKET ESD_UNIX_SOCKET_NAME
158 #define MODULE_ARGUMENTS_COMMON "sink", "source", "auth-anonymous", "cookie",
159 #ifdef USE_TCP_SOCKETS
160 #include "module-esound-protocol-tcp-symdef.h"
162 #include "module-esound-protocol-unix-symdef.h"
165 #if defined(USE_TCP_SOCKETS)
166 #define MODULE_ARGUMENTS MODULE_ARGUMENTS_COMMON "auth-ip-acl",
167 #define AUTH_USAGE "auth-ip-acl=<IP address ACL to allow access> "
169 #define MODULE_ARGUMENTS MODULE_ARGUMENTS_COMMON
173 PA_MODULE_DESCRIPTION("ESOUND protocol "SOCKET_DESCRIPTION)
174 PA_MODULE_USAGE("sink=<sink to connect to> "
175 "source=<source to connect to> "
176 "auth-anonymous=<don't verify cookies?> "
177 "cookie=<path to cookie file> "
181 #error "Broken build system"
184 PA_MODULE_AUTHOR("Lennart Poettering")
185 PA_MODULE_VERSION(PACKAGE_VERSION)
187 static const char* const valid_modargs[] = {
189 #if defined(USE_TCP_SOCKETS)
199 #if defined(USE_TCP_SOCKETS)
208 int pa__init(pa_core *c, pa_module*m) {
209 pa_modargs *ma = NULL;
212 struct userdata *u = NULL;
214 #if defined(USE_TCP_SOCKETS)
215 pa_socket_server *s_ipv4 = NULL, *s_ipv6 = NULL;
216 uint32_t port = IPV4_PORT;
217 const char *listen_on;
226 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
227 pa_log("Failed to parse module arguments");
231 u = pa_xnew0(struct userdata, 1);
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.");
239 listen_on = pa_modargs_get_value(ma, "listen", NULL);
242 s_ipv6 = pa_socket_server_new_ipv6_string(c->mainloop, listen_on, port, TCPWRAP_SERVICE);
243 s_ipv4 = pa_socket_server_new_ipv4_string(c->mainloop, listen_on, port, TCPWRAP_SERVICE);
245 s_ipv6 = pa_socket_server_new_ipv6_any(c->mainloop, port, TCPWRAP_SERVICE);
246 s_ipv4 = pa_socket_server_new_ipv4_any(c->mainloop, port, TCPWRAP_SERVICE);
249 if (!s_ipv4 && !s_ipv6)
253 if (!(u->protocol_ipv4 = protocol_new(c, s_ipv4, m, ma)))
254 pa_socket_server_unref(s_ipv4);
257 if (!(u->protocol_ipv6 = protocol_new(c, s_ipv6, m, ma)))
258 pa_socket_server_unref(s_ipv6);
260 if (!u->protocol_ipv4 && !u->protocol_ipv6)
265 pa_runtime_path(pa_modargs_get_value(ma, "socket", UNIX_SOCKET), tmp, sizeof(tmp));
266 u->socket_path = pa_xstrdup(tmp);
268 #if defined(USE_PROTOCOL_ESOUND)
270 /* This socket doesn't reside in our own runtime dir but in
271 * /tmp/.esd/, hence we have to create the dir first */
273 if (pa_make_secure_parent_dir(u->socket_path, c->is_system_instance ? 0755 : 0700, (uid_t)-1, (gid_t)-1) < 0) {
274 pa_log("Failed to create socket directory: %s\n", pa_cstrerror(errno));
279 if ((r = pa_unix_socket_remove_stale(tmp)) < 0) {
280 pa_log("Failed to remove stale UNIX socket '%s': %s", tmp, pa_cstrerror(errno));
285 pa_log("Removed stale UNIX socket '%s'.", tmp);
287 if (!(s = pa_socket_server_new_unix(c->mainloop, tmp)))
290 if (!(u->protocol_unix = protocol_new(c, s, m, ma)))
307 #if defined(USE_TCP_SOCKETS)
308 if (u->protocol_ipv4)
309 protocol_free(u->protocol_ipv4);
310 if (u->protocol_ipv6)
311 protocol_free(u->protocol_ipv6);
313 if (u->protocol_unix)
314 protocol_free(u->protocol_unix);
317 pa_xfree(u->socket_path);
322 #if defined(USE_TCP_SOCKETS)
324 pa_socket_server_unref(s_ipv4);
326 pa_socket_server_unref(s_ipv6);
329 pa_socket_server_unref(s);
336 void pa__done(pa_core *c, pa_module*m) {
344 #if defined(USE_TCP_SOCKETS)
345 if (u->protocol_ipv4)
346 protocol_free(u->protocol_ipv4);
347 if (u->protocol_ipv6)
348 protocol_free(u->protocol_ipv6);
350 if (u->protocol_unix)
351 protocol_free(u->protocol_unix);
353 #if defined(USE_PROTOCOL_ESOUND)
354 if (u->socket_path) {
355 char *p = pa_parent_dir(u->socket_path);
362 pa_xfree(u->socket_path);