Exemple #1
0
int ip_match (ip_t *adr, ip_t *network, int prefix)
{
    int r ;

    if (adr->family != network->family)
	return 0 ;

    if (prefix && adr->preflen < network->preflen)
	return 0 ;

    switch (network->family)
    {
	case INET4 :
	    r = prefix_match (&adr->u.adr4, &network->u.adr4, network->preflen) ;
	    break ;
	case INET6 :
	    r = prefix_match (&adr->u.adr6, &network->u.adr6, network->preflen) ;
	    break ;
	default :
	    r = 1 ;
	    break ;
    }

    return r == 0 ;
}
Exemple #2
0
struct ospf6_lsa *
ospf6_lsdb_type_head (u_int16_t type, struct ospf6_lsdb *lsdb)
{
  struct route_node *node;
  struct prefix_ipv6 key;
  struct ospf6_lsa *lsa;

  memset (&key, 0, sizeof (key));
  ospf6_lsdb_set_key (&key, &type, sizeof (type));

  /* Walk down tree. */
  node = lsdb->table->top;
  while (node && node->p.prefixlen <= key.prefixlen &&
	 prefix_match (&node->p, (struct prefix *) &key))
    node = node->link[prefix6_bit(&key.prefix, node->p.prefixlen)];

  if (node)
    route_lock_node (node);
  while (node && node->info == NULL)
    node = route_next (node);

  if (node == NULL)
    return NULL;
  else
    route_unlock_node (node);

  if (! prefix_match ((struct prefix *) &key, &node->p))
    return NULL;

  lsa = node->info;
  ospf6_lsa_lock (lsa);

  return lsa;
}
TEST(fv_converter, key_matcher) {
  ASSERT_TRUE(exact_match("hoge").match("hoge"));
  ASSERT_FALSE(exact_match("hoge").match("foo"));

  ASSERT_TRUE(prefix_match("ho").match("hoge"));
  ASSERT_TRUE(prefix_match("hoge").match("hoge"));
  ASSERT_TRUE(prefix_match("").match("hoge"));
  ASSERT_FALSE(prefix_match("hogehgoe").match("hoge"));

  ASSERT_TRUE(suffix_match("ge").match("hoge"));
  ASSERT_TRUE(suffix_match("hoge").match("hoge"));
  ASSERT_TRUE(suffix_match("").match("hoge"));
  ASSERT_FALSE(suffix_match("hogehgoe").match("hoge"));
}
Exemple #4
0
/* Lookup interface by IPv4 address. */
struct interface * if_lookup_address (struct in_addr src)
{
	struct prefix addr;
	int bestlen = 0;
	struct listnode *cnode;
	struct interface *ifp = NULL;
	struct connected *c;
	struct interface *match = NULL;
        int idx = 0;

	addr.family = AF_INET;
	addr.u.prefix4 = src;
	addr.prefixlen = IPV4_MAX_BITLEN;

