Commit 15860c88 authored by David Goulet's avatar David Goulet 🤘
Browse files

addr: Use tor_addr_t instead of uint32_t for IPv4



This changes a LOT of code but in the end, behavior is the same.
Unfortunately, many functions had to be changed to accomodate but in majority
of cases, to become simpler.

Functions are also removed specifically those that were there to convert an
IPv4 as a host format to a tor_addr_t. Those are not needed anymore.

The IPv4 address field has been standardized to "ipv4_addr", the ORPort to
"ipv4_orport" (currently IPv6 uses ipv6_orport) and DirPort to "ipv4_dirport".

This is related to Sponsor 55 work that adds IPv6 support for relays and this
work is needed in order to have a common interface between IPv4 and IPv6.

Closes #40043.

Signed-off-by: David Goulet's avatarDavid Goulet <dgoulet@torproject.org>
parent 8ebbf629
o Code simplification and refactoring (relay address):
- Most of IPv4 representation was using "uint32_t". It has now been moved to
use the internal "tor_addr_t" interface instead. This is so we can
properly integrate IPv6 along IPv4 with common interfaces. Closes ticket
40043.
......@@ -1863,7 +1863,7 @@ channel_tls_process_netinfo_cell(cell_t *cell, channel_tls_t *chan)
if (my_addr_type == NETINFO_ADDR_TYPE_IPV4 && my_addr_len == 4) {
if (!get_options()->BridgeRelay && me &&
tor_addr_eq_ipv4h(&my_apparent_addr, me->addr)) {
tor_addr_eq(&my_apparent_addr, &me->ipv4_addr)) {
TLS_CHAN_TO_BASE(chan)->is_canonical_to_peer = 1;
}
} else if (my_addr_type == NETINFO_ADDR_TYPE_IPV6 &&
......@@ -1917,7 +1917,7 @@ channel_tls_process_netinfo_cell(cell_t *cell, channel_tls_t *chan)
safe_str(hex_str(identity_digest, DIGEST_LEN)),
safe_str(tor_addr_is_null(&my_apparent_addr) ?
"<none>" : fmt_and_decorate_addr(&my_apparent_addr)),
safe_str(fmt_addr32(me->addr)));
safe_str(fmt_addr(&me->ipv4_addr)));
}
/* Act on apparent skew. */
......
......@@ -2507,14 +2507,11 @@ connection_or_send_netinfo,(or_connection_t *conn))
* is an outgoing connection, act like a normal client and omit it. */
if ((public_server_mode(get_options()) || !conn->is_outgoing) &&
(me = router_get_my_routerinfo())) {
tor_addr_t my_addr;
tor_addr_from_ipv4h(&my_addr, me->addr);
uint8_t n_my_addrs = 1 + !tor_addr_is_null(&me->ipv6_addr);
netinfo_cell_set_n_my_addrs(netinfo_cell, n_my_addrs);
netinfo_cell_add_my_addrs(netinfo_cell,
netinfo_addr_from_tor_addr(&my_addr));
netinfo_addr_from_tor_addr(&me->ipv4_addr));
if (!tor_addr_is_null(&me->ipv6_addr)) {
netinfo_cell_add_my_addrs(netinfo_cell,
......
......@@ -389,19 +389,6 @@ addr_policy_permits_tor_addr(const tor_addr_t *addr, uint16_t port,
}
}
/** Return true iff <b> policy</b> (possibly NULL) will allow a connection to
* <b>addr</b>:<b>port</b>. <b>addr</b> is an IPv4 address given in host
* order. */
/* XXXX deprecate when possible. */
static int
addr_policy_permits_address(uint32_t addr, uint16_t port,
smartlist_t *policy)
{
tor_addr_t a;
tor_addr_from_ipv4h(&a, addr);
return addr_policy_permits_tor_addr(&a, port, policy);
}
/** Return true iff we think our firewall will let us make a connection to
* addr:port.
*
......@@ -576,25 +563,6 @@ fascist_firewall_allows_address_ap(const tor_addr_port_t *ap,
pref_ipv6);
}
/* Return true iff we think our firewall will let us make a connection to
* ipv4h_or_addr:ipv4_or_port. ipv4h_or_addr is interpreted in host order.
* Uses ReachableORAddresses or ReachableDirAddresses based on
* fw_connection.
* pref_only and pref_ipv6 work as in fascist_firewall_allows_address_addr().
*/
static int
fascist_firewall_allows_address_ipv4h(uint32_t ipv4h_or_addr,
uint16_t ipv4_or_port,
firewall_connection_t fw_connection,
int pref_only, int pref_ipv6)
{
tor_addr_t ipv4_or_addr;
tor_addr_from_ipv4h(&ipv4_or_addr, ipv4h_or_addr);
return fascist_firewall_allows_address_addr(&ipv4_or_addr, ipv4_or_port,
fw_connection, pref_only,
pref_ipv6);
}
/** Return true iff we think our firewall will let us make a connection to
* ipv4h_addr/ipv6_addr. Uses ipv4_orport/ipv6_orport/ReachableORAddresses or
* ipv4_dirport/ipv6_dirport/ReachableDirAddresses based on IPv4/IPv6 and
......@@ -602,14 +570,14 @@ fascist_firewall_allows_address_ipv4h(uint32_t ipv4h_or_addr,
* pref_only and pref_ipv6 work as in fascist_firewall_allows_address_addr().
*/
static int
fascist_firewall_allows_base(uint32_t ipv4h_addr, uint16_t ipv4_orport,
fascist_firewall_allows_base(const tor_addr_t *ipv4_addr, uint16_t ipv4_orport,
uint16_t ipv4_dirport,
const tor_addr_t *ipv6_addr, uint16_t ipv6_orport,
uint16_t ipv6_dirport,
firewall_connection_t fw_connection,
int pref_only, int pref_ipv6)
{
if (fascist_firewall_allows_address_ipv4h(ipv4h_addr,
if (fascist_firewall_allows_address_addr(ipv4_addr,
(fw_connection == FIREWALL_OR_CONNECTION
? ipv4_orport
: ipv4_dirport),
......@@ -641,10 +609,10 @@ fascist_firewall_allows_ri_impl(const routerinfo_t *ri,
}
/* Assume IPv4 and IPv6 DirPorts are the same */
return fascist_firewall_allows_base(ri->addr, ri->or_port, ri->dir_port,
&ri->ipv6_addr, ri->ipv6_orport,
ri->dir_port, fw_connection, pref_only,
pref_ipv6);
return fascist_firewall_allows_base(&ri->ipv4_addr, ri->ipv4_orport,
ri->ipv4_dirport, &ri->ipv6_addr,
ri->ipv6_orport, ri->ipv4_dirport,
fw_connection, pref_only, pref_ipv6);
}
/** Like fascist_firewall_allows_rs, but takes pref_ipv6. */
......@@ -658,10 +626,10 @@ fascist_firewall_allows_rs_impl(const routerstatus_t *rs,
}
/* Assume IPv4 and IPv6 DirPorts are the same */
return fascist_firewall_allows_base(rs->addr, rs->or_port, rs->dir_port,
&rs->ipv6_addr, rs->ipv6_orport,
rs->dir_port, fw_connection, pref_only,
pref_ipv6);
return fascist_firewall_allows_base(&rs->ipv4_addr, rs->ipv4_orport,
rs->ipv4_dirport, &rs->ipv6_addr,
rs->ipv6_orport, rs->ipv4_dirport,
fw_connection, pref_only, pref_ipv6);
}
/** Like fascist_firewall_allows_base(), but takes rs.
......@@ -892,34 +860,6 @@ fascist_firewall_choose_address_base(const tor_addr_t *ipv4_addr,
}
}
/** Like fascist_firewall_choose_address_base(), but takes a host-order IPv4
* address as the first parameter. */
static void
fascist_firewall_choose_address_ipv4h(uint32_t ipv4h_addr,
uint16_t ipv4_orport,
uint16_t ipv4_dirport,
const tor_addr_t *ipv6_addr,
uint16_t ipv6_orport,
uint16_t ipv6_dirport,
firewall_connection_t fw_connection,
int pref_only,
int pref_ipv6,
tor_addr_port_t* ap)
{
tor_addr_t ipv4_addr;
tor_addr_from_ipv4h(&ipv4_addr, ipv4h_addr);
tor_assert(ap);
tor_addr_make_null(&ap->addr, AF_UNSPEC);
ap->port = 0;
fascist_firewall_choose_address_base(&ipv4_addr, ipv4_orport,
ipv4_dirport, ipv6_addr,
ipv6_orport, ipv6_dirport,
fw_connection, pref_only,
pref_ipv6, ap);
}
/** Like fascist_firewall_choose_address_base(), but takes <b>rs</b>.
* Consults the corresponding node, then falls back to rs if node is NULL.
* This should only happen when there's no valid consensus, and rs doesn't
......@@ -951,12 +891,11 @@ fascist_firewall_choose_address_rs(const routerstatus_t *rs,
? fascist_firewall_prefer_ipv6_orport(options)
: fascist_firewall_prefer_ipv6_dirport(options));
/* Assume IPv4 and IPv6 DirPorts are the same.
* Assume the IPv6 OR and Dir addresses are the same. */
fascist_firewall_choose_address_ipv4h(rs->addr, rs->or_port, rs->dir_port,
&rs->ipv6_addr, rs->ipv6_orport,
rs->dir_port, fw_connection,
pref_only, pref_ipv6, ap);
fascist_firewall_choose_address_base(&rs->ipv4_addr, rs->ipv4_orport,
rs->ipv4_dirport, &rs->ipv6_addr,
rs->ipv6_orport, rs->ipv4_dirport,
fw_connection, pref_only, pref_ipv6,
ap);
}
}
......@@ -1124,17 +1063,14 @@ socks_policy_permits_address(const tor_addr_t *addr)
/** Return true iff the address <b>addr</b> is in a country listed in the
* case-insensitive list of country codes <b>cc_list</b>. */
static int
addr_is_in_cc_list(uint32_t addr, const smartlist_t *cc_list)
addr_is_in_cc_list(const tor_addr_t *addr, const smartlist_t *cc_list)
{
country_t country;
const char *name;
tor_addr_t tar;
if (!cc_list)
return 0;
/* XXXXipv6 */
tor_addr_from_ipv4h(&tar, addr);
country = geoip_get_country_by_addr(&tar);
country = geoip_get_country_by_addr(addr);
name = geoip_get_country_name(country);
return smartlist_contains_string_case(cc_list, name);
}
......@@ -1143,9 +1079,9 @@ addr_is_in_cc_list(uint32_t addr, const smartlist_t *cc_list)
* directory, based on <b>authdir_reject_policy</b>. Else return 0.
*/
int
authdir_policy_permits_address(uint32_t addr, uint16_t port)
authdir_policy_permits_address(const tor_addr_t *addr, uint16_t port)
{
if (! addr_policy_permits_address(addr, port, authdir_reject_policy))
if (!addr_policy_permits_tor_addr(addr, port, authdir_reject_policy))
return 0;
return !addr_is_in_cc_list(addr, get_options()->AuthDirRejectCCs);
}
......@@ -1154,9 +1090,9 @@ authdir_policy_permits_address(uint32_t addr, uint16_t port)
* directory, based on <b>authdir_invalid_policy</b>. Else return 0.
*/
int
authdir_policy_valid_address(uint32_t addr, uint16_t port)
authdir_policy_valid_address(const tor_addr_t *addr, uint16_t port)
{
if (! addr_policy_permits_address(addr, port, authdir_invalid_policy))
if (!addr_policy_permits_tor_addr(addr, port, authdir_invalid_policy))
return 0;
return !addr_is_in_cc_list(addr, get_options()->AuthDirInvalidCCs);
}
......@@ -1165,9 +1101,9 @@ authdir_policy_valid_address(uint32_t addr, uint16_t port)
* based on <b>authdir_badexit_policy</b>. Else return 0.
*/
int
authdir_policy_badexit_address(uint32_t addr, uint16_t port)
authdir_policy_badexit_address(const tor_addr_t *addr, uint16_t port)
{
if (! addr_policy_permits_address(addr, port, authdir_badexit_policy))
if (!addr_policy_permits_tor_addr(addr, port, authdir_badexit_policy))
return 1;
return addr_is_in_cc_list(addr, get_options()->AuthDirBadExitCCs);
}
......@@ -2086,22 +2022,6 @@ policies_copy_addr_to_smartlist(smartlist_t *addr_list, const tor_addr_t *addr)
}
}
/** Helper function that adds ipv4h_addr to a smartlist as a tor_addr_t *,
* as long as it is not tor_addr_is_null(), by converting it to a tor_addr_t
* and passing it to policies_add_addr_to_smartlist.
*
* The caller is responsible for freeing all the tor_addr_t* in the smartlist.
*/
static void
policies_copy_ipv4h_to_smartlist(smartlist_t *addr_list, uint32_t ipv4h_addr)
{
if (ipv4h_addr) {
tor_addr_t ipv4_tor_addr;
tor_addr_from_ipv4h(&ipv4_tor_addr, ipv4h_addr);
policies_copy_addr_to_smartlist(addr_list, &ipv4_tor_addr);
}
}
/** Helper function that adds copies of or_options->OutboundBindAddresses
* to a smartlist as tor_addr_t *, as long as or_options is non-NULL, and
* the addresses are not tor_addr_is_null(), by passing them to
......@@ -2133,8 +2053,8 @@ policies_copy_outbound_addresses_to_smartlist(smartlist_t *addr_list,
* If <b>or_options->ExitPolicyRejectPrivate</b> is true:
* - prepend an entry that rejects all destinations in all netblocks reserved
* for private use.
* - if local_address is non-zero, treat it as a host-order IPv4 address, and
* add it to the list of configured addresses.
* - if ipv4_local_address is non-zero, treat it as a host-order IPv4 address,
* and add it to the list of configured addresses.
* - if ipv6_local_address is non-NULL, and not the null tor_addr_t, add it
* to the list of configured addresses.
* If <b>or_options->ExitPolicyRejectLocalInterfaces</b> is true:
......@@ -2151,7 +2071,7 @@ policies_copy_outbound_addresses_to_smartlist(smartlist_t *addr_list,
*/
int
policies_parse_exit_policy_from_options(const or_options_t *or_options,
uint32_t local_address,
const tor_addr_t *ipv4_local_address,
const tor_addr_t *ipv6_local_address,
smartlist_t **result)
{
......@@ -2192,7 +2112,7 @@ policies_parse_exit_policy_from_options(const or_options_t *or_options,
/* Copy the configured addresses into the tor_addr_t* list */
if (or_options->ExitPolicyRejectPrivate) {
policies_copy_ipv4h_to_smartlist(configured_addresses, local_address);
policies_copy_addr_to_smartlist(configured_addresses, ipv4_local_address);
policies_copy_addr_to_smartlist(configured_addresses, ipv6_local_address);
}
......@@ -3062,7 +2982,7 @@ getinfo_helper_policies(control_connection_t *conn,
/* Copy the configured addresses into the tor_addr_t* list */
if (options->ExitPolicyRejectPrivate) {
policies_copy_ipv4h_to_smartlist(configured_addresses, me->addr);
policies_copy_addr_to_smartlist(configured_addresses, &me->ipv4_addr);
policies_copy_addr_to_smartlist(configured_addresses, &me->ipv6_addr);
}
......
......@@ -102,9 +102,9 @@ void fascist_firewall_choose_address_dir_server(const dir_server_t *ds,
int dir_policy_permits_address(const tor_addr_t *addr);
int socks_policy_permits_address(const tor_addr_t *addr);
int authdir_policy_permits_address(uint32_t addr, uint16_t port);
int authdir_policy_valid_address(uint32_t addr, uint16_t port);
int authdir_policy_badexit_address(uint32_t addr, uint16_t port);
int authdir_policy_permits_address(const tor_addr_t *addr, uint16_t port);
int authdir_policy_valid_address(const tor_addr_t *addr, uint16_t port);
int authdir_policy_badexit_address(const tor_addr_t *addr, uint16_t port);
int validate_addr_policies(const or_options_t *options, char **msg);
void policy_expand_private(smartlist_t **policy);
......@@ -120,7 +120,7 @@ addr_policy_result_t compare_tor_addr_to_node_policy(const tor_addr_t *addr,
int policies_parse_exit_policy_from_options(
const or_options_t *or_options,
uint32_t local_address,
const tor_addr_t *ipv4_local_address,
const tor_addr_t *ipv6_local_address,
smartlist_t **result);
struct config_line_t;
......
......@@ -302,52 +302,22 @@ routerinfo_is_a_configured_bridge(const routerinfo_t *ri)
return get_configured_bridge_by_routerinfo(ri) ? 1 : 0;
}
/**
* Return 1 iff <b>bridge_list</b> contains entry matching
* given; IPv4 address in host byte order (<b>ipv4_addr</b>
* and <b>port</b> (and no identity digest) OR it contains an
* entry whose identity matches <b>digest</b>. Otherwise,
* return 0.
*/
static int
bridge_exists_with_ipv4h_addr_and_port(const uint32_t ipv4_addr,
const uint16_t port,
const char *digest)
{
tor_addr_t node_ipv4;
if (tor_addr_port_is_valid_ipv4h(ipv4_addr, port, 0)) {
tor_addr_from_ipv4h(&node_ipv4, ipv4_addr);
bridge_info_t *bridge =
get_configured_bridge_by_addr_port_digest(&node_ipv4,
port,
digest);
return (bridge != NULL);
}
return 0;
}
/**
* Return 1 iff <b>bridge_list</b> contains entry matching given
* <b>ipv6_addr</b> and <b>port</b> (and no identity digest) OR
* <b>addr</b> and <b>port</b> (and no identity digest) OR
* it contains an entry whose identity matches <b>digest</b>.
* Otherwise, return 0.
*/
static int
bridge_exists_with_ipv6_addr_and_port(const tor_addr_t *ipv6_addr,
const uint16_t port,
const char *digest)
bridge_exists_with_addr_and_port(const tor_addr_t *addr,
const uint16_t port,
const char *digest)
{
if (!tor_addr_port_is_valid(ipv6_addr, port, 0))
if (!tor_addr_port_is_valid(addr, port, 0))
return 0;
bridge_info_t *bridge =
get_configured_bridge_by_addr_port_digest(ipv6_addr,
port,
digest);
get_configured_bridge_by_addr_port_digest(addr, port, digest);
return (bridge != NULL);
}
......@@ -374,29 +344,29 @@ node_is_a_configured_bridge(const node_t *node)
* check for absence of identity digest in a bridge.
*/
if (node->ri) {
if (bridge_exists_with_ipv4h_addr_and_port(node->ri->addr,
node->ri->or_port,
node->identity))
if (bridge_exists_with_addr_and_port(&node->ri->ipv4_addr,
node->ri->ipv4_orport,
node->identity))
return 1;
if (bridge_exists_with_ipv6_addr_and_port(&node->ri->ipv6_addr,
node->ri->ipv6_orport,
node->identity))
if (bridge_exists_with_addr_and_port(&node->ri->ipv6_addr,
node->ri->ipv6_orport,
node->identity))
return 1;
} else if (node->rs) {
if (bridge_exists_with_ipv4h_addr_and_port(node->rs->addr,
node->rs->or_port,
node->identity))
if (bridge_exists_with_addr_and_port(&node->rs->ipv4_addr,
node->rs->ipv4_orport,
node->identity))
return 1;
if (bridge_exists_with_ipv6_addr_and_port(&node->rs->ipv6_addr,
node->rs->ipv6_orport,
node->identity))
if (bridge_exists_with_addr_and_port(&node->rs->ipv6_addr,
node->rs->ipv6_orport,
node->identity))
return 1;
} else if (node->md) {
if (bridge_exists_with_ipv6_addr_and_port(&node->md->ipv6_addr,
node->md->ipv6_orport,
node->identity))
if (bridge_exists_with_addr_and_port(&node->md->ipv6_addr,
node->md->ipv6_orport,
node->identity))
return 1;
}
......@@ -825,25 +795,23 @@ rewrite_node_address_for_bridge(const bridge_info_t *bridge, node_t *node)
* do that safely if we know that no function that connects to an OR
* does so through an address from any source other than node_get_addr().
*/
tor_addr_t addr;
const or_options_t *options = get_options();
if (node->ri) {
routerinfo_t *ri = node->ri;
tor_addr_from_ipv4h(&addr, ri->addr);
if ((!tor_addr_compare(&bridge->addr, &addr, CMP_EXACT) &&
bridge->port == ri->or_port) ||
if ((!tor_addr_compare(&bridge->addr, &ri->ipv4_addr, CMP_EXACT) &&
bridge->port == ri->ipv4_orport) ||
(!tor_addr_compare(&bridge->addr, &ri->ipv6_addr, CMP_EXACT) &&
bridge->port == ri->ipv6_orport)) {
/* they match, so no need to do anything */
} else {
if (tor_addr_family(&bridge->addr) == AF_INET) {
ri->addr = tor_addr_to_ipv4h(&bridge->addr);
ri->or_port = bridge->port;
tor_addr_copy(&ri->ipv4_addr, &bridge->addr);
ri->ipv4_orport = bridge->port;
log_info(LD_DIR,
"Adjusted bridge routerinfo for '%s' to match configured "
"address %s:%d.",
ri->nickname, fmt_addr32(ri->addr), ri->or_port);
ri->nickname, fmt_addr(&ri->ipv4_addr), ri->ipv4_orport);
} else if (tor_addr_family(&bridge->addr) == AF_INET6) {
tor_addr_copy(&ri->ipv6_addr, &bridge->addr);
ri->ipv6_orport = bridge->port;
......@@ -886,21 +854,20 @@ rewrite_node_address_for_bridge(const bridge_info_t *bridge, node_t *node)
}
if (node->rs) {
routerstatus_t *rs = node->rs;
tor_addr_from_ipv4h(&addr, rs->addr);
if ((!tor_addr_compare(&bridge->addr, &addr, CMP_EXACT) &&
bridge->port == rs->or_port) ||
if ((!tor_addr_compare(&bridge->addr, &rs->ipv4_addr, CMP_EXACT) &&
bridge->port == rs->ipv4_orport) ||
(!tor_addr_compare(&bridge->addr, &rs->ipv6_addr, CMP_EXACT) &&
bridge->port == rs->ipv6_orport)) {
/* they match, so no need to do anything */
} else {
if (tor_addr_family(&bridge->addr) == AF_INET) {
rs->addr = tor_addr_to_ipv4h(&bridge->addr);
rs->or_port = bridge->port;
tor_addr_copy(&rs->ipv4_addr, &bridge->addr);
rs->ipv4_orport = bridge->port;
log_info(LD_DIR,
"Adjusted bridge routerstatus for '%s' to match "
"configured address %s.",
rs->nickname, fmt_addrport(&bridge->addr, rs->or_port));
rs->nickname, fmt_addrport(&bridge->addr, rs->ipv4_orport));
/* set IPv6 preferences even if there is no ri */
} else if (tor_addr_family(&bridge->addr) == AF_INET6) {
tor_addr_copy(&rs->ipv6_addr, &bridge->addr);
......
......@@ -225,7 +225,6 @@ format_networkstatus_vote(crypto_pk_t *private_signing_key,
smartlist_t *chunks = smartlist_new();
char fingerprint[FINGERPRINT_LEN+1];
char digest[DIGEST_LEN];
uint32_t addr;
char *protocols_lines = NULL;
char *client_versions_line = NULL, *server_versions_line = NULL;
char *shared_random_vote_str = NULL;
......@@ -237,8 +236,6 @@ format_networkstatus_vote(crypto_pk_t *private_signing_key,
voter = smartlist_get(v3_ns->voters, 0);
addr = voter->addr;
base16_encode(fingerprint, sizeof(fingerprint),
v3_ns->cert->cache_info.identity_digest, DIGEST_LEN);
......@@ -322,7 +319,7 @@ format_networkstatus_vote(crypto_pk_t *private_signing_key,
tor_free(digest_algo_b64_digest_bw_file);
}
const char *ip_str = fmt_addr32(addr);
const char *ip_str = fmt_addr(&voter->ipv4_addr);
if (ip_str[0]) {
smartlist_add_asprintf(chunks,
......@@ -358,7 +355,7 @@ format_networkstatus_vote(crypto_pk_t *private_signing_key,
bw_headers_line ? bw_headers_line : "",
bw_file_digest ? bw_file_digest: "",
voter->nickname, fingerprint, voter->address,
ip_str, voter->dir_port, voter->or_port,
ip_str, voter->ipv4_dirport, voter->ipv4_orport,
voter->contact,
shared_random_vote_str ?
shared_random_vote_str : "");
......@@ -636,9 +633,12 @@ compare_vote_rs(const vote_routerstatus_t *a, const vote_routerstatus_t *b)
if ((r = strcmp(b->status.nickname, a->status.nickname)))
return r;
CMP_FIELD(unsigned, int, addr);
CMP_FIELD(unsigned, int, or_port);
CMP_FIELD(unsigned, int, dir_port);
if ((r = tor_addr_compare(&a->status.ipv4_addr, &b->status.ipv4_addr,
CMP_EXACT))) {
return r;
}
CMP_FIELD(unsigned, int, ipv4_orport);
CMP_FIELD(unsigned, int, ipv4_dirport);
return 0;
}
......@@ -1740,9 +1740,9 @@ networkstatus_compute_consensus(smartlist_t *votes,
smartlist_add_asprintf(chunks,
"dir-source %s%s %s %s %s %d %d\n",
voter->nickname, e->is_legacy ? "-legacy" : "",
fingerprint, voter->address, fmt_addr32(voter->addr),
voter->dir_port,
voter->or_port);
fingerprint, voter->address, fmt_addr(&voter->ipv4_addr),
voter->ipv4_dirport,
voter->ipv4_orport);
if (! e->is_legacy) {
smartlist_add_asprintf(chunks,
"contact %s\n"
......@@ -2039,10 +2039,10 @@ networkstatus_compute_consensus(smartlist_t *votes,
memcpy(rs_out.identity_digest, current_rsa_id, DIGEST_LEN);
memcpy(rs_out.descriptor_digest, rs->status.descriptor_digest,
DIGEST_LEN);
rs_out.addr = rs->status.addr;
tor_addr_copy(&rs_out.ipv4_addr, &rs->status.ipv4_addr);
rs_out.published_on = rs->status.published_on;
rs_out.dir_port = rs->status.dir_port;
rs_out.or_port = rs->status.or_port;
rs_out.ipv4_dirport = rs->status.ipv4_dirport;
rs_out.ipv4_orport = rs->status.ipv4_orport;
tor_addr_copy(&rs_out.ipv6_addr, &alt_orport.addr);
rs_out.ipv6_orport = alt_orport.port;
rs_out.has_bandwidth = 0;
......@@ -4219,12 +4219,14 @@ compare_routerinfo_by_ip_and_bw_(const void **a, const void **b)
uint32_t bw_kb_first, bw_kb_second;
const node_t *node_first, *node_second;
int first_is_running, second_is_running;
uint32_t first_ipv4h = tor_addr_to_ipv4h(&first->ipv4_addr);
uint32_t second_ipv4h = tor_addr_to_ipv4h(&second->ipv4_addr);
/* we return -1 if first should appear before second... that is,
* if first is a better router. */
if (first->addr < second->addr)
if (first_ipv4h < second_ipv4h)
return -1;
else if (first->addr > second->addr)
else if (first_ipv4h > second_ipv4h)
return 1;
/* Potentially, this next bit could cause k n lg n memeq calls. But in
......@@ -4275,7 +4277,7 @@ get_possible_sybil_list(const smartlist_t *routers)
const dirauth_options_t *options = dirauth_get_options();
digestmap_t *omit_as_sybil;
smartlist_t *routers_by_ip = smartlist_new();
uint32_t last_addr;
tor_addr_t last_addr = TOR_ADDR_NULL;
int addr_count;
/* Allow at most this number of Tor servers on a single IP address, ... */
int max_with_same_addr = options->AuthDirMaxServersPerAddr;
......@@ -4286,11 +4288,10 @@ get_possible_sybil_list(const smartlist_t *routers)
smartlist_sort(routers_by_ip, compare_routerinfo_by_ip_and_bw_);
omit_as_sybil = digestmap_new();
last_addr = 0;
addr_count = 0;
SMARTLIST_FOREACH_BEGIN(routers_by_ip, routerinfo_t *, ri) {
if (last_addr != ri->addr) {
last_addr = ri->addr;
if (!tor_addr_eq(&last_addr, &ri->ipv4_addr)) {
tor_addr_copy(&last_addr, &ri->ipv4_addr);
addr_count = 1;
} else if (++addr_count > max_with_same_addr) {
digestmap_set(omit_as_sybil, ri->cache_info.identity_digest, ri);
......@@ -4722,9 +4723,9 @@ dirserv_generate_networkstatus_vote_obj(crypto_pk_t *private_key,
memcpy(voter->identity_digest, identity_digest, DIGEST_LEN);
voter->sigs = smartlist_new();
voter->address = hostname;
voter->addr = tor_addr_to_ipv4h(&addr);
voter->dir_port = router_get_advertised_dir_port(options, 0);
voter->or_port = router_get_advertised_or_port(options);
tor_addr_copy(&voter->ipv4_addr, &addr);
voter->ipv4_dirport = router_get_advertised_dir_port(options, 0);
voter->ipv4_orport = router_get_advertised_or_port(options);
<