void test_channel_port_get_set_unset(void) { uint16_t port; lagopus_result_t ret; struct channel *channel; channel = s_create_data_channel(); ret = channel_port_set(channel, 10101); TEST_ASSERT_EQUAL_MESSAGE(LAGOPUS_RESULT_OK, ret, "channel_port_set() error."); ret = channel_port_get(channel, &port); TEST_ASSERT_EQUAL_MESSAGE(LAGOPUS_RESULT_OK, ret, "channel_port_get() error."); TEST_ASSERT_EQUAL_MESSAGE(10101, port, "channel_port_get() port error"); ret = channel_port_unset(channel); TEST_ASSERT_EQUAL_MESSAGE(LAGOPUS_RESULT_OK, ret, "channel_port_unset() error."); ret = channel_port_get(channel, &port); TEST_ASSERT_EQUAL_MESSAGE(LAGOPUS_RESULT_OK, ret, "channel_port_get() error."); TEST_ASSERT_EQUAL_MESSAGE(6633, port, "channel_port_get() port error"); session_destroy(channel_session_get(channel)); free(channel); }
void test_channel_tcp(void) { int sock4, sock6; socklen_t size; struct sockaddr_in sin; lagopus_ip_address_t *addr4 = NULL; lagopus_ip_address_t *addr6 = NULL; struct channel_list *chan_list ; struct channel *chan4, *chan6, *chan = NULL; lagopus_result_t ret; int cnt; printf("test_channel_tcp in\n"); channel_mgr_initialize(); ret = channel_mgr_channels_lookup_by_dpid(dpid, &chan_list ); TEST_ASSERT_EQUAL(LAGOPUS_RESULT_NOT_FOUND, ret); TEST_ASSERT_EQUAL(NULL, chan_list ); ret = lagopus_ip_address_create("127.0.0.1", true, &addr4); TEST_ASSERT_EQUAL(LAGOPUS_RESULT_OK, ret); ret = lagopus_ip_address_create("::1", false, &addr6); TEST_ASSERT_EQUAL(LAGOPUS_RESULT_OK, ret); ret = channel_mgr_channel_add(bridge_name, dpid, addr4); TEST_ASSERT_EQUAL(LAGOPUS_RESULT_OK, ret); ret = channel_mgr_channel_add(bridge_name, dpid, addr6); TEST_ASSERT_EQUAL(LAGOPUS_RESULT_OK, ret); ret = channel_mgr_channel_add(bridge_name, dpid, addr4); TEST_ASSERT_EQUAL(LAGOPUS_RESULT_ALREADY_EXISTS, ret); ret = channel_mgr_channel_lookup(bridge_name, addr4, &chan4); TEST_ASSERT_EQUAL(LAGOPUS_RESULT_OK, ret); TEST_ASSERT_EQUAL(0, channel_id_get(chan4)); ret = channel_port_set(chan4, 10022); TEST_ASSERT_EQUAL(LAGOPUS_RESULT_OK, ret); ret = channel_local_port_set(chan4, 20022); TEST_ASSERT_EQUAL(LAGOPUS_RESULT_OK, ret); ret = channel_local_addr_set(chan4, addr4); TEST_ASSERT_EQUAL(LAGOPUS_RESULT_OK, ret); ret = channel_mgr_channel_add(bridge_name, dpid, addr6); TEST_ASSERT_EQUAL(LAGOPUS_RESULT_ALREADY_EXISTS, ret); ret = channel_mgr_channel_lookup(bridge_name, addr6, &chan6); TEST_ASSERT_EQUAL(1, channel_id_get(chan6)); TEST_ASSERT_EQUAL(LAGOPUS_RESULT_OK, ret); ret = channel_port_set(chan6, 10023); TEST_ASSERT_EQUAL(LAGOPUS_RESULT_OK, ret); ret = channel_local_port_set(chan6, 20023); TEST_ASSERT_EQUAL(LAGOPUS_RESULT_OK, ret); ret = channel_local_addr_set(chan6, addr6); TEST_ASSERT_EQUAL(LAGOPUS_RESULT_OK, ret); ret = channel_mgr_channel_lookup_by_channel_id(dpid, 1, &chan); TEST_ASSERT_EQUAL(LAGOPUS_RESULT_OK, ret); TEST_ASSERT_NOT_EQUAL(NULL, chan); ret = channel_mgr_channel_lookup_by_channel_id(dpid, 9999, &chan); TEST_ASSERT_EQUAL(LAGOPUS_RESULT_NOT_FOUND, ret); TEST_ASSERT_EQUAL(NULL, chan); cnt = 0; ret = channel_mgr_channels_lookup_by_dpid(dpid, &chan_list ); TEST_ASSERT_EQUAL(LAGOPUS_RESULT_OK, ret); ret = channel_list_iterate(chan_list , channel_count, &cnt); TEST_ASSERT_EQUAL(LAGOPUS_RESULT_OK, ret); TEST_ASSERT_EQUAL(2, cnt); cnt = 0; ret = channel_mgr_dpid_iterate(dpid, channel_count, &cnt); TEST_ASSERT_EQUAL(LAGOPUS_RESULT_OK, ret); TEST_ASSERT_EQUAL(2, cnt); run = true; size = sizeof(sin); printf("accept in\n"); sock4 = accept(s4, (struct sockaddr *)&sin, &size); TEST_ASSERT_NOT_EQUAL(-1, sock4); sock6 = accept(s6, (struct sockaddr *)&sin, &size); TEST_ASSERT_NOT_EQUAL(-1, sock6); ret = channel_mgr_channel_lookup(bridge_name, addr4, &chan4); TEST_ASSERT_EQUAL(LAGOPUS_RESULT_OK, ret); channel_refs_get(chan4); ret = channel_mgr_channel_delete(bridge_name, addr4); TEST_ASSERT_EQUAL(LAGOPUS_RESULT_OK, ret); channel_refs_put(chan4); ret = channel_mgr_channel_delete(bridge_name, addr4); TEST_ASSERT_EQUAL(LAGOPUS_RESULT_OK, ret); ret = channel_mgr_channel_lookup(bridge_name, addr4, &chan4); TEST_ASSERT_EQUAL(LAGOPUS_RESULT_NOT_FOUND, ret); ret = channel_mgr_channel_delete(bridge_name, addr6); TEST_ASSERT_EQUAL(LAGOPUS_RESULT_OK, ret); ret = channel_mgr_channel_lookup(bridge_name, addr6, &chan6); TEST_ASSERT_EQUAL(LAGOPUS_RESULT_NOT_FOUND, ret); TEST_ASSERT_FALSE(channel_mgr_has_alive_channel_by_dpid(dpid)); ret = channel_mgr_event_upcall(); TEST_ASSERT_EQUAL(LAGOPUS_RESULT_OK, ret); lagopus_ip_address_destroy(addr4); lagopus_ip_address_destroy(addr6); close(sock4); close(sock6); }
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; }