Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert Inet::InterfaceId to a class #10979

Merged
merged 3 commits into from
Oct 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions examples/chip-tool/commands/common/Command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,11 @@ static bool ParseAddressWithInterface(const char * addressString, Command::Addre
if (result->ai_family == AF_INET6)
{
struct sockaddr_in6 * addr = reinterpret_cast<struct sockaddr_in6 *>(result->ai_addr);
address->interfaceId = addr->sin6_scope_id;
address->interfaceId = ::chip::Inet::InterfaceId(addr->sin6_scope_id);
}
else
{
address->interfaceId = INET_NULL_INTERFACEID;
address->interfaceId = chip::Inet::InterfaceId::Null();
}

return true;
Expand Down
2 changes: 1 addition & 1 deletion examples/chip-tool/commands/pairing/PairingCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ void PairingCommand::OnDiscoveredDevice(const chip::Dnssd::DiscoveredNodeData &
// Stop Mdns discovery. Is it the right method ?
mController.RegisterDeviceDiscoveryDelegate(nullptr);

Inet::InterfaceId interfaceId = nodeData.ipAddress[0].IsIPv6LinkLocal() ? nodeData.interfaceId[0] : INET_NULL_INTERFACEID;
Inet::InterfaceId interfaceId = nodeData.ipAddress[0].IsIPv6LinkLocal() ? nodeData.interfaceId[0] : Inet::InterfaceId::Null();
PeerAddress peerAddress = PeerAddress::UDP(nodeData.ipAddress[0], port, interfaceId);
CHIP_ERROR err = Pair(mNodeId, peerAddress);
if (CHIP_NO_ERROR != err)
Expand Down
2 changes: 1 addition & 1 deletion examples/chip-tool/commands/pairing/PairingCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class PairingCommand : public CHIPCommand,
chip::Dnssd::DiscoveryFilterType filterType = chip::Dnssd::DiscoveryFilterType::kNone) :
CHIPCommand(commandName),
mPairingMode(mode), mNetworkType(networkType),
mFilterType(filterType), mRemoteAddr{ IPAddress::Any, INET_NULL_INTERFACEID },
mFilterType(filterType), mRemoteAddr{ IPAddress::Any, chip::Inet::InterfaceId::Null() },
mOnDeviceConnectedCallback(OnDeviceConnectedFn, this), mOnDeviceConnectionFailureCallback(OnDeviceConnectionFailureFn, this)
{
AddArgument("node-id", 0, UINT64_MAX, &mNodeId);
Expand Down
4 changes: 2 additions & 2 deletions examples/minimal-mdns/AllInterfaceListener.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class AllInterfaces : public mdns::Minimal::ListenIterator
if (mState == State::kIpV4)
{
#if INET_CONFIG_ENABLE_IPV4
*id = INET_NULL_INTERFACEID;
*id = Inet::InterfaceId::Null();
*type = chip::Inet::IPAddressType::kIPv4;
#endif
mState = State::kIpV6;
Expand Down Expand Up @@ -100,7 +100,7 @@ class AllInterfaces : public mdns::Minimal::ListenIterator
return true; // not a usable interface
}

char name[chip::Inet::InterfaceIterator::kMaxIfNameLength];
char name[chip::Inet::InterfaceId::kMaxIfNameLength];
if (mIterator.GetInterfaceName(name, sizeof(name)) != CHIP_NO_ERROR)
{
printf("!!!! FAILED TO GET INTERFACE NAME\n");
Expand Down
2 changes: 1 addition & 1 deletion examples/shell/shell_common/cmd_ping.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ Transport::PeerAddress GetEchoPeerAddress()
#endif
{

return Transport::PeerAddress::UDP(gDestAddr, gPingArguments.GetEchoPort(), INET_NULL_INTERFACEID);
return Transport::PeerAddress::UDP(gDestAddr, gPingArguments.GetEchoPort(), ::chip::Inet::InterfaceId::Null());
}
}

Expand Down
2 changes: 1 addition & 1 deletion examples/shell/shell_common/cmd_send.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ void ProcessCommand(streamer_t * stream, char * destination)
else
#endif
{
peerAddress = Transport::PeerAddress::UDP(gDestAddr, gSendArguments.GetPort(), INET_NULL_INTERFACEID);
peerAddress = Transport::PeerAddress::UDP(gDestAddr, gSendArguments.GetPort(), Inet::InterfaceId::Null());

err = gSessionManager.Init(&DeviceLayer::SystemLayer(), &gUDPManager, &gMessageCounterManager);
SuccessOrExit(err);
Expand Down
2 changes: 1 addition & 1 deletion src/app/tests/integration/chip_im_initiator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ CHIP_ERROR EstablishSecureSession()

// Attempt to connect to the peer.
err = gSessionManager.NewPairing(chip::Optional<chip::Transport::PeerAddress>::Value(
chip::Transport::PeerAddress::UDP(gDestAddr, CHIP_PORT, INET_NULL_INTERFACEID)),
chip::Transport::PeerAddress::UDP(gDestAddr, CHIP_PORT, chip::Inet::InterfaceId::Null())),
chip::kTestDeviceNodeId, testSecurePairingSecret, chip::CryptoContext::SessionRole::kInitiator,
gFabricIndex);

Expand Down
8 changes: 4 additions & 4 deletions src/controller/CHIPDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@ CHIP_ERROR Device::Serialize(SerializedDevice & output)

serializable.mDeviceTransport = to_underlying(mDeviceAddress.GetTransportType());

ReturnErrorOnFailure(Inet::GetInterfaceName(mDeviceAddress.GetInterface(), Uint8::to_char(serializable.mInterfaceName),
sizeof(serializable.mInterfaceName)));
ReturnErrorOnFailure(mDeviceAddress.GetInterface().GetInterfaceName(Uint8::to_char(serializable.mInterfaceName),
sizeof(serializable.mInterfaceName)));
static_assert(sizeof(serializable.mDeviceAddr) <= INET6_ADDRSTRLEN, "Size of device address must fit within INET6_ADDRSTRLEN");
mDeviceAddress.GetIPAddress().ToString(Uint8::to_char(serializable.mDeviceAddr), sizeof(serializable.mDeviceAddr));

Expand Down Expand Up @@ -202,13 +202,13 @@ CHIP_ERROR Device::Deserialize(const SerializedDevice & input)

// The InterfaceNameToId() API requires initialization of mInterface, and lock/unlock of
// LwIP stack.
Inet::InterfaceId interfaceId = INET_NULL_INTERFACEID;
Inet::InterfaceId interfaceId;
if (serializable.mInterfaceName[0] != '\0')
{
#if CHIP_SYSTEM_CONFIG_USE_LWIP
LOCK_TCPIP_CORE();
#endif
CHIP_ERROR inetErr = Inet::InterfaceNameToId(Uint8::to_const_char(serializable.mInterfaceName), interfaceId);
CHIP_ERROR inetErr = Inet::InterfaceId::InterfaceNameToId(Uint8::to_const_char(serializable.mInterfaceName), interfaceId);
#if CHIP_SYSTEM_CONFIG_USE_LWIP
UNLOCK_TCPIP_CORE();
#endif
Expand Down
6 changes: 3 additions & 3 deletions src/controller/CHIPDeviceController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -543,9 +543,9 @@ CHIP_ERROR DeviceController::GetPeerAddressAndPort(PeerId peerId, Inet::IPAddres
#if CHIP_DEVICE_CONFIG_ENABLE_DNSSD
void DeviceController::OnNodeIdResolved(const chip::Dnssd::ResolvedNodeData & nodeData)
{
CHIP_ERROR err = CHIP_NO_ERROR;
Device * device = nullptr;
Inet::InterfaceId interfaceId = INET_NULL_INTERFACEID;
CHIP_ERROR err = CHIP_NO_ERROR;
Device * device = nullptr;
Inet::InterfaceId interfaceId;

err = GetDevice(nodeData.mPeerId.GetNodeId(), &device);
SuccessOrExit(err);
Expand Down
2 changes: 1 addition & 1 deletion src/controller/SetUpCodePairer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ void SetUpCodePairer::OnDiscoveredDevice(const Dnssd::DiscoveredNodeData & nodeD
LogErrorOnFailure(StopConnectOverIP());
LogErrorOnFailure(StopConnectOverSoftAP());

Inet::InterfaceId interfaceId = nodeData.ipAddress[0].IsIPv6LinkLocal() ? nodeData.interfaceId[0] : INET_NULL_INTERFACEID;
Inet::InterfaceId interfaceId = nodeData.ipAddress[0].IsIPv6LinkLocal() ? nodeData.interfaceId[0] : Inet::InterfaceId::Null();
Transport::PeerAddress peerAddress = Transport::PeerAddress::UDP(nodeData.ipAddress[0], nodeData.port, interfaceId);
RendezvousParameters params = RendezvousParameters().SetPeerAddress(peerAddress);
OnDeviceDiscovered(params);
Expand Down
2 changes: 1 addition & 1 deletion src/controller/python/chip/discovery/NodeResolution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class PythonResolverDelegate : public ResolverDelegate
mSuccessCallback( //
nodeData.mPeerId.GetCompressedFabricId(), //
nodeData.mPeerId.GetNodeId(), //
nodeData.mInterfaceId, //
nodeData.mInterfaceId.GetPlatformInterface(), //
nodeData.mAddress.ToString(ipAddressBuffer, sizeof(ipAddressBuffer)), //
nodeData.mPort //
);
Expand Down
61 changes: 31 additions & 30 deletions src/inet/IPEndPointBasis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,11 @@ namespace chip {
namespace Inet {

#if CHIP_SYSTEM_CONFIG_USE_LWIP || CHIP_SYSTEM_CONFIG_USE_SOCKETS

static CHIP_ERROR CheckMulticastGroupArgs(InterfaceId aInterfaceId, const IPAddress & aAddress)
{
VerifyOrReturnError(IsInterfaceIdPresent(aInterfaceId), INET_ERROR_UNKNOWN_INTERFACE);

VerifyOrReturnError(aInterfaceId.IsPresent(), INET_ERROR_UNKNOWN_INTERFACE);
VerifyOrReturnError(aAddress.IsMulticast(), INET_ERROR_WRONG_ADDRESS_TYPE);

return CHIP_NO_ERROR;
}
#endif // CHIP_SYSTEM_CONFIG_USE_LWIP || CHIP_SYSTEM_CONFIG_USE_SOCKETS
Expand Down Expand Up @@ -244,11 +243,11 @@ struct netif * IPEndPointBasis::FindNetifFromInterfaceId(InterfaceId aInterfaceI
#if LWIP_VERSION_MAJOR >= 2 && LWIP_VERSION_MINOR >= 0 && defined(NETIF_FOREACH)
NETIF_FOREACH(lRetval)
{
if (lRetval == aInterfaceId)
if (lRetval == aInterfaceId.GetPlatformInterface())
break;
}
#else // LWIP_VERSION_MAJOR < 2 || !defined(NETIF_FOREACH)
for (lRetval = netif_list; lRetval != NULL && lRetval != aInterfaceId; lRetval = lRetval->next)
for (lRetval = netif_list; lRetval != NULL && lRetval != aInterfaceId.GetPlatformInterface(); lRetval = lRetval->next)
;
#endif // LWIP_VERSION_MAJOR >= 2 && LWIP_VERSION_MINOR >= 0 && defined(NETIF_FOREACH)

Expand Down Expand Up @@ -359,7 +358,7 @@ CHIP_ERROR IPEndPointBasis::SetMulticastLoopback(IPVersion aIPVersion, bool aLoo

void IPEndPointBasis::InitImpl()
{
mBoundIntfId = INET_NULL_INTERFACEID;
mBoundIntfId = InterfaceId::Null();
}

#if INET_CONFIG_ENABLE_IPV4
Expand All @@ -374,7 +373,7 @@ static CHIP_ERROR SocketsIPv4JoinLeaveMulticastGroup(int aSocket, InterfaceId aI
{
const IPAddress lCurrentAddress = lAddressIterator.GetAddress();

if (lAddressIterator.GetInterface() == aInterfaceId)
if (lAddressIterator.GetInterfaceId() == aInterfaceId)
{
if (lCurrentAddress.IsIPv4())
{
Expand Down Expand Up @@ -410,8 +409,7 @@ CHIP_ERROR IPEndPointBasis::IPv4JoinLeaveMulticastGroupImpl(InterfaceId aInterfa
static CHIP_ERROR SocketsIPv6JoinLeaveMulticastGroup(int aSocket, InterfaceId aInterfaceId, const IPAddress & aAddress,
int aCommand)
{
VerifyOrReturnError(CanCastTo<unsigned int>(aInterfaceId), CHIP_ERROR_UNEXPECTED_EVENT);
const unsigned int lIfIndex = static_cast<unsigned int>(aInterfaceId);
const InterfaceId::PlatformType lIfIndex = aInterfaceId.GetPlatformInterface();

struct ipv6_mreq lMulticastRequest;
memset(&lMulticastRequest, 0, sizeof(lMulticastRequest));
Expand Down Expand Up @@ -455,14 +453,15 @@ CHIP_ERROR IPEndPointBasis::Bind(IPAddressType aAddressType, const IPAddress & a

memset(&sa, 0, sizeof(sa));

sa.sin6_family = AF_INET6;
sa.sin6_port = htons(aPort);
sa.sin6_addr = aAddress.ToIPv6();
if (!CanCastTo<decltype(sa.sin6_scope_id)>(aInterfaceId))
sa.sin6_family = AF_INET6;
sa.sin6_port = htons(aPort);
sa.sin6_addr = aAddress.ToIPv6();
InterfaceId::PlatformType interfaceId = aInterfaceId.GetPlatformInterface();
if (!CanCastTo<decltype(sa.sin6_scope_id)>(interfaceId))
{
return CHIP_ERROR_INCORRECT_STATE;
}
sa.sin6_scope_id = static_cast<decltype(sa.sin6_scope_id)>(aInterfaceId);
sa.sin6_scope_id = static_cast<decltype(sa.sin6_scope_id)>(interfaceId);

if (bind(mSocket, reinterpret_cast<const sockaddr *>(&sa), static_cast<unsigned>(sizeof(sa))) != 0)
lRetval = CHIP_ERROR_POSIX(errno);
Expand Down Expand Up @@ -526,7 +525,7 @@ CHIP_ERROR IPEndPointBasis::BindInterface(IPAddressType aAddressType, InterfaceI
CHIP_ERROR lRetval = CHIP_NO_ERROR;

#if HAVE_SO_BINDTODEVICE
if (aInterfaceId == INET_NULL_INTERFACEID)
if (!aInterfaceId.IsPresent())
{
// Stop interface-based filtering.
if (setsockopt(mSocket, SOL_SOCKET, SO_BINDTODEVICE, "", 0) == -1)
Expand All @@ -539,7 +538,7 @@ CHIP_ERROR IPEndPointBasis::BindInterface(IPAddressType aAddressType, InterfaceI
// Start filtering on the passed interface.
char lInterfaceName[IF_NAMESIZE];

if (if_indextoname(aInterfaceId, lInterfaceName) == NULL)
if (if_indextoname(aInterfaceId.GetPlatformInterface(), lInterfaceName) == NULL)
{
lRetval = CHIP_ERROR_POSIX(errno);
}
Expand Down Expand Up @@ -589,11 +588,12 @@ CHIP_ERROR IPEndPointBasis::SendMsg(const IPPacketInfo * aPktInfo, chip::System:
msgHeader.msg_name = &peerSockAddr;
if (mAddrType == IPAddressType::kIPv6)
{
peerSockAddr.in6.sin6_family = AF_INET6;
peerSockAddr.in6.sin6_port = htons(aPktInfo->DestPort);
peerSockAddr.in6.sin6_addr = aPktInfo->DestAddress.ToIPv6();
VerifyOrReturnError(CanCastTo<decltype(peerSockAddr.in6.sin6_scope_id)>(aPktInfo->Interface), CHIP_ERROR_INCORRECT_STATE);
peerSockAddr.in6.sin6_scope_id = static_cast<decltype(peerSockAddr.in6.sin6_scope_id)>(aPktInfo->Interface);
peerSockAddr.in6.sin6_family = AF_INET6;
peerSockAddr.in6.sin6_port = htons(aPktInfo->DestPort);
peerSockAddr.in6.sin6_addr = aPktInfo->DestAddress.ToIPv6();
InterfaceId::PlatformType intfId = aPktInfo->Interface.GetPlatformInterface();
VerifyOrReturnError(CanCastTo<decltype(peerSockAddr.in6.sin6_scope_id)>(intfId), CHIP_ERROR_INCORRECT_STATE);
peerSockAddr.in6.sin6_scope_id = static_cast<decltype(peerSockAddr.in6.sin6_scope_id)>(intfId);
msgHeader.msg_namelen = sizeof(sockaddr_in6);
}
#if INET_CONFIG_ENABLE_IPV4
Expand All @@ -612,21 +612,22 @@ CHIP_ERROR IPEndPointBasis::SendMsg(const IPPacketInfo * aPktInfo, chip::System:
// for messages to multicast addresses, which under Linux
// don't seem to get sent out the correct interface, despite
// the socket being bound.
InterfaceId intfId = aPktInfo->Interface;
if (intfId == INET_NULL_INTERFACEID)
intfId = mBoundIntfId;
InterfaceId intf = aPktInfo->Interface;
if (!intf.IsPresent())
intf = mBoundIntfId;

// If the packet should be sent over a specific interface, or with a specific source
// address, construct an IP_PKTINFO/IPV6_PKTINFO "control message" to that effect
// add add it to the message header. If the local OS doesn't support IP_PKTINFO/IPV6_PKTINFO
// fail with an error.
if (intfId != INET_NULL_INTERFACEID || aPktInfo->SrcAddress.Type() != IPAddressType::kAny)
if (intf.IsPresent() || aPktInfo->SrcAddress.Type() != IPAddressType::kAny)
{
#if defined(IP_PKTINFO) || defined(IPV6_PKTINFO)
msgHeader.msg_control = controlData;
msgHeader.msg_controllen = sizeof(controlData);

struct cmsghdr * controlHdr = CMSG_FIRSTHDR(&msgHeader);
struct cmsghdr * controlHdr = CMSG_FIRSTHDR(&msgHeader);
InterfaceId::PlatformType intfId = intf.GetPlatformInterface();

#if INET_CONFIG_ENABLE_IPV4

Expand Down Expand Up @@ -874,12 +875,12 @@ void IPEndPointBasis::HandlePendingIO(uint16_t aPort)
if (controlHdr->cmsg_level == IPPROTO_IP && controlHdr->cmsg_type == IP_PKTINFO)
{
struct in_pktinfo * inPktInfo = reinterpret_cast<struct in_pktinfo *> CMSG_DATA(controlHdr);
if (!CanCastTo<InterfaceId>(inPktInfo->ipi_ifindex))
if (!CanCastTo<InterfaceId::PlatformType>(inPktInfo->ipi_ifindex))
{
lStatus = CHIP_ERROR_INCORRECT_STATE;
break;
}
lPacketInfo.Interface = static_cast<InterfaceId>(inPktInfo->ipi_ifindex);
lPacketInfo.Interface = InterfaceId(static_cast<InterfaceId::PlatformType>(inPktInfo->ipi_ifindex));
lPacketInfo.DestAddress = IPAddress(inPktInfo->ipi_addr);
continue;
}
Expand All @@ -890,12 +891,12 @@ void IPEndPointBasis::HandlePendingIO(uint16_t aPort)
if (controlHdr->cmsg_level == IPPROTO_IPV6 && controlHdr->cmsg_type == IPV6_PKTINFO)
{
struct in6_pktinfo * in6PktInfo = reinterpret_cast<struct in6_pktinfo *> CMSG_DATA(controlHdr);
if (!CanCastTo<InterfaceId>(in6PktInfo->ipi6_ifindex))
if (!CanCastTo<InterfaceId::PlatformType>(in6PktInfo->ipi6_ifindex))
{
lStatus = CHIP_ERROR_INCORRECT_STATE;
break;
}
lPacketInfo.Interface = static_cast<InterfaceId>(in6PktInfo->ipi6_ifindex);
lPacketInfo.Interface = InterfaceId(static_cast<InterfaceId::PlatformType>(in6PktInfo->ipi6_ifindex));
lPacketInfo.DestAddress = IPAddress(in6PktInfo->ipi6_addr);
continue;
}
Expand Down
Loading