4 This file is part of polypaudio.
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.
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.
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
33 #ifdef HAVE_SYS_SOCKET_H
34 #include <sys/socket.h>
36 #ifdef HAVE_ARPA_INET_H
37 #include <arpa/inet.h>
39 #ifdef HAVE_NETINET_IN_H
40 #include <netinet/in.h>
43 #include "../polypcore/winsock.h"
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>
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?>"
60 #define SOCKET_DESCRIPTION "(UNIX sockets)"
61 #define SOCKET_USAGE "socket=<path to UNIX socket>"
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"
77 #include "module-simple-protocol-unix-symdef.h"
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"
94 #include "module-cli-protocol-unix-symdef.h"
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"
111 #include "module-http-protocol-unix-symdef.h"
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"
128 #include "module-native-protocol-unix-symdef.h"
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"
146 #include "module-esound-protocol-unix-symdef.h"
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)
151 #error "Broken build system"
154 PA_MODULE_AUTHOR("Lennart Poettering")
155 PA_MODULE_VERSION(PACKAGE_VERSION)
157 static const char* const valid_modargs[] = {
159 #if defined(USE_TCP_SOCKETS) || defined(USE_TCP6_SOCKETS)
168 static pa_socket_server *create_socket_server(pa_core *c, pa_modargs *ma) {
170 #if defined(USE_TCP_SOCKETS) || defined(USE_TCP6_SOCKETS)
172 uint32_t port = IPV4_PORT;
174 if (pa_modargs_get_value_boolean(ma, "loopback", &loopback) < 0) {
175 pa_log(__FILE__": loopback= expects a boolean argument.\n");
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");
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)))
188 if (!(s = pa_socket_server_new_ipv4(c->mainloop, loopback ? INADDR_LOOPBACK : INADDR_ANY, port, TCPWRAP_SERVICE)))
197 v = pa_modargs_get_value(ma, "socket", UNIX_SOCKET);
200 pa_runtime_path(v, tmp, sizeof(tmp));
202 if (pa_make_secure_parent_dir(tmp) < 0) {
203 pa_log(__FILE__": Failed to create secure socket directory.\n");
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));
213 pa_log(__FILE__": Removed stale UNIX socket '%s'.", tmp);
215 if (!(s = pa_socket_server_new_unix(c->mainloop, tmp)))
222 int pa__init(pa_core *c, pa_module*m) {
224 pa_modargs *ma = NULL;
228 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
229 pa_log(__FILE__": Failed to parse module arguments\n");
233 if (!(s = create_socket_server(c, ma)))
236 if (!(m->userdata = protocol_new(c, s, m, ma))) {
237 pa_socket_server_unref(s);
250 void pa__done(pa_core *c, pa_module*m) {
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));
260 protocol_free(m->userdata);