This doesn't require a roundtrip to netd, since anybody can read the SO_MARK.
Change-Id: I51dd17725c4534cb0d5dbc8e93e844e6a7847959
#include "NetdClient.h"
+#include "Fwmark.h"
#include "FwmarkClient.h"
#include "FwmarkCommand.h"
#include "resolv_netid.h"
}
}
+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;
}
* 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"
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
// 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);
* 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
Permission permissionFromString(const char* permission);
-#endif // NETD_SERVER_PERMISSION_H
+#endif // NETD_INCLUDE_PERMISSION_H