]> git.ipfire.org Git - thirdparty/tor.git/commitdiff
Merge remote-tracking branch 'bug28525_029' into maint-0.3.5
authorteor <teor@torproject.org>
Fri, 8 Mar 2019 02:32:04 +0000 (12:32 +1000)
committerteor <teor@torproject.org>
Fri, 8 Mar 2019 02:33:00 +0000 (12:33 +1000)
1  2 
src/lib/net/address.c
src/test/test_addr.c

index 28c8e3f50fc55ee2d817890e2c7f021543f5c207,71d380538685874532bdc091de4caf78f4e59e1e..a2d234b742130cc2686f3587a15bee346762dd4e
@@@ -236,9 -227,139 +236,18 @@@ tor_addr_make_null(tor_addr_t *a, sa_fa
    a->family = family;
  }
  
- /** Return true iff <b>ip</b> is an IP reserved to localhost or local networks
-  * in RFC1918 or RFC4193 or RFC4291. (fec0::/10, deprecated by RFC3879, is
-  * also treated as internal for now.)
 -/** Similar behavior to Unix gethostbyname: resolve <b>name</b>, and set
 - * *<b>addr</b> to the proper IP address and family. The <b>family</b>
 - * argument (which must be AF_INET, AF_INET6, or AF_UNSPEC) declares a
 - * <i>preferred</i> family, though another one may be returned if only one
 - * family is implemented for this address.
 - *
 - * Return 0 on success, -1 on failure; 1 on transient failure.
 - */
 -int
 -tor_addr_lookup(const char *name, uint16_t family, tor_addr_t *addr)
 -{
 -  /* Perhaps eventually this should be replaced by a tor_getaddrinfo or
 -   * something.
 -   */
 -  struct in_addr iaddr;
 -  struct in6_addr iaddr6;
 -  tor_assert(name);
 -  tor_assert(addr);
 -  tor_assert(family == AF_INET || family == AF_INET6 || family == AF_UNSPEC);
 -  if (!*name) {
 -    /* Empty address is an error. */
 -    return -1;
 -  } else if (tor_inet_pton(AF_INET, name, &iaddr)) {
 -    /* It's an IPv4 IP. */
 -    if (family == AF_INET6)
 -      return -1;
 -    tor_addr_from_in(addr, &iaddr);
 -    return 0;
 -  } else if (tor_inet_pton(AF_INET6, name, &iaddr6)) {
 -    if (family == AF_INET)
 -      return -1;
 -    tor_addr_from_in6(addr, &iaddr6);
 -    return 0;
 -  } else {
 -#ifdef HAVE_GETADDRINFO
 -    int err;
 -    struct addrinfo *res=NULL, *res_p;
 -    struct addrinfo *best=NULL;
 -    struct addrinfo hints;
 -    int result = -1;
 -    memset(&hints, 0, sizeof(hints));
 -    hints.ai_family = family;
 -    hints.ai_socktype = SOCK_STREAM;
 -    err = sandbox_getaddrinfo(name, NULL, &hints, &res);
 -    /* The check for 'res' here shouldn't be necessary, but it makes static
 -     * analysis tools happy. */
 -    if (!err && res) {
 -      best = NULL;
 -      for (res_p = res; res_p; res_p = res_p->ai_next) {
 -        if (family == AF_UNSPEC) {
 -          if (res_p->ai_family == AF_INET) {
 -            best = res_p;
 -            break;
 -          } else if (res_p->ai_family == AF_INET6 && !best) {
 -            best = res_p;
 -          }
 -        } else if (family == res_p->ai_family) {
 -          best = res_p;
 -          break;
 -        }
 -      }
 -      if (!best)
 -        best = res;
 -      if (best->ai_family == AF_INET) {
 -        tor_addr_from_in(addr,
 -                         &((struct sockaddr_in*)best->ai_addr)->sin_addr);
 -        result = 0;
 -      } else if (best->ai_family == AF_INET6) {
 -        tor_addr_from_in6(addr,
 -                          &((struct sockaddr_in6*)best->ai_addr)->sin6_addr);
 -        result = 0;
 -      }
 -      sandbox_freeaddrinfo(res);
 -      return result;
 -    }
 -    return (err == EAI_AGAIN) ? 1 : -1;
 -#else
 -    struct hostent *ent;
 -    int err;
 -#ifdef HAVE_GETHOSTBYNAME_R_6_ARG
 -    char buf[2048];
 -    struct hostent hostent;
 -    int r;
 -    r = gethostbyname_r(name, &hostent, buf, sizeof(buf), &ent, &err);
 -#elif defined(HAVE_GETHOSTBYNAME_R_5_ARG)
 -    char buf[2048];
 -    struct hostent hostent;
 -    ent = gethostbyname_r(name, &hostent, buf, sizeof(buf), &err);
 -#elif defined(HAVE_GETHOSTBYNAME_R_3_ARG)
 -    struct hostent_data data;
 -    struct hostent hent;
 -    memset(&data, 0, sizeof(data));
 -    err = gethostbyname_r(name, &hent, &data);
 -    ent = err ? NULL : &hent;
 -#else
 -    ent = gethostbyname(name);
 -#ifdef _WIN32
 -    err = WSAGetLastError();
 -#else
 -    err = h_errno;
 -#endif
 -#endif /* endif HAVE_GETHOSTBYNAME_R_6_ARG. */
 -    if (ent) {
 -      if (ent->h_addrtype == AF_INET) {
 -        tor_addr_from_in(addr, (struct in_addr*) ent->h_addr);
 -      } else if (ent->h_addrtype == AF_INET6) {
 -        tor_addr_from_in6(addr, (struct in6_addr*) ent->h_addr);
 -      } else {
 -        tor_assert(0); // LCOV_EXCL_LINE: gethostbyname() returned bizarre type
 -      }
 -      return 0;
 -    }
 -#ifdef _WIN32
 -    return (err == WSATRY_AGAIN) ? 1 : -1;
 -#else
 -    return (err == TRY_AGAIN) ? 1 : -1;
 -#endif
 -#endif
 -  }
 -}
 -
+ /** Return true iff <b>ip</b> is an IP reserved to localhost or local networks.
+  *
+  * If <b>ip</b> is in RFC1918 or RFC4193 or RFC4291, we will return true.
+  * (fec0::/10, deprecated by RFC3879, is also treated as internal for now
+  * and will return true.)
+  *
+  * If <b>ip</b> is 0.0.0.0 or 100.64.0.0/10 (RFC6598), we will act as:
+  *  - Internal if <b>for_listening</b> is 0, as these addresses are not
+  *    routable on the internet and we won't be publicly accessible to clients.
+  *  - External if <b>for_listening</b> is 1, as clients could connect to us
+  *    from the internet (in the case of 0.0.0.0) or a service provider's
+  *    internal network (in the case of RFC6598).
   */
  int
  tor_addr_is_internal_(const tor_addr_t *addr, int for_listening,
index 985f43b3fa65673170893677b0fecabc3629bc62,5c4b6449cda0440a5f32c8f339caf54c50d1f043..8868edce25e35e6fb55b442774786845cedbcf1c
@@@ -1203,5 -1093,7 +1220,6 @@@ struct testcase_t addr_tests[] = 
    { "sockaddr_to_str", test_addr_sockaddr_to_str, 0, NULL, NULL },
    { "is_loopback", test_addr_is_loopback, 0, NULL, NULL },
    { "make_null", test_addr_make_null, 0, NULL, NULL },
+   { "rfc6598", test_addr_rfc6598, 0, NULL, NULL },
    END_OF_TESTCASES
  };
 -