浏览代码

add a tor_dup_addr() function to simplify malloc()+tor_inet_ntoa()

svn:r4838
Roger Dingledine 20 年之前
父节点
当前提交
d5e426ab51
共有 3 个文件被更改,包括 16 次插入0 次删除
  1. 1 0
      src/common/compat.c
  2. 14 0
      src/common/util.c
  3. 1 0
      src/common/util.h

+ 1 - 0
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. */

+ 14 - 0
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 <b>addr</b>, 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 <b>name</b> looks like it might be a hostname or IP
  * address of some kind. */
 int

+ 1 - 0
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);