From 7315a6764675ca2a148554488504f5abf98ff7e6 Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Sun, 25 Nov 2012 14:57:10 -0500 Subject: Refactor port_cfg_t creation into a port_cfg_new() function This function gives us a single place to set reasonable default flags for port_cfg_t entries, to avoid bugs like the one where we weren't setting ipv4_traffic_ok to 1 on SocksPorts initialized in an older way. --- src/or/config.c | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/src/or/config.c b/src/or/config.c index d0d004951..9f54d3da0 100644 --- a/src/or/config.c +++ b/src/or/config.c @@ -678,7 +678,7 @@ config_free_all(void) if (configured_ports) { SMARTLIST_FOREACH(configured_ports, - port_cfg_t *, p, tor_free(p)); + port_cfg_t *, p, port_cfg_free(p)); smartlist_free(configured_ports); configured_ports = NULL; } @@ -4589,6 +4589,17 @@ parse_dir_fallback_line(const char *line, return r; } +/** Allocate and return a new port_cfg_t with reasonable defaults. */ +static port_cfg_t * +port_cfg_new(void) +{ + port_cfg_t *cfg = tor_malloc_zero(sizeof(port_cfg_t)); + cfg->ipv4_traffic = 1; + cfg->cache_ipv4_answers = 1; + cfg->use_cached_ipv4_answers = 1; + return cfg; +} + /** Free all storage held in port */ static void port_cfg_free(port_cfg_t *port) @@ -4763,7 +4774,7 @@ parse_port_config(smartlist_t *out, if (use_server_options && out) { /* Add a no_listen port. */ - port_cfg_t *cfg = tor_malloc_zero(sizeof(port_cfg_t)); + port_cfg_t *cfg = port_cfg_new(); cfg->type = listener_type; cfg->port = mainport; tor_addr_make_unspec(&cfg->addr); /* Server ports default to 0.0.0.0 */ @@ -4782,15 +4793,13 @@ parse_port_config(smartlist_t *out, return -1; } if (out) { - port_cfg_t *cfg = tor_malloc_zero(sizeof(port_cfg_t)); + port_cfg_t *cfg = port_cfg_new(); cfg->type = listener_type; cfg->port = port ? port : mainport; tor_addr_copy(&cfg->addr, &addr); cfg->session_group = SESSION_GROUP_UNSET; cfg->isolation_flags = ISO_DEFAULT; cfg->no_advertise = 1; - cfg->ipv4_traffic = 1; - cfg->cache_ipv4_answers = cfg->use_cached_ipv4_answers = 1; smartlist_add(out, cfg); } } @@ -4808,14 +4817,12 @@ parse_port_config(smartlist_t *out, * one. */ if (! ports) { if (defaultport && out) { - port_cfg_t *cfg = tor_malloc_zero(sizeof(port_cfg_t)); + port_cfg_t *cfg = port_cfg_new(); cfg->type = listener_type; cfg->port = defaultport; tor_addr_parse(&cfg->addr, defaultaddr); cfg->session_group = SESSION_GROUP_UNSET; cfg->isolation_flags = ISO_DEFAULT; - cfg->ipv4_traffic = 1; - cfg->cache_ipv4_answers = cfg->use_cached_ipv4_answers = 1; smartlist_add(out, cfg); } return 0; @@ -5033,7 +5040,7 @@ parse_port_config(smartlist_t *out, } if (out && port) { - port_cfg_t *cfg = tor_malloc_zero(sizeof(port_cfg_t)); + port_cfg_t *cfg = port_cfg_new(); tor_addr_copy(&cfg->addr, &addr); cfg->port = port; cfg->type = listener_type; -- cgit v1.2.3