diff --git a/src/posix/platform/daemon.cpp b/src/posix/platform/daemon.cpp index dad11a743..5a2a9c438 100644 --- a/src/posix/platform/daemon.cpp +++ b/src/posix/platform/daemon.cpp @@ -47,6 +47,7 @@ #include "cli/cli_config.h" #include "common/code_utils.hpp" #include "posix/platform/platform-posix.h" +#include "posix/platform/utils.hpp" #if OPENTHREAD_POSIX_CONFIG_DAEMON_ENABLE diff --git a/src/posix/platform/infra_if.cpp b/src/posix/platform/infra_if.cpp index e9aed0811..80438b19c 100644 --- a/src/posix/platform/infra_if.cpp +++ b/src/posix/platform/infra_if.cpp @@ -57,10 +57,11 @@ #include #include +#include "infra_if.hpp" +#include "utils.hpp" #include "common/code_utils.hpp" #include "common/debug.hpp" #include "lib/platform/exit_code.h" -#include "posix/platform/infra_if.hpp" bool otPlatInfraIfHasAddress(uint32_t aInfraIfIndex, const otIp6Address *aAddress) { diff --git a/src/posix/platform/misc.cpp b/src/posix/platform/misc.cpp index 7e2a82e4d..2550c122c 100644 --- a/src/posix/platform/misc.cpp +++ b/src/posix/platform/misc.cpp @@ -95,29 +95,3 @@ otPlatMcuPowerState otPlatGetMcuPowerState(otInstance *aInstance) return gPlatMcuPowerState; } - -int SocketWithCloseExec(int aDomain, int aType, int aProtocol, SocketBlockOption aBlockOption) -{ - int rval = 0; - int fd = -1; - -#ifdef __APPLE__ - VerifyOrExit((fd = socket(aDomain, aType, aProtocol)) != -1, perror("socket(SOCK_CLOEXEC)")); - - VerifyOrExit((rval = fcntl(fd, F_GETFD, 0)) != -1, perror("fcntl(F_GETFD)")); - rval |= aBlockOption == kSocketNonBlock ? O_NONBLOCK | FD_CLOEXEC : FD_CLOEXEC; - VerifyOrExit((rval = fcntl(fd, F_SETFD, rval)) != -1, perror("fcntl(F_SETFD)")); -#else - aType |= aBlockOption == kSocketNonBlock ? SOCK_CLOEXEC | SOCK_NONBLOCK : SOCK_CLOEXEC; - VerifyOrExit((fd = socket(aDomain, aType, aProtocol)) != -1, perror("socket(SOCK_CLOEXEC)")); -#endif - -exit: - if (rval == -1) - { - VerifyOrDie(close(fd) == 0, OT_EXIT_ERROR_ERRNO); - fd = -1; - } - - return fd; -} diff --git a/src/posix/platform/multicast_routing.cpp b/src/posix/platform/multicast_routing.cpp index 35d7bffb1..07a00664e 100644 --- a/src/posix/platform/multicast_routing.cpp +++ b/src/posix/platform/multicast_routing.cpp @@ -48,6 +48,7 @@ #include #include +#include "utils.hpp" #include "common/arg_macros.hpp" #include "core/common/debug.hpp" diff --git a/src/posix/platform/netif.cpp b/src/posix/platform/netif.cpp index f8524216c..fb4a36c73 100644 --- a/src/posix/platform/netif.cpp +++ b/src/posix/platform/netif.cpp @@ -154,6 +154,7 @@ extern int #include "ip6_utils.hpp" #include "logger.hpp" #include "resolver.hpp" +#include "utils.hpp" #include "common/code_utils.hpp" unsigned int gNetifIndex = 0; @@ -1874,7 +1875,7 @@ static void mldListenerInit(void) { struct ipv6_mreq mreq6; - sMLDMonitorFd = SocketWithCloseExec(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6, kSocketNonBlock); + sMLDMonitorFd = ot::Posix::SocketWithCloseExec(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6, ot::Posix::kSocketNonBlock); VerifyOrDie(sMLDMonitorFd != -1, OT_EXIT_FAILURE); mreq6.ipv6mr_interface = gNetifIndex; @@ -2067,7 +2068,7 @@ static void platformConfigureTunDevice(otPlatformConfig *aPlatformConfig) struct sockaddr_ctl addr; struct ctl_info info; - sTunFd = SocketWithCloseExec(PF_SYSTEM, SOCK_DGRAM, SYSPROTO_CONTROL, kSocketNonBlock); + sTunFd = ot::Posix::SocketWithCloseExec(PF_SYSTEM, SOCK_DGRAM, SYSPROTO_CONTROL, ot::Posix::kSocketNonBlock); VerifyOrDie(sTunFd >= 0, OT_EXIT_ERROR_ERRNO); memset(&info, 0, sizeof(info)); @@ -2146,9 +2147,9 @@ static void platformConfigureTunDevice(otPlatformConfig *aPlatformConfig) static void platformConfigureNetLink(void) { #ifdef __linux__ - sNetlinkFd = SocketWithCloseExec(AF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE, kSocketNonBlock); + sNetlinkFd = ot::Posix::SocketWithCloseExec(AF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE, ot::Posix::kSocketNonBlock); #elif defined(__APPLE__) || defined(__NetBSD__) || defined(__FreeBSD__) - sNetlinkFd = SocketWithCloseExec(PF_ROUTE, SOCK_RAW, 0, kSocketNonBlock); + sNetlinkFd = ot::Posix::SocketWithCloseExec(PF_ROUTE, SOCK_RAW, 0, ot::Posix::kSocketNonBlock); #else #error "!! Unknown platform !!" #endif @@ -2219,7 +2220,7 @@ void platformNetifInit(otPlatformConfig *aPlatformConfig) (void)LogNote; (void)LogDebg; - sIpFd = SocketWithCloseExec(AF_INET6, SOCK_DGRAM, IPPROTO_IP, kSocketNonBlock); + sIpFd = ot::Posix::SocketWithCloseExec(AF_INET6, SOCK_DGRAM, IPPROTO_IP, ot::Posix::kSocketNonBlock); VerifyOrDie(sIpFd >= 0, OT_EXIT_ERROR_ERRNO); platformConfigureNetLink(); diff --git a/src/posix/platform/platform-posix.h b/src/posix/platform/platform-posix.h index 3e7090a79..d437b1fcf 100644 --- a/src/posix/platform/platform-posix.h +++ b/src/posix/platform/platform-posix.h @@ -342,20 +342,6 @@ void platformTrelUpdateFdSet(otSysMainloopContext *aContext); */ void platformTrelProcess(otInstance *aInstance, const otSysMainloopContext *aContext); -/** - * Creates a socket with SOCK_CLOEXEC flag set. - * - * @param[in] aDomain The communication domain. - * @param[in] aType The semantics of communication. - * @param[in] aProtocol The protocol to use. - * @param[in] aBlockOption Whether to add nonblock flags. - * - * @returns The file descriptor of the created socket. - * - * @retval -1 Failed to create socket. - */ -int SocketWithCloseExec(int aDomain, int aType, int aProtocol, SocketBlockOption aBlockOption); - /** * The name of Thread network interface. */ diff --git a/src/posix/platform/trel.cpp b/src/posix/platform/trel.cpp index 6e6f566dc..6394c0b23 100644 --- a/src/posix/platform/trel.cpp +++ b/src/posix/platform/trel.cpp @@ -49,6 +49,7 @@ #include "logger.hpp" #include "radio_url.hpp" #include "system.hpp" +#include "utils.hpp" #include "common/code_utils.hpp" #if OPENTHREAD_CONFIG_RADIO_LINK_TREL_ENABLE @@ -172,7 +173,7 @@ static void PrepareSocket(uint16_t &aUdpPort) LogDebg("PrepareSocket()"); - sSocket = SocketWithCloseExec(AF_INET6, SOCK_DGRAM, 0, kSocketNonBlock); + sSocket = ot::Posix::SocketWithCloseExec(AF_INET6, SOCK_DGRAM, 0, ot::Posix::kSocketNonBlock); VerifyOrDie(sSocket >= 0, OT_EXIT_ERROR_ERRNO); // Make the socket non-blocking to allow immediate tx attempt. diff --git a/src/posix/platform/udp.cpp b/src/posix/platform/udp.cpp index ebd2009d5..5b736e52a 100644 --- a/src/posix/platform/udp.cpp +++ b/src/posix/platform/udp.cpp @@ -58,6 +58,7 @@ #include "posix/platform/ip6_utils.hpp" #include "posix/platform/mainloop.hpp" #include "posix/platform/udp.hpp" +#include "posix/platform/utils.hpp" using namespace ot::Posix::Ip6Utils; @@ -222,7 +223,7 @@ otError otPlatUdpSocket(otUdpSocket *aUdpSocket) assert(aUdpSocket->mHandle == nullptr); - fd = SocketWithCloseExec(AF_INET6, SOCK_DGRAM, IPPROTO_UDP, kSocketNonBlock); + fd = ot::Posix::SocketWithCloseExec(AF_INET6, SOCK_DGRAM, IPPROTO_UDP, ot::Posix::kSocketNonBlock); VerifyOrExit(fd >= 0, error = OT_ERROR_FAILED); aUdpSocket->mHandle = FdToHandle(fd); diff --git a/src/posix/platform/utils.cpp b/src/posix/platform/utils.cpp index 8183ac391..fd158df9d 100644 --- a/src/posix/platform/utils.cpp +++ b/src/posix/platform/utils.cpp @@ -34,16 +34,50 @@ #include "utils.hpp" #include +#include +#include #include #include +#include +#include +#include +#include +#include #include #include "common/code_utils.hpp" +#include "lib/platform/exit_code.h" namespace ot { namespace Posix { +int SocketWithCloseExec(int aDomain, int aType, int aProtocol, SocketBlockOption aBlockOption) +{ + int rval = 0; + int fd = -1; + +#ifdef __APPLE__ + VerifyOrExit((fd = socket(aDomain, aType, aProtocol)) != -1, perror("socket(SOCK_CLOEXEC)")); + + VerifyOrExit((rval = fcntl(fd, F_GETFD, 0)) != -1, perror("fcntl(F_GETFD)")); + rval |= aBlockOption == kSocketNonBlock ? O_NONBLOCK | FD_CLOEXEC : FD_CLOEXEC; + VerifyOrExit((rval = fcntl(fd, F_SETFD, rval)) != -1, perror("fcntl(F_SETFD)")); +#else + aType |= aBlockOption == kSocketNonBlock ? SOCK_CLOEXEC | SOCK_NONBLOCK : SOCK_CLOEXEC; + VerifyOrExit((fd = socket(aDomain, aType, aProtocol)) != -1, perror("socket(SOCK_CLOEXEC)")); +#endif + +exit: + if (rval == -1) + { + VerifyOrDie(close(fd) == 0, OT_EXIT_ERROR_ERRNO); + fd = -1; + } + + return fd; +} + enum { kSystemCommandMaxLength = 1024, ///< Max length of a system call command. diff --git a/src/posix/platform/utils.hpp b/src/posix/platform/utils.hpp index 038c2f496..73c1e5298 100644 --- a/src/posix/platform/utils.hpp +++ b/src/posix/platform/utils.hpp @@ -34,6 +34,28 @@ namespace ot { namespace Posix { +/** + * Represents socket block/non-block options. + */ +enum SocketBlockOption : uint8_t +{ + kSocketBlock, + kSocketNonBlock, +}; + +/** + * Creates a socket with SOCK_CLOEXEC flag set. + * + * @param[in] aDomain The communication domain. + * @param[in] aType The semantics of communication. + * @param[in] aProtocol The protocol to use. + * @param[in] aBlockOption Whether to add nonblock flags. + * + * @returns The file descriptor of the created socket, or -1 if fails to create the socket. + * @retval -1 Failed to create socket. + */ +int SocketWithCloseExec(int aDomain, int aType, int aProtocol, SocketBlockOption aBlockOption); + /** * Formats a system command to execute. * diff --git a/src/posix/platform/virtual_time.cpp b/src/posix/platform/virtual_time.cpp index 6777456c8..567ab87f4 100644 --- a/src/posix/platform/virtual_time.cpp +++ b/src/posix/platform/virtual_time.cpp @@ -42,6 +42,8 @@ #include #include +#include "utils.hpp" + #if OPENTHREAD_POSIX_VIRTUAL_TIME static const int kMaxNetworkSize = 33; ///< Well-known ID used by a simulated radio supporting promiscuous mode. @@ -83,7 +85,7 @@ void virtualTimeInit(uint16_t aNodeId) sockaddr.sin_port = htons(kBasePort + sPortOffset + aNodeId); sockaddr.sin_addr.s_addr = INADDR_ANY; - sSockFd = SocketWithCloseExec(AF_INET, SOCK_DGRAM, IPPROTO_UDP, kSocketBlock); + sSockFd = ot::Posix::SocketWithCloseExec(AF_INET, SOCK_DGRAM, IPPROTO_UDP, ot::Posix::kSocketBlock); if (sSockFd == -1) {