Merge branch 'bug1992_part1'

This commit is contained in:
Nick Mathewson 2010-10-15 17:08:18 -04:00
commit a7cf788740
8 changed files with 48 additions and 72 deletions

View File

@ -925,6 +925,18 @@ fmt_addr(const tor_addr_t *addr)
return buf; 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 /** 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 * may be an IPv4 address, an IPv6 address, or an IPv6 address surrounded by
* square brackets. * square brackets.

View File

@ -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); 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; char *tor_dup_addr(const tor_addr_t *addr) ATTR_MALLOC;
const char *fmt_addr(const tor_addr_t *addr); 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); 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" /** Flag to specify how to do a comparison between addresses. In an "exact"

View File

@ -2341,7 +2341,7 @@ resolve_my_address(int warn_severity, or_options_t *options,
int explicit_ip=1; int explicit_ip=1;
int explicit_hostname=1; int explicit_hostname=1;
int from_interface=0; int from_interface=0;
char tmpbuf[INET_NTOA_BUF_LEN]; char *addr_string = NULL;
const char *address = options->Address; const char *address = options->Address;
int notice_severity = warn_severity <= LOG_NOTICE ? int notice_severity = warn_severity <= LOG_NOTICE ?
LOG_NOTICE : warn_severity; LOG_NOTICE : warn_severity;
@ -2383,49 +2383,40 @@ resolve_my_address(int warn_severity, or_options_t *options,
return -1; return -1;
} }
from_interface = 1; from_interface = 1;
in.s_addr = htonl(interface_ip); addr = interface_ip;
tor_inet_ntoa(&in,tmpbuf,sizeof(tmpbuf));
log_fn(notice_severity, LD_CONFIG, "Learned IP address '%s' for " 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)); strlcpy(hostname, "<guessed from interfaces>", sizeof(hostname));
} else { /* resolved hostname into addr */ } else { /* resolved hostname into addr */
in.s_addr = htonl(addr);
if (!explicit_hostname && if (!explicit_hostname &&
is_internal_IP(ntohl(in.s_addr), 0)) { is_internal_IP(addr, 0)) {
uint32_t interface_ip; uint32_t interface_ip;
tor_inet_ntoa(&in,tmpbuf,sizeof(tmpbuf));
log_fn(notice_severity, LD_CONFIG, "Guessed local hostname '%s' " log_fn(notice_severity, LD_CONFIG, "Guessed local hostname '%s' "
"resolves to a private IP address (%s). Trying something " "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)) { if (get_interface_address(warn_severity, &interface_ip)) {
log_fn(warn_severity, LD_CONFIG, log_fn(warn_severity, LD_CONFIG,
"Could not get local interface IP address. Too bad."); "Could not get local interface IP address. Too bad.");
} else if (is_internal_IP(interface_ip, 0)) { } 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, log_fn(notice_severity, LD_CONFIG,
"Interface IP address '%s' is a private address too. " "Interface IP address '%s' is a private address too. "
"Ignoring.", tmpbuf); "Ignoring.", fmt_addr32(interface_ip));
} else { } else {
from_interface = 1; from_interface = 1;
in.s_addr = htonl(interface_ip); addr = interface_ip;
tor_inet_ntoa(&in,tmpbuf,sizeof(tmpbuf));
log_fn(notice_severity, LD_CONFIG, log_fn(notice_severity, LD_CONFIG,
"Learned IP address '%s' for local interface." "Learned IP address '%s' for local interface."
" Using that.", tmpbuf); " Using that.", fmt_addr32(addr));
strlcpy(hostname, "<guessed from interfaces>", sizeof(hostname)); strlcpy(hostname, "<guessed from interfaces>", sizeof(hostname));
} }
} }
} }
} }
tor_inet_ntoa(&in,tmpbuf,sizeof(tmpbuf)); addr_string = tor_dup_ip(addr);
if (is_internal_IP(ntohl(in.s_addr), 0) && if (is_internal_IP(addr, 0) && options->_PublishServerDescriptor) {
options->_PublishServerDescriptor) {
/* make sure we're ok with publishing an internal IP */ /* make sure we're ok with publishing an internal IP */
if (!options->DirServers && !options->AlternateDirAuthority) { if (!options->DirServers && !options->AlternateDirAuthority) {
/* if they are using the default dirservers, disallow internal IPs /* 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, log_fn(warn_severity, LD_CONFIG,
"Address '%s' resolves to private IP address '%s'. " "Address '%s' resolves to private IP address '%s'. "
"Tor servers that use the default DirServers must have public " "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; return -1;
} }
if (!explicit_ip) { if (!explicit_ip) {
@ -2441,19 +2433,20 @@ resolve_my_address(int warn_severity, or_options_t *options,
* they're using an internal address. */ * they're using an internal address. */
log_fn(warn_severity, LD_CONFIG, "Address '%s' resolves to private " log_fn(warn_severity, LD_CONFIG, "Address '%s' resolves to private "
"IP address '%s'. Please set the Address config option to be " "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; return -1;
} }
} }
log_debug(LD_CONFIG, "Resolved Address to '%s'.", tmpbuf); log_debug(LD_CONFIG, "Resolved Address to '%s'.", fmt_addr32(addr));
*addr_out = ntohl(in.s_addr); *addr_out = addr;
if (last_resolved_addr && last_resolved_addr != *addr_out) { if (last_resolved_addr && last_resolved_addr != *addr_out) {
/* Leave this as a notice, regardless of the requested severity, /* Leave this as a notice, regardless of the requested severity,
* at least until dynamic IP address support becomes bulletproof. */ * at least until dynamic IP address support becomes bulletproof. */
log_notice(LD_NET, log_notice(LD_NET,
"Your IP address seems to have changed to %s. Updating.", "Your IP address seems to have changed to %s. Updating.",
tmpbuf); addr_string);
ip_address_changed(0); ip_address_changed(0);
} }
if (last_resolved_addr != *addr_out) { 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, control_event_server_status(LOG_NOTICE,
"EXTERNAL_ADDRESS ADDRESS=%s METHOD=%s %s%s", "EXTERNAL_ADDRESS ADDRESS=%s METHOD=%s %s%s",
tmpbuf, method, h?"HOSTNAME=":"", h); addr_string, method, h?"HOSTNAME=":"", h);
} }
last_resolved_addr = *addr_out; last_resolved_addr = *addr_out;
if (hostname_out) if (hostname_out)
*hostname_out = tor_strdup(hostname); *hostname_out = tor_strdup(hostname);
tor_free(addr_string);
return 0; return 0;
} }

