]> code.delx.au - pulseaudio/blob - src/socket-client.c
main part of the native protocol
[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->io_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
114 c = socket_client_new(m);
115 assert(c);
116
117 if ((c->fd = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
118 fprintf(stderr, "socket(): %s\n", strerror(errno));
119 goto fail;
120 }
121
122 sa.sin_family = AF_INET;
123 sa.sin_port = htons(port);
124 sa.sin_addr.s_addr = htonl(address);
125
126 if (do_connect(c, (struct sockaddr*) &sa, sizeof(sa)) < 0)
127 goto fail;
128
129 return c;
130
131 fail:
132 socket_client_free(c);
133 return NULL;
134 }
135
136 struct socket_client* socket_client_new_unix(struct pa_mainloop_api *m, const char *filename) {
137 struct socket_client *c;
138 struct sockaddr_un sa;
139
140 c = socket_client_new(m);
141 assert(c);
142
143 if ((c->fd = socket(PF_LOCAL, SOCK_STREAM, 0)) < 0) {
144 fprintf(stderr, "socket(): %s\n", strerror(errno));
145 goto fail;
146 }
147
148 sa.sun_family = AF_LOCAL;
149 strncpy(sa.sun_path, filename, sizeof(sa.sun_path)-1);
150 sa.sun_path[sizeof(sa.sun_path) - 1] = 0;
151
152 if (do_connect(c, (struct sockaddr*) &sa, sizeof(sa)) < 0)
153 goto fail;
154
155 return c;
156
157 fail:
158 socket_client_free(c);
159 return NULL;
160 }
161
162 void socket_client_free(struct socket_client *c) {
163 assert(c && c->mainloop);
164 if (c->io_source)
165 c->mainloop->cancel_io(c->mainloop, c->io_source);
166 if (c->fixed_source)
167 c->mainloop->cancel_fixed(c->mainloop, c->fixed_source);
168 if (c->fd >= 0)
169 close(c->fd);
170 free(c);
171 }
172
173 void socket_client_set_callback(struct socket_client *c, void (*on_connection)(struct socket_client *c, struct iochannel*io, void *userdata), void *userdata) {
174 assert(c);
175 c->callback = on_connection;
176 c->userdata = userdata;
177 }