Exemplo n.º 1
0
void
setUp(void) {
  size_t s;
  lagopus_ip_address_t *addr;
  struct sockaddr_in *sin;
  uint8_t *p;

  /* Make the root flowinfo. */
  TEST_ASSERT_NULL(flowinfo);
  flowinfo = new_flowinfo_vlan_vid();
  TEST_ASSERT_NOT_NULL(flowinfo);

  TEST_ASSERT_FLOWINFO_FLOW_NUM(flowinfo, 0, __func__);

  /*
   * Make the test flows.
   *
   * ARP matches have the prerequisite.
   */
  for (s = 0; s < ARRAY_LEN(test_flow); s++) {
    TEST_ASSERT_NULL(test_flow[s]);
    test_flow[s] = allocate_test_flow(10 * sizeof(struct match));
    TEST_ASSERT_NOT_NULL(test_flow[s]);
    test_flow[s]->priority = (int)s;
    FLOW_ADD_ARP_PREREQUISITE(test_flow[s]);
  }

  /* Make the test addresses. */
  for (s = 0; s < ARRAY_LEN(test_flow); s++) {
    lagopus_ip_address_create(ipv4srcstr, true, &addr);
    sin = NULL;
    lagopus_ip_address_sockaddr_get(addr, &sin);
    lagopus_ip_address_destroy(addr);
    p = (uint8_t *)&sin->sin_addr.s_addr;
    p[sizeof(sin->sin_addr.s_addr) - 1] = (uint8_t)(TEST_IPV4_ADDR_LSB(s) & 0xff);
    OS_MEMCPY(&ipv4src[s], &sin->sin_addr, sizeof(ipv4src[s]));

    lagopus_ip_address_create(ipv4tgtstr, true, &addr);
    sin = NULL;
    lagopus_ip_address_sockaddr_get(addr, &sin);
    lagopus_ip_address_destroy(addr);
    p = (uint8_t *)&sin->sin_addr.s_addr;
    p[sizeof(sin->sin_addr.s_addr) - 1] = (uint8_t)(TEST_IPV4_ADDR_LSB(s) & 0xff);
    OS_MEMCPY(&ipv4tgt[s], &sin->sin_addr, sizeof(ipv4tgt[s]));

    lagopus_ip_address_create(ipv4maskstr, true, &addr);
    sin = NULL;
    lagopus_ip_address_sockaddr_get(addr, &sin);
    lagopus_ip_address_destroy(addr);
    OS_MEMCPY(&ipv4mask[s], &sin->sin_addr, sizeof(ipv4mask[s]));
  }
}
Exemplo n.º 2
0
lagopus_result_t
channel_mgr_channel_create(const char *channel_name,
                           lagopus_ip_address_t *dst_addr,
                           uint16_t dst_port, lagopus_ip_address_t *src_addr, uint16_t src_port,
                           datastore_channel_protocol_t protocol) {
  lagopus_result_t ret;
  struct channel *chan = NULL;
  struct sockaddr *sa;
  struct sockaddr_storage ss = {0};
  struct addrunion daddr = {0}, saddr = {0};
  enum channel_protocol cprotocol;


  sa = (struct sockaddr *) &ss;
  ret = lagopus_ip_address_sockaddr_get(dst_addr, &sa);
  if (ret != LAGOPUS_RESULT_OK) {
    return ret;
  }
  sockaddr_to_addrunion(sa, &daddr);

  ret = channel_add_internal(channel_name, &daddr, &chan);
  if (ret != LAGOPUS_RESULT_OK) {
    return ret;
  }

  ret = channel_port_set(chan, dst_port);
  if (ret != LAGOPUS_RESULT_OK) {
    goto fail;
  }

  if (src_addr != NULL) {
    ret = lagopus_ip_address_sockaddr_get(src_addr, &sa);
    if (ret != LAGOPUS_RESULT_OK) {
      goto fail;
    }
    sockaddr_to_addrunion(sa, &saddr);

    ret = channel_local_addr_set(chan, &saddr);
    if (ret != LAGOPUS_RESULT_OK) {
      goto fail;
    }
  }

  ret = channel_local_port_set(chan, src_port);
  if (ret != LAGOPUS_RESULT_OK) {
    goto fail;
  }

  switch (protocol) {
    case DATASTORE_CHANNEL_PROTOCOL_TCP:
      cprotocol = PROTOCOL_TCP;
      break;
    case DATASTORE_CHANNEL_PROTOCOL_TLS:
      cprotocol = PROTOCOL_TLS;
      break;
    default:
      return LAGOPUS_RESULT_INVALID_ARGS;
  }

  ret = channel_protocol_set(chan, cprotocol);
  if (ret != LAGOPUS_RESULT_OK) {
    goto fail;
  }

  return LAGOPUS_RESULT_OK;

fail:
  channel_delete_internal(channel_name);
  return ret;
}
Exemplo n.º 3
0
void
test_ip_address_public_sockaddr(void) {
  lagopus_result_t rc;
  lagopus_ip_address_t *ip1 = NULL;
  lagopus_ip_address_t *ip2 = NULL;
  lagopus_ip_address_t *ip3 = NULL;
  lagopus_ip_address_t *ip4 = NULL;
  const char *name1 = "127.0.0.1";
  const char *name2 = "::1";
  const char *name3 = "localhost";
  struct sockaddr *actual_sockaddr1 = NULL;
  struct sockaddr *actual_sockaddr2 = NULL;
  struct sockaddr *actual_sockaddr3 = NULL;
  struct sockaddr *actual_sockaddr4 = NULL;
  socklen_t actual_sockaddr_len = 0;

  char ipv4_addr[INET_ADDRSTRLEN];
  char ipv6_addr[INET6_ADDRSTRLEN];

  rc = lagopus_ip_address_create(name1, true, &ip1);
  TEST_ASSERT_EQUAL(LAGOPUS_RESULT_OK, rc);

  rc = lagopus_ip_address_create(name2, false, &ip2);
  TEST_ASSERT_EQUAL(LAGOPUS_RESULT_OK, rc);

  rc = lagopus_ip_address_create(name3, true, &ip3);
  TEST_ASSERT_EQUAL(LAGOPUS_RESULT_OK, rc);

  // create empty ip.
  ip4 = (lagopus_ip_address_t *) calloc(1, (sizeof(lagopus_ip_address_t)));
  TEST_ASSERT_NOT_NULL(ip4);

  // Normal case of getter
  {
    /* sockaddr. */
    rc = lagopus_ip_address_sockaddr_get(ip1, &actual_sockaddr1);
    TEST_ASSERT_EQUAL(LAGOPUS_RESULT_OK, rc);
    TEST_ASSERT_EQUAL(AF_INET, actual_sockaddr1->sa_family);
    inet_ntop(AF_INET, &((struct sockaddr_in *) actual_sockaddr1)->sin_addr,
              ipv4_addr, INET_ADDRSTRLEN);
    TEST_ASSERT_EQUAL_STRING(name1, ipv4_addr);

    /* length of sockaddr. */
    rc = lagopus_ip_address_sockaddr_len_get(ip1, &actual_sockaddr_len);
    TEST_ASSERT_EQUAL(LAGOPUS_RESULT_OK, rc);
    TEST_ASSERT_EQUAL(sizeof(struct sockaddr_in), actual_sockaddr_len);

    /* sockaddr. */
    rc = lagopus_ip_address_sockaddr_get(ip2, &actual_sockaddr2);
    TEST_ASSERT_EQUAL(LAGOPUS_RESULT_OK, rc);
    TEST_ASSERT_EQUAL(AF_INET6, actual_sockaddr2->sa_family);
    inet_ntop(AF_INET6, &((struct sockaddr_in6 *) actual_sockaddr2)->sin6_addr,
              ipv6_addr, INET6_ADDRSTRLEN);
    TEST_ASSERT_EQUAL_STRING(name2, ipv6_addr);

    /* length of sockaddr. */
    rc = lagopus_ip_address_sockaddr_len_get(ip2, &actual_sockaddr_len);
    TEST_ASSERT_EQUAL(LAGOPUS_RESULT_OK, rc);
    TEST_ASSERT_EQUAL(sizeof(struct sockaddr_in6), actual_sockaddr_len);

    /* sockaddr. */
    rc = lagopus_ip_address_sockaddr_get(ip3, &actual_sockaddr3);
    TEST_ASSERT_EQUAL(LAGOPUS_RESULT_OK, rc);
    TEST_ASSERT_EQUAL(AF_INET, actual_sockaddr3->sa_family);
    inet_ntop(AF_INET, &((struct sockaddr_in *) actual_sockaddr3)->sin_addr,
              ipv4_addr, INET_ADDRSTRLEN);
    TEST_ASSERT_EQUAL_STRING(name1, ipv4_addr);

    /* length of sockaddr. */
    rc = lagopus_ip_address_sockaddr_len_get(ip3, &actual_sockaddr_len);
    TEST_ASSERT_EQUAL(LAGOPUS_RESULT_OK, rc);
    TEST_ASSERT_EQUAL(sizeof(struct sockaddr_in), actual_sockaddr_len);
  }

  // Abnormal case of getter
  {
    rc = lagopus_ip_address_sockaddr_get(NULL, &actual_sockaddr1);
    TEST_ASSERT_EQUAL(LAGOPUS_RESULT_INVALID_ARGS, rc);

    rc = lagopus_ip_address_sockaddr_get(ip1, NULL);
    TEST_ASSERT_EQUAL(LAGOPUS_RESULT_INVALID_ARGS, rc);

    rc = lagopus_ip_address_sockaddr_get(ip4, &actual_sockaddr4);
    TEST_ASSERT_EQUAL(LAGOPUS_RESULT_INVALID_OBJECT, rc);

    rc = lagopus_ip_address_sockaddr_len_get(NULL, &actual_sockaddr_len);
    TEST_ASSERT_EQUAL(LAGOPUS_RESULT_INVALID_ARGS, rc);

    rc = lagopus_ip_address_sockaddr_len_get(ip1, NULL);
    TEST_ASSERT_EQUAL(LAGOPUS_RESULT_INVALID_ARGS, rc);
  }

  free(actual_sockaddr1);
  free(actual_sockaddr2);
  free(actual_sockaddr3);
  free(actual_sockaddr4);

  lagopus_ip_address_destroy(ip1);
  lagopus_ip_address_destroy(ip2);
  lagopus_ip_address_destroy(ip3);
  free(ip4);
}