1 /* Licensed under LGPLv2.1+ - see LICENSE file for details */
4 #include <ccan/tal/tal.h>
5 #include <ccan/typesafe_cb/typesafe_cb.h>
14 * struct io_plan - a plan for input or output.
16 * Each io_conn has zero to two of these active at any time.
21 * struct io_conn - a connection associated with an fd.
26 * io_new_conn - create a new connection.
27 * @ctx: the context to tal from (or NULL)
28 * @fd: the file descriptor.
29 * @init: the function to call for a new connection
30 * @arg: the argument to @init.
32 * This creates a connection which owns @fd, it then calls
33 * @init to initialize the connection, which sets up an io_plan.
35 * Returns NULL on error (and sets errno).
38 * // Dumb init function to print string and tell conn to close.
39 * static struct io_plan *conn_init(struct io_conn *conn, const char *msg)
41 * printf("Created conn %p: %s", conn, msg);
42 * return io_close(conn);
45 * static void create_self_closing_pipe(void)
48 * struct io_conn *conn;
52 * conn = io_new_conn(NULL, fd[0], conn_init, (const char *)"hi!");
57 #define io_new_conn(ctx, fd, init, arg) \
58 io_new_conn_((ctx), (fd), \
59 typesafe_cb_preargs(struct io_plan *, void *, \
61 struct io_conn *conn), \
64 struct io_conn *io_new_conn_(const tal_t *ctx, int fd,
65 struct io_plan *(*init)(struct io_conn *, void *),
69 * io_set_finish - set finish function on a connection.
70 * @conn: the connection.
71 * @finish: the function to call when it's closed or fails.
72 * @arg: the argument to @finish.
74 * @finish will be called when an I/O operation fails, or you call
75 * io_close() on the connection. errno will be set to the value
76 * after the failed I/O, or at the call to io_close(). The fd
77 * will be closed before @finish is called.
80 * static void finish(struct io_conn *conn, const char *msg)
82 * // errno is not 0 after success, so this is a bit useless.
83 * printf("Conn %p closed with errno %i (%s)\n", conn, errno, msg);
86 * // Dumb init function to print string and tell conn to close.
87 * static struct io_plan *conn_init(struct io_conn *conn, const char *msg)
89 * io_set_finish(conn, finish, msg);
90 * return io_close(conn);
93 #define io_set_finish(conn, finish, arg) \
94 io_set_finish_((conn), \
95 typesafe_cb_preargs(void, void *, \
99 void io_set_finish_(struct io_conn *conn,
100 void (*finish)(struct io_conn *, void *),
105 * io_new_listener - create a new accepting listener.
106 * @ctx: the context to tal from (or NULL)
107 * @fd: the file descriptor.
108 * @init: the function to call for a new connection
109 * @arg: the argument to @init.
111 * When @fd becomes readable, we accept(), create a new connection,
112 * (tal'ocated off @ctx) and pass that to init().
114 * Returns NULL on error (and sets errno).
117 * #include <sys/types.h>
118 * #include <sys/socket.h>
123 * // Set up a listening socket, return it.
124 * static struct io_listener *do_listen(const char *port)
126 * struct addrinfo *addrinfo, hints;
129 * memset(&hints, 0, sizeof(hints));
130 * hints.ai_family = AF_UNSPEC;
131 * hints.ai_socktype = SOCK_STREAM;
132 * hints.ai_flags = AI_PASSIVE;
133 * hints.ai_protocol = 0;
135 * if (getaddrinfo(NULL, port, &hints, &addrinfo) != 0)
138 * fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
139 * addrinfo->ai_protocol);
143 * freeaddrinfo(addrinfo);
144 * setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
145 * if (bind(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0) {
149 * if (listen(fd, 1) != 0) {
153 * return io_new_listener(NULL, fd, conn_init, (const char *)"listened!");
156 #define io_new_listener(ctx, fd, init, arg) \
157 io_new_listener_((ctx), (fd), \
158 typesafe_cb_preargs(struct io_plan *, void *, \
160 struct io_conn *conn), \
162 struct io_listener *io_new_listener_(const tal_t *ctx, int fd,
163 struct io_plan *(*init)(struct io_conn *,
168 * io_close_listener - delete a listener.
169 * @listener: the listener returned from io_new_listener.
171 * This closes the fd and frees @listener.
175 * struct io_listener *l = do_listen("8111");
177 * io_loop(NULL, NULL);
178 * io_close_listener(l);
181 void io_close_listener(struct io_listener *listener);
184 * io_write - output plan to write data.
185 * @conn: the connection that plan is for.
186 * @data: the data buffer.
187 * @len: the length to write.
188 * @next: function to call output is done.
189 * @arg: @next argument
191 * This updates the output plan, to write out a data buffer. Once it's all
192 * written, the @next function will be called: on an error, the finish
193 * function is called instead.
195 * Note that the I/O may actually be done immediately.
198 * static struct io_plan *write_to_conn(struct io_conn *conn, const char *msg)
200 * // Write message, then close.
201 * return io_write(conn, msg, strlen(msg), io_close_cb, NULL);
204 #define io_write(conn, data, len, next, arg) \
205 io_write_((conn), (data), (len), \
206 typesafe_cb_preargs(struct io_plan *, void *, \
207 (next), (arg), struct io_conn *), \
209 struct io_plan *io_write_(struct io_conn *conn,
210 const void *data, size_t len,
211 struct io_plan *(*next)(struct io_conn *, void *),
215 * io_read - input plan to read data.
216 * @conn: the connection that plan is for.
217 * @data: the data buffer.
218 * @len: the length to read.
219 * @next: function to call once input is done.
220 * @arg: @next argument
222 * This creates a plan to read data into a buffer. Once it's all
223 * read, the @next function will be called: on an error, the finish
224 * function is called instead.
226 * Note that the I/O may actually be done immediately.
229 * static struct io_plan *read_from_conn(struct io_conn *conn, char *buf)
231 * // Read message, then close.
232 * return io_read(conn, buf, 12, io_close_cb, NULL);
235 #define io_read(conn, data, len, next, arg) \
236 io_read_((conn), (data), (len), \
237 typesafe_cb_preargs(struct io_plan *, void *, \
238 (next), (arg), struct io_conn *), \
240 struct io_plan *io_read_(struct io_conn *conn,
241 void *data, size_t len,
242 struct io_plan *(*next)(struct io_conn *, void *),
247 * io_read_partial - input plan to read some data.
248 * @conn: the connection that plan is for.
249 * @data: the data buffer.
250 * @maxlen: the maximum length to read
251 * @lenp: set to the length actually read.
252 * @next: function to call once input is done.
253 * @arg: @next argument
255 * This creates a plan to read data into a buffer. Once any data is
256 * read, @len is updated and the @next function will be called: on an
257 * error, the finish function is called instead.
259 * Note that the I/O may actually be done immediately.
267 * static struct io_plan *dump(struct io_conn *conn, struct buf *b)
269 * printf("Partial read: '%*s'\n", (int)b->len, b->buf);
271 * return io_close(conn);
274 * static struct io_plan *read_part(struct io_conn *conn, struct buf *b)
276 * // Read message, then dump and close.
277 * return io_read_partial(conn, b->buf, sizeof(b->buf), &b->len, dump, b);
280 #define io_read_partial(conn, data, maxlen, lenp, next, arg) \
281 io_read_partial_((conn), (data), (maxlen), (lenp), \
282 typesafe_cb_preargs(struct io_plan *, void *, \
286 struct io_plan *io_read_partial_(struct io_conn *conn,
287 void *data, size_t maxlen, size_t *lenp,
288 struct io_plan *(*next)(struct io_conn *,
293 * io_write_partial - output plan to write some data.
294 * @conn: the connection that plan is for.
295 * @data: the data buffer.
296 * @maxlen: the maximum length to write
297 * @lenp: set to the length actually written.
298 * @next: function to call once output is done.
299 * @arg: @next argument
301 * This creates a plan to write data from a buffer. Once any data is
302 * written, @len is updated and the @next function will be called: on an
303 * error, the finish function is called instead.
305 * Note that the I/O may actually be done immediately.
313 * static struct io_plan *show_partial(struct io_conn *conn, struct buf *b)
315 * printf("Only wrote: '%*s'\n", (int)b->len, b->buf);
317 * return io_close(conn);
320 * static struct io_plan *write_part(struct io_conn *conn, struct buf *b)
322 * // Write message, then dump and close.
323 * strcpy(b->buf, "Hello world");
324 * return io_write_partial(conn, b->buf, strlen(b->buf),
325 * &b->len, show_partial, b);
328 #define io_write_partial(conn, data, maxlen, lenp, next, arg) \
329 io_write_partial_((conn), (data), (maxlen), (lenp), \
330 typesafe_cb_preargs(struct io_plan *, void *, \
334 struct io_plan *io_write_partial_(struct io_conn *conn,
335 const void *data, size_t maxlen, size_t *lenp,
336 struct io_plan *(*next)(struct io_conn *,
341 * io_always - plan to immediately call next callback
342 * @conn: the connection that plan is for.
343 * @next: function to call.
344 * @arg: @next argument
346 * Sometimes it's neater to plan a callback rather than call it directly;
347 * for example, if you only need to read data for one path and not another.
350 * static struct io_plan *init_conn_with_nothing(struct io_conn *conn,
353 * // Silly example: close on next time around loop.
354 * return io_always(conn, io_close_cb, NULL);
357 #define io_always(conn, next, arg) \
358 io_always_((conn), typesafe_cb_preargs(struct io_plan *, void *, \
363 struct io_plan *io_always_(struct io_conn *conn,
364 struct io_plan *(*next)(struct io_conn *, void *),
368 * io_out_always - output plan to immediately call next callback
369 * @conn: the connection that plan is for.
370 * @next: function to call.
371 * @arg: @next argument
373 * This is a variant of io_always() which uses the output plan; it only
374 * matters if you are using io_duplex, and thus have two plans running at
377 #define io_out_always(conn, next, arg) \
378 io_out_always_((conn), typesafe_cb_preargs(struct io_plan *, void *, \
383 struct io_plan *io_out_always_(struct io_conn *conn,
384 struct io_plan *(*next)(struct io_conn *,
389 * io_connect - create an asynchronous connection to a listening socket.
390 * @conn: the connection that plan is for.
391 * @addr: where to connect.
392 * @init: function to call once it's connected
393 * @arg: @init argument
395 * This initiates a connection, and creates a plan for
396 * (asynchronously) completing it. Once complete, the @init function
400 * #include <sys/types.h>
401 * #include <sys/socket.h>
404 * // Write, then close socket.
405 * static struct io_plan *init_connect(struct io_conn *conn,
406 * struct addrinfo *addrinfo)
408 * return io_connect(conn, addrinfo, io_close_cb, NULL);
414 * struct addrinfo *addrinfo;
416 * fd = socket(AF_INET, SOCK_STREAM, 0);
417 * getaddrinfo("localhost", "8111", NULL, &addrinfo);
418 * io_new_conn(NULL, fd, init_connect, addrinfo);
421 #define io_connect(conn, addr, next, arg) \
422 io_connect_((conn), (addr), \
423 typesafe_cb_preargs(struct io_plan *, void *, \
428 struct io_plan *io_connect_(struct io_conn *conn, const struct addrinfo *addr,
429 struct io_plan *(*next)(struct io_conn *, void *),
433 * io_duplex - set plans for both input and output.
434 * @conn: the connection that plan is for.
435 * @in: the input plan
436 * @out: the output plan
438 * Most plans are either for input or output; io_duplex creates a plan
439 * which does both. This is often used in the init function to create
440 * two independent streams, though it can be used once on any connection.
442 * Note that if either plan closes the connection, it will be closed.
450 * static struct io_plan *read_and_write(struct io_conn *conn, struct buf *b)
452 * return io_duplex(conn,
453 * io_read(conn, b->in, sizeof(b->in), io_close_cb, b),
454 * io_write(conn, b->out, sizeof(b->out), io_close_cb,b));
457 #define io_duplex(conn, in_plan, out_plan) \
458 (io_duplex_prepare(conn), io_duplex_(in_plan, out_plan))
460 struct io_plan *io_duplex_(struct io_plan *in_plan, struct io_plan *out_plan);
461 void io_duplex_prepare(struct io_conn *conn);
464 * io_halfclose - close half of an io_duplex connection.
465 * @conn: the connection that plan is for.
467 * It's common to want to close a duplex connection after both input and
468 * output plans have completed. If either calls io_close() the connection
469 * closes immediately. Instead, io_halfclose() needs to be called twice.
477 * static struct io_plan *finish(struct io_conn *conn, struct buf *b)
479 * return io_halfclose(conn);
482 * static struct io_plan *read_and_write(struct io_conn *conn, struct buf *b)
484 * return io_duplex(conn,
485 * io_read(conn, b->in, sizeof(b->in), finish, b),
486 * io_write(conn, b->out, sizeof(b->out), finish, b));
489 struct io_plan *io_halfclose(struct io_conn *conn);
492 * io_wait - leave a plan idle until something wakes us.
493 * @conn: the connection that plan is for.
494 * @waitaddr: the address to wait on.
495 * @next: function to call after waiting.
496 * @arg: @next argument
498 * This leaves the input or output idle: io_wake(@waitaddr) will be
499 * called later to restart the connection.
502 * // Silly example to wait then close.
503 * static struct io_plan *wait(struct io_conn *conn, void *b)
505 * return io_wait(conn, b, io_close_cb, NULL);
508 #define io_wait(conn, waitaddr, next, arg) \
509 io_wait_((conn), (waitaddr), \
510 typesafe_cb_preargs(struct io_plan *, void *, \
515 struct io_plan *io_wait_(struct io_conn *conn,
517 struct io_plan *(*next)(struct io_conn *, void *),
522 * io_out_wait - leave the output plan idle until something wakes us.
523 * @conn: the connection that plan is for.
524 * @waitaddr: the address to wait on.
525 * @next: function to call after waiting.
526 * @arg: @next argument
528 * io_wait() makes the input plan idle: if you're not using io_duplex it
529 * doesn't matter which plan is waiting. Otherwise, you may need to use
530 * io_out_wait() instead, to specify explicitly that the output plan is
533 #define io_out_wait(conn, waitaddr, next, arg) \
534 io_out_wait_((conn), (waitaddr), \
535 typesafe_cb_preargs(struct io_plan *, void *, \
540 struct io_plan *io_out_wait_(struct io_conn *conn,
542 struct io_plan *(*next)(struct io_conn *, void *),
546 * io_wake - wake up any connections waiting on @wait
547 * @waitaddr: the address to trigger.
549 * All io_conns who have returned io_wait() on @waitaddr will move on
550 * to their next callback.
553 * static struct io_plan *wake_it(struct io_conn *conn, void *b)
556 * return io_close(conn);
559 void io_wake(const void *wait);
562 * io_break - return from io_loop()
563 * @ret: non-NULL value to return from io_loop().
565 * This breaks out of the io_loop. As soon as the current function
566 * returns, any io_close()'d connections will have their finish
567 * callbacks called, then io_loop() with return with @ret.
569 * If io_loop() is called again, then @plan will be carried out.
572 * static struct io_plan *fail_on_timeout(struct io_conn *conn, char *msg)
575 * return io_close(conn);
578 void io_break(const void *ret);
581 * io_never - assert if callback is called.
582 * @conn: the connection that plan is for.
583 * @unused: an unused parameter to make this suitable for use as a callback.
585 * Sometimes you want to make it clear that a callback should never happen
586 * (eg. for io_break). This will assert() if called.
589 * static struct io_plan *break_out(struct io_conn *conn, void *unused)
592 * // We won't ever return from io_break
593 * return io_never(conn, NULL);
596 struct io_plan *io_never(struct io_conn *conn, void *unused);
598 /* FIXME: io_recvfrom/io_sendto */
601 * io_close - plan to close a connection.
602 * @conn: the connection to close.
604 * On return to io_loop, the connection will be closed. It doesn't have
605 * to be the current connection and it doesn't need to be idle. No more
606 * IO or callbacks will occur.
608 * You can close a connection twice without harmful effects.
611 * static struct io_plan *close_on_timeout(struct io_conn *conn, const char *msg)
613 * printf("closing: %s\n", msg);
614 * return io_close(conn);
617 struct io_plan *io_close(struct io_conn *conn);
620 * io_close_cb - helper callback to close a connection.
621 * @conn: the connection.
623 * This schedules a connection to be closed; designed to be used as
624 * a callback function.
627 * #define close_on_timeout io_close_cb
629 struct io_plan *io_close_cb(struct io_conn *, void *unused);
632 * io_loop - process fds until all closed on io_break.
633 * @timers - timers which are waiting to go off (or NULL for none)
634 * @expired - an expired timer (can be NULL if @timers is)
636 * This is the core loop; it exits with the io_break() arg, or NULL if
637 * all connections and listeners are closed, or with @expired set to an
638 * expired timer (if @timers isn't NULL).
641 * io_loop(NULL, NULL);
643 void *io_loop(struct timers *timers, struct timer **expired);
646 * io_conn_fd - get the fd from a connection.
647 * @conn: the connection.
649 * Sometimes useful, eg for getsockname().
651 int io_conn_fd(const struct io_conn *conn);
654 * io_time_override - override the normal call for time.
655 * @nowfn: the function to call.
657 * io usually uses time_mono() internally, but this forces it
658 * to use your function (eg. for debugging). Returns the old
661 struct timemono (*io_time_override(struct timemono (*now)(void)))(void);
664 * io_set_debug - set synchronous mode on a connection.
665 * @conn: the connection.
666 * @debug: whether to enable or disable debug.
668 * Once @debug is true on a connection, all I/O is done synchronously
669 * as soon as it is set, until it is unset or @conn is closed. This
670 * makes it easy to debug what's happening with a connection, but note
671 * that other connections are starved while this is being done.
673 * See also: io_debug_complete()
676 * // Dumb init function to set debug and tell conn to close.
677 * static struct io_plan *conn_init(struct io_conn *conn, const char *msg)
679 * io_set_debug(conn, true);
680 * return io_close(conn);
683 void io_set_debug(struct io_conn *conn, bool debug);
686 * io_debug_complete - empty function called when conn is closing/waiting.
687 * @conn: the connection.
689 * This is for putting a breakpoint onto, when debugging. It is called
690 * when a conn with io_set_debug() true can no longer be synchronous:
691 * 1) It is io_close()'d
692 * 2) It enters io_wait() (sychronous debug will resume after io_wake())
693 * 3) io_break() is called (sychronous debug will resume after io_loop())
695 void io_debug_complete(struct io_conn *conn);
696 #endif /* CCAN_IO_H */