1 /* Licensed under LGPLv2.1+ - see LICENSE file for details */
4 #include <ccan/tal/tal.h>
5 #include <ccan/typesafe_cb/typesafe_cb.h>
15 * struct io_plan - a plan for input or output.
17 * Each io_conn has zero to two of these active at any time.
22 * struct io_conn - a connection associated with an fd.
27 * io_new_conn - create a new connection.
28 * @ctx: the context to tal from (or NULL)
29 * @fd: the file descriptor.
30 * @init: the function to call for a new connection
31 * @arg: the argument to @init.
33 * This creates a connection which owns @fd, it then calls
34 * @init to initialize the connection, which sets up an io_plan.
36 * Returns NULL on error (and sets errno).
39 * // Dumb init function to print string and tell conn to close.
40 * static struct io_plan *conn_init(struct io_conn *conn, const char *msg)
42 * printf("Created conn %p: %s", conn, msg);
43 * return io_close(conn);
46 * static void create_self_closing_pipe(void)
49 * struct io_conn *conn;
53 * conn = io_new_conn(NULL, fd[0], conn_init, (const char *)"hi!");
58 #define io_new_conn(ctx, fd, init, arg) \
59 io_new_conn_((ctx), (fd), \
60 typesafe_cb_preargs(struct io_plan *, void *, \
62 struct io_conn *conn), \
65 struct io_conn *io_new_conn_(const tal_t *ctx, int fd,
66 struct io_plan *(*init)(struct io_conn *, void *),
70 * io_set_finish - set finish function on a connection.
71 * @conn: the connection.
72 * @finish: the function to call when it's closed or fails.
73 * @arg: the argument to @finish.
75 * @finish will be called when an I/O operation fails, or you call
76 * io_close() on the connection. errno will be set to the value
77 * after the failed I/O, or at the call to io_close(). The fd
78 * will be closed before @finish is called.
81 * static void finish(struct io_conn *conn, const char *msg)
83 * // errno is not 0 after success, so this is a bit useless.
84 * printf("Conn %p closed with errno %i (%s)\n", conn, errno, msg);
87 * // Dumb init function to print string and tell conn to close.
88 * static struct io_plan *conn_init(struct io_conn *conn, const char *msg)
90 * io_set_finish(conn, finish, msg);
91 * return io_close(conn);
94 #define io_set_finish(conn, finish, arg) \
95 io_set_finish_((conn), \
96 typesafe_cb_preargs(void, void *, \
100 void io_set_finish_(struct io_conn *conn,
101 void (*finish)(struct io_conn *, void *),
106 * io_new_listener - create a new accepting listener.
107 * @ctx: the context to tal from (or NULL)
108 * @fd: the file descriptor.
109 * @init: the function to call for a new connection
110 * @arg: the argument to @init.
112 * When @fd becomes readable, we accept(), create a new connection,
113 * (tal'ocated off @ctx) and pass that to init(). Note that if there is
114 * an error on this file descriptor, it will be freed.
116 * Returns NULL on error (and sets errno).
119 * #include <sys/types.h>
120 * #include <sys/socket.h>
125 * // Set up a listening socket, return it.
126 * static struct io_listener *do_listen(const char *port)
128 * struct addrinfo *addrinfo, hints;
131 * memset(&hints, 0, sizeof(hints));
132 * hints.ai_family = AF_UNSPEC;
133 * hints.ai_socktype = SOCK_STREAM;
134 * hints.ai_flags = AI_PASSIVE;
135 * hints.ai_protocol = 0;
137 * if (getaddrinfo(NULL, port, &hints, &addrinfo) != 0)
140 * fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
141 * addrinfo->ai_protocol);
145 * freeaddrinfo(addrinfo);
146 * setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
147 * if (bind(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0) {
151 * if (listen(fd, 1) != 0) {
155 * return io_new_listener(NULL, fd, conn_init, (const char *)"listened!");
158 #define io_new_listener(ctx, fd, init, arg) \
159 io_new_listener_((ctx), (fd), \
160 typesafe_cb_preargs(struct io_plan *, void *, \
162 struct io_conn *conn), \
164 struct io_listener *io_new_listener_(const tal_t *ctx, int fd,
165 struct io_plan *(*init)(struct io_conn *,
170 * io_close_listener - delete a listener.
171 * @listener: the listener returned from io_new_listener.
173 * This closes the fd and frees @listener.
177 * struct io_listener *l = do_listen("8111");
179 * io_loop(NULL, NULL);
180 * io_close_listener(l);
183 void io_close_listener(struct io_listener *listener);
186 * io_write - output plan to write data.
187 * @conn: the connection that plan is for.
188 * @data: the data buffer.
189 * @len: the length to write.
190 * @next: function to call output is done.
191 * @arg: @next argument
193 * This updates the output plan, to write out a data buffer. Once it's all
194 * written, the @next function will be called: on an error, the finish
195 * function is called instead.
197 * Note that the I/O may actually be done immediately.
200 * static struct io_plan *write_to_conn(struct io_conn *conn, const char *msg)
202 * // Write message, then close.
203 * return io_write(conn, msg, strlen(msg), io_close_cb, NULL);
206 #define io_write(conn, data, len, next, arg) \
207 io_write_((conn), (data), (len), \
208 typesafe_cb_preargs(struct io_plan *, void *, \
209 (next), (arg), struct io_conn *), \
211 struct io_plan *io_write_(struct io_conn *conn,
212 const void *data, size_t len,
213 struct io_plan *(*next)(struct io_conn *, void *),
217 * io_read - input plan to read data.
218 * @conn: the connection that plan is for.
219 * @data: the data buffer.
220 * @len: the length to read.
221 * @next: function to call once input is done.
222 * @arg: @next argument
224 * This creates a plan to read data into a buffer. Once it's all
225 * read, the @next function will be called: on an error, the finish
226 * function is called instead. If read() returns 0 (EOF) errno is set
229 * Note that the I/O may actually be done immediately.
232 * static struct io_plan *read_from_conn(struct io_conn *conn, char *buf)
234 * // Read message, then close.
235 * return io_read(conn, buf, 12, io_close_cb, NULL);
238 #define io_read(conn, data, len, next, arg) \
239 io_read_((conn), (data), (len), \
240 typesafe_cb_preargs(struct io_plan *, void *, \
241 (next), (arg), struct io_conn *), \
243 struct io_plan *io_read_(struct io_conn *conn,
244 void *data, size_t len,
245 struct io_plan *(*next)(struct io_conn *, void *),
250 * io_read_partial - input plan to read some data.
251 * @conn: the connection that plan is for.
252 * @data: the data buffer.
253 * @maxlen: the maximum length to read
254 * @lenp: set to the length actually read.
255 * @next: function to call once input is done.
256 * @arg: @next argument
258 * This creates a plan to read data into a buffer. Once any data is
259 * read, @len is updated and the @next function will be called: on an
260 * error, the finish function is called instead. If read() returns 0 (EOF)
263 * Note that the I/O may actually be done immediately.
271 * static struct io_plan *dump(struct io_conn *conn, struct buf *b)
273 * printf("Partial read: '%*s'\n", (int)b->len, b->buf);
275 * return io_close(conn);
278 * static struct io_plan *read_part(struct io_conn *conn, struct buf *b)
280 * // Read message, then dump and close.
281 * return io_read_partial(conn, b->buf, sizeof(b->buf), &b->len, dump, b);
284 #define io_read_partial(conn, data, maxlen, lenp, next, arg) \
285 io_read_partial_((conn), (data), (maxlen), (lenp), \
286 typesafe_cb_preargs(struct io_plan *, void *, \
290 struct io_plan *io_read_partial_(struct io_conn *conn,
291 void *data, size_t maxlen, size_t *lenp,
292 struct io_plan *(*next)(struct io_conn *,
297 * io_write_partial - output plan to write some data.
298 * @conn: the connection that plan is for.
299 * @data: the data buffer.
300 * @maxlen: the maximum length to write
301 * @lenp: set to the length actually written.
302 * @next: function to call once output is done.
303 * @arg: @next argument
305 * This creates a plan to write data from a buffer. Once any data is
306 * written, @len is updated and the @next function will be called: on an
307 * error, the finish function is called instead.
309 * Note that the I/O may actually be done immediately.
317 * static struct io_plan *show_partial(struct io_conn *conn, struct buf *b)
319 * printf("Only wrote: '%*s'\n", (int)b->len, b->buf);
321 * return io_close(conn);
324 * static struct io_plan *write_part(struct io_conn *conn, struct buf *b)
326 * // Write message, then dump and close.
327 * strcpy(b->buf, "Hello world");
328 * return io_write_partial(conn, b->buf, strlen(b->buf),
329 * &b->len, show_partial, b);
332 #define io_write_partial(conn, data, maxlen, lenp, next, arg) \
333 io_write_partial_((conn), (data), (maxlen), (lenp), \
334 typesafe_cb_preargs(struct io_plan *, void *, \
338 struct io_plan *io_write_partial_(struct io_conn *conn,
339 const void *data, size_t maxlen, size_t *lenp,
340 struct io_plan *(*next)(struct io_conn *,
345 * io_always - plan to immediately call next callback
346 * @conn: the connection that plan is for.
347 * @next: function to call.
348 * @arg: @next argument
350 * Sometimes it's neater to plan a callback rather than call it directly;
351 * for example, if you only need to read data for one path and not another.
354 * static struct io_plan *init_conn_with_nothing(struct io_conn *conn,
357 * // Silly example: close on next time around loop.
358 * return io_always(conn, io_close_cb, NULL);
361 #define io_always(conn, next, arg) \
362 io_always_((conn), typesafe_cb_preargs(struct io_plan *, void *, \
367 struct io_plan *io_always_(struct io_conn *conn,
368 struct io_plan *(*next)(struct io_conn *, void *),
372 * io_out_always - output plan to immediately call next callback
373 * @conn: the connection that plan is for.
374 * @next: function to call.
375 * @arg: @next argument
377 * This is a variant of io_always() which uses the output plan; it only
378 * matters if you are using io_duplex, and thus have two plans running at
381 #define io_out_always(conn, next, arg) \
382 io_out_always_((conn), typesafe_cb_preargs(struct io_plan *, void *, \
387 struct io_plan *io_out_always_(struct io_conn *conn,
388 struct io_plan *(*next)(struct io_conn *,
393 * io_sock_shutdown - start socket close process (flushes TCP sockets).
394 * @conn: the connection the plan is for
396 * Simply closing a TCP socket can lose data; unfortunately you should
397 * shutdown(SHUT_WR) and wait for the other side to see this and close.
398 * Of course, you also need to set a timer, in case it doesn't (you may
399 * already have some responsiveness timer, of course).
401 * On error, is equivalent to io_close().
404 * #include <ccan/timer/timer.h>
406 * // Timer infra needs wrapper to contain extra data.
407 * struct timeout_timer {
409 * struct io_conn *conn;
411 * static struct timers timers;
413 * static struct io_plan *flush_and_close(struct io_conn *conn)
415 * struct timeout_timer *timeout;
416 * // Freed if conn closes normally.
417 * timeout = tal(conn, struct timeout_timer);
418 * timeout->conn = conn;
420 * timer_addrel(&timers, &timeout->t, time_from_sec(5));
421 * return io_sock_shutdown(conn);
424 struct io_plan *io_sock_shutdown(struct io_conn *conn);
427 * io_connect - create an asynchronous connection to a listening socket.
428 * @conn: the connection that plan is for.
429 * @addr: where to connect.
430 * @init: function to call once it's connected
431 * @arg: @init argument
433 * This initiates a connection, and creates a plan for
434 * (asynchronously) completing it. Once complete, the @init function
438 * #include <sys/types.h>
439 * #include <sys/socket.h>
442 * // Write, then close socket.
443 * static struct io_plan *init_connect(struct io_conn *conn,
444 * struct addrinfo *addrinfo)
446 * return io_connect(conn, addrinfo, io_close_cb, NULL);
452 * struct addrinfo *addrinfo;
454 * fd = socket(AF_INET, SOCK_STREAM, 0);
455 * getaddrinfo("localhost", "8111", NULL, &addrinfo);
456 * io_new_conn(NULL, fd, init_connect, addrinfo);
459 #define io_connect(conn, addr, next, arg) \
460 io_connect_((conn), (addr), \
461 typesafe_cb_preargs(struct io_plan *, void *, \
466 struct io_plan *io_connect_(struct io_conn *conn, const struct addrinfo *addr,
467 struct io_plan *(*next)(struct io_conn *, void *),
471 * io_duplex - set plans for both input and output.
472 * @conn: the connection that plan is for.
473 * @in: the input plan
474 * @out: the output plan
476 * Most plans are either for input or output; io_duplex creates a plan
477 * which does both. This is often used in the init function to create
478 * two independent streams, though it can be used once on any connection.
480 * Note that if either plan closes the connection, it will be closed.
488 * static struct io_plan *read_and_write(struct io_conn *conn, struct buf *b)
490 * return io_duplex(conn,
491 * io_read(conn, b->in, sizeof(b->in), io_close_cb, b),
492 * io_write(conn, b->out, sizeof(b->out), io_close_cb,b));
495 struct io_plan *io_duplex(struct io_conn *conn,
496 struct io_plan *in_plan, struct io_plan *out_plan);
499 * io_halfclose - close half of an io_duplex connection.
500 * @conn: the connection that plan is for.
502 * It's common to want to close a duplex connection after both input and
503 * output plans have completed. If either calls io_close() the connection
504 * closes immediately. Instead, io_halfclose() needs to be called twice.
512 * static struct io_plan *finish(struct io_conn *conn, struct buf *b)
514 * return io_halfclose(conn);
517 * static struct io_plan *read_and_write(struct io_conn *conn, struct buf *b)
519 * return io_duplex(conn,
520 * io_read(conn, b->in, sizeof(b->in), finish, b),
521 * io_write(conn, b->out, sizeof(b->out), finish, b));
524 struct io_plan *io_halfclose(struct io_conn *conn);
527 * io_wait - leave a plan idle until something wakes us.
528 * @conn: the connection that plan is for.
529 * @waitaddr: the address to wait on.
530 * @next: function to call after waiting.
531 * @arg: @next argument
533 * This leaves the input or output idle: io_wake(@waitaddr) will be
534 * called later to restart the connection.
537 * // Silly example to wait then close.
538 * static struct io_plan *wait(struct io_conn *conn, void *b)
540 * return io_wait(conn, b, io_close_cb, NULL);
543 #define io_wait(conn, waitaddr, next, arg) \
544 io_wait_((conn), (waitaddr), \
545 typesafe_cb_preargs(struct io_plan *, void *, \
550 struct io_plan *io_wait_(struct io_conn *conn,
552 struct io_plan *(*next)(struct io_conn *, void *),
557 * io_out_wait - leave the output plan idle until something wakes us.
558 * @conn: the connection that plan is for.
559 * @waitaddr: the address to wait on.
560 * @next: function to call after waiting.
561 * @arg: @next argument
563 * io_wait() makes the input plan idle: if you're not using io_duplex it
564 * doesn't matter which plan is waiting. Otherwise, you may need to use
565 * io_out_wait() instead, to specify explicitly that the output plan is
568 #define io_out_wait(conn, waitaddr, next, arg) \
569 io_out_wait_((conn), (waitaddr), \
570 typesafe_cb_preargs(struct io_plan *, void *, \
575 struct io_plan *io_out_wait_(struct io_conn *conn,
577 struct io_plan *(*next)(struct io_conn *, void *),
581 * io_wake - wake up any connections waiting on @wait
582 * @waitaddr: the address to trigger.
584 * All io_conns who have returned io_wait() on @waitaddr will move on
585 * to their next callback.
588 * static struct io_plan *wake_it(struct io_conn *conn, void *b)
591 * return io_close(conn);
594 void io_wake(const void *wait);
597 * io_break - return from io_loop()
598 * @ret: non-NULL value to return from io_loop().
600 * This breaks out of the io_loop. As soon as the current function
601 * returns, any io_close()'d connections will have their finish
602 * callbacks called, then io_loop() with return with @ret.
604 * If io_loop() is called again, then @plan will be carried out.
607 * static struct io_plan *fail_on_timeout(struct io_conn *conn, char *msg)
610 * return io_close(conn);
613 void io_break(const void *ret);
616 * io_never - assert if callback is called.
617 * @conn: the connection that plan is for.
618 * @unused: an unused parameter to make this suitable for use as a callback.
620 * Sometimes you want to make it clear that a callback should never happen
621 * (eg. for io_break). This will assert() if called.
624 * static struct io_plan *break_out(struct io_conn *conn, void *unused)
627 * // We won't ever return from io_break
628 * return io_never(conn, NULL);
631 struct io_plan *io_never(struct io_conn *conn, void *unused);
633 /* FIXME: io_recvfrom/io_sendto */
636 * io_close - close a connection.
637 * @conn: the connection to close.
639 * The connection is immediately freed: it doesn't have to be the
640 * current connection and it doesn't need to be idle. No more IO or
641 * callbacks will occur, but if a function was added by io_set_finish()
642 * it will be called with the current errno preserved.
644 * This is equivalent to tal_free(io_conn), except it returns an io_plan
645 * for use in an io callback.
648 * static struct io_plan *close_on_timeout(struct io_conn *conn, const char *msg)
650 * printf("closing: %s\n", msg);
651 * return io_close(conn);
654 struct io_plan *io_close(struct io_conn *conn);
657 * io_close_cb - helper callback to close a connection.
658 * @conn: the connection.
660 * This is closes a connection; designed to be used as a callback
664 * #define close_on_timeout io_close_cb
666 struct io_plan *io_close_cb(struct io_conn *, void *unused);
669 * io_close_taken_fd - close a connection, but remove the filedescriptor first.
670 * @conn: the connection to take the file descriptor from and close.
672 * io_close closes the file descriptor underlying the io_conn; this version does
673 * not. Presumably you have used io_conn_fd() on it beforehand and will take
674 * care of the fd yourself.
676 * Note that this also turns off O_NONBLOCK on the fd.
679 * static struct io_plan *steal_fd(struct io_conn *conn, int *fd)
681 * *fd = io_conn_fd(conn);
682 * printf("stealing fd %i and closing\n", *fd);
683 * return io_close_taken_fd(conn);
686 struct io_plan *io_close_taken_fd(struct io_conn *conn);
689 * io_loop - process fds until all closed on io_break.
690 * @timers - timers which are waiting to go off (or NULL for none)
691 * @expired - an expired timer (can be NULL if @timers is)
693 * This is the core loop; it exits with the io_break() arg, or NULL if
694 * all connections and listeners are closed, or with @expired set to an
695 * expired timer (if @timers isn't NULL).
698 * io_loop(NULL, NULL);
700 void *io_loop(struct timers *timers, struct timer **expired);
703 * io_conn_fd - get the fd from a connection.
704 * @conn: the connection.
706 * Sometimes useful, eg for getsockname(). Note that the fd is O_NONBLOCK.
711 int io_conn_fd(const struct io_conn *conn);
714 * io_plan_in_started - is this conn doing input I/O now?
717 * This returns true if input I/O has been performed on the conn but
718 * @next hasn't been called yet. For example, io_read() may have done
721 * This can be useful if we want to terminate a connection only after
722 * reading a whole packet: if this returns true, we would wait until
725 bool io_plan_in_started(const struct io_conn *conn);
728 * io_plan_out_started - is this conn doing output I/O now?
731 * This returns true if output I/O has been performed on the conn but
732 * @next hasn't been called yet. For example, io_write() may have done
735 * This can be useful if we want to terminate a connection only after
736 * writing a whole packet: if this returns true, we would wait until
739 bool io_plan_out_started(const struct io_conn *conn);
742 * io_flush_sync - (synchronously) complete any outstanding output.
743 * @conn: the connection.
745 * This is generally used as an emergency escape, for example when we
746 * want to write an error message on a socket before terminating, but it may
747 * be in the middle of existing I/O. We don't want to service any other
750 * This returns true if all pending output is complete, false on error.
751 * The next callback is not called on the conn, but will be as soon as
752 * io_loop() is called.
757 bool io_flush_sync(struct io_conn *conn);
760 * io_conn_exclusive - set/unset an io_conn to exclusively serviced
761 * @conn: the connection
762 * @exclusive: whether to be exclusive or not
764 * If any io_conn is set exclusive, then no non-exclusive io_conn (or
765 * io_listener) will be serviced by io_loop(). If it's a io_duplex io_conn(),
766 * then io_conn_exclusive() makes the read-side exclusive; io_conn_out_exclusive()
767 * makes the write-side exclusive.
769 * This allows you to temporarily service only one (or several) fds.
770 * For example, you might want to flush out one io_conn and not
771 * receive any new connections or read any other input.
773 * Returns true if any exclusive io_conn remain, otherwise false.
774 * (This is useful for checking your own logic: dangling exclusive io_conn
777 bool io_conn_exclusive(struct io_conn *conn, bool exclusive);
780 * io_conn_out_exclusive - set/unset exclusive on the write-side of a duplex
781 * @conn: the connection, post io_duplex
782 * @exclusive: whether to be exclusive or not
784 * See io_conn_exclusive() above.
786 bool io_conn_out_exclusive(struct io_conn *conn, bool exclusive);
789 * io_fd_block - helper to set an fd blocking/nonblocking.
790 * @fd: the file descriptor
791 * @block: true to set blocking, false to set non-blocking.
793 * Generally only fails is @fd isn't a valid file descriptor, otherwise
796 bool io_fd_block(int fd, bool block);
799 * io_time_override - override the normal call for time.
800 * @nowfn: the function to call.
802 * io usually uses time_mono() internally, but this forces it
803 * to use your function (eg. for debugging). Returns the old
806 struct timemono (*io_time_override(struct timemono (*now)(void)))(void);
809 * io_poll_override - override the normal call for poll.
810 * @pollfn: the function to call.
812 * io usually uses poll() internally, but this forces it to use your
813 * function (eg. for debugging, suppressing fds, or polling on others unknown
814 * to ccan/io). Returns the old one.
816 int (*io_poll_override(int (*poll)(struct pollfd *fds, nfds_t nfds, int timeout)))(struct pollfd *, nfds_t, int);
818 #endif /* CCAN_IO_H */