示例#1
0
int main(int argc, char **argv)
{
    tdb* t = tdb_init();
    assert(tdb_open(t, argv[1]) == 0);
    parse_filter(t, argv[2], 1);
    return 0;
}
示例#2
0
static int cmd_info(const char* db_path)
{
	tdb* db = tdb_init(); assert(db);
	int err = tdb_open(db, db_path);
	if(err) {
		REPORT_ERROR("Failed to open TDB at directory %s. error=%i\n",
			     db_path, err);
		return 1;
	}

	printf("DB at %s:\n"
	       " version: %" PRIu64 "\n"
	       " #trails: %" PRIu64 "\n"
	       " #events: %" PRIu64 "\n"
	       " #fields: %" PRIu64 "\n"
	       "\n"
	       " min timestamp: %" PRIu64 "\n"
	       " max timestamp: %" PRIu64 "\n",
	       db_path,
	       tdb_version(db),
	       tdb_num_trails(db),
	       tdb_num_events(db),
	       tdb_num_fields(db),
	       tdb_min_timestamp(db),
	       tdb_max_timestamp(db));

       printf("\nColumns: \n");
       printf(" field[00] = %s (implicit)\n", tdb_get_field_name(db, 0 ));
       for(unsigned fid = 1; fid < tdb_num_fields(db); ++fid) {
	       printf(" field[%02u] = %s\n", fid, tdb_get_field_name(db, fid));
       }

       tdb_close(db);
       return err;
}
示例#3
0
static tdb *make_tdb(const char *root,
                     const uint64_t *tstamps,
                     uint32_t num,
                     int should_fail)
{
    static uint8_t uuid[16];
    const char *fields[] = {};
    tdb_cons* c = tdb_cons_init();
    test_cons_settings(c);
    uint64_t zero = 0;
    uint32_t i;

    assert(tdb_cons_open(c, root, fields, 0) == 0);

    for (i = 0; i < num; i++)
        assert(tdb_cons_add(c, uuid, tstamps[i], fields, &zero) == 0);

    assert(tdb_cons_finalize(c) ==
           (should_fail ? TDB_ERR_TIMESTAMP_TOO_LARGE: 0));
    tdb_cons_close(c);

    if (!should_fail){
        tdb* t = tdb_init();
        assert(tdb_open(t, root) == 0);
        return t;
    }else
        return NULL;
}
示例#4
0
static tdb *create_db(const char *root, uint32_t id, uint32_t len)
{
    char path[TDB_MAX_PATH_SIZE];
    const char *fields[] = {"id"};
    const char *values[] = {(const char*)&id};
    uint64_t lengths[] = {4};
    uint32_t i, j;
    static uint8_t uuid[16];
    uint64_t timestamp = 100;

    tdb_path(path, "%s.%u", root, id);
    tdb_cons* cons = tdb_cons_init();
    test_cons_settings(cons);
    assert(tdb_cons_open(cons, path, fields, 1) == 0);

    for (i = 1000; i < 1000 + NUM_COMMON_TRAILS; i++){
        memcpy(uuid, &i, 4);
        for (timestamp = 1000, j = 0; j < len; j++){
            assert(tdb_cons_add(cons, uuid, timestamp, values, lengths) == 0);
            timestamp += id;
        }
    }
    assert(tdb_cons_finalize(cons) == 0);
    tdb_cons_close(cons);
    tdb* db = tdb_init();
    assert(tdb_open(db, path) == 0);
    return db;
}
示例#5
0
static int cmd_dump(const char* db_path)
{
	tdb* db = tdb_init(); assert(db);
	int err = tdb_open(db, db_path);
	if(err) {
		REPORT_ERROR("Failed to open TDB at directory %s. error=%i\n",
			     db_path, err);
		return 1;
	}

	tdb_cursor* const c = tdb_cursor_new(db);
	const uint64_t num_trails = tdb_num_trails(db);

	for(uint64_t trail_id = 0; trail_id < num_trails; ++trail_id) {
		err = tdb_get_trail(c, trail_id);
		if(err) {
			REPORT_ERROR("Failed to decode trail %" PRIu64 ". error=%i\n",
				     trail_id, err);

			goto out;
		}

		dump_trail(db, tdb_get_uuid(db, trail_id), c);
		putchar('\n');
	}

out:
	tdb_cursor_free(c);
	tdb_close(db);
	return err;
}
示例#6
0
static int cmd_decode(const char* path, const char** field_names, int names_length)
{
	tdb* db = tdb_init(); assert(db);
	tdb_error err = tdb_open(db, path);
	if(err) {
		REPORT_ERROR("Failed to open TDB. error=%i\n", err);
		return 1;
	}

	tdb_field* ids;
	err = resolve_fieldids(&ids, db, field_names, names_length);
	if(err)
		goto out;

	/** actual field names (except for timestamp) */
	for(int i = 0; i < names_length; ++i) {
		assert(ids[i] > 0 && "reading from timestamp column 0 not supported");
		--ids[i];
	}

	TIMED("cmd_decode", err, do_get_all_and_decode(db, path, ids, (unsigned)names_length));

out:
	tdb_close(db);
	return err;
}
示例#7
0
int main(int argc, char** argv)
{
    tdb* t = tdb_init();
    assert(t && "Expected tdb_init() to succeed.");
    tdb_dontneed(t);
    tdb_close(t);

    return 0;
}
示例#8
0
int main(int argc, char** argv)
{
    static uint8_t uuid[16];
    const char *fields[] = {"a", "b"};
    const char *values[] = {"foo", "ba"};
    const uint64_t lengths[] = {3, 2};
    tdb_opt_value val = {.value = UINT64_MAX};
    uint64_t i;
    char pkgname[4096];
    strcpy(pkgname, getenv("TDB_TMP_DIR"));
    strcat(pkgname, "test.tdb");
    tdb_cons* c = tdb_cons_init();
    test_cons_settings(c);
    assert(tdb_cons_open(c, pkgname, fields, 2) == 0);

    assert(tdb_cons_set_opt(c, TDB_OPT_ONLY_DIFF_ITEMS, TDB_TRUE) ==
           TDB_ERR_UNKNOWN_OPTION);

    assert(tdb_cons_set_opt(c, TDB_OPT_CONS_OUTPUT_FORMAT, val) ==
           TDB_ERR_INVALID_OPTION_VALUE);

    assert(tdb_cons_set_opt(c,
                            TDB_OPT_CONS_OUTPUT_FORMAT,
                            opt_val(TDB_OPT_CONS_OUTPUT_FORMAT_PACKAGE)) == 0);

    assert(tdb_cons_get_opt(c, TDB_OPT_ONLY_DIFF_ITEMS, &val) ==
           TDB_ERR_UNKNOWN_OPTION);

    assert(tdb_cons_get_opt(c, TDB_OPT_CONS_OUTPUT_FORMAT, &val) == 0);
    assert(val.value == TDB_OPT_CONS_OUTPUT_FORMAT_PACKAGE);

    for (i = 0; i < NUM_EVENTS; i++)
       assert(tdb_cons_add(c, uuid, i, values, lengths) == 0);

    assert(tdb_cons_finalize(c) == 0);
    tdb_cons_close(c);
    tdb* t = tdb_init();
    assert(tdb_open(t, pkgname) == 0);

    tdb_cursor *cursor = tdb_cursor_new(t);
    const tdb_event *event;
    assert(tdb_get_trail(cursor, 0) == 0);

    for (i = 0; (event = tdb_cursor_next(cursor)); i++){
        assert(event->timestamp == i);
        assert(event->num_items == 2);
    }
    assert(i == NUM_EVENTS);
    tdb_close(t);
    tdb_cursor_free(cursor);
    return 0;
}
示例#9
0
static int cmd_append_all(const char* output_path, const char* input)
{
	tdb* db = tdb_init(); assert(db);
	int err = tdb_open(db, input);
	if(err) {
		REPORT_ERROR("Failed to open TDB. error=%i\n", err);
		return 1;
	}

	const uint64_t num_fields = tdb_num_fields(db) - 1;
	const char**   field_ids  = duplicate_fieldids(db);
	tdb_cons*      cons       = tdb_cons_init();
	assert(cons);

	err = tdb_cons_open(cons, output_path, field_ids, num_fields);
	if(err) {
		REPORT_ERROR("Failed to create TDB cons. error=%i\n", err);
		goto free_fieldids;
	}

	TIMED("tdb_cons_append()", err, tdb_cons_append(cons, db));
	if(err) {
		REPORT_ERROR("Failed to append DB. error=%i\n", err);
		goto close_cons;
	}

	err = tdb_cons_finalize(cons);
	if(err) {
		REPORT_ERROR("Failed to finalize output DB. error=%i\n", err);
		goto close_cons;
	}

	printf("Successfully converted / rewritten DB.\n");


close_cons:
	tdb_cons_close(cons);

free_fieldids:
	/* to make the compiler not complain about casting const'ness away,
	   let's pull out this small, dirty trick */
	for(uint64_t i = 0; i < num_fields; ++i) {
		void* make_compiler_happy;
		memcpy(&make_compiler_happy, field_ids + i, sizeof(void*));
		free(make_compiler_happy);
	}
	free(field_ids);

	tdb_close(db);

	return err ? 1 : 0;
}
示例#10
0
/**
 * copies a subset of data from one DB into another one. The subset is
 * given by field names
 */
