]> code.delx.au - pulseaudio/blob - polyp/socket-server.c
5f332f0c1d64f6e680cd44a707405a26e377c924
[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
42 struct pa_socket_server {
43 int fd;
44 char *filename;
45
46 void (*on_connection)(struct pa_socket_server*s, struct pa_iochannel *io, void *userdata);
47 void *userdata;
48
49 void *mainloop_source;
50 struct pa_mainloop_api *mainloop;
51 enum { SOCKET_SERVER_GENERIC, SOCKET_SERVER_IPV4, SOCKET_SERVER_UNIX } type;
52 };
53
54 static void callback(struct pa_mainloop_api *mainloop, void *id, int fd, enum pa_mainloop_api_io_events events, void *userdata) {
55 struct pa_socket_server *s = userdata;
56 struct pa_iochannel *io;
57 int nfd;
58 assert(s && s->mainloop == mainloop && s->mainloop_source == id && id && fd >= 0 && fd == s->fd && events == PA_MAINLOOP_API_IO_EVENT_INPUT);
59
60 if ((nfd = accept(fd, NULL, NULL)) < 0) {
61 fprintf(stderr, "accept(): %s\n", strerror(errno));
62 return;
63 }
64
65 if (!s->on_connection) {
66 close(nfd);
67 return;
68 }
69
70 /* There should be a check for socket type here */
71 if (s->type == SOCKET_SERVER_IPV4)
72 pa_socket_tcp_low_delay(fd);
73 else
74 pa_socket_low_delay(fd);
75
76 io = pa_iochannel_new(s->mainloop, nfd, nfd);
77 assert(io);
78 s->on_connection(s, io, s->userdata);
79 }
80
81 struct pa_socket_server* pa_socket_server_new(struct pa_mainloop_api *m, int fd) {
82 struct pa_socket_server *s;
83 assert(m && fd >= 0);
84
85 s = pa_xmalloc(sizeof(struct pa_socket_server));
86 s->fd = fd;
87 s->filename = NULL;
88 s->on_connection = NULL;
89 s->userdata = NULL;
90
91 s->mainloop = m;
92 s->mainloop_source = m->source_io(m, fd, PA_MAINLOOP_API_IO_EVENT_INPUT, callback, s);
93 assert(s->mainloop_source);
94
95 s->type = SOCKET_SERVER_GENERIC;
96
97 return s;
98 }
99
100 struct pa_socket_server* pa_socket_server_new_unix(struct pa_mainloop_api *m, const char *filename) {
101 int fd = -1;
102 struct sockaddr_un sa;
103 struct pa_socket_server *s;
104
105 assert(m && filename);
106
107 if ((fd = socket(PF_LOCAL, SOCK_STREAM, 0)) < 0) {
108 fprintf(stderr, "socket(): %s\n", strerror(errno));
109 goto fail;
110 }
111
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;
115
116 pa_socket_low_delay(fd);
117
118 if (bind(fd, (struct sockaddr*) &sa, SUN_LEN(&sa)) < 0) {
119 fprintf(stderr, "bind(): %s\n", strerror(errno));
120 goto fail;
121 }
122
123 if (listen(fd, 5) < 0) {
124 fprintf(stderr, "listen(): %s\n", strerror(errno));
125 goto fail;
126 }
127
128 s = pa_socket_server_new(m, fd);
129 assert(s);
130
131 s->filename = pa_xstrdup(filename);
132 s->type = SOCKET_SERVER_UNIX;
133
134 return s;
135
136 fail:
137 if (fd >= 0)
138 close(fd);
139
140 return NULL;
141 }
142
143 struct pa_socket_server* pa_socket_server_new_ipv4(struct pa_mainloop_api *m, uint32_t address, uint16_t port) {
144 struct pa_socket_server *ss;
145 int fd = -1;
146 struct sockaddr_in sa;
147 int on = 1;
148
149 assert(m && port);
150
151 if ((fd = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
152 fprintf(stderr, "socket(): %s\n", strerror(errno));
153 goto fail;
154 }
155
156 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0)
157 fprintf(stderr, "setsockopt(): %s\n", strerror(errno));
158
159 pa_socket_tcp_low_delay(fd);
160
161 sa.sin_family = AF_INET;
162 sa.sin_port = htons(port);
163 sa.sin_addr.s_addr = htonl(address);
164
165 if (bind(fd, (struct sockaddr *) &sa, sizeof(sa)) < 0) {
166 fprintf(stderr, "bind(): %s\n", strerror(errno));
167 goto fail;
168 }
169
170 if (listen(fd, 5) < 0) {
171 fprintf(stderr, "listen(): %s\n", strerror(errno));
172 goto fail;
173 }
174
175 if ((ss = pa_socket_server_new(m, fd)))
176 ss->type = SOCKET_SERVER_IPV4;
177
178 return ss;
179
180 fail:
181 if (fd >= 0)
182 close(fd);
183
184 return NULL;
185 }
186
187 void pa_socket_server_free(struct pa_socket_server*s) {
188 assert(s);
189 close(s->fd);
190
191 if (s->filename) {
192 unlink(s->filename);
193 pa_xfree(s->filename);
194 }
195
196 s->mainloop->cancel_io(s->mainloop, s->mainloop_source);
197 pa_xfree(s);
198 }
199
200 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) {
201 assert(s);
202
203 s->on_connection = on_connection;
204 s->userdata = userdata;
205 }