]> code.delx.au - pulseaudio/blob - polyp/socket-client.c
make use F_CLOEXEC wherever useful
[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 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 <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
36 #include "socket-client.h"
37 #include "socket-util.h"
38 #include "util.h"
39 #include "xmalloc.h"
40
41 struct pa_socket_client {
42 int ref;
43 struct pa_mainloop_api *mainloop;
44 int fd;
45 struct pa_io_event *io_event;
46 struct pa_defer_event *defer_event;
47 void (*callback)(struct pa_socket_client*c, struct pa_iochannel *io, void *userdata);
48 void *userdata;
49 };
50
51 static struct pa_socket_client*pa_socket_client_new(struct pa_mainloop_api *m) {
52 struct pa_socket_client *c;
53 assert(m);
54
55 c = pa_xmalloc(sizeof(struct pa_socket_client));
56 c->ref = 1;
57 c->mainloop = m;
58 c->fd = -1;
59 c->io_event = NULL;
60 c->defer_event = NULL;
61 c->callback = NULL;
62 c->userdata = NULL;
63 return c;
64 }
65
66 static void do_call(struct pa_socket_client *c) {
67 struct pa_iochannel *io = NULL;
68 int error;
69 socklen_t lerror;
70 assert(c && c->callback);
71
72 pa_socket_client_ref(c);
73
74 lerror = sizeof(error);
75 if (getsockopt(c->fd, SOL_SOCKET, SO_ERROR, &error, &lerror) < 0) {
76 fprintf(stderr, "getsockopt(): %s\n", strerror(errno));
77 goto finish;
78 }
79
80 if (lerror != sizeof(error)) {
81 fprintf(stderr, "getsocktop() returned invalid size.\n");
82 goto finish;
83 }
84
85 if (error != 0) {
86 fprintf(stderr, "connect(): %s\n", strerror(error));
87 goto finish;
88 }
89
90 io = pa_iochannel_new(c->mainloop, c->fd, c->fd);
91 assert(io);
92
93 finish:
94 if (!io)
95 close(c->fd);
96 c->fd = -1;
97
98 assert(c->callback);
99 c->callback(c, io, c->userdata);
100
101 pa_socket_client_unref(c);
102 }
103
104 static void connect_fixed_cb(struct pa_mainloop_api *m, struct pa_defer_event *e, void *userdata) {
105 struct pa_socket_client *c = userdata;
106 assert(m && c && c->defer_event == e);
107 m->defer_free(c->defer_event);
108 c->defer_event = NULL;
109 do_call(c);
110 }
111
112 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) {
113 struct pa_socket_client *c = userdata;
114 assert(m && c && c->io_event == e && fd >= 0);
115 m->io_free(c->io_event);
116 c->io_event = NULL;
117 do_call(c);
118 }
119
120 static int do_connect(struct pa_socket_client *c, const struct sockaddr *sa, socklen_t len) {
121 int r;
122 assert(c && sa && len);
123
124 pa_make_nonblock_fd(c->fd);
125
126 if ((r = connect(c->fd, sa, len)) < 0) {
127 if (errno != EINPROGRESS) {
128 /*fprintf(stderr, "connect(): %s\n", strerror(errno));*/
129 return -1;
130 }
131
132 c->io_event = c->mainloop->io_new(c->mainloop, c->fd, PA_IO_EVENT_OUTPUT, connect_io_cb, c);
133 assert(c->io_event);
134 } else {
135 c->defer_event = c->mainloop->defer_new(c->mainloop, connect_fixed_cb, c);
136 assert(c->defer_event);
137 }
138
139 return 0;
140 }
141
142 struct pa_socket_client* pa_socket_client_new_ipv4(struct pa_mainloop_api *m, uint32_t address, uint16_t port) {
143 struct pa_socket_client *c;
144 struct sockaddr_in sa;
145 assert(m && address && port);
146
147 c = pa_socket_client_new(m);
148 assert(c);
149
150 if ((c->fd = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
151 fprintf(stderr, "socket(): %s\n", strerror(errno));
152 goto fail;
153 }
154
155 pa_fd_set_cloexec(c->fd, 1);
156 pa_socket_tcp_low_delay(c->fd);
157
158 sa.sin_family = AF_INET;
159 sa.sin_port = htons(port);
160 sa.sin_addr.s_addr = htonl(address);
161
162 if (do_connect(c, (struct sockaddr*) &sa, sizeof(sa)) < 0)
163 goto fail;
164
165 return c;
166
167 fail:
168 pa_socket_client_unref(c);
169 return NULL;
170 }
171
172 struct pa_socket_client* pa_socket_client_new_unix(struct pa_mainloop_api *m, const char *filename) {
173 struct pa_socket_client *c;
174 struct sockaddr_un sa;
175 assert(m && filename);
176
177 c = pa_socket_client_new(m);
178 assert(c);
179
180 if ((c->fd = socket(PF_LOCAL, SOCK_STREAM, 0)) < 0) {
181 fprintf(stderr, "socket(): %s\n", strerror(errno));
182 goto fail;
183 }
184
185 pa_fd_set_cloexec(c->fd, 1);
186 pa_socket_low_delay(c->fd);
187
188 sa.sun_family = AF_LOCAL;
189 strncpy(sa.sun_path, filename, sizeof(sa.sun_path)-1);
190 sa.sun_path[sizeof(sa.sun_path) - 1] = 0;
191
192 if (do_connect(c, (struct sockaddr*) &sa, sizeof(sa)) < 0)
193 goto fail;
194
195 return c;
196
197 fail:
198 pa_socket_client_unref(c);
199 return NULL;
200 }
201
202 struct pa_socket_client* pa_socket_client_new_sockaddr(struct pa_mainloop_api *m, const struct sockaddr *sa, size_t salen) {
203 struct pa_socket_client *c;
204 assert(m && sa);
205 c = pa_socket_client_new(m);
206 assert(c);
207
208 if ((c->fd = socket(sa->sa_family, SOCK_STREAM, 0)) < 0) {
209 fprintf(stderr, "socket(): %s\n", strerror(errno));
210 goto fail;
211 }
212
213 pa_fd_set_cloexec(c->fd, 1);
214 if (sa->sa_family == AF_INET)
215 pa_socket_tcp_low_delay(c->fd);
216 else
217 pa_socket_low_delay(c->fd);
218
219 if (do_connect(c, sa, salen) < 0)
220 goto fail;
221
222 return c;
223
224 fail:
225 pa_socket_client_unref(c);
226 return NULL;
227
228 }
229
230 void socket_client_free(struct pa_socket_client *c) {
231 assert(c && c->mainloop);
232 if (c->io_event)
233 c->mainloop->io_free(c->io_event);
234 if (c->defer_event)
235 c->mainloop->defer_free(c->defer_event);
236 if (c->fd >= 0)
237 close(c->fd);
238 pa_xfree(c);
239 }
240
241 void pa_socket_client_unref(struct pa_socket_client *c) {
242 assert(c && c->ref >= 1);
243
244 if (!(--(c->ref)))
245 socket_client_free(c);
246 }
247
248 struct pa_socket_client* pa_socket_client_ref(struct pa_socket_client *c) {
249 assert(c && c->ref >= 1);
250 c->ref++;
251 return c;
252 }
253
254 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) {
255 assert(c);
256 c->callback = on_connection;
257 c->userdata = userdata;
258 }