static int cmd_recode(const char* output_path, const char* input,
		      const char** field_names, int names_length)
{
	assert(names_length > 0);

	tdb* const db = tdb_init(); assert(db);
	int err = tdb_open(db, input);
	if(err) {
		REPORT_ERROR("Failed to open TDB. error=%i\n", err);
		return 1;
	}

	tdb_field* field_ids;
	err = resolve_fieldids(&field_ids, db, field_names, names_length);
	if(err < 0) {
		goto free_tdb;
	}

	tdb_cons* const cons = tdb_cons_init();
	assert(cons);

	err = tdb_cons_open(cons, output_path, field_names, (unsigned)names_length);
	if(err) {
		REPORT_ERROR("Failed to create TDB cons. error=%i\n", err);
		goto free_ids;
	}

	TIMED("recode", err, do_recode(cons, db, field_ids, names_length));
	if(err)
		goto close_cons;

	err = tdb_cons_finalize(cons);
	if(err) {
		REPORT_ERROR("Failed to finalize output DB. error=%i\n", err);
	}

close_cons:
	tdb_cons_close(cons);
free_ids:
	free(field_ids);
free_tdb:
	tdb_close(db);
	return err;
}
示例#11
0
static int cmd_get_all_and_decode(char** dbs, int argc)
{
	for(int i = 0; i < argc; ++i) {
		const char* path = dbs[i];
		tdb* db = tdb_init();
		int err = tdb_open(db, path);
		if(err) {
			printf("Error code %i while opening TDB at %s\n", err, path);
			return 1;
		}

		const unsigned int nfields = (unsigned)tdb_num_fields(db) - 1u;
		tdb_field ids[nfields];
		for (unsigned int field_id = 0; field_id < nfields; ++field_id)
			ids[field_id] = field_id;

		TIMED("get_all", err, do_get_all_and_decode(db, path, ids, nfields));
		tdb_close(db);
	}

	return 0;
}
示例#12
0
文件: mdb_tdb.c 项目: andreiw/polaris
const mdb_tdb_ops_t *
mdb_tdb_load(const char *path)
{
	td_err_e (*tdb_init)(void);
	mdb_tdb_lib_t *t;
	td_err_e err;
	void *hdl;

	/*
	 * Search through the existing cache of thread_db libraries and see if
	 * we have this one loaded already.  If so, just return its ops vector.
	 */
	for (t = tdb_list; t != NULL; t = t->tdb_next) {
		if (strcmp(path, t->tdb_pathname) == 0)
			break;
	}

	if (t != NULL)
		return (&t->tdb_ops);

	/*
	 * Otherwise dlmopen the new library, look up its td_init() function,
	 * and call it.  If any of this fails, we return NULL for failure.
	 */
	if (access(path, F_OK) == -1)
		return (NULL);

	if ((hdl = dlmopen(LM_ID_BASE, path, RTLD_LAZY | RTLD_LOCAL)) == NULL) {
		(void) set_errno(EMDB_RTLD);
		return (NULL);
	}

	if ((tdb_init = (td_err_e (*)(void))dlsym(hdl, "td_init")) == NULL) {
		(void) dlclose(hdl);
		(void) set_errno(tdb_to_errno(TD_NOCAPAB));
		return (NULL);
	}

	if ((err = tdb_init()) != TD_OK) {
		(void) dlclose(hdl);
		(void) set_errno(tdb_to_errno(err));
		return (NULL);
	}

	/*
	 * If td_init() succeeds, we can't fail from here on.  Allocate a new
	 * library entry and add it to our linked list.
	 */
	t = mdb_alloc(sizeof (mdb_tdb_lib_t), UM_SLEEP);

	(void) strncpy(t->tdb_pathname, path, MAXPATHLEN);
	t->tdb_pathname[MAXPATHLEN - 1] = '\0';
	t->tdb_handle = hdl;
	t->tdb_next = tdb_list;
	tdb_list = t;

	/*
	 * For each function we need to call in the thread_db library, look it
	 * up using dlsym().  If we find it, add it to the ops vector.  If not,
	 * put the address of our default function (see above) in that slot.
	 */

	t->tdb_ops.td_ta_new = (td_err_e (*)())dlsym(hdl, "td_ta_new");
	if (t->tdb_ops.td_ta_new == NULL)
		t->tdb_ops.td_ta_new = (td_err_e (*)())tdb_notsup;

	t->tdb_ops.td_ta_delete = (td_err_e (*)())dlsym(hdl, "td_ta_delete");
	if (t->tdb_ops.td_ta_delete == NULL)
		t->tdb_ops.td_ta_delete = (td_err_e (*)())tdb_notsup;

	t->tdb_ops.td_ta_thr_iter = (td_err_e (*)())
	    dlsym(hdl, "td_ta_thr_iter");
	if (t->tdb_ops.td_ta_thr_iter == NULL)
		t->tdb_ops.td_ta_thr_iter = (td_err_e (*)())tdb_notsup;

	t->tdb_ops.td_ta_map_id2thr = (td_err_e (*)())
	    dlsym(hdl, "td_ta_map_id2thr");
	if (t->tdb_ops.td_ta_map_id2thr == NULL)
		t->tdb_ops.td_ta_map_id2thr = (td_err_e (*)())tdb_notsup;

	t->tdb_ops.td_ta_map_lwp2thr = (td_err_e (*)())
	    dlsym(hdl, "td_ta_map_lwp2thr");
	if (t->tdb_ops.td_ta_map_lwp2thr == NULL)
		t->tdb_ops.td_ta_map_lwp2thr = (td_err_e (*)())tdb_notsup;

	t->tdb_ops.td_thr_get_info = (td_err_e (*)())
	    dlsym(hdl, "td_thr_get_info");
	if (t->tdb_ops.td_thr_get_info == NULL)
		t->tdb_ops.td_thr_get_info = (td_err_e (*)())tdb_notsup;

	t->tdb_ops.td_thr_getgregs = (td_err_e (*)())
	    dlsym(hdl, "td_thr_getgregs");
	if (t->tdb_ops.td_thr_getgregs == NULL)
		t->tdb_ops.td_thr_getgregs = (td_err_e (*)())tdb_notsup;

	t->tdb_ops.td_thr_setgregs = (td_err_e (*)())
	    dlsym(hdl, "td_thr_setgregs");
	if (t->tdb_ops.td_thr_setgregs == NULL)
		t->tdb_ops.td_thr_setgregs = (td_err_e (*)())tdb_notsup;

	t->tdb_ops.td_thr_getfpregs = (td_err_e (*)())
	    dlsym(hdl, "td_thr_getfpregs");
	if (t->tdb_ops.td_thr_getfpregs == NULL)
		t->tdb_ops.td_thr_getfpregs = (td_err_e (*)())tdb_notsup;

	t->tdb_ops.td_thr_setfpregs = (td_err_e (*)())
	    dlsym(hdl, "td_thr_setfpregs");
	if (t->tdb_ops.td_thr_setfpregs == NULL)
		t->tdb_ops.td_thr_setfpregs = (td_err_e (*)())tdb_notsup;

	t->tdb_ops.td_thr_tlsbase = (td_err_e (*)())
	    dlsym(hdl, "td_thr_tlsbase");
	if (t->tdb_ops.td_thr_tlsbase == NULL)
		t->tdb_ops.td_thr_tlsbase = (td_err_e (*)())tdb_notsup;

#ifdef __sparc
	t->tdb_ops.td_thr_getxregs = (td_err_e (*)())
	    dlsym(hdl, "td_thr_getxregs");
	if (t->tdb_ops.td_thr_getxregs == NULL)
		t->tdb_ops.td_thr_getxregs = (td_err_e (*)())tdb_notsup;

	t->tdb_ops.td_thr_setxregs = (td_err_e (*)())
	    dlsym(hdl, "td_thr_setxregs");
	if (t->tdb_ops.td_thr_setxregs == NULL)
		t->tdb_ops.td_thr_setxregs = (td_err_e (*)())tdb_notsup;
#endif	/* __sparc */

	return (&t->tdb_ops);
}
示例#13
0
int main(int argc, char **argv)
{
    static const char **fields;
    static uint64_t *lengths;
    dsfmt_t state;
    Pvoid_t uuids = NULL;
    tdb_cons* c = tdb_cons_init();
    test_cons_settings(c);
    uint64_t i, j;
    __uint128_t prev_uuid = 0;
    Word_t key;
    int tst;

    assert(tdb_cons_open(c, argv[1], fields, 0) == 0);
    dsfmt_init_gen_rand(&state, 2489);

    for (i = 0; i < NUM_TRAILS; i++){
        uint8_t uuid[16];
        gen_random_uuid(uuid, &state);
        memcpy(&key, uuid, 8);

        J1S(tst, uuids, key);
        if (!tst){
            printf("half-word collision! change random seed!\n");
            return -1;
        }

        for (j = 0; j < NUM_EVENTS; j++)
            tdb_cons_add(c, uuid, i * 100 + j, fields, lengths);
    }
    J1C(key, uuids, 0, -1);
    assert(key == NUM_TRAILS);
    assert(tdb_cons_finalize(c) == 0);
    tdb_cons_close(c);

    tdb* t = tdb_init();
    assert(tdb_open(t, argv[1]) == 0);

    assert(tdb_num_trails(t) == NUM_TRAILS);
    assert(tdb_num_events(t) == NUM_TRAILS * NUM_EVENTS);

    for (i = 0; i < NUM_TRAILS; i++){
        __uint128_t this_uuid;

        /* uuids must be monotonically increasing */
        memcpy(&this_uuid, tdb_get_uuid(t, i), 16);
        assert(this_uuid > prev_uuid);
        prev_uuid = this_uuid;

        /* remove this uuid from the uuid set and make sure it exists */
        memcpy(&key, &this_uuid, 8);
        J1U(tst, uuids, key);
        assert(tst == 1);
    }

    /* make sure we retrieved all uuids */
    J1C(key, uuids, 0, -1);
    assert(key == 0);

    return 0;
}
示例#14
0
/*
 * IPSEC <> netlink interface
 * Copyright (C) 1996, 1997  John Ioannidis.
 * Copyright (C) 1998, 1999, 2000, 2001  Richard Guy Briggs.
 * 
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */

