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 static void set_blocking(int fd, bool block)
72 int flags = fcntl(fd, F_GETFL);
79 fcntl(fd, F_SETFL, flags);
82 struct io_conn *io_new_conn_(const tal_t *ctx, int fd,
83 struct io_plan *(*init)(struct io_conn *, void *),
86 struct io_conn *conn = tal(ctx, struct io_conn);
91 conn->fd.listener = false;
94 conn->finish_arg = NULL;
95 list_node_init(&conn->always);
96 list_node_init(&conn->closing);
100 return tal_free(conn);
102 /* Keep our I/O async. */
103 set_blocking(fd, false);
105 /* We start with out doing nothing, and in doing our init. */
106 conn->plan[IO_OUT].status = IO_UNSET;
108 conn->plan[IO_IN].next = init;
109 conn->plan[IO_IN].next_arg = arg;
110 next_plan(conn, &conn->plan[IO_IN]);
115 void io_set_finish_(struct io_conn *conn,
116 void (*finish)(struct io_conn *, void *),
119 conn->finish = finish;
120 conn->finish_arg = arg;
123 struct io_plan_arg *io_plan_arg(struct io_conn *conn, enum io_direction dir)
125 assert(conn->plan[dir].status == IO_UNSET);
127 conn->plan[dir].status = IO_POLLING;
128 return &conn->plan[dir].arg;
131 static struct io_plan *set_always(struct io_conn *conn,
132 enum io_direction dir,
133 struct io_plan *(*next)(struct io_conn *,
137 struct io_plan *plan = &conn->plan[dir];
139 plan->status = IO_ALWAYS;
140 backend_new_always(conn);
141 return io_set_plan(conn, dir, NULL, next, arg);
144 static struct io_plan *io_always_dir(struct io_conn *conn,
145 enum io_direction dir,
146 struct io_plan *(*next)(struct io_conn *,
150 return set_always(conn, dir, next, arg);
153 struct io_plan *io_always_(struct io_conn *conn,
154 struct io_plan *(*next)(struct io_conn *, void *),
157 return io_always_dir(conn, IO_IN, next, arg);
160 struct io_plan *io_out_always_(struct io_conn *conn,
161 struct io_plan *(*next)(struct io_conn *,
165 return io_always_dir(conn, IO_OUT, next, arg);
168 static int do_write(int fd, struct io_plan_arg *arg)
170 ssize_t ret = write(fd, arg->u1.cp, arg->u2.s);
176 return arg->u2.s == 0;
179 /* Queue some data to be written. */
180 struct io_plan *io_write_(struct io_conn *conn, const 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_OUT);
187 return set_always(conn, IO_OUT, next, next_arg);
189 arg->u1.const_vp = data;
192 return io_set_plan(conn, IO_OUT, do_write, next, next_arg);
195 static int do_read(int fd, struct io_plan_arg *arg)
197 ssize_t ret = read(fd, arg->u1.cp, arg->u2.s);
203 return arg->u2.s == 0;
206 /* Queue a request to read into a buffer. */
207 struct io_plan *io_read_(struct io_conn *conn,
208 void *data, size_t len,
209 struct io_plan *(*next)(struct io_conn *, void *),
212 struct io_plan_arg *arg = io_plan_arg(conn, IO_IN);
215 return set_always(conn, IO_IN, next, next_arg);
220 return io_set_plan(conn, IO_IN, do_read, next, next_arg);
223 static int do_read_partial(int fd, struct io_plan_arg *arg)
225 ssize_t ret = read(fd, arg->u1.cp, *(size_t *)arg->u2.vp);
229 *(size_t *)arg->u2.vp = ret;
233 /* Queue a partial request to read into a buffer. */
234 struct io_plan *io_read_partial_(struct io_conn *conn,
235 void *data, size_t maxlen, size_t *len,
236 struct io_plan *(*next)(struct io_conn *,
240 struct io_plan_arg *arg = io_plan_arg(conn, IO_IN);
243 return set_always(conn, IO_IN, next, next_arg);
246 /* We store the max len in here temporarily. */
250 return io_set_plan(conn, IO_IN, do_read_partial, next, next_arg);
253 static int do_write_partial(int fd, struct io_plan_arg *arg)
255 ssize_t ret = write(fd, arg->u1.cp, *(size_t *)arg->u2.vp);
259 *(size_t *)arg->u2.vp = ret;
263 /* Queue a partial write request. */
264 struct io_plan *io_write_partial_(struct io_conn *conn,
265 const void *data, size_t maxlen, size_t *len,
266 struct io_plan *(*next)(struct io_conn *,
270 struct io_plan_arg *arg = io_plan_arg(conn, IO_OUT);
273 return set_always(conn, IO_OUT, next, next_arg);
275 arg->u1.const_vp = data;
276 /* We store the max len in here temporarily. */
280 return io_set_plan(conn, IO_OUT, do_write_partial, next, next_arg);
283 static int do_connect(int fd, struct io_plan_arg *arg)
286 socklen_t len = sizeof(err);
288 /* Has async connect finished? */
289 ret = getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &len);
295 } else if (err == EINPROGRESS)
302 struct io_plan *io_connect_(struct io_conn *conn, const struct addrinfo *addr,
303 struct io_plan *(*next)(struct io_conn *, void *),
306 int fd = io_conn_fd(conn);
308 /* We don't actually need the arg, but we need it polling. */
309 io_plan_arg(conn, IO_OUT);
311 /* Note that io_new_conn() will make fd O_NONBLOCK */
313 /* Immediate connect can happen. */
314 if (connect(fd, addr->ai_addr, addr->ai_addrlen) == 0)
315 return set_always(conn, IO_OUT, next, next_arg);
317 if (errno != EINPROGRESS)
318 return io_close(conn);
320 return io_set_plan(conn, IO_OUT, do_connect, next, next_arg);
323 static struct io_plan *io_wait_dir(struct io_conn *conn,
325 enum io_direction dir,
326 struct io_plan *(*next)(struct io_conn *,
330 struct io_plan_arg *arg = io_plan_arg(conn, dir);
331 arg->u1.const_vp = wait;
333 conn->plan[dir].status = IO_WAITING;
335 return io_set_plan(conn, dir, NULL, next, next_arg);
338 struct io_plan *io_wait_(struct io_conn *conn,
340 struct io_plan *(*next)(struct io_conn *, void *),
343 return io_wait_dir(conn, wait, IO_IN, next, next_arg);
346 struct io_plan *io_out_wait_(struct io_conn *conn,
348 struct io_plan *(*next)(struct io_conn *, void *),
351 return io_wait_dir(conn, wait, IO_OUT, next, next_arg);
354 void io_wake(const void *wait)
359 static int do_plan(struct io_conn *conn, struct io_plan *plan)
361 /* Someone else might have called io_close() on us. */
362 if (plan->status == IO_CLOSING)
365 /* We shouldn't have polled for this event if this wasn't true! */
366 assert(plan->status == IO_POLLING);
368 switch (plan->io(conn->fd.fd, &plan->arg)) {
375 next_plan(conn, plan);
378 /* IO should only return -1, 0 or 1 */
383 void io_ready(struct io_conn *conn, int pollflags)
385 if (pollflags & POLLIN)
386 do_plan(conn, &conn->plan[IO_IN]);
388 if (pollflags & POLLOUT)
389 do_plan(conn, &conn->plan[IO_OUT]);
392 void io_do_always(struct io_conn *conn)
394 if (conn->plan[IO_IN].status == IO_ALWAYS)
395 next_plan(conn, &conn->plan[IO_IN]);
397 if (conn->plan[IO_OUT].status == IO_ALWAYS)
398 next_plan(conn, &conn->plan[IO_OUT]);
401 void io_do_wakeup(struct io_conn *conn, enum io_direction dir)
403 struct io_plan *plan = &conn->plan[dir];
405 assert(plan->status == IO_WAITING);
407 set_always(conn, dir, plan->next, plan->next_arg);
410 /* Close the connection, we're done. */
411 struct io_plan *io_close(struct io_conn *conn)
413 /* Already closing? Don't close twice. */
414 if (conn->plan[IO_IN].status == IO_CLOSING)
415 return &conn->plan[IO_IN];
417 conn->plan[IO_IN].status = conn->plan[IO_OUT].status = IO_CLOSING;
418 conn->plan[IO_IN].arg.u1.s = errno;
419 backend_new_closing(conn);
421 return io_set_plan(conn, IO_IN, NULL, NULL, NULL);
424 struct io_plan *io_close_cb(struct io_conn *conn, void *next_arg)
426 return io_close(conn);
429 /* Exit the loop, returning this (non-NULL) arg. */
430 void io_break(const void *ret)
433 io_loop_return = (void *)ret;
436 struct io_plan *io_never(struct io_conn *conn, void *unused)
438 return io_always(conn, io_never_called, NULL);
441 int io_conn_fd(const struct io_conn *conn)
446 void io_duplex_prepare(struct io_conn *conn)
448 assert(conn->plan[IO_IN].status == IO_UNSET);
449 assert(conn->plan[IO_OUT].status == IO_UNSET);
451 /* We can't sync debug until we've set both: io_wait() and io_always
452 * can't handle it. */
453 conn->debug_saved = conn->debug;
454 io_set_debug(conn, false);
457 struct io_plan *io_duplex_(struct io_plan *in_plan, struct io_plan *out_plan)
459 struct io_conn *conn;
461 /* in_plan must be conn->plan[IO_IN], out_plan must be [IO_OUT] */
462 assert(out_plan == in_plan + 1);
465 conn = container_of(in_plan, struct io_conn, plan[IO_IN]);
466 io_set_debug(conn, conn->debug_saved);
468 /* Now set the plans again, to invoke sync debug. */
469 io_set_plan(conn, IO_OUT,
470 out_plan->io, out_plan->next, out_plan->next_arg);
471 io_set_plan(conn, IO_IN,
472 in_plan->io, in_plan->next, in_plan->next_arg);
477 struct io_plan *io_halfclose(struct io_conn *conn)
479 /* Already closing? Don't close twice. */
480 if (conn->plan[IO_IN].status == IO_CLOSING)
481 return &conn->plan[IO_IN];
483 /* Both unset? OK. */
484 if (conn->plan[IO_IN].status == IO_UNSET
485 && conn->plan[IO_OUT].status == IO_UNSET)
486 return io_close(conn);
488 /* We leave this unset then. */
489 if (conn->plan[IO_IN].status == IO_UNSET)
490 return &conn->plan[IO_IN];
492 return &conn->plan[IO_OUT];
495 struct io_plan *io_set_plan(struct io_conn *conn, enum io_direction dir,
496 int (*io)(int fd, struct io_plan_arg *arg),
497 struct io_plan *(*next)(struct io_conn *, void *),
500 struct io_plan *plan = &conn->plan[dir];
504 plan->next_arg = next_arg;
505 assert(plan->status == IO_CLOSING || next != NULL);
510 if (io_loop_return) {
511 io_debug_complete(conn);
515 switch (plan->status) {
517 while (do_plan(conn, plan) == 0);
519 /* Shouldn't happen, since you said you did plan! */
523 /* If other one is ALWAYS, leave in list! */
524 if (conn->plan[!dir].status != IO_ALWAYS)
525 remove_from_always(conn);
526 next_plan(conn, plan);
530 io_debug_complete(conn);
536 void io_set_debug(struct io_conn *conn, bool debug)
540 /* Debugging means fds must block. */
541 set_blocking(io_conn_fd(conn), debug);
544 void io_debug_complete(struct io_conn *conn)