blob: 4bb261e7ca58ac9f6fb852c5e57a01ff4adace28 [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
Jeff Sharkey8e188ed2012-07-12 18:32:03 -070020#include <string>
21#include <list>
Erik Klinecc4f2732016-08-03 11:24:27 +090022#include <ifaddrs.h>
23#include <netdb.h>
Jeff Sharkey8e188ed2012-07-12 18:32:03 -070024#include <stdarg.h>
Lorenzo Colitti699aa992016-04-15 10:22:37 +090025
26#include <chrono>
27
Felipe Leme5ebbbd82016-03-07 09:25:50 -080028#include <private/android_filesystem_config.h>
Robert Greenwaltc4621772012-01-31 12:46:45 -080029
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090030#include "utils/RWLock.h"
31
Chad Brubaker2a390122014-02-19 17:51:05 -080032const int PROTECT_MARK = 0x1;
Felipe Leme5ebbbd82016-03-07 09:25:50 -080033const int MAX_SYSTEM_UID = AID_APP - 1;
Chad Brubaker2a390122014-02-19 17:51:05 -080034
Robert Greenwaltc4621772012-01-31 12:46:45 -080035extern const char * const IPTABLES_PATH;
JP Abgrall0031cea2012-04-17 16:38:23 -070036extern const char * const IP6TABLES_PATH;
Robert Greenwaltc4621772012-01-31 12:46:45 -080037extern const char * const IP_PATH;
38extern const char * const TC_PATH;
39extern const char * const OEM_SCRIPT_PATH;
40extern const char * const ADD;
41extern const char * const DEL;
42
Jeff Sharkey8e188ed2012-07-12 18:32:03 -070043enum IptablesTarget { V4, V6, V4V6 };
44
45int execIptables(IptablesTarget target, ...);
46int execIptablesSilently(IptablesTarget target, ...);
Lorenzo Colitti89faa342016-02-26 11:38:47 +090047int execIptablesRestore(IptablesTarget target, const std::string& commands);
Lorenzo Colitticd283772017-01-31 19:00:49 +090048int execIptablesRestoreWithOutput(IptablesTarget target, const std::string& commands,
49 std::string *output);
Lorenzo Colittic1306ea2017-03-27 05:52:31 +090050int execIptablesRestoreCommand(IptablesTarget target, const std::string& table,
51 const std::string& command, std::string *output);
JP Abgrall69261cb2014-06-19 18:35:24 -070052bool isIfaceName(const char *name);
Lorenzo Colittiba25df92014-06-18 00:22:17 +090053int parsePrefix(const char *prefix, uint8_t *family, void *address, int size, uint8_t *prefixlen);
Lorenzo Colitti839d7d62017-04-03 15:37:19 +090054void blockSigpipe();
Jeff Sharkey8e188ed2012-07-12 18:32:03 -070055
Rom Lemarchand001f0a42013-01-31 12:41:03 -080056#define ARRAY_SIZE(a) (sizeof(a) / sizeof(*(a)))
57
Lorenzo Colittia10ac322014-04-11 18:26:17 +090058#define __INT_STRLEN(i) sizeof(#i)
59#define _INT_STRLEN(i) __INT_STRLEN(i)
Lorenzo Colitti0a3eb852016-02-23 16:59:21 +090060#define INT32_STRLEN _INT_STRLEN(INT32_MIN)
Lorenzo Colittia10ac322014-04-11 18:26:17 +090061#define UINT32_STRLEN _INT_STRLEN(UINT32_MAX)
62#define UINT32_HEX_STRLEN sizeof("0x12345678")
63
Sreeram Ramachandranf4f6c8d2014-06-23 09:54:06 -070064#define WARN_UNUSED_RESULT __attribute__((__warn_unused_result__))
65
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -070066const uid_t INVALID_UID = static_cast<uid_t>(-1);
67
Erik Klinecc4f2732016-08-03 11:24:27 +090068
69struct AddrinfoDeleter {
70 void operator()(struct addrinfo* p) const {
71 if (p != nullptr) {
72 freeaddrinfo(p);
73 }
74 }
75};
76
77typedef std::unique_ptr<struct addrinfo, struct AddrinfoDeleter> ScopedAddrinfo;
78
79
80struct IfaddrsDeleter {
81 void operator()(struct ifaddrs *p) const {
82 if (p != nullptr) {
83 freeifaddrs(p);
84 }
85 }
86};
87
88typedef std::unique_ptr<struct ifaddrs, struct IfaddrsDeleter> ScopedIfaddrs;
89
Lorenzo Colittie4d626e2016-02-02 17:19:04 +090090namespace android {
91namespace net {
92
93/**
94 * This lock exists to make NetdNativeService RPCs (which come in on multiple Binder threads)
95 * coexist with the commands in CommandListener.cpp. These are presumed not thread-safe because
96 * CommandListener has only one user (NetworkManagementService), which is connected through a
97 * FrameworkListener that passes in commands one at a time.
98 */
99extern android::RWLock gBigNetdLock;
100
101} // namespace net
102} // namespace android
103
104#endif // _NETD_CONSTANTS_H