net:Resolve udp disconnection, status not synchronized error
libuvtestcase:
TEST_IMPL(udp_connect) {
RETURN_SKIP(
"IBMi PASE's UDP connection can not be disconnected with AF_UNSPEC.");
uv_udp_send_t req;
struct sockaddr_in ext_addr;
struct sockaddr_in tmp_addr;
int r;
int addrlen;
close_cb_called = 0;
cl_send_cb_called = 0;
sv_recv_cb_called = 0;
ASSERT(0 == uv_ip4_addr("[0.0.0.0](http://0.0.0.0/)", TEST_PORT, &lo_addr));
r = uv_udp_init(uv_default_loop(), &server);
ASSERT(r == 0);
r = uv_udp_bind(&server, (const struct sockaddr*) &lo_addr, 0);
ASSERT(r == 0);
r = uv_udp_recv_start(&server, alloc_cb, sv_recv_cb);
ASSERT(r == 0);
r = uv_udp_init(uv_default_loop(), &client);
ASSERT(r == 0);
buf = uv_buf_init("EXIT", 4);
/* connect() to INADDR_ANY fails on Windows wih WSAEADDRNOTAVAIL */
ASSERT_EQ(0, uv_ip4_addr("[0.0.0.0](http://0.0.0.0/)", TEST_PORT, &tmp_addr));
r = uv_udp_connect(&client, (const struct sockaddr*) &tmp_addr);
ASSERT_EQ(r, UV_EADDRNOTAVAIL);
ASSERT_EQ(r, 0);
r = uv_udp_connect(&client, NULL);
ASSERT_EQ(r, 0);
ASSERT(0 == uv_ip4_addr("[8.8.8.8](http://8.8.8.8/)", TEST_PORT, &ext_addr));
ASSERT(0 == uv_ip4_addr("[127.0.0.1](http://127.0.0.1/)", TEST_PORT, &lo_addr));
r = uv_udp_connect(&client, (const struct sockaddr*) &lo_addr);
ASSERT(r == 0);
r = uv_udp_connect(&client, (const struct sockaddr*) &ext_addr);
ASSERT(r == UV_EISCONN);
addrlen = sizeof(tmp_addr);
r = uv_udp_getpeername(&client, (struct sockaddr*) &tmp_addr, &addrlen);
ASSERT(r == 0);
/* To send messages in connected UDP sockets addr must be NULL */
r = uv_udp_try_send(&client, &buf, 1, (const struct sockaddr*) &lo_addr);
ASSERT(r == UV_EISCONN);
r = uv_udp_try_send(&client, &buf, 1, NULL);
ASSERT(r == 4);
r = uv_udp_try_send(&client, &buf, 1, (const struct sockaddr*) &ext_addr);
ASSERT(r == UV_EISCONN);
r = uv_udp_connect(&client, NULL);
ASSERT(r == 0);
r = uv_udp_connect(&client, NULL);
ASSERT(r == UV_ENOTCONN);
addrlen = sizeof(tmp_addr);
r = uv_udp_getpeername(&client, (struct sockaddr*) &tmp_addr, &addrlen);
ASSERT(r == UV_ENOTCONN);
/* To send messages in disconnected UDP sockets addr must be set */
r = uv_udp_try_send(&client, &buf, 1, (const struct sockaddr*) &lo_addr);
ASSERT(r == 4);
r = uv_udp_try_send(&client, &buf, 1, NULL);
ASSERT(r == UV_EDESTADDRREQ);
r = uv_udp_connect(&client, (const struct sockaddr*) &lo_addr);
ASSERT(r == 0);
r = uv_udp_send(&req,
&client,
&buf,
1,
(const struct sockaddr*) &lo_addr,
cl_send_cb);
ASSERT(r == UV_EISCONN);
r = uv_udp_send(&req, &client, &buf, 1, NULL, cl_send_cb);
ASSERT(r == 0);
uv_run(uv_default_loop(), UV_RUN_DEFAULT);
ASSERT(close_cb_called == 2);
ASSERT(sv_recv_cb_called == 4);
ASSERT(cl_send_cb_called == 2);
ASSERT(client.send_queue_size == 0);
ASSERT(server.send_queue_size == 0);
MAKE_VALGRIND_HAPPY();
return 0;
}
Signed-off-by: wangchen <wangchen41@xiaomi.com>