Beispiel #1
0
scamper_outfile_t *scamper_outfiles_get(const char *name)
{
  const scamper_outfile_t findme = {(char *)name, NULL, 0};
  if(name == NULL)
    return outfile_def;
  return splaytree_find(outfiles, &findme);
}
scamper_task_t *scamper_task_find(scamper_task_sig_t *sig)
{
  s2t_t fm, *s2t;

  fm.sig = sig;
  if(sig->sig_type == SCAMPER_TASK_SIG_TYPE_TX_IP)
    s2t = splaytree_find(tx_ip, &fm);
  else if(sig->sig_type == SCAMPER_TASK_SIG_TYPE_TX_ND)
    s2t = splaytree_find(tx_nd, &fm);
  else
    return NULL;

  if(s2t != NULL)
    return s2t->task;
  return NULL;
}
static void tx_nd_check(scamper_dl_rec_t *dl)
{
  scamper_task_sig_t sig;
  scamper_addr_t ip;
  s2t_t fm, *s2t;

  if(splaytree_count(tx_nd) <= 0)
    return;

  if(SCAMPER_DL_IS_ARP_OP_REPLY(dl) && SCAMPER_DL_IS_ARP_PRO_IPV4(dl))
    {
      ip.type = SCAMPER_ADDR_TYPE_IPV4;
      ip.addr = dl->dl_arp_spa;
    }
  else if(SCAMPER_DL_IS_ICMP6_ND_NADV(dl))
    {
      ip.type = SCAMPER_ADDR_TYPE_IPV6;
      ip.addr = dl->dl_icmp6_nd_target;
    }
  else return;

  sig.sig_type = SCAMPER_TASK_SIG_TYPE_TX_ND;
  sig.sig_tx_nd_ip = &ip;
  fm.sig = &sig;
  if((s2t = splaytree_find(tx_nd, &fm)) == NULL)
    return;

  if(s2t->task->funcs->handle_dl != NULL)
    s2t->task->funcs->handle_dl(s2t->task, dl);

  return;
}
static void tx_ip_check(scamper_dl_rec_t *dl)
{
  scamper_task_sig_t sig;
  scamper_addr_t addr;
  s2t_t fm, *s2t;

  if(SCAMPER_DL_IS_IPV4(dl))
    addr.type = SCAMPER_ADDR_TYPE_IPV4;
  else if(SCAMPER_DL_IS_IPV6(dl))
    addr.type = SCAMPER_ADDR_TYPE_IPV6;
  else
    return;

  if(dl->dl_ip_off != 0)
    {
      addr.addr = dl->dl_ip_src;
    }
  else if(SCAMPER_DL_IS_TCP(dl))
    {
      if((dl->dl_tcp_flags & TH_SYN) && (dl->dl_tcp_flags & TH_ACK) == 0)
	addr.addr = dl->dl_ip_dst;
      else
	addr.addr = dl->dl_ip_src;
    }
  else if(SCAMPER_DL_IS_ICMP(dl))
    {
      if(SCAMPER_DL_IS_ICMP_ECHO_REQUEST(dl))
	addr.addr = dl->dl_ip_dst;
      else if(SCAMPER_DL_IS_ICMP_ECHO_REPLY(dl))
	addr.addr = dl->dl_ip_src;
      else if(SCAMPER_DL_IS_ICMP_TTL_EXP(dl))
	addr.addr = dl->dl_icmp_ip_dst;
      else if(SCAMPER_DL_IS_ICMP_UNREACH(dl))
	addr.addr = dl->dl_icmp_ip_dst;
      else if(SCAMPER_DL_IS_ICMP_PACKET_TOO_BIG(dl))
	addr.addr = dl->dl_icmp_ip_dst;
      else
	return;
    }
  else
    {
      addr.addr = dl->dl_ip_dst;
    }

  fm.sig = &sig;
  sig.sig_type = SCAMPER_TASK_SIG_TYPE_TX_IP;
  sig.sig_tx_ip_dst = &addr;

  if((s2t = splaytree_find(tx_ip, &fm)) == NULL)
    return;

  if(s2t->task->funcs->handle_dl != NULL)
    s2t->task->funcs->handle_dl(s2t->task, dl);

  return;
}
Beispiel #5
0
/*
 * scamper_addr2mac_whohas
 *
 * return the MAC address associated with an IP address, if it is cached.
 */
