コード例 #1
0
static void
test_netaddr_is_in_subnet(void) {
  struct netaddr_str str1, str2;
  size_t a, s;
  START_TEST();

  for (s = 0; s < sizeof(in_subnet_subnets) / sizeof(*in_subnet_subnets); s++) {
    for (a = 0; a < sizeof(in_subnet_addrs) / sizeof(*in_subnet_addrs); a++) {
      CHECK_TRUE(
          in_subnet_results[a][s] == netaddr_binary_is_in_subnet(&in_subnet_subnets[s], &in_subnet_addrs[a],
              netaddr_get_maxprefix(&in_subnet_addrs[a])/8, in_subnet_addrs[a]._type),
          "%s should %sbe in %s",
          netaddr_to_string(&str1, &in_subnet_addrs[a]),
          in_subnet_results[a][s] ? "" : "not ",
          netaddr_to_string(&str2, &in_subnet_subnets[s]));

      CHECK_TRUE(
          in_subnet_results[a][s] == netaddr_is_in_subnet(&in_subnet_subnets[s], &in_subnet_addrs[a]),
          "%s should %sbe in %s",
          netaddr_to_string(&str1, &in_subnet_addrs[a]),
          in_subnet_results[a][s] ? "" : "not ",
          netaddr_to_string(&str2, &in_subnet_subnets[s]));
    }
  }

  END_TEST();
}
コード例 #2
0
ファイル: cfg_validate.c プロジェクト: gabri94/OONF
/**
 * Validate if a value against a specific network address
 * @param out output buffer for error messages
 * @param section_name name of configuration section
 * @param entry_name name of configuration entry
 * @param value value that needs to be validated
 * @param prefix true if the address might be a network prefix
 * @param af_types list of address families the address might be
 * @param af_types_count number of address families specified
 * @return 0 if value is valid, -1 otherwise
 */
int
cfg_validate_netaddr(struct autobuf *out, const char *section_name,
    const char *entry_name, const char *value,
    bool prefix, const int8_t *af_types, size_t af_types_count) {
  struct netaddr addr;
  uint8_t max_prefix;
  size_t i;

  if (netaddr_from_string(&addr, value)) {
    cfg_append_printable_line(out, "Value '%s' for entry '%s'"
        " in section %s is no valid network address",
        value, entry_name, section_name);
    return -1;
  }

  max_prefix = netaddr_get_maxprefix(&addr);

  /* check prefix length */
  if (netaddr_get_prefix_length(&addr) > max_prefix) {
    cfg_append_printable_line(out, "Value '%s' for entry '%s'"
        " in section %s has an illegal prefix length",
        value, entry_name, section_name);
    return -1;
  }
  if (!prefix && netaddr_get_prefix_length(&addr) != max_prefix) {
    cfg_append_printable_line(out, "Value '%s' for entry '%s'"
        " in section %s must be a single address, not a prefix",
        value, entry_name, section_name);
    return -1;
  }

  for (i=0; i<af_types_count; i++) {
    if (af_types[i] == netaddr_get_address_family(&addr)) {
      return 0;
    }
  }

  /* at least one condition was set, but no one matched */
  cfg_append_printable_line(out, "Value '%s' for entry '%s'"
      " in section '%s' is wrong address type",
      value, entry_name, section_name);
  return -1;
}