1 /* Licensed under LGPLv2.1+ - see LICENSE file for details */
5 #include <sys/socket.h>
16 struct io_listener *io_new_listener_(const tal_t *ctx, int fd,
17 struct io_plan *(*init)(struct io_conn *,
21 struct io_listener *l = tal(ctx, struct io_listener);
25 l->fd.listener = true;
35 void io_close_listener(struct io_listener *l)
42 static struct io_plan *io_never_called(struct io_conn *conn, void *arg)
47 static void next_plan(struct io_conn *conn, struct io_plan *plan)
49 struct io_plan *(*next)(struct io_conn *, void *arg);
53 plan->status = IO_UNSET;
55 plan->next = io_never_called;
57 plan = next(conn, plan->next_arg);
59 /* It should have set a plan inside this conn. */
60 assert(plan == &conn->plan[IO_IN]
61 || plan == &conn->plan[IO_OUT]);
62 assert(conn->plan[IO_IN].status != IO_UNSET
63 || conn->plan[IO_OUT].status != IO_UNSET);
65 backend_new_plan(conn);
68 struct io_conn *io_new_conn_(const tal_t *ctx, int fd,
69 struct io_plan *(*init)(struct io_conn *, void *),
72 struct io_conn *conn = tal(ctx, struct io_conn);
77 conn->fd.listener = false;
80 conn->finish_arg = NULL;
84 return tal_free(conn);
86 /* We start with out doing nothing, and in doing our init. */
87 conn->plan[IO_OUT].status = IO_UNSET;
89 conn->plan[IO_IN].next = init;
90 conn->plan[IO_IN].next_arg = arg;
91 next_plan(conn, &conn->plan[IO_IN]);
96 void io_set_finish_(struct io_conn *conn,
97 void (*finish)(struct io_conn *, void *),
100 conn->finish = finish;
101 conn->finish_arg = arg;
104 struct io_plan *io_get_plan(struct io_conn *conn, enum io_direction dir)
106 assert(conn->plan[dir].status == IO_UNSET);
108 conn->plan[dir].status = IO_POLLING;
109 return &conn->plan[dir];
112 static struct io_plan *set_always(struct io_conn *conn,
113 struct io_plan *plan,
114 struct io_plan *(*next)(struct io_conn *,
119 plan->next_arg = arg;
120 plan->status = IO_ALWAYS;
122 backend_new_always(conn);
126 struct io_plan *io_always_(struct io_conn *conn,
127 enum io_direction dir,
128 struct io_plan *(*next)(struct io_conn *, void *),
131 struct io_plan *plan = io_get_plan(conn, dir);
134 set_always(conn, plan, next, arg);
139 static int do_write(int fd, struct io_plan *plan)
141 ssize_t ret = write(fd, plan->u1.cp, plan->u2.s);
147 return plan->u2.s == 0;
150 /* Queue some data to be written. */
151 struct io_plan *io_write_(struct io_conn *conn, const void *data, size_t len,
152 struct io_plan *(*next)(struct io_conn *, void *),
155 struct io_plan *plan = io_get_plan(conn, IO_OUT);
160 return set_always(conn, plan, next, arg);
162 plan->u1.const_vp = data;
166 plan->next_arg = arg;
171 static int do_read(int fd, struct io_plan *plan)
173 ssize_t ret = read(fd, plan->u1.cp, plan->u2.s);
179 return plan->u2.s == 0;
182 /* Queue a request to read into a buffer. */
183 struct io_plan *io_read_(struct io_conn *conn,
184 void *data, size_t len,
185 struct io_plan *(*next)(struct io_conn *, void *),
188 struct io_plan *plan = io_get_plan(conn, IO_IN);
193 return set_always(conn, plan, next, arg);
199 plan->next_arg = arg;
204 static int do_read_partial(int fd, struct io_plan *plan)
206 ssize_t ret = read(fd, plan->u1.cp, *(size_t *)plan->u2.vp);
210 *(size_t *)plan->u2.vp = ret;
214 /* Queue a partial request to read into a buffer. */
215 struct io_plan *io_read_partial_(struct io_conn *conn,
216 void *data, size_t maxlen, size_t *len,
217 struct io_plan *(*next)(struct io_conn *,
221 struct io_plan *plan = io_get_plan(conn, IO_IN);
226 return set_always(conn, plan, next, arg);
229 /* We store the max len in here temporarily. */
232 plan->io = do_read_partial;
234 plan->next_arg = arg;
239 static int do_write_partial(int fd, struct io_plan *plan)
241 ssize_t ret = write(fd, plan->u1.cp, *(size_t *)plan->u2.vp);
245 *(size_t *)plan->u2.vp = ret;
249 /* Queue a partial write request. */
250 struct io_plan *io_write_partial_(struct io_conn *conn,
251 const void *data, size_t maxlen, size_t *len,
252 struct io_plan *(*next)(struct io_conn *,
256 struct io_plan *plan = io_get_plan(conn, IO_OUT);
261 return set_always(conn, plan, next, arg);
263 plan->u1.const_vp = data;
264 /* We store the max len in here temporarily. */
267 plan->io = do_write_partial;
269 plan->next_arg = arg;
274 static int do_connect(int fd, struct io_plan *plan)
277 socklen_t len = sizeof(err);
279 /* Has async connect finished? */
280 ret = getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &len);
285 /* Restore blocking if it was initially. */
286 fcntl(fd, F_SETFL, plan->u1.s);
288 } else if (err == EINPROGRESS)
295 struct io_plan *io_connect_(struct io_conn *conn, const struct addrinfo *addr,
296 struct io_plan *(*next)(struct io_conn *, void *),
299 struct io_plan *plan = io_get_plan(conn, IO_IN);
300 int fd = io_conn_fd(conn);
304 /* Save old flags, set nonblock if not already. */
305 plan->u1.s = fcntl(fd, F_GETFL);
306 fcntl(fd, F_SETFL, plan->u1.s | O_NONBLOCK);
308 /* Immediate connect can happen. */
309 if (connect(fd, addr->ai_addr, addr->ai_addrlen) == 0)
310 return set_always(conn, plan, next, arg);
312 if (errno != EINPROGRESS)
313 return io_close(conn);
316 plan->next_arg = arg;
317 plan->io = do_connect;
322 struct io_plan *io_wait_(struct io_conn *conn,
323 const void *wait, enum io_direction dir,
324 struct io_plan *(*next)(struct io_conn *, void *),
327 struct io_plan *plan = io_get_plan(conn, dir);
332 plan->next_arg = arg;
333 plan->u1.const_vp = wait;
334 plan->status = IO_WAITING;
339 void io_wake(const void *wait)
344 static void do_plan(struct io_conn *conn, struct io_plan *plan)
346 /* Someone else might have called io_close() on us. */
347 if (plan->status == IO_CLOSING)
350 /* We shouldn't have polled for this event if this wasn't true! */
351 assert(plan->status == IO_POLLING);
353 switch (plan->io(conn->fd.fd, plan)) {
360 next_plan(conn, plan);
363 /* IO should only return -1, 0 or 1 */
368 void io_ready(struct io_conn *conn, int pollflags)
370 if (pollflags & POLLIN)
371 do_plan(conn, &conn->plan[IO_IN]);
373 if (pollflags & POLLOUT)
374 do_plan(conn, &conn->plan[IO_OUT]);
377 void io_do_always(struct io_conn *conn)
379 if (conn->plan[IO_IN].status == IO_ALWAYS)
380 next_plan(conn, &conn->plan[IO_IN]);
382 if (conn->plan[IO_OUT].status == IO_ALWAYS)
383 next_plan(conn, &conn->plan[IO_OUT]);
386 void io_do_wakeup(struct io_conn *conn, struct io_plan *plan)
388 assert(plan->status == IO_WAITING);
389 next_plan(conn, plan);
392 /* Close the connection, we're done. */
393 struct io_plan *io_close(struct io_conn *conn)
395 /* Already closing? Don't close twice. */
396 if (conn->plan[IO_IN].status == IO_CLOSING)
397 return &conn->plan[IO_IN];
399 conn->plan[IO_IN].status = conn->plan[IO_OUT].status = IO_CLOSING;
400 conn->plan[IO_IN].u1.s = errno;
401 backend_new_closing(conn);
403 return &conn->plan[IO_IN];
406 struct io_plan *io_close_cb(struct io_conn *conn, void *arg)
408 return io_close(conn);
411 /* Exit the loop, returning this (non-NULL) arg. */
412 void io_break(const void *ret)
415 io_loop_return = (void *)ret;
418 struct io_plan *io_never(struct io_conn *conn)
420 return io_always(conn, IO_IN, io_never_called, NULL);
423 int io_conn_fd(const struct io_conn *conn)