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_LOAD_ONCE(FALSE
);
182 PA_MODULE_AUTHOR("Lennart Poettering");
183 PA_MODULE_VERSION(PACKAGE_VERSION
);
185 static const char* const valid_modargs
[] = {
187 #if defined(USE_TCP_SOCKETS)
197 #if defined(USE_TCP_SOCKETS)
206 int pa__init(pa_module
*m
) {
207 pa_modargs
*ma
= NULL
;
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
;
220 #if defined(USE_PROTOCOL_ESOUND)
221 #if defined(USE_PERUSER_ESOUND_SOCKET)
222 char esdsocketpath
[PATH_MAX
];
224 const char esdsocketpath
[] = "/tmp/.esd/socket";
231 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
232 pa_log("Failed to parse module arguments");
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.");
244 listen_on
= pa_modargs_get_value(ma
, "listen", NULL
);
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
);
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
)
258 if (!(u
->protocol_ipv4
= protocol_new(m
->core
, s_ipv4
, m
, ma
)))
259 pa_socket_server_unref(s_ipv4
);
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
)
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());
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
));
287 pa_runtime_path(pa_modargs_get_value(ma
, "socket", UNIX_SOCKET
), tmp
, sizeof(tmp
));
288 u
->socket_path
= pa_xstrdup(tmp
);
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
));
297 pa_log("Removed stale UNIX socket '%s'.", tmp
);
299 if (!(s
= pa_socket_server_new_unix(m
->core
->mainloop
, tmp
)))
302 if (!(u
->protocol_unix
= protocol_new(m
->core
, s
, m
, ma
)))
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
);
325 if (u
->protocol_unix
)
326 protocol_free(u
->protocol_unix
);
329 pa_xfree(u
->socket_path
);
334 #if defined(USE_TCP_SOCKETS)
336 pa_socket_server_unref(s_ipv4
);
338 pa_socket_server_unref(s_ipv6
);
341 pa_socket_server_unref(s
);
348 void pa__done(pa_module
*m
) {
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
);
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
);
372 pa_xfree(u
->socket_path
);