commit 98e84212f96bcda222e61dd8ad7bce98f08717c1 Author: David Goulet dgoulet@ev0ke.net Date: Fri Feb 24 12:33:32 2017 -0500
utils: Add useful function for later use
Signed-off-by: David Goulet dgoulet@ev0ke.net --- src/common/utils.c | 28 ++++++++++++++++++++++++++++ src/common/utils.h | 7 ++++--- 2 files changed, 32 insertions(+), 3 deletions(-)
diff --git a/src/common/utils.c b/src/common/utils.c index 8fe9c6e..f02b1b4 100644 --- a/src/common/utils.c +++ b/src/common/utils.c @@ -366,3 +366,31 @@ int utils_get_port_from_addr(const struct sockaddr *sa)
return port; } + +/* + * For a given sockaddr, return a const pointer to the address data structure. + * Return NULL if family is not IPv4 or IPv6. + */ +ATTR_HIDDEN +const char *utils_get_addr_from_sockaddr(const struct sockaddr *sa) +{ + static char buf[256]; + const void *addrp; + + assert(sa); + + memset(buf, 0, sizeof(buf)); + + if (sa->sa_family == AF_INET) { + addrp = &((const struct sockaddr_in *) sa)->sin_addr; + } else if (sa->sa_family == AF_INET6) { + addrp = &((const struct sockaddr_in6 *) sa)->sin6_addr; + } else { + goto end; + } + + inet_ntop(sa->sa_family, addrp, buf, sizeof(buf)); + +end: + return buf; +} diff --git a/src/common/utils.h b/src/common/utils.h index 83ea825..74f4f5e 100644 --- a/src/common/utils.h +++ b/src/common/utils.h @@ -1,7 +1,7 @@ /* - * Copyright (C) 2000-2008 - Shaun Clowes delius@progsoc.org - * 2008-2011 - Robert Hogan robert@roberthogan.net - * 2013 - David Goulet dgoulet@ev0ke.net + * Copyright (C) 2000-2008 - Shaun Clowes delius@progsoc.org + * 2008-2011 - Robert Hogan robert@roberthogan.net + * 2013 - David Goulet dgoulet@ev0ke.net * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License, version 2 only, as @@ -34,5 +34,6 @@ int utils_sockaddr_is_localhost(const struct sockaddr *sa); int utils_localhost_resolve(const char *name, int af, void *buf, size_t len); int utils_is_addr_any(const struct sockaddr *sa); int utils_get_port_from_addr(const struct sockaddr *sa); +const char *utils_get_addr_from_sockaddr(const struct sockaddr *sa);
#endif /* TORSOCKS_UTILS_H */
tor-commits@lists.torproject.org