incubator-nuttx/net/socket
wangchen 14202651b2 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>
2023-08-03 03:12:17 -07:00
..
CMakeLists.txt cmake/build: fix build break on cmake 2023-07-25 15:00:10 +02:00
Kconfig net/socket: fix kconfig warning 2023-05-30 16:06:17 +08:00
Make.defs net: Implement shutdown() interface and tcp shutdown 2023-01-31 11:15:01 +08:00
accept.c net/local: Return the unblock handle correctly in local_accept 2023-03-09 09:17:19 +01:00
bind.c net: Make si_bind callback optional 2023-03-06 20:58:27 +02:00
connect.c net:Resolve udp disconnection, status not synchronized error 2023-08-03 03:12:17 -07:00
getpeername.c net: Remove the empty si_getpeername implementation 2023-03-06 20:58:27 +02:00
getsockname.c net: Make si_getsockname callback optional 2023-03-06 20:58:27 +02:00
getsockopt.c socket: divide errno & s_error 2023-02-23 16:40:19 +01:00
listen.c net: Make si_listen callback optional 2023-03-06 20:58:27 +02:00
net_close.c net/inet: move socket flags into socket_conn_s 2022-02-10 15:04:33 -03:00
net_dup2.c net/tcp/monitor: do not migrate the state to close 2022-02-11 18:56:40 +09:00
net_fstat.c net: remove conn-related casts 2023-05-10 19:32:09 -03:00
net_poll.c net: Make si_poll callback optional 2023-03-06 20:58:27 +02:00
net_sendfile.c socket: divide errno & s_error 2023-02-23 16:40:19 +01:00
net_sockif.c net: Remove usrsock specific process from common code as much as possible 2022-11-28 16:32:41 +09:00
net_timeo.c net: Author Gregory Nutt: update licenses to Apache 2021-02-20 00:38:18 -08:00
recv.c net/semantic/parser: fix compile warning found by sparse 2023-05-30 23:00:00 +08:00
recvfrom.c socket/recvfrom: Fix buffer copy direction when using BUILD_KERNEL. 2023-06-22 11:56:01 +08:00
recvmsg.c recvmsg: control msg support multi-attribute return 2023-05-04 12:04:11 +02:00
send.c net/socket: Combine send() with sendto() 2023-05-27 03:24:06 +08:00
sendmsg.c socket: divide errno & s_error 2023-02-23 16:40:19 +01:00
sendto.c socket/send/recv: Copy user parameters to kernel memory (BUILD_KERNEL) 2023-05-27 03:24:06 +08:00
setsockopt.c net: add parameter check in psock_setsockopt 2022-11-29 00:20:09 +08:00
shutdown.c socket: divide errno & s_error 2023-02-23 16:40:19 +01:00
socket.c net: Remove protocol argument from si_setup callback 2023-02-13 22:41:19 +08:00
socket.h socket: divide errno & s_error 2023-02-23 16:40:19 +01:00
socketpair.c socketpair: Add SOCK_NONBLOCK support into type field 2022-10-21 16:57:20 +08:00