1 /*
2  * Copyright (C) 2023 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 package android.net;
18 
19 import android.content.Context;
20 import android.os.Build;
21 import android.os.RemoteException;
22 
23 import androidx.annotation.NonNull;
24 import androidx.annotation.RequiresApi;
25 
26 /**
27  * A manager class for talking to the routing coordinator service.
28  *
29  * This class should only be used by the connectivity and tethering module. This is enforced
30  * by the build rules. Do not change build rules to gain access to this class from elsewhere.
31  * @hide
32  */
33 @RequiresApi(Build.VERSION_CODES.S)
34 public class RoutingCoordinatorManager {
35     @NonNull final Context mContext;
36     @NonNull final IRoutingCoordinator mService;
37 
RoutingCoordinatorManager(@onNull final Context context, @NonNull final IRoutingCoordinator service)38     public RoutingCoordinatorManager(@NonNull final Context context,
39             @NonNull final IRoutingCoordinator service) {
40         mContext = context;
41         mService = service;
42     }
43 
44     /**
45      * Add a route for specific network
46      *
47      * @param netId the network to add the route to
48      * @param route the route to add
49      * @throws ServiceSpecificException in case of failure, with an error code indicating the
50      *         cause of the failure.
51      */
addRoute(final int netId, final RouteInfo route)52     public void addRoute(final int netId, final RouteInfo route) {
53         try {
54             mService.addRoute(netId, route);
55         } catch (RemoteException e) {
56             throw e.rethrowFromSystemServer();
57         }
58     }
59 
60     /**
61      * Remove a route for specific network
62      *
63      * @param netId the network to remove the route from
64      * @param route the route to remove
65      * @throws ServiceSpecificException in case of failure, with an error code indicating the
66      *         cause of the failure.
67      */
removeRoute(final int netId, final RouteInfo route)68     public void removeRoute(final int netId, final RouteInfo route) {
69         try {
70             mService.removeRoute(netId, route);
71         } catch (RemoteException e) {
72             throw e.rethrowFromSystemServer();
73         }
74     }
75 
76     /**
77      * Update a route for specific network
78      *
79      * @param netId the network to update the route for
80      * @param route parcelable with route information
81      * @throws ServiceSpecificException in case of failure, with an error code indicating the
82      *         cause of the failure.
83      */
updateRoute(final int netId, final RouteInfo route)84     public void updateRoute(final int netId, final RouteInfo route) {
85         try {
86             mService.updateRoute(netId, route);
87         } catch (RemoteException e) {
88             throw e.rethrowFromSystemServer();
89         }
90     }
91 
92     /**
93      * Adds an interface to a network. The interface must not be assigned to any network, including
94      * the specified network.
95      *
96      * @param netId the network to add the interface to.
97      * @param iface the name of the interface to add.
98      *
99      * @throws ServiceSpecificException in case of failure, with an error code corresponding to the
100      *         unix errno.
101      */
addInterfaceToNetwork(final int netId, final String iface)102     public void addInterfaceToNetwork(final int netId, final String iface) {
103         try {
104             mService.addInterfaceToNetwork(netId, iface);
105         } catch (RemoteException e) {
106             throw e.rethrowFromSystemServer();
107         }
108     }
109 
110     /**
111      * Removes an interface from a network. The interface must be assigned to the specified network.
112      *
113      * @param netId the network to remove the interface from.
114      * @param iface the name of the interface to remove.
115      *
116      * @throws ServiceSpecificException in case of failure, with an error code corresponding to the
117      *         unix errno.
118      */
removeInterfaceFromNetwork(final int netId, final String iface)119     public void removeInterfaceFromNetwork(final int netId, final String iface) {
120         try {
121             mService.removeInterfaceFromNetwork(netId, iface);
122         } catch (RemoteException e) {
123             throw e.rethrowFromSystemServer();
124         }
125     }
126 
127     /**
128      * Add forwarding ip rule
129      *
130      * @param fromIface interface name to add forwarding ip rule
131      * @param toIface interface name to add forwarding ip rule
132      * @throws ServiceSpecificException in case of failure, with an error code indicating the
133      *         cause of the failure.
134      */
addInterfaceForward(final String fromIface, final String toIface)135     public void addInterfaceForward(final String fromIface, final String toIface) {
136         try {
137             mService.addInterfaceForward(fromIface, toIface);
138         } catch (RemoteException e) {
139             throw e.rethrowFromSystemServer();
140         }
141     }
142 
143     /**
144      * Remove forwarding ip rule
145      *
146      * @param fromIface interface name to remove forwarding ip rule
147      * @param toIface interface name to remove forwarding ip rule
148      * @throws ServiceSpecificException in case of failure, with an error code indicating the
149      *         cause of the failure.
150      */
removeInterfaceForward(final String fromIface, final String toIface)151     public void removeInterfaceForward(final String fromIface, final String toIface) {
152         try {
153             mService.removeInterfaceForward(fromIface, toIface);
154         } catch (RemoteException e) {
155             throw e.rethrowFromSystemServer();
156         }
157     }
158 }
159