Chad Brubaker | d261793 | 2013-06-21 15:26:35 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 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 "UidMarkMap.h" |
| 18 | |
| 19 | UidMarkMap::UidMarkEntry::UidMarkEntry(int start, int end, int new_mark) : |
| 20 | uid_start(start), |
| 21 | uid_end(end), |
| 22 | mark(new_mark) { |
| 23 | }; |
| 24 | |
| 25 | bool UidMarkMap::add(int uid_start, int uid_end, int mark) { |
| 26 | android::RWLock::AutoWLock lock(mRWLock); |
| 27 | if (uid_start > uid_end) { |
| 28 | return false; |
| 29 | } |
| 30 | android::netd::List<UidMarkEntry*>::iterator it; |
| 31 | for (it = mMap.begin(); it != mMap.end(); it++) { |
| 32 | UidMarkEntry *entry = *it; |
| 33 | if (entry->uid_start <= uid_end && uid_start <= entry->uid_end) { |
| 34 | return false; |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | UidMarkEntry *e = new UidMarkEntry(uid_start, uid_end, mark); |
| 39 | mMap.push_back(e); |
| 40 | return true; |
| 41 | }; |
| 42 | |
| 43 | bool UidMarkMap::remove(int uid_start, int uid_end, int mark) { |
| 44 | android::RWLock::AutoWLock lock(mRWLock); |
| 45 | android::netd::List<UidMarkEntry*>::iterator it; |
| 46 | for (it = mMap.begin(); it != mMap.end(); it++) { |
| 47 | UidMarkEntry *entry = *it; |
| 48 | if (entry->uid_start == uid_start && entry->uid_end == uid_end && entry->mark == mark) { |
| 49 | mMap.erase(it); |
| 50 | delete entry; |
| 51 | return true; |
| 52 | } |
| 53 | } |
| 54 | return false; |
| 55 | }; |
| 56 | |
| 57 | int UidMarkMap::getMark(int uid) { |
| 58 | android::RWLock::AutoRLock lock(mRWLock); |
| 59 | android::netd::List<UidMarkEntry*>::iterator it; |
| 60 | for (it = mMap.begin(); it != mMap.end(); it++) { |
| 61 | UidMarkEntry *entry = *it; |
| 62 | if (entry->uid_start <= uid && entry->uid_end >= uid) { |
| 63 | return entry->mark; |
| 64 | } |
| 65 | } |
| 66 | return -1; |
| 67 | }; |
Chad Brubaker | 2251c0f | 2013-06-27 17:20:39 -0700 | [diff] [blame] | 68 | |
| 69 | bool UidMarkMap::anyRulesForMark(int mark) { |
| 70 | android::RWLock::AutoRLock lock(mRWLock); |
| 71 | android::netd::List<UidMarkEntry*>::iterator it; |
| 72 | for (it = mMap.begin(); it != mMap.end(); it++) { |
| 73 | UidMarkEntry *entry = *it; |
| 74 | if (entry->mark == mark) { |
| 75 | return true; |
| 76 | } |
| 77 | } |
| 78 | return false; |
| 79 | } |