]> code.delx.au - pulseaudio/blob - src/socket-client.c
b10f8ab1a6d3228b276c7d5dc445f3f27323dc14
[pulseaudio] / src / socket-client.c
1 #include <unistd.h>
2 #include <stdio.h>
3 #include <errno.h>
4 #include <string.h>
5 #include <assert.h>
6 #include <stdlib.h>
7 #include <sys/un.h>
8 #include <netinet/in.h>
9 #include <arpa/inet.h>
10
11 #include "socket-client.h"
12 #include "util.h"
13
14 struct socket_client {
15 struct pa_mainloop_api *mainloop;
16 int fd;
17
18 void *io_source, *fixed_source;
19 void (*callback)(struct socket_client*c, struct iochannel *io, void *userdata);
20 void *userdata;
21 };
22
23 static struct socket_client*socket_client_new(struct pa_mainloop_api *m) {
24 struct socket_client *c;
25 assert(m);
26
27 c = malloc(sizeof(struct socket_client));
28 assert(c);
29 c->mainloop = m;
30 c->fd = -1;
31 c->io_source = c->fixed_source = NULL;
32 c->callback = NULL;
33 c->userdata = NULL;
34 return c;
35 }
36
37 static void do_call(struct socket_client *c) {
38 struct iochannel *io;
39 int error, lerror;
40 assert(c && c->callback);
41
42 lerror = sizeof(error);
43 if (getsockopt(c->fd, SOL_SOCKET, SO_ERROR, &error, &lerror) < 0) {
44 fprintf(stderr, "getsockopt(): %s\n", strerror(errno));
45 goto failed;
46 }
47
48 if (lerror != sizeof(error)) {
49 fprintf(stderr, "getsocktop() returned invalid size.\n");
50 goto failed;
51 }
52
53 if (error != 0) {
54 fprintf(stderr, "connect(): %s\n", strerror(error));
55 goto failed;
56 }
57
58 io = iochannel_new(c->mainloop, c->fd, c->fd);
59 assert(io);
60 c->fd = -1;
61 c->callback(c, io, c->userdata);
62
63 return;
64
65 failed:
66 close(c->fd);
67 c->fd = -1;
68 c->callback(c, NULL, c->userdata);
69 return;
70 }
71
72 static void connect_fixed_cb(struct pa_mainloop_api *m, void *id, void *userdata) {
73 struct socket_client *c = userdata;
74 assert(m && c && c->fixed_source == id);
75 m->cancel_fixed(m, c->fixed_source);
76 c->fixed_source = NULL;
77 do_call(c);
78 }
79
80 static void connect_io_cb(struct pa_mainloop_api*m, void *id, int fd, enum pa_mainloop_api_io_events events, void *userdata) {
81 struct socket_client *c = userdata;
82 assert(m && c && c->io_source == id && fd >= 0 && events == PA_MAINLOOP_API_IO_EVENT_OUTPUT);
83 m->cancel_io(m, c->io_source);
84 c->io_source = NULL;
85 do_call(c);
86 }
87
88 static int do_connect(struct socket_client *c, const struct sockaddr *sa, socklen_t len) {
89 int r;
90 assert(c && sa && len);
91
92 make_nonblock_fd(c->fd);
93
94 if ((r = connect(c->fd, sa, len)) < 0) {
95 if (r != EINPROGRESS) {
96 fprintf(stderr, "connect(): %s\n", strerror(errno));
97 return -1;
98 }
99
100 c->io_source = c->mainloop->source_io(c->mainloop, c->fd, PA_MAINLOOP_API_IO_EVENT_OUTPUT, connect_io_cb, c);
101 assert(c->io_source);
102 } else {
103 c->fixed_source = c->mainloop->source_fixed(c->mainloop, connect_fixed_cb, c);
104 assert(c->fixed_source);
105 }
106
107 return 0;
108 }
109
110 struct socket_client* socket_client_new_ipv4(struct pa_mainloop_api *m, uint32_t address, uint16_t port) {
111 struct socket_client *c;
112 struct sockaddr_in sa;
113 assert(m && address && port);
114
115 c = socket_client_new(m);
116 assert(c);
117
118 if ((c->fd = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
119 fprintf(stderr, "socket(): %s\n", strerror(errno));
120 goto fail;
121 }
122
123 sa.sin_family = AF_INET;
124 sa.sin_port = htons(port);
125 sa.sin_addr.s_addr = htonl(address);
126
127 if (do_connect(c, (struct sockaddr*) &sa, sizeof(sa)) < 0)
128 goto fail;
129
130 return c;
131
132 fail:
133 socket_client_free(c);
134 return NULL;
135 }
136
137 struct socket_client* socket_client_new_unix(struct pa_mainloop_api *m, const char *filename) {
138 struct socket_client *c;
139 struct sockaddr_un sa;
140 assert(m && filename);
141
142 c = socket_client_new(m);
143 assert(c);
144
145 if ((c->fd = socket(PF_LOCAL, SOCK_STREAM, 0)) < 0) {
146 fprintf(stderr, "socket(): %s\n", strerror(errno));
147 goto fail;
148 }
149
150 sa.sun_family = AF_LOCAL;
151 strncpy(sa.sun_path, filename, sizeof(sa.sun_path)-1);
152 sa.sun_path[sizeof(sa.sun_path) - 1] = 0;
153
154 if (do_connect(c, (struct sockaddr*) &sa, sizeof(sa)) < 0)
155 goto fail;
156
157 return c;
158
159 fail:
160 socket_client_free(c);
161 return NULL;
162 }
163
164 void socket_client_free(struct socket_client *c) {
165 assert(c && c->mainloop);
166 if (c->io_source)
167 c->mainloop->cancel_io(c->mainloop, c->io_source);
168 if (c->fixed_source)
169 c->mainloop->cancel_fixed(c->mainloop, c->fixed_source);
170 if (c->fd >= 0)
171 close(c->fd);
172 free(c);
173 }
174
175 void socket_client_set_callback(struct socket_client *c, void (*on_connection)(struct socket_client *c, struct iochannel*io, void *userdata), void *userdata) {
176 assert(c);
177 c->callback = on_connection;
178 c->userdata = userdata;
179 }