View File

@ -1185,7 +1185,6 @@ static char *
addressmap_get_virtual_address(int type) addressmap_get_virtual_address(int type)
{ {
char buf[64]; char buf[64];
struct in_addr in;
tor_assert(addressmap); tor_assert(addressmap);
if (type == RESOLVED_TYPE_HOSTNAME) { if (type == RESOLVED_TYPE_HOSTNAME) {
@ -1206,9 +1205,7 @@ addressmap_get_virtual_address(int type)
(next_virtual_addr & 0xff) == 0xff) { (next_virtual_addr & 0xff) == 0xff) {
++next_virtual_addr; ++next_virtual_addr;
} }
in.s_addr = htonl(next_virtual_addr); if (!strmap_get(addressmap, fmt_addr32(next_virtual_addr))) {
tor_inet_ntoa(&in, buf, sizeof(buf));
if (!strmap_get(addressmap, buf)) {
++next_virtual_addr; ++next_virtual_addr;
break; break;
} }
@ -2298,13 +2295,11 @@ tell_controller_about_resolved_result(edge_connection_t *conn,
answer_type == RESOLVED_TYPE_HOSTNAME)) { answer_type == RESOLVED_TYPE_HOSTNAME)) {
return; /* we already told the controller. */ return; /* we already told the controller. */
} else if (answer_type == RESOLVED_TYPE_IPV4 && answer_len >= 4) { } else if (answer_type == RESOLVED_TYPE_IPV4 && answer_len >= 4) {
struct in_addr in; char *cp = tor_dup_ip(get_uint32(answer));
char buf[INET_NTOA_BUF_LEN];
in.s_addr = get_uint32(answer);
tor_inet_ntoa(&in, buf, sizeof(buf));
control_event_address_mapped(conn->socks_request->address, control_event_address_mapped(conn->socks_request->address,
buf, expires, NULL); cp, expires, NULL);
} else if (answer_type == RESOLVED_TYPE_HOSTNAME && answer_len <256) { tor_free(cp);
} else if (answer_type == RESOLVED_TYPE_HOSTNAME && answer_len < 256) {
char *cp = tor_strndup(answer, answer_len); char *cp = tor_strndup(answer, answer_len);
control_event_address_mapped(conn->socks_request->address, control_event_address_mapped(conn->socks_request->address,
cp, expires, NULL); cp, expires, NULL);

View File

@ -1985,20 +1985,16 @@ routerstatus_format_entry(char *buf, size_t buf_len,
routerstatus_format_type_t format) routerstatus_format_type_t format)
{ {
int r; int r;
struct in_addr in;
char *cp; char *cp;
char *summary; char *summary;
char published[ISO_TIME_LEN+1]; char published[ISO_TIME_LEN+1];
char ipaddr[INET_NTOA_BUF_LEN];
char identity64[BASE64_DIGEST_LEN+1]; char identity64[BASE64_DIGEST_LEN+1];
char digest64[BASE64_DIGEST_LEN+1]; char digest64[BASE64_DIGEST_LEN+1];
format_iso_time(published, rs->published_on); format_iso_time(published, rs->published_on);
digest_to_base64(identity64, rs->identity_digest); digest_to_base64(identity64, rs->identity_digest);
digest_to_base64(digest64, rs->descriptor_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 = tor_snprintf(buf, buf_len,
"r %s %s %s%s%s %s %d %d\n", "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)?"":digest64,
(format==NS_V3_CONSENSUS_MICRODESC)?"":" ", (format==NS_V3_CONSENSUS_MICRODESC)?"":" ",
published, published,
ipaddr, fmt_addr32(rs->addr),
(int)rs->or_port, (int)rs->or_port,
(int)rs->dir_port); (int)rs->dir_port);
if (r<0) { if (r<0) {
@ -2722,10 +2718,8 @@ generate_v2_networkstatus_opinion(void)
char *outp, *endp; char *outp, *endp;
or_options_t *options = get_options(); or_options_t *options = get_options();
char fingerprint[FINGERPRINT_LEN+1]; char fingerprint[FINGERPRINT_LEN+1];
char ipaddr[INET_NTOA_BUF_LEN];
char published[ISO_TIME_LEN+1]; char published[ISO_TIME_LEN+1];
char digest[DIGEST_LEN]; char digest[DIGEST_LEN];
struct in_addr in;
uint32_t addr; uint32_t addr;
crypto_pk_env_t *private_key; crypto_pk_env_t *private_key;
routerlist_t *rl = router_get_routerlist(); routerlist_t *rl = router_get_routerlist();
@ -2746,8 +2740,6 @@ generate_v2_networkstatus_opinion(void)
log_warn(LD_NET, "Couldn't resolve my hostname"); log_warn(LD_NET, "Couldn't resolve my hostname");
goto done; goto done;
} }
in.s_addr = htonl(addr);
tor_inet_ntoa(&in, ipaddr, sizeof(ipaddr));
format_iso_time(published, now); format_iso_time(published, now);
@ -2793,7 +2785,7 @@ generate_v2_networkstatus_opinion(void)
"dir-options%s%s%s%s\n" "dir-options%s%s%s%s\n"
"%s" /* client version line, server version line. */ "%s" /* client version line, server version line. */
"dir-signing-key\n%s", "dir-signing-key\n%s",
hostname, ipaddr, (int)options->DirPort, hostname, fmt_addr32(addr), (int)options->DirPort,
fingerprint, fingerprint,
contact, contact,
published, published,

View File

@ -83,9 +83,7 @@ format_networkstatus_vote(crypto_pk_env_t *private_signing_key,
const char *client_versions = NULL, *server_versions = NULL; const char *client_versions = NULL, *server_versions = NULL;
char *outp, *endp; char *outp, *endp;
char fingerprint[FINGERPRINT_LEN+1]; char fingerprint[FINGERPRINT_LEN+1];
char ipaddr[INET_NTOA_BUF_LEN];
char digest[DIGEST_LEN]; char digest[DIGEST_LEN];
struct in_addr in;
uint32_t addr; uint32_t addr;
routerlist_t *rl = router_get_routerlist(); routerlist_t *rl = router_get_routerlist();
char *version_lines = NULL; char *version_lines = NULL;
@ -98,8 +96,6 @@ format_networkstatus_vote(crypto_pk_env_t *private_signing_key,
voter = smartlist_get(v3_ns->voters, 0); voter = smartlist_get(v3_ns->voters, 0);
addr = voter->addr; addr = voter->addr;
in.s_addr = htonl(addr);
tor_inet_ntoa(&in, ipaddr, sizeof(ipaddr));
base16_encode(fingerprint, sizeof(fingerprint), base16_encode(fingerprint, sizeof(fingerprint),
v3_ns->cert->cache_info.identity_digest, DIGEST_LEN); v3_ns->cert->cache_info.identity_digest, DIGEST_LEN);
@ -186,7 +182,8 @@ format_networkstatus_vote(crypto_pk_env_t *private_signing_key,
flags, flags,
params, params,
voter->nickname, fingerprint, voter->address, voter->nickname, fingerprint, voter->address,
ipaddr, voter->dir_port, voter->or_port, voter->contact); fmt_addr32(addr), voter->dir_port, voter->or_port,
voter->contact);
if (r < 0) { if (r < 0) {
log_err(LD_BUG, "Insufficient memory for network status line"); log_err(LD_BUG, "Insufficient memory for network status line");
@ -1529,8 +1526,6 @@ networkstatus_compute_consensus(smartlist_t *votes,
smartlist_sort(dir_sources, _compare_dir_src_ents_by_authority_id); smartlist_sort(dir_sources, _compare_dir_src_ents_by_authority_id);
SMARTLIST_FOREACH_BEGIN(dir_sources, const dir_src_ent_t *, e) { SMARTLIST_FOREACH_BEGIN(dir_sources, const dir_src_ent_t *, e) {
struct in_addr in;
char ip[INET_NTOA_BUF_LEN];
char fingerprint[HEX_DIGEST_LEN+1]; char fingerprint[HEX_DIGEST_LEN+1];
char votedigest[HEX_DIGEST_LEN+1]; char votedigest[HEX_DIGEST_LEN+1];
networkstatus_t *v = e->v; networkstatus_t *v = e->v;
@ -1540,8 +1535,6 @@ networkstatus_compute_consensus(smartlist_t *votes,
if (e->is_legacy) if (e->is_legacy)
tor_assert(consensus_method >= 2); tor_assert(consensus_method >= 2);
in.s_addr = htonl(voter->addr);
tor_inet_ntoa(&in, ip, sizeof(ip));
base16_encode(fingerprint, sizeof(fingerprint), e->digest, DIGEST_LEN); base16_encode(fingerprint, sizeof(fingerprint), e->digest, DIGEST_LEN);
base16_encode(votedigest, sizeof(votedigest), voter->vote_digest, base16_encode(votedigest, sizeof(votedigest), voter->vote_digest,
DIGEST_LEN); DIGEST_LEN);
@ -1549,7 +1542,7 @@ networkstatus_compute_consensus(smartlist_t *votes,
tor_asprintf(&buf, tor_asprintf(&buf,
"dir-source %s%s %s %s %s %d %d\n", "dir-source %s%s %s %s %s %d %d\n",
voter->nickname, e->is_legacy ? "-legacy" : "", voter->nickname, e->is_legacy ? "-legacy" : "",
fingerprint, voter->address, ip, fingerprint, voter->address, fmt_addr32(voter->addr),
voter->dir_port, voter->dir_port,
voter->or_port); voter->or_port);
smartlist_add(chunks, buf); smartlist_add(chunks, buf);

View File

@ -908,12 +908,8 @@ connection_edge_process_relay_cell_not_open(
int ttl; int ttl;
if (!addr || (get_options()->ClientDNSRejectInternalAddresses && if (!addr || (get_options()->ClientDNSRejectInternalAddresses &&
is_internal_IP(addr, 0))) { is_internal_IP(addr, 0))) {
char buf[INET_NTOA_BUF_LEN]; log_info(LD_APP, "...but it claims the IP address was %s. Closing.",
struct in_addr a; fmt_addr32(addr));
a.s_addr = htonl(addr);
tor_inet_ntoa(&a, buf, sizeof(buf));
log_info(LD_APP,
"...but it claims the IP address was %s. Closing.", buf);
connection_edge_end(conn, END_STREAM_REASON_TORPROTOCOL); connection_edge_end(conn, END_STREAM_REASON_TORPROTOCOL);
connection_mark_unattached_ap(conn, END_STREAM_REASON_TORPROTOCOL); connection_mark_unattached_ap(conn, END_STREAM_REASON_TORPROTOCOL);
return 0; return 0;
@ -990,11 +986,8 @@ connection_edge_process_relay_cell_not_open(
uint32_t addr = ntohl(get_uint32(cell->payload+RELAY_HEADER_SIZE+2)); uint32_t addr = ntohl(get_uint32(cell->payload+RELAY_HEADER_SIZE+2));
if (get_options()->ClientDNSRejectInternalAddresses && if (get_options()->ClientDNSRejectInternalAddresses &&
is_internal_IP(addr, 0)) { is_internal_IP(addr, 0)) {
char buf[INET_NTOA_BUF_LEN]; log_info(LD_APP,"Got a resolve with answer %s. Rejecting.",
struct in_addr a; fmt_addr32(addr));
a.s_addr = htonl(addr);
tor_inet_ntoa(&a, buf, sizeof(buf));
log_info(LD_APP,"Got a resolve with answer %s. Rejecting.", buf);
connection_ap_handshake_socks_resolved(conn, connection_ap_handshake_socks_resolved(conn,
RESOLVED_TYPE_ERROR_TRANSIENT, RESOLVED_TYPE_ERROR_TRANSIENT,
0, NULL, 0, TIME_MAX); 0, NULL, 0, TIME_MAX);

View File

@ -1262,8 +1262,6 @@ static int router_guess_address_from_dir_headers(uint32_t *guess);
int int
router_pick_published_address(or_options_t *options, uint32_t *addr) router_pick_published_address(or_options_t *options, uint32_t *addr)
{ {
char buf[INET_NTOA_BUF_LEN];
struct in_addr a;
if (resolve_my_address(LOG_INFO, options, addr, NULL) < 0) { if (resolve_my_address(LOG_INFO, options, addr, NULL) < 0) {
log_info(LD_CONFIG, "Could not determine our address locally. " log_info(LD_CONFIG, "Could not determine our address locally. "
"Checking if directory headers provide any hints."); "Checking if directory headers provide any hints.");
@ -1273,9 +1271,7 @@ router_pick_published_address(or_options_t *options, uint32_t *addr)
return -1; return -1;
} }
} }
a.s_addr = htonl(*addr); log_info(LD_CONFIG,"Success: chose address '%s'.", fmt_addr32(*addr));
tor_inet_ntoa(&a, buf, sizeof(buf));
log_info(LD_CONFIG,"Success: chose address '%s'.", buf);
return 0; return 0;
} }