blob: ca581e3049299f5a98417f8bd9e806e513afe227 [file] [log] [blame]
Robert Greenwaltc4621772012-01-31 12:46:45 -08001/*
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
Erik Klinecc4f2732016-08-03 11:24:27 +090020#include <ifaddrs.h>
21#include <netdb.h>
Bernie Innocentia0381392018-08-15 04:33:45 +090022#include <stddef.h>
23#include <stdint.h>
Lorenzo Colitti699aa992016-04-15 10:22:37 +090024
Bernie Innocentia0381392018-08-15 04:33:45 +090025#include <mutex>
26#include <string>
Lorenzo Colitti699aa992016-04-15 10:22:37 +090027
Felipe Leme5ebbbd82016-03-07 09:25:50 -080028#include <private/android_filesystem_config.h>
Robert Greenwaltc4621772012-01-31 12:46:45 -080029
Felipe Leme5ebbbd82016-03-07 09:25:50 -080030const int MAX_SYSTEM_UID = AID_APP - 1;
Chad Brubaker2a390122014-02-19 17:51:05 -080031
Ben Schwartze7601812017-04-28 16:38:29 -040032extern const size_t SHA256_SIZE;
33
Jeff Sharkey8e188ed2012-07-12 18:32:03 -070034enum IptablesTarget { V4, V6, V4V6 };
35
Lorenzo Colitti89faa342016-02-26 11:38:47 +090036int execIptablesRestore(IptablesTarget target, const std::string& commands);
Lorenzo Colitticd283772017-01-31 19:00:49 +090037int execIptablesRestoreWithOutput(IptablesTarget target, const std::string& commands,
38 std::string *output);
Lorenzo Colittic1306ea2017-03-27 05:52:31 +090039int execIptablesRestoreCommand(IptablesTarget target, const std::string& table,
40 const std::string& command, std::string *output);
Joel Scherpelzbcad6612017-05-30 10:55:11 +090041bool isIfaceName(const std::string& name);
Lorenzo Colittiba25df92014-06-18 00:22:17 +090042int parsePrefix(const char *prefix, uint8_t *family, void *address, int size, uint8_t *prefixlen);
Lorenzo Colitti839d7d62017-04-03 15:37:19 +090043void blockSigpipe();
Lorenzo Colitti548bbd42017-08-28 23:05:12 +090044void setCloseOnExec(const char *sock);
Jeff Sharkey8e188ed2012-07-12 18:32:03 -070045
Rom Lemarchand001f0a42013-01-31 12:41:03 -080046#define ARRAY_SIZE(a) (sizeof(a) / sizeof(*(a)))
47
Lorenzo Colittia10ac322014-04-11 18:26:17 +090048#define __INT_STRLEN(i) sizeof(#i)
49#define _INT_STRLEN(i) __INT_STRLEN(i)
Lorenzo Colitti0a3eb852016-02-23 16:59:21 +090050#define INT32_STRLEN _INT_STRLEN(INT32_MIN)
Lorenzo Colittia10ac322014-04-11 18:26:17 +090051#define UINT32_STRLEN _INT_STRLEN(UINT32_MAX)
52#define UINT32_HEX_STRLEN sizeof("0x12345678")
Benedict Wongb9baf262017-12-03 15:43:08 -080053#define IPSEC_IFACE_PREFIX "ipsec"
Lorenzo Colittia10ac322014-04-11 18:26:17 +090054
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070055#define WARN_UNUSED_RESULT __attribute__((__warn_unused_result__))
56
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -070057const uid_t INVALID_UID = static_cast<uid_t>(-1);
58
Erik Klinecc4f2732016-08-03 11:24:27 +090059
60struct AddrinfoDeleter {
61 void operator()(struct addrinfo* p) const {
62 if (p != nullptr) {
63 freeaddrinfo(p);
64 }
65 }
66};
67
68typedef std::unique_ptr<struct addrinfo, struct AddrinfoDeleter> ScopedAddrinfo;
69
70
71struct IfaddrsDeleter {
72 void operator()(struct ifaddrs *p) const {
73 if (p != nullptr) {
74 freeifaddrs(p);
75 }
76 }
77};
78
79typedef std::unique_ptr<struct ifaddrs, struct IfaddrsDeleter> ScopedIfaddrs;
80
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090081namespace android {
82namespace net {
83
84/**
85 * This lock exists to make NetdNativeService RPCs (which come in on multiple Binder threads)
86 * coexist with the commands in CommandListener.cpp. These are presumed not thread-safe because
87 * CommandListener has only one user (NetworkManagementService), which is connected through a
88 * FrameworkListener that passes in commands one at a time.
89 */
Luke Huangd1ee4622018-06-29 13:49:58 +080090extern std::mutex gBigNetdLock;
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090091
92} // namespace net
93} // namespace android
94
95#endif // _NETD_CONSTANTS_H