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 *io_get_plan(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];
115 static struct io_plan *set_always(struct io_conn *conn,
116 struct io_plan *plan,
117 struct io_plan *(*next)(struct io_conn *,
121 plan->status = IO_ALWAYS;
122 backend_new_always(conn);
123 return io_set_plan(conn, plan, NULL, next, arg);
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 return set_always(conn, plan, next, arg);
143 static int do_write(int fd, struct io_plan *plan)
145 ssize_t ret = write(fd, plan->u1.cp, plan->u2.s);
151 return plan->u2.s == 0;
154 /* Queue some data to be written. */
155 struct io_plan *io_write_(struct io_conn *conn, const void *data, size_t len,
156 struct io_plan *(*next)(struct io_conn *, void *),
159 struct io_plan *plan = io_get_plan(conn, IO_OUT);
162 return set_always(conn, plan, next, arg);
164 plan->u1.const_vp = data;
167 return io_set_plan(conn, plan, do_write, next, arg);
170 static int do_read(int fd, struct io_plan *plan)
172 ssize_t ret = read(fd, plan->u1.cp, plan->u2.s);
178 return plan->u2.s == 0;
181 /* Queue a request to read into a buffer. */
182 struct io_plan *io_read_(struct io_conn *conn,
183 void *data, size_t len,
184 struct io_plan *(*next)(struct io_conn *, void *),
187 struct io_plan *plan = io_get_plan(conn, IO_IN);
192 return set_always(conn, plan, next, arg);
197 return io_set_plan(conn, plan, do_read, next, arg);
200 static int do_read_partial(int fd, struct io_plan *plan)
202 ssize_t ret = read(fd, plan->u1.cp, *(size_t *)plan->u2.vp);
206 *(size_t *)plan->u2.vp = ret;
210 /* Queue a partial request to read into a buffer. */
211 struct io_plan *io_read_partial_(struct io_conn *conn,
212 void *data, size_t maxlen, size_t *len,
213 struct io_plan *(*next)(struct io_conn *,
217 struct io_plan *plan = io_get_plan(conn, IO_IN);
220 return set_always(conn, plan, next, arg);
223 /* We store the max len in here temporarily. */
227 return io_set_plan(conn, plan, do_read_partial, next, arg);
230 static int do_write_partial(int fd, struct io_plan *plan)
232 ssize_t ret = write(fd, plan->u1.cp, *(size_t *)plan->u2.vp);
236 *(size_t *)plan->u2.vp = ret;
240 /* Queue a partial write request. */
241 struct io_plan *io_write_partial_(struct io_conn *conn,
242 const void *data, size_t maxlen, size_t *len,
243 struct io_plan *(*next)(struct io_conn *,
247 struct io_plan *plan = io_get_plan(conn, IO_OUT);
250 return set_always(conn, plan, next, arg);
252 plan->u1.const_vp = data;
253 /* We store the max len in here temporarily. */
257 return io_set_plan(conn, plan, do_write_partial, next, arg);
260 static int do_connect(int fd, struct io_plan *plan)
263 socklen_t len = sizeof(err);
265 /* Has async connect finished? */
266 ret = getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &len);
271 /* Restore blocking if it was initially. */
272 fcntl(fd, F_SETFL, plan->u1.s);
274 } else if (err == EINPROGRESS)
281 struct io_plan *io_connect_(struct io_conn *conn, const struct addrinfo *addr,
282 struct io_plan *(*next)(struct io_conn *, void *),
285 struct io_plan *plan = io_get_plan(conn, IO_IN);
286 int fd = io_conn_fd(conn);
290 /* Save old flags, set nonblock if not already. */
291 plan->u1.s = fcntl(fd, F_GETFL);
292 fcntl(fd, F_SETFL, plan->u1.s | O_NONBLOCK);
294 /* Immediate connect can happen. */
295 if (connect(fd, addr->ai_addr, addr->ai_addrlen) == 0)
296 return set_always(conn, plan, next, arg);
298 if (errno != EINPROGRESS)
299 return io_close(conn);
301 return io_set_plan(conn, plan, do_connect, next, arg);
304 struct io_plan *io_wait_(struct io_conn *conn,
306 struct io_plan *(*next)(struct io_conn *, void *),
309 struct io_plan *plan;
311 /* If we're duplex, we want this on the current plan. Otherwise,
313 if (conn->plan[IO_IN].status == IO_UNSET)
314 plan = io_get_plan(conn, IO_IN);
316 plan = io_get_plan(conn, IO_OUT);
318 plan->status = IO_WAITING;
319 plan->u1.const_vp = wait;
321 return io_set_plan(conn, plan, NULL, next, arg);
324 void io_wake(const void *wait)
329 static int do_plan(struct io_conn *conn, struct io_plan *plan)
331 /* Someone else might have called io_close() on us. */
332 if (plan->status == IO_CLOSING)
335 /* We shouldn't have polled for this event if this wasn't true! */
336 assert(plan->status == IO_POLLING);
338 switch (plan->io(conn->fd.fd, plan)) {
345 next_plan(conn, plan);
348 /* IO should only return -1, 0 or 1 */
353 void io_ready(struct io_conn *conn, int pollflags)
355 if (pollflags & POLLIN)
356 do_plan(conn, &conn->plan[IO_IN]);
358 if (pollflags & POLLOUT)
359 do_plan(conn, &conn->plan[IO_OUT]);
362 void io_do_always(struct io_conn *conn)
364 if (conn->plan[IO_IN].status == IO_ALWAYS)
365 next_plan(conn, &conn->plan[IO_IN]);
367 if (conn->plan[IO_OUT].status == IO_ALWAYS)
368 next_plan(conn, &conn->plan[IO_OUT]);
371 void io_do_wakeup(struct io_conn *conn, struct io_plan *plan)
373 assert(plan->status == IO_WAITING);
374 next_plan(conn, plan);
377 /* Close the connection, we're done. */
378 struct io_plan *io_close(struct io_conn *conn)
380 /* Already closing? Don't close twice. */
381 if (conn->plan[IO_IN].status == IO_CLOSING)
382 return &conn->plan[IO_IN];
384 conn->plan[IO_IN].status = conn->plan[IO_OUT].status = IO_CLOSING;
385 conn->plan[IO_IN].u1.s = errno;
386 backend_new_closing(conn);
388 return io_set_plan(conn, &conn->plan[IO_IN], NULL, NULL, NULL);
391 struct io_plan *io_close_cb(struct io_conn *conn, void *arg)
393 return io_close(conn);
396 /* Exit the loop, returning this (non-NULL) arg. */
397 void io_break(const void *ret)
400 io_loop_return = (void *)ret;
403 struct io_plan *io_never(struct io_conn *conn, void *unused)
405 return io_always(conn, io_never_called, NULL);
408 int io_conn_fd(const struct io_conn *conn)
413 void io_duplex_prepare(struct io_conn *conn)
415 assert(conn->plan[IO_IN].status == IO_UNSET);
416 assert(conn->plan[IO_OUT].status == IO_UNSET);
418 conn->debug_saved = conn->debug;
422 struct io_plan *io_duplex_(struct io_plan *in_plan, struct io_plan *out_plan)
424 struct io_conn *conn;
426 /* in_plan must be conn->plan[IO_IN], out_plan must be [IO_OUT] */
427 assert(out_plan == in_plan + 1);
430 conn = container_of(in_plan, struct io_conn, plan[IO_IN]);
431 conn->debug = conn->debug_saved;
433 /* Now set the plans again, to invoke sync debug. */
435 out_plan, out_plan->io, out_plan->next, out_plan->next_arg);
437 in_plan, in_plan->io, in_plan->next, in_plan->next_arg);
442 struct io_plan *io_set_plan(struct io_conn *conn, struct io_plan *plan,
443 int (*io)(int fd, struct io_plan *plan),
444 struct io_plan *(*next)(struct io_conn *, void *),
447 struct io_plan *other;
451 plan->next_arg = next_arg;
452 assert(plan->status == IO_CLOSING || next != NULL);
457 if (io_loop_return) {
458 io_debug_complete(conn);
462 switch (plan->status) {
464 while (do_plan(conn, plan) == 0);
466 /* Shouldn't happen, since you said you did plan! */
470 /* If other one is ALWAYS, leave in list! */
471 if (plan == &conn->plan[IO_IN])
472 other = &conn->plan[IO_OUT];
474 other = &conn->plan[IO_IN];
475 if (other->status != IO_ALWAYS)
476 remove_from_always(conn);
477 next_plan(conn, plan);
481 io_debug_complete(conn);
487 void io_set_debug(struct io_conn *conn, bool debug)
492 void io_debug_complete(struct io_conn *conn)