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.

163 lines
6.8 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. /** Issue a resolver query on the specified session. The arguments are
  87. * compatible with the ones of libc's res_query(3). The function returns a new
  88. * query object. When the query is completed you may retrieve the results using
  89. * asyncns_res_done(). */
  90. asyncns_query_t* asyncns_res_query(asyncns_t *asyncns, const char *dname, int class, int type);
  91. /** Issue an resolver query on the specified session. The arguments are
  92. * compatible with the ones of libc's res_search(3). The function returns a new
  93. * query object. When the query is completed you may retrieve the results using
  94. * asyncns_res_done(). */
  95. asyncns_query_t* asyncns_res_search(asyncns_t *asyncns, const char *dname, int class, int type);
  96. /** Retrieve the results of a preceding asyncns_res_query() or
  97. * asyncns_res_search call. The query object q is destroyed by this
  98. * call and may not be used any further. Returns a pointer to the
  99. * answer of the res_query call. If the query is not completed yet
  100. * -EAGAIN is returned, on failure -errno is returned, otherwise the
  101. * length of answer is returned. Make sure to free the answer is a
  102. * call to asyncns_freeanswer(). */
  103. int asyncns_res_done(asyncns_t *asyncns, asyncns_query_t* q, unsigned char **answer);
  104. /** Return the next completed query object. If no query has been
  105. * completed yet, return NULL. Please note that you need to run
  106. * asyncns_wait() before this function will return sensible data. */
  107. asyncns_query_t* asyncns_getnext(asyncns_t *asyncns);
  108. /** Return the number of query objects (completed or not) attached to
  109. * this session */
  110. int asyncns_getnqueries(asyncns_t *asyncns);
  111. /** Cancel a currently running query. q is is destroyed by this call
  112. * and may not be used any futher. */
  113. void asyncns_cancel(asyncns_t *asyncns, asyncns_query_t* q);
  114. /** Free the addrinfo structure as returned by
  115. * asyncns_getaddrinfo_done(). Make sure to use this functions instead
  116. * of the libc's freeaddrinfo()! */
  117. void asyncns_freeaddrinfo(struct addrinfo *ai);
  118. /** Free the answer data as returned by asyncns_res_done().*/
  119. void asyncns_freeanswer(unsigned char *answer);
  120. /** Returns non-zero when the query operation specified by q has been completed */
  121. int asyncns_isdone(asyncns_t *asyncns, asyncns_query_t*q);
  122. /** Assign some opaque userdata with a query object */
  123. void asyncns_setuserdata(asyncns_t *asyncns, asyncns_query_t *q, void *userdata);
  124. /** Return userdata assigned to a query object. Use
  125. * asyncns_setuserdata() to set this data. If no data has been set
  126. * prior to this call it returns NULL. */
  127. void* asyncns_getuserdata(asyncns_t *asyncns, asyncns_query_t *q);
  128. #ifdef __cplusplus
  129. }
  130. #endif
  131. #endif