From 4c18bd9164104e0a1a81ea505d569937edb30afa Mon Sep 17 00:00:00 2001 From: Niranjan Pendharkar Date: Mon, 24 Jul 2017 09:54:07 -0700 Subject: netd: recognize NDK type in ndc network api ndc network command now accepts strings describing net_handle_t. For e.g. ndc network create handle42966108894 Test: manual Bug: 36682246 CRs-fixed: 2070022 Change-Id: I44857af3d82ea408ae429f9fe16fcf5c876851ae --- server/NetworkController.h | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) (limited to 'server/NetworkController.h') diff --git a/server/NetworkController.h b/server/NetworkController.h index d5451ee8..eff38232 100644 --- a/server/NetworkController.h +++ b/server/NetworkController.h @@ -17,6 +17,7 @@ #ifndef NETD_SERVER_NETWORK_CONTROLLER_H #define NETD_SERVER_NETWORK_CONTROLLER_H +#include #include "NetdConstants.h" #include "Permission.h" @@ -33,6 +34,36 @@ struct android_net_context; namespace android { namespace net { +// Utility to convert from netId to net_handle_t. Doing this here as opposed to exporting +// from net.c as it may have NDK implications. Besides no conversion available in net.c for +// obtaining handle given netId. +// TODO: Use getnetidfromhandle() in net.c. +static inline unsigned netHandleToNetId(net_handle_t fromNetHandle) { + const uint32_t k32BitMask = 0xffffffff; + // This value MUST be kept in sync with the corresponding value in + // the android.net.Network#getNetworkHandle() implementation. + const uint32_t kHandleMagic = 0xfacade; + + // Check for minimum acceptable version of the API in the low bits. + if (fromNetHandle != NETWORK_UNSPECIFIED && + (fromNetHandle & k32BitMask) != kHandleMagic) { + return 0; + } + + return ((fromNetHandle >> (CHAR_BIT * sizeof(k32BitMask))) & k32BitMask); +} + +// Utility to convert from nethandle to netid, keep in sync with getNetworkHandle +// in Network.java. +static inline net_handle_t netIdToNetHandle(unsigned fromNetId) { + const net_handle_t HANDLE_MAGIC = 0xfacade; + + if (!fromNetId) { + return NETWORK_UNSPECIFIED; + } + return (((net_handle_t)fromNetId << 32) | HANDLE_MAGIC); +} + class DumpWriter; class Network; class UidRanges; -- cgit v1.2.3