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 static struct io_plan *io_always_dir(struct io_conn *conn,
129 enum io_direction dir,
130 struct io_plan *(*next)(struct io_conn *,
134 return set_always(conn, dir, next, arg);
137 struct io_plan *io_always_(struct io_conn *conn,
138 struct io_plan *(*next)(struct io_conn *, void *),
141 return io_always_dir(conn, IO_IN, next, arg);
144 struct io_plan *io_out_always_(struct io_conn *conn,
145 struct io_plan *(*next)(struct io_conn *,
149 return io_always_dir(conn, IO_OUT, next, arg);
152 static int do_write(int fd, struct io_plan_arg *arg)
154 ssize_t ret = write(fd, arg->u1.cp, arg->u2.s);
160 return arg->u2.s == 0;
163 /* Queue some data to be written. */
164 struct io_plan *io_write_(struct io_conn *conn, const void *data, size_t len,
165 struct io_plan *(*next)(struct io_conn *, void *),
168 struct io_plan_arg *arg = io_plan_arg(conn, IO_OUT);
171 return set_always(conn, IO_OUT, next, next_arg);
173 arg->u1.const_vp = data;
176 return io_set_plan(conn, IO_OUT, do_write, next, next_arg);
179 static int do_read(int fd, struct io_plan_arg *arg)
181 ssize_t ret = read(fd, arg->u1.cp, arg->u2.s);
187 return arg->u2.s == 0;
190 /* Queue a request to read into a buffer. */
191 struct io_plan *io_read_(struct io_conn *conn,
192 void *data, size_t len,
193 struct io_plan *(*next)(struct io_conn *, void *),
196 struct io_plan_arg *arg = io_plan_arg(conn, IO_IN);
199 return set_always(conn, IO_IN, next, next_arg);
204 return io_set_plan(conn, IO_IN, do_read, next, next_arg);
207 static int do_read_partial(int fd, struct io_plan_arg *arg)
209 ssize_t ret = read(fd, arg->u1.cp, *(size_t *)arg->u2.vp);
213 *(size_t *)arg->u2.vp = ret;
217 /* Queue a partial request to read into a buffer. */
218 struct io_plan *io_read_partial_(struct io_conn *conn,
219 void *data, size_t maxlen, size_t *len,
220 struct io_plan *(*next)(struct io_conn *,
224 struct io_plan_arg *arg = io_plan_arg(conn, IO_IN);
227 return set_always(conn, IO_IN, next, next_arg);
230 /* We store the max len in here temporarily. */
234 return io_set_plan(conn, IO_IN, do_read_partial, next, next_arg);
237 static int do_write_partial(int fd, struct io_plan_arg *arg)
239 ssize_t ret = write(fd, arg->u1.cp, *(size_t *)arg->u2.vp);
243 *(size_t *)arg->u2.vp = ret;
247 /* Queue a partial write request. */
248 struct io_plan *io_write_partial_(struct io_conn *conn,
249 const void *data, size_t maxlen, size_t *len,
250 struct io_plan *(*next)(struct io_conn *,
254 struct io_plan_arg *arg = io_plan_arg(conn, IO_OUT);
257 return set_always(conn, IO_OUT, next, next_arg);
259 arg->u1.const_vp = data;
260 /* We store the max len in here temporarily. */
264 return io_set_plan(conn, IO_OUT, do_write_partial, next, next_arg);
267 static int do_connect(int fd, struct io_plan_arg *arg)
270 socklen_t len = sizeof(err);
272 /* Has async connect finished? */
273 ret = getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &len);
278 /* Restore blocking if it was initially. */
279 fcntl(fd, F_SETFL, arg->u1.s);
281 } else if (err == EINPROGRESS)
288 struct io_plan *io_connect_(struct io_conn *conn, const struct addrinfo *addr,
289 struct io_plan *(*next)(struct io_conn *, void *),
292 struct io_plan_arg *arg = io_plan_arg(conn, IO_IN);
293 int fd = io_conn_fd(conn);
295 /* Save old flags, set nonblock if not already. */
296 arg->u1.s = fcntl(fd, F_GETFL);
297 fcntl(fd, F_SETFL, arg->u1.s | O_NONBLOCK);
299 /* Immediate connect can happen. */
300 if (connect(fd, addr->ai_addr, addr->ai_addrlen) == 0)
301 return set_always(conn, IO_IN, next, next_arg);
303 if (errno != EINPROGRESS)
304 return io_close(conn);
306 return io_set_plan(conn, IO_IN, do_connect, next, next_arg);
309 static struct io_plan *io_wait_dir(struct io_conn *conn,
311 enum io_direction dir,
312 struct io_plan *(*next)(struct io_conn *,
316 struct io_plan_arg *arg = io_plan_arg(conn, dir);
317 arg->u1.const_vp = wait;
319 conn->plan[dir].status = IO_WAITING;
321 return io_set_plan(conn, dir, NULL, next, next_arg);
324 struct io_plan *io_wait_(struct io_conn *conn,
326 struct io_plan *(*next)(struct io_conn *, void *),
329 return io_wait_dir(conn, wait, IO_IN, next, next_arg);
332 struct io_plan *io_out_wait_(struct io_conn *conn,
334 struct io_plan *(*next)(struct io_conn *, void *),
337 return io_wait_dir(conn, wait, IO_OUT, next, next_arg);
340 void io_wake(const void *wait)
345 static int do_plan(struct io_conn *conn, struct io_plan *plan)
347 /* Someone else might have called io_close() on us. */
348 if (plan->status == IO_CLOSING)
351 /* We shouldn't have polled for this event if this wasn't true! */
352 assert(plan->status == IO_POLLING);
354 switch (plan->io(conn->fd.fd, &plan->arg)) {
361 next_plan(conn, plan);
364 /* IO should only return -1, 0 or 1 */
369 void io_ready(struct io_conn *conn, int pollflags)
371 if (pollflags & POLLIN)
372 do_plan(conn, &conn->plan[IO_IN]);
374 if (pollflags & POLLOUT)
375 do_plan(conn, &conn->plan[IO_OUT]);
378 void io_do_always(struct io_conn *conn)
380 if (conn->plan[IO_IN].status == IO_ALWAYS)
381 next_plan(conn, &conn->plan[IO_IN]);
383 if (conn->plan[IO_OUT].status == IO_ALWAYS)
384 next_plan(conn, &conn->plan[IO_OUT]);
387 void io_do_wakeup(struct io_conn *conn, enum io_direction dir)
389 struct io_plan *plan = &conn->plan[dir];
391 assert(plan->status == IO_WAITING);
393 set_always(conn, dir, plan->next, plan->next_arg);
396 /* Close the connection, we're done. */
397 struct io_plan *io_close(struct io_conn *conn)
399 /* Already closing? Don't close twice. */
400 if (conn->plan[IO_IN].status == IO_CLOSING)
401 return &conn->plan[IO_IN];
403 conn->plan[IO_IN].status = conn->plan[IO_OUT].status = IO_CLOSING;
404 conn->plan[IO_IN].arg.u1.s = errno;
405 backend_new_closing(conn);
407 return io_set_plan(conn, IO_IN, NULL, NULL, NULL);
410 struct io_plan *io_close_cb(struct io_conn *conn, void *next_arg)
412 return io_close(conn);
415 /* Exit the loop, returning this (non-NULL) arg. */
416 void io_break(const void *ret)
419 io_loop_return = (void *)ret;
422 struct io_plan *io_never(struct io_conn *conn, void *unused)
424 return io_always(conn, io_never_called, NULL);
427 int io_conn_fd(const struct io_conn *conn)
432 void io_duplex_prepare(struct io_conn *conn)
434 assert(conn->plan[IO_IN].status == IO_UNSET);
435 assert(conn->plan[IO_OUT].status == IO_UNSET);
437 /* We can't sync debug until we've set both: io_wait() and io_always
438 * can't handle it. */
439 conn->debug_saved = conn->debug;
443 struct io_plan *io_duplex_(struct io_plan *in_plan, struct io_plan *out_plan)
445 struct io_conn *conn;
447 /* in_plan must be conn->plan[IO_IN], out_plan must be [IO_OUT] */
448 assert(out_plan == in_plan + 1);
451 conn = container_of(in_plan, struct io_conn, plan[IO_IN]);
452 conn->debug = conn->debug_saved;
454 /* Now set the plans again, to invoke sync debug. */
455 io_set_plan(conn, IO_OUT,
456 out_plan->io, out_plan->next, out_plan->next_arg);
457 io_set_plan(conn, IO_IN,
458 in_plan->io, in_plan->next, in_plan->next_arg);
463 struct io_plan *io_halfclose(struct io_conn *conn)
465 /* Already closing? Don't close twice. */
466 if (conn->plan[IO_IN].status == IO_CLOSING)
467 return &conn->plan[IO_IN];
469 /* Both unset? OK. */
470 if (conn->plan[IO_IN].status == IO_UNSET
471 && conn->plan[IO_OUT].status == IO_UNSET)
472 return io_close(conn);
474 /* We leave this unset then. */
475 if (conn->plan[IO_IN].status == IO_UNSET)
476 return &conn->plan[IO_IN];
478 return &conn->plan[IO_OUT];
481 struct io_plan *io_set_plan(struct io_conn *conn, enum io_direction dir,
482 int (*io)(int fd, struct io_plan_arg *arg),
483 struct io_plan *(*next)(struct io_conn *, void *),
486 struct io_plan *plan = &conn->plan[dir];
490 plan->next_arg = next_arg;
491 assert(plan->status == IO_CLOSING || next != NULL);
496 if (io_loop_return) {
497 io_debug_complete(conn);
501 switch (plan->status) {
503 while (do_plan(conn, plan) == 0);
505 /* Shouldn't happen, since you said you did plan! */
509 /* If other one is ALWAYS, leave in list! */
510 if (conn->plan[!dir].status != IO_ALWAYS)
511 remove_from_always(conn);
512 next_plan(conn, plan);
516 io_debug_complete(conn);
522 void io_set_debug(struct io_conn *conn, bool debug)
527 void io_debug_complete(struct io_conn *conn)