Esempio n. 1
0
iow_t *bgpcorsaro_io_prepare_file_full(bgpcorsaro_t *bgpcorsaro,
                                       const char *plugin_name,
                                       bgpcorsaro_interval_t *interval,
                                       int compress_type, int compress_level,
                                       int flags)
{
  iow_t *f = NULL;
  char *outfileuri;

  /* generate a file name based on the plugin name */
  if ((outfileuri = generate_file_name(bgpcorsaro, plugin_name, interval,
                                       compress_type)) == NULL) {
    bgpcorsaro_log(__func__, bgpcorsaro, "could not generate file name for %s",
                   plugin_name);
    return NULL;
  }

  if ((f = wandio_wcreate(outfileuri, compress_type, compress_level, flags)) ==
      NULL) {
    bgpcorsaro_log(__func__, bgpcorsaro, "could not open %s for writing",
                   outfileuri);
    return NULL;
  }

  free(outfileuri);
  return f;
}
Esempio n. 2
0
corsaro_file_t *corsaro_file_open(corsaro_t *corsaro,
			      const char *filename,
			      corsaro_file_mode_t mode,
			      corsaro_file_compress_t compress_type,
			      int compress_level,
			      int flags)
{
  corsaro_file_t *f = NULL;

  size_t flen, rlen, len;
  char *ptr, *traceuri;

  if((f = malloc(sizeof(corsaro_file_t))) == NULL)
    {
      corsaro_log(__func__, corsaro, "could not malloc new corsaro_file_t");
      return NULL;
    }

  f->mode = mode;

  /* did they ask for a libtrace file? */
  switch(mode)
    {
    case CORSARO_FILE_MODE_TRACE:
      flen = strlen(CORSARO_FILE_TRACE_FORMAT);
      rlen = strlen(filename);
      len = flen+rlen+1;
      if((ptr = traceuri = malloc(len)) == NULL)
	{
	  corsaro_log(__func__, corsaro, "could not malloc traceuri");
	  return NULL;
	}
      strncpy(traceuri, CORSARO_FILE_TRACE_FORMAT, flen);
      ptr += flen;
      strncpy(ptr, filename, rlen);
      traceuri[len-1] = '\0';
      f->trace_io = trace_create_output(traceuri);
      free(traceuri);

      if (trace_is_err_output(f->trace_io))
	{
	  corsaro_log(__func__, corsaro, "trace_create_output failed for %s",
		    filename);
	  return NULL;
	}
      if(trace_config_output(f->trace_io, TRACE_OPTION_OUTPUT_COMPRESS,
			     &compress_level) ||
	 trace_config_output(f->trace_io, TRACE_OPTION_OUTPUT_COMPRESSTYPE,
			     &compress_type) != 0)
	{
	  corsaro_log(__func__, corsaro,
		    "could not set compression levels for trace");
	  return NULL;
	}
      if (trace_start_output(f->trace_io) == -1) {
	corsaro_log(__func__, corsaro, "trace_start_output failed for %s",
		  filename);
	return NULL;
      }
      /* trace is configured! */
      break;

    case CORSARO_FILE_MODE_ASCII:
    case CORSARO_FILE_MODE_BINARY:
      if((f->wand_io = wandio_wcreate(filename, compress_type,
				      compress_level, flags)) == NULL)
	{
	  corsaro_log(__func__, corsaro, "wandio could not create file %s",
		    filename);
	  free(f);
	  return NULL;
	}
      break;

    default:
      corsaro_log(__func__, corsaro, "invalid file mode %d", mode);
      free(f);
      return NULL;
    }

  return f;
}