Remi NGUYEN VAN | c094a54 | 2018-12-07 16:52:24 +0900 | [diff] [blame] | 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 | package com.android.server; |
| 18 | |
Remi NGUYEN VAN | 0e3d0923 | 2018-12-04 12:13:09 +0900 | [diff] [blame] | 19 | import static android.net.dhcp.IDhcpServer.STATUS_INVALID_ARGUMENT; |
| 20 | import static android.net.dhcp.IDhcpServer.STATUS_SUCCESS; |
| 21 | import static android.net.dhcp.IDhcpServer.STATUS_UNKNOWN_ERROR; |
| 22 | |
| 23 | import static com.android.server.util.PermissionUtil.checkNetworkStackCallingPermission; |
Remi NGUYEN VAN | c094a54 | 2018-12-07 16:52:24 +0900 | [diff] [blame] | 24 | |
| 25 | import android.annotation.NonNull; |
| 26 | import android.annotation.Nullable; |
| 27 | import android.app.Service; |
| 28 | import android.content.Intent; |
| 29 | import android.net.INetworkStackConnector; |
Remi NGUYEN VAN | 0e3d0923 | 2018-12-04 12:13:09 +0900 | [diff] [blame] | 30 | import android.net.dhcp.DhcpServer; |
| 31 | import android.net.dhcp.DhcpServingParams; |
| 32 | import android.net.dhcp.DhcpServingParamsParcel; |
| 33 | import android.net.dhcp.IDhcpServerCallbacks; |
| 34 | import android.net.util.SharedLog; |
Remi NGUYEN VAN | c094a54 | 2018-12-07 16:52:24 +0900 | [diff] [blame] | 35 | import android.os.IBinder; |
Remi NGUYEN VAN | 0e3d0923 | 2018-12-04 12:13:09 +0900 | [diff] [blame] | 36 | import android.os.RemoteException; |
Remi NGUYEN VAN | c094a54 | 2018-12-07 16:52:24 +0900 | [diff] [blame] | 37 | |
| 38 | import java.io.FileDescriptor; |
| 39 | import java.io.PrintWriter; |
| 40 | |
| 41 | /** |
| 42 | * Android service used to start the network stack when bound to via an intent. |
| 43 | * |
| 44 | * <p>The service returns a binder for the system server to communicate with the network stack. |
| 45 | */ |
| 46 | public class NetworkStackService extends Service { |
| 47 | private static final String TAG = NetworkStackService.class.getSimpleName(); |
| 48 | |
| 49 | /** |
| 50 | * Create a binder connector for the system server to communicate with the network stack. |
| 51 | * |
| 52 | * <p>On platforms where the network stack runs in the system server process, this method may |
| 53 | * be called directly instead of obtaining the connector by binding to the service. |
| 54 | */ |
| 55 | public static IBinder makeConnector() { |
| 56 | return new NetworkStackConnector(); |
| 57 | } |
| 58 | |
| 59 | @NonNull |
| 60 | @Override |
| 61 | public IBinder onBind(Intent intent) { |
| 62 | return makeConnector(); |
| 63 | } |
| 64 | |
| 65 | private static class NetworkStackConnector extends INetworkStackConnector.Stub { |
Remi NGUYEN VAN | 0e3d0923 | 2018-12-04 12:13:09 +0900 | [diff] [blame] | 66 | @NonNull |
| 67 | private final SharedLog mLog = new SharedLog(TAG); |
| 68 | |
| 69 | @Override |
| 70 | public void makeDhcpServer(@NonNull String ifName, @NonNull DhcpServingParamsParcel params, |
| 71 | @NonNull IDhcpServerCallbacks cb) throws RemoteException { |
| 72 | checkNetworkStackCallingPermission(); |
| 73 | final DhcpServer server; |
| 74 | try { |
| 75 | server = new DhcpServer( |
| 76 | ifName, |
| 77 | DhcpServingParams.fromParcelableObject(params), |
| 78 | mLog.forSubComponent(ifName + ".DHCP")); |
| 79 | } catch (DhcpServingParams.InvalidParameterException e) { |
| 80 | mLog.e("Invalid DhcpServingParams", e); |
| 81 | cb.onDhcpServerCreated(STATUS_INVALID_ARGUMENT, null); |
| 82 | return; |
| 83 | } catch (Exception e) { |
| 84 | mLog.e("Unknown error starting DhcpServer", e); |
| 85 | cb.onDhcpServerCreated(STATUS_UNKNOWN_ERROR, null); |
| 86 | return; |
| 87 | } |
| 88 | cb.onDhcpServerCreated(STATUS_SUCCESS, server); |
| 89 | } |
Remi NGUYEN VAN | c094a54 | 2018-12-07 16:52:24 +0900 | [diff] [blame] | 90 | |
| 91 | @Override |
| 92 | protected void dump(@NonNull FileDescriptor fd, @NonNull PrintWriter fout, |
| 93 | @Nullable String[] args) { |
Remi NGUYEN VAN | 0e3d0923 | 2018-12-04 12:13:09 +0900 | [diff] [blame] | 94 | checkNetworkStackCallingPermission(); |
Remi NGUYEN VAN | c094a54 | 2018-12-07 16:52:24 +0900 | [diff] [blame] | 95 | fout.println("NetworkStack logs:"); |
Remi NGUYEN VAN | 0e3d0923 | 2018-12-04 12:13:09 +0900 | [diff] [blame] | 96 | mLog.dump(fd, fout, args); |
Remi NGUYEN VAN | c094a54 | 2018-12-07 16:52:24 +0900 | [diff] [blame] | 97 | } |
| 98 | } |
| 99 | } |