Fix issues found by nickm.

* Document fmt_addr_impl() and friends.
* Parenthesize macro arguments.
* Rename get_first_listener_addrport_for_pt() to
  get_first_listener_addrport_string().
* Handle port_cfg_t with no_listen.
* Handle failure of router_get_active_listener_port_by_type().
* Add an XXX to router_get_active_listener_port_by_type().
This commit is contained in:
George Kadianakis 2012-04-12 22:42:37 +02:00
parent b03f90b538
commit 6d2898607b
6 changed files with 30 additions and 12 deletions

View File

@ -986,10 +986,15 @@ tor_dup_addr(const tor_addr_t *addr)
} }
} }
/** Return a string representing the address <b>addr</b>. This string is /** Return a string representing the address <b>addr</b>. This string
* statically allocated, and must not be freed. Each call to * is statically allocated, and must not be freed. Each call to
* <b>fmt_addr</b> invalidates the last result of the function. This * <b>fmt_addr_impl</b> invalidates the last result of the function.
* function is not thread-safe. */ * This function is not thread-safe. If <b>decorate</b> is set, add
* brackets to IPv6 addresses.
*
* It's better to use the wrapper macros of this function:
* <b>fmt_addr()</b> and <b>fmt_and_decorate_addr()</b>.
*/
const char * const char *
fmt_addr_impl(const tor_addr_t *addr, int decorate) fmt_addr_impl(const tor_addr_t *addr, int decorate)
{ {

View File

@ -135,8 +135,13 @@ 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;
#define fmt_addr(a) fmt_addr_impl(a, 0)
#define fmt_and_decorate_addr(a) fmt_addr_impl(a, 1) /** Wrapper function of fmt_addr_impl(). It does not decorate IPv6
* addresses. */
#define fmt_addr(a) fmt_addr_impl((a), 0)
/** Wrapper function of fmt_addr_impl(). It decorates IPv6
* addresses. */
#define fmt_and_decorate_addr(a) fmt_addr_impl((a), 1)
const char *fmt_addr_impl(const tor_addr_t *addr, int decorate); const char *fmt_addr_impl(const tor_addr_t *addr, int decorate);
const char * fmt_addr32(uint32_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);

View File

@ -6033,9 +6033,10 @@ get_configured_ports(void)
* caller to free it after use. * caller to free it after use.
* *
* This function is meant to be used by the pluggable transport proxy * This function is meant to be used by the pluggable transport proxy
* spawning code. */ * spawning code, please make sure that it fits your purposes before
* using it. */
char * char *
get_first_listener_addrport_for_pt(int listener_type) get_first_listener_addrport_string(int listener_type)
{ {
static const char *ipv4_localhost = "127.0.0.1"; static const char *ipv4_localhost = "127.0.0.1";
static const char *ipv6_localhost = "[::1]"; static const char *ipv6_localhost = "[::1]";
@ -6047,6 +6048,8 @@ get_first_listener_addrport_for_pt(int listener_type)
return NULL; return NULL;
SMARTLIST_FOREACH_BEGIN(configured_ports, const port_cfg_t *, cfg) { SMARTLIST_FOREACH_BEGIN(configured_ports, const port_cfg_t *, cfg) {
if (cfg->no_listen)
continue;
if (cfg->type == listener_type && if (cfg->type == listener_type &&
tor_addr_family(&cfg->addr) != AF_UNSPEC) { tor_addr_family(&cfg->addr) != AF_UNSPEC) {
@ -6064,10 +6067,13 @@ get_first_listener_addrport_for_pt(int listener_type)
/* If a listener is configured with port 'auto', we are forced /* If a listener is configured with port 'auto', we are forced
to iterate all listener connections and find out in which to iterate all listener connections and find out in which
port it ended up listening: */ port it ended up listening: */
if (cfg->port == CFG_AUTO_PORT) if (cfg->port == CFG_AUTO_PORT) {
port = router_get_active_listener_port_by_type(listener_type); port = router_get_active_listener_port_by_type(listener_type);
else if (!port)
return NULL;
} else {
port = cfg->port; port = cfg->port;
}
tor_asprintf(&string, "%s:%u", address, port); tor_asprintf(&string, "%s:%u", address, port);

View File

@ -72,7 +72,7 @@ int get_first_advertised_port_by_type_af(int listener_type,
#define get_primary_dir_port() \ #define get_primary_dir_port() \
(get_first_advertised_port_by_type_af(CONN_TYPE_DIR_LISTENER, AF_INET)) (get_first_advertised_port_by_type_af(CONN_TYPE_DIR_LISTENER, AF_INET))
char *get_first_listener_addrport_for_pt(int listener_type); char *get_first_listener_addrport_string(int listener_type);
int options_need_geoip_info(const or_options_t *options, int options_need_geoip_info(const or_options_t *options,
const char **reason_out); const char **reason_out);

View File

@ -1218,6 +1218,8 @@ consider_publishable_server(int force)
/** Return the port of the first active listener of type /** Return the port of the first active listener of type
* <b>listener_type</b>. */ * <b>listener_type</b>. */
/** XXX not a very good interface. it's not reliable when there are
multiple listeners. */
uint16_t uint16_t
router_get_active_listener_port_by_type(int listener_type) router_get_active_listener_port_by_type(int listener_type)
{ {

View File

@ -992,7 +992,7 @@ create_managed_proxy_environment(const managed_proxy_t *mp)
if (mp->is_server) { if (mp->is_server) {
{ {
char *orport_tmp = get_first_listener_addrport_for_pt(CONN_TYPE_OR_LISTENER); char *orport_tmp = get_first_listener_addrport_string(CONN_TYPE_OR_LISTENER);
smartlist_add_asprintf(envs, "TOR_PT_ORPORT=%s", orport_tmp); smartlist_add_asprintf(envs, "TOR_PT_ORPORT=%s", orport_tmp);
tor_free(orport_tmp); tor_free(orport_tmp);
} }