aboutsummaryrefslogtreecommitdiffstats
path: root/src/libutil/addr.h
diff options
context:
space:
mode:
authorVsevolod Stakhov <vsevolod@highsecure.ru>2015-03-12 14:17:14 +0000
committerVsevolod Stakhov <vsevolod@highsecure.ru>2015-03-12 14:17:14 +0000
commita943f89d45c1ee9073d894aee7d28077187120b7 (patch)
tree534b1f25b8dec5b93237792352dd0628d4f76b63 /src/libutil/addr.h
parente80283ac81f3677ed70f084275f157781d0ebba1 (diff)
downloadrspamd-a943f89d45c1ee9073d894aee7d28077187120b7.tar.gz
rspamd-a943f89d45c1ee9073d894aee7d28077187120b7.zip
Rework inet_address structure.
- Now addresses are opaque. - Save memory by splitting unix and inet sockaddrs. - Rework all functions related to inet addrs. - Use ptr arrays instead of home-made static arrays.
Diffstat (limited to 'src/libutil/addr.h')
-rw-r--r--src/libutil/addr.h100
1 files changed, 70 insertions, 30 deletions
diff --git a/src/libutil/addr.h b/src/libutil/addr.h
index b5282e34c..d3778f6a7 100644
--- a/src/libutil/addr.h
+++ b/src/libutil/addr.h
@@ -27,25 +27,26 @@
#include "mem_pool.h"
/**
- * Union that is used for storing sockaddrs
- */
-union sa_union {
- struct sockaddr_storage ss;
- struct sockaddr sa;
- struct sockaddr_in s4;
- struct sockaddr_in6 s6;
- struct sockaddr_un su;
-};
-
-typedef struct _rspamd_inet_addr_s {
- union sa_union addr;
- socklen_t slen;
- gint af;
- /* Unix socket specific */
- gint mode;
- uid_t owner;
- gid_t group;
-} rspamd_inet_addr_t;
+ * Opaque structure
+ */
+typedef struct rspamd_inet_addr_s rspamd_inet_addr_t;
+
+/**
+ * Create new inet address structure based on the address familiy and opaque init pointer
+ * @param af
+ * @param init
+ * @return new inet addr
+ */
+rspamd_inet_addr_t * rspamd_inet_address_new (int af, const void *init);
+
+/**
+ * Create new inet address structure from struct sockaddr
+ * @param sa
+ * @param slen
+ * @return
+ */
+rspamd_inet_addr_t * rspamd_inet_address_from_sa (const struct sockaddr *sa,
+ socklen_t slen);
/**
* Try to parse address from string
@@ -53,7 +54,7 @@ typedef struct _rspamd_inet_addr_s {
* @param src IP string representation
* @return TRUE if addr has been parsed
*/
-gboolean rspamd_parse_inet_address (rspamd_inet_addr_t *target,
+gboolean rspamd_parse_inet_address (rspamd_inet_addr_t **target,
const char *src);
/**
@@ -61,14 +62,52 @@ gboolean rspamd_parse_inet_address (rspamd_inet_addr_t *target,
* @param addr
* @return statically allocated string pointer (not thread safe)
*/
-const char * rspamd_inet_address_to_string (rspamd_inet_addr_t *addr);
+const char * rspamd_inet_address_to_string (const rspamd_inet_addr_t *addr);
/**
* Returns port number for the specified inet address in host byte order
* @param addr
* @return
*/
-uint16_t rspamd_inet_address_get_port (rspamd_inet_addr_t *addr);
+uint16_t rspamd_inet_address_get_port (const rspamd_inet_addr_t *addr);
+
+/**
+ * Returns address family of inet address
+ * @param addr
+ * @return
+ */
+gint rspamd_inet_address_get_af (const rspamd_inet_addr_t *addr);
+
+
+/**
+ * Makes a radix key from inet address
+ * @param addr
+ * @param klen
+ * @return
+ */
+guchar * rspamd_inet_address_get_radix_key (const rspamd_inet_addr_t *addr, guint *klen);
+
+/**
+ * Receive data from an unconnected socket and fill the inet_addr structure if needed
+ * @param fd
+ * @param buf
+ * @param len
+ * @param target
+ * @return same as recvfrom(2)
+ */
+gssize rspamd_inet_address_recvfrom (gint fd, void *buf, gsize len, gint fl,
+ rspamd_inet_addr_t **target);
+
+/**
+ * Send data via unconnected socket using the specified inet_addr structure
+ * @param fd
+ * @param buf
+ * @param len
+ * @param target
+ * @return
+ */
+gssize rspamd_inet_address_sendto (gint fd, const void *buf, gsize len, gint fl,
+ const rspamd_inet_addr_t *addr);
/**
* Set port for inet address
@@ -81,7 +120,7 @@ void rspamd_inet_address_set_port (rspamd_inet_addr_t *addr, uint16_t port);
* @param async perform operations asynchronously
* @return newly created and connected socket
*/
-int rspamd_inet_address_connect (rspamd_inet_addr_t *addr, gint type,
+int rspamd_inet_address_connect (const rspamd_inet_addr_t *addr, gint type,
gboolean async);
/**
@@ -91,7 +130,7 @@ int rspamd_inet_address_connect (rspamd_inet_addr_t *addr, gint type,
* @param async
* @return
*/
-int rspamd_inet_address_listen (rspamd_inet_addr_t *addr, gint type,
+int rspamd_inet_address_listen (const rspamd_inet_addr_t *addr, gint type,
gboolean async);
/**
* Check whether specified ip is valid (not INADDR_ANY or INADDR_NONE) for ipv4 or ipv6
@@ -99,18 +138,18 @@ int rspamd_inet_address_listen (rspamd_inet_addr_t *addr, gint type,
* @param af address family (AF_INET or AF_INET6)
* @return TRUE if the address is valid
*/
-gboolean rspamd_ip_is_valid (rspamd_inet_addr_t *addr);
+gboolean rspamd_ip_is_valid (const rspamd_inet_addr_t *addr);
/**
* Accept from listening socket filling addr structure
* @param sock listening socket
- * @param addr
+ * @param addr allocated inet addr structur
* @return
*/
-gint rspamd_accept_from_socket (gint sock, rspamd_inet_addr_t *addr);
+gint rspamd_accept_from_socket (gint sock, rspamd_inet_addr_t **addr);
gboolean rspamd_parse_host_port_priority_strv (gchar **tokens,
- rspamd_inet_addr_t **addr, guint *max_addrs, guint *priority,
+ GPtrArray **addrs, guint *priority,
gchar **name, guint default_port, rspamd_mempool_t *pool);
/**
@@ -121,7 +160,7 @@ gboolean rspamd_parse_host_port_priority_strv (gchar **tokens,
* @return TRUE if string was parsed
*/
gboolean rspamd_parse_host_port_priority (const gchar *str,
- rspamd_inet_addr_t **addr, guint *max_addrs,
+ GPtrArray **addrs,
guint *priority, gchar **name, guint default_port,
rspamd_mempool_t *pool);
@@ -132,8 +171,9 @@ gboolean rspamd_parse_host_port_priority (const gchar *str,
* @return TRUE if string was parsed
*/
gboolean rspamd_parse_host_port (const gchar *str,
- rspamd_inet_addr_t **addr, guint *max_addrs,
+ GPtrArray **addrs,
gchar **name, guint default_port, rspamd_mempool_t *pool);
+void rspamd_inet_address_destroy (rspamd_inet_addr_t *addr);
#endif /* ADDR_H_ */