/* * Set the given string address in a configuration object. * * Return 0 on success or else a negative value. On error, the address was not * recognized. */ static int set_tor_address(const char *addr, struct configuration *config) { int ret; assert(addr); assert(config); ret = utils_is_address_ipv4(addr); if (ret == 1 ) { config->conf_file.tor_domain = CONNECTION_DOMAIN_INET; } else { ret = utils_is_address_ipv6(addr); if (ret != 1) { /* At this point, the addr is either v4 nor v6 so error. */ ERR("Config file unknown tor address: %s", addr); goto error; } config->conf_file.tor_domain = CONNECTION_DOMAIN_INET6; } config->conf_file.tor_address = strdup(addr); if (!config->conf_file.tor_address) { ret = -ENOMEM; goto error; } DBG("Config file setting tor address to %s", addr); ret = 0; error: return ret; }
static void test_is_address_ipv6(void) { int ret = 0; diag("Utils IPv6 test"); ret = utils_is_address_ipv6("::1"); ok(ret == 1, "Valid IPv6 address"); ret = utils_is_address_ipv6("2001:DB8:0:0:8:800:200C:417A"); ok(ret == 1, "Valid IPv6 address"); ret = utils_is_address_ipv6("2001:DB8:0:0:8:800:200C:G"); ok(ret == -1, "Invalid IPv6 address"); ret = utils_is_address_ipv6("192.168.0.1"); ok(ret == -1, "Invalid IPv6 address when IPv4"); }