]> code.delx.au - pulseaudio/blob - polyp/socket-client.c
Make the whole stuff LGPL only
[pulseaudio] / polyp / socket-client.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 Lesser General Public License as
8 published by the Free Software Foundation; either version 2.1 of the
9 License, 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 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License 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 <unistd.h>
27 #include <stdio.h>
28 #include <errno.h>
29 #include <string.h>
30 #include <assert.h>
31 #include <stdlib.h>
32 #include <sys/un.h>
33 #include <netinet/in.h>
34 #include <arpa/inet.h>
35 #include <netdb.h>
36
37 #include "socket-client.h"
38 #include "socket-util.h"
39 #include "util.h"
40 #include "xmalloc.h"
41 #include "log.h"
42
43 struct pa_socket_client {
44 int ref;
45 struct pa_mainloop_api *mainloop;
46 int fd;
47 struct pa_io_event *io_event;
48 struct pa_defer_event *defer_event;
49 void (*callback)(struct pa_socket_client*c, struct pa_iochannel *io, void *userdata);
50 void *userdata;
51 int local;
52 };
53
54 static struct pa_socket_client*pa_socket_client_new(struct pa_mainloop_api *m) {
55 struct pa_socket_client *c;
56 assert(m);
57
58 c = pa_xmalloc(sizeof(struct pa_socket_client));
59 c->ref = 1;
60 c->mainloop = m;
61 c->fd = -1;
62 c->io_event = NULL;
63 c->defer_event = NULL;
64 c->callback = NULL;
65 c->userdata = NULL;
66 c->local = 0;
67 return c;
68 }
69
70 static void do_call(struct pa_socket_client *c) {
71 struct pa_iochannel *io = NULL;
72 int error;
73 socklen_t lerror;
74 assert(c && c->callback);
75
76 pa_socket_client_ref(c);
77
78 lerror = sizeof(error);
79 if (getsockopt(c->fd, SOL_SOCKET, SO_ERROR, &error, &lerror) < 0) {
80 pa_log(__FILE__": getsockopt(): %s\n", strerror(errno));
81 goto finish;
82 }
83
84 if (lerror != sizeof(error)) {
85 pa_log(__FILE__": getsockopt() returned invalid size.\n");
86 goto finish;
87 }
88
89 if (error != 0) {
90 /* pa_log(__FILE__": connect(): %s\n", strerror(error)); */
91 errno = error;
92 goto finish;
93 }
94
95 io = pa_iochannel_new(c->mainloop, c->fd, c->fd);
96 assert(io);
97
98 finish:
99 if (!io)
100 close(c->fd);
101 c->fd = -1;
102
103 assert(c->callback);
104 c->callback(c, io, c->userdata);
105
106 pa_socket_client_unref(c);
107 }
108
109 static void connect_fixed_cb(struct pa_mainloop_api *m, struct pa_defer_event *e, void *userdata) {
110 struct pa_socket_client *c = userdata;
111 assert(m && c && c->defer_event == e);
112 m->defer_free(c->defer_event);
113 c->defer_event = NULL;
114 do_call(c);
115 }
116
117 static void connect_io_cb(struct pa_mainloop_api*m, struct pa_io_event *e, int fd, enum pa_io_event_flags f, void *userdata) {
118 struct pa_socket_client *c = userdata;
119 assert(m && c && c->io_event == e && fd >= 0);
120 m->io_free(c->io_event);
121 c->io_event = NULL;
122 do_call(c);
123 }
124
125 static int do_connect(struct pa_socket_client *c, const struct sockaddr *sa, socklen_t len) {
126 int r;
127 assert(c && sa && len);
128
129 pa_make_nonblock_fd(c->fd);
130
131 if ((r = connect(c->fd, sa, len)) < 0) {
132 if (errno != EINPROGRESS) {
133 /*pa_log(__FILE__": connect(): %s\n", strerror(errno));*/
134 return -1;
135 }
136
137 c->io_event = c->mainloop->io_new(c->mainloop, c->fd, PA_IO_EVENT_OUTPUT, connect_io_cb, c);
138 assert(c->io_event);
139 } else {
140 c->defer_event = c->mainloop->defer_new(c->mainloop, connect_fixed_cb, c);
141 assert(c->defer_event);
142 }
143
144 return 0;
145 }
146
147 struct pa_socket_client* pa_socket_client_new_ipv4(struct pa_mainloop_api *m, uint32_t address, uint16_t port) {
148 struct sockaddr_in sa;
149 assert(m && port > 0);
150
151 memset(&sa, 0, sizeof(sa));
152 sa.sin_family = AF_INET;
153 sa.sin_port = htons(port);
154 sa.sin_addr.s_addr = htonl(address);
155
156 return pa_socket_client_new_sockaddr(m, (struct sockaddr*) &sa, sizeof(sa));
157 }
158
159 struct pa_socket_client* pa_socket_client_new_unix(struct pa_mainloop_api *m, const char *filename) {
160 struct sockaddr_un sa;
161 assert(m && filename);
162
163 memset(&sa, 0, sizeof(sa));
164 sa.sun_family = AF_LOCAL;
165 strncpy(sa.sun_path, filename, sizeof(sa.sun_path)-1);
166 sa.sun_path[sizeof(sa.sun_path) - 1] = 0;
167
168 return pa_socket_client_new_sockaddr(m, (struct sockaddr*) &sa, sizeof(sa));
169 }
170
171 struct pa_socket_client* pa_socket_client_new_sockaddr(struct pa_mainloop_api *m, const struct sockaddr *sa, size_t salen) {
172 struct pa_socket_client *c;
173 assert(m && sa);
174 c = pa_socket_client_new(m);
175 assert(c);
176
177 switch (sa->sa_family) {
178 case AF_UNIX:
179 c->local = 1;
180 break;
181
182 case AF_INET:
183 c->local = ((const struct sockaddr_in*) sa)->sin_addr.s_addr == INADDR_LOOPBACK;
184 break;
185
186 case AF_INET6:
187 c->local = memcmp(&((const struct sockaddr_in6*) sa)->sin6_addr, &in6addr_loopback, sizeof(struct in6_addr)) == 0;
188 break;
189
190 default:
191 c->local = 0;
192 }
193
194 if ((c->fd = socket(sa->sa_family, SOCK_STREAM, 0)) < 0) {
195 pa_log(__FILE__": socket(): %s\n", strerror(errno));
196 goto fail;
197 }
198
199 pa_fd_set_cloexec(c->fd, 1);
200 if (sa->sa_family == AF_INET || sa->sa_family == AF_INET6)
201 pa_socket_tcp_low_delay(c->fd);
202 else
203 pa_socket_low_delay(c->fd);
204
205 if (do_connect(c, sa, salen) < 0)
206 goto fail;
207
208 return c;
209
210 fail:
211 pa_socket_client_unref(c);
212 return NULL;
213
214 }
215
216 void socket_client_free(struct pa_socket_client *c) {
217 assert(c && c->mainloop);
218 if (c->io_event)
219 c->mainloop->io_free(c->io_event);
220 if (c->defer_event)
221 c->mainloop->defer_free(c->defer_event);
222 if (c->fd >= 0)
223 close(c->fd);
224 pa_xfree(c);
225 }
226
227 void pa_socket_client_unref(struct pa_socket_client *c) {
228 assert(c && c->ref >= 1);
229
230 if (!(--(c->ref)))
231 socket_client_free(c);
232 }
233
234 struct pa_socket_client* pa_socket_client_ref(struct pa_socket_client *c) {
235 assert(c && c->ref >= 1);
236 c->ref++;
237 return c;
238 }
239
240 void pa_socket_client_set_callback(struct pa_socket_client *c, void (*on_connection)(struct pa_socket_client *c, struct pa_iochannel*io, void *userdata), void *userdata) {
241 assert(c);
242 c->callback = on_connection;
243 c->userdata = userdata;
244 }
245
246 struct pa_socket_client* pa_socket_client_new_ipv6(struct pa_mainloop_api *m, uint8_t address[16], uint16_t port) {
247 struct sockaddr_in6 sa;
248
249 memset(&sa, 0, sizeof(sa));
250 sa.sin6_family = AF_INET6;
251 sa.sin6_port = htons(port);
252 memcpy(&sa.sin6_addr, address, sizeof(sa.sin6_addr));
253
254 return pa_socket_client_new_sockaddr(m, (struct sockaddr*) &sa, sizeof(sa));
255 }
256
257 /* Parse addresses in one of the following forms:
258 * HOSTNAME
259 * HOSTNAME:PORT
260 * [HOSTNAME]
261 * [HOSTNAME]:PORT
262 *
263 * Return a newly allocated string of the hostname and fill in *port if specified */
264
265 static char *parse_address(const char *s, uint16_t *port) {
266 assert(s && port);
267 if (*s == '[') {
268 char *e;
269 if (!(e = strchr(s+1, ']')))
270 return NULL;
271
272 if (e[1] == ':')
273 *port = atoi(e+2);
274 else if (e[1] != 0)
275 return NULL;
276
277 return pa_xstrndup(s+1, e-s-1);
278 } else {
279 char *e;
280
281 if (!(e = strrchr(s, ':')))
282 return pa_xstrdup(s);
283
284 *port = atoi(e+1);
285 return pa_xstrndup(s, e-s);
286 }
287 }
288
289 struct pa_socket_client* pa_socket_client_new_string(struct pa_mainloop_api *m, const char*name, uint16_t default_port) {
290 const char *p;
291 struct pa_socket_client *c = NULL;
292 enum { KIND_UNIX, KIND_TCP_AUTO, KIND_TCP4, KIND_TCP6 } kind = KIND_TCP_AUTO;
293 assert(m && name);
294
295 if (*name == '{') {
296 char hn[256], *pfx;
297 /* The URL starts with a host specification for detecting local connections */
298
299 if (!pa_get_host_name(hn, sizeof(hn)))
300 return NULL;
301
302 pfx = pa_sprintf_malloc("{%s}", hn);
303 if (!pa_startswith(name, pfx))
304 /* Not local */
305 return NULL;
306
307 p = name + strlen(pfx);
308 } else
309 p = name;
310
311 if (*p == '/')
312 kind = KIND_UNIX;
313 else if (pa_startswith(p, "unix:")) {
314 kind = KIND_UNIX;
315 p += sizeof("unix:")-1;
316 } else if (pa_startswith(p, "tcp:") || pa_startswith(p, "tcp4:")) {
317 kind = KIND_TCP4;
318 p += sizeof("tcp:")-1;
319 } else if (pa_startswith(p, "tcp6:")) {
320 kind = KIND_TCP6;
321 p += sizeof("tcp6:")-1;
322 }
323
324 switch (kind) {
325 case KIND_UNIX:
326 return pa_socket_client_new_unix(m, p);
327
328 case KIND_TCP_AUTO: /* Fallthrough */
329 case KIND_TCP4:
330 case KIND_TCP6: {
331 uint16_t port = default_port;
332 char *h;
333 struct addrinfo hints, *res;
334
335 if (!(h = parse_address(p, &port)))
336 return NULL;
337
338 memset(&hints, 0, sizeof(hints));
339 hints.ai_family = kind == KIND_TCP4 ? AF_INET : (kind == KIND_TCP6 ? AF_INET6 : AF_UNSPEC);
340
341 if (getaddrinfo(h, NULL, &hints, &res) < 0 || !res)
342 return NULL;
343
344 if (res->ai_addr->sa_family == AF_INET)
345 ((struct sockaddr_in*) res->ai_addr)->sin_port = htons(port);
346 else if (res->ai_addr->sa_family == AF_INET6)
347 ((struct sockaddr_in6*) res->ai_addr)->sin6_port = htons(port);
348 else
349 return NULL;
350
351 c = pa_socket_client_new_sockaddr(m, res->ai_addr, res->ai_addrlen);
352 freeaddrinfo(res);
353 return c;
354 }
355 }
356
357 /* Should never be reached */
358 assert(0);
359 return NULL;
360
361 }
362
363 int pa_socket_client_is_local(struct pa_socket_client *c) {
364 assert(c);
365 return c->local;
366 }