]> code.delx.au - pulseaudio/blob - polyp/socket-server.c
implement proper logging
[pulseaudio] / polyp / socket-server.c
1 /* $Id$ */
2
3 /***
4 This file is part of polypaudio.
5
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.
10
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.
15
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
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdlib.h>
27 #include <assert.h>
28 #include <errno.h>
29 #include <string.h>
30 #include <sys/types.h>
31 #include <sys/socket.h>
32 #include <stdio.h>
33 #include <unistd.h>
34 #include <sys/un.h>
35 #include <netinet/in.h>
36 #include <arpa/inet.h>
37
38 #include "socket-server.h"
39 #include "socket-util.h"
40 #include "xmalloc.h"
41 #include "util.h"
42 #include "log.h"
43
44 struct pa_socket_server {
45 int ref;
46 int fd;
47 char *filename;
48
49 void (*on_connection)(struct pa_socket_server*s, struct pa_iochannel *io, void *userdata);
50 void *userdata;
51
52 struct pa_io_event *io_event;
53 struct pa_mainloop_api *mainloop;
54 enum { SOCKET_SERVER_GENERIC, SOCKET_SERVER_IPV4, SOCKET_SERVER_UNIX } type;
55 };
56
57 static void callback(struct pa_mainloop_api *mainloop, struct pa_io_event *e, int fd, enum pa_io_event_flags f, void *userdata) {
58 struct pa_socket_server *s = userdata;
59 struct pa_iochannel *io;
60 int nfd;
61 assert(s && s->mainloop == mainloop && s->io_event == e && e && fd >= 0 && fd == s->fd);
62
63 pa_socket_server_ref(s);
64
65 if ((nfd = accept(fd, NULL, NULL)) < 0) {
66 pa_log(__FILE__": accept(): %s\n", strerror(errno));
67 goto finish;
68 }
69
70 pa_fd_set_cloexec(nfd, 1);
71
72 if (!s->on_connection) {
73 close(nfd);
74 goto finish;
75 }
76
77 /* There should be a check for socket type here */
78 if (s->type == SOCKET_SERVER_IPV4)
79 pa_socket_tcp_low_delay(fd);
80 else
81 pa_socket_low_delay(fd);
82
83 io = pa_iochannel_new(s->mainloop, nfd, nfd);
84 assert(io);
85 s->on_connection(s, io, s->userdata);
86
87 finish:
88 pa_socket_server_unref(s);
89 }
90
91 struct pa_socket_server* pa_socket_server_new(struct pa_mainloop_api *m, int fd) {
92 struct pa_socket_server *s;
93 assert(m && fd >= 0);
94
95 s = pa_xmalloc(sizeof(struct pa_socket_server));
96 s->ref = 1;
97 s->fd = fd;
98 s->filename = NULL;
99 s->on_connection = NULL;
100 s->userdata = NULL;
101
102 s->mainloop = m;
103 s->io_event = m->io_new(m, fd, PA_IO_EVENT_INPUT, callback, s);
104 assert(s->io_event);
105
106 s->type = SOCKET_SERVER_GENERIC;
107
108 return s;
109 }
110
111 struct pa_socket_server* pa_socket_server_ref(struct pa_socket_server *s) {
112 assert(s && s->ref >= 1);
113 s->ref++;
114 return s;
115 }
116
117 struct pa_socket_server* pa_socket_server_new_unix(struct pa_mainloop_api *m, const char *filename) {
118 int fd = -1;
119 struct sockaddr_un sa;
120 struct pa_socket_server *s;
121
122 assert(m && filename);
123
124 if ((fd = socket(PF_LOCAL, SOCK_STREAM, 0)) < 0) {
125 pa_log(__FILE__": socket(): %s\n", strerror(errno));
126 goto fail;
127 }
128
129 pa_fd_set_cloexec(fd, 1);
130
131 sa.sun_family = AF_LOCAL;
132 strncpy(sa.sun_path, filename, sizeof(sa.sun_path)-1);
133 sa.sun_path[sizeof(sa.sun_path) - 1] = 0;
134
135 pa_socket_low_delay(fd);
136
137 if (bind(fd, (struct sockaddr*) &sa, SUN_LEN(&sa)) < 0) {
138 pa_log(__FILE__": bind(): %s\n", strerror(errno));
139 goto fail;
140 }
141
142 if (listen(fd, 5) < 0) {
143 pa_log(__FILE__": listen(): %s\n", strerror(errno));
144 goto fail;
145 }
146
147 s = pa_socket_server_new(m, fd);
148 assert(s);
149
150 s->filename = pa_xstrdup(filename);
151 s->type = SOCKET_SERVER_UNIX;
152
153 return s;
154
155 fail:
156 if (fd >= 0)
157 close(fd);
158
159 return NULL;
160 }
161
162 struct pa_socket_server* pa_socket_server_new_ipv4(struct pa_mainloop_api *m, uint32_t address, uint16_t port) {
163 struct pa_socket_server *ss;
164 int fd = -1;
165 struct sockaddr_in sa;
166 int on = 1;
167
168 assert(m && port);
169
170 if ((fd = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
171 pa_log(__FILE__": socket(): %s\n", strerror(errno));
172 goto fail;
173 }
174
175 pa_fd_set_cloexec(fd, 1);
176
177 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0)
178 pa_log(__FILE__": setsockopt(): %s\n", strerror(errno));
179
180 pa_socket_tcp_low_delay(fd);
181
182 sa.sin_family = AF_INET;
183 sa.sin_port = htons(port);
184 sa.sin_addr.s_addr = htonl(address);
185
186 if (bind(fd, (struct sockaddr *) &sa, sizeof(sa)) < 0) {
187 pa_log(__FILE__": bind(): %s\n", strerror(errno));
188 goto fail;
189 }
190
191 if (listen(fd, 5) < 0) {
192 pa_log(__FILE__": listen(): %s\n", strerror(errno));
193 goto fail;
194 }
195
196 if ((ss = pa_socket_server_new(m, fd)))
197 ss->type = SOCKET_SERVER_IPV4;
198
199 return ss;
200
201 fail:
202 if (fd >= 0)
203 close(fd);
204
205 return NULL;
206 }
207
208 static void socket_server_free(struct pa_socket_server*s) {
209 assert(s);
210 close(s->fd);
211
212 if (s->filename) {
213 unlink(s->filename);
214 pa_xfree(s->filename);
215 }
216
217 s->mainloop->io_free(s->io_event);
218 pa_xfree(s);
219 }
220
221 void pa_socket_server_unref(struct pa_socket_server *s) {
222 assert(s && s->ref >= 1);
223
224 if (!(--(s->ref)))
225 socket_server_free(s);
226 }
227
228 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) {
229 assert(s && s->ref >= 1);
230
231 s->on_connection = on_connection;
232 s->userdata = userdata;
233 }