4 This file is part of polypaudio.
6 polypaudio 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.
11 polypaudio 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.
16 You should have received a copy of the GNU Lesser General Public License
17 along with polypaudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
33 #ifdef HAVE_SYS_SOCKET_H
34 #include <sys/socket.h>
36 #ifdef HAVE_ARPA_INET_H
37 #include <arpa/inet.h>
39 #ifdef HAVE_NETINET_IN_H
40 #include <netinet/in.h>
43 #include "../polypcore/winsock.h"
45 #include <polyp/xmalloc.h>
47 #include <polypcore/module.h>
48 #include <polypcore/socket-server.h>
49 #include <polypcore/socket-util.h>
50 #include <polypcore/util.h>
51 #include <polypcore/modargs.h>
52 #include <polypcore/log.h>
53 #include <polypcore/native-common.h>
55 #ifdef USE_TCP_SOCKETS
56 #define SOCKET_DESCRIPTION "(TCP sockets)"
57 #define SOCKET_USAGE "port=<TCP port number> loopback=<listen on loopback device only?> listen=<address to listen on>"
59 #define SOCKET_DESCRIPTION "(UNIX sockets)"
60 #define SOCKET_USAGE "socket=<path to UNIX socket>"
63 #if defined(USE_PROTOCOL_SIMPLE)
64 #include <polypcore/protocol-simple.h>
65 #define protocol_new pa_protocol_simple_new
66 #define protocol_free pa_protocol_simple_free
67 #define TCPWRAP_SERVICE "polypaudio-simple"
68 #define IPV4_PORT 4711
69 #define UNIX_SOCKET "simple"
70 #define MODULE_ARGUMENTS "rate", "format", "channels", "sink", "source", "playback", "record",
71 #if defined(USE_TCP_SOCKETS)
72 #include "module-simple-protocol-tcp-symdef.h"
74 #include "module-simple-protocol-unix-symdef.h"
76 PA_MODULE_DESCRIPTION("Simple protocol "SOCKET_DESCRIPTION)
77 PA_MODULE_USAGE("rate=<sample rate> format=<sample format> channels=<number of channels> sink=<sink to connect to> source=<source to connect to> playback=<enable playback?> record=<enable record?> "SOCKET_USAGE)
78 #elif defined(USE_PROTOCOL_CLI)
79 #include <polypcore/protocol-cli.h>
80 #define protocol_new pa_protocol_cli_new
81 #define protocol_free pa_protocol_cli_free
82 #define TCPWRAP_SERVICE "polypaudio-cli"
83 #define IPV4_PORT 4712
84 #define UNIX_SOCKET "cli"
85 #define MODULE_ARGUMENTS
86 #ifdef USE_TCP_SOCKETS
87 #include "module-cli-protocol-tcp-symdef.h"
89 #include "module-cli-protocol-unix-symdef.h"
91 PA_MODULE_DESCRIPTION("Command line interface protocol "SOCKET_DESCRIPTION)
92 PA_MODULE_USAGE(SOCKET_USAGE)
93 #elif defined(USE_PROTOCOL_HTTP)
94 #include <polypcore/protocol-http.h>
95 #define protocol_new pa_protocol_http_new
96 #define protocol_free pa_protocol_http_free
97 #define TCPWRAP_SERVICE "polypaudio-http"
98 #define IPV4_PORT 4714
99 #define UNIX_SOCKET "http"
100 #define MODULE_ARGUMENTS
101 #ifdef USE_TCP_SOCKETS
102 #include "module-http-protocol-tcp-symdef.h"
104 #include "module-http-protocol-unix-symdef.h"
106 PA_MODULE_DESCRIPTION("HTTP "SOCKET_DESCRIPTION)
107 PA_MODULE_USAGE(SOCKET_USAGE)
108 #elif defined(USE_PROTOCOL_NATIVE)
109 #include <polypcore/protocol-native.h>
110 #define protocol_new pa_protocol_native_new
111 #define protocol_free pa_protocol_native_free
112 #define TCPWRAP_SERVICE "polypaudio-native"
113 #define IPV4_PORT PA_NATIVE_DEFAULT_PORT
114 #define UNIX_SOCKET PA_NATIVE_DEFAULT_UNIX_SOCKET
115 #define MODULE_ARGUMENTS_COMMON "cookie", "auth-anonymous",
116 #ifdef USE_TCP_SOCKETS
117 #include "module-native-protocol-tcp-symdef.h"
119 #include "module-native-protocol-unix-symdef.h"
122 #if defined(SCM_CREDENTIALS) && !defined(USE_TCP_SOCKETS)
123 #define MODULE_ARGUMENTS MODULE_ARGUMENTS_COMMON "auth-group",
124 #define AUTH_USAGE "auth-group=<local group to allow access>"
126 #define MODULE_ARGUMENTS MODULE_ARGUMENTS_COMMON
130 PA_MODULE_DESCRIPTION("Native protocol "SOCKET_DESCRIPTION)
131 PA_MODULE_USAGE("auth-anonymous=<don't check for cookies?> cookie=<path to cookie file> "AUTH_USAGE SOCKET_USAGE)
132 #elif defined(USE_PROTOCOL_ESOUND)
133 #include <polypcore/protocol-esound.h>
134 #include <polypcore/esound.h>
135 #define protocol_new pa_protocol_esound_new
136 #define protocol_free pa_protocol_esound_free
137 #define TCPWRAP_SERVICE "esound"
138 #define IPV4_PORT ESD_DEFAULT_PORT
139 #define UNIX_SOCKET ESD_UNIX_SOCKET_NAME
140 #define MODULE_ARGUMENTS "sink", "source", "auth-anonymous", "cookie",
141 #ifdef USE_TCP_SOCKETS
142 #include "module-esound-protocol-tcp-symdef.h"
144 #include "module-esound-protocol-unix-symdef.h"
146 PA_MODULE_DESCRIPTION("ESOUND protocol "SOCKET_DESCRIPTION)
147 PA_MODULE_USAGE("sink=<sink to connect to> source=<source to connect to> auth-anonymous=<don't check for cookies?> cookie=<path to cookie file> "SOCKET_USAGE)
149 #error "Broken build system"
152 PA_MODULE_AUTHOR("Lennart Poettering")
153 PA_MODULE_VERSION(PACKAGE_VERSION)
155 static const char* const valid_modargs[] = {
157 #if defined(USE_TCP_SOCKETS)
168 #if defined(USE_TCP_SOCKETS)
177 int pa__init(pa_core *c, pa_module*m) {
178 pa_modargs *ma = NULL;
181 struct userdata *u = NULL;
183 #if defined(USE_TCP_SOCKETS)
184 pa_socket_server *s_ipv4 = NULL, *s_ipv6 = NULL;
186 uint32_t port = IPV4_PORT;
187 const char *listen_on;
197 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
198 pa_log(__FILE__": Failed to parse module arguments");
202 u = pa_xnew0(struct userdata, 1);
204 #if defined(USE_TCP_SOCKETS)
205 if (pa_modargs_get_value_boolean(ma, "loopback", &loopback) < 0) {
206 pa_log(__FILE__": loopback= expects a boolean argument.");
210 if (pa_modargs_get_value_u32(ma, "port", &port) < 0 || port < 1 || port > 0xFFFF) {
211 pa_log(__FILE__": port= expects a numerical argument between 1 and 65535.");
215 listen_on = pa_modargs_get_value(ma, "listen", NULL);
218 s_ipv6 = pa_socket_server_new_ipv6_string(c->mainloop, listen_on, port, TCPWRAP_SERVICE);
219 s_ipv4 = pa_socket_server_new_ipv4_string(c->mainloop, listen_on, port, TCPWRAP_SERVICE);
220 } else if (loopback) {
221 s_ipv6 = pa_socket_server_new_ipv6_loopback(c->mainloop, port, TCPWRAP_SERVICE);
222 s_ipv4 = pa_socket_server_new_ipv4_loopback(c->mainloop, port, TCPWRAP_SERVICE);
224 s_ipv6 = pa_socket_server_new_ipv6_any(c->mainloop, port, TCPWRAP_SERVICE);
225 s_ipv4 = pa_socket_server_new_ipv4_any(c->mainloop, port, TCPWRAP_SERVICE);
228 if (!s_ipv4 && !s_ipv6)
232 if (!(u->protocol_ipv4 = protocol_new(c, s_ipv4, m, ma)))
233 pa_socket_server_unref(s_ipv4);
236 if (!(u->protocol_ipv6 = protocol_new(c, s_ipv6, m, ma)))
237 pa_socket_server_unref(s_ipv6);
239 if (!u->protocol_ipv4 && !u->protocol_ipv6)
243 v = pa_modargs_get_value(ma, "socket", UNIX_SOCKET);
244 pa_runtime_path(v, tmp, sizeof(tmp));
245 u->socket_path = pa_xstrdup(tmp);
247 if (pa_make_secure_parent_dir(tmp) < 0) {
248 pa_log(__FILE__": Failed to create secure socket directory.");
252 if ((r = pa_unix_socket_remove_stale(tmp)) < 0) {
253 pa_log(__FILE__": Failed to remove stale UNIX socket '%s': %s", tmp, strerror(errno));
258 pa_log(__FILE__": Removed stale UNIX socket '%s'.", tmp);
260 if (!(s = pa_socket_server_new_unix(c->mainloop, tmp)))
263 if (!(u->protocol_unix = protocol_new(c, s, m, ma)))
280 #if defined(USE_TCP_SOCKETS)
281 if (u->protocol_ipv4)
282 protocol_free(u->protocol_ipv4);
283 if (u->protocol_ipv6)
284 protocol_free(u->protocol_ipv6);
286 if (u->protocol_unix)
287 protocol_free(u->protocol_unix);
290 pa_xfree(u->socket_path);
295 #if defined(USE_TCP_SOCKETS)
297 pa_socket_server_unref(s_ipv4);
299 pa_socket_server_unref(s_ipv6);
302 pa_socket_server_unref(s);
309 void pa__done(pa_core *c, pa_module*m) {
317 #if defined(USE_TCP_SOCKETS)
318 if (u->protocol_ipv4)
319 protocol_free(u->protocol_ipv4);
320 if (u->protocol_ipv6)
321 protocol_free(u->protocol_ipv6);
323 if (u->protocol_unix)
324 protocol_free(u->protocol_unix);
326 if (u->socket_path) {
329 if ((p = pa_parent_dir(u->socket_path))) {
330 if (rmdir(p) < 0 && errno != ENOENT && errno != ENOTEMPTY)
331 pa_log(__FILE__": Failed to remove %s: %s.", u->socket_path, strerror(errno));
336 pa_xfree(u->socket_path);