nuttx-mirror/net/inet
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 build: add initial cmake build system 2023-07-08 13:50:48 +08:00
inet.h net: Simplify the tcp/udp existence check 2023-05-03 17:40:00 +03:00
inet_globals.c net: extract l3 header build code into new functions 2022-11-29 18:36:15 +08:00
inet_sockif.c net:Resolve udp disconnection, status not synchronized error 2023-08-03 03:12:17 -07:00
inet_txdrain.c net: Author Gregory Nutt: update licenses to Apache 2021-02-20 00:38:18 -08:00
ipv4_build_header.c support ipv4 ToS and ipv6 TrafficClass 2023-01-29 13:43:44 +08:00
ipv4_getpeername.c net: remove conn-related casts 2023-05-10 19:32:09 -03:00
ipv4_getsockname.c net: remove conn-related casts 2023-05-10 19:32:09 -03:00
ipv4_getsockopt.c net: remove conn-related casts 2023-05-10 19:32:09 -03:00
ipv4_setsockopt.c inet/ipv4_setsockopt.c:fix libuv udp option error 2023-08-02 22:54:13 -07:00
ipv6_build_header.c support ipv4 ToS and ipv6 TrafficClass 2023-01-29 13:43:44 +08:00
ipv6_getpeername.c net: remove conn-related casts 2023-05-10 19:32:09 -03:00
ipv6_getsockname.c net: remove conn-related casts 2023-05-10 19:32:09 -03:00
ipv6_getsockopt.c net: remove conn-related casts 2023-05-10 19:32:09 -03:00
ipv6_setsockopt.c ipv6_setsockopt: Merge similarity logic 2023-05-15 16:48:18 +08:00
Kconfig Networking: Move INET socket interface out of net/sockets to its own directory net/inet 2017-08-06 14:48:19 -06:00
Make.defs support ipv4 ToS and ipv6 TrafficClass 2023-01-29 13:43:44 +08:00