Make _with_err return routerinfo, like old function does

This commit is contained in:
rl1987 2018-05-10 16:13:16 +03:00
parent b8ed6e2ac6
commit 36f7d0a940
4 changed files with 52 additions and 36 deletions

View File

@ -2999,9 +2999,8 @@ getinfo_helper_policies(control_connection_t *conn,
smartlist_free(private_policy_strings); smartlist_free(private_policy_strings);
} else if (!strcmp(question, "exit-policy/reject-private/relay")) { } else if (!strcmp(question, "exit-policy/reject-private/relay")) {
const or_options_t *options = get_options(); const or_options_t *options = get_options();
const routerinfo_t *me = NULL; int err = 0;
const routerinfo_t *me = router_get_my_routerinfo_with_err(&err);
int err = router_get_my_routerinfo_with_err((routerinfo_t **)&me);
if (!me) { if (!me) {
*errmsg = routerinfo_errno_to_string(err); *errmsg = routerinfo_errno_to_string(err);
@ -3043,8 +3042,13 @@ getinfo_helper_policies(control_connection_t *conn,
int include_ipv4 = 0; int include_ipv4 = 0;
int include_ipv6 = 0; int include_ipv6 = 0;
const routerinfo_t *me = NULL; int err = 0;
int err = router_get_my_routerinfo_with_err((routerinfo_t **)&me); const routerinfo_t *me = router_get_my_routerinfo_with_err(&err);
if (!me) {
*errmsg = routerinfo_errno_to_string(err);
return routerinfo_err_is_transient(err) ? -1 : 0;
}
if (!strcmp(question, "exit-policy/ipv4")) { if (!strcmp(question, "exit-policy/ipv4")) {
include_ipv4 = 1; include_ipv4 = 1;
@ -3056,14 +3060,9 @@ getinfo_helper_policies(control_connection_t *conn,
return 0; /* No such key. */ return 0; /* No such key. */
} }
if (!me) {
*errmsg = routerinfo_errno_to_string(err);
return routerinfo_err_is_transient(err) ? -1 : 0;
} else {
*answer = router_dump_exit_policy_to_string(me,include_ipv4, *answer = router_dump_exit_policy_to_string(me,include_ipv4,
include_ipv6); include_ipv6);
} }
}
return 0; return 0;
} }

View File

@ -2073,28 +2073,41 @@ router_get_my_routerinfo,(void))
return desc_routerinfo; return desc_routerinfo;
} }
/** Set <b>ri</b> to routerinfo of this OR. Rebuild it from /** Return routerinfo of this OR. Rebuild it from
* scratch if needed. Return 0 on success or an appropriate * scratch if needed. Set <b>*err</b> to 0 on success or to
* TOR_ROUTERINFO_ERROR_* value on failure. * appropriate TOR_ROUTERINFO_ERROR_* value on failure.
*/ */
MOCK_IMPL(int, MOCK_IMPL(const routerinfo_t *,
router_get_my_routerinfo_with_err,(routerinfo_t **ri)) router_get_my_routerinfo_with_err,(int *err))
{ {
if (!server_mode(get_options())) if (!server_mode(get_options())) {
return TOR_ROUTERINFO_ERROR_NOT_A_SERVER; if (err)
*err = TOR_ROUTERINFO_ERROR_NOT_A_SERVER;
if (!desc_clean_since) { return NULL;
int err = router_rebuild_descriptor(0);
if (err < 0)
return err;
} }
if (!desc_routerinfo) if (!desc_clean_since) {
return TOR_ROUTERINFO_ERROR_NOT_SO_FAST; int rebuild_err = router_rebuild_descriptor(0);
if (rebuild_err < 0) {
if (err)
*err = rebuild_err;
if (ri) return NULL;
*ri = desc_routerinfo; }
return 0; }
if (!desc_routerinfo) {
if (err)
*err = TOR_ROUTERINFO_ERROR_NOT_SO_FAST;
return NULL;
}
if (err)
*err = 0;
return desc_routerinfo;
} }
/** OR only: Return a signed server descriptor for this OR, rebuilding a fresh /** OR only: Return a signed server descriptor for this OR, rebuilding a fresh

View File

@ -92,7 +92,7 @@ void router_new_address_suggestion(const char *suggestion,
int router_compare_to_my_exit_policy(const tor_addr_t *addr, uint16_t port); int router_compare_to_my_exit_policy(const tor_addr_t *addr, uint16_t port);
MOCK_DECL(int, router_my_exit_policy_is_reject_star,(void)); MOCK_DECL(int, router_my_exit_policy_is_reject_star,(void));
MOCK_DECL(const routerinfo_t *, router_get_my_routerinfo, (void)); MOCK_DECL(const routerinfo_t *, router_get_my_routerinfo, (void));
MOCK_DECL(int, router_get_my_routerinfo_with_err,(routerinfo_t **ri)); MOCK_DECL(const routerinfo_t *, router_get_my_routerinfo_with_err,(int *err));
extrainfo_t *router_get_my_extrainfo(void); extrainfo_t *router_get_my_extrainfo(void);
const char *router_get_my_descriptor(void); const char *router_get_my_descriptor(void);
const char *router_get_descriptor_gen_reason(void); const char *router_get_descriptor_gen_reason(void);

View File

@ -1498,16 +1498,20 @@ test_dump_exit_policy_to_string(void *arg)
static routerinfo_t *mock_desc_routerinfo = NULL; static routerinfo_t *mock_desc_routerinfo = NULL;
static int routerinfo_err; static int routerinfo_err;
static int static const routerinfo_t *
mock_router_get_my_routerinfo_with_err(routerinfo_t **ri) mock_router_get_my_routerinfo_with_err(int *err)
{ {
if (routerinfo_err) if (routerinfo_err) {
return routerinfo_err; if (err)
*err = routerinfo_err;
if (ri) return NULL;
*ri = mock_desc_routerinfo; }
return 0; if (err)
*err = 0;
return mock_desc_routerinfo;
} }
#define DEFAULT_POLICY_STRING "reject *:*" #define DEFAULT_POLICY_STRING "reject *:*"