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 (or duplex) */
60 assert(plan == &conn->plan[IO_IN]
61 || plan == &conn->plan[IO_OUT]
62 || plan == &conn->plan[2]);
63 assert(conn->plan[IO_IN].status != IO_UNSET
64 || conn->plan[IO_OUT].status != IO_UNSET);
66 backend_new_plan(conn);
69 struct io_conn *io_new_conn_(const tal_t *ctx, int fd,
70 struct io_plan *(*init)(struct io_conn *, void *),
73 struct io_conn *conn = tal(ctx, struct io_conn);
78 conn->fd.listener = false;
81 conn->finish_arg = NULL;
85 return tal_free(conn);
87 /* We start with out doing nothing, and in doing our init. */
88 conn->plan[IO_OUT].status = IO_UNSET;
90 conn->plan[IO_IN].next = init;
91 conn->plan[IO_IN].next_arg = arg;
92 next_plan(conn, &conn->plan[IO_IN]);
97 void io_set_finish_(struct io_conn *conn,
98 void (*finish)(struct io_conn *, void *),
101 conn->finish = finish;
102 conn->finish_arg = arg;
105 struct io_plan *io_get_plan(struct io_conn *conn, enum io_direction dir)
107 assert(conn->plan[dir].status == IO_UNSET);
109 conn->plan[dir].status = IO_POLLING;
110 return &conn->plan[dir];
113 static struct io_plan *set_always(struct io_conn *conn,
114 struct io_plan *plan,
115 struct io_plan *(*next)(struct io_conn *,
120 plan->next_arg = arg;
121 plan->status = IO_ALWAYS;
123 backend_new_always(conn);
127 struct io_plan *io_always_(struct io_conn *conn,
128 struct io_plan *(*next)(struct io_conn *, void *),
131 struct io_plan *plan;
133 /* If we're duplex, we want this on the current plan. Otherwise,
135 if (conn->plan[IO_IN].status == IO_UNSET)
136 plan = io_get_plan(conn, IO_IN);
138 plan = io_get_plan(conn, IO_OUT);
141 set_always(conn, plan, next, arg);
146 static int do_write(int fd, struct io_plan *plan)
148 ssize_t ret = write(fd, plan->u1.cp, plan->u2.s);
154 return plan->u2.s == 0;
157 /* Queue some data to be written. */
158 struct io_plan *io_write_(struct io_conn *conn, const void *data, size_t len,
159 struct io_plan *(*next)(struct io_conn *, void *),
162 struct io_plan *plan = io_get_plan(conn, IO_OUT);
167 return set_always(conn, plan, next, arg);
169 plan->u1.const_vp = data;
173 plan->next_arg = arg;
178 static int do_read(int fd, struct io_plan *plan)
180 ssize_t ret = read(fd, plan->u1.cp, plan->u2.s);
186 return plan->u2.s == 0;
189 /* Queue a request to read into a buffer. */
190 struct io_plan *io_read_(struct io_conn *conn,
191 void *data, size_t len,
192 struct io_plan *(*next)(struct io_conn *, void *),
195 struct io_plan *plan = io_get_plan(conn, IO_IN);
200 return set_always(conn, plan, next, arg);
206 plan->next_arg = arg;
211 static int do_read_partial(int fd, struct io_plan *plan)
213 ssize_t ret = read(fd, plan->u1.cp, *(size_t *)plan->u2.vp);
217 *(size_t *)plan->u2.vp = ret;
221 /* Queue a partial request to read into a buffer. */
222 struct io_plan *io_read_partial_(struct io_conn *conn,
223 void *data, size_t maxlen, size_t *len,
224 struct io_plan *(*next)(struct io_conn *,
228 struct io_plan *plan = io_get_plan(conn, IO_IN);
233 return set_always(conn, plan, next, arg);
236 /* We store the max len in here temporarily. */
239 plan->io = do_read_partial;
241 plan->next_arg = arg;
246 static int do_write_partial(int fd, struct io_plan *plan)
248 ssize_t ret = write(fd, plan->u1.cp, *(size_t *)plan->u2.vp);
252 *(size_t *)plan->u2.vp = ret;
256 /* Queue a partial write request. */
257 struct io_plan *io_write_partial_(struct io_conn *conn,
258 const void *data, size_t maxlen, size_t *len,
259 struct io_plan *(*next)(struct io_conn *,
263 struct io_plan *plan = io_get_plan(conn, IO_OUT);
268 return set_always(conn, plan, next, arg);
270 plan->u1.const_vp = data;
271 /* We store the max len in here temporarily. */
274 plan->io = do_write_partial;
276 plan->next_arg = arg;
281 static int do_connect(int fd, struct io_plan *plan)
284 socklen_t len = sizeof(err);
286 /* Has async connect finished? */
287 ret = getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &len);
292 /* Restore blocking if it was initially. */
293 fcntl(fd, F_SETFL, plan->u1.s);
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 struct io_plan *plan = io_get_plan(conn, IO_IN);
307 int fd = io_conn_fd(conn);
311 /* Save old flags, set nonblock if not already. */
312 plan->u1.s = fcntl(fd, F_GETFL);
313 fcntl(fd, F_SETFL, plan->u1.s | O_NONBLOCK);
315 /* Immediate connect can happen. */
316 if (connect(fd, addr->ai_addr, addr->ai_addrlen) == 0)
317 return set_always(conn, plan, next, arg);
319 if (errno != EINPROGRESS)
320 return io_close(conn);
323 plan->next_arg = arg;
324 plan->io = do_connect;
329 struct io_plan *io_wait_(struct io_conn *conn,
331 struct io_plan *(*next)(struct io_conn *, void *),
334 struct io_plan *plan;
336 /* If we're duplex, we want this on the current plan. Otherwise,
338 if (conn->plan[IO_IN].status == IO_UNSET)
339 plan = io_get_plan(conn, IO_IN);
341 plan = io_get_plan(conn, IO_OUT);
346 plan->next_arg = arg;
347 plan->u1.const_vp = wait;
348 plan->status = IO_WAITING;
353 void io_wake(const void *wait)
358 static void do_plan(struct io_conn *conn, struct io_plan *plan)
360 /* Someone else might have called io_close() on us. */
361 if (plan->status == IO_CLOSING)
364 /* We shouldn't have polled for this event if this wasn't true! */
365 assert(plan->status == IO_POLLING);
367 switch (plan->io(conn->fd.fd, plan)) {
374 next_plan(conn, plan);
377 /* IO should only return -1, 0 or 1 */
382 void io_ready(struct io_conn *conn, int pollflags)
384 if (pollflags & POLLIN)
385 do_plan(conn, &conn->plan[IO_IN]);
387 if (pollflags & POLLOUT)
388 do_plan(conn, &conn->plan[IO_OUT]);
391 void io_do_always(struct io_conn *conn)
393 if (conn->plan[IO_IN].status == IO_ALWAYS)
394 next_plan(conn, &conn->plan[IO_IN]);
396 if (conn->plan[IO_OUT].status == IO_ALWAYS)
397 next_plan(conn, &conn->plan[IO_OUT]);
400 void io_do_wakeup(struct io_conn *conn, struct io_plan *plan)
402 assert(plan->status == IO_WAITING);
403 next_plan(conn, plan);
406 /* Close the connection, we're done. */
407 struct io_plan *io_close(struct io_conn *conn)
409 /* Already closing? Don't close twice. */
410 if (conn->plan[IO_IN].status == IO_CLOSING)
411 return &conn->plan[IO_IN];
413 conn->plan[IO_IN].status = conn->plan[IO_OUT].status = IO_CLOSING;
414 conn->plan[IO_IN].u1.s = errno;
415 backend_new_closing(conn);
417 return &conn->plan[IO_IN];
420 struct io_plan *io_close_cb(struct io_conn *conn, void *arg)
422 return io_close(conn);
425 /* Exit the loop, returning this (non-NULL) arg. */
426 void io_break(const void *ret)
429 io_loop_return = (void *)ret;
432 struct io_plan *io_never(struct io_conn *conn, void *unused)
434 return io_always(conn, io_never_called, NULL);
437 int io_conn_fd(const struct io_conn *conn)
442 struct io_plan *io_duplex(struct io_plan *in_plan, struct io_plan *out_plan)
444 /* in_plan must be conn->plan[IO_IN], out_plan must be [IO_OUT] */
445 assert(out_plan == in_plan + 1);