Sreeram Ramachandran | b1425cc | 2014-06-23 18:54:27 -0700 | [diff] [blame] | 1 | /* |
| 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 | #include "UidRanges.h" |
| 18 | |
| 19 | #include "NetdConstants.h" |
| 20 | |
Robin Lee | dc0d578 | 2016-07-20 14:17:11 +0100 | [diff] [blame^] | 21 | #include <inttypes.h> |
| 22 | #include <limits.h> |
Sreeram Ramachandran | b1425cc | 2014-06-23 18:54:27 -0700 | [diff] [blame] | 23 | #include <stdlib.h> |
| 24 | |
Lorenzo Colitti | fff4bd3 | 2016-04-14 00:56:01 +0900 | [diff] [blame] | 25 | #include <android-base/stringprintf.h> |
Robin Lee | dc0d578 | 2016-07-20 14:17:11 +0100 | [diff] [blame^] | 26 | #include <log/log.h> |
Lorenzo Colitti | fff4bd3 | 2016-04-14 00:56:01 +0900 | [diff] [blame] | 27 | |
| 28 | using android::base::StringAppendF; |
Robin Lee | dc0d578 | 2016-07-20 14:17:11 +0100 | [diff] [blame^] | 29 | using android::net::UidRange; |
Lorenzo Colitti | fff4bd3 | 2016-04-14 00:56:01 +0900 | [diff] [blame] | 30 | |
Sreeram Ramachandran | e09b20a | 2014-07-05 17:15:14 -0700 | [diff] [blame] | 31 | bool UidRanges::hasUid(uid_t uid) const { |
Robin Lee | dc0d578 | 2016-07-20 14:17:11 +0100 | [diff] [blame^] | 32 | if (uid > (unsigned) INT32_MAX) { |
| 33 | ALOGW("UID larger than 32 bits: %" PRIu64, static_cast<uint64_t>(uid)); |
| 34 | return false; |
| 35 | } |
| 36 | const int32_t intUid = static_cast<int32_t>(uid); |
| 37 | |
| 38 | auto iter = std::lower_bound(mRanges.begin(), mRanges.end(), UidRange(intUid, intUid)); |
| 39 | return (iter != mRanges.end() && iter->getStart() == intUid) || |
| 40 | (iter != mRanges.begin() && (--iter)->getStop() >= intUid); |
Sreeram Ramachandran | e09b20a | 2014-07-05 17:15:14 -0700 | [diff] [blame] | 41 | } |
| 42 | |
Robin Lee | dc0d578 | 2016-07-20 14:17:11 +0100 | [diff] [blame^] | 43 | const std::vector<UidRange>& UidRanges::getRanges() const { |
Sreeram Ramachandran | b1425cc | 2014-06-23 18:54:27 -0700 | [diff] [blame] | 44 | return mRanges; |
| 45 | } |
| 46 | |
| 47 | bool UidRanges::parseFrom(int argc, char* argv[]) { |
| 48 | mRanges.clear(); |
| 49 | for (int i = 0; i < argc; ++i) { |
| 50 | if (!*argv[i]) { |
| 51 | // The UID string is empty. |
| 52 | return false; |
| 53 | } |
| 54 | char* endPtr; |
| 55 | uid_t uidStart = strtoul(argv[i], &endPtr, 0); |
| 56 | uid_t uidEnd; |
| 57 | if (!*endPtr) { |
| 58 | // Found a single UID. The range contains just the one UID. |
| 59 | uidEnd = uidStart; |
| 60 | } else if (*endPtr == '-') { |
| 61 | if (!*++endPtr) { |
| 62 | // Unexpected end of string. |
| 63 | return false; |
| 64 | } |
| 65 | uidEnd = strtoul(endPtr, &endPtr, 0); |
| 66 | if (*endPtr) { |
| 67 | // Illegal trailing chars. |
| 68 | return false; |
| 69 | } |
| 70 | if (uidEnd < uidStart) { |
| 71 | // Invalid order. |
| 72 | return false; |
| 73 | } |
| 74 | } else { |
| 75 | // Not a single uid, not a range. Found some other illegal char. |
| 76 | return false; |
| 77 | } |
| 78 | if (uidStart == INVALID_UID || uidEnd == INVALID_UID) { |
| 79 | // Invalid UIDs. |
| 80 | return false; |
| 81 | } |
Robin Lee | dc0d578 | 2016-07-20 14:17:11 +0100 | [diff] [blame^] | 82 | mRanges.push_back(UidRange(uidStart, uidEnd)); |
Sreeram Ramachandran | b1425cc | 2014-06-23 18:54:27 -0700 | [diff] [blame] | 83 | } |
| 84 | std::sort(mRanges.begin(), mRanges.end()); |
| 85 | return true; |
| 86 | } |
| 87 | |
Robin Lee | dc0d578 | 2016-07-20 14:17:11 +0100 | [diff] [blame^] | 88 | UidRanges::UidRanges(const std::vector<UidRange>& ranges) { |
| 89 | mRanges = ranges; |
Robin Lee | b808736 | 2016-03-30 18:43:08 +0100 | [diff] [blame] | 90 | std::sort(mRanges.begin(), mRanges.end()); |
| 91 | } |
| 92 | |
Sreeram Ramachandran | b1425cc | 2014-06-23 18:54:27 -0700 | [diff] [blame] | 93 | void UidRanges::add(const UidRanges& other) { |
| 94 | auto middle = mRanges.insert(mRanges.end(), other.mRanges.begin(), other.mRanges.end()); |
| 95 | std::inplace_merge(mRanges.begin(), middle, mRanges.end()); |
| 96 | } |
| 97 | |
| 98 | void UidRanges::remove(const UidRanges& other) { |
| 99 | auto end = std::set_difference(mRanges.begin(), mRanges.end(), other.mRanges.begin(), |
| 100 | other.mRanges.end(), mRanges.begin()); |
| 101 | mRanges.erase(end, mRanges.end()); |
| 102 | } |
Lorenzo Colitti | fff4bd3 | 2016-04-14 00:56:01 +0900 | [diff] [blame] | 103 | |
| 104 | std::string UidRanges::toString() const { |
| 105 | std::string s("UidRanges{ "); |
Robin Lee | dc0d578 | 2016-07-20 14:17:11 +0100 | [diff] [blame^] | 106 | for (const auto &range : mRanges) { |
| 107 | if (range.length() == 0) { |
| 108 | StringAppendF(&s, "<BAD: %u-%u> ", range.getStart(), range.getStop()); |
| 109 | } else if (range.length() == 1) { |
| 110 | StringAppendF(&s, "%u ", range.getStart()); |
Lorenzo Colitti | fff4bd3 | 2016-04-14 00:56:01 +0900 | [diff] [blame] | 111 | } else { |
Robin Lee | dc0d578 | 2016-07-20 14:17:11 +0100 | [diff] [blame^] | 112 | StringAppendF(&s, "%u-%u ", range.getStart(), range.getStop()); |
Lorenzo Colitti | fff4bd3 | 2016-04-14 00:56:01 +0900 | [diff] [blame] | 113 | } |
| 114 | } |
| 115 | StringAppendF(&s, "}"); |
| 116 | return s; |
| 117 | } |