blob: 9123eb1711682702df0623bc7d8642bb2d864895 [file] [log] [blame]
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -07001/*
2 * Copyright (C) 2014 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_SERVER_UID_RANGES_H
18#define NETD_SERVER_UID_RANGES_H
19
Luke Huang94658ac2018-10-18 19:35:12 +090020#include "android/net/INetd.h"
Robin Lee9f9aae92016-03-30 18:33:07 +010021
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -070022#include <sys/types.h>
23#include <utility>
24#include <vector>
25
Lorenzo Colitti7035f222017-02-13 18:29:00 +090026namespace android {
27namespace net {
28
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -070029class UidRanges {
30public:
Patrick Rohre2f1b5a2022-01-25 21:36:50 +010031 static constexpr int SUB_PRIORITY_HIGHEST = 0;
Patrick Rohre6f198c2022-01-25 13:50:31 +010032 static constexpr int SUB_PRIORITY_LOWEST = 998;
33 // "Special" value used for giving UIDs access to a network (by installing explicit and implicit
34 // network rules) without automatically making that network default. This rule works in
35 // conjunction with the other subpriorities, meaning that the network could still be configured
36 // as the app's default using one of the lower values (0-998).
37 // Note: SUB_PRIORITY_NO_DEFAULT *must* be SUB_PRIORITY_LOWEST + 1!
38 static constexpr int SUB_PRIORITY_NO_DEFAULT = 999;
Ken Chen4ea88462021-05-23 14:56:43 +080039
Lorenzo Colitti563d98b2016-04-24 13:13:14 +090040 UidRanges() {}
Luke Huang94658ac2018-10-18 19:35:12 +090041 UidRanges(const std::vector<android::net::UidRangeParcel>& ranges);
Lorenzo Colitti563d98b2016-04-24 13:13:14 +090042
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -070043 bool hasUid(uid_t uid) const;
Luke Huang94658ac2018-10-18 19:35:12 +090044 const std::vector<UidRangeParcel>& getRanges() const;
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -070045
46 bool parseFrom(int argc, char* argv[]);
Lorenzo Colittifff4bd32016-04-14 00:56:01 +090047 std::string toString() const;
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -070048
49 void add(const UidRanges& other);
50 void remove(const UidRanges& other);
51
Ken Chen868ae632021-02-24 17:50:08 +080052 // check if 'mRanges' has uid overlap between elements.
53 bool overlapsSelf() const;
54 // check if this object has uid overlap with the input object.
55 bool overlaps(const UidRanges& other) const;
Ken Chen4ea88462021-05-23 14:56:43 +080056 bool empty() const { return mRanges.empty(); }
Ken Chen868ae632021-02-24 17:50:08 +080057
Luke Huang7720e4a2019-02-20 15:09:28 +080058 private:
Ken Chen7006bec2021-07-09 23:50:37 +080059 // Keep it sorted. The overlaps() implements binary search, which requires a sorted data.
Luke Huang7720e4a2019-02-20 15:09:28 +080060 std::vector<UidRangeParcel> mRanges;
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -070061};
62
Lorenzo Colitti7035f222017-02-13 18:29:00 +090063} // namespace net
64} // namespace android
65
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -070066#endif // NETD_SERVER_UID_RANGES_H