]> code.delx.au - pulseaudio/blob - src/modules/module-protocol-stub.c
Move xmalloc to the public side (libpolyp).
[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/xmalloc.h>
46
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>
54
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>"
58 #else
59 #define SOCKET_DESCRIPTION "(UNIX sockets)"
60 #define SOCKET_USAGE "socket=<path to UNIX socket>"
61 #endif
62
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"
73 #else
74 #include "module-simple-protocol-unix-symdef.h"
75 #endif
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"
88 #else
89 #include "module-cli-protocol-unix-symdef.h"
90 #endif
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"
103 #else
104 #include "module-http-protocol-unix-symdef.h"
105 #endif
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"
118 #else
119 #include "module-native-protocol-unix-symdef.h"
120 #endif
121
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>"
125 #else
126 #define MODULE_ARGUMENTS MODULE_ARGUMENTS_COMMON
127 #define AUTH_USAGE
128 #endif
129
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"
143 #else
144 #include "module-esound-protocol-unix-symdef.h"
145 #endif
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)
148 #else
149 #error "Broken build system"
150 #endif
151
152 PA_MODULE_AUTHOR("Lennart Poettering")
153 PA_MODULE_VERSION(PACKAGE_VERSION)
154
155 static const char* const valid_modargs[] = {
156 MODULE_ARGUMENTS
157 #if defined(USE_TCP_SOCKETS)
158 "port",
159 "loopback",
160 "listen",
161 #else
162 "socket",
163 #endif
164 NULL
165 };
166
167 struct userdata {
168 #if defined(USE_TCP_SOCKETS)
169 void *protocol_ipv4;
170 void *protocol_ipv6;
171 #else
172 void *protocol_unix;
173 char *socket_path;
174 #endif
175 };
176
177 int pa__init(pa_core *c, pa_module*m) {
178 pa_modargs *ma = NULL;
179 int ret = -1;
180
181 struct userdata *u = NULL;
182
183 #if defined(USE_TCP_SOCKETS)
184 pa_socket_server *s_ipv4 = NULL, *s_ipv6 = NULL;
185 int loopback = 1;
186 uint32_t port = IPV4_PORT;
187 const char *listen_on;
188 #else
189 pa_socket_server *s;
190 int r;
191 const char *v;
192 char tmp[PATH_MAX];
193 #endif
194
195 assert(c && m);
196
197 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
198 pa_log(__FILE__": Failed to parse module arguments");
199 goto finish;
200 }
201
202 u = pa_xnew0(struct userdata, 1);
203
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.");
207 goto fail;
208 }
209
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.");
212 goto fail;
213 }
214
215 listen_on = pa_modargs_get_value(ma, "listen", NULL);
216
217 if (listen_on) {
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);
223 } else {
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);
226 }
227
228 if (!s_ipv4 && !s_ipv6)
229 goto fail;
230
231 if (s_ipv4)
232 if (!(u->protocol_ipv4 = protocol_new(c, s_ipv4, m, ma)))
233 pa_socket_server_unref(s_ipv4);
234
235 if (s_ipv6)
236 if (!(u->protocol_ipv6 = protocol_new(c, s_ipv6, m, ma)))
237 pa_socket_server_unref(s_ipv6);
238
239 if (!u->protocol_ipv4 && !u->protocol_ipv6)
240 goto fail;
241
242 #else
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);
246
247 if (pa_make_secure_parent_dir(tmp) < 0) {
248 pa_log(__FILE__": Failed to create secure socket directory.");
249 goto fail;
250 }
251
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));
254 goto fail;
255 }
256
257 if (r)
258 pa_log(__FILE__": Removed stale UNIX socket '%s'.", tmp);
259
260 if (!(s = pa_socket_server_new_unix(c->mainloop, tmp)))
261 goto fail;
262
263 if (!(u->protocol_unix = protocol_new(c, s, m, ma)))
264 goto fail;
265
266 #endif
267
268 m->userdata = u;
269
270 ret = 0;
271
272 finish:
273 if (ma)
274 pa_modargs_free(ma);
275
276 return ret;
277
278 fail:
279 if (u) {
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);
285 #else
286 if (u->protocol_unix)
287 protocol_free(u->protocol_unix);
288
289 if (u->socket_path)
290 pa_xfree(u->socket_path);
291 #endif
292
293 pa_xfree(u);
294 } else {
295 #if defined(USE_TCP_SOCKETS)
296 if (s_ipv4)
297 pa_socket_server_unref(s_ipv4);
298 if (s_ipv6)
299 pa_socket_server_unref(s_ipv6);
300 #else
301 if (s)
302 pa_socket_server_unref(s);
303 #endif
304 }
305
306 goto finish;
307 }
308
309 void pa__done(pa_core *c, pa_module*m) {
310 struct userdata *u;
311
312 assert(c);
313 assert(m);
314
315 u = m->userdata;
316
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);
322 #else
323 if (u->protocol_unix)
324 protocol_free(u->protocol_unix);
325
326 if (u->socket_path) {
327 char *p;
328
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));
332
333 pa_xfree(p);
334 }
335
336 pa_xfree(u->socket_path);
337 }
338 #endif
339
340 pa_xfree(u);
341 }