nuttx/net/udp
wangchen 99ee94728a net:add IP_MULTICAST_IF & IPV6_MULTICAST_IF function implementation
refer to https://man7.org/linux/man-pages/man7/ip.7.html
IP_MULTICAST_IF (since Linux 1.2)
              Set the local device for a multicast socket.  The argument
              for setsockopt(2) is an ip_mreqn or (since Linux 3.5)
              ip_mreq structure similar to IP_ADD_MEMBERSHIP, or an
              in_addr structure.  (The kernel determines which structure
              is being passed based on the size passed in optlen.)  For
              getsockopt(2), the argument is an in_addr structure.
refer to https://man7.org/linux/man-pages/man7/ipv6.7.html
IPV6_MULTICAST_IF
              Set the device for outgoing multicast packets on the
              socket.  This is allowed only for SOCK_DGRAM and SOCK_RAW
              socket.  The argument is a pointer to an interface index
              (see netdevice(7)) in an integer.
testcase1:
TEST_IMPL(udp_multicast_interface) {
/* TODO(gengjiawen): Fix test on QEMU. */
  RETURN_SKIP("Test does not currently work in QEMU");

  int r;
  uv_udp_send_t req;
  uv_buf_t buf;
  struct sockaddr_in addr;
  struct sockaddr_in baddr;

  close_cb_called = 0;
  sv_send_cb_called = 0;
  ASSERT(0 == uv_ip4_addr("239.255.0.1", TEST_PORT, &addr));

  r = uv_udp_init(uv_default_loop(), &server);
  ASSERT(r == 0);

  ASSERT(0 == uv_ip4_addr("0.0.0.0", 0, &baddr));
  r = uv_udp_bind(&server, (const struct sockaddr*)&baddr, 0);
  ASSERT(r == 0);

  r = uv_udp_set_multicast_interface(&server, "0.0.0.0");
  ASSERT(r == 0);

  /* server sends "PING" */
  buf = uv_buf_init("PING", 4);
  r = uv_udp_send(&req,
                  &server,
                  &buf,
                  1,
                  (const struct sockaddr*)&addr,
                  sv_send_cb);
  ASSERT(r == 0);

  ASSERT(close_cb_called == 0);
  ASSERT(sv_send_cb_called == 0);

  /* run the loop till all events are processed */
  uv_run(uv_default_loop(), UV_RUN_DEFAULT);

  ASSERT(sv_send_cb_called == 1);
  ASSERT(close_cb_called == 1);

  ASSERT(client.send_queue_size == 0);
  ASSERT(server.send_queue_size == 0);

  MAKE_VALGRIND_HAPPY();
  return 0;
}
testcase2:
TEST_IMPL(udp_multicast_interface6) {
/* TODO(gengjiawen): Fix test on QEMU. */
  RETURN_SKIP("Test does not currently work in QEMU");

  int r;
  uv_udp_send_t req;
  uv_buf_t buf;
  struct sockaddr_in6 addr;
  struct sockaddr_in6 baddr;

  if (!can_ipv6())
    RETURN_SKIP("IPv6 not supported");

  close_cb_called = 0;
  sv_send_cb_called = 0;

  ASSERT(0 == uv_ip6_addr("::1", TEST_PORT, &addr));

  r = uv_udp_init(uv_default_loop(), &server);
  ASSERT(r == 0);

  ASSERT(0 == uv_ip6_addr("::", 0, &baddr));
  r = uv_udp_bind(&server, (const struct sockaddr*)&baddr, 0);
  ASSERT(r == 0);

  r = uv_udp_set_multicast_interface(&server, "::1%lo0");
  r = uv_udp_set_multicast_interface(&server, NULL);
  ASSERT(r == 0);

  /* server sends "PING" */
  buf = uv_buf_init("PING", 4);
  r = uv_udp_send(&req,
                  &server,
                  &buf,
                  1,
                  (const struct sockaddr*)&addr,
                  sv_send_cb);
  ASSERT(r == 0);

  ASSERT(close_cb_called == 0);
  ASSERT(sv_send_cb_called == 0);

  /* run the loop till all events are processed */
  uv_run(uv_default_loop(), UV_RUN_DEFAULT);

  ASSERT(sv_send_cb_called == 1);
  ASSERT(close_cb_called == 1);

  MAKE_VALGRIND_HAPPY();
  return 0;
}

Signed-off-by: wangchen <wangchen41@xiaomi.com>
2023-08-25 17:16:50 +08:00
..
CMakeLists.txt build: add initial cmake build system 2023-07-08 13:50:48 +08:00
Kconfig Kconfig: Simplify the conditional default statement 2023-07-16 14:39:20 -03:00
Make.defs net/inet: add support of FIONREAD 2021-07-05 06:20:52 -05:00
udp_callback.c net/udp: Fix hybrid dual-stack IPv6/IPv4 socket 2023-08-22 09:09:21 +08:00
udp_close.c net: remove conn-related casts 2023-05-10 19:32:09 -03:00
udp_conn.c net:Add check for address binding 2023-08-03 03:16:31 -07:00
udp_devpoll.c net/devif_poll: optimize device buffer alloc in txpoll 2023-01-13 16:41:10 +08:00
udp_finddev.c net:add IP_MULTICAST_IF & IPV6_MULTICAST_IF function implementation 2023-08-25 17:16:50 +08:00
udp_input.c net: move device buffer define to common header 2022-10-28 00:32:16 -04:00
udp_ioctl.c net: Add FIOC_FILEPATH ioctl support for ICMP(v6)/RPMsg/Usrsock sockets 2023-08-15 15:00:59 +08:00
udp_ipselect.c net: move device buffer define to common header 2022-10-28 00:32:16 -04:00
udp_netpoll.c net/udp: Change conn->readahead to I/O buffer chain 2023-07-18 10:51:45 +08:00
udp_notifier.c net/udp: Change conn->readahead to I/O buffer chain 2023-07-18 10:51:45 +08:00
udp_recvfrom.c net/udp: Fix hybrid dual-stack IPv6/IPv4 socket 2023-08-22 09:09:21 +08:00
udp_send.c mm/iob: Add support for increasing length in iob_update_pktlen 2023-08-22 16:34:21 +09:00
udp_sendto_buffered.c mm/iob: Add support for increasing length in iob_update_pktlen 2023-08-22 16:34:21 +09:00
udp_sendto_unbuffered.c net/udp: Fix hybrid dual-stack IPv6/IPv4 socket 2023-08-22 09:09:21 +08:00
udp_setsockopt.c net/sockopt: move BINDTODEVICE to socket level 2022-08-25 17:56:52 +08:00
udp_txdrain.c net: remove conn-related casts 2023-05-10 19:32:09 -03:00
udp_wrbuffer_dump.c net: Author Gregory Nutt: update licenses to Apache 2021-02-20 00:38:18 -08:00
udp_wrbuffer.c net/xx/wrbuffer: Do not use SEM_INITIALIZER for buffers 2023-08-25 00:02:07 +08:00
udp.h net:add IP_MULTICAST_IF & IPV6_MULTICAST_IF function implementation 2023-08-25 17:16:50 +08:00