Exemplo n.º 1
0
void
case_dict_iter()
{
    struct dict *dict = dict();
    char *key1 = "key1"; size_t len1 = 4;
    char *key2 = "key2"; size_t len2 = 4;
    char *key3 = "key3"; size_t len3 = 4;
    char *key4 = "key4"; size_t len4 = 4;
    char *key5 = "key5"; size_t len5 = 4;
    char *key6 = "key6"; size_t len6 = 4;
    assert(dict_set(dict, key1, len1, "val1") == DICT_OK);
    assert(dict_set(dict, key2, len2, "val2") == DICT_OK);
    assert(dict_set(dict, key3, len3, "val3") == DICT_OK);
    assert(dict_set(dict, key4, len4, "val4") == DICT_OK);
    assert(dict_set(dict, key5, len5, "val5") == DICT_OK);
    assert(dict_set(dict, key6, len6, "val6") == DICT_OK);

    struct dict_iter *iter = dict_iter(dict);

    assert(dict_iter_next(iter) != NULL);
    assert(dict_iter_next(iter) != NULL);
    assert(dict_iter_next(iter) != NULL);
    assert(dict_iter_next(iter) != NULL);
    assert(dict_iter_next(iter) != NULL);
    assert(dict_iter_next(iter) != NULL);
    assert(dict_iter_next(iter) == NULL);
    dict_iter_free(iter);
    dict_free(dict);
}
Exemplo n.º 2
0
static PyObject *
dict_to_py(struct dict *dict)
{
	PyObject	*o, *s;
	const char *key;
	char *value;
	void *iter;

	o = PyDict_New();

	iter = NULL;
	while (dict_iter(dict, &iter, &key, (void **)&value)) {
		s = PyString_FromString(value);
		if (s == NULL)
			goto fail;
		if (PyDict_SetItemString(o, key, s) == -1)
			goto fail;
	}

	return o;

    fail:
	if (o)
		Py_DECREF(o);
	if (s)
		Py_DECREF(s);

	return NULL;
}
Exemplo n.º 3
0
static int
imsg_add_params(struct ibuf *buf, struct dict *params)
{
	size_t count;
	const char *key;
	char *value;
	void *iter;

	count = 0;
	if (params)
		count = dict_count(params);

	if (imsg_add(buf, &count, sizeof(count)) == -1)
		return (-1);

	if (count == 0)
		return (0);

	iter = NULL;
	while (dict_iter(params, &iter, &key, (void **)&value)) {
		if (imsg_add(buf, key, strlen(key) + 1) == -1)
			return (-1);
		if (imsg_add(buf, value, strlen(value) + 1) == -1)
			return (-1);
	}

	return (0);
}
Exemplo n.º 4
0
Arquivo: ca.c Projeto: gunhu/OpenSMTPD
void
ca_init(void)
{
	BIO		*in = NULL;
	EVP_PKEY	*pkey = NULL;
	struct pki	*pki;
	const char	*k;
	void		*iter_dict;

	log_debug("debug: init private ssl-tree");
	iter_dict = NULL;
	while (dict_iter(env->sc_pki_dict, &iter_dict, &k, (void **)&pki)) {
		if (pki->pki_key == NULL)
			continue;

		if ((in = BIO_new_mem_buf(pki->pki_key,
		    pki->pki_key_len)) == NULL)
			fatalx("ca_launch: key");

		if ((pkey = PEM_read_bio_PrivateKey(in,
		    NULL, NULL, NULL)) == NULL)
			fatalx("ca_launch: PEM");
		BIO_free(in);

		pki->pki_pkey = pkey;

		explicit_bzero(pki->pki_key, pki->pki_key_len);
		free(pki->pki_key);
		pki->pki_key = NULL;
	}
}
Exemplo n.º 5
0
static int
table_sqlite_fetch(int service, char *dst, size_t sz)
{
	const char	*k;
	int		 s;

	if (service != K_SOURCE)
		return (-1);

	if (stmt_fetch_source == NULL)
		return (-1);

	if (source_ncall < source_refresh &&
	    time(NULL) - source_update < source_expire)
	    goto fetch;

	source_iter = NULL;
	while (dict_poproot(&sources, NULL))
		;

	while ((s = sqlite3_step(stmt_fetch_source)) == SQLITE_ROW)
		dict_set(&sources, sqlite3_column_text(stmt_fetch_source, 0), NULL);

	if (s != SQLITE_DONE)
		log_warnx("warn: table-sqlite: sqlite3_step: %s",
		    sqlite3_errmsg(db));

	sqlite3_reset(stmt_fetch_source);

	source_update = time(NULL);
	source_ncall = 0;

    fetch:

	source_ncall += 1;

        if (! dict_iter(&sources, &source_iter, &k, (void **)NULL)) {
		source_iter = NULL;
		if (! dict_iter(&sources, &source_iter, &k, (void **)NULL))
			return (0);
	}

	if (strlcpy(dst, k, sz) >= sz)
		return (-1);

	return (1);
}
Exemplo n.º 6
0
static int
table_static_fetch(void *hdl, enum table_service service, union lookup *lk)
{
	struct table   *t = hdl;
	const char     *k;

	if (! dict_iter(&t->t_dict, &t->t_iter, &k, (void **)NULL)) {
		t->t_iter = NULL;
		if (! dict_iter(&t->t_dict, &t->t_iter, &k, (void **)NULL))
			return 0;
	}

	if (lk == NULL)
		return 1;

	return table_parse_lookup(service, NULL, k, lk);
}
Exemplo n.º 7
0
void dictExample() {
	dict_t *d = (dict_t *)malloc(sizeof(dict_t));
	char *var; 
	
	
	char *key1 = strToHeap("key1");
	char *key2 = strToHeap("key2");
	char *key3 = strToHeap("key3");
	
	char *value1 = strToHeap("var1");
	char *value2 = strToHeap("var2");
	char *value3 = strToHeap("var3");
	
	dict_init(d); 
	dict_set(d, key1, value1); 
	dict_set(d, key2, value2); 
	dict_set(d, key3, value3); 
	
	dict_get(d, "key1", (void **)&var); 
	printf("key1=>%s\n", var);
	dict_get(d, "key2", (void **)&var); 
	printf("key1=>%s\n", var);
	dict_get(d, "key3", (void **)&var); 
	printf("key1=>%s\n", var);
	
	printf("dict size:%d\n", dict_size(d));
	
	
	
	if (1 == dict_del(d, key3)) {
		key3 = NULL;
		value3 = NULL;
		printf("del key3 done\n");
	}
	
	char **ks = (char **)malloc(dict_size(d)*sizeof(char*));
	int i; 
	dict_keys(d, ks); 
	for(i = 0; i < dict_size(d); i++)
		printf("%s ",*(ks+i) );
	printf("\n");
	
	char *k, *v; 
	while(dict_iter(d, &k, (void **)&v))
		printf("%s = >%s\n", k, v);
	
	dict_reset(d); 
	
	dict_destory(d); 
	free(d); 
}
Exemplo n.º 8
0
static int
table_static_lookup(void *hdl, const char *key, enum table_service service,
    union lookup *lk)
{
	struct table   *m  = hdl;
	char	       *line;
	int		ret;
	int	       (*match)(const char *, const char *) = NULL;
	size_t		i;
	void	       *iter;
	const char     *k;
	char	       *v;

