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 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 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>
38 #include "socket-server.h"
39 #include "socket-util.h"
41 struct pa_socket_server
{
45 void (*on_connection
)(struct pa_socket_server
*s
, struct pa_iochannel
*io
, void *userdata
);
48 void *mainloop_source
;
49 struct pa_mainloop_api
*mainloop
;
50 enum { SOCKET_SERVER_GENERIC
, SOCKET_SERVER_IPV4
, SOCKET_SERVER_UNIX
} type
;
53 static void callback(struct pa_mainloop_api
*mainloop
, void *id
, int fd
, enum pa_mainloop_api_io_events events
, void *userdata
) {
54 struct pa_socket_server
*s
= userdata
;
55 struct pa_iochannel
*io
;
57 assert(s
&& s
->mainloop
== mainloop
&& s
->mainloop_source
== id
&& id
&& fd
>= 0 && fd
== s
->fd
&& events
== PA_MAINLOOP_API_IO_EVENT_INPUT
);
59 if ((nfd
= accept(fd
, NULL
, NULL
)) < 0) {
60 fprintf(stderr
, "accept(): %s\n", strerror(errno
));
64 if (!s
->on_connection
) {
69 /* There should be a check for socket type here */
70 if (s
->type
== SOCKET_SERVER_IPV4
)
71 pa_socket_tcp_low_delay(fd
);
73 pa_socket_low_delay(fd
);
75 io
= pa_iochannel_new(s
->mainloop
, nfd
, nfd
);
77 s
->on_connection(s
, io
, s
->userdata
);
80 struct pa_socket_server
* pa_socket_server_new(struct pa_mainloop_api
*m
, int fd
) {
81 struct pa_socket_server
*s
;
84 s
= malloc(sizeof(struct pa_socket_server
));
88 s
->on_connection
= NULL
;
92 s
->mainloop_source
= m
->source_io(m
, fd
, PA_MAINLOOP_API_IO_EVENT_INPUT
, callback
, s
);
93 assert(s
->mainloop_source
);
95 s
->type
= SOCKET_SERVER_GENERIC
;
100 struct pa_socket_server
* pa_socket_server_new_unix(struct pa_mainloop_api
*m
, const char *filename
) {
102 struct sockaddr_un sa
;
103 struct pa_socket_server
*s
;
105 assert(m
&& filename
);
107 if ((fd
= socket(PF_LOCAL
, SOCK_STREAM
, 0)) < 0) {
108 fprintf(stderr
, "socket(): %s\n", strerror(errno
));
112 sa
.sun_family
= AF_LOCAL
;
113 strncpy(sa
.sun_path
, filename
, sizeof(sa
.sun_path
)-1);
114 sa
.sun_path
[sizeof(sa
.sun_path
) - 1] = 0;
116 pa_socket_low_delay(fd
);
118 if (bind(fd
, (struct sockaddr
*) &sa
, SUN_LEN(&sa
)) < 0) {
119 fprintf(stderr
, "bind(): %s\n", strerror(errno
));
123 if (listen(fd
, 5) < 0) {
124 fprintf(stderr
, "listen(): %s\n", strerror(errno
));
128 s
= pa_socket_server_new(m
, fd
);
131 s
->filename
= strdup(filename
);
134 s
->type
= SOCKET_SERVER_UNIX
;
145 struct pa_socket_server
* pa_socket_server_new_ipv4(struct pa_mainloop_api
*m
, uint32_t address
, uint16_t port
) {
146 struct pa_socket_server
*ss
;
148 struct sockaddr_in sa
;
153 if ((fd
= socket(PF_INET
, SOCK_STREAM
, 0)) < 0) {
154 fprintf(stderr
, "socket(): %s\n", strerror(errno
));
158 if (setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, &on
, sizeof(on
)) < 0)
159 fprintf(stderr
, "setsockopt(): %s\n", strerror(errno
));
161 pa_socket_tcp_low_delay(fd
);
163 sa
.sin_family
= AF_INET
;
164 sa
.sin_port
= htons(port
);
165 sa
.sin_addr
.s_addr
= htonl(address
);
167 if (bind(fd
, (struct sockaddr
*) &sa
, sizeof(sa
)) < 0) {
168 fprintf(stderr
, "bind(): %s\n", strerror(errno
));
172 if (listen(fd
, 5) < 0) {
173 fprintf(stderr
, "listen(): %s\n", strerror(errno
));
177 if ((ss
= pa_socket_server_new(m
, fd
)))
178 ss
->type
= SOCKET_SERVER_IPV4
;
189 void pa_socket_server_free(struct pa_socket_server
*s
) {
199 s
->mainloop
->cancel_io(s
->mainloop
, s
->mainloop_source
);
204 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
) {
207 s
->on_connection
= on_connection
;
208 s
->userdata
= userdata
;