From d5e426ab51b7fec1f7411da2c10b08d651846aee Mon Sep 17 00:00:00 2001 From: Roger Dingledine Date: Fri, 26 Aug 2005 07:37:07 +0000 Subject: [PATCH] add a tor_dup_addr() function to simplify malloc()+tor_inet_ntoa() svn:r4838 --- src/common/compat.c | 1 + src/common/util.c | 14 ++++++++++++++ src/common/util.h | 1 + 3 files changed, 16 insertions(+) diff --git a/src/common/compat.c b/src/common/compat.c index b3c1b17235..596967248d 100644 --- a/src/common/compat.c +++ b/src/common/compat.c @@ -579,6 +579,7 @@ tor_lookup_hostname(const char *name, uint32_t *addr) * something. */ struct in_addr iaddr; + tor_assert(name); tor_assert(addr); if (!*name) { /* Empty address is an error. */ diff --git a/src/common/util.c b/src/common/util.c index acc5cb9d44..280f161d4c 100644 --- a/src/common/util.c +++ b/src/common/util.c @@ -1314,6 +1314,20 @@ tor_inet_ntoa(struct in_addr *in, char *buf, size_t buf_len) (int)(uint8_t)((a )&0xff)); } +/** Given a host-order addr, call tor_inet_ntoa() on it + * and return a strdup of the resulting address. + */ +char * +tor_dup_addr(uint32_t addr) +{ + char buf[INET_NTOA_BUF_LEN]; + struct in_addr in; + + in.s_addr = htonl(addr); + tor_inet_ntoa(&in, buf, sizeof(buf)); + return tor_strdup(buf); +} + /* Return true iff name looks like it might be a hostname or IP * address of some kind. */ int diff --git a/src/common/util.h b/src/common/util.h index 2197618c33..7dfce88992 100644 --- a/src/common/util.h +++ b/src/common/util.h @@ -134,6 +134,7 @@ int parse_addr_and_port_range(const char *s, uint32_t *addr_out, uint16_t *port_max_out); #define INET_NTOA_BUF_LEN 16 int tor_inet_ntoa(struct in_addr *in, char *buf, size_t buf_len); +char *tor_dup_addr(uint32_t addr); int is_plausible_address(const char *name); int get_interface_address(uint32_t *addr);