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
35 #ifdef HAVE_SYS_SOCKET_H
36 #include <sys/socket.h>
38 #ifdef HAVE_ARPA_INET_H
39 #include <arpa/inet.h>
41 #ifdef HAVE_NETINET_IN_H
42 #include <netinet/in.h>
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>"
62 #define SOCKET_DESCRIPTION "(UNIX sockets)"
63 #define SOCKET_USAGE "socket=<path to UNIX socket>"
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"
77 #include "module-simple-protocol-unix-symdef.h"
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?> "
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"
99 #include "module-cli-protocol-unix-symdef.h"
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"
114 #include "module-http-protocol-unix-symdef.h"
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"
129 #include "module-native-protocol-unix-symdef.h"
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> "
139 #define MODULE_ARGUMENTS MODULE_ARGUMENTS_COMMON
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> "
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"
159 #include "module-esound-protocol-unix-symdef.h"
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> "
166 #define MODULE_ARGUMENTS MODULE_ARGUMENTS_COMMON
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> "
178 #error "Broken build system"
181 PA_MODULE_AUTHOR("Lennart Poettering")
182 PA_MODULE_VERSION(PACKAGE_VERSION
)
184 static const char* const valid_modargs
[] = {
186 #if defined(USE_TCP_SOCKETS)
196 #if defined(USE_TCP_SOCKETS)
205 int pa__init(pa_module
*m
) {
206 pa_modargs
*ma
= NULL
;
208 struct userdata
*u
= NULL
;
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
;
219 #if defined(USE_PROTOCOL_ESOUND)
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(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
);
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
);
249 if (!s_ipv4
&& !s_ipv6
)
253 if (!(u
->protocol_ipv4
= protocol_new(m
->core
, s_ipv4
, m
, ma
)))
254 pa_socket_server_unref(s_ipv4
);
257 if (!(u
->protocol_ipv6
= protocol_new(m
->core
, s_ipv6
, m
, ma
)))
258 pa_socket_server_unref(s_ipv6
);
260 if (!u
->protocol_ipv4
&& !u
->protocol_ipv6
)
265 #if defined(USE_PROTOCOL_ESOUND)
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
);
271 /* This socket doesn't reside in our own runtime dir but in
272 * /tmp/.esd/, hence we have to create the dir first */
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
));
280 pa_runtime_path(pa_modargs_get_value(ma
, "socket", UNIX_SOCKET
), tmp
, sizeof(tmp
));
281 u
->socket_path
= pa_xstrdup(tmp
);
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
));
290 pa_log("Removed stale UNIX socket '%s'.", tmp
);
292 if (!(s
= pa_socket_server_new_unix(m
->core
->mainloop
, tmp
)))
295 if (!(u
->protocol_unix
= protocol_new(m
->core
, s
, m
, ma
)))
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
);
318 if (u
->protocol_unix
)
319 protocol_free(u
->protocol_unix
);
322 pa_xfree(u
->socket_path
);
327 #if defined(USE_TCP_SOCKETS)
329 pa_socket_server_unref(s_ipv4
);
331 pa_socket_server_unref(s_ipv6
);
334 pa_socket_server_unref(s
);
341 void pa__done(pa_module
*m
) {
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
);
354 if (u
->protocol_unix
)
355 protocol_free(u
->protocol_unix
);
357 #if defined(USE_PROTOCOL_ESOUND)
358 if (u
->socket_path
) {
359 char *p
= pa_parent_dir(u
->socket_path
);
365 pa_xfree(u
->socket_path
);