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 <polypcore/socket-util.h>
62 #include <polypcore/xmalloc.h>
63 #include <polypcore/util.h>
64 #include <polypcore/log.h>
66 #include "socket-server.h"
68 struct pa_socket_server
{
72 char *tcpwrap_service
;
74 void (*on_connection
)(pa_socket_server
*s
, pa_iochannel
*io
, void *userdata
);
77 pa_io_event
*io_event
;
78 pa_mainloop_api
*mainloop
;
79 enum { SOCKET_SERVER_GENERIC
, SOCKET_SERVER_IPV4
, SOCKET_SERVER_UNIX
, SOCKET_SERVER_IPV6
} type
;
82 static void callback(pa_mainloop_api
*mainloop
, pa_io_event
*e
, int fd
, PA_GCC_UNUSED pa_io_event_flags_t f
, void *userdata
) {
83 pa_socket_server
*s
= userdata
;
86 assert(s
&& s
->mainloop
== mainloop
&& s
->io_event
== e
&& e
&& fd
>= 0 && fd
== s
->fd
);
88 pa_socket_server_ref(s
);
90 if ((nfd
= accept(fd
, NULL
, NULL
)) < 0) {
91 pa_log(__FILE__
": accept(): %s\n", strerror(errno
));
95 pa_fd_set_cloexec(nfd
, 1);
97 if (!s
->on_connection
) {
104 if (s
->tcpwrap_service
) {
105 struct request_info req
;
107 request_init(&req
, RQ_DAEMON
, s
->tcpwrap_service
, RQ_FILE
, nfd
, NULL
);
109 if (!hosts_access(&req
)) {
110 pa_log_warn(__FILE__
": TCP connection refused by tcpwrap.\n");
115 pa_log_info(__FILE__
": TCP connection accepted by tcpwrap.\n");
119 /* There should be a check for socket type here */
120 if (s
->type
== SOCKET_SERVER_IPV4
)
121 pa_socket_tcp_low_delay(fd
);
123 pa_socket_low_delay(fd
);
125 io
= pa_iochannel_new(s
->mainloop
, nfd
, nfd
);
127 s
->on_connection(s
, io
, s
->userdata
);
130 pa_socket_server_unref(s
);
133 pa_socket_server
* pa_socket_server_new(pa_mainloop_api
*m
, int fd
) {
135 assert(m
&& fd
>= 0);
137 s
= pa_xmalloc(sizeof(pa_socket_server
));
141 s
->on_connection
= NULL
;
143 s
->tcpwrap_service
= NULL
;
146 s
->io_event
= m
->io_new(m
, fd
, PA_IO_EVENT_INPUT
, callback
, s
);
149 s
->type
= SOCKET_SERVER_GENERIC
;
154 pa_socket_server
* pa_socket_server_ref(pa_socket_server
*s
) {
155 assert(s
&& s
->ref
>= 1);
162 pa_socket_server
* pa_socket_server_new_unix(pa_mainloop_api
*m
, const char *filename
) {
164 struct sockaddr_un sa
;
167 assert(m
&& filename
);
169 if ((fd
= socket(PF_UNIX
, SOCK_STREAM
, 0)) < 0) {
170 pa_log(__FILE__
": socket(): %s\n", strerror(errno
));
174 pa_fd_set_cloexec(fd
, 1);
176 sa
.sun_family
= AF_UNIX
;
177 strncpy(sa
.sun_path
, filename
, sizeof(sa
.sun_path
)-1);
178 sa
.sun_path
[sizeof(sa
.sun_path
) - 1] = 0;
180 pa_socket_low_delay(fd
);
182 if (bind(fd
, (struct sockaddr
*) &sa
, SUN_LEN(&sa
)) < 0) {
183 pa_log(__FILE__
": bind(): %s\n", strerror(errno
));
187 if (listen(fd
, 5) < 0) {
188 pa_log(__FILE__
": listen(): %s\n", strerror(errno
));
192 s
= pa_socket_server_new(m
, fd
);
195 s
->filename
= pa_xstrdup(filename
);
196 s
->type
= SOCKET_SERVER_UNIX
;
207 #else /* HAVE_SYS_UN_H */
209 pa_socket_server
* pa_socket_server_new_unix(pa_mainloop_api
*m
, const char *filename
) {
213 #endif /* HAVE_SYS_UN_H */
215 pa_socket_server
* pa_socket_server_new_ipv4(pa_mainloop_api
*m
, uint32_t address
, uint16_t port
, const char *tcpwrap_service
) {
216 pa_socket_server
*ss
;
218 struct sockaddr_in sa
;
223 if ((fd
= socket(PF_INET
, SOCK_STREAM
, 0)) < 0) {
224 pa_log(__FILE__
": socket(PF_INET): %s\n", strerror(errno
));
228 pa_fd_set_cloexec(fd
, 1);
230 if (setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (void*)&on
, sizeof(on
)) < 0)
231 pa_log(__FILE__
": setsockopt(): %s\n", strerror(errno
));
233 pa_socket_tcp_low_delay(fd
);
235 memset(&sa
, 0, sizeof(sa
));
236 sa
.sin_family
= AF_INET
;
237 sa
.sin_port
= htons(port
);
238 sa
.sin_addr
.s_addr
= htonl(address
);
240 if (bind(fd
, (struct sockaddr
*) &sa
, sizeof(sa
)) < 0) {
241 pa_log(__FILE__
": bind(): %s\n", strerror(errno
));
245 if (listen(fd
, 5) < 0) {
246 pa_log(__FILE__
": listen(): %s\n", strerror(errno
));
250 if ((ss
= pa_socket_server_new(m
, fd
))) {
251 ss
->type
= SOCKET_SERVER_IPV4
;
252 ss
->tcpwrap_service
= pa_xstrdup(tcpwrap_service
);
264 pa_socket_server
* pa_socket_server_new_ipv6(pa_mainloop_api
*m
, const uint8_t address
[16], uint16_t port
, const char *tcpwrap_service
) {
265 pa_socket_server
*ss
;
267 struct sockaddr_in6 sa
;
272 if ((fd
= socket(PF_INET6
, SOCK_STREAM
, 0)) < 0) {
273 pa_log(__FILE__
": socket(PF_INET6): %s\n", strerror(errno
));
277 pa_fd_set_cloexec(fd
, 1);
279 if (setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (void*)&on
, sizeof(on
)) < 0)
280 pa_log(__FILE__
": setsockopt(): %s\n", strerror(errno
));
282 pa_socket_tcp_low_delay(fd
);
284 memset(&sa
, 0, sizeof(sa
));
285 sa
.sin6_family
= AF_INET6
;
286 sa
.sin6_port
= htons(port
);
287 memcpy(sa
.sin6_addr
.s6_addr
, address
, 16);
289 if (bind(fd
, (struct sockaddr
*) &sa
, sizeof(sa
)) < 0) {
290 pa_log(__FILE__
": bind(): %s\n", strerror(errno
));
294 if (listen(fd
, 5) < 0) {
295 pa_log(__FILE__
": listen(): %s\n", strerror(errno
));
299 if ((ss
= pa_socket_server_new(m
, fd
))) {
300 ss
->type
= SOCKET_SERVER_IPV6
;
301 ss
->tcpwrap_service
= pa_xstrdup(tcpwrap_service
);
313 pa_socket_server
* pa_socket_server_new_ip_loopback(pa_mainloop_api
*m
, uint16_t port
, const char *tcpwrap_service
) {
319 if (!(s
= pa_socket_server_new_ipv6(m
, in6addr_loopback
.s6_addr
, port
, tcpwrap_service
)))
320 s
= pa_socket_server_new_ipv4(m
, INADDR_LOOPBACK
, port
, tcpwrap_service
);
325 pa_socket_server
* pa_socket_server_new_ip_any(pa_mainloop_api
*m
, uint16_t port
, const char *tcpwrap_service
) {
331 if (!(s
= pa_socket_server_new_ipv6(m
, in6addr_any
.s6_addr
, port
, tcpwrap_service
)))
332 s
= pa_socket_server_new_ipv4(m
, INADDR_ANY
, port
, tcpwrap_service
);
338 static void socket_server_free(pa_socket_server
*s
) {
344 pa_xfree(s
->filename
);
347 pa_xfree(s
->tcpwrap_service
);
349 s
->mainloop
->io_free(s
->io_event
);
353 void pa_socket_server_unref(pa_socket_server
*s
) {
354 assert(s
&& s
->ref
>= 1);
357 socket_server_free(s
);
360 void pa_socket_server_set_callback(pa_socket_server
*s
, void (*on_connection
)(pa_socket_server
*s
, pa_iochannel
*io
, void *userdata
), void *userdata
) {
361 assert(s
&& s
->ref
>= 1);
363 s
->on_connection
= on_connection
;
364 s
->userdata
= userdata
;
368 char *pa_socket_server_get_address(pa_socket_server
*s
, char *c
, size_t l
) {
369 assert(s
&& c
&& l
> 0);
372 case SOCKET_SERVER_IPV6
: {
373 struct sockaddr_in6 sa
;
374 socklen_t sa_len
= sizeof(sa
);
376 if (getsockname(s
->fd
, (struct sockaddr
*) &sa
, &sa_len
) < 0) {
377 pa_log(__FILE__
": getsockname() failed: %s\n", strerror(errno
));
381 if (memcmp(&in6addr_any
, &sa
.sin6_addr
, sizeof(in6addr_any
)) == 0) {
383 if (!pa_get_fqdn(fqdn
, sizeof(fqdn
)))
386 snprintf(c
, l
, "tcp6:%s:%u", fqdn
, (unsigned) ntohs(sa
.sin6_port
));
388 } else if (memcmp(&in6addr_loopback
, &sa
.sin6_addr
, sizeof(in6addr_loopback
)) == 0) {
390 if (!pa_get_host_name(hn
, sizeof(hn
)))
393 snprintf(c
, l
, "{%s}tcp6:localhost:%u", hn
, (unsigned) ntohs(sa
.sin6_port
));
395 char ip
[INET6_ADDRSTRLEN
];
397 if (!inet_ntop(AF_INET6
, &sa
.sin6_addr
, ip
, sizeof(ip
))) {
398 pa_log(__FILE__
": inet_ntop() failed: %s\n", strerror(errno
));
402 snprintf(c
, l
, "tcp6:[%s]:%u", ip
, (unsigned) ntohs(sa
.sin6_port
));
408 case SOCKET_SERVER_IPV4
: {
409 struct sockaddr_in sa
;
410 socklen_t sa_len
= sizeof(sa
);
412 if (getsockname(s
->fd
, (struct sockaddr
*) &sa
, &sa_len
) < 0) {
413 pa_log(__FILE__
": getsockname() failed: %s\n", strerror(errno
));
417 if (sa
.sin_addr
.s_addr
== INADDR_ANY
) {
419 if (!pa_get_fqdn(fqdn
, sizeof(fqdn
)))
422 snprintf(c
, l
, "tcp:%s:%u", fqdn
, (unsigned) ntohs(sa
.sin_port
));
423 } else if (sa
.sin_addr
.s_addr
== INADDR_LOOPBACK
) {
425 if (!pa_get_host_name(hn
, sizeof(hn
)))
428 snprintf(c
, l
, "{%s}tcp:localhost:%u", hn
, (unsigned) ntohs(sa
.sin_port
));
430 char ip
[INET_ADDRSTRLEN
];
432 if (!inet_ntop(AF_INET
, &sa
.sin_addr
, ip
, sizeof(ip
))) {
433 pa_log(__FILE__
": inet_ntop() failed: %s\n", strerror(errno
));
437 snprintf(c
, l
, "tcp:[%s]:%u", ip
, (unsigned) ntohs(sa
.sin_port
));
444 case SOCKET_SERVER_UNIX
: {
450 if (!pa_get_host_name(hn
, sizeof(hn
)))
453 snprintf(c
, l
, "{%s}unix:%s", hn
, s
->filename
);