1 /* 2 * Copyright (C) 2012 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #pragma once 18 19 #include <ifaddrs.h> 20 #include <netdb.h> 21 #include <stddef.h> 22 #include <stdint.h> 23 24 #include <mutex> 25 #include <string> 26 27 #include <netdutils/UidConstants.h> 28 #include <private/android_filesystem_config.h> 29 30 enum IptablesTarget { V4, V6, V4V6 }; 31 32 int execIptablesRestore(IptablesTarget target, const std::string& commands); 33 int execIptablesRestoreWithOutput(IptablesTarget target, const std::string& commands, 34 std::string *output); 35 int execIptablesRestoreCommand(IptablesTarget target, const std::string& table, 36 const std::string& command, std::string *output); 37 bool isIfaceName(const std::string& name); 38 int parsePrefix(const char *prefix, uint8_t *family, void *address, int size, uint8_t *prefixlen); 39 void blockSigpipe(); 40 void setCloseOnExec(const char *sock); 41 42 // TODO: use std::size() instead. 43 #define ARRAY_SIZE(a) (sizeof(a) / sizeof(*(a))) 44 45 #define __INT_STRLEN(i) sizeof(#i) 46 #define _INT_STRLEN(i) __INT_STRLEN(i) 47 #define INT32_STRLEN _INT_STRLEN(INT32_MIN) 48 #define UINT32_STRLEN _INT_STRLEN(UINT32_MAX) 49 #define UINT32_HEX_STRLEN sizeof("0x12345678") 50 #define IPSEC_IFACE_PREFIX "ipsec" 51 52 const uid_t INVALID_UID = static_cast<uid_t>(-1); 53 54 constexpr char TCP_RMEM_PROC_FILE[] = "/proc/sys/net/ipv4/tcp_rmem"; 55 constexpr char TCP_WMEM_PROC_FILE[] = "/proc/sys/net/ipv4/tcp_wmem"; 56 57 struct IfaddrsDeleter { operatorIfaddrsDeleter58 void operator()(struct ifaddrs *p) const { 59 if (p != nullptr) { 60 freeifaddrs(p); 61 } 62 } 63 }; 64 65 typedef std::unique_ptr<struct ifaddrs, struct IfaddrsDeleter> ScopedIfaddrs; 66 67 namespace android::net { 68 69 /** 70 * This lock exists to make NetdNativeService RPCs (which come in on multiple Binder threads) 71 * coexist with the commands in CommandListener.cpp. These are presumed not thread-safe because 72 * CommandListener has only one user (NetworkManagementService), which is connected through a 73 * FrameworkListener that passes in commands one at a time. 74 */ 75 extern std::mutex gBigNetdLock; 76 77 } // namespace android::net 78