Commit a7cf7887 authored by Nick Mathewson's avatar Nick Mathewson 🦀
Browse files

Merge branch 'bug1992_part1'

parents d6bd2e55 b97da61b
Loading
Loading
Loading
Loading
+12 −0
Original line number Diff line number Diff line
@@ -925,6 +925,18 @@ fmt_addr(const tor_addr_t *addr)
  return buf;
}

/** Like fmt_addr(), but for IPv4 addresses. Also not thread-safe, also
 * clobbers its return buffer on repeated calls. */
const char *
fmt_addr32(uint32_t addr)
{
  static char buf[INET_NTOA_BUF_LEN];
  struct in_addr in;
  in.s_addr = htonl(addr);
  tor_inet_ntoa(&in, buf, sizeof(buf));
  return buf;
}

/** Convert the string in <b>src</b> to a tor_addr_t <b>addr</b>.  The string
 * may be an IPv4 address, an IPv6 address, or an IPv6 address surrounded by
 * square brackets.
+1 −0
Original line number Diff line number Diff line
@@ -108,6 +108,7 @@ tor_addr_eq_ipv4h(const tor_addr_t *a, uint32_t u)
int tor_addr_lookup(const char *name, uint16_t family, tor_addr_t *addr_out);
char *tor_dup_addr(const tor_addr_t *addr) ATTR_MALLOC;
const char *fmt_addr(const tor_addr_t *addr);
const char * fmt_addr32(uint32_t addr);
int get_interface_address6(int severity, sa_family_t family, tor_addr_t *addr);

/** Flag to specify how to do a comparison between addresses.  In an "exact"
+20 −26
Original line number Diff line number Diff line
@@ -2341,7 +2341,7 @@ resolve_my_address(int warn_severity, or_options_t *options,
  int explicit_ip=1;
  int explicit_hostname=1;
  int from_interface=0;
  char tmpbuf[INET_NTOA_BUF_LEN];
  char *addr_string = NULL;
  const char *address = options->Address;
  int notice_severity = warn_severity <= LOG_NOTICE ?
                          LOG_NOTICE : warn_severity;
@@ -2383,49 +2383,40 @@ resolve_my_address(int warn_severity, or_options_t *options,
        return -1;
      }
      from_interface = 1;
      in.s_addr = htonl(interface_ip);
      tor_inet_ntoa(&in,tmpbuf,sizeof(tmpbuf));
      addr = interface_ip;
      log_fn(notice_severity, LD_CONFIG, "Learned IP address '%s' for "
             "local interface. Using that.", tmpbuf);
             "local interface. Using that.", fmt_addr32(addr));
      strlcpy(hostname, "<guessed from interfaces>", sizeof(hostname));
    } else { /* resolved hostname into addr */
      in.s_addr = htonl(addr);

      if (!explicit_hostname &&
          is_internal_IP(ntohl(in.s_addr), 0)) {
          is_internal_IP(addr, 0)) {
        uint32_t interface_ip;

        tor_inet_ntoa(&in,tmpbuf,sizeof(tmpbuf));
        log_fn(notice_severity, LD_CONFIG, "Guessed local hostname '%s' "
               "resolves to a private IP address (%s). Trying something "
               "else.", hostname, tmpbuf);
               "else.", hostname, fmt_addr32(addr));

        if (get_interface_address(warn_severity, &interface_ip)) {
          log_fn(warn_severity, LD_CONFIG,
                 "Could not get local interface IP address. Too bad.");
        } else if (is_internal_IP(interface_ip, 0)) {
          struct in_addr in2;
          in2.s_addr = htonl(interface_ip);
          tor_inet_ntoa(&in2,tmpbuf,sizeof(tmpbuf));
          log_fn(notice_severity, LD_CONFIG,
                 "Interface IP address '%s' is a private address too. "
                 "Ignoring.", tmpbuf);
                 "Ignoring.", fmt_addr32(interface_ip));
        } else {
          from_interface = 1;
          in.s_addr = htonl(interface_ip);
          tor_inet_ntoa(&in,tmpbuf,sizeof(tmpbuf));
          addr = interface_ip;
          log_fn(notice_severity, LD_CONFIG,
                 "Learned IP address '%s' for local interface."
                 " Using that.", tmpbuf);
                 " Using that.", fmt_addr32(addr));
          strlcpy(hostname, "<guessed from interfaces>", sizeof(hostname));
        }
      }
    }
  }

  tor_inet_ntoa(&in,tmpbuf,sizeof(tmpbuf));
  if (is_internal_IP(ntohl(in.s_addr), 0) &&
      options->_PublishServerDescriptor) {
  addr_string = tor_dup_ip(addr);
  if (is_internal_IP(addr, 0) && options->_PublishServerDescriptor) {
    /* make sure we're ok with publishing an internal IP */
    if (!options->DirServers && !options->AlternateDirAuthority) {
      /* if they are using the default dirservers, disallow internal IPs
@@ -2433,7 +2424,8 @@ resolve_my_address(int warn_severity, or_options_t *options,
      log_fn(warn_severity, LD_CONFIG,
             "Address '%s' resolves to private IP address '%s'. "
             "Tor servers that use the default DirServers must have public "
             "IP addresses.", hostname, tmpbuf);
             "IP addresses.", hostname, addr_string);
      tor_free(addr_string);
      return -1;
    }
    if (!explicit_ip) {
@@ -2441,19 +2433,20 @@ resolve_my_address(int warn_severity, or_options_t *options,
       * they're using an internal address. */
      log_fn(warn_severity, LD_CONFIG, "Address '%s' resolves to private "
             "IP address '%s'. Please set the Address config option to be "
             "the IP address you want to use.", hostname, tmpbuf);
             "the IP address you want to use.", hostname, addr_string);
      tor_free(addr_string);
      return -1;
    }
  }

  log_debug(LD_CONFIG, "Resolved Address to '%s'.", tmpbuf);
  *addr_out = ntohl(in.s_addr);
  log_debug(LD_CONFIG, "Resolved Address to '%s'.", fmt_addr32(addr));
  *addr_out = addr;
  if (last_resolved_addr && last_resolved_addr != *addr_out) {
    /* Leave this as a notice, regardless of the requested severity,
     * at least until dynamic IP address support becomes bulletproof. */
    log_notice(LD_NET,
               "Your IP address seems to have changed to %s. Updating.",
               tmpbuf);
               addr_string);
    ip_address_changed(0);
  }
  if (last_resolved_addr != *addr_out) {
@@ -2472,11 +2465,12 @@ resolve_my_address(int warn_severity, or_options_t *options,
    }
    control_event_server_status(LOG_NOTICE,
                                "EXTERNAL_ADDRESS ADDRESS=%s METHOD=%s %s%s",
                                tmpbuf, method, h?"HOSTNAME=":"", h);
                                addr_string, method, h?"HOSTNAME=":"", h);
  }
  last_resolved_addr = *addr_out;
  if (hostname_out)
    *hostname_out = tor_strdup(hostname);
  tor_free(addr_string);
  return 0;
}

