Exemple #1
0
/*
 * Initialize data structures
 */
int init_addresses(void)
{
	if (!db_url.s) {
		LM_INFO("db_url parameter of permissions module not set, "
				"disabling allow_address\n");
		return 0;
	} else {
		if (db_bind_mod(&db_url, &perm_dbf) < 0) {
			LM_ERR("load a database support module\n");
			return -1;
		}

		if (!DB_CAPABILITY(perm_dbf, DB_CAP_QUERY)) {
			LM_ERR("database module does not implement 'query' function\n");
			return -1;
		}
	}

	addr_hash_table_1 = addr_hash_table_2 = 0;
	addr_hash_table = 0;

	db_handle = perm_dbf.init(&db_url);
	if (!db_handle) {
		LM_ERR("unable to connect database\n");
		return -1;
	}

	if(db_check_table_version(&perm_dbf, db_handle, &address_table, TABLE_VERSION) < 0) {
		LM_ERR("error during table version check.\n");
		perm_dbf.close(db_handle);
		return -1;
	}

	addr_hash_table_1 = new_addr_hash_table();
	if (!addr_hash_table_1) return -1;

	addr_hash_table_2  = new_addr_hash_table();
	if (!addr_hash_table_2) goto error;

	addr_hash_table = (struct addr_list ***)shm_malloc
		(sizeof(struct addr_list **));
	if (!addr_hash_table) {
		LM_ERR("no more shm memory for addr_hash_table\n");
		goto error;
	}

	*addr_hash_table = addr_hash_table_1;

	subnet_table_1 = new_subnet_table();
	if (!subnet_table_1) goto error;

	subnet_table_2 = new_subnet_table();
	if (!subnet_table_2) goto error;

	subnet_table = (struct subnet **)shm_malloc(sizeof(struct subnet *));
	if (!subnet_table) {
		LM_ERR("no more shm memory for subnet_table\n");
		goto error;
	}

	*subnet_table = subnet_table_1;

	domain_list_table_1 = new_domain_name_table();
	if (!domain_list_table_1) goto error;

	domain_list_table_2 = new_domain_name_table();
	if (!domain_list_table_2) goto error;

	domain_list_table = (struct domain_name_list ***)shm_malloc(sizeof(struct domain_name_list **));
	if (!domain_list_table) {
		LM_ERR("no more shm memory for domain name table\n");
		goto error;
	}

	*domain_list_table = domain_list_table_1;


	if (reload_address_table() == -1) {
		LM_CRIT("reload of address table failed\n");
		goto error;
	}

	perm_dbf.close(db_handle);
	db_handle = 0;

	return 0;

error:
	if (addr_hash_table_1) {
		free_addr_hash_table(addr_hash_table_1);
		addr_hash_table_1 = 0;
	}
	if (addr_hash_table_2) {
		free_addr_hash_table(addr_hash_table_2);
		addr_hash_table_2 = 0;
	}
	if (addr_hash_table) {
		shm_free(addr_hash_table);
		addr_hash_table = 0;
	}
	if (subnet_table_1) {
		free_subnet_table(subnet_table_1);
		subnet_table_1 = 0;
	}
	if (subnet_table_2) {
		free_subnet_table(subnet_table_2);
		subnet_table_2 = 0;
	}
	if (subnet_table) {
		shm_free(subnet_table);
		subnet_table = 0;
	}

	if (domain_list_table_1) {
		free_domain_name_table(domain_list_table_1);
		domain_list_table_1 = 0;
	}
	if (domain_list_table_2) {
		free_domain_name_table(domain_list_table_2);
		domain_list_table_2 = 0;
	}
	if (domain_list_table) {
		shm_free(domain_list_table);
		domain_list_table = 0;
	}

	perm_dbf.close(db_handle);
	db_handle = 0;
	return -1;
}
Exemple #2
0
/*
 * Initialize data structures
 */
int init_address(struct pm_partition *partition)
{
	struct pm_part_struct *part_struct;
	/* Check if hash table needs to be loaded from address table */
	if (!partition->url.s) {
		LM_INFO("db_url parameter of permissions module not set, "
			"disabling allow_address\n");
		return 0;
	}

	part_struct = pkg_malloc(sizeof (struct pm_part_struct));
	if (part_struct == NULL) {
		LM_ERR("no more pkg mem\n");
		return -1;
	}
	memset(part_struct, 0, sizeof(struct pm_part_struct));

	part_struct->name = partition->name;
	part_struct->url = partition->url;
	part_struct->table = partition->table;

	if (db_bind_mod(&partition->url, &part_struct->perm_dbf) < 0) {
		LM_ERR("load a database support module\n");
		return -1;
	}

	if (!DB_CAPABILITY(part_struct->perm_dbf, DB_CAP_QUERY)) {
		LM_ERR("database module does not implement 'query' function\n");
		return -1;
	}

	part_struct->hash_table_1 = part_struct->hash_table_2 = 0;
	part_struct->hash_table = 0;

	part_struct->db_handle = part_struct->perm_dbf.init(&partition->url);
	if (!part_struct->db_handle) {
		LM_ERR("unable to connect database\n");
		return -1;
	}

	if (db_check_table_version(&part_struct->perm_dbf, part_struct->db_handle,
				&partition->table,
				TABLE_VERSION) < 0) {
		LM_ERR("error during table version check.\n");
		part_struct->perm_dbf.close(part_struct->db_handle);
		return -1;
	}

	part_struct->hash_table_1 = hash_create();
	if (!part_struct->hash_table_1) return -1;

	part_struct->hash_table_2  = hash_create();
	if (!part_struct->hash_table_2) goto error;

	part_struct->hash_table = (struct address_list ***)shm_malloc
							(sizeof(struct address_list **));
	if (!part_struct->hash_table) goto error;

	*part_struct->hash_table = part_struct->hash_table_1;

	part_struct->subnet_table_1 = new_subnet_table();
    if (!part_struct->subnet_table_1) goto error;

    part_struct->subnet_table_2 = new_subnet_table();
    if (!part_struct->subnet_table_2) goto error;

	part_struct->subnet_table = (struct subnet **)shm_malloc(sizeof(struct subnet *));
	if (!part_struct->subnet_table) goto error;

	*part_struct->subnet_table = part_struct->subnet_table_1;

	if (reload_address_table(part_struct) == -1) {
		LM_CRIT("reload of address table failed\n");
		goto error;
	}

	part_struct->perm_dbf.close(part_struct->db_handle);
	part_struct->db_handle = 0;

	add_part_struct(part_struct);

	return 0;

error:
	if (part_struct->hash_table_1) {
		hash_destroy(part_struct->hash_table_1);
		part_struct->hash_table_1 = 0;
	}
	if (part_struct->hash_table_2) {
		hash_destroy(part_struct->hash_table_2);
		part_struct->hash_table_2 = 0;
	}
	if (part_struct->hash_table) {
		shm_free(part_struct->hash_table);
		part_struct->hash_table = 0;
	}

	if (part_struct->subnet_table_1) {
		free_subnet_table(part_struct->subnet_table_1);
		part_struct->subnet_table_1 = 0;
	}

	if (part_struct->subnet_table_2) {
		free_subnet_table(part_struct->subnet_table_2);
		part_struct->subnet_table_2 = 0;
    }
	if (part_struct->subnet_table) {
		shm_free(part_struct->subnet_table);
		part_struct->subnet_table = 0;
	}
	part_struct->perm_dbf.close(part_struct->db_handle);
	part_struct->db_handle = 0;

	pkg_free(part_struct);
	return -1;
}