Exemplo n.º 1
0
static int addMessageHeader(struct rfc5444_writer *wr, struct rfc5444_writer_message *msg) {
  struct netaddr orig;

  CHECK_TRUE(0 == netaddr_from_string(&orig, "10.5.11.1"), "failed to initialize ip");

  rfc5444_writer_set_msg_header(wr, msg, true, false, false, false);
  rfc5444_writer_set_msg_originator(wr, msg, netaddr_get_binptr(&orig));
  return RFC5444_OKAY;
}
Exemplo n.º 2
0
static void
test_netaddr_from_string(void) {
  size_t i;
  int ret;
  struct netaddr netaddr_buf;

  START_TEST();

  /* test successful string to netaddr conversions first */
  for (i=0; i<sizeof(string_tests) / sizeof(*string_tests); i++) {
    ret = netaddr_from_string(&netaddr_buf, string_tests[i].str);
    CHECK_TRUE(ret == 0, "netaddr_from_string (%s) returns %d", string_tests[i].str, ret);

    if (ret == 0) {
      CHECK_TRUE(memcmp(&netaddr_buf, &string_tests[i].bin, sizeof(netaddr_buf)) == 0,
          "netaddr_from_string (%s) value", string_tests[i].str);
    }
  }

  /* test special cases of string to netaddr conversions next */
  for (i=0; i<sizeof(good_netaddr_from_string) / sizeof(*good_netaddr_from_string); i++) {
    ret = netaddr_from_string(&netaddr_buf, good_netaddr_from_string[i].str);
    CHECK_TRUE(ret == 0, "netaddr_from_string (%s) returns %d", good_netaddr_from_string[i].str, ret);

    if (ret == 0) {
      CHECK_TRUE(memcmp(&netaddr_buf, &good_netaddr_from_string[i].bin, sizeof(netaddr_buf)) == 0,
          "netaddr_from_string (%s) value", good_netaddr_from_string[i].str);
    }
  }

  /* test error cases of string to netaddr conversions next */
  for (i=0; i<sizeof(bad_netaddr_from_string)/sizeof(*bad_netaddr_from_string); i++) {
    CHECK_TRUE (0 != netaddr_from_string(&netaddr_buf, bad_netaddr_from_string[i]),
        "netaddr_from_string %s returns %d", bad_netaddr_from_string[i], ret);
  }

  END_TEST();
}
Exemplo n.º 3
0
static void
test_netaddr_create_host(void) {
  struct netaddr netmask, host, result;
  struct netaddr_str buf1, buf2, buf3;
  char buffer1[17], buffer2[17];
  size_t i;

  memset(buffer1, 255, sizeof(buffer1));
  memset(buffer2, 0, sizeof(buffer2));

  START_TEST();

  for (i=0; i<ARRAYSIZE(_create_host_test); i++) {
    CHECK_TRUE(netaddr_from_string(&netmask, _create_host_test[i].netmask) == 0,
        "error in parsing netmask %"PRINTF_SIZE_T_SPECIFIER" %s", i, _create_host_test[i].netmask);
    CHECK_TRUE(netaddr_from_string(&result, _create_host_test[i].result) == 0,
        "error in parsing result %"PRINTF_SIZE_T_SPECIFIER" %s", i, _create_host_test[i].result);

    if (_create_host_test[i].buf_len >= 0) {
      CHECK_TRUE(netaddr_create_host_bin(&host, &netmask, buffer1, _create_host_test[i].buf_len) == 0,
          "error in creating host %"PRINTF_SIZE_T_SPECIFIER" %s with length %d",
          i, netaddr_to_string(&buf1,  &netmask), _create_host_test[i].buf_len);
    }
    else {
      CHECK_TRUE(netaddr_create_host_bin(&host, &netmask, buffer2, -_create_host_test[i].buf_len) == 0,
          "error in creating host %"PRINTF_SIZE_T_SPECIFIER" %s with length %d",
          i, netaddr_to_string(&buf1,  &netmask), _create_host_test[i].buf_len);
    }

    CHECK_TRUE(netaddr_cmp(&host, &result) == 0,
        "Error, host %"PRINTF_SIZE_T_SPECIFIER" %s != result %s (netmask %s, len=%d)",
        i, netaddr_to_string(&buf1, &host), netaddr_to_string(&buf2, &result),
        netaddr_to_string(&buf3, &netmask), _create_host_test[i].buf_len);
  }

  END_TEST();
}
Exemplo n.º 4
0
static void addAddresses(struct rfc5444_writer *wr) {
  struct netaddr ip;
  struct rfc5444_writer_address *addr;
  char value0 =  0, value1 = 1;
  uint16_t metric;

  CHECK_TRUE(0 == netaddr_from_string(&ip, "10.5.11.1"), "failed to initialize ip");
  addr = rfc5444_writer_add_address(wr, cpr.creator, &ip, false);
  rfc5444_writer_add_addrtlv(wr, addr, &addrtlvs[IDX_LOCAL_IF], &value1, 1, false);

  CHECK_TRUE(0 == netaddr_from_string(&ip, "10.5.11.2"), "failed to initialize ip");
  addr = rfc5444_writer_add_address(wr, cpr.creator, &ip, false);
  rfc5444_writer_add_addrtlv(wr, addr, &addrtlvs[IDX_LOCAL_IF], &value1, 1, false);

  CHECK_TRUE(0 == netaddr_from_string(&ip, "10.5.11.3"), "failed to initialize ip");
  addr = rfc5444_writer_add_address(wr, cpr.creator, &ip, false);
  rfc5444_writer_add_addrtlv(wr, addr, &addrtlvs[IDX_LOCAL_IF], &value0, 1, false);

  CHECK_TRUE(0 == netaddr_from_string(&ip, "10.5.10.1"), "failed to initialize ip");
  addr = rfc5444_writer_add_address(wr, cpr.creator, &ip, false);
  rfc5444_writer_add_addrtlv(wr, addr, &addrtlvs[IDX_OTHERNEIGH], &value1, 1, false);
  metric = htons(0x2024);
  rfc5444_writer_add_addrtlv(wr, addr, &addrtlvs[IDX_METRIC1], &metric, sizeof(metric), true);
  metric = htons(0x1013);
  rfc5444_writer_add_addrtlv(wr, addr, &addrtlvs[IDX_METRIC2], &metric, sizeof(metric), true);

  CHECK_TRUE(0 == netaddr_from_string(&ip, "10.5.12.1"), "failed to initialize ip");
  addr = rfc5444_writer_add_address(wr, cpr.creator, &ip, false);
  rfc5444_writer_add_addrtlv(wr, addr, &addrtlvs[IDX_OTHERNEIGH], &value1, 1, false);
  metric = htons(0x202f);
  rfc5444_writer_add_addrtlv(wr, addr, &addrtlvs[IDX_METRIC1], &metric, sizeof(metric), true);
  metric = htons(0x1013);
  rfc5444_writer_add_addrtlv(wr, addr, &addrtlvs[IDX_METRIC2], &metric, sizeof(metric), true);

  CHECK_TRUE(0 == netaddr_from_string(&ip, "10.5.12.2"), "failed to initialize ip");
  addr = rfc5444_writer_add_address(wr, cpr.creator, &ip, false);
  rfc5444_writer_add_addrtlv(wr, addr, &addrtlvs[IDX_OTHERNEIGH], &value1, 1, false);
  metric = htons(0x202f);
  rfc5444_writer_add_addrtlv(wr, addr, &addrtlvs[IDX_METRIC1], &metric, sizeof(metric), true);
  metric = htons(0x1013);
  rfc5444_writer_add_addrtlv(wr, addr, &addrtlvs[IDX_METRIC2], &metric, sizeof(metric), true);

  CHECK_TRUE(0 == netaddr_from_string(&ip, "10.5.12.3"), "failed to initialize ip");
  addr = rfc5444_writer_add_address(wr, cpr.creator, &ip, false);
  rfc5444_writer_add_addrtlv(wr, addr, &addrtlvs[IDX_OTHERNEIGH], &value1, 1, false);
  metric = htons(0x202f);
  rfc5444_writer_add_addrtlv(wr, addr, &addrtlvs[IDX_METRIC1], &metric, sizeof(metric), true);
  metric = htons(0x1013);
  rfc5444_writer_add_addrtlv(wr, addr, &addrtlvs[IDX_METRIC2], &metric, sizeof(metric), true);
}
Exemplo n.º 5
0
/**
 * 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;
}
Exemplo n.º 6
0
static void addAddresses(struct rfc5444_writer *wr) {
    struct netaddr ip; // = { { 10,1,0,101}, AF_INET, 32 };
    struct rfc5444_writer_address *addr;
    int value1 = 1;
    int i,j;

    CHECK_TRUE(0 == netaddr_from_string(&ip, "10.0.0.0"), "failed to initialize ip");

    for (i=0; i<100; i++) {
        for (j=0; j<100; j++) {
            ip._addr[2] = i;
            ip._addr[3] = j;
            //ip._addr[0] = 10; ip._addr[1] = 1; ip._addr[2] = 0; ip._addr[3] = 101;
            addr = rfc5444_writer_add_address(wr, cpr.creator, &ip, false);
            CHECK_TRUE(rfc5444_writer_add_addrtlv(wr, addr, &addrtlvs, &value1, sizeof(value1), false) == 0,
                       "Out of memory for address tlv");
            value1++;
        }
    }
}