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 #ifndef NETD_INCLUDE_PERMISSION_H
18 #define NETD_INCLUDE_PERMISSION_H
19 
20 // This enum represents the permissions we care about for networking. When applied to an app, it's
21 // the permission the app (UID) has been granted. When applied to a network, it's the permission an
22 // app must hold to be allowed to use the network. PERMISSION_NONE means "no special permission is
23 // held by the app" or "no special permission is required to use the network".
24 //
25 // Permissions are flags that can be OR'ed together to represent combinations of permissions.
26 //
27 // PERMISSION_NONE is used for regular networks and apps, such as those that hold the
28 // android.permission.INTERNET framework permission.
29 //
30 // PERMISSION_NETWORK is used for privileged networks and apps that can manipulate or access them,
31 // such as those that hold the android.permission.CHANGE_NETWORK_STATE framework permission.
32 //
33 // PERMISSION_SYSTEM is used for system apps, such as those that are installed on the system
34 // partition, those that hold the android.permission.CONNECTIVITY_INTERNAL framework permission and
35 // those whose UID is less than FIRST_APPLICATION_UID.
36 enum Permission {
37     PERMISSION_NONE    = 0x0,
38     PERMISSION_NETWORK = 0x1,
39     PERMISSION_SYSTEM  = 0x3,  // Includes PERMISSION_NETWORK.
40 };
41 
permissionToName(Permission permission)42 inline const char *permissionToName(Permission permission) {
43     switch (permission) {
44         case PERMISSION_NONE:    return "NONE";
45         case PERMISSION_NETWORK: return "NETWORK";
46         case PERMISSION_SYSTEM:  return "SYSTEM";
47         // No default statement. We want to see errors of the form:
48         // "enumeration value 'PERMISSION_SYSTEM' not handled in switch [-Werror,-Wswitch]".
49     }
50 }
51 
stringToPermission(const char * arg)52 inline Permission stringToPermission(const char* arg) {
53     if (!strcmp(arg, "NETWORK")) {
54         return PERMISSION_NETWORK;
55     }
56     if (!strcmp(arg, "SYSTEM")) {
57         return PERMISSION_SYSTEM;
58     }
59     return PERMISSION_NONE;
60 }
61 
62 #endif  // NETD_INCLUDE_PERMISSION_H
63