From 91dc3369d3b75928816bc26f9e15ab140a520ee2 Mon Sep 17 00:00:00 2001 From: Sreeram Ramachandran Date: Wed, 21 May 2014 08:55:19 -0700 Subject: [PATCH] Move netd_client into netd. (cherry picked from commit f1b21c5c735e9150c8f29bdb52db2f3eb3ffc469) Change-Id: Ie4b6b303225c93f2448a503d6ea9cebb552cbad5 --- include/netd_client/FwmarkCommands.h | 29 --------- libnetd_client/Android.mk | 22 ------- libnetd_client/FwmarkClient.cpp | 96 ---------------------------- libnetd_client/FwmarkClient.h | 38 ----------- libnetd_client/NetdClient.cpp | 84 ------------------------ 5 files changed, 269 deletions(-) delete mode 100644 include/netd_client/FwmarkCommands.h delete mode 100644 libnetd_client/Android.mk delete mode 100644 libnetd_client/FwmarkClient.cpp delete mode 100644 libnetd_client/FwmarkClient.h delete mode 100644 libnetd_client/NetdClient.cpp diff --git a/include/netd_client/FwmarkCommands.h b/include/netd_client/FwmarkCommands.h deleted file mode 100644 index 0d22f02e0..000000000 --- a/include/netd_client/FwmarkCommands.h +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Copyright (C) 2014 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef NETD_CLIENT_FWMARK_COMMANDS_H -#define NETD_CLIENT_FWMARK_COMMANDS_H - -#include - -// Commands sent from clients to the fwmark server to mark sockets (i.e., set their SO_MARK). -const uint8_t FWMARK_COMMAND_ON_CREATE = 0; -const uint8_t FWMARK_COMMAND_ON_CONNECT = 1; -const uint8_t FWMARK_COMMAND_ON_ACCEPT = 2; -const uint8_t FWMARK_COMMAND_SELECT_NETWORK = 3; -const uint8_t FWMARK_COMMAND_PROTECT_FROM_VPN = 4; - -#endif // NETD_CLIENT_FWMARK_COMMANDS_H diff --git a/libnetd_client/Android.mk b/libnetd_client/Android.mk deleted file mode 100644 index 2b7562615..000000000 --- a/libnetd_client/Android.mk +++ /dev/null @@ -1,22 +0,0 @@ -# Copyright (C) 2014 The Android Open Source Project -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -LOCAL_PATH := $(call my-dir) - -include $(CLEAR_VARS) - -LOCAL_MODULE := libnetd_client -LOCAL_SRC_FILES := FwmarkClient.cpp NetdClient.cpp - -include $(BUILD_SHARED_LIBRARY) diff --git a/libnetd_client/FwmarkClient.cpp b/libnetd_client/FwmarkClient.cpp deleted file mode 100644 index e360b4e16..000000000 --- a/libnetd_client/FwmarkClient.cpp +++ /dev/null @@ -1,96 +0,0 @@ -/* - * Copyright (C) 2014 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "FwmarkClient.h" - -#include -#include -#include -#include - -namespace { - -const sockaddr_un FWMARK_SERVER_PATH = {AF_UNIX, "/dev/socket/fwmarkd"}; - -} // namespace - -bool FwmarkClient::shouldSetFwmark(int sockfd, const sockaddr* addr) { - return sockfd >= 0 && addr && (addr->sa_family == AF_INET || addr->sa_family == AF_INET6) && - !getenv("ANDROID_NO_USE_FWMARK_CLIENT"); -} - -FwmarkClient::FwmarkClient() : mChannel(-1) { -} - -FwmarkClient::~FwmarkClient() { - if (mChannel >= 0) { - // We don't care about errors while closing the channel, so restore any previous error. - int error = errno; - close(mChannel); - errno = error; - } -} - -bool FwmarkClient::send(void* data, size_t len, int fd) { - mChannel = socket(AF_UNIX, SOCK_STREAM, 0); - if (mChannel == -1) { - return false; - } - - if (TEMP_FAILURE_RETRY(connect(mChannel, reinterpret_cast(&FWMARK_SERVER_PATH), - sizeof(FWMARK_SERVER_PATH))) == -1) { - // If we are unable to connect to the fwmark server, assume there's no error. This protects - // against future changes if the fwmark server goes away. - errno = 0; - return true; - } - - iovec iov; - iov.iov_base = data; - iov.iov_len = len; - - msghdr message; - memset(&message, 0, sizeof(message)); - message.msg_iov = &iov; - message.msg_iovlen = 1; - - union { - cmsghdr cmh; - char cmsg[CMSG_SPACE(sizeof(fd))]; - } cmsgu; - - memset(cmsgu.cmsg, 0, sizeof(cmsgu.cmsg)); - message.msg_control = cmsgu.cmsg; - message.msg_controllen = sizeof(cmsgu.cmsg); - - cmsghdr* const cmsgh = CMSG_FIRSTHDR(&message); - cmsgh->cmsg_len = CMSG_LEN(sizeof(fd)); - cmsgh->cmsg_level = SOL_SOCKET; - cmsgh->cmsg_type = SCM_RIGHTS; - memcpy(CMSG_DATA(cmsgh), &fd, sizeof(fd)); - - if (TEMP_FAILURE_RETRY(sendmsg(mChannel, &message, 0)) == -1) { - return false; - } - - int error = 0; - if (TEMP_FAILURE_RETRY(recv(mChannel, &error, sizeof(error), 0)) == -1) { - return false; - } - - errno = error; - return !error; -} diff --git a/libnetd_client/FwmarkClient.h b/libnetd_client/FwmarkClient.h deleted file mode 100644 index 4cf0cc0d6..000000000 --- a/libnetd_client/FwmarkClient.h +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright (C) 2014 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef NETD_CLIENT_FWMARK_CLIENT_H -#define NETD_CLIENT_FWMARK_CLIENT_H - -#include - -class FwmarkClient { -public: - // Returns true if |sockfd| should be sent to the fwmark server to have its SO_MARK set. - static bool shouldSetFwmark(int sockfd, const sockaddr* addr); - - FwmarkClient(); - ~FwmarkClient(); - - // Sends |data| to the fwmark server, along with |fd| as ancillary data using cmsg(3). - // Returns true on success. - bool send(void* data, size_t len, int fd); - -private: - int mChannel; -}; - -#endif // NETD_CLIENT_INCLUDE_FWMARK_CLIENT_H diff --git a/libnetd_client/NetdClient.cpp b/libnetd_client/NetdClient.cpp deleted file mode 100644 index 8deea1e8e..000000000 --- a/libnetd_client/NetdClient.cpp +++ /dev/null @@ -1,84 +0,0 @@ -/* - * Copyright (C) 2014 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "FwmarkClient.h" -#include "netd_client/FwmarkCommands.h" - -#include -#include - -namespace { - -int closeFdAndRestoreErrno(int fd) { - int error = errno; - close(fd); - errno = error; - return -1; -} - -typedef int (*ConnectFunctionType)(int, const sockaddr*, socklen_t); -typedef int (*AcceptFunctionType)(int, sockaddr*, socklen_t*); - -ConnectFunctionType libcConnect = 0; -AcceptFunctionType libcAccept = 0; - -int netdClientConnect(int sockfd, const sockaddr* addr, socklen_t addrlen) { - if (FwmarkClient::shouldSetFwmark(sockfd, addr)) { - char data[] = {FWMARK_COMMAND_ON_CONNECT}; - if (!FwmarkClient().send(data, sizeof(data), sockfd)) { - return -1; - } - } - return libcConnect(sockfd, addr, addrlen); -} - -int netdClientAccept(int sockfd, sockaddr* addr, socklen_t* addrlen) { - int acceptedSocket = libcAccept(sockfd, addr, addrlen); - if (acceptedSocket == -1) { - return -1; - } - sockaddr socketAddress; - if (!addr) { - socklen_t socketAddressLen = sizeof(socketAddress); - if (getsockname(acceptedSocket, &socketAddress, &socketAddressLen) == -1) { - return closeFdAndRestoreErrno(acceptedSocket); - } - addr = &socketAddress; - } - if (FwmarkClient::shouldSetFwmark(acceptedSocket, addr)) { - char data[] = {FWMARK_COMMAND_ON_ACCEPT}; - if (!FwmarkClient().send(data, sizeof(data), acceptedSocket)) { - return closeFdAndRestoreErrno(acceptedSocket); - } - } - return acceptedSocket; -} - -} // namespace - -extern "C" void netdClientInitConnect(ConnectFunctionType* function) { - if (function && *function) { - libcConnect = *function; - *function = netdClientConnect; - } -} - -extern "C" void netdClientInitAccept(AcceptFunctionType* function) { - if (function && *function) { - libcAccept = *function; - *function = netdClientAccept; - } -}