• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef _NETD_CONSTANTS_H
18 #define _NETD_CONSTANTS_H
19 
20 #include <ifaddrs.h>
21 #include <netdb.h>
22 #include <stddef.h>
23 #include <stdint.h>
24 
25 #include <mutex>
26 #include <string>
27 
28 #include <netdutils/UidConstants.h>
29 #include <private/android_filesystem_config.h>
30 
31 // Referred from SHA256_DIGEST_LENGTH in boringssl
32 constexpr size_t SHA256_SIZE = 32;
33 
34 enum IptablesTarget { V4, V6, V4V6 };
35 
36 int execIptablesRestore(IptablesTarget target, const std::string& commands);
37 int execIptablesRestoreWithOutput(IptablesTarget target, const std::string& commands,
38                                   std::string *output);
39 int execIptablesRestoreCommand(IptablesTarget target, const std::string& table,
40                                const std::string& command, std::string *output);
41 bool isIfaceName(const std::string& name);
42 int parsePrefix(const char *prefix, uint8_t *family, void *address, int size, uint8_t *prefixlen);
43 void blockSigpipe();
44 void setCloseOnExec(const char *sock);
45 
46 // TODO: use std::size() instead.
47 #define ARRAY_SIZE(a) (sizeof(a) / sizeof(*(a)))
48 
49 #define __INT_STRLEN(i) sizeof(#i)
50 #define _INT_STRLEN(i) __INT_STRLEN(i)
51 #define INT32_STRLEN _INT_STRLEN(INT32_MIN)
52 #define UINT32_STRLEN _INT_STRLEN(UINT32_MAX)
53 #define UINT32_HEX_STRLEN sizeof("0x12345678")
54 #define IPSEC_IFACE_PREFIX "ipsec"
55 
56 #define WARN_UNUSED_RESULT __attribute__((__warn_unused_result__))
57 
58 const uid_t INVALID_UID = static_cast<uid_t>(-1);
59 
60 constexpr char TCP_RMEM_PROC_FILE[] = "/proc/sys/net/ipv4/tcp_rmem";
61 constexpr char TCP_WMEM_PROC_FILE[] = "/proc/sys/net/ipv4/tcp_wmem";
62 
63 struct AddrinfoDeleter {
operatorAddrinfoDeleter64     void operator()(struct addrinfo* p) const {
65         if (p != nullptr) {
66             freeaddrinfo(p);
67         }
68     }
69 };
70 
71 typedef std::unique_ptr<struct addrinfo, struct AddrinfoDeleter> ScopedAddrinfo;
72 
73 
74 struct IfaddrsDeleter {
operatorIfaddrsDeleter75     void operator()(struct ifaddrs *p) const {
76         if (p != nullptr) {
77             freeifaddrs(p);
78         }
79     }
80 };
81 
82 typedef std::unique_ptr<struct ifaddrs, struct IfaddrsDeleter> ScopedIfaddrs;
83 
84 namespace android {
85 namespace net {
86 
87 /**
88  * This lock exists to make NetdNativeService RPCs (which come in on multiple Binder threads)
89  * coexist with the commands in CommandListener.cpp. These are presumed not thread-safe because
90  * CommandListener has only one user (NetworkManagementService), which is connected through a
91  * FrameworkListener that passes in commands one at a time.
92  */
93 extern std::mutex gBigNetdLock;
94 
95 }  // namespace net
96 }  // namespace android
97 
98 #endif  // _NETD_CONSTANTS_H
99