]> code.delx.au - pulseaudio/blob - src/modules/module-protocol-stub.c
8eba352b6ae7c75e3dbc1de965ddbb29fda6233f
[pulseaudio] / src / modules / module-protocol-stub.c
1 /* $Id$ */
2
3 /***
4 This file is part of polypaudio.
5
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.
10
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.
15
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
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <string.h>
27 #include <errno.h>
28 #include <stdio.h>
29 #include <assert.h>
30 #include <unistd.h>
31 #include <limits.h>
32
33 #ifdef HAVE_SYS_SOCKET_H
34 #include <sys/socket.h>
35 #endif
36 #ifdef HAVE_ARPA_INET_H
37 #include <arpa/inet.h>
38 #endif
39 #ifdef HAVE_NETINET_IN_H
40 #include <netinet/in.h>
41 #endif
42
43 #include "../polypcore/winsock.h"
44
45 #include <polyp/error.h>
46 #include <polyp/xmalloc.h>
47
48 #include <polypcore/module.h>
49 #include <polypcore/socket-server.h>
50 #include <polypcore/socket-util.h>
51 #include <polypcore/core-util.h>
52 #include <polypcore/modargs.h>
53 #include <polypcore/log.h>
54 #include <polypcore/native-common.h>
55
56 #ifdef USE_TCP_SOCKETS
57 #define SOCKET_DESCRIPTION "(TCP sockets)"
58 #define SOCKET_USAGE "port=<TCP port number> loopback=<listen on loopback device only?> listen=<address to listen on>"
59 #else
60 #define SOCKET_DESCRIPTION "(UNIX sockets)"
61 #define SOCKET_USAGE "socket=<path to UNIX socket>"
62 #endif
63
64 #if defined(USE_PROTOCOL_SIMPLE)
65 #include <polypcore/protocol-simple.h>
66 #define protocol_new pa_protocol_simple_new
67 #define protocol_free pa_protocol_simple_free
68 #define TCPWRAP_SERVICE "polypaudio-simple"
69 #define IPV4_PORT 4711
70 #define UNIX_SOCKET "simple"
71 #define MODULE_ARGUMENTS "rate", "format", "channels", "sink", "source", "playback", "record",
72 #if defined(USE_TCP_SOCKETS)
73 #include "module-simple-protocol-tcp-symdef.h"
74 #else
75 #include "module-simple-protocol-unix-symdef.h"
76 #endif
77 PA_MODULE_DESCRIPTION("Simple protocol "SOCKET_DESCRIPTION)
78 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)
79 #elif defined(USE_PROTOCOL_CLI)
80 #include <polypcore/protocol-cli.h>
81 #define protocol_new pa_protocol_cli_new
82 #define protocol_free pa_protocol_cli_free
83 #define TCPWRAP_SERVICE "polypaudio-cli"
84 #define IPV4_PORT 4712
85 #define UNIX_SOCKET "cli"
86 #define MODULE_ARGUMENTS
87 #ifdef USE_TCP_SOCKETS
88 #include "module-cli-protocol-tcp-symdef.h"
89 #else
90 #include "module-cli-protocol-unix-symdef.h"
91 #endif
92 PA_MODULE_DESCRIPTION("Command line interface protocol "SOCKET_DESCRIPTION)
93 PA_MODULE_USAGE(SOCKET_USAGE)
94 #elif defined(USE_PROTOCOL_HTTP)
95 #include <polypcore/protocol-http.h>
96 #define protocol_new pa_protocol_http_new
97 #define protocol_free pa_protocol_http_free
98 #define TCPWRAP_SERVICE "polypaudio-http"
99 #define IPV4_PORT 4714
100 #define UNIX_SOCKET "http"
101 #define MODULE_ARGUMENTS
102 #ifdef USE_TCP_SOCKETS
103 #include "module-http-protocol-tcp-symdef.h"
104 #else
105 #include "module-http-protocol-unix-symdef.h"
106 #endif
107 PA_MODULE_DESCRIPTION("HTTP "SOCKET_DESCRIPTION)
108 PA_MODULE_USAGE(SOCKET_USAGE)
109 #elif defined(USE_PROTOCOL_NATIVE)
110 #include <polypcore/protocol-native.h>
111 #define protocol_new pa_protocol_native_new
112 #define protocol_free pa_protocol_native_free
113 #define TCPWRAP_SERVICE "polypaudio-native"
114 #define IPV4_PORT PA_NATIVE_DEFAULT_PORT
115 #define UNIX_SOCKET PA_NATIVE_DEFAULT_UNIX_SOCKET
116 #define MODULE_ARGUMENTS_COMMON "cookie", "auth-anonymous",
117 #ifdef USE_TCP_SOCKETS
118 #include "module-native-protocol-tcp-symdef.h"
119 #else
120 #include "module-native-protocol-unix-symdef.h"
121 #endif
122
123 #if defined(SCM_CREDENTIALS) && !defined(USE_TCP_SOCKETS)
124 #define MODULE_ARGUMENTS MODULE_ARGUMENTS_COMMON "auth-group",
125 #define AUTH_USAGE "auth-group=<local group to allow access>"
126 #else
127 #define MODULE_ARGUMENTS MODULE_ARGUMENTS_COMMON
128 #define AUTH_USAGE
129 #endif
130
131 PA_MODULE_DESCRIPTION("Native protocol "SOCKET_DESCRIPTION)
132 PA_MODULE_USAGE("auth-anonymous=<don't check for cookies?> cookie=<path to cookie file> "AUTH_USAGE SOCKET_USAGE)
133 #elif defined(USE_PROTOCOL_ESOUND)
134 #include <polypcore/protocol-esound.h>
135 #include <polypcore/esound.h>
136 #define protocol_new pa_protocol_esound_new
137 #define protocol_free pa_protocol_esound_free
138 #define TCPWRAP_SERVICE "esound"
139 #define IPV4_PORT ESD_DEFAULT_PORT
140 #define UNIX_SOCKET ESD_UNIX_SOCKET_NAME
141 #define MODULE_ARGUMENTS "sink", "source", "auth-anonymous", "cookie",
142 #ifdef USE_TCP_SOCKETS
143 #include "module-esound-protocol-tcp-symdef.h"
144 #else
145 #include "module-esound-protocol-unix-symdef.h"
146 #endif
147 PA_MODULE_DESCRIPTION("ESOUND protocol "SOCKET_DESCRIPTION)
148 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 #else
150 #error "Broken build system"
151 #endif
152
153 PA_MODULE_AUTHOR("Lennart Poettering")
154 PA_MODULE_VERSION(PACKAGE_VERSION)
155
156 static const char* const valid_modargs[] = {
157 MODULE_ARGUMENTS
158 #if defined(USE_TCP_SOCKETS)
159 "port",
160 "loopback",
161 "listen",
162 #else
163 "socket",
164 #endif
165 NULL
166 };
167
168 struct userdata {
169 #if defined(USE_TCP_SOCKETS)
170 void *protocol_ipv4;
171 void *protocol_ipv6;
172 #else
173 void *protocol_unix;
174 char *socket_path;
175 #endif
176 };
177
178 int pa__init(pa_core *c, pa_module*m) {
179 pa_modargs *ma = NULL;
180 int ret = -1;
181
182 struct userdata *u = NULL;
183
184 #if defined(USE_TCP_SOCKETS)
185 pa_socket_server *s_ipv4 = NULL, *s_ipv6 = NULL;
186 int loopback = 1;
187 uint32_t port = IPV4_PORT;
188 const char *listen_on;
189 #else
190 pa_socket_server *s;
191 int r;
192 const char *v;
193 char tmp[PATH_MAX];
194 #endif
195
196 assert(c && m);
197
198 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
199 pa_log(__FILE__": Failed to parse module arguments");
200 goto finish;
201 }
202
203 u = pa_xnew0(struct userdata, 1);
204
205 #if defined(USE_TCP_SOCKETS)
206 if (pa_modargs_get_value_boolean(ma, "loopback", &loopback) < 0) {
207 pa_log(__FILE__": loopback= expects a boolean argument.");
208 goto fail;
209 }
210
211 if (pa_modargs_get_value_u32(ma, "port", &port) < 0 || port < 1 || port > 0xFFFF) {
212 pa_log(__FILE__": port= expects a numerical argument between 1 and 65535.");
213 goto fail;
214 }
215
216 listen_on = pa_modargs_get_value(ma, "listen", NULL);
217
218 if (listen_on) {
219 s_ipv6 = pa_socket_server_new_ipv6_string(c->mainloop, listen_on, port, TCPWRAP_SERVICE);
220 s_ipv4 = pa_socket_server_new_ipv4_string(c->mainloop, listen_on, port, TCPWRAP_SERVICE);
221 } else if (loopback) {
222 s_ipv6 = pa_socket_server_new_ipv6_loopback(c->mainloop, port, TCPWRAP_SERVICE);
223 s_ipv4 = pa_socket_server_new_ipv4_loopback(c->mainloop, port, TCPWRAP_SERVICE);
224 } else {
225 s_ipv6 = pa_socket_server_new_ipv6_any(c->mainloop, port, TCPWRAP_SERVICE);
226 s_ipv4 = pa_socket_server_new_ipv4_any(c->mainloop, port, TCPWRAP_SERVICE);
227 }
228
229 if (!s_ipv4 && !s_ipv6)
230 goto fail;
231
232 if (s_ipv4)
233 if (!(u->protocol_ipv4 = protocol_new(c, s_ipv4, m, ma)))
234 pa_socket_server_unref(s_ipv4);
235
236 if (s_ipv6)
237 if (!(u->protocol_ipv6 = protocol_new(c, s_ipv6, m, ma)))
238 pa_socket_server_unref(s_ipv6);
239
240 if (!u->protocol_ipv4 && !u->protocol_ipv6)
241 goto fail;
242
243 #else
244 v = pa_modargs_get_value(ma, "socket", UNIX_SOCKET);
245 pa_runtime_path(v, tmp, sizeof(tmp));
246 u->socket_path = pa_xstrdup(tmp);
247
248 if (pa_make_secure_parent_dir(tmp) < 0) {
249 pa_log(__FILE__": Failed to create secure socket directory.");
250 goto fail;
251 }
252
253 if ((r = pa_unix_socket_remove_stale(tmp)) < 0) {
254 pa_log(__FILE__": Failed to remove stale UNIX socket '%s': %s", tmp, pa_cstrerror(errno));
255 goto fail;
256 }
257
258 if (r)
259 pa_log(__FILE__": Removed stale UNIX socket '%s'.", tmp);
260
261 if (!(s = pa_socket_server_new_unix(c->mainloop, tmp)))
262 goto fail;
263
264 if (!(u->protocol_unix = protocol_new(c, s, m, ma)))
265 goto fail;
266
267 #endif
268
269 m->userdata = u;
270
271 ret = 0;
272
273 finish:
274 if (ma)
275 pa_modargs_free(ma);
276
277 return ret;
278
279 fail:
280 if (u) {
281 #if defined(USE_TCP_SOCKETS)
282 if (u->protocol_ipv4)
283 protocol_free(u->protocol_ipv4);
284 if (u->protocol_ipv6)
285 protocol_free(u->protocol_ipv6);
286 #else
287 if (u->protocol_unix)
288 protocol_free(u->protocol_unix);
289
290 if (u->socket_path)
291 pa_xfree(u->socket_path);
292 #endif
293
294 pa_xfree(u);
295 } else {
296 #if defined(USE_TCP_SOCKETS)
297 if (s_ipv4)
298 pa_socket_server_unref(s_ipv4);
299 if (s_ipv6)
300 pa_socket_server_unref(s_ipv6);
301 #else
302 if (s)
303 pa_socket_server_unref(s);
304 #endif
305 }
306
307 goto finish;
308 }
309
310 void pa__done(pa_core *c, pa_module*m) {
311 struct userdata *u;
312
313 assert(c);
314 assert(m);
315
316 u = m->userdata;
317
318 #if defined(USE_TCP_SOCKETS)
319 if (u->protocol_ipv4)
320 protocol_free(u->protocol_ipv4);
321 if (u->protocol_ipv6)
322 protocol_free(u->protocol_ipv6);
323 #else
324 if (u->protocol_unix)
325 protocol_free(u->protocol_unix);
326
327 if (u->socket_path) {
328 char *p;
329
330 if ((p = pa_parent_dir(u->socket_path))) {
331 if (rmdir(p) < 0 && errno != ENOENT && errno != ENOTEMPTY)
332 pa_log(__FILE__": Failed to remove %s: %s.", u->socket_path, pa_cstrerror(errno));
333
334 pa_xfree(p);
335 }
336
337 pa_xfree(u->socket_path);
338 }
339 #endif
340
341 pa_xfree(u);
342 }