mirror of
https://github.com/espressif/openthread.git
synced 2025-08-06 14:52:18 +08:00
[posix] move SocketWithCloseExec()
helper to common utils.hpp
(#11427)
This commit is contained in:

committed by
GitHub

parent
f42af4e2ee
commit
08a8767fcd
@ -47,6 +47,7 @@
|
|||||||
#include "cli/cli_config.h"
|
#include "cli/cli_config.h"
|
||||||
#include "common/code_utils.hpp"
|
#include "common/code_utils.hpp"
|
||||||
#include "posix/platform/platform-posix.h"
|
#include "posix/platform/platform-posix.h"
|
||||||
|
#include "posix/platform/utils.hpp"
|
||||||
|
|
||||||
#if OPENTHREAD_POSIX_CONFIG_DAEMON_ENABLE
|
#if OPENTHREAD_POSIX_CONFIG_DAEMON_ENABLE
|
||||||
|
|
||||||
|
@ -57,10 +57,11 @@
|
|||||||
#include <openthread/border_router.h>
|
#include <openthread/border_router.h>
|
||||||
#include <openthread/platform/infra_if.h>
|
#include <openthread/platform/infra_if.h>
|
||||||
|
|
||||||
|
#include "infra_if.hpp"
|
||||||
|
#include "utils.hpp"
|
||||||
#include "common/code_utils.hpp"
|
#include "common/code_utils.hpp"
|
||||||
#include "common/debug.hpp"
|
#include "common/debug.hpp"
|
||||||
#include "lib/platform/exit_code.h"
|
#include "lib/platform/exit_code.h"
|
||||||
#include "posix/platform/infra_if.hpp"
|
|
||||||
|
|
||||||
bool otPlatInfraIfHasAddress(uint32_t aInfraIfIndex, const otIp6Address *aAddress)
|
bool otPlatInfraIfHasAddress(uint32_t aInfraIfIndex, const otIp6Address *aAddress)
|
||||||
{
|
{
|
||||||
|
@ -95,29 +95,3 @@ otPlatMcuPowerState otPlatGetMcuPowerState(otInstance *aInstance)
|
|||||||
|
|
||||||
return gPlatMcuPowerState;
|
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;
|
|
||||||
}
|
|
||||||
|
@ -48,6 +48,7 @@
|
|||||||
#include <openthread/backbone_router_ftd.h>
|
#include <openthread/backbone_router_ftd.h>
|
||||||
#include <openthread/logging.h>
|
#include <openthread/logging.h>
|
||||||
|
|
||||||
|
#include "utils.hpp"
|
||||||
#include "common/arg_macros.hpp"
|
#include "common/arg_macros.hpp"
|
||||||
#include "core/common/debug.hpp"
|
#include "core/common/debug.hpp"
|
||||||
|
|
||||||
|
@ -154,6 +154,7 @@ extern int
|
|||||||
#include "ip6_utils.hpp"
|
#include "ip6_utils.hpp"
|
||||||
#include "logger.hpp"
|
#include "logger.hpp"
|
||||||
#include "resolver.hpp"
|
#include "resolver.hpp"
|
||||||
|
#include "utils.hpp"
|
||||||
#include "common/code_utils.hpp"
|
#include "common/code_utils.hpp"
|
||||||
|
|
||||||
unsigned int gNetifIndex = 0;
|
unsigned int gNetifIndex = 0;
|
||||||
@ -1874,7 +1875,7 @@ static void mldListenerInit(void)
|
|||||||
{
|
{
|
||||||
struct ipv6_mreq mreq6;
|
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);
|
VerifyOrDie(sMLDMonitorFd != -1, OT_EXIT_FAILURE);
|
||||||
|
|
||||||
mreq6.ipv6mr_interface = gNetifIndex;
|
mreq6.ipv6mr_interface = gNetifIndex;
|
||||||
@ -2067,7 +2068,7 @@ static void platformConfigureTunDevice(otPlatformConfig *aPlatformConfig)
|
|||||||
struct sockaddr_ctl addr;
|
struct sockaddr_ctl addr;
|
||||||
struct ctl_info info;
|
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);
|
VerifyOrDie(sTunFd >= 0, OT_EXIT_ERROR_ERRNO);
|
||||||
|
|
||||||
memset(&info, 0, sizeof(info));
|
memset(&info, 0, sizeof(info));
|
||||||
@ -2146,9 +2147,9 @@ static void platformConfigureTunDevice(otPlatformConfig *aPlatformConfig)
|
|||||||
static void platformConfigureNetLink(void)
|
static void platformConfigureNetLink(void)
|
||||||
{
|
{
|
||||||
#ifdef __linux__
|
#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__)
|
#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
|
#else
|
||||||
#error "!! Unknown platform !!"
|
#error "!! Unknown platform !!"
|
||||||
#endif
|
#endif
|
||||||
@ -2219,7 +2220,7 @@ void platformNetifInit(otPlatformConfig *aPlatformConfig)
|
|||||||
(void)LogNote;
|
(void)LogNote;
|
||||||
(void)LogDebg;
|
(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);
|
VerifyOrDie(sIpFd >= 0, OT_EXIT_ERROR_ERRNO);
|
||||||
|
|
||||||
platformConfigureNetLink();
|
platformConfigureNetLink();
|
||||||
|
@ -342,20 +342,6 @@ void platformTrelUpdateFdSet(otSysMainloopContext *aContext);
|
|||||||
*/
|
*/
|
||||||
void platformTrelProcess(otInstance *aInstance, const 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.
|
* The name of Thread network interface.
|
||||||
*/
|
*/
|
||||||
|
@ -49,6 +49,7 @@
|
|||||||
#include "logger.hpp"
|
#include "logger.hpp"
|
||||||
#include "radio_url.hpp"
|
#include "radio_url.hpp"
|
||||||
#include "system.hpp"
|
#include "system.hpp"
|
||||||
|
#include "utils.hpp"
|
||||||
#include "common/code_utils.hpp"
|
#include "common/code_utils.hpp"
|
||||||
|
|
||||||
#if OPENTHREAD_CONFIG_RADIO_LINK_TREL_ENABLE
|
#if OPENTHREAD_CONFIG_RADIO_LINK_TREL_ENABLE
|
||||||
@ -172,7 +173,7 @@ static void PrepareSocket(uint16_t &aUdpPort)
|
|||||||
|
|
||||||
LogDebg("PrepareSocket()");
|
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);
|
VerifyOrDie(sSocket >= 0, OT_EXIT_ERROR_ERRNO);
|
||||||
|
|
||||||
// Make the socket non-blocking to allow immediate tx attempt.
|
// Make the socket non-blocking to allow immediate tx attempt.
|
||||||
|
@ -58,6 +58,7 @@
|
|||||||
#include "posix/platform/ip6_utils.hpp"
|
#include "posix/platform/ip6_utils.hpp"
|
||||||
#include "posix/platform/mainloop.hpp"
|
#include "posix/platform/mainloop.hpp"
|
||||||
#include "posix/platform/udp.hpp"
|
#include "posix/platform/udp.hpp"
|
||||||
|
#include "posix/platform/utils.hpp"
|
||||||
|
|
||||||
using namespace ot::Posix::Ip6Utils;
|
using namespace ot::Posix::Ip6Utils;
|
||||||
|
|
||||||
@ -222,7 +223,7 @@ otError otPlatUdpSocket(otUdpSocket *aUdpSocket)
|
|||||||
|
|
||||||
assert(aUdpSocket->mHandle == nullptr);
|
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);
|
VerifyOrExit(fd >= 0, error = OT_ERROR_FAILED);
|
||||||
|
|
||||||
aUdpSocket->mHandle = FdToHandle(fd);
|
aUdpSocket->mHandle = FdToHandle(fd);
|
||||||
|
@ -34,16 +34,50 @@
|
|||||||
#include "utils.hpp"
|
#include "utils.hpp"
|
||||||
|
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <stdint.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <sys/ioctl.h>
|
||||||
|
#include <sys/select.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
#include <openthread/logging.h>
|
#include <openthread/logging.h>
|
||||||
|
|
||||||
#include "common/code_utils.hpp"
|
#include "common/code_utils.hpp"
|
||||||
|
#include "lib/platform/exit_code.h"
|
||||||
|
|
||||||
namespace ot {
|
namespace ot {
|
||||||
namespace Posix {
|
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
|
enum
|
||||||
{
|
{
|
||||||
kSystemCommandMaxLength = 1024, ///< Max length of a system call command.
|
kSystemCommandMaxLength = 1024, ///< Max length of a system call command.
|
||||||
|
@ -34,6 +34,28 @@
|
|||||||
namespace ot {
|
namespace ot {
|
||||||
namespace Posix {
|
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.
|
* Formats a system command to execute.
|
||||||
*
|
*
|
||||||
|
@ -42,6 +42,8 @@
|
|||||||
#include <sys/select.h>
|
#include <sys/select.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
|
#include "utils.hpp"
|
||||||
|
|
||||||
#if OPENTHREAD_POSIX_VIRTUAL_TIME
|
#if OPENTHREAD_POSIX_VIRTUAL_TIME
|
||||||
|
|
||||||
static const int kMaxNetworkSize = 33; ///< Well-known ID used by a simulated radio supporting promiscuous mode.
|
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_port = htons(kBasePort + sPortOffset + aNodeId);
|
||||||
sockaddr.sin_addr.s_addr = INADDR_ANY;
|
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)
|
if (sSockFd == -1)
|
||||||
{
|
{
|
||||||
|
Reference in New Issue
Block a user