]> git.ozlabs.org Git - ccan/blob - ccan/io/io.h
fdpass: fix complilation on FreeBSD.
[ccan] / ccan / io / io.h
1 /* Licensed under LGPLv2.1+ - see LICENSE file for details */
2 #ifndef CCAN_IO_H
3 #define CCAN_IO_H
4 #include <ccan/tal/tal.h>
5 #include <ccan/typesafe_cb/typesafe_cb.h>
6 #include <stdbool.h>
7 #include <poll.h>
8 #include <unistd.h>
9
10 struct timers;
11 struct timer;
12 struct list_head;
13
14 /**
15  * struct io_plan - a plan for input or output.
16  *
17  * Each io_conn has zero to two of these active at any time.
18  */
19 struct io_plan;
20
21 /**
22  * struct io_conn - a connection associated with an fd.
23  */
24 struct io_conn;
25
26 /**
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.
32  *
33  * This creates a connection which owns @fd, it then calls
34  * @init to initialize the connection, which sets up an io_plan.
35  *
36  * Returns NULL on error (and sets errno).
37  *
38  * Example:
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)
41  * {
42  *      printf("Created conn %p: %s", conn, msg);
43  *      return io_close(conn);
44  * }
45  *
46  * static void create_self_closing_pipe(void)
47  * {
48  *      int fd[2];
49  *      struct io_conn *conn;
50  *
51  *      if (pipe(fd) != 0)
52  *              exit(1);
53  *      conn = io_new_conn(NULL, fd[0], conn_init, (const char *)"hi!");
54  *      if (!conn)
55  *              exit(1);
56  * }
57  */
58 #define io_new_conn(ctx, fd, init, arg)                                 \
59         io_new_conn_((ctx), (fd),                                       \
60                      typesafe_cb_preargs(struct io_plan *, void *,      \
61                                          (init), (arg),                 \
62                                          struct io_conn *),             \
63                      (void *)(arg))
64
65 struct io_conn *io_new_conn_(const tal_t *ctx, int fd,
66                              struct io_plan *(*init)(struct io_conn *, void *),
67                              void *arg);
68
69 /**
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.
74  *
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.
79  *
80  * Example:
81  * static void finish(struct io_conn *conn, const char *msg)
82  * {
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);
85  * }
86  *
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)
89  * {
90  *      io_set_finish(conn, finish, msg);
91  *      return io_close(conn);
92  * }
93  */
94 #define io_set_finish(conn, finish, arg)                                \
95         io_set_finish_((conn),                                          \
96                        typesafe_cb_preargs(void, void *,                \
97                                            (finish), (arg),             \
98                                            struct io_conn *),           \
99                        (void *)(arg))
100 void io_set_finish_(struct io_conn *conn,
101                     void (*finish)(struct io_conn *, void *),
102                     void *arg);
103
104
105 /**
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.
111  *
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.
115  *
116  * Note: if the accept fails (usually due to EMFILE), init() will be called
117  * wth
118  * Returns NULL on error (and sets errno).
119  *
120  * Example:
121  * #include <sys/types.h>
122  * #include <sys/socket.h>
123  * #include <netdb.h>
124  *
125  * ...
126  *
127  * // Set up a listening socket, return it.
128  * static struct io_listener *do_listen(const char *port)
129  * {
130  *      struct addrinfo *addrinfo, hints;
131  *      int fd, on = 1;
132  *
133  *      memset(&hints, 0, sizeof(hints));
134  *      hints.ai_family = AF_UNSPEC;
135  *      hints.ai_socktype = SOCK_STREAM;
136  *      hints.ai_flags = AI_PASSIVE;
137  *      hints.ai_protocol = 0;
138  *
139  *      if (getaddrinfo(NULL, port, &hints, &addrinfo) != 0)
140  *              return NULL;
141  *
142  *      fd = socket(addrinfo->ai_family, addrinfo->ai_socktype,
143  *                  addrinfo->ai_protocol);
144  *      if (fd < 0)
145  *              return NULL;
146  *
147  *      freeaddrinfo(addrinfo);
148  *      setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
149  *      if (bind(fd, addrinfo->ai_addr, addrinfo->ai_addrlen) != 0) {
150  *              close(fd);
151  *              return NULL;
152  *      }
153  *      if (listen(fd, 1) != 0) {
154  *              close(fd);
155  *              return NULL;
156  *      }
157  *      return io_new_listener(NULL, fd, conn_init, (const char *)"listened!");
158  * }
159  */
160 #define io_new_listener(ctx, fd, init, arg)                             \
161         io_new_listener_((ctx), (fd),                                   \
162                          typesafe_cb_preargs(struct io_plan *, void *,  \
163                                              (init), (arg),             \
164                                              struct io_conn *conn),     \
165                          (void *)(arg))
166 struct io_listener *io_new_listener_(const tal_t *ctx, int fd,
167                                      struct io_plan *(*init)(struct io_conn *,
168                                                              void *),
169                                      void *arg);
170
171 /**
172  * io_close_listener - delete a listener.
173  * @listener: the listener returned from io_new_listener.
174  *
175  * This closes the fd and frees @listener.
176  *
177  * Example:
178  * ...
179  *      struct io_listener *l = do_listen("8111");
180  *      if (l) {
181  *              io_loop(NULL, NULL);
182  *              io_close_listener(l);
183  *      }
184  */
185 void io_close_listener(struct io_listener *listener);
186
187 /**
188  * io_write - output plan to write data.
189  * @conn: the connection that plan is for.
190  * @data: the data buffer.
191  * @len: the length to write.
192  * @next: function to call output is done.
193  * @arg: @next argument
194  *
195  * This updates the output plan, to write out a data buffer.  Once it's all
196  * written, the @next function will be called: on an error, the finish
197  * function is called instead.
198  *
199  * Note that the I/O may actually be done immediately.
200  *
201  * Example:
202  * static struct io_plan *write_to_conn(struct io_conn *conn, const char *msg)
203  * {
204  *      // Write message, then close.
205  *      return io_write(conn, msg, strlen(msg), io_close_cb, NULL);
206  * }
207  */
208 #define io_write(conn, data, len, next, arg)                            \
209         io_write_((conn), (data), (len),                                \
210                   typesafe_cb_preargs(struct io_plan *, void *,         \
211                                       (next), (arg), struct io_conn *), \
212                   (arg))
213 struct io_plan *io_write_(struct io_conn *conn,
214                           const void *data, size_t len,
215                           struct io_plan *(*next)(struct io_conn *, void *),
216                           void *arg);
217
218 /**
219  * io_read - input plan to read data.
220  * @conn: the connection that plan is for.
221  * @data: the data buffer.
222  * @len: the length to read.
223  * @next: function to call once input is done.
224  * @arg: @next argument
225  *
226  * This creates a plan to read data into a buffer.  Once it's all
227  * read, the @next function will be called: on an error, the finish
228  * function is called instead.  If read() returns 0 (EOF) errno is set
229  * to 0.
230  *
231  * Note that the I/O may actually be done immediately.
232  *
233  * Example:
234  * static struct io_plan *read_from_conn(struct io_conn *conn, char *buf)
235  * {
236  *      // Read message, then close.
237  *      return io_read(conn, buf, 12, io_close_cb, NULL);
238  * }
239  */
240 #define io_read(conn, data, len, next, arg)                             \
241         io_read_((conn), (data), (len),                                 \
242                  typesafe_cb_preargs(struct io_plan *, void *,          \
243                                      (next), (arg), struct io_conn *),  \
244                  (arg))
245 struct io_plan *io_read_(struct io_conn *conn,
246                          void *data, size_t len,
247                          struct io_plan *(*next)(struct io_conn *, void *),
248                          void *arg);
249
250
251 /**
252  * io_read_partial - input plan to read some data.
253  * @conn: the connection that plan is for.
254  * @data: the data buffer.
255  * @maxlen: the maximum length to read
256  * @lenp: set to the length actually read.
257  * @next: function to call once input is done.
258  * @arg: @next argument
259  *
260  * This creates a plan to read data into a buffer.  Once any data is
261  * read, @len is updated and the @next function will be called: on an
262  * error, the finish function is called instead.  If read() returns 0 (EOF)
263  * errno is set to 0.
264  *
265  * Note that the I/O may actually be done immediately.
266  *
267  * Example:
268  * struct buf {
269  *      size_t len;
270  *      char buf[12];
271  * };
272  *
273  * static struct io_plan *dump(struct io_conn *conn, struct buf *b)
274  * {
275  *      printf("Partial read: '%*s'\n", (int)b->len, b->buf);
276  *      free(b);
277  *      return io_close(conn);
278  * }
279  *
280  * static struct io_plan *read_part(struct io_conn *conn, struct buf *b)
281  * {
282  *      // Read message, then dump and close.
283  *      return io_read_partial(conn, b->buf, sizeof(b->buf), &b->len, dump, b);
284  * }
285  */
286 #define io_read_partial(conn, data, maxlen, lenp, next, arg)            \
287         io_read_partial_((conn), (data), (maxlen), (lenp),              \
288                          typesafe_cb_preargs(struct io_plan *, void *,  \
289                                              (next), (arg),             \
290                                              struct io_conn *),         \
291                          (arg))
292 struct io_plan *io_read_partial_(struct io_conn *conn,
293                                  void *data, size_t maxlen, size_t *lenp,
294                                  struct io_plan *(*next)(struct io_conn *,
295                                                          void *),
296                                  void *arg);
297
298 /**
299  * io_write_partial - output plan to write some data.
300  * @conn: the connection that plan is for.
301  * @data: the data buffer.
302  * @maxlen: the maximum length to write
303  * @lenp: set to the length actually written.
304  * @next: function to call once output is done.
305  * @arg: @next argument
306  *
307  * This creates a plan to write data from a buffer.   Once any data is
308  * written, @len is updated and the @next function will be called: on an
309  * error, the finish function is called instead.
310  *
311  * Note that the I/O may actually be done immediately.
312  *
313  * Example:
314  * struct buf {
315  *      size_t len;
316  *      char buf[12];
317  * };
318  *
319  * static struct io_plan *show_partial(struct io_conn *conn, struct buf *b)
320  * {
321  *      printf("Only wrote: '%*s'\n", (int)b->len, b->buf);
322  *      free(b);
323  *      return io_close(conn);
324  * }
325  *
326  * static struct io_plan *write_part(struct io_conn *conn, struct buf *b)
327  * {
328  *      // Write message, then dump and close.
329  *      strcpy(b->buf, "Hello world");
330  *      return io_write_partial(conn, b->buf, strlen(b->buf),
331  *                              &b->len, show_partial, b);
332  * }
333  */
334 #define io_write_partial(conn, data, maxlen, lenp, next, arg)           \
335         io_write_partial_((conn), (data), (maxlen), (lenp),             \
336                           typesafe_cb_preargs(struct io_plan *, void *, \
337                                               (next), (arg),            \
338                                               struct io_conn *),        \
339                           (arg))
340 struct io_plan *io_write_partial_(struct io_conn *conn,
341                                   const void *data, size_t maxlen, size_t *lenp,
342                                   struct io_plan *(*next)(struct io_conn *,
343                                                           void*),
344                                   void *arg);
345
346 /**
347  * io_always - plan to immediately call next callback
348  * @conn: the connection that plan is for.
349  * @next: function to call.
350  * @arg: @next argument
351  *
352  * Sometimes it's neater to plan a callback rather than call it directly;
353  * for example, if you only need to read data for one path and not another.
354  *
355  * Example:
356  * static struct io_plan *init_conn_with_nothing(struct io_conn *conn,
357  *                                               void *unused)
358  * {
359  *      // Silly example: close on next time around loop.
360  *      return io_always(conn, io_close_cb, NULL);
361  * }
362  */
363 #define io_always(conn, next, arg)                                      \
364         io_always_((conn), typesafe_cb_preargs(struct io_plan *, void *, \
365                                                (next), (arg),           \
366                                                struct io_conn *),       \
367                    (arg))
368
369 struct io_plan *io_always_(struct io_conn *conn,
370                            struct io_plan *(*next)(struct io_conn *, void *),
371                            void *arg);
372
373 /**
374  * io_out_always - output plan to immediately call next callback
375  * @conn: the connection that plan is for.
376  * @next: function to call.
377  * @arg: @next argument
378  *
379  * This is a variant of io_always() which uses the output plan; it only
380  * matters if you are using io_duplex, and thus have two plans running at
381  * once.
382  */
383 #define io_out_always(conn, next, arg)                                  \
384         io_out_always_((conn), typesafe_cb_preargs(struct io_plan *, void *, \
385                                                    (next), (arg),       \
386                                                    struct io_conn *),   \
387                        (arg))
388
389 struct io_plan *io_out_always_(struct io_conn *conn,
390                                struct io_plan *(*next)(struct io_conn *,
391                                                        void *),
392                                void *arg);
393
394 /**
395  * io_sock_shutdown - start socket close process (flushes TCP sockets).
396  * @conn: the connection the plan is for
397  *
398  * Simply closing a TCP socket can lose data; unfortunately you should
399  * shutdown(SHUT_WR) and wait for the other side to see this and close.
400  * Of course, you also need to set a timer, in case it doesn't (you may
401  * already have some responsiveness timer, of course).
402  *
403  * On error, is equivalent to io_close().
404  *
405  * Example:
406  * #include <ccan/timer/timer.h>
407  *
408  * // Timer infra needs wrapper to contain extra data.
409  * struct timeout_timer {
410  *    struct timer t;
411  *    struct io_conn *conn;
412  * };
413  * static struct timers timers;
414  *
415  * static struct io_plan *flush_and_close(struct io_conn *conn)
416  * {
417  *    struct timeout_timer *timeout;
418  *    // Freed if conn closes normally.
419  *    timeout = tal(conn, struct timeout_timer);
420  *    timeout->conn = conn;
421  *    timer_addrel(&timers, &timeout->t, time_from_sec(5));
422  *    return io_sock_shutdown(conn);
423  * }
424  */
425 struct io_plan *io_sock_shutdown(struct io_conn *conn);
426
427 /**
428  * io_connect - create an asynchronous connection to a listening socket.
429  * @conn: the connection that plan is for.
430  * @addr: where to connect.
431  * @init: function to call once it's connected
432  * @arg: @init argument
433  *
434  * This initiates a connection, and creates a plan for
435  * (asynchronously) completing it.  Once complete, the @init function
436  * will be called.
437  *
438  * Example:
439  * #include <sys/types.h>
440  * #include <sys/socket.h>
441  * #include <netdb.h>
442  *
443  * // Write, then close socket.
444  * static struct io_plan *init_connect(struct io_conn *conn,
445  *                                     struct addrinfo *addrinfo)
446  * {
447  *      return io_connect(conn, addrinfo, io_close_cb, NULL);
448  * }
449  *
450  * ...
451  *
452  *      int fd;
453  *      struct addrinfo *addrinfo;
454  *
455  *      fd = socket(AF_INET, SOCK_STREAM, 0);
456  *      getaddrinfo("localhost", "8111", NULL, &addrinfo);
457  *      io_new_conn(NULL, fd, init_connect, addrinfo);
458  */
459 struct addrinfo;
460 #define io_connect(conn, addr, next, arg)                               \
461         io_connect_((conn), (addr),                                     \
462                     typesafe_cb_preargs(struct io_plan *, void *,       \
463                                         (next), (arg),                  \
464                                         struct io_conn *),              \
465                     (arg))
466
467 struct io_plan *io_connect_(struct io_conn *conn, const struct addrinfo *addr,
468                             struct io_plan *(*next)(struct io_conn *, void *),
469                             void *arg);
470
471 /**
472  * io_duplex - set plans for both input and output.
473  * @conn: the connection that plan is for.
474  * @in: the input plan
475  * @out: the output plan
476  *
477  * Most plans are either for input or output; io_duplex creates a plan
478  * which does both.  This is often used in the init function to create
479  * two independent streams, though it can be used once on any connection.
480  *
481  * Note that if either plan closes the connection, it will be closed.
482  *
483  * Example:
484  * struct buf {
485  *      char in[100];
486  *      char out[100];
487  * };
488  *
489  * static struct io_plan *read_and_write(struct io_conn *conn, struct buf *b)
490  * {
491  *      return io_duplex(conn,
492  *                       io_read(conn, b->in, sizeof(b->in), io_close_cb, b),
493  *                       io_write(conn, b->out, sizeof(b->out), io_close_cb,b));
494  * }
495  */
496 struct io_plan *io_duplex(struct io_conn *conn,
497                           struct io_plan *in_plan, struct io_plan *out_plan);
498
499 /**
500  * io_halfclose - close half of an io_duplex connection.
501  * @conn: the connection that plan is for.
502  *
503  * It's common to want to close a duplex connection after both input and
504  * output plans have completed.  If either calls io_close() the connection
505  * closes immediately.  Instead, io_halfclose() needs to be called twice.
506  *
507  * Example:
508  * struct buf {
509  *      char in[100];
510  *      char out[100];
511  * };
512  *
513  * static struct io_plan *finish(struct io_conn *conn, struct buf *b)
514  * {
515  *      return io_halfclose(conn);
516  * }
517  *
518  * static struct io_plan *read_and_write(struct io_conn *conn, struct buf *b)
519  * {
520  *      return io_duplex(conn,
521  *                       io_read(conn, b->in, sizeof(b->in), finish, b),
522  *                       io_write(conn, b->out, sizeof(b->out), finish, b));
523  * }
524  */
525 struct io_plan *io_halfclose(struct io_conn *conn);
526
527 /**
528  * io_wait - leave a plan idle until something wakes us.
529  * @conn: the connection that plan is for.
530  * @waitaddr: the address to wait on.
531  * @next: function to call after waiting.
532  * @arg: @next argument
533  *
534  * This leaves the input or output idle: io_wake(@waitaddr) will be
535  * called later to restart the connection.
536  *
537  * Example:
538  * // Silly example to wait then close.
539  * static struct io_plan *wait(struct io_conn *conn, void *b)
540  * {
541  *      return io_wait(conn, b, io_close_cb, NULL);
542  * }
543  */
544 #define io_wait(conn, waitaddr, next, arg)                              \
545         io_wait_((conn), (waitaddr),                                    \
546                  typesafe_cb_preargs(struct io_plan *, void *,          \
547                                      (next), (arg),                     \
548                                      struct io_conn *),                 \
549                  (arg))
550
551 struct io_plan *io_wait_(struct io_conn *conn,
552                          const void *wait,
553                          struct io_plan *(*next)(struct io_conn *, void *),
554                          void *arg);
555
556
557 /**
558  * io_out_wait - leave the output plan idle until something wakes us.
559  * @conn: the connection that plan is for.
560  * @waitaddr: the address to wait on.
561  * @next: function to call after waiting.
562  * @arg: @next argument
563  *
564  * io_wait() makes the input plan idle: if you're not using io_duplex it
565  * doesn't matter which plan is waiting.  Otherwise, you may need to use
566  * io_out_wait() instead, to specify explicitly that the output plan is
567  * waiting.
568  */
569 #define io_out_wait(conn, waitaddr, next, arg)                          \
570         io_out_wait_((conn), (waitaddr),                                \
571                      typesafe_cb_preargs(struct io_plan *, void *,      \
572                                          (next), (arg),                 \
573                                          struct io_conn *),             \
574                      (arg))
575
576 struct io_plan *io_out_wait_(struct io_conn *conn,
577                              const void *wait,
578                              struct io_plan *(*next)(struct io_conn *, void *),
579                              void *arg);
580
581 /**
582  * io_wake - wake up any connections waiting on @wait
583  * @waitaddr: the address to trigger.
584  *
585  * All io_conns who have returned io_wait() on @waitaddr will move on
586  * to their next callback.
587  *
588  * Example:
589  * static struct io_plan *wake_it(struct io_conn *conn, void *b)
590  * {
591  *      io_wake(b);
592  *      return io_close(conn);
593  * }
594  */
595 void io_wake(const void *wait);
596
597 /**
598  * io_break - return from io_loop()
599  * @ret: non-NULL value to return from io_loop().
600  *
601  * This breaks out of the io_loop.  As soon as the current function
602  * returns, any io_close()'d connections will have their finish
603  * callbacks called, then io_loop() with return with @ret.
604  *
605  * If io_loop() is called again, then @plan will be carried out.
606  *
607  * Example:
608  *      static struct io_plan *fail_on_timeout(struct io_conn *conn, char *msg)
609  *      {
610  *              io_break(msg);
611  *              return io_close(conn);
612  *      }
613  */
614 void io_break(const void *ret);
615
616 /**
617  * io_never - assert if callback is called.
618  * @conn: the connection that plan is for.
619  * @unused: an unused parameter to make this suitable for use as a callback.
620  *
621  * Sometimes you want to make it clear that a callback should never happen
622  * (eg. for io_break).  This will assert() if called.
623  *
624  * Example:
625  * static struct io_plan *break_out(struct io_conn *conn, void *unused)
626  * {
627  *      io_break(conn);
628  *      // We won't ever return from io_break
629  *      return io_never(conn, NULL);
630  * }
631  */
632 struct io_plan *io_never(struct io_conn *conn, void *unused);
633
634 /* FIXME: io_recvfrom/io_sendto */
635
636 /**
637  * io_close - close a connection.
638  * @conn: the connection to close.
639  *
640  * The connection is immediately freed: it doesn't have to be the
641  * current connection and it doesn't need to be idle.  No more IO or
642  * callbacks will occur, but if a function was added by io_set_finish()
643  * it will be called with the current errno preserved.
644  *
645  * This is equivalent to tal_free(io_conn), except it returns an io_plan
646  * for use in an io callback.
647  *
648  * Example:
649  * static struct io_plan *close_on_timeout(struct io_conn *conn, const char *msg)
650  * {
651  *      printf("closing: %s\n", msg);
652  *      return io_close(conn);
653  * }
654  */
655 struct io_plan *io_close(struct io_conn *conn);
656
657 /**
658  * io_close_cb - helper callback to close a connection.
659  * @conn: the connection.
660  *
661  * This is closes a connection; designed to be used as a callback
662  * function.
663  *
664  * Example:
665  *      #define close_on_timeout io_close_cb
666  */
667 struct io_plan *io_close_cb(struct io_conn *, void *unused);
668
669 /**
670  * io_close_taken_fd - close a connection, but remove the filedescriptor first.
671  * @conn: the connection to take the file descriptor from and close.
672  *
673  * io_close closes the file descriptor underlying the io_conn; this version does
674  * not.  Presumably you have used io_conn_fd() on it beforehand and will take
675  * care of the fd yourself.
676  *
677  * Note that this also turns off O_NONBLOCK on the fd.
678  *
679  * Example:
680  * static struct io_plan *steal_fd(struct io_conn *conn, int *fd)
681  * {
682  *      *fd = io_conn_fd(conn);
683  *      printf("stealing fd %i and closing\n", *fd);
684  *      return io_close_taken_fd(conn);
685  * }
686  */
687 struct io_plan *io_close_taken_fd(struct io_conn *conn);
688
689 /**
690  * io_loop - process fds until all closed on io_break.
691  * @timers - timers which are waiting to go off (or NULL for none)
692  * @expired - an expired timer (can be NULL if @timers is)
693  *
694  * This is the core loop; it exits with the io_break() arg, or NULL if
695  * all connections and listeners are closed, or with @expired set to an
696  * expired timer (if @timers isn't NULL).
697  *
698  * Example:
699  *      io_loop(NULL, NULL);
700  */
701 void *io_loop(struct timers *timers, struct timer **expired);
702
703 /**
704  * io_conn_fd - get the fd from a connection.
705  * @conn: the connection.
706  *
707  * Sometimes useful, eg for getsockname().  Note that the fd is O_NONBLOCK.
708  *
709  * See Also:
710  *      io_close_taken_fd
711  */
712 int io_conn_fd(const struct io_conn *conn);
713
714 /**
715  * io_plan_in_started - is this conn doing input I/O now?
716  * @conn: the conn.
717  *
718  * This returns true if input I/O has been performed on the conn but
719  * @next hasn't been called yet.  For example, io_read() may have done
720  * a partial read.
721  *
722  * This can be useful if we want to terminate a connection only after
723  * reading a whole packet: if this returns true, we would wait until
724  * @next is called.
725  */
726 bool io_plan_in_started(const struct io_conn *conn);
727
728 /**
729  * io_plan_out_started - is this conn doing output I/O now?
730  * @conn: the conn.
731  *
732  * This returns true if output I/O has been performed on the conn but
733  * @next hasn't been called yet.  For example, io_write() may have done
734  * a partial write.
735  *
736  * This can be useful if we want to terminate a connection only after
737  * writing a whole packet: if this returns true, we would wait until
738  * @next is called.
739  */
740 bool io_plan_out_started(const struct io_conn *conn);
741
742 /**
743  * io_flush_sync - (synchronously) complete any outstanding output.
744  * @conn: the connection.
745  *
746  * This is generally used as an emergency escape, for example when we
747  * want to write an error message on a socket before terminating, but it may
748  * be in the middle of existing I/O.  We don't want to service any other
749  * IO, either.
750  *
751  * This returns true if all pending output is complete, false on error.
752  * The next callback is not called on the conn, but will be as soon as
753  * io_loop() is called.
754  *
755  * See Also:
756  *      io_close_taken_fd
757  */
758 bool io_flush_sync(struct io_conn *conn);
759
760 /**
761  * io_conn_exclusive - set/unset an io_conn to exclusively serviced
762  * @conn: the connection
763  * @exclusive: whether to be exclusive or not
764  *
765  * If any io_conn is set exclusive, then no non-exclusive io_conn (or
766  * io_listener) will be serviced by io_loop().  If it's a io_duplex io_conn(),
767  * then io_conn_exclusive() makes the read-side exclusive; io_conn_out_exclusive()
768  * makes the write-side exclusive.
769  *
770  * This allows you to temporarily service only one (or several) fds.
771  * For example, you might want to flush out one io_conn and not
772  * receive any new connections or read any other input.
773  *
774  * Returns true if any exclusive io_conn remain, otherwise false.
775  * (This is useful for checking your own logic: dangling exclusive io_conn
776  * are dangerous!).
777  */
778 bool io_conn_exclusive(struct io_conn *conn, bool exclusive);
779
780 /**
781  * io_conn_out_exclusive - set/unset exclusive on the write-side of a duplex
782  * @conn: the connection, post io_duplex
783  * @exclusive: whether to be exclusive or not
784  *
785  * See io_conn_exclusive() above.
786  */
787 bool io_conn_out_exclusive(struct io_conn *conn, bool exclusive);
788
789 /**
790  * io_fd_block - helper to set an fd blocking/nonblocking.
791  * @fd: the file descriptor
792  * @block: true to set blocking, false to set non-blocking.
793  *
794  * Generally only fails is @fd isn't a valid file descriptor, otherwise
795  * returns true.
796  */
797 bool io_fd_block(int fd, bool block);
798
799 /**
800  * io_time_override - override the normal call for time.
801  * @nowfn: the function to call.
802  *
803  * io usually uses time_mono() internally, but this forces it
804  * to use your function (eg. for debugging).  Returns the old
805  * one.
806  */
807 struct timemono (*io_time_override(struct timemono (*now)(void)))(void);
808
809 /**
810  * io_poll_override - override the normal call for poll.
811  * @pollfn: the function to call.
812  *
813  * io usually uses poll() internally, but this forces it to use your
814  * function (eg. for debugging, suppressing fds, or polling on others unknown
815  * to ccan/io).  Returns the old one.
816  */
817 int (*io_poll_override(int (*poll)(struct pollfd *fds, nfds_t nfds, int timeout)))(struct pollfd *, nfds_t, int);
818
819 /**
820  * io_have_fd - do we own this file descriptor?
821  * @fd: the file descriptor.
822  * @listener: if non-NULL, set to true if it's a listening socket (io_listener).
823  *
824  * Returns NULL if we don't own it, otherwise a struct io_conn * or struct io_listener *.
825  */
826 const void *io_have_fd(int fd, bool *listener);
827
828 /**
829  * io_set_extended_errors - enable callbacks for errors.
830  * @state: true or false.
831  *
832  * Defaults false for compatibility.  See io_new_conn for what this changes.
833  */
834 void io_set_extended_errors(bool state);
835 bool io_get_extended_errors(void);
836
837 #endif /* CCAN_IO_H */