	for (i = 0; i < nitems(keycmp); ++i)
		if (keycmp[i].service == service)
			match = keycmp[i].func;

	line = NULL;
	iter = NULL;
	ret = 0;
	while (dict_iter(&m->t_dict, &iter, &k, (void **)&v)) {
		if (match) {
			if (match(key, k)) {
				line = v;
				ret = 1;
			}
		}
		else {
			if (strcmp(key, k) == 0) {
				line = v;
				ret = 1;
			}
		}
		if (ret)
			break;
	}

	if (lk == NULL)
		return ret ? 1 : 0;

	if (ret == 0)
		return 0;

	return table_parse_lookup(service, key, line, lk);
}
Exemplo n.º 9
0
void
m_add_params(struct mproc *m, struct dict *d)
{
	const char *key;
	char *value;
	void *iter;

	if (d == NULL) {
		m_add_size(m, 0);
		return;
	}
	m_add_size(m, dict_count(d));
	iter = NULL;
	while (dict_iter(d, &iter, &key, (void **)&value)) {
		m_add_string(m, key);
		m_add_string(m, value);
	}
}
Exemplo n.º 10
0
static int
table_postgres_fetch(int service, char *dst, size_t sz)
{
	char		*stmt;
	PGresult	*res;
	const char	*k, *errfld;
	int		 i;

	if (config->db == NULL && config_connect(config) == 0)
		return (-1);

    retry:

	if (service != K_SOURCE)
		return (-1);

	stmt = config->stmt_fetch_source;

	if (stmt == NULL)
		return (-1);

	if (config->source_ncall < config->source_refresh &&
	    time(NULL) - config->source_update < config->source_expire)
	    goto fetch;

	res = PQexecPrepared(config->db, stmt, 0, NULL, NULL, NULL, 0);

	if (PQresultStatus(res) != PGRES_TUPLES_OK) {
		errfld = PQresultErrorField(res, PG_DIAG_SQLSTATE);
		if (errfld[0] == '0' && errfld[1] == '8') {
			log_warnx("warn: table-postgres: trying to reconnect after error: %s",
			    PQerrorMessage(config->db));
			PQclear(res);
			if (config_connect(config))
				goto retry;
			return (-1);
		}
		log_warnx("warn: table-postgres: PQexecPrepared: %s",
		    PQerrorMessage(config->db));
		PQclear(res);
		return (-1);
	}

	config->source_iter = NULL;
	while (dict_poproot(&config->sources, NULL))
		;

	for (i = 0; i < PQntuples(res); i++)
		dict_set(&config->sources, PQgetvalue(res, i, 0), NULL);

	PQclear(res);

	config->source_update = time(NULL);
	config->source_ncall = 0;

    fetch:

	config->source_ncall += 1;

	if (! dict_iter(&config->sources, &config->source_iter, &k, (void **)NULL)) {
		config->source_iter = NULL;
		if (! dict_iter(&config->sources, &config->source_iter, &k, (void **)NULL))
			return (0);
	}

	if (strlcpy(dst, k, sz) >= sz)
		return (-1);

	return (1);
}
Exemplo n.º 11
0
static int
table_mysql_fetch(int service, char *dst, size_t sz)
{
	MYSQL_STMT	*stmt;
	const char	*k;
	int		 s;

    retry:

	if (service != K_SOURCE)
		return (-1);

	stmt = config->stmt_fetch_source;

	if (stmt == NULL)
		return (-1);

	if (config->source_ncall < config->source_refresh &&
	    time(NULL) - config->source_update < config->source_expire)
	    goto fetch;

	if (mysql_stmt_execute(stmt)) {
		if (mysql_stmt_errno(stmt) == CR_SERVER_LOST ||
		    mysql_stmt_errno(stmt) == CR_SERVER_GONE_ERROR ||
		    mysql_stmt_errno(stmt) == CR_COMMANDS_OUT_OF_SYNC) {
			log_warnx("warn: table-mysql: trying to reconnect after error: %s",
			    mysql_stmt_error(stmt));
			if (config_connect(config))
				goto retry;
			return (-1);
		}
		log_warnx("warn: table-mysql: mysql_stmt_execute: %s",
		    mysql_stmt_error(stmt));
		return (-1);
	}

	config->source_iter = NULL;
	while(dict_poproot(&config->sources, NULL, NULL))
		;

	while ((s = mysql_stmt_fetch(stmt)) == 0)
		dict_set(&config->sources, results_buffer[0], NULL);

	if (s && s != MYSQL_NO_DATA)
		log_warnx("warn: table-mysql: mysql_stmt_fetch: %s",
		    mysql_stmt_error(stmt));

	if (mysql_stmt_free_result(stmt))
		log_warnx("warn: table-mysql: mysql_stmt_free_result: %s",
		    mysql_stmt_error(stmt));

	config->source_update = time(NULL);
	config->source_ncall = 0;

    fetch:

	config->source_ncall += 1;

	if (! dict_iter(&config->sources, &config->source_iter, &k, (void **)NULL)) {
		config->source_iter = NULL;
		if (! dict_iter(&config->sources, &config->source_iter, &k, (void **)NULL))
			return (0);
	}

	if (strlcpy(dst, k, sz) >= sz)
		return (-1);

	return (1);
}
Exemplo n.º 12
0
void
purge_config(uint8_t what)
{
    struct listener	*l;
    struct table	*t;
    struct rule	*r;
    struct pki	*p;
    const char	*k;
    void		*iter_dict;

    if (what & PURGE_LISTENERS) {
        while ((l = TAILQ_FIRST(env->sc_listeners)) != NULL) {
            TAILQ_REMOVE(env->sc_listeners, l, entry);
            free(l);
        }
        free(env->sc_listeners);
        env->sc_listeners = NULL;
    }
    if (what & PURGE_TABLES) {
        while (dict_root(env->sc_tables_dict, NULL, (void **)&t))
            table_destroy(t);
        free(env->sc_tables_dict);
        env->sc_tables_dict = NULL;
    }
    if (what & PURGE_RULES) {
        while ((r = TAILQ_FIRST(env->sc_rules)) != NULL) {
            TAILQ_REMOVE(env->sc_rules, r, r_entry);
            free(r);
        }
        free(env->sc_rules);
        env->sc_rules = NULL;
    }
    if (what & PURGE_PKI) {
        while (dict_poproot(env->sc_pki_dict, (void **)&p)) {
            explicit_bzero(p->pki_cert, p->pki_cert_len);
            free(p->pki_cert);
            if (p->pki_key) {
                explicit_bzero(p->pki_key, p->pki_key_len);
                free(p->pki_key);
            }
            if (p->pki_pkey)
                EVP_PKEY_free(p->pki_pkey);
            free(p);
        }
        free(env->sc_pki_dict);
        env->sc_pki_dict = NULL;
    } else if (what & PURGE_PKI_KEYS) {
        iter_dict = NULL;
        while (dict_iter(env->sc_pki_dict, &iter_dict, &k,
                         (void **)&p)) {
            explicit_bzero(p->pki_cert, p->pki_cert_len);
            free(p->pki_cert);
            p->pki_cert = NULL;
            if (p->pki_key) {
                explicit_bzero(p->pki_key, p->pki_key_len);
                free(p->pki_key);
                p->pki_key = NULL;
            }
            if (p->pki_pkey)
                EVP_PKEY_free(p->pki_pkey);
            p->pki_pkey = NULL;
        }
    }
}