blob: 99e7a99dc673cfec78fc3fc1a1565a58544e91f6 [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:
Ken Chend9aa98a2021-05-23 14:56:43 +080031 static constexpr int DEFAULT_SUB_PRIORITY = 0;
32 static constexpr int LOWEST_SUB_PRIORITY = 999;
33
Lorenzo Colitti563d98b2016-04-24 13:13:14 +090034 UidRanges() {}
Luke Huang94658ac2018-10-18 19:35:12 +090035 UidRanges(const std::vector<android::net::UidRangeParcel>& ranges);
Lorenzo Colitti563d98b2016-04-24 13:13:14 +090036
Sreeram Ramachandrane09b20a2014-07-05 17:15:14 -070037 bool hasUid(uid_t uid) const;
Luke Huang94658ac2018-10-18 19:35:12 +090038 const std::vector<UidRangeParcel>& getRanges() const;
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -070039
40 bool parseFrom(int argc, char* argv[]);
Lorenzo Colittifff4bd32016-04-14 00:56:01 +090041 std::string toString() const;
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -070042
43 void add(const UidRanges& other);
44 void remove(const UidRanges& other);
45
Ken Chen868ae632021-02-24 17:50:08 +080046 // check if 'mRanges' has uid overlap between elements.
47 bool overlapsSelf() const;
48 // check if this object has uid overlap with the input object.
49 bool overlaps(const UidRanges& other) const;
Ken Chend9aa98a2021-05-23 14:56:43 +080050 bool empty() const { return mRanges.empty(); }
Ken Chen868ae632021-02-24 17:50:08 +080051
Luke Huang7720e4a2019-02-20 15:09:28 +080052 private:
Ken Chen868ae632021-02-24 17:50:08 +080053 // a utility to check if two UidRangeParcels have uid overlap.
54 bool isOverlapped(const UidRangeParcel& r1, const UidRangeParcel& r2) const;
55
Luke Huang7720e4a2019-02-20 15:09:28 +080056 std::vector<UidRangeParcel> mRanges;
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -070057};
58
Lorenzo Colitti7035f222017-02-13 18:29:00 +090059} // namespace net
60} // namespace android
61
Sreeram Ramachandranb1425cc2014-06-23 18:54:27 -070062#endif // NETD_SERVER_UID_RANGES_H