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

[Android] Move nameindex implementation #29775

Merged
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
6 changes: 6 additions & 0 deletions src/inet/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -141,5 +141,11 @@ static_library("inet") {
public_deps += [ "${nlfaultinjection_root}:nlfaultinjection" ]
}

if (current_os == "android") {
public_deps += [ "${chip_root}/src/platform/android:inet" ]
} else {
sources += [ "InetInterfaceImplDefault.cpp" ]
}

cflags = [ "-Wconversion" ]
}
147 changes: 3 additions & 144 deletions src/inet/InetInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
#ifdef HAVE_SYS_SOCKIO_H
#include <sys/sockio.h>
#endif /* HAVE_SYS_SOCKIO_H */
#include "InetInterfaceImpl.h"
#include <ifaddrs.h>
#include <net/if.h>
#include <sys/ioctl.h>
Expand Down Expand Up @@ -502,140 +503,6 @@ void CloseIOCTLSocket()
}
}

#ifdef __ANDROID__

static struct if_nameindex * backport_if_nameindex(void);
static void backport_if_freenameindex(struct if_nameindex *);

static void backport_if_freenameindex(struct if_nameindex * inArray)
{
if (inArray == nullptr)
{
return;
}

for (size_t i = 0; inArray[i].if_index != 0; i++)
{
if (inArray[i].if_name != nullptr)
{
Platform::MemoryFree(inArray[i].if_name);
}
}

Platform::MemoryFree(inArray);
}

static struct if_nameindex * backport_if_nameindex(void)
{
int err;
size_t intfIter = 0;
size_t maxIntfNum = 0;
size_t numIntf = 0;
size_t numAddrs = 0;
struct if_nameindex * retval = nullptr;
struct if_nameindex * tmpval = nullptr;
struct ifaddrs * addrList = nullptr;
struct ifaddrs * addrIter = nullptr;
const char * lastIntfName = "";

err = getifaddrs(&addrList);
VerifyOrExit(err >= 0, );

// coalesce on consecutive interface names
for (addrIter = addrList; addrIter != nullptr; addrIter = addrIter->ifa_next)
{
numAddrs++;
if (strcmp(addrIter->ifa_name, lastIntfName) != 0)
{
numIntf++;
lastIntfName = addrIter->ifa_name;
}
}

tmpval = (struct if_nameindex *) Platform::MemoryAlloc((numIntf + 1) * sizeof(struct if_nameindex));
VerifyOrExit(tmpval != nullptr, );
memset(tmpval, 0, (numIntf + 1) * sizeof(struct if_nameindex));

lastIntfName = "";
for (addrIter = addrList; addrIter != nullptr; addrIter = addrIter->ifa_next)
{
if (strcmp(addrIter->ifa_name, lastIntfName) == 0)
{
continue;
}

unsigned index = if_nametoindex(addrIter->ifa_name);
if (index != 0)
{
tmpval[intfIter].if_index = index;
tmpval[intfIter].if_name = strdup(addrIter->ifa_name);
intfIter++;
}
lastIntfName = addrIter->ifa_name;
}

// coalesce on interface index
maxIntfNum = 0;
for (size_t i = 0; tmpval[i].if_index != 0; i++)
{
if (maxIntfNum < tmpval[i].if_index)
{
maxIntfNum = tmpval[i].if_index;
}
}

retval = (struct if_nameindex *) Platform::MemoryAlloc((maxIntfNum + 1) * sizeof(struct if_nameindex));
VerifyOrExit(retval != nullptr, );
memset(retval, 0, (maxIntfNum + 1) * sizeof(struct if_nameindex));

for (size_t i = 0; tmpval[i].if_index != 0; i++)
{
struct if_nameindex * intf = &tmpval[i];
if (retval[intf->if_index - 1].if_index == 0)
{
retval[intf->if_index - 1] = *intf;
}
else
{
free(intf->if_name);
intf->if_index = 0;
intf->if_name = 0;
}
}

intfIter = 0;

// coalesce potential gaps between indeces
for (size_t i = 0; i < maxIntfNum; i++)
{
if (retval[i].if_index != 0)
{
retval[intfIter] = retval[i];
intfIter++;
}
}

for (size_t i = intfIter; i < maxIntfNum; i++)
{
retval[i].if_index = 0;
retval[i].if_name = nullptr;
}

exit:
if (tmpval != nullptr)
{
Platform::MemoryFree(tmpval);
}

if (addrList != nullptr)
{
freeifaddrs(addrList);
}

return retval;
}
#endif // __ANDROID__

InterfaceIterator::InterfaceIterator()
{
mIntfArray = nullptr;
Expand All @@ -648,11 +515,7 @@ InterfaceIterator::~InterfaceIterator()
{
if (mIntfArray != nullptr)
{
#ifdef __ANDROID__
backport_if_freenameindex(mIntfArray);
#else
if_freenameindex(mIntfArray);
#endif
if_freenameindexImpl(mIntfArray);
mIntfArray = nullptr;
}
}
Expand All @@ -666,11 +529,7 @@ bool InterfaceIterator::Next()
{
if (mIntfArray == nullptr)
{
#ifdef __ANDROID__
mIntfArray = backport_if_nameindex();
#else
mIntfArray = if_nameindex();
#endif
mIntfArray = if_nameindexImpl();
}
else if (mIntfArray[mCurIntf].if_index != 0)
{
Expand Down
34 changes: 34 additions & 0 deletions src/inet/InetInterfaceImpl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
*
* Copyright (c) 2020-2023 Project CHIP Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#pragma once

#include <system/SystemConfig.h>

#if CHIP_SYSTEM_CONFIG_USE_SOCKETS && CHIP_SYSTEM_CONFIG_USE_BSD_IFADDRS
#include <net/if.h>

namespace chip {
namespace Inet {
// Some platforms have broken if_nameindex and if_freenameindex implementations.
// Allow platforms to either wrap a non-broken platform implementation or have a custom
// implementation.
struct if_nameindex * if_nameindexImpl();
void if_freenameindexImpl(struct if_nameindex * inArray);
} // namespace Inet
} // namespace chip
#endif
33 changes: 33 additions & 0 deletions src/inet/InetInterfaceImplDefault.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
*
* Copyright (c) 2020-2023 Project CHIP Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <inet/InetInterfaceImpl.h>

#if CHIP_SYSTEM_CONFIG_USE_SOCKETS && CHIP_SYSTEM_CONFIG_USE_BSD_IFADDRS
#include <net/if.h>
namespace chip {
namespace Inet {
struct if_nameindex * if_nameindexImpl()
{
return if_nameindex();
}
void if_freenameindexImpl(struct if_nameindex * inArray)
{
if_freenameindex(inArray);
}
} // namespace Inet
} // namespace chip
#endif
5 changes: 5 additions & 0 deletions src/platform/android/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ static_library("android") {
public_configs = []
}

source_set("inet") {
sources = [ "InetInterfaceImpl.cpp" ]
deps = [ "${chip_root}/src/lib/support" ]
}

android_library("java") {
output_name = "AndroidPlatform.jar"

Expand Down
Loading
Loading