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 struct io_plan *(*next)(struct io_conn *, void *),
130 struct io_plan *plan;
132 /* If we're duplex, we want this on the current plan. Otherwise,
134 if (conn->plan[IO_IN].status == IO_UNSET)
135 plan = io_get_plan(conn, IO_IN);
137 plan = io_get_plan(conn, IO_OUT);
140 set_always(conn, plan, next, arg);
145 static int do_write(int fd, struct io_plan *plan)
147 ssize_t ret = write(fd, plan->u1.cp, plan->u2.s);
153 return plan->u2.s == 0;
156 /* Queue some data to be written. */
157 struct io_plan *io_write_(struct io_conn *conn, const void *data, size_t len,
158 struct io_plan *(*next)(struct io_conn *, void *),
161 struct io_plan *plan = io_get_plan(conn, IO_OUT);
166 return set_always(conn, plan, next, arg);
168 plan->u1.const_vp = data;
172 plan->next_arg = arg;
177 static int do_read(int fd, struct io_plan *plan)
179 ssize_t ret = read(fd, plan->u1.cp, plan->u2.s);
185 return plan->u2.s == 0;
188 /* Queue a request to read into a buffer. */
189 struct io_plan *io_read_(struct io_conn *conn,
190 void *data, size_t len,
191 struct io_plan *(*next)(struct io_conn *, void *),
194 struct io_plan *plan = io_get_plan(conn, IO_IN);
199 return set_always(conn, plan, next, arg);
205 plan->next_arg = arg;
210 static int do_read_partial(int fd, struct io_plan *plan)
212 ssize_t ret = read(fd, plan->u1.cp, *(size_t *)plan->u2.vp);
216 *(size_t *)plan->u2.vp = ret;
220 /* Queue a partial request to read into a buffer. */
221 struct io_plan *io_read_partial_(struct io_conn *conn,
222 void *data, size_t maxlen, size_t *len,
223 struct io_plan *(*next)(struct io_conn *,
227 struct io_plan *plan = io_get_plan(conn, IO_IN);
232 return set_always(conn, plan, next, arg);
235 /* We store the max len in here temporarily. */
238 plan->io = do_read_partial;
240 plan->next_arg = arg;
245 static int do_write_partial(int fd, struct io_plan *plan)
247 ssize_t ret = write(fd, plan->u1.cp, *(size_t *)plan->u2.vp);
251 *(size_t *)plan->u2.vp = ret;
255 /* Queue a partial write request. */
256 struct io_plan *io_write_partial_(struct io_conn *conn,
257 const void *data, size_t maxlen, size_t *len,
258 struct io_plan *(*next)(struct io_conn *,
262 struct io_plan *plan = io_get_plan(conn, IO_OUT);
267 return set_always(conn, plan, next, arg);
269 plan->u1.const_vp = data;
270 /* We store the max len in here temporarily. */
273 plan->io = do_write_partial;
275 plan->next_arg = arg;
280 static int do_connect(int fd, struct io_plan *plan)
283 socklen_t len = sizeof(err);
285 /* Has async connect finished? */
286 ret = getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &len);
291 /* Restore blocking if it was initially. */
292 fcntl(fd, F_SETFL, plan->u1.s);
294 } else if (err == EINPROGRESS)
301 struct io_plan *io_connect_(struct io_conn *conn, const struct addrinfo *addr,
302 struct io_plan *(*next)(struct io_conn *, void *),
305 struct io_plan *plan = io_get_plan(conn, IO_IN);
306 int fd = io_conn_fd(conn);
310 /* Save old flags, set nonblock if not already. */
311 plan->u1.s = fcntl(fd, F_GETFL);
312 fcntl(fd, F_SETFL, plan->u1.s | O_NONBLOCK);
314 /* Immediate connect can happen. */
315 if (connect(fd, addr->ai_addr, addr->ai_addrlen) == 0)
316 return set_always(conn, plan, next, arg);
318 if (errno != EINPROGRESS)
319 return io_close(conn);
322 plan->next_arg = arg;
323 plan->io = do_connect;
328 struct io_plan *io_wait_(struct io_conn *conn,
330 struct io_plan *(*next)(struct io_conn *, void *),
333 struct io_plan *plan;
335 /* If we're duplex, we want this on the current plan. Otherwise,
337 if (conn->plan[IO_IN].status == IO_UNSET)
338 plan = io_get_plan(conn, IO_IN);
340 plan = io_get_plan(conn, IO_OUT);
345 plan->next_arg = arg;
346 plan->u1.const_vp = wait;
347 plan->status = IO_WAITING;
352 void io_wake(const void *wait)
357 static void do_plan(struct io_conn *conn, struct io_plan *plan)
359 /* Someone else might have called io_close() on us. */
360 if (plan->status == IO_CLOSING)
363 /* We shouldn't have polled for this event if this wasn't true! */
364 assert(plan->status == IO_POLLING);
366 switch (plan->io(conn->fd.fd, plan)) {
373 next_plan(conn, plan);
376 /* IO should only return -1, 0 or 1 */
381 void io_ready(struct io_conn *conn, int pollflags)
383 if (pollflags & POLLIN)
384 do_plan(conn, &conn->plan[IO_IN]);
386 if (pollflags & POLLOUT)
387 do_plan(conn, &conn->plan[IO_OUT]);
390 void io_do_always(struct io_conn *conn)
392 if (conn->plan[IO_IN].status == IO_ALWAYS)
393 next_plan(conn, &conn->plan[IO_IN]);
395 if (conn->plan[IO_OUT].status == IO_ALWAYS)
396 next_plan(conn, &conn->plan[IO_OUT]);
399 void io_do_wakeup(struct io_conn *conn, struct io_plan *plan)
401 assert(plan->status == IO_WAITING);
402 next_plan(conn, plan);
405 /* Close the connection, we're done. */
406 struct io_plan *io_close(struct io_conn *conn)
408 /* Already closing? Don't close twice. */
409 if (conn->plan[IO_IN].status == IO_CLOSING)
410 return &conn->plan[IO_IN];
412 conn->plan[IO_IN].status = conn->plan[IO_OUT].status = IO_CLOSING;
413 conn->plan[IO_IN].u1.s = errno;
414 backend_new_closing(conn);
416 return &conn->plan[IO_IN];
419 struct io_plan *io_close_cb(struct io_conn *conn, void *arg)
421 return io_close(conn);
424 /* Exit the loop, returning this (non-NULL) arg. */
425 void io_break(const void *ret)
428 io_loop_return = (void *)ret;
431 struct io_plan *io_never(struct io_conn *conn)
433 return io_always(conn, io_never_called, NULL);
436 int io_conn_fd(const struct io_conn *conn)