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.

addr.h 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*-
  2. * Copyright 2016 Vsevolod Stakhov
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef ADDR_H_
  17. #define ADDR_H_
  18. #include "config.h"
  19. #ifdef HAVE_SYS_SOCKET_H
  20. #include <sys/socket.h>
  21. #endif
  22. #ifdef HAVE_NETINET_IN_H
  23. #include <netinet/in.h>
  24. #endif
  25. #ifdef HAVE_ARPA_INET_H
  26. #include <arpa/inet.h>
  27. #endif
  28. /* unix sockets */
  29. #ifdef HAVE_SYS_UN_H
  30. #include <sys/un.h>
  31. #endif
  32. #include "mem_pool.h"
  33. /**
  34. * Opaque structure
  35. */
  36. typedef struct rspamd_inet_addr_s rspamd_inet_addr_t;
  37. void **rspamd_inet_library_init (void);
  38. void rspamd_inet_library_destroy (void);
  39. /**
  40. * Create new inet address structure based on the address familiy and opaque init pointer
  41. * @param af
  42. * @param init
  43. * @return new inet addr
  44. */
  45. rspamd_inet_addr_t * rspamd_inet_address_new (int af, const void *init);
  46. /**
  47. * Create new inet address structure from struct sockaddr
  48. * @param sa
  49. * @param slen
  50. * @return
  51. */
  52. rspamd_inet_addr_t * rspamd_inet_address_from_sa (const struct sockaddr *sa,
  53. socklen_t slen);
  54. /**
  55. * Parse string with ipv6 address of length `len` to `target` which should be
  56. * at least sizeof (in6_addr_t)
  57. * @param text input string
  58. * @param len lenth of `text` (if 0, then `text` must be zero terminated)
  59. * @param target target structure
  60. * @return TRUE if the address has been parsed, otherwise `target` content is undefined
  61. */
  62. gboolean rspamd_parse_inet_address_ip6 (const guchar *text, gsize len,
  63. gpointer target);
  64. /**
  65. * Parse string with ipv4 address of length `len` to `target` which should be
  66. * at least sizeof (in4_addr_t)
  67. * @param text input string
  68. * @param len lenth of `text` (if 0, then `text` must be zero terminated)
  69. * @param target target structure
  70. * @return TRUE if the address has been parsed, otherwise `target` content is undefined
  71. */
  72. gboolean rspamd_parse_inet_address_ip4 (const guchar *text, gsize len,
  73. gpointer target);
  74. /**
  75. * Try to parse address from string
  76. * @param target target to fill
  77. * @param src IP string representation
  78. * @return TRUE if addr has been parsed
  79. */
  80. gboolean rspamd_parse_inet_address (rspamd_inet_addr_t **target,
  81. const char *src,
  82. gsize srclen);
  83. /**
  84. * Returns string representation of inet address
  85. * @param addr
  86. * @return statically allocated string pointer (not thread safe)
  87. */
  88. const char * rspamd_inet_address_to_string (const rspamd_inet_addr_t *addr);
  89. /**
  90. * Returns port number for the specified inet address in host byte order
  91. * @param addr
  92. * @return
  93. */
  94. uint16_t rspamd_inet_address_get_port (const rspamd_inet_addr_t *addr);
  95. /**
  96. * Returns address family of inet address
  97. * @param addr
  98. * @return
  99. */
  100. gint rspamd_inet_address_get_af (const rspamd_inet_addr_t *addr);
  101. /**
  102. * Makes a radix key from inet address
  103. * @param addr
  104. * @param klen
  105. * @return
  106. */
  107. guchar * rspamd_inet_address_get_radix_key (const rspamd_inet_addr_t *addr, guint *klen);
  108. /**
  109. * Receive data from an unconnected socket and fill the inet_addr structure if needed
  110. * @param fd
  111. * @param buf
  112. * @param len
  113. * @param target
  114. * @return same as recvfrom(2)
  115. */
  116. gssize rspamd_inet_address_recvfrom (gint fd, void *buf, gsize len, gint fl,
  117. rspamd_inet_addr_t **target);
  118. /**
  119. * Send data via unconnected socket using the specified inet_addr structure
  120. * @param fd
  121. * @param buf
  122. * @param len
  123. * @param target
  124. * @return
  125. */
  126. gssize rspamd_inet_address_sendto (gint fd, const void *buf, gsize len, gint fl,
  127. const rspamd_inet_addr_t *addr);
  128. /**
  129. * Set port for inet address
  130. */
  131. void rspamd_inet_address_set_port (rspamd_inet_addr_t *addr, uint16_t port);
  132. /**
  133. * Connect to inet_addr address
  134. * @param addr
  135. * @param async perform operations asynchronously
  136. * @return newly created and connected socket
  137. */
  138. int rspamd_inet_address_connect (const rspamd_inet_addr_t *addr, gint type,
  139. gboolean async);
  140. /**
  141. * Listen on a specified inet address
  142. * @param addr
  143. * @param type
  144. * @param async
  145. * @return
  146. */
  147. int rspamd_inet_address_listen (const rspamd_inet_addr_t *addr, gint type,
  148. gboolean async);
  149. /**
  150. * Check whether specified ip is valid (not INADDR_ANY or INADDR_NONE) for ipv4 or ipv6
  151. * @param ptr pointer to struct in_addr or struct in6_addr
  152. * @param af address family (AF_INET or AF_INET6)
  153. * @return TRUE if the address is valid
  154. */
  155. gboolean rspamd_ip_is_valid (const rspamd_inet_addr_t *addr);
  156. /**
  157. * Accept from listening socket filling addr structure
  158. * @param sock listening socket
  159. * @param addr allocated inet addr structur
  160. * @return
  161. */
  162. gint rspamd_accept_from_socket (gint sock, rspamd_inet_addr_t **addr);
  163. /**
  164. * Parse host[:port[:priority]] line
  165. * @param ina host address
  166. * @param port port
  167. * @param priority priority
  168. * @return TRUE if string was parsed
  169. */
  170. gboolean rspamd_parse_host_port_priority (const gchar *str,
  171. GPtrArray **addrs,
  172. guint *priority, gchar **name, guint default_port,
  173. rspamd_mempool_t *pool);
  174. /**
  175. * Destroy the specified IP address
  176. * @param addr
  177. */
  178. void rspamd_inet_address_destroy (rspamd_inet_addr_t *addr);
  179. /**
  180. * Apply the specified mask to an address (ignored for AF_UNIX)
  181. * @param addr
  182. * @param mask
  183. */
  184. void rspamd_inet_address_apply_mask (rspamd_inet_addr_t *addr, guint mask);
  185. /**
  186. * Compare a1 and a2 and return value >0, ==0 and <0 if a1 is more, equal or less than a2 correspondingly
  187. * @param a1
  188. * @param a2
  189. * @return
  190. */
  191. gint rspamd_inet_address_compare (const rspamd_inet_addr_t *a1,
  192. const rspamd_inet_addr_t *a2);
  193. /**
  194. * Utility function to compare addresses by in g_ptr_array
  195. * @param a1
  196. * @param a2
  197. * @return
  198. */
  199. gint rspamd_inet_address_compare_ptr (gconstpointer a1,
  200. gconstpointer a2);
  201. /**
  202. * Performs deep copy of rspamd inet addr
  203. * @param addr
  204. * @return
  205. */
  206. rspamd_inet_addr_t *rspamd_inet_address_copy (const rspamd_inet_addr_t *addr);
  207. /**
  208. * Returns hash for inet address
  209. */
  210. guint rspamd_inet_address_hash (gconstpointer a);
  211. /**
  212. * Returns true if two address are equal
  213. */
  214. gboolean rspamd_inet_address_equal (gconstpointer a, gconstpointer b);
  215. /**
  216. * Returns TRUE if an address belongs to some local address
  217. */
  218. gboolean rspamd_inet_address_is_local (const rspamd_inet_addr_t *addr);
  219. #endif /* ADDR_H_ */