b69716cc73280270e3dd1a08457435a749e8c8d7
[profile/ivi/pulseaudio.git] / 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?>"
56 #elif defined(USE_TCP6_SOCKETS)
57 #define SOCKET_DESCRIPTION "(TCP/IPv6 sockets)"
58 #define SOCKET_USAGE "port=<TCP port number> loopback=<listen on loopback device only?>"
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   #elif defined(USE_TCP6_SOCKETS)
75     #include "module-simple-protocol-tcp6-symdef.h"
76   #else
77     #include "module-simple-protocol-unix-symdef.h"
78   #endif
79   PA_MODULE_DESCRIPTION("Simple protocol "SOCKET_DESCRIPTION)
80   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)
81 #elif defined(USE_PROTOCOL_CLI)
82   #include <polypcore/protocol-cli.h> 
83   #define protocol_new pa_protocol_cli_new
84   #define protocol_free pa_protocol_cli_free
85   #define TCPWRAP_SERVICE "polypaudio-cli"
86   #define IPV4_PORT 4712
87   #define UNIX_SOCKET "cli"
88   #define MODULE_ARGUMENTS 
89   #ifdef USE_TCP_SOCKETS
90     #include "module-cli-protocol-tcp-symdef.h"
91   #elif defined(USE_TCP6_SOCKETS)
92     #include "module-cli-protocol-tcp6-symdef.h"
93   #else
94     #include "module-cli-protocol-unix-symdef.h"
95   #endif
96   PA_MODULE_DESCRIPTION("Command line interface protocol "SOCKET_DESCRIPTION)
97   PA_MODULE_USAGE(SOCKET_USAGE)
98 #elif defined(USE_PROTOCOL_HTTP)
99   #include <polypcore/protocol-http.h>
100   #define protocol_new pa_protocol_http_new
101   #define protocol_free pa_protocol_http_free
102   #define TCPWRAP_SERVICE "polypaudio-http"
103   #define IPV4_PORT 4714
104   #define UNIX_SOCKET "http"
105   #define MODULE_ARGUMENTS 
106   #ifdef USE_TCP_SOCKETS
107     #include "module-http-protocol-tcp-symdef.h"
108   #elif defined(USE_TCP6_SOCKETS)
109     #include "module-http-protocol-tcp6-symdef.h"
110   #else
111     #include "module-http-protocol-unix-symdef.h"
112   #endif
113   PA_MODULE_DESCRIPTION("HTTP "SOCKET_DESCRIPTION)
114   PA_MODULE_USAGE(SOCKET_USAGE)
115 #elif defined(USE_PROTOCOL_NATIVE)
116   #include <polypcore/protocol-native.h>
117   #define protocol_new pa_protocol_native_new
118   #define protocol_free pa_protocol_native_free
119   #define TCPWRAP_SERVICE "polypaudio-native"
120   #define IPV4_PORT PA_NATIVE_DEFAULT_PORT
121   #define UNIX_SOCKET PA_NATIVE_DEFAULT_UNIX_SOCKET
122   #define MODULE_ARGUMENTS "public", "cookie",
123   #ifdef USE_TCP_SOCKETS
124     #include "module-native-protocol-tcp-symdef.h"
125   #elif defined(USE_TCP6_SOCKETS)
126     #include "module-native-protocol-tcp6-symdef.h"
127   #else
128     #include "module-native-protocol-unix-symdef.h"
129   #endif
130   PA_MODULE_DESCRIPTION("Native protocol "SOCKET_DESCRIPTION)
131   PA_MODULE_USAGE("public=<don't check for cookies?> cookie=<path to cookie file> "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", "public", "cookie",
141   #ifdef USE_TCP_SOCKETS
142     #include "module-esound-protocol-tcp-symdef.h"
143   #elif defined(USE_TCP6_SOCKETS)
144     #include "module-esound-protocol-tcp6-symdef.h"
145   #else
146     #include "module-esound-protocol-unix-symdef.h"
147   #endif
148   PA_MODULE_DESCRIPTION("ESOUND protocol "SOCKET_DESCRIPTION)
149   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)
150 #else
151   #error "Broken build system" 
152 #endif
153
154 PA_MODULE_AUTHOR("Lennart Poettering")
155 PA_MODULE_VERSION(PACKAGE_VERSION)
156
157 static const char* const valid_modargs[] = {
158     MODULE_ARGUMENTS
159 #if defined(USE_TCP_SOCKETS) || defined(USE_TCP6_SOCKETS)
160     "port",
161     "loopback",
162 #else
163     "socket",
164 #endif
165     NULL
166 };
167
168 static pa_socket_server *create_socket_server(pa_core *c, pa_modargs *ma) {
169     pa_socket_server *s;
170 #if defined(USE_TCP_SOCKETS) || defined(USE_TCP6_SOCKETS)
171     int loopback = 1;
172     uint32_t port = IPV4_PORT;
173
174     if (pa_modargs_get_value_boolean(ma, "loopback", &loopback) < 0) {
175         pa_log(__FILE__": loopback= expects a boolean argument.\n");
176         return NULL;
177     }
178
179     if (pa_modargs_get_value_u32(ma, "port", &port) < 0 || port < 1 || port > 0xFFFF) {
180         pa_log(__FILE__": port= expects a numerical argument between 1 and 65535.\n");
181         return NULL;
182     }
183
184 #ifdef USE_TCP6_SOCKETS
185     if (!(s = pa_socket_server_new_ipv6(c->mainloop, loopback ? (const uint8_t*) &in6addr_loopback : (const uint8_t*) &in6addr_any, port)))
186         return NULL;
187 #else
188     if (!(s = pa_socket_server_new_ipv4(c->mainloop, loopback ? INADDR_LOOPBACK : INADDR_ANY, port, TCPWRAP_SERVICE)))
189         return NULL;
190 #endif
191     
192 #else
193     int r;
194     const char *v;
195     char tmp[PATH_MAX];
196
197     v = pa_modargs_get_value(ma, "socket", UNIX_SOCKET);
198     assert(v);
199
200     pa_runtime_path(v, tmp, sizeof(tmp));
201
202     if (pa_make_secure_parent_dir(tmp) < 0) {
203         pa_log(__FILE__": Failed to create secure socket directory.\n");
204         return NULL;
205     }
206
207     if ((r = pa_unix_socket_remove_stale(tmp)) < 0) {
208         pa_log(__FILE__": Failed to remove stale UNIX socket '%s': %s\n", tmp, strerror(errno));
209         return NULL;
210     }
211     
212     if (r)
213         pa_log(__FILE__": Removed stale UNIX socket '%s'.", tmp);
214     
215     if (!(s = pa_socket_server_new_unix(c->mainloop, tmp)))
216         return NULL;
217     
218 #endif
219     return s;
220 }
221
222 int pa__init(pa_core *c, pa_module*m) {
223     pa_socket_server *s;
224     pa_modargs *ma = NULL;
225     int ret = -1;
226     assert(c && m);
227
228     if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
229         pa_log(__FILE__": Failed to parse module arguments\n");
230         goto finish;
231     }
232
233     if (!(s = create_socket_server(c, ma)))
234         goto finish;
235
236     if (!(m->userdata = protocol_new(c, s, m, ma))) {
237         pa_socket_server_unref(s);
238         goto finish;
239     }
240
241     ret = 0;
242
243 finish:
244     if (ma)
245         pa_modargs_free(ma);
246
247     return ret;
248 }
249
250 void pa__done(pa_core *c, pa_module*m) {
251     assert(c && m);
252
253 #if defined(USE_PROTOCOL_ESOUND)
254         if (remove (ESD_UNIX_SOCKET_NAME) != 0)
255                 pa_log("%s: Failed to remove %s : %s.\n", __FILE__, ESD_UNIX_SOCKET_NAME, strerror (errno));
256         if (remove (ESD_UNIX_SOCKET_DIR) != 0)
257                 pa_log("%s: Failed to remove %s : %s.\n", __FILE__, ESD_UNIX_SOCKET_DIR, strerror (errno));
258 #endif
259
260     protocol_free(m->userdata);
261 }