char ipsec_netlink_c_version[] = "RCSID $Id: ipsec_netlink.c,v 1.56 2002/01/29 17:17:55 mcr Exp $";

#include <linux/config.h>
#include <linux/version.h>
#include <linux/kernel.h> /* printk() */

#include "ipsec_param.h"

#ifdef MALLOC_SLAB
# include <linux/slab.h> /* kmalloc() */
#else /* MALLOC_SLAB */
# include <linux/malloc.h> /* kmalloc() */
#endif /* MALLOC_SLAB */
#include <linux/errno.h>  /* error codes */
#include <linux/types.h>  /* size_t */
#include <linux/interrupt.h> /* mark_bh */

#include <linux/netdevice.h>   /* struct device, and other headers */
#include <linux/etherdevice.h> /* eth_type_trans */
#include <linux/ip.h>          /* struct iphdr */
#include <linux/skbuff.h>
#include <freeswan.h>
#ifdef SPINLOCK
# ifdef SPINLOCK_23
#  include <linux/spinlock.h> /* *lock* */
# else /* 23_SPINLOCK */
#  include <asm/spinlock.h> /* *lock* */
# endif /* 23_SPINLOCK */
#endif /* SPINLOCK */
#ifdef NET_21
# include <asm/uaccess.h>
# include <linux/in6.h>
# define ip_chk_addr inet_addr_type
# define IS_MYADDR RTN_LOCAL
#endif
#include <asm/checksum.h>
#include <net/ip.h>
#ifdef NETLINK_SOCK
# include <linux/netlink.h>
#else
# include <net/netlink.h>
#endif

