You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

142 lines
5.6 KiB

  1. #ifndef fooasyncnshfoo
  2. #define fooasyncnshfoo
  3. /***
  4. This file is part of libasyncns.
  5. Copyright 2005-2008 Lennart Poettering
  6. libasyncns is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU Lesser General Public License as
  8. published by the Free Software Foundation, either version 2.1 of the
  9. License, or (at your option) any later version.
  10. libasyncns is distributed in the hope that it will be useful, but
  11. WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. Lesser General Public License for more details.
  14. You should have received a copy of the GNU Lesser General Public
  15. License along with libasyncns. If not, see
  16. <http://www.gnu.org/licenses/>.
  17. ***/
  18. #include <sys/types.h>
  19. #include <sys/socket.h>
  20. #include <netdb.h>
  21. /** \mainpage
  22. *
  23. * \section moo Method of operation
  24. *
  25. * To use libasyncns allocate an asyncns_t object with
  26. * asyncns_new(). This will spawn a number of worker threads (or processes, depending on what is available) which
  27. * are subsequently used to process the queries the controlling
  28. * program issues via asyncns_getaddrinfo() and
  29. * asyncns_getnameinfo(). Use asyncns_free() to shut down the worker
  30. * threads/processes.
  31. *
  32. * Since libasyncns may fork off new processes you have to make sure that
  33. * your program is not irritated by spurious SIGCHLD signals.
  34. */
  35. /** \example asyncns-test.c
  36. * An example program */
  37. #ifdef __cplusplus
  38. extern "C" {
  39. #endif
  40. /** An opaque libasyncns session structure */
  41. typedef struct asyncns asyncns_t;
  42. /** An opaque libasyncns query structure */
  43. typedef struct asyncns_query asyncns_query_t;
  44. /** Allocate a new libasyncns session with n_proc worker processes/threads */
  45. asyncns_t* asyncns_new(unsigned n_proc);
  46. /** Free a libasyncns session. This destroys all attached
  47. * asyncns_query_t objects automatically */
  48. void asyncns_free(asyncns_t *asyncns);
  49. /** Return the UNIX file descriptor to select() for readability
  50. * on. Use this function to integrate libasyncns with your custom main
  51. * loop. */
  52. int asyncns_fd(asyncns_t *asyncns);
  53. /** Process pending responses. After this function is called you can
  54. * get the next completed query object(s) using asyncns_getnext(). If
  55. * block is non-zero wait until at least one response has been
  56. * processed. If block is zero, process all pending responses and
  57. * return. */
  58. int asyncns_wait(asyncns_t *asyncns, int block);
  59. /** Issue a name to address query on the specified session. The
  60. * arguments are compatible with the ones of libc's
  61. * getaddrinfo(3). The function returns a new query object. When the
  62. * query is completed you may retrieve the results using
  63. * asyncns_getaddrinfo_done().*/
  64. asyncns_query_t* asyncns_getaddrinfo(asyncns_t *asyncns, const char *node, const char *service, const struct addrinfo *hints);
  65. /** Retrieve the results of a preceding asyncns_getaddrinfo()
  66. * call. Returns a addrinfo structure and a return value compatible
  67. * with libc's getaddrinfo(3). The query object q is destroyed by this
  68. * call and may not be used any further. Make sure to free the
  69. * returned addrinfo structure with asyncns_freeaddrinfo() and not
  70. * libc's freeaddrinfo(3)! If the query is not completed yet EAI_AGAIN
  71. * is returned.*/
  72. int asyncns_getaddrinfo_done(asyncns_t *asyncns, asyncns_query_t* q, struct addrinfo **ret_res);
  73. /** Issue an address to name query on the specified session. The
  74. * arguments are compatible with the ones of libc's
  75. * getnameinfo(3). The function returns a new query object. When the
  76. * query is completed you may retrieve the results using
  77. * asyncns_getnameinfo_done(). Set gethost (resp. getserv) to non-zero
  78. * if you want to query the hostname (resp. the service name). */
  79. asyncns_query_t* asyncns_getnameinfo(asyncns_t *asyncns, const struct sockaddr *sa, socklen_t salen, int flags, int gethost, int getserv);
  80. /** Retrieve the results of a preceding asyncns_getnameinfo()
  81. * call. Returns the hostname and the service name in ret_host and
  82. * ret_serv. The query object q is destroyed by this call and may not
  83. * be used any further. If the query is not completed yet EAI_AGAIN is
  84. * returned. */
  85. int asyncns_getnameinfo_done(asyncns_t *asyncns, asyncns_query_t* q, char *ret_host, size_t hostlen, char *ret_serv, size_t servlen);
  86. /** Return the next completed query object. If no query has been
  87. * completed yet, return NULL. Please note that you need to run
  88. * asyncns_wait() before this function will return sensible data. */
  89. asyncns_query_t* asyncns_getnext(asyncns_t *asyncns);
  90. /** Return the number of query objects (completed or not) attached to
  91. * this session */
  92. int asyncns_getnqueries(asyncns_t *asyncns);
  93. /** Cancel a currently running query. q is is destroyed by this call
  94. * and may not be used any futher. */
  95. void asyncns_cancel(asyncns_t *asyncns, asyncns_query_t* q);
  96. /** Free the addrinfo structure as returned by
  97. * asyncns_getaddrinfo_done(). Make sure to use this functions instead
  98. * of the libc's freeaddrinfo()! */
  99. void asyncns_freeaddrinfo(struct addrinfo *ai);
  100. /** Free the answer data as returned by asyncns_res_done().*/
  101. void asyncns_freeanswer(unsigned char *answer);
  102. /** Returns non-zero when the query operation specified by q has been completed */
  103. int asyncns_isdone(asyncns_t *asyncns, asyncns_query_t*q);
  104. /** Assign some opaque userdata with a query object */
  105. void asyncns_setuserdata(asyncns_t *asyncns, asyncns_query_t *q, void *userdata);
  106. /** Return userdata assigned to a query object. Use
  107. * asyncns_setuserdata() to set this data. If no data has been set
  108. * prior to this call it returns NULL. */
  109. void* asyncns_getuserdata(asyncns_t *asyncns, asyncns_query_t *q);
  110. #ifdef __cplusplus
  111. }
  112. #endif
  113. #endif