        while (idx < get_max_port ()) {
		for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, c))
		{
			if (c->address && (c->address->family == AF_INET) &&
					prefix_match(CONNECTED_PREFIX(c), &addr) &&
					(c->address->prefixlen > bestlen))
			{
				bestlen = c->address->prefixlen;
				match = ifp;
			}
		}
                idx++;
        }
	return match;
}
Exemple #5
0
/* Lookup interface by IPv4 address. */
struct interface *
if_lookup_address_vrf (struct in_addr src, vrf_id_t vrf_id)
{
  struct listnode *node;
  struct prefix addr;
  int bestlen = 0;
  struct listnode *cnode;
  struct interface *ifp;
  struct connected *c;
  struct interface *match;

  addr.family = AF_INET;
  addr.u.prefix4 = src;
  addr.prefixlen = IPV4_MAX_BITLEN;

  match = NULL;

  for (ALL_LIST_ELEMENTS_RO (vrf_iflist (vrf_id), node, ifp))
    {
      for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, c))
	{
	  if (c->address && (c->address->family == AF_INET) &&
	      prefix_match(CONNECTED_PREFIX(c), &addr) &&
	      (c->address->prefixlen > bestlen))
	    {
	      bestlen = c->address->prefixlen;
	      match = ifp;
	    }
	}
    }
  return match;
}
Exemple #6
0
/*
*  check if interface with given address is configured and
*  return it if yes.  special treatment for PtP networks.
*/
struct ospf_interface *
ospf_if_is_configured (struct ospf *ospf, struct in_addr *address)
{
  struct listnode *node, *nnode;
  struct ospf_interface *oi;
  struct prefix_ipv4 addr;

  addr.family = AF_INET;
  addr.prefix = *address;
  addr.prefixlen = IPV4_MAX_PREFIXLEN;

  for (ALL_LIST_ELEMENTS (ospf->oiflist, node, nnode, oi))
    if (oi->type != OSPF_IFTYPE_VIRTUALLINK)
      {
        if (oi->type == OSPF_IFTYPE_POINTOPOINT)
	  {
	    /* special leniency: match if addr is anywhere on peer subnet */
	    if (prefix_match(CONNECTED_PREFIX(oi->connected),
			     (struct prefix *)&addr))
	      return oi;
	  }
        else
	  {
	    if (IPV4_ADDR_SAME (address, &oi->address->u.prefix4))
	      return oi;
	  }
      }
  return NULL;
}
Exemple #7
0
/* determine receiving interface by source of packet */
struct ospf_interface *
ospf_if_lookup_recv_if (struct ospf *ospf, struct in_addr src)
{
  struct listnode *node;
  struct prefix_ipv4 addr;
  struct ospf_interface *oi, *match;

  addr.family = AF_INET;
  addr.prefix = src;
  addr.prefixlen = IPV4_MAX_BITLEN;

  match = NULL;

  for (ALL_LIST_ELEMENTS_RO (ospf->oiflist, node, oi))
    {
      if (oi->type == OSPF_IFTYPE_VIRTUALLINK)
	continue;
      
      if (if_is_loopback (oi->ifp))
        continue;

      if (prefix_match (CONNECTED_PREFIX(oi->connected),
      			(struct prefix *) &addr))
	{
	  if ( (match == NULL) || 
	       (match->address->prefixlen < oi->address->prefixlen)
	     )
	    match = oi;
	}
    }

  return match;
}
Exemple #8
0
char* sr_find_next_hop(struct sr_instance* sr, uint32_t dest)
{
    struct sr_rt* rt_walker = 0;
    struct sr_rt* next_hop = 0;
    int max_prefix = 0;
    int temp = 0;

    if(sr->routing_table == 0)
    {
        printf(" *warning* Routing table empty \n");
        return 0;
    }

    rt_walker = sr->routing_table;
    
    while(rt_walker)
    {
        sr_print_routing_entry(rt_walker);
	temp = prefix_match(ntohl((rt_walker->dest).s_addr),
			    ntohl(dest),
			    ntohl((rt_walker->mask).s_addr));
	printf("%d\n", temp);
	if (temp > max_prefix)
	{
	    next_hop = rt_walker;
	    max_prefix = temp;
	}
        rt_walker = rt_walker->next; 
    }
    if (!next_hop)
	return 0;
    printf("find a interface=%s\n",next_hop->interface);
    return next_hop->interface;
}
Exemple #9
0
/*
*  check if interface with given address is configured and
*  return it if yes.
*/
struct ospf_interface *
ospf_if_is_configured (struct ospf *ospf, struct in_addr *address)
{
  listnode node;
  struct ospf_interface *oi;
  struct prefix *addr;
  struct prefix query_addr;
  
  for (node = listhead (ospf->oiflist); node; nextnode (node))
    if ((oi = getdata (node)) != NULL && oi->type != OSPF_IFTYPE_VIRTUALLINK)
      {
/*	if (oi->type == OSPF_IFTYPE_POINTOPOINT)
	  addr = oi->connected->destination;
	else
*/
	  addr = oi->address;

	  query_addr.family = AF_INET;
	  query_addr.u.prefix4 = *address;
	  query_addr.prefixlen = IPV4_ALLOWABLE_BITLEN_P2P;
	  
	if (oi->type == OSPF_IFTYPE_POINTOPOINT && 
	     prefix_match (addr, &query_addr))
	     return oi;
	else if (IPV4_ADDR_SAME (address, &addr->u.prefix4))
	  return oi;
      }

  return NULL;
}
Exemple #10
0
struct interface *
if_lookup_by_ipv6 (struct in6_addr *addr)
{
  struct listnode *ifnode;
  struct listnode *cnode;
  struct interface *ifp;
  struct connected *connected;
  struct prefix_ipv6 p;
  struct prefix *cp; 
  
  p.family = AF_INET6;
  p.prefix = *addr;
  p.prefixlen = IPV6_MAX_BITLEN;

  for (ALL_LIST_ELEMENTS_RO (iflist, ifnode, ifp))
    {
      for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, connected))
	{
	  cp = connected->address;
	    
	  if (cp->family == AF_INET6)
	    if (prefix_match (cp, (struct prefix *)&p))
	      return ifp;
	}
    }
  return NULL;
}
Exemple #11
0
int TF_Test::run(const char * const *prefixes, const char *suite)
{
    int old_valgrind_errs = 0, new_valgrind_errs;
    int old_valgrind_leaks = 0, new_valgrind_leaks;

    signal(SIGALRM, alarm_handler);
    // signal(SIGALRM, SIG_IGN);
    alarm(MAX_TEST_TIME);
    start_time = time(NULL);

    fails = runs = 0;
    for (TF_Test *cur = first; cur; cur = cur->next)
    {
        if ((!prefixes
            || prefix_match(cur->idstr, prefixes)
            || prefix_match(cur->descr, prefixes))
            && (!suite || strcmp(cur->m_suite, suite) == 0))
        {
            printf("%s: Testing \"%s\" in %s:\n", cur->m_suite, cur->descr,
                   cur->idstr);
            cur->main();

            new_valgrind_errs = memerrs();
            TFPASS(new_valgrind_errs == old_valgrind_errs);
            old_valgrind_errs = new_valgrind_errs;

            new_valgrind_leaks = memleaks();
            TFPASS(new_valgrind_leaks == old_valgrind_leaks);
            old_valgrind_leaks = new_valgrind_leaks;

            printf("\n");
        }
    }

    if (prefixes && *prefixes)
        printf("TF_Test: WARNING: only ran tests starting with "
               "specifed prefix(es).\n");
    else if (suite)
        printf("TF_Test: WARNING: only ran suite %s\n", suite);
    else
        printf("TF_Test: ran all tests.\n");
    printf("TF_Test: %d test%s, %d failure%s.\n",
           runs, runs==1 ? "" : "s",
           fails, fails==1 ? "": "s");

    return fails != 0;
}
Exemple #12
0
static StateNode* lookup_state(MarkovData* data, const char** prefix) {
    int h = hash_prefix(data, prefix);
    
    for (StateNode* node = data->statetab[h]; node != NULL; node = node->next) {
        if (prefix_match(node->prefix, prefix, data->prefix_len)) {
            return node;
        }
    }
    return NULL;
}
Exemple #13
0
ptree* pat_search(ptree* trie, unsigned int key) {
  ptree* last = NULL;

  while (trie && prefix_match(trie, key)) {
    if (trie->value >= 0) { last = trie; }
    trie = search_dir(trie, key) ? trie->right : trie->left;
  }

  return last;
}
Exemple #14
0
std::string JSONEnumService::lookup_uri_from_user(const std::string &user, SAS::TrailId trail) const
{
  std::string uri;

  TRC_DEBUG("Translating URI via JSON ENUM lookup");

  if (user.empty())
  {
    TRC_INFO("No dial string supplied, so don't do ENUM lookup");
    return std::string();
  }

  std::string aus = user_to_aus(user);

  // Take a read lock on the mutex in RAII style
  boost::shared_lock<boost::shared_mutex> read_lock(_number_prefixes_rw_lock);

  const struct NumberPrefix* pfix = prefix_match(aus);

  if (pfix == NULL)
  {
    TRC_WARNING("No matching number range %s from ENUM lookup", user.c_str());
    SAS::Event event(trail, SASEvent::ENUM_INCOMPLETE, 0);
    event.add_var_param(user);
    SAS::report_event(event);
    return uri;
  }

  // Apply the regular expression to the user string to generate a new
  // URI.
  try
  {
    uri = boost::regex_replace(aus, pfix->match, pfix->replace);
  }
  catch(...) // LCOV_EXCL_START Only throws if expression too complex or similar hard-to-hit conditions
  {
    TRC_ERROR("Failed to translate number with regex");
    SAS::Event event(trail, SASEvent::ENUM_INCOMPLETE, 1);
    event.add_var_param(user);
    SAS::report_event(event);
    return uri;
    // LCOV_EXCL_STOP
  }

  TRC_INFO("Number %s found, translated URI = %s", user.c_str(), uri.c_str());
  SAS::Event event(trail, SASEvent::ENUM_COMPLETE, 0);
  event.add_var_param(user);
  event.add_var_param(uri);
  SAS::report_event(event);

  return uri;
}
Exemple #15
0
void bgp_table_range_lookup(const struct bgp_table *table, struct prefix *p,
			    uint8_t maxlen, struct list *matches)
{
	struct bgp_node *node = bgp_node_from_rnode(table->route_table->top);
	struct bgp_node *matched = NULL;

	while (node && node->p.prefixlen <= p->prefixlen
	       && prefix_match(&node->p, p)) {
		if (node->info && node->p.prefixlen == p->prefixlen) {
			matched = node;
			break;
		}
		node = bgp_node_from_rnode(node->link[prefix_bit(
			&p->u.prefix, node->p.prefixlen)]);
	}

	if (node == NULL)
		return;

	if ((matched == NULL && node->p.prefixlen > maxlen) || !node->parent)
		return;
	else if (matched == NULL)
		matched = node = bgp_node_from_rnode(node->parent);

	if (matched->info) {
		bgp_lock_node(matched);
		listnode_add(matches, matched);
	}

	while ((node = bgp_route_next_until_maxlen(node, matched, maxlen))) {
		if (prefix_match(p, &node->p)) {
			if (node->info) {
				bgp_lock_node(node);
				listnode_add(matches, node);
			}
		}
	}
}
Exemple #16
0
int main()
{
    insert();

    longest_match("binder");
    longest_match("bracelet");
    longest_match("apple");

    prefix_match("aff");
    prefix_match("bi");
    prefix_match("a");

    greedy_match("avoid");
    greedy_match("bring");
    greedy_match("attack");

    traverse();

    tree.erase("bro");
    prefix_match("bro");

    return EXIT_SUCCESS;
}
Exemple #17
0
/* determine receiving interface by source of packet */
struct ospf_interface *
ospf_if_lookup_recv_if (struct ospf *ospf, struct in_addr src)
{
  listnode node;
  struct prefix_ipv4 addr;
  struct ospf_interface *oi, *match;

  addr.family = AF_INET;
  addr.prefix = src;

  match = NULL;

  for (node = listhead (ospf->oiflist); node; nextnode (node))
    {
      oi = getdata (node);
      
      if (oi->type == OSPF_IFTYPE_VIRTUALLINK)
	continue;
      
      if (oi->type == OSPF_IFTYPE_POINTOPOINT)
	{
	  addr.prefixlen = IPV4_ALLOWABLE_BITLEN_P2P;
	  if (IPV4_ADDR_SAME (&oi->connected->destination->u.prefix4, &src) ||
	  	prefix_match (oi->address, (struct prefix *) &addr))
	    return oi;
	}
      else
	{
	  addr.prefixlen = IPV4_MAX_BITLEN;
	  if (prefix_match (oi->address, (struct prefix *) &addr))
	    match = oi;
	}
    }

  return match;
}
/* Lookup interface by IPv4 address. */
struct interface *
if_lookup_address (struct in_addr src)
{
  struct listnode *node;
  struct prefix addr;
  int bestlen = 0;
  struct listnode *cnode;
  struct interface *ifp;
  struct prefix *p;
  struct connected *c;
  struct interface *match;

  addr.family = AF_INET;
  addr.u.prefix4 = src;
  addr.prefixlen = IPV4_MAX_BITLEN;

  match = NULL;

  for (ALL_LIST_ELEMENTS_RO (iflist, node, ifp))
    {
      for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, c))
	{
	  if (c->address && (c->address->family == AF_INET))
	    {
	      if (CONNECTED_POINTOPOINT_HOST(c))
		{
		 /* PTP  links are conventionally identified 
		    by the address of the far end - MAG */
		  if (IPV4_ADDR_SAME (&c->destination->u.prefix4, &src))
		    return ifp;
		}
	      else
		{
		  p = c->address;

		  if (prefix_match (p, &addr) && p->prefixlen > bestlen)
		    {
		      bestlen = p->prefixlen;
		      match = ifp;
		    }
		}
	    }
	}
    }
  return match;
}
Exemple #19
0
struct sr_rt *sr_find_rt_entry(struct sr_rt* head, uint32_t ip)
{
    struct sr_rt *curr = head, *rval = NULL;
    
