static void test_address_get_if_addrs(void *arg) { int rv; uint32_t addr_h = 0; tor_addr_t tor_addr; (void)arg; rv = get_interface_address(LOG_ERR, &addr_h); /* When the network is down, a system might not have any non-local * non-multicast IPv4 addresses, not even internal ones. * Unit tests shouldn't fail because of this. */ if (rv == 0) { tor_addr_from_ipv4h(&tor_addr, addr_h); tt_assert(!tor_addr_is_loopback(&tor_addr)); tt_assert(!tor_addr_is_multicast(&tor_addr)); /* The address may or may not be an internal address */ tt_assert(tor_addr_is_v4(&tor_addr)); } done: return; }
/** Return 1 iff <b>smartlist</b> contains a tor_addr_t structure * that is an IPv4 address. Otherwise, return 0. */ static int smartlist_contains_ipv4_tor_addr(smartlist_t *smartlist) { SMARTLIST_FOREACH_BEGIN(smartlist, tor_addr_t *, tor_addr) { if (tor_addr_is_v4(tor_addr)) { return 1; } } SMARTLIST_FOREACH_END(tor_addr); return 0; }
/** Return 1 iff <b>smartlist</b> contains a tor_addr_t structure * that is an IPv6 address. Otherwise, return 0. */ static int smartlist_contains_ipv6_tor_addr(smartlist_t *smartlist) { SMARTLIST_FOREACH_BEGIN(smartlist, tor_addr_t *, tor_addr) { /* Since there's no tor_addr_is_v6, assume all non-v4s are v6 */ if (!tor_addr_is_v4(tor_addr)) { return 1; } } SMARTLIST_FOREACH_END(tor_addr); return 0; }
static void test_conn_lookup_addr_helper(const char *address, int family, tor_addr_t *addr) { int rv = 0; tt_assert(addr); rv = tor_addr_lookup(address, family, addr); /* XXXX - should we retry on transient failure? */ tt_assert(rv == 0); tt_assert(tor_addr_is_loopback(addr)); tt_assert(tor_addr_is_v4(addr)); return; done: tor_addr_make_null(addr, TEST_CONN_FAMILY); }
static void test_address_get_if_addrs6(void *arg) { int rv; tor_addr_t tor_addr; (void)arg; rv = get_interface_address6(LOG_ERR, AF_INET6, &tor_addr); /* Work even on systems without IPv6 interfaces */ if (rv == 0) { tt_assert(!tor_addr_is_loopback(&tor_addr)); tt_assert(!tor_addr_is_multicast(&tor_addr)); /* The address may or may not be an internal address */ tt_assert(!tor_addr_is_v4(&tor_addr)); } done: return; }
/* Add all possible link specifiers in node to lspecs. * legacy ID is mandatory thus MUST be present in node. If the primary address * is not IPv4, log a BUG() warning, and return an empty smartlist. * Includes ed25519 id and IPv6 link specifiers if present in the node. */ static void get_lspecs_from_node(const node_t *node, smartlist_t *lspecs) { link_specifier_t *ls; tor_addr_port_t ap; tor_assert(node); tor_assert(lspecs); /* Get the relay's IPv4 address. */ node_get_prim_orport(node, &ap); /* We expect the node's primary address to be a valid IPv4 address. * This conforms to the protocol, which requires either an IPv4 or IPv6 * address (or both). */ if (BUG(!tor_addr_is_v4(&ap.addr)) || BUG(!tor_addr_port_is_valid_ap(&ap, 0))) { return; } ls = link_specifier_new(); link_specifier_set_ls_type(ls, LS_IPV4); link_specifier_set_un_ipv4_addr(ls, tor_addr_to_ipv4h(&ap.addr)); link_specifier_set_un_ipv4_port(ls, ap.port); /* Four bytes IPv4 and two bytes port. */ link_specifier_set_ls_len(ls, sizeof(ap.addr.addr.in_addr) + sizeof(ap.port)); smartlist_add(lspecs, ls); /* Legacy ID is mandatory and will always be present in node. */ ls = link_specifier_new(); link_specifier_set_ls_type(ls, LS_LEGACY_ID); memcpy(link_specifier_getarray_un_legacy_id(ls), node->identity, link_specifier_getlen_un_legacy_id(ls)); link_specifier_set_ls_len(ls, link_specifier_getlen_un_legacy_id(ls)); smartlist_add(lspecs, ls); /* ed25519 ID is only included if the node has it. */ if (!ed25519_public_key_is_zero(&node->ed25519_id)) { ls = link_specifier_new(); link_specifier_set_ls_type(ls, LS_ED25519_ID); memcpy(link_specifier_getarray_un_ed25519_id(ls), &node->ed25519_id, link_specifier_getlen_un_ed25519_id(ls)); link_specifier_set_ls_len(ls, link_specifier_getlen_un_ed25519_id(ls)); smartlist_add(lspecs, ls); } /* Check for IPv6. If so, include it as well. */ if (node_has_ipv6_orport(node)) { ls = link_specifier_new(); node_get_pref_ipv6_orport(node, &ap); link_specifier_set_ls_type(ls, LS_IPV6); size_t addr_len = link_specifier_getlen_un_ipv6_addr(ls); const uint8_t *in6_addr = tor_addr_to_in6_addr8(&ap.addr); uint8_t *ipv6_array = link_specifier_getarray_un_ipv6_addr(ls); memcpy(ipv6_array, in6_addr, addr_len); link_specifier_set_un_ipv6_port(ls, ap.port); /* Sixteen bytes IPv6 and two bytes port. */ link_specifier_set_ls_len(ls, addr_len + sizeof(ap.port)); smartlist_add(lspecs, ls); } }