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 9.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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. #include "rdns.h"
  20. #ifdef HAVE_SYS_SOCKET_H
  21. #include <sys/socket.h>
  22. #endif
  23. #ifdef HAVE_NETINET_IN_H
  24. #include <netinet/in.h>
  25. #endif
  26. #ifdef HAVE_ARPA_INET_H
  27. #include <arpa/inet.h>
  28. #endif
  29. /* unix sockets */
  30. #ifdef HAVE_SYS_UN_H
  31. #include <sys/un.h>
  32. #endif
  33. #include "mem_pool.h"
  34. #ifdef __cplusplus
  35. extern "C" {
  36. #endif
  37. /**
  38. * Opaque structure
  39. */
  40. typedef struct rspamd_inet_addr_s rspamd_inet_addr_t;
  41. /**
  42. * Returns pointer storage for global singleton (map for local addresses)
  43. * @return
  44. */
  45. void **rspamd_inet_library_init(void);
  46. /**
  47. * Returns local addresses singleton
  48. * @return
  49. */
  50. void *rspamd_inet_library_get_lib_ctx(void);
  51. /**
  52. * Cleanup library (currently it does nothing)
  53. */
  54. void rspamd_inet_library_destroy(void);
  55. /**
  56. * Create new inet address structure based on the address family and opaque init pointer
  57. * @param af
  58. * @param init
  59. * @return new inet addr
  60. */
  61. rspamd_inet_addr_t *rspamd_inet_address_new(int af, const void *init);
  62. /**
  63. * Create new inet address structure from struct sockaddr
  64. * @param sa
  65. * @param slen
  66. * @return
  67. */
  68. rspamd_inet_addr_t *rspamd_inet_address_from_sa(const struct sockaddr *sa,
  69. socklen_t slen);
  70. /**
  71. * Create new inet address from rdns reply
  72. * @param rep reply element
  73. * @return new ipv4 or ipv6 addr (port is NOT set)
  74. */
  75. rspamd_inet_addr_t *rspamd_inet_address_from_rnds(
  76. const struct rdns_reply_entry *rep);
  77. /**
  78. * Parse string with ipv6 address of length `len` to `target` which should be
  79. * at least sizeof (struct in6_addr)
  80. * @param text input string
  81. * @param len length of `text` (if 0, then `text` must be zero terminated)
  82. * @param target target structure
  83. * @return TRUE if the address has been parsed, otherwise `target` content is undefined
  84. */
  85. gboolean rspamd_parse_inet_address_ip6(const unsigned char *text, gsize len,
  86. gpointer target);
  87. enum rspamd_inet_address_parse_flags {
  88. RSPAMD_INET_ADDRESS_PARSE_DEFAULT = 0,
  89. RSPAMD_INET_ADDRESS_PARSE_REMOTE = 1u << 0u,
  90. RSPAMD_INET_ADDRESS_PARSE_NO_UNIX = 1u << 1u,
  91. RSPAMD_INET_ADDRESS_PARSE_NO_PORT = 1u << 2u,
  92. };
  93. /**
  94. * Parse string with ipv4 address of length `len` to `target` which should be
  95. * at least sizeof (in4_addr_t)
  96. * @param text input string
  97. * @param len length of `text` (if 0, then `text` must be zero terminated)
  98. * @param target target structure
  99. * @return TRUE if the address has been parsed, otherwise `target` content is undefined
  100. */
  101. gboolean rspamd_parse_inet_address_ip4(const unsigned char *text, gsize len,
  102. gpointer target);
  103. /**
  104. * Parse ipv4 or ipv6 address to a static buffer `target`. Does not support Unix sockets
  105. * @param src
  106. * @param srclen
  107. * @param target
  108. * @return
  109. */
  110. gboolean rspamd_parse_inet_address_ip(const char *src,
  111. gsize srclen,
  112. rspamd_inet_addr_t *target);
  113. /**
  114. * Try to parse address from string
  115. * @param target target to fill
  116. * @param src IP string representation
  117. * @return TRUE if addr has been parsed
  118. */
  119. gboolean rspamd_parse_inet_address(rspamd_inet_addr_t **target,
  120. const char *src,
  121. gsize srclen,
  122. enum rspamd_inet_address_parse_flags how);
  123. /**
  124. * Use memory pool allocated inet address
  125. * @param src
  126. * @param srclen
  127. * @param pool
  128. * @return
  129. */
  130. rspamd_inet_addr_t *rspamd_parse_inet_address_pool(const char *src,
  131. gsize srclen,
  132. rspamd_mempool_t *pool,
  133. enum rspamd_inet_address_parse_flags how);
  134. /**
  135. * Returns string representation of inet address
  136. * @param addr
  137. * @return statically allocated string pointer (not thread safe)
  138. */
  139. const char *rspamd_inet_address_to_string(const rspamd_inet_addr_t *addr);
  140. /**
  141. * Returns pretty string representation of inet address
  142. * @param addr
  143. * @return statically allocated string pointer (not thread safe)
  144. */
  145. const char *rspamd_inet_address_to_string_pretty(const rspamd_inet_addr_t *addr);
  146. /**
  147. * Returns port number for the specified inet address in host byte order
  148. * @param addr
  149. * @return
  150. */
  151. uint16_t rspamd_inet_address_get_port(const rspamd_inet_addr_t *addr);
  152. /**
  153. * Returns address family of inet address
  154. * @param addr
  155. * @return
  156. */
  157. int rspamd_inet_address_get_af(const rspamd_inet_addr_t *addr);
  158. /**
  159. * Returns sockaddr and size for this address
  160. * @param addr
  161. * @param sz
  162. * @return
  163. */
  164. struct sockaddr *rspamd_inet_address_get_sa(const rspamd_inet_addr_t *addr,
  165. socklen_t *sz);
  166. /**
  167. * Makes a radix key from inet address
  168. * @param addr
  169. * @param klen
  170. * @return
  171. */
  172. unsigned char *rspamd_inet_address_get_hash_key(const rspamd_inet_addr_t *addr, unsigned int *klen);
  173. /**
  174. * Receive data from an unconnected socket and fill the inet_addr structure if needed
  175. * @param fd
  176. * @param buf
  177. * @param len
  178. * @param target
  179. * @return same as recvfrom(2)
  180. */
  181. gssize rspamd_inet_address_recvfrom(int fd, void *buf, gsize len, int fl,
  182. rspamd_inet_addr_t **target);
  183. /**
  184. * Send data via unconnected socket using the specified inet_addr structure
  185. * @param fd
  186. * @param buf
  187. * @param len
  188. * @param target
  189. * @return
  190. */
  191. gssize rspamd_inet_address_sendto(int fd, const void *buf, gsize len, int fl,
  192. const rspamd_inet_addr_t *addr);
  193. /**
  194. * Set port for inet address
  195. */
  196. void rspamd_inet_address_set_port(rspamd_inet_addr_t *addr, uint16_t port);
  197. /**
  198. * Connect to inet_addr address
  199. * @param addr
  200. * @param async perform operations asynchronously
  201. * @return newly created and connected socket
  202. */
  203. int rspamd_inet_address_connect(const rspamd_inet_addr_t *addr, int type,
  204. gboolean async);
  205. enum rspamd_inet_address_listen_opts {
  206. RSPAMD_INET_ADDRESS_LISTEN_DEFAULT = 0,
  207. RSPAMD_INET_ADDRESS_LISTEN_ASYNC = (1u << 0u),
  208. RSPAMD_INET_ADDRESS_LISTEN_REUSEPORT = (1u << 1u),
  209. RSPAMD_INET_ADDRESS_LISTEN_NOLISTEN = (1u << 2u),
  210. };
  211. /**
  212. * Listen on a specified inet address
  213. * @param addr
  214. * @param type
  215. * @param opts
  216. * @return
  217. */
  218. int rspamd_inet_address_listen(const rspamd_inet_addr_t *addr, int type,
  219. enum rspamd_inet_address_listen_opts opts,
  220. int listen_queue);
  221. /**
  222. * Check whether specified ip is valid (not INADDR_ANY or INADDR_NONE) for ipv4 or ipv6
  223. * @param ptr pointer to struct in_addr or struct in6_addr
  224. * @param af address family (AF_INET or AF_INET6)
  225. * @return TRUE if the address is valid
  226. */
  227. gboolean rspamd_ip_is_valid(const rspamd_inet_addr_t *addr);
  228. typedef void (*rspamd_accept_throttling_handler)(int, void *);
  229. /**
  230. * Accept from listening socket filling addr structure
  231. * @param sock listening socket
  232. * @param target allocated inet addr structure
  233. * @return
  234. */
  235. int rspamd_accept_from_socket(int sock,
  236. rspamd_inet_addr_t **target,
  237. rspamd_accept_throttling_handler hdl,
  238. void *hdl_data);
  239. enum rspamd_parse_host_port_result {
  240. RSPAMD_PARSE_ADDR_FAIL = 0,
  241. RSPAMD_PARSE_ADDR_RESOLVED = 1,
  242. RSPAMD_PARSE_ADDR_NUMERIC = 2,
  243. };
  244. /**
  245. * Parse host[:port[:priority]] line
  246. * @param ina host address
  247. * @param port port
  248. * @param priority priority
  249. * @return RSPAMD_PARSE_ADDR_FAIL in case of error, RSPAMD_PARSE_ADDR_NUMERIC in case of pure ip/unix socket
  250. */
  251. enum rspamd_parse_host_port_result
  252. rspamd_parse_host_port_priority(const char *str,
  253. GPtrArray **addrs,
  254. unsigned int *priority, char **name,
  255. unsigned int default_port,
  256. gboolean allow_listen,
  257. rspamd_mempool_t *pool);
  258. /**
  259. * Destroy the specified IP address
  260. * @param addr
  261. */
  262. void rspamd_inet_address_free(rspamd_inet_addr_t *addr);
  263. /**
  264. * Apply the specified mask to an address (ignored for AF_UNIX)
  265. * @param addr
  266. * @param mask
  267. */
  268. void rspamd_inet_address_apply_mask(rspamd_inet_addr_t *addr, unsigned int mask);
  269. /**
  270. * Compare a1 and a2 and return value >0, ==0 and <0 if a1 is more, equal or less than a2 correspondingly
  271. * @param a1
  272. * @param a2
  273. * @return
  274. */
  275. int rspamd_inet_address_compare(const rspamd_inet_addr_t *a1,
  276. const rspamd_inet_addr_t *a2, gboolean compare_ports);
  277. /**
  278. * Utility function to compare addresses by in g_ptr_array
  279. * @param a1
  280. * @param a2
  281. * @return
  282. */
  283. int rspamd_inet_address_compare_ptr(gconstpointer a1,
  284. gconstpointer a2);
  285. /**
  286. * Performs deep copy of rspamd inet addr
  287. * @param addr
  288. * @return
  289. */
  290. rspamd_inet_addr_t *rspamd_inet_address_copy(const rspamd_inet_addr_t *addr, rspamd_mempool_t *pool);
  291. /**
  292. * Returns hash for inet address (ignoring port)
  293. */
  294. unsigned int rspamd_inet_address_hash(gconstpointer a);
  295. unsigned int rspamd_inet_address_port_hash(gconstpointer a);
  296. /**
  297. * Returns true if two address are equal
  298. */
  299. gboolean rspamd_inet_address_equal(gconstpointer a, gconstpointer b);
  300. gboolean rspamd_inet_address_port_equal(gconstpointer a, gconstpointer b);
  301. /**
  302. * Returns TRUE if an address belongs to some local address
  303. */
  304. gboolean rspamd_inet_address_is_local(const rspamd_inet_addr_t *addr);
  305. /**
  306. * Returns size of storage required to store a complete IP address
  307. * @return
  308. */
  309. gsize rspamd_inet_address_storage_size(void);
  310. #ifdef __cplusplus
  311. }
  312. #endif
  313. #endif /* ADDR_H_ */