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