]> git.ozlabs.org Git - ccan/blob - ccan/io/io.c
ccan/io: generic init function for listening connections.
[ccan] / ccan / io / io.c
1 /* Licensed under LGPLv2.1+ - see LICENSE file for details */
2 #include "io.h"
3 #include "backend.h"
4 #include <sys/types.h>
5 #include <sys/socket.h>
6 #include <netdb.h>
7 #include <string.h>
8 #include <errno.h>
9 #include <stdlib.h>
10 #include <assert.h>
11 #include <poll.h>
12
13 void *io_loop_return;
14
15 struct io_listener *io_new_listener_(int fd,
16                                      void (*init)(int fd, void *arg),
17                                      void *arg)
18 {
19         struct io_listener *l = malloc(sizeof(*l));
20
21         if (!l)
22                 return NULL;
23
24         l->fd.listener = true;
25         l->fd.fd = fd;
26         l->init = init;
27         l->arg = arg;
28         if (!add_listener(l)) {
29                 free(l);
30                 return NULL;
31         }
32         return l;
33 }
34
35 void io_close_listener(struct io_listener *l)
36 {
37         close(l->fd.fd);
38         del_listener(l);
39         free(l);
40 }
41
42 struct io_conn *io_new_conn_(int fd,
43                              struct io_plan (*start)(struct io_conn *, void *),
44                              void (*finish)(struct io_conn *, void *),
45                              void *arg)
46 {
47         struct io_conn *conn = malloc(sizeof(*conn));
48
49         if (!conn)
50                 return NULL;
51
52         conn->fd.listener = false;
53         conn->fd.fd = fd;
54         conn->plan.next = start;
55         conn->finish = finish;
56         conn->finish_arg = conn->plan.next_arg = arg;
57         conn->plan.pollflag = 0;
58         conn->plan.state = IO_NEXT;
59         conn->duplex = NULL;
60         conn->timeout = NULL;
61         if (!add_conn(conn)) {
62                 free(conn);
63                 return NULL;
64         }
65         return conn;
66 }
67
68 struct io_conn *io_duplex_(struct io_conn *old,
69                              struct io_plan (*start)(struct io_conn *, void *),
70                              void (*finish)(struct io_conn *, void *),
71                              void *arg)
72 {
73         struct io_conn *conn;
74
75         assert(!old->duplex);
76
77         conn = malloc(sizeof(*conn));
78         if (!conn)
79                 return NULL;
80
81         conn->fd.listener = false;
82         conn->fd.fd = old->fd.fd;
83         conn->plan.next = start;
84         conn->finish = finish;
85         conn->finish_arg = conn->plan.next_arg = arg;
86         conn->plan.pollflag = 0;
87         conn->plan.state = IO_NEXT;
88         conn->duplex = old;
89         conn->timeout = NULL;
90         if (!add_duplex(conn)) {
91                 free(conn);
92                 return NULL;
93         }
94         old->duplex = conn;
95         return conn;
96 }
97
98 bool io_timeout_(struct io_conn *conn, struct timespec ts,
99                  struct io_plan (*cb)(struct io_conn *, void *), void *arg)
100 {
101         if (!conn->timeout) {
102                 conn->timeout = malloc(sizeof(*conn->timeout));
103                 if (!conn->timeout)
104                         return false;
105         } else
106                 assert(!timeout_active(conn));
107
108         conn->timeout->next = cb;
109         conn->timeout->next_arg = arg;
110         backend_add_timeout(conn, ts);
111         return true;
112 }
113
114 static enum io_result do_write(struct io_conn *conn)
115 {
116         ssize_t ret = write(conn->fd.fd, conn->plan.u.write.buf, conn->plan.u.write.len);
117         if (ret < 0)
118                 return RESULT_CLOSE;
119
120         conn->plan.u.write.buf += ret;
121         conn->plan.u.write.len -= ret;
122         if (conn->plan.u.write.len == 0)
123                 return RESULT_FINISHED;
124         else
125                 return RESULT_AGAIN;
126 }
127
128 /* Queue some data to be written. */
129 struct io_plan io_write_(const void *data, size_t len,
130                          struct io_plan (*cb)(struct io_conn *, void *),
131                          void *arg)
132 {
133         struct io_plan plan;
134
135         plan.u.write.buf = data;
136         plan.u.write.len = len;
137         plan.io = do_write;
138         plan.next = cb;
139         plan.next_arg = arg;
140         plan.pollflag = POLLOUT;
141         plan.state = IO_IO;
142         return plan;
143 }
144
145 static enum io_result do_read(struct io_conn *conn)
146 {
147         ssize_t ret = read(conn->fd.fd, conn->plan.u.read.buf,
148                            conn->plan.u.read.len);
149         if (ret <= 0)
150                 return RESULT_CLOSE;
151         conn->plan.u.read.buf += ret;
152         conn->plan.u.read.len -= ret;
153         if (conn->plan.u.read.len == 0)
154                 return RESULT_FINISHED;
155         else
156                 return RESULT_AGAIN;
157 }
158
159 /* Queue a request to read into a buffer. */
160 struct io_plan io_read_(void *data, size_t len,
161                         struct io_plan (*cb)(struct io_conn *, void *),
162                         void *arg)
163 {
164         struct io_plan plan;
165
166         plan.u.read.buf = data;
167         plan.u.read.len = len;
168         plan.io = do_read;
169         plan.next = cb;
170         plan.next_arg = arg;
171         plan.pollflag = POLLIN;
172         plan.state = IO_IO;
173         return plan;
174 }
175
176 static enum io_result do_read_partial(struct io_conn *conn)
177 {
178         ssize_t ret = read(conn->fd.fd, conn->plan.u.readpart.buf,
179                            *conn->plan.u.readpart.lenp);
180         if (ret <= 0)
181                 return RESULT_CLOSE;
182         *conn->plan.u.readpart.lenp = ret;
183         return RESULT_FINISHED;
184 }
185
186 /* Queue a partial request to read into a buffer. */
187 struct io_plan io_read_partial_(void *data, size_t *len,
188                                 struct io_plan (*cb)(struct io_conn *, void *),
189                                 void *arg)
190 {
191         struct io_plan plan;
192
193         plan.u.readpart.buf = data;
194         plan.u.readpart.lenp = len;
195         plan.io = do_read_partial;
196         plan.next = cb;
197         plan.next_arg = arg;
198         plan.pollflag = POLLIN;
199         plan.state = IO_IO;
200
201         return plan;
202 }
203
204 static enum io_result do_write_partial(struct io_conn *conn)
205 {
206         ssize_t ret = write(conn->fd.fd, conn->plan.u.writepart.buf,
207                             *conn->plan.u.writepart.lenp);
208         if (ret < 0)
209                 return RESULT_CLOSE;
210         *conn->plan.u.writepart.lenp = ret;
211         return RESULT_FINISHED;
212 }
213
214 /* Queue a partial write request. */
215 struct io_plan io_write_partial_(const void *data, size_t *len,
216                                  struct io_plan (*cb)(struct io_conn*, void *),
217                                  void *arg)
218 {
219         struct io_plan plan;
220
221         plan.u.writepart.buf = data;
222         plan.u.writepart.lenp = len;
223         plan.io = do_write_partial;
224         plan.next = cb;
225         plan.next_arg = arg;
226         plan.pollflag = POLLOUT;
227         plan.state = IO_IO;
228
229         return plan;
230 }
231
232 struct io_plan io_idle(void)
233 {
234         struct io_plan plan;
235
236         plan.pollflag = 0;
237         plan.state = IO_IDLE;
238
239         return plan;
240 }
241
242 void io_wake_(struct io_conn *conn,
243               struct io_plan (*fn)(struct io_conn *, void *), void *arg)
244
245 {
246         /* It might have finished, but we haven't called its finish() yet. */
247         if (conn->plan.state == IO_FINISHED)
248                 return;
249         assert(conn->plan.state == IO_IDLE);
250         conn->plan.next = fn;
251         conn->plan.next_arg = arg;
252         conn->plan.pollflag = 0;
253         conn->plan.state = IO_NEXT;
254         backend_wakeup(conn);
255 }
256
257 static struct io_plan do_next(struct io_conn *conn)
258 {
259         if (timeout_active(conn))
260                 backend_del_timeout(conn);
261         return conn->plan.next(conn, conn->plan.next_arg);
262 }
263
264 struct io_plan do_ready(struct io_conn *conn)
265 {
266         assert(conn->plan.state == IO_IO);
267         switch (conn->plan.io(conn)) {
268         case RESULT_CLOSE:
269                 return io_close(conn, NULL);
270         case RESULT_FINISHED:
271                 return do_next(conn);
272         case RESULT_AGAIN:
273                 return conn->plan;
274         default:
275                 abort();
276         }
277 }
278
279 /* Useful next functions. */
280 /* Close the connection, we're done. */
281 struct io_plan io_close(struct io_conn *conn, void *arg)
282 {
283         struct io_plan plan;
284
285         plan.state = IO_FINISHED;
286         plan.pollflag = 0;
287
288         return plan;
289 }
290
291 /* Exit the loop, returning this (non-NULL) arg. */
292 struct io_plan io_break_(void *ret,
293                          struct io_plan (*fn)(struct io_conn *, void *),
294                          void *arg)
295 {
296         struct io_plan plan;
297
298         io_loop_return = ret;
299
300         plan.state = IO_NEXT;
301         plan.pollflag = 0;
302         plan.next = fn;
303         plan.next_arg = arg;
304
305         return plan;
306 }