addr_resolv.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /*
  2. * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
  3. * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #ifndef __PJ_ADDR_RESOLV_H__
  20. #define __PJ_ADDR_RESOLV_H__
  21. /**
  22. * @file addr_resolv.h
  23. * @brief IP address resolution.
  24. */
  25. #include <pj/sock.h>
  26. PJ_BEGIN_DECL
  27. /**
  28. * @defgroup pj_addr_resolve Network Address Resolution
  29. * @ingroup PJ_IO
  30. * @{
  31. *
  32. * This module provides function to resolve Internet address of the
  33. * specified host name. To resolve a particular host name, application
  34. * can just call #pj_gethostbyname().
  35. *
  36. * Example:
  37. * <pre>
  38. * ...
  39. * pj_hostent he;
  40. * pj_status_t rc;
  41. * pj_str_t host = pj_str("host.example.com");
  42. *
  43. * rc = pj_gethostbyname( &host, &he);
  44. * if (rc != PJ_SUCCESS) {
  45. * char errbuf[80];
  46. * pj_strerror( rc, errbuf, sizeof(errbuf));
  47. * PJ_LOG(2,("sample", "Unable to resolve host, error=%s", errbuf));
  48. * return rc;
  49. * }
  50. *
  51. * // process address...
  52. * addr.sin_addr.s_addr = *(pj_uint32_t*)he.h_addr;
  53. * ...
  54. * </pre>
  55. *
  56. * It's pretty simple really...
  57. */
  58. /** This structure describes an Internet host address. */
  59. typedef struct pj_hostent
  60. {
  61. char *h_name; /**< The official name of the host. */
  62. char **h_aliases; /**< Aliases list. */
  63. int h_addrtype; /**< Host address type. */
  64. int h_length; /**< Length of address. */
  65. char **h_addr_list; /**< List of addresses. */
  66. } pj_hostent;
  67. /** Shortcut to h_addr_list[0] */
  68. #define h_addr h_addr_list[0]
  69. /**
  70. * This structure describes address information pj_getaddrinfo().
  71. */
  72. typedef struct pj_addrinfo
  73. {
  74. char ai_canonname[PJ_MAX_HOSTNAME]; /**< Canonical name for host*/
  75. pj_sockaddr ai_addr; /**< Binary address. */
  76. } pj_addrinfo;
  77. /**
  78. * This function fills the structure of type pj_hostent for a given host name.
  79. * For host resolution function that also works with IPv6, please see
  80. * #pj_getaddrinfo().
  81. *
  82. * @param name Host name to resolve. Specifying IPv4 address here
  83. * may fail on some platforms (e.g. Windows)
  84. * @param he The pj_hostent structure to be filled. Note that
  85. * the pointers in this structure points to temporary
  86. * variables which value will be reset upon subsequent
  87. * invocation.
  88. *
  89. * @return PJ_SUCCESS, or the appropriate error codes.
  90. */
  91. PJ_DECL(pj_status_t) pj_gethostbyname(const pj_str_t *name, pj_hostent *he);
  92. /**
  93. * Resolve the primary IP address of local host.
  94. *
  95. * @param af The desired address family to query. Valid values
  96. * are pj_AF_INET() or pj_AF_INET6().
  97. * @param addr On successful resolution, the address family and address
  98. * part of this socket address will be filled up with the host
  99. * IP address, in network byte order. Other parts of the socket
  100. * address are untouched.
  101. *
  102. * @return PJ_SUCCESS on success, or the appropriate error code.
  103. */
  104. PJ_DECL(pj_status_t) pj_gethostip(int af, pj_sockaddr *addr);
  105. /**
  106. * Get the interface IP address to send data to the specified destination.
  107. *
  108. * @param af The desired address family to query. Valid values
  109. * are pj_AF_INET() or pj_AF_INET6().
  110. * @param dst The destination host.
  111. * @param itf_addr On successful resolution, the address family and address
  112. * part of this socket address will be filled up with the host
  113. * IP address, in network byte order. Other parts of the socket
  114. * address should be ignored.
  115. * @param allow_resolve If \a dst may contain hostname (instead of IP
  116. * address), specify whether hostname resolution should
  117. * be performed. If not, default interface address will
  118. * be returned.
  119. * @param p_dst_addr If not NULL, it will be filled with the IP address of
  120. * the destination host.
  121. *
  122. * @return PJ_SUCCESS on success, or the appropriate error code.
  123. */
  124. PJ_DECL(pj_status_t) pj_getipinterface(int af,
  125. const pj_str_t *dst,
  126. pj_sockaddr *itf_addr,
  127. pj_bool_t allow_resolve,
  128. pj_sockaddr *p_dst_addr);
  129. /**
  130. * Get the IP address of the default interface. Default interface is the
  131. * interface of the default route.
  132. *
  133. * @param af The desired address family to query. Valid values
  134. * are pj_AF_INET() or pj_AF_INET6().
  135. * @param addr On successful resolution, the address family and address
  136. * part of this socket address will be filled up with the host
  137. * IP address, in network byte order. Other parts of the socket
  138. * address are untouched.
  139. *
  140. * @return PJ_SUCCESS on success, or the appropriate error code.
  141. */
  142. PJ_DECL(pj_status_t) pj_getdefaultipinterface(int af,
  143. pj_sockaddr *addr);
  144. /**
  145. * This function translates the name of a service location (for example,
  146. * a host name) and returns a set of addresses and associated information
  147. * to be used in creating a socket with which to address the specified
  148. * service.
  149. *
  150. * @param af The desired address family to query. Valid values
  151. * are pj_AF_INET(), pj_AF_INET6(), or pj_AF_UNSPEC().
  152. * @param name Descriptive name or an address string, such as host
  153. * name.
  154. * @param count On input, it specifies the number of elements in
  155. * \a ai array. On output, this will be set with the
  156. * number of address informations found for the
  157. * specified name.
  158. * @param ai Array of address info to be filled with the information
  159. * about the host.
  160. *
  161. * @return PJ_SUCCESS on success, or the appropriate error code.
  162. */
  163. PJ_DECL(pj_status_t) pj_getaddrinfo(int af, const pj_str_t *name,
  164. unsigned *count, pj_addrinfo ai[]);
  165. /**
  166. * Enumeration of IP address type.
  167. */
  168. typedef enum pj_addr_type
  169. {
  170. /**
  171. * Disabled: 0.0.0.0/8 or ::/128
  172. */
  173. PJ_ADDR_TYPE_DISABLED = 1,
  174. /**
  175. * Loopback: 127.0.0.0/8 or ::1/128
  176. */
  177. PJ_ADDR_TYPE_LOOPBACK = 2,
  178. /**
  179. * Link-local: 169.254.0.0/16 or fe80::/64
  180. */
  181. PJ_ADDR_TYPE_LINK_LOCAL = 4,
  182. /**
  183. * Private: 10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16 or fc00::/7
  184. */
  185. PJ_ADDR_TYPE_PRIVATE = 8,
  186. /**
  187. * Multicast: 224.0.0.0/3 or ff00::/8
  188. */
  189. PJ_ADDR_TYPE_MULTICAST = 16,
  190. } pj_addr_type;
  191. /**
  192. * Check IP address type.
  193. *
  194. * @param addr The IP address to check, must be IPv4 or IPv6 address.
  195. * @param type Bit mask of address type pj_addr_type.
  196. *
  197. * @return PJ_TRUE if the type of specified address \addr match to
  198. * the specified types \a type.
  199. */
  200. PJ_DECL(pj_bool_t) pj_check_addr_type(const pj_sockaddr *addr, unsigned type);
  201. /** @} */
  202. PJ_END_DECL
  203. #endif /* __PJ_ADDR_RESOLV_H__ */