    ssize_t longestMatch = 0;
    ssize_t currMatch    = 0;
    while (curr)
    {
        currMatch = prefix_match(ip, curr->dest.s_addr);
        if (currMatch >= 8 && currMatch > longestMatch)
        {
            longestMatch = currMatch;
            rval = curr;
        }
        curr = curr->next;
    }
    return rval;
}
Exemple #20
0
static ucli_status_t
debug_counter_ucli_ucli__show__(ucli_context_t* uc)
{
    UCLI_COMMAND_INFO(uc,
                      "show", -1,
                      "$summary#show all debug counters.");

    const char *prefix = NULL;
    if (uc->pargs->count > 0) {
	UCLI_ARGPARSE_OR_RETURN(uc, "s", &prefix);
    }

    list_head_t *counters = debug_counter_list();
    list_links_t *cur;
    LIST_FOREACH(counters, cur) {
	debug_counter_t *counter = container_of(cur, links, debug_counter_t);
	if (prefix_match(counter->name, prefix)) {
	    ucli_printf(uc, "%s: %"PRIu64"\n", counter->name, counter->value);
	}
    }
Exemple #21
0
/* determine receiving interface by source of packet */
struct ospf_interface *
ospf_if_lookup_recv_if (struct ospf *ospf, struct in_addr src)
{
  struct listnode *node;
  struct prefix_ipv4 addr;
  struct ospf_interface *oi, *match;

  addr.family = AF_INET;
  addr.prefix = src;
  addr.prefixlen = IPV4_MAX_BITLEN;

  match = NULL;

  for (ALL_LIST_ELEMENTS_RO (ospf->oiflist, node, oi))
    {
      if (oi->type == OSPF_IFTYPE_VIRTUALLINK)
	continue;
      
      if (if_is_loopback (oi->ifp))
        continue;
      
      if ((oi->type == OSPF_IFTYPE_POINTOPOINT) &&
	  CONNECTED_DEST_HOST(oi->connected))
	{
	  if (IPV4_ADDR_SAME (&oi->connected->destination->u.prefix4, &src))
	    return oi;
	}
      else
	{
	  if (prefix_match (oi->address, (struct prefix *) &addr))
	    {
	      if ( (match == NULL) || 
	           (match->address->prefixlen < oi->address->prefixlen)
	         )
	        match = oi;
	    }
	}
    }

  return match;
}
/* determine receiving interface by ifp and source address */
struct ospf_interface *
ospf_if_lookup_recv_if (struct ospf *ospf, struct in_addr src,
			struct interface *ifp)
{
  struct route_node *rn;
  struct prefix_ipv4 addr;
  struct ospf_interface *oi, *match;

  addr.family = AF_INET;
  addr.prefix = src;
  addr.prefixlen = IPV4_MAX_BITLEN;

  match = NULL;

  for (rn = route_top (IF_OIFS (ifp)); rn; rn = route_next (rn))
    {
      oi = rn->info;

      if (!oi) /* oi can be NULL for PtP aliases */
	continue;

      if (oi->type == OSPF_IFTYPE_VIRTUALLINK)
	continue;

      if (if_is_loopback (oi->ifp))
        continue;

      if (prefix_match (CONNECTED_PREFIX(oi->connected),
      			(struct prefix *) &addr))
	{
	  if ( (match == NULL) || 
	       (match->address->prefixlen < oi->address->prefixlen)
	     )
	    match = oi;
	}
    }

  return match;
}
/*
*  check if interface with given address is configured and
*  return it if yes.  special treatment for PtP networks.
*/
struct ospf_interface *
ospf_if_is_configured (struct ospf *ospf, struct in_addr *address)
{
  struct listnode *node;
  struct ospf_interface *oi;
  struct prefix_ipv4 addr;

  addr.family = AF_INET;
  addr.prefix = *address;
  addr.prefixlen = IPV4_MAX_PREFIXLEN;
  
  for (node = listhead (ospf->oiflist); node; nextnode (node))
    if ((oi = getdata (node)) != NULL && oi->type != OSPF_IFTYPE_VIRTUALLINK)
      {
	if (oi->type == OSPF_IFTYPE_POINTOPOINT)
	  {
	    if (CONNECTED_DEST_HOST(oi->connected))
	      {
		/* match only destination addr, since local addr is most likely
		 * not unique (borrowed from another interface) */
		if (IPV4_ADDR_SAME (address,
				    &oi->connected->destination->u.prefix4))
		return oi;
	      }
	    else
	      {
		/* special leniency: match if addr is anywhere on PtP subnet */
		if (prefix_match(oi->address,(struct prefix *)&addr))
		  return oi;
	      }
	  }
	else
	  {
	    if (IPV4_ADDR_SAME (address, &oi->address->u.prefix4))
	      return oi;
	  }
      }
  return NULL;
}
Exemple #24
0
std::string JSONEnumService::lookup_uri_from_user(const std::string &user, SAS::TrailId trail) const
{
  std::string uri;

  TRC_DEBUG("Translating URI via JSON ENUM lookup");

  if (user.empty())
  {
    TRC_INFO("No dial string supplied, so don't do ENUM lookup");
    return std::string();
  }

  std::string aus = user_to_aus(user);
  struct NumberPrefix* pfix = prefix_match(aus);

  if (pfix == NULL)
  {
    TRC_INFO("No matching number range %s from ENUM lookup", user.c_str());
    return uri;
  }

  // Apply the regular expression to the user string to generate a new
  // URI.
  try
  {
    uri = boost::regex_replace(aus, pfix->match, pfix->replace);
  }
  catch(...) // LCOV_EXCL_START Only throws if expression too complex or similar hard-to-hit conditions
  {
    TRC_ERROR("Failed to translate number with regex");
    return uri;
    // LCOV_EXCL_STOP
  }

  TRC_INFO("Number %s found, translated URI = %s", user.c_str(), uri.c_str());

  return uri;
}
Exemple #25
0
int main()
{
    uint32_t ip1;
    uint32_t ip2;
    uint32_t mask;

    unsigned char* p;
    int i;
    p = &ip1;
    for (i = 0; i < 4; i++)
    {
        *(p++) = i;
    }

    p = &ip2;
    for (i = 0; i < 4; i++)
    {
        *(p++) = i;
    }


    p = &mask;
    p++;
    *(p++) = 128;
    for (i = 2; i < 4; i++)
    {
        *(p++) = 255;
    }


    print_addr_ip_int(ip1 & mask);
    print_addr_ip_int(ip2 & mask);
    print_addr_ip_int(mask);


    printf("%d\n", prefix_match(ip1, ip2, mask));
    return 0;
}
Exemple #26
0
/* Find the IPv4 address on our side that will be used when packets
   are sent to dst. */
struct connected *
connected_lookup_address (struct interface *ifp, struct in_addr dst)
{
  struct prefix addr;
  struct listnode *cnode;
  struct connected *c;
  struct connected *match;

  addr.family = AF_INET;
  addr.u.prefix4 = dst;
  addr.prefixlen = IPV4_MAX_BITLEN;

  match = NULL;

  for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, c))
    {
      if (c->address && (c->address->family == AF_INET) &&
	  prefix_match(CONNECTED_PREFIX(c), &addr) &&
	  (!match || (c->address->prefixlen > match->address->prefixlen)))
	match = c;
    }
  return match;
}
Exemple #27
0
bool com::masaers::cmdlp::expand_prefix(const std::string& prefix_path, std::vector<std::string>& filenames) {
	bool result = true;
	std::string dirname;
	std::string fileprefix;
	{
		std::size_t i = prefix_path.find_last_of('/');
		if (i != std::string::npos) {
			// Found a last slash to split on
			dirname    = prefix_path.substr(0, i);
			fileprefix = prefix_path.substr(i + 1);
		} else {
		   // No slashes found
			dirname    = ".";
			fileprefix = prefix_path;
		}
	}
	std::string path = dirname;
	path.push_back('/');
	std::size_t fpos = path.size();
	struct dirent* entry;
	DIR* dir = opendir(dirname.c_str());
	if (dir != nullptr) {
		for (entry = readdir(dir); entry != nullptr; entry = readdir(dir)) {
			const char* filename = entry->d_name;
			if (prefix_match(fileprefix, filename)) {
				path.replace(fpos, path.size() - fpos, filename);
				if (is_file(path)) {
					filenames.push_back(normalize_path(path));
				}
			}
		}
		closedir(dir);
	} else {
		result = false;
	}
	return result;
}
Exemple #28
0
static void
do_use_prefix(int len, struct rr_pco_match *rpm,
	      struct in6_rrenumreq *irr, int ifindex)
{
	struct rr_pco_use *rpu, *rpulim;
	struct rainfo *rai;
	struct prefix *pp;

