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
30 #include <sys/types.h>
34 #ifdef HAVE_SYS_SOCKET_H
35 #include <sys/socket.h>
40 #define SUN_LEN(ptr) \
41 ((size_t)(((struct sockaddr_un *) 0)->sun_path) + strlen((ptr)->sun_path))
44 #ifdef HAVE_ARPA_INET_H
45 #include <arpa/inet.h>
47 #ifdef HAVE_NETINET_IN_H
48 #include <netinet/in.h>
55 #ifndef HAVE_INET_NTOP
56 #include "inet_ntop.h"
61 #include "socket-server.h"
62 #include "socket-util.h"
67 struct pa_socket_server
{
71 char *tcpwrap_service
;
73 void (*on_connection
)(pa_socket_server
*s
, pa_iochannel
*io
, void *userdata
);
76 pa_io_event
*io_event
;
77 pa_mainloop_api
*mainloop
;
78 enum { SOCKET_SERVER_GENERIC
, SOCKET_SERVER_IPV4
, SOCKET_SERVER_UNIX
, SOCKET_SERVER_IPV6
} type
;
81 static void callback(pa_mainloop_api
*mainloop
, pa_io_event
*e
, int fd
, PA_GCC_UNUSED pa_io_event_flags_t f
, void *userdata
) {
82 pa_socket_server
*s
= userdata
;
85 assert(s
&& s
->mainloop
== mainloop
&& s
->io_event
== e
&& e
&& fd
>= 0 && fd
== s
->fd
);
87 pa_socket_server_ref(s
);
89 if ((nfd
= accept(fd
, NULL
, NULL
)) < 0) {
90 pa_log(__FILE__
": accept(): %s\n", strerror(errno
));
94 pa_fd_set_cloexec(nfd
, 1);
96 if (!s
->on_connection
) {
103 if (s
->type
== SOCKET_SERVER_IPV4
&& s
->tcpwrap_service
) {
104 struct request_info req
;
106 request_init(&req
, RQ_DAEMON
, s
->tcpwrap_service
, RQ_FILE
, nfd
, NULL
);
108 if (!hosts_access(&req
)) {
109 pa_log(__FILE__
": TCP connection refused by tcpwrap.\n");
114 pa_log(__FILE__
": TCP connection accepted by tcpwrap.\n");
118 /* There should be a check for socket type here */
119 if (s
->type
== SOCKET_SERVER_IPV4
)
120 pa_socket_tcp_low_delay(fd
);
122 pa_socket_low_delay(fd
);
124 io
= pa_iochannel_new(s
->mainloop
, nfd
, nfd
);
126 s
->on_connection(s
, io
, s
->userdata
);
129 pa_socket_server_unref(s
);
132 pa_socket_server
* pa_socket_server_new(pa_mainloop_api
*m
, int fd
) {
134 assert(m
&& fd
>= 0);
136 s
= pa_xmalloc(sizeof(pa_socket_server
));
140 s
->on_connection
= NULL
;
142 s
->tcpwrap_service
= NULL
;
145 s
->io_event
= m
->io_new(m
, fd
, PA_IO_EVENT_INPUT
, callback
, s
);
148 s
->type
= SOCKET_SERVER_GENERIC
;
153 pa_socket_server
* pa_socket_server_ref(pa_socket_server
*s
) {
154 assert(s
&& s
->ref
>= 1);
161 pa_socket_server
* pa_socket_server_new_unix(pa_mainloop_api
*m
, const char *filename
) {
163 struct sockaddr_un sa
;
166 assert(m
&& filename
);
168 if ((fd
= socket(PF_UNIX
, SOCK_STREAM
, 0)) < 0) {
169 pa_log(__FILE__
": socket(): %s\n", strerror(errno
));
173 pa_fd_set_cloexec(fd
, 1);
175 sa
.sun_family
= AF_UNIX
;
176 strncpy(sa
.sun_path
, filename
, sizeof(sa
.sun_path
)-1);
177 sa
.sun_path
[sizeof(sa
.sun_path
) - 1] = 0;
179 pa_socket_low_delay(fd
);
181 if (bind(fd
, (struct sockaddr
*) &sa
, SUN_LEN(&sa
)) < 0) {
182 pa_log(__FILE__
": bind(): %s\n", strerror(errno
));
186 if (listen(fd
, 5) < 0) {
187 pa_log(__FILE__
": listen(): %s\n", strerror(errno
));
191 s
= pa_socket_server_new(m
, fd
);
194 s
->filename
= pa_xstrdup(filename
);
195 s
->type
= SOCKET_SERVER_UNIX
;
206 #else /* HAVE_SYS_UN_H */
208 pa_socket_server
* pa_socket_server_new_unix(pa_mainloop_api
*m
, const char *filename
) {
212 #endif /* HAVE_SYS_UN_H */
214 pa_socket_server
* pa_socket_server_new_ipv4(pa_mainloop_api
*m
, uint32_t address
, uint16_t port
, const char *tcpwrap_service
) {
215 pa_socket_server
*ss
;
217 struct sockaddr_in sa
;
222 if ((fd
= socket(PF_INET
, SOCK_STREAM
, 0)) < 0) {
223 pa_log(__FILE__
": socket(): %s\n", strerror(errno
));
227 pa_fd_set_cloexec(fd
, 1);
229 if (setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (void*)&on
, sizeof(on
)) < 0)
230 pa_log(__FILE__
": setsockopt(): %s\n", strerror(errno
));
232 pa_socket_tcp_low_delay(fd
);
234 memset(&sa
, 0, sizeof(sa
));
235 sa
.sin_family
= AF_INET
;
236 sa
.sin_port
= htons(port
);
237 sa
.sin_addr
.s_addr
= htonl(address
);
239 if (bind(fd
, (struct sockaddr
*) &sa
, sizeof(sa
)) < 0) {
240 pa_log(__FILE__
": bind(): %s\n", strerror(errno
));
244 if (listen(fd
, 5) < 0) {
245 pa_log(__FILE__
": listen(): %s\n", strerror(errno
));
249 if ((ss
= pa_socket_server_new(m
, fd
))) {
250 ss
->type
= SOCKET_SERVER_IPV4
;
251 ss
->tcpwrap_service
= pa_xstrdup(tcpwrap_service
);
263 pa_socket_server
* pa_socket_server_new_ipv6(pa_mainloop_api
*m
, const uint8_t address
[16], uint16_t port
) {
264 pa_socket_server
*ss
;
266 struct sockaddr_in6 sa
;
271 if ((fd
= socket(PF_INET6
, SOCK_STREAM
, 0)) < 0) {
272 pa_log(__FILE__
": socket(): %s\n", strerror(errno
));
276 pa_fd_set_cloexec(fd
, 1);
278 if (setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (void*)&on
, sizeof(on
)) < 0)
279 pa_log(__FILE__
": setsockopt(): %s\n", strerror(errno
));
281 pa_socket_tcp_low_delay(fd
);
283 memset(&sa
, 0, sizeof(sa
));
284 sa
.sin6_family
= AF_INET6
;
285 sa
.sin6_port
= htons(port
);
286 memcpy(sa
.sin6_addr
.s6_addr
, address
, 16);
288 if (bind(fd
, (struct sockaddr
*) &sa
, sizeof(sa
)) < 0) {
289 pa_log(__FILE__
": bind(): %s\n", strerror(errno
));
293 if (listen(fd
, 5) < 0) {
294 pa_log(__FILE__
": listen(): %s\n", strerror(errno
));
298 if ((ss
= pa_socket_server_new(m
, fd
)))
299 ss
->type
= SOCKET_SERVER_IPV6
;
310 static void socket_server_free(pa_socket_server
*s
) {
316 pa_xfree(s
->filename
);
319 pa_xfree(s
->tcpwrap_service
);
321 s
->mainloop
->io_free(s
->io_event
);
325 void pa_socket_server_unref(pa_socket_server
*s
) {
326 assert(s
&& s
->ref
>= 1);
329 socket_server_free(s
);
332 void pa_socket_server_set_callback(pa_socket_server
*s
, void (*on_connection
)(pa_socket_server
*s
, pa_iochannel
*io
, void *userdata
), void *userdata
) {
333 assert(s
&& s
->ref
>= 1);
335 s
->on_connection
= on_connection
;
336 s
->userdata
= userdata
;
340 char *pa_socket_server_get_address(pa_socket_server
*s
, char *c
, size_t l
) {
341 assert(s
&& c
&& l
> 0);
344 case SOCKET_SERVER_IPV6
: {
345 struct sockaddr_in6 sa
;
346 socklen_t sa_len
= sizeof(sa
);
348 if (getsockname(s
->fd
, (struct sockaddr
*) &sa
, &sa_len
) < 0) {
349 pa_log(__FILE__
": getsockname() failed: %s\n", strerror(errno
));
353 if (memcmp(&in6addr_any
, &sa
.sin6_addr
, sizeof(in6addr_any
)) == 0) {
355 if (!pa_get_fqdn(fqdn
, sizeof(fqdn
)))
358 snprintf(c
, l
, "tcp6:%s:%u", fqdn
, (unsigned) ntohs(sa
.sin6_port
));
360 } else if (memcmp(&in6addr_loopback
, &sa
.sin6_addr
, sizeof(in6addr_loopback
)) == 0) {
362 if (!pa_get_host_name(hn
, sizeof(hn
)))
365 snprintf(c
, l
, "{%s}tcp6:localhost:%u", hn
, (unsigned) ntohs(sa
.sin6_port
));
367 char ip
[INET6_ADDRSTRLEN
];
369 if (!inet_ntop(AF_INET6
, &sa
.sin6_addr
, ip
, sizeof(ip
))) {
370 pa_log(__FILE__
": inet_ntop() failed: %s\n", strerror(errno
));
374 snprintf(c
, l
, "tcp6:[%s]:%u", ip
, (unsigned) ntohs(sa
.sin6_port
));
380 case SOCKET_SERVER_IPV4
: {
381 struct sockaddr_in sa
;
382 socklen_t sa_len
= sizeof(sa
);
384 if (getsockname(s
->fd
, (struct sockaddr
*) &sa
, &sa_len
) < 0) {
385 pa_log(__FILE__
": getsockname() failed: %s\n", strerror(errno
));
389 if (sa
.sin_addr
.s_addr
== INADDR_ANY
) {
391 if (!pa_get_fqdn(fqdn
, sizeof(fqdn
)))
394 snprintf(c
, l
, "tcp:%s:%u", fqdn
, (unsigned) ntohs(sa
.sin_port
));
395 } else if (sa
.sin_addr
.s_addr
== INADDR_LOOPBACK
) {
397 if (!pa_get_host_name(hn
, sizeof(hn
)))
400 snprintf(c
, l
, "{%s}tcp:localhost:%u", hn
, (unsigned) ntohs(sa
.sin_port
));
402 char ip
[INET_ADDRSTRLEN
];
404 if (!inet_ntop(AF_INET
, &sa
.sin_addr
, ip
, sizeof(ip
))) {
405 pa_log(__FILE__
": inet_ntop() failed: %s\n", strerror(errno
));
409 snprintf(c
, l
, "tcp:[%s]:%u", ip
, (unsigned) ntohs(sa
.sin_port
));
416 case SOCKET_SERVER_UNIX
: {
422 if (!pa_get_host_name(hn
, sizeof(hn
)))
425 snprintf(c
, l
, "{%s}unix:%s", hn
, s
->filename
);