This repository has been archived by the owner on Nov 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
NetworkController.cpp
120 lines (103 loc) · 3.72 KB
/
NetworkController.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
* 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 <resolv_netid.h>
#define LOG_TAG "NetworkController"
#include <cutils/log.h>
#include "NetworkController.h"
// Mark 1 is reserved for SecondaryTableController::PROTECT_MARK.
NetworkController::NetworkController()
: mDefaultNetId(NETID_UNSET),
mNextFreeNetId(10) {}
void NetworkController::clearNetworkPreference() {
android::RWLock::AutoWLock lock(mRWLock);
mUidMap.clear();
mPidMap.clear();
}
unsigned NetworkController::getDefaultNetwork() const {
return mDefaultNetId;
}
void NetworkController::setDefaultNetwork(unsigned netId) {
android::RWLock::AutoWLock lock(mRWLock);
mDefaultNetId = netId;
}
void NetworkController::setNetworkForPid(int pid, unsigned netId) {
android::RWLock::AutoWLock lock(mRWLock);
if (netId == 0) {
mPidMap.erase(pid);
} else {
mPidMap[pid] = netId;
}
}
bool NetworkController::setNetworkForUidRange(int uid_start, int uid_end, unsigned netId,
bool forward_dns) {
android::RWLock::AutoWLock lock(mRWLock);
if (uid_start > uid_end || netId == NETID_UNSET)
return false;
for (std::list<UidEntry>::iterator it = mUidMap.begin(); it != mUidMap.end(); ++it) {
if (it->uid_start != uid_start || it->uid_end != uid_end || it->netId != netId)
continue;
it->forward_dns = forward_dns;
return true;
}
mUidMap.push_front(UidEntry(uid_start, uid_end, netId, forward_dns));
return true;
}
bool NetworkController::clearNetworkForUidRange(int uid_start, int uid_end, unsigned netId) {
android::RWLock::AutoWLock lock(mRWLock);
if (uid_start > uid_end || netId == NETID_UNSET)
return false;
for (std::list<UidEntry>::iterator it = mUidMap.begin(); it != mUidMap.end(); ++it) {
if (it->uid_start != uid_start || it->uid_end != uid_end || it->netId != netId)
continue;
mUidMap.erase(it);
return true;
}
return false;
}
unsigned NetworkController::getNetwork(int uid, unsigned requested_netId, int pid,
bool for_dns) const {
android::RWLock::AutoRLock lock(mRWLock);
for (std::list<UidEntry>::const_iterator it = mUidMap.begin(); it != mUidMap.end(); ++it) {
if (uid < it->uid_start || it->uid_end < uid)
continue;
if (for_dns && !it->forward_dns)
break;
return it->netId;
}
if (requested_netId != NETID_UNSET)
return requested_netId;
if (pid != PID_UNSPECIFIED) {
std::map<int, unsigned>::const_iterator it = mPidMap.find(pid);
if (it != mPidMap.end())
return it->second;
}
return mDefaultNetId;
}
unsigned NetworkController::getNetworkId(const char* interface) {
std::map<std::string, unsigned>::const_iterator it = mIfaceNetidMap.find(interface);
if (it != mIfaceNetidMap.end())
return it->second;
unsigned netId = mNextFreeNetId++;
mIfaceNetidMap[interface] = netId;
return netId;
}
NetworkController::UidEntry::UidEntry(
int start, int end, unsigned netId, bool forward_dns)
: uid_start(start),
uid_end(end),
netId(netId),
forward_dns(forward_dns) {
}