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;
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");
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 * @dir: IO_IN or IO_OUT
344 * @next: function to call.
345 * @arg: @next argument
347 * Sometimes it's neater to plan a callback rather than call it directly;
348 * for example, if you only need to read data for one path and not another.
351 * static struct io_plan *init_conn_with_nothing(struct io_conn *conn,
354 * // Silly example: close on next time around loop.
355 * return io_always(conn, IO_IN, io_close_cb, NULL);
358 #define io_always(conn, dir, next, arg) \
359 io_always_((conn), dir, typesafe_cb_preargs(struct io_plan *, void *, \
364 struct io_plan *io_always_(struct io_conn *conn, enum io_direction dir,
365 struct io_plan *(*next)(struct io_conn *, void *),
369 * io_connect - create an asynchronous connection to a listening socket.
370 * @conn: the connection that plan is for.
371 * @addr: where to connect.
372 * @init: function to call once it's connected
373 * @arg: @init argument
375 * This initiates a connection, and creates a plan for
376 * (asynchronously) completing it. Once complete, the @init function
380 * #include <sys/types.h>
381 * #include <sys/socket.h>
384 * // Write, then close socket.
385 * static struct io_plan *init_connect(struct io_conn *conn,
386 * struct addrinfo *addrinfo)
388 * return io_connect(conn, addrinfo, io_close_cb, NULL);
394 * struct addrinfo *addrinfo;
396 * fd = socket(AF_INET, SOCK_STREAM, 0);
397 * getaddrinfo("localhost", "8111", NULL, &addrinfo);
398 * io_new_conn(NULL, fd, init_connect, addrinfo);
401 #define io_connect(conn, addr, next, arg) \
402 io_connect_((conn), (addr), \
403 typesafe_cb_preargs(struct io_plan *, void *, \
408 struct io_plan *io_connect_(struct io_conn *conn, const struct addrinfo *addr,
409 struct io_plan *(*next)(struct io_conn *, void *),
413 * io_wait - leave a plan idle until something wakes us.
414 * @conn: the connection that plan is for.
415 * @waitaddr: the address to wait on.
416 * @dir: IO_IN or IO_OUT
417 * @next: function to call after waiting.
418 * @arg: @next argument
420 * This leaves the input or output idle: io_wake(@waitaddr) will be
421 * called later to restart the connection.
424 * // Silly example to wait then close.
425 * static struct io_plan *wait(struct io_conn *conn, void *b)
427 * return io_wait(conn, b, IO_IN, io_close_cb, NULL);
430 #define io_wait(conn, waitaddr, dir, next, arg) \
431 io_wait_((conn), (waitaddr), (dir), \
432 typesafe_cb_preargs(struct io_plan *, void *, \
437 struct io_plan *io_wait_(struct io_conn *conn,
438 const void *wait, enum io_direction dir,
439 struct io_plan *(*next)(struct io_conn *, void *),
444 * io_wake - wake up any connections waiting on @wait
445 * @waitaddr: the address to trigger.
447 * All io_conns who have returned io_wait() on @waitaddr will move on
448 * to their next callback.
451 * static struct io_plan *wake_it(struct io_conn *conn, void *b)
454 * return io_close(conn);
457 void io_wake(const void *wait);
460 * io_break - return from io_loop()
461 * @ret: non-NULL value to return from io_loop().
463 * This breaks out of the io_loop. As soon as the current function
464 * returns, any io_close()'d connections will have their finish
465 * callbacks called, then io_loop() with return with @ret.
467 * If io_loop() is called again, then @plan will be carried out.
470 * static struct io_plan *fail_on_timeout(struct io_conn *conn, char *msg)
473 * return io_close(conn);
476 void io_break(const void *ret);
479 * io_never - assert if callback is called.
480 * @conn: the connection that plan is for.
482 * Sometimes you want to make it clear that a callback should never happen
483 * (eg. for io_break). This will assert() if called.
486 * static struct io_plan *break_out(struct io_conn *conn, void *unused)
489 * // We won't ever return from io_break
490 * return io_never(conn);
493 struct io_plan *io_never(struct io_conn *conn);
495 /* FIXME: io_recvfrom/io_sendto */
498 * io_close - plan to close a connection.
499 * @conn: the connection to close.
501 * On return to io_loop, the connection will be closed. It doesn't have
502 * to be the current connection and it doesn't need to be idle. No more
503 * IO or callbacks will occur.
505 * You can close a connection twice without harmful effects.
508 * static struct io_plan *close_on_timeout(struct io_conn *conn, const char *msg)
510 * printf("closing: %s\n", msg);
511 * return io_close(conn);
514 struct io_plan *io_close(struct io_conn *conn);
517 * io_close_cb - helper callback to close a connection.
518 * @conn: the connection.
520 * This schedules a connection to be closed; designed to be used as
521 * a callback function.
524 * #define close_on_timeout io_close_cb
526 struct io_plan *io_close_cb(struct io_conn *, void *unused);
529 * io_loop - process fds until all closed on io_break.
531 * This is the core loop; it exits with the io_break() arg, or NULL if
532 * all connections and listeners are closed.
540 * io_conn_fd - get the fd from a connection.
541 * @conn: the connection.
543 * Sometimes useful, eg for getsockname().
545 int io_conn_fd(const struct io_conn *conn);
546 #endif /* CCAN_IO_H */