OSDN Git Service

Use struct android_net_context when interfacing with bionic
authorErik Kline <ek@google.com>
Thu, 25 Jun 2015 09:24:46 +0000 (18:24 +0900)
committerErik Kline <ek@google.com>
Mon, 6 Jul 2015 00:18:12 +0000 (09:18 +0900)
Add a new NetworkController::getNetworkContext() that builds the
contents of a struct net_context out of getNetworkForConnect()
and getNetworkForDns().

Bug: 19470192
Bug: 20733156
Bug: 21832279
Change-Id: I5a69b0413a83d33be28b78c0a99359b109517a8f

server/DnsProxyListener.cpp
server/DnsProxyListener.h
server/NetworkController.cpp
server/NetworkController.h

index a0f0a30..6c71c5b 100644 (file)
@@ -48,18 +48,14 @@ DnsProxyListener::DnsProxyListener(const NetworkController* netCtrl) :
     registerCmd(new GetHostByNameCmd(this));
 }
 
-DnsProxyListener::GetAddrInfoHandler::GetAddrInfoHandler(SocketClient *c,
-                                                         char* host,
-                                                         char* service,
-                                                         struct addrinfo* hints,
-                                                         unsigned netId,
-                                                         uint32_t mark)
+DnsProxyListener::GetAddrInfoHandler::GetAddrInfoHandler(
+        SocketClient *c, char* host, char* service, struct addrinfo* hints,
+        const struct android_net_context& netcontext)
         : mClient(c),
           mHost(host),
           mService(service),
           mHints(hints),
-          mNetId(netId),
-          mMark(mark) {
+          mNetContext(netcontext) {
 }
 
 DnsProxyListener::GetAddrInfoHandler::~GetAddrInfoHandler() {
@@ -160,11 +156,14 @@ static bool sendaddrinfo(SocketClient* c, struct addrinfo* ai) {
 
 void DnsProxyListener::GetAddrInfoHandler::run() {
     if (DBG) {
-        ALOGD("GetAddrInfoHandler, now for %s / %s / %u / %u", mHost, mService, mNetId, mMark);
+        ALOGD("GetAddrInfoHandler, now for %s / %s / {%u,%u,%u,%u,%u}", mHost, mService,
+                mNetContext.app_netid, mNetContext.app_mark,
+                mNetContext.dns_netid, mNetContext.dns_mark,
+                mNetContext.uid);
     }
 
     struct addrinfo* result = NULL;
-    uint32_t rv = android_getaddrinfofornet(mHost, mService, mHints, mNetId, mMark, &result);
+    uint32_t rv = android_getaddrinfofornetcontext(mHost, mService, mHints, &mNetContext, &result);
     if (rv) {
         // getaddrinfo failed
         mClient->sendBinaryMsg(ResponseCode::DnsProxyOperationFailed, &rv, sizeof(rv));
@@ -229,7 +228,8 @@ int DnsProxyListener::GetAddrInfoCmd::runCommand(SocketClient *cli,
     unsigned netId = strtoul(argv[7], NULL, 10);
     uid_t uid = cli->getUid();
 
-    uint32_t mark = mDnsProxyListener->mNetCtrl->getNetworkForDns(&netId, uid);
+    struct android_net_context netcontext;
+    mDnsProxyListener->mNetCtrl->getNetworkContext(netId, uid, &netcontext);
 
     if (ai_flags != -1 || ai_family != -1 ||
         ai_socktype != -1 || ai_protocol != -1) {
@@ -242,21 +242,23 @@ int DnsProxyListener::GetAddrInfoCmd::runCommand(SocketClient *cli,
         // Only implement AI_ADDRCONFIG if application is using default network since our
         // implementation only works on the default network.
         if ((hints->ai_flags & AI_ADDRCONFIG) &&
-                netId != mDnsProxyListener->mNetCtrl->getDefaultNetwork()) {
+                netcontext.dns_netid != mDnsProxyListener->mNetCtrl->getDefaultNetwork()) {
             hints->ai_flags &= ~AI_ADDRCONFIG;
         }
     }
 
     if (DBG) {
-        ALOGD("GetAddrInfoHandler for %s / %s / %u / %d / %u",
+        ALOGD("GetAddrInfoHandler for %s / %s / {%u,%u,%u,%u,%u}",
              name ? name : "[nullhost]",
              service ? service : "[nullservice]",
-             netId, uid, mark);
+             netcontext.app_netid, netcontext.app_mark,
+             netcontext.dns_netid, netcontext.dns_mark,
+             netcontext.uid);
     }
 
     cli->incRef();
     DnsProxyListener::GetAddrInfoHandler* handler =
-            new DnsProxyListener::GetAddrInfoHandler(cli, name, service, hints, netId, mark);
+            new DnsProxyListener::GetAddrInfoHandler(cli, name, service, hints, netcontext);
     handler->start();
 
     return 0;
index 106961e..bd7251d 100644 (file)
@@ -17,6 +17,7 @@
 #ifndef _DNSPROXYLISTENER_H__
 #define _DNSPROXYLISTENER_H__
 
+#include <resolv_netid.h>  // struct android_net_context
 #include <sysutils/FrameworkListener.h>
 
 #include "NetdCommand.h"
@@ -46,8 +47,7 @@ private:
                            char* host,
                            char* service,
                            struct addrinfo* hints,
-                           unsigned netId,
-                           uint32_t mark);
+                           const struct android_net_context& netcontext);
         ~GetAddrInfoHandler();
 
         static void* threadStart(void* handler);
@@ -59,8 +59,7 @@ private:
         char* mHost;    // owned
         char* mService; // owned
         struct addrinfo* mHints;  // owned
-        unsigned mNetId;
-        uint32_t mMark;
+        struct android_net_context mNetContext;
     };
 
     /* ------ gethostbyname ------*/
index 76e4a6a..93a0763 100644 (file)
@@ -237,6 +237,30 @@ unsigned NetworkController::getNetworkForConnect(uid_t uid) const {
     return mDefaultNetId;
 }
 
+void NetworkController::getNetworkContext(
+        unsigned netId, uid_t uid, struct android_net_context* netcontext) const {
+    struct android_net_context nc = {
+            .app_netid = netId,
+            .app_mark = MARK_UNSET,
+            .dns_netid = netId,
+            .dns_mark = MARK_UNSET,
+            .uid = uid,
+    };
+
+    if (nc.app_netid == NETID_UNSET) {
+        nc.app_netid = getNetworkForConnect(uid);
+    }
+    Fwmark fwmark;
+    fwmark.netId = nc.app_netid;
+    nc.app_mark = fwmark.intValue;
+
+    nc.dns_mark = getNetworkForDns(&(nc.dns_netid), uid);
+
+    if (netcontext) {
+        *netcontext = nc;
+    }
+}
+
 unsigned NetworkController::getNetworkForInterface(const char* interface) const {
     android::RWLock::AutoRLock lock(mRWLock);
     for (const auto& entry : mNetworks) {
index 073745d..6d72aeb 100644 (file)
@@ -56,6 +56,7 @@ public:
     uint32_t getNetworkForDns(unsigned* netId, uid_t uid) const;
     unsigned getNetworkForUser(uid_t uid) const;
     unsigned getNetworkForConnect(uid_t uid) const;
+    void getNetworkContext(unsigned netId, uid_t uid, struct android_net_context* netcontext) const;
     unsigned getNetworkForInterface(const char* interface) const;
     bool isVirtualNetwork(unsigned netId) const;