#include "radij.h"
#include "ipsec_encap.h"
#include "ipsec_radij.h"
#include "ipsec_netlink.h"
#include "ipsec_xform.h"

#include "ipsec_rcv.h"
#include "ipsec_ah.h"
#include "ipsec_esp.h"

#ifdef CONFIG_IPSEC_DEBUG
# include "ipsec_tunnel.h"
#endif /* CONFIG_IPSEC_DEBUG */

#include <pfkeyv2.h>
#include <pfkey.h>

#ifdef CONFIG_IPSEC_DEBUG
 int debug_netlink = 0;
#endif /* CONFIG_IPSEC_DEBUG */

#define SENDERR(_x) do { len = -(_x); goto errlab; } while (0)

#if 0
int 
#ifdef NETLINK_SOCK
ipsec_callback(int proto, struct sk_buff *skb)
#else /* NETLINK_SOCK */
ipsec_callback(struct sk_buff *skb)
#endif /* NETLINK_SOCK */
{
	/*
	 * this happens when we write to /dev/ipsec (c 36 10)
	 */
	int len = skb->len;
	u_char *dat = (u_char *)skb->data;
	struct encap_msghdr *em	= (struct encap_msghdr *)dat;
	struct tdb *tdbp, *tprev;
	int i, nspis, error = 0;
#ifdef CONFIG_IPSEC_DEBUG
	struct eroute *eret;
	char sa[SATOA_BUF];
	size_t sa_len;
	struct sk_buff *first, *last;


	sa_len = satoa(em->em_said, 0, sa, SATOA_BUF);

	if(debug_netlink) {
		printk("klips_debug:ipsec_callback: "
		       "skb=0x%p skblen=%ld em_magic=%d em_type=%d\n",
		       skb,
		       (unsigned long int)skb->len,
		       em->em_magic,
		       em->em_type);
		switch(em->em_type) {
		case EMT_SETDEBUG:
			printk("klips_debug:ipsec_callback: "
			       "set ipsec_debug level\n");
			break;
		case EMT_DELEROUTE:
		case EMT_CLREROUTE:
		case EMT_CLRSPIS:
			break;
		default:
			printk("klips_debug:ipsec_callback: "
			       "called for SA:%s\n",
			       sa_len ? sa : " (error)");
		}
	}
#endif /* CONFIG_IPSEC_DEBUG */

	/* XXXX Temporarily disable netlink I/F code until it gets permanantly
	   ripped out in favour of PF_KEYv2 I/F. */
	SENDERR(EPROTONOSUPPORT);

	/*	em = (struct encap_msghdr *)dat; */
	if (em->em_magic != EM_MAGIC) {
		printk("klips_debug:ipsec_callback: "
		       "bad magic=%d failed, should be %d\n",
		       em->em_magic,
		       EM_MAGIC);
		SENDERR(EINVAL);
	}
	switch (em->em_type) {
	case EMT_SETDEBUG:
#ifdef CONFIG_IPSEC_DEBUG
		if(em->em_db_nl >> (sizeof(em->em_db_nl) * 8 - 1)) {
			em->em_db_nl &= ~(1 << (sizeof(em->em_db_nl) * 8 -1));
			debug_tunnel  |= em->em_db_tn;
			debug_netlink |= em->em_db_nl;
			debug_xform   |= em->em_db_xf;
			debug_eroute  |= em->em_db_er;
			debug_spi     |= em->em_db_sp;
			debug_radij   |= em->em_db_rj;
			debug_esp     |= em->em_db_es;
			debug_ah      |= em->em_db_ah;
			debug_rcv     |= em->em_db_rx;
			debug_pfkey   |= em->em_db_ky;
			if(debug_netlink)
				printk("klips_debug:ipsec_callback: set\n");
		} else {
			if(debug_netlink)
				printk("klips_debug:ipsec_callback: unset\n");
			debug_tunnel  &= em->em_db_tn;
			debug_netlink &= em->em_db_nl;
			debug_xform   &= em->em_db_xf;
			debug_eroute  &= em->em_db_er;
			debug_spi     &= em->em_db_sp;
			debug_radij   &= em->em_db_rj;
			debug_esp     &= em->em_db_es;
			debug_ah      &= em->em_db_ah;
			debug_rcv     &= em->em_db_rx;
			debug_pfkey   &= em->em_db_ky;
		}
#else /* CONFIG_IPSEC_DEBUG */
		printk("klips_debug:ipsec_callback: "
		       "debugging not enabled\n");
		SENDERR(EINVAL);
#endif /* CONFIG_IPSEC_DEBUG */
		break;

	case EMT_SETEROUTE:
		if ((error = ipsec_makeroute(&(em->em_eaddr), &(em->em_emask), em->em_ersaid, 0, NULL, NULL, NULL)))
			SENDERR(-error);
		break;

	case EMT_REPLACEROUTE:
		if ((error = ipsec_breakroute(&(em->em_eaddr), &(em->em_emask), &first, &last)) == EINVAL) {
			kfree_skb(first);
			kfree_skb(last);
			SENDERR(-error);
		}
		if ((error = ipsec_makeroute(&(em->em_eaddr), &(em->em_emask), em->em_ersaid, NULL, NULL)))
			SENDERR(-error);
		break;

	case EMT_DELEROUTE:
		if ((error = ipsec_breakroute(&(em->em_eaddr), &(em->em_emask), &first, &last)))
			kfree_skb(first);
			kfree_skb(last);
			SENDERR(-error);
		break;

	case EMT_CLREROUTE:
		if ((error = ipsec_cleareroutes()))
			SENDERR(-error);
		break;

	case EMT_SETSPI:
		if (em->em_if >= 5)	/* XXX -- why 5? */
			SENDERR(ENODEV);
		
		tdbp = gettdb(&(em->em_said));
		if (tdbp == NULL) {
			tdbp = (struct tdb *)kmalloc(sizeof (*tdbp), GFP_ATOMIC);

			if (tdbp == NULL)
				SENDERR(ENOBUFS);
			
			memset((caddr_t)tdbp, 0, sizeof(*tdbp));
			
			tdbp->tdb_said = em->em_said;
			tdbp->tdb_flags = em->em_flags;
			
			if(ip_chk_addr((unsigned long)em->em_said.dst.s_addr) == IS_MYADDR) {
				tdbp->tdb_flags |= EMT_INBOUND;
			}
			KLIPS_PRINT(debug_netlink & DB_NL_TDBCB,
				    "klips_debug:ipsec_callback: "
				    "existing Tunnel Descriptor Block not found (this is good) for SA: %s, %s-bound, allocating.\n",
				    sa_len ? sa : " (error)",
				    (tdbp->tdb_flags & EMT_INBOUND) ? "in" : "out");

/* XXX  		tdbp->tdb_rcvif = &(enc_softc[em->em_if].enc_if);*/
			tdbp->tdb_rcvif = NULL;
		} else {
			KLIPS_PRINT(debug_netlink & DB_NL_TDBCB,
				    "klips_debug:ipsec_callback: "
				    "EMT_SETSPI found an old Tunnel Descriptor Block for SA: %s, delete it first.\n",
				    sa_len ? sa : " (error)");
			SENDERR(EEXIST);
		}
		
		if ((error = tdb_init(tdbp, em))) {
			KLIPS_PRINT(debug_netlink & DB_NL_TDBCB,
				    "klips_debug:ipsec_callback: "
				    "EMT_SETSPI not successful for SA: %s, deleting.\n",
				    sa_len ? sa : " (error)");
			ipsec_tdbwipe(tdbp);
			
			SENDERR(-error);
		}

		tdbp->tdb_lifetime_addtime_c = jiffies/HZ;
		tdbp->tdb_state = 1;
		if(!tdbp->tdb_lifetime_allocations_c) {
			tdbp->tdb_lifetime_allocations_c += 1;
		}

		puttdb(tdbp);
		KLIPS_PRINT(debug_netlink & DB_NL_TDBCB,
			    "klips_debug:ipsec_callback: "
			    "EMT_SETSPI successful for SA: %s\n",
			    sa_len ? sa : " (error)");
		break;
		
	case EMT_DELSPI:
		if (em->em_if >= 5)	/* XXX -- why 5? */
			SENDERR(ENODEV);
		
		spin_lock_bh(&tdb_lock);
		
		tdbp = gettdb(&(em->em_said));
		if (tdbp == NULL) {
			KLIPS_PRINT(debug_netlink & DB_NL_TDBCB,
				    "klips_debug:ipsec_callback: "
				    "EMT_DELSPI Tunnel Descriptor Block not found for SA%s, could not delete.\n",
				    sa_len ? sa : " (error)");
			spin_unlock_bh(&tdb_lock);
			SENDERR(ENXIO);  /* XXX -- wrong error message... */
		} else {
			if((error = deltdbchain(tdbp))) {
				spin_unlock_bh(&tdb_lock);
				SENDERR(-error);
			}
		}
		spin_unlock_bh(&tdb_lock);

		break;
		
	case EMT_GRPSPIS:
		nspis = (len - EMT_GRPSPIS_FLEN) / sizeof(em->em_rel[0]);
		if ((nspis * (sizeof(em->em_rel[0]))) != (len - EMT_GRPSPIS_FLEN)) {
			printk("klips_debug:ipsec_callback: "
			       "EMT_GRPSPI message size incorrect, expected nspis(%d)*%d, got %d.\n",
			       nspis,
			       sizeof(em->em_rel[0]),
			       (len - EMT_GRPSPIS_FLEN));
			SENDERR(EINVAL);
			break;
		}
		
		spin_lock_bh(&tdb_lock);

		for (i = 0; i < nspis; i++) {
			KLIPS_PRINT(debug_netlink,
				    "klips_debug:ipsec_callback: "
				    "EMT_GRPSPI for SA(%d) %s,\n",
				    i,
				    sa_len ? sa : " (error)");
			if ((tdbp = gettdb(&(em->em_rel[i].emr_said))) == NULL) {
				KLIPS_PRINT(debug_netlink,
					    "klips_debug:ipsec_callback: "
					    "EMT_GRPSPI Tunnel Descriptor Block not found for SA%s, could not group.\n",
					    sa_len ? sa : " (error)");
				spin_unlock_bh(&tdb_lock);
				SENDERR(ENXIO);
			} else {
				if(tdbp->tdb_inext || tdbp->tdb_onext) {
					KLIPS_PRINT(debug_netlink,
						    "klips_debug:ipsec_callback: "
						    "EMT_GRPSPI Tunnel Descriptor Block already grouped for SA: %s, can't regroup.\n",
						    sa_len ? sa : " (error)");
					spin_unlock_bh(&tdb_lock);
					SENDERR(EBUSY);
				}
				em->em_rel[i].emr_tdb = tdbp;
			}
		}
		tprev = em->em_rel[0].emr_tdb;
		tprev->tdb_inext = NULL;
		for (i = 1; i < nspis; i++) {
			tdbp = em->em_rel[i].emr_tdb;
			tprev->tdb_onext = tdbp;
			tdbp->tdb_inext = tprev;
			tprev = tdbp;
		}
		tprev->tdb_onext = NULL;

		spin_unlock_bh(&tdb_lock);

		error = 0;
		break;
		
	case EMT_UNGRPSPIS:
		if (len != (8 + (sizeof(struct sa_id) + sizeof(struct tdb *)) /* 12 */) ) {
			printk("klips_debug:ipsec_callback: "
			       "EMT_UNGRPSPIS message size incorrect, expected %d, got %d.\n",
			       8 + (sizeof(struct sa_id) + sizeof(struct tdb *)),
				    len);
			SENDERR(EINVAL);
			break;
		}
		
		spin_lock_bh(&tdb_lock);

		if ((tdbp = gettdb(&(em->em_rel[0].emr_said))) == NULL) {
			KLIPS_PRINT(debug_netlink,
				    "klips_debug:ipsec_callback: "
				    "EMT_UGRPSPI Tunnel Descriptor Block not found for SA%s, could not ungroup.\n",
				    sa_len ? sa : " (error)");
			spin_unlock_bh(&tdb_lock);
			SENDERR(ENXIO);
		}
		while(tdbp->tdb_onext) {
			tdbp = tdbp->tdb_onext;
		}
		while(tdbp->tdb_inext) {
			tprev = tdbp;
			tdbp = tdbp->tdb_inext;
			tprev->tdb_inext = NULL;
			tdbp->tdb_onext = NULL;
		}

		spin_unlock_bh(&tdb_lock);

		break;
		
	case EMT_CLRSPIS:
		KLIPS_PRINT(debug_netlink,
			    "klips_debug:ipsec_callback: "
			    "spi clear called.\n");
		if (em->em_if >= 5)	/* XXX -- why 5? */
			SENDERR(ENODEV);
		ipsec_tdbcleanup(0);
		break;
	default:
		KLIPS_PRINT(debug_netlink,
			    "klips_debug:ipsec_callback: "
			    "unknown message type\n");
		SENDERR(EINVAL);
	}
示例#15
0
int main(int argc, char** argv)
{
    static uint8_t uuid[16];
    const char *fields[] = {"a", "b"};
    uint64_t lengths[] = {1, 1};
    char val1 = 0;
    char val2 = 0;
    char *values[] = {&val1, &val2};
    uint64_t i, j, num_events = 0;

    tdb_cons* c = tdb_cons_init();
    test_cons_settings(c);

    assert(tdb_cons_open(c, getenv("TDB_TMP_DIR"), fields, 2) == 0);

    for (i = 1; i < NUM_TRAILS + 1; i++){
        int x = (i % 5) + 1;
        memcpy(uuid, &i, 8);
        /* make sure at least some trails are longer than
           CURSOR_BUFFER_NUM_EVENTS */
        for (j = 0; j < i * 10; j++){
            /* let's add some complexity in edge encoding */
            val1 = (j % 10) ? 1: 0;
            val2 = (j % x) ? 1: 0;
            assert(tdb_cons_add(c, uuid, j, (const char**)values, lengths) == 0);
            ++num_events;
        }
    }
    assert(tdb_cons_finalize(c) == 0);
    tdb_cons_close(c);

    tdb* t = tdb_init();
    assert(tdb_open(t, getenv("TDB_TMP_DIR")) == 0);
    tdb_cursor *cursor = tdb_cursor_new(t);

    assert(tdb_num_events(t) == num_events);

    for (num_events = 0, i = 0; i < NUM_TRAILS; i++){
        const tdb_event *event;
        int x = ((i + 1) % 5) + 1;

        assert(tdb_get_trail(cursor, i) == 0);

        /* test tdb_get_trail_length */

        uint64_t n = tdb_get_trail_length(cursor);
        assert(n == (i + 1) * 10);
        num_events += n;
        assert(tdb_get_trail_length(cursor) == 0);

        /* test cursor */

        assert(tdb_get_trail(cursor, i) == 0);
        j = 0;
        while ((event = tdb_cursor_next(cursor))){
            const char *v;
            uint64_t len;

            assert(event->timestamp == j);
            assert(event->num_items == 2);
            val1 = (j % 10) ? 1: 0;
            val2 = (j % x) ? 1: 0;

            v = tdb_get_item_value(t, event->items[0], &len);
            assert(len == 1);
            assert(*v == val1);

            v = tdb_get_item_value(t, event->items[1], &len);
            assert(len == 1);
            assert(*v == val2);

            ++j;
        }
    }

    assert(tdb_num_events(t) == num_events);

    tdb_cursor_free(cursor);
    tdb_close(t);
    return 0;
}
示例#16
0
文件: tdbadm.c 项目: jgarzik/tabled
int main(int argc, char *argv[])
{
	unsigned int db_flags;
	error_t aprc;
	int rc = 1;

	aprc = argp_parse(&argp, argc, argv, 0, NULL, NULL);
	if (aprc) {
		fprintf(stderr, "argp_parse failed: %s\n", strerror(aprc));
		return 1;
	}

	read_config();

	if (!tdb_dir)
		die("no tdb dir (-t) specified\n");

	if (tdb_init(&tdb, tdb_dir, NULL, "tdbadm", false,
		     0, NULL, true, tdb_state_cb))
		goto err_dbinit;

	/* Usually takes about 12s, if vote is involved. */
	while (!tdb_is_master)
		sleep(2);

	db_flags = DB_CREATE | DB_THREAD;
	if (tdb_up(&tdb, db_flags))
		goto err_dbup;

	switch (mode_adm) {
	case mode_user:
		do_mode_user();
		break;
	case mode_user_list:
		do_user_list();
		break;
	case mode_bucket_list:
		do_bucket_list();
		break;
	case mode_acl_list:
		do_acl_list();
		break;
	case mode_obj_list:
		do_obj_list();
		break;
	case mode_all_lists:
		printf("Users:\n");
		do_user_list();

		printf("\nBuckets:\n");
		do_bucket_list();

		printf("\nACLs:\n");
		do_acl_list();

		printf("\nObjects:\n");
		do_obj_list();

		printf("\nObjectCount: ");
		do_obj_cnt();
		break;
	default:
		fprintf(stderr, "%s: invalid mode\n", argv[0]);
		goto err_act;
	}

	rc = 0;

 err_act:
	tdb_down(&tdb);
 err_dbup:
	tdb_fini(&tdb);
 err_dbinit:
	return rc;
}