]> code.delx.au - pulseaudio/blob - src/modules/module-protocol-stub.c
* Add new "auth-group=" parameter to protocol-native-unix
[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 <polypcore/module.h>
46 #include <polypcore/socket-server.h>
47 #include <polypcore/socket-util.h>
48 #include <polypcore/util.h>
49 #include <polypcore/modargs.h>
50 #include <polypcore/log.h>
51 #include <polypcore/native-common.h>
52
53 #ifdef USE_TCP_SOCKETS
54 #define SOCKET_DESCRIPTION "(TCP sockets)"
55 #define SOCKET_USAGE "port=<TCP port number> loopback=<listen on loopback device only?> listen=<address to listen on>"
56 #else
57 #define SOCKET_DESCRIPTION "(UNIX sockets)"
58 #define SOCKET_USAGE "socket=<path to UNIX socket>"
59 #endif
60
61 #if defined(USE_PROTOCOL_SIMPLE)
62 #include <polypcore/protocol-simple.h>
63 #define protocol_new pa_protocol_simple_new
64 #define protocol_free pa_protocol_simple_free
65 #define TCPWRAP_SERVICE "polypaudio-simple"
66 #define IPV4_PORT 4711
67 #define UNIX_SOCKET "simple"
68 #define MODULE_ARGUMENTS "rate", "format", "channels", "sink", "source", "playback", "record",
69 #if defined(USE_TCP_SOCKETS)
70 #include "module-simple-protocol-tcp-symdef.h"
71 #else
72 #include "module-simple-protocol-unix-symdef.h"
73 #endif
74 PA_MODULE_DESCRIPTION("Simple protocol "SOCKET_DESCRIPTION)
75 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)
76 #elif defined(USE_PROTOCOL_CLI)
77 #include <polypcore/protocol-cli.h>
78 #define protocol_new pa_protocol_cli_new
79 #define protocol_free pa_protocol_cli_free
80 #define TCPWRAP_SERVICE "polypaudio-cli"
81 #define IPV4_PORT 4712
82 #define UNIX_SOCKET "cli"
83 #define MODULE_ARGUMENTS
84 #ifdef USE_TCP_SOCKETS
85 #include "module-cli-protocol-tcp-symdef.h"
86 #else
87 #include "module-cli-protocol-unix-symdef.h"
88 #endif
89 PA_MODULE_DESCRIPTION("Command line interface protocol "SOCKET_DESCRIPTION)
90 PA_MODULE_USAGE(SOCKET_USAGE)
91 #elif defined(USE_PROTOCOL_HTTP)
92 #include <polypcore/protocol-http.h>
93 #define protocol_new pa_protocol_http_new
94 #define protocol_free pa_protocol_http_free
95 #define TCPWRAP_SERVICE "polypaudio-http"
96 #define IPV4_PORT 4714
97 #define UNIX_SOCKET "http"
98 #define MODULE_ARGUMENTS
99 #ifdef USE_TCP_SOCKETS
100 #include "module-http-protocol-tcp-symdef.h"
101 #else
102 #include "module-http-protocol-unix-symdef.h"
103 #endif
104 PA_MODULE_DESCRIPTION("HTTP "SOCKET_DESCRIPTION)
105 PA_MODULE_USAGE(SOCKET_USAGE)
106 #elif defined(USE_PROTOCOL_NATIVE)
107 #include <polypcore/protocol-native.h>
108 #define protocol_new pa_protocol_native_new
109 #define protocol_free pa_protocol_native_free
110 #define TCPWRAP_SERVICE "polypaudio-native"
111 #define IPV4_PORT PA_NATIVE_DEFAULT_PORT
112 #define UNIX_SOCKET PA_NATIVE_DEFAULT_UNIX_SOCKET
113 #define MODULE_ARGUMENTS_COMMON "cookie", "auth-anonymous",
114 #ifdef USE_TCP_SOCKETS
115 #include "module-native-protocol-tcp-symdef.h"
116 #else
117 #include "module-native-protocol-unix-symdef.h"
118 #endif
119
120 #if defined(SCM_CREDENTIALS) && !defined(USE_TCP_SOCKETS)
121 #define MODULE_ARGUMENTS MODULE_ARGUMENTS_COMMON "auth-group",
122 #define AUTH_USAGE "auth-group=<local group to allow access>"
123 #else
124 #define MODULE_ARGUMENTS MODULE_ARGUMENTS_COMMON
125 #define AUTH_USAGE
126 #endif
127
128 PA_MODULE_DESCRIPTION("Native protocol "SOCKET_DESCRIPTION)
129 PA_MODULE_USAGE("auth-anonymous=<don't check for cookies?> cookie=<path to cookie file> "AUTH_USAGE SOCKET_USAGE)
130 #elif defined(USE_PROTOCOL_ESOUND)
131 #include <polypcore/protocol-esound.h>
132 #include <polypcore/esound.h>
133 #define protocol_new pa_protocol_esound_new
134 #define protocol_free pa_protocol_esound_free
135 #define TCPWRAP_SERVICE "esound"
136 #define IPV4_PORT ESD_DEFAULT_PORT
137 #define UNIX_SOCKET ESD_UNIX_SOCKET_NAME
138 #define MODULE_ARGUMENTS "sink", "source", "auth-anonymous", "cookie",
139 #ifdef USE_TCP_SOCKETS
140 #include "module-esound-protocol-tcp-symdef.h"
141 #else
142 #include "module-esound-protocol-unix-symdef.h"
143 #endif
144 PA_MODULE_DESCRIPTION("ESOUND protocol "SOCKET_DESCRIPTION)
145 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)
146 #else
147 #error "Broken build system"
148 #endif
149
150 PA_MODULE_AUTHOR("Lennart Poettering")
151 PA_MODULE_VERSION(PACKAGE_VERSION)
152
153 static const char* const valid_modargs[] = {
154 MODULE_ARGUMENTS
155 #if defined(USE_TCP_SOCKETS)
156 "port",
157 "loopback",
158 "listen",
159 #else
160 "socket",
161 #endif
162 NULL
163 };
164
165 static pa_socket_server *create_socket_server(pa_core *c, pa_modargs *ma) {
166 pa_socket_server *s;
167 #if defined(USE_TCP_SOCKETS)
168 int loopback = 1;
169 uint32_t port = IPV4_PORT;
170 const char *listen_on;
171
172 if (pa_modargs_get_value_boolean(ma, "loopback", &loopback) < 0) {
173 pa_log(__FILE__": loopback= expects a boolean argument.");
174 return NULL;
175 }
176
177 if (pa_modargs_get_value_u32(ma, "port", &port) < 0 || port < 1 || port > 0xFFFF) {
178 pa_log(__FILE__": port= expects a numerical argument between 1 and 65535.");
179 return NULL;
180 }
181
182 listen_on = pa_modargs_get_value(ma, "listen", NULL);
183
184 if (listen_on) {
185 if (!(s = pa_socket_server_new_ip_string(c->mainloop, listen_on, port, TCPWRAP_SERVICE)))
186 return NULL;
187 } else if (loopback) {
188 if (!(s = pa_socket_server_new_ip_loopback(c->mainloop, port, TCPWRAP_SERVICE)))
189 return NULL;
190 } else {
191 if (!(s = pa_socket_server_new_ip_any(c->mainloop, port, TCPWRAP_SERVICE)))
192 return NULL;
193 }
194
195 #else
196 int r;
197 const char *v;
198 char tmp[PATH_MAX];
199
200 v = pa_modargs_get_value(ma, "socket", UNIX_SOCKET);
201 assert(v);
202
203 pa_runtime_path(v, tmp, sizeof(tmp));
204
205 if (pa_make_secure_parent_dir(tmp) < 0) {
206 pa_log(__FILE__": Failed to create secure socket directory.");
207 return NULL;
208 }
209
210 if ((r = pa_unix_socket_remove_stale(tmp)) < 0) {
211 pa_log(__FILE__": Failed to remove stale UNIX socket '%s': %s", tmp, strerror(errno));
212 return NULL;
213 }
214
215 if (r)
216 pa_log(__FILE__": Removed stale UNIX socket '%s'.", tmp);
217
218 if (!(s = pa_socket_server_new_unix(c->mainloop, tmp)))
219 return NULL;
220
221 #endif
222 return s;
223 }
224
225 int pa__init(pa_core *c, pa_module*m) {
226 pa_socket_server *s;
227 pa_modargs *ma = NULL;
228 int ret = -1;
229 assert(c && m);
230
231 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
232 pa_log(__FILE__": Failed to parse module arguments");
233 goto finish;
234 }
235
236 if (!(s = create_socket_server(c, ma)))
237 goto finish;
238
239 if (!(m->userdata = protocol_new(c, s, m, ma))) {
240 pa_socket_server_unref(s);
241 goto finish;
242 }
243
244 ret = 0;
245
246 finish:
247 if (ma)
248 pa_modargs_free(ma);
249
250 return ret;
251 }
252
253 void pa__done(pa_core *c, pa_module*m) {
254 assert(c && m);
255
256 #if defined(USE_PROTOCOL_ESOUND) && !defined(USE_TCP_SOCKETS)
257 if (remove(ESD_UNIX_SOCKET_NAME) != 0)
258 pa_log("%s: Failed to remove %s : %s.", __FILE__, ESD_UNIX_SOCKET_NAME, strerror (errno));
259 if (remove(ESD_UNIX_SOCKET_DIR) != 0)
260 pa_log("%s: Failed to remove %s : %s.", __FILE__, ESD_UNIX_SOCKET_DIR, strerror (errno));
261 #endif
262
263 protocol_free(m->userdata);
264 }