OSDN Git Service

Add getNetworkForSocket().
authorSreeram Ramachandran <sreeram@google.com>
Fri, 20 Jun 2014 18:59:40 +0000 (11:59 -0700)
committerSreeram Ramachandran <sreeram@google.com>
Fri, 20 Jun 2014 19:17:35 +0000 (12:17 -0700)
This doesn't require a roundtrip to netd, since anybody can read the SO_MARK.

Change-Id: I51dd17725c4534cb0d5dbc8e93e844e6a7847959

client/NetdClient.cpp
include/Fwmark.h [moved from server/Fwmark.h with 93% similarity]
include/NetdClient.h
include/Permission.h [moved from server/Permission.h with 94% similarity]

index 8a54354..7380224 100644 (file)
@@ -16,6 +16,7 @@
 
 #include "NetdClient.h"
 
+#include "Fwmark.h"
 #include "FwmarkClient.h"
 #include "FwmarkCommand.h"
 #include "resolv_netid.h"
@@ -163,6 +164,19 @@ extern "C" void netdClientInitNetIdForResolv(NetIdForResolvFunctionType* functio
     }
 }
 
+extern "C" int getNetworkForSocket(unsigned* netId, int socketFd) {
+    if (!netId || socketFd < 0) {
+        return EBADF;
+    }
+    Fwmark fwmark;
+    socklen_t fwmarkLen = sizeof(fwmark.intValue);
+    if (getsockopt(socketFd, SOL_SOCKET, SO_MARK, &fwmark.intValue, &fwmarkLen) == -1) {
+        return errno;
+    }
+    *netId = fwmark.netId;
+    return 0;
+}
+
 extern "C" unsigned getNetworkForProcess() {
     return netIdForProcess;
 }
similarity index 93%
rename from server/Fwmark.h
rename to include/Fwmark.h
index cb2900f..7360e1b 100644 (file)
@@ -14,8 +14,8 @@
  * limitations under the License.
  */
 
-#ifndef NETD_SERVER_FWMARK_H
-#define NETD_SERVER_FWMARK_H
+#ifndef NETD_INCLUDE_FWMARK_H
+#define NETD_INCLUDE_FWMARK_H
 
 #include "Permission.h"
 
@@ -39,4 +39,4 @@ static const unsigned FWMARK_NET_ID_MASK = 0xffff;
 
 static_assert(sizeof(Fwmark) == sizeof(uint32_t), "The entire fwmark must fit into 32 bits");
 
-#endif  // NETD_SERVER_FWMARK_H
+#endif  // NETD_INCLUDE_FWMARK_H
index 0b75d13..1a2f8be 100644 (file)
@@ -24,6 +24,7 @@ __BEGIN_DECLS
 
 // All functions below that return an int return 0 on success or an errno value on failure.
 
+int getNetworkForSocket(unsigned* netId, int socketFd);
 int setNetworkForSocket(unsigned netId, int socketFd);
 
 unsigned getNetworkForProcess(void);
similarity index 94%
rename from server/Permission.h
rename to include/Permission.h
index 38f946b..1d0ff8d 100644 (file)
@@ -14,8 +14,8 @@
  * limitations under the License.
  */
 
-#ifndef NETD_SERVER_PERMISSION_H
-#define NETD_SERVER_PERMISSION_H
+#ifndef NETD_INCLUDE_PERMISSION_H
+#define NETD_INCLUDE_PERMISSION_H
 
 // This enum represents the permissions we care about for networking. When applied to an app, it's
 // the permission the app (UID) has been granted. When applied to a network, it's the permission an
@@ -37,4 +37,4 @@ enum Permission {
 
 Permission permissionFromString(const char* permission);
 
-#endif  // NETD_SERVER_PERMISSION_H
+#endif  // NETD_INCLUDE_PERMISSION_H