]> code.delx.au - pulseaudio/blob - polyp/module-protocol-stub.c
6a0f88a28b8e8d5274670d4d13bb11142f7ab714
[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 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 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_NATIVE)
89 #include "protocol-native.h"
90 #define protocol_new pa_protocol_native_new
91 #define protocol_free pa_protocol_native_free
92 #define TCPWRAP_SERVICE "polypaudio-native"
93 #define IPV4_PORT PA_NATIVE_DEFAULT_PORT
94 #define UNIX_SOCKET PA_NATIVE_DEFAULT_UNIX_SOCKET
95 #define MODULE_ARGUMENTS "public", "cookie",
96 #ifdef USE_TCP_SOCKETS
97 #include "module-native-protocol-tcp-symdef.h"
98 #elif defined(USE_TCP6_SOCKETS)
99 #include "module-native-protocol-tcp6-symdef.h"
100 #else
101 #include "module-native-protocol-unix-symdef.h"
102 #endif
103 PA_MODULE_DESCRIPTION("Native protocol "SOCKET_DESCRIPTION)
104 PA_MODULE_USAGE("public=<don't check for cookies?> cookie=<path to cookie file> "SOCKET_USAGE)
105 #elif defined(USE_PROTOCOL_ESOUND)
106 #include "protocol-esound.h"
107 #include "esound.h"
108 #define protocol_new pa_protocol_esound_new
109 #define protocol_free pa_protocol_esound_free
110 #define TCPWRAP_SERVICE "esound"
111 #define IPV4_PORT ESD_DEFAULT_PORT
112 #define UNIX_SOCKET ESD_UNIX_SOCKET_NAME
113 #define MODULE_ARGUMENTS "sink", "source", "public", "cookie",
114 #ifdef USE_TCP_SOCKETS
115 #include "module-esound-protocol-tcp-symdef.h"
116 #elif defined(USE_TCP6_SOCKETS)
117 #include "module-esound-protocol-tcp6-symdef.h"
118 #else
119 #include "module-esound-protocol-unix-symdef.h"
120 #endif
121 PA_MODULE_DESCRIPTION("EsounD protocol "SOCKET_DESCRIPTION)
122 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)
123 #else
124 #error "Broken build system"
125 #endif
126
127 PA_MODULE_AUTHOR("Lennart Poettering")
128 PA_MODULE_VERSION(PACKAGE_VERSION)
129
130 static const char* const valid_modargs[] = {
131 MODULE_ARGUMENTS
132 #if defined(USE_TCP_SOCKETS) || defined(USE_TCP6_SOCKETS)
133 "port",
134 "loopback",
135 #else
136 "socket",
137 #endif
138 NULL
139 };
140
141 static struct pa_socket_server *create_socket_server(struct pa_core *c, struct pa_modargs *ma) {
142 struct pa_socket_server *s;
143 #if defined(USE_TCP_SOCKETS) || defined(USE_TCP6_SOCKETS)
144 int loopback = 1;
145 uint32_t port = IPV4_PORT;
146
147 if (pa_modargs_get_value_boolean(ma, "loopback", &loopback) < 0) {
148 pa_log(__FILE__": loopback= expects a numerical argument.\n");
149 return NULL;
150 }
151
152 if (pa_modargs_get_value_u32(ma, "port", &port) < 0 || port < 1 || port > 0xFFFF) {
153 pa_log(__FILE__": port= expects a numerical argument between 1 and 65535.\n");
154 return NULL;
155 }
156
157 #ifdef USE_TCP6_SOCKETS
158 if (!(s = pa_socket_server_new_ipv6(c->mainloop, loopback ? (uint8_t*) &in6addr_loopback : (uint8_t*) &in6addr_any, port)))
159 return NULL;
160 #else
161 if (!(s = pa_socket_server_new_ipv4(c->mainloop, loopback ? INADDR_LOOPBACK : INADDR_ANY, port, TCPWRAP_SERVICE)))
162 return NULL;
163 #endif
164
165 #else
166 int r;
167 const char *v;
168 char tmp[PATH_MAX];
169
170 v = pa_modargs_get_value(ma, "socket", UNIX_SOCKET);
171 assert(v);
172
173 pa_runtime_path(v, tmp, sizeof(tmp));
174
175 if (pa_make_secure_parent_dir(tmp) < 0) {
176 pa_log(__FILE__": Failed to create secure socket directory.\n");
177 return NULL;
178 }
179
180 if ((r = pa_unix_socket_remove_stale(tmp)) < 0) {
181 pa_log(__FILE__": Failed to remove stale UNIX socket '%s': %s\n", tmp, strerror(errno));
182 return NULL;
183 }
184
185 if (r)
186 pa_log(__FILE__": Removed stale UNIX socket '%s'.", tmp);
187
188 if (!(s = pa_socket_server_new_unix(c->mainloop, tmp)))
189 return NULL;
190
191 #endif
192 return s;
193 }
194
195 int pa__init(struct pa_core *c, struct pa_module*m) {
196 struct pa_socket_server *s;
197 struct pa_modargs *ma = NULL;
198 int ret = -1;
199 assert(c && m);
200
201 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
202 pa_log(__FILE__": Failed to parse module arguments\n");
203 goto finish;
204 }
205
206 if (!(s = create_socket_server(c, ma)))
207 goto finish;
208
209 if (!(m->userdata = protocol_new(c, s, m, ma))) {
210 pa_socket_server_unref(s);
211 goto finish;
212 }
213
214 ret = 0;
215
216 finish:
217 if (ma)
218 pa_modargs_free(ma);
219
220 return ret;
221 }
222
223 void pa__done(struct pa_core *c, struct pa_module*m) {
224 assert(c && m);
225
226 protocol_free(m->userdata);
227 }