scamper_addr_t *scamper_addr2mac_whohas(const int ifindex, scamper_addr_t *dst)
{
  addr2mac_t findme, *addr2mac;

  findme.ifindex = ifindex;
  findme.ip = dst;

  /* see if this IP address has a record in our tree */
  if((addr2mac = splaytree_find(tree, &findme)) != NULL)
    {
      return addr2mac->mac;
    }

  return NULL;
}
Beispiel #6
0
/*
 * fd_find
 *
 * search the tree of file descriptors known to scamper for a matching
 * entry.  if one is found, increment its reference count and return it.
 */
static scamper_fd_t *fd_find(scamper_fd_t *findme)
{
  scamper_fd_t *fdn;

  if((fdn = splaytree_find(fd_tree, findme)) != NULL)
    {
      if(fdn->refcnt == 0 && fdn->rc0 != NULL)
	{
	  dlist_node_pop(refcnt_0, fdn->rc0);
	  fdn->rc0 = NULL;
	}

      fdn->refcnt++;
    }

  return fdn;
}
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
scamper_firewall_entry_t *scamper_firewall_entry_get(scamper_firewall_rule_t *sfw)
{
  scamper_firewall_entry_t findme, *entry = NULL;
  int n, af, p, sp, dp;
  void *s, *d;

  /* sanity check the rule */
  if((sfw->sfw_5tuple_proto != IPPROTO_TCP &&
      sfw->sfw_5tuple_proto != IPPROTO_UDP) ||
      sfw->sfw_5tuple_sport == 0 ||
      sfw->sfw_5tuple_dport == 0 ||
     (sfw->sfw_5tuple_dst == NULL || sfw->sfw_5tuple_src == NULL ||
      sfw->sfw_5tuple_src->type != sfw->sfw_5tuple_dst->type))
    {
      scamper_debug(__func__, "invalid 5tuple rule");
      goto err;
    }

  if(sfw->sfw_5tuple_src->type == SCAMPER_ADDR_TYPE_IPV4)
    {
      af = AF_INET;
      if(have_ipv4 == 0)
	{
	  scamper_debug(__func__, "IPv4 rule requested but no IPv4 firewall");
	  goto err;
	}
    }
  else if(sfw->sfw_5tuple_src->type == SCAMPER_ADDR_TYPE_IPV6)
    {
      af = AF_INET6;
      if(have_ipv6 == 0)
	{
	  scamper_debug(__func__, "IPv6 rule requested but no IPv6 firewall");
	  goto err;
	}
    }
  else
    {
      scamper_debug(__func__, "invalid src type");
      goto err;
    }

  findme.rule = sfw;
  if((entry = splaytree_find(entries, &findme)) != NULL)
    {
      entry->refcnt++;
      return entry;
    }

  if((entry = firewall_entry_get()) == NULL)
    goto err;

  entry->refcnt = 1;
  if((entry->rule = firewall_rule_dup(sfw)) == NULL ||
     (entry->node = splaytree_insert(entries, entry)) == NULL)
    {
      goto err;
    }

  n  = entry->slot;
  p  = sfw->sfw_5tuple_proto;
  dp = sfw->sfw_5tuple_dport;
  sp = sfw->sfw_5tuple_sport;
  s  = sfw->sfw_5tuple_src->addr;
  if(sfw->sfw_5tuple_dst == NULL)
    d = NULL;
  else
    d = sfw->sfw_5tuple_dst->addr;

#if defined(HAVE_IPFW)
#ifdef WITHOUT_PRIVSEP
  if(scamper_firewall_ipfw_add(n, af, p, s, d, sp, dp) != 0)
    goto err;
#else
  if(scamper_privsep_ipfw_add(n, af, p, s, d, sp, dp) != 0)
    goto err;
#endif
#endif

  return entry;

 err:
  if(entry != NULL)
    {
      if(entry->rule != NULL)
	firewall_rule_free(entry->rule);
      free(entry);
    }
  return NULL;
}