浏览代码

Don't use gethostbyname() in resolve_my_address()

Tor has tor_lookup_hostname(), which prefers ipv4 addresses automatically.
Bug 1244 occured because gethostbyname() returned an ipv6 address, which
Tor cannot handle currently. Fixes bug 1244; bugfix on 0.0.2pre25.
Reported by Mike Mestnik.
Sebastian Hahn 14 年之前
父节点
当前提交
a168cd2a54
共有 2 个文件被更改,包括 6 次插入5 次删除
  1. 3 0
      ChangeLog
  2. 3 5
      src/or/config.c

+ 3 - 0
ChangeLog

@@ -20,6 +20,9 @@ Changes in version 0.2.1.23 - 2010-0?-??
       on, detect the OpenSSL version at run-time, not compile time.  We
       need to do this because Apple doesn't update its dev-tools headers
       when it updates its libraries in a security patch.
+    - Refactor resolve_my_address() a little, to not use gethostbyname()
+      anymore. Fixes bug 1244; bugfix on 0.0.2pre25. Reported by Mike
+      Mestnik.
 
   o Minor features:
     - Avoid a mad rush at the beginning of each month when each client

+ 3 - 5
src/or/config.c

@@ -2339,7 +2339,7 @@ resolve_my_address(int warn_severity, or_options_t *options,
                    uint32_t *addr_out, char **hostname_out)
 {
   struct in_addr in;
-  struct hostent *rent;
+  uint32_t addr;
   char hostname[256];
   int explicit_ip=1;
   int explicit_hostname=1;
@@ -2369,8 +2369,7 @@ resolve_my_address(int warn_severity, or_options_t *options,
   if (tor_inet_aton(hostname, &in) == 0) {
     /* then we have to resolve it */
     explicit_ip = 0;
-    rent = (struct hostent *)gethostbyname(hostname);
-    if (!rent) {
+    if(!tor_lookup_hostname(hostname, &addr)) {
       uint32_t interface_ip;
 
       if (explicit_hostname) {
@@ -2393,8 +2392,7 @@ resolve_my_address(int warn_severity, or_options_t *options,
              "local interface. Using that.", tmpbuf);
       strlcpy(hostname, "<guessed from interfaces>", sizeof(hostname));
     } else {
-      tor_assert(rent->h_length == 4);
-      memcpy(&in.s_addr, rent->h_addr, rent->h_length);
+      in.s_addr = htonl(addr);
 
       if (!explicit_hostname &&
           is_internal_IP(ntohl(in.s_addr), 0)) {