1 /*
2 * Copyright (C) 2018 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 //#define LOG_NDEBUG 0
18 #define LOG_TAG "NetworkUtils"
19 #include <utils/Log.h>
20
21 #include "NetworkUtils.h"
22 #include <cutils/qtaguid.h>
23 #include <NetdClient.h>
24
25 namespace android {
26
27 // static
RegisterSocketUserTag(int sockfd,uid_t uid,uint32_t kTag)28 void NetworkUtils::RegisterSocketUserTag(int sockfd, uid_t uid, uint32_t kTag) {
29 int res = qtaguid_tagSocket(sockfd, kTag, uid);
30 if (res != 0) {
31 ALOGE("Failed tagging socket %d for uid %d (My UID=%d)", sockfd, uid, geteuid());
32 }
33 }
34
35 // static
UnRegisterSocketUserTag(int sockfd)36 void NetworkUtils::UnRegisterSocketUserTag(int sockfd) {
37 int res = qtaguid_untagSocket(sockfd);
38 if (res != 0) {
39 ALOGE("Failed untagging socket %d (My UID=%d)", sockfd, geteuid());
40 }
41 }
42
43 // static
RegisterSocketUserMark(int sockfd,uid_t uid)44 void NetworkUtils::RegisterSocketUserMark(int sockfd, uid_t uid) {
45 setNetworkForUser(uid, sockfd);
46 }
47
48 // static
UnRegisterSocketUserMark(int sockfd)49 void NetworkUtils::UnRegisterSocketUserMark(int sockfd) {
50 RegisterSocketUserMark(sockfd, geteuid());
51 }
52
53 } // namespace android
54