]>
code.delx.au - pulseaudio/blob - polyp/socket-server.c
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>
31 #include <sys/socket.h>
35 #include <netinet/in.h>
36 #include <arpa/inet.h>
42 #include "socket-server.h"
43 #include "socket-util.h"
48 struct pa_socket_server
{
52 char *tcpwrap_service
;
54 void (*on_connection
)(struct pa_socket_server
*s
, struct pa_iochannel
*io
, void *userdata
);
57 struct pa_io_event
*io_event
;
58 struct pa_mainloop_api
*mainloop
;
59 enum { SOCKET_SERVER_GENERIC
, SOCKET_SERVER_IPV4
, SOCKET_SERVER_UNIX
, SOCKET_SERVER_IPV6
} type
;
62 static void callback(struct pa_mainloop_api
*mainloop
, struct pa_io_event
*e
, int fd
, enum pa_io_event_flags f
, void *userdata
) {
63 struct pa_socket_server
*s
= userdata
;
64 struct pa_iochannel
*io
;
66 assert(s
&& s
->mainloop
== mainloop
&& s
->io_event
== e
&& e
&& fd
>= 0 && fd
== s
->fd
);
68 pa_socket_server_ref(s
);
70 if ((nfd
= accept(fd
, NULL
, NULL
)) < 0) {
71 pa_log(__FILE__
": accept(): %s\n", strerror(errno
));
75 pa_fd_set_cloexec(nfd
, 1);
77 if (!s
->on_connection
) {
84 if (s
->type
== SOCKET_SERVER_IPV4
&& s
->tcpwrap_service
) {
85 struct request_info req
;
87 request_init(&req
, RQ_DAEMON
, s
->tcpwrap_service
, RQ_FILE
, nfd
, NULL
);
89 if (!hosts_access(&req
)) {
90 pa_log(__FILE__
": TCP connection refused by tcpwrap.\n");
95 pa_log(__FILE__
": TCP connection accepted by tcpwrap.\n");
99 /* There should be a check for socket type here */
100 if (s
->type
== SOCKET_SERVER_IPV4
)
101 pa_socket_tcp_low_delay(fd
);
103 pa_socket_low_delay(fd
);
105 io
= pa_iochannel_new(s
->mainloop
, nfd
, nfd
);
107 s
->on_connection(s
, io
, s
->userdata
);
110 pa_socket_server_unref(s
);
113 struct pa_socket_server
* pa_socket_server_new(struct pa_mainloop_api
*m
, int fd
) {
114 struct pa_socket_server
*s
;
115 assert(m
&& fd
>= 0);
117 s
= pa_xmalloc(sizeof(struct pa_socket_server
));
121 s
->on_connection
= NULL
;
123 s
->tcpwrap_service
= NULL
;
126 s
->io_event
= m
->io_new(m
, fd
, PA_IO_EVENT_INPUT
, callback
, s
);
129 s
->type
= SOCKET_SERVER_GENERIC
;
134 struct pa_socket_server
* pa_socket_server_ref(struct pa_socket_server
*s
) {
135 assert(s
&& s
->ref
>= 1);
140 struct pa_socket_server
* pa_socket_server_new_unix(struct pa_mainloop_api
*m
, const char *filename
) {
142 struct sockaddr_un sa
;
143 struct pa_socket_server
*s
;
145 assert(m
&& filename
);
147 if ((fd
= socket(PF_LOCAL
, SOCK_STREAM
, 0)) < 0) {
148 pa_log(__FILE__
": socket(): %s\n", strerror(errno
));
152 pa_fd_set_cloexec(fd
, 1);
154 sa
.sun_family
= AF_LOCAL
;
155 strncpy(sa
.sun_path
, filename
, sizeof(sa
.sun_path
)-1);
156 sa
.sun_path
[sizeof(sa
.sun_path
) - 1] = 0;
158 pa_socket_low_delay(fd
);
160 if (bind(fd
, (struct sockaddr
*) &sa
, SUN_LEN(&sa
)) < 0) {
161 pa_log(__FILE__
": bind(): %s\n", strerror(errno
));
165 if (listen(fd
, 5) < 0) {
166 pa_log(__FILE__
": listen(): %s\n", strerror(errno
));
170 s
= pa_socket_server_new(m
, fd
);
173 s
->filename
= pa_xstrdup(filename
);
174 s
->type
= SOCKET_SERVER_UNIX
;
185 struct pa_socket_server
* pa_socket_server_new_ipv4(struct pa_mainloop_api
*m
, uint32_t address
, uint16_t port
, const char *tcpwrap_service
) {
186 struct pa_socket_server
*ss
;
188 struct sockaddr_in sa
;
193 if ((fd
= socket(PF_INET
, SOCK_STREAM
, 0)) < 0) {
194 pa_log(__FILE__
": socket(): %s\n", strerror(errno
));
198 pa_fd_set_cloexec(fd
, 1);
200 if (setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, &on
, sizeof(on
)) < 0)
201 pa_log(__FILE__
": setsockopt(): %s\n", strerror(errno
));
203 pa_socket_tcp_low_delay(fd
);
205 memset(&sa
, sizeof(sa
), 0);
206 sa
.sin_family
= AF_INET
;
207 sa
.sin_port
= htons(port
);
208 sa
.sin_addr
.s_addr
= htonl(address
);
210 if (bind(fd
, (struct sockaddr
*) &sa
, sizeof(sa
)) < 0) {
211 pa_log(__FILE__
": bind(): %s\n", strerror(errno
));
215 if (listen(fd
, 5) < 0) {
216 pa_log(__FILE__
": listen(): %s\n", strerror(errno
));
220 if ((ss
= pa_socket_server_new(m
, fd
))) {
221 ss
->type
= SOCKET_SERVER_IPV4
;
222 ss
->tcpwrap_service
= pa_xstrdup(tcpwrap_service
);
234 struct pa_socket_server
* pa_socket_server_new_ipv6(struct pa_mainloop_api
*m
, uint8_t address
[16], uint16_t port
) {
235 struct pa_socket_server
*ss
;
237 struct sockaddr_in6 sa
;
242 if ((fd
= socket(PF_INET6
, SOCK_STREAM
, 0)) < 0) {
243 pa_log(__FILE__
": socket(): %s\n", strerror(errno
));
247 pa_fd_set_cloexec(fd
, 1);
249 if (setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, &on
, sizeof(on
)) < 0)
250 pa_log(__FILE__
": setsockopt(): %s\n", strerror(errno
));
252 pa_socket_tcp_low_delay(fd
);
254 memset(&sa
, sizeof(sa
), 0);
255 sa
.sin6_family
= AF_INET6
;
256 sa
.sin6_port
= htons(port
);
257 memcpy(sa
.sin6_addr
.s6_addr
, address
, 16);
259 if (bind(fd
, (struct sockaddr
*) &sa
, sizeof(sa
)) < 0) {
260 pa_log(__FILE__
": bind(): %s\n", strerror(errno
));
264 if (listen(fd
, 5) < 0) {
265 pa_log(__FILE__
": listen(): %s\n", strerror(errno
));
269 if ((ss
= pa_socket_server_new(m
, fd
)))
270 ss
->type
= SOCKET_SERVER_IPV6
;
281 static void socket_server_free(struct pa_socket_server
*s
) {
287 pa_xfree(s
->filename
);
290 pa_xfree(s
->tcpwrap_service
);
292 s
->mainloop
->io_free(s
->io_event
);
296 void pa_socket_server_unref(struct pa_socket_server
*s
) {
297 assert(s
&& s
->ref
>= 1);
300 socket_server_free(s
);
303 void pa_socket_server_set_callback(struct pa_socket_server
*s
, void (*on_connection
)(struct pa_socket_server
*s
, struct pa_iochannel
*io
, void *userdata
), void *userdata
) {
304 assert(s
&& s
->ref
>= 1);
306 s
->on_connection
= on_connection
;
307 s
->userdata
= userdata
;
311 char *pa_socket_server_get_address(struct pa_socket_server
*s
, char *c
, size_t l
) {
312 assert(s
&& c
&& l
> 0);
315 case SOCKET_SERVER_IPV6
: {
316 struct sockaddr_in6 sa
;
317 socklen_t l
= sizeof(sa
);
319 if (getsockname(s
->fd
, (struct sockaddr
*) &sa
, &l
) < 0) {
320 pa_log(__FILE__
": getsockname() failed: %s\n", strerror(errno
));
324 if (memcmp(&in6addr_any
, &sa
.sin6_addr
, sizeof(in6addr_any
)) == 0) {
326 if (!pa_get_fqdn(fqdn
, sizeof(fqdn
)))
329 snprintf(c
, l
, "tcp6:%s:%u", fqdn
, (unsigned) ntohs(sa
.sin6_port
));
331 } else if (memcmp(&in6addr_loopback
, &sa
.sin6_addr
, sizeof(in6addr_loopback
)) == 0) {
333 if (!pa_get_host_name(hn
, sizeof(hn
)))
336 snprintf(c
, l
, "{%s}tcp6:localhost:%u", hn
, (unsigned) ntohs(sa
.sin6_port
));
338 char ip
[INET6_ADDRSTRLEN
];
340 if (!inet_ntop(AF_INET6
, &sa
.sin6_addr
, ip
, sizeof(ip
))) {
341 pa_log(__FILE__
": inet_ntop() failed: %s\n", strerror(errno
));
345 snprintf(c
, l
, "tcp6:[%s]:%u", ip
, (unsigned) ntohs(sa
.sin6_port
));
351 case SOCKET_SERVER_IPV4
: {
352 struct sockaddr_in sa
;
353 socklen_t l
= sizeof(sa
);
355 if (getsockname(s
->fd
, &sa
, &l
) < 0) {
356 pa_log(__FILE__
": getsockname() failed: %s\n", strerror(errno
));
360 if (sa
.sin_addr
.s_addr
== INADDR_ANY
) {
362 if (!pa_get_fqdn(fqdn
, sizeof(fqdn
)))
365 snprintf(c
, l
, "tcp:%s:%u", fqdn
, (unsigned) ntohs(sa
.sin_port
));
366 } else if (sa
.sin_addr
.s_addr
== INADDR_LOOPBACK
) {
368 if (!pa_get_host_name(hn
, sizeof(hn
)))
371 snprintf(c
, l
, "{%s}tcp:localhost:%u", hn
, (unsigned) ntohs(sa
.sin_port
));
373 char ip
[INET_ADDRSTRLEN
];
375 if (!inet_ntop(AF_INET
, &sa
.sin_addr
, ip
, sizeof(ip
))) {
376 pa_log(__FILE__
": inet_ntop() failed: %s\n", strerror(errno
));
380 snprintf(c
, l
, "tcp:[%s]:%u", ip
, (unsigned) ntohs(sa
.sin_port
));
387 case SOCKET_SERVER_UNIX
: {
393 if (!pa_get_host_name(hn
, sizeof(hn
)))
396 snprintf(c
, l
, "{%s}unix:%s", hn
, s
->filename
);