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