From 24a118cdaabbf1a7a08f2ceac9f7322f170876c7 Mon Sep 17 00:00:00 2001 From: smithAchang Date: Sun, 19 Mar 2023 09:17:56 +0800 Subject: [PATCH 01/39] can compile ok --- ACE/ace/RAW_Socket.cpp | 293 +++++++++++++++++++++++++++++++++++++++++ ACE/ace/RAW_Socket.h | 130 ++++++++++++++++++ ACE/ace/ace.mpc | 1 + 3 files changed, 424 insertions(+) create mode 100644 ACE/ace/RAW_Socket.cpp create mode 100644 ACE/ace/RAW_Socket.h diff --git a/ACE/ace/RAW_Socket.cpp b/ACE/ace/RAW_Socket.cpp new file mode 100644 index 0000000000000..0832f141882d1 --- /dev/null +++ b/ACE/ace/RAW_Socket.cpp @@ -0,0 +1,293 @@ +#include "ace/RAW_Socket.h" + + +#include "ace/ACE.h" +#include "ace/Sock_Connect.h" +#include "ace/Log_Category.h" +#include "ace/OS_NS_netdb.h" +#include "ace/OS_NS_sys_socket.h" +#if defined (ACE_HAS_ALLOC_HOOKS) +# include "ace/Malloc_Base.h" +#endif /* ACE_HAS_ALLOC_HOOKS */ + +ACE_BEGIN_VERSIONED_NAMESPACE_DECL + +ACE_ALLOC_HOOK_DEFINE (ACE_RAW_SOCKET) + + +void +ACE_RAW_SOCKET::dump () const +{ + ACE_TRACE ("ACE_RAW_SOCKET::dump"); +} + +/// @brief default constructor +ACE_RAW_SOCKET::ACE_RAW_SOCKET () +{ + ACE_TRACE ("ACE_RAW_SOCKET::ACE_RAW_SOCKET"); +} + +/// @brief constructor with parameters +/// @param local the local address supporting ACE_Addr::sap_any +/// @param protocol filter the protocol based on IP network layer +/// @param reuse_addr Maybe no any meaning for raw socket +ACE_RAW_SOCKET::ACE_RAW_SOCKET (ACE_Addr const & local, + int protocol, + int reuse_addr) +{ + ACE_TRACE ("ACE_RAW_SOCKET::ACE_RAW_SOCKET"); + if (this->open (local, protocol, reuse_addr) == -1) + { + ACELIB_ERROR ((LM_ERROR, + ACE_TEXT ("%p\n"), + ACE_TEXT ("ACE_RAW_SOCKET"))); + } + +} + +ssize_t +ACE_RAW_SOCKET::recv (void *buf, + size_t n, + ACE_Addr &addr, + int flags, + const ACE_Time_Value *timeout) const +{ + + if (this->get_handle () == ACE_INVALID_HANDLE) + { + return -1; + } + + if(timeout && ACE::handle_read_ready (this->get_handle (), timeout) != 1) + { + return -1; + } + + sockaddr *saddr = (sockaddr *) addr.get_addr (); + int addr_len = addr.get_size (); + + ssize_t const status = ACE_OS::recvfrom (this->get_handle (), + (char *) buf, + n, + flags, + (sockaddr *) saddr, + &addr_len); + addr.set_size (addr_len); + addr.set_type (saddr->sa_family); + return status; +} + +ssize_t +ACE_RAW_SOCKET::send (const void *buf, + size_t n, + const ACE_Addr &addr, + int flags, + const ACE_Time_Value *timeout) const +{ + // Check the status of the current socket. + if (this->get_handle () == ACE_INVALID_HANDLE) + { + return -1; + } + + if(timeout && ACE::handle_write_ready (this->get_handle (), timeout) != 1) + { + return -1; + } + + + sockaddr *saddr = (sockaddr *) addr.get_addr (); + int len = addr.get_size (); + return ACE_OS::sendto (this->get_handle (), + (const char *) buf, + n, + flags, + (struct sockaddr *) saddr, + len); + +} + + +#if defined (ACE_HAS_MSG) + +// Send an iovec of size N to ADDR as a datagram (connectionless +// version). + +ssize_t +ACE_RAW_SOCKET::send (const iovec iov[], + int n, + const ACE_Addr &addr, + int flags) const +{ + ACE_TRACE ("ACE_RAW_SOCKET::send"); + + if (this->get_handle () == ACE_INVALID_HANDLE) + { + return -1; + } + + msghdr send_msg; + + send_msg.msg_iov = (iovec *) iov; + send_msg.msg_iovlen = n; +#if defined (ACE_HAS_SOCKADDR_MSG_NAME) + send_msg.msg_name = (struct sockaddr *) addr.get_addr (); +#else + send_msg.msg_name = (char *) addr.get_addr (); +#endif /* ACE_HAS_SOCKADDR_MSG_NAME */ + send_msg.msg_namelen = addr.get_size (); + +#if defined (ACE_HAS_4_4BSD_SENDMSG_RECVMSG) + send_msg.msg_control = 0; + send_msg.msg_controllen = 0; + send_msg.msg_flags = 0; +#elif !defined ACE_LACKS_SENDMSG + send_msg.msg_accrights = 0; + send_msg.msg_accrightslen = 0; +#endif /* ACE_HAS_4_4BSD_SENDMSG_RECVMSG */ + +#ifdef ACE_WIN32 + send_msg.msg_control = 0; + send_msg.msg_controllen = 0; +#endif + + return ACE_OS::sendmsg (this->get_handle (), + &send_msg, + flags); +} + +// Recv an iovec of size N to ADDR as a datagram (connectionless +// version). +ssize_t +ACE_RAW_SOCKET::recv (iovec iov[], + int n, + ACE_Addr &addr, + int flags, + ACE_INET_Addr *to_addr) const +{ + ACE_TRACE ("ACE_RAW_SOCKET::recv"); + + if (this->get_handle () == ACE_INVALID_HANDLE) + { + return -1; + } + + msghdr recv_msg; + +#if defined (ACE_HAS_4_4BSD_SENDMSG_RECVMSG) || defined ACE_WIN32 +#define ACE_USE_MSG_CONTROL + union control_buffer { + cmsghdr control_msg_header; +#if defined (IP_RECVDSTADDR) + u_char padding[ACE_CMSG_SPACE (sizeof (in_addr))]; +#elif defined (IP_PKTINFO) + u_char padding[ACE_CMSG_SPACE (sizeof (in_pktinfo))]; +#endif +#if defined (ACE_HAS_IPV6) + u_char padding6[ACE_CMSG_SPACE (sizeof (in6_pktinfo))]; +#endif + } cbuf; +#else + ACE_UNUSED_ARG (to_addr); +#endif + + recv_msg.msg_iov = (iovec *) iov; + recv_msg.msg_iovlen = n; +#if defined (ACE_HAS_SOCKADDR_MSG_NAME) + recv_msg.msg_name = (struct sockaddr *) addr.get_addr (); +#else + recv_msg.msg_name = (char *) addr.get_addr (); +#endif /* ACE_HAS_SOCKADDR_MSG_NAME */ + recv_msg.msg_namelen = addr.get_size (); + +#ifdef ACE_USE_MSG_CONTROL + recv_msg.msg_control = to_addr ? &cbuf : 0; + recv_msg.msg_controllen = to_addr ? sizeof (cbuf) : 0; +#elif !defined ACE_LACKS_SENDMSG + recv_msg.msg_accrights = 0; + recv_msg.msg_accrightslen = 0; +#endif + + ssize_t status = ACE_OS::recvmsg (this->get_handle (), + &recv_msg, + flags); + addr.set_size (recv_msg.msg_namelen); + addr.set_type (((sockaddr_in *) addr.get_addr())->sin_family); + +#ifdef ACE_USE_MSG_CONTROL + if (to_addr) { + this->get_local_addr (*to_addr); + if (to_addr->get_type() == AF_INET) { +#if defined (IP_RECVDSTADDR) || defined (IP_PKTINFO) + for (cmsghdr *ptr = ACE_CMSG_FIRSTHDR (&recv_msg); ptr; ptr = ACE_CMSG_NXTHDR (&recv_msg, ptr)) { +#if defined (IP_RECVDSTADDR) + if (ptr->cmsg_level == IPPROTO_IP && ptr->cmsg_type == IP_RECVDSTADDR) { + to_addr->set_address ((const char *) (ACE_CMSG_DATA (ptr)), + sizeof (struct in_addr), + 0); + break; + } +#else + if (ptr->cmsg_level == IPPROTO_IP && ptr->cmsg_type == IP_PKTINFO) { + to_addr->set_address ((const char *) &(((in_pktinfo *) (ACE_CMSG_DATA (ptr)))->ipi_addr), + sizeof (struct in_addr), + 0); + break; + } +#endif + } +#endif + } +#if defined (ACE_HAS_IPV6) && defined (IPV6_PKTINFO) + else if (to_addr->get_type() == AF_INET6) { + for (cmsghdr *ptr = ACE_CMSG_FIRSTHDR (&recv_msg); ptr; ptr = ACE_CMSG_NXTHDR (&recv_msg, ptr)) { + if (ptr->cmsg_level == IPPROTO_IPV6 && ptr->cmsg_type == IPV6_PKTINFO) { + to_addr->set_address ((const char *) &(((in6_pktinfo *)(ACE_CMSG_DATA (ptr)))->ipi6_addr), + sizeof (struct in6_addr), + 0); + + break; + } + } + } +#endif + } +#endif + + return status; +} + +#endif + +int +ACE_RAW_SOCKET::open (ACE_Addr const & local, int protocol, int reuse_addr) +{ + ACE_TRACE ("ACE_RAW_SOCKET::open"); + + if (this->get_handle () != ACE_INVALID_HANDLE) + { + return -1; + } + + int protocol_family = PF_UNSPEC; + + if (local != ACE_Addr::sap_any) + { + protocol_family = local.get_type (); + } + + if (protocol_family == PF_UNSPEC) + { + #if defined (ACE_HAS_IPV6) + protocol_family = ACE::ipv6_enabled () ? PF_INET6 : PF_INET; + #else + protocol_family = PF_INET; + #endif /* ACE_HAS_IPV6 */ + } + + return ACE_SOCK::open (SOCK_RAW, protocol_family, protocol, reuse_addr) ; +} + + +ACE_END_VERSIONED_NAMESPACE_DECL + diff --git a/ACE/ace/RAW_Socket.h b/ACE/ace/RAW_Socket.h new file mode 100644 index 0000000000000..1d6bd7e993f31 --- /dev/null +++ b/ACE/ace/RAW_Socket.h @@ -0,0 +1,130 @@ +// -*- C++ -*- + +//============================================================================= +/** + * @file RAW_Socket.h + * + * @author Smith.Achang + */ +//============================================================================= + +#ifndef ACE_RAW_SOCKET_H +#define ACE_RAW_SOCKET_H + +#include /**/ "ace/pre.h" + +#include /**/ "ace/ACE_export.h" + +#if !defined (ACE_LACKS_PRAGMA_ONCE) +# pragma once +#endif /* ACE_LACKS_PRAGMA_ONCE */ + + +#include "ace/SOCK.h" +#include "ace/Copy_Disabled.h" + +#include "ace/Time_Value.h" +#include "ace/os_include/netinet/os_in.h" +#include "ace/Addr.h" +#include "ace/INET_Addr.h" + +ACE_BEGIN_VERSIONED_NAMESPACE_DECL + +/** + * @class ACE_RAW_Socket + * + * @brief An RAW socket implemention class that forms the basis for some usage of user-space network protocol stack + * e.g. using RAW + protocol[UDP] socket to avoid creating multiple UDP dgram sockets with only bound port variance + */ +class ACE_Export ACE_RAW_SOCKET : public ACE_SOCK, private ACE_Copy_Disabled +{ + public: + /// Default constructor. + ACE_RAW_SOCKET (); + + /// Constructor that bind a local address and fiter UDP protocol. + ACE_RAW_SOCKET (ACE_Addr const & local, + int protocol = IPPROTO_UDP, + int reuse_addr = 1); + + /** + * @name Data transfer routines. + * + * Data transfer routines. + */ + //@{ + + /** + * Wait up to @a timeout amount of time to send a datagram to + * @a buf. The ACE_Time_Value indicates how long to blocking + * trying to receive. If @a timeout == 0, the caller will block + * until action is possible, else will wait until the relative time + * specified in *@a timeout elapses). If times out a -1 is + * returned with @c errno == ETIME. If it succeeds the number of + * bytes sent is returned. + */ + ssize_t send (const void *buf, + size_t n, + const ACE_Addr &addr, + int flags = 0, + const ACE_Time_Value *timeout = NULL) const; + + + /** + * Wait up to @a timeout amount of time to receive a datagram into + * @a buf. The ACE_Time_Value indicates how long to blocking + * trying to receive. If @a timeout == 0, the caller will block + * until action is possible, else will wait until the relative time + * specified in *@a timeout elapses). If times out a -1 is + * returned with @c errno == ETIME. If it succeeds the number of + * bytes received is returned. + */ + ssize_t recv (void *buf, + size_t n, + ACE_Addr &addr, + int flags = 0, + const ACE_Time_Value *timeout = NULL) const; + +#if defined (ACE_HAS_MSG) + + /// Send an of size @a n to the datagram socket (uses + /// ). + ssize_t send (const iovec iov[], + int n, + const ACE_Addr &addr, + int flags = 0) const; + + /// Recv an of size @a n to the datagram socket (uses + /// ). The IP destination address will be placed in @a + /// *to_addr if it is not null and set_option has been called with + /// 1) level IPPROTO_IP, option ACE_RECVPKTINFO, and value 1 for + /// IPV4 addresses or 2) IPPROTO_IPV6, option ACE_RECVPKTINFO6, and + /// value 1 for IPV6 addresses. + ssize_t recv (iovec iov[], + int n, + ACE_Addr &addr, + int flags = 0, + ACE_INET_Addr *to_addr = 0) const; + + #endif + + //@} + + /// Wrapper around the BSD-style @c socket system call (no QoS). + int open (ACE_Addr const & local, + int protocol = IPPROTO_UDP, + int reuse_addr = 1); + + /// Dump the state of object. + void dump () const; + + /// Declare the dynamic allocation hooks. + ACE_ALLOC_HOOK_DECLARE; + +}; + +ACE_END_VERSIONED_NAMESPACE_DECL + +#include /**/ "ace/post.h" + +#endif /* ACE_RAW_SOCKET_H */ diff --git a/ACE/ace/ace.mpc b/ACE/ace/ace.mpc index 97e8b5fec648e..d536e4b322e3f 100644 --- a/ACE/ace/ace.mpc +++ b/ACE/ace/ace.mpc @@ -197,6 +197,7 @@ project(ACE) : ace_output, acedefaults, install, other, codecs, token, svcconf, Process_Mutex.cpp Process_Semaphore.cpp Profile_Timer.cpp + RAW_Socket.cpp Reactor.cpp Reactor_Impl.cpp Reactor_Notification_Strategy.cpp From 925d8644ab66300abd6b8aedecbaafd3439403f9 Mon Sep 17 00:00:00 2001 From: smithAchang Date: Sun, 19 Mar 2023 12:34:36 +0800 Subject: [PATCH 02/39] add doc --- ACE/ace/RAW_Socket.cpp | 15 +++++---------- ACE/ace/RAW_Socket.h | 43 +++++++++++++++++++++++++++++------------- 2 files changed, 35 insertions(+), 23 deletions(-) diff --git a/ACE/ace/RAW_Socket.cpp b/ACE/ace/RAW_Socket.cpp index 0832f141882d1..b5766b1f993ba 100644 --- a/ACE/ace/RAW_Socket.cpp +++ b/ACE/ace/RAW_Socket.cpp @@ -31,7 +31,7 @@ ACE_RAW_SOCKET::ACE_RAW_SOCKET () /// @param local the local address supporting ACE_Addr::sap_any /// @param protocol filter the protocol based on IP network layer /// @param reuse_addr Maybe no any meaning for raw socket -ACE_RAW_SOCKET::ACE_RAW_SOCKET (ACE_Addr const & local, +ACE_RAW_SOCKET::ACE_RAW_SOCKET (ACE_INET_Addr const & local, int protocol, int reuse_addr) { @@ -116,7 +116,7 @@ ACE_RAW_SOCKET::send (const void *buf, ssize_t ACE_RAW_SOCKET::send (const iovec iov[], int n, - const ACE_Addr &addr, + const ACE_INET_Addr &addr, int flags) const { ACE_TRACE ("ACE_RAW_SOCKET::send"); @@ -161,7 +161,7 @@ ACE_RAW_SOCKET::send (const iovec iov[], ssize_t ACE_RAW_SOCKET::recv (iovec iov[], int n, - ACE_Addr &addr, + ACE_INET_Addr &addr, int flags, ACE_INET_Addr *to_addr) const { @@ -260,7 +260,7 @@ ACE_RAW_SOCKET::recv (iovec iov[], #endif int -ACE_RAW_SOCKET::open (ACE_Addr const & local, int protocol, int reuse_addr) +ACE_RAW_SOCKET::open (ACE_INET_Addr const & local, int protocol, int reuse_addr) { ACE_TRACE ("ACE_RAW_SOCKET::open"); @@ -269,13 +269,8 @@ ACE_RAW_SOCKET::open (ACE_Addr const & local, int protocol, int reuse_addr) return -1; } - int protocol_family = PF_UNSPEC; + int protocol_family = local.get_type (); - if (local != ACE_Addr::sap_any) - { - protocol_family = local.get_type (); - } - if (protocol_family == PF_UNSPEC) { #if defined (ACE_HAS_IPV6) diff --git a/ACE/ace/RAW_Socket.h b/ACE/ace/RAW_Socket.h index 1d6bd7e993f31..026710c65c275 100644 --- a/ACE/ace/RAW_Socket.h +++ b/ACE/ace/RAW_Socket.h @@ -25,7 +25,6 @@ #include "ace/Time_Value.h" #include "ace/os_include/netinet/os_in.h" -#include "ace/Addr.h" #include "ace/INET_Addr.h" ACE_BEGIN_VERSIONED_NAMESPACE_DECL @@ -33,17 +32,27 @@ ACE_BEGIN_VERSIONED_NAMESPACE_DECL /** * @class ACE_RAW_Socket * - * @brief An RAW socket implemention class that forms the basis for some usage of user-space network protocol stack - * e.g. using RAW + protocol[UDP] socket to avoid creating multiple UDP dgram sockets with only bound port variance + * @brief An RAW Socket implemention class. + * + * An RAW Socket can be used for some user-space network protocol stack. + * - setting the protocol para to be IPPROTO_UDP or other e.g. IPPROTO_SCTP will filter all relative packages with the destination is its bound address. + * - setting the protocol para to be IPPROTO_RAW will make it as a send only socket for any customized dgram formed from IP header. + * Notice the source address if provided can be different from its bound address. + * + * @note If you really want to receive all IP packets, use a packet(7) socket with the ETH_P_IP protocol. + * For "Single Responsibility Principle" the behavior has notable difference, so the feature is not implemented here. */ class ACE_Export ACE_RAW_SOCKET : public ACE_SOCK, private ACE_Copy_Disabled { - public: +public: /// Default constructor. ACE_RAW_SOCKET (); - + /// Constructor that bind a local address and fiter UDP protocol. - ACE_RAW_SOCKET (ACE_Addr const & local, + /// @param local local IP address to bind + /// @param protocol IPPROTO_UDP as default value. often used as a user-space UDP stack + /// @param reuse_addr Maybe meaningless for RAW Socket + ACE_RAW_SOCKET (ACE_INET_Addr const & local, int protocol = IPPROTO_UDP, int reuse_addr = 1); @@ -88,21 +97,21 @@ class ACE_Export ACE_RAW_SOCKET : public ACE_SOCK, private ACE_Copy_Disabled #if defined (ACE_HAS_MSG) /// Send an of size @a n to the datagram socket (uses - /// ). + /// ) with Scatter-Gather I/O. ssize_t send (const iovec iov[], int n, - const ACE_Addr &addr, + const ACE_INET_Addr &addr, int flags = 0) const; /// Recv an of size @a n to the datagram socket (uses - /// ). The IP destination address will be placed in @a + /// ) with Scatter-Gather I/O. The IP destination address will be placed in @a /// *to_addr if it is not null and set_option has been called with /// 1) level IPPROTO_IP, option ACE_RECVPKTINFO, and value 1 for /// IPV4 addresses or 2) IPPROTO_IPV6, option ACE_RECVPKTINFO6, and /// value 1 for IPV6 addresses. ssize_t recv (iovec iov[], int n, - ACE_Addr &addr, + ACE_INET_Addr &addr, int flags = 0, ACE_INET_Addr *to_addr = 0) const; @@ -110,9 +119,17 @@ class ACE_Export ACE_RAW_SOCKET : public ACE_SOCK, private ACE_Copy_Disabled //@} - /// Wrapper around the BSD-style @c socket system call (no QoS). - int open (ACE_Addr const & local, - int protocol = IPPROTO_UDP, + /** + * Wrapper around the BSD-style @c socket system call (no QoS). + * @param local local IP address to bind + * @param protocol filter the protocol based on IP network layer. Using IPPROTO_UDP as default value for often used as a user-space UDP stack + * @param reuse_addr Maybe meaningless for RAW Socket + * + * @attention can be re-opened after closed + */ + + int open (ACE_INET_Addr const & local, + int protocol = IPPROTO_UDP, int reuse_addr = 1); /// Dump the state of object. From e0d1804c3d244f2d9ef443d29110bf282510571a Mon Sep 17 00:00:00 2001 From: smithAchang Date: Sun, 19 Mar 2023 21:25:01 +0800 Subject: [PATCH 03/39] add test project --- ACE/ace/RAW_Socket.cpp | 190 ++++------------------------------ ACE/ace/RAW_Socket.h | 49 ++++----- ACE/ace/RAW_Socket.inl | 22 ++++ ACE/tests/RAW_Socket_Test.cpp | 94 +++++++++++++++++ ACE/tests/tests.mpc | 7 ++ 5 files changed, 165 insertions(+), 197 deletions(-) create mode 100644 ACE/ace/RAW_Socket.inl create mode 100644 ACE/tests/RAW_Socket_Test.cpp diff --git a/ACE/ace/RAW_Socket.cpp b/ACE/ace/RAW_Socket.cpp index b5766b1f993ba..d2f80a68791f1 100644 --- a/ACE/ace/RAW_Socket.cpp +++ b/ACE/ace/RAW_Socket.cpp @@ -1,11 +1,11 @@ #include "ace/RAW_Socket.h" +#if !defined (__ACE_INLINE__) +#include "ace/RAW_Socket.inl" +#endif /* __ACE_INLINE__ */ #include "ace/ACE.h" -#include "ace/Sock_Connect.h" #include "ace/Log_Category.h" -#include "ace/OS_NS_netdb.h" -#include "ace/OS_NS_sys_socket.h" #if defined (ACE_HAS_ALLOC_HOOKS) # include "ace/Malloc_Base.h" #endif /* ACE_HAS_ALLOC_HOOKS */ @@ -22,18 +22,15 @@ ACE_RAW_SOCKET::dump () const } /// @brief default constructor -ACE_RAW_SOCKET::ACE_RAW_SOCKET () +ACE_RAW_SOCKET::ACE_RAW_SOCKET () : protocol_(IPPROTO_UDP) { ACE_TRACE ("ACE_RAW_SOCKET::ACE_RAW_SOCKET"); } /// @brief constructor with parameters -/// @param local the local address supporting ACE_Addr::sap_any -/// @param protocol filter the protocol based on IP network layer -/// @param reuse_addr Maybe no any meaning for raw socket ACE_RAW_SOCKET::ACE_RAW_SOCKET (ACE_INET_Addr const & local, int protocol, - int reuse_addr) + int reuse_addr) : protocol_(protocol) { ACE_TRACE ("ACE_RAW_SOCKET::ACE_RAW_SOCKET"); if (this->open (local, protocol, reuse_addr) == -1) @@ -41,14 +38,13 @@ ACE_RAW_SOCKET::ACE_RAW_SOCKET (ACE_INET_Addr const & local, ACELIB_ERROR ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("ACE_RAW_SOCKET"))); - } - + } } ssize_t ACE_RAW_SOCKET::recv (void *buf, size_t n, - ACE_Addr &addr, + ACE_INET_Addr &addr, int flags, const ACE_Time_Value *timeout) const { @@ -80,7 +76,7 @@ ACE_RAW_SOCKET::recv (void *buf, ssize_t ACE_RAW_SOCKET::send (const void *buf, size_t n, - const ACE_Addr &addr, + const ACE_INET_Addr &addr, int flags, const ACE_Time_Value *timeout) const { @@ -89,6 +85,11 @@ ACE_RAW_SOCKET::send (const void *buf, { return -1; } + + if(is_send_only()) + { + return -1; + } if(timeout && ACE::handle_write_ready (this->get_handle (), timeout) != 1) { @@ -107,158 +108,6 @@ ACE_RAW_SOCKET::send (const void *buf, } - -#if defined (ACE_HAS_MSG) - -// Send an iovec of size N to ADDR as a datagram (connectionless -// version). - -ssize_t -ACE_RAW_SOCKET::send (const iovec iov[], - int n, - const ACE_INET_Addr &addr, - int flags) const -{ - ACE_TRACE ("ACE_RAW_SOCKET::send"); - - if (this->get_handle () == ACE_INVALID_HANDLE) - { - return -1; - } - - msghdr send_msg; - - send_msg.msg_iov = (iovec *) iov; - send_msg.msg_iovlen = n; -#if defined (ACE_HAS_SOCKADDR_MSG_NAME) - send_msg.msg_name = (struct sockaddr *) addr.get_addr (); -#else - send_msg.msg_name = (char *) addr.get_addr (); -#endif /* ACE_HAS_SOCKADDR_MSG_NAME */ - send_msg.msg_namelen = addr.get_size (); - -#if defined (ACE_HAS_4_4BSD_SENDMSG_RECVMSG) - send_msg.msg_control = 0; - send_msg.msg_controllen = 0; - send_msg.msg_flags = 0; -#elif !defined ACE_LACKS_SENDMSG - send_msg.msg_accrights = 0; - send_msg.msg_accrightslen = 0; -#endif /* ACE_HAS_4_4BSD_SENDMSG_RECVMSG */ - -#ifdef ACE_WIN32 - send_msg.msg_control = 0; - send_msg.msg_controllen = 0; -#endif - - return ACE_OS::sendmsg (this->get_handle (), - &send_msg, - flags); -} - -// Recv an iovec of size N to ADDR as a datagram (connectionless -// version). -ssize_t -ACE_RAW_SOCKET::recv (iovec iov[], - int n, - ACE_INET_Addr &addr, - int flags, - ACE_INET_Addr *to_addr) const -{ - ACE_TRACE ("ACE_RAW_SOCKET::recv"); - - if (this->get_handle () == ACE_INVALID_HANDLE) - { - return -1; - } - - msghdr recv_msg; - -#if defined (ACE_HAS_4_4BSD_SENDMSG_RECVMSG) || defined ACE_WIN32 -#define ACE_USE_MSG_CONTROL - union control_buffer { - cmsghdr control_msg_header; -#if defined (IP_RECVDSTADDR) - u_char padding[ACE_CMSG_SPACE (sizeof (in_addr))]; -#elif defined (IP_PKTINFO) - u_char padding[ACE_CMSG_SPACE (sizeof (in_pktinfo))]; -#endif -#if defined (ACE_HAS_IPV6) - u_char padding6[ACE_CMSG_SPACE (sizeof (in6_pktinfo))]; -#endif - } cbuf; -#else - ACE_UNUSED_ARG (to_addr); -#endif - - recv_msg.msg_iov = (iovec *) iov; - recv_msg.msg_iovlen = n; -#if defined (ACE_HAS_SOCKADDR_MSG_NAME) - recv_msg.msg_name = (struct sockaddr *) addr.get_addr (); -#else - recv_msg.msg_name = (char *) addr.get_addr (); -#endif /* ACE_HAS_SOCKADDR_MSG_NAME */ - recv_msg.msg_namelen = addr.get_size (); - -#ifdef ACE_USE_MSG_CONTROL - recv_msg.msg_control = to_addr ? &cbuf : 0; - recv_msg.msg_controllen = to_addr ? sizeof (cbuf) : 0; -#elif !defined ACE_LACKS_SENDMSG - recv_msg.msg_accrights = 0; - recv_msg.msg_accrightslen = 0; -#endif - - ssize_t status = ACE_OS::recvmsg (this->get_handle (), - &recv_msg, - flags); - addr.set_size (recv_msg.msg_namelen); - addr.set_type (((sockaddr_in *) addr.get_addr())->sin_family); - -#ifdef ACE_USE_MSG_CONTROL - if (to_addr) { - this->get_local_addr (*to_addr); - if (to_addr->get_type() == AF_INET) { -#if defined (IP_RECVDSTADDR) || defined (IP_PKTINFO) - for (cmsghdr *ptr = ACE_CMSG_FIRSTHDR (&recv_msg); ptr; ptr = ACE_CMSG_NXTHDR (&recv_msg, ptr)) { -#if defined (IP_RECVDSTADDR) - if (ptr->cmsg_level == IPPROTO_IP && ptr->cmsg_type == IP_RECVDSTADDR) { - to_addr->set_address ((const char *) (ACE_CMSG_DATA (ptr)), - sizeof (struct in_addr), - 0); - break; - } -#else - if (ptr->cmsg_level == IPPROTO_IP && ptr->cmsg_type == IP_PKTINFO) { - to_addr->set_address ((const char *) &(((in_pktinfo *) (ACE_CMSG_DATA (ptr)))->ipi_addr), - sizeof (struct in_addr), - 0); - break; - } -#endif - } -#endif - } -#if defined (ACE_HAS_IPV6) && defined (IPV6_PKTINFO) - else if (to_addr->get_type() == AF_INET6) { - for (cmsghdr *ptr = ACE_CMSG_FIRSTHDR (&recv_msg); ptr; ptr = ACE_CMSG_NXTHDR (&recv_msg, ptr)) { - if (ptr->cmsg_level == IPPROTO_IPV6 && ptr->cmsg_type == IPV6_PKTINFO) { - to_addr->set_address ((const char *) &(((in6_pktinfo *)(ACE_CMSG_DATA (ptr)))->ipi6_addr), - sizeof (struct in6_addr), - 0); - - break; - } - } - } -#endif - } -#endif - - return status; -} - -#endif - int ACE_RAW_SOCKET::open (ACE_INET_Addr const & local, int protocol, int reuse_addr) { @@ -271,16 +120,15 @@ ACE_RAW_SOCKET::open (ACE_INET_Addr const & local, int protocol, int reuse_addr) int protocol_family = local.get_type (); - if (protocol_family == PF_UNSPEC) + + if(ACE_SOCK::open (SOCK_RAW, protocol_family, protocol, reuse_addr) == -1) { - #if defined (ACE_HAS_IPV6) - protocol_family = ACE::ipv6_enabled () ? PF_INET6 : PF_INET; - #else - protocol_family = PF_INET; - #endif /* ACE_HAS_IPV6 */ + return -1; } - return ACE_SOCK::open (SOCK_RAW, protocol_family, protocol, reuse_addr) ; + this->protocol_ = protocol; + + return 0; } diff --git a/ACE/ace/RAW_Socket.h b/ACE/ace/RAW_Socket.h index 026710c65c275..fb8cb8333413f 100644 --- a/ACE/ace/RAW_Socket.h +++ b/ACE/ace/RAW_Socket.h @@ -4,7 +4,7 @@ /** * @file RAW_Socket.h * - * @author Smith.Achang + * @author Smith.Achang */ //============================================================================= @@ -35,9 +35,15 @@ ACE_BEGIN_VERSIONED_NAMESPACE_DECL * @brief An RAW Socket implemention class. * * An RAW Socket can be used for some user-space network protocol stack. - * - setting the protocol para to be IPPROTO_UDP or other e.g. IPPROTO_SCTP will filter all relative packages with the destination is its bound address. + * - setting the protocol para to be IPPROTO_UDP will filter all UPD protocol packages with the destination is its bound address. + * It can reduce the total num of needed socket with only port difference + * + * - setting the protocol para to be IPPROTO_SCTP will filter all SCTP protocol packages with the destination is its bound address. + * It can form the basis of a user-space SCTP protocol stack in more general platforms + * * - setting the protocol para to be IPPROTO_RAW will make it as a send only socket for any customized dgram formed from IP header. * Notice the source address if provided can be different from its bound address. + * Notice the RAW socket does not support fragment function when passed package exceeds the MTU, so it need a upper layer framment before called * * @note If you really want to receive all IP packets, use a packet(7) socket with the ETH_P_IP protocol. * For "Single Responsibility Principle" the behavior has notable difference, so the feature is not implemented here. @@ -74,7 +80,7 @@ class ACE_Export ACE_RAW_SOCKET : public ACE_SOCK, private ACE_Copy_Disabled */ ssize_t send (const void *buf, size_t n, - const ACE_Addr &addr, + const ACE_INET_Addr &addr, int flags = 0, const ACE_Time_Value *timeout = NULL) const; @@ -90,32 +96,10 @@ class ACE_Export ACE_RAW_SOCKET : public ACE_SOCK, private ACE_Copy_Disabled */ ssize_t recv (void *buf, size_t n, - ACE_Addr &addr, - int flags = 0, - const ACE_Time_Value *timeout = NULL) const; - -#if defined (ACE_HAS_MSG) - - /// Send an of size @a n to the datagram socket (uses - /// ) with Scatter-Gather I/O. - ssize_t send (const iovec iov[], - int n, - const ACE_INET_Addr &addr, - int flags = 0) const; - - /// Recv an of size @a n to the datagram socket (uses - /// ) with Scatter-Gather I/O. The IP destination address will be placed in @a - /// *to_addr if it is not null and set_option has been called with - /// 1) level IPPROTO_IP, option ACE_RECVPKTINFO, and value 1 for - /// IPV4 addresses or 2) IPPROTO_IPV6, option ACE_RECVPKTINFO6, and - /// value 1 for IPV6 addresses. - ssize_t recv (iovec iov[], - int n, ACE_INET_Addr &addr, int flags = 0, - ACE_INET_Addr *to_addr = 0) const; + const ACE_Time_Value *timeout = NULL) const; - #endif //@} @@ -134,14 +118,27 @@ class ACE_Export ACE_RAW_SOCKET : public ACE_SOCK, private ACE_Copy_Disabled /// Dump the state of object. void dump () const; + + + /// Return @c true if the protocol is IPPROTO_RAW. + bool is_send_only () const; + + /// Return the protocol field value. + int protocol () const; /// Declare the dynamic allocation hooks. ACE_ALLOC_HOOK_DECLARE; +private: + int protocol_; }; ACE_END_VERSIONED_NAMESPACE_DECL +#if defined (__ACE_INLINE__) +#include "ace/RAW_Socket.inl" +#endif /* __ACE_INLINE__ */ + #include /**/ "ace/post.h" #endif /* ACE_RAW_SOCKET_H */ diff --git a/ACE/ace/RAW_Socket.inl b/ACE/ace/RAW_Socket.inl new file mode 100644 index 0000000000000..13963aed96a2b --- /dev/null +++ b/ACE/ace/RAW_Socket.inl @@ -0,0 +1,22 @@ +#include "ace/ACE.h" +#include "ace/Global_Macros.h" +#include "ace/RAW_Socket.h" + +ACE_BEGIN_VERSIONED_NAMESPACE_DECL + + +ACE_INLINE bool +ACE_RAW_SOCKET::is_send_only () const +{ + return this->protocol_ == IPPROTO_RAW; +} + +ACE_INLINE int +ACE_RAW_SOCKET:: protocol () const +{ + return this->protocol_; +} + + +ACE_END_VERSIONED_NAMESPACE_DECL + diff --git a/ACE/tests/RAW_Socket_Test.cpp b/ACE/tests/RAW_Socket_Test.cpp new file mode 100644 index 0000000000000..23e0f45fd1dbb --- /dev/null +++ b/ACE/tests/RAW_Socket_Test.cpp @@ -0,0 +1,94 @@ +// ============================================================================ +// ============================================================================ +// +// = LIBRARY +// tests +// +// = DESCRIPTION +// This program tests both the short and long option support in +// , and demonstrates how to use it. +// +// = AUTHOR +// Don Hinton +// +// ============================================================================ + +#include "test_config.h" +#include "ace/RAW_Socket.h" + + +#define EXCEPTION_RETURN(expression, message)\ +do{\ + if(expression)\ + {\ + ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT (message)), 1);\ +}\ +}while(0) + + +/* + * This is the heart of the test. It test the api of RAW socket class + * + * It returns 0 for success and 1 for error so we can keep track of the + * total error count. + */ + + +static int +run_option_test () +{ + + ACE_DEBUG ((LM_INFO, "%s begin to run ...\n", __func__)); + + ACE_INET_Addr addr; + ACE_INET_Addr (12711, "127.0.0.1"); + ACE_RAW_SOCKET rawSocket(addr); + + EXCEPTION_RETURN(rawSocket.get_handle() == ACE_INVALID_HANDLE, " can not bind the addr\n"); + + int rc = rawSocket.enable(ACE_NONBLOCK); + + EXCEPTION_RETURN(rc < 0, " can not bind the addr\n"); + + + int optval = 0, optlen = sizeof(optval); + rc = rawSocket.get_option(SOL_SOCKET, SO_RCVBUF, &optval, &optlen); + + EXCEPTION_RETURN(rc < 0, " get SO_RCVBUF in failure\n"); + + optlen = sizeof(optval); + int new_optval = optval << 1; + rc = rawSocket.set_option(SOL_SOCKET, SO_RCVBUF, &new_optval, sizeof(new_optval)); + + EXCEPTION_RETURN(rc < 0, " set SO_RCVBUF new value in failure\n"); + + new_optval = 0; + optlen = sizeof(new_optval); + + rawSocket.get_option(SOL_SOCKET, SO_RCVBUF, &new_optval, &optlen); + + EXCEPTION_RETURN(new_optval < optval, " set SO_RCVBUF with no effect\n"); + + ACE_DEBUG ((LM_INFO, "old optval: %d, new optval ...\n", optval, new_optval)); + + return 1; +} + + + +int +run_main (int, ACE_TCHAR *argv[]) +{ + ACE_START_TEST (ACE_TEXT ("RAW_Socket_Test")); + ACE_UNUSED_ARG (argv); + int retval = 0; + + + // Run the tests for each type of ordering. + retval = run_option_test (); + + + ACE_END_TEST; + + return retval; +} diff --git a/ACE/tests/tests.mpc b/ACE/tests/tests.mpc index c98a938e1844a..f241e6fe34183 100644 --- a/ACE/tests/tests.mpc +++ b/ACE/tests/tests.mpc @@ -1419,6 +1419,13 @@ project(Pipe Test) : acetest { } } +project(RAW Socket Test) : acetest { + exename = RAW_Socket_Test + Source_Files { + RAW_Socket_Test.cpp + } +} + project(RB Tree Test) : acetest { exename = RB_Tree_Test Source_Files { From d70bbd37abaf92f93260993fa443ef2a49972844 Mon Sep 17 00:00:00 2001 From: smithAchang Date: Mon, 20 Mar 2023 22:59:09 +0800 Subject: [PATCH 04/39] test IPv4 raw and big pkg --- ACE/ace/RAW_Socket.cpp | 2 +- ACE/tests/RAW_Socket_Test.cpp | 207 ++++++++++++++++++++++++++++++++-- 2 files changed, 201 insertions(+), 8 deletions(-) diff --git a/ACE/ace/RAW_Socket.cpp b/ACE/ace/RAW_Socket.cpp index d2f80a68791f1..ab926271fbe9a 100644 --- a/ACE/ace/RAW_Socket.cpp +++ b/ACE/ace/RAW_Socket.cpp @@ -86,7 +86,7 @@ ACE_RAW_SOCKET::send (const void *buf, return -1; } - if(is_send_only()) + if(this->is_send_only()) { return -1; } diff --git a/ACE/tests/RAW_Socket_Test.cpp b/ACE/tests/RAW_Socket_Test.cpp index 23e0f45fd1dbb..14f197656dd0e 100644 --- a/ACE/tests/RAW_Socket_Test.cpp +++ b/ACE/tests/RAW_Socket_Test.cpp @@ -5,16 +5,78 @@ // tests // // = DESCRIPTION -// This program tests both the short and long option support in -// , and demonstrates how to use it. +// This program tests the basic APIs supported in +// , and demonstrates how to use it. // // = AUTHOR -// Don Hinton +// Smith Achang // // ============================================================================ #include "test_config.h" #include "ace/RAW_Socket.h" +#include "ace/SOCK_Dgram.h" + +typedef struct _IP_HEADER_t +{ + uint8_t bVersionAndHeaderLen; // 版本信息(前4位),头长度(后4位) + uint8_t bTypeOfService; // 服务类型8位 + uint16_t u16TotalLenOfPacket; // 数据包长度 + uint16_t u16PacketID; // 数据包标识 + uint16_t u16Sliceinfo; // 分片使用 + uint8_t bTTL; // 存活时间 + uint8_t bTypeOfProtocol; // 协议类型 + uint16_t u16CheckSum; // 校验和 + uint32_t u32SourIp; // 源ip + uint32_t u32DestIp; // 目的ip +} __attribute__((packed)) IPv4_HEADER_t, *IPv4_HEADER_t_Ptr; + +typedef struct _IPv6_HEADER_t +{ + union + { + uint8_t abVTF[4]; // 版本信息 most top 4 bits , traffic 8 bits + uint32_t dwVTF; // flow label lefted 20 bits; + }; + + uint16_t u16PayloadLen; // 数据包长度 + uint8_t bNextHeader; // 数据包标识 + uint8_t bHopLimit; // max hop limit + + union + { + uint8_t abSrcAddr[16]; + uint32_t au32SrcAddr[4]; + uint64_t au64SrcAddr[2]; + }; + + union + { + uint8_t abDstAddr[16]; + uint32_t au32DstAddr[4]; + uint64_t au64DstAddr[2]; + }; + +} __attribute__((packed)) IPv6_HEADER_t, *IPv6_HEADER_t_Ptr; + +typedef struct _UDP_HEADER_t +{ + uint16_t u16SrcPort; + uint16_t u16DstPort; + uint16_t u16Length; + uint16_t u16CheckSum; +} __attribute__((packed)) UDP_HEADER_t, *UDP_HEADER_t_Ptr; + + +class SockGuard : private ACE_Copy_Disabled +{ +public: +SockGuard(ACE_SOCK& sock):sock_(sock){}; +~SockGuard(){ sock_.close(); }; + +private: + ACE_SOCK& sock_; +}; #define EXCEPTION_RETURN(expression, message)\ @@ -25,6 +87,8 @@ do{\ }\ }while(0) +static char sendbuf[4096]; +static char recvbuf[4096]; /* * This is the heart of the test. It test the api of RAW socket class @@ -39,10 +103,10 @@ run_option_test () { ACE_DEBUG ((LM_INFO, "%s begin to run ...\n", __func__)); - - ACE_INET_Addr addr; - ACE_INET_Addr (12711, "127.0.0.1"); + + ACE_INET_Addr addr(u_short(0), "127.0.0.1"); ACE_RAW_SOCKET rawSocket(addr); + SockGuard guard(rawSocket); EXCEPTION_RETURN(rawSocket.get_handle() == ACE_INVALID_HANDLE, " can not bind the addr\n"); @@ -71,10 +135,137 @@ run_option_test () ACE_DEBUG ((LM_INFO, "old optval: %d, new optval ...\n", optval, new_optval)); - return 1; + return 0; +} + + +static int +run_reopen_test () +{ + + ACE_DEBUG ((LM_INFO, "%s begin to run ...\n", __func__)); + + ACE_INET_Addr addr((u_short)0, "127.0.0.1"); + ACE_RAW_SOCKET rawSocket(addr); + SockGuard guard(rawSocket); + + EXCEPTION_RETURN(rawSocket.get_handle() == ACE_INVALID_HANDLE, " can not bind the addr\n"); + + + rawSocket.close(); + + EXCEPTION_RETURN(rawSocket.get_handle() != ACE_INVALID_HANDLE, " close in failure\n"); + + ACE_INET_Addr addr2((u_short)0, "127.0.0.8"); + int rc = rawSocket.open(addr2); + + EXCEPTION_RETURN(rc < 0, " reopen in failue\n"); + EXCEPTION_RETURN(rawSocket.get_handle() == ACE_INVALID_HANDLE, " handle is invalid after re-open\n"); + + + return 0; +} + +static int raw_recv_data_until_meet_condition(ACE_RAW_SOCKET& raw, u_short port, size_t n) +{ + ACE_INET_Addr local; + raw.get_local_addr(local); + do + { + ACE_INET_Addr remote; + ssize_t len = raw.recv(recvbuf, sizeof(recvbuf), remote); + + if(len < 0) + { + return 1; + } + + if(local.get_type() == AF_INET) + { + IPv4_HEADER_t_Ptr ptIPv4Header = (IPv4_HEADER_t_Ptr)recvbuf; + UDP_HEADER_t_Ptr ptUDPHeader = (UDP_HEADER_t_Ptr)(recvbuf + sizeof(IPv4_HEADER_t)); + u_short nDstPort = ntohs(ptUDPHeader->u16DstPort) ; + if(port == nDstPort && len == (n + sizeof(IPv4_HEADER_t) + sizeof(UDP_HEADER_t))) + { + ACE_DEBUG ((LM_INFO, "%s recv expected pkgs ...\n", __func__)); + break; + } + } + + + ACE_DEBUG ((LM_DEBUG, "%s recv unexpected pkgs ...\n", __func__)); + ACE_OS::sleep(1); + } while (1); + + return 0; +} + +static int +run_data_transfer_test_common_child_flow_sendby_self (ACE_RAW_SOCKET& raw, ACE_INET_Addr& client_addr, ACE_INET_Addr& server_addr, size_t n) +{ + ACE_DEBUG ((LM_INFO, "%s begin to run when sending data by self ...\n", __func__)); + IPv4_HEADER_t_Ptr ptIPv4Header = (IPv4_HEADER_t_Ptr)sendbuf; + UDP_HEADER_t_Ptr ptUDPHeader = (UDP_HEADER_t_Ptr)(sendbuf + sizeof(IPv4_HEADER_t_Ptr)); + + ptUDPHeader->u16SrcPort = htons(client_addr.get_port_number()); + ptUDPHeader->u16DstPort = htons(server_addr.get_port_number()); + ptUDPHeader->u16Length = htons(n + sizeof(UDP_HEADER_t)); + ptUDPHeader->u16CheckSum = 0; + + int rc = raw.send(ptUDPHeader, n + sizeof(UDP_HEADER_t), server_addr); + EXCEPTION_RETURN(rc != (n + sizeof(UDP_HEADER_t)), " raw socket can not send test pkg to server\n"); + + u_short server_port = server_addr.get_port_number(); + rc = raw_recv_data_until_meet_condition(raw, server_port, n); + EXCEPTION_RETURN(rc != 0, " can recv test pkg from raw socket\n"); + + return 0; } +static int +run_data_transfer_test_common () +{ + ACE_DEBUG ((LM_INFO, "%s begin to run using the port assigned by OS to avoid conflict ...\n", __func__)); + + ACE_INET_Addr addr((u_short)0, "127.0.0.1"); + + ACE_SOCK_Dgram dgram(addr); + SockGuard dgram_guard(dgram); + + ACE_SOCK_Dgram dgram_server(addr); + SockGuard dgram_server_guard(dgram_server); + + ACE_RAW_SOCKET rawSocket(addr); + SockGuard raw_guard(rawSocket); + + ACE_INET_Addr client_addr ,server_addr; + int rc = dgram.get_local_addr (client_addr); + + EXCEPTION_RETURN(rc < 0, " can not get client bound address\n"); + + rc = dgram_server.get_local_addr (server_addr); + EXCEPTION_RETURN(rc < 0, " can not get server address\n"); + + size_t n = 512; + rc = dgram.send(sendbuf, n, server_addr); + EXCEPTION_RETURN(rc != n, " can send test pkg to server\n"); + + u_short server_port = server_addr.get_port_number(); + rc = raw_recv_data_until_meet_condition(rawSocket, server_port, n); + + EXCEPTION_RETURN(rc != 0, " can recv test pkg from raw socket\n"); + + rc = run_data_transfer_test_common_child_flow_sendby_self (rawSocket, client_addr, server_addr, n + 1); + EXCEPTION_RETURN(rc != 0, " can recv test pkg from raw socket when sending by self\n"); + + ACE_DEBUG ((LM_INFO, "%s test send & recv big pkt ...\n", __func__)); + rc = run_data_transfer_test_common_child_flow_sendby_self (rawSocket, client_addr, server_addr, n + 2048); + EXCEPTION_RETURN(rc != 0, " can recv test pkg from raw socket when sending big pkg by self\n"); + + + return 0; +} int run_main (int, ACE_TCHAR *argv[]) @@ -86,6 +277,8 @@ run_main (int, ACE_TCHAR *argv[]) // Run the tests for each type of ordering. retval = run_option_test (); + retval += run_reopen_test(); + retval += run_data_transfer_test_common(); ACE_END_TEST; From 2ba1faeeb660a807796d9921d3aef84dccf2d4f5 Mon Sep 17 00:00:00 2001 From: smithAchang Date: Tue, 21 Mar 2023 22:35:34 +0800 Subject: [PATCH 05/39] raw generic test --- ACE/ace/RAW_Socket.cpp | 31 +++------- ACE/tests/RAW_Socket_Test.cpp | 111 ++++++++++++++++++++++++++++++---- 2 files changed, 108 insertions(+), 34 deletions(-) diff --git a/ACE/ace/RAW_Socket.cpp b/ACE/ace/RAW_Socket.cpp index ab926271fbe9a..0f007bfbf7260 100644 --- a/ACE/ace/RAW_Socket.cpp +++ b/ACE/ace/RAW_Socket.cpp @@ -33,12 +33,12 @@ ACE_RAW_SOCKET::ACE_RAW_SOCKET (ACE_INET_Addr const & local, int reuse_addr) : protocol_(protocol) { ACE_TRACE ("ACE_RAW_SOCKET::ACE_RAW_SOCKET"); + if (this->open (local, protocol, reuse_addr) == -1) - { ACELIB_ERROR ((LM_ERROR, - ACE_TEXT ("%p\n"), - ACE_TEXT ("ACE_RAW_SOCKET"))); - } + ACE_TEXT ("%p\n"), + ACE_TEXT ("ACE_RAW_SOCKET"))); + } ssize_t @@ -50,14 +50,13 @@ ACE_RAW_SOCKET::recv (void *buf, { if (this->get_handle () == ACE_INVALID_HANDLE) - { return -1; - } + if(this->is_send_only()) + return -1; + if(timeout && ACE::handle_read_ready (this->get_handle (), timeout) != 1) - { return -1; - } sockaddr *saddr = (sockaddr *) addr.get_addr (); int addr_len = addr.get_size (); @@ -82,21 +81,11 @@ ACE_RAW_SOCKET::send (const void *buf, { // Check the status of the current socket. if (this->get_handle () == ACE_INVALID_HANDLE) - { return -1; - } - - if(this->is_send_only()) - { - return -1; - } if(timeout && ACE::handle_write_ready (this->get_handle (), timeout) != 1) - { return -1; - } - sockaddr *saddr = (sockaddr *) addr.get_addr (); int len = addr.get_size (); return ACE_OS::sendto (this->get_handle (), @@ -114,17 +103,13 @@ ACE_RAW_SOCKET::open (ACE_INET_Addr const & local, int protocol, int reuse_addr) ACE_TRACE ("ACE_RAW_SOCKET::open"); if (this->get_handle () != ACE_INVALID_HANDLE) - { - return -1; - } + return -1; int protocol_family = local.get_type (); if(ACE_SOCK::open (SOCK_RAW, protocol_family, protocol, reuse_addr) == -1) - { return -1; - } this->protocol_ = protocol; diff --git a/ACE/tests/RAW_Socket_Test.cpp b/ACE/tests/RAW_Socket_Test.cpp index 14f197656dd0e..63e46cb97fe64 100644 --- a/ACE/tests/RAW_Socket_Test.cpp +++ b/ACE/tests/RAW_Socket_Test.cpp @@ -182,7 +182,6 @@ static int raw_recv_data_until_meet_condition(ACE_RAW_SOCKET& raw, u_short port, if(local.get_type() == AF_INET) { - IPv4_HEADER_t_Ptr ptIPv4Header = (IPv4_HEADER_t_Ptr)recvbuf; UDP_HEADER_t_Ptr ptUDPHeader = (UDP_HEADER_t_Ptr)(recvbuf + sizeof(IPv4_HEADER_t)); u_short nDstPort = ntohs(ptUDPHeader->u16DstPort) ; if(port == nDstPort && len == (n + sizeof(IPv4_HEADER_t) + sizeof(UDP_HEADER_t))) @@ -201,7 +200,7 @@ static int raw_recv_data_until_meet_condition(ACE_RAW_SOCKET& raw, u_short port, } static int -run_data_transfer_test_common_child_flow_sendby_self (ACE_RAW_SOCKET& raw, ACE_INET_Addr& client_addr, ACE_INET_Addr& server_addr, size_t n) +run_raw_udp_test_child_flow_sendby_self (ACE_RAW_SOCKET& raw, ACE_INET_Addr& client_addr, ACE_INET_Addr& server_addr, size_t n) { ACE_DEBUG ((LM_INFO, "%s begin to run when sending data by self ...\n", __func__)); IPv4_HEADER_t_Ptr ptIPv4Header = (IPv4_HEADER_t_Ptr)sendbuf; @@ -223,9 +222,9 @@ run_data_transfer_test_common_child_flow_sendby_self (ACE_RAW_SOCKET& raw, ACE_I } static int -run_data_transfer_test_common () +run_raw_udp_test () { - ACE_DEBUG ((LM_INFO, "%s begin to run using the port assigned by OS to avoid conflict ...\n", __func__)); + ACE_DEBUG ((LM_INFO, "%s begin to run using the port auto assigned by OS to avoid port conflict ...\n", __func__)); ACE_INET_Addr addr((u_short)0, "127.0.0.1"); @@ -256,29 +255,119 @@ run_data_transfer_test_common () EXCEPTION_RETURN(rc != 0, " can recv test pkg from raw socket\n"); - rc = run_data_transfer_test_common_child_flow_sendby_self (rawSocket, client_addr, server_addr, n + 1); + rc = run_raw_udp_test_child_flow_sendby_self (rawSocket, client_addr, server_addr, n + 1); EXCEPTION_RETURN(rc != 0, " can recv test pkg from raw socket when sending by self\n"); - ACE_DEBUG ((LM_INFO, "%s test send & recv big pkt ...\n", __func__)); - rc = run_data_transfer_test_common_child_flow_sendby_self (rawSocket, client_addr, server_addr, n + 2048); - EXCEPTION_RETURN(rc != 0, " can recv test pkg from raw socket when sending big pkg by self\n"); - - + if(ACE_OS::getuid() == 0) + { + ACE_DEBUG ((LM_INFO, "%s test send & recv big pkt ...\n", __func__)); + rc = run_raw_udp_test_child_flow_sendby_self (rawSocket, client_addr, server_addr, n + 2048); + EXCEPTION_RETURN(rc != 0, " can recv test pkg from raw socket when sending big pkg by self\n"); + } + return 0; } +static int +run_raw_generic_test () +{ + ACE_INET_Addr bindAddr((u_short)0, "127.0.0.1"), remote; + ACE_INET_Addr client_addr((u_short)0, "127.0.0.7") ,server_addr((u_short)0, "127.0.0.8"); + ACE_SOCK_Dgram client_dgram(client_addr); + SockGuard client_dgram_guard(client_dgram); + + ACE_SOCK_Dgram server_dgram(server_addr); + SockGuard server_dgram_guard(server_dgram); + + ACE_RAW_SOCKET rawSocket(bindAddr, IPPROTO_RAW); + SockGuard raw_guard(rawSocket); + + EXCEPTION_RETURN(rawSocket.is_send_only() == false, " raw socket is not send only\n"); + + ssize_t len = rawSocket.recv(recvbuf, sizeof(recvbuf), remote); + EXCEPTION_RETURN(len != -1, " raw generic socket is send only , must not can recv data\n"); + + + client_dgram.get_local_addr (client_addr); + server_dgram.get_local_addr (server_addr); + + IPv4_HEADER_t_Ptr ptIPv4Header = (IPv4_HEADER_t_Ptr)sendbuf; + UDP_HEADER_t_Ptr ptUDPHeader = (UDP_HEADER_t_Ptr)(sendbuf + sizeof(IPv4_HEADER_t)); + u_short n = 2048; + + *ptIPv4Header = {}; + + ptIPv4Header->bVersionAndHeaderLen = 0x45; + ptIPv4Header->u16TotalLenOfPacket = htons(sizeof(IPv4_HEADER_t) + sizeof(UDP_HEADER_t) + n); // 数据包长度 + ptIPv4Header->u16PacketID = ACE_OS::rand(); + ptIPv4Header->bTTL = 64; + ptIPv4Header->bTypeOfProtocol = IPPROTO_UDP; + ptIPv4Header->u16CheckSum = 0; + ptIPv4Header->u32SourIp = (static_cast(client_addr.get_addr()))->sin_addr.s_addr; + ptIPv4Header->u32DestIp = (static_cast(server_addr.get_addr()))->sin_addr.s_addr; + + u_short client_port_number = client_addr.get_port_number(); + u_short server_port_number = server_addr.get_port_number(); + + + ptUDPHeader->u16SrcPort = htons(client_port_number); + ptUDPHeader->u16DstPort = htons(server_port_number); + ptUDPHeader->u16Length = htons(sizeof(UDP_HEADER_t) + n); + ptUDPHeader->u16CheckSum = 0; + + if(ACE_OS::getuid() == 0) + { + n = 2048; + len = rawSocket.send(sendbuf, sizeof(IPv4_HEADER_t) + sizeof(UDP_HEADER_t) + n, remote); + EXCEPTION_RETURN(len != -1, " raw generic socket can not send pkg more than MTU\n"); + } + + n = 468; + ptUDPHeader->u16Length = htons(sizeof(UDP_HEADER_t) + n); + len = rawSocket.send(sendbuf, sizeof(IPv4_HEADER_t) + sizeof(UDP_HEADER_t) + n, remote); + EXCEPTION_RETURN(static_cast(len) != (sizeof(IPv4_HEADER_t) + sizeof(UDP_HEADER_t) + n), " raw generic socket send pkg in failure\n"); + + ACE_OS::sleep(1); + ACE_DEBUG ((LM_INFO, "%s enable nonblock status ...\n", __func__)); + server_dgram.enable(ACE_NONBLOCK); + len = server_dgram.recv(recvbuf, sizeof(recvbuf), remote); + EXCEPTION_RETURN(static_cast(len) != n, " server socket receives pkg in failure length is not the same\n"); + EXCEPTION_RETURN(static_cast(remote.get_addr())->sin_addr.s_addr != static_cast(client_addr.get_addr())->sin_addr.s_addr, " server socket receives pkg in failure: the source IP is not the same\n"); + + return 0; +} + int run_main (int, ACE_TCHAR *argv[]) { ACE_START_TEST (ACE_TEXT ("RAW_Socket_Test")); ACE_UNUSED_ARG (argv); int retval = 0; + + // set the lo interface MTU + if(ACE_OS::getuid() == 0) + { + ACE_INET_Addr anyAddr((u_short)0); + ACE_SOCK_Dgram netdevice(anyAddr); + SockGuard dgram_guard(netdevice); + + struct ifreq tReq = {}; + ACE_OS::snprintf(tReq.ifr_name, sizeof(tReq.ifr_name), "%s", "lo"); + tReq.ifr_mtu = 1400; + ACE_OS::ioctl(netdevice.get_handle(), SIOCSIFMTU, &tReq); + + tReq.ifr_mtu = 0; + ACE_OS::ioctl(netdevice.get_handle(), SIOCGIFMTU, &tReq); + EXCEPTION_RETURN(tReq.ifr_mtu != 1400, " can set MTU for lo interface\n"); + } + // Run the tests for each type of ordering. retval = run_option_test (); retval += run_reopen_test(); - retval += run_data_transfer_test_common(); + retval += run_raw_udp_test(); + retval += run_raw_generic_test(); ACE_END_TEST; From 05b67ee3d87879550fa44d331f19020c96040d9f Mon Sep 17 00:00:00 2001 From: smitAchang Date: Wed, 22 Mar 2023 20:07:09 +0800 Subject: [PATCH 06/39] add pkginfo funciton --- ACE/ace/RAW_Socket.cpp | 134 +++++++++++++++++++++++++++++++--- ACE/ace/RAW_Socket.h | 26 +++---- ACE/tests/RAW_Socket_Test.cpp | 114 +++++++++++++++++++++++++---- 3 files changed, 238 insertions(+), 36 deletions(-) diff --git a/ACE/ace/RAW_Socket.cpp b/ACE/ace/RAW_Socket.cpp index 0f007bfbf7260..36d8deca09def 100644 --- a/ACE/ace/RAW_Socket.cpp +++ b/ACE/ace/RAW_Socket.cpp @@ -29,11 +29,11 @@ ACE_RAW_SOCKET::ACE_RAW_SOCKET () : protocol_(IPPROTO_UDP) /// @brief constructor with parameters ACE_RAW_SOCKET::ACE_RAW_SOCKET (ACE_INET_Addr const & local, - int protocol, - int reuse_addr) : protocol_(protocol) + int protocol) : protocol_(protocol) { ACE_TRACE ("ACE_RAW_SOCKET::ACE_RAW_SOCKET"); - + /// reuse_addr Maybe meaningless for RAW Socket + const int reuse_addr = 1; if (this->open (local, protocol, reuse_addr) == -1) ACELIB_ERROR ((LM_ERROR, ACE_TEXT ("%p\n"), @@ -46,7 +46,8 @@ ACE_RAW_SOCKET::recv (void *buf, size_t n, ACE_INET_Addr &addr, int flags, - const ACE_Time_Value *timeout) const + const ACE_Time_Value *timeout + ACE_INET_Addr *to_addr) const { if (this->get_handle () == ACE_INVALID_HANDLE) @@ -57,18 +58,131 @@ ACE_RAW_SOCKET::recv (void *buf, if(timeout && ACE::handle_read_ready (this->get_handle (), timeout) != 1) return -1; - - sockaddr *saddr = (sockaddr *) addr.get_addr (); - int addr_len = addr.get_size (); - ssize_t const status = ACE_OS::recvfrom (this->get_handle (), + + if(to_addr == NULL) + { + + sockaddr *saddr = (sockaddr *) addr.get_addr (); + int addr_len = addr.get_size (); + + ssize_t const status = ACE_OS::recvfrom (this->get_handle (), (char *) buf, n, flags, (sockaddr *) saddr, &addr_len); - addr.set_size (addr_len); - addr.set_type (saddr->sa_family); + + addr.set_size (addr_len); + addr.set_type (saddr->sa_family); + return status; + } + else + { + this->get_local_addr (*to_addr); + if(!local.is_any()) + { + sockaddr *saddr = (sockaddr *) addr.get_addr (); + int addr_len = addr.get_size (); + + ssize_t const status = ACE_OS::recvfrom (this->get_handle (), + (char *) buf, + n, + flags, + (sockaddr *) saddr, + &addr_len); + + addr.set_size (addr_len); + addr.set_type (saddr->sa_family); + return status; + } + + } + + + #if defined (ACE_HAS_4_4BSD_SENDMSG_RECVMSG) || defined ACE_WIN32 + #define ACE_USE_MSG_CONTROL + union control_buffer { + cmsghdr control_msg_header; + #if defined (IP_RECVDSTADDR) + u_char padding[ACE_CMSG_SPACE (sizeof (in_addr))]; + #elif defined (IP_PKTINFO) + u_char padding[ACE_CMSG_SPACE (sizeof (in_pktinfo))]; + #endif + #if defined (ACE_HAS_IPV6) + u_char padding6[ACE_CMSG_SPACE (sizeof (in6_pktinfo))]; + #endif + } cbuf; + #endif + + struct iovec iov; + iov.io_base = buf; + iov.iov_len = n; + + msghdr recv_msg = {}; + recv_msg.msg_iov = (iovec*)&iov; + recv_msg.msg_iovlen = 1; + + #if defined (ACE_HAS_SOCKADDR_MSG_NAME) + recv_msg.msg_name = (struct sockaddr *) addr.get_addr (); + #else + recv_msg.msg_name = (char *) addr.get_addr (); + #endif /* ACE_HAS_SOCKADDR_MSG_NAME */ + recv_msg.msg_namelen = addr.get_size (); + + #ifdef ACE_USE_MSG_CONTROL + recv_msg.msg_control = &cbuf + recv_msg.msg_controllen = sizeof (cbuf) ; + #elif !defined ACE_LACKS_SENDMSG + recv_msg.msg_accrights = 0; + recv_msg.msg_accrightslen = 0; + #endif + + ssize_t status = ACE_OS::recvmsg (this->get_handle (), + &recv_msg, + flags); + + addr.set_size (recv_msg.msg_namelen); + addr.set_type (((sockaddr_in *) addr.get_addr())->sin_family); + + + #ifdef ACE_USE_MSG_CONTROL + if (to_addr->get_type() == AF_INET) { + #if defined (IP_RECVDSTADDR) || defined (IP_PKTINFO) + for (cmsghdr *ptr = ACE_CMSG_FIRSTHDR (&recv_msg); ptr; ptr = ACE_CMSG_NXTHDR (&recv_msg, ptr)) { + #if defined (IP_RECVDSTADDR) + if (ptr->cmsg_level == IPPROTO_IP && ptr->cmsg_type == IP_RECVDSTADDR) { + to_addr->set_address ((const char *) (ACE_CMSG_DATA (ptr)), + sizeof (struct in_addr), + 0); + break; + } + #else + if (ptr->cmsg_level == IPPROTO_IP && ptr->cmsg_type == IP_PKTINFO) { + to_addr->set_address ((const char *) &(((in_pktinfo *) (ACE_CMSG_DATA (ptr)))->ipi_addr), + sizeof (struct in_addr), + 0); + break; + } + #endif + } + #endif + } + #if defined (ACE_HAS_IPV6) && defined (IPV6_PKTINFO) + else if (to_addr->get_type() == AF_INET6) { + for (cmsghdr *ptr = ACE_CMSG_FIRSTHDR (&recv_msg); ptr; ptr = ACE_CMSG_NXTHDR (&recv_msg, ptr)) { + if (ptr->cmsg_level == IPPROTO_IPV6 && ptr->cmsg_type == IPV6_PKTINFO) { + to_addr->set_address ((const char *) &(((in6_pktinfo *)(ACE_CMSG_DATA (ptr)))->ipi6_addr), + sizeof (struct in6_addr), + 0); + + break; + } + } + } + #endif + #endif + return status; } diff --git a/ACE/ace/RAW_Socket.h b/ACE/ace/RAW_Socket.h index fb8cb8333413f..d2accc459c9db 100644 --- a/ACE/ace/RAW_Socket.h +++ b/ACE/ace/RAW_Socket.h @@ -35,15 +35,15 @@ ACE_BEGIN_VERSIONED_NAMESPACE_DECL * @brief An RAW Socket implemention class. * * An RAW Socket can be used for some user-space network protocol stack. - * - setting the protocol para to be IPPROTO_UDP will filter all UPD protocol packages with the destination is its bound address. - * It can reduce the total num of needed socket with only port difference + * - Setting the protocol para to be IPPROTO_UDP will filter all UDP protocol packages with the destination is its bound address. + * IT can reduce the total num of socket needed with only port difference. * - * - setting the protocol para to be IPPROTO_SCTP will filter all SCTP protocol packages with the destination is its bound address. - * It can form the basis of a user-space SCTP protocol stack in more general platforms + * - Setting the protocol para to be IPPROTO_SCTP will filter all SCTP protocol packages with the destination is its bound address. + * It can form the basis of a user-space SCTP protocol stack in more general platforms. * - * - setting the protocol para to be IPPROTO_RAW will make it as a send only socket for any customized dgram formed from IP header. - * Notice the source address if provided can be different from its bound address. - * Notice the RAW socket does not support fragment function when passed package exceeds the MTU, so it need a upper layer framment before called + * - Setting the protocol para to be IPPROTO_RAW will make it as a send only socket for any customized package formed from IP header to be send. + * Notice the source address if provided in the customized package can be different from its bound address. + * Notice the RAW socket does not support fragment function when the passed package exceeds the MTU, so it need a upper layer framment before called * * @note If you really want to receive all IP packets, use a packet(7) socket with the ETH_P_IP protocol. * For "Single Responsibility Principle" the behavior has notable difference, so the feature is not implemented here. @@ -57,10 +57,8 @@ class ACE_Export ACE_RAW_SOCKET : public ACE_SOCK, private ACE_Copy_Disabled /// Constructor that bind a local address and fiter UDP protocol. /// @param local local IP address to bind /// @param protocol IPPROTO_UDP as default value. often used as a user-space UDP stack - /// @param reuse_addr Maybe meaningless for RAW Socket - ACE_RAW_SOCKET (ACE_INET_Addr const & local, - int protocol = IPPROTO_UDP, - int reuse_addr = 1); + ACE_RAW_SOCKET (ACE_INET_Addr const & local, int protocol = IPPROTO_UDP); + /** * @name Data transfer routines. @@ -92,13 +90,15 @@ class ACE_Export ACE_RAW_SOCKET : public ACE_SOCK, private ACE_Copy_Disabled * until action is possible, else will wait until the relative time * specified in *@a timeout elapses). If times out a -1 is * returned with @c errno == ETIME. If it succeeds the number of - * bytes received is returned. + * bytes received is returned. + * When the RAW socket is bound to wildcard address, @a to_addr can get the destination info */ ssize_t recv (void *buf, size_t n, ACE_INET_Addr &addr, int flags = 0, - const ACE_Time_Value *timeout = NULL) const; + const ACE_Time_Value *timeout = NULL, + ACE_INET_Addr *to_addr = NULL) const; //@} diff --git a/ACE/tests/RAW_Socket_Test.cpp b/ACE/tests/RAW_Socket_Test.cpp index 63e46cb97fe64..575a3c679181c 100644 --- a/ACE/tests/RAW_Socket_Test.cpp +++ b/ACE/tests/RAW_Socket_Test.cpp @@ -166,29 +166,46 @@ run_reopen_test () return 0; } -static int raw_recv_data_until_meet_condition(ACE_RAW_SOCKET& raw, u_short port, size_t n) +static int raw_recv_data_until_meet_condition(ACE_RAW_SOCKET& raw, u_short port, size_t n, ACE_INET_Addr& remote, ACE_INET_Addr* to_addr) { ACE_INET_Addr local; raw.get_local_addr(local); + + ssize_t len = 0; + do { - ACE_INET_Addr remote; - ssize_t len = raw.recv(recvbuf, sizeof(recvbuf), remote); + + if(to_addr == NULL) + { + len = raw.recv(recvbuf, sizeof(recvbuf), remote); + } + else + { + len = raw.recv(recvbuf, sizeof(recvbuf), remote, 0/*flags*/, nullptr, to_addr); + } + if(len < 0) { - return 1; + return -1; } + UDP_HEADER_t_Ptr ptUDPHeader; if(local.get_type() == AF_INET) { - UDP_HEADER_t_Ptr ptUDPHeader = (UDP_HEADER_t_Ptr)(recvbuf + sizeof(IPv4_HEADER_t)); - u_short nDstPort = ntohs(ptUDPHeader->u16DstPort) ; - if(port == nDstPort && len == (n + sizeof(IPv4_HEADER_t) + sizeof(UDP_HEADER_t))) - { + ptUDPHeader = (UDP_HEADER_t_Ptr)(recvbuf + sizeof(IPv4_HEADER_t)); + } + else + { + ptUDPHeader = (UDP_HEADER_t_Ptr)recvbuf; + } + + u_short nDstPort = ntohs(ptUDPHeader->u16DstPort) ; + if(port == nDstPort && len == (n + sizeof(IPv4_HEADER_t) + sizeof(UDP_HEADER_t))) + { ACE_DEBUG ((LM_INFO, "%s recv expected pkgs ...\n", __func__)); break; - } } @@ -215,7 +232,8 @@ run_raw_udp_test_child_flow_sendby_self (ACE_RAW_SOCKET& raw, ACE_INET_Addr& cli EXCEPTION_RETURN(rc != (n + sizeof(UDP_HEADER_t)), " raw socket can not send test pkg to server\n"); u_short server_port = server_addr.get_port_number(); - rc = raw_recv_data_until_meet_condition(raw, server_port, n); + ACE_INET_Addr remote; + rc = raw_recv_data_until_meet_condition(raw, server_port, n, remote); EXCEPTION_RETURN(rc != 0, " can recv test pkg from raw socket\n"); return 0; @@ -237,7 +255,7 @@ run_raw_udp_test () ACE_RAW_SOCKET rawSocket(addr); SockGuard raw_guard(rawSocket); - ACE_INET_Addr client_addr ,server_addr; + ACE_INET_Addr client_addr ,server_addr,remote; int rc = dgram.get_local_addr (client_addr); EXCEPTION_RETURN(rc < 0, " can not get client bound address\n"); @@ -251,7 +269,7 @@ run_raw_udp_test () EXCEPTION_RETURN(rc != n, " can send test pkg to server\n"); u_short server_port = server_addr.get_port_number(); - rc = raw_recv_data_until_meet_condition(rawSocket, server_port, n); + rc = raw_recv_data_until_meet_condition(rawSocket, server_port, n, remote); EXCEPTION_RETURN(rc != 0, " can recv test pkg from raw socket\n"); @@ -271,8 +289,10 @@ run_raw_udp_test () static int run_raw_generic_test () { + ACE_DEBUG ((LM_INFO, "%s begin to run generic raw socket i.e. send only RAW socket ...\n", __func__)); + ACE_INET_Addr bindAddr((u_short)0, "127.0.0.1"), remote; - ACE_INET_Addr client_addr((u_short)0, "127.0.0.7") ,server_addr((u_short)0, "127.0.0.8"); + ACE_INET_Addr client_addr((u_short)0, "127.0.0.7") ,server_addr((u_short)0, "127.0.0.8"); ACE_SOCK_Dgram client_dgram(client_addr); SockGuard client_dgram_guard(client_dgram); @@ -337,12 +357,63 @@ run_raw_generic_test () return 0; } +#if defined (ACE_HAS_IPV6) +static int +run_ipv6_pkginfo_test () +{ + ACE_DEBUG ((LM_INFO, "%s begin to run IPv6 pkginfo test ...\n", __func__)); + + ACE_INET_Addr bindAddr((u_short)0, "::1"), remote; + ACE_INET_Addr anyAddr((u_short)0, "::"); + + ACE_INET_Addr client_addr((u_short)0, "::1") ,server_addr((u_short)0, "::1"); + + ACE_SOCK_Dgram client_dgram(client_addr); + SockGuard client_dgram_guard(client_dgram); + client_dgram.get_local_addr(client_addr); + + ACE_SOCK_Dgram server_dgram(server_addr); + SockGuard server_dgram_guard(server_dgram); + server_dgram.get_local_addr(server_addr); + + ACE_RAW_SOCKET rawSocket(bindAddr, IPPROTO_UDP); + rawSocket.enable(ACE_NONBLOCK); + SockGuard raw_guard(rawSocket); + ACE_RAW_SOCKET rawWildcardSocket(anyAddr, IPPROTO_UDP); + rawWildcardSocket.enable(ACE_NONBLOCK); + SockGuard raw_guard(rawWildcardSocket); + + client_dgram.send("hello world", sizeof("hello world"), server_addr); + ACE_OS::sleep(1); + + + int rc = raw_recv_data_until_meet_condition(rawSocket, server_addr.get_port_number(), sizeof("hello world"), remote); + + EXCEPTION_RETURN(rc != 0, " can not recv expectedRecvLen\n"); + + ACE_INET_Addr to_addr; + rc = raw_recv_data_until_meet_condition(rawWildcardSocket, server_addr.get_port_number(), sizeof("hello world"), remote, &to_addr); + EXCEPTION_RETURN(rc != 0, " can not recv expectedRecvLen with to_addr when provided to wildcard RAW socket\n"); + EXCEPTION_RETURN(remote != to_addr, " to_addr with invalid value when provided to wildcard RAW socket\n"); + + + client_dgram.send("hello world", sizeof("hello world"), server_addr); + + rc = raw_recv_data_until_meet_condition(rawSocket, server_addr.get_port_number(), sizeof("hello world"), remote, &to_addr); + EXCEPTION_RETURN(len != expectedRecvLen, " can not recv expectedRecvLen with to_addr when provided to non wildcard RAW socket\n"); + EXCEPTION_RETURN(remote != to_addr, " to_addr with invalid value when provided to non wildcard RAW socket\n"); + + return 0; +} +#endif + int run_main (int, ACE_TCHAR *argv[]) { ACE_START_TEST (ACE_TEXT ("RAW_Socket_Test")); ACE_UNUSED_ARG (argv); int retval = 0; + int oldMTU = 1500; // set the lo interface MTU if(ACE_OS::getuid() == 0) @@ -368,9 +439,26 @@ run_main (int, ACE_TCHAR *argv[]) retval += run_reopen_test(); retval += run_raw_udp_test(); retval += run_raw_generic_test(); + + #if defined (ACE_HAS_IPV6) + retval += run_ipv6_pkginfo_test(); + #endif ACE_END_TEST; + if(ACE_OS::getuid() == 0) + { + ACE_INET_Addr anyAddr((u_short)0); + ACE_SOCK_Dgram netdevice(anyAddr); + SockGuard dgram_guard(netdevice); + + struct ifreq tReq = {}; + ACE_OS::snprintf(tReq.ifr_name, sizeof(tReq.ifr_name), "%s", "lo"); + tReq.ifr_mtu = oldMTU; + ACE_OS::ioctl(netdevice.get_handle(), SIOCSIFMTU, &tReq); + } + + return retval; } From 0a0d4e567ed5f142ca465891e544dbbb42e8dc18 Mon Sep 17 00:00:00 2001 From: smithAchang Date: Wed, 22 Mar 2023 23:11:04 +0800 Subject: [PATCH 07/39] add IPv6 pkginfo test --- ACE/ace/RAW_Socket.cpp | 80 +++++++++++++++---------------- ACE/ace/RAW_Socket.h | 19 ++++---- ACE/tests/RAW_Socket_Test.cpp | 90 +++++++++++++++++++++++++---------- 3 files changed, 112 insertions(+), 77 deletions(-) diff --git a/ACE/ace/RAW_Socket.cpp b/ACE/ace/RAW_Socket.cpp index 36d8deca09def..8c75d741d035f 100644 --- a/ACE/ace/RAW_Socket.cpp +++ b/ACE/ace/RAW_Socket.cpp @@ -32,21 +32,37 @@ ACE_RAW_SOCKET::ACE_RAW_SOCKET (ACE_INET_Addr const & local, int protocol) : protocol_(protocol) { ACE_TRACE ("ACE_RAW_SOCKET::ACE_RAW_SOCKET"); - /// reuse_addr Maybe meaningless for RAW Socket - const int reuse_addr = 1; - if (this->open (local, protocol, reuse_addr) == -1) + + if (this->open (local, protocol) == -1) ACELIB_ERROR ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("ACE_RAW_SOCKET"))); } +static inline ssize_t using_common_recv(const ACE_RAW_SOCKET* raw, void *buf, size_t n, ACE_INET_Addr &addr, int flags) +{ + sockaddr *saddr = (sockaddr *) addr.get_addr (); + int addr_len = addr.get_size (); + + ssize_t const status = ACE_OS::recvfrom (raw->get_handle (), + (char *) buf, + n, + flags, + (sockaddr *) saddr, + &addr_len); + + addr.set_size (addr_len); + addr.set_type (saddr->sa_family); + return status; +} + ssize_t ACE_RAW_SOCKET::recv (void *buf, size_t n, ACE_INET_Addr &addr, int flags, - const ACE_Time_Value *timeout + const ACE_Time_Value *timeout, ACE_INET_Addr *to_addr) const { @@ -62,39 +78,14 @@ ACE_RAW_SOCKET::recv (void *buf, if(to_addr == NULL) { - - sockaddr *saddr = (sockaddr *) addr.get_addr (); - int addr_len = addr.get_size (); - - ssize_t const status = ACE_OS::recvfrom (this->get_handle (), - (char *) buf, - n, - flags, - (sockaddr *) saddr, - &addr_len); - - addr.set_size (addr_len); - addr.set_type (saddr->sa_family); - return status; + return using_common_recv(this, buf, n, addr, flags); } else { this->get_local_addr (*to_addr); - if(!local.is_any()) + if(!to_addr->is_any()) { - sockaddr *saddr = (sockaddr *) addr.get_addr (); - int addr_len = addr.get_size (); - - ssize_t const status = ACE_OS::recvfrom (this->get_handle (), - (char *) buf, - n, - flags, - (sockaddr *) saddr, - &addr_len); - - addr.set_size (addr_len); - addr.set_type (saddr->sa_family); - return status; + return using_common_recv(this, buf, n, addr, flags); } } @@ -116,8 +107,8 @@ ACE_RAW_SOCKET::recv (void *buf, #endif struct iovec iov; - iov.io_base = buf; - iov.iov_len = n; + iov.iov_base = buf; + iov.iov_len = n; msghdr recv_msg = {}; recv_msg.msg_iov = (iovec*)&iov; @@ -131,8 +122,8 @@ ACE_RAW_SOCKET::recv (void *buf, recv_msg.msg_namelen = addr.get_size (); #ifdef ACE_USE_MSG_CONTROL - recv_msg.msg_control = &cbuf - recv_msg.msg_controllen = sizeof (cbuf) ; + recv_msg.msg_control = &cbuf; + recv_msg.msg_controllen = sizeof (cbuf); #elif !defined ACE_LACKS_SENDMSG recv_msg.msg_accrights = 0; recv_msg.msg_accrightslen = 0; @@ -212,7 +203,7 @@ ACE_RAW_SOCKET::send (const void *buf, } int -ACE_RAW_SOCKET::open (ACE_INET_Addr const & local, int protocol, int reuse_addr) +ACE_RAW_SOCKET::open (ACE_INET_Addr const & local, int protocol) { ACE_TRACE ("ACE_RAW_SOCKET::open"); @@ -220,13 +211,22 @@ ACE_RAW_SOCKET::open (ACE_INET_Addr const & local, int protocol, int reuse_addr) return -1; int protocol_family = local.get_type (); - - + /// reuse_addr Maybe meaningless for RAW Socket + const int reuse_addr = 1; + if(ACE_SOCK::open (SOCK_RAW, protocol_family, protocol, reuse_addr) == -1) return -1; this->protocol_ = protocol; - + + ACE_INET_Addr bindAddr; + this->get_local_addr(bindAddr); + if(bindAddr.is_any()) + { + int yes = 1; + this->set_option(IPPROTO_IPV6, IPV6_RECVPKTINFO, &yes, sizeof(yes)); + } + return 0; } diff --git a/ACE/ace/RAW_Socket.h b/ACE/ace/RAW_Socket.h index d2accc459c9db..f0acbd78845f8 100644 --- a/ACE/ace/RAW_Socket.h +++ b/ACE/ace/RAW_Socket.h @@ -2,7 +2,7 @@ //============================================================================= /** - * @file RAW_Socket.h + * @file RAW_Socket.h * * @author Smith.Achang */ @@ -56,7 +56,7 @@ class ACE_Export ACE_RAW_SOCKET : public ACE_SOCK, private ACE_Copy_Disabled /// Constructor that bind a local address and fiter UDP protocol. /// @param local local IP address to bind - /// @param protocol IPPROTO_UDP as default value. often used as a user-space UDP stack + /// @param protocol IPPROTO_UDP as default value because often used as a user-space UDP stack ACE_RAW_SOCKET (ACE_INET_Addr const & local, int protocol = IPPROTO_UDP); @@ -70,7 +70,7 @@ class ACE_Export ACE_RAW_SOCKET : public ACE_SOCK, private ACE_Copy_Disabled /** * Wait up to @a timeout amount of time to send a datagram to * @a buf. The ACE_Time_Value indicates how long to blocking - * trying to receive. If @a timeout == 0, the caller will block + * trying to receive. If @a timeout == nullptr, the caller will block * until action is possible, else will wait until the relative time * specified in *@a timeout elapses). If times out a -1 is * returned with @c errno == ETIME. If it succeeds the number of @@ -80,13 +80,13 @@ class ACE_Export ACE_RAW_SOCKET : public ACE_SOCK, private ACE_Copy_Disabled size_t n, const ACE_INET_Addr &addr, int flags = 0, - const ACE_Time_Value *timeout = NULL) const; + const ACE_Time_Value *timeout = nullptr) const; /** * Wait up to @a timeout amount of time to receive a datagram into * @a buf. The ACE_Time_Value indicates how long to blocking - * trying to receive. If @a timeout == 0, the caller will block + * trying to receive. If @a timeout == nullptr, the caller will block * until action is possible, else will wait until the relative time * specified in *@a timeout elapses). If times out a -1 is * returned with @c errno == ETIME. If it succeeds the number of @@ -97,8 +97,8 @@ class ACE_Export ACE_RAW_SOCKET : public ACE_SOCK, private ACE_Copy_Disabled size_t n, ACE_INET_Addr &addr, int flags = 0, - const ACE_Time_Value *timeout = NULL, - ACE_INET_Addr *to_addr = NULL) const; + const ACE_Time_Value *timeout = nullptr, + ACE_INET_Addr *to_addr = nullptr) const; //@} @@ -107,14 +107,11 @@ class ACE_Export ACE_RAW_SOCKET : public ACE_SOCK, private ACE_Copy_Disabled * Wrapper around the BSD-style @c socket system call (no QoS). * @param local local IP address to bind * @param protocol filter the protocol based on IP network layer. Using IPPROTO_UDP as default value for often used as a user-space UDP stack - * @param reuse_addr Maybe meaningless for RAW Socket * * @attention can be re-opened after closed */ - int open (ACE_INET_Addr const & local, - int protocol = IPPROTO_UDP, - int reuse_addr = 1); + int open (ACE_INET_Addr const & local, int protocol = IPPROTO_UDP); /// Dump the state of object. void dump () const; diff --git a/ACE/tests/RAW_Socket_Test.cpp b/ACE/tests/RAW_Socket_Test.cpp index 575a3c679181c..2bb95f1fbf206 100644 --- a/ACE/tests/RAW_Socket_Test.cpp +++ b/ACE/tests/RAW_Socket_Test.cpp @@ -166,17 +166,18 @@ run_reopen_test () return 0; } -static int raw_recv_data_until_meet_condition(ACE_RAW_SOCKET& raw, u_short port, size_t n, ACE_INET_Addr& remote, ACE_INET_Addr* to_addr) +static int raw_recv_data_until_meet_condition(ACE_RAW_SOCKET& raw, u_short port, size_t n, ACE_INET_Addr& remote, ACE_INET_Addr* to_addr = nullptr) { ACE_INET_Addr local; raw.get_local_addr(local); ssize_t len = 0; + ssize_t expectedLen; do { - if(to_addr == NULL) + if(to_addr == nullptr) { len = raw.recv(recvbuf, sizeof(recvbuf), remote); } @@ -188,6 +189,7 @@ static int raw_recv_data_until_meet_condition(ACE_RAW_SOCKET& raw, u_short port, if(len < 0) { + ACE_DEBUG ((LM_INFO, "%s IPv6 recv expected pkgs ...\n", __func__)); return -1; } @@ -195,21 +197,29 @@ static int raw_recv_data_until_meet_condition(ACE_RAW_SOCKET& raw, u_short port, if(local.get_type() == AF_INET) { ptUDPHeader = (UDP_HEADER_t_Ptr)(recvbuf + sizeof(IPv4_HEADER_t)); + expectedLen = (n + sizeof(IPv4_HEADER_t) + sizeof(UDP_HEADER_t)); + u_short nDstPort = ntohs(ptUDPHeader->u16DstPort); + + if(port == nDstPort && len == expectedLen) + { + ACE_DEBUG ((LM_INFO, "%s IPv4 recv expected pkgs ...\n", __func__)); + break; + } } else { - ptUDPHeader = (UDP_HEADER_t_Ptr)recvbuf; - } + ptUDPHeader = (UDP_HEADER_t_Ptr)recvbuf; + expectedLen = (n + sizeof(UDP_HEADER_t)); + u_short nDstPort = ntohs(ptUDPHeader->u16DstPort); - u_short nDstPort = ntohs(ptUDPHeader->u16DstPort) ; - if(port == nDstPort && len == (n + sizeof(IPv4_HEADER_t) + sizeof(UDP_HEADER_t))) - { - ACE_DEBUG ((LM_INFO, "%s recv expected pkgs ...\n", __func__)); + if(port == nDstPort && len == expectedLen) + { + ACE_DEBUG ((LM_INFO, "%s IPv6 recv expected pkgs ...\n", __func__)); break; + } } - - ACE_DEBUG ((LM_DEBUG, "%s recv unexpected pkgs ...\n", __func__)); + ACE_DEBUG ((LM_DEBUG, "%s recv unexpected pkgs len: %d, expectedLen: %d ...\n", __func__, len, expectedLen)); ACE_OS::sleep(1); } while (1); @@ -220,7 +230,7 @@ static int run_raw_udp_test_child_flow_sendby_self (ACE_RAW_SOCKET& raw, ACE_INET_Addr& client_addr, ACE_INET_Addr& server_addr, size_t n) { ACE_DEBUG ((LM_INFO, "%s begin to run when sending data by self ...\n", __func__)); - IPv4_HEADER_t_Ptr ptIPv4Header = (IPv4_HEADER_t_Ptr)sendbuf; + UDP_HEADER_t_Ptr ptUDPHeader = (UDP_HEADER_t_Ptr)(sendbuf + sizeof(IPv4_HEADER_t_Ptr)); ptUDPHeader->u16SrcPort = htons(client_addr.get_port_number()); @@ -229,7 +239,8 @@ run_raw_udp_test_child_flow_sendby_self (ACE_RAW_SOCKET& raw, ACE_INET_Addr& cli ptUDPHeader->u16CheckSum = 0; int rc = raw.send(ptUDPHeader, n + sizeof(UDP_HEADER_t), server_addr); - EXCEPTION_RETURN(rc != (n + sizeof(UDP_HEADER_t)), " raw socket can not send test pkg to server\n"); + ssize_t expectedLen = n + sizeof(UDP_HEADER_t); + EXCEPTION_RETURN(rc != expectedLen, " raw socket can not send test pkg to server\n"); u_short server_port = server_addr.get_port_number(); ACE_INET_Addr remote; @@ -264,7 +275,7 @@ run_raw_udp_test () EXCEPTION_RETURN(rc < 0, " can not get server address\n"); - size_t n = 512; + ssize_t n = 512; rc = dgram.send(sendbuf, n, server_addr); EXCEPTION_RETURN(rc != n, " can send test pkg to server\n"); @@ -337,20 +348,25 @@ run_raw_generic_test () if(ACE_OS::getuid() == 0) { + ACE_DEBUG ((LM_INFO, "%s raw generic socket will send bytes exceeding the MTU ...\n", __func__)); n = 2048; - len = rawSocket.send(sendbuf, sizeof(IPv4_HEADER_t) + sizeof(UDP_HEADER_t) + n, remote); + len = rawSocket.send(sendbuf, sizeof(IPv4_HEADER_t) + sizeof(UDP_HEADER_t) + n, server_addr); EXCEPTION_RETURN(len != -1, " raw generic socket can not send pkg more than MTU\n"); } n = 468; ptUDPHeader->u16Length = htons(sizeof(UDP_HEADER_t) + n); - len = rawSocket.send(sendbuf, sizeof(IPv4_HEADER_t) + sizeof(UDP_HEADER_t) + n, remote); - EXCEPTION_RETURN(static_cast(len) != (sizeof(IPv4_HEADER_t) + sizeof(UDP_HEADER_t) + n), " raw generic socket send pkg in failure\n"); + len = rawSocket.send(sendbuf, sizeof(IPv4_HEADER_t) + sizeof(UDP_HEADER_t) + n, server_addr); + size_t expectedLen = (sizeof(IPv4_HEADER_t) + sizeof(UDP_HEADER_t) + n); + ACE_DEBUG ((LM_INFO, "%s raw generic socket send %d bytes, expected %u bytes ...\n", __func__, len, expectedLen)); + EXCEPTION_RETURN(static_cast(len) != expectedLen, " raw generic socket send pkg in failure\n"); ACE_OS::sleep(1); ACE_DEBUG ((LM_INFO, "%s enable nonblock status ...\n", __func__)); server_dgram.enable(ACE_NONBLOCK); len = server_dgram.recv(recvbuf, sizeof(recvbuf), remote); + expectedLen = n; + ACE_DEBUG ((LM_INFO, "%s udp server socket recv %d bytes, expected %u bytes ...\n", __func__, len, expectedLen)); EXCEPTION_RETURN(static_cast(len) != n, " server socket receives pkg in failure length is not the same\n"); EXCEPTION_RETURN(static_cast(remote.get_addr())->sin_addr.s_addr != static_cast(client_addr.get_addr())->sin_addr.s_addr, " server socket receives pkg in failure: the source IP is not the same\n"); @@ -363,11 +379,12 @@ run_ipv6_pkginfo_test () { ACE_DEBUG ((LM_INFO, "%s begin to run IPv6 pkginfo test ...\n", __func__)); - ACE_INET_Addr bindAddr((u_short)0, "::1"), remote; + ACE_INET_Addr bindAddr((u_short)0, "::1"); ACE_INET_Addr anyAddr((u_short)0, "::"); ACE_INET_Addr client_addr((u_short)0, "::1") ,server_addr((u_short)0, "::1"); + ACE_SOCK_Dgram client_dgram(client_addr); SockGuard client_dgram_guard(client_dgram); client_dgram.get_local_addr(client_addr); @@ -376,33 +393,52 @@ run_ipv6_pkginfo_test () SockGuard server_dgram_guard(server_dgram); server_dgram.get_local_addr(server_addr); + ACE_DEBUG ((LM_INFO, "%s get the real bound addr and port client_port: %u, server_port: %u...\n", __func__, client_addr.get_port_number(), server_addr.get_port_number())); + + + ACE_RAW_SOCKET rawSocket(bindAddr, IPPROTO_UDP); rawSocket.enable(ACE_NONBLOCK); SockGuard raw_guard(rawSocket); + ACE_RAW_SOCKET rawWildcardSocket(anyAddr, IPPROTO_UDP); rawWildcardSocket.enable(ACE_NONBLOCK); - SockGuard raw_guard(rawWildcardSocket); + SockGuard raw_wildcard_guard(rawWildcardSocket); client_dgram.send("hello world", sizeof("hello world"), server_addr); + + ACE_DEBUG ((LM_INFO, "%s the send pkg will be received by two raw sockets ...\n", __func__)); ACE_OS::sleep(1); - + ACE_INET_Addr remote; int rc = raw_recv_data_until_meet_condition(rawSocket, server_addr.get_port_number(), sizeof("hello world"), remote); - EXCEPTION_RETURN(rc != 0, " can not recv expectedRecvLen\n"); + EXCEPTION_RETURN(rc != 0, " non wildcard raw socket can not recv expectedRecvLen\n"); ACE_INET_Addr to_addr; - rc = raw_recv_data_until_meet_condition(rawWildcardSocket, server_addr.get_port_number(), sizeof("hello world"), remote, &to_addr); - EXCEPTION_RETURN(rc != 0, " can not recv expectedRecvLen with to_addr when provided to wildcard RAW socket\n"); - EXCEPTION_RETURN(remote != to_addr, " to_addr with invalid value when provided to wildcard RAW socket\n"); - + ACE_DEBUG ((LM_INFO, "%s send pkg again to test common raw socket with to_adr parameter ...\n", __func__)); client_dgram.send("hello world", sizeof("hello world"), server_addr); + ACE_OS::sleep(1); + + int yes = 1; rc = raw_recv_data_until_meet_condition(rawSocket, server_addr.get_port_number(), sizeof("hello world"), remote, &to_addr); - EXCEPTION_RETURN(len != expectedRecvLen, " can not recv expectedRecvLen with to_addr when provided to non wildcard RAW socket\n"); - EXCEPTION_RETURN(remote != to_addr, " to_addr with invalid value when provided to non wildcard RAW socket\n"); + EXCEPTION_RETURN(rc != 0, " non wildcard raw socket can not recv expectedRecvLen with to_addr parameter\n"); + + in6_addr* remote_sin6_addr = &(static_cast(remote.get_addr())->sin6_addr); + in6_addr* to_sin6_addr = &(static_cast(to_addr.get_addr())->sin6_addr); + int cmp = ACE_OS::memcmp(remote_sin6_addr, to_sin6_addr, sizeof(*to_sin6_addr)); + EXCEPTION_RETURN(cmp != 0, " non wildcard raw socket got to_addr with invalid value\n"); + + rc = raw_recv_data_until_meet_condition(rawWildcardSocket, server_addr.get_port_number(), sizeof("hello world"), remote, &to_addr); + EXCEPTION_RETURN(rc != 0, " can not recv expectedRecvLen with to_addr when provided to wildcard RAW socket\n"); + remote_sin6_addr = &(static_cast(remote.get_addr())->sin6_addr); + to_sin6_addr = &(static_cast(to_addr.get_addr())->sin6_addr); + cmp = ACE_OS::memcmp(remote_sin6_addr, to_sin6_addr, sizeof(*to_sin6_addr)); + EXCEPTION_RETURN(cmp != 0, " to_addr with invalid value when provided to wildcard RAW socket\n"); + return 0; } #endif @@ -442,6 +478,8 @@ run_main (int, ACE_TCHAR *argv[]) #if defined (ACE_HAS_IPV6) retval += run_ipv6_pkginfo_test(); + #elif + ACE_DEBUG ((LM_INFO, "%s without IPv6 macro ...\n", __func__)); #endif From 940d1c9128a011d8aac72197c19b1fe29b99681f Mon Sep 17 00:00:00 2001 From: smithAchang Date: Thu, 23 Mar 2023 20:50:21 +0800 Subject: [PATCH 08/39] compatible for centos7 --- ACE/ace/RAW_Socket.h | 10 +++++----- ACE/tests/RAW_Socket_Test.cpp | 23 +++++++++++++---------- 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/ACE/ace/RAW_Socket.h b/ACE/ace/RAW_Socket.h index f0acbd78845f8..c78d12bafb964 100644 --- a/ACE/ace/RAW_Socket.h +++ b/ACE/ace/RAW_Socket.h @@ -70,7 +70,7 @@ class ACE_Export ACE_RAW_SOCKET : public ACE_SOCK, private ACE_Copy_Disabled /** * Wait up to @a timeout amount of time to send a datagram to * @a buf. The ACE_Time_Value indicates how long to blocking - * trying to receive. If @a timeout == nullptr, the caller will block + * trying to receive. If @a timeout == NULL, the caller will block * until action is possible, else will wait until the relative time * specified in *@a timeout elapses). If times out a -1 is * returned with @c errno == ETIME. If it succeeds the number of @@ -80,13 +80,13 @@ class ACE_Export ACE_RAW_SOCKET : public ACE_SOCK, private ACE_Copy_Disabled size_t n, const ACE_INET_Addr &addr, int flags = 0, - const ACE_Time_Value *timeout = nullptr) const; + const ACE_Time_Value *timeout = NULL) const; /** * Wait up to @a timeout amount of time to receive a datagram into * @a buf. The ACE_Time_Value indicates how long to blocking - * trying to receive. If @a timeout == nullptr, the caller will block + * trying to receive. If @a timeout == NULL, the caller will block * until action is possible, else will wait until the relative time * specified in *@a timeout elapses). If times out a -1 is * returned with @c errno == ETIME. If it succeeds the number of @@ -97,8 +97,8 @@ class ACE_Export ACE_RAW_SOCKET : public ACE_SOCK, private ACE_Copy_Disabled size_t n, ACE_INET_Addr &addr, int flags = 0, - const ACE_Time_Value *timeout = nullptr, - ACE_INET_Addr *to_addr = nullptr) const; + const ACE_Time_Value *timeout = NULL, + ACE_INET_Addr *to_addr = NULL) const; //@} diff --git a/ACE/tests/RAW_Socket_Test.cpp b/ACE/tests/RAW_Socket_Test.cpp index 2bb95f1fbf206..df60f48d98fd9 100644 --- a/ACE/tests/RAW_Socket_Test.cpp +++ b/ACE/tests/RAW_Socket_Test.cpp @@ -166,7 +166,7 @@ run_reopen_test () return 0; } -static int raw_recv_data_until_meet_condition(ACE_RAW_SOCKET& raw, u_short port, size_t n, ACE_INET_Addr& remote, ACE_INET_Addr* to_addr = nullptr) +static int raw_recv_data_until_meet_condition(ACE_RAW_SOCKET& raw, u_short port, size_t n, ACE_INET_Addr& remote, ACE_INET_Addr* to_addr = NULL) { ACE_INET_Addr local; raw.get_local_addr(local); @@ -177,13 +177,13 @@ static int raw_recv_data_until_meet_condition(ACE_RAW_SOCKET& raw, u_short port, do { - if(to_addr == nullptr) + if(to_addr == NULL) { len = raw.recv(recvbuf, sizeof(recvbuf), remote); } else { - len = raw.recv(recvbuf, sizeof(recvbuf), remote, 0/*flags*/, nullptr, to_addr); + len = raw.recv(recvbuf, sizeof(recvbuf), remote, 0/*flags*/, NULL, to_addr); } @@ -202,8 +202,8 @@ static int raw_recv_data_until_meet_condition(ACE_RAW_SOCKET& raw, u_short port, if(port == nDstPort && len == expectedLen) { - ACE_DEBUG ((LM_INFO, "%s IPv4 recv expected pkgs ...\n", __func__)); - break; + ACE_DEBUG ((LM_INFO, "%s IPv4 recv expected pkgs ...\n", __func__)); + break; } } else @@ -214,8 +214,8 @@ static int raw_recv_data_until_meet_condition(ACE_RAW_SOCKET& raw, u_short port, if(port == nDstPort && len == expectedLen) { - ACE_DEBUG ((LM_INFO, "%s IPv6 recv expected pkgs ...\n", __func__)); - break; + ACE_DEBUG ((LM_INFO, "%s IPv6 recv expected pkgs ...\n", __func__)); + break; } } @@ -420,8 +420,6 @@ run_ipv6_pkginfo_test () ACE_DEBUG ((LM_INFO, "%s send pkg again to test common raw socket with to_adr parameter ...\n", __func__)); client_dgram.send("hello world", sizeof("hello world"), server_addr); ACE_OS::sleep(1); - - int yes = 1; rc = raw_recv_data_until_meet_condition(rawSocket, server_addr.get_port_number(), sizeof("hello world"), remote, &to_addr); EXCEPTION_RETURN(rc != 0, " non wildcard raw socket can not recv expectedRecvLen with to_addr parameter\n"); @@ -460,6 +458,11 @@ run_main (int, ACE_TCHAR *argv[]) struct ifreq tReq = {}; ACE_OS::snprintf(tReq.ifr_name, sizeof(tReq.ifr_name), "%s", "lo"); + + tReq.ifr_mtu = 0; + ACE_OS::ioctl(netdevice.get_handle(), SIOCGIFMTU, &tReq); + oldMTU = tReq.ifr_mtu; + tReq.ifr_mtu = 1400; ACE_OS::ioctl(netdevice.get_handle(), SIOCSIFMTU, &tReq); @@ -478,7 +481,7 @@ run_main (int, ACE_TCHAR *argv[]) #if defined (ACE_HAS_IPV6) retval += run_ipv6_pkginfo_test(); - #elif + #else ACE_DEBUG ((LM_INFO, "%s without IPv6 macro ...\n", __func__)); #endif From 2b5c0c96a9ffd36b64880a3aef7887418db1ddb7 Mon Sep 17 00:00:00 2001 From: SmithAchang Date: Sat, 25 Mar 2023 21:18:23 +0800 Subject: [PATCH 09/39] win32Compile --- ACE/ace/RAW_Socket.cpp | 32 ++++++++++++++++++++++---------- ACE/tests/RAW_Socket_Test.cpp | 19 ++++++++++++++++--- 2 files changed, 38 insertions(+), 13 deletions(-) diff --git a/ACE/ace/RAW_Socket.cpp b/ACE/ace/RAW_Socket.cpp index 8c75d741d035f..4c7fbe8c5f2a8 100644 --- a/ACE/ace/RAW_Socket.cpp +++ b/ACE/ace/RAW_Socket.cpp @@ -1,15 +1,24 @@ #include "ace/RAW_Socket.h" -#if !defined (__ACE_INLINE__) -#include "ace/RAW_Socket.inl" -#endif /* __ACE_INLINE__ */ - #include "ace/ACE.h" #include "ace/Log_Category.h" #if defined (ACE_HAS_ALLOC_HOOKS) # include "ace/Malloc_Base.h" #endif /* ACE_HAS_ALLOC_HOOKS */ +// Included so users have access to ACE_RECVPKTINFO and ACE_RECVPKTINFO6 . +#include "ace/OS_NS_sys_socket.h" + +#if defined (ACE_HAS_IPV6) && defined (ACE_WIN32) +#include /**/ +#endif + + +#if !defined (__ACE_INLINE__) +#include "ace/RAW_Socket.inl" +#endif /* __ACE_INLINE__ */ + + ACE_BEGIN_VERSIONED_NAMESPACE_DECL ACE_ALLOC_HOOK_DEFINE (ACE_RAW_SOCKET) @@ -45,7 +54,7 @@ static inline ssize_t using_common_recv(const ACE_RAW_SOCKET* raw, void *buf, si sockaddr *saddr = (sockaddr *) addr.get_addr (); int addr_len = addr.get_size (); - ssize_t const status = ACE_OS::recvfrom (raw->get_handle (), + ssize_t status = ACE_OS::recvfrom (raw->get_handle (), (char *) buf, n, flags, @@ -107,7 +116,7 @@ ACE_RAW_SOCKET::recv (void *buf, #endif struct iovec iov; - iov.iov_base = buf; + iov.iov_base = static_cast(buf); iov.iov_len = n; msghdr recv_msg = {}; @@ -221,12 +230,15 @@ ACE_RAW_SOCKET::open (ACE_INET_Addr const & local, int protocol) ACE_INET_Addr bindAddr; this->get_local_addr(bindAddr); - if(bindAddr.is_any()) + +#if defined (ACE_HAS_IPV6) && defined (IPV6_PKTINFO) && defined(IPV6_RECVPKTINFO) + if (bindAddr.is_any()) { - int yes = 1; - this->set_option(IPPROTO_IPV6, IPV6_RECVPKTINFO, &yes, sizeof(yes)); + int yes = 1; + this->set_option(IPPROTO_IPV6, IPV6_RECVPKTINFO, &yes, sizeof(yes)); } - +#endif + return 0; } diff --git a/ACE/tests/RAW_Socket_Test.cpp b/ACE/tests/RAW_Socket_Test.cpp index 2bb95f1fbf206..9d51a3252d72b 100644 --- a/ACE/tests/RAW_Socket_Test.cpp +++ b/ACE/tests/RAW_Socket_Test.cpp @@ -16,7 +16,13 @@ #include "test_config.h" #include "ace/RAW_Socket.h" #include "ace/SOCK_Dgram.h" +#include "ace/OS_NS_unistd.h" +#include "ace/OS_NS_string.h" +#include "ace/OS_NS_stdlib.h" + +#pragma pack(push) +#pragma pack(1) typedef struct _IP_HEADER_t { uint8_t bVersionAndHeaderLen; // 版本信息(前4位),头长度(后4位) @@ -29,7 +35,7 @@ typedef struct _IP_HEADER_t uint16_t u16CheckSum; // 校验和 uint32_t u32SourIp; // 源ip uint32_t u32DestIp; // 目的ip -} __attribute__((packed)) IPv4_HEADER_t, *IPv4_HEADER_t_Ptr; +} IPv4_HEADER_t, *IPv4_HEADER_t_Ptr; typedef struct _IPv6_HEADER_t { @@ -57,7 +63,7 @@ typedef struct _IPv6_HEADER_t uint64_t au64DstAddr[2]; }; -} __attribute__((packed)) IPv6_HEADER_t, *IPv6_HEADER_t_Ptr; +} IPv6_HEADER_t, *IPv6_HEADER_t_Ptr; typedef struct _UDP_HEADER_t { @@ -65,7 +71,9 @@ typedef struct _UDP_HEADER_t uint16_t u16DstPort; uint16_t u16Length; uint16_t u16CheckSum; -} __attribute__((packed)) UDP_HEADER_t, *UDP_HEADER_t_Ptr; +} UDP_HEADER_t, *UDP_HEADER_t_Ptr; + +#pragma pack(pop) class SockGuard : private ACE_Copy_Disabled @@ -451,6 +459,8 @@ run_main (int, ACE_TCHAR *argv[]) int retval = 0; int oldMTU = 1500; + +#if !defined (ACE_WIN32) // set the lo interface MTU if(ACE_OS::getuid() == 0) { @@ -467,6 +477,7 @@ run_main (int, ACE_TCHAR *argv[]) ACE_OS::ioctl(netdevice.get_handle(), SIOCGIFMTU, &tReq); EXCEPTION_RETURN(tReq.ifr_mtu != 1400, " can set MTU for lo interface\n"); } +#endif @@ -485,6 +496,7 @@ run_main (int, ACE_TCHAR *argv[]) ACE_END_TEST; +#if !defined (ACE_WIN32) if(ACE_OS::getuid() == 0) { ACE_INET_Addr anyAddr((u_short)0); @@ -496,6 +508,7 @@ run_main (int, ACE_TCHAR *argv[]) tReq.ifr_mtu = oldMTU; ACE_OS::ioctl(netdevice.get_handle(), SIOCSIFMTU, &tReq); } +#endif return retval; From 07f636edcdf4ddfed04dcad1da243a9a5aadfab8 Mon Sep 17 00:00:00 2001 From: smithAchang Date: Sun, 26 Mar 2023 22:14:03 +0800 Subject: [PATCH 10/39] iovec for raw socket --- ACE/ace/RAW_Socket.cpp | 292 +++++++++++++++++++++++--------- ACE/ace/RAW_Socket.h | 23 ++- ACE/tests/RAW_Socket_Test.cpp | 302 +++++++++++++++++++++++++++++++--- 3 files changed, 509 insertions(+), 108 deletions(-) diff --git a/ACE/ace/RAW_Socket.cpp b/ACE/ace/RAW_Socket.cpp index 4c7fbe8c5f2a8..6c11b9df83418 100644 --- a/ACE/ace/RAW_Socket.cpp +++ b/ACE/ace/RAW_Socket.cpp @@ -15,14 +15,44 @@ #if !defined (__ACE_INLINE__) -#include "ace/RAW_Socket.inl" +# include "ace/RAW_Socket.inl" #endif /* __ACE_INLINE__ */ - ACE_BEGIN_VERSIONED_NAMESPACE_DECL ACE_ALLOC_HOOK_DEFINE (ACE_RAW_SOCKET) +#if defined (ACE_HAS_4_4BSD_SENDMSG_RECVMSG) || defined ACE_WIN32 + #define ACE_USE_MSG_CONTROL + union control_buffer { + cmsghdr control_msg_header; + #if defined (IP_RECVDSTADDR) + u_char padding[ACE_CMSG_SPACE (sizeof (in_addr))]; + #elif defined (IP_PKTINFO) + u_char padding[ACE_CMSG_SPACE (sizeof (in_pktinfo))]; + #endif + #if defined (ACE_HAS_IPV6) + u_char padding6[ACE_CMSG_SPACE (sizeof (in6_pktinfo))]; + #endif + }; +#endif + +#define SEND_EXCEPTION_RETURN() do{\ + if (this->get_handle () == ACE_INVALID_HANDLE)\ + return -1; \ + if(timeout && ACE::handle_write_ready (this->get_handle (), timeout) != 1)\ + return -1; \ +}while(0) + +#define RECV_EXCEPTION_RETURN() do{\ + if (this->get_handle () == ACE_INVALID_HANDLE)\ + return -1; \ + if(this->is_send_only())\ + return -1; \ + if(timeout && ACE::handle_read_ready (this->get_handle (), timeout) != 1)\ + return -1; \ +}while(0) + void ACE_RAW_SOCKET::dump () const @@ -66,6 +96,68 @@ static inline ssize_t using_common_recv(const ACE_RAW_SOCKET* raw, void *buf, si return status; } +static inline void fillMsgHdr(msghdr& recv_msg, ACE_INET_Addr &addr, void* pcbuf, size_t cbuf_size) +{ + #if defined (ACE_HAS_SOCKADDR_MSG_NAME) + recv_msg.msg_name = (struct sockaddr *) addr.get_addr (); + #else + recv_msg.msg_name = (char *) addr.get_addr (); + #endif /* ACE_HAS_SOCKADDR_MSG_NAME */ + + recv_msg.msg_namelen = addr.get_size (); + + #ifdef ACE_USE_MSG_CONTROL + recv_msg.msg_control = pcbuf; + recv_msg.msg_controllen = cbuf_size; + #elif !defined ACE_LACKS_SENDMSG + recv_msg.msg_accrights = 0; + recv_msg.msg_accrightslen = 0; + #endif + +} + +static inline void getToAddrFromMsgHdr(msghdr& recv_msg, ACE_INET_Addr& to_addr) +{ +#ifdef ACE_USE_MSG_CONTROL + if (to_addr.get_type() == AF_INET) { + #if defined (IP_RECVDSTADDR) || defined (IP_PKTINFO) + for (cmsghdr *ptr = ACE_CMSG_FIRSTHDR (&recv_msg); ptr; ptr = ACE_CMSG_NXTHDR (&recv_msg, ptr)) { + #if defined (IP_RECVDSTADDR) + if (ptr->cmsg_level == IPPROTO_IP && ptr->cmsg_type == IP_RECVDSTADDR) { + to_addr->set_address ((const char *) (ACE_CMSG_DATA (ptr)), + sizeof (struct in_addr), + 0); + break; + } + #else + if (ptr->cmsg_level == IPPROTO_IP && ptr->cmsg_type == IP_PKTINFO) { + to_addr.set_address ((const char *) &(((in_pktinfo *) (ACE_CMSG_DATA (ptr)))->ipi_addr), + sizeof (struct in_addr), + 0); + break; + } + #endif + } + #endif + } + + #if defined (ACE_HAS_IPV6) && defined (IPV6_PKTINFO) + else if (to_addr.get_type() == AF_INET6) { + for (cmsghdr *ptr = ACE_CMSG_FIRSTHDR (&recv_msg); ptr; ptr = ACE_CMSG_NXTHDR (&recv_msg, ptr)) { + if (ptr->cmsg_level == IPPROTO_IPV6 && ptr->cmsg_type == IPV6_PKTINFO) { + to_addr.set_address ((const char *) &(((in6_pktinfo *)(ACE_CMSG_DATA (ptr)))->ipi6_addr), + sizeof (struct in6_addr), + 0); + + break; + } + } + } + #endif +#endif + +} + ssize_t ACE_RAW_SOCKET::recv (void *buf, size_t n, @@ -74,16 +166,9 @@ ACE_RAW_SOCKET::recv (void *buf, const ACE_Time_Value *timeout, ACE_INET_Addr *to_addr) const { + ACE_TRACE ("ACE_RAW_SOCKET::recv"); - if (this->get_handle () == ACE_INVALID_HANDLE) - return -1; - - if(this->is_send_only()) - return -1; - - if(timeout && ACE::handle_read_ready (this->get_handle (), timeout) != 1) - return -1; - + RECV_EXCEPTION_RETURN(); if(to_addr == NULL) { @@ -99,22 +184,6 @@ ACE_RAW_SOCKET::recv (void *buf, } - - #if defined (ACE_HAS_4_4BSD_SENDMSG_RECVMSG) || defined ACE_WIN32 - #define ACE_USE_MSG_CONTROL - union control_buffer { - cmsghdr control_msg_header; - #if defined (IP_RECVDSTADDR) - u_char padding[ACE_CMSG_SPACE (sizeof (in_addr))]; - #elif defined (IP_PKTINFO) - u_char padding[ACE_CMSG_SPACE (sizeof (in_pktinfo))]; - #endif - #if defined (ACE_HAS_IPV6) - u_char padding6[ACE_CMSG_SPACE (sizeof (in6_pktinfo))]; - #endif - } cbuf; - #endif - struct iovec iov; iov.iov_base = static_cast(buf); iov.iov_len = n; @@ -123,19 +192,18 @@ ACE_RAW_SOCKET::recv (void *buf, recv_msg.msg_iov = (iovec*)&iov; recv_msg.msg_iovlen = 1; - #if defined (ACE_HAS_SOCKADDR_MSG_NAME) - recv_msg.msg_name = (struct sockaddr *) addr.get_addr (); - #else - recv_msg.msg_name = (char *) addr.get_addr (); - #endif /* ACE_HAS_SOCKADDR_MSG_NAME */ - recv_msg.msg_namelen = addr.get_size (); - #ifdef ACE_USE_MSG_CONTROL - recv_msg.msg_control = &cbuf; - recv_msg.msg_controllen = sizeof (cbuf); - #elif !defined ACE_LACKS_SENDMSG - recv_msg.msg_accrights = 0; - recv_msg.msg_accrightslen = 0; + union control_buffer cbuf; + if(to_addr != NULL) + { + fillMsgHdr(recv_msg, addr, &cbuf, sizeof(cbuf)); + } + else + { + fillMsgHdr(recv_msg, addr, NULL, 0); + } + #else + fillMsgHdr(recv_msg, addr, NULL, 0); #endif ssize_t status = ACE_OS::recvmsg (this->get_handle (), @@ -145,44 +213,57 @@ ACE_RAW_SOCKET::recv (void *buf, addr.set_size (recv_msg.msg_namelen); addr.set_type (((sockaddr_in *) addr.get_addr())->sin_family); - #ifdef ACE_USE_MSG_CONTROL - if (to_addr->get_type() == AF_INET) { - #if defined (IP_RECVDSTADDR) || defined (IP_PKTINFO) - for (cmsghdr *ptr = ACE_CMSG_FIRSTHDR (&recv_msg); ptr; ptr = ACE_CMSG_NXTHDR (&recv_msg, ptr)) { - #if defined (IP_RECVDSTADDR) - if (ptr->cmsg_level == IPPROTO_IP && ptr->cmsg_type == IP_RECVDSTADDR) { - to_addr->set_address ((const char *) (ACE_CMSG_DATA (ptr)), - sizeof (struct in_addr), - 0); - break; - } - #else - if (ptr->cmsg_level == IPPROTO_IP && ptr->cmsg_type == IP_PKTINFO) { - to_addr->set_address ((const char *) &(((in_pktinfo *) (ACE_CMSG_DATA (ptr)))->ipi_addr), - sizeof (struct in_addr), - 0); - break; - } + if(to_addr != NULL) + getToAddrFromMsgHdr(recv_msg, *to_addr); #endif - } - #endif - } - #if defined (ACE_HAS_IPV6) && defined (IPV6_PKTINFO) - else if (to_addr->get_type() == AF_INET6) { - for (cmsghdr *ptr = ACE_CMSG_FIRSTHDR (&recv_msg); ptr; ptr = ACE_CMSG_NXTHDR (&recv_msg, ptr)) { - if (ptr->cmsg_level == IPPROTO_IPV6 && ptr->cmsg_type == IPV6_PKTINFO) { - to_addr->set_address ((const char *) &(((in6_pktinfo *)(ACE_CMSG_DATA (ptr)))->ipi6_addr), - sizeof (struct in6_addr), - 0); + + return status; +} - break; - } - } - } - #endif + +ssize_t +ACE_RAW_SOCKET::recv (iovec iov[], + size_t n, + ACE_INET_Addr &addr, + int flags, + const ACE_Time_Value *timeout, + ACE_INET_Addr *to_addr) const +{ + ACE_TRACE ("ACE_RAW_SOCKET::recv"); + + RECV_EXCEPTION_RETURN(); + + + msghdr recv_msg = {}; + recv_msg.msg_iov = (struct iovec *)iov; + recv_msg.msg_iovlen = n; + + /*default*/ + fillMsgHdr(recv_msg, addr, NULL, 0); + + #ifdef ACE_USE_MSG_CONTROL + union control_buffer cbuf; + if(to_addr != NULL) + { + this->get_local_addr(*to_addr); + + fillMsgHdr(recv_msg, addr, &cbuf, sizeof(cbuf)); + } #endif + ssize_t status = ACE_OS::recvmsg (this->get_handle (), + &recv_msg, + flags); + + addr.set_size (recv_msg.msg_namelen); + addr.set_type (((sockaddr_in *) addr.get_addr())->sin_family); + + #ifdef ACE_USE_MSG_CONTROL + if(to_addr != NULL) + getToAddrFromMsgHdr(recv_msg, *to_addr); + #endif + return status; } @@ -193,12 +274,10 @@ ACE_RAW_SOCKET::send (const void *buf, int flags, const ACE_Time_Value *timeout) const { + ACE_TRACE ("ACE_RAW_SOCKET::send"); + // Check the status of the current socket. - if (this->get_handle () == ACE_INVALID_HANDLE) - return -1; - - if(timeout && ACE::handle_write_ready (this->get_handle (), timeout) != 1) - return -1; + SEND_EXCEPTION_RETURN(); sockaddr *saddr = (sockaddr *) addr.get_addr (); int len = addr.get_size (); @@ -211,6 +290,48 @@ ACE_RAW_SOCKET::send (const void *buf, } +ssize_t +ACE_RAW_SOCKET::send (const iovec iov[], + size_t n, + const ACE_INET_Addr &addr, + int flags, + const ACE_Time_Value *timeout) const +{ + ACE_TRACE ("ACE_RAW_SOCKET::send"); + + // Check the status of the current socket. + SEND_EXCEPTION_RETURN(); + + msghdr send_msg = {}; + + send_msg.msg_iov = (iovec *) iov; + send_msg.msg_iovlen = n; + + #if defined (ACE_HAS_SOCKADDR_MSG_NAME) + send_msg.msg_name = (struct sockaddr *) addr.get_addr (); + #else + send_msg.msg_name = (char *) addr.get_addr (); + #endif /* ACE_HAS_SOCKADDR_MSG_NAME */ + send_msg.msg_namelen = addr.get_size (); + + #if defined (ACE_HAS_4_4BSD_SENDMSG_RECVMSG) + send_msg.msg_control = 0; + send_msg.msg_controllen = 0; + send_msg.msg_flags = 0; + #elif !defined ACE_LACKS_SENDMSG + send_msg.msg_accrights = 0; + send_msg.msg_accrightslen = 0; + #endif /* ACE_HAS_4_4BSD_SENDMSG_RECVMSG */ + + #ifdef ACE_WIN32 + send_msg.msg_control = 0; + send_msg.msg_controllen = 0; + #endif + + return ACE_OS::sendmsg (this->get_handle (), &send_msg, flags); + +} + int ACE_RAW_SOCKET::open (ACE_INET_Addr const & local, int protocol) { @@ -231,13 +352,22 @@ ACE_RAW_SOCKET::open (ACE_INET_Addr const & local, int protocol) ACE_INET_Addr bindAddr; this->get_local_addr(bindAddr); -#if defined (ACE_HAS_IPV6) && defined (IPV6_PKTINFO) && defined(IPV6_RECVPKTINFO) - if (bindAddr.is_any()) + #if defined (ACE_HAS_IPV6) && defined (IPV6_PKTINFO) && defined(IPV6_RECVPKTINFO) + if (bindAddr.get_type() == PF_INET6 && bindAddr.is_any()) { int yes = 1; - this->set_option(IPPROTO_IPV6, IPV6_RECVPKTINFO, &yes, sizeof(yes)); + this->set_option(IPPROTO_IPV6, ACE_RECVPKTINFO6, &yes, sizeof(yes)); } -#endif + #endif + + #if defined (IP_RECVDSTADDR) || defined (IP_PKTINFO) + if (bindAddr.get_type() == PF_INET && bindAddr.is_any()) + { + int yes = 1; + this->set_option(IPPROTO_IP, ACE_RECVPKTINFO, &yes, sizeof(yes)); + } + #endif + return 0; } diff --git a/ACE/ace/RAW_Socket.h b/ACE/ace/RAW_Socket.h index c78d12bafb964..6bbb50d3b1ccf 100644 --- a/ACE/ace/RAW_Socket.h +++ b/ACE/ace/RAW_Socket.h @@ -76,7 +76,7 @@ class ACE_Export ACE_RAW_SOCKET : public ACE_SOCK, private ACE_Copy_Disabled * returned with @c errno == ETIME. If it succeeds the number of * bytes sent is returned. */ - ssize_t send (const void *buf, +ssize_t send (const void *buf, size_t n, const ACE_INET_Addr &addr, int flags = 0, @@ -91,7 +91,7 @@ class ACE_Export ACE_RAW_SOCKET : public ACE_SOCK, private ACE_Copy_Disabled * specified in *@a timeout elapses). If times out a -1 is * returned with @c errno == ETIME. If it succeeds the number of * bytes received is returned. - * When the RAW socket is bound to wildcard address, @a to_addr can get the destination info + * The IP destination address will be placed in @a *to_addr if it is not null */ ssize_t recv (void *buf, size_t n, @@ -100,6 +100,25 @@ class ACE_Export ACE_RAW_SOCKET : public ACE_SOCK, private ACE_Copy_Disabled const ACE_Time_Value *timeout = NULL, ACE_INET_Addr *to_addr = NULL) const; +/** + * Send an of size @a n to the datagram socket (uses) + * The IP destination address will be placed in @a *to_addr if it is not null +*/ +ssize_t send (const iovec iov[], + size_t n, + const ACE_INET_Addr &addr, + int flags = 0, + const ACE_Time_Value *timeout = NULL) const; +/** + * Recv an of size @a n to the datagram socket (uses ). + * The IP destination address will be placed in @a *to_addr if it is not null. + */ +ssize_t recv (iovec iov[], + size_t n, + ACE_INET_Addr &addr, + int flags = 0, + const ACE_Time_Value *timeout = NULL, + ACE_INET_Addr *to_addr = NULL) const; //@} diff --git a/ACE/tests/RAW_Socket_Test.cpp b/ACE/tests/RAW_Socket_Test.cpp index aa9c05ac70fcc..34507eba84c5c 100644 --- a/ACE/tests/RAW_Socket_Test.cpp +++ b/ACE/tests/RAW_Socket_Test.cpp @@ -25,29 +25,29 @@ #pragma pack(1) typedef struct _IP_HEADER_t { - uint8_t bVersionAndHeaderLen; // 版本信息(前4位),头长度(后4位) - uint8_t bTypeOfService; // 服务类型8位 - uint16_t u16TotalLenOfPacket; // 数据包长度 - uint16_t u16PacketID; // 数据包标识 - uint16_t u16Sliceinfo; // 分片使用 - uint8_t bTTL; // 存活时间 - uint8_t bTypeOfProtocol; // 协议类型 - uint16_t u16CheckSum; // 校验和 - uint32_t u32SourIp; // 源ip - uint32_t u32DestIp; // 目的ip + uint8_t bVersionAndHeaderLen; + uint8_t bTypeOfService; + uint16_t u16TotalLenOfPacket; + uint16_t u16PacketID; + uint16_t u16Sliceinfo; + uint8_t bTTL; + uint8_t bTypeOfProtocol; + uint16_t u16CheckSum; + uint32_t u32SourIp; + uint32_t u32DestIp; } IPv4_HEADER_t, *IPv4_HEADER_t_Ptr; typedef struct _IPv6_HEADER_t { union { - uint8_t abVTF[4]; // 版本信息 most top 4 bits , traffic 8 bits - uint32_t dwVTF; // flow label lefted 20 bits; + uint8_t abVTF[4]; + uint32_t dwVTF; }; - uint16_t u16PayloadLen; // 数据包长度 - uint8_t bNextHeader; // 数据包标识 - uint8_t bHopLimit; // max hop limit + uint16_t u16PayloadLen; + uint8_t bNextHeader; + uint8_t bHopLimit; union { @@ -174,30 +174,71 @@ run_reopen_test () return 0; } -static int raw_recv_data_until_meet_condition(ACE_RAW_SOCKET& raw, u_short port, size_t n, ACE_INET_Addr& remote, ACE_INET_Addr* to_addr = NULL) +static void readUdpSocektToEmpty(ACE_SOCK_Dgram& udpSock) +{ + ACE_INET_Addr remote; + while(1) + { + int rc = udpSock.recv(recvbuf, sizeof(recvbuf),remote); + if(rc == -1) + break; + } + +} + +static int raw_recv_data_until_meet_condition(ACE_RAW_SOCKET& raw, u_short port, size_t n, ACE_INET_Addr& remote, bool bUseIOVec = false, ACE_INET_Addr* to_addr = NULL) { ACE_INET_Addr local; raw.get_local_addr(local); ssize_t len = 0; ssize_t expectedLen; - + do { - - if(to_addr == NULL) + ACE_OS::memset(recvbuf, 0, sizeof(recvbuf)); + + if(bUseIOVec) { - len = raw.recv(recvbuf, sizeof(recvbuf), remote); + iovec vec[5]; + unsigned int i=0; + const unsigned int oneByteRecvVecNum = (sizeof(vec)/sizeof(vec[0])) - 1; + for(; i< oneByteRecvVecNum; ++i) + { + vec[i].iov_base = &recvbuf[i]; + vec[i].iov_len = 1; + } + + vec[i].iov_base = &recvbuf[i]; + vec[i].iov_len = sizeof(recvbuf) - oneByteRecvVecNum; + + if(to_addr == NULL) + { + raw.recv(vec, (sizeof(vec)/sizeof(vec[0])) , remote); + } + else + { + len = raw.recv(vec, (sizeof(vec)/sizeof(vec[0])), remote, 0/*flags*/, NULL, to_addr); + } } else { - len = raw.recv(recvbuf, sizeof(recvbuf), remote, 0/*flags*/, NULL, to_addr); + if(to_addr == NULL) + { + len = raw.recv(recvbuf, sizeof(recvbuf), remote); + } + else + { + len = raw.recv(recvbuf, sizeof(recvbuf), remote, 0/*flags*/, NULL, to_addr); + } + } + if(len < 0) { - ACE_DEBUG ((LM_INFO, "%s IPv6 recv expected pkgs ...\n", __func__)); + ACE_DEBUG ((LM_INFO, "%s receive prcess reach the end ...\n", __func__)); return -1; } @@ -429,7 +470,7 @@ run_ipv6_pkginfo_test () client_dgram.send("hello world", sizeof("hello world"), server_addr); ACE_OS::sleep(1); - rc = raw_recv_data_until_meet_condition(rawSocket, server_addr.get_port_number(), sizeof("hello world"), remote, &to_addr); + rc = raw_recv_data_until_meet_condition(rawSocket, server_addr.get_port_number(), sizeof("hello world"), remote, false, &to_addr); EXCEPTION_RETURN(rc != 0, " non wildcard raw socket can not recv expectedRecvLen with to_addr parameter\n"); in6_addr* remote_sin6_addr = &(static_cast(remote.get_addr())->sin6_addr); @@ -438,17 +479,225 @@ run_ipv6_pkginfo_test () EXCEPTION_RETURN(cmp != 0, " non wildcard raw socket got to_addr with invalid value\n"); - rc = raw_recv_data_until_meet_condition(rawWildcardSocket, server_addr.get_port_number(), sizeof("hello world"), remote, &to_addr); + rc = raw_recv_data_until_meet_condition(rawWildcardSocket, server_addr.get_port_number(), sizeof("hello world"), remote, false, &to_addr); EXCEPTION_RETURN(rc != 0, " can not recv expectedRecvLen with to_addr when provided to wildcard RAW socket\n"); remote_sin6_addr = &(static_cast(remote.get_addr())->sin6_addr); to_sin6_addr = &(static_cast(to_addr.get_addr())->sin6_addr); cmp = ACE_OS::memcmp(remote_sin6_addr, to_sin6_addr, sizeof(*to_sin6_addr)); EXCEPTION_RETURN(cmp != 0, " to_addr with invalid value when provided to wildcard RAW socket\n"); - + + return 0; } #endif +static int +run_iovec_IPv6_api_test () +{ + ACE_DEBUG ((LM_INFO, "%s begin to run IPv6 iovec api test ...\n", __func__)); + + ACE_INET_Addr bindAddr((u_short)0, "::1"); + ACE_INET_Addr anyAddr((u_short)0, "::"); + + ACE_INET_Addr client_addr((u_short)0, "::1") ,server_addr((u_short)0, "::1"); + + + ACE_SOCK_Dgram client_dgram(client_addr); + SockGuard client_dgram_guard(client_dgram); + client_dgram.get_local_addr(client_addr); + + ACE_SOCK_Dgram server_dgram(server_addr); + server_dgram.enable(ACE_NONBLOCK); + SockGuard server_dgram_guard(server_dgram); + server_dgram.get_local_addr(server_addr); + + ACE_DEBUG ((LM_INFO, "%s get the real bound addr and port client_port: %u, server_port: %u...\n", __func__, client_addr.get_port_number(), server_addr.get_port_number())); + + + + ACE_RAW_SOCKET rawSocket(bindAddr, IPPROTO_UDP); + rawSocket.enable(ACE_NONBLOCK); + SockGuard raw_guard(rawSocket); + + client_dgram.send("hello world", sizeof("hello world"), server_addr); + + ACE_DEBUG ((LM_INFO, "%s the send pkg will be received by raw sockets ...\n", __func__)); + ACE_OS::sleep(1); + + ACE_INET_Addr remote; + int rc = raw_recv_data_until_meet_condition(rawSocket, server_addr.get_port_number(), sizeof("hello world"), remote); + + EXCEPTION_RETURN(rc != 0, " raw socket can not recv expectedRecvLen\n"); + EXCEPTION_RETURN(ACE_OS::strncmp(recvbuf + sizeof(UDP_HEADER_t), "hello world", sizeof("hello world")) != 0, " non wildcard raw socket can not recv expected content\n"); + + ACE_INET_Addr to_addr; + + ACE_DEBUG ((LM_INFO, "%s send pkg again to test common raw socket with to_adr parameter ...\n", __func__)); + client_dgram.send("hello world", sizeof("hello world"), server_addr); + ACE_OS::sleep(1); + + rc = raw_recv_data_until_meet_condition(rawSocket, server_addr.get_port_number(), sizeof("hello world"), remote, true, &to_addr); + EXCEPTION_RETURN(rc != 0, " raw socket can not recv expectedRecvLen with to_addr parameter\n"); + EXCEPTION_RETURN(ACE_OS::strncmp(recvbuf + sizeof(UDP_HEADER_t), "hello world", sizeof("hello world")) != 0, " raw socket can not recv expected content with to_addr parameter\n"); + + in6_addr* remote_sin6_addr = &(static_cast(remote.get_addr())->sin6_addr); + in6_addr* to_sin6_addr = &(static_cast(to_addr.get_addr())->sin6_addr); + int cmp = ACE_OS::memcmp(remote_sin6_addr, to_sin6_addr, sizeof(*to_sin6_addr)); + EXCEPTION_RETURN(cmp != 0, " raw socket got to_addr with invalid value\n"); + + ACE_DEBUG ((LM_INFO, "%s test iovec send ...\n", __func__)); + + + IPv4_HEADER_t_Ptr ptIPv4Header = (IPv4_HEADER_t_Ptr)sendbuf; + UDP_HEADER_t_Ptr ptUDPHeader = (UDP_HEADER_t_Ptr)(sendbuf + sizeof(IPv4_HEADER_t)); + u_short n = sizeof("hello world"); + + *ptIPv4Header = {}; + + ptIPv4Header->bVersionAndHeaderLen = 0x45; + ptIPv4Header->u16TotalLenOfPacket = htons(sizeof(IPv4_HEADER_t) + sizeof(UDP_HEADER_t) + n); // 数据包长度 + ptIPv4Header->u16PacketID = ACE_OS::rand(); + ptIPv4Header->bTTL = 64; + ptIPv4Header->bTypeOfProtocol = IPPROTO_UDP; + ptIPv4Header->u16CheckSum = 0; + ptIPv4Header->u32SourIp = (static_cast(client_addr.get_addr()))->sin_addr.s_addr; + ptIPv4Header->u32DestIp = (static_cast(server_addr.get_addr()))->sin_addr.s_addr; + + u_short client_port_number = client_addr.get_port_number(); + u_short server_port_number = server_addr.get_port_number(); + + + ptUDPHeader->u16SrcPort = htons(client_port_number); + ptUDPHeader->u16DstPort = htons(server_port_number); + ptUDPHeader->u16Length = htons(sizeof(UDP_HEADER_t) + n); + ptUDPHeader->u16CheckSum = 0; + + + iovec iov_udp[2]; + iov_udp[0].iov_base = ptUDPHeader; + iov_udp[0].iov_len = sizeof(UDP_HEADER_t); + iov_udp[1].iov_base = (void*)"hello world"; + iov_udp[1].iov_len = sizeof("hello world"); + + ACE_DEBUG ((LM_INFO, "%s test iovec using common udp6 socket ...\n", __func__)); + + rc = client_dgram.send(iov_udp, (int)(sizeof(iov_udp)/sizeof(iov_udp[0])), server_addr); + EXCEPTION_RETURN(rc == -1, " udp6 socket can not send using iov \n"); + + readUdpSocektToEmpty(server_dgram); + ACE_INET_Addr iov_server_addr(server_addr); + + ACE_DEBUG ((LM_INFO, "%s must set port to zero ??? ...\n", __func__)); + iov_server_addr.set_port_number(0); + rc = rawSocket.send(iov_udp, sizeof(iov_udp)/sizeof(iov_udp[0]), iov_server_addr); + EXCEPTION_RETURN(rc == -1, " raw6 socket can not send using iov \n"); + ACE_OS::sleep(1); + + rc = server_dgram.recv(recvbuf, sizeof(recvbuf), remote); + EXCEPTION_RETURN(rc == -1, " server socket6 can not recv pkg by iov send\n"); + EXCEPTION_RETURN(ACE_OS::strcmp(recvbuf, "hello world") != 0, " the content of server socket6 receive pkg is not expected by iov send\n"); + return 0; +} + +static int +run_iovec_IPv4_api_test () +{ + ACE_DEBUG ((LM_INFO, "%s begin to run IPv4 iovec api test ...\n", __func__)); + + ACE_INET_Addr bindAddr((u_short)0, "127.0.0.1"); + + ACE_INET_Addr client_addr((u_short)0, "127.0.0.1") ,server_addr((u_short)0, "127.0.0.1"); + + + ACE_SOCK_Dgram client_dgram(client_addr); + SockGuard client_dgram_guard(client_dgram); + client_dgram.get_local_addr(client_addr); + + ACE_SOCK_Dgram server_dgram(server_addr); + server_dgram.enable(ACE_NONBLOCK); + SockGuard server_dgram_guard(server_dgram); + server_dgram.get_local_addr(server_addr); + + ACE_DEBUG ((LM_INFO, "%s get the real bound addr and port client_port: %u, server_port: %u...\n", __func__, client_addr.get_port_number(), server_addr.get_port_number())); + + + + ACE_RAW_SOCKET rawSocket(bindAddr, IPPROTO_UDP); + rawSocket.enable(ACE_NONBLOCK); + SockGuard raw_guard(rawSocket); + + client_dgram.send("hello world", sizeof("hello world"), server_addr); + + ACE_DEBUG ((LM_INFO, "%s the send pkg will be received by raw sockets ...\n", __func__)); + ACE_OS::sleep(1); + + ACE_INET_Addr remote; + int rc = raw_recv_data_until_meet_condition(rawSocket, server_addr.get_port_number(), sizeof("hello world"), remote); + + EXCEPTION_RETURN(rc != 0, " raw socket can not recv expectedRecvLen\n"); + EXCEPTION_RETURN(ACE_OS::strncmp(recvbuf + sizeof(IPv4_HEADER_t) + sizeof(UDP_HEADER_t), "hello world", sizeof("hello world")) != 0, " non wildcard raw socket can not recv expected content\n"); + + ACE_INET_Addr to_addr; + + ACE_DEBUG ((LM_INFO, "%s send pkg again to test common raw socket with to_adr parameter ...\n", __func__)); + client_dgram.send("hello world", sizeof("hello world"), server_addr); + ACE_OS::sleep(1); + + rc = raw_recv_data_until_meet_condition(rawSocket, server_addr.get_port_number(), sizeof("hello world"), remote, true, &to_addr); + EXCEPTION_RETURN(rc != 0, " raw socket can not recv expectedRecvLen with to_addr parameter\n"); + EXCEPTION_RETURN(ACE_OS::strncmp(recvbuf + sizeof(IPv4_HEADER_t) + sizeof(UDP_HEADER_t), "hello world", sizeof("hello world")) != 0, " raw socket can not recv expected content with to_addr parameter\n"); + + in_addr* remote_sin_addr = &(static_cast(remote.get_addr())->sin_addr); + in_addr* to_sin_addr = &(static_cast(to_addr.get_addr())->sin_addr); + int cmp = ACE_OS::memcmp(remote_sin_addr, to_sin_addr, sizeof(*to_sin_addr)); + EXCEPTION_RETURN(cmp != 0, " raw socket got to_addr with invalid value\n"); + + ACE_DEBUG ((LM_INFO, "%s test iovec send ...\n", __func__)); + readUdpSocektToEmpty(server_dgram); + + IPv4_HEADER_t_Ptr ptIPv4Header = (IPv4_HEADER_t_Ptr)sendbuf; + UDP_HEADER_t_Ptr ptUDPHeader = (UDP_HEADER_t_Ptr)(sendbuf + sizeof(IPv4_HEADER_t)); + u_short n = sizeof("hello world"); + + *ptIPv4Header = {}; + + ptIPv4Header->bVersionAndHeaderLen = 0x45; + ptIPv4Header->u16TotalLenOfPacket = htons(sizeof(IPv4_HEADER_t) + sizeof(UDP_HEADER_t) + n); // 数据包长度 + ptIPv4Header->u16PacketID = ACE_OS::rand(); + ptIPv4Header->bTTL = 64; + ptIPv4Header->bTypeOfProtocol = IPPROTO_UDP; + ptIPv4Header->u16CheckSum = 0; + ptIPv4Header->u32SourIp = (static_cast(client_addr.get_addr()))->sin_addr.s_addr; + ptIPv4Header->u32DestIp = (static_cast(server_addr.get_addr()))->sin_addr.s_addr; + + u_short client_port_number = client_addr.get_port_number(); + u_short server_port_number = server_addr.get_port_number(); + + + ptUDPHeader->u16SrcPort = htons(client_port_number); + ptUDPHeader->u16DstPort = htons(server_port_number); + ptUDPHeader->u16Length = htons(sizeof(UDP_HEADER_t) + n); + ptUDPHeader->u16CheckSum = 0; + ACE_OS::strcpy(sendbuf + sizeof(IPv4_HEADER_t) + sizeof(UDP_HEADER_t), "hello world"); + + iovec iov_udp[2]; + iov_udp[0].iov_base = ptUDPHeader; + iov_udp[0].iov_len = sizeof(UDP_HEADER_t); + iov_udp[1].iov_base = (void*)"hello world"; + iov_udp[1].iov_len = sizeof("hello world"); + + rc = rawSocket.send(iov_udp, sizeof(iov_udp)/sizeof(iov_udp[0]), server_addr); + EXCEPTION_RETURN(rc == -1, " raw4 socket can send using iov\n"); + ACE_OS::sleep(1); + + rc = server_dgram.recv(recvbuf, sizeof(recvbuf), remote); + EXCEPTION_RETURN(rc == -1, " server socket4 can not recv pkg by iov send\n"); + EXCEPTION_RETURN(ACE_OS::strcmp(recvbuf, "hello world") != 0, " the content of server socket4 receive pkg is not expected by iov send\n"); + + + return 0; +} + int run_main (int, ACE_TCHAR *argv[]) { @@ -489,6 +738,9 @@ run_main (int, ACE_TCHAR *argv[]) retval += run_reopen_test(); retval += run_raw_udp_test(); retval += run_raw_generic_test(); + retval += run_iovec_IPv4_api_test(); + retval += run_iovec_IPv6_api_test(); + #if defined (ACE_HAS_IPV6) retval += run_ipv6_pkginfo_test(); From e2b61f31dcdb698b0b65da46f98fcab29ef47df4 Mon Sep 17 00:00:00 2001 From: smithAchang Date: Mon, 27 Mar 2023 22:56:00 +0800 Subject: [PATCH 11/39] ipv6 iovec test --- ACE/ace/RAW_Socket.cpp | 131 ++++++++++++++++++++++------------ ACE/tests/RAW_Socket_Test.cpp | 99 +++++++++++++++++++++---- 2 files changed, 169 insertions(+), 61 deletions(-) diff --git a/ACE/ace/RAW_Socket.cpp b/ACE/ace/RAW_Socket.cpp index 6c11b9df83418..99a1c58ed1d6d 100644 --- a/ACE/ace/RAW_Socket.cpp +++ b/ACE/ace/RAW_Socket.cpp @@ -222,51 +222,6 @@ ACE_RAW_SOCKET::recv (void *buf, } -ssize_t -ACE_RAW_SOCKET::recv (iovec iov[], - size_t n, - ACE_INET_Addr &addr, - int flags, - const ACE_Time_Value *timeout, - ACE_INET_Addr *to_addr) const -{ - ACE_TRACE ("ACE_RAW_SOCKET::recv"); - - RECV_EXCEPTION_RETURN(); - - - msghdr recv_msg = {}; - recv_msg.msg_iov = (struct iovec *)iov; - recv_msg.msg_iovlen = n; - - /*default*/ - fillMsgHdr(recv_msg, addr, NULL, 0); - - #ifdef ACE_USE_MSG_CONTROL - union control_buffer cbuf; - if(to_addr != NULL) - { - this->get_local_addr(*to_addr); - - fillMsgHdr(recv_msg, addr, &cbuf, sizeof(cbuf)); - } - #endif - - ssize_t status = ACE_OS::recvmsg (this->get_handle (), - &recv_msg, - flags); - - addr.set_size (recv_msg.msg_namelen); - addr.set_type (((sockaddr_in *) addr.get_addr())->sin_family); - - #ifdef ACE_USE_MSG_CONTROL - if(to_addr != NULL) - getToAddrFromMsgHdr(recv_msg, *to_addr); - #endif - - return status; -} - ssize_t ACE_RAW_SOCKET::send (const void *buf, size_t n, @@ -290,6 +245,7 @@ ACE_RAW_SOCKET::send (const void *buf, } +#if defined (ACE_HAS_MSG) ssize_t ACE_RAW_SOCKET::send (const iovec iov[], size_t n, @@ -332,6 +288,85 @@ ACE_RAW_SOCKET::send (const iovec iov[], } + +ssize_t +ACE_RAW_SOCKET::recv (iovec iov[], + size_t n, + ACE_INET_Addr &addr, + int flags, + const ACE_Time_Value *timeout, + ACE_INET_Addr *to_addr) const +{ + ACE_TRACE ("ACE_RAW_SOCKET::recv"); + + RECV_EXCEPTION_RETURN(); + + + msghdr recv_msg = {}; + recv_msg.msg_iov = (struct iovec *)iov; + recv_msg.msg_iovlen = n; + + /*default*/ + fillMsgHdr(recv_msg, addr, NULL, 0); + + #ifdef ACE_USE_MSG_CONTROL + union control_buffer cbuf; + if(to_addr != NULL) + { + this->get_local_addr(*to_addr); + + fillMsgHdr(recv_msg, addr, &cbuf, sizeof(cbuf)); + } + #endif + + ssize_t status = ACE_OS::recvmsg (this->get_handle (), + &recv_msg, + flags); + + addr.set_size (recv_msg.msg_namelen); + addr.set_type (((sockaddr_in *) addr.get_addr())->sin_family); + + #ifdef ACE_USE_MSG_CONTROL + if(to_addr != NULL) + getToAddrFromMsgHdr(recv_msg, *to_addr); + #endif + + return status; +} + +#else + +ssize_t +ACE_RAW_SOCKET::send (const iovec iov[], + size_t n, + const ACE_INET_Addr &addr, + int flags, + const ACE_Time_Value *timeout) const +{ + ACE_TRACE ("ACE_RAW_SOCKET::send iovec"); + + + // immediately fail when unsupported + return -1; +} + +ssize_t +ACE_RAW_SOCKET::recv (iovec iov[], + size_t n, + ACE_INET_Addr &addr, + int flags, + const ACE_Time_Value *timeout, + ACE_INET_Addr *to_addr) const +{ + ACE_TRACE ("ACE_RAW_SOCKET::recv iovec"); + + + // immediately fail when unsupported + return -1; +} + +#endif + int ACE_RAW_SOCKET::open (ACE_INET_Addr const & local, int protocol) { @@ -340,13 +375,17 @@ ACE_RAW_SOCKET::open (ACE_INET_Addr const & local, int protocol) if (this->get_handle () != ACE_INVALID_HANDLE) return -1; - int protocol_family = local.get_type (); + int protocol_family = local.get_type (); /// reuse_addr Maybe meaningless for RAW Socket const int reuse_addr = 1; if(ACE_SOCK::open (SOCK_RAW, protocol_family, protocol, reuse_addr) == -1) return -1; + if(ACE_OS::bind (this->get_handle (), (struct sockaddr *)local.get_addr(), local.get_addr_size()) == -1) + return -1; + + this->protocol_ = protocol; ACE_INET_Addr bindAddr; diff --git a/ACE/tests/RAW_Socket_Test.cpp b/ACE/tests/RAW_Socket_Test.cpp index 34507eba84c5c..223308bf027c3 100644 --- a/ACE/tests/RAW_Socket_Test.cpp +++ b/ACE/tests/RAW_Socket_Test.cpp @@ -75,6 +75,80 @@ typedef struct _UDP_HEADER_t #pragma pack(pop) +uint32_t Checksum(uint32_t cksum, uint8_t *pBuffer, uint32_t size) +{ + + if ((NULL == pBuffer) || (0 == size)) + { + // return passed value + return cksum; + } + + uint32_t num = 0; + uint8_t *p = pBuffer; + + while (size > 1) + { + + cksum += ((uint16_t)p[num] << 8 & 0xff00) | (uint16_t)p[num + 1] & 0x00FF; + size -= 2; + num += 2; + } + + if (size > 0) + { + cksum += ((uint16_t)p[num] << 8) & 0xFFFF; + num += 1; + } + + while (cksum >> 16) + { + cksum = (cksum & 0xFFFF) + (cksum >> 16); + } + + return cksum; +} + +void ipv4_header_checksum(IPv4_HEADER_t_Ptr pIpHeader) +{ + uint8_t ipHeadLen = (pIpHeader->bVersionAndHeaderLen & 0x0F) << 2; + + pIpHeader->u16CheckSum = 0; + + uint32_t sum = Checksum(0, (uint8_t *)pIpHeader, ipHeadLen); + + pIpHeader->u16CheckSum = htons((uint16_t)(~sum)); + +} + +void udp6_header_checksum(IPv6_HEADER_t_Ptr pIpHeader, UDP_HEADER_t_Ptr ptUDPHeader, size_t udpLen) +{ + uint32_t sum = 0; + + //udpLen = sizeof(UDP_HEADER_T) + contentLen; + + // + ptUDPHeader->u16Length = htons(udpLen); + sum += udpLen; + + //sum += (ptUDPHeader->u16Length >> 8 & 0x00FF); + //sum += (ptUDPHeader->u16Length << 8 & 0xFF00); + + sum = Checksum(sum, (uint8_t *)&pIpHeader->abSrcAddr, 16); + sum = Checksum(sum, (uint8_t *)&pIpHeader->abDstAddr, 16); + + sum += ((uint16_t)pIpHeader->bNextHeader & 0x00FF); + //finish the pseudo header checksum + + //udp section + ptUDPHeader->u16CheckSum = 0; + sum = Checksum(sum, (uint8_t *)ptUDPHeader, udpLen); + + ptUDPHeader->u16CheckSum = htons((uint16_t)(~sum)); + +} + + class SockGuard : private ACE_Copy_Disabled { @@ -548,29 +622,24 @@ run_iovec_IPv6_api_test () ACE_DEBUG ((LM_INFO, "%s test iovec send ...\n", __func__)); - IPv4_HEADER_t_Ptr ptIPv4Header = (IPv4_HEADER_t_Ptr)sendbuf; - UDP_HEADER_t_Ptr ptUDPHeader = (UDP_HEADER_t_Ptr)(sendbuf + sizeof(IPv4_HEADER_t)); + IPv6_HEADER_t_Ptr ptIPv6Header = (IPv6_HEADER_t_Ptr)sendbuf; + UDP_HEADER_t_Ptr ptUDPHeader = (UDP_HEADER_t_Ptr)(sendbuf + sizeof(IPv6_HEADER_t)); u_short n = sizeof("hello world"); - *ptIPv4Header = {}; - - ptIPv4Header->bVersionAndHeaderLen = 0x45; - ptIPv4Header->u16TotalLenOfPacket = htons(sizeof(IPv4_HEADER_t) + sizeof(UDP_HEADER_t) + n); // 数据包长度 - ptIPv4Header->u16PacketID = ACE_OS::rand(); - ptIPv4Header->bTTL = 64; - ptIPv4Header->bTypeOfProtocol = IPPROTO_UDP; - ptIPv4Header->u16CheckSum = 0; - ptIPv4Header->u32SourIp = (static_cast(client_addr.get_addr()))->sin_addr.s_addr; - ptIPv4Header->u32DestIp = (static_cast(server_addr.get_addr()))->sin_addr.s_addr; - + *ptIPv6Header = {}; + ptIPv6Header->bNextHeader = IPPROTO_UDP; + memcpy(ptIPv6Header->abSrcAddr, static_cast(client_addr.get_addr())->sin6_addr.s6_addr, 16); + memcpy(ptIPv6Header->abDstAddr, static_cast(server_addr.get_addr())->sin6_addr.s6_addr, 16); + u_short client_port_number = client_addr.get_port_number(); u_short server_port_number = server_addr.get_port_number(); ptUDPHeader->u16SrcPort = htons(client_port_number); ptUDPHeader->u16DstPort = htons(server_port_number); - ptUDPHeader->u16Length = htons(sizeof(UDP_HEADER_t) + n); - ptUDPHeader->u16CheckSum = 0; + //fill content + ACE_OS::strcpy(sendbuf + sizeof(IPv6_HEADER_t) + sizeof(UDP_HEADER_t), "hello world"); + udp6_header_checksum(ptIPv6Header, ptUDPHeader, sizeof(UDP_HEADER_t) + n); iovec iov_udp[2]; From 40de2bc2efcf2f1318c4881fbb37dfe3139bcb5e Mon Sep 17 00:00:00 2001 From: smitAchang Date: Tue, 28 Mar 2023 20:09:09 +0800 Subject: [PATCH 12/39] windows use --- ACE/ace/RAW_Socket.cpp | 114 ++++++++++++++++++++--------------------- ACE/ace/RAW_Socket.h | 7 +-- 2 files changed, 60 insertions(+), 61 deletions(-) diff --git a/ACE/ace/RAW_Socket.cpp b/ACE/ace/RAW_Socket.cpp index 99a1c58ed1d6d..2df14f2bc31c0 100644 --- a/ACE/ace/RAW_Socket.cpp +++ b/ACE/ace/RAW_Socket.cpp @@ -118,43 +118,43 @@ static inline void fillMsgHdr(msghdr& recv_msg, ACE_INET_Addr &addr, void* pcbuf static inline void getToAddrFromMsgHdr(msghdr& recv_msg, ACE_INET_Addr& to_addr) { -#ifdef ACE_USE_MSG_CONTROL - if (to_addr.get_type() == AF_INET) { - #if defined (IP_RECVDSTADDR) || defined (IP_PKTINFO) - for (cmsghdr *ptr = ACE_CMSG_FIRSTHDR (&recv_msg); ptr; ptr = ACE_CMSG_NXTHDR (&recv_msg, ptr)) { - #if defined (IP_RECVDSTADDR) - if (ptr->cmsg_level == IPPROTO_IP && ptr->cmsg_type == IP_RECVDSTADDR) { - to_addr->set_address ((const char *) (ACE_CMSG_DATA (ptr)), - sizeof (struct in_addr), - 0); - break; - } - #else - if (ptr->cmsg_level == IPPROTO_IP && ptr->cmsg_type == IP_PKTINFO) { - to_addr.set_address ((const char *) &(((in_pktinfo *) (ACE_CMSG_DATA (ptr)))->ipi_addr), - sizeof (struct in_addr), - 0); - break; + #ifdef ACE_USE_MSG_CONTROL + if (to_addr.get_type() == AF_INET) { + #if defined (IP_RECVDSTADDR) || defined (IP_PKTINFO) + for (cmsghdr *ptr = ACE_CMSG_FIRSTHDR (&recv_msg); ptr; ptr = ACE_CMSG_NXTHDR (&recv_msg, ptr)) { + #if defined (IP_RECVDSTADDR) + if (ptr->cmsg_level == IPPROTO_IP && ptr->cmsg_type == IP_RECVDSTADDR) { + to_addr.set_address ((const char *) (ACE_CMSG_DATA (ptr)), + sizeof (struct in_addr), + 0); + break; + } + #else + if (ptr->cmsg_level == IPPROTO_IP && ptr->cmsg_type == IP_PKTINFO) { + to_addr.set_address ((const char *) &(((in_pktinfo *) (ACE_CMSG_DATA (ptr)))->ipi_addr), + sizeof (struct in_addr), + 0); + break; + } + #endif } - #endif + #endif } - #endif - } - #if defined (ACE_HAS_IPV6) && defined (IPV6_PKTINFO) - else if (to_addr.get_type() == AF_INET6) { - for (cmsghdr *ptr = ACE_CMSG_FIRSTHDR (&recv_msg); ptr; ptr = ACE_CMSG_NXTHDR (&recv_msg, ptr)) { - if (ptr->cmsg_level == IPPROTO_IPV6 && ptr->cmsg_type == IPV6_PKTINFO) { - to_addr.set_address ((const char *) &(((in6_pktinfo *)(ACE_CMSG_DATA (ptr)))->ipi6_addr), - sizeof (struct in6_addr), - 0); + #if defined (ACE_HAS_IPV6) && defined (IPV6_PKTINFO) + else if (to_addr.get_type() == AF_INET6) { + for (cmsghdr *ptr = ACE_CMSG_FIRSTHDR (&recv_msg); ptr; ptr = ACE_CMSG_NXTHDR (&recv_msg, ptr)) { + if (ptr->cmsg_level == IPPROTO_IPV6 && ptr->cmsg_type == IPV6_PKTINFO) { + to_addr.set_address ((const char *) &(((in6_pktinfo *)(ACE_CMSG_DATA (ptr)))->ipi6_addr), + sizeof (struct in6_addr), + 0); - break; + break; + } } } - } + #endif #endif -#endif } @@ -203,7 +203,7 @@ ACE_RAW_SOCKET::recv (void *buf, fillMsgHdr(recv_msg, addr, NULL, 0); } #else - fillMsgHdr(recv_msg, addr, NULL, 0); + fillMsgHdr(recv_msg, addr, NULL, 0); #endif ssize_t status = ACE_OS::recvmsg (this->get_handle (), @@ -242,13 +242,12 @@ ACE_RAW_SOCKET::send (const void *buf, flags, (struct sockaddr *) saddr, len); - } #if defined (ACE_HAS_MSG) ssize_t ACE_RAW_SOCKET::send (const iovec iov[], - size_t n, + int n, const ACE_INET_Addr &addr, int flags, const ACE_Time_Value *timeout) const @@ -263,35 +262,34 @@ ACE_RAW_SOCKET::send (const iovec iov[], send_msg.msg_iov = (iovec *) iov; send_msg.msg_iovlen = n; - #if defined (ACE_HAS_SOCKADDR_MSG_NAME) - send_msg.msg_name = (struct sockaddr *) addr.get_addr (); - #else - send_msg.msg_name = (char *) addr.get_addr (); - #endif /* ACE_HAS_SOCKADDR_MSG_NAME */ - send_msg.msg_namelen = addr.get_size (); - - #if defined (ACE_HAS_4_4BSD_SENDMSG_RECVMSG) - send_msg.msg_control = 0; - send_msg.msg_controllen = 0; - send_msg.msg_flags = 0; - #elif !defined ACE_LACKS_SENDMSG - send_msg.msg_accrights = 0; - send_msg.msg_accrightslen = 0; - #endif /* ACE_HAS_4_4BSD_SENDMSG_RECVMSG */ - - #ifdef ACE_WIN32 - send_msg.msg_control = 0; - send_msg.msg_controllen = 0; - #endif + #if defined (ACE_HAS_SOCKADDR_MSG_NAME) + send_msg.msg_name = (struct sockaddr *) addr.get_addr (); + #else + send_msg.msg_name = (char *) addr.get_addr (); + #endif /* ACE_HAS_SOCKADDR_MSG_NAME */ + send_msg.msg_namelen = addr.get_size (); + + #if defined (ACE_HAS_4_4BSD_SENDMSG_RECVMSG) + send_msg.msg_control = 0; + send_msg.msg_controllen = 0; + send_msg.msg_flags = 0; + #elif !defined ACE_LACKS_SENDMSG + send_msg.msg_accrights = 0; + send_msg.msg_accrightslen = 0; + #endif /* ACE_HAS_4_4BSD_SENDMSG_RECVMSG */ + + #ifdef ACE_WIN32 + send_msg.msg_control = 0; + send_msg.msg_controllen = 0; + #endif return ACE_OS::sendmsg (this->get_handle (), &send_msg, flags); - } ssize_t ACE_RAW_SOCKET::recv (iovec iov[], - size_t n, + int n, ACE_INET_Addr &addr, int flags, const ACE_Time_Value *timeout, @@ -335,10 +333,10 @@ ACE_RAW_SOCKET::recv (iovec iov[], } #else - +// not supported will fail immediately ssize_t ACE_RAW_SOCKET::send (const iovec iov[], - size_t n, + int n, const ACE_INET_Addr &addr, int flags, const ACE_Time_Value *timeout) const @@ -352,7 +350,7 @@ ACE_RAW_SOCKET::send (const iovec iov[], ssize_t ACE_RAW_SOCKET::recv (iovec iov[], - size_t n, + int n, ACE_INET_Addr &addr, int flags, const ACE_Time_Value *timeout, diff --git a/ACE/ace/RAW_Socket.h b/ACE/ace/RAW_Socket.h index 6bbb50d3b1ccf..66c6d2a93db76 100644 --- a/ACE/ace/RAW_Socket.h +++ b/ACE/ace/RAW_Socket.h @@ -103,18 +103,20 @@ ssize_t send (const void *buf, /** * Send an of size @a n to the datagram socket (uses) * The IP destination address will be placed in @a *to_addr if it is not null + * @return if iovec call is unsupported by platforms, the method will return -1 immediately */ ssize_t send (const iovec iov[], - size_t n, + int n, const ACE_INET_Addr &addr, int flags = 0, const ACE_Time_Value *timeout = NULL) const; /** * Recv an of size @a n to the datagram socket (uses ). * The IP destination address will be placed in @a *to_addr if it is not null. + * @return if iovec call is unsupported by platforms, the method will return -1 immediately */ ssize_t recv (iovec iov[], - size_t n, + int n, ACE_INET_Addr &addr, int flags = 0, const ACE_Time_Value *timeout = NULL, @@ -135,7 +137,6 @@ ssize_t recv (iovec iov[], /// Dump the state of object. void dump () const; - /// Return @c true if the protocol is IPPROTO_RAW. bool is_send_only () const; From cc25a6c3be9e4bd242eafc63a50a4b9ac3cee912 Mon Sep 17 00:00:00 2001 From: SmithAchang Date: Tue, 28 Mar 2023 22:10:45 +0800 Subject: [PATCH 13/39] use c++ cast --- ACE/ace/RAW_Socket.cpp | 46 +++++++++++++++++++++--------------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/ACE/ace/RAW_Socket.cpp b/ACE/ace/RAW_Socket.cpp index 2df14f2bc31c0..665f22af9aa11 100644 --- a/ACE/ace/RAW_Socket.cpp +++ b/ACE/ace/RAW_Socket.cpp @@ -79,12 +79,12 @@ ACE_RAW_SOCKET::ACE_RAW_SOCKET (ACE_INET_Addr const & local, } -static inline ssize_t using_common_recv(const ACE_RAW_SOCKET* raw, void *buf, size_t n, ACE_INET_Addr &addr, int flags) +static inline ssize_t using_common_recv(const ACE_RAW_SOCKET& raw, void *buf, size_t n, ACE_INET_Addr &addr, int flags) { - sockaddr *saddr = (sockaddr *) addr.get_addr (); + sockaddr *saddr = static_cast(addr.get_addr ()); int addr_len = addr.get_size (); - ssize_t status = ACE_OS::recvfrom (raw->get_handle (), + const ssize_t status = ACE_OS::recvfrom (raw.get_handle (), (char *) buf, n, flags, @@ -96,12 +96,12 @@ static inline ssize_t using_common_recv(const ACE_RAW_SOCKET* raw, void *buf, si return status; } -static inline void fillMsgHdr(msghdr& recv_msg, ACE_INET_Addr &addr, void* pcbuf, size_t cbuf_size) +static inline void fillMsgHdr(msghdr& recv_msg, const ACE_INET_Addr &addr, void* pcbuf, int cbuf_size) { #if defined (ACE_HAS_SOCKADDR_MSG_NAME) - recv_msg.msg_name = (struct sockaddr *) addr.get_addr (); + recv_msg.msg_name = static_cast (addr.get_addr ()); #else - recv_msg.msg_name = (char *) addr.get_addr (); + recv_msg.msg_name = static_cast(addr.get_addr ()); #endif /* ACE_HAS_SOCKADDR_MSG_NAME */ recv_msg.msg_namelen = addr.get_size (); @@ -124,7 +124,7 @@ static inline void getToAddrFromMsgHdr(msghdr& recv_msg, ACE_INET_Addr& to_addr) for (cmsghdr *ptr = ACE_CMSG_FIRSTHDR (&recv_msg); ptr; ptr = ACE_CMSG_NXTHDR (&recv_msg, ptr)) { #if defined (IP_RECVDSTADDR) if (ptr->cmsg_level == IPPROTO_IP && ptr->cmsg_type == IP_RECVDSTADDR) { - to_addr.set_address ((const char *) (ACE_CMSG_DATA (ptr)), + to_addr.set_address (reinterpret_cast(ACE_CMSG_DATA (ptr)), sizeof (struct in_addr), 0); break; @@ -145,7 +145,7 @@ static inline void getToAddrFromMsgHdr(msghdr& recv_msg, ACE_INET_Addr& to_addr) else if (to_addr.get_type() == AF_INET6) { for (cmsghdr *ptr = ACE_CMSG_FIRSTHDR (&recv_msg); ptr; ptr = ACE_CMSG_NXTHDR (&recv_msg, ptr)) { if (ptr->cmsg_level == IPPROTO_IPV6 && ptr->cmsg_type == IPV6_PKTINFO) { - to_addr.set_address ((const char *) &(((in6_pktinfo *)(ACE_CMSG_DATA (ptr)))->ipi6_addr), + to_addr.set_address (reinterpret_cast(&((reinterpret_cast((ACE_CMSG_DATA(ptr))))->ipi6_addr)), sizeof (struct in6_addr), 0); @@ -172,24 +172,24 @@ ACE_RAW_SOCKET::recv (void *buf, if(to_addr == NULL) { - return using_common_recv(this, buf, n, addr, flags); + return using_common_recv(*this, buf, n, addr, flags); } else { this->get_local_addr (*to_addr); if(!to_addr->is_any()) { - return using_common_recv(this, buf, n, addr, flags); + return using_common_recv(*this, buf, n, addr, flags); } } struct iovec iov; iov.iov_base = static_cast(buf); - iov.iov_len = n; + iov.iov_len = static_cast(n); msghdr recv_msg = {}; - recv_msg.msg_iov = (iovec*)&iov; + recv_msg.msg_iov = &iov; recv_msg.msg_iovlen = 1; #ifdef ACE_USE_MSG_CONTROL @@ -206,12 +206,12 @@ ACE_RAW_SOCKET::recv (void *buf, fillMsgHdr(recv_msg, addr, NULL, 0); #endif - ssize_t status = ACE_OS::recvmsg (this->get_handle (), + const ssize_t status = ACE_OS::recvmsg (this->get_handle (), &recv_msg, flags); addr.set_size (recv_msg.msg_namelen); - addr.set_type (((sockaddr_in *) addr.get_addr())->sin_family); + addr.set_type ((static_cast(addr.get_addr()))->sin_family); #ifdef ACE_USE_MSG_CONTROL if(to_addr != NULL) @@ -234,8 +234,8 @@ ACE_RAW_SOCKET::send (const void *buf, // Check the status of the current socket. SEND_EXCEPTION_RETURN(); - sockaddr *saddr = (sockaddr *) addr.get_addr (); - int len = addr.get_size (); + sockaddr *saddr = static_cast(addr.get_addr ()); + const int len = addr.get_size (); return ACE_OS::sendto (this->get_handle (), (const char *) buf, n, @@ -259,11 +259,11 @@ ACE_RAW_SOCKET::send (const iovec iov[], msghdr send_msg = {}; - send_msg.msg_iov = (iovec *) iov; + send_msg.msg_iov = const_cast(iov); send_msg.msg_iovlen = n; #if defined (ACE_HAS_SOCKADDR_MSG_NAME) - send_msg.msg_name = (struct sockaddr *) addr.get_addr (); + send_msg.msg_name = static_cast(addr.get_addr()); #else send_msg.msg_name = (char *) addr.get_addr (); #endif /* ACE_HAS_SOCKADDR_MSG_NAME */ @@ -301,7 +301,7 @@ ACE_RAW_SOCKET::recv (iovec iov[], msghdr recv_msg = {}; - recv_msg.msg_iov = (struct iovec *)iov; + recv_msg.msg_iov = static_cast(iov); recv_msg.msg_iovlen = n; /*default*/ @@ -317,12 +317,12 @@ ACE_RAW_SOCKET::recv (iovec iov[], } #endif - ssize_t status = ACE_OS::recvmsg (this->get_handle (), + const ssize_t status = ACE_OS::recvmsg (this->get_handle (), &recv_msg, flags); addr.set_size (recv_msg.msg_namelen); - addr.set_type (((sockaddr_in *) addr.get_addr())->sin_family); + addr.set_type ((static_cast(addr.get_addr()))->sin_family); #ifdef ACE_USE_MSG_CONTROL if(to_addr != NULL) @@ -373,14 +373,14 @@ ACE_RAW_SOCKET::open (ACE_INET_Addr const & local, int protocol) if (this->get_handle () != ACE_INVALID_HANDLE) return -1; - int protocol_family = local.get_type (); + const int protocol_family = local.get_type (); /// reuse_addr Maybe meaningless for RAW Socket const int reuse_addr = 1; if(ACE_SOCK::open (SOCK_RAW, protocol_family, protocol, reuse_addr) == -1) return -1; - if(ACE_OS::bind (this->get_handle (), (struct sockaddr *)local.get_addr(), local.get_addr_size()) == -1) + if(ACE_OS::bind (this->get_handle (), static_cast(local.get_addr()), local.get_addr_size()) == -1) return -1; From bcf3c39de964451638082c23d6cb6a1fe537e75e Mon Sep 17 00:00:00 2001 From: smithAchang Date: Tue, 28 Mar 2023 22:22:03 +0800 Subject: [PATCH 14/39] test modify because api change --- ACE/tests/RAW_Socket_Test.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/ACE/tests/RAW_Socket_Test.cpp b/ACE/tests/RAW_Socket_Test.cpp index 223308bf027c3..8f247f0d815e5 100644 --- a/ACE/tests/RAW_Socket_Test.cpp +++ b/ACE/tests/RAW_Socket_Test.cpp @@ -288,11 +288,11 @@ static int raw_recv_data_until_meet_condition(ACE_RAW_SOCKET& raw, u_short port, if(to_addr == NULL) { - raw.recv(vec, (sizeof(vec)/sizeof(vec[0])) , remote); + raw.recv(vec, (int)(sizeof(vec)/sizeof(vec[0])) , remote); } else { - len = raw.recv(vec, (sizeof(vec)/sizeof(vec[0])), remote, 0/*flags*/, NULL, to_addr); + len = raw.recv(vec, (int)(sizeof(vec)/sizeof(vec[0])), remote, 0/*flags*/, NULL, to_addr); } } else @@ -658,7 +658,7 @@ run_iovec_IPv6_api_test () ACE_DEBUG ((LM_INFO, "%s must set port to zero ??? ...\n", __func__)); iov_server_addr.set_port_number(0); - rc = rawSocket.send(iov_udp, sizeof(iov_udp)/sizeof(iov_udp[0]), iov_server_addr); + rc = rawSocket.send(iov_udp, (int)(sizeof(iov_udp)/sizeof(iov_udp[0])), iov_server_addr); EXCEPTION_RETURN(rc == -1, " raw6 socket can not send using iov \n"); ACE_OS::sleep(1); @@ -755,7 +755,7 @@ run_iovec_IPv4_api_test () iov_udp[1].iov_base = (void*)"hello world"; iov_udp[1].iov_len = sizeof("hello world"); - rc = rawSocket.send(iov_udp, sizeof(iov_udp)/sizeof(iov_udp[0]), server_addr); + rc = rawSocket.send(iov_udp, (int)(sizeof(iov_udp)/sizeof(iov_udp[0])), server_addr); EXCEPTION_RETURN(rc == -1, " raw4 socket can send using iov\n"); ACE_OS::sleep(1); From fa90b7124613450843226de3f95832604f551c94 Mon Sep 17 00:00:00 2001 From: smitAchang Date: Wed, 29 Mar 2023 19:57:01 +0800 Subject: [PATCH 15/39] test at windows --- ACE/tests/RAW_Socket_Test.cpp | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/ACE/tests/RAW_Socket_Test.cpp b/ACE/tests/RAW_Socket_Test.cpp index 8f247f0d815e5..30e7067efc171 100644 --- a/ACE/tests/RAW_Socket_Test.cpp +++ b/ACE/tests/RAW_Socket_Test.cpp @@ -342,7 +342,7 @@ static int raw_recv_data_until_meet_condition(ACE_RAW_SOCKET& raw, u_short port, } } - ACE_DEBUG ((LM_DEBUG, "%s recv unexpected pkgs len: %d, expectedLen: %d ...\n", __func__, len, expectedLen)); + ACE_DEBUG ((LM_DEBUG, "%s recv unexpected pkgs len: %d, srcPort: %u, dstPort:%u; expectedLen: %d, expectedPort: %u ...\n", __func__, len, ntohs(ptUDPHeader->u16SrcPort), ntohs(ptUDPHeader->u16DstPort), expectedLen, port)); ACE_OS::sleep(1); } while (1); @@ -402,6 +402,9 @@ run_raw_udp_test () rc = dgram.send(sendbuf, n, server_addr); EXCEPTION_RETURN(rc != n, " can send test pkg to server\n"); + rc = dgram_server.recv(recvbuf, sizeof(recvbuf), remote); + EXCEPTION_RETURN(rc != n, " server socket can not recv the pkg client has send. It will be recv lately by raw socket\n"); + u_short server_port = server_addr.get_port_number(); rc = raw_recv_data_until_meet_condition(rawSocket, server_port, n, remote); @@ -643,9 +646,9 @@ run_iovec_IPv6_api_test () iovec iov_udp[2]; - iov_udp[0].iov_base = ptUDPHeader; + iov_udp[0].iov_base = reinterpret_cast(ptUDPHeader); iov_udp[0].iov_len = sizeof(UDP_HEADER_t); - iov_udp[1].iov_base = (void*)"hello world"; + iov_udp[1].iov_base = "hello world"; iov_udp[1].iov_len = sizeof("hello world"); ACE_DEBUG ((LM_INFO, "%s test iovec using common udp6 socket ...\n", __func__)); @@ -750,9 +753,9 @@ run_iovec_IPv4_api_test () ACE_OS::strcpy(sendbuf + sizeof(IPv4_HEADER_t) + sizeof(UDP_HEADER_t), "hello world"); iovec iov_udp[2]; - iov_udp[0].iov_base = ptUDPHeader; + iov_udp[0].iov_base = reinterpret_cast(ptUDPHeader); iov_udp[0].iov_len = sizeof(UDP_HEADER_t); - iov_udp[1].iov_base = (void*)"hello world"; + iov_udp[1].iov_base = "hello world"; iov_udp[1].iov_len = sizeof("hello world"); rc = rawSocket.send(iov_udp, (int)(sizeof(iov_udp)/sizeof(iov_udp[0])), server_addr); From c21578425e81b2d95f2365a015804b462a7d77d7 Mon Sep 17 00:00:00 2001 From: smithAchang Date: Wed, 29 Mar 2023 21:08:51 +0800 Subject: [PATCH 16/39] windows&linux test compile problem --- ACE/tests/RAW_Socket_Test.cpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/ACE/tests/RAW_Socket_Test.cpp b/ACE/tests/RAW_Socket_Test.cpp index 30e7067efc171..16ba3121fee69 100644 --- a/ACE/tests/RAW_Socket_Test.cpp +++ b/ACE/tests/RAW_Socket_Test.cpp @@ -413,12 +413,14 @@ run_raw_udp_test () rc = run_raw_udp_test_child_flow_sendby_self (rawSocket, client_addr, server_addr, n + 1); EXCEPTION_RETURN(rc != 0, " can recv test pkg from raw socket when sending by self\n"); + #if !defined (ACE_WIN32) if(ACE_OS::getuid() == 0) { ACE_DEBUG ((LM_INFO, "%s test send & recv big pkt ...\n", __func__)); rc = run_raw_udp_test_child_flow_sendby_self (rawSocket, client_addr, server_addr, n + 2048); EXCEPTION_RETURN(rc != 0, " can recv test pkg from raw socket when sending big pkg by self\n"); } + #endif return 0; } @@ -472,6 +474,7 @@ run_raw_generic_test () ptUDPHeader->u16Length = htons(sizeof(UDP_HEADER_t) + n); ptUDPHeader->u16CheckSum = 0; + #if !defined (ACE_WIN32) if(ACE_OS::getuid() == 0) { ACE_DEBUG ((LM_INFO, "%s raw generic socket will send bytes exceeding the MTU ...\n", __func__)); @@ -479,6 +482,7 @@ run_raw_generic_test () len = rawSocket.send(sendbuf, sizeof(IPv4_HEADER_t) + sizeof(UDP_HEADER_t) + n, server_addr); EXCEPTION_RETURN(len != -1, " raw generic socket can not send pkg more than MTU\n"); } + #endif n = 468; ptUDPHeader->u16Length = htons(sizeof(UDP_HEADER_t) + n); @@ -648,8 +652,13 @@ run_iovec_IPv6_api_test () iovec iov_udp[2]; iov_udp[0].iov_base = reinterpret_cast(ptUDPHeader); iov_udp[0].iov_len = sizeof(UDP_HEADER_t); + #if defined (ACE_WIN32) iov_udp[1].iov_base = "hello world"; iov_udp[1].iov_len = sizeof("hello world"); + #else + iov_udp[1].iov_base = const_cast("hello world"); + iov_udp[1].iov_len = sizeof("hello world"); + #endif ACE_DEBUG ((LM_INFO, "%s test iovec using common udp6 socket ...\n", __func__)); @@ -755,8 +764,13 @@ run_iovec_IPv4_api_test () iovec iov_udp[2]; iov_udp[0].iov_base = reinterpret_cast(ptUDPHeader); iov_udp[0].iov_len = sizeof(UDP_HEADER_t); + #if defined (ACE_WIN32) iov_udp[1].iov_base = "hello world"; iov_udp[1].iov_len = sizeof("hello world"); + #else + iov_udp[1].iov_base = const_cast("hello world"); + iov_udp[1].iov_len = sizeof("hello world"); + #endif rc = rawSocket.send(iov_udp, (int)(sizeof(iov_udp)/sizeof(iov_udp[0])), server_addr); EXCEPTION_RETURN(rc == -1, " raw4 socket can send using iov\n"); From e12a99e831837b0e715f13666f85a5801b122c9a Mon Sep 17 00:00:00 2001 From: smithAchang Date: Thu, 30 Mar 2023 22:22:54 +0800 Subject: [PATCH 17/39] first coversation with ACE team --- ACE/ace/RAW_Socket.cpp | 38 ++++++++++----------- ACE/ace/RAW_Socket.h | 14 ++++---- ACE/tests/RAW_Socket_Test.cpp | 64 +++++++++++++++++------------------ 3 files changed, 58 insertions(+), 58 deletions(-) diff --git a/ACE/ace/RAW_Socket.cpp b/ACE/ace/RAW_Socket.cpp index 665f22af9aa11..40405e392b3c4 100644 --- a/ACE/ace/RAW_Socket.cpp +++ b/ACE/ace/RAW_Socket.cpp @@ -23,28 +23,28 @@ ACE_BEGIN_VERSIONED_NAMESPACE_DECL ACE_ALLOC_HOOK_DEFINE (ACE_RAW_SOCKET) #if defined (ACE_HAS_4_4BSD_SENDMSG_RECVMSG) || defined ACE_WIN32 - #define ACE_USE_MSG_CONTROL +#define ACE_USE_MSG_CONTROL union control_buffer { cmsghdr control_msg_header; - #if defined (IP_RECVDSTADDR) +#if defined (IP_RECVDSTADDR) u_char padding[ACE_CMSG_SPACE (sizeof (in_addr))]; - #elif defined (IP_PKTINFO) +#elif defined (IP_PKTINFO) u_char padding[ACE_CMSG_SPACE (sizeof (in_pktinfo))]; - #endif - #if defined (ACE_HAS_IPV6) +#endif +#if defined (ACE_HAS_IPV6) u_char padding6[ACE_CMSG_SPACE (sizeof (in6_pktinfo))]; - #endif +#endif }; #endif -#define SEND_EXCEPTION_RETURN() do{\ +#define ACE_SEND_EXCEPTION_RETURN() do{\ if (this->get_handle () == ACE_INVALID_HANDLE)\ return -1; \ if(timeout && ACE::handle_write_ready (this->get_handle (), timeout) != 1)\ return -1; \ }while(0) -#define RECV_EXCEPTION_RETURN() do{\ +#define ACE_RECV_EXCEPTION_RETURN() do{\ if (this->get_handle () == ACE_INVALID_HANDLE)\ return -1; \ if(this->is_send_only())\ @@ -84,7 +84,7 @@ static inline ssize_t using_common_recv(const ACE_RAW_SOCKET& raw, void *buf, si sockaddr *saddr = static_cast(addr.get_addr ()); int addr_len = addr.get_size (); - const ssize_t status = ACE_OS::recvfrom (raw.get_handle (), + ssize_t const status = ACE_OS::recvfrom (raw.get_handle (), (char *) buf, n, flags, @@ -120,7 +120,7 @@ static inline void getToAddrFromMsgHdr(msghdr& recv_msg, ACE_INET_Addr& to_addr) { #ifdef ACE_USE_MSG_CONTROL if (to_addr.get_type() == AF_INET) { - #if defined (IP_RECVDSTADDR) || defined (IP_PKTINFO) + #if defined (IP_RECVDSTADDR) || defined (IP_PKTINFO) for (cmsghdr *ptr = ACE_CMSG_FIRSTHDR (&recv_msg); ptr; ptr = ACE_CMSG_NXTHDR (&recv_msg, ptr)) { #if defined (IP_RECVDSTADDR) if (ptr->cmsg_level == IPPROTO_IP && ptr->cmsg_type == IP_RECVDSTADDR) { @@ -168,7 +168,7 @@ ACE_RAW_SOCKET::recv (void *buf, { ACE_TRACE ("ACE_RAW_SOCKET::recv"); - RECV_EXCEPTION_RETURN(); + ACE_RECV_EXCEPTION_RETURN(); if(to_addr == NULL) { @@ -206,7 +206,7 @@ ACE_RAW_SOCKET::recv (void *buf, fillMsgHdr(recv_msg, addr, NULL, 0); #endif - const ssize_t status = ACE_OS::recvmsg (this->get_handle (), + ssize_t const status = ACE_OS::recvmsg (this->get_handle (), &recv_msg, flags); @@ -232,10 +232,10 @@ ACE_RAW_SOCKET::send (const void *buf, ACE_TRACE ("ACE_RAW_SOCKET::send"); // Check the status of the current socket. - SEND_EXCEPTION_RETURN(); + ACE_SEND_EXCEPTION_RETURN(); sockaddr *saddr = static_cast(addr.get_addr ()); - const int len = addr.get_size (); + int const len = addr.get_size (); return ACE_OS::sendto (this->get_handle (), (const char *) buf, n, @@ -255,7 +255,7 @@ ACE_RAW_SOCKET::send (const iovec iov[], ACE_TRACE ("ACE_RAW_SOCKET::send"); // Check the status of the current socket. - SEND_EXCEPTION_RETURN(); + ACE_SEND_EXCEPTION_RETURN(); msghdr send_msg = {}; @@ -297,7 +297,7 @@ ACE_RAW_SOCKET::recv (iovec iov[], { ACE_TRACE ("ACE_RAW_SOCKET::recv"); - RECV_EXCEPTION_RETURN(); + ACE_RECV_EXCEPTION_RETURN(); msghdr recv_msg = {}; @@ -317,7 +317,7 @@ ACE_RAW_SOCKET::recv (iovec iov[], } #endif - const ssize_t status = ACE_OS::recvmsg (this->get_handle (), + ssize_t const status = ACE_OS::recvmsg (this->get_handle (), &recv_msg, flags); @@ -373,9 +373,9 @@ ACE_RAW_SOCKET::open (ACE_INET_Addr const & local, int protocol) if (this->get_handle () != ACE_INVALID_HANDLE) return -1; - const int protocol_family = local.get_type (); + int const protocol_family = local.get_type (); /// reuse_addr Maybe meaningless for RAW Socket - const int reuse_addr = 1; + int const reuse_addr = 1; if(ACE_SOCK::open (SOCK_RAW, protocol_family, protocol, reuse_addr) == -1) return -1; diff --git a/ACE/ace/RAW_Socket.h b/ACE/ace/RAW_Socket.h index 66c6d2a93db76..23af8362c3e4b 100644 --- a/ACE/ace/RAW_Socket.h +++ b/ACE/ace/RAW_Socket.h @@ -35,15 +35,15 @@ ACE_BEGIN_VERSIONED_NAMESPACE_DECL * @brief An RAW Socket implemention class. * * An RAW Socket can be used for some user-space network protocol stack. - * - Setting the protocol para to be IPPROTO_UDP will filter all UDP protocol packages with the destination is its bound address. - * IT can reduce the total num of socket needed with only port difference. + * - Setting the protocol parameter to be IPPROTO_UDP will filter all UDP protocol packages with the destination is its bound address. + * It can reduce the total num of socket needed with only port difference. * - * - Setting the protocol para to be IPPROTO_SCTP will filter all SCTP protocol packages with the destination is its bound address. - * It can form the basis of a user-space SCTP protocol stack in more general platforms. + * - Setting the protocol parameter to be IPPROTO_SCTP will filter all SCTP protocol packages with the destination is its bound address. + * It can form the basis of a user-space SCTP protocol stack in more general way. * - * - Setting the protocol para to be IPPROTO_RAW will make it as a send only socket for any customized package formed from IP header to be send. - * Notice the source address if provided in the customized package can be different from its bound address. - * Notice the RAW socket does not support fragment function when the passed package exceeds the MTU, so it need a upper layer framment before called + * - Setting the protocol parameter to be IPPROTO_RAW will make it as a send only socket for any customized datagram formed from the IP header to be sent. + * Notice the source address can be different from its bound address if provided in the customized package or keep them default to zero. + * Notice the IPPROTO_RAW RAW socket does not support fragment function when the passed package exceeds the MTU, so it need a upper layer fragment before called * * @note If you really want to receive all IP packets, use a packet(7) socket with the ETH_P_IP protocol. * For "Single Responsibility Principle" the behavior has notable difference, so the feature is not implemented here. diff --git a/ACE/tests/RAW_Socket_Test.cpp b/ACE/tests/RAW_Socket_Test.cpp index 16ba3121fee69..b4aa73454316b 100644 --- a/ACE/tests/RAW_Socket_Test.cpp +++ b/ACE/tests/RAW_Socket_Test.cpp @@ -184,7 +184,7 @@ static int run_option_test () { - ACE_DEBUG ((LM_INFO, "%s begin to run ...\n", __func__)); + ACE_DEBUG ((LM_INFO, "%C begin to run ...\n", __func__)); ACE_INET_Addr addr(u_short(0), "127.0.0.1"); ACE_RAW_SOCKET rawSocket(addr); @@ -225,7 +225,7 @@ static int run_reopen_test () { - ACE_DEBUG ((LM_INFO, "%s begin to run ...\n", __func__)); + ACE_DEBUG ((LM_INFO, "%C begin to run ...\n", __func__)); ACE_INET_Addr addr((u_short)0, "127.0.0.1"); ACE_RAW_SOCKET rawSocket(addr); @@ -276,7 +276,7 @@ static int raw_recv_data_until_meet_condition(ACE_RAW_SOCKET& raw, u_short port, { iovec vec[5]; unsigned int i=0; - const unsigned int oneByteRecvVecNum = (sizeof(vec)/sizeof(vec[0])) - 1; + unsigned int const oneByteRecvVecNum = (sizeof(vec)/sizeof(vec[0])) - 1; for(; i< oneByteRecvVecNum; ++i) { vec[i].iov_base = &recvbuf[i]; @@ -312,7 +312,7 @@ static int raw_recv_data_until_meet_condition(ACE_RAW_SOCKET& raw, u_short port, if(len < 0) { - ACE_DEBUG ((LM_INFO, "%s receive prcess reach the end ...\n", __func__)); + ACE_DEBUG ((LM_INFO, "%C receive prcess reach the end ...\n", __func__)); return -1; } @@ -325,7 +325,7 @@ static int raw_recv_data_until_meet_condition(ACE_RAW_SOCKET& raw, u_short port, if(port == nDstPort && len == expectedLen) { - ACE_DEBUG ((LM_INFO, "%s IPv4 recv expected pkgs ...\n", __func__)); + ACE_DEBUG ((LM_INFO, "%C IPv4 recv expected pkgs ...\n", __func__)); break; } } @@ -337,12 +337,12 @@ static int raw_recv_data_until_meet_condition(ACE_RAW_SOCKET& raw, u_short port, if(port == nDstPort && len == expectedLen) { - ACE_DEBUG ((LM_INFO, "%s IPv6 recv expected pkgs ...\n", __func__)); + ACE_DEBUG ((LM_INFO, "%C IPv6 recv expected pkgs ...\n", __func__)); break; } } - ACE_DEBUG ((LM_DEBUG, "%s recv unexpected pkgs len: %d, srcPort: %u, dstPort:%u; expectedLen: %d, expectedPort: %u ...\n", __func__, len, ntohs(ptUDPHeader->u16SrcPort), ntohs(ptUDPHeader->u16DstPort), expectedLen, port)); + ACE_DEBUG ((LM_DEBUG, "%C recv unexpected pkgs len: %d, srcPort: %u, dstPort:%u; expectedLen: %d, expectedPort: %u ...\n", __func__, len, ntohs(ptUDPHeader->u16SrcPort), ntohs(ptUDPHeader->u16DstPort), expectedLen, port)); ACE_OS::sleep(1); } while (1); @@ -352,7 +352,7 @@ static int raw_recv_data_until_meet_condition(ACE_RAW_SOCKET& raw, u_short port, static int run_raw_udp_test_child_flow_sendby_self (ACE_RAW_SOCKET& raw, ACE_INET_Addr& client_addr, ACE_INET_Addr& server_addr, size_t n) { - ACE_DEBUG ((LM_INFO, "%s begin to run when sending data by self ...\n", __func__)); + ACE_DEBUG ((LM_INFO, "%C begin to run when sending data by self ...\n", __func__)); UDP_HEADER_t_Ptr ptUDPHeader = (UDP_HEADER_t_Ptr)(sendbuf + sizeof(IPv4_HEADER_t_Ptr)); @@ -376,7 +376,7 @@ run_raw_udp_test_child_flow_sendby_self (ACE_RAW_SOCKET& raw, ACE_INET_Addr& cli static int run_raw_udp_test () { - ACE_DEBUG ((LM_INFO, "%s begin to run using the port auto assigned by OS to avoid port conflict ...\n", __func__)); + ACE_DEBUG ((LM_INFO, "%C begin to run using the port auto assigned by OS to avoid port conflict ...\n", __func__)); ACE_INET_Addr addr((u_short)0, "127.0.0.1"); @@ -416,7 +416,7 @@ run_raw_udp_test () #if !defined (ACE_WIN32) if(ACE_OS::getuid() == 0) { - ACE_DEBUG ((LM_INFO, "%s test send & recv big pkt ...\n", __func__)); + ACE_DEBUG ((LM_INFO, "%C test send & recv big pkt ...\n", __func__)); rc = run_raw_udp_test_child_flow_sendby_self (rawSocket, client_addr, server_addr, n + 2048); EXCEPTION_RETURN(rc != 0, " can recv test pkg from raw socket when sending big pkg by self\n"); } @@ -428,7 +428,7 @@ run_raw_udp_test () static int run_raw_generic_test () { - ACE_DEBUG ((LM_INFO, "%s begin to run generic raw socket i.e. send only RAW socket ...\n", __func__)); + ACE_DEBUG ((LM_INFO, "%C begin to run generic raw socket i.e. send only RAW socket ...\n", __func__)); ACE_INET_Addr bindAddr((u_short)0, "127.0.0.1"), remote; ACE_INET_Addr client_addr((u_short)0, "127.0.0.7") ,server_addr((u_short)0, "127.0.0.8"); @@ -477,7 +477,7 @@ run_raw_generic_test () #if !defined (ACE_WIN32) if(ACE_OS::getuid() == 0) { - ACE_DEBUG ((LM_INFO, "%s raw generic socket will send bytes exceeding the MTU ...\n", __func__)); + ACE_DEBUG ((LM_INFO, "%C raw generic socket will send bytes exceeding the MTU ...\n", __func__)); n = 2048; len = rawSocket.send(sendbuf, sizeof(IPv4_HEADER_t) + sizeof(UDP_HEADER_t) + n, server_addr); EXCEPTION_RETURN(len != -1, " raw generic socket can not send pkg more than MTU\n"); @@ -488,15 +488,15 @@ run_raw_generic_test () ptUDPHeader->u16Length = htons(sizeof(UDP_HEADER_t) + n); len = rawSocket.send(sendbuf, sizeof(IPv4_HEADER_t) + sizeof(UDP_HEADER_t) + n, server_addr); size_t expectedLen = (sizeof(IPv4_HEADER_t) + sizeof(UDP_HEADER_t) + n); - ACE_DEBUG ((LM_INFO, "%s raw generic socket send %d bytes, expected %u bytes ...\n", __func__, len, expectedLen)); + ACE_DEBUG ((LM_INFO, "%C raw generic socket send %d bytes, expected %u bytes ...\n", __func__, len, expectedLen)); EXCEPTION_RETURN(static_cast(len) != expectedLen, " raw generic socket send pkg in failure\n"); ACE_OS::sleep(1); - ACE_DEBUG ((LM_INFO, "%s enable nonblock status ...\n", __func__)); + ACE_DEBUG ((LM_INFO, "%C enable nonblock status ...\n", __func__)); server_dgram.enable(ACE_NONBLOCK); len = server_dgram.recv(recvbuf, sizeof(recvbuf), remote); expectedLen = n; - ACE_DEBUG ((LM_INFO, "%s udp server socket recv %d bytes, expected %u bytes ...\n", __func__, len, expectedLen)); + ACE_DEBUG ((LM_INFO, "%C udp server socket recv %d bytes, expected %u bytes ...\n", __func__, len, expectedLen)); EXCEPTION_RETURN(static_cast(len) != n, " server socket receives pkg in failure length is not the same\n"); EXCEPTION_RETURN(static_cast(remote.get_addr())->sin_addr.s_addr != static_cast(client_addr.get_addr())->sin_addr.s_addr, " server socket receives pkg in failure: the source IP is not the same\n"); @@ -507,7 +507,7 @@ run_raw_generic_test () static int run_ipv6_pkginfo_test () { - ACE_DEBUG ((LM_INFO, "%s begin to run IPv6 pkginfo test ...\n", __func__)); + ACE_DEBUG ((LM_INFO, "%C begin to run IPv6 pkginfo test ...\n", __func__)); ACE_INET_Addr bindAddr((u_short)0, "::1"); ACE_INET_Addr anyAddr((u_short)0, "::"); @@ -523,7 +523,7 @@ run_ipv6_pkginfo_test () SockGuard server_dgram_guard(server_dgram); server_dgram.get_local_addr(server_addr); - ACE_DEBUG ((LM_INFO, "%s get the real bound addr and port client_port: %u, server_port: %u...\n", __func__, client_addr.get_port_number(), server_addr.get_port_number())); + ACE_DEBUG ((LM_INFO, "%C get the real bound addr and port client_port: %u, server_port: %u...\n", __func__, client_addr.get_port_number(), server_addr.get_port_number())); @@ -537,7 +537,7 @@ run_ipv6_pkginfo_test () client_dgram.send("hello world", sizeof("hello world"), server_addr); - ACE_DEBUG ((LM_INFO, "%s the send pkg will be received by two raw sockets ...\n", __func__)); + ACE_DEBUG ((LM_INFO, "%C the send pkg will be received by two raw sockets ...\n", __func__)); ACE_OS::sleep(1); ACE_INET_Addr remote; @@ -547,7 +547,7 @@ run_ipv6_pkginfo_test () ACE_INET_Addr to_addr; - ACE_DEBUG ((LM_INFO, "%s send pkg again to test common raw socket with to_adr parameter ...\n", __func__)); + ACE_DEBUG ((LM_INFO, "%C send pkg again to test common raw socket with to_adr parameter ...\n", __func__)); client_dgram.send("hello world", sizeof("hello world"), server_addr); ACE_OS::sleep(1); @@ -575,7 +575,7 @@ run_ipv6_pkginfo_test () static int run_iovec_IPv6_api_test () { - ACE_DEBUG ((LM_INFO, "%s begin to run IPv6 iovec api test ...\n", __func__)); + ACE_DEBUG ((LM_INFO, "%C begin to run IPv6 iovec api test ...\n", __func__)); ACE_INET_Addr bindAddr((u_short)0, "::1"); ACE_INET_Addr anyAddr((u_short)0, "::"); @@ -592,7 +592,7 @@ run_iovec_IPv6_api_test () SockGuard server_dgram_guard(server_dgram); server_dgram.get_local_addr(server_addr); - ACE_DEBUG ((LM_INFO, "%s get the real bound addr and port client_port: %u, server_port: %u...\n", __func__, client_addr.get_port_number(), server_addr.get_port_number())); + ACE_DEBUG ((LM_INFO, "%C get the real bound addr and port client_port: %u, server_port: %u...\n", __func__, client_addr.get_port_number(), server_addr.get_port_number())); @@ -602,7 +602,7 @@ run_iovec_IPv6_api_test () client_dgram.send("hello world", sizeof("hello world"), server_addr); - ACE_DEBUG ((LM_INFO, "%s the send pkg will be received by raw sockets ...\n", __func__)); + ACE_DEBUG ((LM_INFO, "%C the send pkg will be received by raw sockets ...\n", __func__)); ACE_OS::sleep(1); ACE_INET_Addr remote; @@ -613,7 +613,7 @@ run_iovec_IPv6_api_test () ACE_INET_Addr to_addr; - ACE_DEBUG ((LM_INFO, "%s send pkg again to test common raw socket with to_adr parameter ...\n", __func__)); + ACE_DEBUG ((LM_INFO, "%C send pkg again to test common raw socket with to_adr parameter ...\n", __func__)); client_dgram.send("hello world", sizeof("hello world"), server_addr); ACE_OS::sleep(1); @@ -626,7 +626,7 @@ run_iovec_IPv6_api_test () int cmp = ACE_OS::memcmp(remote_sin6_addr, to_sin6_addr, sizeof(*to_sin6_addr)); EXCEPTION_RETURN(cmp != 0, " raw socket got to_addr with invalid value\n"); - ACE_DEBUG ((LM_INFO, "%s test iovec send ...\n", __func__)); + ACE_DEBUG ((LM_INFO, "%C test iovec send ...\n", __func__)); IPv6_HEADER_t_Ptr ptIPv6Header = (IPv6_HEADER_t_Ptr)sendbuf; @@ -660,7 +660,7 @@ run_iovec_IPv6_api_test () iov_udp[1].iov_len = sizeof("hello world"); #endif - ACE_DEBUG ((LM_INFO, "%s test iovec using common udp6 socket ...\n", __func__)); + ACE_DEBUG ((LM_INFO, "%C test iovec using common udp6 socket ...\n", __func__)); rc = client_dgram.send(iov_udp, (int)(sizeof(iov_udp)/sizeof(iov_udp[0])), server_addr); EXCEPTION_RETURN(rc == -1, " udp6 socket can not send using iov \n"); @@ -668,7 +668,7 @@ run_iovec_IPv6_api_test () readUdpSocektToEmpty(server_dgram); ACE_INET_Addr iov_server_addr(server_addr); - ACE_DEBUG ((LM_INFO, "%s must set port to zero ??? ...\n", __func__)); + ACE_DEBUG ((LM_INFO, "%C must set port to zero ??? ...\n", __func__)); iov_server_addr.set_port_number(0); rc = rawSocket.send(iov_udp, (int)(sizeof(iov_udp)/sizeof(iov_udp[0])), iov_server_addr); EXCEPTION_RETURN(rc == -1, " raw6 socket can not send using iov \n"); @@ -683,7 +683,7 @@ run_iovec_IPv6_api_test () static int run_iovec_IPv4_api_test () { - ACE_DEBUG ((LM_INFO, "%s begin to run IPv4 iovec api test ...\n", __func__)); + ACE_DEBUG ((LM_INFO, "%C begin to run IPv4 iovec api test ...\n", __func__)); ACE_INET_Addr bindAddr((u_short)0, "127.0.0.1"); @@ -699,7 +699,7 @@ run_iovec_IPv4_api_test () SockGuard server_dgram_guard(server_dgram); server_dgram.get_local_addr(server_addr); - ACE_DEBUG ((LM_INFO, "%s get the real bound addr and port client_port: %u, server_port: %u...\n", __func__, client_addr.get_port_number(), server_addr.get_port_number())); + ACE_DEBUG ((LM_INFO, "%C get the real bound addr and port client_port: %u, server_port: %u...\n", __func__, client_addr.get_port_number(), server_addr.get_port_number())); @@ -709,7 +709,7 @@ run_iovec_IPv4_api_test () client_dgram.send("hello world", sizeof("hello world"), server_addr); - ACE_DEBUG ((LM_INFO, "%s the send pkg will be received by raw sockets ...\n", __func__)); + ACE_DEBUG ((LM_INFO, "%C the send pkg will be received by raw sockets ...\n", __func__)); ACE_OS::sleep(1); ACE_INET_Addr remote; @@ -720,7 +720,7 @@ run_iovec_IPv4_api_test () ACE_INET_Addr to_addr; - ACE_DEBUG ((LM_INFO, "%s send pkg again to test common raw socket with to_adr parameter ...\n", __func__)); + ACE_DEBUG ((LM_INFO, "%C send pkg again to test common raw socket with to_adr parameter ...\n", __func__)); client_dgram.send("hello world", sizeof("hello world"), server_addr); ACE_OS::sleep(1); @@ -733,7 +733,7 @@ run_iovec_IPv4_api_test () int cmp = ACE_OS::memcmp(remote_sin_addr, to_sin_addr, sizeof(*to_sin_addr)); EXCEPTION_RETURN(cmp != 0, " raw socket got to_addr with invalid value\n"); - ACE_DEBUG ((LM_INFO, "%s test iovec send ...\n", __func__)); + ACE_DEBUG ((LM_INFO, "%C test iovec send ...\n", __func__)); readUdpSocektToEmpty(server_dgram); IPv4_HEADER_t_Ptr ptIPv4Header = (IPv4_HEADER_t_Ptr)sendbuf; @@ -831,7 +831,7 @@ run_main (int, ACE_TCHAR *argv[]) #if defined (ACE_HAS_IPV6) retval += run_ipv6_pkginfo_test(); #else - ACE_DEBUG ((LM_INFO, "%s without IPv6 macro ...\n", __func__)); + ACE_DEBUG ((LM_INFO, "%C without IPv6 macro ...\n", __func__)); #endif From 65f64f50bb67a97ef59e5f1cab369fc257e0fc6c Mon Sep 17 00:00:00 2001 From: smithAchang Date: Sun, 2 Apr 2023 09:56:14 +0800 Subject: [PATCH 18/39] fix nullptr & trailing whitespace --- ACE/ace/RAW_Socket.cpp | 54 +++---- ACE/ace/RAW_Socket.h | 34 ++--- ACE/tests/RAW_Socket_Test.cpp | 263 ++++++++++++++++------------------ 3 files changed, 166 insertions(+), 185 deletions(-) diff --git a/ACE/ace/RAW_Socket.cpp b/ACE/ace/RAW_Socket.cpp index 40405e392b3c4..3b681624cf982 100644 --- a/ACE/ace/RAW_Socket.cpp +++ b/ACE/ace/RAW_Socket.cpp @@ -34,7 +34,7 @@ ACE_ALLOC_HOOK_DEFINE (ACE_RAW_SOCKET) #if defined (ACE_HAS_IPV6) u_char padding6[ACE_CMSG_SPACE (sizeof (in6_pktinfo))]; #endif - }; + }; #endif #define ACE_SEND_EXCEPTION_RETURN() do{\ @@ -52,7 +52,7 @@ ACE_ALLOC_HOOK_DEFINE (ACE_RAW_SOCKET) if(timeout && ACE::handle_read_ready (this->get_handle (), timeout) != 1)\ return -1; \ }while(0) - + void ACE_RAW_SOCKET::dump () const @@ -76,7 +76,7 @@ ACE_RAW_SOCKET::ACE_RAW_SOCKET (ACE_INET_Addr const & local, ACELIB_ERROR ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("ACE_RAW_SOCKET"))); - + } static inline ssize_t using_common_recv(const ACE_RAW_SOCKET& raw, void *buf, size_t n, ACE_INET_Addr &addr, int flags) @@ -103,7 +103,7 @@ static inline void fillMsgHdr(msghdr& recv_msg, const ACE_INET_Addr &addr, void* #else recv_msg.msg_name = static_cast(addr.get_addr ()); #endif /* ACE_HAS_SOCKADDR_MSG_NAME */ - + recv_msg.msg_namelen = addr.get_size (); #ifdef ACE_USE_MSG_CONTROL @@ -170,7 +170,7 @@ ACE_RAW_SOCKET::recv (void *buf, ACE_RECV_EXCEPTION_RETURN(); - if(to_addr == NULL) + if(to_addr == nullptr) { return using_common_recv(*this, buf, n, addr, flags); } @@ -193,17 +193,17 @@ ACE_RAW_SOCKET::recv (void *buf, recv_msg.msg_iovlen = 1; #ifdef ACE_USE_MSG_CONTROL - union control_buffer cbuf; - if(to_addr != NULL) + union control_buffer cbuf; + if(to_addr != nullptr) { fillMsgHdr(recv_msg, addr, &cbuf, sizeof(cbuf)); } else { - fillMsgHdr(recv_msg, addr, NULL, 0); + fillMsgHdr(recv_msg, addr, nullptr, 0); } #else - fillMsgHdr(recv_msg, addr, NULL, 0); + fillMsgHdr(recv_msg, addr, nullptr, 0); #endif ssize_t const status = ACE_OS::recvmsg (this->get_handle (), @@ -214,10 +214,10 @@ ACE_RAW_SOCKET::recv (void *buf, addr.set_type ((static_cast(addr.get_addr()))->sin_family); #ifdef ACE_USE_MSG_CONTROL - if(to_addr != NULL) + if(to_addr != nullptr) getToAddrFromMsgHdr(recv_msg, *to_addr); #endif - + return status; } @@ -233,7 +233,7 @@ ACE_RAW_SOCKET::send (const void *buf, // Check the status of the current socket. ACE_SEND_EXCEPTION_RETURN(); - + sockaddr *saddr = static_cast(addr.get_addr ()); int const len = addr.get_size (); return ACE_OS::sendto (this->get_handle (), @@ -245,7 +245,7 @@ ACE_RAW_SOCKET::send (const void *buf, } #if defined (ACE_HAS_MSG) -ssize_t +ssize_t ACE_RAW_SOCKET::send (const iovec iov[], int n, const ACE_INET_Addr &addr, @@ -299,20 +299,20 @@ ACE_RAW_SOCKET::recv (iovec iov[], ACE_RECV_EXCEPTION_RETURN(); - + msghdr recv_msg = {}; recv_msg.msg_iov = static_cast(iov); recv_msg.msg_iovlen = n; /*default*/ - fillMsgHdr(recv_msg, addr, NULL, 0); + fillMsgHdr(recv_msg, addr, nullptr, 0); #ifdef ACE_USE_MSG_CONTROL - union control_buffer cbuf; - if(to_addr != NULL) + union control_buffer cbuf; + if(to_addr != nullptr) { this->get_local_addr(*to_addr); - + fillMsgHdr(recv_msg, addr, &cbuf, sizeof(cbuf)); } #endif @@ -325,16 +325,16 @@ ACE_RAW_SOCKET::recv (iovec iov[], addr.set_type ((static_cast(addr.get_addr()))->sin_family); #ifdef ACE_USE_MSG_CONTROL - if(to_addr != NULL) + if(to_addr != nullptr) getToAddrFromMsgHdr(recv_msg, *to_addr); #endif - + return status; } #else // not supported will fail immediately -ssize_t +ssize_t ACE_RAW_SOCKET::send (const iovec iov[], int n, const ACE_INET_Addr &addr, @@ -343,10 +343,10 @@ ACE_RAW_SOCKET::send (const iovec iov[], { ACE_TRACE ("ACE_RAW_SOCKET::send iovec"); - + // immediately fail when unsupported return -1; -} +} ssize_t ACE_RAW_SOCKET::recv (iovec iov[], @@ -358,7 +358,7 @@ ACE_RAW_SOCKET::recv (iovec iov[], { ACE_TRACE ("ACE_RAW_SOCKET::recv iovec"); - + // immediately fail when unsupported return -1; } @@ -382,10 +382,10 @@ ACE_RAW_SOCKET::open (ACE_INET_Addr const & local, int protocol) if(ACE_OS::bind (this->get_handle (), static_cast(local.get_addr()), local.get_addr_size()) == -1) return -1; - + this->protocol_ = protocol; - + ACE_INET_Addr bindAddr; this->get_local_addr(bindAddr); @@ -405,7 +405,7 @@ ACE_RAW_SOCKET::open (ACE_INET_Addr const & local, int protocol) } #endif - + return 0; } diff --git a/ACE/ace/RAW_Socket.h b/ACE/ace/RAW_Socket.h index 23af8362c3e4b..6575f63b59049 100644 --- a/ACE/ace/RAW_Socket.h +++ b/ACE/ace/RAW_Socket.h @@ -33,19 +33,19 @@ ACE_BEGIN_VERSIONED_NAMESPACE_DECL * @class ACE_RAW_Socket * * @brief An RAW Socket implemention class. - * + * * An RAW Socket can be used for some user-space network protocol stack. * - Setting the protocol parameter to be IPPROTO_UDP will filter all UDP protocol packages with the destination is its bound address. * It can reduce the total num of socket needed with only port difference. - * + * * - Setting the protocol parameter to be IPPROTO_SCTP will filter all SCTP protocol packages with the destination is its bound address. * It can form the basis of a user-space SCTP protocol stack in more general way. - * + * * - Setting the protocol parameter to be IPPROTO_RAW will make it as a send only socket for any customized datagram formed from the IP header to be sent. * Notice the source address can be different from its bound address if provided in the customized package or keep them default to zero. * Notice the IPPROTO_RAW RAW socket does not support fragment function when the passed package exceeds the MTU, so it need a upper layer fragment before called - * - * @note If you really want to receive all IP packets, use a packet(7) socket with the ETH_P_IP protocol. + * + * @note If you really want to receive all IP packets, use a packet(7) socket with the ETH_P_IP protocol. * For "Single Responsibility Principle" the behavior has notable difference, so the feature is not implemented here. */ class ACE_Export ACE_RAW_SOCKET : public ACE_SOCK, private ACE_Copy_Disabled @@ -53,12 +53,12 @@ class ACE_Export ACE_RAW_SOCKET : public ACE_SOCK, private ACE_Copy_Disabled public: /// Default constructor. ACE_RAW_SOCKET (); - + /// Constructor that bind a local address and fiter UDP protocol. /// @param local local IP address to bind /// @param protocol IPPROTO_UDP as default value because often used as a user-space UDP stack ACE_RAW_SOCKET (ACE_INET_Addr const & local, int protocol = IPPROTO_UDP); - + /** * @name Data transfer routines. @@ -80,7 +80,7 @@ ssize_t send (const void *buf, size_t n, const ACE_INET_Addr &addr, int flags = 0, - const ACE_Time_Value *timeout = NULL) const; + const ACE_Time_Value *timeout = nullptr) const; /** @@ -90,15 +90,15 @@ ssize_t send (const void *buf, * until action is possible, else will wait until the relative time * specified in *@a timeout elapses). If times out a -1 is * returned with @c errno == ETIME. If it succeeds the number of - * bytes received is returned. + * bytes received is returned. * The IP destination address will be placed in @a *to_addr if it is not null */ ssize_t recv (void *buf, size_t n, ACE_INET_Addr &addr, int flags = 0, - const ACE_Time_Value *timeout = NULL, - ACE_INET_Addr *to_addr = NULL) const; + const ACE_Time_Value *timeout = nullptr, + ACE_INET_Addr *to_addr = nullptr) const; /** * Send an of size @a n to the datagram socket (uses) @@ -109,7 +109,7 @@ ssize_t send (const iovec iov[], int n, const ACE_INET_Addr &addr, int flags = 0, - const ACE_Time_Value *timeout = NULL) const; + const ACE_Time_Value *timeout = nullptr) const; /** * Recv an of size @a n to the datagram socket (uses ). * The IP destination address will be placed in @a *to_addr if it is not null. @@ -119,8 +119,8 @@ ssize_t recv (iovec iov[], int n, ACE_INET_Addr &addr, int flags = 0, - const ACE_Time_Value *timeout = NULL, - ACE_INET_Addr *to_addr = NULL) const; + const ACE_Time_Value *timeout = nullptr, + ACE_INET_Addr *to_addr = nullptr) const; //@} @@ -128,15 +128,15 @@ ssize_t recv (iovec iov[], * Wrapper around the BSD-style @c socket system call (no QoS). * @param local local IP address to bind * @param protocol filter the protocol based on IP network layer. Using IPPROTO_UDP as default value for often used as a user-space UDP stack - * + * * @attention can be re-opened after closed */ - + int open (ACE_INET_Addr const & local, int protocol = IPPROTO_UDP); /// Dump the state of object. void dump () const; - + /// Return @c true if the protocol is IPPROTO_RAW. bool is_send_only () const; diff --git a/ACE/tests/RAW_Socket_Test.cpp b/ACE/tests/RAW_Socket_Test.cpp index b4aa73454316b..488b8bd8da331 100644 --- a/ACE/tests/RAW_Socket_Test.cpp +++ b/ACE/tests/RAW_Socket_Test.cpp @@ -25,29 +25,29 @@ #pragma pack(1) typedef struct _IP_HEADER_t { - uint8_t bVersionAndHeaderLen; - uint8_t bTypeOfService; - uint16_t u16TotalLenOfPacket; - uint16_t u16PacketID; - uint16_t u16Sliceinfo; - uint8_t bTTL; - uint8_t bTypeOfProtocol; - uint16_t u16CheckSum; - uint32_t u32SourIp; - uint32_t u32DestIp; + uint8_t bVersionAndHeaderLen; + uint8_t bTypeOfService; + uint16_t u16TotalLenOfPacket; + uint16_t u16PacketID; + uint16_t u16Sliceinfo; + uint8_t bTTL; + uint8_t bTypeOfProtocol; + uint16_t u16CheckSum; + uint32_t u32SourIp; + uint32_t u32DestIp; } IPv4_HEADER_t, *IPv4_HEADER_t_Ptr; typedef struct _IPv6_HEADER_t { union { - uint8_t abVTF[4]; - uint32_t dwVTF; + uint8_t abVTF[4]; + uint32_t dwVTF; }; - uint16_t u16PayloadLen; - uint8_t bNextHeader; - uint8_t bHopLimit; + uint16_t u16PayloadLen; + uint8_t bNextHeader; + uint8_t bHopLimit; union { @@ -77,8 +77,8 @@ typedef struct _UDP_HEADER_t uint32_t Checksum(uint32_t cksum, uint8_t *pBuffer, uint32_t size) { - - if ((NULL == pBuffer) || (0 == size)) + + if ((nullptr == pBuffer) || (0 == size)) { // return passed value return cksum; @@ -130,16 +130,16 @@ void udp6_header_checksum(IPv6_HEADER_t_Ptr pIpHeader, UDP_HEADER_t_Ptr ptUDPHea // ptUDPHeader->u16Length = htons(udpLen); sum += udpLen; - + //sum += (ptUDPHeader->u16Length >> 8 & 0x00FF); //sum += (ptUDPHeader->u16Length << 8 & 0xFF00); - + sum = Checksum(sum, (uint8_t *)&pIpHeader->abSrcAddr, 16); sum = Checksum(sum, (uint8_t *)&pIpHeader->abDstAddr, 16); - sum += ((uint16_t)pIpHeader->bNextHeader & 0x00FF); + sum += ((uint16_t)pIpHeader->bNextHeader & 0x00FF); //finish the pseudo header checksum - + //udp section ptUDPHeader->u16CheckSum = 0; sum = Checksum(sum, (uint8_t *)ptUDPHeader, udpLen); @@ -150,8 +150,8 @@ void udp6_header_checksum(IPv6_HEADER_t_Ptr pIpHeader, UDP_HEADER_t_Ptr ptUDPHea -class SockGuard : private ACE_Copy_Disabled -{ +class SockGuard : private ACE_Copy_Disabled +{ public: SockGuard(ACE_SOCK& sock):sock_(sock){}; ~SockGuard(){ sock_.close(); }; @@ -161,7 +161,7 @@ SockGuard(ACE_SOCK& sock):sock_(sock){}; }; -#define EXCEPTION_RETURN(expression, message)\ +#define ACE_TEST_EXCEPTION_RETURN(expression, message)\ do{\ if(expression)\ {\ @@ -183,37 +183,37 @@ static char recvbuf[4096]; static int run_option_test () { - + ACE_DEBUG ((LM_INFO, "%C begin to run ...\n", __func__)); ACE_INET_Addr addr(u_short(0), "127.0.0.1"); ACE_RAW_SOCKET rawSocket(addr); SockGuard guard(rawSocket); - EXCEPTION_RETURN(rawSocket.get_handle() == ACE_INVALID_HANDLE, " can not bind the addr\n"); + ACE_TEST_EXCEPTION_RETURN(rawSocket.get_handle() == ACE_INVALID_HANDLE, " can not bind the addr\n"); int rc = rawSocket.enable(ACE_NONBLOCK); - EXCEPTION_RETURN(rc < 0, " can not bind the addr\n"); + ACE_TEST_EXCEPTION_RETURN(rc < 0, " can not bind the addr\n"); int optval = 0, optlen = sizeof(optval); rc = rawSocket.get_option(SOL_SOCKET, SO_RCVBUF, &optval, &optlen); - - EXCEPTION_RETURN(rc < 0, " get SO_RCVBUF in failure\n"); + + ACE_TEST_EXCEPTION_RETURN(rc < 0, " get SO_RCVBUF in failure\n"); optlen = sizeof(optval); int new_optval = optval << 1; rc = rawSocket.set_option(SOL_SOCKET, SO_RCVBUF, &new_optval, sizeof(new_optval)); - EXCEPTION_RETURN(rc < 0, " set SO_RCVBUF new value in failure\n"); + ACE_TEST_EXCEPTION_RETURN(rc < 0, " set SO_RCVBUF new value in failure\n"); new_optval = 0; optlen = sizeof(new_optval); rawSocket.get_option(SOL_SOCKET, SO_RCVBUF, &new_optval, &optlen); - EXCEPTION_RETURN(new_optval < optval, " set SO_RCVBUF with no effect\n"); + ACE_TEST_EXCEPTION_RETURN(new_optval < optval, " set SO_RCVBUF with no effect\n"); ACE_DEBUG ((LM_INFO, "old optval: %d, new optval ...\n", optval, new_optval)); @@ -224,26 +224,26 @@ run_option_test () static int run_reopen_test () { - + ACE_DEBUG ((LM_INFO, "%C begin to run ...\n", __func__)); - + ACE_INET_Addr addr((u_short)0, "127.0.0.1"); ACE_RAW_SOCKET rawSocket(addr); SockGuard guard(rawSocket); - EXCEPTION_RETURN(rawSocket.get_handle() == ACE_INVALID_HANDLE, " can not bind the addr\n"); + ACE_TEST_EXCEPTION_RETURN(rawSocket.get_handle() == ACE_INVALID_HANDLE, " can not bind the addr\n"); rawSocket.close(); - EXCEPTION_RETURN(rawSocket.get_handle() != ACE_INVALID_HANDLE, " close in failure\n"); + ACE_TEST_EXCEPTION_RETURN(rawSocket.get_handle() != ACE_INVALID_HANDLE, " close in failure\n"); ACE_INET_Addr addr2((u_short)0, "127.0.0.8"); int rc = rawSocket.open(addr2); - EXCEPTION_RETURN(rc < 0, " reopen in failue\n"); - EXCEPTION_RETURN(rawSocket.get_handle() == ACE_INVALID_HANDLE, " handle is invalid after re-open\n"); - + ACE_TEST_EXCEPTION_RETURN(rc < 0, " reopen in failue\n"); + ACE_TEST_EXCEPTION_RETURN(rawSocket.get_handle() == ACE_INVALID_HANDLE, " handle is invalid after re-open\n"); + return 0; } @@ -260,14 +260,14 @@ static void readUdpSocektToEmpty(ACE_SOCK_Dgram& udpSock) } -static int raw_recv_data_until_meet_condition(ACE_RAW_SOCKET& raw, u_short port, size_t n, ACE_INET_Addr& remote, bool bUseIOVec = false, ACE_INET_Addr* to_addr = NULL) +static int raw_recv_data_until_meet_condition(ACE_RAW_SOCKET& raw, u_short port, size_t n, ACE_INET_Addr& remote, bool bUseIOVec = false, ACE_INET_Addr* to_addr = nullptr) { ACE_INET_Addr local; raw.get_local_addr(local); ssize_t len = 0; ssize_t expectedLen; - + do { ACE_OS::memset(recvbuf, 0, sizeof(recvbuf)); @@ -286,29 +286,27 @@ static int raw_recv_data_until_meet_condition(ACE_RAW_SOCKET& raw, u_short port, vec[i].iov_base = &recvbuf[i]; vec[i].iov_len = sizeof(recvbuf) - oneByteRecvVecNum; - if(to_addr == NULL) + if(to_addr == nullptr) { raw.recv(vec, (int)(sizeof(vec)/sizeof(vec[0])) , remote); } else { - len = raw.recv(vec, (int)(sizeof(vec)/sizeof(vec[0])), remote, 0/*flags*/, NULL, to_addr); - } + len = raw.recv(vec, (int)(sizeof(vec)/sizeof(vec[0])), remote, 0/*flags*/, nullptr, to_addr); + } } else { - if(to_addr == NULL) + if(to_addr == nullptr) { - len = raw.recv(recvbuf, sizeof(recvbuf), remote); + len = raw.recv(recvbuf, sizeof(recvbuf), remote); } else { - len = raw.recv(recvbuf, sizeof(recvbuf), remote, 0/*flags*/, NULL, to_addr); + len = raw.recv(recvbuf, sizeof(recvbuf), remote, 0/*flags*/, nullptr, to_addr); } } - - if(len < 0) { @@ -341,11 +339,11 @@ static int raw_recv_data_until_meet_condition(ACE_RAW_SOCKET& raw, u_short port, break; } } - + ACE_DEBUG ((LM_DEBUG, "%C recv unexpected pkgs len: %d, srcPort: %u, dstPort:%u; expectedLen: %d, expectedPort: %u ...\n", __func__, len, ntohs(ptUDPHeader->u16SrcPort), ntohs(ptUDPHeader->u16DstPort), expectedLen, port)); ACE_OS::sleep(1); } while (1); - + return 0; } @@ -363,13 +361,13 @@ run_raw_udp_test_child_flow_sendby_self (ACE_RAW_SOCKET& raw, ACE_INET_Addr& cli int rc = raw.send(ptUDPHeader, n + sizeof(UDP_HEADER_t), server_addr); ssize_t expectedLen = n + sizeof(UDP_HEADER_t); - EXCEPTION_RETURN(rc != expectedLen, " raw socket can not send test pkg to server\n"); + ACE_TEST_EXCEPTION_RETURN(rc != expectedLen, " raw socket can not send test pkg to server\n"); u_short server_port = server_addr.get_port_number(); ACE_INET_Addr remote; rc = raw_recv_data_until_meet_condition(raw, server_port, n, remote); - EXCEPTION_RETURN(rc != 0, " can recv test pkg from raw socket\n"); - + ACE_TEST_EXCEPTION_RETURN(rc != 0, " can recv test pkg from raw socket\n"); + return 0; } @@ -379,7 +377,7 @@ run_raw_udp_test () ACE_DEBUG ((LM_INFO, "%C begin to run using the port auto assigned by OS to avoid port conflict ...\n", __func__)); ACE_INET_Addr addr((u_short)0, "127.0.0.1"); - + ACE_SOCK_Dgram dgram(addr); SockGuard dgram_guard(dgram); @@ -392,36 +390,36 @@ run_raw_udp_test () ACE_INET_Addr client_addr ,server_addr,remote; int rc = dgram.get_local_addr (client_addr); - EXCEPTION_RETURN(rc < 0, " can not get client bound address\n"); + ACE_TEST_EXCEPTION_RETURN(rc < 0, " can not get client bound address\n"); rc = dgram_server.get_local_addr (server_addr); - EXCEPTION_RETURN(rc < 0, " can not get server address\n"); + ACE_TEST_EXCEPTION_RETURN(rc < 0, " can not get server address\n"); ssize_t n = 512; rc = dgram.send(sendbuf, n, server_addr); - EXCEPTION_RETURN(rc != n, " can send test pkg to server\n"); + ACE_TEST_EXCEPTION_RETURN(rc != n, " can send test pkg to server\n"); rc = dgram_server.recv(recvbuf, sizeof(recvbuf), remote); - EXCEPTION_RETURN(rc != n, " server socket can not recv the pkg client has send. It will be recv lately by raw socket\n"); + ACE_TEST_EXCEPTION_RETURN(rc != n, " server socket can not recv the pkg client has send. It will be recv lately by raw socket\n"); u_short server_port = server_addr.get_port_number(); rc = raw_recv_data_until_meet_condition(rawSocket, server_port, n, remote); - EXCEPTION_RETURN(rc != 0, " can recv test pkg from raw socket\n"); + ACE_TEST_EXCEPTION_RETURN(rc != 0, " can recv test pkg from raw socket\n"); rc = run_raw_udp_test_child_flow_sendby_self (rawSocket, client_addr, server_addr, n + 1); - EXCEPTION_RETURN(rc != 0, " can recv test pkg from raw socket when sending by self\n"); + ACE_TEST_EXCEPTION_RETURN(rc != 0, " can recv test pkg from raw socket when sending by self\n"); - #if !defined (ACE_WIN32) + #if !defined (ACE_WIN32) if(ACE_OS::getuid() == 0) { ACE_DEBUG ((LM_INFO, "%C test send & recv big pkt ...\n", __func__)); rc = run_raw_udp_test_child_flow_sendby_self (rawSocket, client_addr, server_addr, n + 2048); - EXCEPTION_RETURN(rc != 0, " can recv test pkg from raw socket when sending big pkg by self\n"); + ACE_TEST_EXCEPTION_RETURN(rc != 0, " can recv test pkg from raw socket when sending big pkg by self\n"); } #endif - + return 0; } @@ -441,12 +439,12 @@ run_raw_generic_test () ACE_RAW_SOCKET rawSocket(bindAddr, IPPROTO_RAW); SockGuard raw_guard(rawSocket); - EXCEPTION_RETURN(rawSocket.is_send_only() == false, " raw socket is not send only\n"); + ACE_TEST_EXCEPTION_RETURN(rawSocket.is_send_only() == false, " raw socket is not send only\n"); ssize_t len = rawSocket.recv(recvbuf, sizeof(recvbuf), remote); - EXCEPTION_RETURN(len != -1, " raw generic socket is send only , must not can recv data\n"); + ACE_TEST_EXCEPTION_RETURN(len != -1, " raw generic socket is send only , must not can recv data\n"); + - client_dgram.get_local_addr (client_addr); server_dgram.get_local_addr (server_addr); @@ -458,9 +456,9 @@ run_raw_generic_test () ptIPv4Header->bVersionAndHeaderLen = 0x45; ptIPv4Header->u16TotalLenOfPacket = htons(sizeof(IPv4_HEADER_t) + sizeof(UDP_HEADER_t) + n); // 数据包长度 - ptIPv4Header->u16PacketID = ACE_OS::rand(); + ptIPv4Header->u16PacketID = ACE_OS::rand(); ptIPv4Header->bTTL = 64; - ptIPv4Header->bTypeOfProtocol = IPPROTO_UDP; + ptIPv4Header->bTypeOfProtocol = IPPROTO_UDP; ptIPv4Header->u16CheckSum = 0; ptIPv4Header->u32SourIp = (static_cast(client_addr.get_addr()))->sin_addr.s_addr; ptIPv4Header->u32DestIp = (static_cast(server_addr.get_addr()))->sin_addr.s_addr; @@ -474,22 +472,22 @@ run_raw_generic_test () ptUDPHeader->u16Length = htons(sizeof(UDP_HEADER_t) + n); ptUDPHeader->u16CheckSum = 0; - #if !defined (ACE_WIN32) + #if !defined (ACE_WIN32) if(ACE_OS::getuid() == 0) { ACE_DEBUG ((LM_INFO, "%C raw generic socket will send bytes exceeding the MTU ...\n", __func__)); n = 2048; len = rawSocket.send(sendbuf, sizeof(IPv4_HEADER_t) + sizeof(UDP_HEADER_t) + n, server_addr); - EXCEPTION_RETURN(len != -1, " raw generic socket can not send pkg more than MTU\n"); + ACE_TEST_EXCEPTION_RETURN(len != -1, " raw generic socket can not send pkg more than MTU\n"); } #endif - + n = 468; ptUDPHeader->u16Length = htons(sizeof(UDP_HEADER_t) + n); len = rawSocket.send(sendbuf, sizeof(IPv4_HEADER_t) + sizeof(UDP_HEADER_t) + n, server_addr); size_t expectedLen = (sizeof(IPv4_HEADER_t) + sizeof(UDP_HEADER_t) + n); ACE_DEBUG ((LM_INFO, "%C raw generic socket send %d bytes, expected %u bytes ...\n", __func__, len, expectedLen)); - EXCEPTION_RETURN(static_cast(len) != expectedLen, " raw generic socket send pkg in failure\n"); + ACE_TEST_EXCEPTION_RETURN(static_cast(len) != expectedLen, " raw generic socket send pkg in failure\n"); ACE_OS::sleep(1); ACE_DEBUG ((LM_INFO, "%C enable nonblock status ...\n", __func__)); @@ -497,9 +495,9 @@ run_raw_generic_test () len = server_dgram.recv(recvbuf, sizeof(recvbuf), remote); expectedLen = n; ACE_DEBUG ((LM_INFO, "%C udp server socket recv %d bytes, expected %u bytes ...\n", __func__, len, expectedLen)); - EXCEPTION_RETURN(static_cast(len) != n, " server socket receives pkg in failure length is not the same\n"); - EXCEPTION_RETURN(static_cast(remote.get_addr())->sin_addr.s_addr != static_cast(client_addr.get_addr())->sin_addr.s_addr, " server socket receives pkg in failure: the source IP is not the same\n"); - + ACE_TEST_EXCEPTION_RETURN(static_cast(len) != n, " server socket receives pkg in failure length is not the same\n"); + ACE_TEST_EXCEPTION_RETURN(static_cast(remote.get_addr())->sin_addr.s_addr != static_cast(client_addr.get_addr())->sin_addr.s_addr, " server socket receives pkg in failure: the source IP is not the same\n"); + return 0; } @@ -514,7 +512,7 @@ run_ipv6_pkginfo_test () ACE_INET_Addr client_addr((u_short)0, "::1") ,server_addr((u_short)0, "::1"); - + ACE_SOCK_Dgram client_dgram(client_addr); SockGuard client_dgram_guard(client_dgram); client_dgram.get_local_addr(client_addr); @@ -525,8 +523,6 @@ run_ipv6_pkginfo_test () ACE_DEBUG ((LM_INFO, "%C get the real bound addr and port client_port: %u, server_port: %u...\n", __func__, client_addr.get_port_number(), server_addr.get_port_number())); - - ACE_RAW_SOCKET rawSocket(bindAddr, IPPROTO_UDP); rawSocket.enable(ACE_NONBLOCK); SockGuard raw_guard(rawSocket); @@ -534,40 +530,38 @@ run_ipv6_pkginfo_test () ACE_RAW_SOCKET rawWildcardSocket(anyAddr, IPPROTO_UDP); rawWildcardSocket.enable(ACE_NONBLOCK); SockGuard raw_wildcard_guard(rawWildcardSocket); - + client_dgram.send("hello world", sizeof("hello world"), server_addr); - + ACE_DEBUG ((LM_INFO, "%C the send pkg will be received by two raw sockets ...\n", __func__)); ACE_OS::sleep(1); - ACE_INET_Addr remote; + ACE_INET_Addr remote; int rc = raw_recv_data_until_meet_condition(rawSocket, server_addr.get_port_number(), sizeof("hello world"), remote); - - EXCEPTION_RETURN(rc != 0, " non wildcard raw socket can not recv expectedRecvLen\n"); + + ACE_TEST_EXCEPTION_RETURN(rc != 0, " non wildcard raw socket can not recv expectedRecvLen\n"); ACE_INET_Addr to_addr; ACE_DEBUG ((LM_INFO, "%C send pkg again to test common raw socket with to_adr parameter ...\n", __func__)); client_dgram.send("hello world", sizeof("hello world"), server_addr); ACE_OS::sleep(1); - + rc = raw_recv_data_until_meet_condition(rawSocket, server_addr.get_port_number(), sizeof("hello world"), remote, false, &to_addr); - EXCEPTION_RETURN(rc != 0, " non wildcard raw socket can not recv expectedRecvLen with to_addr parameter\n"); - + ACE_TEST_EXCEPTION_RETURN(rc != 0, " non wildcard raw socket can not recv expectedRecvLen with to_addr parameter\n"); + in6_addr* remote_sin6_addr = &(static_cast(remote.get_addr())->sin6_addr); in6_addr* to_sin6_addr = &(static_cast(to_addr.get_addr())->sin6_addr); int cmp = ACE_OS::memcmp(remote_sin6_addr, to_sin6_addr, sizeof(*to_sin6_addr)); - EXCEPTION_RETURN(cmp != 0, " non wildcard raw socket got to_addr with invalid value\n"); + ACE_TEST_EXCEPTION_RETURN(cmp != 0, " non wildcard raw socket got to_addr with invalid value\n"); - rc = raw_recv_data_until_meet_condition(rawWildcardSocket, server_addr.get_port_number(), sizeof("hello world"), remote, false, &to_addr); - EXCEPTION_RETURN(rc != 0, " can not recv expectedRecvLen with to_addr when provided to wildcard RAW socket\n"); + ACE_TEST_EXCEPTION_RETURN(rc != 0, " can not recv expectedRecvLen with to_addr when provided to wildcard RAW socket\n"); remote_sin6_addr = &(static_cast(remote.get_addr())->sin6_addr); to_sin6_addr = &(static_cast(to_addr.get_addr())->sin6_addr); cmp = ACE_OS::memcmp(remote_sin6_addr, to_sin6_addr, sizeof(*to_sin6_addr)); - EXCEPTION_RETURN(cmp != 0, " to_addr with invalid value when provided to wildcard RAW socket\n"); - - + ACE_TEST_EXCEPTION_RETURN(cmp != 0, " to_addr with invalid value when provided to wildcard RAW socket\n"); + return 0; } #endif @@ -582,7 +576,6 @@ run_iovec_IPv6_api_test () ACE_INET_Addr client_addr((u_short)0, "::1") ,server_addr((u_short)0, "::1"); - ACE_SOCK_Dgram client_dgram(client_addr); SockGuard client_dgram_guard(client_dgram); client_dgram.get_local_addr(client_addr); @@ -594,50 +587,47 @@ run_iovec_IPv6_api_test () ACE_DEBUG ((LM_INFO, "%C get the real bound addr and port client_port: %u, server_port: %u...\n", __func__, client_addr.get_port_number(), server_addr.get_port_number())); - - ACE_RAW_SOCKET rawSocket(bindAddr, IPPROTO_UDP); rawSocket.enable(ACE_NONBLOCK); SockGuard raw_guard(rawSocket); client_dgram.send("hello world", sizeof("hello world"), server_addr); - + ACE_DEBUG ((LM_INFO, "%C the send pkg will be received by raw sockets ...\n", __func__)); ACE_OS::sleep(1); - ACE_INET_Addr remote; + ACE_INET_Addr remote; int rc = raw_recv_data_until_meet_condition(rawSocket, server_addr.get_port_number(), sizeof("hello world"), remote); - - EXCEPTION_RETURN(rc != 0, " raw socket can not recv expectedRecvLen\n"); - EXCEPTION_RETURN(ACE_OS::strncmp(recvbuf + sizeof(UDP_HEADER_t), "hello world", sizeof("hello world")) != 0, " non wildcard raw socket can not recv expected content\n"); + + ACE_TEST_EXCEPTION_RETURN(rc != 0, " raw socket can not recv expectedRecvLen\n"); + ACE_TEST_EXCEPTION_RETURN(ACE_OS::strncmp(recvbuf + sizeof(UDP_HEADER_t), "hello world", sizeof("hello world")) != 0, " non wildcard raw socket can not recv expected content\n"); ACE_INET_Addr to_addr; ACE_DEBUG ((LM_INFO, "%C send pkg again to test common raw socket with to_adr parameter ...\n", __func__)); client_dgram.send("hello world", sizeof("hello world"), server_addr); ACE_OS::sleep(1); - + rc = raw_recv_data_until_meet_condition(rawSocket, server_addr.get_port_number(), sizeof("hello world"), remote, true, &to_addr); - EXCEPTION_RETURN(rc != 0, " raw socket can not recv expectedRecvLen with to_addr parameter\n"); - EXCEPTION_RETURN(ACE_OS::strncmp(recvbuf + sizeof(UDP_HEADER_t), "hello world", sizeof("hello world")) != 0, " raw socket can not recv expected content with to_addr parameter\n"); + ACE_TEST_EXCEPTION_RETURN(rc != 0, " raw socket can not recv expectedRecvLen with to_addr parameter\n"); + ACE_TEST_EXCEPTION_RETURN(ACE_OS::strncmp(recvbuf + sizeof(UDP_HEADER_t), "hello world", sizeof("hello world")) != 0, " raw socket can not recv expected content with to_addr parameter\n"); in6_addr* remote_sin6_addr = &(static_cast(remote.get_addr())->sin6_addr); in6_addr* to_sin6_addr = &(static_cast(to_addr.get_addr())->sin6_addr); int cmp = ACE_OS::memcmp(remote_sin6_addr, to_sin6_addr, sizeof(*to_sin6_addr)); - EXCEPTION_RETURN(cmp != 0, " raw socket got to_addr with invalid value\n"); - + ACE_TEST_EXCEPTION_RETURN(cmp != 0, " raw socket got to_addr with invalid value\n"); + ACE_DEBUG ((LM_INFO, "%C test iovec send ...\n", __func__)); - IPv6_HEADER_t_Ptr ptIPv6Header = (IPv6_HEADER_t_Ptr)sendbuf; UDP_HEADER_t_Ptr ptUDPHeader = (UDP_HEADER_t_Ptr)(sendbuf + sizeof(IPv6_HEADER_t)); u_short n = sizeof("hello world"); *ptIPv6Header = {}; - ptIPv6Header->bNextHeader = IPPROTO_UDP; + ptIPv6Header->bNextHeader = IPPROTO_UDP; memcpy(ptIPv6Header->abSrcAddr, static_cast(client_addr.get_addr())->sin6_addr.s6_addr, 16); memcpy(ptIPv6Header->abDstAddr, static_cast(server_addr.get_addr())->sin6_addr.s6_addr, 16); - + u_short client_port_number = client_addr.get_port_number(); u_short server_port_number = server_addr.get_port_number(); @@ -647,12 +637,11 @@ run_iovec_IPv6_api_test () //fill content ACE_OS::strcpy(sendbuf + sizeof(IPv6_HEADER_t) + sizeof(UDP_HEADER_t), "hello world"); udp6_header_checksum(ptIPv6Header, ptUDPHeader, sizeof(UDP_HEADER_t) + n); - iovec iov_udp[2]; iov_udp[0].iov_base = reinterpret_cast(ptUDPHeader); iov_udp[0].iov_len = sizeof(UDP_HEADER_t); - #if defined (ACE_WIN32) + #if defined (ACE_WIN32) iov_udp[1].iov_base = "hello world"; iov_udp[1].iov_len = sizeof("hello world"); #else @@ -663,7 +652,7 @@ run_iovec_IPv6_api_test () ACE_DEBUG ((LM_INFO, "%C test iovec using common udp6 socket ...\n", __func__)); rc = client_dgram.send(iov_udp, (int)(sizeof(iov_udp)/sizeof(iov_udp[0])), server_addr); - EXCEPTION_RETURN(rc == -1, " udp6 socket can not send using iov \n"); + ACE_TEST_EXCEPTION_RETURN(rc == -1, " udp6 socket can not send using iov \n"); readUdpSocektToEmpty(server_dgram); ACE_INET_Addr iov_server_addr(server_addr); @@ -671,12 +660,13 @@ run_iovec_IPv6_api_test () ACE_DEBUG ((LM_INFO, "%C must set port to zero ??? ...\n", __func__)); iov_server_addr.set_port_number(0); rc = rawSocket.send(iov_udp, (int)(sizeof(iov_udp)/sizeof(iov_udp[0])), iov_server_addr); - EXCEPTION_RETURN(rc == -1, " raw6 socket can not send using iov \n"); + ACE_TEST_EXCEPTION_RETURN(rc == -1, " raw6 socket can not send using iov \n"); ACE_OS::sleep(1); rc = server_dgram.recv(recvbuf, sizeof(recvbuf), remote); - EXCEPTION_RETURN(rc == -1, " server socket6 can not recv pkg by iov send\n"); - EXCEPTION_RETURN(ACE_OS::strcmp(recvbuf, "hello world") != 0, " the content of server socket6 receive pkg is not expected by iov send\n"); + ACE_TEST_EXCEPTION_RETURN(rc == -1, " server socket6 can not recv pkg by iov send\n"); + ACE_TEST_EXCEPTION_RETURN(ACE_OS::strcmp(recvbuf, "hello world") != 0, " the content of server socket6 receive pkg is not expected by iov send\n"); + return 0; } @@ -689,7 +679,6 @@ run_iovec_IPv4_api_test () ACE_INET_Addr client_addr((u_short)0, "127.0.0.1") ,server_addr((u_short)0, "127.0.0.1"); - ACE_SOCK_Dgram client_dgram(client_addr); SockGuard client_dgram_guard(client_dgram); client_dgram.get_local_addr(client_addr); @@ -701,37 +690,35 @@ run_iovec_IPv4_api_test () ACE_DEBUG ((LM_INFO, "%C get the real bound addr and port client_port: %u, server_port: %u...\n", __func__, client_addr.get_port_number(), server_addr.get_port_number())); - - ACE_RAW_SOCKET rawSocket(bindAddr, IPPROTO_UDP); rawSocket.enable(ACE_NONBLOCK); SockGuard raw_guard(rawSocket); client_dgram.send("hello world", sizeof("hello world"), server_addr); - + ACE_DEBUG ((LM_INFO, "%C the send pkg will be received by raw sockets ...\n", __func__)); ACE_OS::sleep(1); - ACE_INET_Addr remote; + ACE_INET_Addr remote; int rc = raw_recv_data_until_meet_condition(rawSocket, server_addr.get_port_number(), sizeof("hello world"), remote); - - EXCEPTION_RETURN(rc != 0, " raw socket can not recv expectedRecvLen\n"); - EXCEPTION_RETURN(ACE_OS::strncmp(recvbuf + sizeof(IPv4_HEADER_t) + sizeof(UDP_HEADER_t), "hello world", sizeof("hello world")) != 0, " non wildcard raw socket can not recv expected content\n"); + + ACE_TEST_EXCEPTION_RETURN(rc != 0, " raw socket can not recv expectedRecvLen\n"); + ACE_TEST_EXCEPTION_RETURN(ACE_OS::strncmp(recvbuf + sizeof(IPv4_HEADER_t) + sizeof(UDP_HEADER_t), "hello world", sizeof("hello world")) != 0, " non wildcard raw socket can not recv expected content\n"); ACE_INET_Addr to_addr; ACE_DEBUG ((LM_INFO, "%C send pkg again to test common raw socket with to_adr parameter ...\n", __func__)); client_dgram.send("hello world", sizeof("hello world"), server_addr); ACE_OS::sleep(1); - + rc = raw_recv_data_until_meet_condition(rawSocket, server_addr.get_port_number(), sizeof("hello world"), remote, true, &to_addr); - EXCEPTION_RETURN(rc != 0, " raw socket can not recv expectedRecvLen with to_addr parameter\n"); - EXCEPTION_RETURN(ACE_OS::strncmp(recvbuf + sizeof(IPv4_HEADER_t) + sizeof(UDP_HEADER_t), "hello world", sizeof("hello world")) != 0, " raw socket can not recv expected content with to_addr parameter\n"); + ACE_TEST_EXCEPTION_RETURN(rc != 0, " raw socket can not recv expectedRecvLen with to_addr parameter\n"); + ACE_TEST_EXCEPTION_RETURN(ACE_OS::strncmp(recvbuf + sizeof(IPv4_HEADER_t) + sizeof(UDP_HEADER_t), "hello world", sizeof("hello world")) != 0, " raw socket can not recv expected content with to_addr parameter\n"); in_addr* remote_sin_addr = &(static_cast(remote.get_addr())->sin_addr); in_addr* to_sin_addr = &(static_cast(to_addr.get_addr())->sin_addr); int cmp = ACE_OS::memcmp(remote_sin_addr, to_sin_addr, sizeof(*to_sin_addr)); - EXCEPTION_RETURN(cmp != 0, " raw socket got to_addr with invalid value\n"); + ACE_TEST_EXCEPTION_RETURN(cmp != 0, " raw socket got to_addr with invalid value\n"); ACE_DEBUG ((LM_INFO, "%C test iovec send ...\n", __func__)); readUdpSocektToEmpty(server_dgram); @@ -744,9 +731,9 @@ run_iovec_IPv4_api_test () ptIPv4Header->bVersionAndHeaderLen = 0x45; ptIPv4Header->u16TotalLenOfPacket = htons(sizeof(IPv4_HEADER_t) + sizeof(UDP_HEADER_t) + n); // 数据包长度 - ptIPv4Header->u16PacketID = ACE_OS::rand(); + ptIPv4Header->u16PacketID = ACE_OS::rand(); ptIPv4Header->bTTL = 64; - ptIPv4Header->bTypeOfProtocol = IPPROTO_UDP; + ptIPv4Header->bTypeOfProtocol = IPPROTO_UDP; ptIPv4Header->u16CheckSum = 0; ptIPv4Header->u32SourIp = (static_cast(client_addr.get_addr()))->sin_addr.s_addr; ptIPv4Header->u32DestIp = (static_cast(server_addr.get_addr()))->sin_addr.s_addr; @@ -764,7 +751,7 @@ run_iovec_IPv4_api_test () iovec iov_udp[2]; iov_udp[0].iov_base = reinterpret_cast(ptUDPHeader); iov_udp[0].iov_len = sizeof(UDP_HEADER_t); - #if defined (ACE_WIN32) + #if defined (ACE_WIN32) iov_udp[1].iov_base = "hello world"; iov_udp[1].iov_len = sizeof("hello world"); #else @@ -773,14 +760,13 @@ run_iovec_IPv4_api_test () #endif rc = rawSocket.send(iov_udp, (int)(sizeof(iov_udp)/sizeof(iov_udp[0])), server_addr); - EXCEPTION_RETURN(rc == -1, " raw4 socket can send using iov\n"); + ACE_TEST_EXCEPTION_RETURN(rc == -1, " raw4 socket can send using iov\n"); ACE_OS::sleep(1); rc = server_dgram.recv(recvbuf, sizeof(recvbuf), remote); - EXCEPTION_RETURN(rc == -1, " server socket4 can not recv pkg by iov send\n"); - EXCEPTION_RETURN(ACE_OS::strcmp(recvbuf, "hello world") != 0, " the content of server socket4 receive pkg is not expected by iov send\n"); + ACE_TEST_EXCEPTION_RETURN(rc == -1, " server socket4 can not recv pkg by iov send\n"); + ACE_TEST_EXCEPTION_RETURN(ACE_OS::strcmp(recvbuf, "hello world") != 0, " the content of server socket4 receive pkg is not expected by iov send\n"); - return 0; } @@ -793,7 +779,7 @@ run_main (int, ACE_TCHAR *argv[]) int oldMTU = 1500; -#if !defined (ACE_WIN32) +#if !defined (ACE_WIN32) // set the lo interface MTU if(ACE_OS::getuid() == 0) { @@ -813,11 +799,9 @@ run_main (int, ACE_TCHAR *argv[]) tReq.ifr_mtu = 0; ACE_OS::ioctl(netdevice.get_handle(), SIOCGIFMTU, &tReq); - EXCEPTION_RETURN(tReq.ifr_mtu != 1400, " can set MTU for lo interface\n"); + ACE_TEST_EXCEPTION_RETURN(tReq.ifr_mtu != 1400, " can set MTU for lo interface\n"); } #endif - - // Run the tests for each type of ordering. retval = run_option_test (); @@ -826,18 +810,16 @@ run_main (int, ACE_TCHAR *argv[]) retval += run_raw_generic_test(); retval += run_iovec_IPv4_api_test(); retval += run_iovec_IPv6_api_test(); - #if defined (ACE_HAS_IPV6) retval += run_ipv6_pkginfo_test(); #else ACE_DEBUG ((LM_INFO, "%C without IPv6 macro ...\n", __func__)); #endif - ACE_END_TEST; -#if !defined (ACE_WIN32) +#if !defined (ACE_WIN32) if(ACE_OS::getuid() == 0) { ACE_INET_Addr anyAddr((u_short)0); @@ -850,7 +832,6 @@ run_main (int, ACE_TCHAR *argv[]) ACE_OS::ioctl(netdevice.get_handle(), SIOCSIFMTU, &tReq); } #endif - return retval; } From d2d8376b1490bdcfe716adfb416dc4a9478a3e1f Mon Sep 17 00:00:00 2001 From: SmithAchang Date: Sun, 2 Apr 2023 10:36:09 +0800 Subject: [PATCH 19/39] windows compile check --- ACE/tests/RAW_Socket_Test.cpp | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/ACE/tests/RAW_Socket_Test.cpp b/ACE/tests/RAW_Socket_Test.cpp index 488b8bd8da331..b07a3f0582892 100644 --- a/ACE/tests/RAW_Socket_Test.cpp +++ b/ACE/tests/RAW_Socket_Test.cpp @@ -641,13 +641,9 @@ run_iovec_IPv6_api_test () iovec iov_udp[2]; iov_udp[0].iov_base = reinterpret_cast(ptUDPHeader); iov_udp[0].iov_len = sizeof(UDP_HEADER_t); - #if defined (ACE_WIN32) - iov_udp[1].iov_base = "hello world"; - iov_udp[1].iov_len = sizeof("hello world"); - #else iov_udp[1].iov_base = const_cast("hello world"); iov_udp[1].iov_len = sizeof("hello world"); - #endif + ACE_DEBUG ((LM_INFO, "%C test iovec using common udp6 socket ...\n", __func__)); @@ -751,13 +747,8 @@ run_iovec_IPv4_api_test () iovec iov_udp[2]; iov_udp[0].iov_base = reinterpret_cast(ptUDPHeader); iov_udp[0].iov_len = sizeof(UDP_HEADER_t); - #if defined (ACE_WIN32) - iov_udp[1].iov_base = "hello world"; - iov_udp[1].iov_len = sizeof("hello world"); - #else iov_udp[1].iov_base = const_cast("hello world"); iov_udp[1].iov_len = sizeof("hello world"); - #endif rc = rawSocket.send(iov_udp, (int)(sizeof(iov_udp)/sizeof(iov_udp[0])), server_addr); ACE_TEST_EXCEPTION_RETURN(rc == -1, " raw4 socket can send using iov\n"); From 44d171f4f36c8797f60b64545dbb7720165c4690 Mon Sep 17 00:00:00 2001 From: smithAchang Date: Sun, 2 Apr 2023 20:53:11 +0800 Subject: [PATCH 20/39] avoid ace_for_tao compile because the minimal requirement --- ACE/tests/RAW_Socket_Test.cpp | 2 +- ACE/tests/tests.mpc | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/ACE/tests/RAW_Socket_Test.cpp b/ACE/tests/RAW_Socket_Test.cpp index b07a3f0582892..186e6d7a147c0 100644 --- a/ACE/tests/RAW_Socket_Test.cpp +++ b/ACE/tests/RAW_Socket_Test.cpp @@ -90,7 +90,7 @@ uint32_t Checksum(uint32_t cksum, uint8_t *pBuffer, uint32_t size) while (size > 1) { - cksum += ((uint16_t)p[num] << 8 & 0xff00) | (uint16_t)p[num + 1] & 0x00FF; + cksum += ((uint16_t)p[num] << 8 & 0xff00) | ((uint16_t)p[num + 1] & 0x00FF); size -= 2; num += 2; } diff --git a/ACE/tests/tests.mpc b/ACE/tests/tests.mpc index f241e6fe34183..0ddaa5d56ca2d 100644 --- a/ACE/tests/tests.mpc +++ b/ACE/tests/tests.mpc @@ -1420,6 +1420,7 @@ project(Pipe Test) : acetest { } project(RAW Socket Test) : acetest { + avoids += ace_for_tao exename = RAW_Socket_Test Source_Files { RAW_Socket_Test.cpp From a893284b14bc29684525c3daf1797ef3146ab2b4 Mon Sep 17 00:00:00 2001 From: smithAchang Date: Mon, 3 Apr 2023 20:07:46 +0800 Subject: [PATCH 21/39] fix Codacy Static Code Analysis --- ACE/ace/RAW_Socket.cpp | 20 ++++++++++---------- ACE/tests/RAW_Socket_Test.cpp | 27 ++++++++++++++------------- 2 files changed, 24 insertions(+), 23 deletions(-) diff --git a/ACE/ace/RAW_Socket.cpp b/ACE/ace/RAW_Socket.cpp index 3b681624cf982..5bf9b4edfd050 100644 --- a/ACE/ace/RAW_Socket.cpp +++ b/ACE/ace/RAW_Socket.cpp @@ -81,14 +81,14 @@ ACE_RAW_SOCKET::ACE_RAW_SOCKET (ACE_INET_Addr const & local, static inline ssize_t using_common_recv(const ACE_RAW_SOCKET& raw, void *buf, size_t n, ACE_INET_Addr &addr, int flags) { - sockaddr *saddr = static_cast(addr.get_addr ()); - int addr_len = addr.get_size (); + struct sockaddr *saddr = static_cast(addr.get_addr ()); + int addr_len = addr.get_size (); ssize_t const status = ACE_OS::recvfrom (raw.get_handle (), - (char *) buf, + static_cast(buf), n, flags, - (sockaddr *) saddr, + saddr, &addr_len); addr.set_size (addr_len); @@ -107,8 +107,8 @@ static inline void fillMsgHdr(msghdr& recv_msg, const ACE_INET_Addr &addr, void* recv_msg.msg_namelen = addr.get_size (); #ifdef ACE_USE_MSG_CONTROL - recv_msg.msg_control = pcbuf; - recv_msg.msg_controllen = cbuf_size; + recv_msg.msg_control = pcbuf; + recv_msg.msg_controllen = cbuf_size; #elif !defined ACE_LACKS_SENDMSG recv_msg.msg_accrights = 0; recv_msg.msg_accrightslen = 0; @@ -234,13 +234,13 @@ ACE_RAW_SOCKET::send (const void *buf, // Check the status of the current socket. ACE_SEND_EXCEPTION_RETURN(); - sockaddr *saddr = static_cast(addr.get_addr ()); + struct sockaddr *saddr = static_cast(addr.get_addr ()); int const len = addr.get_size (); return ACE_OS::sendto (this->get_handle (), - (const char *) buf, + static_cast(buf), n, flags, - (struct sockaddr *) saddr, + saddr, len); } @@ -265,7 +265,7 @@ ACE_RAW_SOCKET::send (const iovec iov[], #if defined (ACE_HAS_SOCKADDR_MSG_NAME) send_msg.msg_name = static_cast(addr.get_addr()); #else - send_msg.msg_name = (char *) addr.get_addr (); + send_msg.msg_name = static_cast(addr.get_addr ()); #endif /* ACE_HAS_SOCKADDR_MSG_NAME */ send_msg.msg_namelen = addr.get_size (); diff --git a/ACE/tests/RAW_Socket_Test.cpp b/ACE/tests/RAW_Socket_Test.cpp index 186e6d7a147c0..e608ac0035f57 100644 --- a/ACE/tests/RAW_Socket_Test.cpp +++ b/ACE/tests/RAW_Socket_Test.cpp @@ -115,7 +115,7 @@ void ipv4_header_checksum(IPv4_HEADER_t_Ptr pIpHeader) pIpHeader->u16CheckSum = 0; - uint32_t sum = Checksum(0, (uint8_t *)pIpHeader, ipHeadLen); + uint32_t sum = Checksum(0, reinterpret_cast(pIpHeader), ipHeadLen); pIpHeader->u16CheckSum = htons((uint16_t)(~sum)); @@ -134,15 +134,15 @@ void udp6_header_checksum(IPv6_HEADER_t_Ptr pIpHeader, UDP_HEADER_t_Ptr ptUDPHea //sum += (ptUDPHeader->u16Length >> 8 & 0x00FF); //sum += (ptUDPHeader->u16Length << 8 & 0xFF00); - sum = Checksum(sum, (uint8_t *)&pIpHeader->abSrcAddr, 16); - sum = Checksum(sum, (uint8_t *)&pIpHeader->abDstAddr, 16); + sum = Checksum(sum, &pIpHeader->abSrcAddr[0], 16); + sum = Checksum(sum, &pIpHeader->abDstAddr[0], 16); sum += ((uint16_t)pIpHeader->bNextHeader & 0x00FF); //finish the pseudo header checksum //udp section ptUDPHeader->u16CheckSum = 0; - sum = Checksum(sum, (uint8_t *)ptUDPHeader, udpLen); + sum = Checksum(sum, reinterpret_cast(ptUDPHeader), udpLen); ptUDPHeader->u16CheckSum = htons((uint16_t)(~sum)); @@ -153,7 +153,7 @@ void udp6_header_checksum(IPv6_HEADER_t_Ptr pIpHeader, UDP_HEADER_t_Ptr ptUDPHea class SockGuard : private ACE_Copy_Disabled { public: -SockGuard(ACE_SOCK& sock):sock_(sock){}; +explicit SockGuard(ACE_SOCK& sock):sock_(sock){}; ~SockGuard(){ sock_.close(); }; private: @@ -207,6 +207,7 @@ run_option_test () rc = rawSocket.set_option(SOL_SOCKET, SO_RCVBUF, &new_optval, sizeof(new_optval)); ACE_TEST_EXCEPTION_RETURN(rc < 0, " set SO_RCVBUF new value in failure\n"); + ACE_UNUSED_ARG(new_optval); new_optval = 0; optlen = sizeof(new_optval); @@ -317,7 +318,7 @@ static int raw_recv_data_until_meet_condition(ACE_RAW_SOCKET& raw, u_short port, UDP_HEADER_t_Ptr ptUDPHeader; if(local.get_type() == AF_INET) { - ptUDPHeader = (UDP_HEADER_t_Ptr)(recvbuf + sizeof(IPv4_HEADER_t)); + ptUDPHeader = reinterpret_cast(recvbuf + sizeof(IPv4_HEADER_t)); expectedLen = (n + sizeof(IPv4_HEADER_t) + sizeof(UDP_HEADER_t)); u_short nDstPort = ntohs(ptUDPHeader->u16DstPort); @@ -329,7 +330,7 @@ static int raw_recv_data_until_meet_condition(ACE_RAW_SOCKET& raw, u_short port, } else { - ptUDPHeader = (UDP_HEADER_t_Ptr)recvbuf; + ptUDPHeader = reinterpret_cast(recvbuf); expectedLen = (n + sizeof(UDP_HEADER_t)); u_short nDstPort = ntohs(ptUDPHeader->u16DstPort); @@ -352,7 +353,7 @@ run_raw_udp_test_child_flow_sendby_self (ACE_RAW_SOCKET& raw, ACE_INET_Addr& cli { ACE_DEBUG ((LM_INFO, "%C begin to run when sending data by self ...\n", __func__)); - UDP_HEADER_t_Ptr ptUDPHeader = (UDP_HEADER_t_Ptr)(sendbuf + sizeof(IPv4_HEADER_t_Ptr)); + UDP_HEADER_t_Ptr ptUDPHeader = reinterpret_cast(sendbuf + sizeof(IPv4_HEADER_t_Ptr)); ptUDPHeader->u16SrcPort = htons(client_addr.get_port_number()); ptUDPHeader->u16DstPort = htons(server_addr.get_port_number()); @@ -448,8 +449,8 @@ run_raw_generic_test () client_dgram.get_local_addr (client_addr); server_dgram.get_local_addr (server_addr); - IPv4_HEADER_t_Ptr ptIPv4Header = (IPv4_HEADER_t_Ptr)sendbuf; - UDP_HEADER_t_Ptr ptUDPHeader = (UDP_HEADER_t_Ptr)(sendbuf + sizeof(IPv4_HEADER_t)); + IPv4_HEADER_t_Ptr ptIPv4Header = reinterpret_cast(sendbuf); + UDP_HEADER_t_Ptr ptUDPHeader = reinterpret_cast(sendbuf + sizeof(IPv4_HEADER_t)); u_short n = 2048; *ptIPv4Header = {}; @@ -619,8 +620,8 @@ run_iovec_IPv6_api_test () ACE_DEBUG ((LM_INFO, "%C test iovec send ...\n", __func__)); - IPv6_HEADER_t_Ptr ptIPv6Header = (IPv6_HEADER_t_Ptr)sendbuf; - UDP_HEADER_t_Ptr ptUDPHeader = (UDP_HEADER_t_Ptr)(sendbuf + sizeof(IPv6_HEADER_t)); + IPv6_HEADER_t_Ptr ptIPv6Header = reinterpret_cast(sendbuf); + UDP_HEADER_t_Ptr ptUDPHeader = reinterpret_cast(sendbuf + sizeof(IPv6_HEADER_t)); u_short n = sizeof("hello world"); *ptIPv6Header = {}; @@ -767,7 +768,7 @@ run_main (int, ACE_TCHAR *argv[]) ACE_START_TEST (ACE_TEXT ("RAW_Socket_Test")); ACE_UNUSED_ARG (argv); int retval = 0; - int oldMTU = 1500; + int oldMTU; #if !defined (ACE_WIN32) From e22df784ad603e8602af5d53b6da0eec4fa13a4c Mon Sep 17 00:00:00 2001 From: SmithAchang Date: Mon, 3 Apr 2023 20:53:40 +0800 Subject: [PATCH 22/39] fix Codacy Static Code Analysis on windows platform --- ACE/tests/RAW_Socket_Test.cpp | 59 +++++++++++++++++------------------ 1 file changed, 29 insertions(+), 30 deletions(-) diff --git a/ACE/tests/RAW_Socket_Test.cpp b/ACE/tests/RAW_Socket_Test.cpp index e608ac0035f57..f81fb1118654f 100644 --- a/ACE/tests/RAW_Socket_Test.cpp +++ b/ACE/tests/RAW_Socket_Test.cpp @@ -90,14 +90,14 @@ uint32_t Checksum(uint32_t cksum, uint8_t *pBuffer, uint32_t size) while (size > 1) { - cksum += ((uint16_t)p[num] << 8 & 0xff00) | ((uint16_t)p[num + 1] & 0x00FF); + cksum += (static_cast(p[num]) << 8 & 0xff00) | (static_cast(p[num + 1]) & 0x00FF); size -= 2; num += 2; } if (size > 0) { - cksum += ((uint16_t)p[num] << 8) & 0xFFFF; + cksum += (static_cast(p[num]) << 8) & 0xFFFF; num += 1; } @@ -117,7 +117,7 @@ void ipv4_header_checksum(IPv4_HEADER_t_Ptr pIpHeader) uint32_t sum = Checksum(0, reinterpret_cast(pIpHeader), ipHeadLen); - pIpHeader->u16CheckSum = htons((uint16_t)(~sum)); + pIpHeader->u16CheckSum = htons(static_cast(~sum)); } @@ -137,14 +137,14 @@ void udp6_header_checksum(IPv6_HEADER_t_Ptr pIpHeader, UDP_HEADER_t_Ptr ptUDPHea sum = Checksum(sum, &pIpHeader->abSrcAddr[0], 16); sum = Checksum(sum, &pIpHeader->abDstAddr[0], 16); - sum += ((uint16_t)pIpHeader->bNextHeader & 0x00FF); + sum += (static_cast(pIpHeader->bNextHeader) & 0x00FF); //finish the pseudo header checksum //udp section ptUDPHeader->u16CheckSum = 0; sum = Checksum(sum, reinterpret_cast(ptUDPHeader), udpLen); - ptUDPHeader->u16CheckSum = htons((uint16_t)(~sum)); + ptUDPHeader->u16CheckSum = htons(static_cast(~sum)); } @@ -186,7 +186,7 @@ run_option_test () ACE_DEBUG ((LM_INFO, "%C begin to run ...\n", __func__)); - ACE_INET_Addr addr(u_short(0), "127.0.0.1"); + ACE_INET_Addr addr(static_cast(0), "127.0.0.1"); ACE_RAW_SOCKET rawSocket(addr); SockGuard guard(rawSocket); @@ -228,7 +228,7 @@ run_reopen_test () ACE_DEBUG ((LM_INFO, "%C begin to run ...\n", __func__)); - ACE_INET_Addr addr((u_short)0, "127.0.0.1"); + ACE_INET_Addr addr(static_cast(0), "127.0.0.1"); ACE_RAW_SOCKET rawSocket(addr); SockGuard guard(rawSocket); @@ -239,7 +239,7 @@ run_reopen_test () ACE_TEST_EXCEPTION_RETURN(rawSocket.get_handle() != ACE_INVALID_HANDLE, " close in failure\n"); - ACE_INET_Addr addr2((u_short)0, "127.0.0.8"); + ACE_INET_Addr addr2(static_cast(0), "127.0.0.8"); int rc = rawSocket.open(addr2); ACE_TEST_EXCEPTION_RETURN(rc < 0, " reopen in failue\n"); @@ -289,11 +289,11 @@ static int raw_recv_data_until_meet_condition(ACE_RAW_SOCKET& raw, u_short port, if(to_addr == nullptr) { - raw.recv(vec, (int)(sizeof(vec)/sizeof(vec[0])) , remote); + raw.recv(vec, static_cast(sizeof(vec)/sizeof(vec[0])) , remote); } else { - len = raw.recv(vec, (int)(sizeof(vec)/sizeof(vec[0])), remote, 0/*flags*/, nullptr, to_addr); + len = raw.recv(vec, static_cast(sizeof(vec)/sizeof(vec[0])), remote, 0/*flags*/, nullptr, to_addr); } } else @@ -377,7 +377,7 @@ run_raw_udp_test () { ACE_DEBUG ((LM_INFO, "%C begin to run using the port auto assigned by OS to avoid port conflict ...\n", __func__)); - ACE_INET_Addr addr((u_short)0, "127.0.0.1"); + ACE_INET_Addr addr(static_cast(0), "127.0.0.1"); ACE_SOCK_Dgram dgram(addr); SockGuard dgram_guard(dgram); @@ -429,8 +429,8 @@ run_raw_generic_test () { ACE_DEBUG ((LM_INFO, "%C begin to run generic raw socket i.e. send only RAW socket ...\n", __func__)); - ACE_INET_Addr bindAddr((u_short)0, "127.0.0.1"), remote; - ACE_INET_Addr client_addr((u_short)0, "127.0.0.7") ,server_addr((u_short)0, "127.0.0.8"); + ACE_INET_Addr bindAddr(static_cast(0), "127.0.0.1"), remote; + ACE_INET_Addr client_addr(static_cast(0), "127.0.0.7") ,server_addr(static_cast(0), "127.0.0.8"); ACE_SOCK_Dgram client_dgram(client_addr); SockGuard client_dgram_guard(client_dgram); @@ -508,10 +508,10 @@ run_ipv6_pkginfo_test () { ACE_DEBUG ((LM_INFO, "%C begin to run IPv6 pkginfo test ...\n", __func__)); - ACE_INET_Addr bindAddr((u_short)0, "::1"); - ACE_INET_Addr anyAddr((u_short)0, "::"); + ACE_INET_Addr bindAddr(static_cast(0), "::1"); + ACE_INET_Addr anyAddr(static_cast(0), "::"); - ACE_INET_Addr client_addr((u_short)0, "::1") ,server_addr((u_short)0, "::1"); + ACE_INET_Addr client_addr(static_cast(0), "::1") ,server_addr(static_cast(0), "::1"); ACE_SOCK_Dgram client_dgram(client_addr); @@ -572,10 +572,10 @@ run_iovec_IPv6_api_test () { ACE_DEBUG ((LM_INFO, "%C begin to run IPv6 iovec api test ...\n", __func__)); - ACE_INET_Addr bindAddr((u_short)0, "::1"); - ACE_INET_Addr anyAddr((u_short)0, "::"); + ACE_INET_Addr bindAddr(static_cast(0), "::1"); + ACE_INET_Addr anyAddr(static_cast(0), "::"); - ACE_INET_Addr client_addr((u_short)0, "::1") ,server_addr((u_short)0, "::1"); + ACE_INET_Addr client_addr(static_cast(0), "::1") ,server_addr(static_cast(0), "::1"); ACE_SOCK_Dgram client_dgram(client_addr); SockGuard client_dgram_guard(client_dgram); @@ -648,7 +648,7 @@ run_iovec_IPv6_api_test () ACE_DEBUG ((LM_INFO, "%C test iovec using common udp6 socket ...\n", __func__)); - rc = client_dgram.send(iov_udp, (int)(sizeof(iov_udp)/sizeof(iov_udp[0])), server_addr); + rc = client_dgram.send(iov_udp, static_cast(sizeof(iov_udp)/sizeof(iov_udp[0])), server_addr); ACE_TEST_EXCEPTION_RETURN(rc == -1, " udp6 socket can not send using iov \n"); readUdpSocektToEmpty(server_dgram); @@ -656,7 +656,7 @@ run_iovec_IPv6_api_test () ACE_DEBUG ((LM_INFO, "%C must set port to zero ??? ...\n", __func__)); iov_server_addr.set_port_number(0); - rc = rawSocket.send(iov_udp, (int)(sizeof(iov_udp)/sizeof(iov_udp[0])), iov_server_addr); + rc = rawSocket.send(iov_udp, static_cast(sizeof(iov_udp)/sizeof(iov_udp[0])), iov_server_addr); ACE_TEST_EXCEPTION_RETURN(rc == -1, " raw6 socket can not send using iov \n"); ACE_OS::sleep(1); @@ -672,9 +672,9 @@ run_iovec_IPv4_api_test () { ACE_DEBUG ((LM_INFO, "%C begin to run IPv4 iovec api test ...\n", __func__)); - ACE_INET_Addr bindAddr((u_short)0, "127.0.0.1"); + ACE_INET_Addr bindAddr(static_cast(0), "127.0.0.1"); - ACE_INET_Addr client_addr((u_short)0, "127.0.0.1") ,server_addr((u_short)0, "127.0.0.1"); + ACE_INET_Addr client_addr(static_cast(0), "127.0.0.1") ,server_addr(static_cast(0), "127.0.0.1"); ACE_SOCK_Dgram client_dgram(client_addr); SockGuard client_dgram_guard(client_dgram); @@ -720,8 +720,8 @@ run_iovec_IPv4_api_test () ACE_DEBUG ((LM_INFO, "%C test iovec send ...\n", __func__)); readUdpSocektToEmpty(server_dgram); - IPv4_HEADER_t_Ptr ptIPv4Header = (IPv4_HEADER_t_Ptr)sendbuf; - UDP_HEADER_t_Ptr ptUDPHeader = (UDP_HEADER_t_Ptr)(sendbuf + sizeof(IPv4_HEADER_t)); + IPv4_HEADER_t_Ptr ptIPv4Header = reinterpret_cast(sendbuf); + UDP_HEADER_t_Ptr ptUDPHeader = reinterpret_cast(sendbuf + sizeof(IPv4_HEADER_t)); u_short n = sizeof("hello world"); *ptIPv4Header = {}; @@ -751,7 +751,7 @@ run_iovec_IPv4_api_test () iov_udp[1].iov_base = const_cast("hello world"); iov_udp[1].iov_len = sizeof("hello world"); - rc = rawSocket.send(iov_udp, (int)(sizeof(iov_udp)/sizeof(iov_udp[0])), server_addr); + rc = rawSocket.send(iov_udp, static_cast(sizeof(iov_udp)/sizeof(iov_udp[0])), server_addr); ACE_TEST_EXCEPTION_RETURN(rc == -1, " raw4 socket can send using iov\n"); ACE_OS::sleep(1); @@ -768,14 +768,13 @@ run_main (int, ACE_TCHAR *argv[]) ACE_START_TEST (ACE_TEXT ("RAW_Socket_Test")); ACE_UNUSED_ARG (argv); int retval = 0; - int oldMTU; - #if !defined (ACE_WIN32) // set the lo interface MTU + int oldMTU; if(ACE_OS::getuid() == 0) { - ACE_INET_Addr anyAddr((u_short)0); + ACE_INET_Addr anyAddr(static_cast(0)); ACE_SOCK_Dgram netdevice(anyAddr); SockGuard dgram_guard(netdevice); @@ -814,7 +813,7 @@ run_main (int, ACE_TCHAR *argv[]) #if !defined (ACE_WIN32) if(ACE_OS::getuid() == 0) { - ACE_INET_Addr anyAddr((u_short)0); + ACE_INET_Addr anyAddr(static_cast(0)); ACE_SOCK_Dgram netdevice(anyAddr); SockGuard dgram_guard(netdevice); From 170827cf6cabc6676b62ef4ef93db553ce1a74ab Mon Sep 17 00:00:00 2001 From: smitAchang Date: Tue, 4 Apr 2023 14:06:18 +0800 Subject: [PATCH 23/39] fix codacy static code analysis --- ACE/tests/RAW_Socket_Test.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/ACE/tests/RAW_Socket_Test.cpp b/ACE/tests/RAW_Socket_Test.cpp index f81fb1118654f..44d94be30bc18 100644 --- a/ACE/tests/RAW_Socket_Test.cpp +++ b/ACE/tests/RAW_Socket_Test.cpp @@ -98,7 +98,6 @@ uint32_t Checksum(uint32_t cksum, uint8_t *pBuffer, uint32_t size) if (size > 0) { cksum += (static_cast(p[num]) << 8) & 0xFFFF; - num += 1; } while (cksum >> 16) @@ -207,7 +206,7 @@ run_option_test () rc = rawSocket.set_option(SOL_SOCKET, SO_RCVBUF, &new_optval, sizeof(new_optval)); ACE_TEST_EXCEPTION_RETURN(rc < 0, " set SO_RCVBUF new value in failure\n"); - ACE_UNUSED_ARG(new_optval); + ACE_DEBUG ((LM_INFO, "%C set_option got optlen: %d ...\n", __func__, optlen)); new_optval = 0; optlen = sizeof(new_optval); From bc99292277f84a52bab3a2532315e9dd5bbe897f Mon Sep 17 00:00:00 2001 From: smitAchang Date: Tue, 4 Apr 2023 15:57:01 +0800 Subject: [PATCH 24/39] format cpp with alignment --- ACE/ace/RAW_Socket.cpp | 258 +++++++++++++++++++++-------------------- 1 file changed, 130 insertions(+), 128 deletions(-) diff --git a/ACE/ace/RAW_Socket.cpp b/ACE/ace/RAW_Socket.cpp index 5bf9b4edfd050..d8745bf8f5f56 100644 --- a/ACE/ace/RAW_Socket.cpp +++ b/ACE/ace/RAW_Socket.cpp @@ -37,14 +37,14 @@ ACE_ALLOC_HOOK_DEFINE (ACE_RAW_SOCKET) }; #endif -#define ACE_SEND_EXCEPTION_RETURN() do{\ +#define ACE_SEND_EXCEPTION_RETURN() do {\ if (this->get_handle () == ACE_INVALID_HANDLE)\ return -1; \ if(timeout && ACE::handle_write_ready (this->get_handle (), timeout) != 1)\ return -1; \ }while(0) -#define ACE_RECV_EXCEPTION_RETURN() do{\ +#define ACE_RECV_EXCEPTION_RETURN() do {\ if (this->get_handle () == ACE_INVALID_HANDLE)\ return -1; \ if(this->is_send_only())\ @@ -57,25 +57,25 @@ ACE_ALLOC_HOOK_DEFINE (ACE_RAW_SOCKET) void ACE_RAW_SOCKET::dump () const { - ACE_TRACE ("ACE_RAW_SOCKET::dump"); + ACE_TRACE ("ACE_RAW_SOCKET::dump"); } /// @brief default constructor ACE_RAW_SOCKET::ACE_RAW_SOCKET () : protocol_(IPPROTO_UDP) { - ACE_TRACE ("ACE_RAW_SOCKET::ACE_RAW_SOCKET"); + ACE_TRACE ("ACE_RAW_SOCKET::ACE_RAW_SOCKET"); } /// @brief constructor with parameters ACE_RAW_SOCKET::ACE_RAW_SOCKET (ACE_INET_Addr const & local, int protocol) : protocol_(protocol) { - ACE_TRACE ("ACE_RAW_SOCKET::ACE_RAW_SOCKET"); + ACE_TRACE ("ACE_RAW_SOCKET::ACE_RAW_SOCKET"); - if (this->open (local, protocol) == -1) - ACELIB_ERROR ((LM_ERROR, - ACE_TEXT ("%p\n"), - ACE_TEXT ("ACE_RAW_SOCKET"))); + if (this->open (local, protocol) == -1) + ACELIB_ERROR ((LM_ERROR, + ACE_TEXT ("%p\n"), + ACE_TEXT ("ACE_RAW_SOCKET"))); } @@ -118,9 +118,9 @@ static inline void fillMsgHdr(msghdr& recv_msg, const ACE_INET_Addr &addr, void* static inline void getToAddrFromMsgHdr(msghdr& recv_msg, ACE_INET_Addr& to_addr) { - #ifdef ACE_USE_MSG_CONTROL + #ifdef ACE_USE_MSG_CONTROL if (to_addr.get_type() == AF_INET) { - #if defined (IP_RECVDSTADDR) || defined (IP_PKTINFO) + #if defined (IP_RECVDSTADDR) || defined (IP_PKTINFO) for (cmsghdr *ptr = ACE_CMSG_FIRSTHDR (&recv_msg); ptr; ptr = ACE_CMSG_NXTHDR (&recv_msg, ptr)) { #if defined (IP_RECVDSTADDR) if (ptr->cmsg_level == IPPROTO_IP && ptr->cmsg_type == IP_RECVDSTADDR) { @@ -154,6 +154,9 @@ static inline void getToAddrFromMsgHdr(msghdr& recv_msg, ACE_INET_Addr& to_addr) } } #endif + #else + ACE_UNUSED_ARG(recv_msg); + ACE_UNUSED_ARG (to_addr); #endif } @@ -166,59 +169,58 @@ ACE_RAW_SOCKET::recv (void *buf, const ACE_Time_Value *timeout, ACE_INET_Addr *to_addr) const { - ACE_TRACE ("ACE_RAW_SOCKET::recv"); - - ACE_RECV_EXCEPTION_RETURN(); - - if(to_addr == nullptr) - { - return using_common_recv(*this, buf, n, addr, flags); - } - else - { - this->get_local_addr (*to_addr); - if(!to_addr->is_any()) + ACE_TRACE ("ACE_RAW_SOCKET::recv"); + + ACE_RECV_EXCEPTION_RETURN(); + + if(to_addr == nullptr) { - return using_common_recv(*this, buf, n, addr, flags); + return using_common_recv(*this, buf, n, addr, flags); + } + else + { + this->get_local_addr (*to_addr); + if(!to_addr->is_any()) + { + return using_common_recv(*this, buf, n, addr, flags); + } } - } - - struct iovec iov; - iov.iov_base = static_cast(buf); - iov.iov_len = static_cast(n); + struct iovec iov; + iov.iov_base = static_cast(buf); + iov.iov_len = static_cast(n); - msghdr recv_msg = {}; - recv_msg.msg_iov = &iov; - recv_msg.msg_iovlen = 1; + msghdr recv_msg = {}; + recv_msg.msg_iov = &iov; + recv_msg.msg_iovlen = 1; - #ifdef ACE_USE_MSG_CONTROL - union control_buffer cbuf; - if(to_addr != nullptr) - { - fillMsgHdr(recv_msg, addr, &cbuf, sizeof(cbuf)); - } - else - { - fillMsgHdr(recv_msg, addr, nullptr, 0); - } - #else + #ifdef ACE_USE_MSG_CONTROL + union control_buffer cbuf; + if(to_addr != nullptr) + { + fillMsgHdr(recv_msg, addr, &cbuf, sizeof(cbuf)); + } + else + { + fillMsgHdr(recv_msg, addr, nullptr, 0); + } + #else fillMsgHdr(recv_msg, addr, nullptr, 0); - #endif + #endif - ssize_t const status = ACE_OS::recvmsg (this->get_handle (), - &recv_msg, - flags); + ssize_t const status = ACE_OS::recvmsg (this->get_handle (), + &recv_msg, + flags); - addr.set_size (recv_msg.msg_namelen); - addr.set_type ((static_cast(addr.get_addr()))->sin_family); + addr.set_size (recv_msg.msg_namelen); + addr.set_type ((static_cast(addr.get_addr()))->sin_family); - #ifdef ACE_USE_MSG_CONTROL - if(to_addr != nullptr) - getToAddrFromMsgHdr(recv_msg, *to_addr); - #endif + #ifdef ACE_USE_MSG_CONTROL + if(to_addr != nullptr) + getToAddrFromMsgHdr(recv_msg, *to_addr); + #endif - return status; + return status; } @@ -229,14 +231,14 @@ ACE_RAW_SOCKET::send (const void *buf, int flags, const ACE_Time_Value *timeout) const { - ACE_TRACE ("ACE_RAW_SOCKET::send"); + ACE_TRACE ("ACE_RAW_SOCKET::send"); - // Check the status of the current socket. - ACE_SEND_EXCEPTION_RETURN(); + // Check the status of the current socket. + ACE_SEND_EXCEPTION_RETURN(); - struct sockaddr *saddr = static_cast(addr.get_addr ()); - int const len = addr.get_size (); - return ACE_OS::sendto (this->get_handle (), + struct sockaddr *saddr = static_cast(addr.get_addr ()); + int const len = addr.get_size (); + return ACE_OS::sendto (this->get_handle (), static_cast(buf), n, flags, @@ -263,24 +265,24 @@ ACE_RAW_SOCKET::send (const iovec iov[], send_msg.msg_iovlen = n; #if defined (ACE_HAS_SOCKADDR_MSG_NAME) - send_msg.msg_name = static_cast(addr.get_addr()); + send_msg.msg_name = static_cast(addr.get_addr()); #else - send_msg.msg_name = static_cast(addr.get_addr ()); + send_msg.msg_name = static_cast(addr.get_addr ()); #endif /* ACE_HAS_SOCKADDR_MSG_NAME */ - send_msg.msg_namelen = addr.get_size (); + send_msg.msg_namelen = addr.get_size (); #if defined (ACE_HAS_4_4BSD_SENDMSG_RECVMSG) - send_msg.msg_control = 0; - send_msg.msg_controllen = 0; - send_msg.msg_flags = 0; + send_msg.msg_control = 0; + send_msg.msg_controllen = 0; + send_msg.msg_flags = 0; #elif !defined ACE_LACKS_SENDMSG - send_msg.msg_accrights = 0; - send_msg.msg_accrightslen = 0; + send_msg.msg_accrights = 0; + send_msg.msg_accrightslen = 0; #endif /* ACE_HAS_4_4BSD_SENDMSG_RECVMSG */ #ifdef ACE_WIN32 - send_msg.msg_control = 0; - send_msg.msg_controllen = 0; + send_msg.msg_control = 0; + send_msg.msg_controllen = 0; #endif return ACE_OS::sendmsg (this->get_handle (), &send_msg, flags); @@ -295,41 +297,41 @@ ACE_RAW_SOCKET::recv (iovec iov[], const ACE_Time_Value *timeout, ACE_INET_Addr *to_addr) const { - ACE_TRACE ("ACE_RAW_SOCKET::recv"); + ACE_TRACE ("ACE_RAW_SOCKET::recv"); - ACE_RECV_EXCEPTION_RETURN(); + ACE_RECV_EXCEPTION_RETURN(); - msghdr recv_msg = {}; - recv_msg.msg_iov = static_cast(iov); - recv_msg.msg_iovlen = n; + msghdr recv_msg = {}; + recv_msg.msg_iov = static_cast(iov); + recv_msg.msg_iovlen = n; - /*default*/ - fillMsgHdr(recv_msg, addr, nullptr, 0); + /*default*/ + fillMsgHdr(recv_msg, addr, nullptr, 0); - #ifdef ACE_USE_MSG_CONTROL - union control_buffer cbuf; - if(to_addr != nullptr) - { - this->get_local_addr(*to_addr); + #ifdef ACE_USE_MSG_CONTROL + union control_buffer cbuf; + if(to_addr != nullptr) + { + this->get_local_addr(*to_addr); - fillMsgHdr(recv_msg, addr, &cbuf, sizeof(cbuf)); - } - #endif + fillMsgHdr(recv_msg, addr, &cbuf, sizeof(cbuf)); + } + #endif - ssize_t const status = ACE_OS::recvmsg (this->get_handle (), - &recv_msg, - flags); + ssize_t const status = ACE_OS::recvmsg (this->get_handle (), + &recv_msg, + flags); - addr.set_size (recv_msg.msg_namelen); - addr.set_type ((static_cast(addr.get_addr()))->sin_family); + addr.set_size (recv_msg.msg_namelen); + addr.set_type ((static_cast(addr.get_addr()))->sin_family); - #ifdef ACE_USE_MSG_CONTROL - if(to_addr != nullptr) - getToAddrFromMsgHdr(recv_msg, *to_addr); - #endif + #ifdef ACE_USE_MSG_CONTROL + if(to_addr != nullptr) + getToAddrFromMsgHdr(recv_msg, *to_addr); + #endif - return status; + return status; } #else @@ -341,11 +343,11 @@ ACE_RAW_SOCKET::send (const iovec iov[], int flags, const ACE_Time_Value *timeout) const { - ACE_TRACE ("ACE_RAW_SOCKET::send iovec"); + ACE_TRACE ("ACE_RAW_SOCKET::send iovec"); - // immediately fail when unsupported - return -1; + // immediately fail when unsupported + return -1; } ssize_t @@ -356,11 +358,11 @@ ACE_RAW_SOCKET::recv (iovec iov[], const ACE_Time_Value *timeout, ACE_INET_Addr *to_addr) const { - ACE_TRACE ("ACE_RAW_SOCKET::recv iovec"); + ACE_TRACE ("ACE_RAW_SOCKET::recv iovec"); - // immediately fail when unsupported - return -1; + // immediately fail when unsupported + return -1; } #endif @@ -368,45 +370,45 @@ ACE_RAW_SOCKET::recv (iovec iov[], int ACE_RAW_SOCKET::open (ACE_INET_Addr const & local, int protocol) { - ACE_TRACE ("ACE_RAW_SOCKET::open"); + ACE_TRACE ("ACE_RAW_SOCKET::open"); - if (this->get_handle () != ACE_INVALID_HANDLE) + if (this->get_handle () != ACE_INVALID_HANDLE) return -1; - int const protocol_family = local.get_type (); - /// reuse_addr Maybe meaningless for RAW Socket - int const reuse_addr = 1; + int const protocol_family = local.get_type (); + /// reuse_addr Maybe meaningless for RAW Socket + int const reuse_addr = 1; - if(ACE_SOCK::open (SOCK_RAW, protocol_family, protocol, reuse_addr) == -1) - return -1; + if(ACE_SOCK::open (SOCK_RAW, protocol_family, protocol, reuse_addr) == -1) + return -1; - if(ACE_OS::bind (this->get_handle (), static_cast(local.get_addr()), local.get_addr_size()) == -1) - return -1; + if(ACE_OS::bind (this->get_handle (), static_cast(local.get_addr()), local.get_addr_size()) == -1) + return -1; - this->protocol_ = protocol; + this->protocol_ = protocol; - ACE_INET_Addr bindAddr; - this->get_local_addr(bindAddr); + ACE_INET_Addr bindAddr; + this->get_local_addr(bindAddr); - #if defined (ACE_HAS_IPV6) && defined (IPV6_PKTINFO) && defined(IPV6_RECVPKTINFO) - if (bindAddr.get_type() == PF_INET6 && bindAddr.is_any()) - { - int yes = 1; - this->set_option(IPPROTO_IPV6, ACE_RECVPKTINFO6, &yes, sizeof(yes)); - } - #endif + #if defined (ACE_HAS_IPV6) && defined (IPV6_PKTINFO) && defined(IPV6_RECVPKTINFO) + if (bindAddr.get_type() == PF_INET6 && bindAddr.is_any()) + { + int yes = 1; + this->set_option(IPPROTO_IPV6, ACE_RECVPKTINFO6, &yes, sizeof(yes)); + } + #endif - #if defined (IP_RECVDSTADDR) || defined (IP_PKTINFO) - if (bindAddr.get_type() == PF_INET && bindAddr.is_any()) - { - int yes = 1; - this->set_option(IPPROTO_IP, ACE_RECVPKTINFO, &yes, sizeof(yes)); - } - #endif + #if defined (IP_RECVDSTADDR) || defined (IP_PKTINFO) + if (bindAddr.get_type() == PF_INET && bindAddr.is_any()) + { + int yes = 1; + this->set_option(IPPROTO_IP, ACE_RECVPKTINFO, &yes, sizeof(yes)); + } + #endif - return 0; + return 0; } From a4276fa371b537e22659c126b38d28b5529c8b8c Mon Sep 17 00:00:00 2001 From: smitAchang Date: Tue, 4 Apr 2023 16:29:22 +0800 Subject: [PATCH 25/39] format #ifdef --- ACE/ace/RAW_Socket.cpp | 50 ++++++++++++++++++++---------------------- 1 file changed, 24 insertions(+), 26 deletions(-) diff --git a/ACE/ace/RAW_Socket.cpp b/ACE/ace/RAW_Socket.cpp index d8745bf8f5f56..4c5af351a8543 100644 --- a/ACE/ace/RAW_Socket.cpp +++ b/ACE/ace/RAW_Socket.cpp @@ -118,25 +118,25 @@ static inline void fillMsgHdr(msghdr& recv_msg, const ACE_INET_Addr &addr, void* static inline void getToAddrFromMsgHdr(msghdr& recv_msg, ACE_INET_Addr& to_addr) { - #ifdef ACE_USE_MSG_CONTROL + #if defined(ACE_HAS_4_4BSD_SENDMSG_RECVMSG) || defined ACE_WIN32 if (to_addr.get_type() == AF_INET) { #if defined (IP_RECVDSTADDR) || defined (IP_PKTINFO) for (cmsghdr *ptr = ACE_CMSG_FIRSTHDR (&recv_msg); ptr; ptr = ACE_CMSG_NXTHDR (&recv_msg, ptr)) { - #if defined (IP_RECVDSTADDR) + #if defined (IP_RECVDSTADDR) if (ptr->cmsg_level == IPPROTO_IP && ptr->cmsg_type == IP_RECVDSTADDR) { - to_addr.set_address (reinterpret_cast(ACE_CMSG_DATA (ptr)), + to_addr.set_address (reinterpret_cast(ACE_CMSG_DATA (ptr)), sizeof (struct in_addr), 0); - break; + break; } - #else + #else if (ptr->cmsg_level == IPPROTO_IP && ptr->cmsg_type == IP_PKTINFO) { - to_addr.set_address ((const char *) &(((in_pktinfo *) (ACE_CMSG_DATA (ptr)))->ipi_addr), + to_addr.set_address ((const char *) &(((in_pktinfo *) (ACE_CMSG_DATA (ptr)))->ipi_addr), sizeof (struct in_addr), 0); - break; + break; } - #endif + #endif } #endif } @@ -145,11 +145,10 @@ static inline void getToAddrFromMsgHdr(msghdr& recv_msg, ACE_INET_Addr& to_addr) else if (to_addr.get_type() == AF_INET6) { for (cmsghdr *ptr = ACE_CMSG_FIRSTHDR (&recv_msg); ptr; ptr = ACE_CMSG_NXTHDR (&recv_msg, ptr)) { if (ptr->cmsg_level == IPPROTO_IPV6 && ptr->cmsg_type == IPV6_PKTINFO) { - to_addr.set_address (reinterpret_cast(&((reinterpret_cast((ACE_CMSG_DATA(ptr))))->ipi6_addr)), + to_addr.set_address (reinterpret_cast(&((reinterpret_cast((ACE_CMSG_DATA(ptr))))->ipi6_addr)), sizeof (struct in6_addr), 0); - - break; + break; } } } @@ -157,8 +156,7 @@ static inline void getToAddrFromMsgHdr(msghdr& recv_msg, ACE_INET_Addr& to_addr) #else ACE_UNUSED_ARG(recv_msg); ACE_UNUSED_ARG (to_addr); - #endif - + #endif } ssize_t @@ -194,7 +192,7 @@ ACE_RAW_SOCKET::recv (void *buf, recv_msg.msg_iov = &iov; recv_msg.msg_iovlen = 1; - #ifdef ACE_USE_MSG_CONTROL +#ifdef ACE_USE_MSG_CONTROL union control_buffer cbuf; if(to_addr != nullptr) { @@ -204,9 +202,9 @@ ACE_RAW_SOCKET::recv (void *buf, { fillMsgHdr(recv_msg, addr, nullptr, 0); } - #else +#else fillMsgHdr(recv_msg, addr, nullptr, 0); - #endif +#endif ssize_t const status = ACE_OS::recvmsg (this->get_handle (), &recv_msg, @@ -215,10 +213,10 @@ ACE_RAW_SOCKET::recv (void *buf, addr.set_size (recv_msg.msg_namelen); addr.set_type ((static_cast(addr.get_addr()))->sin_family); - #ifdef ACE_USE_MSG_CONTROL +#ifdef ACE_USE_MSG_CONTROL if(to_addr != nullptr) getToAddrFromMsgHdr(recv_msg, *to_addr); - #endif +#endif return status; } @@ -309,7 +307,7 @@ ACE_RAW_SOCKET::recv (iovec iov[], /*default*/ fillMsgHdr(recv_msg, addr, nullptr, 0); - #ifdef ACE_USE_MSG_CONTROL +#ifdef ACE_USE_MSG_CONTROL union control_buffer cbuf; if(to_addr != nullptr) { @@ -317,7 +315,7 @@ ACE_RAW_SOCKET::recv (iovec iov[], fillMsgHdr(recv_msg, addr, &cbuf, sizeof(cbuf)); } - #endif +#endif ssize_t const status = ACE_OS::recvmsg (this->get_handle (), &recv_msg, @@ -326,10 +324,10 @@ ACE_RAW_SOCKET::recv (iovec iov[], addr.set_size (recv_msg.msg_namelen); addr.set_type ((static_cast(addr.get_addr()))->sin_family); - #ifdef ACE_USE_MSG_CONTROL +#ifdef ACE_USE_MSG_CONTROL if(to_addr != nullptr) getToAddrFromMsgHdr(recv_msg, *to_addr); - #endif +#endif return status; } @@ -391,21 +389,21 @@ ACE_RAW_SOCKET::open (ACE_INET_Addr const & local, int protocol) ACE_INET_Addr bindAddr; this->get_local_addr(bindAddr); - #if defined (ACE_HAS_IPV6) && defined (IPV6_PKTINFO) && defined(IPV6_RECVPKTINFO) +#if defined (ACE_HAS_IPV6) && defined (IPV6_PKTINFO) && defined(IPV6_RECVPKTINFO) if (bindAddr.get_type() == PF_INET6 && bindAddr.is_any()) { int yes = 1; this->set_option(IPPROTO_IPV6, ACE_RECVPKTINFO6, &yes, sizeof(yes)); } - #endif +#endif - #if defined (IP_RECVDSTADDR) || defined (IP_PKTINFO) +#if defined (IP_RECVDSTADDR) || defined (IP_PKTINFO) if (bindAddr.get_type() == PF_INET && bindAddr.is_any()) { int yes = 1; this->set_option(IPPROTO_IP, ACE_RECVPKTINFO, &yes, sizeof(yes)); } - #endif +#endif return 0; From 96112ea1e40ccd88f1fb839330d0195fa1fdd6a5 Mon Sep 17 00:00:00 2001 From: SmithAchang Date: Tue, 4 Apr 2023 23:12:35 +0800 Subject: [PATCH 26/39] try for codacy IP Msg Macro --- ACE/ace/RAW_Socket.cpp | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/ACE/ace/RAW_Socket.cpp b/ACE/ace/RAW_Socket.cpp index 4c5af351a8543..a7d064895549e 100644 --- a/ACE/ace/RAW_Socket.cpp +++ b/ACE/ace/RAW_Socket.cpp @@ -116,10 +116,18 @@ static inline void fillMsgHdr(msghdr& recv_msg, const ACE_INET_Addr &addr, void* } +#ifndef IP_RECVDSTADDR +#define IP_RECVDSTADDR 25 +#endif + +#ifndef IP_PKTINFO +# define IP_PKTINFO 19 +#endif + static inline void getToAddrFromMsgHdr(msghdr& recv_msg, ACE_INET_Addr& to_addr) { - #if defined(ACE_HAS_4_4BSD_SENDMSG_RECVMSG) || defined ACE_WIN32 - if (to_addr.get_type() == AF_INET) { + #if defined(ACE_USE_MSG_CONTROL) + if (to_addr.get_type() == AF_INET) { #if defined (IP_RECVDSTADDR) || defined (IP_PKTINFO) for (cmsghdr *ptr = ACE_CMSG_FIRSTHDR (&recv_msg); ptr; ptr = ACE_CMSG_NXTHDR (&recv_msg, ptr)) { #if defined (IP_RECVDSTADDR) From 8a91104555835e4406aee99de6fe703dee4bcf38 Mon Sep 17 00:00:00 2001 From: SmithAchang Date: Wed, 5 Apr 2023 00:29:13 +0800 Subject: [PATCH 27/39] try issues of codacy --- ACE/ace/RAW_Socket.cpp | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/ACE/ace/RAW_Socket.cpp b/ACE/ace/RAW_Socket.cpp index a7d064895549e..484d3d453dec0 100644 --- a/ACE/ace/RAW_Socket.cpp +++ b/ACE/ace/RAW_Socket.cpp @@ -9,6 +9,8 @@ // Included so users have access to ACE_RECVPKTINFO and ACE_RECVPKTINFO6 . #include "ace/OS_NS_sys_socket.h" +#include "ace/SOCK_Dgram.h" + #if defined (ACE_HAS_IPV6) && defined (ACE_WIN32) #include /**/ #endif @@ -116,14 +118,6 @@ static inline void fillMsgHdr(msghdr& recv_msg, const ACE_INET_Addr &addr, void* } -#ifndef IP_RECVDSTADDR -#define IP_RECVDSTADDR 25 -#endif - -#ifndef IP_PKTINFO -# define IP_PKTINFO 19 -#endif - static inline void getToAddrFromMsgHdr(msghdr& recv_msg, ACE_INET_Addr& to_addr) { #if defined(ACE_USE_MSG_CONTROL) From 79a227573261556b5f4538bc64c4df89208d034a Mon Sep 17 00:00:00 2001 From: SmithAchang Date: Wed, 5 Apr 2023 10:39:35 +0800 Subject: [PATCH 28/39] try to fix codacy issue --- ACE/ace/RAW_Socket.cpp | 37 ++++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/ACE/ace/RAW_Socket.cpp b/ACE/ace/RAW_Socket.cpp index 484d3d453dec0..d6ea59283a83e 100644 --- a/ACE/ace/RAW_Socket.cpp +++ b/ACE/ace/RAW_Socket.cpp @@ -9,8 +9,6 @@ // Included so users have access to ACE_RECVPKTINFO and ACE_RECVPKTINFO6 . #include "ace/OS_NS_sys_socket.h" -#include "ace/SOCK_Dgram.h" - #if defined (ACE_HAS_IPV6) && defined (ACE_WIN32) #include /**/ #endif @@ -121,18 +119,22 @@ static inline void fillMsgHdr(msghdr& recv_msg, const ACE_INET_Addr &addr, void* static inline void getToAddrFromMsgHdr(msghdr& recv_msg, ACE_INET_Addr& to_addr) { #if defined(ACE_USE_MSG_CONTROL) - if (to_addr.get_type() == AF_INET) { - #if defined (IP_RECVDSTADDR) || defined (IP_PKTINFO) - for (cmsghdr *ptr = ACE_CMSG_FIRSTHDR (&recv_msg); ptr; ptr = ACE_CMSG_NXTHDR (&recv_msg, ptr)) { + if (to_addr.get_type() == AF_INET) + { + for (cmsghdr *ptr = ACE_CMSG_FIRSTHDR (&recv_msg); ptr; ptr = ACE_CMSG_NXTHDR (&recv_msg, ptr)) + { #if defined (IP_RECVDSTADDR) - if (ptr->cmsg_level == IPPROTO_IP && ptr->cmsg_type == IP_RECVDSTADDR) { + if (ptr->cmsg_level == IPPROTO_IP && ptr->cmsg_type == IP_RECVDSTADDR) + { to_addr.set_address (reinterpret_cast(ACE_CMSG_DATA (ptr)), sizeof (struct in_addr), 0); break; } - #else - if (ptr->cmsg_level == IPPROTO_IP && ptr->cmsg_type == IP_PKTINFO) { + #endif + #if defined(IP_PKTINFO) + if (ptr->cmsg_level == IPPROTO_IP && ptr->cmsg_type == IP_PKTINFO) + { to_addr.set_address ((const char *) &(((in_pktinfo *) (ACE_CMSG_DATA (ptr)))->ipi_addr), sizeof (struct in_addr), 0); @@ -140,24 +142,25 @@ static inline void getToAddrFromMsgHdr(msghdr& recv_msg, ACE_INET_Addr& to_addr) } #endif } - #endif - } - + } #if defined (ACE_HAS_IPV6) && defined (IPV6_PKTINFO) - else if (to_addr.get_type() == AF_INET6) { - for (cmsghdr *ptr = ACE_CMSG_FIRSTHDR (&recv_msg); ptr; ptr = ACE_CMSG_NXTHDR (&recv_msg, ptr)) { - if (ptr->cmsg_level == IPPROTO_IPV6 && ptr->cmsg_type == IPV6_PKTINFO) { + else if (to_addr.get_type() == AF_INET6) + { + for (cmsghdr *ptr = ACE_CMSG_FIRSTHDR (&recv_msg); ptr; ptr = ACE_CMSG_NXTHDR (&recv_msg, ptr)) + { + if (ptr->cmsg_level == IPPROTO_IPV6 && ptr->cmsg_type == IPV6_PKTINFO) + { to_addr.set_address (reinterpret_cast(&((reinterpret_cast((ACE_CMSG_DATA(ptr))))->ipi6_addr)), sizeof (struct in6_addr), 0); break; } - } } + } #endif #else - ACE_UNUSED_ARG(recv_msg); - ACE_UNUSED_ARG (to_addr); + ACE_UNUSED_ARG(recv_msg); + ACE_UNUSED_ARG (to_addr); #endif } From 4e2ea2bd05fee4d669f93fdcf42c48cbd0b14c24 Mon Sep 17 00:00:00 2001 From: SmithAchang Date: Wed, 5 Apr 2023 11:41:40 +0800 Subject: [PATCH 29/39] remove Redundant blank lines for codefactor issues --- ACE/ace/RAW_Socket.cpp | 2 -- ACE/tests/RAW_Socket_Test.cpp | 9 --------- 2 files changed, 11 deletions(-) diff --git a/ACE/ace/RAW_Socket.cpp b/ACE/ace/RAW_Socket.cpp index d6ea59283a83e..d4f60a851900c 100644 --- a/ACE/ace/RAW_Socket.cpp +++ b/ACE/ace/RAW_Socket.cpp @@ -76,7 +76,6 @@ ACE_RAW_SOCKET::ACE_RAW_SOCKET (ACE_INET_Addr const & local, ACELIB_ERROR ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("ACE_RAW_SOCKET"))); - } static inline ssize_t using_common_recv(const ACE_RAW_SOCKET& raw, void *buf, size_t n, ACE_INET_Addr &addr, int flags) @@ -113,7 +112,6 @@ static inline void fillMsgHdr(msghdr& recv_msg, const ACE_INET_Addr &addr, void* recv_msg.msg_accrights = 0; recv_msg.msg_accrightslen = 0; #endif - } static inline void getToAddrFromMsgHdr(msghdr& recv_msg, ACE_INET_Addr& to_addr) diff --git a/ACE/tests/RAW_Socket_Test.cpp b/ACE/tests/RAW_Socket_Test.cpp index 44d94be30bc18..e793ea7b23945 100644 --- a/ACE/tests/RAW_Socket_Test.cpp +++ b/ACE/tests/RAW_Socket_Test.cpp @@ -62,7 +62,6 @@ typedef struct _IPv6_HEADER_t uint32_t au32DstAddr[4]; uint64_t au64DstAddr[2]; }; - } IPv6_HEADER_t, *IPv6_HEADER_t_Ptr; typedef struct _UDP_HEADER_t @@ -77,7 +76,6 @@ typedef struct _UDP_HEADER_t uint32_t Checksum(uint32_t cksum, uint8_t *pBuffer, uint32_t size) { - if ((nullptr == pBuffer) || (0 == size)) { // return passed value @@ -89,7 +87,6 @@ uint32_t Checksum(uint32_t cksum, uint8_t *pBuffer, uint32_t size) while (size > 1) { - cksum += (static_cast(p[num]) << 8 & 0xff00) | (static_cast(p[num + 1]) & 0x00FF); size -= 2; num += 2; @@ -117,7 +114,6 @@ void ipv4_header_checksum(IPv4_HEADER_t_Ptr pIpHeader) uint32_t sum = Checksum(0, reinterpret_cast(pIpHeader), ipHeadLen); pIpHeader->u16CheckSum = htons(static_cast(~sum)); - } void udp6_header_checksum(IPv6_HEADER_t_Ptr pIpHeader, UDP_HEADER_t_Ptr ptUDPHeader, size_t udpLen) @@ -144,7 +140,6 @@ void udp6_header_checksum(IPv6_HEADER_t_Ptr pIpHeader, UDP_HEADER_t_Ptr ptUDPHea sum = Checksum(sum, reinterpret_cast(ptUDPHeader), udpLen); ptUDPHeader->u16CheckSum = htons(static_cast(~sum)); - } @@ -182,7 +177,6 @@ static char recvbuf[4096]; static int run_option_test () { - ACE_DEBUG ((LM_INFO, "%C begin to run ...\n", __func__)); ACE_INET_Addr addr(static_cast(0), "127.0.0.1"); @@ -224,7 +218,6 @@ run_option_test () static int run_reopen_test () { - ACE_DEBUG ((LM_INFO, "%C begin to run ...\n", __func__)); ACE_INET_Addr addr(static_cast(0), "127.0.0.1"); @@ -257,7 +250,6 @@ static void readUdpSocektToEmpty(ACE_SOCK_Dgram& udpSock) if(rc == -1) break; } - } static int raw_recv_data_until_meet_condition(ACE_RAW_SOCKET& raw, u_short port, size_t n, ACE_INET_Addr& remote, bool bUseIOVec = false, ACE_INET_Addr* to_addr = nullptr) @@ -305,7 +297,6 @@ static int raw_recv_data_until_meet_condition(ACE_RAW_SOCKET& raw, u_short port, { len = raw.recv(recvbuf, sizeof(recvbuf), remote, 0/*flags*/, nullptr, to_addr); } - } if(len < 0) From 06a7a651951bc35c3003a00d452e50620146472b Mon Sep 17 00:00:00 2001 From: SmithAchang Date: Wed, 5 Apr 2023 11:58:23 +0800 Subject: [PATCH 30/39] fix codefactor issue for Complex Method --- ACE/tests/RAW_Socket_Test.cpp | 105 ++++++++++++++-------------------- 1 file changed, 44 insertions(+), 61 deletions(-) diff --git a/ACE/tests/RAW_Socket_Test.cpp b/ACE/tests/RAW_Socket_Test.cpp index e793ea7b23945..21db3c5535ca9 100644 --- a/ACE/tests/RAW_Socket_Test.cpp +++ b/ACE/tests/RAW_Socket_Test.cpp @@ -262,80 +262,63 @@ static int raw_recv_data_until_meet_condition(ACE_RAW_SOCKET& raw, u_short port, do { - ACE_OS::memset(recvbuf, 0, sizeof(recvbuf)); - - if(bUseIOVec) - { - iovec vec[5]; - unsigned int i=0; - unsigned int const oneByteRecvVecNum = (sizeof(vec)/sizeof(vec[0])) - 1; - for(; i< oneByteRecvVecNum; ++i) - { - vec[i].iov_base = &recvbuf[i]; - vec[i].iov_len = 1; - } + ACE_OS::memset(recvbuf, 0, sizeof(recvbuf)); - vec[i].iov_base = &recvbuf[i]; - vec[i].iov_len = sizeof(recvbuf) - oneByteRecvVecNum; - - if(to_addr == nullptr) + if(bUseIOVec) { - raw.recv(vec, static_cast(sizeof(vec)/sizeof(vec[0])) , remote); + iovec vec[5]; + unsigned int i = 0; + unsigned int const oneByteRecvVecNum = (sizeof(vec)/sizeof(vec[0])) - 1; + for(; i< oneByteRecvVecNum; ++i) + { + vec[i].iov_base = &recvbuf[i]; + vec[i].iov_len = 1; + } + + vec[i].iov_base = &recvbuf[i]; + vec[i].iov_len = sizeof(recvbuf) - oneByteRecvVecNum; + + len = raw.recv(vec, static_cast(sizeof(vec)/sizeof(vec[0])), remote, 0/*flags*/, nullptr, to_addr); } else { - len = raw.recv(vec, static_cast(sizeof(vec)/sizeof(vec[0])), remote, 0/*flags*/, nullptr, to_addr); + len = raw.recv(recvbuf, sizeof(recvbuf), remote, 0/*flags*/, nullptr, to_addr); + } + + if(len < 0) + { + ACE_DEBUG ((LM_INFO, "%C receive prcess reach the end ...\n", __func__)); + return -1; } - } - else - { - if(to_addr == nullptr) + + u_short nDstPort; + char const* szInetType; + UDP_HEADER_t_Ptr ptUDPHeader; + if(local.get_type() == AF_INET) { - len = raw.recv(recvbuf, sizeof(recvbuf), remote); + ptUDPHeader = reinterpret_cast(recvbuf + sizeof(IPv4_HEADER_t)); + expectedLen = (n + sizeof(IPv4_HEADER_t) + sizeof(UDP_HEADER_t)); + szInetType = "IPv4"; } else { - len = raw.recv(recvbuf, sizeof(recvbuf), remote, 0/*flags*/, nullptr, to_addr); + ptUDPHeader = reinterpret_cast(recvbuf); + expectedLen = (n + sizeof(UDP_HEADER_t)); + szInetType = "IPv6"; + } + + nDstPort = ntohs (ptUDPHeader->u16DstPort); + if (port == nDstPort && len == expectedLen) + { + ACE_DEBUG ((LM_INFO, "%C %C recv expected pkgs ...\n", __func__, szInetType)); + break; } - } - - if(len < 0) - { - ACE_DEBUG ((LM_INFO, "%C receive prcess reach the end ...\n", __func__)); - return -1; - } - - UDP_HEADER_t_Ptr ptUDPHeader; - if(local.get_type() == AF_INET) - { - ptUDPHeader = reinterpret_cast(recvbuf + sizeof(IPv4_HEADER_t)); - expectedLen = (n + sizeof(IPv4_HEADER_t) + sizeof(UDP_HEADER_t)); - u_short nDstPort = ntohs(ptUDPHeader->u16DstPort); - - if(port == nDstPort && len == expectedLen) - { - ACE_DEBUG ((LM_INFO, "%C IPv4 recv expected pkgs ...\n", __func__)); - break; - } - } - else - { - ptUDPHeader = reinterpret_cast(recvbuf); - expectedLen = (n + sizeof(UDP_HEADER_t)); - u_short nDstPort = ntohs(ptUDPHeader->u16DstPort); - - if(port == nDstPort && len == expectedLen) - { - ACE_DEBUG ((LM_INFO, "%C IPv6 recv expected pkgs ...\n", __func__)); - break; - } - } - - ACE_DEBUG ((LM_DEBUG, "%C recv unexpected pkgs len: %d, srcPort: %u, dstPort:%u; expectedLen: %d, expectedPort: %u ...\n", __func__, len, ntohs(ptUDPHeader->u16SrcPort), ntohs(ptUDPHeader->u16DstPort), expectedLen, port)); - ACE_OS::sleep(1); + + ACE_DEBUG ((LM_DEBUG, "%C recv unexpected pkgs len: %d, srcPort: %u, dstPort:%u; expectedLen: %d, expectedPort: %u ...\n", __func__, len, ntohs(ptUDPHeader->u16SrcPort), ntohs(ptUDPHeader->u16DstPort), expectedLen, port)); + ACE_OS::sleep(1); } while (1); - return 0; + return 0; } static int From 6abfc987cdd6e8f82c8fd1cd2819053ca6f04ede Mon Sep 17 00:00:00 2001 From: SmithAchang Date: Wed, 5 Apr 2023 14:12:26 +0800 Subject: [PATCH 31/39] try to fix codacy issues --- ACE/ace/RAW_Socket.cpp | 8 ++++++++ ACE/tests/RAW_Socket_Test.cpp | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/ACE/ace/RAW_Socket.cpp b/ACE/ace/RAW_Socket.cpp index d4f60a851900c..9242ea190be38 100644 --- a/ACE/ace/RAW_Socket.cpp +++ b/ACE/ace/RAW_Socket.cpp @@ -114,6 +114,14 @@ static inline void fillMsgHdr(msghdr& recv_msg, const ACE_INET_Addr &addr, void* #endif } +#ifndef IP_RECVDSTADDR +#define IP_RECVDSTADDR 25 +#endif + +#ifndef IP_PKTINFO +# define IP_PKTINFO 19 +#endif + static inline void getToAddrFromMsgHdr(msghdr& recv_msg, ACE_INET_Addr& to_addr) { #if defined(ACE_USE_MSG_CONTROL) diff --git a/ACE/tests/RAW_Socket_Test.cpp b/ACE/tests/RAW_Socket_Test.cpp index 21db3c5535ca9..ea15399550961 100644 --- a/ACE/tests/RAW_Socket_Test.cpp +++ b/ACE/tests/RAW_Socket_Test.cpp @@ -257,7 +257,7 @@ static int raw_recv_data_until_meet_condition(ACE_RAW_SOCKET& raw, u_short port, ACE_INET_Addr local; raw.get_local_addr(local); - ssize_t len = 0; + ssize_t len; ssize_t expectedLen; do From b8af5e45fbeeff31a2afe4b00a454183f2ffd115 Mon Sep 17 00:00:00 2001 From: smithAchang Date: Wed, 5 Apr 2023 20:30:57 +0800 Subject: [PATCH 32/39] try to fix for codacy issues, maybe last modification --- ACE/ace/RAW_Socket.cpp | 20 +++++++++++--------- ACE/tests/RAW_Socket_Test.cpp | 2 +- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/ACE/ace/RAW_Socket.cpp b/ACE/ace/RAW_Socket.cpp index 9242ea190be38..df2a0d4288686 100644 --- a/ACE/ace/RAW_Socket.cpp +++ b/ACE/ace/RAW_Socket.cpp @@ -125,12 +125,13 @@ static inline void fillMsgHdr(msghdr& recv_msg, const ACE_INET_Addr &addr, void* static inline void getToAddrFromMsgHdr(msghdr& recv_msg, ACE_INET_Addr& to_addr) { #if defined(ACE_USE_MSG_CONTROL) - if (to_addr.get_type() == AF_INET) + if (to_addr.get_type() == AF_INET) { - for (cmsghdr *ptr = ACE_CMSG_FIRSTHDR (&recv_msg); ptr; ptr = ACE_CMSG_NXTHDR (&recv_msg, ptr)) + #if defined (IP_RECVDSTADDR) || defined (IP_PKTINFO) + for (cmsghdr *ptr = ACE_CMSG_FIRSTHDR (&recv_msg); ptr; ptr = ACE_CMSG_NXTHDR (&recv_msg, ptr)) { #if defined (IP_RECVDSTADDR) - if (ptr->cmsg_level == IPPROTO_IP && ptr->cmsg_type == IP_RECVDSTADDR) + if (ptr->cmsg_level == IPPROTO_IP && ptr->cmsg_type == IP_RECVDSTADDR) { to_addr.set_address (reinterpret_cast(ACE_CMSG_DATA (ptr)), sizeof (struct in_addr), @@ -139,24 +140,25 @@ static inline void getToAddrFromMsgHdr(msghdr& recv_msg, ACE_INET_Addr& to_addr) } #endif #if defined(IP_PKTINFO) - if (ptr->cmsg_level == IPPROTO_IP && ptr->cmsg_type == IP_PKTINFO) + if (ptr->cmsg_level == IPPROTO_IP && ptr->cmsg_type == IP_PKTINFO) { - to_addr.set_address ((const char *) &(((in_pktinfo *) (ACE_CMSG_DATA (ptr)))->ipi_addr), + to_addr.set_address (reinterpret_cast(&((reinterpret_cast(ACE_CMSG_DATA (ptr)))->ipi_addr)), sizeof (struct in_addr), 0); break; } #endif } + #endif } #if defined (ACE_HAS_IPV6) && defined (IPV6_PKTINFO) - else if (to_addr.get_type() == AF_INET6) + else if (to_addr.get_type() == AF_INET6) { - for (cmsghdr *ptr = ACE_CMSG_FIRSTHDR (&recv_msg); ptr; ptr = ACE_CMSG_NXTHDR (&recv_msg, ptr)) + for (cmsghdr *ptr = ACE_CMSG_FIRSTHDR (&recv_msg); ptr; ptr = ACE_CMSG_NXTHDR (&recv_msg, ptr)) { - if (ptr->cmsg_level == IPPROTO_IPV6 && ptr->cmsg_type == IPV6_PKTINFO) + if (ptr->cmsg_level == IPPROTO_IPV6 && ptr->cmsg_type == IPV6_PKTINFO) { - to_addr.set_address (reinterpret_cast(&((reinterpret_cast((ACE_CMSG_DATA(ptr))))->ipi6_addr)), + to_addr.set_address (reinterpret_cast(&((reinterpret_cast((ACE_CMSG_DATA(ptr))))->ipi6_addr)), sizeof (struct in6_addr), 0); break; diff --git a/ACE/tests/RAW_Socket_Test.cpp b/ACE/tests/RAW_Socket_Test.cpp index ea15399550961..818ef4a4bc89e 100644 --- a/ACE/tests/RAW_Socket_Test.cpp +++ b/ACE/tests/RAW_Socket_Test.cpp @@ -278,7 +278,7 @@ static int raw_recv_data_until_meet_condition(ACE_RAW_SOCKET& raw, u_short port, vec[i].iov_base = &recvbuf[i]; vec[i].iov_len = sizeof(recvbuf) - oneByteRecvVecNum; - len = raw.recv(vec, static_cast(sizeof(vec)/sizeof(vec[0])), remote, 0/*flags*/, nullptr, to_addr); + len = raw.recv(vec, static_cast(sizeof(vec)/sizeof(vec[0])), remote, 0/*flags*/, nullptr, to_addr); } else { From 9f2b0db08eec67a9c371cc94b7c07eed71848fae Mon Sep 17 00:00:00 2001 From: smithAchang Date: Thu, 6 Apr 2023 22:15:43 +0800 Subject: [PATCH 33/39] delete the defensive codes for hacking codacy issues --- ACE/ace/RAW_Socket.cpp | 8 -------- 1 file changed, 8 deletions(-) diff --git a/ACE/ace/RAW_Socket.cpp b/ACE/ace/RAW_Socket.cpp index df2a0d4288686..30f9fee6e144f 100644 --- a/ACE/ace/RAW_Socket.cpp +++ b/ACE/ace/RAW_Socket.cpp @@ -114,14 +114,6 @@ static inline void fillMsgHdr(msghdr& recv_msg, const ACE_INET_Addr &addr, void* #endif } -#ifndef IP_RECVDSTADDR -#define IP_RECVDSTADDR 25 -#endif - -#ifndef IP_PKTINFO -# define IP_PKTINFO 19 -#endif - static inline void getToAddrFromMsgHdr(msghdr& recv_msg, ACE_INET_Addr& to_addr) { #if defined(ACE_USE_MSG_CONTROL) From 6c163560d603c4292eb8e4d1f7708a087db57e9a Mon Sep 17 00:00:00 2001 From: smithAchang Date: Wed, 6 Sep 2023 21:58:24 +0800 Subject: [PATCH 34/39] add RAW_Socket_Test to run_test.lst --- ACE/tests/run_test.lst | 1 + 1 file changed, 1 insertion(+) diff --git a/ACE/tests/run_test.lst b/ACE/tests/run_test.lst index c8b018e3eb6cd..67db2060aa98e 100644 --- a/ACE/tests/run_test.lst +++ b/ACE/tests/run_test.lst @@ -197,6 +197,7 @@ Process_Manager_Test: !VxWorks !ACE_FOR_TAO !PHARLAP Process_Manual_Event_Test: !VxWorks !ACE_FOR_TAO !PHARLAP Process_Mutex_Test: !VxWorks !ACE_FOR_TAO !PHARLAP Process_Semaphore_Test: !VxWorks !ACE_FOR_TAO !PHARLAP +RAW_Socket_Test: linux RB_Tree_Test Bug_3332_Regression_Test Reactor_Dispatch_Order_Test From a88a1a86c6cfef3e3a4a848efb12009ee25a1823 Mon Sep 17 00:00:00 2001 From: smithAchang Date: Wed, 6 Sep 2023 22:50:15 +0800 Subject: [PATCH 35/39] fix macro indent --- ACE/ace/RAW_Socket.cpp | 81 ++++++++++++++++++++---------------------- 1 file changed, 38 insertions(+), 43 deletions(-) diff --git a/ACE/ace/RAW_Socket.cpp b/ACE/ace/RAW_Socket.cpp index 30f9fee6e144f..3b9584a5c3cdb 100644 --- a/ACE/ace/RAW_Socket.cpp +++ b/ACE/ace/RAW_Socket.cpp @@ -6,14 +6,13 @@ # include "ace/Malloc_Base.h" #endif /* ACE_HAS_ALLOC_HOOKS */ -// Included so users have access to ACE_RECVPKTINFO and ACE_RECVPKTINFO6 . +// Included so users have access to ACE_RECVPKTINFO and ACE_RECVPKTINFO6. #include "ace/OS_NS_sys_socket.h" #if defined (ACE_HAS_IPV6) && defined (ACE_WIN32) #include /**/ #endif - #if !defined (__ACE_INLINE__) # include "ace/RAW_Socket.inl" #endif /* __ACE_INLINE__ */ @@ -53,7 +52,6 @@ ACE_ALLOC_HOOK_DEFINE (ACE_RAW_SOCKET) return -1; \ }while(0) - void ACE_RAW_SOCKET::dump () const { @@ -97,32 +95,32 @@ static inline ssize_t using_common_recv(const ACE_RAW_SOCKET& raw, void *buf, si static inline void fillMsgHdr(msghdr& recv_msg, const ACE_INET_Addr &addr, void* pcbuf, int cbuf_size) { - #if defined (ACE_HAS_SOCKADDR_MSG_NAME) +#if defined (ACE_HAS_SOCKADDR_MSG_NAME) recv_msg.msg_name = static_cast (addr.get_addr ()); - #else +#else recv_msg.msg_name = static_cast(addr.get_addr ()); - #endif /* ACE_HAS_SOCKADDR_MSG_NAME */ +#endif /* ACE_HAS_SOCKADDR_MSG_NAME */ recv_msg.msg_namelen = addr.get_size (); - #ifdef ACE_USE_MSG_CONTROL +#ifdef ACE_USE_MSG_CONTROL recv_msg.msg_control = pcbuf; recv_msg.msg_controllen = cbuf_size; - #elif !defined ACE_LACKS_SENDMSG +#elif !defined ACE_LACKS_SENDMSG recv_msg.msg_accrights = 0; recv_msg.msg_accrightslen = 0; - #endif +#endif } static inline void getToAddrFromMsgHdr(msghdr& recv_msg, ACE_INET_Addr& to_addr) { - #if defined(ACE_USE_MSG_CONTROL) +#if defined (ACE_HAS_4_4BSD_SENDMSG_RECVMSG) || defined ACE_WIN32 if (to_addr.get_type() == AF_INET) { - #if defined (IP_RECVDSTADDR) || defined (IP_PKTINFO) +#if defined (IP_RECVDSTADDR) || defined (IP_PKTINFO) for (cmsghdr *ptr = ACE_CMSG_FIRSTHDR (&recv_msg); ptr; ptr = ACE_CMSG_NXTHDR (&recv_msg, ptr)) { - #if defined (IP_RECVDSTADDR) +#if defined (IP_RECVDSTADDR) if (ptr->cmsg_level == IPPROTO_IP && ptr->cmsg_type == IP_RECVDSTADDR) { to_addr.set_address (reinterpret_cast(ACE_CMSG_DATA (ptr)), @@ -130,8 +128,8 @@ static inline void getToAddrFromMsgHdr(msghdr& recv_msg, ACE_INET_Addr& to_addr) 0); break; } - #endif - #if defined(IP_PKTINFO) +#endif +#if defined(IP_PKTINFO) if (ptr->cmsg_level == IPPROTO_IP && ptr->cmsg_type == IP_PKTINFO) { to_addr.set_address (reinterpret_cast(&((reinterpret_cast(ACE_CMSG_DATA (ptr)))->ipi_addr)), @@ -139,11 +137,11 @@ static inline void getToAddrFromMsgHdr(msghdr& recv_msg, ACE_INET_Addr& to_addr) 0); break; } - #endif +#endif } - #endif +#endif } - #if defined (ACE_HAS_IPV6) && defined (IPV6_PKTINFO) +#if defined (ACE_HAS_IPV6) && defined (IPV6_PKTINFO) else if (to_addr.get_type() == AF_INET6) { for (cmsghdr *ptr = ACE_CMSG_FIRSTHDR (&recv_msg); ptr; ptr = ACE_CMSG_NXTHDR (&recv_msg, ptr)) @@ -157,11 +155,11 @@ static inline void getToAddrFromMsgHdr(msghdr& recv_msg, ACE_INET_Addr& to_addr) } } } - #endif - #else +#endif +#else ACE_UNUSED_ARG(recv_msg); ACE_UNUSED_ARG (to_addr); - #endif +#endif } ssize_t @@ -267,26 +265,26 @@ ACE_RAW_SOCKET::send (const iovec iov[], send_msg.msg_iov = const_cast(iov); send_msg.msg_iovlen = n; - #if defined (ACE_HAS_SOCKADDR_MSG_NAME) - send_msg.msg_name = static_cast(addr.get_addr()); - #else - send_msg.msg_name = static_cast(addr.get_addr ()); - #endif /* ACE_HAS_SOCKADDR_MSG_NAME */ - send_msg.msg_namelen = addr.get_size (); - - #if defined (ACE_HAS_4_4BSD_SENDMSG_RECVMSG) - send_msg.msg_control = 0; - send_msg.msg_controllen = 0; - send_msg.msg_flags = 0; - #elif !defined ACE_LACKS_SENDMSG - send_msg.msg_accrights = 0; - send_msg.msg_accrightslen = 0; - #endif /* ACE_HAS_4_4BSD_SENDMSG_RECVMSG */ - - #ifdef ACE_WIN32 - send_msg.msg_control = 0; - send_msg.msg_controllen = 0; - #endif +#if defined (ACE_HAS_SOCKADDR_MSG_NAME) + send_msg.msg_name = static_cast(addr.get_addr()); +#else + send_msg.msg_name = static_cast(addr.get_addr ()); +#endif /* ACE_HAS_SOCKADDR_MSG_NAME */ + send_msg.msg_namelen = addr.get_size (); + +#if defined (ACE_HAS_4_4BSD_SENDMSG_RECVMSG) + send_msg.msg_control = 0; + send_msg.msg_controllen = 0; + send_msg.msg_flags = 0; +#elif !defined ACE_LACKS_SENDMSG + send_msg.msg_accrights = 0; + send_msg.msg_accrightslen = 0; +#endif /* ACE_HAS_4_4BSD_SENDMSG_RECVMSG */ + +#ifdef ACE_WIN32 + send_msg.msg_control = 0; + send_msg.msg_controllen = 0; +#endif return ACE_OS::sendmsg (this->get_handle (), &send_msg, flags); } @@ -410,10 +408,7 @@ ACE_RAW_SOCKET::open (ACE_INET_Addr const & local, int protocol) } #endif - return 0; } - ACE_END_VERSIONED_NAMESPACE_DECL - From beab220c0c17195d93813c67dcb10067b6b537ec Mon Sep 17 00:00:00 2001 From: SmithAchang Date: Thu, 7 Sep 2023 21:52:19 +0800 Subject: [PATCH 36/39] each line always indent two space --- ACE/tests/RAW_Socket_Test.cpp | 802 +++++++++++++++++----------------- 1 file changed, 394 insertions(+), 408 deletions(-) diff --git a/ACE/tests/RAW_Socket_Test.cpp b/ACE/tests/RAW_Socket_Test.cpp index 818ef4a4bc89e..5aefbd869df1b 100644 --- a/ACE/tests/RAW_Socket_Test.cpp +++ b/ACE/tests/RAW_Socket_Test.cpp @@ -20,56 +20,55 @@ #include "ace/OS_NS_string.h" #include "ace/OS_NS_stdlib.h" - #pragma pack(push) #pragma pack(1) typedef struct _IP_HEADER_t { - uint8_t bVersionAndHeaderLen; - uint8_t bTypeOfService; - uint16_t u16TotalLenOfPacket; - uint16_t u16PacketID; - uint16_t u16Sliceinfo; - uint8_t bTTL; - uint8_t bTypeOfProtocol; - uint16_t u16CheckSum; - uint32_t u32SourIp; - uint32_t u32DestIp; + uint8_t bVersionAndHeaderLen; + uint8_t bTypeOfService; + uint16_t u16TotalLenOfPacket; + uint16_t u16PacketID; + uint16_t u16Sliceinfo; + uint8_t bTTL; + uint8_t bTypeOfProtocol; + uint16_t u16CheckSum; + uint32_t u32SourIp; + uint32_t u32DestIp; } IPv4_HEADER_t, *IPv4_HEADER_t_Ptr; typedef struct _IPv6_HEADER_t { - union - { - uint8_t abVTF[4]; - uint32_t dwVTF; - }; - - uint16_t u16PayloadLen; - uint8_t bNextHeader; - uint8_t bHopLimit; - - union - { - uint8_t abSrcAddr[16]; - uint32_t au32SrcAddr[4]; - uint64_t au64SrcAddr[2]; - }; - - union - { - uint8_t abDstAddr[16]; - uint32_t au32DstAddr[4]; - uint64_t au64DstAddr[2]; - }; + union + { + uint8_t abVTF[4]; + uint32_t dwVTF; + }; + + uint16_t u16PayloadLen; + uint8_t bNextHeader; + uint8_t bHopLimit; + + union + { + uint8_t abSrcAddr[16]; + uint32_t au32SrcAddr[4]; + uint64_t au64SrcAddr[2]; + }; + + union + { + uint8_t abDstAddr[16]; + uint32_t au32DstAddr[4]; + uint64_t au64DstAddr[2]; + }; } IPv6_HEADER_t, *IPv6_HEADER_t_Ptr; typedef struct _UDP_HEADER_t { - uint16_t u16SrcPort; - uint16_t u16DstPort; - uint16_t u16Length; - uint16_t u16CheckSum; + uint16_t u16SrcPort; + uint16_t u16DstPort; + uint16_t u16Length; + uint16_t u16CheckSum; } UDP_HEADER_t, *UDP_HEADER_t_Ptr; #pragma pack(pop) @@ -78,8 +77,8 @@ uint32_t Checksum(uint32_t cksum, uint8_t *pBuffer, uint32_t size) { if ((nullptr == pBuffer) || (0 == size)) { - // return passed value - return cksum; + // return passed value + return cksum; } uint32_t num = 0; @@ -87,9 +86,9 @@ uint32_t Checksum(uint32_t cksum, uint8_t *pBuffer, uint32_t size) while (size > 1) { - cksum += (static_cast(p[num]) << 8 & 0xff00) | (static_cast(p[num + 1]) & 0x00FF); - size -= 2; - num += 2; + cksum += (static_cast(p[num]) << 8 & 0xff00) | (static_cast(p[num + 1]) & 0x00FF); + size -= 2; + num += 2; } if (size > 0) @@ -107,51 +106,49 @@ uint32_t Checksum(uint32_t cksum, uint8_t *pBuffer, uint32_t size) void ipv4_header_checksum(IPv4_HEADER_t_Ptr pIpHeader) { - uint8_t ipHeadLen = (pIpHeader->bVersionAndHeaderLen & 0x0F) << 2; + uint8_t ipHeadLen = (pIpHeader->bVersionAndHeaderLen & 0x0F) << 2; - pIpHeader->u16CheckSum = 0; + pIpHeader->u16CheckSum = 0; - uint32_t sum = Checksum(0, reinterpret_cast(pIpHeader), ipHeadLen); + uint32_t sum = Checksum(0, reinterpret_cast(pIpHeader), ipHeadLen); - pIpHeader->u16CheckSum = htons(static_cast(~sum)); + pIpHeader->u16CheckSum = htons(static_cast(~sum)); } void udp6_header_checksum(IPv6_HEADER_t_Ptr pIpHeader, UDP_HEADER_t_Ptr ptUDPHeader, size_t udpLen) { - uint32_t sum = 0; + uint32_t sum = 0; - //udpLen = sizeof(UDP_HEADER_T) + contentLen; + //udpLen = sizeof(UDP_HEADER_T) + contentLen; - // - ptUDPHeader->u16Length = htons(udpLen); - sum += udpLen; + // + ptUDPHeader->u16Length = htons(udpLen); + sum += udpLen; - //sum += (ptUDPHeader->u16Length >> 8 & 0x00FF); - //sum += (ptUDPHeader->u16Length << 8 & 0xFF00); + //sum += (ptUDPHeader->u16Length >> 8 & 0x00FF); + //sum += (ptUDPHeader->u16Length << 8 & 0xFF00); - sum = Checksum(sum, &pIpHeader->abSrcAddr[0], 16); - sum = Checksum(sum, &pIpHeader->abDstAddr[0], 16); + sum = Checksum(sum, &pIpHeader->abSrcAddr[0], 16); + sum = Checksum(sum, &pIpHeader->abDstAddr[0], 16); - sum += (static_cast(pIpHeader->bNextHeader) & 0x00FF); - //finish the pseudo header checksum + sum += (static_cast(pIpHeader->bNextHeader) & 0x00FF); + //finish the pseudo header checksum - //udp section - ptUDPHeader->u16CheckSum = 0; - sum = Checksum(sum, reinterpret_cast(ptUDPHeader), udpLen); + //udp section + ptUDPHeader->u16CheckSum = 0; + sum = Checksum(sum, reinterpret_cast(ptUDPHeader), udpLen); - ptUDPHeader->u16CheckSum = htons(static_cast(~sum)); + ptUDPHeader->u16CheckSum = htons(static_cast(~sum)); } - - class SockGuard : private ACE_Copy_Disabled { public: -explicit SockGuard(ACE_SOCK& sock):sock_(sock){}; -~SockGuard(){ sock_.close(); }; + explicit SockGuard(ACE_SOCK& sock):sock_(sock){}; + ~SockGuard(){ sock_.close(); }; private: - ACE_SOCK& sock_; + ACE_SOCK& sock_; }; @@ -160,7 +157,7 @@ do{\ if(expression)\ {\ ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT (message)), 1);\ -}\ + }\ }while(0) static char sendbuf[4096]; @@ -189,7 +186,6 @@ run_option_test () ACE_TEST_EXCEPTION_RETURN(rc < 0, " can not bind the addr\n"); - int optval = 0, optlen = sizeof(optval); rc = rawSocket.get_option(SOL_SOCKET, SO_RCVBUF, &optval, &optlen); @@ -214,7 +210,6 @@ run_option_test () return 0; } - static int run_reopen_test () { @@ -226,7 +221,6 @@ run_reopen_test () ACE_TEST_EXCEPTION_RETURN(rawSocket.get_handle() == ACE_INVALID_HANDLE, " can not bind the addr\n"); - rawSocket.close(); ACE_TEST_EXCEPTION_RETURN(rawSocket.get_handle() != ACE_INVALID_HANDLE, " close in failure\n"); @@ -237,7 +231,6 @@ run_reopen_test () ACE_TEST_EXCEPTION_RETURN(rc < 0, " reopen in failue\n"); ACE_TEST_EXCEPTION_RETURN(rawSocket.get_handle() == ACE_INVALID_HANDLE, " handle is invalid after re-open\n"); - return 0; } @@ -254,95 +247,95 @@ static void readUdpSocektToEmpty(ACE_SOCK_Dgram& udpSock) static int raw_recv_data_until_meet_condition(ACE_RAW_SOCKET& raw, u_short port, size_t n, ACE_INET_Addr& remote, bool bUseIOVec = false, ACE_INET_Addr* to_addr = nullptr) { - ACE_INET_Addr local; - raw.get_local_addr(local); - - ssize_t len; - ssize_t expectedLen; - - do - { - ACE_OS::memset(recvbuf, 0, sizeof(recvbuf)); - - if(bUseIOVec) - { - iovec vec[5]; - unsigned int i = 0; - unsigned int const oneByteRecvVecNum = (sizeof(vec)/sizeof(vec[0])) - 1; - for(; i< oneByteRecvVecNum; ++i) - { - vec[i].iov_base = &recvbuf[i]; - vec[i].iov_len = 1; - } - - vec[i].iov_base = &recvbuf[i]; - vec[i].iov_len = sizeof(recvbuf) - oneByteRecvVecNum; - - len = raw.recv(vec, static_cast(sizeof(vec)/sizeof(vec[0])), remote, 0/*flags*/, nullptr, to_addr); - } - else - { - len = raw.recv(recvbuf, sizeof(recvbuf), remote, 0/*flags*/, nullptr, to_addr); - } - - if(len < 0) - { - ACE_DEBUG ((LM_INFO, "%C receive prcess reach the end ...\n", __func__)); - return -1; - } - - u_short nDstPort; - char const* szInetType; - UDP_HEADER_t_Ptr ptUDPHeader; - if(local.get_type() == AF_INET) - { - ptUDPHeader = reinterpret_cast(recvbuf + sizeof(IPv4_HEADER_t)); - expectedLen = (n + sizeof(IPv4_HEADER_t) + sizeof(UDP_HEADER_t)); - szInetType = "IPv4"; - } - else - { - ptUDPHeader = reinterpret_cast(recvbuf); - expectedLen = (n + sizeof(UDP_HEADER_t)); - szInetType = "IPv6"; - } - - nDstPort = ntohs (ptUDPHeader->u16DstPort); - if (port == nDstPort && len == expectedLen) - { - ACE_DEBUG ((LM_INFO, "%C %C recv expected pkgs ...\n", __func__, szInetType)); - break; - } - - ACE_DEBUG ((LM_DEBUG, "%C recv unexpected pkgs len: %d, srcPort: %u, dstPort:%u; expectedLen: %d, expectedPort: %u ...\n", __func__, len, ntohs(ptUDPHeader->u16SrcPort), ntohs(ptUDPHeader->u16DstPort), expectedLen, port)); - ACE_OS::sleep(1); - } while (1); - - return 0; + ACE_INET_Addr local; + raw.get_local_addr(local); + + ssize_t len; + ssize_t expectedLen; + + do + { + ACE_OS::memset(recvbuf, 0, sizeof(recvbuf)); + + if(bUseIOVec) + { + iovec vec[5]; + unsigned int i = 0; + unsigned int const oneByteRecvVecNum = (sizeof(vec)/sizeof(vec[0])) - 1; + for(; i< oneByteRecvVecNum; ++i) + { + vec[i].iov_base = &recvbuf[i]; + vec[i].iov_len = 1; + } + + vec[i].iov_base = &recvbuf[i]; + vec[i].iov_len = sizeof(recvbuf) - oneByteRecvVecNum; + + len = raw.recv(vec, static_cast(sizeof(vec)/sizeof(vec[0])), remote, 0/*flags*/, nullptr, to_addr); + } + else + { + len = raw.recv(recvbuf, sizeof(recvbuf), remote, 0/*flags*/, nullptr, to_addr); + } + + if(len < 0) + { + ACE_DEBUG ((LM_INFO, "%C receive prcess reach the end ...\n", __func__)); + return -1; + } + + u_short nDstPort; + char const* szInetType; + UDP_HEADER_t_Ptr ptUDPHeader; + if(local.get_type() == AF_INET) + { + ptUDPHeader = reinterpret_cast(recvbuf + sizeof(IPv4_HEADER_t)); + expectedLen = (n + sizeof(IPv4_HEADER_t) + sizeof(UDP_HEADER_t)); + szInetType = "IPv4"; + } + else + { + ptUDPHeader = reinterpret_cast(recvbuf); + expectedLen = (n + sizeof(UDP_HEADER_t)); + szInetType = "IPv6"; + } + + nDstPort = ntohs (ptUDPHeader->u16DstPort); + if (port == nDstPort && len == expectedLen) + { + ACE_DEBUG ((LM_INFO, "%C %C recv expected pkgs ...\n", __func__, szInetType)); + break; + } + + ACE_DEBUG ((LM_DEBUG, "%C recv unexpected pkgs len: %d, srcPort: %u, dstPort:%u; expectedLen: %d, expectedPort: %u ...\n", __func__, len, ntohs(ptUDPHeader->u16SrcPort), ntohs(ptUDPHeader->u16DstPort), expectedLen, port)); + ACE_OS::sleep(1); + } while (1); + + return 0; } static int run_raw_udp_test_child_flow_sendby_self (ACE_RAW_SOCKET& raw, ACE_INET_Addr& client_addr, ACE_INET_Addr& server_addr, size_t n) { - ACE_DEBUG ((LM_INFO, "%C begin to run when sending data by self ...\n", __func__)); + ACE_DEBUG ((LM_INFO, "%C begin to run when sending data by self ...\n", __func__)); - UDP_HEADER_t_Ptr ptUDPHeader = reinterpret_cast(sendbuf + sizeof(IPv4_HEADER_t_Ptr)); + UDP_HEADER_t_Ptr ptUDPHeader = reinterpret_cast(sendbuf + sizeof(IPv4_HEADER_t_Ptr)); - ptUDPHeader->u16SrcPort = htons(client_addr.get_port_number()); - ptUDPHeader->u16DstPort = htons(server_addr.get_port_number()); - ptUDPHeader->u16Length = htons(n + sizeof(UDP_HEADER_t)); - ptUDPHeader->u16CheckSum = 0; + ptUDPHeader->u16SrcPort = htons(client_addr.get_port_number()); + ptUDPHeader->u16DstPort = htons(server_addr.get_port_number()); + ptUDPHeader->u16Length = htons(n + sizeof(UDP_HEADER_t)); + ptUDPHeader->u16CheckSum = 0; - int rc = raw.send(ptUDPHeader, n + sizeof(UDP_HEADER_t), server_addr); - ssize_t expectedLen = n + sizeof(UDP_HEADER_t); - ACE_TEST_EXCEPTION_RETURN(rc != expectedLen, " raw socket can not send test pkg to server\n"); + int rc = raw.send(ptUDPHeader, n + sizeof(UDP_HEADER_t), server_addr); + ssize_t expectedLen = n + sizeof(UDP_HEADER_t); + ACE_TEST_EXCEPTION_RETURN(rc != expectedLen, " raw socket can not send test pkg to server\n"); - u_short server_port = server_addr.get_port_number(); - ACE_INET_Addr remote; - rc = raw_recv_data_until_meet_condition(raw, server_port, n, remote); - ACE_TEST_EXCEPTION_RETURN(rc != 0, " can recv test pkg from raw socket\n"); + u_short server_port = server_addr.get_port_number(); + ACE_INET_Addr remote; + rc = raw_recv_data_until_meet_condition(raw, server_port, n, remote); + ACE_TEST_EXCEPTION_RETURN(rc != 0, " can recv test pkg from raw socket\n"); - return 0; + return 0; } static int @@ -385,14 +378,14 @@ run_raw_udp_test () rc = run_raw_udp_test_child_flow_sendby_self (rawSocket, client_addr, server_addr, n + 1); ACE_TEST_EXCEPTION_RETURN(rc != 0, " can recv test pkg from raw socket when sending by self\n"); - #if !defined (ACE_WIN32) +#if !defined (ACE_WIN32) if(ACE_OS::getuid() == 0) { - ACE_DEBUG ((LM_INFO, "%C test send & recv big pkt ...\n", __func__)); - rc = run_raw_udp_test_child_flow_sendby_self (rawSocket, client_addr, server_addr, n + 2048); - ACE_TEST_EXCEPTION_RETURN(rc != 0, " can recv test pkg from raw socket when sending big pkg by self\n"); + ACE_DEBUG ((LM_INFO, "%C test send & recv big pkt ...\n", __func__)); + rc = run_raw_udp_test_child_flow_sendby_self (rawSocket, client_addr, server_addr, n + 2048); + ACE_TEST_EXCEPTION_RETURN(rc != 0, " can recv test pkg from raw socket when sending big pkg by self\n"); } - #endif +#endif return 0; } @@ -400,339 +393,332 @@ run_raw_udp_test () static int run_raw_generic_test () { - ACE_DEBUG ((LM_INFO, "%C begin to run generic raw socket i.e. send only RAW socket ...\n", __func__)); - - ACE_INET_Addr bindAddr(static_cast(0), "127.0.0.1"), remote; - ACE_INET_Addr client_addr(static_cast(0), "127.0.0.7") ,server_addr(static_cast(0), "127.0.0.8"); - ACE_SOCK_Dgram client_dgram(client_addr); - SockGuard client_dgram_guard(client_dgram); - - ACE_SOCK_Dgram server_dgram(server_addr); - SockGuard server_dgram_guard(server_dgram); - - ACE_RAW_SOCKET rawSocket(bindAddr, IPPROTO_RAW); - SockGuard raw_guard(rawSocket); - - ACE_TEST_EXCEPTION_RETURN(rawSocket.is_send_only() == false, " raw socket is not send only\n"); - - ssize_t len = rawSocket.recv(recvbuf, sizeof(recvbuf), remote); - ACE_TEST_EXCEPTION_RETURN(len != -1, " raw generic socket is send only , must not can recv data\n"); - - - client_dgram.get_local_addr (client_addr); - server_dgram.get_local_addr (server_addr); - - IPv4_HEADER_t_Ptr ptIPv4Header = reinterpret_cast(sendbuf); - UDP_HEADER_t_Ptr ptUDPHeader = reinterpret_cast(sendbuf + sizeof(IPv4_HEADER_t)); - u_short n = 2048; - - *ptIPv4Header = {}; - - ptIPv4Header->bVersionAndHeaderLen = 0x45; - ptIPv4Header->u16TotalLenOfPacket = htons(sizeof(IPv4_HEADER_t) + sizeof(UDP_HEADER_t) + n); // 数据包长度 - ptIPv4Header->u16PacketID = ACE_OS::rand(); - ptIPv4Header->bTTL = 64; - ptIPv4Header->bTypeOfProtocol = IPPROTO_UDP; - ptIPv4Header->u16CheckSum = 0; - ptIPv4Header->u32SourIp = (static_cast(client_addr.get_addr()))->sin_addr.s_addr; - ptIPv4Header->u32DestIp = (static_cast(server_addr.get_addr()))->sin_addr.s_addr; - - u_short client_port_number = client_addr.get_port_number(); - u_short server_port_number = server_addr.get_port_number(); - - - ptUDPHeader->u16SrcPort = htons(client_port_number); - ptUDPHeader->u16DstPort = htons(server_port_number); - ptUDPHeader->u16Length = htons(sizeof(UDP_HEADER_t) + n); - ptUDPHeader->u16CheckSum = 0; - - #if !defined (ACE_WIN32) - if(ACE_OS::getuid() == 0) - { - ACE_DEBUG ((LM_INFO, "%C raw generic socket will send bytes exceeding the MTU ...\n", __func__)); - n = 2048; - len = rawSocket.send(sendbuf, sizeof(IPv4_HEADER_t) + sizeof(UDP_HEADER_t) + n, server_addr); - ACE_TEST_EXCEPTION_RETURN(len != -1, " raw generic socket can not send pkg more than MTU\n"); - } - #endif - - n = 468; - ptUDPHeader->u16Length = htons(sizeof(UDP_HEADER_t) + n); - len = rawSocket.send(sendbuf, sizeof(IPv4_HEADER_t) + sizeof(UDP_HEADER_t) + n, server_addr); - size_t expectedLen = (sizeof(IPv4_HEADER_t) + sizeof(UDP_HEADER_t) + n); - ACE_DEBUG ((LM_INFO, "%C raw generic socket send %d bytes, expected %u bytes ...\n", __func__, len, expectedLen)); - ACE_TEST_EXCEPTION_RETURN(static_cast(len) != expectedLen, " raw generic socket send pkg in failure\n"); - - ACE_OS::sleep(1); - ACE_DEBUG ((LM_INFO, "%C enable nonblock status ...\n", __func__)); - server_dgram.enable(ACE_NONBLOCK); - len = server_dgram.recv(recvbuf, sizeof(recvbuf), remote); - expectedLen = n; - ACE_DEBUG ((LM_INFO, "%C udp server socket recv %d bytes, expected %u bytes ...\n", __func__, len, expectedLen)); - ACE_TEST_EXCEPTION_RETURN(static_cast(len) != n, " server socket receives pkg in failure length is not the same\n"); - ACE_TEST_EXCEPTION_RETURN(static_cast(remote.get_addr())->sin_addr.s_addr != static_cast(client_addr.get_addr())->sin_addr.s_addr, " server socket receives pkg in failure: the source IP is not the same\n"); - - return 0; + ACE_DEBUG ((LM_INFO, "%C begin to run generic raw socket i.e. send only RAW socket ...\n", __func__)); + + ACE_INET_Addr bindAddr(static_cast(0), "127.0.0.1"), remote; + ACE_INET_Addr client_addr(static_cast(0), "127.0.0.7") ,server_addr(static_cast(0), "127.0.0.8"); + ACE_SOCK_Dgram client_dgram(client_addr); + SockGuard client_dgram_guard(client_dgram); + + ACE_SOCK_Dgram server_dgram(server_addr); + SockGuard server_dgram_guard(server_dgram); + + ACE_RAW_SOCKET rawSocket(bindAddr, IPPROTO_RAW); + SockGuard raw_guard(rawSocket); + + ACE_TEST_EXCEPTION_RETURN(rawSocket.is_send_only() == false, " raw socket is not send only\n"); + + ssize_t len = rawSocket.recv(recvbuf, sizeof(recvbuf), remote); + ACE_TEST_EXCEPTION_RETURN(len != -1, " raw generic socket is send only , must not can recv data\n"); + + client_dgram.get_local_addr (client_addr); + server_dgram.get_local_addr (server_addr); + + IPv4_HEADER_t_Ptr ptIPv4Header = reinterpret_cast(sendbuf); + UDP_HEADER_t_Ptr ptUDPHeader = reinterpret_cast(sendbuf + sizeof(IPv4_HEADER_t)); + u_short n = 2048; + + *ptIPv4Header = {}; + ptIPv4Header->bVersionAndHeaderLen = 0x45; + ptIPv4Header->u16TotalLenOfPacket = htons(sizeof(IPv4_HEADER_t) + sizeof(UDP_HEADER_t) + n); // 数据包长度 + ptIPv4Header->u16PacketID = ACE_OS::rand(); + ptIPv4Header->bTTL = 64; + ptIPv4Header->bTypeOfProtocol = IPPROTO_UDP; + ptIPv4Header->u16CheckSum = 0; + ptIPv4Header->u32SourIp = (static_cast(client_addr.get_addr()))->sin_addr.s_addr; + ptIPv4Header->u32DestIp = (static_cast(server_addr.get_addr()))->sin_addr.s_addr; + + u_short client_port_number = client_addr.get_port_number(); + u_short server_port_number = server_addr.get_port_number(); + + ptUDPHeader->u16SrcPort = htons(client_port_number); + ptUDPHeader->u16DstPort = htons(server_port_number); + ptUDPHeader->u16Length = htons(sizeof(UDP_HEADER_t) + n); + ptUDPHeader->u16CheckSum = 0; + +#if !defined (ACE_WIN32) + if(ACE_OS::getuid() == 0) + { + ACE_DEBUG ((LM_INFO, "%C raw generic socket will send bytes exceeding the MTU ...\n", __func__)); + n = 2048; + len = rawSocket.send(sendbuf, sizeof(IPv4_HEADER_t) + sizeof(UDP_HEADER_t) + n, server_addr); + ACE_TEST_EXCEPTION_RETURN(len != -1, " raw generic socket can not send pkg more than MTU\n"); + } +#endif + + n = 468; + ptUDPHeader->u16Length = htons(sizeof(UDP_HEADER_t) + n); + len = rawSocket.send(sendbuf, sizeof(IPv4_HEADER_t) + sizeof(UDP_HEADER_t) + n, server_addr); + size_t expectedLen = (sizeof(IPv4_HEADER_t) + sizeof(UDP_HEADER_t) + n); + ACE_DEBUG ((LM_INFO, "%C raw generic socket send %d bytes, expected %u bytes ...\n", __func__, len, expectedLen)); + ACE_TEST_EXCEPTION_RETURN(static_cast(len) != expectedLen, " raw generic socket send pkg in failure\n"); + + ACE_OS::sleep(1); + ACE_DEBUG ((LM_INFO, "%C enable nonblock status ...\n", __func__)); + server_dgram.enable(ACE_NONBLOCK); + len = server_dgram.recv(recvbuf, sizeof(recvbuf), remote); + expectedLen = n; + ACE_DEBUG ((LM_INFO, "%C udp server socket recv %d bytes, expected %u bytes ...\n", __func__, len, expectedLen)); + ACE_TEST_EXCEPTION_RETURN(static_cast(len) != n, " server socket receives pkg in failure length is not the same\n"); + ACE_TEST_EXCEPTION_RETURN(static_cast(remote.get_addr())->sin_addr.s_addr != static_cast(client_addr.get_addr())->sin_addr.s_addr, " server socket receives pkg in failure: the source IP is not the same\n"); + + return 0; } #if defined (ACE_HAS_IPV6) static int run_ipv6_pkginfo_test () { - ACE_DEBUG ((LM_INFO, "%C begin to run IPv6 pkginfo test ...\n", __func__)); + ACE_DEBUG ((LM_INFO, "%C begin to run IPv6 pkginfo test ...\n", __func__)); - ACE_INET_Addr bindAddr(static_cast(0), "::1"); - ACE_INET_Addr anyAddr(static_cast(0), "::"); + ACE_INET_Addr bindAddr(static_cast(0), "::1"); + ACE_INET_Addr anyAddr(static_cast(0), "::"); - ACE_INET_Addr client_addr(static_cast(0), "::1") ,server_addr(static_cast(0), "::1"); + ACE_INET_Addr client_addr(static_cast(0), "::1") ,server_addr(static_cast(0), "::1"); + ACE_SOCK_Dgram client_dgram(client_addr); + SockGuard client_dgram_guard(client_dgram); + client_dgram.get_local_addr(client_addr); - ACE_SOCK_Dgram client_dgram(client_addr); - SockGuard client_dgram_guard(client_dgram); - client_dgram.get_local_addr(client_addr); + ACE_SOCK_Dgram server_dgram(server_addr); + SockGuard server_dgram_guard(server_dgram); + server_dgram.get_local_addr(server_addr); - ACE_SOCK_Dgram server_dgram(server_addr); - SockGuard server_dgram_guard(server_dgram); - server_dgram.get_local_addr(server_addr); + ACE_DEBUG ((LM_INFO, "%C get the real bound addr and port client_port: %u, server_port: %u...\n", __func__, client_addr.get_port_number(), server_addr.get_port_number())); - ACE_DEBUG ((LM_INFO, "%C get the real bound addr and port client_port: %u, server_port: %u...\n", __func__, client_addr.get_port_number(), server_addr.get_port_number())); - - ACE_RAW_SOCKET rawSocket(bindAddr, IPPROTO_UDP); - rawSocket.enable(ACE_NONBLOCK); - SockGuard raw_guard(rawSocket); + ACE_RAW_SOCKET rawSocket(bindAddr, IPPROTO_UDP); + rawSocket.enable(ACE_NONBLOCK); + SockGuard raw_guard(rawSocket); - ACE_RAW_SOCKET rawWildcardSocket(anyAddr, IPPROTO_UDP); - rawWildcardSocket.enable(ACE_NONBLOCK); - SockGuard raw_wildcard_guard(rawWildcardSocket); + ACE_RAW_SOCKET rawWildcardSocket(anyAddr, IPPROTO_UDP); + rawWildcardSocket.enable(ACE_NONBLOCK); + SockGuard raw_wildcard_guard(rawWildcardSocket); - client_dgram.send("hello world", sizeof("hello world"), server_addr); + client_dgram.send("hello world", sizeof("hello world"), server_addr); - ACE_DEBUG ((LM_INFO, "%C the send pkg will be received by two raw sockets ...\n", __func__)); - ACE_OS::sleep(1); + ACE_DEBUG ((LM_INFO, "%C the send pkg will be received by two raw sockets ...\n", __func__)); + ACE_OS::sleep(1); - ACE_INET_Addr remote; - int rc = raw_recv_data_until_meet_condition(rawSocket, server_addr.get_port_number(), sizeof("hello world"), remote); + ACE_INET_Addr remote; + int rc = raw_recv_data_until_meet_condition(rawSocket, server_addr.get_port_number(), sizeof("hello world"), remote); - ACE_TEST_EXCEPTION_RETURN(rc != 0, " non wildcard raw socket can not recv expectedRecvLen\n"); + ACE_TEST_EXCEPTION_RETURN(rc != 0, " non wildcard raw socket can not recv expectedRecvLen\n"); - ACE_INET_Addr to_addr; + ACE_INET_Addr to_addr; - ACE_DEBUG ((LM_INFO, "%C send pkg again to test common raw socket with to_adr parameter ...\n", __func__)); - client_dgram.send("hello world", sizeof("hello world"), server_addr); - ACE_OS::sleep(1); + ACE_DEBUG ((LM_INFO, "%C send pkg again to test common raw socket with to_adr parameter ...\n", __func__)); + client_dgram.send("hello world", sizeof("hello world"), server_addr); + ACE_OS::sleep(1); - rc = raw_recv_data_until_meet_condition(rawSocket, server_addr.get_port_number(), sizeof("hello world"), remote, false, &to_addr); - ACE_TEST_EXCEPTION_RETURN(rc != 0, " non wildcard raw socket can not recv expectedRecvLen with to_addr parameter\n"); + rc = raw_recv_data_until_meet_condition(rawSocket, server_addr.get_port_number(), sizeof("hello world"), remote, false, &to_addr); + ACE_TEST_EXCEPTION_RETURN(rc != 0, " non wildcard raw socket can not recv expectedRecvLen with to_addr parameter\n"); - in6_addr* remote_sin6_addr = &(static_cast(remote.get_addr())->sin6_addr); - in6_addr* to_sin6_addr = &(static_cast(to_addr.get_addr())->sin6_addr); - int cmp = ACE_OS::memcmp(remote_sin6_addr, to_sin6_addr, sizeof(*to_sin6_addr)); - ACE_TEST_EXCEPTION_RETURN(cmp != 0, " non wildcard raw socket got to_addr with invalid value\n"); + in6_addr* remote_sin6_addr = &(static_cast(remote.get_addr())->sin6_addr); + in6_addr* to_sin6_addr = &(static_cast(to_addr.get_addr())->sin6_addr); + int cmp = ACE_OS::memcmp(remote_sin6_addr, to_sin6_addr, sizeof(*to_sin6_addr)); + ACE_TEST_EXCEPTION_RETURN(cmp != 0, " non wildcard raw socket got to_addr with invalid value\n"); - rc = raw_recv_data_until_meet_condition(rawWildcardSocket, server_addr.get_port_number(), sizeof("hello world"), remote, false, &to_addr); - ACE_TEST_EXCEPTION_RETURN(rc != 0, " can not recv expectedRecvLen with to_addr when provided to wildcard RAW socket\n"); - remote_sin6_addr = &(static_cast(remote.get_addr())->sin6_addr); - to_sin6_addr = &(static_cast(to_addr.get_addr())->sin6_addr); - cmp = ACE_OS::memcmp(remote_sin6_addr, to_sin6_addr, sizeof(*to_sin6_addr)); - ACE_TEST_EXCEPTION_RETURN(cmp != 0, " to_addr with invalid value when provided to wildcard RAW socket\n"); + rc = raw_recv_data_until_meet_condition(rawWildcardSocket, server_addr.get_port_number(), sizeof("hello world"), remote, false, &to_addr); + ACE_TEST_EXCEPTION_RETURN(rc != 0, " can not recv expectedRecvLen with to_addr when provided to wildcard RAW socket\n"); + remote_sin6_addr = &(static_cast(remote.get_addr())->sin6_addr); + to_sin6_addr = &(static_cast(to_addr.get_addr())->sin6_addr); + cmp = ACE_OS::memcmp(remote_sin6_addr, to_sin6_addr, sizeof(*to_sin6_addr)); + ACE_TEST_EXCEPTION_RETURN(cmp != 0, " to_addr with invalid value when provided to wildcard RAW socket\n"); - return 0; + return 0; } #endif static int run_iovec_IPv6_api_test () { - ACE_DEBUG ((LM_INFO, "%C begin to run IPv6 iovec api test ...\n", __func__)); + ACE_DEBUG ((LM_INFO, "%C begin to run IPv6 iovec api test ...\n", __func__)); - ACE_INET_Addr bindAddr(static_cast(0), "::1"); - ACE_INET_Addr anyAddr(static_cast(0), "::"); + ACE_INET_Addr bindAddr(static_cast(0), "::1"); + ACE_INET_Addr anyAddr(static_cast(0), "::"); - ACE_INET_Addr client_addr(static_cast(0), "::1") ,server_addr(static_cast(0), "::1"); + ACE_INET_Addr client_addr(static_cast(0), "::1") ,server_addr(static_cast(0), "::1"); - ACE_SOCK_Dgram client_dgram(client_addr); - SockGuard client_dgram_guard(client_dgram); - client_dgram.get_local_addr(client_addr); + ACE_SOCK_Dgram client_dgram(client_addr); + SockGuard client_dgram_guard(client_dgram); + client_dgram.get_local_addr(client_addr); - ACE_SOCK_Dgram server_dgram(server_addr); - server_dgram.enable(ACE_NONBLOCK); - SockGuard server_dgram_guard(server_dgram); - server_dgram.get_local_addr(server_addr); + ACE_SOCK_Dgram server_dgram(server_addr); + server_dgram.enable(ACE_NONBLOCK); + SockGuard server_dgram_guard(server_dgram); + server_dgram.get_local_addr(server_addr); - ACE_DEBUG ((LM_INFO, "%C get the real bound addr and port client_port: %u, server_port: %u...\n", __func__, client_addr.get_port_number(), server_addr.get_port_number())); + ACE_DEBUG ((LM_INFO, "%C get the real bound addr and port client_port: %u, server_port: %u...\n", __func__, client_addr.get_port_number(), server_addr.get_port_number())); - ACE_RAW_SOCKET rawSocket(bindAddr, IPPROTO_UDP); - rawSocket.enable(ACE_NONBLOCK); - SockGuard raw_guard(rawSocket); - - client_dgram.send("hello world", sizeof("hello world"), server_addr); - - ACE_DEBUG ((LM_INFO, "%C the send pkg will be received by raw sockets ...\n", __func__)); - ACE_OS::sleep(1); + ACE_RAW_SOCKET rawSocket(bindAddr, IPPROTO_UDP); + rawSocket.enable(ACE_NONBLOCK); + SockGuard raw_guard(rawSocket); - ACE_INET_Addr remote; - int rc = raw_recv_data_until_meet_condition(rawSocket, server_addr.get_port_number(), sizeof("hello world"), remote); + client_dgram.send("hello world", sizeof("hello world"), server_addr); - ACE_TEST_EXCEPTION_RETURN(rc != 0, " raw socket can not recv expectedRecvLen\n"); - ACE_TEST_EXCEPTION_RETURN(ACE_OS::strncmp(recvbuf + sizeof(UDP_HEADER_t), "hello world", sizeof("hello world")) != 0, " non wildcard raw socket can not recv expected content\n"); + ACE_DEBUG ((LM_INFO, "%C the send pkg will be received by raw sockets ...\n", __func__)); + ACE_OS::sleep(1); - ACE_INET_Addr to_addr; + ACE_INET_Addr remote; + int rc = raw_recv_data_until_meet_condition(rawSocket, server_addr.get_port_number(), sizeof("hello world"), remote); - ACE_DEBUG ((LM_INFO, "%C send pkg again to test common raw socket with to_adr parameter ...\n", __func__)); - client_dgram.send("hello world", sizeof("hello world"), server_addr); - ACE_OS::sleep(1); + ACE_TEST_EXCEPTION_RETURN(rc != 0, " raw socket can not recv expectedRecvLen\n"); + ACE_TEST_EXCEPTION_RETURN(ACE_OS::strncmp(recvbuf + sizeof(UDP_HEADER_t), "hello world", sizeof("hello world")) != 0, " non wildcard raw socket can not recv expected content\n"); - rc = raw_recv_data_until_meet_condition(rawSocket, server_addr.get_port_number(), sizeof("hello world"), remote, true, &to_addr); - ACE_TEST_EXCEPTION_RETURN(rc != 0, " raw socket can not recv expectedRecvLen with to_addr parameter\n"); - ACE_TEST_EXCEPTION_RETURN(ACE_OS::strncmp(recvbuf + sizeof(UDP_HEADER_t), "hello world", sizeof("hello world")) != 0, " raw socket can not recv expected content with to_addr parameter\n"); + ACE_INET_Addr to_addr; - in6_addr* remote_sin6_addr = &(static_cast(remote.get_addr())->sin6_addr); - in6_addr* to_sin6_addr = &(static_cast(to_addr.get_addr())->sin6_addr); - int cmp = ACE_OS::memcmp(remote_sin6_addr, to_sin6_addr, sizeof(*to_sin6_addr)); - ACE_TEST_EXCEPTION_RETURN(cmp != 0, " raw socket got to_addr with invalid value\n"); + ACE_DEBUG ((LM_INFO, "%C send pkg again to test common raw socket with to_adr parameter ...\n", __func__)); + client_dgram.send("hello world", sizeof("hello world"), server_addr); + ACE_OS::sleep(1); - ACE_DEBUG ((LM_INFO, "%C test iovec send ...\n", __func__)); + rc = raw_recv_data_until_meet_condition(rawSocket, server_addr.get_port_number(), sizeof("hello world"), remote, true, &to_addr); + ACE_TEST_EXCEPTION_RETURN(rc != 0, " raw socket can not recv expectedRecvLen with to_addr parameter\n"); + ACE_TEST_EXCEPTION_RETURN(ACE_OS::strncmp(recvbuf + sizeof(UDP_HEADER_t), "hello world", sizeof("hello world")) != 0, " raw socket can not recv expected content with to_addr parameter\n"); - IPv6_HEADER_t_Ptr ptIPv6Header = reinterpret_cast(sendbuf); - UDP_HEADER_t_Ptr ptUDPHeader = reinterpret_cast(sendbuf + sizeof(IPv6_HEADER_t)); - u_short n = sizeof("hello world"); + in6_addr* remote_sin6_addr = &(static_cast(remote.get_addr())->sin6_addr); + in6_addr* to_sin6_addr = &(static_cast(to_addr.get_addr())->sin6_addr); + int cmp = ACE_OS::memcmp(remote_sin6_addr, to_sin6_addr, sizeof(*to_sin6_addr)); + ACE_TEST_EXCEPTION_RETURN(cmp != 0, " raw socket got to_addr with invalid value\n"); - *ptIPv6Header = {}; - ptIPv6Header->bNextHeader = IPPROTO_UDP; - memcpy(ptIPv6Header->abSrcAddr, static_cast(client_addr.get_addr())->sin6_addr.s6_addr, 16); - memcpy(ptIPv6Header->abDstAddr, static_cast(server_addr.get_addr())->sin6_addr.s6_addr, 16); + ACE_DEBUG ((LM_INFO, "%C test iovec send ...\n", __func__)); - u_short client_port_number = client_addr.get_port_number(); - u_short server_port_number = server_addr.get_port_number(); + IPv6_HEADER_t_Ptr ptIPv6Header = reinterpret_cast(sendbuf); + UDP_HEADER_t_Ptr ptUDPHeader = reinterpret_cast(sendbuf + sizeof(IPv6_HEADER_t)); + u_short n = sizeof("hello world"); + *ptIPv6Header = {}; + ptIPv6Header->bNextHeader = IPPROTO_UDP; + memcpy(ptIPv6Header->abSrcAddr, static_cast(client_addr.get_addr())->sin6_addr.s6_addr, 16); + memcpy(ptIPv6Header->abDstAddr, static_cast(server_addr.get_addr())->sin6_addr.s6_addr, 16); - ptUDPHeader->u16SrcPort = htons(client_port_number); - ptUDPHeader->u16DstPort = htons(server_port_number); - //fill content - ACE_OS::strcpy(sendbuf + sizeof(IPv6_HEADER_t) + sizeof(UDP_HEADER_t), "hello world"); - udp6_header_checksum(ptIPv6Header, ptUDPHeader, sizeof(UDP_HEADER_t) + n); + u_short client_port_number = client_addr.get_port_number(); + u_short server_port_number = server_addr.get_port_number(); - iovec iov_udp[2]; - iov_udp[0].iov_base = reinterpret_cast(ptUDPHeader); - iov_udp[0].iov_len = sizeof(UDP_HEADER_t); - iov_udp[1].iov_base = const_cast("hello world"); - iov_udp[1].iov_len = sizeof("hello world"); + ptUDPHeader->u16SrcPort = htons(client_port_number); + ptUDPHeader->u16DstPort = htons(server_port_number); + //fill content + ACE_OS::strcpy(sendbuf + sizeof(IPv6_HEADER_t) + sizeof(UDP_HEADER_t), "hello world"); + udp6_header_checksum(ptIPv6Header, ptUDPHeader, sizeof(UDP_HEADER_t) + n); + iovec iov_udp[2]; + iov_udp[0].iov_base = reinterpret_cast(ptUDPHeader); + iov_udp[0].iov_len = sizeof(UDP_HEADER_t); + iov_udp[1].iov_base = const_cast("hello world"); + iov_udp[1].iov_len = sizeof("hello world"); - ACE_DEBUG ((LM_INFO, "%C test iovec using common udp6 socket ...\n", __func__)); + ACE_DEBUG ((LM_INFO, "%C test iovec using common udp6 socket ...\n", __func__)); - rc = client_dgram.send(iov_udp, static_cast(sizeof(iov_udp)/sizeof(iov_udp[0])), server_addr); - ACE_TEST_EXCEPTION_RETURN(rc == -1, " udp6 socket can not send using iov \n"); + rc = client_dgram.send(iov_udp, static_cast(sizeof(iov_udp)/sizeof(iov_udp[0])), server_addr); + ACE_TEST_EXCEPTION_RETURN(rc == -1, " udp6 socket can not send using iov \n"); - readUdpSocektToEmpty(server_dgram); - ACE_INET_Addr iov_server_addr(server_addr); + readUdpSocektToEmpty(server_dgram); + ACE_INET_Addr iov_server_addr(server_addr); - ACE_DEBUG ((LM_INFO, "%C must set port to zero ??? ...\n", __func__)); - iov_server_addr.set_port_number(0); - rc = rawSocket.send(iov_udp, static_cast(sizeof(iov_udp)/sizeof(iov_udp[0])), iov_server_addr); - ACE_TEST_EXCEPTION_RETURN(rc == -1, " raw6 socket can not send using iov \n"); - ACE_OS::sleep(1); + ACE_DEBUG ((LM_INFO, "%C must set port to zero ??? ...\n", __func__)); + iov_server_addr.set_port_number(0); + rc = rawSocket.send(iov_udp, static_cast(sizeof(iov_udp)/sizeof(iov_udp[0])), iov_server_addr); + ACE_TEST_EXCEPTION_RETURN(rc == -1, " raw6 socket can not send using iov \n"); + ACE_OS::sleep(1); - rc = server_dgram.recv(recvbuf, sizeof(recvbuf), remote); - ACE_TEST_EXCEPTION_RETURN(rc == -1, " server socket6 can not recv pkg by iov send\n"); - ACE_TEST_EXCEPTION_RETURN(ACE_OS::strcmp(recvbuf, "hello world") != 0, " the content of server socket6 receive pkg is not expected by iov send\n"); + rc = server_dgram.recv(recvbuf, sizeof(recvbuf), remote); + ACE_TEST_EXCEPTION_RETURN(rc == -1, " server socket6 can not recv pkg by iov send\n"); + ACE_TEST_EXCEPTION_RETURN(ACE_OS::strcmp(recvbuf, "hello world") != 0, " the content of server socket6 receive pkg is not expected by iov send\n"); - return 0; + return 0; } static int run_iovec_IPv4_api_test () { - ACE_DEBUG ((LM_INFO, "%C begin to run IPv4 iovec api test ...\n", __func__)); - - ACE_INET_Addr bindAddr(static_cast(0), "127.0.0.1"); + ACE_DEBUG ((LM_INFO, "%C begin to run IPv4 iovec api test ...\n", __func__)); - ACE_INET_Addr client_addr(static_cast(0), "127.0.0.1") ,server_addr(static_cast(0), "127.0.0.1"); + ACE_INET_Addr bindAddr(static_cast(0), "127.0.0.1"); - ACE_SOCK_Dgram client_dgram(client_addr); - SockGuard client_dgram_guard(client_dgram); - client_dgram.get_local_addr(client_addr); + ACE_INET_Addr client_addr(static_cast(0), "127.0.0.1") ,server_addr(static_cast(0), "127.0.0.1"); - ACE_SOCK_Dgram server_dgram(server_addr); - server_dgram.enable(ACE_NONBLOCK); - SockGuard server_dgram_guard(server_dgram); - server_dgram.get_local_addr(server_addr); + ACE_SOCK_Dgram client_dgram(client_addr); + SockGuard client_dgram_guard(client_dgram); + client_dgram.get_local_addr(client_addr); - ACE_DEBUG ((LM_INFO, "%C get the real bound addr and port client_port: %u, server_port: %u...\n", __func__, client_addr.get_port_number(), server_addr.get_port_number())); + ACE_SOCK_Dgram server_dgram(server_addr); + server_dgram.enable(ACE_NONBLOCK); + SockGuard server_dgram_guard(server_dgram); + server_dgram.get_local_addr(server_addr); - ACE_RAW_SOCKET rawSocket(bindAddr, IPPROTO_UDP); - rawSocket.enable(ACE_NONBLOCK); - SockGuard raw_guard(rawSocket); + ACE_DEBUG ((LM_INFO, "%C get the real bound addr and port client_port: %u, server_port: %u...\n", __func__, client_addr.get_port_number(), server_addr.get_port_number())); - client_dgram.send("hello world", sizeof("hello world"), server_addr); + ACE_RAW_SOCKET rawSocket(bindAddr, IPPROTO_UDP); + rawSocket.enable(ACE_NONBLOCK); + SockGuard raw_guard(rawSocket); - ACE_DEBUG ((LM_INFO, "%C the send pkg will be received by raw sockets ...\n", __func__)); - ACE_OS::sleep(1); + client_dgram.send("hello world", sizeof("hello world"), server_addr); - ACE_INET_Addr remote; - int rc = raw_recv_data_until_meet_condition(rawSocket, server_addr.get_port_number(), sizeof("hello world"), remote); + ACE_DEBUG ((LM_INFO, "%C the send pkg will be received by raw sockets ...\n", __func__)); + ACE_OS::sleep(1); - ACE_TEST_EXCEPTION_RETURN(rc != 0, " raw socket can not recv expectedRecvLen\n"); - ACE_TEST_EXCEPTION_RETURN(ACE_OS::strncmp(recvbuf + sizeof(IPv4_HEADER_t) + sizeof(UDP_HEADER_t), "hello world", sizeof("hello world")) != 0, " non wildcard raw socket can not recv expected content\n"); + ACE_INET_Addr remote; + int rc = raw_recv_data_until_meet_condition(rawSocket, server_addr.get_port_number(), sizeof("hello world"), remote); - ACE_INET_Addr to_addr; + ACE_TEST_EXCEPTION_RETURN(rc != 0, " raw socket can not recv expectedRecvLen\n"); + ACE_TEST_EXCEPTION_RETURN(ACE_OS::strncmp(recvbuf + sizeof(IPv4_HEADER_t) + sizeof(UDP_HEADER_t), "hello world", sizeof("hello world")) != 0, " non wildcard raw socket can not recv expected content\n"); - ACE_DEBUG ((LM_INFO, "%C send pkg again to test common raw socket with to_adr parameter ...\n", __func__)); - client_dgram.send("hello world", sizeof("hello world"), server_addr); - ACE_OS::sleep(1); + ACE_INET_Addr to_addr; - rc = raw_recv_data_until_meet_condition(rawSocket, server_addr.get_port_number(), sizeof("hello world"), remote, true, &to_addr); - ACE_TEST_EXCEPTION_RETURN(rc != 0, " raw socket can not recv expectedRecvLen with to_addr parameter\n"); - ACE_TEST_EXCEPTION_RETURN(ACE_OS::strncmp(recvbuf + sizeof(IPv4_HEADER_t) + sizeof(UDP_HEADER_t), "hello world", sizeof("hello world")) != 0, " raw socket can not recv expected content with to_addr parameter\n"); + ACE_DEBUG ((LM_INFO, "%C send pkg again to test common raw socket with to_adr parameter ...\n", __func__)); + client_dgram.send("hello world", sizeof("hello world"), server_addr); + ACE_OS::sleep(1); - in_addr* remote_sin_addr = &(static_cast(remote.get_addr())->sin_addr); - in_addr* to_sin_addr = &(static_cast(to_addr.get_addr())->sin_addr); - int cmp = ACE_OS::memcmp(remote_sin_addr, to_sin_addr, sizeof(*to_sin_addr)); - ACE_TEST_EXCEPTION_RETURN(cmp != 0, " raw socket got to_addr with invalid value\n"); + rc = raw_recv_data_until_meet_condition(rawSocket, server_addr.get_port_number(), sizeof("hello world"), remote, true, &to_addr); + ACE_TEST_EXCEPTION_RETURN(rc != 0, " raw socket can not recv expectedRecvLen with to_addr parameter\n"); + ACE_TEST_EXCEPTION_RETURN(ACE_OS::strncmp(recvbuf + sizeof(IPv4_HEADER_t) + sizeof(UDP_HEADER_t), "hello world", sizeof("hello world")) != 0, " raw socket can not recv expected content with to_addr parameter\n"); - ACE_DEBUG ((LM_INFO, "%C test iovec send ...\n", __func__)); - readUdpSocektToEmpty(server_dgram); + in_addr* remote_sin_addr = &(static_cast(remote.get_addr())->sin_addr); + in_addr* to_sin_addr = &(static_cast(to_addr.get_addr())->sin_addr); + int cmp = ACE_OS::memcmp(remote_sin_addr, to_sin_addr, sizeof(*to_sin_addr)); + ACE_TEST_EXCEPTION_RETURN(cmp != 0, " raw socket got to_addr with invalid value\n"); - IPv4_HEADER_t_Ptr ptIPv4Header = reinterpret_cast(sendbuf); - UDP_HEADER_t_Ptr ptUDPHeader = reinterpret_cast(sendbuf + sizeof(IPv4_HEADER_t)); - u_short n = sizeof("hello world"); + ACE_DEBUG ((LM_INFO, "%C test iovec send ...\n", __func__)); + readUdpSocektToEmpty(server_dgram); - *ptIPv4Header = {}; + IPv4_HEADER_t_Ptr ptIPv4Header = reinterpret_cast(sendbuf); + UDP_HEADER_t_Ptr ptUDPHeader = reinterpret_cast(sendbuf + sizeof(IPv4_HEADER_t)); + u_short n = sizeof("hello world"); - ptIPv4Header->bVersionAndHeaderLen = 0x45; - ptIPv4Header->u16TotalLenOfPacket = htons(sizeof(IPv4_HEADER_t) + sizeof(UDP_HEADER_t) + n); // 数据包长度 - ptIPv4Header->u16PacketID = ACE_OS::rand(); - ptIPv4Header->bTTL = 64; - ptIPv4Header->bTypeOfProtocol = IPPROTO_UDP; - ptIPv4Header->u16CheckSum = 0; - ptIPv4Header->u32SourIp = (static_cast(client_addr.get_addr()))->sin_addr.s_addr; - ptIPv4Header->u32DestIp = (static_cast(server_addr.get_addr()))->sin_addr.s_addr; + *ptIPv4Header = {}; - u_short client_port_number = client_addr.get_port_number(); - u_short server_port_number = server_addr.get_port_number(); + ptIPv4Header->bVersionAndHeaderLen = 0x45; + ptIPv4Header->u16TotalLenOfPacket = htons(sizeof(IPv4_HEADER_t) + sizeof(UDP_HEADER_t) + n); // 数据包长度 + ptIPv4Header->u16PacketID = ACE_OS::rand(); + ptIPv4Header->bTTL = 64; + ptIPv4Header->bTypeOfProtocol = IPPROTO_UDP; + ptIPv4Header->u16CheckSum = 0; + ptIPv4Header->u32SourIp = (static_cast(client_addr.get_addr()))->sin_addr.s_addr; + ptIPv4Header->u32DestIp = (static_cast(server_addr.get_addr()))->sin_addr.s_addr; + u_short client_port_number = client_addr.get_port_number(); + u_short server_port_number = server_addr.get_port_number(); - ptUDPHeader->u16SrcPort = htons(client_port_number); - ptUDPHeader->u16DstPort = htons(server_port_number); - ptUDPHeader->u16Length = htons(sizeof(UDP_HEADER_t) + n); - ptUDPHeader->u16CheckSum = 0; - ACE_OS::strcpy(sendbuf + sizeof(IPv4_HEADER_t) + sizeof(UDP_HEADER_t), "hello world"); + ptUDPHeader->u16SrcPort = htons(client_port_number); + ptUDPHeader->u16DstPort = htons(server_port_number); + ptUDPHeader->u16Length = htons(sizeof(UDP_HEADER_t) + n); + ptUDPHeader->u16CheckSum = 0; + ACE_OS::strcpy(sendbuf + sizeof(IPv4_HEADER_t) + sizeof(UDP_HEADER_t), "hello world"); - iovec iov_udp[2]; - iov_udp[0].iov_base = reinterpret_cast(ptUDPHeader); - iov_udp[0].iov_len = sizeof(UDP_HEADER_t); - iov_udp[1].iov_base = const_cast("hello world"); - iov_udp[1].iov_len = sizeof("hello world"); + iovec iov_udp[2]; + iov_udp[0].iov_base = reinterpret_cast(ptUDPHeader); + iov_udp[0].iov_len = sizeof(UDP_HEADER_t); + iov_udp[1].iov_base = const_cast("hello world"); + iov_udp[1].iov_len = sizeof("hello world"); - rc = rawSocket.send(iov_udp, static_cast(sizeof(iov_udp)/sizeof(iov_udp[0])), server_addr); - ACE_TEST_EXCEPTION_RETURN(rc == -1, " raw4 socket can send using iov\n"); - ACE_OS::sleep(1); + rc = rawSocket.send(iov_udp, static_cast(sizeof(iov_udp)/sizeof(iov_udp[0])), server_addr); + ACE_TEST_EXCEPTION_RETURN(rc == -1, " raw4 socket can send using iov\n"); + ACE_OS::sleep(1); - rc = server_dgram.recv(recvbuf, sizeof(recvbuf), remote); - ACE_TEST_EXCEPTION_RETURN(rc == -1, " server socket4 can not recv pkg by iov send\n"); - ACE_TEST_EXCEPTION_RETURN(ACE_OS::strcmp(recvbuf, "hello world") != 0, " the content of server socket4 receive pkg is not expected by iov send\n"); + rc = server_dgram.recv(recvbuf, sizeof(recvbuf), remote); + ACE_TEST_EXCEPTION_RETURN(rc == -1, " server socket4 can not recv pkg by iov send\n"); + ACE_TEST_EXCEPTION_RETURN(ACE_OS::strcmp(recvbuf, "hello world") != 0, " the content of server socket4 receive pkg is not expected by iov send\n"); - return 0; + return 0; } int @@ -775,11 +761,11 @@ run_main (int, ACE_TCHAR *argv[]) retval += run_iovec_IPv4_api_test(); retval += run_iovec_IPv6_api_test(); - #if defined (ACE_HAS_IPV6) +#if defined (ACE_HAS_IPV6) retval += run_ipv6_pkginfo_test(); - #else +#else ACE_DEBUG ((LM_INFO, "%C without IPv6 macro ...\n", __func__)); - #endif +#endif ACE_END_TEST; From af7500234366a19eb3a7073daa01739acf7fe866 Mon Sep 17 00:00:00 2001 From: SmithAchang Date: Thu, 7 Sep 2023 22:25:23 +0800 Subject: [PATCH 37/39] empty lines & inden 2 spaces --- ACE/ace/RAW_Socket.cpp | 415 ++++++++++++++++++++--------------------- ACE/ace/RAW_Socket.h | 47 +++-- ACE/ace/RAW_Socket.inl | 2 - 3 files changed, 225 insertions(+), 239 deletions(-) diff --git a/ACE/ace/RAW_Socket.cpp b/ACE/ace/RAW_Socket.cpp index 3b9584a5c3cdb..d2e7384531c87 100644 --- a/ACE/ace/RAW_Socket.cpp +++ b/ACE/ace/RAW_Socket.cpp @@ -14,7 +14,7 @@ #endif #if !defined (__ACE_INLINE__) -# include "ace/RAW_Socket.inl" +#include "ace/RAW_Socket.inl" #endif /* __ACE_INLINE__ */ ACE_BEGIN_VERSIONED_NAMESPACE_DECL @@ -23,142 +23,143 @@ ACE_ALLOC_HOOK_DEFINE (ACE_RAW_SOCKET) #if defined (ACE_HAS_4_4BSD_SENDMSG_RECVMSG) || defined ACE_WIN32 #define ACE_USE_MSG_CONTROL - union control_buffer { - cmsghdr control_msg_header; +union control_buffer +{ + cmsghdr control_msg_header; #if defined (IP_RECVDSTADDR) - u_char padding[ACE_CMSG_SPACE (sizeof (in_addr))]; + u_char padding[ACE_CMSG_SPACE (sizeof (in_addr))]; #elif defined (IP_PKTINFO) - u_char padding[ACE_CMSG_SPACE (sizeof (in_pktinfo))]; + u_char padding[ACE_CMSG_SPACE (sizeof (in_pktinfo))]; #endif #if defined (ACE_HAS_IPV6) - u_char padding6[ACE_CMSG_SPACE (sizeof (in6_pktinfo))]; + u_char padding6[ACE_CMSG_SPACE (sizeof (in6_pktinfo))]; #endif - }; +}; #endif #define ACE_SEND_EXCEPTION_RETURN() do {\ if (this->get_handle () == ACE_INVALID_HANDLE)\ - return -1; \ + return -1;\ if(timeout && ACE::handle_write_ready (this->get_handle (), timeout) != 1)\ - return -1; \ -}while(0) + return -1;\ +} while(0) #define ACE_RECV_EXCEPTION_RETURN() do {\ if (this->get_handle () == ACE_INVALID_HANDLE)\ - return -1; \ + return -1;\ if(this->is_send_only())\ - return -1; \ + return -1;\ if(timeout && ACE::handle_read_ready (this->get_handle (), timeout) != 1)\ - return -1; \ -}while(0) + return -1;\ +} while(0) void ACE_RAW_SOCKET::dump () const { - ACE_TRACE ("ACE_RAW_SOCKET::dump"); + ACE_TRACE ("ACE_RAW_SOCKET::dump"); } /// @brief default constructor ACE_RAW_SOCKET::ACE_RAW_SOCKET () : protocol_(IPPROTO_UDP) { - ACE_TRACE ("ACE_RAW_SOCKET::ACE_RAW_SOCKET"); + ACE_TRACE ("ACE_RAW_SOCKET::ACE_RAW_SOCKET"); } /// @brief constructor with parameters ACE_RAW_SOCKET::ACE_RAW_SOCKET (ACE_INET_Addr const & local, int protocol) : protocol_(protocol) { - ACE_TRACE ("ACE_RAW_SOCKET::ACE_RAW_SOCKET"); + ACE_TRACE ("ACE_RAW_SOCKET::ACE_RAW_SOCKET"); - if (this->open (local, protocol) == -1) - ACELIB_ERROR ((LM_ERROR, - ACE_TEXT ("%p\n"), - ACE_TEXT ("ACE_RAW_SOCKET"))); + if (this->open (local, protocol) == -1) + ACELIB_ERROR ((LM_ERROR, + ACE_TEXT ("%p\n"), + ACE_TEXT ("ACE_RAW_SOCKET"))); } static inline ssize_t using_common_recv(const ACE_RAW_SOCKET& raw, void *buf, size_t n, ACE_INET_Addr &addr, int flags) { - struct sockaddr *saddr = static_cast(addr.get_addr ()); - int addr_len = addr.get_size (); - - ssize_t const status = ACE_OS::recvfrom (raw.get_handle (), - static_cast(buf), - n, - flags, - saddr, - &addr_len); - - addr.set_size (addr_len); - addr.set_type (saddr->sa_family); - return status; + struct sockaddr *saddr = static_cast(addr.get_addr ()); + int addr_len = addr.get_size (); + + ssize_t const status = ACE_OS::recvfrom (raw.get_handle (), + static_cast(buf), + n, + flags, + saddr, + &addr_len); + + addr.set_size (addr_len); + addr.set_type (saddr->sa_family); + return status; } static inline void fillMsgHdr(msghdr& recv_msg, const ACE_INET_Addr &addr, void* pcbuf, int cbuf_size) { #if defined (ACE_HAS_SOCKADDR_MSG_NAME) - recv_msg.msg_name = static_cast (addr.get_addr ()); + recv_msg.msg_name = static_cast (addr.get_addr ()); #else - recv_msg.msg_name = static_cast(addr.get_addr ()); + recv_msg.msg_name = static_cast(addr.get_addr ()); #endif /* ACE_HAS_SOCKADDR_MSG_NAME */ recv_msg.msg_namelen = addr.get_size (); #ifdef ACE_USE_MSG_CONTROL - recv_msg.msg_control = pcbuf; - recv_msg.msg_controllen = cbuf_size; + recv_msg.msg_control = pcbuf; + recv_msg.msg_controllen = cbuf_size; #elif !defined ACE_LACKS_SENDMSG - recv_msg.msg_accrights = 0; - recv_msg.msg_accrightslen = 0; + recv_msg.msg_accrights = 0; + recv_msg.msg_accrightslen = 0; #endif } static inline void getToAddrFromMsgHdr(msghdr& recv_msg, ACE_INET_Addr& to_addr) { #if defined (ACE_HAS_4_4BSD_SENDMSG_RECVMSG) || defined ACE_WIN32 - if (to_addr.get_type() == AF_INET) - { + if (to_addr.get_type() == AF_INET) + { #if defined (IP_RECVDSTADDR) || defined (IP_PKTINFO) - for (cmsghdr *ptr = ACE_CMSG_FIRSTHDR (&recv_msg); ptr; ptr = ACE_CMSG_NXTHDR (&recv_msg, ptr)) - { + for (cmsghdr *ptr = ACE_CMSG_FIRSTHDR (&recv_msg); ptr; ptr = ACE_CMSG_NXTHDR (&recv_msg, ptr)) + { #if defined (IP_RECVDSTADDR) - if (ptr->cmsg_level == IPPROTO_IP && ptr->cmsg_type == IP_RECVDSTADDR) - { - to_addr.set_address (reinterpret_cast(ACE_CMSG_DATA (ptr)), - sizeof (struct in_addr), - 0); - break; - } + if (ptr->cmsg_level == IPPROTO_IP && ptr->cmsg_type == IP_RECVDSTADDR) + { + to_addr.set_address (reinterpret_cast(ACE_CMSG_DATA (ptr)), + sizeof (struct in_addr), + 0); + break; + } #endif #if defined(IP_PKTINFO) - if (ptr->cmsg_level == IPPROTO_IP && ptr->cmsg_type == IP_PKTINFO) - { - to_addr.set_address (reinterpret_cast(&((reinterpret_cast(ACE_CMSG_DATA (ptr)))->ipi_addr)), - sizeof (struct in_addr), - 0); - break; - } -#endif - } + if (ptr->cmsg_level == IPPROTO_IP && ptr->cmsg_type == IP_PKTINFO) + { + to_addr.set_address (reinterpret_cast(&((reinterpret_cast(ACE_CMSG_DATA (ptr)))->ipi_addr)), + sizeof (struct in_addr), + 0); + break; + } #endif } +#endif + } #if defined (ACE_HAS_IPV6) && defined (IPV6_PKTINFO) - else if (to_addr.get_type() == AF_INET6) - { - for (cmsghdr *ptr = ACE_CMSG_FIRSTHDR (&recv_msg); ptr; ptr = ACE_CMSG_NXTHDR (&recv_msg, ptr)) - { - if (ptr->cmsg_level == IPPROTO_IPV6 && ptr->cmsg_type == IPV6_PKTINFO) - { - to_addr.set_address (reinterpret_cast(&((reinterpret_cast((ACE_CMSG_DATA(ptr))))->ipi6_addr)), - sizeof (struct in6_addr), - 0); - break; - } - } - } + else if (to_addr.get_type() == AF_INET6) + { + for (cmsghdr *ptr = ACE_CMSG_FIRSTHDR (&recv_msg); ptr; ptr = ACE_CMSG_NXTHDR (&recv_msg, ptr)) + { + if (ptr->cmsg_level == IPPROTO_IPV6 && ptr->cmsg_type == IPV6_PKTINFO) + { + to_addr.set_address (reinterpret_cast(&((reinterpret_cast((ACE_CMSG_DATA(ptr))))->ipi6_addr)), + sizeof (struct in6_addr), + 0); + break; + } + } + } #endif #else - ACE_UNUSED_ARG(recv_msg); - ACE_UNUSED_ARG (to_addr); + ACE_UNUSED_ARG(recv_msg); + ACE_UNUSED_ARG (to_addr); #endif } @@ -170,61 +171,60 @@ ACE_RAW_SOCKET::recv (void *buf, const ACE_Time_Value *timeout, ACE_INET_Addr *to_addr) const { - ACE_TRACE ("ACE_RAW_SOCKET::recv"); - - ACE_RECV_EXCEPTION_RETURN(); - - if(to_addr == nullptr) + ACE_TRACE ("ACE_RAW_SOCKET::recv"); + + ACE_RECV_EXCEPTION_RETURN(); + + if(to_addr == nullptr) + { + return using_common_recv(*this, buf, n, addr, flags); + } + else + { + this->get_local_addr (*to_addr); + if(!to_addr->is_any()) { - return using_common_recv(*this, buf, n, addr, flags); - } - else - { - this->get_local_addr (*to_addr); - if(!to_addr->is_any()) - { - return using_common_recv(*this, buf, n, addr, flags); - } + return using_common_recv(*this, buf, n, addr, flags); } + } - struct iovec iov; - iov.iov_base = static_cast(buf); - iov.iov_len = static_cast(n); + struct iovec iov; + iov.iov_base = static_cast(buf); + iov.iov_len = static_cast(n); - msghdr recv_msg = {}; - recv_msg.msg_iov = &iov; - recv_msg.msg_iovlen = 1; + msghdr recv_msg = {}; + recv_msg.msg_iov = &iov; + recv_msg.msg_iovlen = 1; #ifdef ACE_USE_MSG_CONTROL - union control_buffer cbuf; - if(to_addr != nullptr) - { - fillMsgHdr(recv_msg, addr, &cbuf, sizeof(cbuf)); - } - else - { - fillMsgHdr(recv_msg, addr, nullptr, 0); - } -#else + union control_buffer cbuf; + if(to_addr != nullptr) + { + fillMsgHdr(recv_msg, addr, &cbuf, sizeof(cbuf)); + } + else + { fillMsgHdr(recv_msg, addr, nullptr, 0); + } +#else + fillMsgHdr(recv_msg, addr, nullptr, 0); #endif - ssize_t const status = ACE_OS::recvmsg (this->get_handle (), - &recv_msg, - flags); + ssize_t const status = ACE_OS::recvmsg (this->get_handle (), + &recv_msg, + flags); - addr.set_size (recv_msg.msg_namelen); - addr.set_type ((static_cast(addr.get_addr()))->sin_family); + addr.set_size (recv_msg.msg_namelen); + addr.set_type ((static_cast(addr.get_addr()))->sin_family); #ifdef ACE_USE_MSG_CONTROL - if(to_addr != nullptr) - getToAddrFromMsgHdr(recv_msg, *to_addr); + if(to_addr != nullptr) + getToAddrFromMsgHdr(recv_msg, *to_addr); #endif - return status; + return status; } - ssize_t ACE_RAW_SOCKET::send (const void *buf, size_t n, @@ -232,64 +232,62 @@ ACE_RAW_SOCKET::send (const void *buf, int flags, const ACE_Time_Value *timeout) const { - ACE_TRACE ("ACE_RAW_SOCKET::send"); - - // Check the status of the current socket. - ACE_SEND_EXCEPTION_RETURN(); - - struct sockaddr *saddr = static_cast(addr.get_addr ()); - int const len = addr.get_size (); - return ACE_OS::sendto (this->get_handle (), - static_cast(buf), - n, - flags, - saddr, - len); + ACE_TRACE ("ACE_RAW_SOCKET::send"); + + // Check the status of the current socket. + ACE_SEND_EXCEPTION_RETURN(); + + struct sockaddr *saddr = static_cast(addr.get_addr ()); + int const len = addr.get_size (); + return ACE_OS::sendto (this->get_handle (), + static_cast(buf), + n, + flags, + saddr, + len); } #if defined (ACE_HAS_MSG) ssize_t ACE_RAW_SOCKET::send (const iovec iov[], - int n, - const ACE_INET_Addr &addr, - int flags, - const ACE_Time_Value *timeout) const + int n, + const ACE_INET_Addr &addr, + int flags, + const ACE_Time_Value *timeout) const { - ACE_TRACE ("ACE_RAW_SOCKET::send"); + ACE_TRACE ("ACE_RAW_SOCKET::send"); - // Check the status of the current socket. - ACE_SEND_EXCEPTION_RETURN(); + // Check the status of the current socket. + ACE_SEND_EXCEPTION_RETURN(); - msghdr send_msg = {}; - - send_msg.msg_iov = const_cast(iov); - send_msg.msg_iovlen = n; + msghdr send_msg = {}; + send_msg.msg_iov = const_cast(iov); + send_msg.msg_iovlen = n; #if defined (ACE_HAS_SOCKADDR_MSG_NAME) - send_msg.msg_name = static_cast(addr.get_addr()); + send_msg.msg_name = static_cast(addr.get_addr()); #else - send_msg.msg_name = static_cast(addr.get_addr ()); + send_msg.msg_name = static_cast(addr.get_addr ()); #endif /* ACE_HAS_SOCKADDR_MSG_NAME */ - send_msg.msg_namelen = addr.get_size (); + send_msg.msg_namelen = addr.get_size (); #if defined (ACE_HAS_4_4BSD_SENDMSG_RECVMSG) - send_msg.msg_control = 0; - send_msg.msg_controllen = 0; - send_msg.msg_flags = 0; + send_msg.msg_control = 0; + send_msg.msg_controllen = 0; + send_msg.msg_flags = 0; #elif !defined ACE_LACKS_SENDMSG - send_msg.msg_accrights = 0; - send_msg.msg_accrightslen = 0; + send_msg.msg_accrights = 0; + send_msg.msg_accrightslen = 0; #endif /* ACE_HAS_4_4BSD_SENDMSG_RECVMSG */ #ifdef ACE_WIN32 - send_msg.msg_control = 0; - send_msg.msg_controllen = 0; + send_msg.msg_control = 0; + send_msg.msg_controllen = 0; #endif - return ACE_OS::sendmsg (this->get_handle (), &send_msg, flags); + return ACE_OS::sendmsg (this->get_handle (), &send_msg, flags); } - ssize_t ACE_RAW_SOCKET::recv (iovec iov[], int n, @@ -298,57 +296,53 @@ ACE_RAW_SOCKET::recv (iovec iov[], const ACE_Time_Value *timeout, ACE_INET_Addr *to_addr) const { - ACE_TRACE ("ACE_RAW_SOCKET::recv"); + ACE_TRACE ("ACE_RAW_SOCKET::recv"); - ACE_RECV_EXCEPTION_RETURN(); + ACE_RECV_EXCEPTION_RETURN(); + msghdr recv_msg = {}; + recv_msg.msg_iov = static_cast(iov); + recv_msg.msg_iovlen = n; - msghdr recv_msg = {}; - recv_msg.msg_iov = static_cast(iov); - recv_msg.msg_iovlen = n; - - /*default*/ - fillMsgHdr(recv_msg, addr, nullptr, 0); + /*default*/ + fillMsgHdr(recv_msg, addr, nullptr, 0); #ifdef ACE_USE_MSG_CONTROL - union control_buffer cbuf; - if(to_addr != nullptr) - { - this->get_local_addr(*to_addr); - - fillMsgHdr(recv_msg, addr, &cbuf, sizeof(cbuf)); - } + union control_buffer cbuf; + if(to_addr != nullptr) + { + this->get_local_addr(*to_addr); + fillMsgHdr(recv_msg, addr, &cbuf, sizeof(cbuf)); + } #endif - ssize_t const status = ACE_OS::recvmsg (this->get_handle (), - &recv_msg, - flags); + ssize_t const status = ACE_OS::recvmsg (this->get_handle (), + &recv_msg, + flags); - addr.set_size (recv_msg.msg_namelen); - addr.set_type ((static_cast(addr.get_addr()))->sin_family); + addr.set_size (recv_msg.msg_namelen); + addr.set_type ((static_cast(addr.get_addr()))->sin_family); #ifdef ACE_USE_MSG_CONTROL - if(to_addr != nullptr) - getToAddrFromMsgHdr(recv_msg, *to_addr); + if(to_addr != nullptr) + getToAddrFromMsgHdr(recv_msg, *to_addr); #endif - return status; + return status; } - #else // not supported will fail immediately ssize_t ACE_RAW_SOCKET::send (const iovec iov[], - int n, - const ACE_INET_Addr &addr, - int flags, - const ACE_Time_Value *timeout) const + int n, + const ACE_INET_Addr &addr, + int flags, + const ACE_Time_Value *timeout) const { - ACE_TRACE ("ACE_RAW_SOCKET::send iovec"); - + ACE_TRACE ("ACE_RAW_SOCKET::send iovec"); - // immediately fail when unsupported - return -1; + // Immediately fail when unsupported. + return -1; } ssize_t @@ -359,56 +353,53 @@ ACE_RAW_SOCKET::recv (iovec iov[], const ACE_Time_Value *timeout, ACE_INET_Addr *to_addr) const { - ACE_TRACE ("ACE_RAW_SOCKET::recv iovec"); - + ACE_TRACE ("ACE_RAW_SOCKET::recv iovec"); - // immediately fail when unsupported - return -1; + // Immediately fail when unsupported. + return -1; } - #endif int ACE_RAW_SOCKET::open (ACE_INET_Addr const & local, int protocol) { - ACE_TRACE ("ACE_RAW_SOCKET::open"); - - if (this->get_handle () != ACE_INVALID_HANDLE) - return -1; + ACE_TRACE ("ACE_RAW_SOCKET::open"); - int const protocol_family = local.get_type (); - /// reuse_addr Maybe meaningless for RAW Socket - int const reuse_addr = 1; + if (this->get_handle () != ACE_INVALID_HANDLE) + return -1; - if(ACE_SOCK::open (SOCK_RAW, protocol_family, protocol, reuse_addr) == -1) - return -1; + int const protocol_family = local.get_type (); + /// Reuse_addr Maybe meaningless for RAW Socket. + int const reuse_addr = 1; - if(ACE_OS::bind (this->get_handle (), static_cast(local.get_addr()), local.get_addr_size()) == -1) - return -1; + if(ACE_SOCK::open (SOCK_RAW, protocol_family, protocol, reuse_addr) == -1) + return -1; + if(ACE_OS::bind (this->get_handle (), static_cast(local.get_addr()), local.get_addr_size()) == -1) + return -1; - this->protocol_ = protocol; + this->protocol_ = protocol; - ACE_INET_Addr bindAddr; - this->get_local_addr(bindAddr); + ACE_INET_Addr bindAddr; + this->get_local_addr(bindAddr); #if defined (ACE_HAS_IPV6) && defined (IPV6_PKTINFO) && defined(IPV6_RECVPKTINFO) - if (bindAddr.get_type() == PF_INET6 && bindAddr.is_any()) - { - int yes = 1; - this->set_option(IPPROTO_IPV6, ACE_RECVPKTINFO6, &yes, sizeof(yes)); - } + if (bindAddr.get_type() == PF_INET6 && bindAddr.is_any()) + { + int yes = 1; + this->set_option(IPPROTO_IPV6, ACE_RECVPKTINFO6, &yes, sizeof(yes)); + } #endif #if defined (IP_RECVDSTADDR) || defined (IP_PKTINFO) - if (bindAddr.get_type() == PF_INET && bindAddr.is_any()) - { - int yes = 1; - this->set_option(IPPROTO_IP, ACE_RECVPKTINFO, &yes, sizeof(yes)); - } + if (bindAddr.get_type() == PF_INET && bindAddr.is_any()) + { + int yes = 1; + this->set_option(IPPROTO_IP, ACE_RECVPKTINFO, &yes, sizeof(yes)); + } #endif - return 0; + return 0; } ACE_END_VERSIONED_NAMESPACE_DECL diff --git a/ACE/ace/RAW_Socket.h b/ACE/ace/RAW_Socket.h index 6575f63b59049..c7a5faf0dc0eb 100644 --- a/ACE/ace/RAW_Socket.h +++ b/ACE/ace/RAW_Socket.h @@ -19,7 +19,6 @@ # pragma once #endif /* ACE_LACKS_PRAGMA_ONCE */ - #include "ace/SOCK.h" #include "ace/Copy_Disabled.h" @@ -59,7 +58,6 @@ class ACE_Export ACE_RAW_SOCKET : public ACE_SOCK, private ACE_Copy_Disabled /// @param protocol IPPROTO_UDP as default value because often used as a user-space UDP stack ACE_RAW_SOCKET (ACE_INET_Addr const & local, int protocol = IPPROTO_UDP); - /** * @name Data transfer routines. * @@ -76,7 +74,7 @@ class ACE_Export ACE_RAW_SOCKET : public ACE_SOCK, private ACE_Copy_Disabled * returned with @c errno == ETIME. If it succeeds the number of * bytes sent is returned. */ -ssize_t send (const void *buf, + ssize_t send (const void *buf, size_t n, const ACE_INET_Addr &addr, int flags = 0, @@ -100,27 +98,27 @@ ssize_t send (const void *buf, const ACE_Time_Value *timeout = nullptr, ACE_INET_Addr *to_addr = nullptr) const; -/** - * Send an of size @a n to the datagram socket (uses) - * The IP destination address will be placed in @a *to_addr if it is not null - * @return if iovec call is unsupported by platforms, the method will return -1 immediately -*/ -ssize_t send (const iovec iov[], - int n, - const ACE_INET_Addr &addr, - int flags = 0, - const ACE_Time_Value *timeout = nullptr) const; -/** - * Recv an of size @a n to the datagram socket (uses ). - * The IP destination address will be placed in @a *to_addr if it is not null. - * @return if iovec call is unsupported by platforms, the method will return -1 immediately - */ -ssize_t recv (iovec iov[], - int n, - ACE_INET_Addr &addr, - int flags = 0, - const ACE_Time_Value *timeout = nullptr, - ACE_INET_Addr *to_addr = nullptr) const; + /** + * Send an of size @a n to the datagram socket (uses) + * The IP destination address will be placed in @a *to_addr if it is not null + * @return if iovec call is unsupported by platforms, the method will return -1 immediately + */ + ssize_t send (const iovec iov[], + int n, + const ACE_INET_Addr &addr, + int flags = 0, + const ACE_Time_Value *timeout = nullptr) const; + /** + * Recv an of size @a n to the datagram socket (uses ). + * The IP destination address will be placed in @a *to_addr if it is not null. + * @return if iovec call is unsupported by platforms, the method will return -1 immediately + */ + ssize_t recv (iovec iov[], + int n, + ACE_INET_Addr &addr, + int flags = 0, + const ACE_Time_Value *timeout = nullptr, + ACE_INET_Addr *to_addr = nullptr) const; //@} @@ -131,7 +129,6 @@ ssize_t recv (iovec iov[], * * @attention can be re-opened after closed */ - int open (ACE_INET_Addr const & local, int protocol = IPPROTO_UDP); /// Dump the state of object. diff --git a/ACE/ace/RAW_Socket.inl b/ACE/ace/RAW_Socket.inl index 13963aed96a2b..e11330087db06 100644 --- a/ACE/ace/RAW_Socket.inl +++ b/ACE/ace/RAW_Socket.inl @@ -4,7 +4,6 @@ ACE_BEGIN_VERSIONED_NAMESPACE_DECL - ACE_INLINE bool ACE_RAW_SOCKET::is_send_only () const { @@ -17,6 +16,5 @@ ACE_RAW_SOCKET:: protocol () const return this->protocol_; } - ACE_END_VERSIONED_NAMESPACE_DECL From 2acb784cf99c46d79d402088296d33b138b2b0a6 Mon Sep 17 00:00:00 2001 From: SmithAchang Date: Thu, 7 Sep 2023 22:38:22 +0800 Subject: [PATCH 38/39] other fixes --- ACE/ace/RAW_Socket.h | 2 +- ACE/tests/RAW_Socket_Test.cpp | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/ACE/ace/RAW_Socket.h b/ACE/ace/RAW_Socket.h index c7a5faf0dc0eb..3c39ebb08480a 100644 --- a/ACE/ace/RAW_Socket.h +++ b/ACE/ace/RAW_Socket.h @@ -80,7 +80,6 @@ class ACE_Export ACE_RAW_SOCKET : public ACE_SOCK, private ACE_Copy_Disabled int flags = 0, const ACE_Time_Value *timeout = nullptr) const; - /** * Wait up to @a timeout amount of time to receive a datagram into * @a buf. The ACE_Time_Value indicates how long to blocking @@ -108,6 +107,7 @@ class ACE_Export ACE_RAW_SOCKET : public ACE_SOCK, private ACE_Copy_Disabled const ACE_INET_Addr &addr, int flags = 0, const ACE_Time_Value *timeout = nullptr) const; + /** * Recv an of size @a n to the datagram socket (uses ). * The IP destination address will be placed in @a *to_addr if it is not null. diff --git a/ACE/tests/RAW_Socket_Test.cpp b/ACE/tests/RAW_Socket_Test.cpp index 5aefbd869df1b..2ef1b99ea27f1 100644 --- a/ACE/tests/RAW_Socket_Test.cpp +++ b/ACE/tests/RAW_Socket_Test.cpp @@ -153,12 +153,12 @@ class SockGuard : private ACE_Copy_Disabled #define ACE_TEST_EXCEPTION_RETURN(expression, message)\ -do{\ +do {\ if(expression)\ {\ ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT (message)), 1);\ }\ -}while(0) +} while(0) static char sendbuf[4096]; static char recvbuf[4096]; @@ -169,8 +169,6 @@ static char recvbuf[4096]; * It returns 0 for success and 1 for error so we can keep track of the * total error count. */ - - static int run_option_test () { @@ -234,7 +232,8 @@ run_reopen_test () return 0; } -static void readUdpSocektToEmpty(ACE_SOCK_Dgram& udpSock) +static void +readUdpSocektToEmpty(ACE_SOCK_Dgram& udpSock) { ACE_INET_Addr remote; while(1) @@ -245,7 +244,8 @@ static void readUdpSocektToEmpty(ACE_SOCK_Dgram& udpSock) } } -static int raw_recv_data_until_meet_condition(ACE_RAW_SOCKET& raw, u_short port, size_t n, ACE_INET_Addr& remote, bool bUseIOVec = false, ACE_INET_Addr* to_addr = nullptr) +static int +raw_recv_data_until_meet_condition(ACE_RAW_SOCKET& raw, u_short port, size_t n, ACE_INET_Addr& remote, bool bUseIOVec = false, ACE_INET_Addr* to_addr = nullptr) { ACE_INET_Addr local; raw.get_local_addr(local); From 0418cf13f023a5a1c5aac1cce527c9ae22a93805 Mon Sep 17 00:00:00 2001 From: smitAchang Date: Sat, 9 Sep 2023 10:25:30 +0800 Subject: [PATCH 39/39] =?UTF-8?q?1=E3=80=81fix=20Test=20File=20trailing=20?= =?UTF-8?q?whitespace=202=E3=80=81keep=20up=20with=20Sock=5FDgram.cpp=20re?= =?UTF-8?q?cv=20api?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ACE/ace/RAW_Socket.cpp | 5 ++--- ACE/tests/RAW_Socket_Test.cpp | 4 ++-- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/ACE/ace/RAW_Socket.cpp b/ACE/ace/RAW_Socket.cpp index d2e7384531c87..0a0822fb3c97e 100644 --- a/ACE/ace/RAW_Socket.cpp +++ b/ACE/ace/RAW_Socket.cpp @@ -115,7 +115,7 @@ static inline void fillMsgHdr(msghdr& recv_msg, const ACE_INET_Addr &addr, void* static inline void getToAddrFromMsgHdr(msghdr& recv_msg, ACE_INET_Addr& to_addr) { -#if defined (ACE_HAS_4_4BSD_SENDMSG_RECVMSG) || defined ACE_WIN32 +#ifdef ACE_USE_MSG_CONTROL if (to_addr.get_type() == AF_INET) { #if defined (IP_RECVDSTADDR) || defined (IP_PKTINFO) @@ -129,8 +129,7 @@ static inline void getToAddrFromMsgHdr(msghdr& recv_msg, ACE_INET_Addr& to_addr) 0); break; } -#endif -#if defined(IP_PKTINFO) +#else if (ptr->cmsg_level == IPPROTO_IP && ptr->cmsg_type == IP_PKTINFO) { to_addr.set_address (reinterpret_cast(&((reinterpret_cast(ACE_CMSG_DATA (ptr)))->ipi_addr)), diff --git a/ACE/tests/RAW_Socket_Test.cpp b/ACE/tests/RAW_Socket_Test.cpp index 2ef1b99ea27f1..82e7554e12a04 100644 --- a/ACE/tests/RAW_Socket_Test.cpp +++ b/ACE/tests/RAW_Socket_Test.cpp @@ -232,7 +232,7 @@ run_reopen_test () return 0; } -static void +static void readUdpSocektToEmpty(ACE_SOCK_Dgram& udpSock) { ACE_INET_Addr remote; @@ -244,7 +244,7 @@ readUdpSocektToEmpty(ACE_SOCK_Dgram& udpSock) } } -static int +static int raw_recv_data_until_meet_condition(ACE_RAW_SOCKET& raw, u_short port, size_t n, ACE_INET_Addr& remote, bool bUseIOVec = false, ACE_INET_Addr* to_addr = nullptr) { ACE_INET_Addr local;