	rpu = (struct rr_pco_use *)(rpm + 1);
	rpulim = (struct rr_pco_use *)((char *)rpm + len);

	if (rpu == rpulim) {	/* no use prefix */
		if (rpm->rpm_code == RPM_PCO_ADD)
			return;

		irr->irr_u_uselen = 0;
		irr->irr_u_keeplen = 0;
		irr->irr_raf_mask_onlink = 0;
		irr->irr_raf_mask_auto = 0;
		irr->irr_vltime = 0;
		irr->irr_pltime = 0;
		memset(&irr->irr_flags, 0, sizeof(irr->irr_flags));
		irr->irr_useprefix.sin6_len = 0; /* let it mean, no addition */
		irr->irr_useprefix.sin6_family = 0;
		irr->irr_useprefix.sin6_addr = in6addr_any;
		if (ioctl(s, rrcmd2pco[rpm->rpm_code], (caddr_t)irr) < 0 &&
		    errno != EADDRNOTAVAIL)
			syslog(LOG_ERR, "<%s> ioctl: %s", __func__,
			       strerror(errno));
		return;
	}

	for (rpu = (struct rr_pco_use *)(rpm + 1),
	     rpulim = (struct rr_pco_use *)((char *)rpm + len);
	     rpu < rpulim;
	     rpu += 1) {
		/* init in6_rrenumreq fields */
		irr->irr_u_uselen = rpu->rpu_uselen;
		irr->irr_u_keeplen = rpu->rpu_keeplen;
		irr->irr_raf_mask_onlink =
			!!(rpu->rpu_ramask & ICMP6_RR_PCOUSE_RAFLAGS_ONLINK);
		irr->irr_raf_mask_auto =
			!!(rpu->rpu_ramask & ICMP6_RR_PCOUSE_RAFLAGS_AUTO);
		irr->irr_vltime = ntohl(rpu->rpu_vltime);
		irr->irr_pltime = ntohl(rpu->rpu_pltime);
		irr->irr_raf_onlink =
			(rpu->rpu_raflags & ICMP6_RR_PCOUSE_RAFLAGS_ONLINK) == 0 ? 0 : 1;
		irr->irr_raf_auto =
			(rpu->rpu_raflags & ICMP6_RR_PCOUSE_RAFLAGS_AUTO) == 0 ? 0 : 1;
		irr->irr_rrf_decrvalid =
			(rpu->rpu_flags & ICMP6_RR_PCOUSE_FLAGS_DECRVLTIME) == 0 ? 0 : 1;
		irr->irr_rrf_decrprefd =
			(rpu->rpu_flags & ICMP6_RR_PCOUSE_FLAGS_DECRPLTIME) == 0 ? 0 : 1;
		irr->irr_useprefix.sin6_len = sizeof(irr->irr_useprefix);
		irr->irr_useprefix.sin6_family = AF_INET6;
		irr->irr_useprefix.sin6_addr = rpu->rpu_prefix;

		if (ioctl(s, rrcmd2pco[rpm->rpm_code], (caddr_t)irr) < 0 &&
		    errno != EADDRNOTAVAIL)
			syslog(LOG_ERR, "<%s> ioctl: %s", __func__,
			       strerror(errno));

		/* very adhoc: should be rewritten */
		if (rpm->rpm_code == RPM_PCO_CHANGE &&
		    IN6_ARE_ADDR_EQUAL(&rpm->rpm_prefix, &rpu->rpu_prefix) &&
		    rpm->rpm_matchlen == rpu->rpu_uselen &&
		    rpu->rpu_uselen == rpu->rpu_keeplen) {
			if ((rai = if_indextorainfo(ifindex)) == NULL)
				continue; /* non-advertising IF */

			for (pp = rai->prefix.next; pp != &rai->prefix;
			     pp = pp->next) {
				struct timeval now;

				if (prefix_match(&pp->prefix, pp->prefixlen,
						 &rpm->rpm_prefix,
						 rpm->rpm_matchlen)) {
					/* change parameters */
					pp->validlifetime = ntohl(rpu->rpu_vltime);
					pp->preflifetime = ntohl(rpu->rpu_pltime);
					if (irr->irr_rrf_decrvalid) {
						gettimeofday(&now, 0);
						pp->vltimeexpire =
							now.tv_sec + pp->validlifetime;
					} else
						pp->vltimeexpire = 0;
					if (irr->irr_rrf_decrprefd) {
						gettimeofday(&now, 0);
						pp->pltimeexpire =
							now.tv_sec + pp->preflifetime;
					} else
						pp->pltimeexpire = 0;
				}
			}
		}
	}
}
Exemple #29
0
int wvtest_run_all(char * const *prefixes)
{
    int old_valgrind_errs = 0, new_valgrind_errs;
    int old_valgrind_leaks = 0, new_valgrind_leaks;

#ifdef _WIN32
    /* I should be doing something to do with SetTimer here,
     * not sure exactly what just yet */
#else
    char *disable = getenv("WVTEST_DISABLE_TIMEOUT");
    if (disable != NULL && disable[0] != '\0' && disable[0] != '0')
        signal(SIGALRM, SIG_IGN);
    else
        signal(SIGALRM, alarm_handler);
    alarm(MAX_TEST_TIME);
#endif
    start_time = time(NULL);

    // make sure we can always start out in the same directory, so tests have
    // access to their files.  If a test uses chdir(), we want to be able to
    // reverse it.
    char wd[1024];
    if (!getcwd(wd, sizeof(wd)))
	strcpy(wd, ".");

    const char *slowstr1 = getenv("WVTEST_MIN_SLOWNESS");
    const char *slowstr2 = getenv("WVTEST_MAX_SLOWNESS");
    int min_slowness = 0, max_slowness = 65535;
    if (slowstr1) min_slowness = atoi(slowstr1);
    if (slowstr2) max_slowness = atoi(slowstr2);

#ifdef _WIN32
    run_twice = false;
#else
    char *parallel_str = getenv("WVTEST_PARALLEL");
    if (parallel_str)
        run_twice = atoi(parallel_str) > 0;
#endif

    // there are lots of fflush() calls in here because stupid win32 doesn't
    // flush very often by itself.
    fails = runs = 0;
    struct WvTest *cur;

    for (cur = wvtest_first; cur != NULL; cur = cur->next)
    {
	if (cur->slowness <= max_slowness
	    && cur->slowness >= min_slowness
	    && (!prefixes
		|| prefix_match(cur->idstr, prefixes)
		|| prefix_match(cur->descr, prefixes)))
	{
#ifndef _WIN32
            // set SIGPIPE back to default, helps catch tests which don't set
            // this signal to SIG_IGN (which is almost always what you want)
            // on startup
            signal(SIGPIPE, SIG_DFL);

            pid_t child = 0;
            if (run_twice)
            {
                // I see everything twice!
                printf("Running test in parallel.\n");
                child = fork();
            }
#endif

	    printf("\nTesting \"%s\" in %s:\n", cur->descr, cur->idstr);
	    fflush(stdout);

	    cur->main();
	    chdir(wd);

	    new_valgrind_errs = memerrs();
	    WVPASS(new_valgrind_errs == old_valgrind_errs);
	    old_valgrind_errs = new_valgrind_errs;

	    new_valgrind_leaks = memleaks();
	    WVPASS(new_valgrind_leaks == old_valgrind_leaks);
	    old_valgrind_leaks = new_valgrind_leaks;

	    fflush(stderr);
	    printf("\n");
	    fflush(stdout);

#ifndef _WIN32
            if (run_twice)
            {
                if (!child)
                {
                    // I see everything once!
                    printf("Child exiting.\n");
                    _exit(0);
                }
                else
                {
                    printf("Waiting for child to exit.\n");
                    int result;
                    while ((result = waitpid(child, NULL, 0)) == -1 &&
                            errno == EINTR)
                        printf("Waitpid interrupted, retrying.\n");
                }
            }
#endif

            WVPASS(no_running_children());
	}
    }

    WVPASS(runs > 0);

    if (prefixes && *prefixes && **prefixes)
	printf("WvTest: WARNING: only ran tests starting with "
	       "specifed prefix(es).\n");
    else
	printf("WvTest: ran all tests.\n");
    printf("WvTest: %d test%s, %d failure%s.\n",
	   runs, runs==1 ? "" : "s",
	   fails, fails==1 ? "": "s");
    fflush(stdout);

    return fails != 0;
}
Exemple #30
0
boolean IdxsynFile_Lookup(IdxsynFile * pMe, const char *str, long *idx, long *idx_suggest)
{
	long idx2 = 0, idx_suggest2 = 0;
	boolean bFound = FALSE;
	long iFrom = 0;
	long iTo = pMe->npages - 2;
	int cmpint = 0;
	long iThisIndex = 0;

	if (stardict_strcmp(str, pMe->first.keystr)<0) {
		*idx = 0;
		*idx_suggest = 0;
		return FALSE;
	} else if (stardict_strcmp(str, pMe->real_last.keystr) >0) {
		*idx = INVALID_INDEX;
		*idx_suggest = pMe->wordcount - 1;
		return FALSE;
	} else {
		// find the page number where the search word might be
		iFrom = 0;
		iThisIndex = 0;
		while (iFrom <= iTo) {
			iThisIndex = (iFrom + iTo) / 2;
			cmpint = stardict_strcmp(str, IdxsynFile_GetFirstOnPageKey(pMe, iThisIndex));
			if (cmpint > 0)
				iFrom = iThisIndex + 1;
			else if (cmpint < 0)
				iTo = iThisIndex - 1;
			else {
				bFound = TRUE;
				break;
			}
		}
		if (!bFound) {
			idx2 = iTo;    //prev
		} else {
			idx2 = iThisIndex;
		}
	}
	if (!bFound) {
		// the search word is on the page number idx if it's anywhere
		uint32 netr = IdxsynFile_LoadPage(pMe, idx2);
		iFrom =  1; // Needn't search the first word anymore.
		iTo = netr - 1;
		iThisIndex = 0;
		while (iFrom <= iTo) {
			iThisIndex = (iFrom + iTo) / 2;
			cmpint = stardict_strcmp(str, pMe->page.entries[iThisIndex].keystr);
			if (cmpint > 0)
				iFrom = iThisIndex + 1;
			else if (cmpint < 0)
				iTo = iThisIndex - 1;
			else {
				bFound = TRUE;
				break;
			}
		}
		idx2 *= ENTR_PER_PAGE;
		if (!bFound) {
			int best, back;
			idx2 += iFrom;    //next
			idx_suggest2 = idx2;

			best = prefix_match (str, pMe->page.entries[idx_suggest2 % ENTR_PER_PAGE].keystr);
			for (;;) {
				if ((iTo = idx_suggest2 - 1) < 0)
					break;
				if (idx_suggest2 % ENTR_PER_PAGE == 0)
					IdxsynFile_LoadPage(pMe, iTo / ENTR_PER_PAGE);
				back = prefix_match (str, pMe->page.entries[iTo % ENTR_PER_PAGE].keystr);
				if (!back || back < best)
					break;
				best = back;
				idx_suggest2 = iTo;
			}
		} else {
			idx2 += iThisIndex;
			idx_suggest2 = idx2;
		}
	} else {
		idx2 *=ENTR_PER_PAGE;
		idx_suggest2 = idx2;
	}

	*idx = idx2;
	*idx_suggest = idx_suggest2;

	return bFound;
}