1/*
2 * Copyright (C) 2009 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.dumprendertree.forwarder;
18
19import android.util.Log;
20
21import java.io.DataInputStream;
22import java.io.IOException;
23import java.io.InputStream;
24import java.io.OutputStream;
25import java.net.Socket;
26
27public class AdbUtils {
28
29    private static final String ADB_OK = "OKAY";
30    private static final int ADB_PORT = 5037;
31    private static final String ADB_HOST = "127.0.0.1";
32    private static final int ADB_RESPONSE_SIZE = 4;
33
34    private static final String LOGTAG = "AdbUtils";
35
36    /**
37     *
38     * Convert integer format IP into xxx.xxx.xxx.xxx format
39     *
40     * @param host IP address in integer format
41     * @return human readable format
42     */
43    public static String convert(int host) {
44        return ((host >> 24) & 0xFF) + "."
45        + ((host >> 16) & 0xFF) + "."
46        + ((host >> 8) & 0xFF) + "."
47        + (host & 0xFF);
48    }
49
50    /**
51     *
52     * Resolve DNS name into IP address
53     *
54     * @param host DNS name
55     * @return IP address in integer format
56     * @throws IOException
57     */
58    public static int resolve(String host)  throws IOException {
59        Socket localSocket = new Socket(ADB_HOST, ADB_PORT);
60        DataInputStream dis = new DataInputStream(localSocket.getInputStream());
61        OutputStream os = localSocket.getOutputStream();
62        int count_read = 0;
63
64        if (localSocket == null || dis == null || os == null)
65            return -1;
66        String cmd = "dns:" + host;
67
68        if(!sendAdbCmd(dis, os, cmd))
69            return -1;
70
71        count_read = dis.readInt();
72        localSocket.close();
73        return count_read;
74    }
75
76    /**
77     *
78     * Send an ADB command using existing socket connection
79     *
80     * the streams provided must be from a socket connected to adbd already
81     *
82     * @param is input stream of the socket connection
83     * @param os output stream of the socket
84     * @param cmd the adb command to send
85     * @return if adb gave a success response
86     * @throws IOException
87     */
88    public static boolean sendAdbCmd(InputStream is, OutputStream os,
89            String cmd) throws IOException {
90        byte[] buf = new byte[ADB_RESPONSE_SIZE];
91
92        cmd = String.format("%04X", cmd.length()) + cmd;
93        os.write(cmd.getBytes());
94        int read = is.read(buf);
95        if(read != ADB_RESPONSE_SIZE || !ADB_OK.equals(new String(buf))) {
96            Log.w(LOGTAG, "adb cmd faild.");
97            return false;
98        }
99        return true;
100    }
101
102    /**
103     *
104     * Get a tcp socket connection to specified IP address and port proxied by adb
105     *
106     * The proxying is transparent, e.g. if a socket is returned, then it can be written to and
107     * read from as if it is directly connected to the target
108     *
109     * @param remoteAddress IP address of the host to connect to
110     * @param remotePort port of the host to connect to
111     * @return a valid Socket instance if successful, null otherwise
112     */
113    public static Socket getForwardedSocket(int remoteAddress, int remotePort) {
114        try {
115            Socket socket = new Socket(ADB_HOST, ADB_PORT);
116            String cmd = "tcp:" + remotePort + ":" + convert(remoteAddress);
117            if(!sendAdbCmd(socket.getInputStream(), socket.getOutputStream(), cmd)) {
118                socket.close();
119                return null;
120            }
121            return socket;
122        } catch (IOException ioe) {
123            Log.w(LOGTAG, "error creating adb socket", ioe);
124            return null;
125        }
126    }
127}
128