+5 −10
Original line number Diff line number Diff line
@@ -1185,7 +1185,6 @@ static char *
addressmap_get_virtual_address(int type)
{
  char buf[64];
  struct in_addr in;
  tor_assert(addressmap);

  if (type == RESOLVED_TYPE_HOSTNAME) {
@@ -1206,9 +1205,7 @@ addressmap_get_virtual_address(int type)
             (next_virtual_addr & 0xff) == 0xff) {
        ++next_virtual_addr;
      }
      in.s_addr = htonl(next_virtual_addr);
      tor_inet_ntoa(&in, buf, sizeof(buf));
      if (!strmap_get(addressmap, buf)) {
      if (!strmap_get(addressmap, fmt_addr32(next_virtual_addr))) {
        ++next_virtual_addr;
        break;
      }
@@ -2298,12 +2295,10 @@ tell_controller_about_resolved_result(edge_connection_t *conn,
                   answer_type == RESOLVED_TYPE_HOSTNAME)) {
    return; /* we already told the controller. */
  } else if (answer_type == RESOLVED_TYPE_IPV4 && answer_len >= 4) {
    struct in_addr in;
    char buf[INET_NTOA_BUF_LEN];
    in.s_addr = get_uint32(answer);
    tor_inet_ntoa(&in, buf, sizeof(buf));
    char *cp = tor_dup_ip(get_uint32(answer));
    control_event_address_mapped(conn->socks_request->address,
                                 buf, expires, NULL);
                                 cp, expires, NULL);
    tor_free(cp);
  } else if (answer_type == RESOLVED_TYPE_HOSTNAME && answer_len < 256) {
    char *cp = tor_strndup(answer, answer_len);
    control_event_address_mapped(conn->socks_request->address,
+2 −10
Original line number Diff line number Diff line
@@ -1985,20 +1985,16 @@ routerstatus_format_entry(char *buf, size_t buf_len,
                          routerstatus_format_type_t format)
{
  int r;
  struct in_addr in;
  char *cp;
  char *summary;

  char published[ISO_TIME_LEN+1];
  char ipaddr[INET_NTOA_BUF_LEN];
  char identity64[BASE64_DIGEST_LEN+1];
  char digest64[BASE64_DIGEST_LEN+1];

  format_iso_time(published, rs->published_on);
  digest_to_base64(identity64, rs->identity_digest);
  digest_to_base64(digest64, rs->descriptor_digest);
  in.s_addr = htonl(rs->addr);
  tor_inet_ntoa(&in, ipaddr, sizeof(ipaddr));

  r = tor_snprintf(buf, buf_len,
                   "r %s %s %s%s%s %s %d %d\n",
@@ -2007,7 +2003,7 @@ routerstatus_format_entry(char *buf, size_t buf_len,
                   (format==NS_V3_CONSENSUS_MICRODESC)?"":digest64,
                   (format==NS_V3_CONSENSUS_MICRODESC)?"":" ",
                   published,
                   ipaddr,
                   fmt_addr32(rs->addr),
                   (int)rs->or_port,
                   (int)rs->dir_port);
  if (r<0) {
@@ -2722,10 +2718,8 @@ generate_v2_networkstatus_opinion(void)
  char *outp, *endp;
  or_options_t *options = get_options();
  char fingerprint[FINGERPRINT_LEN+1];
  char ipaddr[INET_NTOA_BUF_LEN];
  char published[ISO_TIME_LEN+1];
  char digest[DIGEST_LEN];
  struct in_addr in;
  uint32_t addr;
  crypto_pk_env_t *private_key;
  routerlist_t *rl = router_get_routerlist();
@@ -2746,8 +2740,6 @@ generate_v2_networkstatus_opinion(void)
    log_warn(LD_NET, "Couldn't resolve my hostname");
    goto done;
  }
  in.s_addr = htonl(addr);
  tor_inet_ntoa(&in, ipaddr, sizeof(ipaddr));

  format_iso_time(published, now);

@@ -2793,7 +2785,7 @@ generate_v2_networkstatus_opinion(void)
               "dir-options%s%s%s%s\n"
               "%s" /* client version line, server version line. */
               "dir-signing-key\n%s",
               hostname, ipaddr, (int)options->DirPort,
               hostname, fmt_addr32(addr), (int)options->DirPort,
               fingerprint,
               contact,
               published,
Loading