Commit 00b2b69a authored by Nick Mathewson's avatar Nick Mathewson
Browse files

Fix names of functions that convert strings to addrs

Now let's have "lookup" indicate that there can be a hostname
resolution, and "parse" indicate that there wasn't.  Previously, we
had one "lookup" function that did resolution; four "parse" functions,
half of which did resolution; and a "from_str()" function that didn't
do resolution.  That's confusing and error-prone!

The code changes in this commit are exactly the result of this perl
script, run under "perl -p -i.bak" :

  s/tor_addr_port_parse/tor_addr_port_lookup/g;
  s/parse_addr_port(?=[^_])/addr_port_lookup/g;
  s/tor_addr_from_str/tor_addr_parse/g;

This patch leaves aton and pton alone: their naming convention and
behavior is is determined by the sockets API.

More renaming may be needed.
parent 69921837
o Code simplifications and refactoring:
- Rename Tor functions that turn strings into addresses, so that
"parse" indicates that no hostname resolution occurs, and
"lookup" indicates that hostname resolution may occur. This
should help prevent mistakes in the future. Fixes bug 3512.
......@@ -455,7 +455,7 @@ tor_addr_parse_reverse_lookup_name(tor_addr_t *result, const char *address,
if (accept_regular) {
tor_addr_t tmp;
int r = tor_addr_from_str(&tmp, address);
int r = tor_addr_parse(&tmp, address);
if (r < 0)
return 0;
if (r != family && family != AF_UNSPEC)
......@@ -984,7 +984,7 @@ fmt_addr32(uint32_t addr)
* Return an address family on success, or -1 if an invalid address string is
* provided. */
int
tor_addr_from_str(tor_addr_t *addr, const char *src)
tor_addr_parse(tor_addr_t *addr, const char *src)
{
char *tmp = NULL; /* Holds substring if we got a dotted quad. */
int result;
......@@ -1012,7 +1012,7 @@ tor_addr_from_str(tor_addr_t *addr, const char *src)
* address as needed, and put the result in <b>addr_out</b> and (optionally)
* <b>port_out</b>. Return 0 on success, negative on failure. */
int
tor_addr_port_parse(const char *s, tor_addr_t *addr_out, uint16_t *port_out)
tor_addr_port_lookup(const char *s, tor_addr_t *addr_out, uint16_t *port_out)
{
const char *port;
tor_addr_t addr;
......@@ -1159,7 +1159,7 @@ is_internal_IP(uint32_t ip, int for_listening)
* Return 0 on success, -1 on failure.
*/
int
parse_addr_port(int severity, const char *addrport, char **address,
addr_port_lookup(int severity, const char *addrport, char **address,
uint32_t *addr, uint16_t *port_out)
{
const char *colon;
......
......@@ -159,14 +159,14 @@ int tor_addr_to_reverse_lookup_name(char *out, size_t outlen,
int tor_addr_parse_reverse_lookup_name(tor_addr_t *result, const char *address,
int family, int accept_regular);
int tor_addr_port_parse(const char *s, tor_addr_t *addr_out,
int tor_addr_port_lookup(const char *s, tor_addr_t *addr_out,
uint16_t *port_out);
int tor_addr_parse_mask_ports(const char *s,
tor_addr_t *addr_out, maskbits_t *mask_out,
uint16_t *port_min_out, uint16_t *port_max_out);
const char * tor_addr_to_str(char *dest, const tor_addr_t *addr, size_t len,
int decorate);
int tor_addr_from_str(tor_addr_t *addr, const char *src);
int tor_addr_parse(tor_addr_t *addr, const char *src);
void tor_addr_copy(tor_addr_t *dest, const tor_addr_t *src);
void tor_addr_from_ipv4n(tor_addr_t *dest, uint32_t v4addr);
/** Set <b>dest</b> to the IPv4 address encoded in <b>v4addr</b> in host
......@@ -183,7 +183,7 @@ int tor_addr_is_loopback(const tor_addr_t *addr);
/* IPv4 helpers */
int is_internal_IP(uint32_t ip, int for_listening) ATTR_PURE;
int parse_addr_port(int severity, const char *addrport, char **address,
int addr_port_lookup(int severity, const char *addrport, char **address,
uint32_t *addr, uint16_t *port_out);
int parse_port_range(const char *port, uint16_t *port_min_out,
uint16_t *port_max_out);
......
......@@ -117,7 +117,7 @@ circuit_is_acceptable(const origin_circuit_t *origin_circ,
if (tor_digest_is_zero(digest)) {
/* we don't know the digest; have to compare addr:port */
tor_addr_t addr;
int r = tor_addr_from_str(&addr, conn->socks_request->address);
int r = tor_addr_parse(&addr, conn->socks_request->address);
if (r < 0 ||
!tor_addr_eq(&build_state->chosen_exit->addr, &addr) ||
build_state->chosen_exit->port != conn->socks_request->port)
......@@ -1454,7 +1454,7 @@ circuit_get_open_circ_or_launch(entry_connection_t *conn,
log_info(LD_DIR, "Broken exit digest on tunnel conn. Closing.");
return -1;
}
if (tor_addr_from_str(&addr, conn->socks_request->address) < 0) {
if (tor_addr_parse(&addr, conn->socks_request->address) < 0) {
log_info(LD_DIR, "Broken address %s on tunnel conn. Closing.",
escaped_safe_str_client(conn->socks_request->address));
return -1;
......
......@@ -2877,7 +2877,7 @@ is_listening_on_low_port(int port_option,
return (port_option < 1024);
for (l = listen_options; l; l = l->next) {
parse_addr_port(LOG_WARN, l->value, NULL, NULL, &p);
addr_port_lookup(LOG_WARN, l->value, NULL, NULL, &p);
if (p<1024) {
return 1;
}
......@@ -3573,7 +3573,7 @@ options_validate(or_options_t *old_options, or_options_t *options,
REJECT("Failed to parse accounting options. See logs for details.");
if (options->HTTPProxy) { /* parse it now */
if (tor_addr_port_parse(options->HTTPProxy,
if (tor_addr_port_lookup(options->HTTPProxy,
&options->HTTPProxyAddr, &options->HTTPProxyPort) < 0)
REJECT("HTTPProxy failed to parse or resolve. Please fix.");
if (options->HTTPProxyPort == 0) { /* give it a default */
......@@ -3587,7 +3587,7 @@ options_validate(or_options_t *old_options, or_options_t *options,
}
if (options->HTTPSProxy) { /* parse it now */
if (tor_addr_port_parse(options->HTTPSProxy,
if (tor_addr_port_lookup(options->HTTPSProxy,
&options->HTTPSProxyAddr, &options->HTTPSProxyPort) <0)
REJECT("HTTPSProxy failed to parse or resolve. Please fix.");
if (options->HTTPSProxyPort == 0) { /* give it a default */
......@@ -3601,7 +3601,7 @@ options_validate(or_options_t *old_options, or_options_t *options,
}
if (options->Socks4Proxy) { /* parse it now */
if (tor_addr_port_parse(options->Socks4Proxy,
if (tor_addr_port_lookup(options->Socks4Proxy,
&options->Socks4ProxyAddr,
&options->Socks4ProxyPort) <0)
REJECT("Socks4Proxy failed to parse or resolve. Please fix.");
......@@ -3611,7 +3611,7 @@ options_validate(or_options_t *old_options, or_options_t *options,
}
if (options->Socks5Proxy) { /* parse it now */
if (tor_addr_port_parse(options->Socks5Proxy,
if (tor_addr_port_lookup(options->Socks5Proxy,
&options->Socks5ProxyAddr,
&options->Socks5ProxyPort) <0)
REJECT("Socks5Proxy failed to parse or resolve. Please fix.");
......@@ -4684,7 +4684,7 @@ parse_bridge_line(const char *line, int validate_only)
addrport = field1;
}
if (tor_addr_port_parse(addrport, &addr, &port)<0) {
if (tor_addr_port_lookup(addrport, &addr, &port)<0) {
log_warn(LD_CONFIG, "Error parsing Bridge address '%s'", addrport);
goto err;
}
......@@ -4827,7 +4827,7 @@ parse_client_transport_line(const char *line, int validate_only)
addrport = smartlist_get(items, 2);
if (tor_addr_port_parse(addrport, &addr, &port)<0) {
if (tor_addr_port_lookup(addrport, &addr, &port)<0) {
log_warn(LD_CONFIG, "Error parsing transport "
"address '%s'", addrport);
goto err;
......@@ -4948,7 +4948,7 @@ parse_server_transport_line(const char *line, int validate_only)
addrport = smartlist_get(items, 2);
if (tor_addr_port_parse(addrport, &addr, &port)<0) {
if (tor_addr_port_lookup(addrport, &addr, &port)<0) {
log_warn(LD_CONFIG, "Error parsing transport "
"address '%s'", addrport);
goto err;
......@@ -5060,7 +5060,7 @@ parse_dir_server_line(const char *line, dirinfo_type_t required_type,
}
addrport = smartlist_get(items, 0);
smartlist_del_keeporder(items, 0);
if (parse_addr_port(LOG_WARN, addrport, &address, NULL, &dir_port)<0) {
if (addr_port_lookup(LOG_WARN, addrport, &address, NULL, &dir_port)<0) {
log_warn(LD_CONFIG, "Error parsing DirServer address '%s'", addrport);
goto err;
}
......@@ -5228,7 +5228,7 @@ parse_client_port_config(smartlist_t *out,
for (; listenaddrs; listenaddrs = listenaddrs->next) {
tor_addr_t addr;
uint16_t port = 0;
if (tor_addr_port_parse(listenaddrs->value, &addr, &port) < 0) {
if (tor_addr_port_lookup(listenaddrs->value, &addr, &port) < 0) {
log_warn(LD_CONFIG, "Unable to parse %sListenAddress '%s'",
portname, listenaddrs->value);
return -1;
......@@ -5256,7 +5256,7 @@ parse_client_port_config(smartlist_t *out,
port_cfg_t *cfg = tor_malloc_zero(sizeof(port_cfg_t));
cfg->type = listener_type;
cfg->port = defaultport;
tor_addr_from_str(&cfg->addr, defaultaddr);
tor_addr_parse(&cfg->addr, defaultaddr);
cfg->session_group = SESSION_GROUP_UNSET;
cfg->isolation_flags = ISO_DEFAULT;
smartlist_add(out, cfg);
......@@ -5294,11 +5294,11 @@ parse_client_port_config(smartlist_t *out,
addrport = smartlist_get(elts, 0);
if (!strcmp(addrport, "auto")) {
port = CFG_AUTO_PORT;
tor_addr_from_str(&addr, defaultaddr);
tor_addr_parse(&addr, defaultaddr);
} else if (!strcasecmpend(addrport, ":auto")) {
char *addrtmp = tor_strndup(addrport, strlen(addrport)-5);
port = CFG_AUTO_PORT;
if (tor_addr_port_parse(addrtmp, &addr, &ptmp)<0 || ptmp) {
if (tor_addr_port_lookup(addrtmp, &addr, &ptmp)<0 || ptmp) {
log_warn(LD_CONFIG, "Invalid address '%s' for %sPort",
escaped(addrport), portname);
tor_free(addrtmp);
......@@ -5309,8 +5309,8 @@ parse_client_port_config(smartlist_t *out,
"9050" might be a valid address. */
port = (int) tor_parse_long(addrport, 10, 0, 65535, &ok, NULL);
if (ok) {
tor_addr_from_str(&addr, defaultaddr);
} else if (tor_addr_port_parse(addrport, &addr, &ptmp) == 0) {
tor_addr_parse(&addr, defaultaddr);
} else if (tor_addr_port_lookup(addrport, &addr, &ptmp) == 0) {
if (ptmp == 0) {
log_warn(LD_CONFIG, "%sPort line has address but no port", portname);
goto err;
......@@ -5946,7 +5946,7 @@ state_transport_line_is_valid(const char *line)
}
addrport = smartlist_get(items, 1);
if (tor_addr_port_parse(addrport, &addr, &port) < 0) {
if (tor_addr_port_lookup(addrport, &addr, &port) < 0) {
log_warn(LD_CONFIG, "state: Could not parse addrport.");
goto err;
}
......
......@@ -1893,7 +1893,7 @@ retry_listeners(smartlist_t *old_conns,
int retval = 0;
if (default_addr) {
tor_addr_from_str(&dflt_addr, default_addr);
tor_addr_parse(&dflt_addr, default_addr);
} else {
tor_addr_make_unspec(&dflt_addr);
}
......@@ -1917,7 +1917,7 @@ retry_listeners(smartlist_t *old_conns,
port->is_unix_addr = 1;
memcpy(port->unix_addr, c->value, len+1);
} else {
if (tor_addr_port_parse(c->value, &addr, &portval) < 0) {
if (tor_addr_port_lookup(c->value, &addr, &portval) < 0) {
log_warn(LD_CONFIG, "Can't parse/resolve %s %s",
c->key, c->value);
retval = -1;
......
......@@ -691,7 +691,7 @@ connection_ap_fail_onehop(const char *failed_digest,
if (!build_state || !build_state->chosen_exit ||
!entry_conn->socks_request || !entry_conn->socks_request->address)
continue;
if (tor_addr_from_str(&addr, entry_conn->socks_request->address)<0 ||
if (tor_addr_parse(&addr, entry_conn->socks_request->address)<0 ||
!tor_addr_eq(&build_state->chosen_exit->addr, &addr) ||
build_state->chosen_exit->port != entry_conn->socks_request->port)
continue;
......@@ -1918,7 +1918,7 @@ connection_ap_handshake_rewrite_and_attach(entry_connection_t *conn,
if (options->ClientRejectInternalAddresses &&
!conn->use_begindir && !conn->chosen_exit_name && !circ) {
tor_addr_t addr;
if (tor_addr_from_str(&addr, socks->address) >= 0 &&
if (tor_addr_parse(&addr, socks->address) >= 0 &&
tor_addr_is_internal(&addr, 0)) {
/* If this is an explicit private address with no chosen exit node,
* then we really don't want to try to connect to it. That's
......@@ -2894,7 +2894,7 @@ connection_exit_begin_conn(cell_t *cell, circuit_t *circ)
END_STREAM_REASON_TORPROTOCOL, NULL);
return 0;
}
if (parse_addr_port(LOG_PROTOCOL_WARN,
if (addr_port_lookup(LOG_PROTOCOL_WARN,
(char*)(cell->payload+RELAY_HEADER_SIZE),
&address,NULL,&port)<0) {
log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL,
......
......@@ -3207,7 +3207,7 @@ dirserv_orconn_tls_done(const char *address,
log_info(LD_DIRSERV, "Found router %s to be reachable at %s:%d. Yay.",
router_describe(ri),
address, ri->or_port);
if (tor_addr_from_str(&addr, ri->address) != -1)
if (tor_addr_parse(&addr, ri->address) != -1)
addrp = &addr;
else
log_warn(LD_BUG, "Couldn't parse IP address \"%s\"", ri->address);
......
......@@ -687,7 +687,7 @@ dns_resolve_impl(edge_connection_t *exitconn, int is_resolve,
/* first check if exitconn->_base.address is an IP. If so, we already
* know the answer. */
if (tor_addr_from_str(&addr, exitconn->_base.address) >= 0) {
if (tor_addr_parse(&addr, exitconn->_base.address) >= 0) {
if (tor_addr_family(&addr) == AF_INET) {
tor_addr_copy(&exitconn->_base.addr, &addr);
exitconn->address_ttl = DEFAULT_DNS_TTL;
......@@ -1198,7 +1198,7 @@ configure_nameservers(int force)
#ifdef HAVE_EVDNS_SET_DEFAULT_OUTGOING_BIND_ADDRESS
if (options->OutboundBindAddress) {
tor_addr_t addr;
if (tor_addr_from_str(&addr, options->OutboundBindAddress) < 0) {
if (tor_addr_parse(&addr, options->OutboundBindAddress) < 0) {
log_warn(LD_CONFIG,"Outbound bind address '%s' didn't parse. Ignoring.",
options->OutboundBindAddress);
} else {
......
......@@ -258,7 +258,7 @@ parse_port_config(const char *string)
} else {
addrport = smartlist_get(sl,1);
if (strchr(addrport, ':') || strchr(addrport, '.')) {
if (tor_addr_port_parse(addrport, &addr, &p)<0) {
if (tor_addr_port_lookup(addrport, &addr, &p)<0) {
log_warn(LD_CONFIG,"Unparseable address in hidden service port "
"configuration.");
goto err;
......
......@@ -1812,8 +1812,8 @@ authority_cert_parse_from_string(const char *s, const char **end_of_string)
struct in_addr in;
char *address = NULL;
tor_assert(tok->n_args);
/* XXX023 use tor_addr_port_parse() below instead. -RD */
if (parse_addr_port(LOG_WARN, tok->args[0], &address, NULL,
/* XXX023 use tor_addr_port_lookup() below instead. -RD */
if (addr_port_lookup(LOG_WARN, tok->args[0], &address, NULL,
&cert->dir_port)<0 ||
tor_inet_aton(address, &in) == 0) {
log_warn(LD_DIR, "Couldn't parse dir-address in certificate");
......@@ -4982,7 +4982,7 @@ rend_parse_introduction_points(rend_service_descriptor_t *parsed,
info->identity_digest, DIGEST_LEN);
/* Parse IP address. */
tok = find_by_keyword(tokens, R_IPO_IP_ADDRESS);
if (tor_addr_from_str(&info->addr, tok->args[0])<0) {
if (tor_addr_parse(&info->addr, tok->args[0])<0) {
log_warn(LD_REND, "Could not parse introduction point address.");
rend_intro_point_free(intro);
goto err;
......
......@@ -715,7 +715,7 @@ parse_smethod_line(const char *line, managed_proxy_t *mp)
}
addrport = smartlist_get(items, 2);
if (tor_addr_port_parse(addrport, &addr, &port)<0) {
if (tor_addr_port_lookup(addrport, &addr, &port)<0) {
log_warn(LD_CONFIG, "Error parsing transport "
"address '%s'", addrport);
goto err;
......@@ -800,7 +800,7 @@ parse_cmethod_line(const char *line, managed_proxy_t *mp)
}
addrport = smartlist_get(items, 3);
if (tor_addr_port_parse(addrport, &addr, &port)<0) {
if (tor_addr_port_lookup(addrport, &addr, &port)<0) {
log_warn(LD_CONFIG, "Error parsing transport "
"address '%s'", addrport);
goto err;
......
......@@ -14,30 +14,30 @@ test_addr_basic(void)
uint16_t u16;
char *cp;
/* Test parse_addr_port */
/* Test addr_port_lookup */
cp = NULL; u32 = 3; u16 = 3;
test_assert(!parse_addr_port(LOG_WARN, "1.2.3.4", &cp, &u32, &u16));
test_assert(!addr_port_lookup(LOG_WARN, "1.2.3.4", &cp, &u32, &u16));
test_streq(cp, "1.2.3.4");
test_eq(u32, 0x01020304u);
test_eq(u16, 0);
tor_free(cp);
test_assert(!parse_addr_port(LOG_WARN, "4.3.2.1:99", &cp, &u32, &u16));
test_assert(!addr_port_lookup(LOG_WARN, "4.3.2.1:99", &cp, &u32, &u16));
test_streq(cp, "4.3.2.1");
test_eq(u32, 0x04030201u);
test_eq(u16, 99);
tor_free(cp);
test_assert(!parse_addr_port(LOG_WARN, "nonexistent.address:4040",
test_assert(!addr_port_lookup(LOG_WARN, "nonexistent.address:4040",
&cp, NULL, &u16));
test_streq(cp, "nonexistent.address");
test_eq(u16, 4040);
tor_free(cp);
test_assert(!parse_addr_port(LOG_WARN, "localhost:9999", &cp, &u32, &u16));
test_assert(!addr_port_lookup(LOG_WARN, "localhost:9999", &cp, &u32, &u16));
test_streq(cp, "localhost");
test_eq(u32, 0x7f000001u);
test_eq(u16, 9999);
tor_free(cp);
u32 = 3;
test_assert(!parse_addr_port(LOG_WARN, "localhost", NULL, &u32, &u16));
test_assert(!addr_port_lookup(LOG_WARN, "localhost", NULL, &u32, &u16));
test_eq(cp, NULL);
test_eq(u32, 0x7f000001u);
test_eq(u16, 0);
......@@ -364,10 +364,10 @@ test_addr_ip6_helpers(void)
test_addr_compare_masked("0::2:2:1", ==, "0::8000:2:1", 80);
/* Test decorated addr_to_string. */
test_eq(AF_INET6, tor_addr_from_str(&t1, "[123:45:6789::5005:11]"));
test_eq(AF_INET6, tor_addr_parse(&t1, "[123:45:6789::5005:11]"));
p1 = tor_addr_to_str(buf, &t1, sizeof(buf), 1);
test_streq(p1, "[123:45:6789::5005:11]");
test_eq(AF_INET, tor_addr_from_str(&t1, "18.0.0.1"));
test_eq(AF_INET, tor_addr_parse(&t1, "18.0.0.1"));
p1 = tor_addr_to_str(buf, &t1, sizeof(buf), 1);
test_streq(p1, "18.0.0.1");
......
......@@ -169,7 +169,7 @@ parse_commandline(int argc, char **argv)
fprintf(stderr, "No argument to -a\n");
return 1;
}
if (parse_addr_port(LOG_ERR, argv[++i], NULL, &addr, &port)<0)
if (addr_port_lookup(LOG_ERR, argv[++i], NULL, &addr, &port)<0)
return 1;
in.s_addr = htonl(addr);
tor_inet_ntoa(&in, b, sizeof(b));
......
......@@ -393,7 +393,7 @@ main(int argc, char **argv)
socksport = 9050; /* 9050 */
}
} else if (n_args == 2) {
if (parse_addr_port(LOG_WARN, arg[1], NULL, &sockshost, &socksport)<0) {
if (addr_port_lookup(LOG_WARN, arg[1], NULL, &sockshost, &socksport)<0) {
fprintf(stderr, "Couldn't parse/resolve address %s", arg[1]);
return 1;
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment