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
diff --git a/server/NetworkController.h b/server/NetworkController.h
index d5451ee..eff3823 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 <android/multinetwork.h>
#include "NetdConstants.h"
#include "Permission.h"
@@ -33,6 +34,36 @@
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;