Beispiel #1
0
static void cleanup(void)
{
  int i;

  if(filter != NULL)
    {
      scamper_file_filter_free(filter);
      filter = NULL;
    }

  if(outfile != NULL)
    {
      scamper_file_close(outfile);
      outfile = NULL;
    }

  if(infiles != NULL)
    {
      for(i=0; i<infile_cnt; i++)
	{
	  if(infiles[i] != NULL)
	    {
	      scamper_file_close(infiles[i]);
	      infiles[i] = NULL;
	    }
	}
      free(infiles);
    }

  return;
}
static scamper_file_t *file_open(int fd, char *fn, char mode, int type)
{
  scamper_file_t *sf;
  int (*open_func)(scamper_file_t *);

  if(mode == 'r')      open_func = file_open_read;
  else if(mode == 'w') open_func = file_open_write;
  else if(mode == 'a') open_func = file_open_append;
  else return NULL;

  if((sf = (scamper_file_t *)malloc_zero(sizeof(scamper_file_t))) == NULL)
    {
      return NULL;
    }

  sf->type = type;
  sf->fd   = fd;

  if(fn != NULL && (sf->filename = strdup(fn)) == NULL)
    {
      return NULL;
    }

  if(open_func(sf) == -1)
    {
      scamper_file_close(sf);
      return NULL;
    }

  return sf;
}
Beispiel #3
0
static int simple_cat(void)
{
  uint16_t type;
  void *data;
  int i, rc;

  for(i=0; i<infile_cnt; i++)
    {
      while((rc = scamper_file_read(infiles[i], filter, &type, &data)) == 0)
	{
	  /* EOF */
	  if(data == NULL)
	    break;

	  if(write_obj(type, data) != 0)
	    {
	      return -1;
	    }
	}

      /* error when reading the input file */
      if(rc != 0)
	{
	  return -1;
	}

      scamper_file_close(infiles[i]);
      infiles[i] = NULL;
    }

  return 0;
}
Beispiel #4
0
static int outfile_opendef(char *filename, char *type)
{
  scamper_file_t *sf;
  int flags;
  mode_t mode;
  char sf_mode;
  int fd;

  flags = O_WRONLY | O_TRUNC | O_CREAT;
  sf_mode = 'w';

#ifndef _WIN32
  mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
#else
  mode = _S_IREAD | _S_IWRITE;
#endif

  if(strcmp(filename, "-") == 0)
    {
      fd = STDOUT_FILENO;
    }
  else
    {
#if defined(WITHOUT_PRIVSEP)
      fd = open(filename, flags, mode);
#else
      fd = scamper_privsep_open_file(filename, flags, mode);
#endif
    }

  if(fd == -1)
    {
      return -1;
    }

  if((sf = scamper_file_openfd(fd, filename, sf_mode, type)) == NULL)
    {
      close(fd);
      return -1;
    }

  if((outfile_def = outfile_alloc(filename, sf)) == NULL)
    {
      scamper_file_close(sf);
      return -1;
    }

  return 0;
}
Beispiel #5
0
static void outfile_free(scamper_outfile_t *sof)
{
  assert(sof != NULL);

  if(sof->name != NULL && sof->sf != NULL)
    scamper_debug(__func__, "name %s fd %d", sof->name,
		  scamper_file_getfd(sof->sf));

  if(sof->name != NULL)
    {
      splaytree_remove_item(outfiles, sof);
      free(sof->name);
    }

  if(sof->sf != NULL)
    {
      scamper_file_close(sof->sf);
    }

  free(sof);
  return;
}
Beispiel #6
0
scamper_outfile_t *scamper_outfile_open(char *name, char *file, char *mo)
{
  scamper_outfile_t *sof;
  scamper_file_t *sf;
  int flags;
  mode_t mode;
  char sf_mode;
  int fd;

#if defined(WITHOUT_PRIVSEP) && !defined(_WIN32)
  uid_t uid;
#endif

  if(name == NULL || file == NULL || mo == NULL)
    {
      return NULL;
    }

  if((sof = scamper_outfiles_get(name)) != NULL)
    {
      return NULL;
    }

  if(strcasecmp(mo, "append") == 0)
    {
      flags = O_RDWR | O_APPEND | O_CREAT;
      sf_mode = 'a';
    }
  else if(strcasecmp(mo, "truncate") == 0)
    {
      flags = O_WRONLY | O_TRUNC | O_CREAT;
      sf_mode = 'w';
    }
  else
    {
      return NULL;
    }

#ifndef _WIN32
  mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
#else
  mode = _S_IREAD | _S_IWRITE;
#endif

#if defined(WITHOUT_PRIVSEP)
  fd = open(file, flags, mode);
#else
  fd = scamper_privsep_open_file(file, flags, mode);
#endif

  /* make sure the fd is valid, otherwise bail */
  if(fd == -1)
    {
      return NULL;
    }

#if defined(WITHOUT_PRIVSEP) && !defined(_WIN32)
  if((uid = getuid()) != geteuid() && fchown(fd, uid, -1) != 0)
    {
      printerror(errno, strerror, __func__, "could not fchown");
    }
#endif

  if((sf = scamper_file_openfd(fd, file, sf_mode, "warts")) == NULL)
    {
      close(fd);
      return NULL;
    }

  if((sof = outfile_alloc(name, sf)) == NULL)
    {
      scamper_file_close(sf);
      return NULL;
    }

  return sof;
}
Beispiel #7
0
int main(int argc, char *argv[])
{
  scamper_file_t *file[2];
  scamper_file_filter_t *filter;
  scamper_trace_t *trace;
  tracepair_t *pair, fm;
  uint16_t type = SCAMPER_FILE_OBJ_TRACE;
  char buf[256];
  int i, filec_open;

#ifdef _WIN32
  WSADATA wsaData;
  WSAStartup(MAKEWORD(2,2), &wsaData);
#endif

  if(check_options(argc, argv) != 0)
    goto err;

  if((filter = scamper_file_filter_alloc(&type, 1)) == NULL)
    {
      fprintf(stderr, "could not allocate filter\n");
      goto err;
    }

  memset(file, 0, sizeof(file));
  for(i=0; i<filec; i++)
    {
      if((file[i] = scamper_file_open(files[i], 'r', NULL)) == NULL)
	{
	  fprintf(stderr, "could not open %s\n", files[i]);
	  goto err;
	}
    }
  filec_open = filec;

  if((pairs = splaytree_alloc(tracepair_cmp)) == NULL)
    {
      fprintf(stderr, "could not alloc tracepair tree\n");
      goto err;
    }
  splaytree_onremove(pairs, (splaytree_onremove_t)tracepair_onremove);

  while(filec_open != 0)
    {
      for(i=0; i<filec; i++)
	{
	  if(file[i] == NULL)
	    continue;

	  if(scamper_file_read(file[i], filter, &type, (void *)&trace) != 0)
	    {
	      fprintf(stderr, "could not read from %s\n", files[i]);
	      goto err;
	    }

	  if(trace == NULL)
	    {
	      filec_open--;
	      scamper_file_close(file[i]);
	      continue;
	    }
	  assert(type == SCAMPER_FILE_OBJ_TRACE);

	  fm.tracec = 1;
	  fm.traces[0] = trace;

	  if((pair = splaytree_find(pairs, &fm)) == NULL)
	    {
	      if((pair = malloc_zero(sizeof(tracepair_t))) == NULL)
		goto err;
	      pair->traces[i] = trace;
	      pair->tracec = 1;
	      if((pair->node = splaytree_insert(pairs, pair)) == NULL)
		goto err;
	    }
	  else
	    {
	      if(pair->traces[i] != NULL)
		{
		  fprintf(stderr, "repeated trace for %s\n",
			  scamper_addr_tostr(trace->dst, buf, sizeof(buf)));
		  goto err;
		}
	      pair->traces[i] = trace;
	      pair->tracec++;
	    }

	  if(pair->tracec != filec)
	    continue;

	  splaytree_remove_node(pairs, pair->node);
	  tracepair_process(pair);
	  tracepair_free(pair);
	}
    }

  return 0;

 err:
  return -1;
}
Beispiel #8
0
int main(int argc, char *argv[])
{
  uint16_t types[] = {
    SCAMPER_FILE_OBJ_CYCLE_START,
    SCAMPER_FILE_OBJ_CYCLE_STOP,
    SCAMPER_FILE_OBJ_PING,
    SCAMPER_FILE_OBJ_TRACE,
    SCAMPER_FILE_OBJ_DEALIAS,
    SCAMPER_FILE_OBJ_TBIT,
  };
  scamper_file_t *in, *out;
  scamper_file_filter_t *filter;
  char **files = NULL;
  int filec;
  uint16_t type;
  void *data;
  int i;

  if((out = scamper_file_openfd(STDOUT_FILENO, NULL, 'w', "json")) == NULL)
    {
      fprintf(stderr, "could not associate stdout\n");
      return -1;
    }

  filter = scamper_file_filter_alloc(types, sizeof(types)/sizeof(uint16_t));
  if(filter == NULL)
    {
      fprintf(stderr, "could not allocate filter\n");
      return -1;
    }

  filec = argc - 1;
  if(filec > 0)
    files = argv + 1;

  for(i=0; i<=filec; i++)
    {
      if(filec == 0)
	{
	  if((in = scamper_file_openfd(STDIN_FILENO,"-",'r',"warts")) == NULL)
	    {
	      fprintf(stderr, "could not use stdin\n");
	      return -1;
	    }
	}
      else if(i < filec)
	{
	  if((in = scamper_file_open(files[i], 'r', NULL)) == NULL)
	    {
	      fprintf(stderr, "could not open %s: %s\n",
		      files[i], strerror(errno));
	      return -1;
	    }
	}
      else break;

      while(scamper_file_read(in, filter, &type, (void *)&data) == 0)
	{
	  if(data == NULL)
	    break; /* EOF */

	  if(scamper_file_write_obj(out, type, data) != 0)
	    return -1;

	  if(type == SCAMPER_FILE_OBJ_PING)
	    scamper_ping_free(data);
	  else if(type == SCAMPER_FILE_OBJ_TRACE)
	    scamper_trace_free(data);
	  else if(type == SCAMPER_FILE_OBJ_DEALIAS)
	    scamper_dealias_free(data);
	  else if(type == SCAMPER_FILE_OBJ_TBIT)
	    scamper_tbit_free(data);
	}

      scamper_file_close(in);
    }

  scamper_file_filter_free(filter);
  scamper_file_close(out);
  return 0;
}
Beispiel #9
0
static int sort_cat_fill(heap_t *heap, sort_struct_t *s)
{
  int i = s->file;

  if(scamper_file_read(infiles[i], filter, &s->type, &s->data) == 0)
    {
      /* EOF */
      if(s->data == NULL)
	{
	  scamper_file_close(infiles[i]);
	  infiles[i] = NULL;
	  return 0;
	}

      switch(s->type)
	{
	case SCAMPER_FILE_OBJ_TRACELB:
	  timeval_cpy(&s->tv, &((scamper_tracelb_t *)s->data)->start);
	  break;

	case SCAMPER_FILE_OBJ_TRACE:
	  timeval_cpy(&s->tv, &((scamper_trace_t *)s->data)->start);
	  break;

	case SCAMPER_FILE_OBJ_PING:
	  timeval_cpy(&s->tv, &((scamper_ping_t *)s->data)->start);
	  break;

	case SCAMPER_FILE_OBJ_DEALIAS:
	  timeval_cpy(&s->tv, &((scamper_dealias_t *)s->data)->start);
	  break;

	case SCAMPER_FILE_OBJ_TBIT:
	  timeval_cpy(&s->tv, &((scamper_tbit_t *)s->data)->start);
	  break;

	case SCAMPER_FILE_OBJ_NEIGHBOURDISC:
	  timeval_cpy(&s->tv, &((scamper_neighbourdisc_t *)s->data)->start);
	  break;

	case SCAMPER_FILE_OBJ_STING:
	  timeval_cpy(&s->tv, &((scamper_sting_t *)s->data)->start);
	  break;

	case SCAMPER_FILE_OBJ_CYCLE_START:
	  s->tv.tv_sec = ((scamper_cycle_t *)s->data)->start_time;
	  s->tv.tv_usec = 0;
	  break;

	case SCAMPER_FILE_OBJ_CYCLE_STOP:
	  s->tv.tv_sec = ((scamper_cycle_t *)s->data)->stop_time;
	  s->tv.tv_usec = 1000000;
	  break;
	}

      if(heap_insert(heap, s) == NULL)
	{
	  return -1;
	}
    }
  else return -1;

  return 0;
}