1 /*
2  * Copyright (C) 2020 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.wifi;
18 
19 import android.net.IpConfiguration;
20 import android.net.LinkAddress;
21 import android.net.ProxyInfo;
22 import android.net.StaticIpConfiguration;
23 import android.util.Log;
24 
25 import java.io.DataOutputStream;
26 import java.io.IOException;
27 import java.net.InetAddress;
28 
29 /**
30  * Copied from frameworks/base/services/core/java/com/android/server/net/IpConfigStore.java
31  */
32 public class IpConfigStoreTestWriter {
33     private static final String TAG = "IpConfigStoreTestWriter";
34 
35     private static final String ID_KEY = "id";
36     private static final String IP_ASSIGNMENT_KEY = "ipAssignment";
37     private static final String LINK_ADDRESS_KEY = "linkAddress";
38     private static final String GATEWAY_KEY = "gateway";
39     private static final String DNS_KEY = "dns";
40     private static final String PROXY_SETTINGS_KEY = "proxySettings";
41     private static final String PROXY_HOST_KEY = "proxyHost";
42     private static final String PROXY_PORT_KEY = "proxyPort";
43     private static final String PROXY_PAC_FILE = "proxyPac";
44     private static final String EXCLUSION_LIST_KEY = "exclusionList";
45     private static final String EOS = "eos";
46 
47     /** Serialize an IpConfiguration object */
writeConfig(DataOutputStream out, String configKey, IpConfiguration config, int version)48     public static boolean writeConfig(DataOutputStream out, String configKey,
49             IpConfiguration config, int version) throws IOException {
50         boolean written = false;
51 
52         try {
53             switch (config.ipAssignment) {
54                 case STATIC:
55                     out.writeUTF(IP_ASSIGNMENT_KEY);
56                     out.writeUTF(config.ipAssignment.toString());
57                     StaticIpConfiguration staticIpConfiguration = config.staticIpConfiguration;
58                     if (staticIpConfiguration != null) {
59                         if (staticIpConfiguration.ipAddress != null) {
60                             LinkAddress ipAddress = staticIpConfiguration.ipAddress;
61                             out.writeUTF(LINK_ADDRESS_KEY);
62                             out.writeUTF(ipAddress.getAddress().getHostAddress());
63                             out.writeInt(ipAddress.getPrefixLength());
64                         }
65                         if (staticIpConfiguration.gateway != null) {
66                             out.writeUTF(GATEWAY_KEY);
67                             out.writeInt(0);  // Default route.
68                             out.writeInt(1);  // Have a gateway.
69                             out.writeUTF(staticIpConfiguration.gateway.getHostAddress());
70                         }
71                         for (InetAddress inetAddr : staticIpConfiguration.dnsServers) {
72                             out.writeUTF(DNS_KEY);
73                             out.writeUTF(inetAddr.getHostAddress());
74                         }
75                     }
76                     written = true;
77                     break;
78                 case DHCP:
79                     out.writeUTF(IP_ASSIGNMENT_KEY);
80                     out.writeUTF(config.ipAssignment.toString());
81                     written = true;
82                     break;
83                 case UNASSIGNED:
84                     /* Ignore */
85                     break;
86                 default:
87                     loge("Ignore invalid ip assignment while writing");
88                     break;
89             }
90 
91             switch (config.proxySettings) {
92                 case STATIC:
93                     ProxyInfo proxyProperties = config.httpProxy;
94                     String exclusionList = proxyProperties.getExclusionListAsString();
95                     out.writeUTF(PROXY_SETTINGS_KEY);
96                     out.writeUTF(config.proxySettings.toString());
97                     out.writeUTF(PROXY_HOST_KEY);
98                     out.writeUTF(proxyProperties.getHost());
99                     out.writeUTF(PROXY_PORT_KEY);
100                     out.writeInt(proxyProperties.getPort());
101                     if (exclusionList != null) {
102                         out.writeUTF(EXCLUSION_LIST_KEY);
103                         out.writeUTF(exclusionList);
104                     }
105                     written = true;
106                     break;
107                 case PAC:
108                     ProxyInfo proxyPacProperties = config.httpProxy;
109                     out.writeUTF(PROXY_SETTINGS_KEY);
110                     out.writeUTF(config.proxySettings.toString());
111                     out.writeUTF(PROXY_PAC_FILE);
112                     out.writeUTF(proxyPacProperties.getPacFileUrl().toString());
113                     written = true;
114                     break;
115                 case NONE:
116                     out.writeUTF(PROXY_SETTINGS_KEY);
117                     out.writeUTF(config.proxySettings.toString());
118                     written = true;
119                     break;
120                 case UNASSIGNED:
121                     /* Ignore */
122                     break;
123                 default:
124                     loge("Ignore invalid proxy settings while writing");
125                     break;
126             }
127 
128             if (written) {
129                 out.writeUTF(ID_KEY);
130                 if (version < 3) {
131                     out.writeInt(Integer.valueOf(configKey));
132                 } else {
133                     out.writeUTF(configKey);
134                 }
135             }
136         } catch (NullPointerException e) {
137             loge("Failure in writing " + config + e);
138         }
139         out.writeUTF(EOS);
140 
141         return written;
142     }
143 
loge(String s)144     private static void loge(String s) {
145         Log.e(TAG, s);
146     }
147 }
148