コード例 #1
0
ファイル: unit_test.c プロジェクト: eroullit/net-toolbox
static void check_str_wrapper(void)
{
	int ret;
	hi_handle_t *hi_handle;
	char key[32];
	uint16_t i;
	const char *data = "data element";
	void *data_ptr;

	fputs(" o check string wrapper functions tests ...", stdout);

	ret = hi_init_str(&hi_handle, 23);
	assert(ret == 0);

	for (i = 0; i < 23; i++)
	{
		sprintf(key, "%u", i);

		ret = hi_insert_str(hi_handle, key, data);
		assert(ret == 0);

		ret = hi_get_str(hi_handle, key, &data_ptr);
		assert(ret == 0);
	}

	ret = hi_fini(hi_handle);
	assert(ret == 0);

	puts(" passed");
}
コード例 #2
0
ファイル: unit_test.c プロジェクト: eroullit/net-toolbox
static void check_preambel_test(void)
{
  int ret;
  hi_handle_t *hi_handle;
  const char *key = "23";
  const char *data = "data element";
  void *data_ptr;

  /* initialize hashish handle */
  hi_init_str(&hi_handle, 23);

  /* insert an key/data pair */
  ret = hi_insert_str(hi_handle, key, data);

  /* search for a pair with a string key and store result */
  hi_get_str(hi_handle, key, &data_ptr);

  /* free the hashish handle */
  hi_fini(hi_handle);

}
コード例 #3
0
ファイル: unit_test.c プロジェクト: eroullit/net-toolbox
static void check_hi_load_factor(void)
{
	int ret;
	hi_handle_t *hi_handle;
	double load_factor;

	fputs(" o check_hi_load_factor test ...", stdout);

	/* initialize hashish handle */
	hi_init_str(&hi_handle, 3);

	ret = hi_insert_str(hi_handle, "1", NULL);
	ret = hi_insert_str(hi_handle, "2", NULL);

	load_factor = hi_table_load_factor(hi_handle);

	assert(load_factor == ((double)3)/2);

	/* free the hashish handle */
	hi_fini(hi_handle);

	fputs("passed\n", stdout);
}
コード例 #4
0
int main (int argc, char *argv[])
{
    char          opt;             /* short options: character */
    char          optstr[] = "hp:c:P:r:stuvlf:";
    int           port;            /* port number */
    int           cport    = 4740; /* target collector port number */
    int           protocol = IPFIX_PROTO_TCP;
    int           do_log = 0; /* flag to stre whether logging to stdout was enabled */

    /** set default options
     */
    port    = 4739;
    datadir = NULL;
    snprintf( progname, sizeof(progname), "%s", basename( argv[0]) );

    /* --- command line parsing ---
     */
    int cisset = 0;
    while( ( opt = getopt( argc, argv, optstr ) ) != EOF ) {

        switch ( opt )
        {
          case 'o':
              if ((sourceid=atoi(optarg)) <0) {
                  fprintf( stderr, "Invalid -o argument!\n" );
                  exit(1);
              }
              break;

          case 'p':
              if ((port=atoi(optarg)) <0) {
                  fprintf( stderr, "Invalid -p argument!\n" );
                  exit(1);
              }
              break;

          case 'v':
              verbose_level ++;
              break;

          case 'l':
              do_log = 1;
              break;

          case 'P':
              if ((cport=atoi(optarg)) <0) {
                fprintf( stderr, "Invalid -P argument!\n" );
                exit(1);
              }
              break;

          case 'r':
              if ((degrade=atoi(optarg)) <0) {
                fprintf( stderr, "Invalid -r argument!\n" );
                exit(1);
              }
              break;

          case 'c':
              if (chost)
                  free(chost); /* executed in case of multiple "-c <collectorhost>" */
              chost = strdup(optarg);
              cisset = 1;
              break;

          case 'f':
              if (datadir) 
                  free(datadir); /* executed in case of multiple "-f <datadir>" */
              datadir = strdup(optarg);
              break;

          case 's':
              protocol = IPFIX_PROTO_SCTP;
              break;

          case 't':
              protocol = IPFIX_PROTO_TCP;
              break;

          case 'u':
              protocol = IPFIX_PROTO_UDP;
              break;

          case 'h':
          default:
              usage(progname);
              exit(1);
        }
    }

    if (!chost && !datadir && !do_log) {
        fprintf( stderr, "You must specify either an IPFIX target collector host or a datadir or enable log to stdout. Type option '-h' for help.\n");
        exit(1);
    }

    /** init hashtable structures */
    hi_init_uint32_t(&indegree,  1000);
    hi_init_uint32_t(&outdegree, 1000);
    hi_init_uint32_t(&flows,     1000);
    hi_init_str     (&templates, 100);

    /** init logging
     */
    mlog_set_vlevel( verbose_level );

    /** init ipfix lib
     */
    if ( ipfix_init() <0 ) {
        fprintf( stderr, "ipfix_init() failed: %s\n", strerror(errno) );
        exit(1);
    }
    if ( ipfix_add_vendor_information_elements( ipfix_ft_fokus ) <0 ) {
        fprintf( stderr, "ipfix_add_ie() failed: %s\n", strerror(errno) );
        exit(1);
    }
 
    /** init ipfix import/export 
     */
    if ( ipfix_open( &ipfixh, sourceid, IPFIX_VERSION ) <0 ) {
        fprintf( stderr, "ipfix_open() failed: %s\n", strerror(errno) );
        exit(1);
    }

    /** signal handler
     */
    signal( SIGKILL, exit_func );
    signal( SIGTERM, exit_func );
    signal( SIGINT,  exit_func );

    /** initialize callback methods
     */

    /** activate stdout log output
     *  if "-l" was specified on cmd line
     */
    if (do_log) {
        (void) ipfix_col_start_msglog( stdout );
    }

    /** activate file export
     *  if "-f <datadir>" was specified on cmd line
     */
    if (datadir) {
        (void) ipfix_col_init_fileexport( datadir );
    }

    if (chost) {
        if ( ipfix_add_collector( ipfixh, chost, cport, protocol ) <0 ) {
            fprintf( stderr, "ipfix_add_collector(%s,%d) failed: %s\n",
                     chost, cport, strerror(errno));
            exit(1);
        }

        /** activate callback for re-export
         */
        if ( (g_colinfo=calloc( 1, sizeof(ipfix_col_info_t))) ==NULL) {
          fprintf( stderr, "a calloc failed while initializing callback methods.\n" );
          return -1;
        }

        g_colinfo->export_newsource = export_newsource_cb;
        g_colinfo->export_newmsg    = export_newmsg_cb;
        g_colinfo->export_trecord   = export_trecord_cb;
        g_colinfo->export_drecord   = export_drecord_cb;
        g_colinfo->export_cleanup   = export_cleanup_cb;
        g_colinfo->data = NULL;

        if ( ipfix_col_register_export( g_colinfo ) <0 ) {
            fprintf( stderr, "ipfix_col_register_export() failed: %s\n", strerror(errno) );
            exit(1);
        }
    }

    /** open ipfix collector port(s)
     */
    if ( ipfix_col_listen( &ntcp_s, &tcp_s, IPFIX_PROTO_TCP, 
                           port, AF_INET, 10 ) <0 ) {
        fprintf( stderr, "[%s] ipfix_listen(tcp) failed.\n",
                 progname );
        return -1;
    }

    /** event loop
     */
    (void) mpoll_loop( -1 );

    exit(1);
}
コード例 #5
0
int main(int argc, char **argv)
{

	int ret, verbose = 0; unsigned int i;
	char *affix = NULL;
	char *prefix = NULL;
	int opt = 0;
	int len = 0;
	int fd1, fd2;
	char *png_filename = NULL;
	unsigned int entries = DEFAULT_ENTRIES;
	unsigned int table_size = DEFAULT_HASHTABLE_SIZE;
	struct drand48_data r_d;
	hi_handle_t *hi_handle;
	uint32_t (*hashf)(const uint8_t *, uint32_t);
	const char *hashfname;

	hashf = NULL;

	srand48_r((long int)time(NULL), &r_d);

	while ((opt = getopt(argc, argv, "hqn:l:p:t:a:g:f:v")) != -1) {
		switch (opt) {
		case 'q':
			{
			int max_fd, std_fd, j;
			if ((max_fd = (int) sysconf(_SC_OPEN_MAX)) < 0) {
				max_fd = 256;
			}
			for (j = max_fd - 1; j >= 0; --j) {
				close(j);
			}
			std_fd = open("/dev/null", O_RDWR);
			fd1 = dup(std_fd);
			fd2 = dup(std_fd);
			}
			break;
		case 'f':
			if (!(strcasecmp(optarg, "elf"))) {
				hashf = lhi_hash_elf;
				hashfname = "lhi_hash_elf";
			} else if (!(strcasecmp(optarg, "torek"))) {
				hashf = lhi_hash_torek;
				hashfname = "lhi_hash_torek";
			} else if (!(strcasecmp(optarg, "dumb1"))) {
				hashf = lhi_hash_dumb1;
				hashfname = "lhi_hash_dumb1";
			} else if (!(strcasecmp(optarg, "phong"))) {
				hashf = lhi_hash_phong;
				hashfname = "lhi_hash_phong";
			} else {
				fprintf(stderr, "Hashing function not supported: %s\n",
						optarg);
				usage(EXIT_FAILURE, argv[0]);
			}
			break;
		case 'h':
			usage(EXIT_SUCCESS, argv[0]);
			break;
		case 'n':
			entries = atoi(optarg);
			break;
		case 'l':
			len = atoi(optarg);
			break;
		case 'p':
			prefix = strdup(optarg);
			break;
		case 't':
			table_size = atoi(optarg);
			break;
		case 'v':
			verbose++;
			break;
		case 'a':
			affix = strdup(optarg);
			break;
		case 'g':
#ifdef HAVE_LIBGD
			png_filename = strdup(optarg);
# else
			fprintf(stderr, "sorry - you build without gd library support\n");
			usage(EXIT_FAILURE, argv[0]);
#endif
			break;
		case '?':
			fprintf(stderr, "No such option: `%c'\n\n", optopt);
			usage(EXIT_FAILURE, argv[0]);
			break;
		}
	}

	fputs("# String Distribution Hash Test\n", stderr);

	/* initialize secure[tm] rand() seed */
	//init_seed();

	/* initialize hash table */
	ret = hi_init_str(&hi_handle, table_size);

	if (hashf != NULL) {
		lhi_sethashfunc(hi_handle, hashf);
		fprintf(stderr, "# take %s as hash function\n", hashfname);
	}

	/* fill hash table */
	for(i = 0; i < entries; i++) {

		char *key = NULL, *tmp_key;
		size_t key_len = 0;

		/* compound key */
		if (len == 0) {
			len = (rand() % (MAX_STRING_LEN - MIN_STRING_LEN + 1)) + MIN_STRING_LEN;
		}
		if (random_string(len, &tmp_key, &r_d) < 0)
			exit(EXIT_FAILURE);

		if (prefix)
			key_len += strlen(prefix);
		key_len += strlen(tmp_key);
		if (affix)
			key_len += strlen(affix);


		key = malloc(key_len + 1);
		if (key == NULL) {
			fprintf(stderr, "malloc %s\n", strerror(errno));
			exit(1);
		}

		if (prefix != NULL) {
			sprintf(&key[0], "%s%s", prefix, tmp_key);
		} else {
			sprintf(key, "%s", tmp_key);
		}

		free(tmp_key);
		tmp_key = NULL;

		if (affix)
			strcat(key, affix);

		if (verbose >= 1)
			fprintf(stdout, "key: %s\n", key);

		ret = hi_insert_str(hi_handle, (void *) key, NULL);
		if (ret < 0)
			fprintf(stderr, "# WARNING: Can't insert key (maybe a duplicated key: %s)!\n", key);
	}

	/* print statistic */
	ret = hi_size(hi_handle);
	fprintf(stderr, "# hash table entries: %d\n", ret);

	if (png_filename) { /* grapical output */

#ifdef HAVE_LIBGD
# define RECT_SIZE 5
# define RECT_BODER_SIZE 1

		int j;
		uint32_t x, y;
		gdImagePtr im;
		FILE *pngout;
		int black, white, red, max_list_len = 0;


		/* calculate maximum listsize */
		for(i = 0; i < table_size; i++) {
			int tmp_bucket_size = hi_bucket_size(hi_handle, i);
			max_list_len = max(max_list_len, tmp_bucket_size);
		}


		/* create a image with max_list_len X table_size */
		im = gdImageCreate((max_list_len * (RECT_SIZE + RECT_BODER_SIZE * 2)) + 2,
				(table_size * ((RECT_SIZE + RECT_BODER_SIZE * 2)) + 2));

		black = gdImageColorAllocate(im, 255, 231, 186);
		white = gdImageColorAllocate(im, 255, 165, 79);
		red   = gdImageColorAllocate(im, 205, 102, 29);

		x = 1;
		y = 1;

		for (i = 0; i < table_size; i++) {
			int bucket_size =  hi_bucket_size(hi_handle, i);
			for (j = 0; j < bucket_size; j++) {
				gdImageFilledRectangle(im, x + 1, y + 1, x + RECT_SIZE, y + RECT_SIZE, white);
				gdImageRectangle(im, x, y, x + RECT_SIZE + (RECT_BODER_SIZE << 1), y + RECT_SIZE + (RECT_BODER_SIZE << 1), red);
				x += RECT_SIZE + (RECT_BODER_SIZE << 1);
			}
			x = 1;
			y += RECT_SIZE + (RECT_BODER_SIZE << 1);
		}

		pngout = fopen(png_filename, "wb");
		gdImagePng(im, pngout);
		fclose(pngout);
		gdImageDestroy(im);

# undef RECT_SIZE
# undef RECT_BODER_SIZE
#endif /* HAVE_LIBGD */

	}

	if (verbose >= 1) {  /* terminal output */
		for(i = 0; i < table_size; i++) {
			fprintf(stderr, "bucket no: %d bucket size: %d\n",
					i, hi_bucket_size(hi_handle, i));
		}

	}

	/* delete table */
	hi_fini(hi_handle);

	return 0;
}
コード例 #6
0
PbniHashStr::PbniHashStr( IPB_Session * pSession )
:m_pSession( pSession )
{
	if (HI_ERR_SUCCESS != hi_init_str(&m_hi_handle, TABLE_STR_SIZE))
		exit(-1); //todo: how to return a constructor problem ?
}