blob: 7fea1e038ceeea91fc786b41d22a2b8983f2d74a [file] [log] [blame]
Remi NGUYEN VANc094a542018-12-07 16:52:24 +09001/*
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
17package com.android.server;
18
Remi NGUYEN VAN0e3d09232018-12-04 12:13:09 +090019import static android.net.dhcp.IDhcpServer.STATUS_INVALID_ARGUMENT;
20import static android.net.dhcp.IDhcpServer.STATUS_SUCCESS;
21import static android.net.dhcp.IDhcpServer.STATUS_UNKNOWN_ERROR;
22
23import static com.android.server.util.PermissionUtil.checkNetworkStackCallingPermission;
Remi NGUYEN VANc094a542018-12-07 16:52:24 +090024
25import android.annotation.NonNull;
26import android.annotation.Nullable;
27import android.app.Service;
28import android.content.Intent;
29import android.net.INetworkStackConnector;
Remi NGUYEN VAN0e3d09232018-12-04 12:13:09 +090030import android.net.dhcp.DhcpServer;
31import android.net.dhcp.DhcpServingParams;
32import android.net.dhcp.DhcpServingParamsParcel;
33import android.net.dhcp.IDhcpServerCallbacks;
34import android.net.util.SharedLog;
Remi NGUYEN VANc094a542018-12-07 16:52:24 +090035import android.os.IBinder;
Remi NGUYEN VAN0e3d09232018-12-04 12:13:09 +090036import android.os.RemoteException;
Remi NGUYEN VANc094a542018-12-07 16:52:24 +090037
38import java.io.FileDescriptor;
39import 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 */
46public 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 VAN0e3d09232018-12-04 12:13:09 +090066 @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 VANc094a542018-12-07 16:52:24 +090090
91 @Override
92 protected void dump(@NonNull FileDescriptor fd, @NonNull PrintWriter fout,
93 @Nullable String[] args) {
Remi NGUYEN VAN0e3d09232018-12-04 12:13:09 +090094 checkNetworkStackCallingPermission();
Remi NGUYEN VANc094a542018-12-07 16:52:24 +090095 fout.println("NetworkStack logs:");
Remi NGUYEN VAN0e3d09232018-12-04 12:13:09 +090096 mLog.dump(fd, fout, args);
Remi NGUYEN VANc094a542018-12-07 16:52:24 +090097 }
98 }
99}