diff options
author | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2016-05-16 12:38:02 +0100 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2016-05-16 12:39:07 +0100 |
commit | 0c01103c021a7be7a62235aa3d14b5ca9ac46472 (patch) | |
tree | a05baffb7dae2450d67ad7bc15ca7f1745171be1 /src/libutil | |
parent | 5bf7ecf87bfe2687970493206e95b28773318c13 (diff) | |
download | rspamd-0c01103c021a7be7a62235aa3d14b5ca9ac46472.tar.gz rspamd-0c01103c021a7be7a62235aa3d14b5ca9ac46472.zip |
[Feature] Add function for pretty printing of inet addresses
Diffstat (limited to 'src/libutil')
-rw-r--r-- | src/libutil/addr.c | 30 | ||||
-rw-r--r-- | src/libutil/addr.h | 7 |
2 files changed, 37 insertions, 0 deletions
diff --git a/src/libutil/addr.c b/src/libutil/addr.c index 18414a98c..5c5a51e13 100644 --- a/src/libutil/addr.c +++ b/src/libutil/addr.c @@ -667,6 +667,36 @@ rspamd_inet_address_to_string (const rspamd_inet_addr_t *addr) return "undefined"; } +const char * +rspamd_inet_address_to_string_pretty (const rspamd_inet_addr_t *addr) +{ + static char addr_str[PATH_MAX + 5]; + + if (addr == NULL) { + return "<empty inet address>"; + } + + switch (addr->af) { + case AF_INET: + rspamd_snprintf (addr_str, sizeof (addr_str), "%s:%d", + rspamd_inet_address_to_string (addr), + rspamd_inet_address_get_port (addr)); + break; + case AF_INET6: + rspamd_snprintf (addr_str, sizeof (addr_str), "[%s]:%d", + rspamd_inet_address_to_string (addr), + rspamd_inet_address_get_port (addr)); + break; + case AF_UNIX: + rspamd_snprintf (addr_str, sizeof (addr_str), "unix:%s", + rspamd_inet_address_to_string (addr), + rspamd_inet_address_get_port (addr)); + break; + } + + return addr_str; +} + uint16_t rspamd_inet_address_get_port (const rspamd_inet_addr_t *addr) { diff --git a/src/libutil/addr.h b/src/libutil/addr.h index afed3f8fc..bb9fd2573 100644 --- a/src/libutil/addr.h +++ b/src/libutil/addr.h @@ -108,6 +108,13 @@ gboolean rspamd_parse_inet_address (rspamd_inet_addr_t **target, const char * rspamd_inet_address_to_string (const rspamd_inet_addr_t *addr); /** + * Returns pretty string representation of inet address + * @param addr + * @return statically allocated string pointer (not thread safe) + */ +const char * rspamd_inet_address_to_string_pretty (const rspamd_inet_addr_t *addr); + +/** * Returns port number for the specified inet address in host byte order * @param addr * @return |