File: tcp_client.cpp

package info (click to toggle)
android-platform-tools 34.0.5-12
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 150,900 kB
  • sloc: cpp: 805,786; java: 293,500; ansic: 128,288; xml: 127,491; python: 41,481; sh: 14,245; javascript: 9,665; cs: 3,846; asm: 2,049; makefile: 1,917; yacc: 440; awk: 368; ruby: 183; sql: 140; perl: 88; lex: 67
file content (176 lines) | stat: -rw-r--r-- 5,480 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
 * Copyright (C) 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "tcp_client.h"
#include "constants.h"

#include <android-base/errors.h>
#include <android-base/logging.h>
#include <android-base/parseint.h>
#include <android-base/properties.h>
#include <android-base/stringprintf.h>
#include <android-base/strings.h>

static constexpr int kDefaultPort = 5554;
static constexpr int kProtocolVersion = 1;
static constexpr int kHandshakeTimeoutMs = 2000;
static constexpr size_t kHandshakeLength = 4;

// Extract the big-endian 8-byte message length into a 64-bit number.
static uint64_t ExtractMessageLength(const void* buffer) {
    uint64_t ret = 0;
    for (int i = 0; i < 8; ++i) {
        ret |= uint64_t{reinterpret_cast<const uint8_t*>(buffer)[i]} << (56 - i * 8);
    }
    return ret;
}

// Encode the 64-bit number into a big-endian 8-byte message length.
static void EncodeMessageLength(uint64_t length, void* buffer) {
    for (int i = 0; i < 8; ++i) {
        reinterpret_cast<uint8_t*>(buffer)[i] = length >> (56 - i * 8);
    }
}

ClientTcpTransport::ClientTcpTransport() {
    service_ = Socket::NewServer(Socket::Protocol::kTcp, kDefaultPort);

    // A workaround to notify recovery to continue its work.
    android::base::SetProperty("sys.usb.ffs.ready", "1");
}

ssize_t ClientTcpTransport::Read(void* data, size_t len) {
    if (len > SSIZE_MAX) {
        return -1;
    }

    size_t total_read = 0;
    do {
        // Read a new message
        while (message_bytes_left_ == 0) {
            if (socket_ == nullptr) {
                ListenFastbootSocket();
            }

            char buffer[8];
            if (socket_->ReceiveAll(buffer, 8, 0) == 8) {
                message_bytes_left_ = ExtractMessageLength(buffer);
            } else {
                // If connection is closed by host, Receive will return 0 immediately.
                socket_.reset(nullptr);
                // In DATA phase, return error.
                if (downloading_) {
                    return -1;
                }
            }
        }

        size_t read_length = len - total_read;
        if (read_length > message_bytes_left_) {
            read_length = message_bytes_left_;
        }
        ssize_t bytes_read =
                socket_->ReceiveAll(reinterpret_cast<char*>(data) + total_read, read_length, 0);
        if (bytes_read == -1) {
            socket_.reset(nullptr);
            return -1;
        } else {
            message_bytes_left_ -= bytes_read;
            total_read += bytes_read;
        }
    // There are more than one DATA phases if the downloading buffer is too
    // large, like a very big system image. All of data phases should be
    // received until the whole buffer is filled in that case.
    } while (downloading_ && total_read < len);

    return total_read;
}

ssize_t ClientTcpTransport::Write(const void* data, size_t len) {
    if (socket_ == nullptr || len > SSIZE_MAX) {
        return -1;
    }

    // Use multi-buffer writes for better performance.
    char header[8];
    EncodeMessageLength(len, header);

    if (!socket_->Send(std::vector<cutils_socket_buffer_t>{{header, 8}, {data, len}})) {
        socket_.reset(nullptr);
        return -1;
    }

    // In DATA phase
    if (android::base::StartsWith(reinterpret_cast<const char*>(data), RESPONSE_DATA)) {
        downloading_ = true;
    } else {
        downloading_ = false;
    }

    return len;
}

int ClientTcpTransport::Close() {
    if (socket_ == nullptr) {
        return -1;
    }
    socket_.reset(nullptr);

    return 0;
}

int ClientTcpTransport::Reset() {
    return Close();
}

void ClientTcpTransport::ListenFastbootSocket() {
    while (true) {
        socket_ = service_->Accept();

        // Handshake
        char buffer[kHandshakeLength + 1];
        buffer[kHandshakeLength] = '\0';
        if (socket_->ReceiveAll(buffer, kHandshakeLength, kHandshakeTimeoutMs) !=
            kHandshakeLength) {
            PLOG(ERROR) << "No Handshake message received";
            socket_.reset(nullptr);
            continue;
        }

        if (memcmp(buffer, "FB", 2) != 0) {
            PLOG(ERROR) << "Unrecognized initialization message";
            socket_.reset(nullptr);
            continue;
        }

        int version = 0;
        if (!android::base::ParseInt(buffer + 2, &version) || version < kProtocolVersion) {
            LOG(ERROR) << "Unknown TCP protocol version " << buffer + 2
                       << ", our version: " << kProtocolVersion;
            socket_.reset(nullptr);
            continue;
        }

        std::string handshake_message(android::base::StringPrintf("FB%02d", kProtocolVersion));
        if (!socket_->Send(handshake_message.c_str(), kHandshakeLength)) {
            PLOG(ERROR) << "Failed to send initialization message";
            socket_.reset(nullptr);
            continue;
        }

        break;
    }
}