1 /* Licensed under LGPLv2.1+ - see LICENSE file for details */
5 #include <sys/socket.h>
13 #include <ccan/container_of/container_of.h>
17 struct io_listener *io_new_listener_(const tal_t *ctx, int fd,
18 struct io_plan *(*init)(struct io_conn *,
22 struct io_listener *l = tal(ctx, struct io_listener);
26 l->fd.listener = true;
36 void io_close_listener(struct io_listener *l)
43 static struct io_plan *io_never_called(struct io_conn *conn, void *arg)
48 static void next_plan(struct io_conn *conn, struct io_plan *plan)
50 struct io_plan *(*next)(struct io_conn *, void *arg);
54 plan->status = IO_UNSET;
56 plan->next = io_never_called;
58 plan = next(conn, plan->next_arg);
60 /* It should have set a plan inside this conn (or duplex) */
61 assert(plan == &conn->plan[IO_IN]
62 || plan == &conn->plan[IO_OUT]
63 || plan == &conn->plan[2]);
64 assert(conn->plan[IO_IN].status != IO_UNSET
65 || conn->plan[IO_OUT].status != IO_UNSET);
67 backend_new_plan(conn);
70 struct io_conn *io_new_conn_(const tal_t *ctx, int fd,
71 struct io_plan *(*init)(struct io_conn *, void *),
74 struct io_conn *conn = tal(ctx, struct io_conn);
79 conn->fd.listener = false;
82 conn->finish_arg = NULL;
87 return tal_free(conn);
89 /* We start with out doing nothing, and in doing our init. */
90 conn->plan[IO_OUT].status = IO_UNSET;
92 conn->plan[IO_IN].next = init;
93 conn->plan[IO_IN].next_arg = arg;
94 next_plan(conn, &conn->plan[IO_IN]);
99 void io_set_finish_(struct io_conn *conn,
100 void (*finish)(struct io_conn *, void *),
103 conn->finish = finish;
104 conn->finish_arg = arg;
107 struct io_plan_arg *io_plan_arg(struct io_conn *conn, enum io_direction dir)
109 assert(conn->plan[dir].status == IO_UNSET);
111 conn->plan[dir].status = IO_POLLING;
112 return &conn->plan[dir].arg;
115 static struct io_plan *set_always(struct io_conn *conn,
116 enum io_direction dir,
117 struct io_plan *(*next)(struct io_conn *,
121 struct io_plan *plan = &conn->plan[dir];
123 plan->status = IO_ALWAYS;
124 backend_new_always(conn);
125 return io_set_plan(conn, dir, NULL, next, arg);
128 struct io_plan *io_always_(struct io_conn *conn,
129 struct io_plan *(*next)(struct io_conn *, void *),
132 /* If we're duplex, we want this on the current plan. Otherwise,
134 if (conn->plan[IO_IN].status == IO_UNSET)
135 return set_always(conn, IO_IN, next, arg);
137 return set_always(conn, IO_OUT, next, arg);
140 static int do_write(int fd, struct io_plan_arg *arg)
142 ssize_t ret = write(fd, arg->u1.cp, arg->u2.s);
148 return arg->u2.s == 0;
151 /* Queue some data to be written. */
152 struct io_plan *io_write_(struct io_conn *conn, const void *data, size_t len,
153 struct io_plan *(*next)(struct io_conn *, void *),
156 struct io_plan_arg *arg = io_plan_arg(conn, IO_OUT);
159 return set_always(conn, IO_OUT, next, next_arg);
161 arg->u1.const_vp = data;
164 return io_set_plan(conn, IO_OUT, do_write, next, next_arg);
167 static int do_read(int fd, struct io_plan_arg *arg)
169 ssize_t ret = read(fd, arg->u1.cp, arg->u2.s);
175 return arg->u2.s == 0;
178 /* Queue a request to read into a buffer. */
179 struct io_plan *io_read_(struct io_conn *conn,
180 void *data, size_t len,
181 struct io_plan *(*next)(struct io_conn *, void *),
184 struct io_plan_arg *arg = io_plan_arg(conn, IO_IN);
187 return set_always(conn, IO_IN, next, next_arg);
192 return io_set_plan(conn, IO_IN, do_read, next, next_arg);
195 static int do_read_partial(int fd, struct io_plan_arg *arg)
197 ssize_t ret = read(fd, arg->u1.cp, *(size_t *)arg->u2.vp);
201 *(size_t *)arg->u2.vp = ret;
205 /* Queue a partial request to read into a buffer. */
206 struct io_plan *io_read_partial_(struct io_conn *conn,
207 void *data, size_t maxlen, size_t *len,
208 struct io_plan *(*next)(struct io_conn *,
212 struct io_plan_arg *arg = io_plan_arg(conn, IO_IN);
215 return set_always(conn, IO_IN, next, next_arg);
218 /* We store the max len in here temporarily. */
222 return io_set_plan(conn, IO_IN, do_read_partial, next, next_arg);
225 static int do_write_partial(int fd, struct io_plan_arg *arg)
227 ssize_t ret = write(fd, arg->u1.cp, *(size_t *)arg->u2.vp);
231 *(size_t *)arg->u2.vp = ret;
235 /* Queue a partial write request. */
236 struct io_plan *io_write_partial_(struct io_conn *conn,
237 const void *data, size_t maxlen, size_t *len,
238 struct io_plan *(*next)(struct io_conn *,
242 struct io_plan_arg *arg = io_plan_arg(conn, IO_OUT);
245 return set_always(conn, IO_OUT, next, next_arg);
247 arg->u1.const_vp = data;
248 /* We store the max len in here temporarily. */
252 return io_set_plan(conn, IO_OUT, do_write_partial, next, next_arg);
255 static int do_connect(int fd, struct io_plan_arg *arg)
258 socklen_t len = sizeof(err);
260 /* Has async connect finished? */
261 ret = getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &len);
266 /* Restore blocking if it was initially. */
267 fcntl(fd, F_SETFL, arg->u1.s);
269 } else if (err == EINPROGRESS)
276 struct io_plan *io_connect_(struct io_conn *conn, const struct addrinfo *addr,
277 struct io_plan *(*next)(struct io_conn *, void *),
280 struct io_plan_arg *arg = io_plan_arg(conn, IO_IN);
281 int fd = io_conn_fd(conn);
283 /* Save old flags, set nonblock if not already. */
284 arg->u1.s = fcntl(fd, F_GETFL);
285 fcntl(fd, F_SETFL, arg->u1.s | O_NONBLOCK);
287 /* Immediate connect can happen. */
288 if (connect(fd, addr->ai_addr, addr->ai_addrlen) == 0)
289 return set_always(conn, IO_IN, next, next_arg);
291 if (errno != EINPROGRESS)
292 return io_close(conn);
294 return io_set_plan(conn, IO_IN, do_connect, next, next_arg);
297 struct io_plan *io_wait_(struct io_conn *conn,
299 struct io_plan *(*next)(struct io_conn *, void *),
302 enum io_direction dir;
303 struct io_plan_arg *arg;
305 /* If we're duplex, we want this on the current plan. Otherwise,
307 if (conn->plan[IO_IN].status == IO_UNSET)
312 arg = io_plan_arg(conn, dir);
313 arg->u1.const_vp = wait;
315 conn->plan[dir].status = IO_WAITING;
317 return io_set_plan(conn, dir, NULL, next, next_arg);
320 void io_wake(const void *wait)
325 static int do_plan(struct io_conn *conn, struct io_plan *plan)
327 /* Someone else might have called io_close() on us. */
328 if (plan->status == IO_CLOSING)
331 /* We shouldn't have polled for this event if this wasn't true! */
332 assert(plan->status == IO_POLLING);
334 switch (plan->io(conn->fd.fd, &plan->arg)) {
341 next_plan(conn, plan);
344 /* IO should only return -1, 0 or 1 */
349 void io_ready(struct io_conn *conn, int pollflags)
351 if (pollflags & POLLIN)
352 do_plan(conn, &conn->plan[IO_IN]);
354 if (pollflags & POLLOUT)
355 do_plan(conn, &conn->plan[IO_OUT]);
358 void io_do_always(struct io_conn *conn)
360 if (conn->plan[IO_IN].status == IO_ALWAYS)
361 next_plan(conn, &conn->plan[IO_IN]);
363 if (conn->plan[IO_OUT].status == IO_ALWAYS)
364 next_plan(conn, &conn->plan[IO_OUT]);
367 void io_do_wakeup(struct io_conn *conn, struct io_plan *plan)
369 assert(plan->status == IO_WAITING);
370 next_plan(conn, plan);
373 /* Close the connection, we're done. */
374 struct io_plan *io_close(struct io_conn *conn)
376 /* Already closing? Don't close twice. */
377 if (conn->plan[IO_IN].status == IO_CLOSING)
378 return &conn->plan[IO_IN];
380 conn->plan[IO_IN].status = conn->plan[IO_OUT].status = IO_CLOSING;
381 conn->plan[IO_IN].arg.u1.s = errno;
382 backend_new_closing(conn);
384 return io_set_plan(conn, IO_IN, NULL, NULL, NULL);
387 struct io_plan *io_close_cb(struct io_conn *conn, void *next_arg)
389 return io_close(conn);
392 /* Exit the loop, returning this (non-NULL) arg. */
393 void io_break(const void *ret)
396 io_loop_return = (void *)ret;
399 struct io_plan *io_never(struct io_conn *conn, void *unused)
401 return io_always(conn, io_never_called, NULL);
404 int io_conn_fd(const struct io_conn *conn)
409 void io_duplex_prepare(struct io_conn *conn)
411 assert(conn->plan[IO_IN].status == IO_UNSET);
412 assert(conn->plan[IO_OUT].status == IO_UNSET);
414 /* We can't sync debug until we've set both: io_wait() and io_always
415 * can't handle it. */
416 conn->debug_saved = conn->debug;
420 struct io_plan *io_duplex_(struct io_plan *in_plan, struct io_plan *out_plan)
422 struct io_conn *conn;
424 /* in_plan must be conn->plan[IO_IN], out_plan must be [IO_OUT] */
425 assert(out_plan == in_plan + 1);
428 conn = container_of(in_plan, struct io_conn, plan[IO_IN]);
429 conn->debug = conn->debug_saved;
431 /* Now set the plans again, to invoke sync debug. */
432 io_set_plan(conn, IO_OUT,
433 out_plan->io, out_plan->next, out_plan->next_arg);
434 io_set_plan(conn, IO_IN,
435 in_plan->io, in_plan->next, in_plan->next_arg);
440 struct io_plan *io_set_plan(struct io_conn *conn, enum io_direction dir,
441 int (*io)(int fd, struct io_plan_arg *arg),
442 struct io_plan *(*next)(struct io_conn *, void *),
445 struct io_plan *plan = &conn->plan[dir];
449 plan->next_arg = next_arg;
450 assert(plan->status == IO_CLOSING || next != NULL);
455 if (io_loop_return) {
456 io_debug_complete(conn);
460 switch (plan->status) {
462 while (do_plan(conn, plan) == 0);
464 /* Shouldn't happen, since you said you did plan! */
468 /* If other one is ALWAYS, leave in list! */
469 if (conn->plan[!dir].status != IO_ALWAYS)
470 remove_from_always(conn);
471 next_plan(conn, plan);
475 io_debug_complete(conn);
481 void io_set_debug(struct io_conn *conn, bool debug)
486 void io_debug_complete(struct io_conn *conn)